Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2698,8 +2698,13 @@ MaybeLocal<Object> StatementExecutionHelper::Run(Environment* env,
int r = sqlite3_reset(stmt);
CHECK_ERROR_OR_THROW(isolate, db, r, SQLITE_OK, MaybeLocal<Object>());

sqlite3_int64 last_insert_rowid = sqlite3_last_insert_rowid(db->Connection());
sqlite3_int64 changes = sqlite3_changes64(db->Connection());
// Read-only statements (e.g. SELECT) never modify rows, so report 0
// instead of leaking the count from the most recent write statement.
bool is_readonly = sqlite3_stmt_readonly(stmt);
sqlite3_int64 last_insert_rowid =
is_readonly ? 0 : sqlite3_last_insert_rowid(db->Connection());
sqlite3_int64 changes =
is_readonly ? 0 : sqlite3_changes64(db->Connection());
Local<Value> last_insert_rowid_val;
Local<Value> changes_val;

Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-sqlite-statement-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@
`);
t.assert.strictEqual(setup, undefined);
const stmt = db.prepare('SELECT * FROM storage');
t.assert.deepStrictEqual(stmt.run(), { changes: 1, lastInsertRowid: 1 });

Check failure on line 241 in test/parallel/test-sqlite-statement-sync.js

View workflow job for this annotation

GitHub Actions / test-linux (ubuntu-24.04-arm)

--- stdout --- Test failure: 'executes a query and returns change metadata' Location: test/parallel/test-sqlite-statement-sync.js:232:3 AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected { + changes: 0, + lastInsertRowid: 0 - changes: 1, - lastInsertRowid: 1 } at assert.<computed> [as deepStrictEqual] (node:internal/test_runner/test:324:18) at TestContext.<anonymous> (/home/runner/work/node/node/node/test/parallel/test-sqlite-statement-sync.js:241:14) at Test.runInAsyncScope (node:async_hooks:226:14) at Test.run (node:internal/test_runner/test:1201:25) at Test.start (node:internal/test_runner/test:1096:17) at node:internal/test_runner/test:1617:71 at node:internal/per_context/primordials:466:82 at new Promise (<anonymous>) at new SafePromise (node:internal/per_context/primordials:435:3) at node:internal/per_context/primordials:466:9 { generatedMessage: true, code: 'ERR_ASSERTION', actual: [Object], expected: [Object], operator: 'deepStrictEqual', diff: 'simple' } Command: out/Release/node --expose-gc --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/node/node/node/test/parallel/test-sqlite-statement-sync.js

Check failure on line 241 in test/parallel/test-sqlite-statement-sync.js

View workflow job for this annotation

GitHub Actions / test-linux (ubuntu-24.04)

--- stdout --- Test failure: 'executes a query and returns change metadata' Location: test/parallel/test-sqlite-statement-sync.js:232:3 AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected { + changes: 0, + lastInsertRowid: 0 - changes: 1, - lastInsertRowid: 1 } at assert.<computed> [as deepStrictEqual] (node:internal/test_runner/test:324:18) at TestContext.<anonymous> (/home/runner/work/node/node/node/test/parallel/test-sqlite-statement-sync.js:241:14) at Test.runInAsyncScope (node:async_hooks:226:14) at Test.run (node:internal/test_runner/test:1201:25) at Test.start (node:internal/test_runner/test:1096:17) at node:internal/test_runner/test:1617:71 at node:internal/per_context/primordials:466:82 at new Promise (<anonymous>) at new SafePromise (node:internal/per_context/primordials:435:3) at node:internal/per_context/primordials:466:9 { generatedMessage: true, code: 'ERR_ASSERTION', actual: [Object], expected: [Object], operator: 'deepStrictEqual', diff: 'simple' } Command: out/Release/node --expose-gc --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/node/node/node/test/parallel/test-sqlite-statement-sync.js

Check failure on line 241 in test/parallel/test-sqlite-statement-sync.js

View workflow job for this annotation

GitHub Actions / test-macOS

--- stdout --- Test failure: 'executes a query and returns change metadata' Location: test/parallel/test-sqlite-statement-sync.js:232:3 AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected { + changes: 0, + lastInsertRowid: 0 - changes: 1, - lastInsertRowid: 1 } at assert.<computed> [as deepStrictEqual] (node:internal/test_runner/test:324:18) at TestContext.<anonymous> (/Users/runner/work/node/node/node/test/parallel/test-sqlite-statement-sync.js:241:14) at Test.runInAsyncScope (node:async_hooks:226:14) at Test.run (node:internal/test_runner/test:1201:25) at Test.start (node:internal/test_runner/test:1096:17) at node:internal/test_runner/test:1617:71 at node:internal/per_context/primordials:466:82 at new Promise (<anonymous>) at new SafePromise (node:internal/per_context/primordials:435:3) at node:internal/per_context/primordials:466:9 { generatedMessage: true, code: 'ERR_ASSERTION', actual: [Object], expected: [Object], operator: 'deepStrictEqual', diff: 'simple' } Command: out/Release/node --expose-gc --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /Users/runner/work/node/node/node/test/parallel/test-sqlite-statement-sync.js

Check failure on line 241 in test/parallel/test-sqlite-statement-sync.js

View workflow job for this annotation

GitHub Actions / x86_64-linux: with shared libraries

--- stdout --- Test failure: 'executes a query and returns change metadata' Location: test/parallel/test-sqlite-statement-sync.js:232:3 AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected { + changes: 0, + lastInsertRowid: 0 - changes: 1, - lastInsertRowid: 1 } at assert.<computed> [as deepStrictEqual] (node:internal/test_runner/test:324:18) at TestContext.<anonymous> (/home/runner/work/_temp/node-v26.0.0-nightly2026-03-064071f7abc2-slim/test/parallel/test-sqlite-statement-sync.js:241:14) at Test.runInAsyncScope (node:async_hooks:226:14) at Test.run (node:internal/test_runner/test:1201:25) at Test.start (node:internal/test_runner/test:1096:17) at node:internal/test_runner/test:1617:71 at node:internal/per_context/primordials:466:82 at new Promise (<anonymous>) at new SafePromise (node:internal/per_context/primordials:435:3) at node:internal/per_context/primordials:466:9 { generatedMessage: true, code: 'ERR_ASSERTION', actual: [Object], expected: [Object], operator: 'deepStrictEqual', diff: 'simple' } Command: out/Release/node --expose-gc --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/_temp/node-v26.0.0-nightly2026-03-064071f7abc2-slim/test/parallel/test-sqlite-statement-sync.js

Check failure on line 241 in test/parallel/test-sqlite-statement-sync.js

View workflow job for this annotation

GitHub Actions / aarch64-linux: with shared libraries

--- stdout --- Test failure: 'executes a query and returns change metadata' Location: test/parallel/test-sqlite-statement-sync.js:232:3 AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: + actual - expected { + changes: 0, + lastInsertRowid: 0 - changes: 1, - lastInsertRowid: 1 } at assert.<computed> [as deepStrictEqual] (node:internal/test_runner/test:324:18) at TestContext.<anonymous> (/home/runner/work/_temp/node-v26.0.0-nightly2026-03-064071f7abc2-slim/test/parallel/test-sqlite-statement-sync.js:241:14) at Test.runInAsyncScope (node:async_hooks:226:14) at Test.run (node:internal/test_runner/test:1201:25) at Test.start (node:internal/test_runner/test:1096:17) at node:internal/test_runner/test:1617:71 at node:internal/per_context/primordials:466:82 at new Promise (<anonymous>) at new SafePromise (node:internal/per_context/primordials:435:3) at node:internal/per_context/primordials:466:9 { generatedMessage: true, code: 'ERR_ASSERTION', actual: [Object], expected: [Object], operator: 'deepStrictEqual', diff: 'simple' } Command: out/Release/node --expose-gc --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/_temp/node-v26.0.0-nightly2026-03-064071f7abc2-slim/test/parallel/test-sqlite-statement-sync.js
});

test('SQLite throws when trying to bind too many parameters', (t) => {
Expand Down Expand Up @@ -909,3 +909,36 @@
);
});
});

suite('read-only statements report zero changes', () => {
test('SELECT after INSERT reports zero changes', (t) => {
const db = new DatabaseSync(':memory:');
t.after(() => { db.close(); });
db.exec('CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)');
const insert = db.prepare('INSERT INTO test (name) VALUES (?)');
t.assert.deepStrictEqual(
insert.run('foo'),
{ changes: 1, lastInsertRowid: 1 },
);
const select = db.prepare('SELECT * FROM test');
t.assert.deepStrictEqual(
select.run(),
{ changes: 0, lastInsertRowid: 0 },
);
});

test('SELECT after multiple INSERTs reports zero changes', (t) => {
const db = new DatabaseSync(':memory:');
t.after(() => { db.close(); });
db.exec('CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)');
const insert = db.prepare('INSERT INTO test (name) VALUES (?)');
insert.run('a');
insert.run('b');
insert.run('c');
const select = db.prepare('SELECT * FROM test');
t.assert.deepStrictEqual(
select.run(),
{ changes: 0, lastInsertRowid: 0 },
);
});
});
Loading