sqlmath 2022.4.28 → 2022.8.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.npmignore +1 -0
- package/CHANGELOG.md +83 -1
- package/LICENSE +2 -2
- package/README.md +25 -3
- package/_binary_sqlmath_napi8_darwin_x64.node +0 -0
- package/_binary_sqlmath_napi8_linux_x64.node +0 -0
- package/_binary_sqlmath_napi8_win32_x64.node +0 -0
- package/_binary_sqlmath_shell_darwin_x64 +0 -0
- package/_binary_sqlmath_shell_linux_x64 +0 -0
- package/_binary_sqlmath_shell_win32_x64.exe +0 -0
- package/jslint.mjs +477 -155
- package/package.json +4 -4
- package/sqlmath.mjs +836 -1702
package/sqlmath.mjs
CHANGED
|
@@ -1,17 +1,112 @@
|
|
|
1
|
+
// MIT License
|
|
2
|
+
//
|
|
3
|
+
// Copyright (c) 2021 Kai Zhu
|
|
4
|
+
//
|
|
5
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
// in the Software without restriction, including without limitation the rights
|
|
8
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
// furnished to do so, subject to the following conditions:
|
|
11
|
+
//
|
|
12
|
+
// The above copyright notice and this permission notice shall be included in
|
|
13
|
+
// all copies or substantial portions of the Software.
|
|
14
|
+
//
|
|
15
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
// SOFTWARE.
|
|
22
|
+
|
|
23
|
+
|
|
1
24
|
/*jslint beta, bitwise, name, node*/
|
|
25
|
+
/*global FinalizationRegistry*/
|
|
2
26
|
"use strict";
|
|
3
|
-
import {createRequire} from "module";
|
|
4
|
-
import jslint from "./jslint.mjs";
|
|
5
27
|
|
|
6
|
-
let
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
let
|
|
28
|
+
let FILENAME_DBTMP = "/tmp/__dbtmp1";
|
|
29
|
+
let IS_BROWSER;
|
|
30
|
+
let JSBATON_ARGC = 16;
|
|
31
|
+
let SQLITE_DATATYPE_BLOB = 0x04;
|
|
32
|
+
// let SQLITE_DATATYPE_BLOB_0 = 0x14;
|
|
33
|
+
let SQLITE_DATATYPE_EXTERNALBUFFER = -0x01;
|
|
34
|
+
let SQLITE_DATATYPE_FLOAT = 0x02;
|
|
35
|
+
// let SQLITE_DATATYPE_FLOAT_0 = 0x12;
|
|
36
|
+
let SQLITE_DATATYPE_INTEGER = 0x01;
|
|
37
|
+
let SQLITE_DATATYPE_INTEGER_0 = 0x11;
|
|
38
|
+
let SQLITE_DATATYPE_INTEGER_1 = 0x21;
|
|
39
|
+
let SQLITE_DATATYPE_NULL = 0x05;
|
|
40
|
+
let SQLITE_DATATYPE_OFFSET = 768;
|
|
41
|
+
let SQLITE_DATATYPE_TEXT = 0x03;
|
|
42
|
+
let SQLITE_DATATYPE_TEXT_0 = 0x13;
|
|
43
|
+
let SQLITE_MAX_LENGTH2 = 1_000_000_000;
|
|
44
|
+
let SQLITE_OPEN_AUTOPROXY = 0x00000020; /* VFS only */
|
|
45
|
+
let SQLITE_OPEN_CREATE = 0x00000004; /* Ok for sqlite3_open_v2() */
|
|
46
|
+
let SQLITE_OPEN_DELETEONCLOSE = 0x00000008; /* VFS only */
|
|
47
|
+
let SQLITE_OPEN_EXCLUSIVE = 0x00000010; /* VFS only */
|
|
48
|
+
let SQLITE_OPEN_FULLMUTEX = 0x00010000; /* Ok for sqlite3_open_v2() */
|
|
49
|
+
let SQLITE_OPEN_MAIN_DB = 0x00000100; /* VFS only */
|
|
50
|
+
let SQLITE_OPEN_MAIN_JOURNAL = 0x00000800; /* VFS only */
|
|
51
|
+
let SQLITE_OPEN_MEMORY = 0x00000080; /* Ok for sqlite3_open_v2() */
|
|
52
|
+
let SQLITE_OPEN_NOFOLLOW = 0x01000000; /* Ok for sqlite3_open_v2() */
|
|
53
|
+
let SQLITE_OPEN_NOMUTEX = 0x00008000; /* Ok for sqlite3_open_v2() */
|
|
54
|
+
let SQLITE_OPEN_PRIVATECACHE = 0x00040000; /* Ok for sqlite3_open_v2() */
|
|
55
|
+
let SQLITE_OPEN_READONLY = 0x00000001; /* Ok for sqlite3_open_v2() */
|
|
56
|
+
let SQLITE_OPEN_READWRITE = 0x00000002; /* Ok for sqlite3_open_v2() */
|
|
57
|
+
let SQLITE_OPEN_SHAREDCACHE = 0x00020000; /* Ok for sqlite3_open_v2() */
|
|
58
|
+
let SQLITE_OPEN_SUBJOURNAL = 0x00002000; /* VFS only */
|
|
59
|
+
let SQLITE_OPEN_SUPER_JOURNAL = 0x00004000; /* VFS only */
|
|
60
|
+
let SQLITE_OPEN_TEMP_DB = 0x00000200; /* VFS only */
|
|
61
|
+
let SQLITE_OPEN_TEMP_JOURNAL = 0x00001000; /* VFS only */
|
|
62
|
+
let SQLITE_OPEN_TRANSIENT_DB = 0x00000400; /* VFS only */
|
|
63
|
+
let SQLITE_OPEN_URI = 0x00000040; /* Ok for sqlite3_open_v2() */
|
|
64
|
+
let SQLITE_OPEN_WAL = 0x00080000; /* VFS only */
|
|
65
|
+
let cModule;
|
|
66
|
+
let consoleError = console.error;
|
|
67
|
+
let dbDict = new WeakMap(); // private-dict of sqlite-database-connections
|
|
68
|
+
let dbFinalizationRegistry;
|
|
69
|
+
// init debugInline
|
|
70
|
+
let debugInline = (function () {
|
|
71
|
+
let __consoleError = function () {
|
|
72
|
+
return;
|
|
73
|
+
};
|
|
74
|
+
function debug(...argv) {
|
|
75
|
+
|
|
76
|
+
// This function will print <argv> to stderr and then return <argv>[0].
|
|
77
|
+
|
|
78
|
+
__consoleError("\n\ndebugInline");
|
|
79
|
+
__consoleError(...argv);
|
|
80
|
+
__consoleError("\n");
|
|
81
|
+
return argv[0];
|
|
82
|
+
}
|
|
83
|
+
debug(); // Coverage-hack.
|
|
84
|
+
__consoleError = console.error; //jslint-ignore-line
|
|
85
|
+
return debug;
|
|
86
|
+
}());
|
|
87
|
+
let sqlMessageDict = {}; // dict of web-worker-callbacks
|
|
88
|
+
let sqlMessageId = 0;
|
|
89
|
+
let sqlWorker;
|
|
90
|
+
|
|
91
|
+
function assertJsonEqual(aa, bb, message) {
|
|
92
|
+
|
|
93
|
+
// This function will assert JSON.stringify(<aa>) === JSON.stringify(<bb>).
|
|
94
|
+
|
|
95
|
+
aa = JSON.stringify(objectDeepCopyWithKeysSorted(aa), undefined, 1);
|
|
96
|
+
bb = JSON.stringify(objectDeepCopyWithKeysSorted(bb), undefined, 1);
|
|
97
|
+
if (aa !== bb) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
"\n" + aa + "\n!==\n" + bb
|
|
100
|
+
+ (
|
|
101
|
+
typeof message === "string"
|
|
102
|
+
? " - " + message
|
|
103
|
+
: message
|
|
104
|
+
? " - " + JSON.stringify(message)
|
|
105
|
+
: ""
|
|
106
|
+
)
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
15
110
|
|
|
16
111
|
function assertNumericalEqual(aa, bb, message) {
|
|
17
112
|
|
|
@@ -29,274 +124,324 @@ function assertNumericalEqual(aa, bb, message) {
|
|
|
29
124
|
}
|
|
30
125
|
}
|
|
31
126
|
|
|
127
|
+
function assertOrThrow(condition, message) {
|
|
32
128
|
|
|
33
|
-
|
|
34
|
-
file sqlmath.js
|
|
35
|
-
*/
|
|
36
|
-
(function () {
|
|
37
|
-
let JSBATON_ARGC = 16;
|
|
38
|
-
// let SIZEOF_MESSAGE_DEFAULT = 768;
|
|
39
|
-
let SQLITE_MAX_LENGTH2 = 1000000000;
|
|
40
|
-
let SQLITE_OPEN_AUTOPROXY = 0x00000020; /* VFS only */
|
|
41
|
-
let SQLITE_OPEN_CREATE = 0x00000004; /* Ok for sqlite3_open_v2() */
|
|
42
|
-
let SQLITE_OPEN_DELETEONCLOSE = 0x00000008; /* VFS only */
|
|
43
|
-
let SQLITE_OPEN_EXCLUSIVE = 0x00000010; /* VFS only */
|
|
44
|
-
let SQLITE_OPEN_FULLMUTEX = 0x00010000; /* Ok for sqlite3_open_v2() */
|
|
45
|
-
let SQLITE_OPEN_MAIN_DB = 0x00000100; /* VFS only */
|
|
46
|
-
let SQLITE_OPEN_MAIN_JOURNAL = 0x00000800; /* VFS only */
|
|
47
|
-
let SQLITE_OPEN_MEMORY = 0x00000080; /* Ok for sqlite3_open_v2() */
|
|
48
|
-
let SQLITE_OPEN_NOFOLLOW = 0x01000000; /* Ok for sqlite3_open_v2() */
|
|
49
|
-
let SQLITE_OPEN_NOMUTEX = 0x00008000; /* Ok for sqlite3_open_v2() */
|
|
50
|
-
let SQLITE_OPEN_PRIVATECACHE = 0x00040000; /* Ok for sqlite3_open_v2() */
|
|
51
|
-
let SQLITE_OPEN_READONLY = 0x00000001; /* Ok for sqlite3_open_v2() */
|
|
52
|
-
let SQLITE_OPEN_READWRITE = 0x00000002; /* Ok for sqlite3_open_v2() */
|
|
53
|
-
let SQLITE_OPEN_SHAREDCACHE = 0x00020000; /* Ok for sqlite3_open_v2() */
|
|
54
|
-
let SQLITE_OPEN_SUBJOURNAL = 0x00002000; /* VFS only */
|
|
55
|
-
let SQLITE_OPEN_SUPER_JOURNAL = 0x00004000; /* VFS only */
|
|
56
|
-
let SQLITE_OPEN_TEMP_DB = 0x00000200; /* VFS only */
|
|
57
|
-
let SQLITE_OPEN_TEMP_JOURNAL = 0x00001000; /* VFS only */
|
|
58
|
-
let SQLITE_OPEN_TRANSIENT_DB = 0x00000400; /* VFS only */
|
|
59
|
-
let SQLITE_OPEN_URI = 0x00000040; /* Ok for sqlite3_open_v2() */
|
|
60
|
-
let SQLITE_OPEN_WAL = 0x00080000; /* VFS only */
|
|
61
|
-
let addon;
|
|
62
|
-
let consoleError = console.error;
|
|
63
|
-
let dbDict = new WeakMap(); // private map of sqlite-database-connections
|
|
64
|
-
let requireCjs = createRequire(import.meta.url);
|
|
65
|
-
let testList;
|
|
129
|
+
// This function will throw <message> if <condition> is falsy.
|
|
66
130
|
|
|
67
|
-
|
|
131
|
+
if (!condition) {
|
|
132
|
+
throw (
|
|
133
|
+
(!message || typeof message === "string")
|
|
134
|
+
? new Error(String(message).slice(0, 2048))
|
|
135
|
+
: message
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function cCallAsync(baton, cFuncName, ...argList) {
|
|
68
141
|
// this function will serialize <argList> to a c <baton>,
|
|
69
142
|
// suitable for passing into napi
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
err.stack += errStack;
|
|
116
|
-
throw err;
|
|
143
|
+
let argi = 0;
|
|
144
|
+
let errStack;
|
|
145
|
+
assertOrThrow(
|
|
146
|
+
argList.length < 16,
|
|
147
|
+
"cCallAsync - argList.length must be less than than 16"
|
|
148
|
+
);
|
|
149
|
+
baton = baton || jsbatonCreate();
|
|
150
|
+
// pad argList to length JSBATON_ARGC
|
|
151
|
+
while (argList.length < 2 * JSBATON_ARGC) {
|
|
152
|
+
argList.push(0n);
|
|
153
|
+
}
|
|
154
|
+
// serialize js-value to c-value
|
|
155
|
+
argList = argList.map(function (value, ii) {
|
|
156
|
+
argi = ii;
|
|
157
|
+
switch (typeof value) {
|
|
158
|
+
case "bigint":
|
|
159
|
+
case "boolean":
|
|
160
|
+
baton.setBigInt64(8 + argi * 8, BigInt(value), true);
|
|
161
|
+
return value;
|
|
162
|
+
case "number":
|
|
163
|
+
// check for min/max safe-integer
|
|
164
|
+
assertOrThrow(
|
|
165
|
+
(
|
|
166
|
+
-9_007_199_254_740_991 <= value
|
|
167
|
+
&& value <= 9_007_199_254_740_991
|
|
168
|
+
),
|
|
169
|
+
(
|
|
170
|
+
"non-bigint integer must be within inclusive-range"
|
|
171
|
+
+ " -9,007,199,254,740,991 to 9,007,199,254,740,991"
|
|
172
|
+
)
|
|
173
|
+
);
|
|
174
|
+
baton.setBigInt64(8 + argi * 8, BigInt(value), true);
|
|
175
|
+
return value;
|
|
176
|
+
// case "object":
|
|
177
|
+
// break;
|
|
178
|
+
case "string":
|
|
179
|
+
baton = jsbatonValuePush({
|
|
180
|
+
argi,
|
|
181
|
+
baton,
|
|
182
|
+
value: (
|
|
183
|
+
value.endsWith("\u0000")
|
|
184
|
+
? value
|
|
185
|
+
// append null-terminator to string
|
|
186
|
+
: value + "\u0000"
|
|
187
|
+
)
|
|
117
188
|
});
|
|
189
|
+
return;
|
|
118
190
|
}
|
|
119
|
-
|
|
191
|
+
if (ArrayBuffer.isView(value)) {
|
|
192
|
+
return new DataView(
|
|
193
|
+
value.buffer,
|
|
194
|
+
value.byteOffset,
|
|
195
|
+
value.byteLength
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
if (isExternalBuffer(value)) {
|
|
199
|
+
return value;
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
// encode cFuncName into baton
|
|
203
|
+
argi += 1;
|
|
204
|
+
baton = jsbatonValuePush({
|
|
205
|
+
argi,
|
|
206
|
+
baton,
|
|
207
|
+
value: cFuncName + "\u0000"
|
|
208
|
+
});
|
|
209
|
+
// prepend baton, cFuncName to argList
|
|
210
|
+
argList = [
|
|
211
|
+
baton, cFuncName, ...argList
|
|
212
|
+
];
|
|
213
|
+
// preserve stack-trace
|
|
214
|
+
errStack = new Error().stack.replace((
|
|
215
|
+
/.*$/m
|
|
216
|
+
), "");
|
|
217
|
+
try {
|
|
218
|
+
return (
|
|
219
|
+
IS_BROWSER
|
|
220
|
+
? await sqlMessagePost(...argList)
|
|
221
|
+
: await cModule[cFuncName](argList)
|
|
222
|
+
);
|
|
223
|
+
} catch (err) {
|
|
224
|
+
err.stack += errStack;
|
|
225
|
+
assertOrThrow(undefined, err);
|
|
120
226
|
}
|
|
227
|
+
}
|
|
121
228
|
|
|
122
|
-
|
|
123
|
-
// this function will call <
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
229
|
+
function dbCallAsync(baton, cFuncName, db, ...argList) {
|
|
230
|
+
// this function will call <cFuncName> using db <argList>[0]
|
|
231
|
+
let __db = dbDeref(db);
|
|
232
|
+
// increment __db.busy
|
|
233
|
+
__db.busy += 1;
|
|
234
|
+
return cCallAsync(
|
|
235
|
+
baton,
|
|
236
|
+
cFuncName,
|
|
237
|
+
__db.ptr,
|
|
238
|
+
...argList
|
|
239
|
+
).finally(function () {
|
|
240
|
+
// decrement __db.busy
|
|
241
|
+
__db.busy -= 1;
|
|
242
|
+
assertOrThrow(__db.busy >= 0, `invalid __db.busy ${__db.busy}`);
|
|
243
|
+
});
|
|
244
|
+
}
|
|
135
245
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
246
|
+
async function dbCloseAsync({
|
|
247
|
+
db
|
|
248
|
+
}) {
|
|
139
249
|
// this function will close sqlite-database-connection <db>
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
250
|
+
let __db = dbDeref(db);
|
|
251
|
+
// prevent segfault - do not close db if actions are pending
|
|
252
|
+
assertOrThrow(
|
|
253
|
+
__db.busy === 0,
|
|
254
|
+
"db cannot close with " + __db.busy + " actions pending"
|
|
255
|
+
);
|
|
256
|
+
// cleanup connPool
|
|
257
|
+
await Promise.all(__db.connPool.map(async function (ptr) {
|
|
258
|
+
let val = ptr[0];
|
|
259
|
+
ptr[0] = 0n;
|
|
260
|
+
await cCallAsync(
|
|
261
|
+
undefined,
|
|
262
|
+
"_dbClose",
|
|
263
|
+
val,
|
|
264
|
+
__db.filename
|
|
145
265
|
);
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
ptr[0] = 0n;
|
|
150
|
-
await cCall("__dbCloseAsync", [
|
|
151
|
-
val
|
|
152
|
-
]);
|
|
153
|
-
}));
|
|
154
|
-
dbDict.delete(db);
|
|
155
|
-
}
|
|
266
|
+
}));
|
|
267
|
+
dbDict.delete(db);
|
|
268
|
+
}
|
|
156
269
|
|
|
157
|
-
|
|
270
|
+
function dbDeref(db) {
|
|
158
271
|
// this function will get private-object mapped to <db>
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
272
|
+
let __db = dbDict.get(db);
|
|
273
|
+
assertOrThrow(__db?.connPool[0] > 0, "invalid or closed db");
|
|
274
|
+
assertOrThrow(__db.busy >= 0, "invalid db.busy " + __db.busy);
|
|
275
|
+
__db.ii = (__db.ii + 1) % __db.connPool.length;
|
|
276
|
+
__db.ptr = __db.connPool[__db.ii][0];
|
|
277
|
+
assertOrThrow(__db.ptr > 0n, "invalid or closed db");
|
|
278
|
+
return __db;
|
|
279
|
+
}
|
|
167
280
|
|
|
168
|
-
|
|
169
|
-
|
|
281
|
+
function dbExecAndReturnLastBlobAsync({
|
|
282
|
+
bindList = [],
|
|
283
|
+
db,
|
|
284
|
+
sql
|
|
285
|
+
}) {
|
|
286
|
+
// this function will exec <sql> in <db> and return last value retrieved
|
|
287
|
+
// from execution as raw blob/buffer
|
|
288
|
+
return dbExecAsync({
|
|
289
|
+
bindList,
|
|
170
290
|
db,
|
|
171
|
-
responseType,
|
|
172
|
-
sql
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
291
|
+
responseType: "lastBlob",
|
|
292
|
+
sql
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
async function dbExecAsync({
|
|
297
|
+
bindList = [],
|
|
298
|
+
db,
|
|
299
|
+
modeRetry,
|
|
300
|
+
responseType,
|
|
301
|
+
sql
|
|
302
|
+
}) {
|
|
179
303
|
// this function will exec <sql> in <db> and return <result>
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
await dbTableInsertAsync({
|
|
190
|
-
colList: tmpColList,
|
|
191
|
-
colListPriority: tmpColListPriority,
|
|
192
|
-
csv: tmpCsv,
|
|
304
|
+
let baton;
|
|
305
|
+
let bindByKey;
|
|
306
|
+
let bindListLength;
|
|
307
|
+
let externalbufferList;
|
|
308
|
+
let result;
|
|
309
|
+
while (modeRetry > 0) {
|
|
310
|
+
try {
|
|
311
|
+
return await dbExecAsync({
|
|
312
|
+
bindList,
|
|
193
313
|
db,
|
|
194
|
-
|
|
195
|
-
|
|
314
|
+
responseType,
|
|
315
|
+
sql
|
|
196
316
|
});
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
bindListLength,
|
|
209
|
-
serialize.bufResult,
|
|
210
|
-
bindByKey,
|
|
211
|
-
(
|
|
212
|
-
responseType === "lastBlob"
|
|
213
|
-
? 1
|
|
214
|
-
: 0
|
|
215
|
-
)
|
|
216
|
-
].concat(serialize.bufSharedList));
|
|
217
|
-
result = result[1];
|
|
218
|
-
switch (responseType) {
|
|
219
|
-
case "arraybuffer":
|
|
220
|
-
case "lastBlob":
|
|
221
|
-
return result;
|
|
222
|
-
case "list":
|
|
223
|
-
return JSON.parse(new TextDecoder().decode(result));
|
|
224
|
-
default:
|
|
225
|
-
result = JSON.parse(new TextDecoder().decode(result));
|
|
226
|
-
return result.map(function (rowList) {
|
|
227
|
-
let colList = rowList.shift();
|
|
228
|
-
return rowList.map(function (row) {
|
|
229
|
-
let dict = {};
|
|
230
|
-
colList.forEach(function (key, ii) {
|
|
231
|
-
dict[key] = row[ii];
|
|
232
|
-
});
|
|
233
|
-
return dict;
|
|
234
|
-
});
|
|
317
|
+
} catch (err) {
|
|
318
|
+
assertOrThrow(modeRetry > 0, err);
|
|
319
|
+
consoleError(err);
|
|
320
|
+
consoleError(
|
|
321
|
+
"dbExecAsync - retry failed sql-query with "
|
|
322
|
+
+ modeRetry
|
|
323
|
+
+ " remaining retries"
|
|
324
|
+
);
|
|
325
|
+
modeRetry -= 1;
|
|
326
|
+
await new Promise(function (resolve) {
|
|
327
|
+
setTimeout(resolve, 50);
|
|
235
328
|
});
|
|
236
329
|
}
|
|
237
330
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
331
|
+
baton = jsbatonCreate();
|
|
332
|
+
bindByKey = !Array.isArray(bindList);
|
|
333
|
+
bindListLength = (
|
|
334
|
+
Array.isArray(bindList)
|
|
335
|
+
? bindList.length
|
|
336
|
+
: Object.keys(bindList).length
|
|
337
|
+
);
|
|
338
|
+
externalbufferList = [];
|
|
339
|
+
Object.entries(bindList).forEach(function ([
|
|
340
|
+
key, val
|
|
341
|
+
]) {
|
|
342
|
+
if (bindByKey) {
|
|
343
|
+
baton = jsbatonValuePush({
|
|
344
|
+
baton,
|
|
345
|
+
value: ":" + key + "\u0000"
|
|
346
|
+
});
|
|
255
347
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
348
|
+
baton = jsbatonValuePush({
|
|
349
|
+
baton,
|
|
350
|
+
externalbufferList,
|
|
351
|
+
value: val
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
result = await dbCallAsync(
|
|
355
|
+
baton,
|
|
356
|
+
"_dbExec",
|
|
357
|
+
db, // 0
|
|
358
|
+
String(sql) + "\n;\nPRAGMA noop", // 1
|
|
359
|
+
bindListLength, // 2
|
|
360
|
+
bindByKey, // 3
|
|
361
|
+
( // 4
|
|
362
|
+
responseType === "lastBlob"
|
|
363
|
+
? 1
|
|
364
|
+
: 0
|
|
365
|
+
),
|
|
366
|
+
undefined, // 5
|
|
367
|
+
undefined, // 6
|
|
368
|
+
undefined, // 7 - response
|
|
369
|
+
...externalbufferList // 8
|
|
370
|
+
);
|
|
371
|
+
result = result[2 + 7];
|
|
372
|
+
switch (responseType) {
|
|
373
|
+
case "arraybuffer":
|
|
374
|
+
case "lastBlob":
|
|
375
|
+
return result;
|
|
376
|
+
case "list":
|
|
377
|
+
return JSON.parse(new TextDecoder().decode(result));
|
|
378
|
+
default:
|
|
379
|
+
result = JSON.parse(new TextDecoder().decode(result));
|
|
380
|
+
return result.map(function (rowList) {
|
|
381
|
+
let colList = rowList.shift();
|
|
382
|
+
return rowList.map(function (row) {
|
|
383
|
+
let dict = {};
|
|
384
|
+
colList.forEach(function (key, ii) {
|
|
385
|
+
dict[key] = row[ii];
|
|
386
|
+
});
|
|
387
|
+
return dict;
|
|
388
|
+
});
|
|
270
389
|
});
|
|
271
390
|
}
|
|
391
|
+
}
|
|
272
392
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
393
|
+
async function dbFileExportAsync({
|
|
394
|
+
db,
|
|
395
|
+
dbData,
|
|
396
|
+
filename,
|
|
397
|
+
modeExport = 1
|
|
398
|
+
}) {
|
|
399
|
+
// This function will export <db> to <filename>
|
|
400
|
+
if (IS_BROWSER) {
|
|
401
|
+
filename = FILENAME_DBTMP;
|
|
282
402
|
}
|
|
403
|
+
assertOrThrow(
|
|
404
|
+
typeof filename === "string" && filename,
|
|
405
|
+
`invalid filename ${filename}`
|
|
406
|
+
);
|
|
407
|
+
return await dbCallAsync(
|
|
408
|
+
undefined,
|
|
409
|
+
"_dbFileImportOrExport",
|
|
410
|
+
db, // 0. sqlite3 * pInMemory,
|
|
411
|
+
String(filename), // 1. char *zFilename,
|
|
412
|
+
modeExport, // 2. const int isSave
|
|
413
|
+
undefined, // 3. undefined
|
|
414
|
+
dbData // 4. dbData
|
|
415
|
+
);
|
|
416
|
+
}
|
|
283
417
|
|
|
284
|
-
|
|
418
|
+
async function dbFileImportAsync({
|
|
419
|
+
db,
|
|
420
|
+
dbData,
|
|
421
|
+
filename
|
|
422
|
+
}) {
|
|
423
|
+
// This function will import <filename> to <db>
|
|
424
|
+
await dbFileExportAsync({
|
|
285
425
|
db,
|
|
286
|
-
|
|
287
|
-
}) {
|
|
288
|
-
// This function will save <db> to <filename>
|
|
289
|
-
assertOrThrow(filename, "invalid filename " + filename);
|
|
290
|
-
await dbCallAsync("__dbMemoryLoadOrSave", db, [
|
|
291
|
-
String(filename), 1
|
|
292
|
-
]);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
async function dbOpenAsync({
|
|
426
|
+
dbData,
|
|
296
427
|
filename,
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
428
|
+
modeExport: 0
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
async function dbNoopAsync(...argList) {
|
|
433
|
+
// this function will do nothing except return argList
|
|
434
|
+
return await cCallAsync(undefined, "_dbNoop", ...argList);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
async function dbOpenAsync({
|
|
438
|
+
afterFinalization,
|
|
439
|
+
dbData,
|
|
440
|
+
filename,
|
|
441
|
+
flags,
|
|
442
|
+
rawPtr,
|
|
443
|
+
threadCount = 1
|
|
444
|
+
}) {
|
|
300
445
|
// this function will open and return sqlite-database-connection <db>
|
|
301
446
|
// int sqlite3_open_v2(
|
|
302
447
|
// const char *filename, /* Database filename (UTF-8) */
|
|
@@ -304,1496 +449,485 @@ file sqlmath.js
|
|
|
304
449
|
// int flags, /* Flags */
|
|
305
450
|
// const char *zVfs /* Name of VFS module to use */
|
|
306
451
|
// );
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
]);
|
|
325
|
-
ptr = ptr[0][0];
|
|
326
|
-
finalizer = new BigInt64Array(addon.__dbFinalizerCreate());
|
|
327
|
-
finalizer[0] = BigInt(ptr);
|
|
328
|
-
return finalizer;
|
|
329
|
-
}));
|
|
330
|
-
dbDict.set(db, {
|
|
331
|
-
busy: 0,
|
|
332
|
-
connPool,
|
|
333
|
-
ii: 0,
|
|
334
|
-
ptr: 0n
|
|
335
|
-
});
|
|
336
|
-
return db;
|
|
452
|
+
let connPool;
|
|
453
|
+
let db = {
|
|
454
|
+
filename
|
|
455
|
+
};
|
|
456
|
+
assertOrThrow(
|
|
457
|
+
typeof filename === "string",
|
|
458
|
+
`invalid filename ${filename}`
|
|
459
|
+
);
|
|
460
|
+
assertOrThrow(
|
|
461
|
+
!dbData || isExternalBuffer(dbData),
|
|
462
|
+
"dbData must be ArrayBuffer"
|
|
463
|
+
);
|
|
464
|
+
if (rawPtr) {
|
|
465
|
+
rawPtr = [
|
|
466
|
+
BigInt(rawPtr)
|
|
467
|
+
];
|
|
468
|
+
threadCount = 1;
|
|
337
469
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
assertOrThrow((
|
|
357
|
-
/^temp\."[A-Z_a-z][0-9A-Z_a-z]*?"$/
|
|
358
|
-
).test(tableName), "invalid tableName " + tableName);
|
|
359
|
-
// parse csv
|
|
360
|
-
if (!rowList && csv) {
|
|
361
|
-
rowList = jsonRowListFromCsv({
|
|
362
|
-
csv
|
|
363
|
-
});
|
|
364
|
-
}
|
|
365
|
-
rowList = jsonRowListNormalize({
|
|
366
|
-
colList,
|
|
367
|
-
colListPriority,
|
|
368
|
-
rowList
|
|
369
|
-
});
|
|
370
|
-
colList = rowList.shift();
|
|
371
|
-
sqlCreateTable = (
|
|
372
|
-
"DROP TABLE IF EXISTS " + tableName + ";"
|
|
373
|
-
+ "CREATE TEMP TABLE " + tableName + "(" + colList.join(",") + ");"
|
|
470
|
+
connPool = await Promise.all(Array.from(new Array(
|
|
471
|
+
threadCount
|
|
472
|
+
), async function () {
|
|
473
|
+
let ptr = rawPtr || await cCallAsync(
|
|
474
|
+
undefined,
|
|
475
|
+
"_dbOpen",
|
|
476
|
+
// 0. const char *filename, Database filename (UTF-8)
|
|
477
|
+
String(filename),
|
|
478
|
+
// 1. sqlite3 **ppDb, OUT: SQLite db handle
|
|
479
|
+
undefined,
|
|
480
|
+
// 2. int flags, Flags
|
|
481
|
+
flags ?? (
|
|
482
|
+
SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI
|
|
483
|
+
),
|
|
484
|
+
// 3. const char *zVfs Name of VFS module to use
|
|
485
|
+
undefined,
|
|
486
|
+
// 4. wasm-only - arraybuffer of raw sqlite-database to open in wasm
|
|
487
|
+
dbData
|
|
374
488
|
);
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
});
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
}
|
|
489
|
+
ptr = rawPtr || [
|
|
490
|
+
ptr[0].getBigInt64(4 + 4, true)
|
|
491
|
+
];
|
|
492
|
+
dbFinalizationRegistry.register(db, {
|
|
493
|
+
afterFinalization,
|
|
494
|
+
ptr
|
|
495
|
+
});
|
|
496
|
+
return ptr;
|
|
497
|
+
}));
|
|
498
|
+
dbDict.set(db, {
|
|
499
|
+
busy: 0,
|
|
500
|
+
connPool,
|
|
501
|
+
filename,
|
|
502
|
+
ii: 0
|
|
503
|
+
});
|
|
504
|
+
return db;
|
|
505
|
+
}
|
|
390
506
|
|
|
391
|
-
|
|
392
|
-
// this function will
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
Math.min(2 ** Math.ceil(Math.log2(nn)), SQLITE_MAX_LENGTH2)
|
|
423
|
-
));
|
|
424
|
-
// copy tmp to bufResult with offset
|
|
425
|
-
bufferSetBuffer(bufResult, tmp, 0);
|
|
426
|
-
// save bufResult
|
|
427
|
-
serialize.bufResult = bufResult;
|
|
428
|
-
}
|
|
429
|
-
bufResult.setUint8(offset, datatype);
|
|
430
|
-
offset += 1;
|
|
431
|
-
}
|
|
432
|
-
function bufferSetBigint64(offset, val) {
|
|
433
|
-
// this function will set bigint <val> to buffer <bufResult> at <offset>
|
|
434
|
-
assertOrThrow(
|
|
435
|
-
BIGINT64_MIN <= val && val <= BIGINT64_MAX,
|
|
436
|
-
(
|
|
437
|
-
"The value of \"value\" is out of range."
|
|
438
|
-
+ " It must be >= -(2n ** 63n) and < 2n ** 63n."
|
|
439
|
-
)
|
|
440
|
-
);
|
|
441
|
-
bufResult.setBigInt64(offset, val, true);
|
|
442
|
-
}
|
|
443
|
-
function bufferSetBuffer(aa, bb, offset) {
|
|
444
|
-
// this function will set buffer <bb> to buffer <aa> at <offset>
|
|
445
|
-
aa = new Uint8Array(aa.buffer, aa.byteOffset, aa.byteLength);
|
|
446
|
-
bb = new Uint8Array(bb.buffer, bb.byteOffset, bb.byteLength);
|
|
447
|
-
aa.set(bb, offset);
|
|
448
|
-
return bb.byteLength;
|
|
449
|
-
}
|
|
450
|
-
function serialize(val) {
|
|
451
|
-
// this function will write to <bufResult>, <val> at given <offset>
|
|
507
|
+
function isExternalBuffer(buf) {
|
|
508
|
+
// this function will check if <buf> is ArrayBuffer or SharedArrayBuffer
|
|
509
|
+
return buf && (
|
|
510
|
+
buf.constructor === ArrayBuffer
|
|
511
|
+
|| (
|
|
512
|
+
typeof SharedArrayBuffer === "function"
|
|
513
|
+
&& buf.constructor === SharedArrayBuffer
|
|
514
|
+
)
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
function jsbatonCreate() {
|
|
519
|
+
// this function will create buffer <baton>
|
|
520
|
+
let baton = new DataView(new ArrayBuffer(1024));
|
|
521
|
+
// offset nalloc, nused
|
|
522
|
+
baton.setInt32(4, SQLITE_DATATYPE_OFFSET, true);
|
|
523
|
+
return baton;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function jsbatonValuePush({
|
|
527
|
+
argi,
|
|
528
|
+
baton,
|
|
529
|
+
externalbufferList,
|
|
530
|
+
value
|
|
531
|
+
}) {
|
|
532
|
+
// this function will push <value> to buffer <baton>
|
|
533
|
+
let nn;
|
|
534
|
+
let nused;
|
|
535
|
+
let tmp;
|
|
536
|
+
let vsize;
|
|
537
|
+
let vtype;
|
|
452
538
|
/*
|
|
453
539
|
#define SQLITE_DATATYPE_BLOB 0x04
|
|
454
|
-
#define SQLITE_DATATYPE_BLOB_0 0x14
|
|
540
|
+
// #define SQLITE_DATATYPE_BLOB_0 0x14
|
|
455
541
|
#define SQLITE_DATATYPE_FLOAT 0x02
|
|
456
|
-
#define SQLITE_DATATYPE_FLOAT_0 0x12
|
|
542
|
+
// #define SQLITE_DATATYPE_FLOAT_0 0x12
|
|
457
543
|
#define SQLITE_DATATYPE_INTEGER 0x01
|
|
458
544
|
#define SQLITE_DATATYPE_INTEGER_0 0x11
|
|
459
545
|
#define SQLITE_DATATYPE_INTEGER_1 0x21
|
|
460
546
|
#define SQLITE_DATATYPE_NULL 0x05
|
|
547
|
+
#define SQLITE_DATATYPE_EXTERNALBUFFER -0x01
|
|
461
548
|
#define SQLITE_DATATYPE_TEXT 0x03
|
|
462
549
|
#define SQLITE_DATATYPE_TEXT_0 0x13
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
550
|
+
// 1. false.bigint
|
|
551
|
+
// 2. false.boolean
|
|
552
|
+
// 3. false.function
|
|
553
|
+
// 4. false.number
|
|
554
|
+
// 5. false.object
|
|
555
|
+
// 6. false.string
|
|
556
|
+
// 7. false.symbol
|
|
557
|
+
// 8. false.undefined
|
|
558
|
+
// 9. true.bigint
|
|
559
|
+
// 10. true.boolean
|
|
560
|
+
// 11. true.function
|
|
561
|
+
// 12. true.number
|
|
562
|
+
// 13. true.object
|
|
563
|
+
// 14. true.string
|
|
564
|
+
// 15. true.symbol
|
|
565
|
+
// 16. true.undefined
|
|
566
|
+
// 17. true.buffer
|
|
567
|
+
// 18. true.externalbuffer
|
|
479
568
|
*/
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
bufSharedList.length <= 0.5 * JSBATON_ARGC,
|
|
484
|
-
(
|
|
485
|
-
"too many SharedArrayBuffer's " + bufSharedList.length
|
|
486
|
-
+ " > " + (0.5 * JSBATON_ARGC)
|
|
487
|
-
)
|
|
488
|
-
);
|
|
489
|
-
bufferAppendDatatype(SQLITE_DATATYPE_SHAREDARRAYBUFFER, 0);
|
|
490
|
-
bufSharedList.push(new DataView(val));
|
|
491
|
-
return;
|
|
492
|
-
}
|
|
493
|
-
// 12. true.boolean
|
|
494
|
-
if (val === 1 || val === 1n || val === true) {
|
|
495
|
-
bufferAppendDatatype(SQLITE_DATATYPE_INTEGER_1, 0);
|
|
496
|
-
return;
|
|
497
|
-
}
|
|
498
|
-
switch (Boolean(val) + "." + typeof(val)) {
|
|
499
|
-
// 1. false.bigint
|
|
500
|
-
case "false.bigint":
|
|
501
|
-
// 2. false.boolean
|
|
502
|
-
case "false.boolean":
|
|
503
|
-
// 4. false.number
|
|
504
|
-
case "false.number":
|
|
505
|
-
bufferAppendDatatype(SQLITE_DATATYPE_INTEGER_0, 0);
|
|
506
|
-
return;
|
|
507
|
-
// 3. false.function
|
|
508
|
-
// case "false.function":
|
|
509
|
-
// 5. false.object
|
|
510
|
-
case "false.object":
|
|
511
|
-
// 7. false.symbol
|
|
512
|
-
// case "false.symbol":
|
|
513
|
-
// 8. false.undefined
|
|
514
|
-
case "false.undefined":
|
|
515
|
-
// 13. true.function
|
|
516
|
-
case "true.function":
|
|
517
|
-
// 17. true.symbol
|
|
518
|
-
case "true.symbol":
|
|
519
|
-
// 18. true.undefined
|
|
520
|
-
// case "true.undefined":
|
|
521
|
-
bufferAppendDatatype(SQLITE_DATATYPE_NULL, 0);
|
|
522
|
-
return;
|
|
523
|
-
// 6. false.string
|
|
524
|
-
case "false.string":
|
|
525
|
-
bufferAppendDatatype(SQLITE_DATATYPE_TEXT_0, 0);
|
|
526
|
-
return;
|
|
527
|
-
// 11. true.bigint
|
|
528
|
-
case "true.bigint":
|
|
529
|
-
bufferAppendDatatype(SQLITE_DATATYPE_INTEGER, 8);
|
|
530
|
-
bufferSetBigint64(offset, val);
|
|
531
|
-
offset += 8;
|
|
532
|
-
return;
|
|
533
|
-
// 14. true.number
|
|
534
|
-
case "true.number":
|
|
535
|
-
bufferAppendDatatype(SQLITE_DATATYPE_FLOAT, 8);
|
|
536
|
-
bufResult.setFloat64(offset, val, true);
|
|
537
|
-
offset += 8;
|
|
538
|
-
return;
|
|
539
|
-
// 16. true.string
|
|
540
|
-
case "true.string":
|
|
541
|
-
val = new TextEncoder().encode(val);
|
|
542
|
-
bufferAppendDatatype(SQLITE_DATATYPE_TEXT, 8 + val.byteLength);
|
|
543
|
-
bufferSetBigint64(offset, BigInt(val.byteLength));
|
|
544
|
-
offset += 8;
|
|
545
|
-
offset += bufferSetBuffer(bufResult, val, offset);
|
|
546
|
-
return;
|
|
547
|
-
// 15. true.object
|
|
548
|
-
default:
|
|
549
|
-
assertOrThrow(
|
|
550
|
-
val && typeof val === "object",
|
|
551
|
-
"invalid data " + (typeof val) + " " + val
|
|
552
|
-
);
|
|
553
|
-
// write buffer
|
|
554
|
-
if (ArrayBuffer.isView(val)) {
|
|
555
|
-
if (val.byteLength === 0) {
|
|
556
|
-
bufferAppendDatatype(SQLITE_DATATYPE_NULL, 0);
|
|
557
|
-
return;
|
|
558
|
-
}
|
|
559
|
-
bufferAppendDatatype(
|
|
560
|
-
SQLITE_DATATYPE_BLOB,
|
|
561
|
-
8 + val.byteLength
|
|
562
|
-
);
|
|
563
|
-
bufferSetBigint64(offset, BigInt(val.byteLength));
|
|
564
|
-
offset += 8;
|
|
565
|
-
// copy val to bufResult with offset
|
|
566
|
-
bufferSetBuffer(bufResult, val, offset);
|
|
567
|
-
offset += val.byteLength;
|
|
568
|
-
return;
|
|
569
|
-
}
|
|
570
|
-
// write JSON.stringify(val)
|
|
571
|
-
val = String(
|
|
572
|
-
typeof val.toJSON === "function"
|
|
573
|
-
? val.toJSON()
|
|
574
|
-
: JSON.stringify(val)
|
|
575
|
-
);
|
|
576
|
-
val = new TextEncoder().encode(val);
|
|
577
|
-
bufferAppendDatatype(SQLITE_DATATYPE_TEXT, 8 + val.byteLength);
|
|
578
|
-
bufferSetBigint64(offset, BigInt(val.byteLength));
|
|
579
|
-
offset += 8;
|
|
580
|
-
offset += bufferSetBuffer(bufResult, val, offset);
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
// save bufResult
|
|
584
|
-
serialize.bufResult = bufResult;
|
|
585
|
-
// save bufSharedList
|
|
586
|
-
serialize.bufSharedList = bufSharedList;
|
|
587
|
-
return serialize;
|
|
569
|
+
// 10. true.boolean
|
|
570
|
+
if (value === 1 || value === 1n) {
|
|
571
|
+
value = true;
|
|
588
572
|
}
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
), "\n");
|
|
654
|
-
/*
|
|
655
|
-
2. The last record in the file may or may not have an ending line
|
|
656
|
-
break. For example:
|
|
657
|
-
aaa,bbb,ccc CRLF
|
|
658
|
-
zzz,yyy,xxx
|
|
659
|
-
*/
|
|
660
|
-
if (csv[csv.length - 1] !== "\n") {
|
|
661
|
-
csv += "\n";
|
|
573
|
+
switch (Boolean(value) + "." + typeof(value)) {
|
|
574
|
+
// 1. false.bigint
|
|
575
|
+
case "false.bigint":
|
|
576
|
+
// 2. false.boolean
|
|
577
|
+
case "false.boolean":
|
|
578
|
+
// 4. false.number
|
|
579
|
+
case "false.number":
|
|
580
|
+
vtype = SQLITE_DATATYPE_INTEGER_0;
|
|
581
|
+
vsize = 0;
|
|
582
|
+
break;
|
|
583
|
+
// 3. false.function
|
|
584
|
+
// case "false.function":
|
|
585
|
+
// 5. false.object
|
|
586
|
+
case "false.object":
|
|
587
|
+
// 7. false.symbol
|
|
588
|
+
case "false.symbol":
|
|
589
|
+
// 8. false.undefined
|
|
590
|
+
case "false.undefined":
|
|
591
|
+
// 11. true.function
|
|
592
|
+
case "true.function":
|
|
593
|
+
// 15. true.symbol
|
|
594
|
+
case "true.symbol":
|
|
595
|
+
// 16. true.undefined
|
|
596
|
+
// case "true.undefined":
|
|
597
|
+
vtype = SQLITE_DATATYPE_NULL;
|
|
598
|
+
vsize = 0;
|
|
599
|
+
break;
|
|
600
|
+
// 6. false.string
|
|
601
|
+
case "false.string":
|
|
602
|
+
vtype = SQLITE_DATATYPE_TEXT_0;
|
|
603
|
+
vsize = 0;
|
|
604
|
+
break;
|
|
605
|
+
// 9. true.bigint
|
|
606
|
+
case "true.bigint":
|
|
607
|
+
vtype = SQLITE_DATATYPE_INTEGER;
|
|
608
|
+
vsize = 8;
|
|
609
|
+
break;
|
|
610
|
+
// 10. true.boolean
|
|
611
|
+
case "true.boolean":
|
|
612
|
+
vtype = SQLITE_DATATYPE_INTEGER_1;
|
|
613
|
+
vsize = 0;
|
|
614
|
+
break;
|
|
615
|
+
// 12. true.number
|
|
616
|
+
case "true.number":
|
|
617
|
+
vtype = SQLITE_DATATYPE_FLOAT;
|
|
618
|
+
vsize = 8;
|
|
619
|
+
break;
|
|
620
|
+
// 13. true.object
|
|
621
|
+
// 14. true.string
|
|
622
|
+
default:
|
|
623
|
+
// 18. true.externalbuffer
|
|
624
|
+
if (isExternalBuffer(value)) {
|
|
625
|
+
assertOrThrow(
|
|
626
|
+
!IS_BROWSER,
|
|
627
|
+
"external ArrayBuffer cannot be passed directly to wasm"
|
|
628
|
+
);
|
|
629
|
+
assertOrThrow(
|
|
630
|
+
externalbufferList.length <= 8,
|
|
631
|
+
"externalbufferList.length must be less than 8"
|
|
632
|
+
);
|
|
633
|
+
externalbufferList.push(new DataView(value));
|
|
634
|
+
vtype = SQLITE_DATATYPE_EXTERNALBUFFER;
|
|
635
|
+
vsize = 4;
|
|
636
|
+
break;
|
|
662
637
|
}
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
if (
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
// build val
|
|
669
|
-
val += match[1];
|
|
670
|
-
match = match[2];
|
|
671
|
-
switch (quote + "." + match) {
|
|
672
|
-
case "false.,":
|
|
673
|
-
/*
|
|
674
|
-
4. Within the header and each record, there may be one or more
|
|
675
|
-
fields, separated by commas. Each line should contain the same
|
|
676
|
-
number of fields throughout the file. Spaces are considered part
|
|
677
|
-
of a field and should not be ignored. The last field in the
|
|
678
|
-
record must not be followed by a comma. For example:
|
|
679
|
-
aaa,bbb,ccc
|
|
680
|
-
*/
|
|
681
|
-
// delimit val
|
|
682
|
-
row.push(val);
|
|
683
|
-
val = "";
|
|
684
|
-
break;
|
|
685
|
-
case "false.\"":
|
|
686
|
-
case "true.\"":
|
|
687
|
-
/*
|
|
688
|
-
5. Each field may or may not be enclosed in double quotes (however
|
|
689
|
-
some programs, such as Microsoft Excel, do not use double quotes
|
|
690
|
-
at all). If fields are not enclosed with double quotes, then
|
|
691
|
-
double quotes may not appear inside the fields. For example:
|
|
692
|
-
"aaa","bbb","ccc" CRLF
|
|
693
|
-
zzz,yyy,xxx
|
|
694
|
-
*/
|
|
695
|
-
assertOrThrow(quote || val === "", (
|
|
696
|
-
"invalid csv - naked double-quote in unquoted-string "
|
|
697
|
-
+ JSON.stringify(val + "\"")
|
|
698
|
-
));
|
|
699
|
-
quote = !quote;
|
|
700
|
-
break;
|
|
701
|
-
// backtrack for naked-double-double-quote
|
|
702
|
-
case "false.\"\"":
|
|
703
|
-
quote = true;
|
|
704
|
-
rgx.lastIndex -= 1;
|
|
705
|
-
break;
|
|
706
|
-
case "false.\n":
|
|
707
|
-
case "false.\r\n":
|
|
708
|
-
/*
|
|
709
|
-
1. Each record is located on a separate line, delimited by a line
|
|
710
|
-
break (CRLF). For example:
|
|
711
|
-
aaa,bbb,ccc CRLF
|
|
712
|
-
zzz,yyy,xxx CRLF
|
|
713
|
-
*/
|
|
714
|
-
// delimit val
|
|
715
|
-
row.push(val);
|
|
716
|
-
val = "";
|
|
717
|
-
// append row
|
|
718
|
-
rowList.push(row);
|
|
719
|
-
// reset row
|
|
720
|
-
row = [];
|
|
721
|
-
break;
|
|
722
|
-
case "true.\"\"":
|
|
723
|
-
/*
|
|
724
|
-
7. If double-quotes are used to enclose fields, then a double-quote
|
|
725
|
-
appearing inside a field must be escaped by preceding it with
|
|
726
|
-
another double quote. For example:
|
|
727
|
-
"aaa","b""bb","ccc"
|
|
728
|
-
*/
|
|
729
|
-
val += "\"";
|
|
638
|
+
// 17. true.buffer
|
|
639
|
+
if (ArrayBuffer.isView(value)) {
|
|
640
|
+
if (value.byteLength === 0) {
|
|
641
|
+
vtype = SQLITE_DATATYPE_NULL;
|
|
642
|
+
vsize = 0;
|
|
730
643
|
break;
|
|
731
|
-
default:
|
|
732
|
-
/*
|
|
733
|
-
6. Fields containing line breaks (CRLF), double quotes, and commas
|
|
734
|
-
should be enclosed in double-quotes. For example:
|
|
735
|
-
"aaa","b CRLF
|
|
736
|
-
bb","ccc" CRLF
|
|
737
|
-
zzz,yyy,xxx
|
|
738
|
-
*/
|
|
739
|
-
assertOrThrow(quote, (
|
|
740
|
-
"invalid csv - illegal character in unquoted-string "
|
|
741
|
-
+ JSON.stringify(match)
|
|
742
|
-
));
|
|
743
|
-
val += match;
|
|
744
644
|
}
|
|
645
|
+
vtype = SQLITE_DATATYPE_BLOB;
|
|
646
|
+
vsize = 4 + value.byteLength;
|
|
647
|
+
break;
|
|
745
648
|
}
|
|
649
|
+
// 13. true.object
|
|
650
|
+
value = String(
|
|
651
|
+
typeof value === "string"
|
|
652
|
+
? value
|
|
653
|
+
: typeof value.toJSON === "function"
|
|
654
|
+
? value.toJSON()
|
|
655
|
+
: JSON.stringify(value)
|
|
656
|
+
);
|
|
657
|
+
// 14. true.string
|
|
658
|
+
value = new TextEncoder().encode(value);
|
|
659
|
+
vtype = SQLITE_DATATYPE_TEXT;
|
|
660
|
+
vsize = 4 + value.byteLength;
|
|
746
661
|
}
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
//
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
if (!colList) {
|
|
780
|
-
colList = rowList[0];
|
|
781
|
-
rowList = rowList.slice(1);
|
|
782
|
-
}
|
|
783
|
-
if (!(colList?.length > 0)) {
|
|
784
|
-
throw new Error("invalid colList " + JSON.stringify(colList));
|
|
785
|
-
}
|
|
786
|
-
colList = colList.map(function (key) {
|
|
787
|
-
// sanitize column-name
|
|
788
|
-
key = String(key).replace((
|
|
789
|
-
/^[^A-Z_a-z]/
|
|
790
|
-
), "c_" + key);
|
|
791
|
-
key = key.replace((
|
|
792
|
-
/[^0-9A-Z_a-z]/g
|
|
793
|
-
), "_");
|
|
794
|
-
// for duplicate column-name, add ordinal _2, _3, _4, ...
|
|
795
|
-
colDict[key] = colDict[key] || 0;
|
|
796
|
-
colDict[key] += 1;
|
|
797
|
-
if (colDict[key] > 1) {
|
|
798
|
-
key += "_" + colDict[key];
|
|
799
|
-
}
|
|
800
|
-
return key;
|
|
801
|
-
});
|
|
802
|
-
// normalize rowList
|
|
803
|
-
rowList = rowList.map(function (row) {
|
|
804
|
-
return (
|
|
805
|
-
row.length === colList.length
|
|
806
|
-
? row
|
|
807
|
-
: colList.map(function (ignore, ii) {
|
|
808
|
-
return row[ii];
|
|
809
|
-
})
|
|
810
|
-
);
|
|
811
|
-
});
|
|
812
|
-
if (!colListPriority) {
|
|
813
|
-
rowList.unshift(colList);
|
|
814
|
-
return rowList;
|
|
662
|
+
nused = baton.getInt32(4, true);
|
|
663
|
+
nn = nused + 1 + vsize;
|
|
664
|
+
assertOrThrow(
|
|
665
|
+
nn <= 0xffff_ffff,
|
|
666
|
+
"jsbaton cannot exceed 0x7fff_ffff / 2,147,483,647 bytes"
|
|
667
|
+
);
|
|
668
|
+
// exponentially grow baton as needed
|
|
669
|
+
if (baton.byteLength < nn) {
|
|
670
|
+
tmp = baton;
|
|
671
|
+
baton = new DataView(new ArrayBuffer(
|
|
672
|
+
Math.min(2 ** Math.ceil(Math.log2(nn)), 0x7fff_ffff)
|
|
673
|
+
));
|
|
674
|
+
// update nalloc
|
|
675
|
+
baton.setInt32(0, baton.byteLength, true);
|
|
676
|
+
// copy tmp to baton
|
|
677
|
+
new Uint8Array(
|
|
678
|
+
baton.buffer,
|
|
679
|
+
baton.byteOffset,
|
|
680
|
+
nused
|
|
681
|
+
).set(new Uint8Array(tmp.buffer, tmp.byteOffset, nused), 0);
|
|
682
|
+
}
|
|
683
|
+
// push vtype
|
|
684
|
+
baton.setUint8(nused, vtype);
|
|
685
|
+
// update nused
|
|
686
|
+
baton.setInt32(4, nused + 1 + vsize, true);
|
|
687
|
+
// handle blob-value
|
|
688
|
+
switch (vtype) {
|
|
689
|
+
case SQLITE_DATATYPE_BLOB:
|
|
690
|
+
case SQLITE_DATATYPE_TEXT:
|
|
691
|
+
// set argv[ii] to blob/text location
|
|
692
|
+
if (argi !== undefined) {
|
|
693
|
+
baton.setInt32(8 + argi * 8, nused, true);
|
|
815
694
|
}
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
695
|
+
vsize -= 4;
|
|
696
|
+
// push vsize
|
|
697
|
+
assertOrThrow(
|
|
698
|
+
0 <= vsize && vsize <= 1_000_000_000,
|
|
699
|
+
(
|
|
700
|
+
"sqlite-blob byte-length must be within inclusive-range"
|
|
701
|
+
+ " 0 to 1,000,000,000"
|
|
702
|
+
)
|
|
703
|
+
);
|
|
704
|
+
baton.setInt32(nused + 1, vsize, true);
|
|
705
|
+
new Uint8Array(
|
|
706
|
+
baton.buffer,
|
|
707
|
+
nused + 1 + 4,
|
|
708
|
+
vsize
|
|
709
|
+
).set(new Uint8Array(value.buffer, value.byteOffset, vsize), 0);
|
|
710
|
+
break;
|
|
711
|
+
case SQLITE_DATATYPE_EXTERNALBUFFER:
|
|
712
|
+
vsize = value.byteLength;
|
|
713
|
+
// push vsize
|
|
714
|
+
assertOrThrow(
|
|
715
|
+
0 <= vsize && vsize <= 1_000_000_000,
|
|
716
|
+
(
|
|
717
|
+
"sqlite-blob byte-length must be within inclusive-range"
|
|
718
|
+
+ " 0 to 1,000,000,000"
|
|
719
|
+
)
|
|
720
|
+
);
|
|
721
|
+
baton.setInt32(nused + 1, vsize, true);
|
|
722
|
+
break;
|
|
723
|
+
case SQLITE_DATATYPE_FLOAT:
|
|
724
|
+
baton.setFloat64(nused + 1, value, true);
|
|
725
|
+
break;
|
|
726
|
+
case SQLITE_DATATYPE_INTEGER:
|
|
727
|
+
assertOrThrow(
|
|
728
|
+
(
|
|
729
|
+
-9_223_372_036_854_775_808n <= value
|
|
730
|
+
&& value <= 9_223_372_036_854_775_807n
|
|
731
|
+
),
|
|
732
|
+
(
|
|
733
|
+
"sqlite-integer must be within inclusive-range "
|
|
734
|
+
+ "-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807"
|
|
735
|
+
)
|
|
736
|
+
);
|
|
737
|
+
baton.setBigInt64(nused + 1, value, true);
|
|
738
|
+
break;
|
|
838
739
|
}
|
|
740
|
+
return baton;
|
|
741
|
+
}
|
|
839
742
|
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
743
|
+
function jsbatonValueString({
|
|
744
|
+
argi,
|
|
745
|
+
baton
|
|
746
|
+
}) {
|
|
747
|
+
// this function will return string-value from <baton> at given <offset>
|
|
748
|
+
let offset = baton.getInt32(4 + 4 + argi * 8, true);
|
|
749
|
+
return new TextDecoder().decode(new Uint8Array(
|
|
750
|
+
baton.buffer,
|
|
751
|
+
offset + 1 + 4,
|
|
752
|
+
// remove null-terminator from string
|
|
753
|
+
baton.getInt32(offset + 1, true) - 1
|
|
754
|
+
));
|
|
755
|
+
}
|
|
846
756
|
|
|
847
|
-
|
|
848
|
-
// this function will test assertXxx's handling-behavior
|
|
849
|
-
// test assertNumericalEqual's handling-behavior
|
|
850
|
-
assertNumericalEqual(1, 1);
|
|
851
|
-
assertErrorThrownAsync(function () {
|
|
852
|
-
assertNumericalEqual(0, 0);
|
|
853
|
-
}, "value cannot be 0 or falsy");
|
|
854
|
-
assertErrorThrownAsync(function () {
|
|
855
|
-
assertNumericalEqual(1, 2);
|
|
856
|
-
}, "1 != 2");
|
|
857
|
-
assertErrorThrownAsync(function () {
|
|
858
|
-
assertNumericalEqual(1, 2, "aa");
|
|
859
|
-
}, "aa");
|
|
860
|
-
}
|
|
757
|
+
function noop(val) {
|
|
861
758
|
|
|
862
|
-
|
|
863
|
-
// this function will test cCall's handling-behavior
|
|
864
|
-
[
|
|
865
|
-
[-0, "0"],
|
|
866
|
-
[-Infinity, "0"],
|
|
867
|
-
[0, "0"],
|
|
868
|
-
[1 / 0, "0"],
|
|
869
|
-
[Infinity, "0"],
|
|
870
|
-
[false, "0"],
|
|
871
|
-
[null, "0"],
|
|
872
|
-
[true, "1"],
|
|
873
|
-
[undefined, "0"],
|
|
874
|
-
[{}, "0"]
|
|
875
|
-
].forEach(async function ([
|
|
876
|
-
aa, bb
|
|
877
|
-
]) {
|
|
878
|
-
let cc;
|
|
879
|
-
cc = String(
|
|
880
|
-
await cCall("noopAsync", [
|
|
881
|
-
aa
|
|
882
|
-
])
|
|
883
|
-
)[0][0];
|
|
884
|
-
assertOrThrow(bb === cc, [aa, bb, cc]);
|
|
885
|
-
cc = String(cCall("noopSync", [
|
|
886
|
-
aa
|
|
887
|
-
]))[0][0];
|
|
888
|
-
assertOrThrow(bb === cc, [aa, bb, cc]);
|
|
889
|
-
});
|
|
890
|
-
}
|
|
759
|
+
// This function will do nothing except return <val>.
|
|
891
760
|
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
let db = await dbOpenAsync({
|
|
895
|
-
filename: ":memory:"
|
|
896
|
-
});
|
|
897
|
-
async function testDbGetLastBlobAsync(val) {
|
|
898
|
-
return await dbGetLastBlobAsync({
|
|
899
|
-
bindList: [
|
|
900
|
-
val
|
|
901
|
-
],
|
|
902
|
-
db,
|
|
903
|
-
sql: "SELECT 1, 2, 3; SELECT 1, 2, ?"
|
|
904
|
-
});
|
|
905
|
-
}
|
|
906
|
-
// test bigint-error handling-behavior
|
|
907
|
-
noop([
|
|
908
|
-
-(2n ** 63n),
|
|
909
|
-
2n ** 63n
|
|
910
|
-
]).forEach(function (val) {
|
|
911
|
-
assertErrorThrownAsync(testDbGetLastBlobAsync.bind(undefined, val));
|
|
912
|
-
});
|
|
913
|
-
// test datatype handling-behavior
|
|
914
|
-
[
|
|
915
|
-
// -1. SharedArrayBuffer
|
|
916
|
-
// new SharedArrayBuffer(0), null,
|
|
917
|
-
// 1. bigint
|
|
918
|
-
-0n, -0,
|
|
919
|
-
-0x7fffffffffffffffn, "-9223372036854775807",
|
|
920
|
-
-1n, -1,
|
|
921
|
-
-2n, -2,
|
|
922
|
-
0n, 0,
|
|
923
|
-
0x7fffffffffffffffn, "9223372036854775807",
|
|
924
|
-
1n, 1,
|
|
925
|
-
2n, 2,
|
|
926
|
-
// 2. boolean
|
|
927
|
-
false, 0,
|
|
928
|
-
true, 1,
|
|
929
|
-
// 3. function
|
|
930
|
-
noop, null,
|
|
931
|
-
// 4. number
|
|
932
|
-
-0, 0,
|
|
933
|
-
-1 / 0, null,
|
|
934
|
-
-1e-999, 0,
|
|
935
|
-
-1e999, null,
|
|
936
|
-
-2, -2,
|
|
937
|
-
-Infinity, null,
|
|
938
|
-
-NaN, 0,
|
|
939
|
-
0, 0,
|
|
940
|
-
1 / 0, null,
|
|
941
|
-
1e-999, 0,
|
|
942
|
-
1e999, null,
|
|
943
|
-
2, 2,
|
|
944
|
-
Infinity, null,
|
|
945
|
-
NaN, 0,
|
|
946
|
-
// 5. object
|
|
947
|
-
new Uint8Array(0), null,
|
|
948
|
-
new TextEncoder().encode(""), null,
|
|
949
|
-
new TextEncoder().encode("\u0000"), null,
|
|
950
|
-
new TextEncoder().encode("\u0000\u{1f600}\u0000"), null,
|
|
951
|
-
[], "[]",
|
|
952
|
-
new Date(0), "1970-01-01T00:00:00.000Z",
|
|
953
|
-
new RegExp(), "{}",
|
|
954
|
-
null, null,
|
|
955
|
-
{}, "{}",
|
|
956
|
-
// 6. string
|
|
957
|
-
"", "",
|
|
958
|
-
"0", "0",
|
|
959
|
-
"1", "1",
|
|
960
|
-
"2", "2",
|
|
961
|
-
"\u0000", "\u0000",
|
|
962
|
-
"\u0000\u{1f600}\u0000", "\u0000\u{1f600}\u0000",
|
|
963
|
-
"a".repeat(9999), "a".repeat(9999),
|
|
964
|
-
// 7. symbol
|
|
965
|
-
Symbol(), null,
|
|
966
|
-
// 8. undefined
|
|
967
|
-
undefined, null
|
|
968
|
-
].forEach(function (aa, ii, list) {
|
|
969
|
-
let bb = list[ii + 1];
|
|
970
|
-
if (ii % 2 === 1) {
|
|
971
|
-
return;
|
|
972
|
-
}
|
|
973
|
-
ii *= 0.5;
|
|
974
|
-
// test dbGetLastBlobAsync's bind handling-behavior
|
|
975
|
-
[
|
|
976
|
-
aa
|
|
977
|
-
].forEach(async function (aa) {
|
|
978
|
-
let cc = String(bb);
|
|
979
|
-
let dd = new TextDecoder().decode(
|
|
980
|
-
await testDbGetLastBlobAsync(aa)
|
|
981
|
-
);
|
|
982
|
-
switch (typeof(aa)) {
|
|
983
|
-
case "bigint":
|
|
984
|
-
aa = Number(aa);
|
|
985
|
-
break;
|
|
986
|
-
case "function":
|
|
987
|
-
case "symbol":
|
|
988
|
-
case "undefined":
|
|
989
|
-
cc = "";
|
|
990
|
-
break;
|
|
991
|
-
case "number":
|
|
992
|
-
switch (aa) {
|
|
993
|
-
case -2:
|
|
994
|
-
cc = "-2.0";
|
|
995
|
-
break;
|
|
996
|
-
case -Infinity:
|
|
997
|
-
cc = "-Inf";
|
|
998
|
-
break;
|
|
999
|
-
case 2:
|
|
1000
|
-
cc = "2.0";
|
|
1001
|
-
break;
|
|
1002
|
-
case Infinity:
|
|
1003
|
-
cc = "Inf";
|
|
1004
|
-
break;
|
|
1005
|
-
}
|
|
1006
|
-
break;
|
|
1007
|
-
case "object":
|
|
1008
|
-
if (ArrayBuffer.isView(aa)) {
|
|
1009
|
-
cc = new TextDecoder().decode(aa);
|
|
1010
|
-
break;
|
|
1011
|
-
}
|
|
1012
|
-
if (aa === null) {
|
|
1013
|
-
cc = "";
|
|
1014
|
-
}
|
|
1015
|
-
break;
|
|
1016
|
-
}
|
|
1017
|
-
// debugInline(ii, aa, bb, cc, dd);
|
|
1018
|
-
assertJsonEqual(cc, dd, {
|
|
1019
|
-
ii,
|
|
1020
|
-
aa, //jslint-quiet
|
|
1021
|
-
bb,
|
|
1022
|
-
cc,
|
|
1023
|
-
dd
|
|
1024
|
-
});
|
|
1025
|
-
});
|
|
1026
|
-
// test dbExecAsync's responseType handling-behavior
|
|
1027
|
-
[
|
|
1028
|
-
"arraybuffer",
|
|
1029
|
-
"list",
|
|
1030
|
-
undefined
|
|
1031
|
-
].forEach(async function (responseType) {
|
|
1032
|
-
let cc = noop(
|
|
1033
|
-
await dbExecAsync({
|
|
1034
|
-
bindList: [
|
|
1035
|
-
aa
|
|
1036
|
-
],
|
|
1037
|
-
db,
|
|
1038
|
-
responseType,
|
|
1039
|
-
sql: "SELECT ? AS val"
|
|
1040
|
-
})
|
|
1041
|
-
);
|
|
1042
|
-
// debugInline(ii, responseType, aa, bb, cc);
|
|
1043
|
-
switch (responseType) {
|
|
1044
|
-
case "arraybuffer":
|
|
1045
|
-
cc = JSON.parse(new TextDecoder().decode(cc))[0][1][0];
|
|
1046
|
-
break;
|
|
1047
|
-
case "list":
|
|
1048
|
-
cc = cc[0][1][0];
|
|
1049
|
-
break;
|
|
1050
|
-
default:
|
|
1051
|
-
cc = cc[0][0].val;
|
|
1052
|
-
}
|
|
1053
|
-
assertJsonEqual(bb, cc, {
|
|
1054
|
-
aa,
|
|
1055
|
-
bb,
|
|
1056
|
-
cc
|
|
1057
|
-
});
|
|
1058
|
-
});
|
|
1059
|
-
// test dbExecAsync's bind handling-behavior
|
|
1060
|
-
[
|
|
1061
|
-
[
|
|
1062
|
-
[
|
|
1063
|
-
bb, bb, 0
|
|
1064
|
-
],
|
|
1065
|
-
(
|
|
1066
|
-
"SELECT 0;"
|
|
1067
|
-
+ " SELECT ? AS c1, ? AS c2, ? AS c3, ? AS c4"
|
|
1068
|
-
+ " UNION ALL SELECT ?1, ?2, ?3, ?4"
|
|
1069
|
-
+ " UNION ALL SELECT ?1, ?2, ?3, ?4"
|
|
1070
|
-
)
|
|
1071
|
-
], [
|
|
1072
|
-
{
|
|
1073
|
-
k1: bb,
|
|
1074
|
-
k2: bb,
|
|
1075
|
-
k3: 0
|
|
1076
|
-
},
|
|
1077
|
-
(
|
|
1078
|
-
"SELECT 0;"
|
|
1079
|
-
+ " SELECT $k1 AS c1, $k2 AS c2, $k3 AS c3, $k4 AS c4"
|
|
1080
|
-
+ " UNION ALL SELECT :k1, :k2, :k3, :k4"
|
|
1081
|
-
+ " UNION ALL SELECT @k1, @k2, @k3, @k4"
|
|
1082
|
-
)
|
|
1083
|
-
]
|
|
1084
|
-
].forEach(async function ([
|
|
1085
|
-
bindList, sql
|
|
1086
|
-
]) {
|
|
1087
|
-
let cc = noop(
|
|
1088
|
-
await dbExecAsync({
|
|
1089
|
-
bindList,
|
|
1090
|
-
db,
|
|
1091
|
-
responseType: "list",
|
|
1092
|
-
sql
|
|
1093
|
-
})
|
|
1094
|
-
);
|
|
1095
|
-
// debugInline(ii, aa, bb, cc);
|
|
1096
|
-
assertJsonEqual(
|
|
1097
|
-
[
|
|
1098
|
-
[
|
|
1099
|
-
[
|
|
1100
|
-
"0"
|
|
1101
|
-
], [
|
|
1102
|
-
0
|
|
1103
|
-
]
|
|
1104
|
-
], [
|
|
1105
|
-
[
|
|
1106
|
-
"c1", "c2", "c3", "c4"
|
|
1107
|
-
], [
|
|
1108
|
-
bb, bb, 0, undefined
|
|
1109
|
-
], [
|
|
1110
|
-
bb, bb, 0, undefined
|
|
1111
|
-
], [
|
|
1112
|
-
bb, bb, 0, undefined
|
|
1113
|
-
]
|
|
1114
|
-
]
|
|
1115
|
-
],
|
|
1116
|
-
cc
|
|
1117
|
-
);
|
|
1118
|
-
});
|
|
1119
|
-
// test dbTableInsertAsync's bind handling-behavior
|
|
1120
|
-
[
|
|
1121
|
-
{
|
|
1122
|
-
// test list-of-list handling-behavior
|
|
1123
|
-
rowList: [
|
|
1124
|
-
[
|
|
1125
|
-
"c1", "c2", "c3"
|
|
1126
|
-
],
|
|
1127
|
-
[
|
|
1128
|
-
aa, aa
|
|
1129
|
-
]
|
|
1130
|
-
]
|
|
1131
|
-
}, {
|
|
1132
|
-
// test list-of-dict handling-behavior
|
|
1133
|
-
rowList: [
|
|
1134
|
-
{
|
|
1135
|
-
"c1": aa,
|
|
1136
|
-
"c2": aa,
|
|
1137
|
-
"c3": undefined
|
|
1138
|
-
}
|
|
1139
|
-
]
|
|
1140
|
-
}, {
|
|
1141
|
-
// test colList and list-of-list handling-behavior
|
|
1142
|
-
colList: [
|
|
1143
|
-
"c1", "c2", "c3"
|
|
1144
|
-
],
|
|
1145
|
-
rowList: [
|
|
1146
|
-
[
|
|
1147
|
-
aa, aa
|
|
1148
|
-
]
|
|
1149
|
-
]
|
|
1150
|
-
}, {
|
|
1151
|
-
// test colList and list-of-dict handling-behavior
|
|
1152
|
-
colList: [
|
|
1153
|
-
"c1", "c2", "c3"
|
|
1154
|
-
],
|
|
1155
|
-
rowList: [
|
|
1156
|
-
{
|
|
1157
|
-
"c1": aa,
|
|
1158
|
-
"c2": aa,
|
|
1159
|
-
"c3": undefined
|
|
1160
|
-
}
|
|
1161
|
-
]
|
|
1162
|
-
}, {
|
|
1163
|
-
// test colList and list-of-list handling-behavior
|
|
1164
|
-
colList: [
|
|
1165
|
-
"c1", "c3", "c2"
|
|
1166
|
-
],
|
|
1167
|
-
colListPriority: [
|
|
1168
|
-
"c1", "c2"
|
|
1169
|
-
],
|
|
1170
|
-
rowList: [
|
|
1171
|
-
[
|
|
1172
|
-
aa, undefined, aa
|
|
1173
|
-
]
|
|
1174
|
-
]
|
|
1175
|
-
}
|
|
1176
|
-
].forEach(async function ({
|
|
1177
|
-
colList,
|
|
1178
|
-
colListPriority,
|
|
1179
|
-
rowList
|
|
1180
|
-
}, jj) {
|
|
1181
|
-
let cc = noop(
|
|
1182
|
-
await dbExecAsync({
|
|
1183
|
-
db,
|
|
1184
|
-
responseType: "list",
|
|
1185
|
-
sql: `SELECT * FROM datatype_${ii}_${jj}`,
|
|
1186
|
-
tmpColList: colList,
|
|
1187
|
-
tmpColListPriority: colListPriority,
|
|
1188
|
-
tmpRowList: rowList,
|
|
1189
|
-
tmpTableName: "datatype_" + ii + "_" + jj
|
|
1190
|
-
})
|
|
1191
|
-
)[0];
|
|
1192
|
-
// debugInline(ii, jj, aa, bb, cc);
|
|
1193
|
-
assertJsonEqual([
|
|
1194
|
-
[
|
|
1195
|
-
"c1", "c2", "c3"
|
|
1196
|
-
], [
|
|
1197
|
-
bb, bb, undefined
|
|
1198
|
-
]
|
|
1199
|
-
], cc);
|
|
1200
|
-
});
|
|
1201
|
-
});
|
|
1202
|
-
}
|
|
761
|
+
return val;
|
|
762
|
+
}
|
|
1203
763
|
|
|
1204
|
-
|
|
1205
|
-
// this function will test dbCloseAsync's handling-behavior
|
|
1206
|
-
let db = await dbOpenAsync({
|
|
1207
|
-
filename: ":memory:"
|
|
1208
|
-
});
|
|
1209
|
-
// test null-case handling-behavior
|
|
1210
|
-
assertErrorThrownAsync(function () {
|
|
1211
|
-
return dbCloseAsync({});
|
|
1212
|
-
}, "invalid or closed db");
|
|
1213
|
-
// test close handling-behavior
|
|
1214
|
-
await dbCloseAsync({
|
|
1215
|
-
db
|
|
1216
|
-
});
|
|
1217
|
-
}
|
|
764
|
+
function objectDeepCopyWithKeysSorted(obj) {
|
|
1218
765
|
|
|
1219
|
-
|
|
1220
|
-
// this function will test dbExecAsync's handling-behavior
|
|
1221
|
-
let db = await dbOpenAsync({
|
|
1222
|
-
filename: ":memory:"
|
|
1223
|
-
});
|
|
1224
|
-
// test null-case handling-behavior
|
|
1225
|
-
assertErrorThrownAsync(function () {
|
|
1226
|
-
return dbExecAsync({
|
|
1227
|
-
db,
|
|
1228
|
-
sql: undefined
|
|
1229
|
-
});
|
|
1230
|
-
}, "near \"undefined\": syntax error");
|
|
1231
|
-
// test race-condition handling-behavior
|
|
1232
|
-
Array.from(new Array(4)).forEach(async function () {
|
|
1233
|
-
let result;
|
|
1234
|
-
try {
|
|
1235
|
-
result = JSON.stringify(
|
|
1236
|
-
await dbExecAsync({
|
|
1237
|
-
bindList: [
|
|
1238
|
-
new TextEncoder().encode("foob"),
|
|
1239
|
-
new TextEncoder().encode("fooba"),
|
|
1240
|
-
new TextEncoder().encode("foobar")
|
|
1241
|
-
],
|
|
1242
|
-
db,
|
|
1243
|
-
responseType: "list",
|
|
1244
|
-
sql: (`
|
|
1245
|
-
CREATE TABLE testDbExecAsync1 AS
|
|
1246
|
-
SELECT 101 AS c101, 102 AS c102
|
|
1247
|
-
--
|
|
1248
|
-
UNION ALL
|
|
1249
|
-
VALUES
|
|
1250
|
-
(201, 202),
|
|
1251
|
-
(301, NULL);
|
|
1252
|
-
CREATE TABLE testDbExecAsync2 AS
|
|
1253
|
-
SELECT 401 AS c401, 402 AS c402, 403 AS c403
|
|
1254
|
-
--
|
|
1255
|
-
UNION ALL
|
|
1256
|
-
VALUES
|
|
1257
|
-
(501, 502.0123, 5030123456789),
|
|
1258
|
-
(601, '602', '603_\"\x01\x08\x09\x0a\x0b\x0c\x0d\x0e'),
|
|
1259
|
-
(?1, ?2, ?3),
|
|
1260
|
-
(tostring(?1), tostring(?2), tostring(?3)),
|
|
1261
|
-
(tobase64(?1), tobase64(?2), tobase64(?3)),
|
|
1262
|
-
(
|
|
1263
|
-
tobase64(uncompress(compress(?1))),
|
|
1264
|
-
tobase64(uncompress(compress(?2))),
|
|
1265
|
-
tobase64(uncompress(compress(?3)))
|
|
1266
|
-
);
|
|
1267
|
-
SELECT * FROM testDbExecAsync1;
|
|
1268
|
-
SELECT * FROM testDbExecAsync2;
|
|
1269
|
-
`)
|
|
1270
|
-
})
|
|
1271
|
-
);
|
|
1272
|
-
assertJsonEqual(result, JSON.stringify([
|
|
1273
|
-
[
|
|
1274
|
-
["c101", "c102"],
|
|
1275
|
-
[101, 102],
|
|
1276
|
-
[201, 202],
|
|
1277
|
-
[301, null]
|
|
1278
|
-
],
|
|
1279
|
-
[
|
|
1280
|
-
["c401", "c402", "c403"],
|
|
1281
|
-
[401, 402, 403],
|
|
1282
|
-
[501, 502.0123, 5030123456789],
|
|
1283
|
-
[601, "602", "603_\"\u0001\b\t\n\u000b\f\r\u000e"],
|
|
1284
|
-
[
|
|
1285
|
-
null, null, null
|
|
1286
|
-
],
|
|
1287
|
-
[
|
|
1288
|
-
"foob", "fooba", "foobar"
|
|
1289
|
-
],
|
|
1290
|
-
[
|
|
1291
|
-
"Zm9vYg==", "Zm9vYmE=", "Zm9vYmFy"
|
|
1292
|
-
],
|
|
1293
|
-
[
|
|
1294
|
-
"Zm9vYg==", "Zm9vYmE=", "Zm9vYmFy"
|
|
1295
|
-
]
|
|
1296
|
-
]
|
|
1297
|
-
]));
|
|
1298
|
-
} catch (err) {
|
|
1299
|
-
assertOrThrow(
|
|
1300
|
-
err.message.indexOf(
|
|
1301
|
-
"table testDbExecAsync1 already exists"
|
|
1302
|
-
) >= 0,
|
|
1303
|
-
err
|
|
1304
|
-
);
|
|
1305
|
-
}
|
|
1306
|
-
});
|
|
1307
|
-
// test close-while-busy handling-behavior
|
|
1308
|
-
assertErrorThrownAsync(function () {
|
|
1309
|
-
return dbCloseAsync({
|
|
1310
|
-
db
|
|
1311
|
-
});
|
|
1312
|
-
}, (
|
|
1313
|
-
/db cannot close with \d+? actions pending/
|
|
1314
|
-
));
|
|
1315
|
-
// test sqlmath-defined-func handling-behavior
|
|
1316
|
-
[
|
|
1317
|
-
"COT(NULL)", null,
|
|
1318
|
-
"COT('-1')", -0.642092615934331,
|
|
1319
|
-
"COT('0')", null,
|
|
1320
|
-
"COT('1')", 0.642092615934331,
|
|
1321
|
-
"COT(-1)", -0.642092615934331,
|
|
1322
|
-
"COT(0)", null,
|
|
1323
|
-
"COT(1)", 0.642092615934331,
|
|
1324
|
-
"COTH(NULL)", null,
|
|
1325
|
-
"COTH('-1')", -1.31303528549933,
|
|
1326
|
-
"COTH('0')", null,
|
|
1327
|
-
"COTH('1')", 1.31303528549933,
|
|
1328
|
-
"COTH(-1)", -1.31303528549933,
|
|
1329
|
-
"COTH(0)", null,
|
|
1330
|
-
"COTH(1)", 1.31303528549933,
|
|
1331
|
-
"ROUNDORZERO(NULL, NULL)", 0,
|
|
1332
|
-
"ROUNDORZERO(NULL, 0)", 0,
|
|
1333
|
-
"ROUNDORZERO(NULL, 0.5)", 0,
|
|
1334
|
-
"ROUNDORZERO(0.5, NULL)", 1,
|
|
1335
|
-
"ROUNDORZERO(0.5, 0.5)", 1,
|
|
1336
|
-
"ROUNDORZERO(0.5, 1)", 0.5,
|
|
1337
|
-
"SIGN(NULL)", null,
|
|
1338
|
-
"SIGN('-1')", -1,
|
|
1339
|
-
"SIGN('0')", 0,
|
|
1340
|
-
"SIGN('1')", 1,
|
|
1341
|
-
"SIGN(-0x7fffffffffffffff)", -1,
|
|
1342
|
-
"SIGN(-1)", -1,
|
|
1343
|
-
"SIGN(-1e999)", -1,
|
|
1344
|
-
"SIGN(0)", 0,
|
|
1345
|
-
"SIGN(0x7fffffffffffffff)", 1,
|
|
1346
|
-
"SIGN(0x8000000000000000)", -1,
|
|
1347
|
-
"SIGN(0xffffffffffffffff)", -1,
|
|
1348
|
-
"SIGN(1)", 1,
|
|
1349
|
-
"SIGN(1e999)", 1,
|
|
1350
|
-
// sentinel
|
|
1351
|
-
"NULL", null
|
|
1352
|
-
].forEach(async function (sql, ii, list) {
|
|
1353
|
-
let bb = list[ii + 1];
|
|
1354
|
-
let cc;
|
|
1355
|
-
if (ii % 2 === 1) {
|
|
1356
|
-
return;
|
|
1357
|
-
}
|
|
1358
|
-
ii *= 0.5;
|
|
1359
|
-
cc = noop(
|
|
1360
|
-
await dbExecAsync({
|
|
1361
|
-
db,
|
|
1362
|
-
responseType: "dict",
|
|
1363
|
-
sql: `SELECT ${sql} AS val`
|
|
1364
|
-
})
|
|
1365
|
-
)[0][0].val;
|
|
1366
|
-
assertJsonEqual(bb, cc, {
|
|
1367
|
-
bb,
|
|
1368
|
-
cc,
|
|
1369
|
-
ii,
|
|
1370
|
-
sql
|
|
1371
|
-
});
|
|
1372
|
-
});
|
|
1373
|
-
}
|
|
766
|
+
// This function will recursively deep-copy <obj> with keys sorted.
|
|
1374
767
|
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
assertErrorThrownAsync(function () {
|
|
1379
|
-
return dbExecWithRetryAsync({});
|
|
1380
|
-
}, "invalid or closed db");
|
|
768
|
+
let sorted;
|
|
769
|
+
if (typeof obj !== "object" || !obj) {
|
|
770
|
+
return obj;
|
|
1381
771
|
}
|
|
1382
772
|
|
|
1383
|
-
|
|
1384
|
-
// this function will test dbMemoryXxx's handling-behavior
|
|
1385
|
-
let data;
|
|
1386
|
-
let db = await dbOpenAsync({
|
|
1387
|
-
filename: ":memory:"
|
|
1388
|
-
});
|
|
1389
|
-
// test null-case handling-behavior
|
|
1390
|
-
assertErrorThrownAsync(function () {
|
|
1391
|
-
return dbMemoryLoadAsync({
|
|
1392
|
-
db
|
|
1393
|
-
});
|
|
1394
|
-
}, "invalid filename undefined");
|
|
1395
|
-
assertErrorThrownAsync(function () {
|
|
1396
|
-
return dbMemorySaveAsync({
|
|
1397
|
-
db
|
|
1398
|
-
});
|
|
1399
|
-
}, "invalid filename undefined");
|
|
1400
|
-
await dbExecAsync({
|
|
1401
|
-
db,
|
|
1402
|
-
sql: "CREATE TABLE t01 AS SELECT 1 AS c01"
|
|
1403
|
-
});
|
|
1404
|
-
await dbMemorySaveAsync({
|
|
1405
|
-
db,
|
|
1406
|
-
filename: ".testDbMemoryXxx.sqlite"
|
|
1407
|
-
});
|
|
1408
|
-
db = await dbOpenAsync({
|
|
1409
|
-
filename: ":memory:"
|
|
1410
|
-
});
|
|
1411
|
-
await dbMemoryLoadAsync({
|
|
1412
|
-
db,
|
|
1413
|
-
filename: ".testDbMemoryXxx.sqlite"
|
|
1414
|
-
});
|
|
1415
|
-
data = await dbExecAsync({
|
|
1416
|
-
db,
|
|
1417
|
-
sql: "SELECT * FROM t01"
|
|
1418
|
-
});
|
|
1419
|
-
assertJsonEqual(data, [
|
|
1420
|
-
[
|
|
1421
|
-
{
|
|
1422
|
-
c01: 1
|
|
1423
|
-
}
|
|
1424
|
-
]
|
|
1425
|
-
]);
|
|
1426
|
-
}
|
|
773
|
+
// Recursively deep-copy list with child-keys sorted.
|
|
1427
774
|
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
// test null-case handling-behavior
|
|
1431
|
-
assertErrorThrownAsync(function () {
|
|
1432
|
-
return dbOpenAsync({});
|
|
1433
|
-
}, "invalid filename undefined");
|
|
775
|
+
if (Array.isArray(obj)) {
|
|
776
|
+
return obj.map(objectDeepCopyWithKeysSorted);
|
|
1434
777
|
}
|
|
1435
778
|
|
|
1436
|
-
|
|
1437
|
-
// this function will test dbTableInsertAsync's handling-behavior
|
|
1438
|
-
let db = await dbOpenAsync({
|
|
1439
|
-
filename: ":memory:"
|
|
1440
|
-
});
|
|
1441
|
-
// test error handling-behavior
|
|
1442
|
-
[
|
|
1443
|
-
[
|
|
1444
|
-
undefined,
|
|
1445
|
-
(
|
|
1446
|
-
/invalid rowList undefined/
|
|
1447
|
-
)
|
|
1448
|
-
], [
|
|
1449
|
-
[
|
|
1450
|
-
[]
|
|
1451
|
-
],
|
|
1452
|
-
(
|
|
1453
|
-
/invalid colList \[\]/
|
|
1454
|
-
)
|
|
1455
|
-
], [
|
|
1456
|
-
[
|
|
1457
|
-
{}
|
|
1458
|
-
],
|
|
1459
|
-
(
|
|
1460
|
-
/invalid colList \[\]/
|
|
1461
|
-
)
|
|
1462
|
-
]
|
|
1463
|
-
].forEach(function ([
|
|
1464
|
-
rowList, rgx
|
|
1465
|
-
]) {
|
|
1466
|
-
assertErrorThrownAsync(
|
|
1467
|
-
dbTableInsertAsync.bind(
|
|
1468
|
-
undefined,
|
|
1469
|
-
{
|
|
1470
|
-
rowList
|
|
1471
|
-
}
|
|
1472
|
-
),
|
|
1473
|
-
rgx
|
|
1474
|
-
);
|
|
1475
|
-
});
|
|
1476
|
-
// test csv handling-behavior
|
|
1477
|
-
[
|
|
1478
|
-
[
|
|
1479
|
-
"0", undefined
|
|
1480
|
-
],
|
|
1481
|
-
[
|
|
1482
|
-
"0,0,0\n1,1,1",
|
|
1483
|
-
[
|
|
1484
|
-
[
|
|
1485
|
-
"c_0", "c_0_2", "c_0_3"
|
|
1486
|
-
], [
|
|
1487
|
-
"1", "1", "1"
|
|
1488
|
-
]
|
|
1489
|
-
]
|
|
1490
|
-
],
|
|
1491
|
-
[
|
|
1492
|
-
(
|
|
1493
|
-
"c1,c1,c2\n"
|
|
1494
|
-
+ "1, 2 \n"
|
|
1495
|
-
+ `"1","""2""","3\r\n"\n`
|
|
1496
|
-
+ "\n"
|
|
1497
|
-
+ "1,2,3\n"
|
|
1498
|
-
),
|
|
1499
|
-
[
|
|
1500
|
-
[
|
|
1501
|
-
"c1", "c1_2", "c2"
|
|
1502
|
-
], [
|
|
1503
|
-
"1", " 2 ", null
|
|
1504
|
-
], [
|
|
1505
|
-
"1", "\"2\"", "3\n"
|
|
1506
|
-
], [
|
|
1507
|
-
"", null, null
|
|
1508
|
-
], [
|
|
1509
|
-
"1", "2", "3"
|
|
1510
|
-
]
|
|
1511
|
-
]
|
|
1512
|
-
]
|
|
1513
|
-
].forEach(async function ([
|
|
1514
|
-
aa, bb
|
|
1515
|
-
], ii) {
|
|
1516
|
-
let cc = noop(
|
|
1517
|
-
await dbExecAsync({
|
|
1518
|
-
db,
|
|
1519
|
-
responseType: "list",
|
|
1520
|
-
sql: `SELECT * FROM temp.csv_${ii}`,
|
|
1521
|
-
tmpCsv: aa,
|
|
1522
|
-
tmpTableName: "csv_" + ii
|
|
1523
|
-
})
|
|
1524
|
-
)[0];
|
|
1525
|
-
assertOrThrow(
|
|
1526
|
-
JSON.stringify(bb) === JSON.stringify(cc),
|
|
1527
|
-
JSON.stringify([
|
|
1528
|
-
ii, aa, bb, cc
|
|
1529
|
-
], undefined, 4)
|
|
1530
|
-
);
|
|
1531
|
-
});
|
|
1532
|
-
}
|
|
1533
|
-
|
|
1534
|
-
async function testSqlError() {
|
|
1535
|
-
// this function will test sql-error's handling-behavior
|
|
1536
|
-
let db = await dbOpenAsync({
|
|
1537
|
-
filename: ":memory:"
|
|
1538
|
-
});
|
|
1539
|
-
[
|
|
1540
|
-
1, "SQL logic error",
|
|
1541
|
-
2, "unknown error",
|
|
1542
|
-
3, "access permission denied",
|
|
1543
|
-
4, "query aborted",
|
|
1544
|
-
5, "database is locked",
|
|
1545
|
-
6, "database table is locked",
|
|
1546
|
-
7, "out of memory",
|
|
1547
|
-
8, "attempt to write a readonly database",
|
|
1548
|
-
9, "interrupted",
|
|
1549
|
-
10, "disk I/O error",
|
|
1550
|
-
11, "database disk image is malformed",
|
|
1551
|
-
12, "unknown operation",
|
|
1552
|
-
13, "database or disk is full",
|
|
1553
|
-
14, "unable to open database file",
|
|
1554
|
-
15, "locking protocol",
|
|
1555
|
-
16, "unknown error",
|
|
1556
|
-
17, "database schema has changed",
|
|
1557
|
-
18, "string or blob too big",
|
|
1558
|
-
19, "constraint failed",
|
|
1559
|
-
20, "datatype mismatch",
|
|
1560
|
-
21, "bad parameter or other API misuse",
|
|
1561
|
-
22, "unknown error",
|
|
1562
|
-
23, "authorization denied",
|
|
1563
|
-
24, "unknown error",
|
|
1564
|
-
25, "column index out of range",
|
|
1565
|
-
26, "file is not a database",
|
|
1566
|
-
27, "notification message",
|
|
1567
|
-
28, "warning message",
|
|
1568
|
-
100, "unknown error",
|
|
1569
|
-
101, "unknown error"
|
|
1570
|
-
].forEach(function (sql, ii, list) {
|
|
1571
|
-
let bb = list[ii + 1];
|
|
1572
|
-
if (ii % 2 === 1) {
|
|
1573
|
-
return;
|
|
1574
|
-
}
|
|
1575
|
-
ii *= 0.5;
|
|
1576
|
-
sql = `SELECT throwerror(${sql})`;
|
|
1577
|
-
assertErrorThrownAsync(function () {
|
|
1578
|
-
return dbExecAsync({
|
|
1579
|
-
db,
|
|
1580
|
-
sql
|
|
1581
|
-
});
|
|
1582
|
-
}, bb);
|
|
1583
|
-
});
|
|
1584
|
-
}
|
|
779
|
+
// Recursively deep-copy obj with keys sorted.
|
|
1585
780
|
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
{
|
|
1593
|
-
aa: [
|
|
1594
|
-
[
|
|
1595
|
-
{
|
|
1596
|
-
"c01": ""
|
|
1597
|
-
}
|
|
1598
|
-
]
|
|
1599
|
-
],
|
|
1600
|
-
sql: "SELECT tobase64(NULL) AS c01"
|
|
1601
|
-
},
|
|
1602
|
-
{
|
|
1603
|
-
aa: [
|
|
1604
|
-
[
|
|
1605
|
-
{
|
|
1606
|
-
"c01": ""
|
|
1607
|
-
}
|
|
1608
|
-
]
|
|
1609
|
-
],
|
|
1610
|
-
sql: "SELECT tobase64(?) AS c01"
|
|
1611
|
-
},
|
|
1612
|
-
{
|
|
1613
|
-
aa: [
|
|
1614
|
-
[
|
|
1615
|
-
{
|
|
1616
|
-
"c01": "AAAAAAAAAAA="
|
|
1617
|
-
}
|
|
1618
|
-
]
|
|
1619
|
-
],
|
|
1620
|
-
bindList: [
|
|
1621
|
-
new Uint8Array(8)
|
|
1622
|
-
],
|
|
1623
|
-
sql: "SELECT tobase64(uncompress(compress(?))) AS c01"
|
|
1624
|
-
}
|
|
1625
|
-
].forEach(async function ({
|
|
1626
|
-
aa,
|
|
1627
|
-
bindList,
|
|
1628
|
-
sql
|
|
1629
|
-
}) {
|
|
1630
|
-
let bb = await dbExecAsync({
|
|
1631
|
-
bindList,
|
|
1632
|
-
db,
|
|
1633
|
-
sql
|
|
1634
|
-
});
|
|
1635
|
-
assertJsonEqual(aa, bb);
|
|
1636
|
-
});
|
|
1637
|
-
}
|
|
781
|
+
sorted = Object.create(null);
|
|
782
|
+
Object.keys(obj).sort().forEach(function (key) {
|
|
783
|
+
sorted[key] = objectDeepCopyWithKeysSorted(obj[key]);
|
|
784
|
+
});
|
|
785
|
+
return sorted;
|
|
786
|
+
}
|
|
1638
787
|
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
788
|
+
async function sqlMessagePost(baton, cFuncName, ...argList) {
|
|
789
|
+
|
|
790
|
+
// This function will post msg to <sqlWorker> and return result
|
|
791
|
+
|
|
792
|
+
let errStack;
|
|
793
|
+
let id;
|
|
794
|
+
let result;
|
|
795
|
+
let timeElapsed = Date.now();
|
|
796
|
+
// increment sqlMessageId
|
|
797
|
+
sqlMessageId += 1;
|
|
798
|
+
id = sqlMessageId;
|
|
799
|
+
// postMessage to web-worker
|
|
800
|
+
sqlWorker.postMessage(
|
|
801
|
+
{
|
|
802
|
+
argList,
|
|
803
|
+
baton,
|
|
804
|
+
cFuncName,
|
|
805
|
+
id
|
|
806
|
+
},
|
|
807
|
+
// transfer arraybuffer without copying
|
|
1644
808
|
[
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
[], 0.125, 1
|
|
1651
|
-
], [
|
|
1652
|
-
[], 0.25, 2
|
|
1653
|
-
], [
|
|
1654
|
-
[], 0.375, 3
|
|
1655
|
-
], [
|
|
1656
|
-
[], 0.5, 4
|
|
1657
|
-
], [
|
|
1658
|
-
[], 0.625, 5
|
|
1659
|
-
], [
|
|
1660
|
-
[], 0.75, 6
|
|
1661
|
-
], [
|
|
1662
|
-
[], 0.875, 7
|
|
1663
|
-
], [
|
|
1664
|
-
[], 1, 8
|
|
1665
|
-
], [
|
|
1666
|
-
[], 99, 8
|
|
1667
|
-
], [
|
|
1668
|
-
[
|
|
1669
|
-
0.5
|
|
1670
|
-
], 0, 0.5
|
|
1671
|
-
], [
|
|
1672
|
-
[
|
|
1673
|
-
0.5
|
|
1674
|
-
], 0.125, 0.5
|
|
1675
|
-
], [
|
|
1676
|
-
[
|
|
1677
|
-
1.5
|
|
1678
|
-
], 0.25, 1.5
|
|
1679
|
-
], [
|
|
1680
|
-
[
|
|
1681
|
-
2.5
|
|
1682
|
-
], 0.375, 2.5
|
|
1683
|
-
], [
|
|
1684
|
-
[
|
|
1685
|
-
3.5
|
|
1686
|
-
], 0.5, 3.5
|
|
1687
|
-
], [
|
|
1688
|
-
[
|
|
1689
|
-
4.5
|
|
1690
|
-
], 0.625, 4.5
|
|
1691
|
-
], [
|
|
1692
|
-
[
|
|
1693
|
-
5.5
|
|
1694
|
-
], 0.75, 5.5
|
|
1695
|
-
], [
|
|
1696
|
-
[
|
|
1697
|
-
6.5
|
|
1698
|
-
], 0.875, 6.5
|
|
1699
|
-
], [
|
|
1700
|
-
[
|
|
1701
|
-
7.5
|
|
1702
|
-
], 1, 8
|
|
1703
|
-
]
|
|
1704
|
-
].forEach(async function ([
|
|
1705
|
-
data, kk, expected
|
|
1706
|
-
], ii) {
|
|
1707
|
-
let actual;
|
|
1708
|
-
data = data.concat([
|
|
1709
|
-
undefined, undefined, 8, 7, 6, 5, 4, 3, 2, 1, undefined
|
|
1710
|
-
]);
|
|
1711
|
-
actual = noop(
|
|
1712
|
-
await dbExecAsync({
|
|
1713
|
-
db,
|
|
1714
|
-
sql: (`
|
|
1715
|
-
SELECT kthpercentile(val, ${kk}) AS val FROM __tmp${ii};
|
|
1716
|
-
-- test null-case handling-behavior
|
|
1717
|
-
SELECT kthpercentile(val, ${kk}) AS val FROM __tmp${ii} WHERE 0;
|
|
1718
|
-
`),
|
|
1719
|
-
tmpRowList: data.map(function (val) {
|
|
1720
|
-
return {
|
|
1721
|
-
val
|
|
1722
|
-
};
|
|
1723
|
-
}),
|
|
1724
|
-
tmpTableName: `__tmp${ii}`
|
|
1725
|
-
})
|
|
1726
|
-
)[0][0].val;
|
|
1727
|
-
assertJsonEqual(actual, expected, {
|
|
1728
|
-
data,
|
|
1729
|
-
kk
|
|
1730
|
-
});
|
|
1731
|
-
});
|
|
1732
|
-
}
|
|
1733
|
-
|
|
1734
|
-
addon = requireCjs(
|
|
1735
|
-
"./_binary_sqlmath"
|
|
1736
|
-
+ "_napi8"
|
|
1737
|
-
+ "_" + process.platform
|
|
1738
|
-
+ "_" + process.arch
|
|
1739
|
-
+ ".node"
|
|
809
|
+
baton.buffer,
|
|
810
|
+
...argList.filter(function (elem) {
|
|
811
|
+
return elem && elem.constructor === ArrayBuffer;
|
|
812
|
+
})
|
|
813
|
+
]
|
|
1740
814
|
);
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
815
|
+
// preserve stack-trace
|
|
816
|
+
errStack = new Error().stack.replace((
|
|
817
|
+
/.*$/m
|
|
818
|
+
), "");
|
|
819
|
+
// await result from web-worker
|
|
820
|
+
result = await new Promise(function (resolve) {
|
|
821
|
+
sqlMessageDict[id] = resolve;
|
|
822
|
+
});
|
|
823
|
+
// cleanup sqlMessageDict
|
|
824
|
+
delete sqlMessageDict[id];
|
|
825
|
+
// debug slow postMessage
|
|
826
|
+
timeElapsed = Date.now() - timeElapsed;
|
|
827
|
+
if (timeElapsed > 500) {
|
|
828
|
+
consoleError(
|
|
829
|
+
"sqlMessagePost - " + JSON.stringify({
|
|
830
|
+
cFuncName,
|
|
831
|
+
timeElapsed
|
|
832
|
+
}) + errStack
|
|
833
|
+
);
|
|
834
|
+
}
|
|
835
|
+
assertOrThrow(!result.errmsg, result.errmsg);
|
|
836
|
+
return [
|
|
837
|
+
result.baton, result.cFuncName, ...result.argList
|
|
1754
838
|
];
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
SQLITE_OPEN_READONLY,
|
|
1769
|
-
SQLITE_OPEN_READWRITE,
|
|
1770
|
-
SQLITE_OPEN_SHAREDCACHE,
|
|
1771
|
-
SQLITE_OPEN_SUBJOURNAL,
|
|
1772
|
-
SQLITE_OPEN_SUPER_JOURNAL,
|
|
1773
|
-
SQLITE_OPEN_TEMP_DB,
|
|
1774
|
-
SQLITE_OPEN_TEMP_JOURNAL,
|
|
1775
|
-
SQLITE_OPEN_TRANSIENT_DB,
|
|
1776
|
-
SQLITE_OPEN_URI,
|
|
1777
|
-
SQLITE_OPEN_WAL,
|
|
1778
|
-
assertErrorThrownAsync,
|
|
1779
|
-
assertNumericalEqual,
|
|
1780
|
-
dbCloseAsync,
|
|
1781
|
-
dbExecAsync,
|
|
1782
|
-
dbExecWithRetryAsync,
|
|
1783
|
-
dbGetLastBlobAsync,
|
|
1784
|
-
dbMemoryLoadAsync,
|
|
1785
|
-
dbMemorySaveAsync,
|
|
1786
|
-
dbOpenAsync,
|
|
1787
|
-
dbTableInsertAsync,
|
|
1788
|
-
debugInline,
|
|
1789
|
-
objectDeepCopyWithKeysSorted,
|
|
1790
|
-
testAll,
|
|
1791
|
-
testList
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
async function sqlmathInit() {
|
|
842
|
+
dbFinalizationRegistry = new FinalizationRegistry(function ({
|
|
843
|
+
afterFinalization,
|
|
844
|
+
ptr
|
|
845
|
+
}) {
|
|
846
|
+
// This function will auto-close any open sqlite3-db-pointer,
|
|
847
|
+
// after its js-wrapper has been garbage-collected
|
|
848
|
+
cCallAsync(undefined, "_dbClose", ptr[0]);
|
|
849
|
+
if (afterFinalization) {
|
|
850
|
+
afterFinalization();
|
|
851
|
+
}
|
|
1792
852
|
});
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
853
|
+
|
|
854
|
+
// Feature-detect nodejs.
|
|
855
|
+
|
|
856
|
+
if (
|
|
857
|
+
typeof process === "object"
|
|
858
|
+
&& typeof process?.versions?.node === "string"
|
|
859
|
+
) {
|
|
860
|
+
cModule = await import("module");
|
|
861
|
+
cModule = cModule.createRequire(import.meta.url);
|
|
862
|
+
cModule = cModule(
|
|
863
|
+
"./_binary_sqlmath"
|
|
864
|
+
+ "_napi8"
|
|
865
|
+
+ "_" + process.platform
|
|
866
|
+
+ "_" + process.arch
|
|
867
|
+
+ ".node"
|
|
868
|
+
);
|
|
869
|
+
if (process.env.npm_config_mode_test) {
|
|
870
|
+
// mock consoleError
|
|
871
|
+
consoleError = noop;
|
|
872
|
+
}
|
|
1796
873
|
}
|
|
1797
|
-
}
|
|
874
|
+
}
|
|
1798
875
|
|
|
1799
|
-
|
|
876
|
+
function sqlmathWebworkerInit({
|
|
877
|
+
Worker
|
|
878
|
+
}) {
|
|
879
|
+
|
|
880
|
+
// Feature-detect browser.
|
|
881
|
+
|
|
882
|
+
IS_BROWSER = true;
|
|
883
|
+
Worker = Worker || globalThis.Worker;
|
|
884
|
+
sqlWorker = new Worker("sqlmath_wasm.js");
|
|
885
|
+
sqlWorker.onmessage = function ({
|
|
886
|
+
data
|
|
887
|
+
}) {
|
|
888
|
+
sqlMessageDict[data.id](data);
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
await sqlmathInit({});
|
|
893
|
+
|
|
894
|
+
export {
|
|
895
|
+
SQLITE_MAX_LENGTH2,
|
|
896
|
+
SQLITE_OPEN_AUTOPROXY,
|
|
897
|
+
SQLITE_OPEN_CREATE,
|
|
898
|
+
SQLITE_OPEN_DELETEONCLOSE,
|
|
899
|
+
SQLITE_OPEN_EXCLUSIVE,
|
|
900
|
+
SQLITE_OPEN_FULLMUTEX,
|
|
901
|
+
SQLITE_OPEN_MAIN_DB,
|
|
902
|
+
SQLITE_OPEN_MAIN_JOURNAL,
|
|
903
|
+
SQLITE_OPEN_MEMORY,
|
|
904
|
+
SQLITE_OPEN_NOFOLLOW,
|
|
905
|
+
SQLITE_OPEN_NOMUTEX,
|
|
906
|
+
SQLITE_OPEN_PRIVATECACHE,
|
|
907
|
+
SQLITE_OPEN_READONLY,
|
|
908
|
+
SQLITE_OPEN_READWRITE,
|
|
909
|
+
SQLITE_OPEN_SHAREDCACHE,
|
|
910
|
+
SQLITE_OPEN_SUBJOURNAL,
|
|
911
|
+
SQLITE_OPEN_SUPER_JOURNAL,
|
|
912
|
+
SQLITE_OPEN_TEMP_DB,
|
|
913
|
+
SQLITE_OPEN_TEMP_JOURNAL,
|
|
914
|
+
SQLITE_OPEN_TRANSIENT_DB,
|
|
915
|
+
SQLITE_OPEN_URI,
|
|
916
|
+
SQLITE_OPEN_WAL,
|
|
917
|
+
assertJsonEqual,
|
|
918
|
+
assertNumericalEqual,
|
|
919
|
+
assertOrThrow,
|
|
920
|
+
dbCloseAsync,
|
|
921
|
+
dbExecAndReturnLastBlobAsync,
|
|
922
|
+
dbExecAsync,
|
|
923
|
+
dbFileExportAsync,
|
|
924
|
+
dbFileImportAsync,
|
|
925
|
+
dbNoopAsync,
|
|
926
|
+
dbOpenAsync,
|
|
927
|
+
debugInline,
|
|
928
|
+
jsbatonValueString,
|
|
929
|
+
noop,
|
|
930
|
+
objectDeepCopyWithKeysSorted,
|
|
931
|
+
sqlmathInit,
|
|
932
|
+
sqlmathWebworkerInit
|
|
933
|
+
};
|