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
6 changes: 5 additions & 1 deletion src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2865,12 +2865,16 @@ MaybeLocal<Value> StatementExecutionHelper::All(Environment* env,
Isolate* isolate = env->isolate();
EscapableHandleScope scope(isolate);
int r;
int num_cols = sqlite3_column_count(stmt);
int num_cols = 0;
LocalVector<Value> rows(isolate);
LocalVector<Value> row_values(isolate);
LocalVector<Name> row_keys(isolate);

while ((r = sqlite3_step(stmt)) == SQLITE_ROW) {
if (num_cols == 0) {
num_cols = sqlite3_column_count(stmt);
}

if (ExtractRowValues(env, stmt, num_cols, use_big_ints, &row_values)
.IsNothing()) {
return MaybeLocal<Value>();
Expand Down
69 changes: 69 additions & 0 deletions test/parallel/test-sqlite-statement-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ suite('StatementSync.prototype.get()', () => {
const stmt = db.prepare('SELECT 1 as __proto__, 2 as constructor, 3 as toString');
t.assert.deepStrictEqual(stmt.get(), { __proto__: null, ['__proto__']: 1, constructor: 2, toString: 3 });
});

test('reflects an added column after the schema changes', (t) => {
using db = new DatabaseSync(':memory:');
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
db.prepare('INSERT INTO storage (key, val) VALUES (?, ?)').run('key1', 'val1');
const stmt = db.prepare('SELECT * FROM storage ORDER BY key');
db.exec("ALTER TABLE storage ADD COLUMN extra TEXT DEFAULT 'def'");
t.assert.deepStrictEqual(stmt.get(), {
__proto__: null, key: 'key1', val: 'val1', extra: 'def',
});
});

test('reflects a dropped column after the schema changes', (t) => {
using db = new DatabaseSync(':memory:');
db.exec('CREATE TABLE storage(key TEXT, val TEXT, extra TEXT)');
db.prepare('INSERT INTO storage (key, val, extra) VALUES (?, ?, ?)')
.run('key1', 'val1', 'x');
const stmt = db.prepare('SELECT * FROM storage ORDER BY key');
db.exec('ALTER TABLE storage DROP COLUMN extra');
t.assert.deepStrictEqual(stmt.get(), {
__proto__: null, key: 'key1', val: 'val1',
});
});
});

suite('StatementSync.prototype.all()', () => {
Expand Down Expand Up @@ -83,6 +106,29 @@ suite('StatementSync.prototype.all()', () => {
{ __proto__: null, key: 'key2', val: 'val2' },
]);
});

test('reflects an added column after the schema changes', (t) => {
using db = new DatabaseSync(':memory:');
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
db.prepare('INSERT INTO storage (key, val) VALUES (?, ?)').run('key1', 'val1');
const stmt = db.prepare('SELECT * FROM storage ORDER BY key');
db.exec("ALTER TABLE storage ADD COLUMN extra TEXT DEFAULT 'def'");
t.assert.deepStrictEqual(stmt.all(), [
{ __proto__: null, key: 'key1', val: 'val1', extra: 'def' },
]);
});

test('reflects a dropped column after the schema changes', (t) => {
using db = new DatabaseSync(':memory:');
db.exec('CREATE TABLE storage(key TEXT, val TEXT, extra TEXT)');
db.prepare('INSERT INTO storage (key, val, extra) VALUES (?, ?, ?)')
.run('key1', 'val1', 'x');
const stmt = db.prepare('SELECT * FROM storage ORDER BY key');
db.exec('ALTER TABLE storage DROP COLUMN extra');
t.assert.deepStrictEqual(stmt.all(), [
{ __proto__: null, key: 'key1', val: 'val1' },
]);
});
});

suite('StatementSync.prototype.iterate()', () => {
Expand Down Expand Up @@ -125,6 +171,29 @@ suite('StatementSync.prototype.iterate()', () => {
}
});

test('reflects an added column after the schema changes', (t) => {
using db = new DatabaseSync(':memory:');
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
db.prepare('INSERT INTO storage (key, val) VALUES (?, ?)').run('key1', 'val1');
const stmt = db.prepare('SELECT * FROM storage ORDER BY key');
db.exec("ALTER TABLE storage ADD COLUMN extra TEXT DEFAULT 'def'");
t.assert.deepStrictEqual(stmt.iterate().toArray(), [
{ __proto__: null, key: 'key1', val: 'val1', extra: 'def' },
]);
});

test('reflects a dropped column after the schema changes', (t) => {
using db = new DatabaseSync(':memory:');
db.exec('CREATE TABLE storage(key TEXT, val TEXT, extra TEXT)');
db.prepare('INSERT INTO storage (key, val, extra) VALUES (?, ?, ?)')
.run('key1', 'val1', 'x');
const stmt = db.prepare('SELECT * FROM storage ORDER BY key');
db.exec('ALTER TABLE storage DROP COLUMN extra');
t.assert.deepStrictEqual(stmt.iterate().toArray(), [
{ __proto__: null, key: 'key1', val: 'val1' },
]);
});

test('iterator keeps the prepared statement from being collected', (t) => {
const db = new DatabaseSync(':memory:');
db.exec(`
Expand Down
Loading