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
14 changes: 14 additions & 0 deletions doc/api/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/node_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
54 changes: 54 additions & 0 deletions test/parallel/test-sqlite-statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Loading