jsql-neo 6.6.4 → 6.6.5

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 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.6.4** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
6
+ > **v6.6.5** — 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
  ![Engines](https://img.shields.io/badge/engines-Native%20%7C%20WASM%20%7C%20Pure%20JS-7ee787)
9
9
  ![MySQL](https://img.shields.io/badge/protocol-MySQL%20compatible-1f6feb)
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * JSQL-NEO v6.6.4 — Rust-Powered Embedded Database (WASM + HTTP)
2
+ * JSQL-NEO v6.6.5 — Rust-Powered Embedded Database (WASM + HTTP)
3
3
  *
4
4
  * @example
5
5
  * const jsql = require('jsql-neo');
package/lib/sql.js CHANGED
@@ -179,30 +179,35 @@ function nameOfToken(t) {
179
179
  * 但此前把 '$' 开头的路径当成字面键名去取(v['$.name']),永远取不到 ——
180
180
  * 所以抽出来共用,避免两处实现漂移。
181
181
  */
182
- function jsonPathGet(data, path) {
182
+ function jsonPathGet(data, path, notFound) {
183
+ // notFound 哨兵:调用方需要区分「路径不存在」与「该路径的值恰好是 JSON null」时
184
+ // 传入一个私有对象;默认仍返回 null,保持 JSON_EXTRACT 等既有行为不变。
185
+ const miss = arguments.length >= 3 ? notFound : null;
183
186
  const p = path == null ? '' : String(path);
184
187
  if (p === '$' || p === '') return data;
185
- const m = p.match(/^\$\.?([^.\[]+)/);
186
- if (!m) return null;
188
+ // 支持 $.a.b、$[1]、$.a[0]。此前 /^\$\.?([^.\[]+)/ 要求 $ 后必须紧跟
189
+ // 非 '[' 的字符,导致纯数组索引 $[1] 被直接判为找不到。
190
+ const m = p.match(/^\$(?:\.|\[|$)/);
191
+ if (!m) return miss;
187
192
  let current = data;
188
193
  for (const seg of p.replace(/^\$\.?/, '').split('.')) {
189
- if (current === null || current === undefined) return null;
194
+ if (current === null || current === undefined) return miss;
190
195
  const arrMatch = seg.match(/^([^\[]*)\[(\d+)\]$/);
191
196
  if (arrMatch) {
192
197
  const key = arrMatch[1];
193
198
  const idx = parseInt(arrMatch[2], 10);
194
199
  if (key) {
195
- if (typeof current !== 'object' || current === null) return null;
200
+ if (typeof current !== 'object' || current === null) return miss;
196
201
  current = current[key];
197
202
  }
198
- if (!Array.isArray(current)) return null;
203
+ if (!Array.isArray(current)) return miss;
199
204
  current = current[idx];
200
205
  } else {
201
- if (typeof current !== 'object' || current === null) return null;
206
+ if (typeof current !== 'object' || current === null) return miss;
202
207
  current = current[seg];
203
208
  }
204
209
  }
205
- return current === undefined ? null : current;
210
+ return current === undefined ? miss : current;
206
211
  }
207
212
 
208
213
  /**
@@ -3356,6 +3361,11 @@ function numericIfNumeric(v) {
3356
3361
  */
3357
3362
  function mysqlNum(v) {
3358
3363
  if (typeof v === 'number') return Number.isFinite(v) ? v : 0;
3364
+ // BigInt 是 issue #38 用来保 >2^53 整数精度的内部表示。此前没有这个分支,
3365
+ // 会掉到最后的 `return 0` —— 于是 SELECT -9223372036854775807 静默变成 0。
3366
+ // 这里转成 number 会让超出 2^53 的值丢精度,但至少不再是「悄悄变 0」;
3367
+ // 需要精确保留的一元负号路径在 case 'negate' 里单独短路。
3368
+ if (typeof v === 'bigint') return Number(v);
3359
3369
  if (typeof v === 'boolean') return v ? 1 : 0;
3360
3370
  if (typeof v === 'string') {
3361
3371
  const m = v.trim().match(/^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?/);
@@ -3607,6 +3617,10 @@ function resolveOperand(operand, row, ctx) {
3607
3617
  case 'negate': {
3608
3618
  const v = resolveOperand(operand.operand, row, ctx);
3609
3619
  if (v === null || v === undefined) return null;
3620
+ // 保精度:大整数是 BigInt,直接取负仍然是 BigInt,交给
3621
+ // finalizeResultBigInts 转成字符串输出,与正数路径一致。
3622
+ // 走 mysqlNum 会先转成 number,把 >2^53 的值弄丢精度。
3623
+ if (typeof v === 'bigint') return operand.positive ? v : -v;
3610
3624
  const n = mysqlNum(v);
3611
3625
  return operand.positive ? n : -n;
3612
3626
  }
@@ -3990,6 +4004,20 @@ function pgAge(from, to) {
3990
4004
  function finiteOrNull(x) {
3991
4005
  return (typeof x === 'number' && !Number.isFinite(x)) ? null : x;
3992
4006
  }
4007
+
4008
+ /** 整数语义、必须用 BigInt 精确算的数学函数(见 applyScalarFunction)。 */
4009
+ const INTEGER_MATH_FNS = new Set([
4010
+ 'ABS', 'SIGN', 'FLOOR', 'CEIL', 'CEILING', 'ROUND', 'TRUNCATE', 'MOD',
4011
+ 'POWER', 'POW',
4012
+ ]);
4013
+
4014
+ /** 入参需要 number(不能是 BigInt)的数学标量函数,见 applyScalarFunction。 */
4015
+ const MATH_SCALAR_FNS = new Set([
4016
+ 'ABS', 'SIGN', 'SQRT', 'POWER', 'POW', 'FLOOR', 'CEIL', 'CEILING', 'MOD',
4017
+ 'ROUND', 'TRUNCATE', 'LOG', 'LOG10', 'LOG2', 'LN', 'EXP',
4018
+ 'SIN', 'COS', 'TAN', 'ASIN', 'ACOS', 'ATAN', 'ATAN2',
4019
+ 'DEGREES', 'RADIANS', 'COT',
4020
+ ]);
3993
4021
  function applyScalarFunction(fnNode, row, ctx) {
3994
4022
  const name = (fnNode.name || '').toUpperCase();
3995
4023
  const rawArgs = fnNode.args || [];
@@ -3997,6 +4025,41 @@ function applyScalarFunction(fnNode, row, ctx) {
3997
4025
  const args = rawArgs.map(a => (a && a.type === 'interval')
3998
4026
  ? { __interval: true, value: a.value, unit: a.unit }
3999
4027
  : resolveOperand(a, row, ctx));
4028
+
4029
+ // >2^53 的整数在 jsql 内部是 BigInt(issue #38)。Math.* 不接受 BigInt,
4030
+ // 直接传会抛 "Cannot convert a BigInt value to a number";一律降级成
4031
+ // number 又会把 MOD(9007199254740993, 2) 算成 0(正确是 1)—— 静默错值
4032
+ // 比抛错更糟。所以分两类:
4033
+ // a) 整数语义的函数走 BigInt 精确路径,保留精度;
4034
+ // b) 真正的浮点运算(SQRT/LOG/三角…)降级为 number,MySQL 下本就返回 DOUBLE。
4035
+ if (args.length && args.some(a => typeof a === 'bigint') && INTEGER_MATH_FNS.has(name)) {
4036
+ const v = args[0];
4037
+ if (typeof v === 'bigint') {
4038
+ switch (name) {
4039
+ case 'ABS': return v < 0n ? -v : v;
4040
+ case 'SIGN': return v > 0n ? 1 : (v < 0n ? -1 : 0);
4041
+ case 'FLOOR': case 'CEIL': case 'CEILING': case 'ROUND': case 'TRUNCATE':
4042
+ return v; // 整数本身就是它的 floor/ceil/round
4043
+ case 'MOD': {
4044
+ const d = args[1];
4045
+ if (d == null || d === 0 || d === 0n) return null;
4046
+ return v % (typeof d === 'bigint' ? d : BigInt(Math.trunc(Number(d))));
4047
+ }
4048
+ case 'POWER': case 'POW': {
4049
+ const e = args[1];
4050
+ if (typeof e === 'bigint' || (typeof e === 'number' && Number.isInteger(e) && e >= 0)) {
4051
+ return v ** BigInt(e);
4052
+ }
4053
+ break; // 非整数指数 → 落到下面的浮点路径
4054
+ }
4055
+ }
4056
+ }
4057
+ }
4058
+ if (MATH_SCALAR_FNS.has(name)) {
4059
+ for (let i = 0; i < args.length; i++) {
4060
+ if (typeof args[i] === 'bigint') args[i] = Number(args[i]);
4061
+ }
4062
+ }
4000
4063
  const session = ctx && ctx.session;
4001
4064
  if (ctx && ctx.functions && Object.prototype.hasOwnProperty.call(ctx.functions, name)) {
4002
4065
  return ctx.functions[name].apply(null, args);
@@ -4880,10 +4943,14 @@ function applyScalarFunction(fnNode, row, ctx) {
4880
4943
  if (typeof json === 'string') {
4881
4944
  try { data = JSON.parse(json); } catch (e) { return null; }
4882
4945
  }
4883
- const searchStr = String(search).toLowerCase();
4946
+ // MySQL 的 search_str 是 **LIKE 模式**(% 任意串、_ 单字符,\ 转义),
4947
+ // 且 JSON_SEARCH 大小写敏感(#105)。旧实现用 String.includes +
4948
+ // toLowerCase:既不支持通配符,又把 'hello' 错配到 'Hello'。
4949
+ // 复用 likeMatch(ci=false),与 SQL 的 LIKE 语义保持一致。
4950
+ const searchStr = String(search);
4884
4951
  const results = [];
4885
4952
  function searchObj(obj, currentPath) {
4886
- if (typeof obj === 'string' && obj.toLowerCase().includes(searchStr)) {
4953
+ if (typeof obj === 'string' && likeMatch(obj, searchStr, false) === true) {
4887
4954
  results.push(currentPath);
4888
4955
  } else if (Array.isArray(obj)) {
4889
4956
  obj.forEach((item, i) => searchObj(item, currentPath + '[' + i + ']'));
@@ -4899,15 +4966,38 @@ function applyScalarFunction(fnNode, row, ctx) {
4899
4966
  return results;
4900
4967
  }
4901
4968
  case 'JSON_CONTAINS': {
4902
- // JSON_CONTAINS(json, value) → 1 or 0
4969
+ // MySQL: JSON_CONTAINS(target, candidate[, path]) → 1 / 0
4970
+ //
4971
+ // 旧实现是 `JSON.stringify(data).includes(JSON.stringify(val))`,
4972
+ // 有两个错(#105):
4973
+ // 1. candidate 是 JSON 文本,从未被解析 —— '2' 一直是字符串 "2",
4974
+ // 所以 '[1,2,3]' 里找不到 '"2"';只有数值字面量 2 碰巧能命中。
4975
+ // 2. 可选的第三个 path 参数被完全忽略。
4976
+ // 同文件里本来就有正确的 jsonContainsValue()(数组/对象/标量递归),
4977
+ // 只是这里没用。现在解析 candidate、用 jsonPathGet 应用 path,再委托它。
4903
4978
  const json = args[0];
4904
4979
  const val = args[1];
4905
- if (json == null) return null;
4980
+ const path = args[2];
4981
+ if (json == null || val === undefined) return null;
4906
4982
  let data = json;
4907
4983
  if (typeof json === 'string') {
4908
4984
  try { data = JSON.parse(json); } catch (e) { return null; }
4909
4985
  }
4910
- return JSON.stringify(data).includes(JSON.stringify(val)) ? 1 : 0;
4986
+ if (path != null && String(path) !== '$' && String(path) !== '') {
4987
+ // 用哨兵区分「路径不存在」和「该路径的值恰好是 JSON null」。
4988
+ // jsonPathGet 默认把两者都返回 null;直接判 === null 会把合法的
4989
+ // JSON null 误判成找不到(JSON_CONTAINS('{"a":null}','null','$.a')
4990
+ // 会错返回 0)。
4991
+ const MISSING = {};
4992
+ const sliced = jsonPathGet(data, String(path), MISSING);
4993
+ if (sliced === MISSING) return 0;
4994
+ data = sliced;
4995
+ }
4996
+ let candidate = val;
4997
+ if (typeof val === 'string') {
4998
+ try { candidate = JSON.parse(val); } catch (e) { /* 非法 JSON 文本按字面字符串处理 */ }
4999
+ }
5000
+ return jsonContainsValue(data, candidate) ? 1 : 0;
4911
5001
  }
4912
5002
  default:
4913
5003
  return null;
@@ -4984,9 +5074,12 @@ function jsonContainsValue(container, target) {
4984
5074
  if (container === null || typeof container !== 'object' || Array.isArray(container)) return false;
4985
5075
  return Object.keys(target).every(k => Object.prototype.hasOwnProperty.call(container, k) && jsonContainsValue(container[k], target[k]));
4986
5076
  }
4987
- if (Array.isArray(container)) return container.some(c => c === target || String(c) === String(target));
5077
+ // JSON 包含是**类型敏感**的:数字 2 与字符串 "2" 是不同值(MySQL / PG
5078
+ // 都如此)。此前用 String(c) === String(target) 做宽松比较,导致
5079
+ // JSON_CONTAINS('[1,2,3]', '"2"') 错返回 1。PG 的 @> 同样受益于此修正。
5080
+ if (Array.isArray(container)) return container.some(c => c === target);
4988
5081
  if (container !== null && typeof container === 'object') return false;
4989
- return container === target || String(container) === String(target);
5082
+ return container === target;
4990
5083
  }
4991
5084
 
4992
5085
  /** JSON 键存在(PG 的 ? / ?| / ?&)。keys 为单个键名或 '{a,b}' 形式的键列表。 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsql-neo",
3
- "version": "6.6.4",
3
+ "version": "6.6.5",
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/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",
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/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/bigint-math.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/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",
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/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/bigint-math.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",
@@ -0,0 +1,63 @@
1
+ /* ---- BigInt 在数学标量函数与一元负号里的精度回归 ---- */
2
+ /*
3
+ * 起点:SELECT -9223372036854775807 静默返回 0。
4
+ * 根因:mysqlNum() 没有 bigint 分支,掉到最后的 `return 0`;negate 又
5
+ * 把所有入参都过一遍 mysqlNum。修完后顺带发现 Math.* 不接受 BigInt
6
+ * (ABS/SQRT/POWER/FLOOR/CEIL/MOD 抛 TypeError),以及「一律降级 number」
7
+ * 会把 MOD(9007199254740993, 2) 算成 0 这种更糟的静默错值。
8
+ *
9
+ * 现在分两类:整数语义函数走 BigInt 精确路径,浮点函数降级为 DOUBLE。
10
+ */
11
+ const path = require('path');
12
+ const ROOT = path.join(__dirname, '..');
13
+ const { Database, executeSQL } = require(path.join(ROOT, 'index.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, (k, v) => typeof v === 'bigint' ? v.toString() + 'n' : v)); }
19
+ }
20
+
21
+ (async () => {
22
+ const db = new Database(':memory:', { autoSave: false });
23
+ const q = async (sql) => {
24
+ const r = await executeSQL(db, sql, { safety: false });
25
+ return r.rows[0][0];
26
+ };
27
+ const BIG = '9223372036854775807'; // int64 max
28
+ const ODD = '9007199254740993'; // 2^53 + 1,float64 无法精确表示
29
+
30
+ // ---------- 一元负号保精度 ----------
31
+ ok('正的大整数仍精确', await q(`SELECT ${BIG}`) === BIG, null);
32
+ ok('负的大整数精确取负(此前静默变 0)', await q(`SELECT -${BIG}`) === '-' + BIG, null);
33
+ ok('int64 min 取负', await q('SELECT -9223372036854775808') === '-9223372036854775808', null);
34
+ ok('普通负数仍是 number', await q('SELECT -5') === -5, null);
35
+ ok('普通负小数仍是 number', await q('SELECT -1.5') === -1.5, null);
36
+
37
+ // ---------- 整数语义的数学函数:BigInt 精确路径 ----------
38
+ ok('ABS 大负数精确', await q(`SELECT ABS(-${BIG})`) === BIG, null);
39
+ ok('ABS 普通负数', await q('SELECT ABS(-5)') === 5, null);
40
+ ok('SIGN 大正数', await q(`SELECT SIGN(${BIG})`) === 1, null);
41
+ ok('SIGN 大负数', await q(`SELECT SIGN(-${BIG})`) === -1, null);
42
+ ok(`MOD(${ODD}, 2) = 1(降级 number 会算成 0)`,
43
+ String(await q(`SELECT MOD(${ODD}, 2)`)) === '1', null);
44
+ ok('MOD 被自身整除 = 0', String(await q(`SELECT MOD(${ODD}, ${ODD})`)) === '0', null);
45
+ ok('FLOOR 大整数不变', String(await q(`SELECT FLOOR(${ODD})`)) === ODD, null);
46
+ ok('CEIL 大整数不变', String(await q(`SELECT CEIL(${ODD})`)) === ODD, null);
47
+ ok('POWER 大整数指数 1 精确', String(await q(`SELECT POWER(${ODD}, 1)`)) === ODD, null);
48
+ ok('MOD 除零仍 NULL', await q('SELECT MOD(5, 0)') === null, null);
49
+
50
+ // ---------- 浮点运算:降级 DOUBLE,不抛错也不静默给 0 ----------
51
+ const sq = await q(`SELECT SQRT(${ODD})`);
52
+ ok('SQRT 大整数返回有限数值', typeof sq === 'number' && Number.isFinite(sq) && sq > 0, sq);
53
+ const lg = await q(`SELECT LOG(${ODD})`);
54
+ ok('LOG 大整数不再抛 TypeError', lg === null || (typeof lg === 'number' && Number.isFinite(lg)), lg);
55
+ ok('#65 SQRT(-1) 仍返回 NULL', await q('SELECT SQRT(-1)') === null, null);
56
+ ok('#65 POWER(-2,0.5) 仍返回 NULL', await q('SELECT POWER(-2,0.5)') === null, null);
57
+
58
+ console.log(`\n${pass} passed, ${fail} failed`);
59
+ process.exit(fail === 0 ? 0 : 1);
60
+ })().catch(e => {
61
+ console.log('[FAIL] unexpected exception: ' + e.stack.split('\n').slice(0, 3).join('\n'));
62
+ process.exit(1);
63
+ });
@@ -0,0 +1,87 @@
1
+ /* ---- issue #105 + JSON 包含/搜索的语义回归 ---- */
2
+ /*
3
+ * #105:JSON_CONTAINS 用字符串 includes 实现(candidate 不解析、path 被忽略),
4
+ * JSON_SEARCH 用 toLowerCase + includes(大小写不敏感、不支持 LIKE 通配符)。
5
+ *
6
+ * 顺带钉死两个差分发现的 bug:
7
+ * - jsonContainsValue 用 String() 宽松比较,字符串 "2" 能命中数字 2
8
+ * - JSON_SEARCH 不认 % / _ 通配符
9
+ * 参考语义:MySQL 8.0 JSON_CONTAINS / JSON_SEARCH(大小写敏感、LIKE 模式)。
10
+ */
11
+ const path = require('path');
12
+ const ROOT = path.join(__dirname, '..');
13
+ const { Database, executeSQL } = require(path.join(ROOT, 'index.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
+ (async () => {
22
+ const db = new Database(':memory:', { autoSave: false });
23
+ const q = async (sql) => {
24
+ const r = await executeSQL(db, sql, { safety: false });
25
+ return r.rows[0][0];
26
+ };
27
+
28
+ // ---------- #105 JSON_CONTAINS ----------
29
+ ok('#105 字符串候选命中数组元素',
30
+ await q("SELECT JSON_CONTAINS('[1,2,3]', '2')") === 1, null);
31
+ ok('#105 第三个 path 参数生效',
32
+ await q("SELECT JSON_CONTAINS('{\"a\":1}', '1', '$.a')") === 1, null);
33
+ ok('#105 嵌套 path',
34
+ await q("SELECT JSON_CONTAINS('{\"a\":{\"b\":1}}', '1', '$.a.b')") === 1, null);
35
+ ok('数值字面量候选仍命中',
36
+ await q("SELECT JSON_CONTAINS('[1,2,3]', 2)") === 1, null);
37
+ ok('path 不存在返回 0',
38
+ await q("SELECT JSON_CONTAINS('{\"a\":1}', '1', '$.b')") === 0, null);
39
+ ok('对象包含(子集)',
40
+ await q("SELECT JSON_CONTAINS('{\"a\":1,\"b\":2}', '{\"a\":1}')") === 1, null);
41
+ ok('对象值不匹配返回 0',
42
+ await q("SELECT JSON_CONTAINS('{\"a\":1}', '{\"a\":2}')") === 0, null);
43
+ ok('数组子集命中',
44
+ await q("SELECT JSON_CONTAINS('[1,2,3]', '[2,3]')") === 1, null);
45
+ ok('数组不含该元素返回 0',
46
+ await q("SELECT JSON_CONTAINS('[1,2,3]', '9')") === 0, null);
47
+
48
+ // ---------- 类型敏感(差分发现) ----------
49
+ ok('字符串 "2" 不应命中数字 2',
50
+ await q("SELECT JSON_CONTAINS('[1,2,3]', '\"2\"')") === 0, null);
51
+ ok('字符串元素只被字符串候选命中',
52
+ await q("SELECT JSON_CONTAINS('[\"2\"]', '\"2\"')") === 1, null);
53
+ ok('数字 2 不应命中字符串元素',
54
+ await q("SELECT JSON_CONTAINS('[\"2\"]', '2')") === 0, null);
55
+
56
+ // ---------- JSON path 解析器(差分发现) ----------
57
+ ok('JSON_EXTRACT 支持纯数组索引 $[1]',
58
+ await q("SELECT JSON_EXTRACT('[1,2,3]', '$[1]')") === 2, null);
59
+ ok('JSON_CONTAINS 的 path 值是合法 JSON null',
60
+ await q("SELECT JSON_CONTAINS('{\"a\":null}', 'null', '$.a')") === 1, null);
61
+ ok('JSON_CONTAINS 支持数组索引 path',
62
+ await q("SELECT JSON_CONTAINS('{\"a\":[1,2]}', '2', '$.a[1]')") === 1, null);
63
+ ok('JSON_EXTRACT 找不到路径仍返回 NULL',
64
+ await q("SELECT JSON_EXTRACT('{\"a\":1}', '$.b')") === null, null);
65
+
66
+ // ---------- #105 JSON_SEARCH 大小写敏感 ----------
67
+ ok('#105 JSON_SEARCH 大小写不同返回 NULL',
68
+ await q("SELECT JSON_SEARCH('[\"Hello\"]', 'one', 'hello')") === null, null);
69
+ ok('JSON_SEARCH 大小写相同命中',
70
+ await q("SELECT JSON_SEARCH('[\"Hello\"]', 'one', 'Hello')") === '$[0]', null);
71
+
72
+ // ---------- LIKE 通配符(差分发现) ----------
73
+ ok('JSON_SEARCH 支持 % 通配符',
74
+ await q("SELECT JSON_SEARCH('[\"hello world\"]', 'one', 'hello%')") === '$[0]', null);
75
+ ok('JSON_SEARCH 支持 _ 通配符',
76
+ await q("SELECT JSON_SEARCH('[\"Hello\"]', 'one', 'H_llo')") === '$[0]', null);
77
+ ok('JSON_SEARCH 的 % 不是字面量',
78
+ await q("SELECT JSON_SEARCH('[\"hello%world\"]', 'one', 'hello%')") === '$[0]', null);
79
+ const all = await q("SELECT JSON_SEARCH('{\"a\":\"x\",\"b\":\"x\"}', 'all', 'x')");
80
+ ok('all 模式拿到两个路径', Array.isArray(all) && all.length === 2, all);
81
+
82
+ console.log(`\n${pass} passed, ${fail} failed`);
83
+ process.exit(fail === 0 ? 0 : 1);
84
+ })().catch(e => {
85
+ console.log('[FAIL] unexpected exception: ' + e.stack.split('\n').slice(0, 3).join('\n'));
86
+ process.exit(1);
87
+ });