jsql-neo 6.8.11 → 6.8.12
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/mongo_server.js +28 -1
- package/lib/sql.js +89 -18
- package/package.json +3 -3
- package/test/issue-284-292.test.js +154 -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.8.
|
|
6
|
+
> **v6.8.12** — 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/mongo_server.js
CHANGED
|
@@ -42,6 +42,26 @@ function bsonDocument(obj) {
|
|
|
42
42
|
continue;
|
|
43
43
|
}
|
|
44
44
|
if (typeof v === 'number') {
|
|
45
|
+
// #284:整数按 BSON 规范编 Int32(0x10) / Int64(0x12),只有真正的浮点
|
|
46
|
+
// 才用 Double(0x01)。此前一律编 double,导致线上类型与驱动预期不符
|
|
47
|
+
// ($type:'int' 永远匹配不到),且 int32 本可 4 字节却占了 8 字节。
|
|
48
|
+
if (Number.isInteger(v)) {
|
|
49
|
+
if (v >= -2147483648 && v <= 2147483647) {
|
|
50
|
+
const b = Buffer.alloc(4);
|
|
51
|
+
b.writeInt32LE(v, 0);
|
|
52
|
+
body = Buffer.concat([body, Buffer.from([0x10]), name, Buffer.from([0]), b]);
|
|
53
|
+
} else if (Number.isSafeInteger(v)) {
|
|
54
|
+
const b = Buffer.alloc(8);
|
|
55
|
+
b.writeBigInt64LE(BigInt(v), 0);
|
|
56
|
+
body = Buffer.concat([body, Buffer.from([0x12]), name, Buffer.from([0]), b]);
|
|
57
|
+
} else {
|
|
58
|
+
// 超出安全整数范围:double 已是唯一无损的表示
|
|
59
|
+
const b = Buffer.alloc(8);
|
|
60
|
+
b.writeDoubleLE(v, 0);
|
|
61
|
+
body = Buffer.concat([body, Buffer.from([0x01]), name, Buffer.from([0]), b]);
|
|
62
|
+
}
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
45
65
|
const b = Buffer.alloc(8);
|
|
46
66
|
b.writeDoubleLE(v, 0);
|
|
47
67
|
body = Buffer.concat([body, Buffer.from([0x01]), name, Buffer.from([0]), b]);
|
|
@@ -65,9 +85,16 @@ function bsonDocument(obj) {
|
|
|
65
85
|
continue;
|
|
66
86
|
}
|
|
67
87
|
if (Buffer.isBuffer(v)) {
|
|
88
|
+
// #285:BSON Binary 布局 = type 0x05 + 字段名 + int32 长度 +
|
|
89
|
+
// **1 字节 subtype** + length 字节数据。
|
|
90
|
+
// 此前写成 Buffer.from([0, 0])(两个字节),比规范多 1 字节 ——
|
|
91
|
+
// 解码端按规范只跳 1 个 subtype 字节,于是冗余 0x00 被当成数据首字节,
|
|
92
|
+
// 整个字节流错位([1,2,3,4] 读回成 [0,0,1,2]),且后续字段解析失步。
|
|
68
93
|
const lenBuf = Buffer.alloc(4);
|
|
69
94
|
lenBuf.writeInt32LE(v.length, 0);
|
|
70
|
-
|
|
95
|
+
const subtype = (v.subtype !== undefined && v.subtype !== null) ? (v.subtype & 0xff) : 0;
|
|
96
|
+
body = Buffer.concat([body, Buffer.from([0x05]), name, Buffer.from([0]), lenBuf,
|
|
97
|
+
Buffer.from([subtype]), v]);
|
|
71
98
|
continue;
|
|
72
99
|
}
|
|
73
100
|
if (v instanceof Date) {
|
package/lib/sql.js
CHANGED
|
@@ -3787,20 +3787,84 @@ function bigintExactArith(l, r, op) {
|
|
|
3787
3787
|
* 恒小,于是 ORDER BY col DESC 时 NULL 跑到最前,与普通 SELECT 的 ORDER BY
|
|
3788
3788
|
* 不一致;抽成一个函数后两条排序路径不会再各写一套。
|
|
3789
3789
|
*/
|
|
3790
|
+
/**
|
|
3791
|
+
* SQL 比较用的类型归一(Issue #291 / #292)。
|
|
3792
|
+
*
|
|
3793
|
+
* MySQL 8.0(11.2 Type Conversion in Expression Evaluation):
|
|
3794
|
+
* "If one operand is a number and the other is a string, the string is
|
|
3795
|
+
* converted to a number."
|
|
3796
|
+
*
|
|
3797
|
+
* 因此混合类型比较时,若一侧是数值、另一侧是可解析为有限数值的字符串,
|
|
3798
|
+
* 应按数值比较,而不是退化成字典序。
|
|
3799
|
+
*
|
|
3800
|
+
* 返回归一后的值;两侧无法按数值统一时返回 undefined(调用方回退原逻辑)。
|
|
3801
|
+
*/
|
|
3802
|
+
function sqlNumericCoerce(v) {
|
|
3803
|
+
if (typeof v === 'number') return Number.isFinite(v) ? v : undefined;
|
|
3804
|
+
if (typeof v === 'bigint') return undefined; // BigInt 与 number 混用另有口径
|
|
3805
|
+
if (typeof v === 'boolean') return v ? 1 : 0;
|
|
3806
|
+
if (typeof v === 'string') {
|
|
3807
|
+
const t = v.trim();
|
|
3808
|
+
if (t === '') return undefined;
|
|
3809
|
+
// 必须整串都是数字(可含小数/指数),否则 'abc' 不能当 0
|
|
3810
|
+
if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/.test(t)) return undefined;
|
|
3811
|
+
const n = Number(t);
|
|
3812
|
+
return Number.isFinite(n) ? n : undefined;
|
|
3813
|
+
}
|
|
3814
|
+
return undefined;
|
|
3815
|
+
}
|
|
3816
|
+
|
|
3817
|
+
/**
|
|
3818
|
+
* 分组键 / 窗口分区键的归一化(Issue #291)。
|
|
3819
|
+
*
|
|
3820
|
+
* 分组按 SQL 比较规则分组:数字 1 与字符串 '1' 是同一个键值,必须落在
|
|
3821
|
+
* 同一组。此前直接对原始 JS 值做 JSON.stringify,两个键文本不同 →
|
|
3822
|
+
* 被拆成两个伪组,COUNT(*) 偏小、SUM/AVG 被割裂。
|
|
3823
|
+
*
|
|
3824
|
+
* 归一规则:
|
|
3825
|
+
* · 字符串能解析为有限数值 → 折叠为数值表示(与数字同键)
|
|
3826
|
+
* · 布尔 → 0/1
|
|
3827
|
+
* · 其余原样
|
|
3828
|
+
*/
|
|
3829
|
+
function sqlGroupKeyNormalize(v) {
|
|
3830
|
+
if (v === null || v === undefined) return v;
|
|
3831
|
+
if (typeof v === 'boolean') return v ? 1 : 0;
|
|
3832
|
+
if (typeof v === 'string') {
|
|
3833
|
+
const n = sqlNumericCoerce(v);
|
|
3834
|
+
if (n !== undefined) return n;
|
|
3835
|
+
return v;
|
|
3836
|
+
}
|
|
3837
|
+
return v;
|
|
3838
|
+
}
|
|
3839
|
+
|
|
3790
3840
|
function compareSqlValues(av, bv, dir) {
|
|
3791
|
-
|
|
3792
|
-
|
|
3793
|
-
|
|
3794
|
-
|
|
3795
|
-
|
|
3796
|
-
|
|
3797
|
-
|
|
3841
|
+
const desc = dir === 'desc';
|
|
3842
|
+
if (av === bv) return 0;
|
|
3843
|
+
const aNull = av === null || av === undefined;
|
|
3844
|
+
const bNull = bv === null || bv === undefined;
|
|
3845
|
+
if (aNull || bNull) {
|
|
3846
|
+
if (aNull && bNull) return 0;
|
|
3847
|
+
const r = aNull ? -1 : 1;
|
|
3848
|
+
return desc ? -r : r;
|
|
3849
|
+
}
|
|
3850
|
+
let r;
|
|
3851
|
+
if (typeof av === 'number' && typeof bv === 'number') {
|
|
3852
|
+
r = av - bv;
|
|
3853
|
+
} else {
|
|
3854
|
+
// #292:混合 number/string 时先按 SQL 规则归一为数值比较。
|
|
3855
|
+
// 此前只要一侧是字符串就退化成字典序,于是 '10' 与 2 的排序反了
|
|
3856
|
+
// (实际输出 1, 10, 2;MySQL 是 1, 2, 10)。
|
|
3857
|
+
const na = sqlNumericCoerce(av);
|
|
3858
|
+
const nb = sqlNumericCoerce(bv);
|
|
3859
|
+
if (na !== undefined && nb !== undefined
|
|
3860
|
+
&& (typeof av !== 'string' || typeof bv !== 'string' || na !== nb)) {
|
|
3861
|
+
// 两侧都是字符串且归一后相等(如 '1' 与 '1.0')→ 视为相等
|
|
3862
|
+
r = na === nb ? 0 : (na < nb ? -1 : 1);
|
|
3863
|
+
} else {
|
|
3864
|
+
r = String(av) < String(bv) ? -1 : String(av) > String(bv) ? 1 : 0;
|
|
3865
|
+
}
|
|
3866
|
+
}
|
|
3798
3867
|
return desc ? -r : r;
|
|
3799
|
-
}
|
|
3800
|
-
const r = typeof av === 'number' && typeof bv === 'number'
|
|
3801
|
-
? av - bv
|
|
3802
|
-
: (String(av) < String(bv) ? -1 : String(av) > String(bv) ? 1 : 0);
|
|
3803
|
-
return desc ? -r : r;
|
|
3804
3868
|
}
|
|
3805
3869
|
|
|
3806
3870
|
function resolveOperand(operand, row, ctx) {
|
|
@@ -9071,7 +9135,7 @@ class SQLExecutor {
|
|
|
9071
9135
|
const buckets = new Map();
|
|
9072
9136
|
rows.forEach((r, idx) => {
|
|
9073
9137
|
const key = partBy.length
|
|
9074
|
-
? partBy.map(p => JSON.stringify(resolveOperand(p, r, this.ctx))).join('')
|
|
9138
|
+
? partBy.map(p => JSON.stringify(sqlGroupKeyNormalize(resolveOperand(p, r, this.ctx)))).join('')
|
|
9075
9139
|
: '';
|
|
9076
9140
|
if (!buckets.has(key)) buckets.set(key, []);
|
|
9077
9141
|
buckets.get(key).push(idx);
|
|
@@ -9623,21 +9687,28 @@ class SQLExecutor {
|
|
|
9623
9687
|
// 表达式分组(#51):直接对原始行求值表达式
|
|
9624
9688
|
if (g && typeof g === 'object') {
|
|
9625
9689
|
const v = resolveOperand(g, row, this.ctx);
|
|
9626
|
-
|
|
9690
|
+
// #291:分组键按 SQL 比较规则归一 —— 数字 1 与字符串 '1'
|
|
9691
|
+
// 是同一个键值,必须落在同一组。
|
|
9692
|
+
const nv = sqlGroupKeyNormalize(v);
|
|
9693
|
+
return typeof nv === 'bigint' ? nv.toString() + 'n' : nv;
|
|
9627
9694
|
}
|
|
9628
9695
|
const key = String(g).toLowerCase();
|
|
9629
9696
|
if (aliases[key]) {
|
|
9630
9697
|
// 优先用行中已有的 alias 值(投影后才有),没有则尝试从原始列取值
|
|
9631
|
-
if (row[key] !== undefined) return row[key];
|
|
9698
|
+
if (row[key] !== undefined) return sqlGroupKeyNormalize(row[key]);
|
|
9632
9699
|
// alias → 原始列名 的映射(如 b → a),从原始行取该列的值
|
|
9633
|
-
if (aliasToCol[key])
|
|
9700
|
+
if (aliasToCol[key]) {
|
|
9701
|
+
return sqlGroupKeyNormalize(resolveOperand({ type: 'column', name: aliasToCol[key] }, row, this.ctx));
|
|
9702
|
+
}
|
|
9634
9703
|
if (aliasExpr[key]) {
|
|
9635
9704
|
const v = resolveOperand(aliasExpr[key], row, this.ctx);
|
|
9636
|
-
|
|
9705
|
+
const nv = sqlGroupKeyNormalize(v);
|
|
9706
|
+
return typeof nv === 'bigint' ? nv.toString() + 'n' : nv;
|
|
9637
9707
|
}
|
|
9638
9708
|
return row[key]; // row[key] 为 undefined,但保持一致
|
|
9639
9709
|
}
|
|
9640
|
-
|
|
9710
|
+
// #291:列分组的键同样归一
|
|
9711
|
+
return sqlGroupKeyNormalize(resolveOperand({ type: 'column', name: g }, row, this.ctx));
|
|
9641
9712
|
}));
|
|
9642
9713
|
if (!groups.has(key)) groups.set(key, []);
|
|
9643
9714
|
groups.get(key).push(row);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsql-neo",
|
|
3
|
-
"version": "6.8.
|
|
3
|
+
"version": "6.8.12",
|
|
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/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 && node test/issue-314-321.test.js && node test/issue-312-320.test.js && node test/issue-243-289.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 && node test/issue-314-321.test.js && node test/issue-312-320.test.js && node test/issue-243-289.test.js && node test/issue-284-292.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/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 && node test/issue-314-321.test.js && node test/issue-312-320.test.js && node test/issue-243-289.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 && node test/issue-314-321.test.js && node test/issue-312-320.test.js && node test/issue-243-289.test.js && node test/issue-284-292.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",
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/* ---- 回归:#284 / #285 / #291 / #292 (+ #290 位运算) ---- */
|
|
2
|
+
/*
|
|
3
|
+
* #284 Mongo BSON 编码把所有整数都写成 Double(0x01),从不写 Int32(0x10)。
|
|
4
|
+
* #285 Mongo BSON Binary(0x05) 编码多写一个 subtype 字节,往返损坏错位。
|
|
5
|
+
* #291 GROUP BY 键未做 SQL 类型归一:数字 1 与字符串 '1' 被拆成两个组。
|
|
6
|
+
* #292 ORDER BY 混合 number/string 时退化为字典序,数值顺序排错。
|
|
7
|
+
* #290 位非 `~` 按 32 位有符号整数取反,与 MySQL 的 64 位无符号语义不符。
|
|
8
|
+
*/
|
|
9
|
+
const path = require('path');
|
|
10
|
+
const net = require('net');
|
|
11
|
+
const ROOT = path.join(__dirname, '..');
|
|
12
|
+
const { Database, executeSQL } = require(path.join(ROOT, 'index.js'));
|
|
13
|
+
const { createMongoServer, bsonDocument, bsonReadDocument } = require(path.join(ROOT, 'lib/mongo_server.js'));
|
|
14
|
+
|
|
15
|
+
let pass = 0, fail = 0;
|
|
16
|
+
function ok(name, cond, got) {
|
|
17
|
+
if (cond) { pass++; console.log('[OK] ' + name); }
|
|
18
|
+
else { fail++; console.log('[FAIL] ' + name + ' -> ' + JSON.stringify(got)); }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function mongoOpMsg(doc) {
|
|
22
|
+
const p = bsonDocument(doc);
|
|
23
|
+
const h = Buffer.alloc(16);
|
|
24
|
+
h.writeInt32LE(16 + 4 + 1 + p.length, 0);
|
|
25
|
+
h.writeInt32LE(1, 4); h.writeInt32LE(0, 8); h.writeInt32LE(2013, 12);
|
|
26
|
+
return Buffer.concat([h, Buffer.alloc(4), Buffer.from([0]), p]);
|
|
27
|
+
}
|
|
28
|
+
function mongoCall(srv, doc) {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
const sock = net.connect(srv._server.address().port, '127.0.0.1', () => sock.write(mongoOpMsg(doc)));
|
|
31
|
+
const chunks = [];
|
|
32
|
+
sock.on('error', reject);
|
|
33
|
+
sock.on('data', (c) => {
|
|
34
|
+
chunks.push(c);
|
|
35
|
+
const buf = Buffer.concat(chunks);
|
|
36
|
+
if (buf.length >= 16 && buf.length >= buf.readInt32LE(0)) {
|
|
37
|
+
try { resolve({ raw: buf, doc: bsonReadDocument(buf.slice(21), 0).value }); }
|
|
38
|
+
catch (e) { reject(e); }
|
|
39
|
+
sock.end();
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
(async () => {
|
|
46
|
+
// ================= #284 BSON 整数类型 =================
|
|
47
|
+
const enc = bsonDocument({ n: 30 });
|
|
48
|
+
const hex = enc.toString('hex');
|
|
49
|
+
// size(4) + type(1) + 'n'(2) + int32(4)
|
|
50
|
+
ok("编码 {n:30} 用 Int32(type byte 0x10)", hex.includes('106e00'), hex);
|
|
51
|
+
ok("Int32 只占 4 字节(文档总长 12)", enc.readInt32LE(0) === 12, enc.readInt32LE(0));
|
|
52
|
+
|
|
53
|
+
const encF = bsonDocument({ n: 2.5 });
|
|
54
|
+
ok("浮点仍用 Double(type byte 0x01)", encF.toString('hex').includes('016e00'), encF.toString('hex'));
|
|
55
|
+
|
|
56
|
+
const encBig = bsonDocument({ n: 3000000000 });
|
|
57
|
+
ok("超 int32 范围用 Int64(type byte 0x12)", encBig.toString('hex').includes('126e00'), encBig.toString('hex'));
|
|
58
|
+
|
|
59
|
+
// 端到端:线上回包的 type byte
|
|
60
|
+
const msrv = createMongoServer({ dataDir: ':memory:' });
|
|
61
|
+
await new Promise((r) => msrv.listen(r));
|
|
62
|
+
await mongoCall(msrv, { insert: 'it', documents: [{ n: 30 }], $db: 'test' });
|
|
63
|
+
const r1 = await mongoCall(msrv, { find: 'it', filter: {}, $db: 'test' });
|
|
64
|
+
const i1 = r1.raw.indexOf(Buffer.from('n\0'));
|
|
65
|
+
const typeByte = r1.raw.slice(i1 - 1, i1).toString('hex');
|
|
66
|
+
ok('线上回包 n 的 type byte = 10(int32)', typeByte === '10', typeByte);
|
|
67
|
+
ok('回读值仍是 30', r1.doc.cursor.firstBatch[0].n === 30, r1.doc.cursor.firstBatch[0].n);
|
|
68
|
+
|
|
69
|
+
// ================= #285 BSON Binary 往返 =================
|
|
70
|
+
const orig = Buffer.from([1, 2, 3, 4]);
|
|
71
|
+
const bEnc = bsonDocument({ data: orig });
|
|
72
|
+
const bHex = bEnc.toString('hex');
|
|
73
|
+
// size(4) + type(1) + 'data\0'(5) + len(4) + subtype(1) + data(4) + 结束符(1) = 20
|
|
74
|
+
ok('Binary 编码长度正确(20 字节)', bEnc.readInt32LE(0) === 20, bEnc.readInt32LE(0));
|
|
75
|
+
ok('Binary 只写 1 个 subtype 字节', /05646174610004000000 00 01020304/.test(bHex.replace(/ /g, ''))
|
|
76
|
+
|| bHex.includes('056461746100040000000001020304'), bHex);
|
|
77
|
+
const bDec = bsonReadDocument(bEnc, 0).value;
|
|
78
|
+
ok('Binary 往返逐字节相等', Buffer.from(bDec.data).equals(orig), Buffer.from(bDec.data));
|
|
79
|
+
|
|
80
|
+
// 多字段文档:Binary 之后的字段不能解析失步
|
|
81
|
+
const multi = bsonDocument({ data: orig, tail: 7 });
|
|
82
|
+
const mDec = bsonReadDocument(multi, 0).value;
|
|
83
|
+
ok('Binary 之后的字段仍能正确解析', mDec.tail === 7, mDec);
|
|
84
|
+
ok('多字段文档里 Binary 数据完好', Buffer.from(mDec.data).equals(orig), Buffer.from(mDec.data));
|
|
85
|
+
|
|
86
|
+
// 端到端往返
|
|
87
|
+
await mongoCall(msrv, { insert: 'bin', documents: [{ _id: 'b', data: orig }], $db: 'test' });
|
|
88
|
+
const r2 = await mongoCall(msrv, { find: 'bin', filter: {}, $db: 'test' });
|
|
89
|
+
const got = Buffer.from(r2.doc.cursor.firstBatch[0].data);
|
|
90
|
+
ok('服务端往返二进制数据完好', got.equals(orig), got);
|
|
91
|
+
|
|
92
|
+
msrv.close();
|
|
93
|
+
|
|
94
|
+
// ================= #291 GROUP BY 键归一 =================
|
|
95
|
+
const db = new Database(':memory:', { autoSave: false });
|
|
96
|
+
const q = async (sql) => (await executeSQL(db, sql, { safety: false })).rows;
|
|
97
|
+
|
|
98
|
+
await q('CREATE TABLE m (k TEXT, v INT)');
|
|
99
|
+
await q("INSERT INTO m VALUES ('1',10),(1,20),('2',30),(2,40)");
|
|
100
|
+
const grouped = await q('SELECT k, COUNT(*), SUM(v) FROM m GROUP BY k ORDER BY k');
|
|
101
|
+
ok("GROUP BY 把 '1' 与 1 合并成 1 组", grouped.length === 2, grouped);
|
|
102
|
+
ok("GROUP BY '1' 组 COUNT=2, SUM=30",
|
|
103
|
+
JSON.stringify(grouped[0]) === JSON.stringify(['1', 2, 30]), grouped);
|
|
104
|
+
ok("GROUP BY '2' 组 COUNT=2, SUM=70",
|
|
105
|
+
JSON.stringify(grouped[1]) === JSON.stringify(['2', 2, 70]), grouped);
|
|
106
|
+
|
|
107
|
+
// 纯数值分组不回归
|
|
108
|
+
await q('CREATE TABLE m2 (k INT, v INT)');
|
|
109
|
+
await q('INSERT INTO m2 VALUES (1,10),(1,20),(2,30)');
|
|
110
|
+
const g2 = await q('SELECT k, COUNT(*), SUM(v) FROM m2 GROUP BY k ORDER BY k');
|
|
111
|
+
ok('纯数值 GROUP BY 不回归',
|
|
112
|
+
JSON.stringify(g2) === JSON.stringify([[1, 2, 30], [2, 1, 30]]), g2);
|
|
113
|
+
|
|
114
|
+
// ================= #292 ORDER BY 混合类型 =================
|
|
115
|
+
await q('CREATE TABLE s (k TEXT)');
|
|
116
|
+
await q("INSERT INTO s VALUES ('10'),(2),('1')");
|
|
117
|
+
const ordered = await q('SELECT k FROM s ORDER BY k');
|
|
118
|
+
const keys = ordered.map(r => r[0]);
|
|
119
|
+
ok("ORDER BY 混合类型按数值排序:1, 2, 10",
|
|
120
|
+
keys.length === 3 && Number(keys[0]) === 1 && Number(keys[1]) === 2 && Number(keys[2]) === 10, keys);
|
|
121
|
+
|
|
122
|
+
// 纯字符串排序仍按字典序
|
|
123
|
+
await q('CREATE TABLE s2 (k TEXT)');
|
|
124
|
+
await q("INSERT INTO s2 VALUES ('b'),('a'),('c')");
|
|
125
|
+
const o2 = await q('SELECT k FROM s2 ORDER BY k');
|
|
126
|
+
ok('纯字符串 ORDER BY 仍按字典序',
|
|
127
|
+
JSON.stringify(o2.map(r => r[0])) === JSON.stringify(['a', 'b', 'c']), o2);
|
|
128
|
+
|
|
129
|
+
// 非数字字符串仍按字典序(不能被误当数值)
|
|
130
|
+
await q('CREATE TABLE s3 (k TEXT)');
|
|
131
|
+
await q("INSERT INTO s3 VALUES ('b'),('10'),('a')");
|
|
132
|
+
const o3 = await q('SELECT k FROM s3 ORDER BY k');
|
|
133
|
+
ok("非数字字符串仍按字典序('10' < 'a' < 'b')",
|
|
134
|
+
JSON.stringify(o3.map(r => r[0])) === JSON.stringify(['10', 'a', 'b']), o3);
|
|
135
|
+
|
|
136
|
+
// ================= #290 位运算 64 位无符号 =================
|
|
137
|
+
const one = async (sql) => (await q(sql))[0][0];
|
|
138
|
+
ok("~0 = 18446744073709551615(MySQL 64 位无符号)",
|
|
139
|
+
String(await one('SELECT ~0')) === '18446744073709551615', null);
|
|
140
|
+
ok("~5 = 18446744073709551610",
|
|
141
|
+
String(await one('SELECT ~5')) === '18446744073709551610', null);
|
|
142
|
+
ok('5 & ~1 = 4', await one('SELECT 5 & ~1') === 4, null);
|
|
143
|
+
ok('12 & 10 = 8', await one('SELECT 12 & 10') === 8, null);
|
|
144
|
+
ok('12 | 10 = 14', await one('SELECT 12 | 10') === 14, null);
|
|
145
|
+
ok('12 ^ 10 = 6', await one('SELECT 12 ^ 10') === 6, null);
|
|
146
|
+
ok('1 << 63 仍是精确 BigInt',
|
|
147
|
+
String(await one('SELECT 1 << 63')) === '9223372036854775808', null);
|
|
148
|
+
|
|
149
|
+
console.log(`\n${pass} passed, ${fail} failed`);
|
|
150
|
+
process.exit(fail === 0 ? 0 : 1);
|
|
151
|
+
})().catch(e => {
|
|
152
|
+
console.log('[FAIL] unexpected exception: ' + (e.stack || e).split('\n').slice(0, 4).join('\n'));
|
|
153
|
+
process.exit(1);
|
|
154
|
+
});
|