jsql-neo 6.8.16 → 6.8.18
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 +12 -6
- package/lib/mysql_compat.js +19 -0
- package/lib/pg_server.js +10 -4
- package/lib/redis_server.js +13 -7
- package/lib/sql.js +31 -10
- package/lib/table.js +103 -20
- package/package.json +3 -3
- package/test/issue-253-269.test.js +135 -0
- package/test/issue-280-310.test.js +77 -0
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.18** — 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
|
@@ -885,10 +885,12 @@ class MongoServer {
|
|
|
885
885
|
const key = dbName + '.' + coll;
|
|
886
886
|
const list = this._indexesByColl.get(key) || [{ name: '_id_', columns: ['_id'], unique: true }];
|
|
887
887
|
for (const spec of (doc.indexes || [])) {
|
|
888
|
-
const
|
|
888
|
+
const keySpec = spec.key || {};
|
|
889
|
+
const columns = Object.keys(keySpec);
|
|
889
890
|
if (columns.length === 0) continue;
|
|
890
891
|
const name = spec.name || columns.map(c => c + '_1').join('_');
|
|
891
|
-
|
|
892
|
+
// #281:保留方向({a:-1}),listIndexes 原样回包
|
|
893
|
+
const entry = { name, columns, keySpec, unique: !!spec.unique, sparse: !!spec.sparse };
|
|
892
894
|
const at = list.findIndex(i => i.name === name);
|
|
893
895
|
if (at >= 0) list[at] = entry; else list.push(entry);
|
|
894
896
|
}
|
|
@@ -908,7 +910,7 @@ class MongoServer {
|
|
|
908
910
|
const list = this._indexesByColl.get(key) || [{ name: '_id_', columns: ['_id'], unique: true }];
|
|
909
911
|
const ns = `${dbName}.${coll}`;
|
|
910
912
|
const firstBatch = list.map(i => ({
|
|
911
|
-
v: 2, key: Object.fromEntries(i.columns.map(c => [c, 1])), name: i.name, ns,
|
|
913
|
+
v: 2, key: i.keySpec || Object.fromEntries(i.columns.map(c => [c, 1])), name: i.name, ns,
|
|
912
914
|
}));
|
|
913
915
|
return { ok: 1, cursor: { id: { $long: 0 }, ns, firstBatch } };
|
|
914
916
|
}
|
|
@@ -920,7 +922,7 @@ class MongoServer {
|
|
|
920
922
|
if (spec === '*') {
|
|
921
923
|
this._indexesByColl.set(key, [{ name: '_id_', columns: ['_id'], unique: true }]);
|
|
922
924
|
} else {
|
|
923
|
-
const name = typeof spec === 'string' ? spec : Object.keys(spec || {}).map(c => c + '
|
|
925
|
+
const name = typeof spec === 'string' ? spec : Object.keys(spec || {}).map(c => c + '_' + (spec[c] < 0 ? '-1' : '1')).join('_');
|
|
924
926
|
this._indexesByColl.set(key, list.filter(i => i.name !== name));
|
|
925
927
|
}
|
|
926
928
|
return { ok: 1, nIndexesWas: list.length };
|
|
@@ -928,8 +930,12 @@ class MongoServer {
|
|
|
928
930
|
case 'count': {
|
|
929
931
|
const coll = String(doc.count);
|
|
930
932
|
const engine = await this._ensureCollection(coll, this._dbOf(doc));
|
|
931
|
-
|
|
932
|
-
|
|
933
|
+
let rows = await this._matching(engine, coll, doc.query);
|
|
934
|
+
// #280:count 也要尊重 skip
|
|
935
|
+
const skip = typeof doc.skip === 'number' && doc.skip > 0 ? Math.trunc(doc.skip) : 0;
|
|
936
|
+
if (skip > 0) rows = rows.slice(skip);
|
|
937
|
+
const limit = Number(doc.limit) > 0 ? Math.trunc(Number(doc.limit)) : 0;
|
|
938
|
+
return { ok: 1, n: limit > 0 ? Math.min(rows.length, limit) : rows.length };
|
|
933
939
|
}
|
|
934
940
|
case 'aggregate': {
|
|
935
941
|
const coll = String(doc.aggregate);
|
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
|
@@ -436,7 +436,7 @@ class PgConnection {
|
|
|
436
436
|
const password = this.server._userPassword(this.user);
|
|
437
437
|
if (password === undefined) {
|
|
438
438
|
// 用户不存在
|
|
439
|
-
this._send(buildErrorResponse('
|
|
439
|
+
this._send(buildErrorResponse('28P01', `password authentication failed for user "${this.user}"`));
|
|
440
440
|
this.socket.end();
|
|
441
441
|
return;
|
|
442
442
|
}
|
|
@@ -493,7 +493,7 @@ class PgConnection {
|
|
|
493
493
|
this._send(frame(0x52, sf.build())); // 'R'
|
|
494
494
|
this._completeAuth();
|
|
495
495
|
} catch (e) {
|
|
496
|
-
this._send(buildErrorResponse('
|
|
496
|
+
this._send(buildErrorResponse('28P01', 'password authentication failed for user "' + this.user + '"'));
|
|
497
497
|
this.socket.end();
|
|
498
498
|
}
|
|
499
499
|
}
|
|
@@ -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/redis_server.js
CHANGED
|
@@ -280,6 +280,10 @@ class RedisServer {
|
|
|
280
280
|
if ((opt === 'EX' || opt === 'PX' || opt === 'NX' || opt === 'XX') && seen.has(opt)) {
|
|
281
281
|
throw new Error('ERR syntax error');
|
|
282
282
|
}
|
|
283
|
+
// #286:EX 与 PX 互斥
|
|
284
|
+
if ((opt === 'EX' && seen.has('PX')) || (opt === 'PX' && seen.has('EX'))) {
|
|
285
|
+
throw new Error('ERR syntax error');
|
|
286
|
+
}
|
|
283
287
|
seen.add(opt);
|
|
284
288
|
if (opt === 'EX') {
|
|
285
289
|
const raw = String(args[++i]).trim();
|
|
@@ -625,12 +629,12 @@ class RedisServer {
|
|
|
625
629
|
const rest = args.slice(1);
|
|
626
630
|
if (rest.length % 2 !== 0) throw new Error('ERR syntax error');
|
|
627
631
|
// #174:先完整校验所有 (score, member),任一非法则整条命令不产生变更
|
|
628
|
-
const zset = v ?
|
|
632
|
+
const zset = v ? Object.assign(Object.create(null), v.val) : Object.create(null);
|
|
629
633
|
let added = 0;
|
|
630
634
|
for (let i = 0; i < rest.length; i += 2) {
|
|
631
635
|
const score = parseRedisFloat(rest[i]);
|
|
632
636
|
const member = rest[i + 1];
|
|
633
|
-
if (!(member
|
|
637
|
+
if (!Object.prototype.hasOwnProperty.call(zset, member)) added++;
|
|
634
638
|
zset[member] = score;
|
|
635
639
|
}
|
|
636
640
|
this.db.set(key, { type: 'zset', val: zset, ttl: v ? v.ttl : null });
|
|
@@ -674,7 +678,7 @@ class RedisServer {
|
|
|
674
678
|
case 'ZSCORE': {
|
|
675
679
|
const v = this._get(args[0]);
|
|
676
680
|
if (v && v.type !== 'zset') throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
|
677
|
-
if (!v || !(args[1]
|
|
681
|
+
if (!v || !Object.prototype.hasOwnProperty.call(v.val, args[1])) return null;
|
|
678
682
|
// ZSCORE 的分数恒为 bulk string(含整数分数),不能落进 integer reply(#67)
|
|
679
683
|
return String(v.val[args[1]]);
|
|
680
684
|
}
|
|
@@ -690,7 +694,7 @@ class RedisServer {
|
|
|
690
694
|
if (v.type !== 'zset') throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
|
691
695
|
let n = 0;
|
|
692
696
|
for (const m of args.slice(1)) {
|
|
693
|
-
if (
|
|
697
|
+
if (Object.prototype.hasOwnProperty.call(v.val, m)) { delete v.val[m]; n++; }
|
|
694
698
|
}
|
|
695
699
|
// #173:最后一个成员删除后键也要删除
|
|
696
700
|
if (Object.keys(v.val).length === 0) this.db.delete(args[0]);
|
|
@@ -702,11 +706,13 @@ class RedisServer {
|
|
|
702
706
|
const inc = parseRedisFloat(args[1]);
|
|
703
707
|
const v = this._get(key);
|
|
704
708
|
let zset;
|
|
705
|
-
if (v && v.type === 'zset') zset = v.val;
|
|
709
|
+
if (v && v.type === 'zset') zset = Object.assign(Object.create(null), v.val);
|
|
706
710
|
else if (v) throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
|
707
|
-
else
|
|
711
|
+
else zset = Object.create(null);
|
|
708
712
|
const member = args[2];
|
|
709
|
-
|
|
713
|
+
const prev = Object.prototype.hasOwnProperty.call(zset, member) ? zset[member] : 0;
|
|
714
|
+
zset[member] = prev + inc;
|
|
715
|
+
this.db.set(key, { type: 'zset', val: zset, ttl: v ? v.ttl : null });
|
|
710
716
|
this._scheduleSnapshot();
|
|
711
717
|
// 同 ZSCORE:分数用 bulk string 回复(#67)
|
|
712
718
|
return String(zset[member]);
|
package/lib/sql.js
CHANGED
|
@@ -6813,13 +6813,16 @@ class SQLExecutor {
|
|
|
6813
6813
|
return out;
|
|
6814
6814
|
};
|
|
6815
6815
|
if (statement.dataRows) {
|
|
6816
|
-
|
|
6817
|
-
|
|
6818
|
-
|
|
6819
|
-
|
|
6820
|
-
|
|
6821
|
-
|
|
6822
|
-
|
|
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
|
-
|
|
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
|
-
|
|
7089
|
-
|
|
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
|
-
|
|
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
|
|
|
@@ -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.
|
|
3
|
+
"version": "6.8.18",
|
|
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 && node test/issue-280-310.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 && node test/issue-280-310.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,77 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Regression tests for #280 #281 #286 #287 #310.
|
|
3
|
+
* node test/issue-280-310.test.js
|
|
4
|
+
*/
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const ROOT = path.join(__dirname, '..');
|
|
7
|
+
const { createRedisServer } = require(path.join(ROOT, 'lib', 'redis_server.js'));
|
|
8
|
+
const { createMongoServer } = require(path.join(ROOT, 'lib', 'mongo_server.js'));
|
|
9
|
+
|
|
10
|
+
let passed = 0, failed = 0;
|
|
11
|
+
function ok(name, cond, extra) {
|
|
12
|
+
if (cond) { passed++; console.log('[OK]', name); }
|
|
13
|
+
else { failed++; console.log('[FAIL]', name, extra !== undefined ? '→ ' + JSON.stringify(extra) : ''); }
|
|
14
|
+
}
|
|
15
|
+
function call(s, cmd, ...args) {
|
|
16
|
+
try { return { value: s.execute(cmd, args) }; } catch (e) { return { error: e.message }; }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
(async () => {
|
|
20
|
+
// #286
|
|
21
|
+
{
|
|
22
|
+
const s = createRedisServer({ port: 0, dataDir: null });
|
|
23
|
+
const r = call(s, 'SET', 'k', 'v', 'EX', '10', 'PX', '5000');
|
|
24
|
+
ok('#286 SET EX+PX is a syntax error', /syntax error/.test(r.error || ''), r);
|
|
25
|
+
}
|
|
26
|
+
// #287
|
|
27
|
+
{
|
|
28
|
+
const s = createRedisServer({ port: 0, dataDir: null });
|
|
29
|
+
const a = call(s, 'ZADD', 'z', 1, '__proto__');
|
|
30
|
+
const b = call(s, 'ZADD', 'z', 2, 'constructor');
|
|
31
|
+
const c = call(s, 'ZADD', 'z', 3, '__proto__');
|
|
32
|
+
ok('#287 ZADD prototype names count as members', a.value === 1 && b.value === 1 && c.value === 0, { a, b, c });
|
|
33
|
+
const score = call(s, 'ZSCORE', 'z', '__proto__');
|
|
34
|
+
ok('#287 ZSCORE finds __proto__ member', score.value === '3', score);
|
|
35
|
+
const rem = call(s, 'ZREM', 'z', '__proto__');
|
|
36
|
+
ok('#287 ZREM removes prototype-named member', rem.value === 1, rem);
|
|
37
|
+
const left = call(s, 'ZRANGE', 'z', 0, -1);
|
|
38
|
+
ok('#287 ZSET contents intact', JSON.stringify(left.value) === JSON.stringify(['constructor']), left.value);
|
|
39
|
+
}
|
|
40
|
+
// #280 / #281
|
|
41
|
+
{
|
|
42
|
+
const mongo = createMongoServer({ dataDir: ':memory:' });
|
|
43
|
+
const dispatch = (cmd, doc) => mongo._dispatch(cmd, doc);
|
|
44
|
+
await dispatch('insert', {
|
|
45
|
+
insert: 'c', $db: 'test',
|
|
46
|
+
documents: [{ n: 1 }, { n: 2 }, { n: 3 }, { n: 4 }],
|
|
47
|
+
});
|
|
48
|
+
const count = await dispatch('count', { count: 'c', query: {}, skip: 2, limit: 1, $db: 'test' });
|
|
49
|
+
ok('#280 count honors skip then limit', count.n === 1, count);
|
|
50
|
+
|
|
51
|
+
await dispatch('createIndexes', { createIndexes: 'c', indexes: [{ key: { n: -1 }, name: 'n_-1', unique: false }], $db: 'test' });
|
|
52
|
+
const idx = await dispatch('listIndexes', { listIndexes: 'c', $db: 'test' });
|
|
53
|
+
const found = (idx.cursor && idx.cursor.firstBatch || []).find(i => i.name === 'n_-1');
|
|
54
|
+
ok('#281 listIndexes keeps index direction', found && found.key && found.key.n === -1, found);
|
|
55
|
+
}
|
|
56
|
+
// #310
|
|
57
|
+
{
|
|
58
|
+
const { Client } = (() => { try { return require(path.join(ROOT, 'node_modules', 'pg')); } catch (e) { return {}; } })();
|
|
59
|
+
if (Client) {
|
|
60
|
+
const { createPgServer } = require(path.join(ROOT, 'lib', 'pg_server.js'));
|
|
61
|
+
const srv = createPgServer({
|
|
62
|
+
port: 0, host: '127.0.0.1', dataDir: ':memory:',
|
|
63
|
+
auth: { root: { password: 'right', databases: ['app'] } },
|
|
64
|
+
});
|
|
65
|
+
await new Promise((r) => srv.listen(r));
|
|
66
|
+
const c = new Client({ host: '127.0.0.1', port: srv.address.port, user: 'root', database: 'app', password: 'wrong' });
|
|
67
|
+
let code = null;
|
|
68
|
+
try { await c.connect(); await c.end(); } catch (e) { code = e.code; }
|
|
69
|
+
ok('#310 wrong password returns 28P01', code === '28P01', code);
|
|
70
|
+
srv.close();
|
|
71
|
+
} else {
|
|
72
|
+
console.log('[SKIP] PG auth (pg not installed)');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
console.log(`\n${passed} passed, ${failed} failed`);
|
|
76
|
+
process.exit(failed === 0 ? 0 : 1);
|
|
77
|
+
})().catch((e) => { console.error('FATAL', e); process.exit(1); });
|