jsql-neo 6.6.5 → 6.6.6

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.5** — 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.6** — 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)
@@ -196,6 +196,20 @@ To check which native addon this installation is actually using:
196
196
  `UNVERIFIED` / `UNREADABLE`.
197
197
  - `jsql native refresh` — re-fetches a checksum-verified addon
198
198
 
199
+ ## SQL functions
200
+
201
+ Full reference: [doc/sql-reference.md](doc/sql-reference.md).
202
+
203
+ | Group | Functions |
204
+ | --- | --- |
205
+ | JSON | `JSON_EXTRACT`, `JSON_CONTAINS` (path + type-sensitive), `JSON_SEARCH` (case-sensitive, LIKE wildcards), `JSON_SET`, `JSON_REMOVE`, `JSON_MERGE_PATCH`, … |
206
+ | Date / time | UTC wall-clock semantics; `TO_DAYS`, `FROM_DAYS`, `TO_SECONDS`, `TIME_TO_SEC`, `SEC_TO_TIME`, `DATE_ADD`, `DATE_FORMAT`, `TIMESTAMPDIFF`, … |
207
+ | Base / utility | `BIN`, `OCT`, `HEX`, `UNHEX`, `CONV`, `BIT_LENGTH`, `CRC32`, `QUOTE`, `FIND_IN_SET`, `INSERT`, `FORMAT`, `INET_ATON` / `INET_NTOA`, `MAKE_SET`, `EXPORT_SET`, `SOUNDEX` |
208
+ | Exact integers | Values above `2^53` stay exact via BigInt; integer-math functions use exact paths, floating-point ones return DOUBLE |
209
+
210
+ `JSON_TYPE` / `JSON_DEPTH` follow **this project's** documented semantics
211
+ (lowercase type names, flat object depth 1), not MySQL's. See the SQL reference.
212
+
199
213
  ## ✨ XQL 集成
200
214
 
201
215
  用 [XQL](https://github.com/vexify-org/XQL) 查询 JSQL 表:把 SQL 直接接到文档上,
@@ -243,6 +257,10 @@ report the reason instead of failing silently. Loopback traffic (the
243
257
  `127.0.0.1` links between `jsql serve` and the client / Web UI) is not
244
258
  external access and is unaffected.
245
259
 
260
+ `jsql native status|refresh|verify` is **entirely local** — it reads
261
+ `native/.installed.json` and hashes the on-disk addon. `verify --json` is
262
+ suitable for CI gates. See [doc/cli.md](doc/cli.md).
263
+
246
264
  ## 📖 Documentation
247
265
 
248
266
  Detailed docs live in `doc/` — click to read.
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * JSQL-NEO v6.6.5 — Rust-Powered Embedded Database (WASM + HTTP)
2
+ * JSQL-NEO v6.6.6 — Rust-Powered Embedded Database (WASM + HTTP)
3
3
  *
4
4
  * @example
5
5
  * const jsql = require('jsql-neo');
@@ -4,6 +4,8 @@
4
4
  * Listens on ONE port and sniffs the first bytes of each connection to route
5
5
  * to the correct wire-protocol handler:
6
6
  *
7
+ * - MongoDB : OP_MSG / OP_QUERY opcode at offset 12 (checked first — its
8
+ * little-endian length can start with 0x00 and look like PG)
7
9
  * - PostgreSQL : first byte 0x00 (big-endian Int32 length, length < 2^24)
8
10
  * - MySQL : first byte 0x0a/0x0d (protocol handshake version / cap flag)
9
11
  * - Redis : anything else (ASCII command or RESP prefix)
package/lib/sql.js CHANGED
@@ -4321,6 +4321,45 @@ function applyScalarFunction(fnNode, row, ctx) {
4321
4321
  d.setUTCDate(doy);
4322
4322
  return fmtSqlDate(true, d);
4323
4323
  }
4324
+ // MySQL 的「天数/秒数 ↔ 日期/时间」互转。此前这五个函数完全没实现,
4325
+ // 调用时落到 switch 的 default 静默返回 null(不是报错,很容易被当成
4326
+ // 「数据里没有这个值」)。基准与 6.6.0 起的 UTC 墙上时间口径一致。
4327
+ case 'TO_DAYS': {
4328
+ const d = parseSqlDate(args[0]);
4329
+ return d ? Math.floor(d.getTime() / 86400000) + 719528 : null;
4330
+ }
4331
+ case 'FROM_DAYS': {
4332
+ if (args[0] == null) return null;
4333
+ const days = Number(args[0]);
4334
+ if (!Number.isFinite(days)) return null;
4335
+ return fmtSqlDate(true, new Date((days - 719528) * 86400000));
4336
+ }
4337
+ case 'TO_SECONDS': {
4338
+ const d = parseSqlDate(args[0]);
4339
+ if (!d) return null;
4340
+ const days = Math.floor(d.getTime() / 86400000) + 719528;
4341
+ const secOfDay = d.getUTCHours() * 3600 + d.getUTCMinutes() * 60 + d.getUTCSeconds();
4342
+ return days * 86400 + secOfDay;
4343
+ }
4344
+ case 'TIME_TO_SEC': {
4345
+ if (args[0] == null) return null;
4346
+ const m = String(args[0]).trim().match(/^(-?)(\d{1,3}):(\d{2})(?::(\d{2}))?$/);
4347
+ if (!m) return null;
4348
+ const sign = m[1] === '-' ? -1 : 1;
4349
+ return sign * (Number(m[2]) * 3600 + Number(m[3]) * 60 +
4350
+ (m[4] === undefined ? 0 : Number(m[4])));
4351
+ }
4352
+ case 'SEC_TO_TIME': {
4353
+ if (args[0] == null) return null;
4354
+ const n = Math.trunc(Number(args[0]));
4355
+ if (!Number.isFinite(n)) return null;
4356
+ const sign = n < 0 ? '-' : '';
4357
+ let rest = Math.abs(n);
4358
+ const h = Math.floor(rest / 3600); rest -= h * 3600;
4359
+ const mi = Math.floor(rest / 60);
4360
+ const s = rest - mi * 60;
4361
+ return sign + String(h).padStart(2, '0') + ':' + pad2(mi) + ':' + pad2(s);
4362
+ }
4324
4363
  case 'PERIOD_DIFF': {
4325
4364
  const p1 = String(args[0] == null ? '' : args[0]);
4326
4365
  const p2 = String(args[1] == null ? '' : args[1]);
@@ -4346,6 +4385,140 @@ function applyScalarFunction(fnNode, row, ctx) {
4346
4385
  }
4347
4386
  return fmtSqlDate(false, nd);
4348
4387
  }
4388
+ // ── MySQL 进制 / 字符串 / IP 工具函数 ──
4389
+ // 这十三个此前全部落到 switch 的 default 静默返回 null —— 调用方很容易
4390
+ // 把「函数没实现」误读成「这个值不存在」。补齐常用的一批;
4391
+ // MAKE_SET / EXPORT_SET / SOUNDEX 仍未实现(未在文档中承诺)。
4392
+ case 'BIT_LENGTH': {
4393
+ if (args[0] == null) return null;
4394
+ return Buffer.byteLength(String(args[0]), 'utf8') * 8;
4395
+ }
4396
+ case 'BIN': case 'OCT': {
4397
+ if (args[0] == null) return null;
4398
+ const v = args[0];
4399
+ const n = typeof v === 'bigint' ? v : BigInt(Math.trunc(Number(v)));
4400
+ return (n < 0n ? BigInt.asUintN(64, n) : n).toString(name === 'BIN' ? 2 : 8);
4401
+ }
4402
+ case 'HEX': {
4403
+ if (args[0] == null) return null;
4404
+ const v = args[0];
4405
+ if (typeof v === 'number' || typeof v === 'bigint') {
4406
+ const n = typeof v === 'bigint' ? v : BigInt(Math.trunc(v));
4407
+ return (n < 0n ? BigInt.asUintN(64, n) : n).toString(16).toUpperCase();
4408
+ }
4409
+ return Buffer.from(String(v), 'utf8').toString('hex').toUpperCase();
4410
+ }
4411
+ case 'UNHEX': {
4412
+ if (args[0] == null) return null;
4413
+ const h = String(args[0]);
4414
+ if (h.length % 2 !== 0 || !/^[0-9a-fA-F]*$/.test(h)) return null;
4415
+ return Buffer.from(h, 'hex').toString('latin1');
4416
+ }
4417
+ case 'CONV': {
4418
+ if (args[0] == null || args[1] == null || args[2] == null) return null;
4419
+ const from = Math.trunc(Number(args[1])), to = Math.trunc(Number(args[2]));
4420
+ if (from < 2 || from > 36 || to < 2 || to > 36) return null;
4421
+ const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
4422
+ let n = 0n;
4423
+ for (const ch of String(args[0]).trim().toUpperCase()) {
4424
+ const d = digits.indexOf(ch);
4425
+ if (d < 0 || d >= from) return null;
4426
+ n = n * BigInt(from) + BigInt(d);
4427
+ }
4428
+ if (n === 0n) return '0';
4429
+ let out = '';
4430
+ while (n > 0n) { out = digits[Number(n % BigInt(to))] + out; n /= BigInt(to); }
4431
+ return out;
4432
+ }
4433
+ case 'QUOTE': {
4434
+ if (args[0] == null) return null;
4435
+ const s = String(args[0]).replace(/\\/g, '\\\\').replace(/\0/g, '\\0')
4436
+ .replace(/\n/g, '\\n').replace(/\r/g, '\\r')
4437
+ .replace(/'/g, "\\'").replace(/"/g, '\\"').replace(/\x1a/g, '\\Z');
4438
+ return "'" + s + "'";
4439
+ }
4440
+ case 'FIND_IN_SET': {
4441
+ if (args[0] == null || args[1] == null) return null;
4442
+ const i = String(args[1]).split(',').indexOf(String(args[0]));
4443
+ return i < 0 ? 0 : i + 1;
4444
+ }
4445
+ case 'INSERT': {
4446
+ const s = args[0], pos = args[1], len = args[2], ins = args[3];
4447
+ if (s == null || pos == null || len == null || ins == null) return null;
4448
+ const str = String(s), p = Math.trunc(Number(pos)), l = Math.trunc(Number(len));
4449
+ if (p < 1 || p > str.length + 1 || l < 0) return str;
4450
+ return str.slice(0, p - 1) + String(ins) + str.slice(p - 1 + l);
4451
+ }
4452
+ case 'INET_ATON': {
4453
+ if (args[0] == null) return null;
4454
+ const m = String(args[0]).match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
4455
+ if (!m) return null;
4456
+ const o = m.slice(1).map(Number);
4457
+ if (o.some(x => x > 255)) return null;
4458
+ return (((o[0] << 24) >>> 0) + (o[1] << 16) + (o[2] << 8) + o[3]) >>> 0;
4459
+ }
4460
+ case 'INET_NTOA': {
4461
+ if (args[0] == null) return null;
4462
+ const n = Number(args[0]) >>> 0;
4463
+ return [(n >>> 24) & 255, (n >>> 16) & 255, (n >>> 8) & 255, n & 255].join('.');
4464
+ }
4465
+ case 'FORMAT': {
4466
+ if (args[0] == null || args[1] == null) return null;
4467
+ const x = Number(args[0]), d = Math.trunc(Number(args[1]));
4468
+ if (!Number.isFinite(x) || !Number.isFinite(d) || d < 0 || d > 30) return null;
4469
+ const [ip, fp] = x.toFixed(d).split('.');
4470
+ const sign = ip.startsWith('-') ? '-' : '';
4471
+ const grouped = (sign ? ip.slice(1) : ip).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
4472
+ return sign + grouped + (fp ? '.' + fp : '');
4473
+ }
4474
+ case 'CRC32': {
4475
+ if (args[0] == null) return null;
4476
+ let crc = 0xFFFFFFFF;
4477
+ for (const b of Buffer.from(String(args[0]), 'utf8')) {
4478
+ crc ^= b;
4479
+ for (let k = 0; k < 8; k++) crc = (crc >>> 1) ^ (0xEDB88320 & -(crc & 1));
4480
+ }
4481
+ return (crc ^ 0xFFFFFFFF) >>> 0;
4482
+ }
4483
+ case 'MAKE_SET': {
4484
+ if (args[0] == null) return null;
4485
+ const bits = Math.trunc(Number(args[0]));
4486
+ if (!Number.isFinite(bits)) return '';
4487
+ const out = [];
4488
+ for (let i = 1; i < args.length; i++) {
4489
+ const v = args[i];
4490
+ if (v != null && (bits & (1 << (i - 1)))) out.push(String(v));
4491
+ }
4492
+ return out.join(',');
4493
+ }
4494
+ case 'EXPORT_SET': {
4495
+ const bits = args[0], on = args[1], off = args[2];
4496
+ if (bits == null || on == null || off == null) return null;
4497
+ const sep = args[3] == null ? ',' : String(args[3]);
4498
+ const n = args[4] == null ? 64 : Math.trunc(Number(args[4]));
4499
+ if (!Number.isFinite(n) || n < 0 || n > 64) return null;
4500
+ const b = BigInt(Math.trunc(Number(bits)));
4501
+ const out = [];
4502
+ for (let i = 0; i < n; i++) {
4503
+ out.push(((b >> BigInt(i)) & 1n) ? String(on) : String(off));
4504
+ }
4505
+ return out.join(sep);
4506
+ }
4507
+ case 'SOUNDEX': {
4508
+ if (args[0] == null) return null;
4509
+ const s = String(args[0]).toUpperCase().replace(/[^A-Z]/g, '');
4510
+ if (!s) return '';
4511
+ const MAP = { B: '1', F: '1', P: '1', V: '1', C: '2', G: '2', J: '2', K: '2', Q: '2', S: '2', X: '2', Z: '2', D: '3', T: '3', L: '4', M: '5', N: '5', R: '6' };
4512
+ let out = s[0];
4513
+ let prev = MAP[s[0]] || '';
4514
+ for (let i = 1; i < s.length && out.length < 4; i++) {
4515
+ const c = MAP[s[i]];
4516
+ if (!c) { prev = ''; continue; } // 元音 / H / W / Y 隔断相邻重复
4517
+ if (c !== prev) out += c;
4518
+ prev = c;
4519
+ }
4520
+ return (out + '000').slice(0, 4);
4521
+ }
4349
4522
  case 'LTRIM': return args[0] == null ? null : String(args[0]).replace(/^\s+/, '');
4350
4523
  case 'RTRIM': return args[0] == null ? null : String(args[0]).replace(/\s+$/, '');
4351
4524
  case 'ABS': return args[0] == null ? null : Math.abs(args[0]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsql-neo",
3
- "version": "6.6.5",
3
+ "version": "6.6.6",
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,13 @@
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 && node test/issue-105.test.js && node test/bigint-math.test.js",
53
+ "check:release": "node scripts/check-release-metadata.js",
54
+ "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/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js",
54
55
  "test:parser": "node test/sql-parser.test.js",
55
56
  "test:btree": "node test/btree.test.js",
56
57
  "test:native": "node test/native.test.js",
57
58
  "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 && node test/issue-105.test.js && node test/bigint-math.test.js",
59
+ "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/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js",
59
60
  "test:coverage": "c8 --all --include='lib/**' --exclude='lib/web_ui.js' --reporter=text --reporter=lcov node test/coverage.js",
60
61
  "test:orms": "node examples/orms/run-all.js",
61
62
  "test:dml": "node test/dml-constraints.test.js",
@@ -0,0 +1,58 @@
1
+ /* ---- MySQL 天数/秒数 ↔ 日期/时间 互转函数 ---- */
2
+ /*
3
+ * 这五个函数此前完全没实现,调用时落到 applyScalarFunction 的 default
4
+ * 静默返回 null —— 很容易被误读成「数据里没这个值」。
5
+ * 基准与 6.6.0 起的 UTC 墙上时间口径一致。
6
+ */
7
+ const path = require('path');
8
+ const ROOT = path.join(__dirname, '..');
9
+ const { Database, executeSQL } = require(path.join(ROOT, 'index.js'));
10
+
11
+ let pass = 0, fail = 0;
12
+ function ok(name, cond, got) {
13
+ if (cond) { pass++; console.log('[OK] ' + name); }
14
+ else { fail++; console.log('[FAIL] ' + name + ' -> ' + JSON.stringify(got)); }
15
+ }
16
+
17
+ (async () => {
18
+ const db = new Database(':memory:', { autoSave: false });
19
+ const q = async (sql) => (await executeSQL(db, sql, { safety: false })).rows[0][0];
20
+
21
+ // ---------- TO_DAYS / FROM_DAYS ----------
22
+ ok('TO_DAYS epoch = 719528', await q("SELECT TO_DAYS('1970-01-01')") === 719528, null);
23
+ ok('FROM_DAYS(719528) = 1970-01-01', await q('SELECT FROM_DAYS(719528)') === '1970-01-01', null);
24
+ for (const d of ['2024-05-06', '2024-02-29', '2000-01-01', '1999-12-31']) {
25
+ const v = await q(`SELECT FROM_DAYS(TO_DAYS('${d}'))`);
26
+ ok('TO_DAYS/FROM_DAYS 往返 ' + d, v === d, v);
27
+ }
28
+ ok('TO_DAYS 非法日期 → NULL', await q("SELECT TO_DAYS('bad')") === null, null);
29
+ ok('TO_DAYS(NULL) → NULL', await q('SELECT TO_DAYS(NULL)') === null, null);
30
+ ok('FROM_DAYS(NULL) → NULL', await q('SELECT FROM_DAYS(NULL)') === null, null);
31
+
32
+ // ---------- TO_SECONDS ----------
33
+ ok('TO_SECONDS epoch = 62167219200',
34
+ await q("SELECT TO_SECONDS('1970-01-01 00:00:00')") === 62167219200, null);
35
+ ok('TO_SECONDS 累加当日秒数',
36
+ await q("SELECT TO_SECONDS('1970-01-01 01:02:03')") === 62167219200 + 3723, null);
37
+ ok('TO_SECONDS 非法 → NULL', await q("SELECT TO_SECONDS('bad')") === null, null);
38
+
39
+ // ---------- TIME_TO_SEC / SEC_TO_TIME ----------
40
+ ok("TIME_TO_SEC('12:30:45') = 45045", await q("SELECT TIME_TO_SEC('12:30:45')") === 45045, null);
41
+ ok("TIME_TO_SEC('-12:30:45') = -45045", await q("SELECT TIME_TO_SEC('-12:30:45')") === -45045, null);
42
+ ok("TIME_TO_SEC('100:00:00') 支持超过 24h", await q("SELECT TIME_TO_SEC('100:00:00')") === 360000, null);
43
+ ok("TIME_TO_SEC('12:30') 省略秒", await q("SELECT TIME_TO_SEC('12:30')") === 45000, null);
44
+ ok('SEC_TO_TIME(45045)', await q('SELECT SEC_TO_TIME(45045)') === '12:30:45', null);
45
+ ok('SEC_TO_TIME(-45045) 带负号', await q('SELECT SEC_TO_TIME(-45045)') === '-12:30:45', null);
46
+ ok('SEC_TO_TIME(360000) 超过 24h', await q('SELECT SEC_TO_TIME(360000)') === '100:00:00', null);
47
+ ok('SEC_TO_TIME(0)', await q('SELECT SEC_TO_TIME(0)') === '00:00:00', null);
48
+ ok('TIME_TO_SEC / SEC_TO_TIME 往返',
49
+ await q("SELECT SEC_TO_TIME(TIME_TO_SEC('50:01:02'))") === '50:01:02', null);
50
+ ok('TIME_TO_SEC 非法 → NULL', await q("SELECT TIME_TO_SEC('bad')") === null, null);
51
+ ok('SEC_TO_TIME(NULL) → NULL', await q('SELECT SEC_TO_TIME(NULL)') === null, null);
52
+
53
+ console.log(`\n${pass} passed, ${fail} failed`);
54
+ process.exit(fail === 0 ? 0 : 1);
55
+ })().catch(e => {
56
+ console.log('[FAIL] unexpected exception: ' + e.stack.split('\n').slice(0, 3).join('\n'));
57
+ process.exit(1);
58
+ });
@@ -0,0 +1,70 @@
1
+ /* ---- MySQL 进制 / 字符串 / IP / 位图工具函数 ---- */
2
+ /*
3
+ * 差分探针发现十几个常用函数全部落到 applyScalarFunction 的 default
4
+ * **静默返回 null** —— 调用方很容易把「没实现」误读成「值不存在」。
5
+ * 本测试覆盖补齐后的语义与边界。
6
+ */
7
+ const path = require('path');
8
+ const ROOT = path.join(__dirname, '..');
9
+ const { Database, executeSQL } = require(path.join(ROOT, 'index.js'));
10
+
11
+ let pass = 0, fail = 0;
12
+ function ok(name, cond, got) {
13
+ if (cond) { pass++; console.log('[OK] ' + name); }
14
+ else { fail++; console.log('[FAIL] ' + name + ' -> ' + JSON.stringify(got)); }
15
+ }
16
+
17
+ (async () => {
18
+ const db = new Database(':memory:', { autoSave: false });
19
+ const q = async (sql) => (await executeSQL(db, sql, { safety: false })).rows[0][0];
20
+
21
+ // ---------- 进制 ----------
22
+ ok("BIT_LENGTH('abc') = 24", await q("SELECT BIT_LENGTH('abc')") === 24, null);
23
+ ok("BIT_LENGTH 中文按字节", await q("SELECT BIT_LENGTH('中文')") === 48, null);
24
+ ok('BIN(10)', await q('SELECT BIN(10)') === '1010', null);
25
+ ok('OCT(10)', await q('SELECT OCT(10)') === '12', null);
26
+ ok('HEX(255)', await q('SELECT HEX(255)') === 'FF', null);
27
+ ok("HEX('AB') 按字节", await q("SELECT HEX('AB')") === '4142', null);
28
+ ok("UNHEX('4142')", await q("SELECT UNHEX('4142')") === 'AB', null);
29
+ ok('HEX/UNHEX 往返', await q("SELECT UNHEX(HEX('hello'))") === 'hello', null);
30
+ ok('UNHEX 奇数长度 → NULL', await q("SELECT UNHEX('ABC')") === null, null);
31
+ ok('UNHEX 非十六进制 → NULL', await q("SELECT UNHEX('ZZ')") === null, null);
32
+ ok("CONV('A',16,2) = 1010", await q("SELECT CONV('A', 16, 2)") === '1010', null);
33
+ ok('CONV(1010,2,10) = 10', await q("SELECT CONV('1010', 2, 10)") === '10', null);
34
+ ok('CONV(0) = 0', await q("SELECT CONV('0', 10, 2)") === '0', null);
35
+ ok('CONV 非法数字 → NULL', await q("SELECT CONV('Z', 10, 2)") === null, null);
36
+ ok('CONV 非法进制 → NULL', await q("SELECT CONV('1', 1, 2)") === null, null);
37
+
38
+ // ---------- 字符串 ----------
39
+ ok("QUOTE('abc')", await q("SELECT QUOTE('abc')") === "'abc'", null);
40
+ ok('QUOTE NULL → NULL', await q('SELECT QUOTE(NULL)') === null, null);
41
+ ok("FIND_IN_SET('b','a,b,c') = 2", await q("SELECT FIND_IN_SET('b', 'a,b,c')") === 2, null);
42
+ ok('FIND_IN_SET 找不到 = 0', await q("SELECT FIND_IN_SET('z', 'a,b,c')") === 0, null);
43
+ ok('FIND_IN_SET 是精确匹配(不是子串)', await q("SELECT FIND_IN_SET('a', 'ab,cd')") === 0, null);
44
+ ok("INSERT('abcdef',2,3,'XY')", await q("SELECT INSERT('abcdef', 2, 3, 'XY')") === 'aXYef', null);
45
+ ok('INSERT 位置越界返回原串', await q("SELECT INSERT('abc', 9, 1, 'X')") === 'abc', null);
46
+ ok('FORMAT(1234.567,2)', await q('SELECT FORMAT(1234.567, 2)') === '1,234.57', null);
47
+ ok('FORMAT 负数', await q('SELECT FORMAT(-1234567.5, 1)') === '-1,234,567.5', null);
48
+ ok("CRC32('abc') 标准值", await q("SELECT CRC32('abc')") === 891568578, null);
49
+ ok('CRC32 空串', await q("SELECT CRC32('')") === 0, null);
50
+
51
+ // ---------- IP ----------
52
+ ok("INET_ATON('127.0.0.1')", await q("SELECT INET_ATON('127.0.0.1')") === 2130706433, null);
53
+ ok('INET_NTOA(2130706433)', await q('SELECT INET_NTOA(2130706433)') === '127.0.0.1', null);
54
+ ok('INET_ATON/NTOA 往返', await q("SELECT INET_NTOA(INET_ATON('192.168.1.10'))") === '192.168.1.10', null);
55
+ ok('INET_ATON 非法段 → NULL', await q("SELECT INET_ATON('999.1.1.1')") === null, null);
56
+
57
+ // ---------- 位图 / 语音 ----------
58
+ ok("MAKE_SET(5,'a','b','c') = a,c", await q("SELECT MAKE_SET(5, 'a', 'b', 'c')") === 'a,c', null);
59
+ ok('MAKE_SET(0) 空串', await q("SELECT MAKE_SET(0, 'a', 'b')") === '', null);
60
+ ok("EXPORT_SET(5,'Y','N',',',8)", await q("SELECT EXPORT_SET(5, 'Y', 'N', ',', 8)") === 'Y,N,Y,N,N,N,N,N', null);
61
+ ok("SOUNDEX('Hello') = H400", await q("SELECT SOUNDEX('Hello')") === 'H400', null);
62
+ ok('SOUNDEX 空串', await q("SELECT SOUNDEX('')") === '', null);
63
+ ok('SOUNDEX(NULL) → NULL', await q('SELECT SOUNDEX(NULL)') === null, null);
64
+
65
+ console.log(`\n${pass} passed, ${fail} failed`);
66
+ process.exit(fail === 0 ? 0 : 1);
67
+ })().catch(e => {
68
+ console.log('[FAIL] unexpected exception: ' + e.stack.split('\n').slice(0, 3).join('\n'));
69
+ process.exit(1);
70
+ });
@@ -0,0 +1,98 @@
1
+ /* ---- 发版元数据门禁:必须能拦住「漏写 CHANGELOG」等脱节 ---- */
2
+ /*
3
+ * 背景:6.6.0/6.6.1、6.6.3/6.6.4 都发生过 release commit 只 bump 版本标记、
4
+ * 没写 CHANGELOG 章节,却照样打 tag 发版。门禁把这件事变成硬失败。
5
+ *
6
+ * 全部用临时目录构造反例,绝不改仓库里的真实文件。
7
+ */
8
+ const fs = require('fs');
9
+ const os = require('os');
10
+ const path = require('path');
11
+ const ROOT = path.join(__dirname, '..');
12
+ const { checkReleaseMetadata } = require(path.join(ROOT, 'scripts', 'check-release-metadata.js'));
13
+
14
+ let pass = 0, fail = 0;
15
+ function ok(name, cond, got) {
16
+ if (cond) { pass++; console.log('[OK] ' + name); }
17
+ else { fail++; console.log('[FAIL] ' + name + ' -> ' + JSON.stringify(got)); }
18
+ }
19
+
20
+ const TMP = fs.mkdtempSync(path.join(os.tmpdir(), 'jsql-release-meta-'));
21
+ const SRC = ['package.json', 'README.md', 'index.js', 'CHANGELOG.md'];
22
+ function fixture(dir, mutate) {
23
+ const root = path.join(TMP, dir);
24
+ fs.mkdirSync(root, { recursive: true });
25
+ const contents = {};
26
+ for (const f of SRC) contents[f] = fs.readFileSync(path.join(ROOT, f), 'utf8');
27
+ mutate(contents);
28
+ for (const f of SRC) fs.writeFileSync(path.join(root, f), contents[f]);
29
+ return root;
30
+ }
31
+ const has = (r, name) => r.checks.find(c => c.name === name);
32
+ // 反例必须随版本 bump 自动跟进:硬编码版本号会让 bump 后替换失配,
33
+ // 反例没构造出来却「通过」,测试自己变成假绿(6.6.5 → 6.6.6 时踩过)。
34
+ const VERSION = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')).version;
35
+ function replaceOrThrow(contents, file, from, to) {
36
+ if (!contents[file].includes(from)) {
37
+ throw new Error('反例构造失败:' + file + ' 里找不到 ' + JSON.stringify(from) + '(当前版本 ' + VERSION + ')');
38
+ }
39
+ contents[file] = contents[file].replace(from, to);
40
+ }
41
+
42
+ try {
43
+ // 真实仓库当前应通过
44
+ const real = checkReleaseMetadata(ROOT);
45
+ ok('真实仓库通过门禁', real.ok === true, real.checks.filter(c => !c.ok));
46
+ ok('门禁检查了 CHANGELOG 章节', !!has(real, 'CHANGELOG 含 [' + real.version + '] 章节'), real.checks.map(c => c.name));
47
+
48
+ // 反例 1:release commit 漏写 CHANGELOG 章节(这正是历史上反复出现的)
49
+ const missing = fixture('missing-section', (c) => {
50
+ const v = JSON.parse(c['package.json']).version;
51
+ const re = new RegExp('^## \\[' + v.replace(/\./g, '\\.') + '\\][^\\n]*\\n', 'm');
52
+ if (!re.test(c['CHANGELOG.md'])) throw new Error('反例构造失败:CHANGELOG 缺章节');
53
+ c['CHANGELOG.md'] = c['CHANGELOG.md'].replace(re, '');
54
+ });
55
+ const r1 = checkReleaseMetadata(missing);
56
+ ok('漏写 CHANGELOG 章节 → FAIL', r1.ok === false, r1.checks.filter(c => !c.ok));
57
+ ok('失败原因明确指向缺失章节',
58
+ r1.checks.some(c => !c.ok && /缺少 ## \[/.test(c.detail)), r1.checks.filter(c => !c.ok));
59
+
60
+ // 反例 2:README 横幅没跟着 bump
61
+ const banner = fixture('bad-banner', (c) => replaceOrThrow(c, 'README.md', '**v' + VERSION + '**', '**v0.0.1**'));
62
+ const r2 = checkReleaseMetadata(banner);
63
+ ok('README 横幅不一致 → FAIL', r2.ok === false, r2.checks.filter(c => !c.ok));
64
+
65
+ // 反例 3:index.js 头注释没跟着 bump
66
+ const idx = fixture('bad-index', (c) => replaceOrThrow(c, 'index.js', 'JSQL-NEO v' + VERSION, 'JSQL-NEO v0.0.1'));
67
+ const r3 = checkReleaseMetadata(idx);
68
+ ok('index.js 头注释不一致 → FAIL', r3.ok === false, r3.checks.filter(c => !c.ok));
69
+
70
+ // 反例 4:版本号格式非法
71
+ const badVer = fixture('bad-version', (c) => replaceOrThrow(c, 'package.json', '"version": "' + VERSION + '"', '"version": "6.6"'));
72
+ const r4 = checkReleaseMetadata(badVer);
73
+ ok('非法版本号 → FAIL', r4.ok === false, r4.checks.filter(c => !c.ok));
74
+
75
+ // 开发中往 Unreleased 累积内容不应让门禁变红(只给软提示)
76
+ const wip = fixture('wip-unreleased', (c) => {
77
+ c['CHANGELOG.md'] = c['CHANGELOG.md'].replace('## [Unreleased]\n', '## [Unreleased]\n\n### Added\n\n- 开发中\n');
78
+ });
79
+ const r5 = checkReleaseMetadata(wip);
80
+ ok('开发中 Unreleased 有内容仍通过', r5.ok === true, r5.checks.filter(c => !c.ok));
81
+ ok('但给出未转正内容的 WARN', r5.warnings.some(w => /未转正/.test(w)), r5.warnings);
82
+
83
+ // 门禁本身不联网:执行期间不应有子进程网络调用
84
+ const cp = require('child_process');
85
+ const orig = { efs: cp.execFileSync, ef: cp.execFile, sp: cp.spawn };
86
+ const calls = [];
87
+ cp.execFileSync = function (...a) { calls.push(a[0]); return orig.efs.apply(this, a); };
88
+ cp.execFile = function (...a) { calls.push(a[0]); return orig.ef.apply(this, a); };
89
+ cp.spawn = function (...a) { calls.push(a[0]); return orig.sp.apply(this, a); };
90
+ checkReleaseMetadata(ROOT);
91
+ cp.execFileSync = orig.efs; cp.execFile = orig.ef; cp.spawn = orig.sp;
92
+ ok('门禁是纯本地检查,零子进程调用', calls.length === 0, calls);
93
+ } finally {
94
+ try { fs.rmSync(TMP, { recursive: true, force: true }); } catch (_) {}
95
+ }
96
+
97
+ console.log(`\n${pass} passed, ${fail} failed`);
98
+ process.exit(fail === 0 ? 0 : 1);