1
1
import 'dart:js_interop' ;
2
+ import 'dart:js_interop_unsafe' ;
2
3
4
+ import 'package:meta/meta.dart' ;
3
5
import 'package:mutex/mutex.dart' ;
4
6
import 'package:sqlite3/wasm.dart' ;
5
7
import 'package:sqlite3_web/sqlite3_web.dart' ;
8
+ import 'package:sqlite3_web/protocol_utils.dart' as proto;
9
+
6
10
import 'throttled_common_database.dart' ;
7
11
8
12
import '../protocol.dart' ;
@@ -12,9 +16,9 @@ import '../protocol.dart';
12
16
/// can be extended to perform custom requests.
13
17
base class AsyncSqliteController extends DatabaseController {
14
18
@override
15
- Future <WorkerDatabase > openDatabase (
16
- WasmSqlite3 sqlite3, String path, String vfs ) async {
17
- final db = sqlite3. open ( path, vfs: vfs );
19
+ Future <WorkerDatabase > openDatabase (WasmSqlite3 sqlite3, String path,
20
+ String vfs, JSAny ? additionalData ) async {
21
+ final db = openUnderlying (sqlite3, path, vfs, additionalData );
18
22
19
23
// Register any custom functions here if needed
20
24
@@ -23,6 +27,18 @@ base class AsyncSqliteController extends DatabaseController {
23
27
return AsyncSqliteDatabase (database: throttled);
24
28
}
25
29
30
+ /// Opens a database with the `sqlite3` package that will be wrapped in a
31
+ /// [ThrottledCommonDatabase] for [openDatabase] .
32
+ @visibleForOverriding
33
+ CommonDatabase openUnderlying (
34
+ WasmSqlite3 sqlite3,
35
+ String path,
36
+ String vfs,
37
+ JSAny ? additionalData,
38
+ ) {
39
+ return sqlite3.open (path, vfs: vfs);
40
+ }
41
+
26
42
@override
27
43
Future <JSAny ?> handleCustomRequest (
28
44
ClientConnection connection, JSAny ? request) {
@@ -61,25 +77,32 @@ class AsyncSqliteDatabase extends WorkerDatabase {
61
77
return database.autocommit.toJS;
62
78
case CustomDatabaseMessageKind .executeInTransaction:
63
79
final sql = message.rawSql.toDart;
64
- final parameters = [
65
- for ( final raw in (message.rawParameters).toDart) raw. dartify ()
66
- ] ;
80
+ final hasTypeInfo = message.typeInfo.isDefinedAndNotNull;
81
+ final parameters = proto. deserializeParameters (
82
+ message.rawParameters, message.typeInfo) ;
67
83
if (database.autocommit) {
68
84
throw SqliteException (0 ,
69
85
"Transaction rolled back by earlier statement. Cannot execute: $sql " );
70
86
}
71
- var res = database.select (sql, parameters);
72
87
73
- var dartMap = resultSetToMap (res);
74
-
75
- var jsObject = dartMap.jsify ();
88
+ var res = database.select (sql, parameters);
89
+ if (hasTypeInfo) {
90
+ // If the client is sending a request that has parameters with type
91
+ // information, it will also support a newer serialization format for
92
+ // result sets.
93
+ return JSObject ()
94
+ ..['format' ] = 2. toJS
95
+ ..['r' ] = proto.serializeResultSet (res);
96
+ } else {
97
+ var dartMap = resultSetToMap (res);
98
+ var jsObject = dartMap.jsify ();
99
+ return jsObject;
100
+ }
76
101
77
- return jsObject;
78
102
case CustomDatabaseMessageKind .executeBatchInTransaction:
79
103
final sql = message.rawSql.toDart;
80
- final parameters = [
81
- for (final raw in (message.rawParameters).toDart) raw.dartify ()
82
- ];
104
+ final parameters = proto.deserializeParameters (
105
+ message.rawParameters, message.typeInfo);
83
106
if (database.autocommit) {
84
107
throw SqliteException (0 ,
85
108
"Transaction rolled back by earlier statement. Cannot execute: $sql " );
0 commit comments