From 3fdcf7a47d28c21dc72220d81dff946638ad27ed Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Wed, 24 Jul 2024 01:34:59 +0900 Subject: [PATCH] src: return `undefined` if no rows are returned in SQLite For now, { key: null, value: null} is returned even though no rows are returned from database when `statement.get()` is called. So return empty value if return value of `sqlite3_step` is `SQLITE_DONE`. PR-URL: https://github.com/nodejs/node/pull/53981 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Yagiz Nizipli --- src/node_sqlite.cc | 3 ++- test/parallel/test-sqlite.js | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index fbef4e3b0d71c8..1d94363bf08907 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -480,7 +480,8 @@ void StatementSync::Get(const FunctionCallbackInfo& args) { auto reset = OnScopeLeave([&]() { sqlite3_reset(stmt->statement_); }); r = sqlite3_step(stmt->statement_); - if (r != SQLITE_ROW && r != SQLITE_DONE) { + if (r == SQLITE_DONE) return; + if (r != SQLITE_ROW) { THROW_ERR_SQLITE_ERROR(env->isolate(), stmt->db_); return; } diff --git a/test/parallel/test-sqlite.js b/test/parallel/test-sqlite.js index d07c3ac01b9a23..3d899063f9c967 100644 --- a/test/parallel/test-sqlite.js +++ b/test/parallel/test-sqlite.js @@ -219,7 +219,9 @@ suite('StatementSync() constructor', () => { suite('StatementSync.prototype.get()', () => { test('executes a query and returns undefined on no results', (t) => { const db = new DatabaseSync(nextDb()); - const stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)'); + let stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)'); + t.assert.strictEqual(stmt.get(), undefined); + stmt = db.prepare('SELECT * FROM storage'); t.assert.strictEqual(stmt.get(), undefined); });