Commit 58ea88e

Browse files
araujoguiaduh95
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 7c61b08 commit 58ea88e

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
@@ -989,6 +989,15 @@ objects. If the prepared statement does not return any results, this method
989989
returns an empty array. The prepared statement [parameters are bound][] using
990990
the values in `namedParameters` and `anonymousParameters`.
991991

992+
### `statement.close()`
993+
994+
<!-- YAML
995+
added: REPLACEME
996+
-->
997+
998+
Finalizes the prepared statement. An exception is thrown if the statement is
999+
already finalized. This method is a wrapper around [`sqlite3_finalize()`][].
1000+
9921001
### `statement.columns()`
9931002

9941003
<!-- YAML
@@ -1683,6 +1692,7 @@ callback function to indicate what type of operation is being authorized.
16831692
[`sqlite3_deserialize()`]: https://sqlite.org/c3ref/deserialize.html
16841693
[`sqlite3_exec()`]: https://www.sqlite.org/c3ref/exec.html
16851694
[`sqlite3_expanded_sql()`]: https://www.sqlite.org/c3ref/expanded_sql.html
1695+
[`sqlite3_finalize()`]: https://www.sqlite.org/c3ref/finalize.html
16861696
[`sqlite3_get_autocommit()`]: https://sqlite.org/c3ref/get_autocommit.html
16871697
[`sqlite3_last_insert_rowid()`]: https://www.sqlite.org/c3ref/last_insert_rowid.html
16881698
[`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
voidStatementSync::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+
voidStatementSync::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
staticvoidSetReadBigInts(const v8::FunctionCallbackInfo<v8::Value>& args);
286286
staticvoidSetReturnArrays(const v8::FunctionCallbackInfo<v8::Value>& args);
287287
staticvoidClose(const v8::FunctionCallbackInfo<v8::Value>& args);
288+
staticvoidDispose(const v8::FunctionCallbackInfo<v8::Value>& args);
288289
v8::MaybeLocal<v8::Value> ColumnToValue(constint column);
289290
v8::MaybeLocal<v8::Name> ColumnNameToName(constint column);
290291
boolGetCachedColumnNames(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"argumentmustbeaboolean/,
110110
});
111111
});
112+
113+
test('throws if the statement is already finalized',(t)=>{
114+
usingdb=newDatabaseSync(':memory:');
115+
constsetup=db.exec(
116+
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;'
117+
);
118+
t.assert.strictEqual(setup,undefined);
119+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
73+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
138+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
316+
conststmt=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: /statementhasbeenfinalized/,
323+
});
324+
});
289325
});
290326

291327
suite('StatementSync.prototype.run()',()=>{
@@ -385,6 +421,18 @@ suite('StatementSync.prototype.run()', () => {
385421
conststmt=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+
usingdb=newDatabaseSync(':memory:');
427+
conststmt=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: /statementhasbeenfinalized/,
434+
});
435+
});
388436
});
389437

390438
suite('StatementSync.prototype.sourceSQL',()=>{
@@ -399,6 +447,16 @@ suite('StatementSync.prototype.sourceSQL', () => {
399447
conststmt=db.prepare(sql);
400448
t.assert.strictEqual(stmt.sourceSQL,sql);
401449
});
450+
451+
test('throws if the statement is already finalized',(t)=>{
452+
usingdb=newDatabaseSync(':memory:');
453+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
482+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
561+
conststmt=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: /statementhasbeenfinalized/,
568+
});
569+
});
490570
});
491571

492572
suite('StatementSync.prototype.setReturnArrays()',()=>{
@@ -505,6 +585,18 @@ suite('StatementSync.prototype.setReturnArrays()', () => {
505585
message: /The"returnArrays"argumentmustbeaboolean/,
506586
});
507587
});
588+
589+
test('throws if the statement is already finalized',(t)=>{
590+
usingdb=newDatabaseSync(':memory:');
591+
conststmt=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: /statementhasbeenfinalized/,
598+
});
599+
});
508600
});
509601

510602
suite('StatementSync.prototype.get() with array output',()=>{
@@ -723,6 +815,18 @@ suite('StatementSync.prototype.setAllowBareNamedParameters()', () => {
723815
message: /The"allowBareNamedParameters"argumentmustbeaboolean/,
724816
});
725817
});
818+
819+
test('throws if the statement is already finalized',(t)=>{
820+
usingdb=newDatabaseSync(':memory:');
821+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
1081+
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
1082+
conststmt=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: /statementhasbeenfinalized/,
1087+
});
1088+
});
1089+
1090+
test('throws if the statement is already finalized',(t)=>{
1091+
usingdb=newDatabaseSync(':memory:');
1092+
conststmt=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: /statementhasbeenfinalized/,
1099+
});
1100+
});
1101+
});
1102+
9741103
suite('StatementSync.prototype[Symbol.dispose]()',()=>{
9751104
test('finalizes an open statement',(t)=>{
9761105
usingdb=newDatabaseSync(':memory:');

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit 58ea88e

Browse files
araujoguiaduh95
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 7c61b08 commit 58ea88e

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
@@ -989,6 +989,15 @@ objects. If the prepared statement does not return any results, this method
989989
returns an empty array. The prepared statement [parameters are bound][] using
990990
the values in `namedParameters` and `anonymousParameters`.
991991

992+
### `statement.close()`
993+
994+
<!-- YAML
995+
added: REPLACEME
996+
-->
997+
998+
Finalizes the prepared statement. An exception is thrown if the statement is
999+
already finalized. This method is a wrapper around [`sqlite3_finalize()`][].
1000+
9921001
### `statement.columns()`
9931002

9941003
<!-- YAML
@@ -1683,6 +1692,7 @@ callback function to indicate what type of operation is being authorized.
16831692
[`sqlite3_deserialize()`]: https://sqlite.org/c3ref/deserialize.html
16841693
[`sqlite3_exec()`]: https://www.sqlite.org/c3ref/exec.html
16851694
[`sqlite3_expanded_sql()`]: https://www.sqlite.org/c3ref/expanded_sql.html
1695+
[`sqlite3_finalize()`]: https://www.sqlite.org/c3ref/finalize.html
16861696
[`sqlite3_get_autocommit()`]: https://sqlite.org/c3ref/get_autocommit.html
16871697
[`sqlite3_last_insert_rowid()`]: https://www.sqlite.org/c3ref/last_insert_rowid.html
16881698
[`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
voidStatementSync::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+
voidStatementSync::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
staticvoidSetReadBigInts(const v8::FunctionCallbackInfo<v8::Value>& args);
286286
staticvoidSetReturnArrays(const v8::FunctionCallbackInfo<v8::Value>& args);
287287
staticvoidClose(const v8::FunctionCallbackInfo<v8::Value>& args);
288+
staticvoidDispose(const v8::FunctionCallbackInfo<v8::Value>& args);
288289
v8::MaybeLocal<v8::Value> ColumnToValue(constint column);
289290
v8::MaybeLocal<v8::Name> ColumnNameToName(constint column);
290291
boolGetCachedColumnNames(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"argumentmustbeaboolean/,
110110
});
111111
});
112+
113+
test('throws if the statement is already finalized',(t)=>{
114+
usingdb=newDatabaseSync(':memory:');
115+
constsetup=db.exec(
116+
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;'
117+
);
118+
t.assert.strictEqual(setup,undefined);
119+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
73+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
138+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
316+
conststmt=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: /statementhasbeenfinalized/,
323+
});
324+
});
289325
});
290326

291327
suite('StatementSync.prototype.run()',()=>{
@@ -385,6 +421,18 @@ suite('StatementSync.prototype.run()', () => {
385421
conststmt=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+
usingdb=newDatabaseSync(':memory:');
427+
conststmt=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: /statementhasbeenfinalized/,
434+
});
435+
});
388436
});
389437

390438
suite('StatementSync.prototype.sourceSQL',()=>{
@@ -399,6 +447,16 @@ suite('StatementSync.prototype.sourceSQL', () => {
399447
conststmt=db.prepare(sql);
400448
t.assert.strictEqual(stmt.sourceSQL,sql);
401449
});
450+
451+
test('throws if the statement is already finalized',(t)=>{
452+
usingdb=newDatabaseSync(':memory:');
453+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
482+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
561+
conststmt=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: /statementhasbeenfinalized/,
568+
});
569+
});
490570
});
491571

492572
suite('StatementSync.prototype.setReturnArrays()',()=>{
@@ -505,6 +585,18 @@ suite('StatementSync.prototype.setReturnArrays()', () => {
505585
message: /The"returnArrays"argumentmustbeaboolean/,
506586
});
507587
});
588+
589+
test('throws if the statement is already finalized',(t)=>{
590+
usingdb=newDatabaseSync(':memory:');
591+
conststmt=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: /statementhasbeenfinalized/,
598+
});
599+
});
508600
});
509601

510602
suite('StatementSync.prototype.get() with array output',()=>{
@@ -723,6 +815,18 @@ suite('StatementSync.prototype.setAllowBareNamedParameters()', () => {
723815
message: /The"allowBareNamedParameters"argumentmustbeaboolean/,
724816
});
725817
});
818+
819+
test('throws if the statement is already finalized',(t)=>{
820+
usingdb=newDatabaseSync(':memory:');
821+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
1081+
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
1082+
conststmt=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: /statementhasbeenfinalized/,
1087+
});
1088+
});
1089+
1090+
test('throws if the statement is already finalized',(t)=>{
1091+
usingdb=newDatabaseSync(':memory:');
1092+
conststmt=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: /statementhasbeenfinalized/,
1099+
});
1100+
});
1101+
});
1102+
9741103
suite('StatementSync.prototype[Symbol.dispose]()',()=>{
9751104
test('finalizes an open statement',(t)=>{
9761105
usingdb=newDatabaseSync(':memory:');

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 58ea88e

Browse files
araujoguiaduh95
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 7c61b08 commit 58ea88e

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
@@ -989,6 +989,15 @@ objects. If the prepared statement does not return any results, this method
989989
returns an empty array. The prepared statement [parameters are bound][] using
990990
the values in `namedParameters` and `anonymousParameters`.
991991

992+
### `statement.close()`
993+
994+
<!-- YAML
995+
added: REPLACEME
996+
-->
997+
998+
Finalizes the prepared statement. An exception is thrown if the statement is
999+
already finalized. This method is a wrapper around [`sqlite3_finalize()`][].
1000+
9921001
### `statement.columns()`
9931002

9941003
<!-- YAML
@@ -1683,6 +1692,7 @@ callback function to indicate what type of operation is being authorized.
16831692
[`sqlite3_deserialize()`]: https://sqlite.org/c3ref/deserialize.html
16841693
[`sqlite3_exec()`]: https://www.sqlite.org/c3ref/exec.html
16851694
[`sqlite3_expanded_sql()`]: https://www.sqlite.org/c3ref/expanded_sql.html
1695+
[`sqlite3_finalize()`]: https://www.sqlite.org/c3ref/finalize.html
16861696
[`sqlite3_get_autocommit()`]: https://sqlite.org/c3ref/get_autocommit.html
16871697
[`sqlite3_last_insert_rowid()`]: https://www.sqlite.org/c3ref/last_insert_rowid.html
16881698
[`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
voidStatementSync::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+
voidStatementSync::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
staticvoidSetReadBigInts(const v8::FunctionCallbackInfo<v8::Value>& args);
286286
staticvoidSetReturnArrays(const v8::FunctionCallbackInfo<v8::Value>& args);
287287
staticvoidClose(const v8::FunctionCallbackInfo<v8::Value>& args);
288+
staticvoidDispose(const v8::FunctionCallbackInfo<v8::Value>& args);
288289
v8::MaybeLocal<v8::Value> ColumnToValue(constint column);
289290
v8::MaybeLocal<v8::Name> ColumnNameToName(constint column);
290291
boolGetCachedColumnNames(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"argumentmustbeaboolean/,
110110
});
111111
});
112+
113+
test('throws if the statement is already finalized',(t)=>{
114+
usingdb=newDatabaseSync(':memory:');
115+
constsetup=db.exec(
116+
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;'
117+
);
118+
t.assert.strictEqual(setup,undefined);
119+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
73+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
138+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
316+
conststmt=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: /statementhasbeenfinalized/,
323+
});
324+
});
289325
});
290326

291327
suite('StatementSync.prototype.run()',()=>{
@@ -385,6 +421,18 @@ suite('StatementSync.prototype.run()', () => {
385421
conststmt=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+
usingdb=newDatabaseSync(':memory:');
427+
conststmt=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: /statementhasbeenfinalized/,
434+
});
435+
});
388436
});
389437

390438
suite('StatementSync.prototype.sourceSQL',()=>{
@@ -399,6 +447,16 @@ suite('StatementSync.prototype.sourceSQL', () => {
399447
conststmt=db.prepare(sql);
400448
t.assert.strictEqual(stmt.sourceSQL,sql);
401449
});
450+
451+
test('throws if the statement is already finalized',(t)=>{
452+
usingdb=newDatabaseSync(':memory:');
453+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
482+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
561+
conststmt=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: /statementhasbeenfinalized/,
568+
});
569+
});
490570
});
491571

492572
suite('StatementSync.prototype.setReturnArrays()',()=>{
@@ -505,6 +585,18 @@ suite('StatementSync.prototype.setReturnArrays()', () => {
505585
message: /The"returnArrays"argumentmustbeaboolean/,
506586
});
507587
});
588+
589+
test('throws if the statement is already finalized',(t)=>{
590+
usingdb=newDatabaseSync(':memory:');
591+
conststmt=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: /statementhasbeenfinalized/,
598+
});
599+
});
508600
});
509601

510602
suite('StatementSync.prototype.get() with array output',()=>{
@@ -723,6 +815,18 @@ suite('StatementSync.prototype.setAllowBareNamedParameters()', () => {
723815
message: /The"allowBareNamedParameters"argumentmustbeaboolean/,
724816
});
725817
});
818+
819+
test('throws if the statement is already finalized',(t)=>{
820+
usingdb=newDatabaseSync(':memory:');
821+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
1081+
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
1082+
conststmt=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: /statementhasbeenfinalized/,
1087+
});
1088+
});
1089+
1090+
test('throws if the statement is already finalized',(t)=>{
1091+
usingdb=newDatabaseSync(':memory:');
1092+
conststmt=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: /statementhasbeenfinalized/,
1099+
});
1100+
});
1101+
});
1102+
9741103
suite('StatementSync.prototype[Symbol.dispose]()',()=>{
9751104
test('finalizes an open statement',(t)=>{
9761105
usingdb=newDatabaseSync(':memory:');

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 58ea88e

Browse files
araujoguiaduh95
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 7c61b08 commit 58ea88e

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
@@ -989,6 +989,15 @@ objects. If the prepared statement does not return any results, this method
989989
returns an empty array. The prepared statement [parameters are bound][] using
990990
the values in `namedParameters` and `anonymousParameters`.
991991

992+
### `statement.close()`
993+
994+
<!-- YAML
995+
added: REPLACEME
996+
-->
997+
998+
Finalizes the prepared statement. An exception is thrown if the statement is
999+
already finalized. This method is a wrapper around [`sqlite3_finalize()`][].
1000+
9921001
### `statement.columns()`
9931002

9941003
<!-- YAML
@@ -1683,6 +1692,7 @@ callback function to indicate what type of operation is being authorized.
16831692
[`sqlite3_deserialize()`]: https://sqlite.org/c3ref/deserialize.html
16841693
[`sqlite3_exec()`]: https://www.sqlite.org/c3ref/exec.html
16851694
[`sqlite3_expanded_sql()`]: https://www.sqlite.org/c3ref/expanded_sql.html
1695+
[`sqlite3_finalize()`]: https://www.sqlite.org/c3ref/finalize.html
16861696
[`sqlite3_get_autocommit()`]: https://sqlite.org/c3ref/get_autocommit.html
16871697
[`sqlite3_last_insert_rowid()`]: https://www.sqlite.org/c3ref/last_insert_rowid.html
16881698
[`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
voidStatementSync::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+
voidStatementSync::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
staticvoidSetReadBigInts(const v8::FunctionCallbackInfo<v8::Value>& args);
286286
staticvoidSetReturnArrays(const v8::FunctionCallbackInfo<v8::Value>& args);
287287
staticvoidClose(const v8::FunctionCallbackInfo<v8::Value>& args);
288+
staticvoidDispose(const v8::FunctionCallbackInfo<v8::Value>& args);
288289
v8::MaybeLocal<v8::Value> ColumnToValue(constint column);
289290
v8::MaybeLocal<v8::Name> ColumnNameToName(constint column);
290291
boolGetCachedColumnNames(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"argumentmustbeaboolean/,
110110
});
111111
});
112+
113+
test('throws if the statement is already finalized',(t)=>{
114+
usingdb=newDatabaseSync(':memory:');
115+
constsetup=db.exec(
116+
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;'
117+
);
118+
t.assert.strictEqual(setup,undefined);
119+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
73+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
138+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
316+
conststmt=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: /statementhasbeenfinalized/,
323+
});
324+
});
289325
});
290326

291327
suite('StatementSync.prototype.run()',()=>{
@@ -385,6 +421,18 @@ suite('StatementSync.prototype.run()', () => {
385421
conststmt=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+
usingdb=newDatabaseSync(':memory:');
427+
conststmt=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: /statementhasbeenfinalized/,
434+
});
435+
});
388436
});
389437

390438
suite('StatementSync.prototype.sourceSQL',()=>{
@@ -399,6 +447,16 @@ suite('StatementSync.prototype.sourceSQL', () => {
399447
conststmt=db.prepare(sql);
400448
t.assert.strictEqual(stmt.sourceSQL,sql);
401449
});
450+
451+
test('throws if the statement is already finalized',(t)=>{
452+
usingdb=newDatabaseSync(':memory:');
453+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
482+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
561+
conststmt=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: /statementhasbeenfinalized/,
568+
});
569+
});
490570
});
491571

492572
suite('StatementSync.prototype.setReturnArrays()',()=>{
@@ -505,6 +585,18 @@ suite('StatementSync.prototype.setReturnArrays()', () => {
505585
message: /The"returnArrays"argumentmustbeaboolean/,
506586
});
507587
});
588+
589+
test('throws if the statement is already finalized',(t)=>{
590+
usingdb=newDatabaseSync(':memory:');
591+
conststmt=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: /statementhasbeenfinalized/,
598+
});
599+
});
508600
});
509601

510602
suite('StatementSync.prototype.get() with array output',()=>{
@@ -723,6 +815,18 @@ suite('StatementSync.prototype.setAllowBareNamedParameters()', () => {
723815
message: /The"allowBareNamedParameters"argumentmustbeaboolean/,
724816
});
725817
});
818+
819+
test('throws if the statement is already finalized',(t)=>{
820+
usingdb=newDatabaseSync(':memory:');
821+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
1081+
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
1082+
conststmt=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: /statementhasbeenfinalized/,
1087+
});
1088+
});
1089+
1090+
test('throws if the statement is already finalized',(t)=>{
1091+
usingdb=newDatabaseSync(':memory:');
1092+
conststmt=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: /statementhasbeenfinalized/,
1099+
});
1100+
});
1101+
});
1102+
9741103
suite('StatementSync.prototype[Symbol.dispose]()',()=>{
9751104
test('finalizes an open statement',(t)=>{
9761105
usingdb=newDatabaseSync(':memory:');

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit 58ea88e

Browse files
araujoguiaduh95
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 7c61b08 commit 58ea88e

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
@@ -989,6 +989,15 @@ objects. If the prepared statement does not return any results, this method
989989
returns an empty array. The prepared statement [parameters are bound][] using
990990
the values in `namedParameters` and `anonymousParameters`.
991991

992+
### `statement.close()`
993+
994+
<!-- YAML
995+
added: REPLACEME
996+
-->
997+
998+
Finalizes the prepared statement. An exception is thrown if the statement is
999+
already finalized. This method is a wrapper around [`sqlite3_finalize()`][].
1000+
9921001
### `statement.columns()`
9931002

9941003
<!-- YAML
@@ -1683,6 +1692,7 @@ callback function to indicate what type of operation is being authorized.
16831692
[`sqlite3_deserialize()`]: https://sqlite.org/c3ref/deserialize.html
16841693
[`sqlite3_exec()`]: https://www.sqlite.org/c3ref/exec.html
16851694
[`sqlite3_expanded_sql()`]: https://www.sqlite.org/c3ref/expanded_sql.html
1695+
[`sqlite3_finalize()`]: https://www.sqlite.org/c3ref/finalize.html
16861696
[`sqlite3_get_autocommit()`]: https://sqlite.org/c3ref/get_autocommit.html
16871697
[`sqlite3_last_insert_rowid()`]: https://www.sqlite.org/c3ref/last_insert_rowid.html
16881698
[`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
voidStatementSync::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+
voidStatementSync::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
staticvoidSetReadBigInts(const v8::FunctionCallbackInfo<v8::Value>& args);
286286
staticvoidSetReturnArrays(const v8::FunctionCallbackInfo<v8::Value>& args);
287287
staticvoidClose(const v8::FunctionCallbackInfo<v8::Value>& args);
288+
staticvoidDispose(const v8::FunctionCallbackInfo<v8::Value>& args);
288289
v8::MaybeLocal<v8::Value> ColumnToValue(constint column);
289290
v8::MaybeLocal<v8::Name> ColumnNameToName(constint column);
290291
boolGetCachedColumnNames(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"argumentmustbeaboolean/,
110110
});
111111
});
112+
113+
test('throws if the statement is already finalized',(t)=>{
114+
usingdb=newDatabaseSync(':memory:');
115+
constsetup=db.exec(
116+
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;'
117+
);
118+
t.assert.strictEqual(setup,undefined);
119+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
73+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
138+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
316+
conststmt=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: /statementhasbeenfinalized/,
323+
});
324+
});
289325
});
290326

291327
suite('StatementSync.prototype.run()',()=>{
@@ -385,6 +421,18 @@ suite('StatementSync.prototype.run()', () => {
385421
conststmt=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+
usingdb=newDatabaseSync(':memory:');
427+
conststmt=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: /statementhasbeenfinalized/,
434+
});
435+
});
388436
});
389437

390438
suite('StatementSync.prototype.sourceSQL',()=>{
@@ -399,6 +447,16 @@ suite('StatementSync.prototype.sourceSQL', () => {
399447
conststmt=db.prepare(sql);
400448
t.assert.strictEqual(stmt.sourceSQL,sql);
401449
});
450+
451+
test('throws if the statement is already finalized',(t)=>{
452+
usingdb=newDatabaseSync(':memory:');
453+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
482+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
561+
conststmt=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: /statementhasbeenfinalized/,
568+
});
569+
});
490570
});
491571

492572
suite('StatementSync.prototype.setReturnArrays()',()=>{
@@ -505,6 +585,18 @@ suite('StatementSync.prototype.setReturnArrays()', () => {
505585
message: /The"returnArrays"argumentmustbeaboolean/,
506586
});
507587
});
588+
589+
test('throws if the statement is already finalized',(t)=>{
590+
usingdb=newDatabaseSync(':memory:');
591+
conststmt=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: /statementhasbeenfinalized/,
598+
});
599+
});
508600
});
509601

510602
suite('StatementSync.prototype.get() with array output',()=>{
@@ -723,6 +815,18 @@ suite('StatementSync.prototype.setAllowBareNamedParameters()', () => {
723815
message: /The"allowBareNamedParameters"argumentmustbeaboolean/,
724816
});
725817
});
818+
819+
test('throws if the statement is already finalized',(t)=>{
820+
usingdb=newDatabaseSync(':memory:');
821+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
1081+
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
1082+
conststmt=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: /statementhasbeenfinalized/,
1087+
});
1088+
});
1089+
1090+
test('throws if the statement is already finalized',(t)=>{
1091+
usingdb=newDatabaseSync(':memory:');
1092+
conststmt=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: /statementhasbeenfinalized/,
1099+
});
1100+
});
1101+
});
1102+
9741103
suite('StatementSync.prototype[Symbol.dispose]()',()=>{
9751104
test('finalizes an open statement',(t)=>{
9761105
usingdb=newDatabaseSync(':memory:');

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 58ea88e

Browse files
araujoguiaduh95
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 7c61b08 commit 58ea88e

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
@@ -989,6 +989,15 @@ objects. If the prepared statement does not return any results, this method
989989
returns an empty array. The prepared statement [parameters are bound][] using
990990
the values in `namedParameters` and `anonymousParameters`.
991991

992+
### `statement.close()`
993+
994+
<!-- YAML
995+
added: REPLACEME
996+
-->
997+
998+
Finalizes the prepared statement. An exception is thrown if the statement is
999+
already finalized. This method is a wrapper around [`sqlite3_finalize()`][].
1000+
9921001
### `statement.columns()`
9931002

9941003
<!-- YAML
@@ -1683,6 +1692,7 @@ callback function to indicate what type of operation is being authorized.
16831692
[`sqlite3_deserialize()`]: https://sqlite.org/c3ref/deserialize.html
16841693
[`sqlite3_exec()`]: https://www.sqlite.org/c3ref/exec.html
16851694
[`sqlite3_expanded_sql()`]: https://www.sqlite.org/c3ref/expanded_sql.html
1695+
[`sqlite3_finalize()`]: https://www.sqlite.org/c3ref/finalize.html
16861696
[`sqlite3_get_autocommit()`]: https://sqlite.org/c3ref/get_autocommit.html
16871697
[`sqlite3_last_insert_rowid()`]: https://www.sqlite.org/c3ref/last_insert_rowid.html
16881698
[`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
voidStatementSync::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+
voidStatementSync::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
staticvoidSetReadBigInts(const v8::FunctionCallbackInfo<v8::Value>& args);
286286
staticvoidSetReturnArrays(const v8::FunctionCallbackInfo<v8::Value>& args);
287287
staticvoidClose(const v8::FunctionCallbackInfo<v8::Value>& args);
288+
staticvoidDispose(const v8::FunctionCallbackInfo<v8::Value>& args);
288289
v8::MaybeLocal<v8::Value> ColumnToValue(constint column);
289290
v8::MaybeLocal<v8::Name> ColumnNameToName(constint column);
290291
boolGetCachedColumnNames(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"argumentmustbeaboolean/,
110110
});
111111
});
112+
113+
test('throws if the statement is already finalized',(t)=>{
114+
usingdb=newDatabaseSync(':memory:');
115+
constsetup=db.exec(
116+
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;'
117+
);
118+
t.assert.strictEqual(setup,undefined);
119+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
73+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
138+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
316+
conststmt=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: /statementhasbeenfinalized/,
323+
});
324+
});
289325
});
290326

291327
suite('StatementSync.prototype.run()',()=>{
@@ -385,6 +421,18 @@ suite('StatementSync.prototype.run()', () => {
385421
conststmt=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+
usingdb=newDatabaseSync(':memory:');
427+
conststmt=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: /statementhasbeenfinalized/,
434+
});
435+
});
388436
});
389437

390438
suite('StatementSync.prototype.sourceSQL',()=>{
@@ -399,6 +447,16 @@ suite('StatementSync.prototype.sourceSQL', () => {
399447
conststmt=db.prepare(sql);
400448
t.assert.strictEqual(stmt.sourceSQL,sql);
401449
});
450+
451+
test('throws if the statement is already finalized',(t)=>{
452+
usingdb=newDatabaseSync(':memory:');
453+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
482+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
561+
conststmt=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: /statementhasbeenfinalized/,
568+
});
569+
});
490570
});
491571

492572
suite('StatementSync.prototype.setReturnArrays()',()=>{
@@ -505,6 +585,18 @@ suite('StatementSync.prototype.setReturnArrays()', () => {
505585
message: /The"returnArrays"argumentmustbeaboolean/,
506586
});
507587
});
588+
589+
test('throws if the statement is already finalized',(t)=>{
590+
usingdb=newDatabaseSync(':memory:');
591+
conststmt=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: /statementhasbeenfinalized/,
598+
});
599+
});
508600
});
509601

510602
suite('StatementSync.prototype.get() with array output',()=>{
@@ -723,6 +815,18 @@ suite('StatementSync.prototype.setAllowBareNamedParameters()', () => {
723815
message: /The"allowBareNamedParameters"argumentmustbeaboolean/,
724816
});
725817
});
818+
819+
test('throws if the statement is already finalized',(t)=>{
820+
usingdb=newDatabaseSync(':memory:');
821+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
1081+
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
1082+
conststmt=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: /statementhasbeenfinalized/,
1087+
});
1088+
});
1089+
1090+
test('throws if the statement is already finalized',(t)=>{
1091+
usingdb=newDatabaseSync(':memory:');
1092+
conststmt=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: /statementhasbeenfinalized/,
1099+
});
1100+
});
1101+
});
1102+
9741103
suite('StatementSync.prototype[Symbol.dispose]()',()=>{
9751104
test('finalizes an open statement',(t)=>{
9761105
usingdb=newDatabaseSync(':memory:');

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 58ea88e

Browse files
araujoguiaduh95
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 7c61b08 commit 58ea88e

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
@@ -989,6 +989,15 @@ objects. If the prepared statement does not return any results, this method
989989
returns an empty array. The prepared statement [parameters are bound][] using
990990
the values in `namedParameters` and `anonymousParameters`.
991991

992+
### `statement.close()`
993+
994+
<!-- YAML
995+
added: REPLACEME
996+
-->
997+
998+
Finalizes the prepared statement. An exception is thrown if the statement is
999+
already finalized. This method is a wrapper around [`sqlite3_finalize()`][].
1000+
9921001
### `statement.columns()`
9931002

9941003
<!-- YAML
@@ -1683,6 +1692,7 @@ callback function to indicate what type of operation is being authorized.
16831692
[`sqlite3_deserialize()`]: https://sqlite.org/c3ref/deserialize.html
16841693
[`sqlite3_exec()`]: https://www.sqlite.org/c3ref/exec.html
16851694
[`sqlite3_expanded_sql()`]: https://www.sqlite.org/c3ref/expanded_sql.html
1695+
[`sqlite3_finalize()`]: https://www.sqlite.org/c3ref/finalize.html
16861696
[`sqlite3_get_autocommit()`]: https://sqlite.org/c3ref/get_autocommit.html
16871697
[`sqlite3_last_insert_rowid()`]: https://www.sqlite.org/c3ref/last_insert_rowid.html
16881698
[`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
voidStatementSync::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+
voidStatementSync::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
staticvoidSetReadBigInts(const v8::FunctionCallbackInfo<v8::Value>& args);
286286
staticvoidSetReturnArrays(const v8::FunctionCallbackInfo<v8::Value>& args);
287287
staticvoidClose(const v8::FunctionCallbackInfo<v8::Value>& args);
288+
staticvoidDispose(const v8::FunctionCallbackInfo<v8::Value>& args);
288289
v8::MaybeLocal<v8::Value> ColumnToValue(constint column);
289290
v8::MaybeLocal<v8::Name> ColumnNameToName(constint column);
290291
boolGetCachedColumnNames(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"argumentmustbeaboolean/,
110110
});
111111
});
112+
113+
test('throws if the statement is already finalized',(t)=>{
114+
usingdb=newDatabaseSync(':memory:');
115+
constsetup=db.exec(
116+
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;'
117+
);
118+
t.assert.strictEqual(setup,undefined);
119+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
73+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
138+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
316+
conststmt=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: /statementhasbeenfinalized/,
323+
});
324+
});
289325
});
290326

291327
suite('StatementSync.prototype.run()',()=>{
@@ -385,6 +421,18 @@ suite('StatementSync.prototype.run()', () => {
385421
conststmt=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+
usingdb=newDatabaseSync(':memory:');
427+
conststmt=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: /statementhasbeenfinalized/,
434+
});
435+
});
388436
});
389437

390438
suite('StatementSync.prototype.sourceSQL',()=>{
@@ -399,6 +447,16 @@ suite('StatementSync.prototype.sourceSQL', () => {
399447
conststmt=db.prepare(sql);
400448
t.assert.strictEqual(stmt.sourceSQL,sql);
401449
});
450+
451+
test('throws if the statement is already finalized',(t)=>{
452+
usingdb=newDatabaseSync(':memory:');
453+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
482+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
561+
conststmt=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: /statementhasbeenfinalized/,
568+
});
569+
});
490570
});
491571

492572
suite('StatementSync.prototype.setReturnArrays()',()=>{
@@ -505,6 +585,18 @@ suite('StatementSync.prototype.setReturnArrays()', () => {
505585
message: /The"returnArrays"argumentmustbeaboolean/,
506586
});
507587
});
588+
589+
test('throws if the statement is already finalized',(t)=>{
590+
usingdb=newDatabaseSync(':memory:');
591+
conststmt=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: /statementhasbeenfinalized/,
598+
});
599+
});
508600
});
509601

510602
suite('StatementSync.prototype.get() with array output',()=>{
@@ -723,6 +815,18 @@ suite('StatementSync.prototype.setAllowBareNamedParameters()', () => {
723815
message: /The"allowBareNamedParameters"argumentmustbeaboolean/,
724816
});
725817
});
818+
819+
test('throws if the statement is already finalized',(t)=>{
820+
usingdb=newDatabaseSync(':memory:');
821+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
1081+
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
1082+
conststmt=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: /statementhasbeenfinalized/,
1087+
});
1088+
});
1089+
1090+
test('throws if the statement is already finalized',(t)=>{
1091+
usingdb=newDatabaseSync(':memory:');
1092+
conststmt=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: /statementhasbeenfinalized/,
1099+
});
1100+
});
1101+
});
1102+
9741103
suite('StatementSync.prototype[Symbol.dispose]()',()=>{
9751104
test('finalizes an open statement',(t)=>{
9761105
usingdb=newDatabaseSync(':memory:');

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit 58ea88e

Browse files
araujoguiaduh95
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 7c61b08 commit 58ea88e

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
@@ -989,6 +989,15 @@ objects. If the prepared statement does not return any results, this method
989989
returns an empty array. The prepared statement [parameters are bound][] using
990990
the values in `namedParameters` and `anonymousParameters`.
991991

992+
### `statement.close()`
993+
994+
<!-- YAML
995+
added: REPLACEME
996+
-->
997+
998+
Finalizes the prepared statement. An exception is thrown if the statement is
999+
already finalized. This method is a wrapper around [`sqlite3_finalize()`][].
1000+
9921001
### `statement.columns()`
9931002

9941003
<!-- YAML
@@ -1683,6 +1692,7 @@ callback function to indicate what type of operation is being authorized.
16831692
[`sqlite3_deserialize()`]: https://sqlite.org/c3ref/deserialize.html
16841693
[`sqlite3_exec()`]: https://www.sqlite.org/c3ref/exec.html
16851694
[`sqlite3_expanded_sql()`]: https://www.sqlite.org/c3ref/expanded_sql.html
1695+
[`sqlite3_finalize()`]: https://www.sqlite.org/c3ref/finalize.html
16861696
[`sqlite3_get_autocommit()`]: https://sqlite.org/c3ref/get_autocommit.html
16871697
[`sqlite3_last_insert_rowid()`]: https://www.sqlite.org/c3ref/last_insert_rowid.html
16881698
[`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
voidStatementSync::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+
voidStatementSync::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
staticvoidSetReadBigInts(const v8::FunctionCallbackInfo<v8::Value>& args);
286286
staticvoidSetReturnArrays(const v8::FunctionCallbackInfo<v8::Value>& args);
287287
staticvoidClose(const v8::FunctionCallbackInfo<v8::Value>& args);
288+
staticvoidDispose(const v8::FunctionCallbackInfo<v8::Value>& args);
288289
v8::MaybeLocal<v8::Value> ColumnToValue(constint column);
289290
v8::MaybeLocal<v8::Name> ColumnNameToName(constint column);
290291
boolGetCachedColumnNames(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"argumentmustbeaboolean/,
110110
});
111111
});
112+
113+
test('throws if the statement is already finalized',(t)=>{
114+
usingdb=newDatabaseSync(':memory:');
115+
constsetup=db.exec(
116+
'CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;'
117+
);
118+
t.assert.strictEqual(setup,undefined);
119+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
73+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
138+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
316+
conststmt=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: /statementhasbeenfinalized/,
323+
});
324+
});
289325
});
290326

291327
suite('StatementSync.prototype.run()',()=>{
@@ -385,6 +421,18 @@ suite('StatementSync.prototype.run()', () => {
385421
conststmt=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+
usingdb=newDatabaseSync(':memory:');
427+
conststmt=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: /statementhasbeenfinalized/,
434+
});
435+
});
388436
});
389437

390438
suite('StatementSync.prototype.sourceSQL',()=>{
@@ -399,6 +447,16 @@ suite('StatementSync.prototype.sourceSQL', () => {
399447
conststmt=db.prepare(sql);
400448
t.assert.strictEqual(stmt.sourceSQL,sql);
401449
});
450+
451+
test('throws if the statement is already finalized',(t)=>{
452+
usingdb=newDatabaseSync(':memory:');
453+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
482+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
561+
conststmt=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: /statementhasbeenfinalized/,
568+
});
569+
});
490570
});
491571

492572
suite('StatementSync.prototype.setReturnArrays()',()=>{
@@ -505,6 +585,18 @@ suite('StatementSync.prototype.setReturnArrays()', () => {
505585
message: /The"returnArrays"argumentmustbeaboolean/,
506586
});
507587
});
588+
589+
test('throws if the statement is already finalized',(t)=>{
590+
usingdb=newDatabaseSync(':memory:');
591+
conststmt=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: /statementhasbeenfinalized/,
598+
});
599+
});
508600
});
509601

510602
suite('StatementSync.prototype.get() with array output',()=>{
@@ -723,6 +815,18 @@ suite('StatementSync.prototype.setAllowBareNamedParameters()', () => {
723815
message: /The"allowBareNamedParameters"argumentmustbeaboolean/,
724816
});
725817
});
818+
819+
test('throws if the statement is already finalized',(t)=>{
820+
usingdb=newDatabaseSync(':memory:');
821+
conststmt=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: /statementhasbeenfinalized/,
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+
usingdb=newDatabaseSync(':memory:');
1081+
db.exec('CREATE TABLE storage(key TEXT, val TEXT)');
1082+
conststmt=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: /statementhasbeenfinalized/,
1087+
});
1088+
});
1089+
1090+
test('throws if the statement is already finalized',(t)=>{
1091+
usingdb=newDatabaseSync(':memory:');
1092+
conststmt=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: /statementhasbeenfinalized/,
1099+
});
1100+
});
1101+
});
1102+
9741103
suite('StatementSync.prototype[Symbol.dispose]()',()=>{
9751104
test('finalizes an open statement',(t)=>{
9761105
usingdb=newDatabaseSync(':memory:');

0 commit comments

Comments
 (0)