jsql-neo 6.5.20 → 6.5.23
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 +9 -2
- package/bin/yaggs-fallback.js +108 -0
- package/index.js +1 -1
- package/lib/bigint_json.js +39 -0
- package/lib/database.js +31 -14
- package/lib/jsql_format.js +11 -1
- package/lib/mysql_server.js +22 -8
- package/lib/native_client.js +38 -8
- package/lib/pg_server.js +5 -1
- package/lib/query.js +7 -4
- package/lib/sql.js +260 -53
- package/lib/table.js +81 -20
- package/lib/tui.js +28 -6
- package/lib/wasm_client.js +62 -10
- package/lib/xql/src/compiler.js +129 -25
- package/lib/xql/src/tokenizer.js +10 -2
- package/native/jsql-neo-native.node +0 -0
- package/nativesrc/jsql-neo-core/src/engine/hybrid.rs +14 -3
- package/nativesrc/jsql-neo-core/src/engine/table.rs +64 -4
- package/nativesrc/jsql-neo-wasm/src/lib.rs +39 -14
- package/package.json +7 -5
- package/postinstall.js +42 -18
- package/test/cli-fallback.test.js +55 -0
- package/test/issue-34-35-36.test.js +142 -0
- package/test/issue-38.test.js +279 -0
- package/test/issue-39.test.js +160 -0
- package/test/issue-40.test.js +264 -0
- package/test/issue-g4.test.js +82 -0
- package/test/native-add-column.test.js +74 -0
- package/test/sql-parser.test.js +9 -1
- package/wasm/browser.mjs +19 -1
- package/wasm/browser_bg.mjs +150 -97
- package/wasm/jsql_neo_wasm.d.ts +5 -0
- package/wasm/jsql_neo_wasm.js +28 -0
- package/wasm/jsql_neo_wasm_bg.wasm +0 -0
- package/wasm/jsql_neo_wasm_bg.wasm.d.ts +1 -0
- package/nativesrc/jsql-neo-native/jsql-neo-native.node +0 -0
package/lib/sql.js
CHANGED
|
@@ -201,6 +201,19 @@ function jsonPathGet(data, path) {
|
|
|
201
201
|
return current === undefined ? null : current;
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
+
/**
|
|
205
|
+
* 16 进制整数字面量(0x?? / X'??')的精确解析(Issue #38)。
|
|
206
|
+
*
|
|
207
|
+
* 旧的 parseInt(hexStr, 16) 在 >2^53 时直接丢精度,且与十进制字面量
|
|
208
|
+
* (已被解析成 BigInt)行为不一致;这里统一成同样的规则:
|
|
209
|
+
* 安全范围内 → number;超出 → BigInt(精确值保留给 int64 列)。
|
|
210
|
+
*/
|
|
211
|
+
function parseHexIntegerLiteral(hexStr) {
|
|
212
|
+
if (hexStr.length === 0) throw new Error('Invalid hexadecimal literal');
|
|
213
|
+
const b = BigInt('0x' + hexStr);
|
|
214
|
+
return b <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(b) : b;
|
|
215
|
+
}
|
|
216
|
+
|
|
204
217
|
function tokenize(sql) {
|
|
205
218
|
const tokens = [];
|
|
206
219
|
let i = 0;
|
|
@@ -303,7 +316,7 @@ function tokenize(sql) {
|
|
|
303
316
|
while (j < n && /[0-9A-Fa-f]/.test(sql[j])) j++;
|
|
304
317
|
if (j === i + 2) throw new Error(`Invalid hexadecimal literal at position ${i}`);
|
|
305
318
|
const hexStr = sql.slice(i + 2, j);
|
|
306
|
-
const numVal =
|
|
319
|
+
const numVal = parseHexIntegerLiteral(hexStr);
|
|
307
320
|
tokens.push(new SQLToken('number', numVal, i));
|
|
308
321
|
i = j;
|
|
309
322
|
continue;
|
|
@@ -317,7 +330,7 @@ function tokenize(sql) {
|
|
|
317
330
|
j++;
|
|
318
331
|
}
|
|
319
332
|
if (j >= n) throw new Error(`Unterminated hexadecimal literal at position ${i}`);
|
|
320
|
-
const numVal =
|
|
333
|
+
const numVal = parseHexIntegerLiteral(hexStr);
|
|
321
334
|
tokens.push(new SQLToken('number', numVal, i));
|
|
322
335
|
i = j + 1;
|
|
323
336
|
continue;
|
|
@@ -484,6 +497,7 @@ class Parser {
|
|
|
484
497
|
this.paramCount = 0; // 顺序占位符 ? 的序号(从 0 开始)
|
|
485
498
|
this.subquerySeq = 0; // FROM 子查询无别名时自动生成 __subN
|
|
486
499
|
this.depth = 0; // 子查询递归深度
|
|
500
|
+
this.barePredicateDepth = 0; // >0 时允许裸布尔谓词(WHERE 1 / WHERE col / WHERE NULL)
|
|
487
501
|
}
|
|
488
502
|
|
|
489
503
|
peek(offset = 0) { return this.tokens[this.pos + offset]; }
|
|
@@ -717,6 +731,8 @@ class Parser {
|
|
|
717
731
|
this.expect('op', '(');
|
|
718
732
|
|
|
719
733
|
const schema = {};
|
|
734
|
+
// 表级 FOREIGN KEY 可能写在列定义之前,先收集、待所有列解析完再绑定
|
|
735
|
+
const pendingFKs = [];
|
|
720
736
|
let hasPk = false;
|
|
721
737
|
while (true) {
|
|
722
738
|
const t = this.peek();
|
|
@@ -753,14 +769,14 @@ class Parser {
|
|
|
753
769
|
} else if (t.type === 'keyword' && t.value === 'CONSTRAINT') {
|
|
754
770
|
this.expectKeyword('CONSTRAINT');
|
|
755
771
|
if (this.peek().type === 'ident') this.next(); // 约束名
|
|
756
|
-
this._parseTableConstraint(schema);
|
|
772
|
+
this._parseTableConstraint(schema, pendingFKs);
|
|
757
773
|
} else if (this.isWord('FOREIGN') || this.isWord('CHECK') ||
|
|
758
774
|
t.type === 'keyword' && (t.value === 'KEY' || t.value === 'INDEX')) {
|
|
759
775
|
// 表级约束:FOREIGN KEY ... REFERENCES ... / CHECK (...) / KEY name (cols)
|
|
760
776
|
// KEY/INDEX 是保留字:裸写作列名(如 CREATE TABLE t (key TEXT))必须报错,
|
|
761
777
|
// 由 _parseTableConstraint 消费 KEY 后遇到类型 token 触发
|
|
762
778
|
// "Expected ',' or ')'",见 identifier-keywords 测试。
|
|
763
|
-
this._parseTableConstraint(schema);
|
|
779
|
+
this._parseTableConstraint(schema, pendingFKs);
|
|
764
780
|
} else if (t.type === 'eof' || (t.type === 'op' && t.value === ')')) {
|
|
765
781
|
break;
|
|
766
782
|
} else {
|
|
@@ -791,6 +807,13 @@ class Parser {
|
|
|
791
807
|
if (!hasPk && schema.id === undefined) {
|
|
792
808
|
schema.id = { type: 'integer', primaryKey: true, autoIncrement: true };
|
|
793
809
|
}
|
|
810
|
+
// 表级外键落到对应列的 def.foreignKey 上,由 lib/table.js 统一注册/校验
|
|
811
|
+
for (const { column, fk } of pendingFKs) {
|
|
812
|
+
if (!schema[column]) {
|
|
813
|
+
throw new Error(`Unknown column '${column}' in foreign key definition of table '${name}'`);
|
|
814
|
+
}
|
|
815
|
+
schema[column].foreignKey = fk;
|
|
816
|
+
}
|
|
794
817
|
return { type: 'createTable', name, schema, ifNotExists };
|
|
795
818
|
}
|
|
796
819
|
|
|
@@ -853,6 +876,25 @@ class Parser {
|
|
|
853
876
|
if (this.peek().type === 'op' && this.peek().value === '(') this._skipParens();
|
|
854
877
|
continue;
|
|
855
878
|
}
|
|
879
|
+
// 列级外键: pid INTEGER REFERENCES p(id) [ON DELETE ...] [ON UPDATE ...]
|
|
880
|
+
// 此前 REFERENCES 没被消费,直接留在流里 → "Expected ',' or ')'"。
|
|
881
|
+
if (this.isWord('REFERENCES')) {
|
|
882
|
+
this.next();
|
|
883
|
+
const refTable = this.parseTableName();
|
|
884
|
+
const refCols = this._parseParenNameList();
|
|
885
|
+
let onDelete = 'restrict';
|
|
886
|
+
let onUpdate = 'restrict';
|
|
887
|
+
while (this.isKeyword('ON')) {
|
|
888
|
+
this.next();
|
|
889
|
+
const which = String(this.next().value).toUpperCase();
|
|
890
|
+
const action = this._parseFKAction();
|
|
891
|
+
if (which === 'DELETE') onDelete = action;
|
|
892
|
+
else if (which === 'UPDATE') onUpdate = action;
|
|
893
|
+
else throw new Error(`Expected DELETE or UPDATE after ON, got '${which}'`);
|
|
894
|
+
}
|
|
895
|
+
def.foreignKey = { table: refTable, field: refCols[0] || 'id', onDelete, onUpdate };
|
|
896
|
+
continue;
|
|
897
|
+
}
|
|
856
898
|
if (t.type === 'keyword') {
|
|
857
899
|
switch (t.value) {
|
|
858
900
|
case 'PRIMARY':
|
|
@@ -946,27 +988,75 @@ class Parser {
|
|
|
946
988
|
}
|
|
947
989
|
}
|
|
948
990
|
|
|
991
|
+
/** 解析括号内的名字列表: (a, b);无括号时返回空数组。 */
|
|
992
|
+
_parseParenNameList() {
|
|
993
|
+
const names = [];
|
|
994
|
+
if (!(this.peek().type === 'op' && this.peek().value === '(')) return names;
|
|
995
|
+
this.next();
|
|
996
|
+
while (true) {
|
|
997
|
+
names.push(this.parseTableName());
|
|
998
|
+
if (this.peek().type === 'op' && this.peek().value === ',') { this.next(); continue; }
|
|
999
|
+
break;
|
|
1000
|
+
}
|
|
1001
|
+
this.expect('op', ')');
|
|
1002
|
+
return names;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
/** 解析外键动作: CASCADE / RESTRICT / SET NULL / NO ACTION */
|
|
1006
|
+
_parseFKAction() {
|
|
1007
|
+
const first = String(this.next().value).toUpperCase();
|
|
1008
|
+
if (first === 'CASCADE') return 'cascade';
|
|
1009
|
+
if (first === 'RESTRICT') return 'restrict';
|
|
1010
|
+
if (first === 'SET') {
|
|
1011
|
+
const second = String(this.next().value).toUpperCase();
|
|
1012
|
+
if (second === 'NULL') return 'set null';
|
|
1013
|
+
throw new Error(`Unsupported foreign key action: 'SET ${second}' (only SET NULL is supported)`);
|
|
1014
|
+
}
|
|
1015
|
+
if (first === 'NO') {
|
|
1016
|
+
if (this.isWord('ACTION')) this.next();
|
|
1017
|
+
return 'no action';
|
|
1018
|
+
}
|
|
1019
|
+
throw new Error(`Unsupported foreign key action: '${first}'`);
|
|
1020
|
+
}
|
|
1021
|
+
|
|
949
1022
|
/**
|
|
950
1023
|
* 解析表级约束(FOREIGN KEY / CHECK / KEY / INDEX / PRIMARY KEY / UNIQUE)。
|
|
951
|
-
*
|
|
1024
|
+
* FOREIGN KEY 会真正解析出 field/refTable/refField/onDelete/onUpdate 并登记,
|
|
1025
|
+
* 与列级 REFERENCES 行为一致(校验在 lib/table.js)。
|
|
952
1026
|
*/
|
|
953
|
-
_parseTableConstraint(schema) {
|
|
1027
|
+
_parseTableConstraint(schema, pendingFKs) {
|
|
954
1028
|
if (this.isWord('FOREIGN')) {
|
|
955
1029
|
this.next();
|
|
956
1030
|
if (!this.isKeyword('KEY')) throw new Error(`Expected KEY after FOREIGN, got '${this.peek().value}'`);
|
|
957
1031
|
this.next();
|
|
958
|
-
|
|
959
|
-
if (
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
1032
|
+
const cols = this._parseParenNameList();
|
|
1033
|
+
if (!cols.length) throw new Error('Expected column list in FOREIGN KEY constraint');
|
|
1034
|
+
if (!this.isWord('REFERENCES')) throw new Error(`Expected REFERENCES in FOREIGN KEY constraint, got '${this.peek().value}'`);
|
|
1035
|
+
this.next();
|
|
1036
|
+
const refTable = this.parseTableName();
|
|
1037
|
+
const refCols = this._parseParenNameList();
|
|
1038
|
+
let onDelete = 'restrict';
|
|
1039
|
+
let onUpdate = 'restrict';
|
|
965
1040
|
while (this.isKeyword('ON')) {
|
|
966
1041
|
this.next();
|
|
967
|
-
this.next();
|
|
968
|
-
const
|
|
969
|
-
if (
|
|
1042
|
+
const which = String(this.next().value).toUpperCase();
|
|
1043
|
+
const action = this._parseFKAction();
|
|
1044
|
+
if (which === 'DELETE') onDelete = action;
|
|
1045
|
+
else if (which === 'UPDATE') onUpdate = action;
|
|
1046
|
+
else throw new Error(`Expected DELETE or UPDATE after ON, got '${which}'`);
|
|
1047
|
+
}
|
|
1048
|
+
// 复合外键无法用现有的单列校验表示;显式报错好过静默丢弃
|
|
1049
|
+
if (cols.length > 1) {
|
|
1050
|
+
throw new Error('Composite foreign key constraints are not supported (FOREIGN KEY with multiple columns)');
|
|
1051
|
+
}
|
|
1052
|
+
const fk = { table: refTable, field: refCols[0] || 'id', onDelete, onUpdate };
|
|
1053
|
+
if (pendingFKs) {
|
|
1054
|
+
// 列可能还没解析到,推迟到 parseCreateTable 末尾绑定
|
|
1055
|
+
pendingFKs.push({ column: cols[0], fk });
|
|
1056
|
+
} else if (schema[cols[0]]) {
|
|
1057
|
+
schema[cols[0]].foreignKey = fk;
|
|
1058
|
+
} else {
|
|
1059
|
+
throw new Error(`Unknown column '${cols[0]}' in foreign key definition`);
|
|
970
1060
|
}
|
|
971
1061
|
return;
|
|
972
1062
|
}
|
|
@@ -1436,7 +1526,7 @@ class Parser {
|
|
|
1436
1526
|
from = this.parseFrom();
|
|
1437
1527
|
}
|
|
1438
1528
|
let where = null;
|
|
1439
|
-
if (this.isKeyword('WHERE')) { this.next(); where = this.
|
|
1529
|
+
if (this.isKeyword('WHERE')) { this.next(); where = this.parseBooleanExpr(); }
|
|
1440
1530
|
let groupBy = null;
|
|
1441
1531
|
let rollup = false;
|
|
1442
1532
|
if (this.isKeyword('GROUP')) {
|
|
@@ -1455,7 +1545,7 @@ class Parser {
|
|
|
1455
1545
|
}
|
|
1456
1546
|
}
|
|
1457
1547
|
let having = null;
|
|
1458
|
-
if (this.isKeyword('HAVING')) { this.next(); having = this.
|
|
1548
|
+
if (this.isKeyword('HAVING')) { this.next(); having = this.parseBooleanExpr(); }
|
|
1459
1549
|
let orderBy = null;
|
|
1460
1550
|
if (this.isKeyword('ORDER')) {
|
|
1461
1551
|
this.expectKeyword('ORDER'); this.expectKeyword('BY');
|
|
@@ -1590,7 +1680,7 @@ class Parser {
|
|
|
1590
1680
|
}
|
|
1591
1681
|
const item = this.parseFromItem();
|
|
1592
1682
|
let on = null;
|
|
1593
|
-
if (this.isKeyword('ON')) { this.next(); on = this.
|
|
1683
|
+
if (this.isKeyword('ON')) { this.next(); on = this.parseBooleanExpr(); }
|
|
1594
1684
|
joins.push({ type, item, on });
|
|
1595
1685
|
}
|
|
1596
1686
|
return { tables, joins };
|
|
@@ -1661,7 +1751,7 @@ class Parser {
|
|
|
1661
1751
|
break;
|
|
1662
1752
|
}
|
|
1663
1753
|
let where = null;
|
|
1664
|
-
if (this.isKeyword('WHERE')) { this.next(); where = this.
|
|
1754
|
+
if (this.isKeyword('WHERE')) { this.next(); where = this.parseBooleanExpr(); }
|
|
1665
1755
|
else console.warn(`⚠️ UPDATE without WHERE clause — this will modify ALL rows in '${table}'`);
|
|
1666
1756
|
// UPDATE ... ORDER BY ... LIMIT n(与 DELETE 对称;按排序后的前 n 行更新)
|
|
1667
1757
|
let orderBy = null, limit = null;
|
|
@@ -1694,7 +1784,7 @@ class Parser {
|
|
|
1694
1784
|
this.expectKeyword('FROM');
|
|
1695
1785
|
const table = this.parseTableName();
|
|
1696
1786
|
let where = null;
|
|
1697
|
-
if (this.isKeyword('WHERE')) { this.next(); where = this.
|
|
1787
|
+
if (this.isKeyword('WHERE')) { this.next(); where = this.parseBooleanExpr(); }
|
|
1698
1788
|
let orderBy = null, limit = null;
|
|
1699
1789
|
if (this.isKeyword('ORDER')) {
|
|
1700
1790
|
this.next(); this.expectKeyword('BY');
|
|
@@ -2039,6 +2129,21 @@ class Parser {
|
|
|
2039
2129
|
return this.parseOr();
|
|
2040
2130
|
}
|
|
2041
2131
|
|
|
2132
|
+
/**
|
|
2133
|
+
* 布尔上下文表达式(WHERE / HAVING / ON 条件)。
|
|
2134
|
+
* 与 parseExpr 相同,但允许「裸布尔谓词」:
|
|
2135
|
+
* WHERE 1 / WHERE col / WHERE NULL / WHERE NULL AND b = 1
|
|
2136
|
+
* 此时 parseComparison 在找不到比较运算符时把左侧表达式直接作为布尔值返回。
|
|
2137
|
+
*/
|
|
2138
|
+
parseBooleanExpr() {
|
|
2139
|
+
this.barePredicateDepth = (this.barePredicateDepth || 0) + 1;
|
|
2140
|
+
try {
|
|
2141
|
+
return this.parseExpr();
|
|
2142
|
+
} finally {
|
|
2143
|
+
this.barePredicateDepth--;
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
|
|
2042
2147
|
/**
|
|
2043
2148
|
* 函数实参用的表达式:与 parseExpr 相同,但叶子允许是"裸操作数"。
|
|
2044
2149
|
* 例如 IF(1 > 0, 'y', 'n') 的第一个参数是完整比较表达式,
|
|
@@ -2897,6 +3002,11 @@ class Parser {
|
|
|
2897
3002
|
// 函数实参场景允许裸操作数(IF(age, ...) / SUM(IF(...)) 等)
|
|
2898
3003
|
if (this.bareExprDepth > 0) return left;
|
|
2899
3004
|
|
|
3005
|
+
// 布尔上下文(WHERE / HAVING / ON)允许裸布尔谓词(#21):
|
|
3006
|
+
// WHERE 1 / WHERE col / WHERE NULL / WHERE 'abc' / WHERE NULL AND b = 1
|
|
3007
|
+
// 左侧表达式直接作为布尔值返回,由求值层按 MySQL 三值逻辑判真。
|
|
3008
|
+
if (this.barePredicateDepth > 0) return left;
|
|
3009
|
+
|
|
2900
3010
|
throw new Error(`Expected comparison operator, got '${t2.value}'`);
|
|
2901
3011
|
}
|
|
2902
3012
|
}
|
|
@@ -3006,6 +3116,44 @@ function numericIfNumeric(v) {
|
|
|
3006
3116
|
return Number.isFinite(n) ? n : null;
|
|
3007
3117
|
}
|
|
3008
3118
|
|
|
3119
|
+
/**
|
|
3120
|
+
* MySQL 数字上下文里的字符串→数字强转:数字与字符串比较时,
|
|
3121
|
+
* 字符串按数值前缀解析,非数字/空串一律按 0(MySQL 语义)。
|
|
3122
|
+
* '' → 0、'a' → 0、'5' → 5、'5abc' → 5、'2024-01-01' → 2024
|
|
3123
|
+
* 与 strict 的 numericIfNumeric 不同:它拒绝非纯数字,这里宽进。
|
|
3124
|
+
*/
|
|
3125
|
+
function mysqlNum(v) {
|
|
3126
|
+
if (typeof v === 'number') return Number.isFinite(v) ? v : 0;
|
|
3127
|
+
if (typeof v === 'boolean') return v ? 1 : 0;
|
|
3128
|
+
if (typeof v === 'string') {
|
|
3129
|
+
const m = v.trim().match(/^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?/);
|
|
3130
|
+
if (!m) return 0;
|
|
3131
|
+
const n = Number(m[0]);
|
|
3132
|
+
return Number.isFinite(n) ? n : 0;
|
|
3133
|
+
}
|
|
3134
|
+
return 0;
|
|
3135
|
+
}
|
|
3136
|
+
|
|
3137
|
+
/**
|
|
3138
|
+
* BigInt 参与的整数算术(Issue #38)。
|
|
3139
|
+
*
|
|
3140
|
+
* BigInt 与 number 不能直接运算(TypeError),而 Number(BigInt) 会丢精度。
|
|
3141
|
+
* 两侧都是整数(BigInt 或整数值 number)且至少一侧是 BigInt 时,用 BigInt
|
|
3142
|
+
* 精确计算;否则返回 undefined,调用方退回 double 语义。
|
|
3143
|
+
*/
|
|
3144
|
+
function bigintExactArith(l, r, op) {
|
|
3145
|
+
if (typeof l !== 'bigint' && typeof r !== 'bigint') return undefined;
|
|
3146
|
+
const lb = typeof l === 'bigint' ? l : (typeof l === 'number' && Number.isInteger(l) ? BigInt(l) : null);
|
|
3147
|
+
const rb = typeof r === 'bigint' ? r : (typeof r === 'number' && Number.isInteger(r) ? BigInt(r) : null);
|
|
3148
|
+
if (lb === null || rb === null) return undefined;
|
|
3149
|
+
switch (op) {
|
|
3150
|
+
case '+': return lb + rb;
|
|
3151
|
+
case '-': return lb - rb;
|
|
3152
|
+
case '*': return lb * rb;
|
|
3153
|
+
default: return undefined;
|
|
3154
|
+
}
|
|
3155
|
+
}
|
|
3156
|
+
|
|
3009
3157
|
function resolveOperand(operand, row, ctx) {
|
|
3010
3158
|
if (operand === null || operand === undefined) return null;
|
|
3011
3159
|
if (typeof operand === 'string') return resolveOperand({ type: 'column', name: operand }, row, ctx);
|
|
@@ -3088,6 +3236,8 @@ function resolveOperand(operand, row, ctx) {
|
|
|
3088
3236
|
// 数字与字符串混合的算术会一路错下去。
|
|
3089
3237
|
// 判据:任一侧是 number,或字符串能被完整解析为数字 → 走数值加法。
|
|
3090
3238
|
case '+': {
|
|
3239
|
+
const bx = bigintExactArith(l, r, '+');
|
|
3240
|
+
if (bx !== undefined) return bx;
|
|
3091
3241
|
// MySQL / SQLite 语义(已用 sqlite3 实测核对):
|
|
3092
3242
|
// '5' + 5 = 10 '5' + '5' = 10 'abc' + 1 = 1 'a' + 'b' = 0
|
|
3093
3243
|
// 任一侧是「数值」时做算术加法,另一侧非数值按 0;
|
|
@@ -3102,12 +3252,16 @@ function resolveOperand(operand, row, ctx) {
|
|
|
3102
3252
|
return 0;
|
|
3103
3253
|
}
|
|
3104
3254
|
case '-': {
|
|
3255
|
+
const bx = bigintExactArith(l, r, '-');
|
|
3256
|
+
if (bx !== undefined) return bx;
|
|
3105
3257
|
// P1-21: 'abc'-1 → NaN,应返回 null
|
|
3106
3258
|
const ln = Number(l), rn = Number(r);
|
|
3107
3259
|
if (isNaN(ln) || isNaN(rn)) return null;
|
|
3108
3260
|
return ln - rn;
|
|
3109
3261
|
}
|
|
3110
3262
|
case '*': {
|
|
3263
|
+
const bx = bigintExactArith(l, r, '*');
|
|
3264
|
+
if (bx !== undefined) return bx;
|
|
3111
3265
|
const ln = Number(l), rn = Number(r);
|
|
3112
3266
|
if (isNaN(ln) || isNaN(rn)) return null;
|
|
3113
3267
|
return ln * rn;
|
|
@@ -3155,6 +3309,8 @@ function resolveOperand(operand, row, ctx) {
|
|
|
3155
3309
|
if (l === null || r === null || l === undefined || r === undefined) return null;
|
|
3156
3310
|
switch (operand.op) {
|
|
3157
3311
|
case '+': {
|
|
3312
|
+
const bx = bigintExactArith(l, r, '+');
|
|
3313
|
+
if (bx !== undefined) return bx;
|
|
3158
3314
|
if (typeof l === 'number' || typeof r === 'number') {
|
|
3159
3315
|
const ln = numericIfNumeric(l), rn = numericIfNumeric(r);
|
|
3160
3316
|
return (ln === null ? 0 : ln) + (rn === null ? 0 : rn);
|
|
@@ -3164,8 +3320,8 @@ function resolveOperand(operand, row, ctx) {
|
|
|
3164
3320
|
if (rn !== null) return rn;
|
|
3165
3321
|
return 0;
|
|
3166
3322
|
}
|
|
3167
|
-
case '-': { const ln = Number(l), rn = Number(r); return isNaN(ln) || isNaN(rn) ? null : ln - rn; }
|
|
3168
|
-
case '*': { const ln = Number(l), rn = Number(r); return isNaN(ln) || isNaN(rn) ? null : ln * rn; }
|
|
3323
|
+
case '-': { const bx = bigintExactArith(l, r, '-'); if (bx !== undefined) return bx; const ln = Number(l), rn = Number(r); return isNaN(ln) || isNaN(rn) ? null : ln - rn; }
|
|
3324
|
+
case '*': { const bx = bigintExactArith(l, r, '*'); if (bx !== undefined) return bx; const ln = Number(l), rn = Number(r); return isNaN(ln) || isNaN(rn) ? null : ln * rn; }
|
|
3169
3325
|
case '/': { const ln = Number(l), rn = Number(r); return isNaN(ln) || isNaN(rn) || rn === 0 ? null : ln / rn; }
|
|
3170
3326
|
case '%': { const ln = Number(l), rn = Number(r); return isNaN(ln) || isNaN(rn) || rn === 0 ? null : ln % rn; }
|
|
3171
3327
|
case 'DIV': return r === 0 ? null : Math.trunc(Number(l) / Number(r));
|
|
@@ -4525,6 +4681,37 @@ function normalizeCmpValue(a, other) {
|
|
|
4525
4681
|
return otherIsNumeric ? (a ? 1 : 0) : a;
|
|
4526
4682
|
}
|
|
4527
4683
|
|
|
4684
|
+
// MySQL 数值强转(#26 / #39):数字原样返回,布尔按 1/0,字符串按 MySQL 规则
|
|
4685
|
+
// 转换(空串和非数字字符串都算 0)。数字与字符串混合比较时必须走这里,
|
|
4686
|
+
// 否则 '10' >= '5' 会退化成字典序比较而得到错误结果。
|
|
4687
|
+
function mysqlNumeric(v) {
|
|
4688
|
+
if (typeof v === 'number') return v;
|
|
4689
|
+
if (typeof v === 'boolean') return v ? 1 : 0;
|
|
4690
|
+
if (typeof v === 'string') {
|
|
4691
|
+
const t = v.trim();
|
|
4692
|
+
if (t === '') return 0;
|
|
4693
|
+
const n = Number(t);
|
|
4694
|
+
return Number.isFinite(n) ? n : 0;
|
|
4695
|
+
}
|
|
4696
|
+
return NaN;
|
|
4697
|
+
}
|
|
4698
|
+
|
|
4699
|
+
/** 严格数值解析:只有真正能解析成有限数值时才返回 number,否则 null。
|
|
4700
|
+
* 用来判断字符串边界是否“看起来像数字”('9' / '0.01' / ' 100 ' 是,'abc' 不是)。
|
|
4701
|
+
* 与 mysqlNumeric 的区别:非数字字符串返回 null 而不是 0 —— 判断“要不要按数值比较”
|
|
4702
|
+
* 时不能用 0 冒充,否则 'abc' 会被当成 0。 */
|
|
4703
|
+
function strictNumeric(v) {
|
|
4704
|
+
if (typeof v === 'number') return Number.isFinite(v) ? v : null;
|
|
4705
|
+
if (typeof v === 'boolean') return v ? 1 : 0;
|
|
4706
|
+
if (typeof v === 'string') {
|
|
4707
|
+
const t = v.trim();
|
|
4708
|
+
if (t === '') return null;
|
|
4709
|
+
const n = Number(t);
|
|
4710
|
+
return Number.isFinite(n) ? n : null;
|
|
4711
|
+
}
|
|
4712
|
+
return null;
|
|
4713
|
+
}
|
|
4714
|
+
|
|
4528
4715
|
// MySQL 默认 collation(utf8mb4_general_ci)下字符串比较不区分大小写,
|
|
4529
4716
|
// 与 LIKE 保持一致。cs=true 表示显式 BINARY/COLLATE 强制大小写敏感。
|
|
4530
4717
|
function ciEq(a, b, cs) {
|
|
@@ -4596,16 +4783,23 @@ function evaluateExpr(expr, row, ctx) {
|
|
|
4596
4783
|
// 布尔与数值互通(MySQL: 1 = TRUE、0 = FALSE)
|
|
4597
4784
|
const nl = normalizeCmpValue(l, r);
|
|
4598
4785
|
const nr = normalizeCmpValue(r, l);
|
|
4786
|
+
// MySQL 类型强转:数字与字符串比较时,字符串按数字上下文转换。
|
|
4787
|
+
// 覆盖 0 = '' → 0 = 0 → 1、'a' = 0 → 0 = 0 → 1、'5' = 5 → 5 = 5 → 1(#26)。
|
|
4788
|
+
// 此前只对「数字 vs 纯数字字符串」生效,'' / 'a' 这类非纯数字字符串
|
|
4789
|
+
// 会退化成字符串比较而错判为不等。
|
|
4599
4790
|
const isNum = (v) => typeof v === 'number';
|
|
4600
|
-
|
|
4601
|
-
//
|
|
4602
|
-
//
|
|
4603
|
-
const
|
|
4604
|
-
|
|
4605
|
-
|
|
4606
|
-
|
|
4607
|
-
|
|
4608
|
-
|
|
4791
|
+
// MySQL 类型强转(#26):数字 vs 字符串统一走全局 mysqlNum
|
|
4792
|
+
// (按数值前缀解析,非数字/空串视为 0,对齐 MySQL 宽松转换)。
|
|
4793
|
+
// 覆盖 '5' = 5、0 = ''、'a' = 0、'09' = 9 等,避免退化成字典序比较。
|
|
4794
|
+
const isStr = (v) => typeof v === 'string';
|
|
4795
|
+
const numVsStr = (isNum(nl) && isStr(nr)) || (isStr(nl) && isNum(nr));
|
|
4796
|
+
if (numVsStr) {
|
|
4797
|
+
const an = mysqlNum(nl), bn = mysqlNum(nr);
|
|
4798
|
+
if (expr.op === '=') return an === bn;
|
|
4799
|
+
const fn = OPERATORS[expr.op];
|
|
4800
|
+
return fn(an, bn);
|
|
4801
|
+
}
|
|
4802
|
+
if (expr.op === '=') return ciEq(nl, nr, cs);
|
|
4609
4803
|
if (typeof nl === 'number' && typeof nr === 'number') { const fn = OPERATORS[expr.op]; return fn(nl, nr); }
|
|
4610
4804
|
const c = ciCompare(nl, nr, cs);
|
|
4611
4805
|
switch (expr.op) {
|
|
@@ -4689,21 +4883,17 @@ function evaluateExpr(expr, row, ctx) {
|
|
|
4689
4883
|
// 10 BETWEEN '5' AND '9' 得 0(应为 1)。
|
|
4690
4884
|
// 两边都是字符串时保持字符串比较(日期 '2024-01-02' 这类不能丢字典序)。
|
|
4691
4885
|
let inRange;
|
|
4692
|
-
const
|
|
4693
|
-
|
|
4694
|
-
|
|
4695
|
-
|
|
4696
|
-
|
|
4697
|
-
|
|
4698
|
-
|
|
4699
|
-
|
|
4700
|
-
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
const vn = typeof v === 'number' ? asNum(v) : null;
|
|
4704
|
-
const ln = asNum(lo), hn = asNum(hi);
|
|
4705
|
-
if (vn !== null && ln !== null && hn !== null) {
|
|
4706
|
-
inRange = vn >= ln && vn <= hn;
|
|
4886
|
+
const vn = strictNumeric(v), ln = strictNumeric(lo), hn = strictNumeric(hi);
|
|
4887
|
+
const vIsNum = typeof v === 'number' || typeof v === 'boolean';
|
|
4888
|
+
const boundIsNum = typeof lo === 'number' || typeof lo === 'boolean' ||
|
|
4889
|
+
typeof hi === 'number' || typeof hi === 'boolean';
|
|
4890
|
+
// 数值比较的判定与 case 'compare' 的 mixedNum 保持一致:
|
|
4891
|
+
// 1) 列值本身就是数值(或布尔),且两个边界都能解析为数值;
|
|
4892
|
+
// 2) 列值是可解析为数值的字符串,而任一边界本身就是数值(混合比较)。
|
|
4893
|
+
// 其余情况(纯字符串列 + 纯字符串边界,如日期 '2024-01-02')保持字典序。
|
|
4894
|
+
const numericRange = ln !== null && hn !== null && (vIsNum || (boundIsNum && vn !== null));
|
|
4895
|
+
if (numericRange) {
|
|
4896
|
+
inRange = vn !== null && vn >= ln && vn <= hn;
|
|
4707
4897
|
} else {
|
|
4708
4898
|
inRange = String(v) >= String(lo) && String(v) <= String(hi);
|
|
4709
4899
|
}
|
|
@@ -4731,6 +4921,8 @@ function evaluateExpr(expr, row, ctx) {
|
|
|
4731
4921
|
if (l === null || r === null || l === undefined || r === undefined) return null;
|
|
4732
4922
|
switch (expr.op) {
|
|
4733
4923
|
case '+': {
|
|
4924
|
+
const bx = bigintExactArith(l, r, '+');
|
|
4925
|
+
if (bx !== undefined) return bx;
|
|
4734
4926
|
if (typeof l === 'number' || typeof r === 'number') {
|
|
4735
4927
|
const ln = numericIfNumeric(l), rn = numericIfNumeric(r);
|
|
4736
4928
|
return (ln === null ? 0 : ln) + (rn === null ? 0 : rn);
|
|
@@ -4740,8 +4932,8 @@ function evaluateExpr(expr, row, ctx) {
|
|
|
4740
4932
|
if (rn !== null) return rn;
|
|
4741
4933
|
return 0;
|
|
4742
4934
|
}
|
|
4743
|
-
case '-': { const ln = Number(l), rn = Number(r); return isNaN(ln) || isNaN(rn) ? null : ln - rn; }
|
|
4744
|
-
case '*': { const ln = Number(l), rn = Number(r); return isNaN(ln) || isNaN(rn) ? null : ln * rn; }
|
|
4935
|
+
case '-': { const bx = bigintExactArith(l, r, '-'); if (bx !== undefined) return bx; const ln = Number(l), rn = Number(r); return isNaN(ln) || isNaN(rn) ? null : ln - rn; }
|
|
4936
|
+
case '*': { const bx = bigintExactArith(l, r, '*'); if (bx !== undefined) return bx; const ln = Number(l), rn = Number(r); return isNaN(ln) || isNaN(rn) ? null : ln * rn; }
|
|
4745
4937
|
case '/': { const ln = Number(l), rn = Number(r); return isNaN(ln) || isNaN(rn) || rn === 0 ? null : ln / rn; }
|
|
4746
4938
|
case '%': { const ln = Number(l), rn = Number(r); return isNaN(ln) || isNaN(rn) || rn === 0 ? null : ln % rn; }
|
|
4747
4939
|
case 'DIV': return r === 0 ? null : Math.trunc(Number(l) / Number(r));
|
|
@@ -4753,7 +4945,14 @@ function evaluateExpr(expr, row, ctx) {
|
|
|
4753
4945
|
default: return null;
|
|
4754
4946
|
}
|
|
4755
4947
|
}
|
|
4756
|
-
default:
|
|
4948
|
+
default: {
|
|
4949
|
+
// 裸布尔谓词(WHERE 1 / WHERE col / WHERE NULL / WHERE 'abc',#21):
|
|
4950
|
+
// 解析层把左侧表达式直接作为布尔条件返回,这里按 MySQL 真值规则
|
|
4951
|
+
// 归一化为 true / false / UNKNOWN(null):NULL → null;0/''/false → false;
|
|
4952
|
+
// 其余(含非空字符串)→ true。此前一律返回 false 导致谓词被静默算错。
|
|
4953
|
+
const v = resolveOperand(expr, row, ctx);
|
|
4954
|
+
return sqlTruthy(v);
|
|
4955
|
+
}
|
|
4757
4956
|
}
|
|
4758
4957
|
}
|
|
4759
4958
|
|
|
@@ -5047,9 +5246,8 @@ class SQLExecutor {
|
|
|
5047
5246
|
}
|
|
5048
5247
|
case 'addForeign':
|
|
5049
5248
|
case 'dropForeign':
|
|
5050
|
-
//
|
|
5051
|
-
//
|
|
5052
|
-
// 不如明确报错。要支持得先做引用完整性检查。
|
|
5249
|
+
// ALTER TABLE ... ADD/DROP FOREIGN KEY 仍未实现(CREATE TABLE 的表级/列级
|
|
5250
|
+
// FOREIGN KEY 已真正生效)。与其返回成功让人以为约束变更了,不如明确报错。
|
|
5053
5251
|
throw new Error(
|
|
5054
5252
|
`ALTER TABLE ... ${op.op === 'addForeign' ? 'ADD FOREIGN KEY' : 'DROP FOREIGN KEY'} is not supported: ` +
|
|
5055
5253
|
`this engine does not implement foreign key constraints.`
|
|
@@ -5953,6 +6151,14 @@ class SQLExecutor {
|
|
|
5953
6151
|
const type = colDef && colDef.type;
|
|
5954
6152
|
if (!type) return val;
|
|
5955
6153
|
if (type === 'integer' || type === 'int' || type === 'bigint' || type === 'smallint' || type === 'tinyint') {
|
|
6154
|
+
// BigInt 必须原样保留:Number(val) 会在 >2^53 时丢精度,
|
|
6155
|
+
// 下推成索引后与行里的 BigInt 严格比较永远不相等(Issue #38)。
|
|
6156
|
+
if (typeof val === 'bigint') return val;
|
|
6157
|
+
// 十进制字符串(参数内联后常见)同样要精确解析,不能用 Number()
|
|
6158
|
+
if (typeof val === 'string' && /^[+-]?\d+$/.test(val.trim())) {
|
|
6159
|
+
const b = BigInt(val.trim());
|
|
6160
|
+
return (b >= BigInt(Number.MIN_SAFE_INTEGER) && b <= BigInt(Number.MAX_SAFE_INTEGER)) ? Number(b) : b;
|
|
6161
|
+
}
|
|
5956
6162
|
const n = Number(val);
|
|
5957
6163
|
return Number.isNaN(n) ? val : n;
|
|
5958
6164
|
}
|
|
@@ -7890,6 +8096,7 @@ function escapeId(name) {
|
|
|
7890
8096
|
|
|
7891
8097
|
function escapeValue(value) {
|
|
7892
8098
|
if (value === null || value === undefined) return 'NULL';
|
|
8099
|
+
if (typeof value === 'bigint') return value.toString(); // Issue #38:精确的整数字面量
|
|
7893
8100
|
if (typeof value === 'number') return Number.isFinite(value) ? String(value) : 'NULL';
|
|
7894
8101
|
if (typeof value === 'boolean') return value ? 'true' : 'false';
|
|
7895
8102
|
if (value instanceof Date) {
|