@@ -13,18 +13,6 @@ pub type Stats {
13
13
Stats ( used : Int , highwater : Int )
14
14
}
15
15
16
- pub type StatusInfo {
17
- StatusInfo (
18
- memory_used : Stats ,
19
- pagecache_used : Stats ,
20
- pagecache_overflow : Stats ,
21
- malloc_size : Stats ,
22
- parser_stack : Stats ,
23
- pagecache_size : Stats ,
24
- malloc_count : Stats ,
25
- )
26
- }
27
-
28
16
pub type Error {
29
17
SqlightError (
30
18
code : ErrorCode ,
@@ -125,8 +113,6 @@ pub type ErrorCode {
125
113
IoerrRdlock
126
114
}
127
115
128
- external type Charlist
129
-
130
116
/// Convert an `Error` to an error code int.
131
117
///
132
118
/// See the SQLite documentation for the full list of error codes.
@@ -309,19 +295,21 @@ pub fn error_code_from_int(code: Int) -> ErrorCode {
309
295
}
310
296
}
311
297
312
- external type NilResult
313
-
314
- external fn open_ ( Charlist ) -> Result ( Connection , Int ) =
315
- "esqlite3" "open"
298
+ if erlang {
299
+ external fn open_ ( String ) -> Result ( Connection , Error ) =
300
+ "sqlight_ffi" "open"
316
301
317
- external fn normalise_result ( NilResult ) -> Result ( Nil , Int ) =
318
- "sqlight_ffi" "normalise_result"
302
+ external fn close_ ( Connection ) -> Result ( Nil , Error ) =
303
+ "sqlight_ffi" "close"
304
+ }
319
305
320
- external fn string_to_charlist ( String ) -> Charlist =
321
- "unicode" "characters_to_list"
306
+ if javascript {
307
+ external fn open_ ( String ) -> Result ( Connection , Error ) =
308
+ "./sqlight_ffi.js" "open"
322
309
323
- external fn close_ ( Connection ) -> NilResult =
324
- "esqlite3" "close"
310
+ external fn close_ ( Connection ) -> Result ( Nil , Error ) =
311
+ "./sqlight_ffi.js" "close"
312
+ }
325
313
326
314
/// Open a connection to a SQLite database.
327
315
///
@@ -350,12 +338,7 @@ external fn close_(Connection) -> NilResult =
350
338
/// ```
351
339
///
352
340
pub fn open ( path : String ) -> Result ( Connection , Error ) {
353
- path
354
- |> string_to_charlist
355
- |> open_
356
- |> result . map_error ( fn ( i ) {
357
- SqlightError ( code : error_code_from_int ( i ) , message : "" , offset : - 1)
358
- } )
341
+ open_ ( path )
359
342
}
360
343
361
344
/// Close a connection to a SQLite database.
@@ -365,10 +348,7 @@ pub fn open(path: String) -> Result(Connection, Error) {
365
348
/// information: <https://www.sqlite.org/c3ref/close.html>.
366
349
///
367
350
pub fn close ( connection : Connection ) -> Result ( Nil , Error ) {
368
- connection
369
- |> close_
370
- |> normalise_result
371
- |> result . map_error ( construct_error ( connection , _) )
351
+ close_ ( connection )
372
352
}
373
353
374
354
/// Open a connection to a SQLite database and execute a function with it, then
@@ -395,14 +375,8 @@ pub fn with_connection(path: String, f: fn(Connection) -> a) -> a {
395
375
value
396
376
}
397
377
398
- /// Get all internal status information for SQLite.
399
- ///
400
- pub external fn status ( ) -> StatusInfo =
401
- "sqlight_ffi" "status"
402
-
403
378
pub fn exec ( sql : String , on connection : Connection ) -> Result ( Nil , Error ) {
404
- run_exec ( connection , sql )
405
- |> result . map_error ( construct_error ( connection , _) )
379
+ exec_ ( sql , connection )
406
380
}
407
381
408
382
pub fn query (
@@ -411,11 +385,7 @@ pub fn query(
411
385
with arguments : List ( Value ) ,
412
386
expecting decoder : Decoder ( t) ,
413
387
) -> Result ( List ( t) , Error ) {
414
- use rows <-
415
- result . then (
416
- run_query ( connection , sql , arguments )
417
- |> result . map_error ( construct_error ( connection , _) ) ,
418
- )
388
+ use rows <- result . then ( run_query ( connection , sql , arguments ) )
419
389
use rows <-
420
390
result . then (
421
391
list . try_map ( over : rows , with : decoder )
@@ -424,39 +394,81 @@ pub fn query(
424
394
Ok ( rows )
425
395
}
426
396
427
- external fn run_query (
428
- Connection ,
429
- String ,
430
- List ( Value ) ,
431
- ) -> Result ( List ( Dynamic ) , Int ) =
432
- "sqlight_ffi" "query"
397
+ if erlang {
398
+ external fn run_query (
399
+ Connection ,
400
+ String ,
401
+ List ( Value ) ,
402
+ ) -> Result ( List ( Dynamic ) , Error ) =
403
+ "sqlight_ffi" "query"
404
+ }
405
+
406
+ if javascript {
407
+ external fn run_query (
408
+ Connection ,
409
+ String ,
410
+ List ( Value ) ,
411
+ ) -> Result ( List ( Dynamic ) , Error ) =
412
+ "./sqlight_ffi.js" "query"
413
+ }
414
+
415
+ if erlang {
416
+ external fn coerce_value ( a) -> Value =
417
+ "sqlight_ffi" "coerce_value"
418
+ }
433
419
434
- external fn run_exec ( Connection , String ) -> Result ( Nil , Int ) =
435
- "sqlight_ffi" "exec"
420
+ if javascript {
421
+ external fn coerce_value ( a) -> Value =
422
+ "./sqlight_ffi.js" "coerce_value"
423
+ }
424
+
425
+ if erlang {
426
+ external fn exec_ ( String , Connection ) -> Result ( Nil , Error ) =
427
+ "sqlight_ffi" "exec"
428
+ }
429
+
430
+ if javascript {
431
+ external fn exec_ ( String , Connection ) -> Result ( Nil , Error ) =
432
+ "./sqlight_ffi.js" "exec"
433
+ }
436
434
437
435
/// Convert a Gleam `Int` to an SQLite int, to be used an argument to a
438
436
/// query.
439
437
///
440
- pub external fn int ( Int ) -> Value =
441
- "sqlight_ffi" "coerce_value"
438
+ pub fn int ( value : Int ) -> Value {
439
+ coerce_value ( value )
440
+ }
442
441
443
442
/// Convert a Gleam `Float` to an SQLite float, to be used an argument to a
444
443
/// query.
445
444
///
446
- pub external fn float ( Float ) -> Value =
447
- "sqlight_ffi" "coerce_value"
445
+ pub fn float ( value : Float ) -> Value {
446
+ coerce_value ( value )
447
+ }
448
448
449
449
/// Convert a Gleam `String` to an SQLite text, to be used an argument to a
450
450
/// query.
451
451
///
452
- pub external fn text ( String ) -> Value =
453
- "sqlight_ffi" "coerce_value"
452
+ pub fn text ( value : String ) -> Value {
453
+ coerce_value ( value )
454
+ }
454
455
455
456
/// Convert a Gleam `BitString` to an SQLite blob, to be used an argument to a
456
457
/// query.
457
458
///
458
- pub external fn blob ( BitString ) -> Value =
459
- "sqlight_ffi" "coerce_value"
459
+ pub fn blob ( value : BitString ) -> Value {
460
+ coerce_blob ( value )
461
+ }
462
+
463
+ if erlang {
464
+ external fn coerce_blob ( BitString ) -> Value =
465
+ "sqlight_ffi" "coerce_value"
466
+ }
467
+
468
+ if javascript {
469
+ external fn coerce_blob ( BitString ) -> Value =
470
+ "./sqlight_ffi.js" "coerce_blob"
471
+ }
460
472
461
473
/// Convert a Gleam `Bool` to an SQLite int, to be used an argument to a
462
474
/// query.
@@ -473,10 +485,19 @@ pub fn bool(value: Bool) -> Value {
473
485
} )
474
486
}
475
487
476
- /// Construct an SQLite null, to be used an argument to a query.
477
- ///
478
- pub external fn null ( ) -> Value =
479
- "sqlight_ffi" "null"
488
+ if erlang {
489
+ /// Construct an SQLite null, to be used an argument to a query.
490
+ ///
491
+ pub external fn null ( ) -> Value =
492
+ "sqlight_ffi" "null"
493
+ }
494
+
495
+ if javascript {
496
+ /// Construct an SQLite null, to be used an argument to a query.
497
+ ///
498
+ pub external fn null ( ) -> Value =
499
+ "./sqlight_ffi.js" "null_"
500
+ }
480
501
481
502
pub fn decode_bool ( value : Dynamic ) -> Result ( Bool , List ( dynamic . DecodeError ) ) {
482
503
case dynamic . int ( value ) {
@@ -486,20 +507,6 @@ pub fn decode_bool(value: Dynamic) -> Result(Bool, List(dynamic.DecodeError)) {
486
507
}
487
508
}
488
509
489
- /// Return a description of the last occurred error.
490
- ///
491
- external fn error_info ( Connection ) -> # ( String , Int ) =
492
- "sqlight_ffi" "error_info"
493
-
494
- fn construct_error ( connection : Connection , error_code : Int ) -> Error {
495
- let # ( message , offset ) = error_info ( connection )
496
- SqlightError (
497
- code : error_code_from_int ( error_code ) ,
498
- message : message ,
499
- offset : offset ,
500
- )
501
- }
502
-
503
510
fn decode_error ( errors : List ( dynamic . DecodeError ) ) -> Error {
504
511
assert [ dynamic . DecodeError ( expected , actual , path ) , .. ] = errors
505
512
let path = string . join ( path , "." )
0 commit comments