jsql-neo 6.8.15 → 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 +1 -1
- package/index.js +1 -1
- package/lib/columnar_fast.js +6 -0
- package/lib/database.js +25 -6
- package/lib/mongo_server.js +9 -0
- package/lib/mysql_compat.js +19 -0
- package/lib/pg_server.js +8 -2
- package/lib/sql.js +56 -13
- package/lib/table.js +166 -23
- package/package.json +3 -3
- package/test/issue-253-269.test.js +135 -0
- package/test/issue-255-270.test.js +145 -0
- package/test/issue-38.test.js +10 -5
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
> **One engine to rule them all** — a Rust-powered embedded database that speaks your language:
|
|
4
4
|
> MySQL, PostgreSQL, MongoDB, Redis, SQL, TypeScript, **and the browser**.
|
|
5
5
|
|
|
6
|
-
> **v6.8.
|
|
6
|
+
> **v6.8.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
|

|
|
9
9
|

|
package/index.js
CHANGED
package/lib/columnar_fast.js
CHANGED
|
@@ -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
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
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
|
-
|
|
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]);
|
package/lib/mongo_server.js
CHANGED
|
@@ -653,6 +653,11 @@ class MongoServer {
|
|
|
653
653
|
const engine = await this._ensureCollection(coll, this._dbOf(doc), docs);
|
|
654
654
|
const ids = [];
|
|
655
655
|
const writeErrors = [];
|
|
656
|
+
// #255:MongoDB 的 insert 命令 ordered 选项默认 true ——
|
|
657
|
+
// 一旦某条文档写入失败(如 E11000 重复键),**后续文档不再插入**。
|
|
658
|
+
// 此前循环从头到尾没读 doc.ordered,冲突之后continue 继续插下一条,
|
|
659
|
+
// 导致「本应中止保护上游批次语义」的写入实际全部落库。
|
|
660
|
+
const ordered = doc.ordered !== false;
|
|
656
661
|
for (let i = 0; i < docs.length; i++) {
|
|
657
662
|
const doc2 = { ...docs[i] };
|
|
658
663
|
// 检查 _id 唯一性
|
|
@@ -680,6 +685,8 @@ class MongoServer {
|
|
|
680
685
|
keyPattern: { _id: 1 },
|
|
681
686
|
keyValue: { _id: doc2._id },
|
|
682
687
|
});
|
|
688
|
+
// #255:ordered:true 时首个写入失败即中止本批(后续文档不插入)
|
|
689
|
+
if (ordered) break;
|
|
683
690
|
continue;
|
|
684
691
|
}
|
|
685
692
|
}
|
|
@@ -703,6 +710,8 @@ class MongoServer {
|
|
|
703
710
|
keyPattern: Object.fromEntries(dupIdx.columns.map(c => [c, 1])),
|
|
704
711
|
keyValue,
|
|
705
712
|
});
|
|
713
|
+
// #255:ordered:true 时首个写入失败即中止本批
|
|
714
|
+
if (ordered) break;
|
|
706
715
|
continue;
|
|
707
716
|
}
|
|
708
717
|
}
|
package/lib/mysql_compat.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
@@ -934,23 +934,35 @@ class Parser {
|
|
|
934
934
|
if (type === 'json' || type === 'jsonb') def.jsonType = true;
|
|
935
935
|
if (/^(BIG)?SERIAL$/i.test(typeRaw)) { def.autoIncrement = true; def.required = true; }
|
|
936
936
|
|
|
937
|
-
// 长度限制: TEXT(255) / VARCHAR(100) / INTEGER(11) ...
|
|
937
|
+
// 长度限制: TEXT(255) / VARCHAR(100) / INTEGER(11) / DECIMAL(10,2) ...
|
|
938
938
|
if (this.peek().type === 'op' && this.peek().value === '(') {
|
|
939
939
|
this.next();
|
|
940
940
|
const lenTok = this.next();
|
|
941
941
|
if (lenTok.type !== 'number') throw new Error(`Expected length after type '(', got '${lenTok.value}'`);
|
|
942
942
|
const len = lenTok.value;
|
|
943
|
+
// #266:DECIMAL(p,s) 的两个参数语义不同 ——
|
|
944
|
+
// p = 总位数(precision),s = 小数位(scale)。
|
|
945
|
+
// 此前把 s 存进了 def.precision,而全库没有任何消费者,
|
|
946
|
+
// 于是列级 DECIMAL(10,2) 完全没有按标度舍入(1.005 原样存成 1.005)。
|
|
947
|
+
let scale = null;
|
|
943
948
|
if (this.peek().type === 'op' && this.peek().value === ',') {
|
|
944
949
|
this.next();
|
|
945
950
|
const decTok = this.next();
|
|
946
951
|
if (decTok.type !== 'number') throw new Error(`Expected precision after ',', got '${decTok.value}'`);
|
|
947
|
-
|
|
952
|
+
scale = decTok.value;
|
|
948
953
|
}
|
|
949
954
|
this.expect('op', ')');
|
|
950
955
|
if (len > 0) {
|
|
951
956
|
def.length = len;
|
|
952
957
|
if (mapped === 'string') def.maxLength = len;
|
|
953
958
|
}
|
|
959
|
+
if (scale !== null) {
|
|
960
|
+
def.scale = scale;
|
|
961
|
+
// 总位数 = p;p - s 才是整数部分允许的位数
|
|
962
|
+
def.precision = Math.max(0, len - scale);
|
|
963
|
+
def.decimalPrecision = len;
|
|
964
|
+
def.decimalScale = scale;
|
|
965
|
+
}
|
|
954
966
|
}
|
|
955
967
|
|
|
956
968
|
while (true) {
|
|
@@ -4817,6 +4829,11 @@ function applyScalarFunction(fnNode, row, ctx) {
|
|
|
4817
4829
|
return (a && b) ? pgAge(b, a) : null;
|
|
4818
4830
|
}
|
|
4819
4831
|
case 'FROM_UNIXTIME': {
|
|
4832
|
+
// #256:NULL 必须传播,不能当成时间戳 0。
|
|
4833
|
+
// JS 里 Number(null) === 0,Number(null) 又是有限数 —— 于是
|
|
4834
|
+
// FROM_UNIXTIME(NULL) 与 FROM_UNIXTIME(0) 输出完全一样。
|
|
4835
|
+
// MySQL:「If unix_timestamp or format is NULL, this function returns NULL」。
|
|
4836
|
+
if (args[0] == null) return null;
|
|
4820
4837
|
const ts = Number(args[0]);
|
|
4821
4838
|
if (!Number.isFinite(ts)) return null;
|
|
4822
4839
|
const d = new Date(ts * 1000);
|
|
@@ -5386,7 +5403,12 @@ function applyScalarFunction(fnNode, row, ctx) {
|
|
|
5386
5403
|
}
|
|
5387
5404
|
case 'RAND': return args.length > 0 && args[0] != null ? seedRand(Number(args[0]))() : Math.random();
|
|
5388
5405
|
case 'UNIX_TIMESTAMP': {
|
|
5389
|
-
|
|
5406
|
+
// #257:显式传 NULL 与「不传参」必须区分。
|
|
5407
|
+
// 此前 `args.length > 0 && args[0] != null` 把两者合并成同一分支,
|
|
5408
|
+
// UNIX_TIMESTAMP(NULL) 于是落到 Date.now() 兜底,返回执行时刻的时间戳。
|
|
5409
|
+
// MySQL:date 为 NULL 时返回 NULL;只有无参形式才返回当前时间戳。
|
|
5410
|
+
if (args.length > 0) {
|
|
5411
|
+
if (args[0] == null) return null;
|
|
5390
5412
|
// 字符串按 UTC 基准解释(与落库值同口径),Date / 数字走绝对时刻
|
|
5391
5413
|
let ms = null;
|
|
5392
5414
|
if (args[0] instanceof Date) ms = args[0].getTime();
|
|
@@ -6791,13 +6813,16 @@ class SQLExecutor {
|
|
|
6791
6813
|
return out;
|
|
6792
6814
|
};
|
|
6793
6815
|
if (statement.dataRows) {
|
|
6794
|
-
|
|
6795
|
-
|
|
6796
|
-
|
|
6797
|
-
|
|
6798
|
-
|
|
6799
|
-
|
|
6800
|
-
|
|
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;
|
|
6801
6826
|
}
|
|
6802
6827
|
// INSERT ... SELECT:先跑 SELECT,再把结果行映射成待插入行
|
|
6803
6828
|
if (statement.select) {
|
|
@@ -6845,7 +6870,12 @@ class SQLExecutor {
|
|
|
6845
6870
|
let vi = 0;
|
|
6846
6871
|
colNames.forEach(c => {
|
|
6847
6872
|
const isDefault = vals[vi] && typeof vals[vi] === 'object' && vals[vi]._default;
|
|
6848
|
-
|
|
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)) {
|
|
6849
6879
|
if (!skipAuto && vi < vals.length) vi++;
|
|
6850
6880
|
return;
|
|
6851
6881
|
}
|
|
@@ -7062,9 +7092,17 @@ class SQLExecutor {
|
|
|
7062
7092
|
toInsert = kept;
|
|
7063
7093
|
}
|
|
7064
7094
|
let ids = [];
|
|
7095
|
+
let insertWarnings = [];
|
|
7065
7096
|
if (toInsert.length > 0) {
|
|
7066
|
-
|
|
7067
|
-
|
|
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();
|
|
7068
7106
|
}
|
|
7069
7107
|
// #164:自增主键在存储层生成,插入前构造的行快照没有该值;
|
|
7070
7108
|
// RETURNING 之前把最终 id 回填,避免返回 null。
|
|
@@ -7086,6 +7124,11 @@ class SQLExecutor {
|
|
|
7086
7124
|
duplicateUpdated: updated,
|
|
7087
7125
|
};
|
|
7088
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
|
+
}
|
|
7089
7132
|
if (statement.returning) this._attachReturning(out, statement, toInsert, schema);
|
|
7090
7133
|
return out;
|
|
7091
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
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
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
|
-
|
|
695
|
-
|
|
696
|
-
data[autoIncField]
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
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 =
|
|
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
|
|
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 <
|
|
780
|
+
for (let i = 0; i < result.length; i++) hook(result[i]);
|
|
726
781
|
}
|
|
727
|
-
this._db._emitChange('insertMany', this._name, { count:
|
|
782
|
+
this._db._emitChange('insertMany', this._name, { count: insertedCount });
|
|
728
783
|
this._db._walLogUpsert(this._name, result);
|
|
729
|
-
for (let i = 0; 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
|
|
|
@@ -2210,11 +2267,18 @@ class Table {
|
|
|
2210
2267
|
// 显式 BigInt:>2^53 也能精确保存(Issue #38)。
|
|
2211
2268
|
result[field] = normalizeInteger(v, t);
|
|
2212
2269
|
} else if (typeof v === 'number') {
|
|
2213
|
-
|
|
2270
|
+
// #270:向整数列写带小数部分的值,MySQL 按四舍五入取整
|
|
2271
|
+
// (1.9 → 2、1.4 → 1;负数 -1.5 → -2,半远离零),
|
|
2272
|
+
// 不报错。此前直接抛 1292,导致任何来自 JSON/CSV/ORM 的
|
|
2273
|
+
// 小数写整型列都会失败。
|
|
2274
|
+
let iv = v;
|
|
2275
|
+
if (!Number.isInteger(iv)) {
|
|
2276
|
+
iv = iv < 0 ? -Math.round(-iv) : Math.round(iv);
|
|
2277
|
+
}
|
|
2214
2278
|
// 超过 2^53-1 的 number 在调用方就已经丢失精度,不能再假装
|
|
2215
2279
|
// 存进去了:明确报错,要求传 BigInt 或十进制字符串。
|
|
2216
|
-
if (!Number.isSafeInteger(
|
|
2217
|
-
result[field] =
|
|
2280
|
+
if (!Number.isSafeInteger(iv)) throw integerError(t, v);
|
|
2281
|
+
result[field] = iv;
|
|
2218
2282
|
} else if (typeof v === 'string' && DECIMAL_INT_RE.test(v.trim())) {
|
|
2219
2283
|
result[field] = normalizeInteger(v.trim(), t);
|
|
2220
2284
|
} else {
|
|
@@ -2229,6 +2293,15 @@ class Table {
|
|
|
2229
2293
|
}
|
|
2230
2294
|
else if (v && v.__decimalRaw) result[field] = Number(v.__decimalRaw);
|
|
2231
2295
|
else throw createError('ER_TRUNCATED_WRONG_VALUE', t, v);
|
|
2296
|
+
// #266:DECIMAL(p,s) 的列必须按标度 s 舍入。
|
|
2297
|
+
// 此前 def.precision 全库没有消费者,列级 DECIMAL(10,2) 形同虚设 ——
|
|
2298
|
+
// 1.005 原样存成 1.005,SUM() 还会累积 IEEE754 误差。
|
|
2299
|
+
// 用定点十进制字符串做舍入,避免 Math.round(v * 100) 的二进制误差。
|
|
2300
|
+
const scale = def.decimalScale !== undefined ? def.decimalScale
|
|
2301
|
+
: (def.scale !== undefined ? def.scale : null);
|
|
2302
|
+
if (scale !== null && scale >= 0 && typeof result[field] === 'number') {
|
|
2303
|
+
result[field] = roundDecimalToScale(result[field], scale);
|
|
2304
|
+
}
|
|
2232
2305
|
} else if (t === 'boolean' || t === 'bool') {
|
|
2233
2306
|
if (typeof v === 'boolean') result[field] = v;
|
|
2234
2307
|
else if (v === 0 || v === 1) result[field] = !!v;
|
|
@@ -2412,4 +2485,74 @@ class Table {
|
|
|
2412
2485
|
}
|
|
2413
2486
|
}
|
|
2414
2487
|
|
|
2488
|
+
/**
|
|
2489
|
+
* 把数值按小数位 scale 四舍五入(半远离零),返回 number。
|
|
2490
|
+
*
|
|
2491
|
+
* #266:DECIMAL(p,s) 列的标度必须生效。直接用
|
|
2492
|
+
* `Math.round(v * 10**scale) / 10**scale` 会有二进制浮点误差
|
|
2493
|
+
* (1.005 * 100 = 100.49999999999999 → Math.round 得 100 → 1.00,
|
|
2494
|
+
* 而 MySQL 的 DECIMAL(10,2) 是 1.01)。
|
|
2495
|
+
*
|
|
2496
|
+
* 这里把数字转成十进制字符串做定点运算:移位取整 → 依据被丢弃的
|
|
2497
|
+
* 第一位小数决定进位(>= 5 进位)→ 还原成 number。
|
|
2498
|
+
*/
|
|
2499
|
+
function roundDecimalToScale(value, scale) {
|
|
2500
|
+
if (!Number.isFinite(value)) return value;
|
|
2501
|
+
const s = String(value);
|
|
2502
|
+
const neg = s.startsWith('-');
|
|
2503
|
+
const body = neg ? s.slice(1) : s;
|
|
2504
|
+
const dot = body.indexOf('.');
|
|
2505
|
+
const intPart = dot >= 0 ? body.slice(0, dot) : body;
|
|
2506
|
+
const fracPart = dot >= 0 ? body.slice(dot + 1) : '';
|
|
2507
|
+
if (!/^\d*$/.test(intPart)) return value; // 指数形式等不处理
|
|
2508
|
+
const keep = fracPart.slice(0, scale).padEnd(scale, '0');
|
|
2509
|
+
const nextDigit = fracPart.charCodeAt(scale);
|
|
2510
|
+
// 决定进位:被丢弃部分的第一个数字 >= '5' → 进位(半远离零)
|
|
2511
|
+
let roundedInt = BigInt(intPart || '0');
|
|
2512
|
+
const keepNum = BigInt(keep || '0');
|
|
2513
|
+
if (scale > 0 && (nextDigit >= 53 /* '5' */ )) {
|
|
2514
|
+
roundedInt = roundedInt * 10n ** BigInt(scale) + keepNum + 1n;
|
|
2515
|
+
} else {
|
|
2516
|
+
roundedInt = roundedInt * 10n ** BigInt(scale) + keepNum;
|
|
2517
|
+
}
|
|
2518
|
+
// 还原成定点字符串
|
|
2519
|
+
let out;
|
|
2520
|
+
if (scale === 0) {
|
|
2521
|
+
out = roundedInt.toString();
|
|
2522
|
+
} else {
|
|
2523
|
+
const digits = roundedInt.toString().padStart(scale + 1, '0');
|
|
2524
|
+
const ip = digits.slice(0, digits.length - scale);
|
|
2525
|
+
const fp = digits.slice(digits.length - scale);
|
|
2526
|
+
out = ip + '.' + fp;
|
|
2527
|
+
}
|
|
2528
|
+
const n = Number(out);
|
|
2529
|
+
return neg && n !== 0 ? -n : n;
|
|
2530
|
+
}
|
|
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
|
+
|
|
2415
2558
|
module.exports = Table;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsql-neo",
|
|
3
|
-
"version": "6.8.
|
|
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",
|
|
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",
|
|
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
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/* ---- 回归:#255 / #256 / #257 / #266 / #270 ---- */
|
|
2
|
+
/*
|
|
3
|
+
* #255 Mongo insert 忽略 ordered 标志:ordered:true 遇重复键后仍继续插入后续文档。
|
|
4
|
+
* #256 FROM_UNIXTIME(NULL) 返回 1970-01-01 00:00:00 而非 NULL(NULL 未传播)。
|
|
5
|
+
* #257 UNIX_TIMESTAMP(NULL) 返回当前时间戳而非 NULL(无参分支被误用)。
|
|
6
|
+
* #266 列类型 DECIMAL(p,s) 的标度在写入时完全不生效。
|
|
7
|
+
* #270 向 INT 列写 1.9 直接抛 1292,MySQL 会四舍五入成 2。
|
|
8
|
+
*/
|
|
9
|
+
const path = require('path');
|
|
10
|
+
const net = require('net');
|
|
11
|
+
const ROOT = path.join(__dirname, '..');
|
|
12
|
+
const { Database, executeSQL } = require(path.join(ROOT, 'index.js'));
|
|
13
|
+
const { createMongoServer, bsonDocument, bsonReadDocument } = require(path.join(ROOT, 'lib/mongo_server.js'));
|
|
14
|
+
|
|
15
|
+
let pass = 0, fail = 0;
|
|
16
|
+
function ok(name, cond, got) {
|
|
17
|
+
if (cond) { pass++; console.log('[OK] ' + name); }
|
|
18
|
+
else { fail++; console.log('[FAIL] ' + name + ' -> ' + JSON.stringify(got)); }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function mongoOpMsg(doc) {
|
|
22
|
+
const p = bsonDocument(doc);
|
|
23
|
+
const h = Buffer.alloc(16);
|
|
24
|
+
h.writeInt32LE(16 + 4 + 1 + p.length, 0);
|
|
25
|
+
h.writeInt32LE(1, 4); h.writeInt32LE(0, 8); h.writeInt32LE(2013, 12);
|
|
26
|
+
return Buffer.concat([h, Buffer.alloc(4), Buffer.from([0]), p]);
|
|
27
|
+
}
|
|
28
|
+
function mongoCall(srv, doc) {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
const sock = net.connect(srv._server.address().port, '127.0.0.1', () => sock.write(mongoOpMsg(doc)));
|
|
31
|
+
const chunks = [];
|
|
32
|
+
sock.on('error', reject);
|
|
33
|
+
sock.on('data', (c) => {
|
|
34
|
+
chunks.push(c);
|
|
35
|
+
const buf = Buffer.concat(chunks);
|
|
36
|
+
if (buf.length >= 16 && buf.length >= buf.readInt32LE(0)) {
|
|
37
|
+
try { resolve(bsonReadDocument(buf.slice(21), 0).value); }
|
|
38
|
+
catch (e) { reject(e); }
|
|
39
|
+
sock.end();
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
(async () => {
|
|
46
|
+
const db = new Database(':memory:', { autoSave: false });
|
|
47
|
+
const q = async (sql) => (await executeSQL(db, sql, { safety: false })).rows;
|
|
48
|
+
const one = async (sql) => (await q(sql))[0][0];
|
|
49
|
+
|
|
50
|
+
// ================= #255 Mongo ordered =================
|
|
51
|
+
{
|
|
52
|
+
const srv = createMongoServer({ dataDir: ':memory:' });
|
|
53
|
+
await new Promise((r) => srv.listen(r));
|
|
54
|
+
await mongoCall(srv, { insert: 'od', documents: [{ _id: 'a', v: 1 }], $db: 'test' });
|
|
55
|
+
const rep = await mongoCall(srv, {
|
|
56
|
+
insert: 'od', ordered: true,
|
|
57
|
+
documents: [{ _id: 'b', v: 2 }, { _id: 'a', v: 9 }, { _id: 'c', v: 3 }],
|
|
58
|
+
$db: 'test',
|
|
59
|
+
});
|
|
60
|
+
const all = await mongoCall(srv, { find: 'od', filter: {}, $db: 'test' });
|
|
61
|
+
const ids = (all.cursor.firstBatch || []).map(x => x._id);
|
|
62
|
+
ok('ordered:true 冲突后不再插入后续文档', ids.length === 2 && !ids.includes('c'), ids);
|
|
63
|
+
ok('ordered:true 回复 n 只统计实际写入数', rep.n === 1, rep.n);
|
|
64
|
+
ok('ordered:true 仍报告 writeErrors', rep.writeErrors && rep.writeErrors.length === 1, rep.writeErrors);
|
|
65
|
+
|
|
66
|
+
// ordered:false 应继续逐条尝试
|
|
67
|
+
await mongoCall(srv, { insert: 'od2', documents: [{ _id: 'x', v: 1 }], $db: 'test' });
|
|
68
|
+
const rep2 = await mongoCall(srv, {
|
|
69
|
+
insert: 'od2', ordered: false,
|
|
70
|
+
documents: [{ _id: 'x', v: 2 }, { _id: 'y', v: 3 }],
|
|
71
|
+
$db: 'test',
|
|
72
|
+
});
|
|
73
|
+
const all2 = await mongoCall(srv, { find: 'od2', filter: {}, $db: 'test' });
|
|
74
|
+
const ids2 = (all2.cursor.firstBatch || []).map(x => x._id);
|
|
75
|
+
ok('ordered:false 冲突后仍继续插入后续文档',
|
|
76
|
+
ids2.includes('x') && ids2.includes('y'), ids2);
|
|
77
|
+
// 本批 2 条:'x' 重复失败、'y' 成功 → n = 1
|
|
78
|
+
ok('ordered:false 回复 n 统计本批成功数', rep2.n === 1, rep2.n);
|
|
79
|
+
ok('ordered:false 仍报告 writeErrors',
|
|
80
|
+
rep2.writeErrors && rep2.writeErrors.length === 1, rep2.writeErrors);
|
|
81
|
+
srv.close();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ================= #256 FROM_UNIXTIME NULL =================
|
|
85
|
+
ok('FROM_UNIXTIME(NULL) = NULL', await one('SELECT FROM_UNIXTIME(NULL)') === null, null);
|
|
86
|
+
ok('FROM_UNIXTIME(NULL) IS NULL 为真',
|
|
87
|
+
await one('SELECT FROM_UNIXTIME(NULL) IS NULL') === 1, null);
|
|
88
|
+
ok('FROM_UNIXTIME(0) 仍是 1970-01-01 00:00:00(不回归)',
|
|
89
|
+
await one('SELECT FROM_UNIXTIME(0)') === '1970-01-01 00:00:00', null);
|
|
90
|
+
ok('FROM_UNIXTIME(NULL, "%Y") 也是 NULL',
|
|
91
|
+
await one("SELECT FROM_UNIXTIME(NULL, '%Y')") === null, null);
|
|
92
|
+
|
|
93
|
+
// ================= #257 UNIX_TIMESTAMP NULL =================
|
|
94
|
+
ok('UNIX_TIMESTAMP(NULL) = NULL', await one('SELECT UNIX_TIMESTAMP(NULL)') === null, null);
|
|
95
|
+
ok('UNIX_TIMESTAMP(NULL) IS NULL 为真',
|
|
96
|
+
await one('SELECT UNIX_TIMESTAMP(NULL) IS NULL') === 1, null);
|
|
97
|
+
const nowTs = await one('SELECT UNIX_TIMESTAMP()');
|
|
98
|
+
ok('UNIX_TIMESTAMP() 无参仍返回当前时间戳',
|
|
99
|
+
typeof nowTs === 'number' && Math.abs(nowTs - Math.floor(Date.now() / 1000)) < 5, nowTs);
|
|
100
|
+
ok("UNIX_TIMESTAMP('1970-01-01 00:00:00') = 0(不回归)",
|
|
101
|
+
await one("SELECT UNIX_TIMESTAMP('1970-01-01 00:00:00')") === 0, null);
|
|
102
|
+
|
|
103
|
+
// ================= #266 DECIMAL 标度 =================
|
|
104
|
+
await q('CREATE TABLE dc (id INT PRIMARY KEY, d DECIMAL(10,2))');
|
|
105
|
+
await q('INSERT INTO dc VALUES (1, 1.005)');
|
|
106
|
+
await q('INSERT INTO dc VALUES (2, 123.456)');
|
|
107
|
+
await q('INSERT INTO dc VALUES (3, 2.675)');
|
|
108
|
+
const dcRows = await q('SELECT id, d FROM dc ORDER BY id');
|
|
109
|
+
ok('DECIMAL(10,2) 把 1.005 舍入成 1.01', dcRows[0][1] === 1.01, dcRows[0]);
|
|
110
|
+
ok('DECIMAL(10,2) 把 123.456 舍入成 123.46', dcRows[1][1] === 123.46, dcRows[1]);
|
|
111
|
+
ok('DECIMAL(10,2) 把 2.675 舍入成 2.68(半远离零,无浮点误差)', dcRows[2][1] === 2.68, dcRows[2]);
|
|
112
|
+
const mul = await q('SELECT d * 100 FROM dc ORDER BY id');
|
|
113
|
+
ok('DECIMAL 乘法无浮点误差(101 / 12346 / 268)',
|
|
114
|
+
JSON.stringify(mul.map(r => r[0])) === JSON.stringify([101, 12346, 268]), mul);
|
|
115
|
+
// 不回归:普通 number 列不受影响
|
|
116
|
+
await q('CREATE TABLE nf (id INT PRIMARY KEY, n FLOAT)');
|
|
117
|
+
await q('INSERT INTO nf VALUES (1, 1.005)');
|
|
118
|
+
ok('FLOAT 列不受 DECIMAL 标度影响',
|
|
119
|
+
(await q('SELECT n FROM nf'))[0][0] === 1.005, await q('SELECT n FROM nf'));
|
|
120
|
+
|
|
121
|
+
// ================= #270 向 INT 列写小数 =================
|
|
122
|
+
await q('CREATE TABLE n (id INT PRIMARY KEY, i INT)');
|
|
123
|
+
await q('INSERT INTO n VALUES (1, 1.9)');
|
|
124
|
+
await q('INSERT INTO n VALUES (2, 1.4)');
|
|
125
|
+
await q('INSERT INTO n VALUES (3, -1.5)');
|
|
126
|
+
const intRows = await q('SELECT id, i FROM n ORDER BY id');
|
|
127
|
+
ok('向 INT 列写 1.9 舍入成 2', intRows[0][1] === 2, intRows[0]);
|
|
128
|
+
ok('向 INT 列写 1.4 舍入成 1', intRows[1][1] === 1, intRows[1]);
|
|
129
|
+
ok('向 INT 列写 -1.5 舍入成 -2(半远离零)', intRows[2][1] === -2, intRows[2]);
|
|
130
|
+
// 不回归:整数值与字符串整数字面量
|
|
131
|
+
await q('INSERT INTO n VALUES (4, 7)');
|
|
132
|
+
await q("INSERT INTO n VALUES (5, '8')");
|
|
133
|
+
ok('整数值不回归', (await q('SELECT i FROM n WHERE id=4'))[0][0] === 7, null);
|
|
134
|
+
ok('整数字符串不回归', (await q('SELECT i FROM n WHERE id=5'))[0][0] === 8, null);
|
|
135
|
+
// 不回归:非数字字符串仍报错
|
|
136
|
+
let strErr = null;
|
|
137
|
+
try { await q("INSERT INTO n VALUES (6, 'abc')"); } catch (e) { strErr = e; }
|
|
138
|
+
ok('非数字字符串写 INT 列仍报 1292', !!strErr, strErr && strErr.message);
|
|
139
|
+
|
|
140
|
+
console.log(`\n${pass} passed, ${fail} failed`);
|
|
141
|
+
process.exit(fail === 0 ? 0 : 1);
|
|
142
|
+
})().catch(e => {
|
|
143
|
+
console.log('[FAIL] unexpected exception: ' + (e.stack || e).split('\n').slice(0, 4).join('\n'));
|
|
144
|
+
process.exit(1);
|
|
145
|
+
});
|
package/test/issue-38.test.js
CHANGED
|
@@ -84,9 +84,12 @@ async function testApi() {
|
|
|
84
84
|
ok('#38 API filter with BigInt matches', hit.length === 1 && hit[0].id === r1.id, hit);
|
|
85
85
|
|
|
86
86
|
// 8. 非法值仍然报错
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
// #270:MySQL 向整型列写带小数部分的值是**四舍五入**而不是报错
|
|
88
|
+
// (1.5 → 2)。这里改为验证舍入结果;布尔仍是非法值。
|
|
89
|
+
let floatRow = null;
|
|
90
|
+
try { floatRow = t.insert({ v: 1.5 }); } catch (e) { floatRow = null; }
|
|
91
|
+
ok('#38 API float into bigint column rounds half away from zero',
|
|
92
|
+
floatRow && Number(floatRow.v) === 2, floatRow);
|
|
90
93
|
threw = null;
|
|
91
94
|
try { t.insert({ v: true }); } catch (e) { threw = e; }
|
|
92
95
|
ok('#38 API boolean into bigint column throws 1292', threw && codeOf(threw) === 1292);
|
|
@@ -258,8 +261,10 @@ async function testNoRegression() {
|
|
|
258
261
|
const rows = db.t.find({});
|
|
259
262
|
ok('#38 regression integer/float/string coercion intact',
|
|
260
263
|
rows.length === 2 && rows[0].n === 42 && rows[1].n === 43 && rows[1].f === 2.5 && rows[1].s === '7', rows);
|
|
261
|
-
|
|
262
|
-
|
|
264
|
+
// #270:MySQL 语义 —— 整数列对带小数部分的值做四舍五入(1.5 → 2),
|
|
265
|
+
// 不再报 1292。
|
|
266
|
+
ok('#38 regression integer rounds 1.5 to 2', (() => {
|
|
267
|
+
try { const r = db.t.insert({ n: 1.5 }); return r.n === 2; } catch (e) { return false; }
|
|
263
268
|
})());
|
|
264
269
|
}
|
|
265
270
|
|