Skip to content

Commit 70c57f7

Browse files
committed
sqlite: read column count after step in StatementSync.all()
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
1 parent c4429c8 commit 70c57f7

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/node_sqlite.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2865,12 +2865,16 @@ MaybeLocal<Value> StatementExecutionHelper::All(Environment* env,
28652865
Isolate* isolate = env->isolate();
28662866
EscapableHandleScope scope(isolate);
28672867
int r;
2868-
int num_cols = sqlite3_column_count(stmt);
2868+
int num_cols = 0;
28692869
LocalVector<Value> rows(isolate);
28702870
LocalVector<Value> row_values(isolate);
28712871
LocalVector<Name> row_keys(isolate);
28722872

28732873
while ((r = sqlite3_step(stmt)) == SQLITE_ROW) {
2874+
if (num_cols == 0) {
2875+
num_cols = sqlite3_column_count(stmt);
2876+
}
2877+
28742878
if (ExtractRowValues(env, stmt, num_cols, use_big_ints, &row_values)
28752879
.IsNothing()) {
28762880
return MaybeLocal<Value>();

test/parallel/test-sqlite-statement-sync.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,33 @@ suite('StatementSync.prototype.all()', () => {
8383
{ __proto__: null, key: 'key2', val: 'val2' },
8484
]);
8585
});
86+
87+
test('reflects an added column on first use after the schema changes', (t) => {
88+
const db = new DatabaseSync(nextDb());
89+
t.after(() => { db.close(); });
90+
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
91+
db.prepare('INSERT INTO storage (key, val) VALUES (?, ?)').run('key1', 'val1');
92+
93+
const stmt = db.prepare('SELECT * FROM storage ORDER BY key');
94+
db.exec("ALTER TABLE storage ADD COLUMN extra TEXT DEFAULT 'def'");
95+
t.assert.deepStrictEqual(stmt.all(), [
96+
{ __proto__: null, key: 'key1', val: 'val1', extra: 'def' },
97+
]);
98+
});
99+
100+
test('reflects a dropped column on first use after the schema changes', (t) => {
101+
const db = new DatabaseSync(nextDb());
102+
t.after(() => { db.close(); });
103+
db.exec('CREATE TABLE storage(key TEXT, val TEXT, extra TEXT)');
104+
db.prepare('INSERT INTO storage (key, val, extra) VALUES (?, ?, ?)')
105+
.run('key1', 'val1', 'x');
106+
107+
const stmt = db.prepare('SELECT * FROM storage ORDER BY key');
108+
db.exec('ALTER TABLE storage DROP COLUMN extra');
109+
t.assert.deepStrictEqual(stmt.all(), [
110+
{ __proto__: null, key: 'key1', val: 'val1' },
111+
]);
112+
});
86113
});
87114

88115
suite('StatementSync.prototype.iterate()', () => {

0 commit comments

Comments
 (0)