jsql-neo 6.8.4 → 6.8.7
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/README.md +1 -1
- package/bin/jsql +5 -2
- package/index.js +1 -1
- package/lib/bigint_json.js +12 -0
- package/lib/cache.js +26 -4
- package/lib/columnar_fast.js +3 -1
- package/lib/database.js +171 -47
- package/lib/date-types.js +27 -19
- package/lib/errors.js +4 -0
- package/lib/migrate.js +20 -8
- package/lib/mongo_server.js +238 -20
- package/lib/mysql_server.js +27 -22
- package/lib/nedb_compat.js +101 -11
- package/lib/pg_server.js +53 -12
- package/lib/redis_server.js +198 -67
- package/lib/sql.js +228 -35
- package/lib/sqlite_compat.js +17 -0
- package/lib/table.js +55 -12
- package/package.json +3 -3
- package/test/extra-coverage.test.js +4 -3
- package/test/issue-162-179.test.js +220 -0
- package/test/issue-181-186.test.js +153 -0
- package/test/issue-187.test.js +36 -0
- package/test/issue-188-237.test.js +481 -0
- package/test/issue-238-241.test.js +65 -0
- package/test/issue-63-68.test.js +2 -2
package/lib/table.js
CHANGED
|
@@ -239,6 +239,8 @@ class Table {
|
|
|
239
239
|
this._defaults = {};
|
|
240
240
|
this._dateFields = {}; // { fieldName: 'date'|'datetime'|'timestamp' }
|
|
241
241
|
|
|
242
|
+
// #198:先收集全部主键列,构造结束后再决定单列主键还是复合主键
|
|
243
|
+
this._primaryKeys = [];
|
|
242
244
|
for (const [field, def] of Object.entries(schema)) {
|
|
243
245
|
// schema 字段名归一化:驼峰 primaryKey/autoIncrement 与下划线
|
|
244
246
|
// primary_key/auto_increment 两种写法都要认。
|
|
@@ -255,7 +257,7 @@ class Table {
|
|
|
255
257
|
if (def.auto_increment !== undefined) delete def.auto_increment;
|
|
256
258
|
}
|
|
257
259
|
const isPk = !!(def.primaryKey || def.primary === true);
|
|
258
|
-
if (isPk) this.
|
|
260
|
+
if (isPk) this._primaryKeys.push(field);
|
|
259
261
|
if (def.autoIncrement) {
|
|
260
262
|
this._autoIncrementField = field;
|
|
261
263
|
if (isPk) this._primaryKey = field;
|
|
@@ -286,6 +288,25 @@ class Table {
|
|
|
286
288
|
this._softDelete = true;
|
|
287
289
|
}
|
|
288
290
|
|
|
291
|
+
// #198:单列主键维持既有 O(1) 索引;复合主键转为复合唯一组按元组判重
|
|
292
|
+
if (this._primaryKeys.length === 1) {
|
|
293
|
+
this._primaryKey = this._primaryKeys[0];
|
|
294
|
+
} else if (this._primaryKeys.length > 1) {
|
|
295
|
+
this._primaryKey = null;
|
|
296
|
+
const columns = this._primaryKeys.slice();
|
|
297
|
+
this._uniqueIndexes.push({ name: 'PRIMARY', columns, key: columns.join('\u001f') });
|
|
298
|
+
}
|
|
299
|
+
// #205:表级 UNIQUE(a,b) 由解析层登记为复合唯一组,不能拆成单列唯一
|
|
300
|
+
if (Array.isArray(schema.__uniqueGroups__)) {
|
|
301
|
+
for (const cols of schema.__uniqueGroups__) {
|
|
302
|
+
const key = cols.join('\u001f');
|
|
303
|
+
if (!this._uniqueIndexes.some(u => u.key === key)) {
|
|
304
|
+
this._uniqueIndexes.push({ name: null, columns: cols.slice(), key });
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
delete schema.__uniqueGroups__;
|
|
308
|
+
}
|
|
309
|
+
|
|
289
310
|
this._pkIndex = this._primaryKey ? new Map() : null;
|
|
290
311
|
this._cachedSchemaFields = Object.keys(schema).filter(f => f !== '_softDelete');
|
|
291
312
|
this._cachedDateFields = Object.keys(this._dateFields);
|
|
@@ -562,7 +583,10 @@ class Table {
|
|
|
562
583
|
}
|
|
563
584
|
}
|
|
564
585
|
let autoIncSeq = 0;
|
|
565
|
-
|
|
586
|
+
// #201:先在临时数组上完成全部校验,全部通过后再统一落库;
|
|
587
|
+
// 任一行违反约束则整条语句原子失败,不残留前序行。
|
|
588
|
+
const prepared = new Array(N);
|
|
589
|
+
const pkSeen = new Set();
|
|
566
590
|
for (let i = 0; i < N; i++) {
|
|
567
591
|
// 在副本上做默认值/计算列/类型归一,避免污染调用方传入的原始对象
|
|
568
592
|
let data = { ...items[i] };
|
|
@@ -579,7 +603,7 @@ class Table {
|
|
|
579
603
|
// 与 _validateConstraints 保持一致:主键(非自增)不接受显式 NULL
|
|
580
604
|
if ((def.primaryKey || def.primary === true) && !def.autoIncrement
|
|
581
605
|
&& data[field] === null) throw createError('ER_BAD_NULL_ERROR', field);
|
|
582
|
-
if (def.unique && data[field] !== undefined) {
|
|
606
|
+
if (def.unique && data[field] !== undefined && data[field] !== null) {
|
|
583
607
|
if (uniqueTracker[field].has(data[field])) throw createError('ER_DUP_ENTRY', String(data[field]), field);
|
|
584
608
|
uniqueTracker[field].add(data[field]);
|
|
585
609
|
}
|
|
@@ -603,6 +627,8 @@ class Table {
|
|
|
603
627
|
if (this._pkIndex && this._pkIndex.has(pkVal)) {
|
|
604
628
|
throw createError('ER_DUP_ENTRY', String(pkVal), this._primaryKey);
|
|
605
629
|
}
|
|
630
|
+
if (pkSeen.has(pkVal)) throw createError('ER_DUP_ENTRY', String(pkVal), this._primaryKey);
|
|
631
|
+
pkSeen.add(pkVal);
|
|
606
632
|
}
|
|
607
633
|
for (const fk of foreignKeys) {
|
|
608
634
|
if (data[fk.field] !== undefined && data[fk.field] !== null) {
|
|
@@ -620,13 +646,17 @@ class Table {
|
|
|
620
646
|
} else if (autoIncField && data[autoIncField] > this._autoIncrement) {
|
|
621
647
|
this._autoIncrement = data[autoIncField];
|
|
622
648
|
}
|
|
649
|
+
prepared[i] = data;
|
|
650
|
+
}
|
|
651
|
+
const result = new Array(N);
|
|
652
|
+
const baseRowIdx = this._rows.length;
|
|
653
|
+
for (let i = 0; i < N; i++) {
|
|
654
|
+
const data = prepared[i];
|
|
623
655
|
const rowIdx = this._rows.length;
|
|
624
656
|
this._rows.push(Object.assign({}, data));
|
|
625
657
|
if (this._pkIndex && this._primaryKey) {
|
|
626
658
|
const pkVal = data[this._primaryKey];
|
|
627
659
|
if (pkVal !== undefined && pkVal !== null) {
|
|
628
|
-
// P1-1: insertMany 主键重复检查
|
|
629
|
-
if (this._pkIndex.has(pkVal)) throw createError('ER_DUP_ENTRY', String(pkVal), this._primaryKey);
|
|
630
660
|
this._pkIndex.set(pkVal, rowIdx);
|
|
631
661
|
}
|
|
632
662
|
}
|
|
@@ -1218,6 +1248,14 @@ class Table {
|
|
|
1218
1248
|
const columns = Array.isArray(field) ? field.slice() : [field];
|
|
1219
1249
|
if (opts && opts.unique) {
|
|
1220
1250
|
const key = columns.join('\u001f');
|
|
1251
|
+
// #207:在已有重复键的数据上建唯一索引必须失败,不能静默建成
|
|
1252
|
+
const seen = new Set();
|
|
1253
|
+
for (const row of this._rows) {
|
|
1254
|
+
if (columns.some(c => row[c] === undefined || row[c] === null)) continue;
|
|
1255
|
+
const k = columns.map(c => String(row[c])).join('\u001f');
|
|
1256
|
+
if (seen.has(k)) throw createError('ER_DUP_ENTRY', k, columns.join(','));
|
|
1257
|
+
seen.add(k);
|
|
1258
|
+
}
|
|
1221
1259
|
if (!this._uniqueIndexes.some(u => u.key === key)) {
|
|
1222
1260
|
this._uniqueIndexes.push({ name: opts.name, columns, key });
|
|
1223
1261
|
}
|
|
@@ -1884,8 +1922,9 @@ class Table {
|
|
|
1884
1922
|
throw createError('ER_BAD_NULL_ERROR', field);
|
|
1885
1923
|
}
|
|
1886
1924
|
|
|
1887
|
-
// PRIMARY KEY
|
|
1888
|
-
if ((def.primaryKey || def.primary === true) &&
|
|
1925
|
+
// PRIMARY KEY 唯一性(复合主键交给 _checkCompositeUnique 按元组判定)
|
|
1926
|
+
if ((def.primaryKey || def.primary === true) && !(this._primaryKeys && this._primaryKeys.length > 1)
|
|
1927
|
+
&& data[field] !== undefined && data[field] !== null) {
|
|
1889
1928
|
let dup = false;
|
|
1890
1929
|
if (uniqueTracker && uniqueTracker[field]) {
|
|
1891
1930
|
dup = uniqueTracker[field].has(data[field]);
|
|
@@ -1901,8 +1940,8 @@ class Table {
|
|
|
1901
1940
|
}
|
|
1902
1941
|
}
|
|
1903
1942
|
|
|
1904
|
-
// UNIQUE(B-Tree 优化 O(log n)
|
|
1905
|
-
if (def.unique && data[field] !== undefined) {
|
|
1943
|
+
// UNIQUE(B-Tree 优化 O(log n))—— NULL 不参与唯一性
|
|
1944
|
+
if (def.unique && data[field] !== undefined && data[field] !== null) {
|
|
1906
1945
|
if (uniqueTracker && uniqueTracker[field]) {
|
|
1907
1946
|
if (uniqueTracker[field].has(data[field])) {
|
|
1908
1947
|
throw createError('ER_DUP_ENTRY', String(data[field]), field);
|
|
@@ -2061,10 +2100,14 @@ class Table {
|
|
|
2061
2100
|
} else {
|
|
2062
2101
|
throw integerError(t, v);
|
|
2063
2102
|
}
|
|
2064
|
-
} else if (t === 'float' || t === 'double' || t === 'number' || t === 'real') {
|
|
2103
|
+
} else if (t === 'float' || t === 'double' || t === 'number' || t === 'real' || t === 'decimal') {
|
|
2065
2104
|
if (typeof v === 'boolean') throw createError('ER_TRUNCATED_WRONG_VALUE', t, v);
|
|
2066
|
-
if (
|
|
2067
|
-
else if (typeof v === '
|
|
2105
|
+
if (def.numericType === 'numeric' && v && v.__decimalRaw) result[field] = v.__decimalRaw;
|
|
2106
|
+
else if (typeof v === 'number') result[field] = v;
|
|
2107
|
+
else if (typeof v === 'string' && v.trim() !== '' && !Number.isNaN(Number(v))) {
|
|
2108
|
+
result[field] = (def.numericType === 'numeric') ? v : Number(v);
|
|
2109
|
+
}
|
|
2110
|
+
else if (v && v.__decimalRaw) result[field] = Number(v.__decimalRaw);
|
|
2068
2111
|
else throw createError('ER_TRUNCATED_WRONG_VALUE', t, v);
|
|
2069
2112
|
} else if (t === 'boolean' || t === 'bool') {
|
|
2070
2113
|
if (typeof v === 'boolean') result[field] = v;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsql-neo",
|
|
3
|
-
"version": "6.8.
|
|
3
|
+
"version": "6.8.7",
|
|
4
4
|
"description": "JSQL-NEO — Rust-powered embedded database with WASM, REST API, B-Tree indexes, WAL, crash recovery",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -52,12 +52,12 @@
|
|
|
52
52
|
"build:native": "node scripts/build-native.js",
|
|
53
53
|
"check:release": "node scripts/check-release-metadata.js",
|
|
54
54
|
"release:prep": "node scripts/release.js",
|
|
55
|
-
"test": "npm run check:release && node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/native.test.js && node test/json-functions.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/query-opt.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/dml-constraints.test.js && node test/star-nag.test.js && node test/plugin-xql.test.js && node test/bug-regression-2.test.js && node test/issue-34-35-36.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-73-76.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/issue-118-136.test.js && node test/issue-127.test.js && node test/issue-138-156.test.js && node test/issue-157-161.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js && node test/mongo-arrays.test.js && node test/issue-117.test.js",
|
|
55
|
+
"test": "npm run check:release && node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/native.test.js && node test/json-functions.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/query-opt.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/dml-constraints.test.js && node test/star-nag.test.js && node test/plugin-xql.test.js && node test/bug-regression-2.test.js && node test/issue-34-35-36.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-73-76.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/issue-118-136.test.js && node test/issue-127.test.js && node test/issue-138-156.test.js && node test/issue-157-161.test.js && node test/issue-162-179.test.js && node test/issue-181-186.test.js && node test/issue-187.test.js && node test/issue-188-237.test.js && node test/issue-238-241.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js && node test/mongo-arrays.test.js && node test/issue-117.test.js",
|
|
56
56
|
"test:parser": "node test/sql-parser.test.js",
|
|
57
57
|
"test:btree": "node test/btree.test.js",
|
|
58
58
|
"test:native": "node test/native.test.js",
|
|
59
59
|
"test:regress": "node test/regress-5.1.0.js",
|
|
60
|
-
"test:all": "npm run check:release && node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/native.test.js && node test/native-multi-instance.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/wasm.test.js && node test/coverage.js && node test/regress-5.1.0.js && node test/join.test.js && node test/btree.test.js && node test/dml-constraints.test.js && node test/savepoint.test.js && node test/alter-table.test.js && node test/null-semantics.test.js && node test/identifier-keywords.test.js && node test/issue-34-35-36.test.js && node test/sql-semantics.test.js && node test/extra-coverage.test.js && node test/filter-compile.test.js && node test/recursive-cte.test.js && node test/index-ddl.test.js && node test/mongo-group.test.js && node test/multiprotocol.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/issue-118-136.test.js && node test/issue-127.test.js && node test/issue-138-156.test.js && node test/issue-157-161.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js && node test/mongo-arrays.test.js && node test/issue-117.test.js",
|
|
60
|
+
"test:all": "npm run check:release && node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/native.test.js && node test/native-multi-instance.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/wasm.test.js && node test/coverage.js && node test/regress-5.1.0.js && node test/join.test.js && node test/btree.test.js && node test/dml-constraints.test.js && node test/savepoint.test.js && node test/alter-table.test.js && node test/null-semantics.test.js && node test/identifier-keywords.test.js && node test/issue-34-35-36.test.js && node test/sql-semantics.test.js && node test/extra-coverage.test.js && node test/filter-compile.test.js && node test/recursive-cte.test.js && node test/index-ddl.test.js && node test/mongo-group.test.js && node test/multiprotocol.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/issue-118-136.test.js && node test/issue-127.test.js && node test/issue-138-156.test.js && node test/issue-157-161.test.js && node test/issue-162-179.test.js && node test/issue-181-186.test.js && node test/issue-187.test.js && node test/issue-188-237.test.js && node test/issue-238-241.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js && node test/mongo-arrays.test.js && node test/issue-117.test.js",
|
|
61
61
|
"test:coverage": "c8 --all --include='lib/**' --exclude='lib/web_ui.js' --reporter=text --reporter=lcov node test/coverage.js",
|
|
62
62
|
"test:orms": "node examples/orms/run-all.js",
|
|
63
63
|
"test:dml": "node test/dml-constraints.test.js",
|
|
@@ -435,15 +435,16 @@ test('CREATE TABLE 带 DEFAULT 值对新行生效', async () => {
|
|
|
435
435
|
assert.strictEqual(r[0][0], 'x');
|
|
436
436
|
});
|
|
437
437
|
|
|
438
|
-
test('ALTER TABLE ADD COLUMN 带 DEFAULT
|
|
438
|
+
test('ALTER TABLE ADD COLUMN 带 DEFAULT 子句(#197:DEFAULT 对新行生效)', async () => {
|
|
439
439
|
const { q } = makeDb();
|
|
440
440
|
await q('CREATE TABLE t (id INT PRIMARY KEY, n STRING)');
|
|
441
441
|
await q("INSERT INTO t VALUES (1,'a')");
|
|
442
442
|
await q("ALTER TABLE t ADD COLUMN extra STRING DEFAULT 'x'");
|
|
443
443
|
await q("INSERT INTO t (id, n) VALUES (2,'b')");
|
|
444
444
|
const r = await rows(q, 'SELECT id, n, extra FROM t ORDER BY id');
|
|
445
|
-
//
|
|
446
|
-
assert.
|
|
445
|
+
// #197:ALTER ADD COLUMN 的 DEFAULT 同时回填存量行并应用到新行
|
|
446
|
+
assert.strictEqual(r[0][2], 'x');
|
|
447
|
+
assert.strictEqual(r[1][2], 'x');
|
|
447
448
|
});
|
|
448
449
|
|
|
449
450
|
test('ALTER TABLE DROP COLUMN', async () => {
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Regression tests for issues #162-#179.
|
|
3
|
+
* node test/issue-162-179.test.js
|
|
4
|
+
*/
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const os = require('os');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const net = require('net');
|
|
9
|
+
|
|
10
|
+
const ROOT = path.join(__dirname, '..');
|
|
11
|
+
const { Database, executeSQL } = require(path.join(ROOT, 'index.js'));
|
|
12
|
+
const Cache = require(path.join(ROOT, 'lib', 'cache.js'));
|
|
13
|
+
const { createRedisServer } = require(path.join(ROOT, 'lib', 'redis_server.js'));
|
|
14
|
+
const { createMongoServer } = require(path.join(ROOT, 'lib', 'mongo_server.js'));
|
|
15
|
+
const bigintJson = require(path.join(ROOT, 'lib', 'bigint_json.js'));
|
|
16
|
+
|
|
17
|
+
let passed = 0, failed = 0;
|
|
18
|
+
function ok(name, cond, extra) {
|
|
19
|
+
if (cond) { passed++; console.log('[OK]', name); }
|
|
20
|
+
else { failed++; console.log('[FAIL]', name, extra !== undefined ? '→ ' + JSON.stringify(extra) : ''); }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function redisCall(srv, cmd, ...args) {
|
|
24
|
+
try { return { value: srv.execute(cmd, args) }; }
|
|
25
|
+
catch (e) { return { error: e.message }; }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function respFrame(args) {
|
|
29
|
+
return '*' + args.length + '\r\n' + args.map((a) => {
|
|
30
|
+
const b = Buffer.from(String(a));
|
|
31
|
+
return '$' + b.length + '\r\n' + a + '\r\n';
|
|
32
|
+
}).join('');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function sqlCases() {
|
|
36
|
+
const db = new Database(':memory:');
|
|
37
|
+
const q = (sql) => executeSQL(db, sql, { safety: false });
|
|
38
|
+
|
|
39
|
+
await q('CREATE TABLE t (id INT, v INT)');
|
|
40
|
+
await q('INSERT INTO t VALUES (1,10),(2,NULL)');
|
|
41
|
+
let r = await q('SELECT id, ISNULL(v) AS isnull_v FROM t');
|
|
42
|
+
ok('#162 ISNULL in SELECT', JSON.stringify(r.rows) === JSON.stringify([[1, 0], [2, 1]]), r.rows);
|
|
43
|
+
r = await q('SELECT ISNULL(NULL) a, ISNULL(1) b, ISNULL(0) c');
|
|
44
|
+
ok('#162 ISNULL constants', JSON.stringify(r.rows) === JSON.stringify([[1, 0, 0]]), r.rows);
|
|
45
|
+
r = await q('SELECT id FROM t WHERE ISNULL(v)');
|
|
46
|
+
ok('#162 ISNULL in WHERE', JSON.stringify(r.rows) === JSON.stringify([[2]]), r.rows);
|
|
47
|
+
|
|
48
|
+
await q('CREATE TABLE sales (region TEXT, amount INT)');
|
|
49
|
+
await q("INSERT INTO sales VALUES ('north',10),('north',20),('south',100)");
|
|
50
|
+
r = await q('SELECT region, SUM(amount) FROM sales GROUP BY region ORDER BY SUM(amount) DESC');
|
|
51
|
+
ok('#163 ORDER BY aggregate DESC', JSON.stringify(r.rows) === JSON.stringify([['south', 100], ['north', 30]]), r.rows);
|
|
52
|
+
r = await q('SELECT region, SUM(amount) FROM sales GROUP BY region ORDER BY SUM(amount) ASC');
|
|
53
|
+
ok('#163 ORDER BY aggregate ASC', JSON.stringify(r.rows) === JSON.stringify([['north', 30], ['south', 100]]), r.rows);
|
|
54
|
+
|
|
55
|
+
await q('CREATE TABLE users (id INT PRIMARY KEY AUTO_INCREMENT, name TEXT)');
|
|
56
|
+
r = await q("INSERT INTO users (name) VALUES ('Alice') RETURNING id, name");
|
|
57
|
+
ok('#164 INSERT RETURNING auto id', JSON.stringify(r.rows) === JSON.stringify([[1, 'Alice']]), r.rows);
|
|
58
|
+
r = await q("INSERT INTO users (name) VALUES ('Bob'),('Cara') RETURNING id, name");
|
|
59
|
+
ok('#164 INSERT RETURNING multi-row', JSON.stringify(r.rows) === JSON.stringify([[2, 'Bob'], [3, 'Cara']]), r.rows);
|
|
60
|
+
|
|
61
|
+
await q('CREATE TABLE ts (ts TIMESTAMP)');
|
|
62
|
+
await q('INSERT INTO ts VALUES (1000000000000)');
|
|
63
|
+
r = await q('SELECT ts FROM ts');
|
|
64
|
+
ok('#168 TIMESTAMP 1e12 boundary', JSON.stringify(r.rows) === JSON.stringify([[1000000000]]), r.rows);
|
|
65
|
+
|
|
66
|
+
r = await q("SELECT DATE_FORMAT(0, '%Y-%m-%d') AS d");
|
|
67
|
+
ok('#170 DATE_FORMAT(0)', JSON.stringify(r.rows) === JSON.stringify([['1970-01-01']]), r.rows);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function cacheCases() {
|
|
71
|
+
const c = new Cache();
|
|
72
|
+
for (const bad of ['abc', '1.5', NaN, Infinity]) {
|
|
73
|
+
let threw = false;
|
|
74
|
+
try { c.select(bad); } catch (e) { threw = true; }
|
|
75
|
+
ok('#165 select rejects ' + String(bad), threw && c._db === 0, c._db);
|
|
76
|
+
}
|
|
77
|
+
let threw = false;
|
|
78
|
+
try { c.hset('h', '_type', 'evil'); } catch (e) { threw = true; }
|
|
79
|
+
ok('#166 hset rejects _type', threw && c.type('h') === 'none', c.type('h'));
|
|
80
|
+
|
|
81
|
+
const c2 = new Cache();
|
|
82
|
+
const big = '9007199254740993';
|
|
83
|
+
ok('#167 incrby bigint', c2.incrby('a', big) === BigInt(big));
|
|
84
|
+
ok('#167 decrby bigint symmetric', c2.decrby('b', big) === -BigInt(big));
|
|
85
|
+
ok('#167 decrby rejects non-integer', (() => {
|
|
86
|
+
try { c2.decrby('c', '1.5'); return false; } catch (e) { return true; }
|
|
87
|
+
})());
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function redisCases() {
|
|
91
|
+
const srv = createRedisServer({ port: 0, dataDir: null });
|
|
92
|
+
redisCall(srv, 'SET', 'a', '1');
|
|
93
|
+
for (const [cmd, args] of [
|
|
94
|
+
['HEXISTS', ['a', 'f']], ['HLEN', ['a']], ['HKEYS', ['a']], ['HVALS', ['a']],
|
|
95
|
+
['SMEMBERS', ['a']], ['SISMEMBER', ['a', 'm']], ['SCARD', ['a']], ['ZSCORE', ['a', 'm']],
|
|
96
|
+
]) {
|
|
97
|
+
const res = redisCall(srv, cmd, ...args);
|
|
98
|
+
ok('#171 ' + cmd + ' WRONGTYPE', /WRONGTYPE/.test(res.error || ''), res);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const srv2 = createRedisServer({ port: 0, dataDir: null });
|
|
102
|
+
const s1 = redisCall(srv2, 'SET', 'k');
|
|
103
|
+
ok('#172 SET missing value', /wrong number of arguments/.test(s1.error || ''), s1);
|
|
104
|
+
ok('#172 no ghost key', redisCall(srv2, 'EXISTS', 'k').value === 0);
|
|
105
|
+
const z1 = redisCall(srv2, 'ZADD', 'z');
|
|
106
|
+
ok('#172 ZADD missing args', /wrong number of arguments/.test(z1.error || ''), z1);
|
|
107
|
+
ok('#172 no ghost zset', redisCall(srv2, 'TYPE', 'z').value === 'none');
|
|
108
|
+
const l1 = redisCall(srv2, 'LPUSH', 'l');
|
|
109
|
+
ok('#172 LPUSH missing value', /wrong number of arguments/.test(l1.error || ''), l1);
|
|
110
|
+
|
|
111
|
+
const srv3 = createRedisServer({ port: 0, dataDir: null });
|
|
112
|
+
redisCall(srv3, 'HSET', 'h', 'f', '1'); redisCall(srv3, 'HDEL', 'h', 'f');
|
|
113
|
+
redisCall(srv3, 'ZADD', 'z', 1, 'a'); redisCall(srv3, 'ZREM', 'z', 'a');
|
|
114
|
+
redisCall(srv3, 'RPUSH', 'l', 'a'); redisCall(srv3, 'LREM', 'l', 0, 'a');
|
|
115
|
+
ok('#173 HDEL deletes empty hash', redisCall(srv3, 'EXISTS', 'h').value === 0);
|
|
116
|
+
ok('#173 ZREM deletes empty zset', redisCall(srv3, 'EXISTS', 'z').value === 0);
|
|
117
|
+
ok('#173 LREM deletes empty list', redisCall(srv3, 'EXISTS', 'l').value === 0);
|
|
118
|
+
|
|
119
|
+
const srv4 = createRedisServer({ port: 0, dataDir: null });
|
|
120
|
+
const zbad = redisCall(srv4, 'ZADD', 'z', 1, 'a', 'bad', 'b');
|
|
121
|
+
ok('#174 ZADD rejects invalid score', /not a valid float/.test(zbad.error || ''), zbad);
|
|
122
|
+
ok('#174 ZADD is atomic', redisCall(srv4, 'ZCARD', 'z').value === 0);
|
|
123
|
+
ok('#174 no ghost zset', redisCall(srv4, 'EXISTS', 'z').value === 0);
|
|
124
|
+
|
|
125
|
+
const srv5 = createRedisServer({ port: 0, dataDir: null });
|
|
126
|
+
redisCall(srv5, 'RPUSH', 'l', 'a', 'b', 'c');
|
|
127
|
+
redisCall(srv5, 'ZADD', 'z', 1, 'a', 2, 'b');
|
|
128
|
+
for (const [cmd, args] of [['LRANGE', ['l', 'x', 'y']], ['LINDEX', ['l', 'x']], ['ZRANGE', ['z', 'x', 'y']]]) {
|
|
129
|
+
const res = redisCall(srv5, cmd, ...args);
|
|
130
|
+
ok('#175 ' + cmd + ' rejects non-integer', /not an integer/.test(res.error || ''), res);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const srv6 = createRedisServer({ port: 0, dataDir: null });
|
|
134
|
+
redisCall(srv6, 'SET', 'exp', 'v', 'PX', '1');
|
|
135
|
+
const end = Date.now() + 30;
|
|
136
|
+
while (Date.now() < end) { /* wait for TTL */ }
|
|
137
|
+
ok('#176 DEL expired key returns 0', redisCall(srv6, 'DEL', 'exp').value === 0);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async function respCase() {
|
|
141
|
+
const srv = createRedisServer({ port: 0, dataDir: null });
|
|
142
|
+
await new Promise((resolve) => srv.listen(resolve));
|
|
143
|
+
const port = srv.server.address().port;
|
|
144
|
+
const sock = net.connect(port, '127.0.0.1');
|
|
145
|
+
await new Promise((resolve) => sock.on('connect', resolve));
|
|
146
|
+
const chunks = [];
|
|
147
|
+
sock.on('data', (c) => chunks.push(c));
|
|
148
|
+
for (const args of [['ECHO', 'OK'], ['HSET', 'h', 'f', 'OK'], ['HGET', 'h', 'f'], ['PING'], ['SET', 's', 'v']]) {
|
|
149
|
+
sock.write(respFrame(args));
|
|
150
|
+
}
|
|
151
|
+
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
152
|
+
const out = Buffer.concat(chunks).toString('utf8');
|
|
153
|
+
ok('#177 ECHO OK is bulk', out.includes('$2\r\nOK\r\n'), out);
|
|
154
|
+
ok('#177 HGET OK is bulk', (out.match(/\$2\r\nOK\r\n/g) || []).length >= 2, out);
|
|
155
|
+
ok('#177 PING is simple', out.includes('+PONG\r\n'), out);
|
|
156
|
+
ok('#177 SET is simple', out.includes('+OK\r\n'), out);
|
|
157
|
+
sock.end();
|
|
158
|
+
srv.stop();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function mongoCase() {
|
|
162
|
+
const mongo = createMongoServer({ dataDir: ':memory:' });
|
|
163
|
+
await mongo._dispatch('insert', {
|
|
164
|
+
insert: 'users',
|
|
165
|
+
documents: [{ _id: 1, name: 'a', age: 20 }, { _id: 2, name: 'b', age: 30 }],
|
|
166
|
+
$db: 'test',
|
|
167
|
+
});
|
|
168
|
+
const engine = await mongo._getEngine('test');
|
|
169
|
+
const schema = engine.getTableSchema('users');
|
|
170
|
+
ok('#178 auto collection schema', schema && Object.keys(schema).length === 3, schema);
|
|
171
|
+
const r = await executeSQL(engine, 'SELECT * FROM users ORDER BY _id', { safety: false });
|
|
172
|
+
ok('#178 SELECT * on mongo collection', JSON.stringify(r.rows) === JSON.stringify([[1, 'a', 20], [2, 'b', 30]]), r.rows);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function bigintCase() {
|
|
176
|
+
const userData = { name: 'x', meta: { __jsql_bigint__: '12345' } };
|
|
177
|
+
const back = bigintJson.parse(bigintJson.stringify(userData));
|
|
178
|
+
ok('#169 user marker object preserved', back.meta && typeof back.meta === 'object' && back.meta.__jsql_bigint__ === '12345', back.meta);
|
|
179
|
+
const round = bigintJson.parse(bigintJson.stringify({ v: 9007199254740993n }));
|
|
180
|
+
ok('#169 real BigInt still round-trips', round.v === 9007199254740993n, round.v);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function snapshotCase() {
|
|
184
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'jsql-rdb-'));
|
|
185
|
+
fs.writeFileSync(path.join(dir, 'data.rdb.json'), '{broken json');
|
|
186
|
+
let reported = null;
|
|
187
|
+
const origError = console.error;
|
|
188
|
+
console.error = (...args) => { reported = args.join(' '); };
|
|
189
|
+
const srv = createRedisServer({ port: 0, dataDir: dir, onError: (e) => { reported = reported || e.message; } });
|
|
190
|
+
console.error = origError;
|
|
191
|
+
const files = fs.readdirSync(dir);
|
|
192
|
+
ok('#179 corrupt snapshot reported', !!reported);
|
|
193
|
+
ok('#179 corrupt snapshot backed up', files.some((f) => f.includes('.corrupt-')));
|
|
194
|
+
ok('#179 corrupt snapshot does not crash', srv.execute('DBSIZE', []) === 0);
|
|
195
|
+
|
|
196
|
+
const dir2 = fs.mkdtempSync(path.join(os.tmpdir(), 'jsql-rdb2-'));
|
|
197
|
+
fs.writeFileSync(path.join(dir2, 'data.rdb.json'), '{broken json');
|
|
198
|
+
fs.writeFileSync(path.join(dir2, 'data.rdb.json.tmp'), JSON.stringify({
|
|
199
|
+
__jsql_redis__: 2,
|
|
200
|
+
databases: { 0: { k: { type: 'string', val: 'from-tmp', ttl: null } } },
|
|
201
|
+
}));
|
|
202
|
+
const origError2 = console.error;
|
|
203
|
+
console.error = () => {};
|
|
204
|
+
const srv2 = createRedisServer({ port: 0, dataDir: dir2, onError: () => {} });
|
|
205
|
+
console.error = origError2;
|
|
206
|
+
ok('#179 recovers from tmp file', srv2.execute('GET', ['k']) === 'from-tmp');
|
|
207
|
+
srv2.stop();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
(async () => {
|
|
211
|
+
await sqlCases();
|
|
212
|
+
cacheCases();
|
|
213
|
+
redisCases();
|
|
214
|
+
await respCase();
|
|
215
|
+
await mongoCase();
|
|
216
|
+
bigintCase();
|
|
217
|
+
snapshotCase();
|
|
218
|
+
console.log(`\n${passed} passed, ${failed} failed`);
|
|
219
|
+
process.exit(failed === 0 ? 0 : 1);
|
|
220
|
+
})().catch((e) => { console.error('FATAL', e); process.exit(1); });
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Regression tests for issues #181-#186.
|
|
3
|
+
* node test/issue-181-186.test.js
|
|
4
|
+
*/
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const os = require('os');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
|
|
9
|
+
const ROOT = path.join(__dirname, '..');
|
|
10
|
+
const { Database } = require(path.join(ROOT, 'index.js'));
|
|
11
|
+
|
|
12
|
+
let passed = 0, failed = 0;
|
|
13
|
+
function ok(name, cond, extra) {
|
|
14
|
+
if (cond) { passed++; console.log('[OK]', name); }
|
|
15
|
+
else { failed++; console.log('[FAIL]', name, extra !== undefined ? '→ ' + JSON.stringify(extra) : ''); }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const q = (db, sql) => db.query(sql, { safety: false });
|
|
19
|
+
|
|
20
|
+
async function columnarSqlWrites() {
|
|
21
|
+
const db = new Database(':memory:');
|
|
22
|
+
await q(db, 'CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT)');
|
|
23
|
+
await q(db, "INSERT INTO t (id,name) VALUES (1,'a'),(2,'b')");
|
|
24
|
+
db.convertToColumnar('t');
|
|
25
|
+
|
|
26
|
+
await q(db, "UPDATE t SET name='z' WHERE id=2");
|
|
27
|
+
let rows = (await q(db, 'SELECT * FROM t')).rows;
|
|
28
|
+
ok('#181 SQL UPDATE invalidates columnar', JSON.stringify(rows) === JSON.stringify([[1, 'a'], [2, 'z']]), rows);
|
|
29
|
+
|
|
30
|
+
await q(db, 'DELETE FROM t WHERE id=1');
|
|
31
|
+
rows = (await q(db, 'SELECT * FROM t')).rows;
|
|
32
|
+
ok('#181 SQL DELETE invalidates columnar', JSON.stringify(rows) === JSON.stringify([[2, 'z']]), rows);
|
|
33
|
+
const count = (await q(db, 'SELECT COUNT(*) FROM t')).rows;
|
|
34
|
+
ok('#181 COUNT after SQL writes', JSON.stringify(count) === JSON.stringify([[1]]), count);
|
|
35
|
+
db.stop();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function subqueryMutations() {
|
|
39
|
+
{
|
|
40
|
+
const db = new Database(':memory:');
|
|
41
|
+
await q(db, 'CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER)');
|
|
42
|
+
await q(db, 'INSERT INTO t (id,a) VALUES (1,10),(2,20),(3,5),(4,30)');
|
|
43
|
+
const del = await q(db, 'DELETE FROM t WHERE id IN (SELECT id FROM t WHERE a > 10)');
|
|
44
|
+
ok('#182 DELETE IN subquery affectedRows', del.affectedRows === 2, del.affectedRows);
|
|
45
|
+
const rows = (await q(db, 'SELECT * FROM t')).rows;
|
|
46
|
+
ok('#182 DELETE IN subquery rows', JSON.stringify(rows) === JSON.stringify([[1, 10], [3, 5]]), rows);
|
|
47
|
+
db.stop();
|
|
48
|
+
}
|
|
49
|
+
{
|
|
50
|
+
const db = new Database(':memory:');
|
|
51
|
+
await q(db, 'CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER)');
|
|
52
|
+
await q(db, 'INSERT INTO t (id,a) VALUES (1,10),(2,20),(3,5),(4,30)');
|
|
53
|
+
const upd = await q(db, 'UPDATE t SET a = a + 1 WHERE id IN (SELECT id FROM t WHERE a > 10)');
|
|
54
|
+
ok('#182 UPDATE IN subquery affectedRows', upd.affectedRows === 2, upd.affectedRows);
|
|
55
|
+
const rows = (await q(db, 'SELECT * FROM t')).rows;
|
|
56
|
+
ok('#182 UPDATE IN subquery rows', JSON.stringify(rows) === JSON.stringify([[1, 10], [2, 21], [3, 5], [4, 31]]), rows);
|
|
57
|
+
db.stop();
|
|
58
|
+
}
|
|
59
|
+
{
|
|
60
|
+
const db = new Database(':memory:');
|
|
61
|
+
await q(db, 'CREATE TABLE t (id INTEGER PRIMARY KEY, a INTEGER)');
|
|
62
|
+
await q(db, 'INSERT INTO t (id,a) VALUES (1,10),(2,20)');
|
|
63
|
+
const del = await q(db, 'DELETE FROM t WHERE EXISTS (SELECT 1 FROM t WHERE a > 15)');
|
|
64
|
+
ok('#182 DELETE EXISTS subquery', del.affectedRows === 2, del.affectedRows);
|
|
65
|
+
db.stop();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function qualifiedGroupOrder() {
|
|
70
|
+
const db = new Database(':memory:');
|
|
71
|
+
await q(db, 'CREATE TABLE t1 (a INTEGER PRIMARY KEY, b INTEGER)');
|
|
72
|
+
await q(db, 'INSERT INTO t1 (a,b) VALUES (1,10),(2,20),(3,10),(4,NULL)');
|
|
73
|
+
const asc = (await q(db, 'SELECT t1.b, COUNT(*) FROM t1 GROUP BY t1.b ORDER BY t1.b')).rows;
|
|
74
|
+
const desc = (await q(db, 'SELECT t1.b, COUNT(*) FROM t1 GROUP BY t1.b ORDER BY t1.b DESC')).rows;
|
|
75
|
+
ok('#183 qualified ORDER BY ASC', JSON.stringify(asc) === JSON.stringify([[null, 1], [10, 2], [20, 1]]), asc);
|
|
76
|
+
ok('#183 qualified ORDER BY DESC', JSON.stringify(desc) === JSON.stringify([[20, 1], [10, 2], [null, 1]]), desc);
|
|
77
|
+
db.stop();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function csvRoundTrip() {
|
|
81
|
+
const file = path.join(os.tmpdir(), 'jsql-csv-' + Date.now() + '.csv');
|
|
82
|
+
fs.writeFileSync(file, 'id,name,note\r\n1,"a,b","line1\r\nline2"\r\n2, padded ,"he said ""hi"""\r\n', 'utf8');
|
|
83
|
+
const db = new Database(':memory:');
|
|
84
|
+
await q(db, 'CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT, note TEXT)');
|
|
85
|
+
db.importCSV(file, 't');
|
|
86
|
+
const rows = (await q(db, 'SELECT * FROM t')).rows;
|
|
87
|
+
ok('#184 quoted newline', rows.length === 2 && rows[0][2] === 'line1\r\nline2', rows);
|
|
88
|
+
ok('#184 quoted comma/quote', rows[0][1] === 'a,b' && rows[1][2] === 'he said "hi"', rows);
|
|
89
|
+
ok('#184 unquoted spaces preserved', rows[1][1] === ' padded ', rows);
|
|
90
|
+
|
|
91
|
+
const out = path.join(os.tmpdir(), 'jsql-csv-out-' + Date.now() + '.csv');
|
|
92
|
+
db.exportCSV(out, 't');
|
|
93
|
+
const db2 = new Database(':memory:');
|
|
94
|
+
await q(db2, 'CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT, note TEXT)');
|
|
95
|
+
db2.importCSV(out, 't');
|
|
96
|
+
const rows2 = (await q(db2, 'SELECT * FROM t')).rows;
|
|
97
|
+
ok('#184 export/import round-trip', JSON.stringify(rows2) === JSON.stringify(rows), rows2);
|
|
98
|
+
db.stop(); db2.stop();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function scalarFunctions() {
|
|
102
|
+
const db = new Database(':memory:');
|
|
103
|
+
const val = async (sql) => (await q(db, sql)).rows[0][0];
|
|
104
|
+
ok('#185 YEARWEEK', await val("SELECT YEARWEEK('2026-11-21')") === 202647);
|
|
105
|
+
ok('#185 WEEKOFYEAR', await val("SELECT WEEKOFYEAR('2026-11-21')") === 47);
|
|
106
|
+
ok('#185 MID alias', await val("SELECT MID('abcdef',2,3)") === 'bcd');
|
|
107
|
+
ok('#185 PERIOD_ADD', await val('SELECT PERIOD_ADD(202601,3)') === 202604);
|
|
108
|
+
ok('#185 TIMESTAMP', await val("SELECT TIMESTAMP('2026-11-21','10:00:00')") === '2026-11-21 10:00:00');
|
|
109
|
+
|
|
110
|
+
let err = null;
|
|
111
|
+
try { await q(db, "SELECT UPPERR('abc')"); } catch (e) { err = e; }
|
|
112
|
+
ok('#185 unknown function throws 1305', err && err.code === 1305 && /UPPERR/.test(err.message), err && err.message);
|
|
113
|
+
|
|
114
|
+
await q(db, 'CREATE TABLE t (id INTEGER PRIMARY KEY, name TEXT)');
|
|
115
|
+
await q(db, "INSERT INTO t VALUES (1,'abc')");
|
|
116
|
+
err = null;
|
|
117
|
+
try { await q(db, "SELECT id FROM t WHERE SUBSTRNG(name,1,1)='a'"); } catch (e) { err = e; }
|
|
118
|
+
ok('#185 unknown function in WHERE throws', err && err.code === 1305, err && err.message);
|
|
119
|
+
db.stop();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function limitExpressions() {
|
|
123
|
+
const db = new Database(':memory:');
|
|
124
|
+
await q(db, 'CREATE TABLE t (id INTEGER PRIMARY KEY)');
|
|
125
|
+
await q(db, 'INSERT INTO t (id) VALUES (1),(2),(3),(4),(5)');
|
|
126
|
+
|
|
127
|
+
for (const sql of [
|
|
128
|
+
'SELECT id FROM t LIMIT 2+1',
|
|
129
|
+
'SELECT id FROM t LIMIT 2 OFFSET 1+1',
|
|
130
|
+
'SELECT id FROM t LIMIT 1.9',
|
|
131
|
+
'SELECT id FROM t LIMIT ?',
|
|
132
|
+
]) {
|
|
133
|
+
let err = null;
|
|
134
|
+
try { await q(db, sql); } catch (e) { err = e; }
|
|
135
|
+
ok('#186 rejects ' + sql, err && err.code === 1064, err && err.message);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
ok('#186 LIMIT 2', JSON.stringify((await q(db, 'SELECT id FROM t LIMIT 2')).rows) === JSON.stringify([[1], [2]]));
|
|
139
|
+
ok('#186 LIMIT 0,2', JSON.stringify((await q(db, 'SELECT id FROM t LIMIT 0,2')).rows) === JSON.stringify([[1], [2]]));
|
|
140
|
+
ok('#186 LIMIT 2 OFFSET 1', JSON.stringify((await q(db, 'SELECT id FROM t LIMIT 2 OFFSET 1')).rows) === JSON.stringify([[2], [3]]));
|
|
141
|
+
db.stop();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
(async () => {
|
|
145
|
+
await columnarSqlWrites();
|
|
146
|
+
await subqueryMutations();
|
|
147
|
+
await qualifiedGroupOrder();
|
|
148
|
+
await csvRoundTrip();
|
|
149
|
+
await scalarFunctions();
|
|
150
|
+
await limitExpressions();
|
|
151
|
+
console.log(`\n${passed} passed, ${failed} failed`);
|
|
152
|
+
process.exit(failed === 0 ? 0 : 1);
|
|
153
|
+
})().catch((e) => { console.error('FATAL', e); process.exit(1); });
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Regression test for issue #187.
|
|
3
|
+
* node test/issue-187.test.js
|
|
4
|
+
*/
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const os = require('os');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
|
|
9
|
+
const ROOT = path.join(__dirname, '..');
|
|
10
|
+
const { createRedisServer } = require(path.join(ROOT, 'lib', 'redis_server.js'));
|
|
11
|
+
|
|
12
|
+
let passed = 0, failed = 0;
|
|
13
|
+
function ok(name, cond, extra) {
|
|
14
|
+
if (cond) { passed++; console.log('[OK]', name); }
|
|
15
|
+
else { failed++; console.log('[FAIL]', name, extra !== undefined ? '→ ' + JSON.stringify(extra) : ''); }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
(async () => {
|
|
19
|
+
const prevCwd = process.cwd();
|
|
20
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'jsql-redis-memory-'));
|
|
21
|
+
process.chdir(dir);
|
|
22
|
+
|
|
23
|
+
const srv = createRedisServer({ port: 0, dataDir: ':memory:' });
|
|
24
|
+
ok('#187 snapshotPath is null in memory mode', srv.snapshotPath() === null);
|
|
25
|
+
srv.execute('SET', ['leak', 'from-process-1']);
|
|
26
|
+
await new Promise((resolve) => setTimeout(resolve, 700));
|
|
27
|
+
ok('#187 no literal :memory: directory', !fs.existsSync(path.join(dir, ':memory')), fs.readdirSync(dir));
|
|
28
|
+
|
|
29
|
+
srv.stop();
|
|
30
|
+
const srv2 = createRedisServer({ port: 0, dataDir: ':memory:' });
|
|
31
|
+
ok('#187 new instance starts empty', srv2.execute('GET', ['leak']) === null);
|
|
32
|
+
|
|
33
|
+
process.chdir(prevCwd);
|
|
34
|
+
console.log(`\n${passed} passed, ${failed} failed`);
|
|
35
|
+
process.exit(failed === 0 ? 0 : 1);
|
|
36
|
+
})().catch((e) => { console.error('FATAL', e); process.exit(1); });
|