From 4c413428107f990ccf1b3bb4833ab7a8bd68eebe Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Mon, 22 Jul 2024 01:17:21 +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`. --- 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 1202d2c8cf2464..93f82c04403ec2 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -471,7 +471,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 99c8b7ee72a5c4..5fc7e569363f65 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); });