Skip to content

Commit 51c0d94

Browse files
araujoguitrivikr
authored andcommitted
sqlite: add StatementSync.prototype.close()
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com> PR-URL: #64232 Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
1 parent 04c104a commit 51c0d94

5 files changed

Lines changed: 167 additions & 1 deletion

File tree

doc/api/sqlite.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,15 @@ objects. If the prepared statement does not return any results, this method
997997
returns an empty array. The prepared statement [parameters are bound][] using
998998
the values in `namedParameters` and `anonymousParameters`.
999999

1000+
### `statement.close()`
1001+
1002+
<!-- YAML
1003+
added: REPLACEME
1004+
-->
1005+
1006+
Finalizes the prepared statement. An exception is thrown if the statement is
1007+
already finalized. This method is a wrapper around [`sqlite3_finalize()`][].
1008+
10001009
### `statement.columns()`
10011010

10021011
<!-- YAML
@@ -1691,6 +1700,7 @@ callback function to indicate what type of operation is being authorized.
16911700
[`sqlite3_deserialize()`]: https://sqlite.org/c3ref/deserialize.html
16921701
[`sqlite3_exec()`]: https://www.sqlite.org/c3ref/exec.html
16931702
[`sqlite3_expanded_sql()`]: https://www.sqlite.org/c3ref/expanded_sql.html
1703+
[`sqlite3_finalize()`]: https://www.sqlite.org/c3ref/finalize.html
16941704
[`sqlite3_get_autocommit()`]: https://sqlite.org/c3ref/get_autocommit.html
16951705
[`sqlite3_last_insert_rowid()`]: https://www.sqlite.org/c3ref/last_insert_rowid.html
16961706
[`sqlite3_load_extension()`]: https://www.sqlite.org/c3ref/load_extension.html

src/node_sqlite.cc

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

26582658
void StatementSync::Close(const FunctionCallbackInfo<Value>& args) {
2659+
StatementSync* stmt;
2660+
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
2661+
Environment* env = Environment::GetCurrent(args);
2662+
THROW_AND_RETURN_ON_BAD_STATE(
2663+
env, stmt->IsFinalized(), "statement has been finalized");
2664+
stmt->Close();
2665+
}
2666+
2667+
void StatementSync::Dispose(const FunctionCallbackInfo<Value>& args) {
26592668
StatementSync* stmt;
26602669
ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This());
26612670
stmt->Close();
@@ -3694,7 +3703,8 @@ Local<FunctionTemplate> StatementSync::GetConstructorTemplate(
36943703
isolate, tmpl, "setReadBigInts", StatementSync::SetReadBigInts);
36953704
SetProtoMethod(
36963705
isolate, tmpl, "setReturnArrays", StatementSync::SetReturnArrays);
3697-
SetProtoDispose(isolate, tmpl, StatementSync::Close);
3706+
SetProtoMethod(isolate, tmpl, "close", StatementSync::Close);
3707+
SetProtoDispose(isolate, tmpl, StatementSync::Dispose);
36983708
env->set_sqlite_statement_sync_constructor_template(tmpl);
36993709
}
37003710
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
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ suite('StatementSync.prototype.get()', () => {
6767
__proto__: null, key: 'key1', val: 'val1',
6868
});
6969
});
70+
71+
test('throws if the statement is already finalized', (t) => {
72+
using db = new DatabaseSync(':memory:');
73+
const stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)');
74+
stmt.close();
75+
t.assert.throws(() => {
76+
stmt.get();
77+
}, {
78+
code: 'ERR_INVALID_STATE',
79+
message: /statement has been finalized/,
80+
});
81+
});
7082
});
7183

7284
suite('StatementSync.prototype.all()', () => {
@@ -120,6 +132,18 @@ suite('StatementSync.prototype.all()', () => {
120132
{ __proto__: null, key: 'key1', val: 'val1' },
121133
]);
122134
});
135+
136+
test('throws if the statement is already finalized', (t) => {
137+
using db = new DatabaseSync(':memory:');
138+
const stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)');
139+
stmt.close();
140+
t.assert.throws(() => {
141+
stmt.all();
142+
}, {
143+
code: 'ERR_INVALID_STATE',
144+
message: /statement has been finalized/,
145+
});
146+
});
123147
});
124148

125149
suite('StatementSync.prototype.iterate()', () => {
@@ -286,6 +310,18 @@ suite('StatementSync.prototype.iterate()', () => {
286310
stmt2.get();
287311
it.next();
288312
});
313+
314+
test('throws if the statement is already finalized', (t) => {
315+
using db = new DatabaseSync(':memory:');
316+
const stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)');
317+
stmt.close();
318+
t.assert.throws(() => {
319+
stmt.iterate();
320+
}, {
321+
code: 'ERR_INVALID_STATE',
322+
message: /statement has been finalized/,
323+
});
324+
});
289325
});
290326

291327
suite('StatementSync.prototype.run()', () => {
@@ -385,6 +421,18 @@ suite('StatementSync.prototype.run()', () => {
385421
const stmt = db.prepare('INSERT INTO data (key, val) VALUES (?1, ?2)');
386422
t.assert.deepStrictEqual(stmt.run(1, 2), { changes: 1, lastInsertRowid: 1 });
387423
});
424+
425+
test('throws if the statement is already finalized', (t) => {
426+
using db = new DatabaseSync(':memory:');
427+
const stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)');
428+
stmt.close();
429+
t.assert.throws(() => {
430+
stmt.run();
431+
}, {
432+
code: 'ERR_INVALID_STATE',
433+
message: /statement has been finalized/,
434+
});
435+
});
388436
});
389437

390438
suite('StatementSync.prototype.sourceSQL', () => {
@@ -399,6 +447,16 @@ suite('StatementSync.prototype.sourceSQL', () => {
399447
const stmt = db.prepare(sql);
400448
t.assert.strictEqual(stmt.sourceSQL, sql);
401449
});
450+
451+
test('throws if the statement is already finalized', (t) => {
452+
using db = new DatabaseSync(':memory:');
453+
const stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)');
454+
stmt.close();
455+
t.assert.throws(() => stmt.sourceSQL, {
456+
code: 'ERR_INVALID_STATE',
457+
message: /statement has been finalized/,
458+
});
459+
});
402460
});
403461

404462
suite('StatementSync.prototype.expandedSQL', () => {
@@ -418,6 +476,16 @@ suite('StatementSync.prototype.expandedSQL', () => {
418476
);
419477
t.assert.strictEqual(stmt.expandedSQL, expanded);
420478
});
479+
480+
test('throws if the statement is already finalized', (t) => {
481+
using db = new DatabaseSync(':memory:');
482+
const stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)');
483+
stmt.close();
484+
t.assert.throws(() => stmt.expandedSQL, {
485+
code: 'ERR_INVALID_STATE',
486+
message: /statement has been finalized/,
487+
});
488+
});
421489
});
422490

423491
suite('StatementSync.prototype.setReadBigInts()', () => {
@@ -487,6 +555,18 @@ suite('StatementSync.prototype.setReadBigInts()', () => {
487555
[`${Number.MAX_SAFE_INTEGER} + 1`]: 2n ** 53n,
488556
});
489557
});
558+
559+
test('throws if the statement is already finalized', (t) => {
560+
using db = new DatabaseSync(':memory:');
561+
const stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)');
562+
stmt.close();
563+
t.assert.throws(() => {
564+
stmt.setReadBigInts(true);
565+
}, {
566+
code: 'ERR_INVALID_STATE',
567+
message: /statement has been finalized/,
568+
});
569+
});
490570
});
491571

492572
suite('StatementSync.prototype.setReturnArrays()', () => {
@@ -505,6 +585,18 @@ suite('StatementSync.prototype.setReturnArrays()', () => {
505585
message: /The "returnArrays" argument must be a boolean/,
506586
});
507587
});
588+
589+
test('throws if the statement is already finalized', (t) => {
590+
using db = new DatabaseSync(':memory:');
591+
const stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)');
592+
stmt.close();
593+
t.assert.throws(() => {
594+
stmt.setReturnArrays(true);
595+
}, {
596+
code: 'ERR_INVALID_STATE',
597+
message: /statement has been finalized/,
598+
});
599+
});
508600
});
509601

510602
suite('StatementSync.prototype.get() with array output', () => {
@@ -723,6 +815,18 @@ suite('StatementSync.prototype.setAllowBareNamedParameters()', () => {
723815
message: /The "allowBareNamedParameters" argument must be a boolean/,
724816
});
725817
});
818+
819+
test('throws if the statement is already finalized', (t) => {
820+
using db = new DatabaseSync(':memory:');
821+
const stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)');
822+
stmt.close();
823+
t.assert.throws(() => {
824+
stmt.setAllowBareNamedParameters(true);
825+
}, {
826+
code: 'ERR_INVALID_STATE',
827+
message: /statement has been finalized/,
828+
});
829+
});
726830
});
727831

728832
suite('options.readBigInts', () => {
@@ -971,6 +1075,31 @@ suite('options.allowBareNamedParameters', () => {
9711075
});
9721076

9731077

1078+
suite('StatementSync.prototype.close()', () => {
1079+
test('finalizes an open statement', (t) => {
1080+
using db = new DatabaseSync(':memory:');
1081+
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
1082+
const stmt = db.prepare('SELECT * FROM storage');
1083+
t.assert.strictEqual(stmt.close(), undefined);
1084+
t.assert.throws(() => stmt.get(), {
1085+
code: 'ERR_INVALID_STATE',
1086+
message: /statement has been finalized/,
1087+
});
1088+
});
1089+
1090+
test('throws if the statement is already finalized', (t) => {
1091+
using db = new DatabaseSync(':memory:');
1092+
const stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)');
1093+
stmt.close();
1094+
t.assert.throws(() => {
1095+
stmt.close();
1096+
}, {
1097+
code: 'ERR_INVALID_STATE',
1098+
message: /statement has been finalized/,
1099+
});
1100+
});
1101+
});
1102+
9741103
suite('StatementSync.prototype[Symbol.dispose]()', () => {
9751104
test('finalizes an open statement', (t) => {
9761105
using db = new DatabaseSync(':memory:');

0 commit comments

Comments
 (0)