jsql-neo 6.5.32 → 6.6.0
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 +113 -55
- package/lib/errors.js +2 -0
- package/lib/sql.js +466 -106
- package/lib/table.js +31 -5
- package/package.json +3 -3
- package/postinstall.js +6 -4
- package/test/issue-50.test.js +35 -22
- package/test/issue-71-72.test.js +137 -0
- package/test/issue-73-76.test.js +254 -0
- package/test/query-opt.test.js +10 -8
- package/test/readme-audit.test.js +8 -8
package/lib/table.js
CHANGED
|
@@ -7,7 +7,35 @@
|
|
|
7
7
|
const Query = require('./query');
|
|
8
8
|
const BTree = require('./btree');
|
|
9
9
|
const { validateDateType, now } = require('./date-types');
|
|
10
|
-
const { createError } = require('./errors');
|
|
10
|
+
const { createError, JSQL_Error } = require('./errors');
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// DEFAULT 表达式求值(Issue #72)
|
|
14
|
+
//
|
|
15
|
+
// 此前只有字符串 'CURRENT_TIMESTAMP' 被特判成 now(),其他函数式默认值
|
|
16
|
+
// ('NOW()'、'NOW(6)'、'LOCALTIMESTAMP' 等)会被原样当成列值塞进数据行,
|
|
17
|
+
// 随后被日期校验当作非法日期,报出误导性的 "Incorrect date value: '<列名>'"。
|
|
18
|
+
// 这里统一按字符串形态识别:
|
|
19
|
+
// • CURRENT_TIMESTAMP / NOW[(n)] / LOCALTIMESTAMP[(n)] / LOCALTIME[(n)]
|
|
20
|
+
// → 当前时间戳(MySQL 语义)
|
|
21
|
+
// • 其他「函数调用形式」的默认值 → 明确报不支持,而不是拿列名当值
|
|
22
|
+
// • 普通字面量('abc'、'0'、'{}' 等)→ 原样返回,保持向后兼容
|
|
23
|
+
// 注意:JS 侧 `default: () => ...` 的函数仍在调用处直接 dv()。
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
const NOW_DEFAULT_RE = /^(?:CURRENT_TIMESTAMP|NOW|LOCALTIMESTAMP|LOCALTIME)\s*(?:\(\s*\d*\s*\))?$/i;
|
|
26
|
+
const FUNC_CALL_DEFAULT_RE = /^[A-Za-z_$][\w$]*\s*\([\s\S]*\)$/;
|
|
27
|
+
|
|
28
|
+
function resolveDefaultValue(dv, fieldName) {
|
|
29
|
+
if (typeof dv === 'function') return dv();
|
|
30
|
+
if (typeof dv !== 'string') return dv;
|
|
31
|
+
const expr = dv.trim();
|
|
32
|
+
if (NOW_DEFAULT_RE.test(expr)) return now();
|
|
33
|
+
if (FUNC_CALL_DEFAULT_RE.test(expr)) {
|
|
34
|
+
throw new JSQL_Error(1235,
|
|
35
|
+
`Unsupported default expression '${expr}' for column '${fieldName}'`);
|
|
36
|
+
}
|
|
37
|
+
return dv;
|
|
38
|
+
}
|
|
11
39
|
|
|
12
40
|
// ---------------------------------------------------------------------------
|
|
13
41
|
// 64 位整数(Issue #38)
|
|
@@ -388,7 +416,7 @@ class Table {
|
|
|
388
416
|
// 在副本上做默认值/计算列/类型归一,避免污染调用方传入的原始对象
|
|
389
417
|
let data = { ...items[i] };
|
|
390
418
|
for (const [f, dv] of Object.entries(defaults)) {
|
|
391
|
-
if (data[f] === undefined) data[f] =
|
|
419
|
+
if (data[f] === undefined) data[f] = resolveDefaultValue(dv, f);
|
|
392
420
|
}
|
|
393
421
|
for (const f of Object.keys(computedFields)) data[f] = computedFields[f](data);
|
|
394
422
|
// 与单条 insert 保持一致:归一时间/数值/布尔/字符串类型(此前 insertMany 跳过了这一步)
|
|
@@ -1781,9 +1809,7 @@ class Table {
|
|
|
1781
1809
|
const result = { ...data };
|
|
1782
1810
|
for (const [field, defaultValue] of Object.entries(this._defaults)) {
|
|
1783
1811
|
if (result[field] === undefined) {
|
|
1784
|
-
result[field] =
|
|
1785
|
-
? defaultValue()
|
|
1786
|
-
: (defaultValue === 'CURRENT_TIMESTAMP' ? now() : defaultValue);
|
|
1812
|
+
result[field] = resolveDefaultValue(defaultValue, field);
|
|
1787
1813
|
}
|
|
1788
1814
|
}
|
|
1789
1815
|
return result;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsql-neo",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.6.0",
|
|
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 && node test/issue-63-68.test.js && node test/issue-69-70.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 && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-73-76.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 && node test/issue-63-68.test.js && node test/issue-69-70.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 && node test/issue-69-70.test.js && node test/issue-71-72.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",
|
package/postinstall.js
CHANGED
|
@@ -26,10 +26,12 @@ const NATIVE_PATH = process.env.JSQL_NATIVE_PATH
|
|
|
26
26
|
// 前缀必须显式设置 JSQL_GHPROXY(逗号分隔多个,按序回退;'' 表示直连);
|
|
27
27
|
// token 只能从环境变量 JSQL_GHPROXY_TOKEN 读取,代码内不再内置任何 token。
|
|
28
28
|
//
|
|
29
|
-
// 供应链安全(issue #25
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
|
|
29
|
+
// 供应链安全(issue #25):已移除内置 de5.net 加速站与硬编码 token。这里只内置
|
|
30
|
+
// 「不需要 token 的公共加速站」,且所有代理下载仍强制通过 SHA256SUMS 校验,
|
|
31
|
+
// 因此第三方端点被投毒也装不进去(校验不通过即回退到下一个候选/直连)。
|
|
32
|
+
// gh-proxy.com 实测:v6.5.32 资产 SHA256 与官方 SHA256SUMS 完全一致,且为合法 ELF。
|
|
33
|
+
// 优先级:JSQL_GHPROXY(用户显式)→ 内置公共加速站 → 直连 github.com。
|
|
34
|
+
const DEFAULT_PROXIES = ['https://gh-proxy.com/'];
|
|
33
35
|
const GHPROXY = process.env.JSQL_GHPROXY !== undefined
|
|
34
36
|
? process.env.JSQL_GHPROXY
|
|
35
37
|
: '';
|
package/test/issue-50.test.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* Issue #50 — DATETIME 列把 ISO-8601 的 Z / 时区偏移当成本地时间原样存储。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* 历史:#50 引入了「带时区标记 → 换算到本地时区;无标记 → 本地字面值」的语义。
|
|
5
|
+
* 自 6.6.0 起引擎改为 **UTC 稳定基准**(见 #75):
|
|
6
|
+
* - 带 Z / ±HH:MM → 先换算成绝对时刻,再以 UTC 墙上时间落库;
|
|
7
|
+
* - 不带时区标记 → 字面值即 UTC 墙上时间,原样落库。
|
|
8
|
+
* 这样 '2024-01-15T10:30:00Z' 与 '2024-01-15 10:30:00' 落库一致,
|
|
9
|
+
* 且结果与运行主机时区无关(旧语义在不同 TZ 的机器上落库值不同)。
|
|
6
10
|
*
|
|
7
|
-
*
|
|
11
|
+
* 断言全部用 Date.UTC 计算期望值,故任何 TZ 下都成立。
|
|
8
12
|
*/
|
|
9
13
|
const path = require('path');
|
|
10
14
|
const ROOT = path.join(__dirname, '..');
|
|
@@ -18,36 +22,37 @@ function ok(name, cond, extra) {
|
|
|
18
22
|
}
|
|
19
23
|
|
|
20
24
|
const pad2 = (n) => String(n).padStart(2, '0');
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
/** 绝对时刻(毫秒)→ UTC 墙上时间 'YYYY-MM-DD HH:MM:SS' —— 即落库格式 */
|
|
26
|
+
function utcStr(ms) {
|
|
27
|
+
const d = new Date(ms);
|
|
28
|
+
return `${d.getUTCFullYear()}-${pad2(d.getUTCMonth() + 1)}-${pad2(d.getUTCDate())} ` +
|
|
29
|
+
`${pad2(d.getUTCHours())}:${pad2(d.getUTCMinutes())}:${pad2(d.getUTCSeconds())}`;
|
|
25
30
|
}
|
|
31
|
+
/** 带时区标记的 ISO 串 → 落库值(UTC 墙上时间) */
|
|
32
|
+
function storedOf(iso) { return utcStr(Date.parse(iso)); }
|
|
26
33
|
|
|
27
34
|
(async () => {
|
|
28
35
|
const db = new Database(':memory:');
|
|
29
36
|
const q = (sql) => executeSQL(db, sql, { safety: false });
|
|
30
37
|
await q('CREATE TABLE t (id INTEGER PRIMARY KEY, dt DATETIME)');
|
|
31
38
|
|
|
32
|
-
// 1) 带 Z
|
|
39
|
+
// 1) 带 Z:绝对时刻不变,按 UTC 墙上时间存储
|
|
33
40
|
await q("INSERT INTO t (id, dt) VALUES (1, '2024-01-15T10:30:00Z')");
|
|
34
41
|
let r = await q('SELECT dt FROM t WHERE id = 1');
|
|
35
|
-
const expectZ =
|
|
36
|
-
ok('#50 ISO Z
|
|
37
|
-
|
|
38
|
-
ok('#50 UTC+8 下 Z 时间 +8 小时', r.rows[0][0] === '2024-01-15 18:30:00', r.rows[0][0]);
|
|
39
|
-
}
|
|
42
|
+
const expectZ = storedOf('2024-01-15T10:30:00Z');
|
|
43
|
+
ok('#50 ISO Z 按绝对时刻存储(UTC 墙上时间)', r.rows[0][0] === expectZ, { got: r.rows[0][0], want: expectZ });
|
|
44
|
+
ok('#50 Z 字面量具体值:2024-01-15 10:30:00', r.rows[0][0] === '2024-01-15 10:30:00', r.rows[0][0]);
|
|
40
45
|
|
|
41
46
|
// 2) 带正偏移 +02:00
|
|
42
47
|
await q("INSERT INTO t (id, dt) VALUES (2, '2024-01-15T10:30:00+02:00')");
|
|
43
48
|
r = await q('SELECT dt FROM t WHERE id = 2');
|
|
44
|
-
const expectPlus2 =
|
|
49
|
+
const expectPlus2 = storedOf('2024-01-15T10:30:00+02:00');
|
|
45
50
|
ok('#50 ISO +02:00 偏移正确换算', r.rows[0][0] === expectPlus2, { got: r.rows[0][0], want: expectPlus2 });
|
|
46
51
|
|
|
47
52
|
// 3) 带负偏移 -05:00
|
|
48
53
|
await q("INSERT INTO t (id, dt) VALUES (3, '2024-01-15T10:30:00-05:00')");
|
|
49
54
|
r = await q('SELECT dt FROM t WHERE id = 3');
|
|
50
|
-
const expectMinus5 =
|
|
55
|
+
const expectMinus5 = storedOf('2024-01-15T10:30:00-05:00');
|
|
51
56
|
ok('#50 ISO -05:00 偏移正确换算', r.rows[0][0] === expectMinus5, { got: r.rows[0][0], want: expectMinus5 });
|
|
52
57
|
|
|
53
58
|
// 4) 带毫秒 + Z
|
|
@@ -55,12 +60,14 @@ function localStr(utcMs) {
|
|
|
55
60
|
r = await q('SELECT dt FROM t WHERE id = 4');
|
|
56
61
|
ok('#50 带毫秒的 Z 同样换算(毫秒截断)', r.rows[0][0] === expectZ, r.rows[0][0]);
|
|
57
62
|
|
|
58
|
-
// 5)
|
|
63
|
+
// 5) 不带时区标记:字面值即 UTC 墙上时间,原样保存(与 Z 等价)
|
|
59
64
|
await q("INSERT INTO t (id, dt) VALUES (5, '2024-01-15T10:30:00')");
|
|
60
65
|
await q("INSERT INTO t (id, dt) VALUES (6, '2024-01-15 10:30:00')");
|
|
61
66
|
r = await q('SELECT dt FROM t WHERE id IN (5,6)');
|
|
62
67
|
ok('#50 无时区的 ISO 串按字面值保存', r.rows[0][0] === '2024-01-15 10:30:00', r.rows[0][0]);
|
|
63
68
|
ok('#50 MySQL 格式原样保留', r.rows[1][0] === '2024-01-15 10:30:00', r.rows[1][0]);
|
|
69
|
+
// 与 1) 同一行值完全相同 → 无时区与 Z 在 UTC 基准下等价(round-trip 闭合的关键)
|
|
70
|
+
ok('#50 无时区与 Z 落库一致', r.rows[0][0] === expectZ && r.rows[1][0] === expectZ, r.rows);
|
|
64
71
|
|
|
65
72
|
// 6) 纯日期列不受影响
|
|
66
73
|
await q('CREATE TABLE d (id INTEGER PRIMARY KEY, dd DATE)');
|
|
@@ -90,7 +97,7 @@ function localStr(utcMs) {
|
|
|
90
97
|
const s = isoCases[i];
|
|
91
98
|
await q(`INSERT INTO tz (id, dt) VALUES (${i + 1}, '${s}')`);
|
|
92
99
|
const got = (await q(`SELECT dt FROM tz WHERE id = ${i + 1}`)).rows[0][0];
|
|
93
|
-
const want =
|
|
100
|
+
const want = storedOf(s);
|
|
94
101
|
ok(`#50 等价写法 ${s} 换算正确`, got === want, { got, want });
|
|
95
102
|
}
|
|
96
103
|
|
|
@@ -98,12 +105,18 @@ function localStr(utcMs) {
|
|
|
98
105
|
await q('CREATE TABLE crossyear (id INTEGER PRIMARY KEY, dt DATETIME)');
|
|
99
106
|
await q("INSERT INTO crossyear VALUES (1, '2024-01-01T00:30:00+09:00')");
|
|
100
107
|
r = await q('SELECT dt FROM crossyear WHERE id = 1');
|
|
101
|
-
//
|
|
102
|
-
const expectCross =
|
|
108
|
+
// +09:00 的 2024-01-01 00:30 == 2023-12-31 15:30Z(跨回上一年)
|
|
109
|
+
const expectCross = storedOf('2024-01-01T00:30:00+09:00');
|
|
103
110
|
ok('#50 时区换算可跨年', r.rows[0][0] === expectCross, { got: r.rows[0][0], want: expectCross });
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
111
|
+
ok('#50 跨年具体值 2023-12-31 15:30:00', r.rows[0][0] === '2023-12-31 15:30:00', r.rows[0][0]);
|
|
112
|
+
|
|
113
|
+
// 10) 主机时区无关性:同一 INSERT 在任何 TZ 下都得到同一落库值
|
|
114
|
+
ok('#50 落库值与主机时区无关(无时区字面量)',
|
|
115
|
+
validateDateType('2024-01-15 10:30:00', 'datetime', 'dt') === '2024-01-15 10:30:00',
|
|
116
|
+
validateDateType('2024-01-15 10:30:00', 'datetime', 'dt'));
|
|
117
|
+
ok('#50 TIMESTAMP 落库值与主机时区无关',
|
|
118
|
+
validateDateType('2020-01-02 03:04:05', 'timestamp', 'ts') === Math.floor(Date.UTC(2020, 0, 2, 3, 4, 5) / 1000),
|
|
119
|
+
validateDateType('2020-01-02 03:04:05', 'timestamp', 'ts'));
|
|
107
120
|
})();
|
|
108
121
|
|
|
109
122
|
setTimeout(() => {
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Issue #71 / #72 回归测试。
|
|
3
|
+
*
|
|
4
|
+
* #72: `CREATE TABLE t (..., ts TIMESTAMP DEFAULT NOW())` 插入时报
|
|
5
|
+
* "Incorrect date value: 'ts'" —— 根因是 Table 侧只把字符串
|
|
6
|
+
* 'CURRENT_TIMESTAMP' 特判成 now(),'NOW()' 等函数默认值被原样当成
|
|
7
|
+
* 列值,随后日期校验拿列名当值报错。现在 NOW()/NOW(n)/LOCALTIME(STAMP)
|
|
8
|
+
* 都按当前时间戳求值,其他不支持的函数表达式明确报错。
|
|
9
|
+
*
|
|
10
|
+
* #71: `SELECT id FROM a WHERE COUNT(*) > 0` 此前静默返回 0 行(聚合在逐行
|
|
11
|
+
* 上下文中求值为 NULL)。现在在解析阶段就报 1111
|
|
12
|
+
* "Invalid use of group function",与 MySQL 一致;HAVING 不受影响。
|
|
13
|
+
*/
|
|
14
|
+
const path = require('path');
|
|
15
|
+
|
|
16
|
+
const ROOT = path.join(__dirname, '..');
|
|
17
|
+
const { Database } = require(path.join(ROOT, 'index.js'));
|
|
18
|
+
const { executeSQL } = require(path.join(ROOT, 'lib/sql.js'));
|
|
19
|
+
|
|
20
|
+
const OPTS = { safety: false };
|
|
21
|
+
let pass = 0, fail = 0;
|
|
22
|
+
|
|
23
|
+
function ok(name, cond, extra) {
|
|
24
|
+
if (cond) { pass++; console.log('[OK] ', name); }
|
|
25
|
+
else { fail++; console.log('[FAIL]', name, extra !== undefined ? '-> ' + JSON.stringify(extra).slice(0, 160) : ''); }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function t(name, fn) {
|
|
29
|
+
const db = new Database(':memory:');
|
|
30
|
+
const exec = (sql) => executeSQL(db, sql, OPTS);
|
|
31
|
+
const rows = async (sql) => (await exec(sql)).rows;
|
|
32
|
+
const mustFail = async (sql) => {
|
|
33
|
+
try { await exec(sql); return false; } catch (_) { return true; }
|
|
34
|
+
};
|
|
35
|
+
try {
|
|
36
|
+
const r = await fn({ db, exec, rows, mustFail });
|
|
37
|
+
ok(name, r === undefined ? true : !!r);
|
|
38
|
+
} catch (e) {
|
|
39
|
+
ok(name, false, e.message);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
(async () => {
|
|
44
|
+
console.log('\n--- Issue #72: DEFAULT NOW() ---');
|
|
45
|
+
await t('TIMESTAMP DEFAULT NOW() 插入成功且取当前时间', async ({ exec, rows }) => {
|
|
46
|
+
const before = Math.floor(Date.now() / 1000);
|
|
47
|
+
await exec('CREATE TABLE t (id INTEGER PRIMARY KEY, ts TIMESTAMP DEFAULT NOW())');
|
|
48
|
+
await exec('INSERT INTO t (id) VALUES (1)');
|
|
49
|
+
const r = await rows('SELECT ts FROM t');
|
|
50
|
+
const ts = r[0][0];
|
|
51
|
+
return typeof ts === 'number' && ts >= before - 5 && ts <= before + 10;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
await t('DATETIME DEFAULT NOW() / NOW(6) 同样工作', async ({ exec, rows }) => {
|
|
55
|
+
await exec('CREATE TABLE t (id INTEGER PRIMARY KEY, a DATETIME DEFAULT NOW(), b DATETIME DEFAULT NOW(6))');
|
|
56
|
+
await exec('INSERT INTO t (id) VALUES (1)');
|
|
57
|
+
const r = await rows('SELECT a, b FROM t');
|
|
58
|
+
return typeof r[0][0] === 'string' && r[0][0].length >= 10
|
|
59
|
+
&& typeof r[0][1] === 'string' && r[0][1].length >= 10;
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
await t('DEFAULT CURRENT_TIMESTAMP 不回归', async ({ exec, rows }) => {
|
|
63
|
+
await exec('CREATE TABLE t (id INTEGER PRIMARY KEY, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP)');
|
|
64
|
+
await exec('INSERT INTO t (id) VALUES (1)');
|
|
65
|
+
const r = await rows('SELECT ts FROM t');
|
|
66
|
+
return typeof r[0][0] === 'number' && r[0][0] > 1600000000;
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
await t('显式日期值仍优先于默认值', async ({ exec, rows }) => {
|
|
70
|
+
await exec("CREATE TABLE t (id INTEGER PRIMARY KEY, ts TIMESTAMP DEFAULT NOW())");
|
|
71
|
+
await exec("INSERT INTO t (id, ts) VALUES (1, '2020-01-02 03:04:05')");
|
|
72
|
+
const r = await rows('SELECT ts FROM t');
|
|
73
|
+
// 无时区字面量按 UTC 墙上时间解释(6.6.0 起的稳定基准):
|
|
74
|
+
// 2020-01-02 03:04:05 UTC == 1577934245。这里用 Date.UTC 计算,
|
|
75
|
+
// 断言与运行环境时区无关(此前硬编码 1577905445 是 +08:00 下的值,UTC runner 必红)。
|
|
76
|
+
return r[0][0] === Math.floor(Date.UTC(2020, 0, 2, 3, 4, 5) / 1000);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
await t('不支持的函数默认值明确报错(不再说 Incorrect date value)', async ({ mustFail, exec }) => {
|
|
80
|
+
await exec('CREATE TABLE t (id INTEGER PRIMARY KEY, ts TIMESTAMP DEFAULT UUID())');
|
|
81
|
+
let msg = '';
|
|
82
|
+
try { await exec('INSERT INTO t (id) VALUES (1)'); } catch (e) { msg = e.message; }
|
|
83
|
+
return /Unsupported default expression/i.test(msg) || await mustFail('INSERT INTO t (id) VALUES (1)');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
await t('普通字面量默认值不回归', async ({ exec, rows }) => {
|
|
87
|
+
await exec("CREATE TABLE t (id INTEGER PRIMARY KEY, s VARCHAR(20) DEFAULT 'abc', n INTEGER DEFAULT 3)");
|
|
88
|
+
await exec('INSERT INTO t (id) VALUES (1)');
|
|
89
|
+
const r = await rows('SELECT s, n FROM t');
|
|
90
|
+
return r[0][0] === 'abc' && r[0][1] === 3;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
console.log('\n--- Issue #71: WHERE 中的聚合函数 ---');
|
|
94
|
+
const setup = async (exec) => {
|
|
95
|
+
await exec('CREATE TABLE a (id INTEGER PRIMARY KEY, v INTEGER)');
|
|
96
|
+
await exec('INSERT INTO a (id, v) VALUES (1, 5), (2, 7), (3, 9)');
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
await t('WHERE COUNT(*) > 0 报错', async ({ exec, mustFail }) => {
|
|
100
|
+
await setup(exec);
|
|
101
|
+
return await mustFail('SELECT id FROM a WHERE COUNT(*) > 0');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
await t('WHERE SUM(v) > 10 报错', async ({ exec, mustFail }) => {
|
|
105
|
+
await setup(exec);
|
|
106
|
+
return await mustFail('SELECT id FROM a WHERE SUM(v) > 10');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
await t('嵌套在 AND / NOT / 算术里的聚合也报错', async ({ exec, mustFail }) => {
|
|
110
|
+
await setup(exec);
|
|
111
|
+
const a = await mustFail('SELECT id FROM a WHERE v > 1 AND AVG(v) > 0');
|
|
112
|
+
const b = await mustFail('SELECT id FROM a WHERE NOT COUNT(*) > 0');
|
|
113
|
+
const c = await mustFail('SELECT id FROM a WHERE COUNT(*) + 1 > 0');
|
|
114
|
+
return a && b && c;
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
await t('普通 WHERE 不回归', async ({ exec, rows }) => {
|
|
118
|
+
await setup(exec);
|
|
119
|
+
const r = await rows('SELECT id FROM a WHERE v > 6');
|
|
120
|
+
return JSON.stringify(r) === JSON.stringify([[2], [3]]);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
await t('HAVING 中的聚合不受影响', async ({ exec, rows }) => {
|
|
124
|
+
await setup(exec);
|
|
125
|
+
const r = await rows('SELECT COUNT(*) AS c FROM a HAVING COUNT(*) > 2');
|
|
126
|
+
return r.length === 1 && r[0][0] === 3;
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
await t('子查询里的聚合合法(WHERE ... IN (SELECT MAX(...)))', async ({ exec, rows }) => {
|
|
130
|
+
await setup(exec);
|
|
131
|
+
const r = await rows('SELECT id FROM a WHERE v IN (SELECT MAX(v) FROM a)');
|
|
132
|
+
return JSON.stringify(r) === JSON.stringify([[3]]);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
console.log(`\n${pass} passed, ${fail} failed`);
|
|
136
|
+
process.exit(fail === 0 ? 0 : 1);
|
|
137
|
+
})().catch(e => { console.error('💥', e); process.exit(1); });
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Issue #73 / #74 / #75 / #76 回归测试。
|
|
3
|
+
*
|
|
4
|
+
* #73: `GROUP BY <数字位置>` 被忽略,所有行塌成一组。
|
|
5
|
+
* 解析器把数字变成 { type:'value' } 常量,执行层对每行求值得到同一个键。
|
|
6
|
+
* 现在按 SELECT 列表 1-based 位置解析,越界明确报错。
|
|
7
|
+
*
|
|
8
|
+
* #74: `ORDER BY <越界位置>` 静默忽略(取到 undefined 列 → 键恒为 null)。
|
|
9
|
+
* 现在越界必须报错;合法位置序号行为不变。
|
|
10
|
+
*
|
|
11
|
+
* #75: INSERT 会把 ISO-8601 DATETIME 字面量规范化成 'YYYY-MM-DD HH:MM:SS',
|
|
12
|
+
* 但 WHERE 比较路径不做同样规范化,同一字面量查不回自己插入的行。
|
|
13
|
+
* 现在比较侧用同一套规则。6.6.0 起规范化基准是 UTC(无时区 = UTC 墙上时间),
|
|
14
|
+
* 因此 '...Z' 与无时区写法在 UTC 字面量上完全等价。
|
|
15
|
+
*
|
|
16
|
+
* #76: JOIN 里未加表名前缀的歧义列名(两张表都有 id)被静默解析成右表的值。
|
|
17
|
+
* 现在报 "column reference 'id' is ambiguous";限定引用与单表查询不受影响。
|
|
18
|
+
*/
|
|
19
|
+
const path = require('path');
|
|
20
|
+
|
|
21
|
+
const ROOT = path.join(__dirname, '..');
|
|
22
|
+
const { Database } = require(path.join(ROOT, 'index.js'));
|
|
23
|
+
const { executeSQL } = require(path.join(ROOT, 'lib/sql.js'));
|
|
24
|
+
|
|
25
|
+
const OPTS = { safety: false };
|
|
26
|
+
let pass = 0, fail = 0;
|
|
27
|
+
|
|
28
|
+
function ok(name, cond, extra) {
|
|
29
|
+
if (cond) { pass++; console.log('[OK] ', name); }
|
|
30
|
+
else { fail++; console.log('[FAIL]', name, extra !== undefined ? '-> ' + JSON.stringify(extra).slice(0, 200) : ''); }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function t(name, fn) {
|
|
34
|
+
const db = new Database(':memory:');
|
|
35
|
+
const exec = (sql) => executeSQL(db, sql, OPTS);
|
|
36
|
+
const rows = async (sql) => (await exec(sql)).rows;
|
|
37
|
+
const mustFail = async (sql) => {
|
|
38
|
+
try { await exec(sql); return false; } catch (_) { return true; }
|
|
39
|
+
};
|
|
40
|
+
const errOf = async (sql) => {
|
|
41
|
+
try { await exec(sql); return ''; } catch (e) { return e.message || String(e); }
|
|
42
|
+
};
|
|
43
|
+
try {
|
|
44
|
+
const r = await fn({ db, exec, rows, mustFail, errOf });
|
|
45
|
+
ok(name, r === undefined ? true : !!r);
|
|
46
|
+
} catch (e) {
|
|
47
|
+
ok(name, false, e.message);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
52
|
+
/** 某 UTC 毫秒对应的 UTC 'YYYY-MM-DD HH:MM:SS'(与 validateDateTime 的落库格式一致)。
|
|
53
|
+
* 6.6.0 起引擎以 UTC 为稳定基准,落库值是 UTC 墙上时间,因此用 getUTC*。 */
|
|
54
|
+
function utcOf(ms) {
|
|
55
|
+
const d = new Date(ms);
|
|
56
|
+
return d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()) + ' ' +
|
|
57
|
+
pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()) + ':' + pad(d.getUTCSeconds());
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
(async () => {
|
|
61
|
+
console.log('\n--- Issue #73: GROUP BY <位置序号> ---');
|
|
62
|
+
const seedAB = async (exec) => {
|
|
63
|
+
await exec('CREATE TABLE t (a INTEGER, b TEXT)');
|
|
64
|
+
await exec("INSERT INTO t (a, b) VALUES (1, 'x'), (1, 'y'), (2, 'z')");
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
await t('GROUP BY 1 按第 1 个输出列分组', async ({ exec, rows }) => {
|
|
68
|
+
await seedAB(exec);
|
|
69
|
+
const r = await rows('SELECT a, COUNT(*) FROM t GROUP BY 1 ORDER BY 1');
|
|
70
|
+
return JSON.stringify(r) === JSON.stringify([[1, 2], [2, 1]]);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
await t('GROUP BY 2 按第 2 个输出列分组', async ({ exec, rows }) => {
|
|
74
|
+
await seedAB(exec);
|
|
75
|
+
const r = await rows('SELECT b, COUNT(*) AS c FROM t GROUP BY b ORDER BY 1');
|
|
76
|
+
const r2 = await rows('SELECT COUNT(*) AS c, b FROM t GROUP BY 2 ORDER BY 2');
|
|
77
|
+
return r.length === 3 && r.every(x => x[1] === 1)
|
|
78
|
+
&& JSON.stringify(r2.map(x => x[1])) === JSON.stringify(r.map(x => x[0]));
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
await t('GROUP BY 位置指向聚合列时明确报错', async ({ exec, errOf }) => {
|
|
82
|
+
await seedAB(exec);
|
|
83
|
+
return /refers to an aggregate/i.test(await errOf('SELECT COUNT(*) AS c FROM t GROUP BY 1'));
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
await t('GROUP BY 1 指向表达式列(别名)同样生效', async ({ exec, rows }) => {
|
|
87
|
+
await seedAB(exec);
|
|
88
|
+
const r = await rows('SELECT a % 2 AS p, COUNT(*) FROM t GROUP BY 1 ORDER BY 1');
|
|
89
|
+
return JSON.stringify(r) === JSON.stringify([[0, 1], [1, 2]]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
await t('GROUP BY 超出输出列数必须报错', async ({ exec, errOf }) => {
|
|
93
|
+
await seedAB(exec);
|
|
94
|
+
return /GROUP BY column index 99 is out of range/i.test(await errOf('SELECT a FROM t GROUP BY 99'));
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
await t('GROUP BY 99 / 0 报错且信息可读', async ({ exec, errOf }) => {
|
|
98
|
+
await seedAB(exec);
|
|
99
|
+
const a = await errOf('SELECT a, COUNT(*) FROM t GROUP BY 99');
|
|
100
|
+
const b = await errOf('SELECT a, COUNT(*) FROM t GROUP BY 0');
|
|
101
|
+
return /GROUP BY column index 99 is out of range/i.test(a)
|
|
102
|
+
&& /GROUP BY column index 0 is out of range/i.test(b);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
await t('GROUP BY 列名不回归', async ({ exec, rows }) => {
|
|
106
|
+
await seedAB(exec);
|
|
107
|
+
const r = await rows('SELECT a, COUNT(*) FROM t GROUP BY a ORDER BY a');
|
|
108
|
+
return JSON.stringify(r) === JSON.stringify([[1, 2], [2, 1]]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
console.log('\n--- Issue #74: ORDER BY <越界位置序号> ---');
|
|
112
|
+
await t('ORDER BY 2(只有 1 列)必须报错', async ({ exec, errOf }) => {
|
|
113
|
+
await seedAB(exec);
|
|
114
|
+
return /ORDER BY column index 2 is out of range/i.test(await errOf('SELECT a FROM t ORDER BY 2'));
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
await t('GROUP BY 路径里的越界 ORDER BY 同样报错', async ({ exec, errOf }) => {
|
|
118
|
+
await seedAB(exec);
|
|
119
|
+
return /ORDER BY column index 3 is out of range/i.test(
|
|
120
|
+
await errOf('SELECT a, COUNT(*) FROM t GROUP BY 1 ORDER BY 3')
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
await t('合法位置序号排序不受影响', async ({ exec, rows }) => {
|
|
125
|
+
await seedAB(exec);
|
|
126
|
+
const r1 = await rows('SELECT a, b FROM t ORDER BY 1 DESC');
|
|
127
|
+
const r2 = await rows('SELECT a, b FROM t ORDER BY 2 DESC');
|
|
128
|
+
return r1[0][0] === 2
|
|
129
|
+
&& JSON.stringify(r2.map(x => x[1])) === JSON.stringify(['z', 'y', 'x']);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
await t('ORDER BY 列名 / 表达式不回归', async ({ exec, rows }) => {
|
|
133
|
+
await seedAB(exec);
|
|
134
|
+
const r1 = await rows('SELECT a FROM t ORDER BY a DESC');
|
|
135
|
+
const r2 = await rows('SELECT a, UPPER(b) AS u FROM t ORDER BY u DESC');
|
|
136
|
+
return r1[0][0] === 2 && r2[0][1] === 'Z';
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
console.log('\n--- Issue #75: DATETIME ISO-8601 字面量可查回 ---');
|
|
140
|
+
const seedDT = async (exec) => {
|
|
141
|
+
await exec('CREATE TABLE dt (id INTEGER PRIMARY KEY, d DATETIME, s TEXT)');
|
|
142
|
+
await exec("INSERT INTO dt (id, d, s) VALUES (1, '2024-01-15T10:30:00Z', '2024-01-15T10:30:00Z')");
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
await t('带 Z 的字面量 insert 后能按同一字面量查回', async ({ exec, rows }) => {
|
|
146
|
+
await seedDT(exec);
|
|
147
|
+
const expected = utcOf(Date.UTC(2024, 0, 15, 10, 30, 0)); // === '2024-01-15 10:30:00'
|
|
148
|
+
const all = await rows('SELECT d FROM dt');
|
|
149
|
+
const hit = await rows("SELECT d FROM dt WHERE d = '2024-01-15T10:30:00Z'");
|
|
150
|
+
// Z 与无时区在 UTC 基准下是同一瞬间的同一墙上时间 → 两种写法都查回
|
|
151
|
+
const hitNoTz = await rows("SELECT d FROM dt WHERE d = '2024-01-15T10:30:00'");
|
|
152
|
+
return all[0][0] === expected && JSON.stringify(hit) === JSON.stringify([[expected]])
|
|
153
|
+
&& JSON.stringify(hitNoTz) === JSON.stringify([[expected]]);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
await t('无时区标记的 T 分隔字面量能往返(insert → where)', async ({ exec, rows }) => {
|
|
157
|
+
await exec('CREATE TABLE dt2 (id INTEGER PRIMARY KEY, d DATETIME)');
|
|
158
|
+
await exec("INSERT INTO dt2 (id, d) VALUES (1, '2024-01-15T10:30:00')");
|
|
159
|
+
const all = await rows('SELECT d FROM dt2');
|
|
160
|
+
const hit = await rows("SELECT d FROM dt2 WHERE d = '2024-01-15T10:30:00'");
|
|
161
|
+
return all[0][0] === '2024-01-15 10:30:00'
|
|
162
|
+
&& JSON.stringify(hit) === JSON.stringify([['2024-01-15 10:30:00']]);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
await t('±HH:MM 偏移字面量同样规范化', async ({ exec, rows }) => {
|
|
166
|
+
await exec('CREATE TABLE dt3 (id INTEGER PRIMARY KEY, d DATETIME)');
|
|
167
|
+
// 2024-01-15T10:30:00+08:00 == 2024-01-15T02:30:00Z → 落库 '2024-01-15 02:30:00'(UTC)
|
|
168
|
+
await exec("INSERT INTO dt3 (id, d) VALUES (1, '2024-01-15T10:30:00+08:00')");
|
|
169
|
+
const stored = utcOf(Date.UTC(2024, 0, 15, 2, 30, 0));
|
|
170
|
+
const hit = await rows("SELECT d FROM dt3 WHERE d = '2024-01-15T10:30:00+08:00'");
|
|
171
|
+
const hitZ = await rows("SELECT d FROM dt3 WHERE d = '2024-01-15T02:30:00Z'");
|
|
172
|
+
const hitNoTz = await rows("SELECT d FROM dt3 WHERE d = '2024-01-15 02:30:00'");
|
|
173
|
+
return JSON.stringify(hit) === JSON.stringify([[stored]])
|
|
174
|
+
&& JSON.stringify(hitZ) === JSON.stringify([[stored]])
|
|
175
|
+
&& JSON.stringify(hitNoTz) === JSON.stringify([[stored]]);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
await t('IN 列表里的 ISO 字面量同样生效', async ({ exec, rows }) => {
|
|
179
|
+
await seedDT(exec);
|
|
180
|
+
const expected = utcOf(Date.UTC(2024, 0, 15, 10, 30, 0));
|
|
181
|
+
const hit = await rows("SELECT d FROM dt WHERE d IN ('2024-01-15T10:30:00Z')");
|
|
182
|
+
const miss = await rows("SELECT d FROM dt WHERE d IN ('2023-01-01T00:00:00Z')");
|
|
183
|
+
return JSON.stringify(hit) === JSON.stringify([[expected]]) && miss.length === 0;
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
await t('TEXT 列仍按原始字符串比较(不被规范化)', async ({ exec, rows }) => {
|
|
187
|
+
await seedDT(exec);
|
|
188
|
+
const hit = await rows("SELECT s FROM dt WHERE s = '2024-01-15T10:30:00Z'");
|
|
189
|
+
const miss = await rows("SELECT s FROM dt WHERE s = '2024-01-15 18:30:00'");
|
|
190
|
+
return JSON.stringify(hit) === JSON.stringify([['2024-01-15T10:30:00Z']]) && miss.length === 0;
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
await t('非法日期字面量不抛错、只是不匹配', async ({ exec, rows }) => {
|
|
194
|
+
await seedDT(exec);
|
|
195
|
+
const r = await rows("SELECT d FROM dt WHERE d = 'not-a-date'");
|
|
196
|
+
return r.length === 0;
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
console.log('\n--- Issue #76: JOIN 歧义列名 ---');
|
|
200
|
+
const seedJoin = async (exec) => {
|
|
201
|
+
await exec('CREATE TABLE u (id INTEGER PRIMARY KEY, name TEXT)');
|
|
202
|
+
await exec('CREATE TABLE o (id INTEGER PRIMARY KEY, u_id INTEGER, amt INTEGER)');
|
|
203
|
+
await exec("INSERT INTO u (id, name) VALUES (1, 'a'), (2, 'b')");
|
|
204
|
+
await exec('INSERT INTO o (id, u_id, amt) VALUES (10, 1, 5), (11, 2, 7)');
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
await t('SELECT 裸 id + ORDER BY 裸 id 报歧义', async ({ exec, errOf }) => {
|
|
208
|
+
await seedJoin(exec);
|
|
209
|
+
return /column reference 'id' is ambiguous/i.test(
|
|
210
|
+
await errOf('SELECT id FROM u JOIN o ON o.u_id = u.id ORDER BY id')
|
|
211
|
+
);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
await t('WHERE 中的裸歧义列也报错(不能静默返回空集)', async ({ exec, errOf }) => {
|
|
215
|
+
await seedJoin(exec);
|
|
216
|
+
return /column reference 'id' is ambiguous/i.test(
|
|
217
|
+
await errOf('SELECT u.id FROM u JOIN o ON o.u_id = u.id WHERE id = 10')
|
|
218
|
+
);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
await t('GROUP BY / ORDER BY 里的裸歧义列也报错', async ({ exec, errOf }) => {
|
|
222
|
+
await seedJoin(exec);
|
|
223
|
+
const a = await errOf('SELECT COUNT(*) FROM u JOIN o ON o.u_id = u.id GROUP BY id');
|
|
224
|
+
const b = await errOf('SELECT COUNT(*) FROM u JOIN o ON o.u_id = u.id ORDER BY id');
|
|
225
|
+
return /column reference 'id' is ambiguous/i.test(a)
|
|
226
|
+
&& /column reference 'id' is ambiguous/i.test(b);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
await t('已加表名前缀的查询不受影响', async ({ exec, rows }) => {
|
|
230
|
+
await seedJoin(exec);
|
|
231
|
+
const r = await rows('SELECT u.id, o.id AS oid, u.name FROM u JOIN o ON o.u_id = u.id ORDER BY o.id');
|
|
232
|
+
return JSON.stringify(r) === JSON.stringify([[1, 10, 'a'], [2, 11, 'b']]);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
await t('非歧义裸列 + 单表查询不受影响', async ({ exec, rows }) => {
|
|
236
|
+
await seedJoin(exec);
|
|
237
|
+
const r1 = await rows('SELECT amt FROM u JOIN o ON o.u_id = u.id ORDER BY amt');
|
|
238
|
+
const r2 = await rows('SELECT id FROM u ORDER BY id DESC');
|
|
239
|
+
const r3 = await rows('SELECT * FROM u JOIN o ON o.u_id = u.id');
|
|
240
|
+
return JSON.stringify(r1) === JSON.stringify([[5], [7]])
|
|
241
|
+
&& JSON.stringify(r2) === JSON.stringify([[2], [1]])
|
|
242
|
+
&& r3.length === 2 && r3[0].length === 5;
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
await t('自连接用别名时裸列仍按别名区分(不误报)', async ({ exec, rows }) => {
|
|
246
|
+
await exec('CREATE TABLE emp (id INTEGER PRIMARY KEY, mgr_id INTEGER, name TEXT)');
|
|
247
|
+
await exec("INSERT INTO emp (id, mgr_id, name) VALUES (1, NULL, 'boss'), (2, 1, 'alice')");
|
|
248
|
+
const r = await rows('SELECT e.name AS en, m.name AS mn FROM emp e LEFT JOIN emp m ON e.mgr_id = m.id ORDER BY e.id');
|
|
249
|
+
return r.length === 2 && r[0][0] === 'boss' && r[1][1] === 'boss';
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
console.log(`\n${pass} passed, ${fail} failed`);
|
|
253
|
+
process.exit(fail === 0 ? 0 : 1);
|
|
254
|
+
})().catch(e => { console.error('💥', e); process.exit(1); });
|