sqlmath 2022.4.28 → 2022.5.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/sqlmath.mjs CHANGED
@@ -1,15 +1,35 @@
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*/
2
25
  "use strict";
3
26
  import {createRequire} from "module";
4
27
  import jslint from "./jslint.mjs";
5
28
 
6
29
  let {
7
- assertErrorThrownAsync,
8
- assertJsonEqual,
9
30
  assertOrThrow,
10
31
  debugInline,
11
- noop,
12
- objectDeepCopyWithKeysSorted
32
+ noop
13
33
  } = jslint;
14
34
  let local = Object.assign({}, jslint);
15
35
 
@@ -62,7 +82,6 @@ file sqlmath.js
62
82
  let consoleError = console.error;
63
83
  let dbDict = new WeakMap(); // private map of sqlite-database-connections
64
84
  let requireCjs = createRequire(import.meta.url);
65
- let testList;
66
85
 
67
86
  function cCall(func, argList) {
68
87
  // this function will serialize <argList> to a c <baton>,
@@ -251,6 +270,9 @@ file sqlmath.js
251
270
  + " remaining retry"
252
271
  );
253
272
  retry -= 1;
273
+ await new Promise(function (resolve) {
274
+ setTimeout(resolve, 50);
275
+ });
254
276
  }
255
277
  }
256
278
  }
@@ -305,7 +327,9 @@ file sqlmath.js
305
327
  // const char *zVfs /* Name of VFS module to use */
306
328
  // );
307
329
  let connPool;
308
- let db = {};
330
+ let db = {
331
+ filename
332
+ };
309
333
  assertOrThrow(
310
334
  typeof filename === "string",
311
335
  "invalid filename " + filename
@@ -330,6 +354,7 @@ file sqlmath.js
330
354
  dbDict.set(db, {
331
355
  busy: 0,
332
356
  connPool,
357
+ filename,
333
358
  ii: 0,
334
359
  ptr: 0n
335
360
  });
@@ -837,900 +862,6 @@ Definition of the CSV Format
837
862
  return rowList;
838
863
  }
839
864
 
840
- function testAll() {
841
- // this function will run all tests
842
- testList.forEach(function (testFunc) {
843
- testFunc();
844
- });
845
- }
846
-
847
- function testAssertXxx() {
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
- }
861
-
862
- function testCcall() {
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
- }
891
-
892
- async function testDbBind() {
893
- // this function will test db's bind handling-behavior
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
- }
1203
-
1204
- async function testDbCloseAsync() {
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
- }
1218
-
1219
- async function testDbExecAsync() {
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
- }
1374
-
1375
- function testDbExecWithRetryAsync() {
1376
- // this function will test dbExecWithRetryAsync's handling-behavior
1377
- // test null-case handling-behavior
1378
- assertErrorThrownAsync(function () {
1379
- return dbExecWithRetryAsync({});
1380
- }, "invalid or closed db");
1381
- }
1382
-
1383
- async function testDbMemoryXxx() {
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
- }
1427
-
1428
- function testDbOpenAsync() {
1429
- // this function will test dbOpenAsync's handling-behavior
1430
- // test null-case handling-behavior
1431
- assertErrorThrownAsync(function () {
1432
- return dbOpenAsync({});
1433
- }, "invalid filename undefined");
1434
- }
1435
-
1436
- async function testDbTableInsertAsync() {
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
- }
1585
-
1586
- async function testSqlExt() {
1587
- // this function will test sql-ext's handling-behavior
1588
- let db = await dbOpenAsync({
1589
- filename: ":memory:"
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
- }
1638
-
1639
- async function testSqlKthpercentile() {
1640
- // this function will test sql-kthpercentile's handling-behavior
1641
- let db = await dbOpenAsync({
1642
- filename: ":memory:"
1643
- });
1644
- [
1645
- [
1646
- [], -99, 1
1647
- ], [
1648
- [], 0, 1
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
865
  addon = requireCjs(
1735
866
  "./_binary_sqlmath"
1736
867
  + "_napi8"
@@ -1738,21 +869,7 @@ SELECT kthpercentile(val, ${kk}) AS val FROM __tmp${ii} WHERE 0;
1738
869
  + "_" + process.arch
1739
870
  + ".node"
1740
871
  );
1741
- testList = [
1742
- testAssertXxx,
1743
- testCcall,
1744
- testDbBind,
1745
- testDbCloseAsync,
1746
- testDbExecAsync,
1747
- testDbExecWithRetryAsync,
1748
- testDbMemoryXxx,
1749
- testDbOpenAsync,
1750
- testDbTableInsertAsync,
1751
- testSqlError,
1752
- testSqlExt,
1753
- testSqlKthpercentile
1754
- ];
1755
- Object.assign(local, {
872
+ Object.assign(local, jslint, {
1756
873
  SQLITE_MAX_LENGTH2,
1757
874
  SQLITE_OPEN_AUTOPROXY,
1758
875
  SQLITE_OPEN_CREATE,
@@ -1775,8 +892,8 @@ SELECT kthpercentile(val, ${kk}) AS val FROM __tmp${ii} WHERE 0;
1775
892
  SQLITE_OPEN_TRANSIENT_DB,
1776
893
  SQLITE_OPEN_URI,
1777
894
  SQLITE_OPEN_WAL,
1778
- assertErrorThrownAsync,
1779
895
  assertNumericalEqual,
896
+ cCall,
1780
897
  dbCloseAsync,
1781
898
  dbExecAsync,
1782
899
  dbExecWithRetryAsync,
@@ -1785,10 +902,7 @@ SELECT kthpercentile(val, ${kk}) AS val FROM __tmp${ii} WHERE 0;
1785
902
  dbMemorySaveAsync,
1786
903
  dbOpenAsync,
1787
904
  dbTableInsertAsync,
1788
- debugInline,
1789
- objectDeepCopyWithKeysSorted,
1790
- testAll,
1791
- testList
905
+ debugInline
1792
906
  });
1793
907
  if (process.env.npm_config_mode_test) {
1794
908
  // mock consoleError