@@ -53,6 +53,18 @@ suite('StatementSync.prototype.get()', () => {
5353 const stmt = db . prepare ( 'SELECT 1 as __proto__, 2 as constructor, 3 as toString' ) ;
5454 t . assert . deepStrictEqual ( stmt . get ( ) , { __proto__ : null , [ '__proto__' ] : 1 , constructor : 2 , toString : 3 } ) ;
5555 } ) ;
56+
57+ test ( 'throws if the statement is already finalized' , ( t ) => {
58+ using db = new DatabaseSync ( ':memory:' ) ;
59+ const stmt = db . prepare ( 'CREATE TABLE storage(key TEXT, val TEXT)' ) ;
60+ stmt . close ( ) ;
61+ t . assert . throws ( ( ) => {
62+ stmt . get ( ) ;
63+ } , {
64+ code : 'ERR_INVALID_STATE' ,
65+ message : / s t a t e m e n t h a s b e e n f i n a l i z e d / ,
66+ } ) ;
67+ } ) ;
5668} ) ;
5769
5870suite ( 'StatementSync.prototype.all()' , ( ) => {
@@ -83,6 +95,18 @@ suite('StatementSync.prototype.all()', () => {
8395 { __proto__ : null , key : 'key2' , val : 'val2' } ,
8496 ] ) ;
8597 } ) ;
98+
99+ test ( 'throws if the statement is already finalized' , ( t ) => {
100+ using db = new DatabaseSync ( ':memory:' ) ;
101+ const stmt = db . prepare ( 'CREATE TABLE storage(key TEXT, val TEXT)' ) ;
102+ stmt . close ( ) ;
103+ t . assert . throws ( ( ) => {
104+ stmt . all ( ) ;
105+ } , {
106+ code : 'ERR_INVALID_STATE' ,
107+ message : / s t a t e m e n t h a s b e e n f i n a l i z e d / ,
108+ } ) ;
109+ } ) ;
86110} ) ;
87111
88112suite ( 'StatementSync.prototype.iterate()' , ( ) => {
@@ -226,6 +250,18 @@ suite('StatementSync.prototype.iterate()', () => {
226250 stmt2 . get ( ) ;
227251 it . next ( ) ;
228252 } ) ;
253+
254+ test ( 'throws if the statement is already finalized' , ( t ) => {
255+ using db = new DatabaseSync ( ':memory:' ) ;
256+ const stmt = db . prepare ( 'CREATE TABLE storage(key TEXT, val TEXT)' ) ;
257+ stmt . close ( ) ;
258+ t . assert . throws ( ( ) => {
259+ stmt . iterate ( ) ;
260+ } , {
261+ code : 'ERR_INVALID_STATE' ,
262+ message : / s t a t e m e n t h a s b e e n f i n a l i z e d / ,
263+ } ) ;
264+ } ) ;
229265} ) ;
230266
231267suite ( 'StatementSync.prototype.run()' , ( ) => {
@@ -325,6 +361,18 @@ suite('StatementSync.prototype.run()', () => {
325361 const stmt = db . prepare ( 'INSERT INTO data (key, val) VALUES (?1, ?2)' ) ;
326362 t . assert . deepStrictEqual ( stmt . run ( 1 , 2 ) , { changes : 1 , lastInsertRowid : 1 } ) ;
327363 } ) ;
364+
365+ test ( 'throws if the statement is already finalized' , ( t ) => {
366+ using db = new DatabaseSync ( ':memory:' ) ;
367+ const stmt = db . prepare ( 'CREATE TABLE storage(key TEXT, val TEXT)' ) ;
368+ stmt . close ( ) ;
369+ t . assert . throws ( ( ) => {
370+ stmt . run ( ) ;
371+ } , {
372+ code : 'ERR_INVALID_STATE' ,
373+ message : / s t a t e m e n t h a s b e e n f i n a l i z e d / ,
374+ } ) ;
375+ } ) ;
328376} ) ;
329377
330378suite ( 'StatementSync.prototype.sourceSQL' , ( ) => {
@@ -339,6 +387,16 @@ suite('StatementSync.prototype.sourceSQL', () => {
339387 const stmt = db . prepare ( sql ) ;
340388 t . assert . strictEqual ( stmt . sourceSQL , sql ) ;
341389 } ) ;
390+
391+ test ( 'throws if the statement is already finalized' , ( t ) => {
392+ using db = new DatabaseSync ( ':memory:' ) ;
393+ const stmt = db . prepare ( 'CREATE TABLE storage(key TEXT, val TEXT)' ) ;
394+ stmt . close ( ) ;
395+ t . assert . throws ( ( ) => stmt . sourceSQL , {
396+ code : 'ERR_INVALID_STATE' ,
397+ message : / s t a t e m e n t h a s b e e n f i n a l i z e d / ,
398+ } ) ;
399+ } ) ;
342400} ) ;
343401
344402suite ( 'StatementSync.prototype.expandedSQL' , ( ) => {
@@ -358,6 +416,16 @@ suite('StatementSync.prototype.expandedSQL', () => {
358416 ) ;
359417 t . assert . strictEqual ( stmt . expandedSQL , expanded ) ;
360418 } ) ;
419+
420+ test ( 'throws if the statement is already finalized' , ( t ) => {
421+ using db = new DatabaseSync ( ':memory:' ) ;
422+ const stmt = db . prepare ( 'CREATE TABLE storage(key TEXT, val TEXT)' ) ;
423+ stmt . close ( ) ;
424+ t . assert . throws ( ( ) => stmt . expandedSQL , {
425+ code : 'ERR_INVALID_STATE' ,
426+ message : / s t a t e m e n t h a s b e e n f i n a l i z e d / ,
427+ } ) ;
428+ } ) ;
361429} ) ;
362430
363431suite ( 'StatementSync.prototype.setReadBigInts()' , ( ) => {
@@ -427,6 +495,18 @@ suite('StatementSync.prototype.setReadBigInts()', () => {
427495 [ `${ Number . MAX_SAFE_INTEGER } + 1` ] : 2n ** 53n ,
428496 } ) ;
429497 } ) ;
498+
499+ test ( 'throws if the statement is already finalized' , ( t ) => {
500+ using db = new DatabaseSync ( ':memory:' ) ;
501+ const stmt = db . prepare ( 'CREATE TABLE storage(key TEXT, val TEXT)' ) ;
502+ stmt . close ( ) ;
503+ t . assert . throws ( ( ) => {
504+ stmt . setReadBigInts ( true ) ;
505+ } , {
506+ code : 'ERR_INVALID_STATE' ,
507+ message : / s t a t e m e n t h a s b e e n f i n a l i z e d / ,
508+ } ) ;
509+ } ) ;
430510} ) ;
431511
432512suite ( 'StatementSync.prototype.setReturnArrays()' , ( ) => {
@@ -445,6 +525,18 @@ suite('StatementSync.prototype.setReturnArrays()', () => {
445525 message : / T h e " r e t u r n A r r a y s " a r g u m e n t m u s t b e a b o o l e a n / ,
446526 } ) ;
447527 } ) ;
528+
529+ test ( 'throws if the statement is already finalized' , ( t ) => {
530+ using db = new DatabaseSync ( ':memory:' ) ;
531+ const stmt = db . prepare ( 'CREATE TABLE storage(key TEXT, val TEXT)' ) ;
532+ stmt . close ( ) ;
533+ t . assert . throws ( ( ) => {
534+ stmt . setReturnArrays ( true ) ;
535+ } , {
536+ code : 'ERR_INVALID_STATE' ,
537+ message : / s t a t e m e n t h a s b e e n f i n a l i z e d / ,
538+ } ) ;
539+ } ) ;
448540} ) ;
449541
450542suite ( 'StatementSync.prototype.get() with array output' , ( ) => {
@@ -663,6 +755,18 @@ suite('StatementSync.prototype.setAllowBareNamedParameters()', () => {
663755 message : / T h e " a l l o w B a r e N a m e d P a r a m e t e r s " a r g u m e n t m u s t b e a b o o l e a n / ,
664756 } ) ;
665757 } ) ;
758+
759+ test ( 'throws if the statement is already finalized' , ( t ) => {
760+ using db = new DatabaseSync ( ':memory:' ) ;
761+ const stmt = db . prepare ( 'CREATE TABLE storage(key TEXT, val TEXT)' ) ;
762+ stmt . close ( ) ;
763+ t . assert . throws ( ( ) => {
764+ stmt . setAllowBareNamedParameters ( true ) ;
765+ } , {
766+ code : 'ERR_INVALID_STATE' ,
767+ message : / s t a t e m e n t h a s b e e n f i n a l i z e d / ,
768+ } ) ;
769+ } ) ;
666770} ) ;
667771
668772suite ( 'options.readBigInts' , ( ) => {
@@ -911,6 +1015,31 @@ suite('options.allowBareNamedParameters', () => {
9111015} ) ;
9121016
9131017
1018+ suite ( 'StatementSync.prototype.close()' , ( ) => {
1019+ test ( 'finalizes an open statement' , ( t ) => {
1020+ using db = new DatabaseSync ( ':memory:' ) ;
1021+ db . exec ( 'CREATE TABLE storage(key TEXT, val TEXT)' ) ;
1022+ const stmt = db . prepare ( 'SELECT * FROM storage' ) ;
1023+ t . assert . strictEqual ( stmt . close ( ) , undefined ) ;
1024+ t . assert . throws ( ( ) => stmt . get ( ) , {
1025+ code : 'ERR_INVALID_STATE' ,
1026+ message : / s t a t e m e n t h a s b e e n f i n a l i z e d / ,
1027+ } ) ;
1028+ } ) ;
1029+
1030+ test ( 'throws if the statement is already finalized' , ( t ) => {
1031+ using db = new DatabaseSync ( ':memory:' ) ;
1032+ const stmt = db . prepare ( 'CREATE TABLE storage(key TEXT, val TEXT)' ) ;
1033+ stmt . close ( ) ;
1034+ t . assert . throws ( ( ) => {
1035+ stmt . close ( ) ;
1036+ } , {
1037+ code : 'ERR_INVALID_STATE' ,
1038+ message : / s t a t e m e n t h a s b e e n f i n a l i z e d / ,
1039+ } ) ;
1040+ } ) ;
1041+ } ) ;
1042+
9141043suite ( 'StatementSync.prototype[Symbol.dispose]()' , ( ) => {
9151044 test ( 'finalizes an open statement' , ( t ) => {
9161045 using db = new DatabaseSync ( ':memory:' ) ;
0 commit comments