jsql-neo 6.5.30 → 6.5.31
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/index.js +1 -1
- package/lib/date-types.js +14 -3
- package/lib/redis_server.js +16 -5
- package/lib/sql.js +25 -2
- package/lib/xql/src/compiler.js +20 -20
- package/package.json +3 -3
- package/test/extra-coverage.test.js +5 -2
- package/test/issue-63-68.test.js +91 -0
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
> **One engine to rule them all** — a Rust-powered embedded database that speaks your language:
|
|
4
4
|
> MySQL, PostgreSQL, MongoDB, Redis, SQL, TypeScript, **and the browser**.
|
|
5
5
|
|
|
6
|
-
> **v6.5.
|
|
6
|
+
> **v6.5.31** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
|
|
7
7
|
|
|
8
8
|

|
|
9
9
|

|
package/index.js
CHANGED
package/lib/date-types.js
CHANGED
|
@@ -74,8 +74,14 @@ function validateDate(value, fieldName) {
|
|
|
74
74
|
return toLocalDateString(value);
|
|
75
75
|
}
|
|
76
76
|
if (typeof value === 'number') {
|
|
77
|
-
// Unix timestamp → date
|
|
78
|
-
|
|
77
|
+
// Unix timestamp → date。NaN / ±Infinity / 超出 Date 范围的数会生成
|
|
78
|
+
// Invalid Date,此前被格式化成字面量 "NaN-NaN-NaN" 静默落库(#64);
|
|
79
|
+
// 与字符串分支保持一致,直接判为非法日期值。
|
|
80
|
+
const d = new Date(value * 1000);
|
|
81
|
+
if (!Number.isFinite(value) || Number.isNaN(d.getTime())) {
|
|
82
|
+
throw createError('ER_INVALID_DATE', fieldName, String(value));
|
|
83
|
+
}
|
|
84
|
+
return toLocalDateString(d);
|
|
79
85
|
}
|
|
80
86
|
if (typeof value === 'string') {
|
|
81
87
|
const s = value.trim();
|
|
@@ -131,7 +137,12 @@ function validateDateTime(value, fieldName) {
|
|
|
131
137
|
return toLocalDateTimeString(value);
|
|
132
138
|
}
|
|
133
139
|
if (typeof value === 'number') {
|
|
134
|
-
|
|
140
|
+
// 同 validateDate:拒绝 NaN / Infinity / 越界时间戳(#64)
|
|
141
|
+
const d = new Date(value * 1000);
|
|
142
|
+
if (!Number.isFinite(value) || Number.isNaN(d.getTime())) {
|
|
143
|
+
throw createError('ER_INVALID_DATE', fieldName, String(value));
|
|
144
|
+
}
|
|
145
|
+
return toLocalDateTimeString(d);
|
|
135
146
|
}
|
|
136
147
|
if (typeof value === 'string') {
|
|
137
148
|
// ISO format: 2024-01-15T10:30:00.000Z / +08:00(可能带时区标记)
|
package/lib/redis_server.js
CHANGED
|
@@ -504,7 +504,8 @@ class RedisServer {
|
|
|
504
504
|
case 'ZSCORE': {
|
|
505
505
|
const v = this._get(args[0]);
|
|
506
506
|
if (!v || v.type !== 'zset' || !(args[1] in v.val)) return null;
|
|
507
|
-
|
|
507
|
+
// ZSCORE 的分数恒为 bulk string(含整数分数),不能落进 integer reply(#67)
|
|
508
|
+
return String(v.val[args[1]]);
|
|
508
509
|
}
|
|
509
510
|
case 'ZCARD': {
|
|
510
511
|
const v = this._get(args[0]);
|
|
@@ -535,7 +536,8 @@ class RedisServer {
|
|
|
535
536
|
const member = args[2];
|
|
536
537
|
zset[member] = (zset[member] || 0) + inc;
|
|
537
538
|
this._scheduleSnapshot();
|
|
538
|
-
|
|
539
|
+
// 同 ZSCORE:分数用 bulk string 回复(#67)
|
|
540
|
+
return String(zset[member]);
|
|
539
541
|
}
|
|
540
542
|
case 'MSET': {
|
|
541
543
|
const rest = args;
|
|
@@ -741,10 +743,19 @@ class RedisServer {
|
|
|
741
743
|
} else if (val === null) {
|
|
742
744
|
out = '$-1\r\n';
|
|
743
745
|
} else if (typeof val === 'number') {
|
|
744
|
-
|
|
746
|
+
// RESP integer reply 只能是十进制整数;小数必须走 bulk string(#67)。
|
|
747
|
+
// ZSCORE / ZINCRBY 的分数已在 handler 里转成字符串,这里是非整数数字的最后兜底。
|
|
748
|
+
out = Number.isInteger(val)
|
|
749
|
+
? ':' + val + '\r\n'
|
|
750
|
+
: '$' + Buffer.byteLength(String(val), 'utf8') + '\r\n' + val + '\r\n';
|
|
745
751
|
} else if (Array.isArray(val)) {
|
|
746
|
-
// 长度用字节数,不能用 String.length ——
|
|
747
|
-
|
|
752
|
+
// 长度用字节数,不能用 String.length —— 否则中文等多字节值会让客户端解帧错位。
|
|
753
|
+
// 批量回复里缺失的键必须是 nil bulk ($-1),不能是 "null" 字符串(#66)。
|
|
754
|
+
out = '*' + val.length + '\r\n' + val.map(v =>
|
|
755
|
+
(v === null || v === undefined)
|
|
756
|
+
? '$-1\r\n'
|
|
757
|
+
: '$' + Buffer.byteLength(String(v), 'utf8') + '\r\n' + v + '\r\n'
|
|
758
|
+
).join('');
|
|
748
759
|
} else if (typeof val === 'string') {
|
|
749
760
|
if (val === 'OK' || val === 'PONG') {
|
|
750
761
|
out = '+' + val + '\r\n';
|
package/lib/sql.js
CHANGED
|
@@ -2609,6 +2609,20 @@ class Parser {
|
|
|
2609
2609
|
if (this.peek().type === 'op' && this.peek().value === '(') { this.next(); this.expect('op', ')'); }
|
|
2610
2610
|
return { type: 'func', name, args: [] };
|
|
2611
2611
|
}
|
|
2612
|
+
// POSITION(substr IN str) —— 标准 SQL / MySQL 写法,等价于 LOCATE(substr, str)。
|
|
2613
|
+
// IN 是关键字,通用函数参数循环会把它当成 IN 表达式的一部分并报
|
|
2614
|
+
// "Expected ( but got 'abc'",所以在这里单独归一(#63)。
|
|
2615
|
+
if (String(t.value).toUpperCase() === 'POSITION' && this.peek().type === 'op' && this.peek().value === '(') {
|
|
2616
|
+
this.next();
|
|
2617
|
+
const substr = this.parseOperand();
|
|
2618
|
+
if (!(this.isWord('IN') || this.isKeyword('IN'))) {
|
|
2619
|
+
throw new Error(`Expected IN in POSITION, got '${this.peek().value}'`);
|
|
2620
|
+
}
|
|
2621
|
+
this.next();
|
|
2622
|
+
const str = this.parseOperand();
|
|
2623
|
+
this.expect('op', ')');
|
|
2624
|
+
return { type: 'func', name: 'LOCATE', args: [substr, str] };
|
|
2625
|
+
}
|
|
2612
2626
|
// EXTRACT(YEAR FROM expr) —— 标准 SQL 的 FROM 形式
|
|
2613
2627
|
if (String(t.value).toUpperCase() === 'EXTRACT' && this.peek().type === 'op' && this.peek().value === '(') {
|
|
2614
2628
|
this.next();
|
|
@@ -3742,6 +3756,15 @@ function pgAge(from, to) {
|
|
|
3742
3756
|
return `${years} years ${months} mons ${days} days`;
|
|
3743
3757
|
}
|
|
3744
3758
|
|
|
3759
|
+
/**
|
|
3760
|
+
* 数学定义域外的输入(SQRT(-1) / POWER(-2,0.5) 等)在 JS 里会得到
|
|
3761
|
+
* NaN / Infinity。MySQL 语义应返回 NULL;NaN 还会通过后续算术与聚合
|
|
3762
|
+
* 继续传播,且 JSON.stringify(NaN) === 'null' 会在序列化时把问题藏起来。
|
|
3763
|
+
* 统一在这里拦掉(#65)。
|
|
3764
|
+
*/
|
|
3765
|
+
function finiteOrNull(x) {
|
|
3766
|
+
return (typeof x === 'number' && !Number.isFinite(x)) ? null : x;
|
|
3767
|
+
}
|
|
3745
3768
|
function applyScalarFunction(fnNode, row, ctx) {
|
|
3746
3769
|
const name = (fnNode.name || '').toUpperCase();
|
|
3747
3770
|
const rawArgs = fnNode.args || [];
|
|
@@ -4084,8 +4107,8 @@ function applyScalarFunction(fnNode, row, ctx) {
|
|
|
4084
4107
|
case 'FLOOR': return args[0] == null ? null : Math.floor(args[0]);
|
|
4085
4108
|
case 'CEIL': case 'CEILING': return args[0] == null ? null : Math.ceil(args[0]);
|
|
4086
4109
|
case 'MOD': return (args[0] == null || args[1] === 0) ? null : args[0] % args[1];
|
|
4087
|
-
case 'POWER': case 'POW': return (args[0] == null || args[1] == null) ? null : Math.pow(args[0], args[1]);
|
|
4088
|
-
case 'SQRT': return args[0] == null ? null : Math.sqrt(args[0]);
|
|
4110
|
+
case 'POWER': case 'POW': return (args[0] == null || args[1] == null) ? null : finiteOrNull(Math.pow(args[0], args[1]));
|
|
4111
|
+
case 'SQRT': return args[0] == null ? null : finiteOrNull(Math.sqrt(args[0]));
|
|
4089
4112
|
case 'IFNULL': case 'NVL': return args[0] != null ? args[0] : args[1];
|
|
4090
4113
|
case 'COALESCE': {
|
|
4091
4114
|
// 不能直接 return find(...):全都取不到值时 find 返回 undefined,
|
package/lib/xql/src/compiler.js
CHANGED
|
@@ -492,6 +492,26 @@ function compileQuery(q, opts = {}) {
|
|
|
492
492
|
B.push(' for (let _i = 0; _i < rows.length; _i++) _out.push(rows[_i]);');
|
|
493
493
|
}
|
|
494
494
|
|
|
495
|
+
// 3) ORDER BY
|
|
496
|
+
if (orderBy) {
|
|
497
|
+
// 每个排序键一个块。注意不能用 continue —— 块级作用域里没有循环可继续,
|
|
498
|
+
// 改成 if 包裹。
|
|
499
|
+
//
|
|
500
|
+
// 字符串用 < / > 而不是 localeCompare:后者是 locale 感知的排序,
|
|
501
|
+
// 6.8 万行排序要调 ~110 万次,实测比朴素比较多一个量级
|
|
502
|
+
// (这一项就是从 2.93 倍降到持平的关键)。
|
|
503
|
+
const cmp = orderBy.map((o, i) => ` { const av = _ok${i}(a), bv = _ok${i}(b);
|
|
504
|
+
if (av !== bv) {
|
|
505
|
+
if (av === null || av === undefined) return -1 * ${o.dir};
|
|
506
|
+
if (bv === null || bv === undefined) return 1 * ${o.dir};
|
|
507
|
+
const d = (typeof av === 'number' && typeof bv === 'number') || (typeof av === 'boolean' && typeof bv === 'boolean')
|
|
508
|
+
? (av < bv ? -1 : 1)
|
|
509
|
+
: (String(av) < String(bv) ? -1 : 1);
|
|
510
|
+
if (d !== 0) return d * ${o.dir};
|
|
511
|
+
} }`).join('\n');
|
|
512
|
+
B.push(' _out.sort((a, b) => {\n' + cmp + '\n return 0;\n });');
|
|
513
|
+
}
|
|
514
|
+
|
|
495
515
|
// 2) SELECT —— 与 WHERE 同理,投影语句也内联进循环。
|
|
496
516
|
// 每列调一次函数的话,6.8 万行 × 2 列就是 13.6 万次调用,
|
|
497
517
|
// 实测占掉整个管道的大半时间(2.81 倍差距主要来自这里)。
|
|
@@ -542,26 +562,6 @@ function compileQuery(q, opts = {}) {
|
|
|
542
562
|
const limit = q.limit;
|
|
543
563
|
const offset = q.offset || 0;
|
|
544
564
|
|
|
545
|
-
// 3) ORDER BY
|
|
546
|
-
if (orderBy) {
|
|
547
|
-
// 每个排序键一个块。注意不能用 continue —— 块级作用域里没有循环可继续,
|
|
548
|
-
// 改成 if 包裹。
|
|
549
|
-
//
|
|
550
|
-
// 字符串用 < / > 而不是 localeCompare:后者是 locale 感知的排序,
|
|
551
|
-
// 6.8 万行排序要调 ~110 万次,实测比朴素比较多一个量级
|
|
552
|
-
// (这一项就是从 2.93 倍降到持平的关键)。
|
|
553
|
-
const cmp = orderBy.map((o, i) => ` { const av = _ok${i}(a), bv = _ok${i}(b);
|
|
554
|
-
if (av !== bv) {
|
|
555
|
-
if (av === null || av === undefined) return -1 * ${o.dir};
|
|
556
|
-
if (bv === null || bv === undefined) return 1 * ${o.dir};
|
|
557
|
-
const d = (typeof av === 'number' && typeof bv === 'number') || (typeof av === 'boolean' && typeof bv === 'boolean')
|
|
558
|
-
? (av < bv ? -1 : 1)
|
|
559
|
-
: (String(av) < String(bv) ? -1 : 1);
|
|
560
|
-
if (d !== 0) return d * ${o.dir};
|
|
561
|
-
} }`).join('\n');
|
|
562
|
-
B.push(' _out.sort((a, b) => {\n' + cmp + '\n return 0;\n });');
|
|
563
|
-
}
|
|
564
|
-
|
|
565
565
|
// 4) OFFSET / LIMIT
|
|
566
566
|
if (offset || (limit !== null && limit !== undefined)) {
|
|
567
567
|
const end = (limit === null || limit === undefined) ? 'undefined' : String(offset + limit);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsql-neo",
|
|
3
|
-
"version": "6.5.
|
|
3
|
+
"version": "6.5.31",
|
|
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",
|
|
@@ -50,12 +50,12 @@
|
|
|
50
50
|
"scripts": {
|
|
51
51
|
"postinstall": "node postinstall.js",
|
|
52
52
|
"build:native": "node scripts/build-native.js",
|
|
53
|
-
"test": "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",
|
|
53
|
+
"test": "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",
|
|
54
54
|
"test:parser": "node test/sql-parser.test.js",
|
|
55
55
|
"test:btree": "node test/btree.test.js",
|
|
56
56
|
"test:native": "node test/native.test.js",
|
|
57
57
|
"test:regress": "node test/regress-5.1.0.js",
|
|
58
|
-
"test:all": "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",
|
|
58
|
+
"test:all": "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",
|
|
59
59
|
"test:coverage": "c8 --all --include='lib/**' --exclude='lib/web_ui.js' --reporter=text --reporter=lcov node test/coverage.js",
|
|
60
60
|
"test:orms": "node examples/orms/run-all.js",
|
|
61
61
|
"test:dml": "node test/dml-constraints.test.js",
|
|
@@ -561,10 +561,13 @@ test('数学函数:ABS / CEIL / FLOOR / ROUND / POWER / SQRT', async () => {
|
|
|
561
561
|
assert.strictEqual(await one(q, 'SELECT SQRT(16)'), 4);
|
|
562
562
|
});
|
|
563
563
|
|
|
564
|
-
|
|
564
|
+
// #65:此前断言 SQRT(-1) 返回 NaN,等于把 bug 钉成了预期。
|
|
565
|
+
// MySQL 语义下数学定义域外的输入应返回 NULL;NaN 会污染后续算术/聚合,
|
|
566
|
+
// 且 JSON.stringify(NaN) === 'null' 会在序列化时把问题藏起来。
|
|
567
|
+
test('SQRT 负数返回 NULL', async () => {
|
|
565
568
|
const { q } = makeDb();
|
|
566
569
|
const v = await one(q, 'SELECT SQRT(-1)');
|
|
567
|
-
assert.
|
|
570
|
+
assert.strictEqual(v, null);
|
|
568
571
|
});
|
|
569
572
|
|
|
570
573
|
test('REPLACE 函数', async () => {
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/* ---- issue #63~#68 回归 ---- */
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const ROOT = path.join(__dirname, '..');
|
|
4
|
+
const { Database, executeSQL } = require(path.join(ROOT, 'index.js'));
|
|
5
|
+
|
|
6
|
+
let pass = 0, fail = 0;
|
|
7
|
+
function ok(name, cond, got) {
|
|
8
|
+
if (cond) { pass++; console.log('[OK] ' + name); }
|
|
9
|
+
else { fail++; console.log('[FAIL] ' + name + ' -> ' + JSON.stringify(got)); }
|
|
10
|
+
}
|
|
11
|
+
const isNull = (v) => v === null || v === undefined;
|
|
12
|
+
|
|
13
|
+
(async () => {
|
|
14
|
+
const db = new Database(':memory:', { autoSave: false });
|
|
15
|
+
const q = async (sql) => (await executeSQL(db, sql, { safety: false })).rows[0][0];
|
|
16
|
+
|
|
17
|
+
// ---------- #63 POSITION(substr IN str) ----------
|
|
18
|
+
ok('#63 POSITION 命中', await q("SELECT POSITION('b' IN 'abc')") === 2, await q("SELECT POSITION('b' IN 'abc')"));
|
|
19
|
+
ok('#63 POSITION 未命中返回 0', await q("SELECT POSITION('z' IN 'abc')") === 0, await q("SELECT POSITION('z' IN 'abc')"));
|
|
20
|
+
ok('#63 与 LOCATE 等价', await q("SELECT LOCATE('b','abc')") === await q("SELECT POSITION('b' IN 'abc')"), null);
|
|
21
|
+
ok('#63 支持列引用', await q("SELECT POSITION('b' IN CONCAT('a','bc'))") === 2, null);
|
|
22
|
+
|
|
23
|
+
// ---------- #64 日期类型拒绝非法数字 ----------
|
|
24
|
+
const dt = require(path.join(ROOT, 'lib', 'date-types.js'));
|
|
25
|
+
for (const v of [NaN, Infinity, -Infinity, 1e20]) {
|
|
26
|
+
let threw = false;
|
|
27
|
+
try { dt.validateDateType(v, 'date', 'd'); } catch (e) { threw = /Incorrect date value/.test(e.message); }
|
|
28
|
+
ok('#64 date 拒绝 ' + String(v), threw, null);
|
|
29
|
+
let threw2 = false;
|
|
30
|
+
try { dt.validateDateType(v, 'datetime', 'd'); } catch (e) { threw2 = /Incorrect date value/.test(e.message); }
|
|
31
|
+
ok('#64 datetime 拒绝 ' + String(v), threw2, null);
|
|
32
|
+
}
|
|
33
|
+
ok('#64 合法时间戳仍可用', dt.validateDateType(0, 'date', 'd') === '1970-01-01', dt.validateDateType(0, 'date', 'd'));
|
|
34
|
+
|
|
35
|
+
// ---------- #65 数值域错误返回 NULL ----------
|
|
36
|
+
for (const sql of ['SELECT SQRT(-1)', 'SELECT POWER(-2,0.5)', 'SELECT SQRT(-1) + 5', 'SELECT LOG(-5)', 'SELECT ACOS(2)']) {
|
|
37
|
+
const v = await q(sql);
|
|
38
|
+
ok('#65 ' + sql + ' → NULL', isNull(v), v);
|
|
39
|
+
}
|
|
40
|
+
ok('#65 SQRT(4) 仍为 2', await q('SELECT SQRT(4)') === 2, null);
|
|
41
|
+
ok('#65 POWER(2,3) 仍为 8', await q('SELECT POWER(2,3)') === 8, null);
|
|
42
|
+
|
|
43
|
+
// ---------- #66 / #67 Redis RESP 序列化 ----------
|
|
44
|
+
const { createRedisServer } = require(path.join(ROOT, 'lib', 'redis_server.js'));
|
|
45
|
+
const srv = createRedisServer({ port: 0 });
|
|
46
|
+
function send(cmd, args) {
|
|
47
|
+
const out = [];
|
|
48
|
+
srv._handle({ write: (s) => out.push(s) }, cmd, args);
|
|
49
|
+
return out.join('');
|
|
50
|
+
}
|
|
51
|
+
send('SET', ['a', '1']);
|
|
52
|
+
const mget = send('MGET', ['a', 'missing']);
|
|
53
|
+
ok('#66 MGET 缺失键为 nil bulk', mget.includes('$-1\r\n') && !mget.includes('$4\r\nnull'), mget);
|
|
54
|
+
ok('#66 MGET 存在的键仍正常', mget.includes('$1\r\n1\r\n'), mget);
|
|
55
|
+
|
|
56
|
+
send('ZADD', ['z', '10.5', 'm']);
|
|
57
|
+
const zscore = send('ZSCORE', ['z', 'm']);
|
|
58
|
+
ok('#67 ZSCORE 小数分数为 bulk string', zscore === '$4\r\n10.5\r\n', zscore);
|
|
59
|
+
const zincr = send('ZINCRBY', ['z', '0.5', 'm']);
|
|
60
|
+
// 10.5 + 0.5 = 11,"11" 是 2 字节,所以是 $2 而不是 $3
|
|
61
|
+
ok('#67 ZINCRBY 分数为 bulk string', /^\$2\r\n11\r\n$/.test(zincr) || /^\$\d+\r\n11\.0+\r\n$/.test(zincr), zincr);
|
|
62
|
+
send('ZADD', ['zi', '10', 'm']);
|
|
63
|
+
const zint = send('ZSCORE', ['zi', 'm']);
|
|
64
|
+
ok('#67 整数分数也是 bulk(不是 :10)', zint === '$2\r\n10\r\n', zint);
|
|
65
|
+
ok('#67 _send 非整数 number 不产出非法 integer reply',
|
|
66
|
+
(() => { const o = []; srv._send({ write: (s) => o.push(s) }, 10.5); return o[0] === '$4\r\n10.5\r\n'; })(), null);
|
|
67
|
+
ok('#67 _send 整数仍是 integer reply',
|
|
68
|
+
(() => { const o = []; srv._send({ write: (s) => o.push(s) }, 3); return o[0] === ':3\r\n'; })(), null);
|
|
69
|
+
|
|
70
|
+
// ---------- #68 XQL ORDER BY 非投影列 ----------
|
|
71
|
+
const { xql } = require(path.join(ROOT, 'lib', 'plugins'));
|
|
72
|
+
const db2 = new Database(':memory:', { autoSave: false });
|
|
73
|
+
await db2.createTable('users', {
|
|
74
|
+
id: { type: 'integer', primaryKey: true, autoIncrement: true },
|
|
75
|
+
name: { type: 'string' }, age: { type: 'integer' },
|
|
76
|
+
});
|
|
77
|
+
await db2.insert('users', [{ name: 'alice', age: 30 }, { name: 'bob', age: 25 }, { name: 'carol', age: 30 }]);
|
|
78
|
+
db2.use(xql());
|
|
79
|
+
const both = await db2.xql('FROM users |> ORDER BY age DESC |> SELECT name, age');
|
|
80
|
+
const nameOnly = await db2.xql('FROM users |> ORDER BY age DESC |> SELECT name');
|
|
81
|
+
ok('#68 投影含排序列时结果正确', both.map(r => r.name).join(',') === 'alice,carol,bob', both.map(r => r.name));
|
|
82
|
+
ok('#68 排序列不在投影里也生效', nameOnly.map(r => r.name).join(',') === 'alice,carol,bob', nameOnly.map(r => r.name));
|
|
83
|
+
const asc = await db2.xql('FROM users |> ORDER BY age ASC |> SELECT name');
|
|
84
|
+
ok('#68 升序同样生效', asc[0].name === 'bob', asc.map(r => r.name));
|
|
85
|
+
|
|
86
|
+
console.log(`\n${pass} passed, ${fail} failed`);
|
|
87
|
+
process.exit(fail === 0 ? 0 : 1);
|
|
88
|
+
})().catch(e => {
|
|
89
|
+
console.log('[FAIL] unexpected exception: ' + e.stack.split('\n').slice(0, 3).join('\n'));
|
|
90
|
+
process.exit(1);
|
|
91
|
+
});
|