-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb-queries.ts
264 lines (221 loc) · 9.07 KB
/
db-queries.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import * as sql from "mssql";
export interface DBConfig {
user: string;
password: string;
server: string;
database: string;
encrypt: boolean;
trustServerCertificate: boolean;
}
export interface SiteItem {
siteID: number;
displayText: string;
}
// Internal utility function handling ConnectionPool and query execution
async function withDatabase<T>(config: DBConfig, callback: (pool: sql.ConnectionPool) => Promise<T>): Promise<T> {
const pool = new sql.ConnectionPool({
user: config.user,
password: config.password,
server: config.server,
database: config.database,
options: {
encrypt: config.encrypt,
trustServerCertificate: config.trustServerCertificate,
},
});
const poolConnect = pool.connect();
pool.on("error", (err) => {
console.error("SQL Pool Error: ", err);
throw new Error(`SQL Pool Error: ${err.message}`);
});
try {
await poolConnect; // Ensure pool connection is established
const result = await callback(pool); // Execute query logic using the pool
return result;
} catch (err) {
console.error("SQL Error", err);
throw new Error(`SQL Error: ${err.message}`);
} finally {
await pool.close(); // Ensure the connection pool is closed
}
}
// ExecuteReader: For returning rows
export async function executeReader(config: DBConfig, query: string, params: { [key: string]: any } = {}): Promise<sql.IResult<any>> {
return withDatabase(config, async (pool) => {
const request = pool.request();
for (const key in params) {
request.input(key, params[key]);
}
return await request.query(query);
});
}
// ExecuteScalar: For returning a single value
export async function executeScalar<T>(config: DBConfig, query: string, params: { [key: string]: any } = {}): Promise<T> {
const result = await executeReader(config, query, params);
return result.recordset[0]?.[Object.keys(result.recordset[0])[0]] as T;
}
// ExecuteNonQuery: For inserts/updates/deletes
export async function executeNonQuery(config: DBConfig, query: string, params: { [key: string]: any } = {}): Promise<number> {
return withDatabase(config, async (pool) => {
const request = pool.request();
for (const key in params) {
request.input(key, params[key]);
}
const result = await request.query(query);
return result.rowsAffected[0]; // Returns the number of affected rows
});
}
export async function queryPIs(config: DBConfig): Promise<string[]> {
return withDatabase(config, async (pool) => {
const result = await pool.request()
.query('select distinct PI from dbo.Animals where PI is not NULL and DataDeleted = 0 order by PI asc;');
console.log('Query Results (PIs):', result.recordset);
return result.recordset.map((row) => row.PI as string); // Return the query result
});
}
export async function queryAnimals(config: DBConfig, pi: string): Promise<string[]> {
return withDatabase(config, async (pool) => {
const result = await pool.request()
.input("pi", sql.VarChar, pi)
.query("select AnimalID from dbo.Animals where PI is not NULL and PI = @pi and DataDeleted = 0 order by AnimalID asc;");
console.log('Query Results (Animals):', result.recordset);
return result.recordset.map((row) => row.AnimalID as string); // Return the query result
});
}
export async function existsAnimal(config: DBConfig, animalID: string): Promise<boolean> {
return withDatabase(config, async (pool) => {
const result = await pool.request()
.input("animalID", sql.VarChar, animalID)
.query("select count(*) as Cnt from dbo.Animals where AnimalID = @animalID and DataDeleted = 0;");
const count = result.recordset[0]?.Cnt;
console.log('Exists Result (Animal):', count > 0);
return count > 0;
});
}
export async function querySites(config: DBConfig, animalID: string): Promise<SiteItem[]> {
return withDatabase(config, async (pool) => {
const result = await pool.request()
.input("animalID", sql.Int, animalID)
.query(`
select s.SiteID, (cast(s.SiteID as varchar(10)) + ':' + (case when st.Comment is not NULL then left(st.Comment, 50) else '' end)) as Display from dbo.Sites s
inner join Experiments e on s.SiteID = e.SiteID
inner join Stacks st on e.ExpID = st.ExpID
where s.AnimalID = @animalID and e.ExpID = st.StackID and s.DataDeleted = 0
order by s.SiteID, st.Comment asc;
`);
console.log('Query Results (Sites):', result.recordset);
// Return the query result
return result.recordset.map((row) => ({
siteID: row.SiteID,
displayText: row.Display,
}));
});
}
export async function queryMissingSites(config: DBConfig, siteIDs: number[]): Promise<number[]> {
if (!siteIDs || siteIDs.length === 0) {
throw new Error("Parameter siteIDs is empty.");
}
return withDatabase(config, async (pool) => {
const siteIDsJoined = siteIDs.join(", ");
const result = await pool.request()
.query(`
select s.SiteID from dbo.Sites s
where s.SiteID in (${siteIDsJoined})
order by s.SiteID asc;
`);
const existingSiteIDs = result.recordset.map((row) => row.SiteID as number);
// No matching siteIDs
if (!existingSiteIDs || existingSiteIDs.length === 0) {
return siteIDs;
}
// Check all provided siteIDs against the result
let missingSiteIDs: number[] = [];
siteIDs.forEach(siteID => {
if (!existingSiteIDs.contains(siteID)) {
missingSiteIDs.push(siteID);
}
});
return missingSiteIDs;
});
}
export async function queryInvalidStacksForAnimal(config: DBConfig, animalID: string, stackIDs: number[]): Promise<number[]> {
if (!stackIDs || stackIDs.length === 0) {
throw new Error("Parameter stackIDs is empty.");
}
return withDatabase(config, async (pool) => {
const stackIDsJoined = stackIDs.join(", ");
const result = await pool.request()
.input("animalID", sql.VarChar, animalID)
.query(`
select distinct st.StackID from dbo.Stacks st inner join dbo.Experiments e on st.ExpID = e.ExpID
inner join dbo.Sites si on e.SiteID = si.SiteID
where st.StackID in (${stackIDsJoined}) and si.AnimalID <> @animalID
order by st.StackID asc;
`);
const wrongStackIDs = result.recordset.map((row) => row.StackID as number);
return wrongStackIDs;
});
}
export async function queryInvalidExperimentsForAnimal(config: DBConfig, animalID: string, expIDs: number[]): Promise<number[]> {
if (!expIDs || expIDs.length === 0) {
throw new Error("Parameter expIDs is empty.");
}
return withDatabase(config, async (pool) => {
const expIDsJoined = expIDs.join(", ");
const result = await pool.request()
.input("animalID", sql.VarChar, animalID)
.query(`
select distinct e.ExpID from dbo.Experiments e
inner join dbo.Sites si on e.SiteID = si.SiteID
where e.ExpID in (${expIDsJoined}) and si.AnimalID <> @animalID
order by e.ExpID asc;
`);
const wrongExpIDs = result.recordset.map((row) => row.ExpID as number);
return wrongExpIDs;
});
}
export async function queryInvalidSitesForAnimal(config: DBConfig, animalID: string, siteIDs: number[]): Promise<number[]> {
if (!siteIDs || siteIDs.length === 0) {
throw new Error("Parameter siteIDs is empty.");
}
return withDatabase(config, async (pool) => {
const siteIDsJoined = siteIDs.join(", ");
const result = await pool.request()
.input("animalID", sql.VarChar, animalID)
.query(`
SELECT DISTINCT SiteID FROM dbo.Sites
WHERE SiteID IN (${siteIDsJoined}) AND AnimalID <> @animalID
ORDER BY SiteID ASC;
`);
const wrongSiteIDs = result.recordset.map((row) => row.SiteID as number);
return wrongSiteIDs;
});
}
export async function queryProjects(config: DBConfig): Promise<string[]> {
return withDatabase(config, async (pool) => {
const result = await pool.request()
.query(`
SELECT DISTINCT Project FROM dbo.Sites
WHERE DataDeleted = 0
ORDER BY Project ASC;
`);
const projects = result.recordset.map((row) => row.Project as string);
return projects;
});
}
export async function queryLocations(config: DBConfig): Promise<string[]> {
return withDatabase(config, async (pool) => {
const result = await pool.request()
.query(`
SELECT DISTINCT Location FROM dbo.Sites
WHERE DataDeleted = 0
ORDER BY Location ASC;
`);
const locations = result.recordset.map((row) => row.Location as string);
return locations;
});
}
export async function addNewSite(config: DBConfig, siteID: number, animalID: string, project: string, location: string, depth: number | null): Promise<number> {
const sql = "INSERT INTO dbo.Sites (SiteID, AnimalID, Project, Location, Depth) VALUES (@SiteID, @AnimalID, @Project, @Location, @Depth);";
return await executeNonQuery(config, sql, { SiteID: siteID, AnimalID: animalID, Project: project, Location: location, Depth: depth });
}