@@ -3,7 +3,7 @@ use neon::types::JsPromise;
3
3
use neon:: { prelude:: * , types:: JsBigInt } ;
4
4
use once_cell:: sync:: OnceCell ;
5
5
use std:: cell:: RefCell ;
6
- use std:: sync:: { Arc , Weak } ;
6
+ use std:: sync:: Arc ;
7
7
use tokio:: { runtime:: Runtime , sync:: Mutex } ;
8
8
use tracing:: trace;
9
9
@@ -18,7 +18,6 @@ fn runtime<'a, C: Context<'a>>(cx: &mut C) -> NeonResult<&'static Runtime> {
18
18
struct Database {
19
19
db : Arc < Mutex < libsql:: Database > > ,
20
20
conn : RefCell < Option < Arc < Mutex < libsql:: Connection > > > > ,
21
- stmts : Arc < Mutex < Vec < Arc < Mutex < libsql:: Statement > > > > > ,
22
21
default_safe_integers : RefCell < bool > ,
23
22
}
24
23
@@ -29,7 +28,6 @@ impl Database {
29
28
Database {
30
29
db : Arc :: new ( Mutex :: new ( db) ) ,
31
30
conn : RefCell :: new ( Some ( Arc :: new ( Mutex :: new ( conn) ) ) ) ,
32
- stmts : Arc :: new ( Mutex :: new ( vec ! [ ] ) ) ,
33
31
default_safe_integers : RefCell :: new ( false ) ,
34
32
}
35
33
}
@@ -91,15 +89,11 @@ impl Database {
91
89
}
92
90
93
91
fn js_close ( mut cx : FunctionContext ) -> JsResult < JsUndefined > {
92
+ // the conn will be closed when the last statement in discarded. In most situation that
93
+ // means immediately because you don't want to hold on a statement for longer that its
94
+ // database is alive.
94
95
trace ! ( "Closing database" ) ;
95
96
let db: Handle < ' _ , JsBox < Database > > = cx. this ( ) ?;
96
- for stmt in db. stmts . blocking_lock ( ) . iter ( ) {
97
- let mut stmt = stmt. blocking_lock ( ) ;
98
- stmt. finalize ( ) ;
99
- }
100
- db. stmts . blocking_lock ( ) . clear ( ) ;
101
- let conn = db. get_conn ( ) ;
102
- conn. blocking_lock ( ) . close ( ) ;
103
97
db. conn . replace ( None ) ;
104
98
Ok ( cx. undefined ( ) )
105
99
}
@@ -185,13 +179,9 @@ impl Database {
185
179
let result = rt. block_on ( async { conn. lock ( ) . await . prepare ( & sql) . await } ) ;
186
180
let stmt = result. or_else ( |err| throw_libsql_error ( & mut cx, err) ) ?;
187
181
let stmt = Arc :: new ( Mutex :: new ( stmt) ) ;
188
- {
189
- let mut stmts = db. stmts . blocking_lock ( ) ;
190
- stmts. push ( stmt. clone ( ) ) ;
191
- }
192
182
let stmt = Statement {
193
- conn : Arc :: downgrade ( & conn) ,
194
- stmt : Arc :: downgrade ( & stmt ) ,
183
+ conn : conn. clone ( ) ,
184
+ stmt,
195
185
raw : RefCell :: new ( false ) ,
196
186
safe_ints : RefCell :: new ( * db. default_safe_integers . borrow ( ) ) ,
197
187
} ;
@@ -207,18 +197,13 @@ impl Database {
207
197
let safe_ints = * db. default_safe_integers . borrow ( ) ;
208
198
let rt = runtime ( & mut cx) ?;
209
199
let conn = db. get_conn ( ) ;
210
- let stmts = db. stmts . clone ( ) ;
211
200
rt. spawn ( async move {
212
201
match conn. lock ( ) . await . prepare ( & sql) . await {
213
202
Ok ( stmt) => {
214
203
let stmt = Arc :: new ( Mutex :: new ( stmt) ) ;
215
- {
216
- let mut stmts = stmts. lock ( ) . await ;
217
- stmts. push ( stmt. clone ( ) ) ;
218
- }
219
204
let stmt = Statement {
220
- conn : Arc :: downgrade ( & conn) ,
221
- stmt : Arc :: downgrade ( & stmt ) ,
205
+ conn : conn. clone ( ) ,
206
+ stmt,
222
207
raw : RefCell :: new ( false ) ,
223
208
safe_ints : RefCell :: new ( safe_ints) ,
224
209
} ;
@@ -371,8 +356,8 @@ pub fn convert_sqlite_code(code: i32) -> String {
371
356
}
372
357
}
373
358
struct Statement {
374
- conn : Weak < Mutex < libsql:: Connection > > ,
375
- stmt : Weak < Mutex < libsql:: Statement > > ,
359
+ conn : Arc < Mutex < libsql:: Connection > > ,
360
+ stmt : Arc < Mutex < libsql:: Statement > > ,
376
361
raw : RefCell < bool > ,
377
362
safe_ints : RefCell < bool > ,
378
363
}
@@ -415,8 +400,7 @@ fn js_value_to_value(
415
400
impl Statement {
416
401
fn js_raw ( mut cx : FunctionContext ) -> JsResult < JsNull > {
417
402
let stmt: Handle < ' _ , JsBox < Statement > > = cx. this ( ) ?;
418
- let raw_stmt = stmt. stmt . upgrade ( ) . unwrap ( ) ;
419
- let raw_stmt = raw_stmt. blocking_lock ( ) ;
403
+ let raw_stmt = stmt. stmt . blocking_lock ( ) ;
420
404
if raw_stmt. columns ( ) . is_empty ( ) {
421
405
return cx. throw_error ( "The raw() method is only for statements that return data" ) ;
422
406
}
@@ -434,14 +418,13 @@ impl Statement {
434
418
let stmt: Handle < ' _ , JsBox < Statement > > = cx. this ( ) ?;
435
419
let params = cx. argument :: < JsValue > ( 0 ) ?;
436
420
let params = convert_params ( & mut cx, & stmt, params) ?;
437
- let raw_stmt = stmt. stmt . upgrade ( ) . unwrap ( ) ;
438
- let mut raw_stmt = raw_stmt. blocking_lock ( ) ;
421
+ let mut raw_stmt = stmt. stmt . blocking_lock ( ) ;
439
422
raw_stmt. reset ( ) ;
440
423
let fut = raw_stmt. execute ( params) ;
441
424
let rt = runtime ( & mut cx) ?;
442
425
let result = rt. block_on ( fut) ;
443
426
let changes = result. or_else ( |err| throw_libsql_error ( & mut cx, err) ) ?;
444
- let raw_conn = stmt. conn . upgrade ( ) . unwrap ( ) ;
427
+ let raw_conn = stmt. conn . clone ( ) ;
445
428
let last_insert_rowid = raw_conn. blocking_lock ( ) . last_insert_rowid ( ) ;
446
429
let info = cx. empty_object ( ) ;
447
430
let changes = cx. number ( changes as f64 ) ;
@@ -456,8 +439,7 @@ impl Statement {
456
439
let params = cx. argument :: < JsValue > ( 0 ) ?;
457
440
let params = convert_params ( & mut cx, & stmt, params) ?;
458
441
let safe_ints = * stmt. safe_ints . borrow ( ) ;
459
- let raw_stmt = stmt. stmt . upgrade ( ) . unwrap ( ) ;
460
- let mut raw_stmt = raw_stmt. blocking_lock ( ) ;
442
+ let mut raw_stmt = stmt. stmt . blocking_lock ( ) ;
461
443
let fut = raw_stmt. query ( params) ;
462
444
let rt = runtime ( & mut cx) ?;
463
445
let result = rt. block_on ( fut) ;
@@ -488,9 +470,8 @@ impl Statement {
488
470
let params = cx. argument :: < JsValue > ( 0 ) ?;
489
471
let params = convert_params ( & mut cx, & stmt, params) ?;
490
472
let rt = runtime ( & mut cx) ?;
491
- let raw_stmt = stmt. stmt . upgrade ( ) . unwrap ( ) ;
492
473
let result = rt. block_on ( async move {
493
- let mut raw_stmt = raw_stmt . lock ( ) . await ;
474
+ let mut raw_stmt = stmt . stmt . lock ( ) . await ;
494
475
raw_stmt. reset ( ) ;
495
476
raw_stmt. query ( params) . await
496
477
} ) ;
@@ -507,16 +488,16 @@ impl Statement {
507
488
let stmt: Handle < ' _ , JsBox < Statement > > = cx. this ( ) ?;
508
489
let params = cx. argument :: < JsValue > ( 0 ) ?;
509
490
let params = convert_params ( & mut cx, & stmt, params) ?;
510
- let raw_stmt = stmt. stmt . upgrade ( ) . unwrap ( ) ;
511
491
{
512
- let mut raw_stmt = raw_stmt . blocking_lock ( ) ;
492
+ let mut raw_stmt = stmt . stmt . blocking_lock ( ) ;
513
493
raw_stmt. reset ( ) ;
514
494
}
515
495
let ( deferred, promise) = cx. promise ( ) ;
516
496
let channel = cx. channel ( ) ;
517
497
let rt = runtime ( & mut cx) ?;
518
498
let raw = * stmt. raw . borrow ( ) ;
519
499
let safe_ints = * stmt. safe_ints . borrow ( ) ;
500
+ let raw_stmt = stmt. stmt . clone ( ) ;
520
501
rt. spawn ( async move {
521
502
let result = {
522
503
let mut raw_stmt = raw_stmt. lock ( ) . await ;
@@ -547,8 +528,7 @@ impl Statement {
547
528
fn js_columns ( mut cx : FunctionContext ) -> JsResult < JsValue > {
548
529
let stmt: Handle < ' _ , JsBox < Statement > > = cx. this ( ) ?;
549
530
let result = cx. empty_array ( ) ;
550
- let stmt = stmt. stmt . upgrade ( ) . unwrap ( ) ;
551
- let raw_stmt = stmt. blocking_lock ( ) ;
531
+ let raw_stmt = stmt. stmt . blocking_lock ( ) ;
552
532
for ( i, col) in raw_stmt. columns ( ) . iter ( ) . enumerate ( ) {
553
533
let column = cx. empty_object ( ) ;
554
534
let column_name = cx. string ( col. name ( ) ) ;
@@ -665,7 +645,7 @@ fn convert_params_object(
665
645
v : Handle < ' _ , JsObject > ,
666
646
) -> NeonResult < libsql:: params:: Params > {
667
647
let mut params = vec ! [ ] ;
668
- let stmt = stmt. stmt . upgrade ( ) . unwrap ( ) ;
648
+ let stmt = & stmt. stmt ;
669
649
let raw_stmt = stmt. blocking_lock ( ) ;
670
650
for idx in 0 ..raw_stmt. parameter_count ( ) {
671
651
let name = raw_stmt. parameter_name ( ( idx + 1 ) as i32 ) . unwrap ( ) ;
0 commit comments