Skip to content

Commit

Permalink
src: return undefined if no rows are returned in SQLite
Browse files Browse the repository at this point in the history
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: #53981
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
  • Loading branch information
deokjinkim authored and RafaelGSS committed Aug 5, 2024
1 parent aa0ac3b commit 3fdcf7a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@ void StatementSync::Get(const FunctionCallbackInfo<Value>& 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;
}
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down

0 comments on commit 3fdcf7a

Please sign in to comment.