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

@@ -6,6 +6,7 @@
*/
import Database from 'better-sqlite3';
import { logSlowQuery } from '../provider.js';
import { SCHEMA_SCRIPTS } from './schema.js';
function normalizeParamValue(value) {
@@ -236,24 +237,29 @@ export class BetterSqlite3Executor {
/** @returns {Promise<import('../provider.js').QueryResult>} */
async query(sql, params = {}) {
const statement = this.db.prepare(sql);
const bindings = bindParams(params, sql);
const start = performance.now();
try {
const statement = this.db.prepare(sql);
const bindings = bindParams(params, sql);
if (statement.reader) {
const rows = statement.all(bindings);
if (statement.reader) {
const rows = statement.all(bindings);
return {
rows,
rowsAffected: rows.length,
insertId: normalizeInsertId(rows[0]?.id),
};
}
const result = statement.run(bindings);
return {
rows,
rowsAffected: rows.length,
insertId: normalizeInsertId(rows[0]?.id),
rows: [],
rowsAffected: result.changes,
insertId: normalizeInsertId(result.lastInsertRowid),
};
} finally {
logSlowQuery(sql, performance.now() - start, params);
}
const result = statement.run(bindings);
return {
rows: [],
rowsAffected: result.changes,
insertId: normalizeInsertId(result.lastInsertRowid),
};
}
}