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 <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This commit is contained in:
Deokjin Kim
2024-07-24 01:34:59 +09:00
committed by GitHub
parent 0ed9a43242
commit db594d042b
2 changed files with 5 additions and 2 deletions

View File

@@ -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;
}

View File

@@ -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);
});