@@ -124,6 +124,8 @@ export function isComplete(statement: string): boolean {
124
124
return Boolean ( sqlite3_complete ( toCString ( statement ) ) ) ;
125
125
}
126
126
127
+ const BIG_MAX = BigInt ( Number . MAX_SAFE_INTEGER ) ;
128
+
127
129
/**
128
130
* Represents a SQLite3 database connection.
129
131
*
@@ -231,9 +233,9 @@ export class Database {
231
233
}
232
234
}
233
235
234
- const pHandle = new Uint32Array ( 2 ) ;
236
+ const pHandle = new BigUint64Array ( 1 ) ;
235
237
const result = sqlite3_open_v2 ( toCString ( this . #path) , pHandle , flags , null ) ;
236
- this . #handle = Deno . UnsafePointer . create ( pHandle [ 0 ] + 2 ** 32 * pHandle [ 1 ] ) ;
238
+ this . #handle = Deno . UnsafePointer . create ( pHandle [ 0 ] ) ;
237
239
if ( result !== 0 ) sqlite3_close_v2 ( this . #handle) ;
238
240
unwrap ( result ) ;
239
241
@@ -314,15 +316,15 @@ export class Database {
314
316
*/
315
317
exec ( sql : string , ...params : RestBindParameters ) : number {
316
318
if ( params . length === 0 ) {
317
- const pErr = new Uint32Array ( 2 ) ;
319
+ const pErr = new BigUint64Array ( 1 ) ;
318
320
sqlite3_exec (
319
321
this . #handle,
320
322
toCString ( sql ) ,
321
323
null ,
322
324
null ,
323
325
new Uint8Array ( pErr . buffer ) ,
324
326
) ;
325
- const errPtr = Deno . UnsafePointer . create ( pErr [ 0 ] + 2 ** 32 * pErr [ 1 ] ) ;
327
+ const errPtr = Deno . UnsafePointer . create ( pErr [ 0 ] ) ;
326
328
if ( errPtr !== null ) {
327
329
const err = readCstr ( errPtr ) ;
328
330
sqlite3_free ( errPtr ) ;
@@ -444,13 +446,19 @@ export class Database {
444
446
const args : any [ ] = [ ] ;
445
447
for ( let i = 0 ; i < nArgs ; i ++ ) {
446
448
const arg = Deno . UnsafePointer . create (
447
- Number ( argptr . getBigUint64 ( i * 8 ) ) ,
449
+ argptr . getBigUint64 ( i * 8 ) ,
448
450
) ;
449
451
const type = sqlite3_value_type ( arg ) ;
450
452
switch ( type ) {
451
- case SQLITE_INTEGER :
452
- args . push ( sqlite3_value_int64 ( arg ) ) ;
453
+ case SQLITE_INTEGER : {
454
+ const value = sqlite3_value_int64 ( arg ) ;
455
+ if ( value < - BIG_MAX || value > BIG_MAX ) {
456
+ args . push ( value ) ;
457
+ } else {
458
+ args . push ( Number ( value ) ) ;
459
+ }
453
460
break ;
461
+ }
454
462
case SQLITE_FLOAT :
455
463
args . push ( sqlite3_value_double ( arg ) ) ;
456
464
break ;
@@ -498,15 +506,16 @@ export class Database {
498
506
} else if ( typeof result === "boolean" ) {
499
507
sqlite3_result_int ( ctx , result ? 1 : 0 ) ;
500
508
} else if ( typeof result === "number" ) {
501
- if ( Number . isSafeInteger ( result ) ) sqlite3_result_int64 ( ctx , result ) ;
502
- else sqlite3_result_double ( ctx , result ) ;
509
+ if ( Number . isSafeInteger ( result ) ) {
510
+ sqlite3_result_int64 ( ctx , BigInt ( result ) ) ;
511
+ } else sqlite3_result_double ( ctx , result ) ;
503
512
} else if ( typeof result === "bigint" ) {
504
513
sqlite3_result_int64 ( ctx , result ) ;
505
514
} else if ( typeof result === "string" ) {
506
515
const buffer = new TextEncoder ( ) . encode ( result ) ;
507
- sqlite3_result_text ( ctx , buffer , buffer . byteLength , 0 ) ;
516
+ sqlite3_result_text ( ctx , buffer , buffer . byteLength , 0n ) ;
508
517
} else if ( result instanceof Uint8Array ) {
509
- sqlite3_result_blob ( ctx , result , result . length , - 1 ) ;
518
+ sqlite3_result_blob ( ctx , result , result . length , - 1n ) ;
510
519
} else {
511
520
const buffer = new TextEncoder ( ) . encode (
512
521
`Invalid return value: ${ Deno . inspect ( result ) } ` ,
@@ -585,13 +594,19 @@ export class Database {
585
594
const args : any [ ] = [ ] ;
586
595
for ( let i = 0 ; i < nArgs ; i ++ ) {
587
596
const arg = Deno . UnsafePointer . create (
588
- Number ( argptr . getBigUint64 ( i * 8 ) ) ,
597
+ argptr . getBigUint64 ( i * 8 ) ,
589
598
) ;
590
599
const type = sqlite3_value_type ( arg ) ;
591
600
switch ( type ) {
592
- case SQLITE_INTEGER :
593
- args . push ( sqlite3_value_int64 ( arg ) ) ;
601
+ case SQLITE_INTEGER : {
602
+ const value = sqlite3_value_int64 ( arg ) ;
603
+ if ( value < - BIG_MAX || value > BIG_MAX ) {
604
+ args . push ( value ) ;
605
+ } else {
606
+ args . push ( Number ( value ) ) ;
607
+ }
594
608
break ;
609
+ }
595
610
case SQLITE_FLOAT :
596
611
args . push ( sqlite3_value_double ( arg ) ) ;
597
612
break ;
@@ -662,15 +677,16 @@ export class Database {
662
677
} else if ( typeof result === "boolean" ) {
663
678
sqlite3_result_int ( ctx , result ? 1 : 0 ) ;
664
679
} else if ( typeof result === "number" ) {
665
- if ( Number . isSafeInteger ( result ) ) sqlite3_result_int64 ( ctx , result ) ;
666
- else sqlite3_result_double ( ctx , result ) ;
680
+ if ( Number . isSafeInteger ( result ) ) {
681
+ sqlite3_result_int64 ( ctx , BigInt ( result ) ) ;
682
+ } else sqlite3_result_double ( ctx , result ) ;
667
683
} else if ( typeof result === "bigint" ) {
668
684
sqlite3_result_int64 ( ctx , result ) ;
669
685
} else if ( typeof result === "string" ) {
670
686
const buffer = new TextEncoder ( ) . encode ( result ) ;
671
- sqlite3_result_text ( ctx , buffer , buffer . byteLength , 0 ) ;
687
+ sqlite3_result_text ( ctx , buffer , buffer . byteLength , 0n ) ;
672
688
} else if ( result instanceof Uint8Array ) {
673
- sqlite3_result_blob ( ctx , result , result . length , - 1 ) ;
689
+ sqlite3_result_blob ( ctx , result , result . length , - 1n ) ;
674
690
} else {
675
691
const buffer = new TextEncoder ( ) . encode (
676
692
`Invalid return value: ${ Deno . inspect ( result ) } ` ,
@@ -729,7 +745,7 @@ export class Database {
729
745
throw new Error ( "Extension loading is not enabled" ) ;
730
746
}
731
747
732
- const pzErrMsg = new Uint32Array ( 2 ) ;
748
+ const pzErrMsg = new BigUint64Array ( 1 ) ;
733
749
734
750
const result = sqlite3_load_extension (
735
751
this . #handle,
@@ -739,7 +755,7 @@ export class Database {
739
755
) ;
740
756
741
757
const pzErrPtr = Deno . UnsafePointer . create (
742
- pzErrMsg [ 0 ] + 2 ** 32 * pzErrMsg [ 1 ] ,
758
+ pzErrMsg [ 0 ] ,
743
759
) ;
744
760
if ( pzErrPtr !== null ) {
745
761
const pzErr = readCstr ( pzErrPtr ) ;
0 commit comments