sqlite: add statement.columnCount getter

Add a getter to `Statement` that returns the number of columns in the
prepared statement's result set, wrapping `sqlite3_column_count()`.

Fixes: https://github.com/nodejs/node/issues/67352
Assisted-by: Claude Code
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67658
Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com>
This commit is contained in:
Guilherme Araújo authored and Antoine du Hamel committed 2026-11-25 18:05:55 +01:00
1 parent 6dc819754e
commit 610491c77a
4 files changed
+83

No files matched your search

+14
View File
@@ -1324,6 +1324,19 @@ aggregate function, or a [`'sqlite.db.query'`][] subscriber. Idle statements
on the same connection can be finalized from such a callback. This method is a
wrapper around [`sqlite3_finalize()`][].
### `statement.columnCount`
<!-- YAML
added: REPLACEME
-->
* Type: {number} The number of columns in the result set returned by the
prepared statement.
This property is `0` for statements that do not return data, such as `INSERT`,
`UPDATE`, and `DELETE` statements without a `RETURNING` clause. This property
is a wrapper around [`sqlite3_column_count()`][].
### `statement.columns()`
<!-- YAML
@@ -2172,6 +2185,7 @@ callback function to indicate what type of operation is being authorized.
[`sqlite3_backup_step()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupstep
[`sqlite3_changes64()`]: https://www.sqlite.org/c3ref/changes.html
[`sqlite3_close_v2()`]: https://www.sqlite.org/c3ref/close.html
[`sqlite3_column_count()`]: https://www.sqlite.org/c3ref/column_count.html
[`sqlite3_column_database_name()`]: https://www.sqlite.org/c3ref/column_database_name.html
[`sqlite3_column_decltype()`]: https://www.sqlite.org/c3ref/column_decltype.html
[`sqlite3_column_name()`]: https://www.sqlite.org/c3ref/column_name.html
+13
View File
@@ -4169,6 +4169,15 @@ void Statement::ExpandedSQLGetter(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(result);
}
void Statement::ColumnCountGetter(const FunctionCallbackInfo<Value>& args) {
Statement* stmt;
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
Environment* env = Environment::GetCurrent(args);
THROW_AND_RETURN_ON_BAD_STATE(
env, stmt->IsFinalized(), "statement has been finalized");
args.GetReturnValue().Set(sqlite3_column_count(stmt->statement_.get()));
}
void Statement::Stat(const FunctionCallbackInfo<Value>& args) {
Statement* stmt;
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
@@ -4624,6 +4633,10 @@ Local<FunctionTemplate> Statement::GetConstructorTemplate(Environment* env) {
tmpl,
FIXED_ONE_BYTE_STRING(isolate, "expandedSQL"),
Statement::ExpandedSQLGetter);
SetSideEffectFreeGetter(isolate,
tmpl,
FIXED_ONE_BYTE_STRING(isolate, "columnCount"),
Statement::ColumnCountGetter);
SetProtoMethodNoSideEffect(isolate, tmpl, "stat", Statement::Stat);
SetProtoMethod(isolate, tmpl, "resetStats", Statement::ResetStats);
SetProtoMethod(isolate,
+2
View File
@@ -396,6 +396,8 @@ class Statement : public BaseObject {
static void SourceSQLGetter(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ExpandedSQLGetter(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void ColumnCountGetter(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void Stat(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ResetStats(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetAllowBareNamedParameters(
+54
View File
@@ -553,6 +553,60 @@ suite('Statement.prototype.expandedSQL', () => {
});
});
suite('Statement.prototype.columnCount', () => {
test('returns the number of result columns', (t) => {
using db = new Database(':memory:');
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
t.assert.strictEqual(db.prepare('SELECT * FROM storage').columnCount, 2);
t.assert.strictEqual(db.prepare('SELECT 1').columnCount, 1);
t.assert.strictEqual(db.prepare('SELECT 1, 2, 3').columnCount, 3);
t.assert.strictEqual(db.prepare('PRAGMA user_version').columnCount, 1);
t.assert.strictEqual(
db.prepare('INSERT INTO storage VALUES (?, ?) RETURNING key').columnCount,
1,
);
t.assert.strictEqual(
db.prepare('UPDATE storage SET val = ? RETURNING *').columnCount,
2,
);
t.assert.strictEqual(
db.prepare('DELETE FROM storage RETURNING val').columnCount,
1,
);
});
test('returns 0 for statements that do not return data', (t) => {
using db = new Database(':memory:');
t.assert.strictEqual(
db.prepare('CREATE TABLE storage(key TEXT, val TEXT)').columnCount,
0,
);
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
t.assert.strictEqual(
db.prepare('INSERT INTO storage VALUES (?, ?)').columnCount,
0,
);
t.assert.strictEqual(
db.prepare('UPDATE storage SET val = ?').columnCount,
0,
);
t.assert.strictEqual(
db.prepare('DELETE FROM storage').columnCount,
0,
);
});
test('throws if the statement is already finalized', (t) => {
using db = new Database(':memory:');
const stmt = db.prepare('SELECT 1');
stmt.close();
t.assert.throws(() => stmt.columnCount, {
code: 'ERR_INVALID_STATE',
message: /statement has been finalized/,
});
});
});
suite('Statement.prototype.stat()', () => {
const counters = [
'fullscanStep', 'sort', 'autoindex', 'vmStep', 'reprepare',