sqlite: add StatementSync.prototype.close() · nodejs/node@41161dd · GitHub
Skip to content

Commit 41161dd

Browse files
araujoguiaduh95
authored andcommitted
sqlite: add StatementSync.prototype.close()
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com> PR-URL: #64232 Backport-PR-URL: #66128 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent adbc757 commit 41161dd

5 files changed

Lines changed: 167 additions & 1 deletion

File tree

‎doc/api/sqlite.md‎

Lines changed: 10 additions & 0 deletions

‎src/node_sqlite.cc‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2691,6 +2691,15 @@ inline bool StatementSync::IsFinalized() {
26912691
}
26922692

26932693
void StatementSync::Close(const FunctionCallbackInfo<Value>& args) {
2694+
StatementSync* stmt;
2695+
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
2696+
Environment* env = Environment::GetCurrent(args);
2697+
THROW_AND_RETURN_ON_BAD_STATE(
2698+
env, stmt->IsFinalized(), "statement has been finalized");
2699+
stmt->Close();
2700+
}
2701+
2702+
void StatementSync::Dispose(const FunctionCallbackInfo<Value>& args) {
26942703
StatementSync* stmt;
26952704
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
26962705
stmt->Close();
@@ -3734,7 +3743,8 @@ Local<FunctionTemplate> StatementSync::GetConstructorTemplate(
37343743
isolate, tmpl, "setReadBigInts", StatementSync::SetReadBigInts);
37353744
SetProtoMethod(
37363745
isolate, tmpl, "setReturnArrays", StatementSync::SetReturnArrays);
3737-
SetProtoDispose(isolate, tmpl, StatementSync::Close);
3746+
SetProtoMethod(isolate, tmpl, "close", StatementSync::Close);
3747+
SetProtoDispose(isolate, tmpl, StatementSync::Dispose);
37383748
env->set_sqlite_statement_sync_constructor_template(tmpl);
37393749
}
37403750
return tmpl;

‎src/node_sqlite.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ class StatementSync : public BaseObject {
285285
static void SetReadBigInts(const v8::FunctionCallbackInfo<v8::Value>& args);
286286
static void SetReturnArrays(const v8::FunctionCallbackInfo<v8::Value>& args);
287287
static void Close(const v8::FunctionCallbackInfo<v8::Value>& args);
288+
static void Dispose(const v8::FunctionCallbackInfo<v8::Value>& args);
288289
v8::MaybeLocal<v8::Value> ColumnToValue(const int column);
289290
v8::MaybeLocal<v8::Name> ColumnNameToName(const int column);
290291
bool GetCachedColumnNames(v8::LocalVector<v8::Name>* keys);

‎test/parallel/test-sqlite-named-parameters.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ suite('StatementSync.prototype.setAllowUnknownNamedParameters()', () => {
109109
message: /The "enabled" argument must be a boolean/,
110110
});
111111
});
112+
113+
test('throws if the statement is already finalized', (t) => {
114+
using db = new DatabaseSync(':memory:');
115+
const setup = db.exec(
116+
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;'
117+
);
118+
t.assert.strictEqual(setup, undefined);
119+
const stmt = db.prepare('INSERT INTO data (key, val) VALUES ($k, $v)');
120+
stmt.close();
121+
t.assert.throws(() => {
122+
stmt.setAllowUnknownNamedParameters(true);
123+
}, {
124+
code: 'ERR_INVALID_STATE',
125+
message: /statement has been finalized/,
126+
});
127+
});
112128
});
113129

114130
suite('options.allowUnknownNamedParameters', () => {

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

Lines changed: 129 additions & 0 deletions

0 commit comments

Comments
 (0)