sqlite: add StatementSync.prototype.close() and [Symbol.dispose]() by araujogui · Pull Request #64232 · nodejs/node · GitHub
Skip to content
Closed
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
19 changes: 19 additions & 0 deletions doc/api/sqlite.md
26 changes: 22 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,21 @@ inline bool StatementSync::IsFinalized() {
return statement_ == nullptr;
}

void StatementSync::Close(const FunctionCallbackInfo<Value>& args) {
StatementSync* 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");
stmt->Close();
}

void StatementSync::Dispose(const FunctionCallbackInfo<Value>& args) {
StatementSync* stmt;
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
stmt->Close();
}

inline int StatementSync::ResetStatement() {
reset_generation_++;
return sqlite3_reset(statement_);
Expand Down Expand Up @@ -3629,6 +3645,8 @@ Local<FunctionTemplate> StatementSync::GetConstructorTemplate(
isolate, tmpl, "setReadBigInts", StatementSync::SetReadBigInts);
SetProtoMethod(
isolate, tmpl, "setReturnArrays", StatementSync::SetReturnArrays);
SetProtoMethod(isolate, tmpl, "close", StatementSync::Close);
SetProtoDispose(isolate, tmpl, StatementSync::Dispose);
env->set_sqlite_statement_sync_constructor_template(tmpl);
}
return tmpl;
Expand Down
3 changes: 3 additions & 0 deletions src/node_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ 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 Close(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 +291,7 @@ class StatementSync : public BaseObject {

private:
~StatementSync() override;
void Close();
BaseObjectPtr<DatabaseSync> db_;
sqlite3_stmt* statement_;
bool return_arrays_ = false;
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-sqlite-named-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ suite('StatementSync.prototype.setAllowUnknownNamedParameters()', () => {
message: /The "enabled" argument must be a boolean/,
});
});

test('throws if the statement is already finalized', (t) => {
using db = new DatabaseSync(':memory:');
const setup = db.exec(
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;'
);
t.assert.strictEqual(setup, undefined);
const stmt = db.prepare('INSERT INTO data (key, val) VALUES ($k, $v)');
stmt.close();
t.assert.throws(() => {
stmt.setAllowUnknownNamedParameters(true);
}, {
code: 'ERR_INVALID_STATE',
message: /statement has been finalized/,
});
});
});

suite('options.allowUnknownNamedParameters', () => {
Expand Down
172 changes: 172 additions & 0 deletions test/parallel/test-sqlite-statement-sync.js
Loading