This commit is contained in:
seb
2026-10-13 12:19:35 +02:00
parent 2ef2c0edbf
commit a5886b7c92
4 changed files with 85 additions and 42 deletions

View File

@@ -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 ?? {},
};
}
}