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
9 changes: 9 additions & 0 deletions doc/api/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,15 @@ added: v22.5.0
The source SQL text of the prepared statement. This property is a
wrapper around [`sqlite3_sql()`][].

### `statement[Symbol.dispose]()`

<!-- YAML
added: REPLACEME
-->

Finalizes the prepared statement. If the prepared statement is already
finalized, then this is a no-op.

## Class: `SQLTagStore`

<!-- YAML
Expand Down
20 changes: 16 additions & 4 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1037,10 +1037,7 @@ void DatabaseSync::FinalizeStatements() {
}

void DatabaseSync::UntrackStatement(StatementSync* statement) {
auto it = statements_.find(statement);
if (it != statements_.end()) {
statements_.erase(it);
}
statements_.erase(statement);
}

inline bool DatabaseSync::IsOpen() {
Expand Down Expand Up @@ -2578,6 +2575,10 @@ StatementSync::StatementSync(Environment* env,
}

StatementSync::~StatementSync() {
Close();
}

void StatementSync::Close() {
if (!IsFinalized()) {
db_->UntrackStatement(this);
Finalize();
Expand All @@ -2598,6 +2599,16 @@ inline bool StatementSync::IsFinalized() {
return statement_ == nullptr;
}

void StatementSync::Dispose(const FunctionCallbackInfo<Value>& args) {
StatementSync* stmt;
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
v8::TryCatch try_catch(args.GetIsolate());
stmt->Close();
if (try_catch.HasCaught()) {
CHECK(try_catch.CanContinue());
}
}

inline int StatementSync::ResetStatement() {
reset_generation_++;
return sqlite3_reset(statement_);
Expand Down Expand Up @@ -3625,6 +3636,7 @@ Local<FunctionTemplate> StatementSync::GetConstructorTemplate(
isolate, tmpl, "setReadBigInts", StatementSync::SetReadBigInts);
SetProtoMethod(
isolate, tmpl, "setReturnArrays", StatementSync::SetReturnArrays);
SetProtoDispose(isolate, tmpl, StatementSync::Dispose);
env->set_sqlite_statement_sync_constructor_template(tmpl);
}
return tmpl;
Expand Down
2 changes: 2 additions & 0 deletions src/node_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ class StatementSync : public BaseObject {
const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetReadBigInts(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetReturnArrays(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Dispose(const v8::FunctionCallbackInfo<v8::Value>& args);
v8::MaybeLocal<v8::Value> ColumnToValue(const int column);
v8::MaybeLocal<v8::Name> ColumnNameToName(const int column);
bool GetCachedColumnNames(v8::LocalVector<v8::Name>* keys);
Expand All @@ -289,6 +290,7 @@ class StatementSync : public BaseObject {

private:
~StatementSync() override;
void Close();
BaseObjectPtr<DatabaseSync> db_;
sqlite3_stmt* statement_;
bool return_arrays_ = false;
Expand Down
43 changes: 43 additions & 0 deletions test/parallel/test-sqlite-statement-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,3 +909,46 @@ suite('options.allowBareNamedParameters', () => {
);
});
});


suite('StatementSync.prototype[Symbol.dispose]()', () => {
test('finalizes an open statement', (t) => {
using db = new DatabaseSync(':memory:');
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
const stmt = db.prepare('SELECT * FROM storage');
stmt[Symbol.dispose]();
t.assert.throws(() => stmt.get(), {
code: 'ERR_INVALID_STATE',
message: /statement has been finalized/,
});
});

test('does not throw on an already-finalized statement', () => {
using db = new DatabaseSync(':memory:');
const stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)');
stmt[Symbol.dispose]();
stmt[Symbol.dispose]();
});

test('works with a using declaration', (t) => {
using db = new DatabaseSync(':memory:');
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
let captured;
{
using stmt = db.prepare('SELECT * FROM storage');
captured = stmt;
t.assert.deepStrictEqual(stmt.all(), []);
}
t.assert.throws(() => captured.get(), {
code: 'ERR_INVALID_STATE',
message: /statement has been finalized/,
});
});

test('closing the database after dispose does not double-finalize', () => {
using db = new DatabaseSync(':memory:');
const stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)');
stmt[Symbol.dispose]();
db.close();
});
});
Loading