jsql-neo 6.8.16 → 6.8.17

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.8.16** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
6
+ > **v6.8.17** — 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.8.16 — Rust-Powered Embedded Database (WASM + HTTP)
2
+ * JSQL-NEO v6.8.17 — Rust-Powered Embedded Database (WASM + HTTP)
3
3
  *
4
4
  * @example
5
5
  * const jsql = require('jsql-neo');
@@ -106,6 +106,12 @@ function whereIndices(statement, data) {
106
106
  const isn = x === null || x === undefined;
107
107
  return node.not ? !isn : isn;
108
108
  }
109
+ // #261:裸比较符必须走 SQL 三值逻辑。
110
+ // 任一操作数为 NULL 时结果是 UNKNOWN → WHERE 中不命中。
111
+ // 此前直接 test(x, op, v):`null !== 'a'` 为 true(NULL 行被误命中),
112
+ // `null === null` 也会命中 —— 与行存通用路径结果不一致。
113
+ if (x === null || x === undefined) return false;
114
+ if (node.val === null || node.val === undefined) return false;
109
115
  return test(x, node.op, node.val);
110
116
  };
111
117
  for (let i = 0; i < n; i++) if (hit(root, i)) idx[m++] = i;
package/lib/database.js CHANGED
@@ -2326,7 +2326,7 @@ class Database {
2326
2326
  }
2327
2327
  }
2328
2328
 
2329
- async insert(tableName, data) {
2329
+ async insert(tableName, data, opts = {}) {
2330
2330
  const table = this._ensureTable(tableName);
2331
2331
  if (!table) throw this._noSuchTable(tableName);
2332
2332
  // #303:beforeInsert 可返回「修改后 data」(PLUGINS.md 契约)。
@@ -2336,10 +2336,28 @@ class Database {
2336
2336
  const arr = hookArgs[1];
2337
2337
  const pk = this._pkOf(table);
2338
2338
  // #201:多行 INSERT 走 insertMany,逐行校验全部通过后再统一落库
2339
- const inserted = (arr.length > 1 && typeof table.insertMany === 'function')
2340
- ? table.insertMany(arr)
2341
- : arr.map(row => table.insert(row));
2339
+ // #269:opts.ignore 让可忽略的错误(CHECK 违反等)只跳过该行
2340
+ let inserted;
2341
+ let ignoreWarnings = [];
2342
+ if (arr.length > 1 && typeof table.insertMany === 'function') {
2343
+ inserted = table.insertMany(arr, { ignore: opts.ignore === true });
2344
+ if (inserted && inserted.warnings) {
2345
+ ignoreWarnings = inserted.warnings;
2346
+ delete inserted.warnings;
2347
+ }
2348
+ } else {
2349
+ // 单行:Table.insert 在 ignore 模式下返回带 warnings 的空数组(该行被跳过)
2350
+ const one = table.insert(arr[0], { ignore: opts.ignore === true });
2351
+ if (one && one.warnings) {
2352
+ ignoreWarnings = one.warnings;
2353
+ inserted = [];
2354
+ } else {
2355
+ inserted = [one];
2356
+ }
2357
+ }
2342
2358
  const ids = inserted.map((r, i) => (pk ? r[pk] : table._rows.length - inserted.length + i + 1));
2359
+ // #269:把 IGNORE 跳过的行以 warnings 形式带回给上层
2360
+ if (ignoreWarnings.length > 0) ids.warnings = ignoreWarnings;
2343
2361
  // WAL 已由 Table.insert() 在写入瞬间逐条落盘(含无主键表的整表镜像)
2344
2362
  this._emit('insert', { table: tableName, count: arr.length, ids });
2345
2363
  await this._runHooks('afterInsert', [tableName, arr, ids]);
@@ -2686,12 +2704,13 @@ class Database {
2686
2704
  return { ok: true, count: removed };
2687
2705
  }
2688
2706
 
2689
- async insertMany(tableName, items) {
2707
+ async insertMany(tableName, items, opts = {}) {
2690
2708
  const table = this._ensureTable(tableName);
2691
2709
  if (!table) throw this._noSuchTable(tableName);
2692
2710
  const arr = Array.isArray(items) ? items : [items];
2693
2711
  if (!(await this._runHooks('beforeInsert', [tableName, arr]))) return [];
2694
- const inserted = table.insertMany(arr);
2712
+ // #269:opts.ignore → 可忽略的错误降级为 warning,只跳过该行
2713
+ const inserted = table.insertMany(arr, { ignore: opts.ignore === true });
2695
2714
  // WAL 由 Table.insertMany() 记录
2696
2715
  this._emit('insert', { table: tableName, count: arr.length });
2697
2716
  await this._runHooks('afterInsert', [tableName, arr, inserted]);
@@ -150,6 +150,25 @@ class Connection extends EventEmitter {
150
150
  allowStacked: this.config.multipleStatements === true,
151
151
  });
152
152
  const results = Array.isArray(r) ? r : [r];
153
+ // #253:多语句必须返回**全部**结果集(与 mysql2 一致)。
154
+ // 此前循环里只保留最后一个,前面的结果集被静默丢弃,
155
+ // 且返回值形状变成带 0/1/rows/fields 的混合对象 —— 既不是单结果也不是数组。
156
+ if (results.length > 1) {
157
+ // mysql2 多语句:返回结果数组,每项是 [rows, fields] 或结果包
158
+ return results.map((res) => {
159
+ const pk = toResultPacket(res, res ? res.table : null, rowsAsArray);
160
+ if (isQueryResult(res)) {
161
+ const a = [pk.rows, pk.fields];
162
+ a.rows = pk.rows;
163
+ a.fields = pk.fields;
164
+ return a;
165
+ }
166
+ const a = [pk];
167
+ a.rows = null;
168
+ a.fields = null;
169
+ return a;
170
+ });
171
+ }
153
172
  const last = results[results.length - 1];
154
173
  const packet = toResultPacket(last, last ? last.table : null, rowsAsArray);
155
174
  if (isQueryResult(last)) {
package/lib/pg_server.js CHANGED
@@ -607,7 +607,12 @@ class PgConnection {
607
607
 
608
608
  _handleDescribe(body) {
609
609
  const kind = body[0];
610
- const name = body.toString('utf8', 1, this._cstringLen(body, 1) - 1);
610
+ // #254:名称解析 off-by-one。
611
+ // _cstringLen(body, 1) 返回「从 start=1 起到 NUL 的长度(含 NUL)」,
612
+ // 而 toString 的 end 是相对 buffer 起点的**绝对**位置。
613
+ // 此前写成 _cstringLen(body,1)-1 → 少 1 字节 → 's1' 被读成 's'、
614
+ // 单字符名 's' 被读成空串,导致「预编译语句不存在」。
615
+ const name = body.toString('utf8', 1, this._cstringLen(body, 1));
611
616
  if (kind === 0x53) { // 'S' statement
612
617
  const stmt = this._prepared.get(name);
613
618
  if (!stmt) throw new Error(`prepared statement "${name}" does not exist`);
@@ -632,7 +637,8 @@ class PgConnection {
632
637
 
633
638
  _handleClose(body) {
634
639
  const kind = body[0];
635
- const name = body.toString('utf8', 1, this._cstringLen(body, 1) - 1);
640
+ // #254:与 _handleDescribe 同样的 off-by-one(名称少读 1 字节)
641
+ const name = body.toString('utf8', 1, this._cstringLen(body, 1));
636
642
  if (kind === 0x53) this._prepared.delete(name);
637
643
  else if (kind === 0x50) this._portal.delete(name);
638
644
  this._send(buildCloseComplete());
package/lib/sql.js CHANGED
@@ -6813,13 +6813,16 @@ class SQLExecutor {
6813
6813
  return out;
6814
6814
  };
6815
6815
  if (statement.dataRows) {
6816
- statement.dataRows = statement.dataRows.map(row => {
6817
- for (const [c, def] of Object.entries(schema)) {
6818
- if (def.autoIncrement && (row[c] === null || row[c] === undefined)) delete row[c];
6819
- }
6820
- return stripDefault(row);
6821
- });
6822
- dataRows = statement.dataRows;
6816
+ statement.dataRows = statement.dataRows.map(row => {
6817
+ for (const [c, def] of Object.entries(schema)) {
6818
+ // #268:自增列显式写 0 与 NULL 一样表示「由引擎分配」
6819
+ if (def.autoIncrement && (row[c] === null || row[c] === undefined || row[c] === 0)) {
6820
+ delete row[c];
6821
+ }
6822
+ }
6823
+ return stripDefault(row);
6824
+ });
6825
+ dataRows = statement.dataRows;
6823
6826
  }
6824
6827
  // INSERT ... SELECT:先跑 SELECT,再把结果行映射成待插入行
6825
6828
  if (statement.select) {
@@ -6867,7 +6870,12 @@ class SQLExecutor {
6867
6870
  let vi = 0;
6868
6871
  colNames.forEach(c => {
6869
6872
  const isDefault = vals[vi] && typeof vals[vi] === 'object' && vals[vi]._default;
6870
- if (schema[c].autoIncrement && (skipAuto || vals[vi] === undefined || vals[vi] === null || isDefault)) {
6873
+ // #268:AUTO_INCREMENT 列显式写 0 与 NULL 一样表示「由引擎分配」
6874
+ // (MySQL 8.0 默认 sql_mode)。此前 0 被当显式主键值,
6875
+ // 于是批内多行 0 互相冲突(Duplicate entry for primary key)。
6876
+ const isAutoZero = schema[c].autoIncrement && vals[vi] === 0;
6877
+ if (schema[c].autoIncrement
6878
+ && (skipAuto || vals[vi] === undefined || vals[vi] === null || isDefault || isAutoZero)) {
6871
6879
  if (!skipAuto && vi < vals.length) vi++;
6872
6880
  return;
6873
6881
  }
@@ -7084,9 +7092,17 @@ class SQLExecutor {
7084
7092
  toInsert = kept;
7085
7093
  }
7086
7094
  let ids = [];
7095
+ let insertWarnings = [];
7087
7096
  if (toInsert.length > 0) {
7088
- ids = await this.engine.insert(statement.name, toInsert);
7089
- await this.engine.flush();
7097
+ // #269:INSERT IGNORE 把可忽略的错误降级为 warning(只跳过该行),
7098
+ // 需要把 ignore 标记传给存储层;普通 INSERT 保持原子失败。
7099
+ const ignoreMode = this._conflictAction(statement) === 'ignore';
7100
+ ids = await this.engine.insert(statement.name, toInsert, { ignore: ignoreMode });
7101
+ if (ignoreMode && ids && ids.warnings) {
7102
+ insertWarnings = ids.warnings;
7103
+ delete ids.warnings;
7104
+ }
7105
+ await this.engine.flush();
7090
7106
  }
7091
7107
  // #164:自增主键在存储层生成,插入前构造的行快照没有该值;
7092
7108
  // RETURNING 之前把最终 id 回填,避免返回 null。
@@ -7108,6 +7124,11 @@ class SQLExecutor {
7108
7124
  duplicateUpdated: updated,
7109
7125
  };
7110
7126
  if (skipped > 0) out.duplicateSkipped = skipped;
7127
+ // #269:INSERT IGNORE 跳过的行以 warnings 形式暴露
7128
+ if (insertWarnings.length > 0) {
7129
+ out.warnings = insertWarnings;
7130
+ out.ignoredRows = insertWarnings.length;
7131
+ }
7111
7132
  if (statement.returning) this._attachReturning(out, statement, toInsert, schema);
7112
7133
  return out;
7113
7134
  }
package/lib/table.js CHANGED
@@ -555,7 +555,7 @@ class Table {
555
555
  if (this._columnar) this._columnar = null;
556
556
  }
557
557
 
558
- insert(data) {
558
+ insert(data, opts = {}) {
559
559
  this._invalidateColumnar();
560
560
  for (const hook of this._hooks.beforeInsert) {
561
561
  data = hook({ ...data }) || data;
@@ -564,13 +564,39 @@ class Table {
564
564
  data = this._applyDefaults(data);
565
565
  data = this._applyComputed(data);
566
566
  data = this._validateDataTypes(data);
567
- this._validateConstraints(data, -1);
568
-
569
- if (this._autoIncrementField && data[this._autoIncrementField] === undefined) {
570
- this._autoIncrement++;
571
- data[this._autoIncrementField] = this._autoIncrement;
572
- } else if (this._autoIncrementField && data[this._autoIncrementField] > this._autoIncrement) {
573
- this._autoIncrement = data[this._autoIncrementField];
567
+ // #269:INSERT IGNORE 的单行路径也要能降级为 warning(跳过该行)
568
+ if (opts.ignore === true) {
569
+ try {
570
+ this._validateConstraints(data, -1);
571
+ } catch (e) {
572
+ if (!isIgnorableInsertError(e)) throw e;
573
+ const warning = {
574
+ index: 0,
575
+ code: e.code || e.errno || 'ER_UNKNOWN',
576
+ errno: e.errno,
577
+ message: e.message,
578
+ };
579
+ const res = [];
580
+ res.warnings = [warning];
581
+ return res;
582
+ }
583
+ } else {
584
+ this._validateConstraints(data, -1);
585
+ }
586
+
587
+ // #268:AUTO_INCREMENT 列显式插入 0(或 NULL)表示「由引擎分配」,
588
+ // 与不提供该列等价 —— MySQL 8.0 默认 sql_mode 就是这样
589
+ // (除非显式开 NO_AUTO_VALUE_ON_ZERO)。
590
+ // 此前只把 undefined 当「未提供」,0 被原样落库 → 表里出现 id=0,
591
+ // 后续自增序列整体错位。
592
+ if (this._autoIncrementField) {
593
+ const av = data[this._autoIncrementField];
594
+ if (av === undefined || av === null || av === 0) {
595
+ this._autoIncrement++;
596
+ data[this._autoIncrementField] = this._autoIncrement;
597
+ } else if (av > this._autoIncrement) {
598
+ this._autoIncrement = av;
599
+ }
574
600
  }
575
601
 
576
602
  const rowIndex = this._rows.length;
@@ -589,8 +615,14 @@ class Table {
589
615
  return data;
590
616
  }
591
617
 
592
- insertMany(items) {
618
+ insertMany(items, opts = {}) {
593
619
  if (items.length === 0) return [];
620
+ // #269:INSERT IGNORE 的行级降级语义。
621
+ // MySQL 的 IGNORE 把「可忽略的错误」(CHECK 违反、重复键、超长、数值越界)
622
+ // 降级为 warning 并**只跳过该行**,其余行照常插入。
623
+ // 此前 IGNORE 只处理了「键冲突」行,CHECK 违反会整条语句抛 3819 并回滚全部行。
624
+ const ignore = opts.ignore === true;
625
+ const warnings = [];
594
626
  for (const hook of this._hooks.beforeInsert) {
595
627
  const mapped = new Array(items.length);
596
628
  for (let i = 0; i < items.length; i++) mapped[i] = hook({ ...items[i] }) || items[i];
@@ -636,7 +668,10 @@ class Table {
636
668
  // 任一行违反约束则整条语句原子失败,不残留前序行。
637
669
  const prepared = new Array(N);
638
670
  const pkSeen = new Set();
671
+ const skippedRows = [];
639
672
  for (let i = 0; i < N; i++) {
673
+ // #269:IGNORE 模式下,可忽略的错误只跳过本行;其它模式保持原子失败。
674
+ try {
640
675
  // 在副本上做默认值/计算列/类型归一,避免污染调用方传入的原始对象
641
676
  let data = { ...items[i] };
642
677
  for (const [f, dv] of Object.entries(defaults)) {
@@ -691,21 +726,41 @@ class Table {
691
726
  if (!exists) throw createError('ER_NO_REFERENCED_ROW');
692
727
  }
693
728
  }
694
- if (autoIncField && data[autoIncField] === undefined) {
695
- autoIncSeq++;
696
- data[autoIncField] = baseAutoInc + autoIncSeq;
697
- this._autoIncrement = data[autoIncField];
698
- } else if (autoIncField && data[autoIncField] > this._autoIncrement) {
699
- this._autoIncrement = data[autoIncField];
729
+ // #268:自增列插 0 / NULL 表示「由引擎分配」(与 insert 单条路径同口径)
730
+ if (autoIncField) {
731
+ const av = data[autoIncField];
732
+ if (av === undefined || av === null || av === 0) {
733
+ autoIncSeq++;
734
+ data[autoIncField] = baseAutoInc + autoIncSeq;
735
+ this._autoIncrement = data[autoIncField];
736
+ } else if (av > this._autoIncrement) {
737
+ this._autoIncrement = av;
738
+ }
700
739
  }
701
740
  prepared[i] = data;
741
+ } catch (e) {
742
+ // #269:IGNORE 模式下把「可忽略的错误」降级为 warning,只跳过本行。
743
+ // MySQL 的 IGNORE 覆盖:CHECK 违反、重复键、数据超长、数值越界、
744
+ // NOT NULL 违反。其它错误(如外键不存在)仍应抛出。
745
+ if (!ignore || !isIgnorableInsertError(e)) throw e;
746
+ warnings.push({
747
+ index: i,
748
+ code: e.code || e.errno || 'ER_UNKNOWN',
749
+ errno: e.errno,
750
+ message: e.message,
751
+ });
752
+ prepared[i] = null; // 标记为跳过
753
+ }
702
754
  }
703
- const result = new Array(N);
755
+ const result = [];
704
756
  const baseRowIdx = this._rows.length;
757
+ let insertedCount = 0;
705
758
  for (let i = 0; i < N; i++) {
706
759
  const data = prepared[i];
760
+ if (data === null) continue; // #269:被 IGNORE 跳过的行不落库
707
761
  const rowIdx = this._rows.length;
708
762
  this._rows.push(Object.assign({}, data));
763
+ insertedCount++;
709
764
  if (this._pkIndex && this._primaryKey) {
710
765
  const pkVal = data[this._primaryKey];
711
766
  if (pkVal !== undefined && pkVal !== null) {
@@ -717,16 +772,18 @@ class Table {
717
772
  if (val !== undefined && val !== null) tree.insert(val, rowIdx);
718
773
  }
719
774
  // 不写回调用方传入的 items,改为返回归一后的新数组
720
- result[i] = data;
775
+ result.push(data);
721
776
  }
722
777
  this._dirty = true;
723
778
  this._db._markDirty(this._name);
724
779
  for (const hook of this._hooks.afterInsert) {
725
- for (let i = 0; i < N; i++) hook(result[i]);
780
+ for (let i = 0; i < result.length; i++) hook(result[i]);
726
781
  }
727
- this._db._emitChange('insertMany', this._name, { count: N });
782
+ this._db._emitChange('insertMany', this._name, { count: insertedCount });
728
783
  this._db._walLogUpsert(this._name, result);
729
- for (let i = 0; i < N; i++) this._markRowDirty(this._rows[this._rows.length - N + i]);
784
+ for (let i = 0; i < insertedCount; i++) this._markRowDirty(this._rows[this._rows.length - insertedCount + i]);
785
+ // #269:把被 IGNORE 跳过的行作为 warnings 返回给上层
786
+ if (warnings.length > 0) result.warnings = warnings;
730
787
  return result;
731
788
  }
732
789
 
@@ -2472,4 +2529,30 @@ function roundDecimalToScale(value, scale) {
2472
2529
  return neg && n !== 0 ? -n : n;
2473
2530
  }
2474
2531
 
2532
+ /**
2533
+ * 判断一个插入错误是否可以被 INSERT IGNORE 降级为 warning(Issue #269)。
2534
+ *
2535
+ * MySQL 的 IGNORE 覆盖:CHECK 违反、重复键、数据超长、数值越界、NOT NULL 违反。
2536
+ * 其它错误(如外键引用不存在的表)不在 IGNORE 覆盖范围内,仍应抛出。
2537
+ */
2538
+ function isIgnorableInsertError(err) {
2539
+ if (!err) return false;
2540
+ // JSQL_Error 的 code 就是数字 errno(如 3819);字符串形态来自自定义错误。
2541
+ const code = err.code;
2542
+ if (typeof code === 'string') {
2543
+ if (code === 'ER_CHECK_CONSTRAINT' || code === 'ER_DUP_ENTRY'
2544
+ || code === 'ER_DATA_TOO_LONG' || code === 'ER_OUT_OF_RANGE'
2545
+ || code === 'ER_BAD_NULL_ERROR' || code === 'ER_TRUNCATED_WRONG_VALUE') {
2546
+ return true;
2547
+ }
2548
+ }
2549
+ // MySQL 数字 errno:3819 CHECK、1062 重复键、1406 超长、1264 越界、
2550
+ // 1048 NOT NULL、1292 截断
2551
+ const errno = (typeof code === 'number' ? code : null)
2552
+ || err.errno
2553
+ || (err.details && err.details.code);
2554
+ return errno === 3819 || errno === 1062 || errno === 1406
2555
+ || errno === 1264 || errno === 1048 || errno === 1292;
2556
+ }
2557
+
2475
2558
  module.exports = Table;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsql-neo",
3
- "version": "6.8.16",
3
+ "version": "6.8.17",
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 && node test/issue-284-292.test.js && node test/issue-301-305.test.js && node test/issue-248-252.test.js && node test/issue-259-264.test.js && node test/issue-255-270.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 && node test/issue-301-305.test.js && node test/issue-248-252.test.js && node test/issue-259-264.test.js && node test/issue-255-270.test.js && node test/issue-253-269.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 && node test/issue-284-292.test.js && node test/issue-301-305.test.js && node test/issue-248-252.test.js && node test/issue-259-264.test.js && node test/issue-255-270.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 && node test/issue-301-305.test.js && node test/issue-248-252.test.js && node test/issue-259-264.test.js && node test/issue-255-270.test.js && node test/issue-253-269.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,135 @@
1
+ /* ---- 回归:#253 / #254 / #261 / #268 / #269 ---- */
2
+ /*
3
+ * #253 mysql_compat 多语句只返回最后一个结果集,前面的结果集被静默丢弃。
4
+ * #254 PG Describe/Close 的语句名解析 off-by-one:'s1' 被读成 's'、
5
+ * 单字符名读成空串。
6
+ * #261 列存快路径的 WHERE 比较符不做 NULL 三值逻辑,与行存通用路径结果不一致。
7
+ * #268 AUTO_INCREMENT 列显式插入 0 被当作字面值保留,MySQL 会分配下一个序列值。
8
+ * #269 INSERT IGNORE 遇到 CHECK 违反时整条语句抛 3819,MySQL 应降级为 warning。
9
+ */
10
+ const path = require('path');
11
+ const ROOT = path.join(__dirname, '..');
12
+ const { Database, executeSQL } = require(path.join(ROOT, 'index.js'));
13
+ const { PgConnection } = require(path.join(ROOT, 'lib/pg_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
+ (async () => {
22
+ const db = new Database(':memory:', { autoSave: false });
23
+ const q = async (sql) => (await executeSQL(db, sql, { safety: false })).rows;
24
+ const one = async (sql) => (await q(sql))[0][0];
25
+
26
+ // ================= #261 列存 WHERE 的 NULL 三值逻辑 =================
27
+ await q('CREATE TABLE t (id INTEGER PRIMARY KEY, v TEXT, n INT)');
28
+ await q("INSERT INTO t (id,v,n) VALUES (1,'a',10),(2,NULL,20),(3,'b',30)");
29
+ const rowPath = {
30
+ c1: (await q("SELECT COUNT(*) AS c FROM t WHERE v <> 'a'"))[0][0],
31
+ c2: (await q('SELECT COUNT(*) AS c FROM t WHERE v = NULL'))[0][0],
32
+ c3: (await q('SELECT COUNT(*) AS c FROM t WHERE v IS NULL'))[0][0],
33
+ s1: (await q("SELECT SUM(n) AS s FROM t WHERE v <> 'a'"))[0][0],
34
+ };
35
+ db.convertToColumnar('t');
36
+ const colPath = {
37
+ c1: (await q("SELECT COUNT(*) AS c FROM t WHERE v <> 'a'"))[0][0],
38
+ c2: (await q('SELECT COUNT(*) AS c FROM t WHERE v = NULL'))[0][0],
39
+ c3: (await q('SELECT COUNT(*) AS c FROM t WHERE v IS NULL'))[0][0],
40
+ s1: (await q("SELECT SUM(n) AS s FROM t WHERE v <> 'a'"))[0][0],
41
+ };
42
+ ok("列存 v <> 'a' 计数与行存一致(NULL 行不命中)",
43
+ colPath.c1 === rowPath.c1, { row: rowPath.c1, col: colPath.c1 });
44
+ ok('列存 v = NULL 计数与行存一致(恒为 0)',
45
+ colPath.c2 === rowPath.c2, { row: rowPath.c2, col: colPath.c2 });
46
+ ok('列存 v IS NULL 计数与行存一致',
47
+ colPath.c3 === rowPath.c3, { row: rowPath.c3, col: colPath.c3 });
48
+ ok('列存 SUM 与行存一致(无浮点/3VL 偏差)',
49
+ colPath.s1 === rowPath.s1, { row: rowPath.s1, col: colPath.s1 });
50
+ // 列存转换后写入 → 缓存失效,仍与行存一致
51
+ await q("INSERT INTO t (id,v,n) VALUES (4,NULL,40)");
52
+ ok('列存写入后缓存失效,NULL 计数仍与行存一致',
53
+ (await q('SELECT COUNT(*) AS c FROM t WHERE v = NULL'))[0][0] === 0, null);
54
+
55
+ // ================= #254 PG Describe/Close 名称解析 =================
56
+ {
57
+ const conn = Object.create(PgConnection.prototype);
58
+ const body = Buffer.concat([Buffer.from('S'), Buffer.from('s1\0', 'utf8')]);
59
+ const name = body.toString('utf8', 1, conn._cstringLen(body, 1));
60
+ ok("PG Describe 解析出的语句名是 's1'(不是 's')", name === 's1', name);
61
+ const body1 = Buffer.concat([Buffer.from('S'), Buffer.from('s\0', 'utf8')]);
62
+ const name1 = body1.toString('utf8', 1, conn._cstringLen(body1, 1));
63
+ ok("PG Describe 单字符名 's' 解析正确(不是空串)", name1 === 's', name1);
64
+ }
65
+
66
+ // ================= #268 AUTO_INCREMENT 显式插 0 =================
67
+ await q('CREATE TABLE au (id INT PRIMARY KEY AUTO_INCREMENT, v INT)');
68
+ await q('INSERT INTO au (id, v) VALUES (0, 1)');
69
+ await q('INSERT INTO au (id, v) VALUES (NULL, 2)');
70
+ await q('INSERT INTO au (v) VALUES (3)');
71
+ const auRows = await q('SELECT id, v FROM au ORDER BY id');
72
+ ok('AUTO_INCREMENT 列插 0 → 分配序列值 1(不出现 id=0)',
73
+ auRows[0][0] === 1, auRows[0]);
74
+ ok('AUTO_INCREMENT 列插 NULL → 分配序列值 2',
75
+ auRows[1][0] === 2, auRows[1]);
76
+ ok('AUTO_INCREMENT 列不提供 → 分配序列值 3',
77
+ auRows[2][0] === 3, auRows[2]);
78
+ ok('AUTO_INCREMENT 序列无 id=0 的行',
79
+ !auRows.some(r => r[0] === 0), auRows);
80
+ // 批量路径
81
+ await q('CREATE TABLE au2 (id INT PRIMARY KEY AUTO_INCREMENT, v INT)');
82
+ await q('INSERT INTO au2 (id,v) VALUES (0,1),(NULL,2),(0,3)');
83
+ const au2Rows = await q('SELECT id FROM au2 ORDER BY id');
84
+ ok('批量插入里多个 0 也各自分配序列值',
85
+ JSON.stringify(au2Rows.map(r => r[0])) === '[1,2,3]', au2Rows);
86
+ // 不回归:显式非零 id 仍保留
87
+ await q('CREATE TABLE au3 (id INT PRIMARY KEY AUTO_INCREMENT, v INT)');
88
+ await q('INSERT INTO au3 (id,v) VALUES (10,1)');
89
+ ok('显式非零 AUTO_INCREMENT 值仍保留',
90
+ (await q('SELECT id FROM au3'))[0][0] === 10, null);
91
+
92
+ // ================= #269 INSERT IGNORE =================
93
+ await q('CREATE TABLE ig (id INT PRIMARY KEY, a INT CHECK (a > 0))');
94
+ let r = await executeSQL(db, 'INSERT IGNORE INTO ig VALUES (1, -5)', { safety: false });
95
+ ok('INSERT IGNORE 遇 CHECK 违反不报错',
96
+ r && r.ok === true, r && r.ok);
97
+ ok('INSERT IGNORE 违反 CHECK 的行被跳过',
98
+ (await q('SELECT * FROM ig')).length === 0, await q('SELECT * FROM ig'));
99
+ ok('INSERT IGNORE 返回 warnings', r.warnings && r.warnings.length === 1, r.warnings);
100
+
101
+ r = await executeSQL(db, 'INSERT IGNORE INTO ig VALUES (1, -5), (3, 5)', { safety: false });
102
+ ok('INSERT IGNORE 混合批次:坏行跳过、好行插入',
103
+ JSON.stringify(await q('SELECT * FROM ig')) === '[[3,5]]', await q('SELECT * FROM ig'));
104
+ ok('INSERT IGNORE 混合批次返回 1 条 warning',
105
+ r.warnings && r.warnings.length === 1, r.warnings);
106
+
107
+ // 不回归:普通 INSERT 遇 CHECK 违反仍抛错
108
+ let igErr = null;
109
+ try { await q('INSERT INTO ig VALUES (9, -1)'); } catch (e) { igErr = e; }
110
+ ok('普通 INSERT 遇 CHECK 违反仍抛 3819',
111
+ igErr && igErr.code === 3819, igErr && igErr.code);
112
+
113
+ // ================= #253 mysql_compat 多语句结果集 =================
114
+ {
115
+ const { createConnection } = require(path.join(ROOT, 'index.js'));
116
+ const conn = createConnection({ multipleStatements: true });
117
+ await conn.connect();
118
+ const res = await conn.query('SELECT 1 AS x; SELECT 2 AS y');
119
+ ok('mysql_compat 多语句返回数组(不是混合对象)', Array.isArray(res), typeof res);
120
+ ok('mysql_compat 多语句返回 2 个结果集', Array.isArray(res) && res.length === 2, res && res.length);
121
+ const first = res && res[0] && (res[0].rows || res[0][0]);
122
+ const second = res && res[1] && (res[1].rows || res[1][0]);
123
+ ok('第一个结果集 {x:1} 未丢失',
124
+ JSON.stringify(first) === JSON.stringify([{ x: 1 }]), first);
125
+ ok('第二个结果集 {y:2} 正确',
126
+ JSON.stringify(second) === JSON.stringify([{ y: 2 }]), second);
127
+ await conn.end();
128
+ }
129
+
130
+ console.log(`\n${pass} passed, ${fail} failed`);
131
+ process.exit(fail === 0 ? 0 : 1);
132
+ })().catch(e => {
133
+ console.log('[FAIL] unexpected exception: ' + (e.stack || e).split('\n').slice(0, 4).join('\n'));
134
+ process.exit(1);
135
+ });