Files
node/test/parallel/test-sqlite-statement-binding-reentry.js
T
Guilherme Araújo f2b698f1cf sqlite: rename DatabaseSync and StatementSync
Rename the DatabaseSync and StatementSync classes to Database and
Statement, and the internal DatabaseSyncLimits helper to
DatabaseLimits. The old names are kept as aliases of the new classes
and are Documentation-only deprecated (DEP0210, DEP0211).

Assisted-by: Claude Code
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65988
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2026-09-26 00:03:45 +00:00

64 lines
1.9 KiB
JavaScript

'use strict';
const { skipIfSQLiteMissing, mustCall } = require('../common');
skipIfSQLiteMissing();
const assert = require('node:assert');
const { test } = require('node:test');
const { Database } = require('node:sqlite');
const reentryError = {
code: 'ERR_INVALID_STATE',
message: 'statement is already being executed',
};
// Binding a named parameter reads properties off the supplied object, so a
// getter runs JavaScript after the statement has been reset but before it is
// stepped. Reentering the same statement there resets it a second time and
// hands out a second iterator over one virtual machine.
for (const method of ['all', 'get', 'run', 'iterate']) {
test(`${method}() reentry during parameter binding is rejected`, () => {
const db = new Database(':memory:');
db.exec(`
CREATE TABLE data (value INTEGER);
INSERT INTO data VALUES (1), (2), (3);
`);
let statement;
const invoke = (params) => (method === 'iterate' ?
[...statement.iterate(params)] :
statement[method](params));
const reenter = mustCall(() => {
assert.throws(() => invoke({ $min: 2 }), reentryError);
return 1;
});
const params = { get $min() { return reenter(); } };
statement = db.prepare('SELECT value FROM data WHERE value >= $min');
invoke(params);
});
}
test('two iterators cannot share one virtual machine', () => {
const db = new Database(':memory:');
db.exec(`
CREATE TABLE data (value INTEGER);
INSERT INTO data VALUES (1), (2), (3);
`);
const statement = db.prepare('SELECT value FROM data WHERE value >= $min');
let inner;
const reenter = mustCall(() => {
assert.throws(() => {
inner = statement.iterate({ $min: 1 });
}, reentryError);
return 1;
});
const params = { get $min() { return reenter(); } };
assert.deepStrictEqual(
[...statement.iterate(params)].map((row) => row.value),
[1, 2, 3],
);
assert.strictEqual(inner, undefined);
});