u
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
import sql from 'mssql';
|
||||
import { logSlowQuery } from '../provider.js';
|
||||
|
||||
function normalizeRows(result) {
|
||||
const recordset = result?.recordset ?? [];
|
||||
@@ -24,11 +25,16 @@ export class MssqlExecutor {
|
||||
|
||||
/** @returns {Promise<import('../provider.js').QueryResult>} */
|
||||
async query(sql, params = {}) {
|
||||
const req = this.#requestFactory();
|
||||
for (const [key, value] of Object.entries(params)) {
|
||||
req.input(key, value);
|
||||
const start = performance.now();
|
||||
try {
|
||||
const req = this.#requestFactory();
|
||||
for (const [key, value] of Object.entries(params)) {
|
||||
req.input(key, value);
|
||||
}
|
||||
return normalizeRows(await req.query(sql));
|
||||
} finally {
|
||||
logSlowQuery(sql, performance.now() - start, params);
|
||||
}
|
||||
return normalizeRows(await req.query(sql));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,22 +43,27 @@ export class MssqlExecutor {
|
||||
* (`getnextnumberwithprefixandpadding`).
|
||||
*/
|
||||
async exec(procName, { inputs = {}, outputs = [] } = {}) {
|
||||
const req = this.#requestFactory();
|
||||
for (const [key, value] of Object.entries(inputs)) {
|
||||
req.input(key, value);
|
||||
const start = performance.now();
|
||||
try {
|
||||
const req = this.#requestFactory();
|
||||
for (const [key, value] of Object.entries(inputs)) {
|
||||
req.input(key, value);
|
||||
}
|
||||
for (const out of outputs) {
|
||||
req.output(out);
|
||||
}
|
||||
const result = await req.execute(procName);
|
||||
return {
|
||||
rows: result.recordset ?? [],
|
||||
rowsAffected: Array.isArray(result.rowsAffected)
|
||||
? result.rowsAffected.reduce((a, b) => a + b, 0)
|
||||
: 0,
|
||||
insertId: null,
|
||||
outputs: result.output ?? {},
|
||||
};
|
||||
} finally {
|
||||
logSlowQuery(`EXEC ${procName}`, performance.now() - start, inputs);
|
||||
}
|
||||
for (const out of outputs) {
|
||||
req.output(out);
|
||||
}
|
||||
const result = await req.execute(procName);
|
||||
return {
|
||||
rows: result.recordset ?? [],
|
||||
rowsAffected: Array.isArray(result.rowsAffected)
|
||||
? result.rowsAffected.reduce((a, b) => a + b, 0)
|
||||
: 0,
|
||||
insertId: null,
|
||||
outputs: result.output ?? {},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user