jsql-neo 6.8.4 → 6.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/bin/jsql +5 -2
- package/index.js +1 -1
- package/lib/bigint_json.js +12 -0
- package/lib/cache.js +20 -4
- package/lib/columnar_fast.js +3 -1
- package/lib/database.js +171 -47
- package/lib/date-types.js +4 -4
- package/lib/errors.js +4 -0
- package/lib/migrate.js +20 -8
- package/lib/mongo_server.js +238 -20
- package/lib/mysql_server.js +27 -22
- package/lib/nedb_compat.js +101 -11
- package/lib/pg_server.js +53 -12
- package/lib/redis_server.js +192 -67
- package/lib/sql.js +212 -35
- package/lib/sqlite_compat.js +17 -0
- package/lib/table.js +55 -12
- package/package.json +3 -3
- package/test/extra-coverage.test.js +4 -3
- package/test/issue-162-179.test.js +220 -0
- package/test/issue-181-186.test.js +153 -0
- package/test/issue-187.test.js +36 -0
- package/test/issue-188-237.test.js +481 -0
- package/test/issue-63-68.test.js +2 -2
package/lib/mongo_server.js
CHANGED
|
@@ -160,6 +160,35 @@ function cloneDoc(v) {
|
|
|
160
160
|
return v;
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
+
/** #220:BSON 类型判别(数组/null/object 不能靠 typeof) */
|
|
164
|
+
function bsonTypeOf(v) {
|
|
165
|
+
if (v === null || v === undefined) return 'null';
|
|
166
|
+
if (Array.isArray(v)) return 'array';
|
|
167
|
+
if (v instanceof ObjectId) return 'objectId';
|
|
168
|
+
if (v instanceof Date) return 'date';
|
|
169
|
+
if (typeof v === 'boolean') return 'bool';
|
|
170
|
+
if (typeof v === 'number') return 'number';
|
|
171
|
+
if (typeof v === 'string') return 'string';
|
|
172
|
+
if (typeof v === 'object') return 'object';
|
|
173
|
+
return typeof v;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** #219:子文档深相等(数组有序、ObjectId 按字节) */
|
|
177
|
+
function deepEqualMongo(a, b) {
|
|
178
|
+
if (a === b) return true;
|
|
179
|
+
if (a instanceof ObjectId || b instanceof ObjectId) return valuesEqual(a, b);
|
|
180
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
181
|
+
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false;
|
|
182
|
+
return a.every((x, i) => deepEqualMongo(x, b[i]));
|
|
183
|
+
}
|
|
184
|
+
if (a && b && typeof a === 'object' && typeof b === 'object') {
|
|
185
|
+
const ka = Object.keys(a), kb = Object.keys(b);
|
|
186
|
+
if (ka.length !== kb.length) return false;
|
|
187
|
+
return ka.every(k => Object.prototype.hasOwnProperty.call(b, k) && deepEqualMongo(a[k], b[k]));
|
|
188
|
+
}
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
|
|
163
192
|
function applyProjection(doc, spec) {
|
|
164
193
|
if (!spec || typeof spec !== 'object') return doc;
|
|
165
194
|
const entries = Object.entries(spec);
|
|
@@ -257,6 +286,8 @@ class MongoServer {
|
|
|
257
286
|
this._sockets = new Set();
|
|
258
287
|
this._dbCollections = new Map(); // db name → Set of collection names
|
|
259
288
|
this._engineByDb = new Map(); // db name → Database (standalone 模式)
|
|
289
|
+
this._indexesByColl = new Map(); // "db.coll" → [{ name, columns, unique }]
|
|
290
|
+
this._autoCollections = new Set(); // db.collection 由 Mongo 自动建表(可继续补列)
|
|
260
291
|
}
|
|
261
292
|
|
|
262
293
|
async _getEngine(db = null) {
|
|
@@ -284,10 +315,50 @@ class MongoServer {
|
|
|
284
315
|
return new Database(':memory:', { autoSave: false });
|
|
285
316
|
}
|
|
286
317
|
|
|
287
|
-
|
|
318
|
+
_inferSchema(docs) {
|
|
319
|
+
const defs = {};
|
|
320
|
+
const list = Array.isArray(docs) ? docs : (docs ? [docs] : []);
|
|
321
|
+
for (const doc of list) {
|
|
322
|
+
if (!doc || typeof doc !== 'object' || Array.isArray(doc)) continue;
|
|
323
|
+
for (const [field, value] of Object.entries(doc)) {
|
|
324
|
+
if (value === null || value === undefined || defs[field]) continue;
|
|
325
|
+
let type = 'string';
|
|
326
|
+
if (typeof value === 'number') type = Number.isInteger(value) ? 'integer' : 'number';
|
|
327
|
+
else if (typeof value === 'boolean') type = 'boolean';
|
|
328
|
+
else if (Array.isArray(value)) type = 'array';
|
|
329
|
+
// Date / Buffer / ObjectId / 嵌套对象一律按 object 透传,避免改写类型
|
|
330
|
+
else if (typeof value === 'object') type = 'object';
|
|
331
|
+
defs[field] = { type };
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return defs;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
async _ensureCollection(name, db = null, sample = null) {
|
|
288
338
|
const engine = await this._getEngine(db);
|
|
289
|
-
|
|
290
|
-
|
|
339
|
+
const key = (db || 'default') + '.' + name;
|
|
340
|
+
const schema = engine.getTableSchema(name);
|
|
341
|
+
const defs = this._inferSchema(sample);
|
|
342
|
+
if (!schema) {
|
|
343
|
+
engine.createTable(name, defs);
|
|
344
|
+
this._autoCollections.add(key);
|
|
345
|
+
} else if (Object.keys(schema).length === 0) {
|
|
346
|
+
// #178:Mongo 自动建表后 schema 为空,SQL 侧 SELECT * 拿不到列。
|
|
347
|
+
const table = typeof engine._ensureTable === 'function' ? engine._ensureTable(name) : null;
|
|
348
|
+
if (table && typeof table.addColumn === 'function') {
|
|
349
|
+
for (const field of Object.keys(defs)) {
|
|
350
|
+
if (!table._schema[field]) table.addColumn(field, defs[field]);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
this._autoCollections.add(key);
|
|
354
|
+
} else {
|
|
355
|
+
// 文档型数据允许新字段;落到 schema 后落盘才不会丢列
|
|
356
|
+
const table = typeof engine._ensureTable === 'function' ? engine._ensureTable(name) : null;
|
|
357
|
+
if (table && typeof table.addColumn === 'function') {
|
|
358
|
+
for (const field of Object.keys(defs)) {
|
|
359
|
+
if (!table._schema[field]) table.addColumn(field, defs[field]);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
291
362
|
}
|
|
292
363
|
if (db) {
|
|
293
364
|
if (!this._dbCollections.has(db)) this._dbCollections.set(db, new Set());
|
|
@@ -532,8 +603,8 @@ class MongoServer {
|
|
|
532
603
|
}
|
|
533
604
|
case 'insert': {
|
|
534
605
|
const coll = String(doc.insert);
|
|
535
|
-
const engine = await this._ensureCollection(coll, this._dbOf(doc));
|
|
536
606
|
const docs = doc.documents || [];
|
|
607
|
+
const engine = await this._ensureCollection(coll, this._dbOf(doc), docs);
|
|
537
608
|
const ids = [];
|
|
538
609
|
const writeErrors = [];
|
|
539
610
|
for (let i = 0; i < docs.length; i++) {
|
|
@@ -566,6 +637,30 @@ class MongoServer {
|
|
|
566
637
|
continue;
|
|
567
638
|
}
|
|
568
639
|
}
|
|
640
|
+
// #221:二级唯一索引必须真正拦截重复键
|
|
641
|
+
{
|
|
642
|
+
const idxKey = this._dbOf(doc) + '.' + coll;
|
|
643
|
+
const uniqIdx = (this._indexesByColl.get(idxKey) || []).filter(ix => ix.unique && ix.name !== '_id_');
|
|
644
|
+
if (uniqIdx.length > 0) {
|
|
645
|
+
const existingRows = await engine.find(coll, {});
|
|
646
|
+
let dupIdx = null;
|
|
647
|
+
for (const ix of uniqIdx) {
|
|
648
|
+
if (ix.columns.some(c => getPath(doc2, c) === undefined || getPath(doc2, c) === null)) continue;
|
|
649
|
+
const hit = existingRows.some(r => ix.columns.every(c => deepEqualMongo(getPath(r, c), getPath(doc2, c))));
|
|
650
|
+
if (hit) { dupIdx = ix; break; }
|
|
651
|
+
}
|
|
652
|
+
if (dupIdx) {
|
|
653
|
+
const keyValue = Object.fromEntries(dupIdx.columns.map(c => [c, getPath(doc2, c)]));
|
|
654
|
+
writeErrors.push({
|
|
655
|
+
index: i, code: 11000,
|
|
656
|
+
errmsg: `E11000 duplicate key error collection: ${this._dbOf(doc)}.${coll} index: ${dupIdx.name} dup key: ${JSON.stringify(keyValue)}`,
|
|
657
|
+
keyPattern: Object.fromEntries(dupIdx.columns.map(c => [c, 1])),
|
|
658
|
+
keyValue,
|
|
659
|
+
});
|
|
660
|
+
continue;
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
569
664
|
await engine.insert(coll, doc2);
|
|
570
665
|
ids.push(doc2._id);
|
|
571
666
|
}
|
|
@@ -582,19 +677,35 @@ class MongoServer {
|
|
|
582
677
|
rows = sortRows(rows, doc.sort);
|
|
583
678
|
}
|
|
584
679
|
const found = rows[0] || null;
|
|
585
|
-
const
|
|
680
|
+
const updateDoc = doc.update || {};
|
|
681
|
+
const isModifier = Object.keys(updateDoc).some(k => k.startsWith('$'));
|
|
586
682
|
let value = null;
|
|
587
683
|
if (found) {
|
|
588
684
|
if (doc.remove) {
|
|
589
685
|
await this._removeRows(engine, coll, [found]);
|
|
590
686
|
value = found;
|
|
687
|
+
} else if (isModifier) {
|
|
688
|
+
const patched = cloneDoc(found);
|
|
689
|
+
applyModifier(patched, updateDoc, { strict: true });
|
|
690
|
+
await this._patchRows(engine, coll, [found], updateDoc);
|
|
691
|
+
value = doc.new ? patched : found;
|
|
591
692
|
} else {
|
|
592
|
-
|
|
593
|
-
|
|
693
|
+
// #218:无 $ 运算符 = replacement,整体替换而非合并
|
|
694
|
+
const patched = cloneDoc(updateDoc);
|
|
695
|
+
if (found._id !== undefined) patched._id = found._id;
|
|
696
|
+
await this._replaceRows(engine, coll, [found], updateDoc);
|
|
594
697
|
value = doc.new ? patched : found;
|
|
595
698
|
}
|
|
596
699
|
} else if (doc.upsert) {
|
|
597
|
-
|
|
700
|
+
// #224:upsert 时修饰符必须求值,不能把 $inc 当字面字段
|
|
701
|
+
let merged;
|
|
702
|
+
if (isModifier) {
|
|
703
|
+
merged = this._nonDollarQuery(q);
|
|
704
|
+
applyModifier(merged, updateDoc, { strict: true });
|
|
705
|
+
} else {
|
|
706
|
+
merged = cloneDoc(updateDoc);
|
|
707
|
+
}
|
|
708
|
+
if (merged._id === undefined && q._id !== undefined) merged._id = q._id;
|
|
598
709
|
await engine.insert(coll, merged);
|
|
599
710
|
value = doc.new ? { ...merged } : null;
|
|
600
711
|
}
|
|
@@ -608,7 +719,9 @@ class MongoServer {
|
|
|
608
719
|
const seen = new Set();
|
|
609
720
|
const values = [];
|
|
610
721
|
for (const r of rows) {
|
|
611
|
-
|
|
722
|
+
// #216:distinct 支持 dot notation,复用 getPath
|
|
723
|
+
const v = getPath(r, key);
|
|
724
|
+
if (v === undefined) continue;
|
|
612
725
|
// ObjectId 与同 hex String 是不同的值:\0 前缀不会与 JSON.stringify 结果冲突
|
|
613
726
|
const dk = v instanceof ObjectId ? '\0oid:' + v.toString() : JSON.stringify(v);
|
|
614
727
|
if (!seen.has(dk)) { seen.add(dk); values.push(v); }
|
|
@@ -653,11 +766,20 @@ class MongoServer {
|
|
|
653
766
|
try {
|
|
654
767
|
for (const u of updates) {
|
|
655
768
|
const q = u.q || {};
|
|
656
|
-
const
|
|
769
|
+
const updateDoc = u.u || {};
|
|
770
|
+
const isModifier = Object.keys(updateDoc).some(k => k.startsWith('$'));
|
|
657
771
|
const multi = u.multi === true;
|
|
658
772
|
let targets = await this._matching(engine, coll, q);
|
|
659
773
|
if (!targets.length && u.upsert) {
|
|
660
|
-
|
|
774
|
+
// #224:upsert 时对查询等值字段应用修饰符
|
|
775
|
+
let merged;
|
|
776
|
+
if (isModifier) {
|
|
777
|
+
merged = this._nonDollarQuery(q);
|
|
778
|
+
applyModifier(merged, updateDoc, { strict: true });
|
|
779
|
+
} else {
|
|
780
|
+
merged = cloneDoc(updateDoc);
|
|
781
|
+
}
|
|
782
|
+
if (merged._id === undefined && q._id !== undefined) merged._id = q._id;
|
|
661
783
|
await engine.insert(coll, merged);
|
|
662
784
|
n++;
|
|
663
785
|
continue;
|
|
@@ -665,14 +787,16 @@ class MongoServer {
|
|
|
665
787
|
if (multi) {
|
|
666
788
|
// 更新所有匹配的行
|
|
667
789
|
for (const target of targets) {
|
|
668
|
-
await this._patchRows(engine, coll, [target],
|
|
790
|
+
if (isModifier) await this._patchRows(engine, coll, [target], updateDoc);
|
|
791
|
+
else await this._replaceRows(engine, coll, [target], updateDoc);
|
|
669
792
|
n++;
|
|
670
793
|
}
|
|
671
794
|
} else {
|
|
672
795
|
// 只更新第一条
|
|
673
|
-
|
|
796
|
+
const target = targets[0] || null;
|
|
674
797
|
if (target) {
|
|
675
|
-
await this._patchRows(engine, coll, [target],
|
|
798
|
+
if (isModifier) await this._patchRows(engine, coll, [target], updateDoc);
|
|
799
|
+
else await this._replaceRows(engine, coll, [target], updateDoc);
|
|
676
800
|
n++;
|
|
677
801
|
}
|
|
678
802
|
}
|
|
@@ -695,6 +819,48 @@ class MongoServer {
|
|
|
695
819
|
}
|
|
696
820
|
return { ok: 1, n };
|
|
697
821
|
}
|
|
822
|
+
case 'createIndexes': {
|
|
823
|
+
// #221:登记唯一/普通索引定义,供 insert/update 校验与 listIndexes 返回
|
|
824
|
+
const coll = String(doc.createIndexes);
|
|
825
|
+
const dbName = this._dbOf(doc);
|
|
826
|
+
await this._ensureCollection(coll, dbName);
|
|
827
|
+
const key = dbName + '.' + coll;
|
|
828
|
+
const list = this._indexesByColl.get(key) || [{ name: '_id_', columns: ['_id'], unique: true }];
|
|
829
|
+
for (const spec of (doc.indexes || [])) {
|
|
830
|
+
const columns = Object.keys(spec.key || {});
|
|
831
|
+
if (columns.length === 0) continue;
|
|
832
|
+
const name = spec.name || columns.map(c => c + '_1').join('_');
|
|
833
|
+
const entry = { name, columns, unique: !!spec.unique, sparse: !!spec.sparse };
|
|
834
|
+
const at = list.findIndex(i => i.name === name);
|
|
835
|
+
if (at >= 0) list[at] = entry; else list.push(entry);
|
|
836
|
+
}
|
|
837
|
+
this._indexesByColl.set(key, list);
|
|
838
|
+
return { ok: 1, createdCollectionAutomatically: false, numIndexesBefore: 1, numIndexesAfter: list.length };
|
|
839
|
+
}
|
|
840
|
+
case 'listIndexes': {
|
|
841
|
+
const coll = String(doc.listIndexes);
|
|
842
|
+
const dbName = this._dbOf(doc);
|
|
843
|
+
const key = dbName + '.' + coll;
|
|
844
|
+
const list = this._indexesByColl.get(key) || [{ name: '_id_', columns: ['_id'], unique: true }];
|
|
845
|
+
const ns = `${dbName}.${coll}`;
|
|
846
|
+
const firstBatch = list.map(i => ({
|
|
847
|
+
v: 2, key: Object.fromEntries(i.columns.map(c => [c, 1])), name: i.name, ns,
|
|
848
|
+
}));
|
|
849
|
+
return { ok: 1, cursor: { id: { $long: 0 }, ns, firstBatch } };
|
|
850
|
+
}
|
|
851
|
+
case 'dropIndexes': {
|
|
852
|
+
const coll = String(doc.dropIndexes);
|
|
853
|
+
const key = this._dbOf(doc) + '.' + coll;
|
|
854
|
+
const list = this._indexesByColl.get(key) || [];
|
|
855
|
+
const spec = doc.index;
|
|
856
|
+
if (spec === '*') {
|
|
857
|
+
this._indexesByColl.set(key, [{ name: '_id_', columns: ['_id'], unique: true }]);
|
|
858
|
+
} else {
|
|
859
|
+
const name = typeof spec === 'string' ? spec : Object.keys(spec || {}).map(c => c + '_1').join('_');
|
|
860
|
+
this._indexesByColl.set(key, list.filter(i => i.name !== name));
|
|
861
|
+
}
|
|
862
|
+
return { ok: 1, nIndexesWas: list.length };
|
|
863
|
+
}
|
|
698
864
|
case 'count': {
|
|
699
865
|
const coll = String(doc.count);
|
|
700
866
|
const engine = await this._ensureCollection(coll, this._dbOf(doc));
|
|
@@ -852,7 +1018,8 @@ class MongoServer {
|
|
|
852
1018
|
return { ok: 1, cursor: { id: { $long: 0 }, ns: `${this._dbOf(doc)}.${coll}`, firstBatch: rows } };
|
|
853
1019
|
}
|
|
854
1020
|
default:
|
|
855
|
-
|
|
1021
|
+
// #222:未知命令必须按协议返回 CommandNotFound,而不是伪成功
|
|
1022
|
+
return { ok: 0, errmsg: `no such command: ${String(cmd)}`, code: 59 };
|
|
856
1023
|
}
|
|
857
1024
|
}
|
|
858
1025
|
|
|
@@ -889,7 +1056,39 @@ class MongoServer {
|
|
|
889
1056
|
for (const k of Object.keys(r)) if (!(k in doc)) doc[k] = undefined;
|
|
890
1057
|
entries.push([key, doc]);
|
|
891
1058
|
}
|
|
892
|
-
if (entries.length > 0)
|
|
1059
|
+
if (entries.length > 0) {
|
|
1060
|
+
await engine.updateByIds(coll, entries);
|
|
1061
|
+
// #223:$unset 必须真正删除键,不能留 undefined(BSON 会编码成 null)
|
|
1062
|
+
const table = engine._tables && engine._tables[coll];
|
|
1063
|
+
if (table && Array.isArray(table._rows)) {
|
|
1064
|
+
for (const row of table._rows) {
|
|
1065
|
+
for (const k of Object.keys(row)) if (row[k] === undefined) delete row[k];
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
/** #218:replacement 语义 —— 整体替换文档,只保留 _id */
|
|
1072
|
+
async _replaceRows(engine, coll, rows, replacement) {
|
|
1073
|
+
const ids = [];
|
|
1074
|
+
const docs = [];
|
|
1075
|
+
for (const r of rows) {
|
|
1076
|
+
const key = this._rowKey(engine, coll, r);
|
|
1077
|
+
if (key === undefined) continue;
|
|
1078
|
+
const d = cloneDoc(replacement || {});
|
|
1079
|
+
if (r._id !== undefined) d._id = r._id;
|
|
1080
|
+
ids.push(key);
|
|
1081
|
+
docs.push(d);
|
|
1082
|
+
}
|
|
1083
|
+
if (ids.length === 0) return;
|
|
1084
|
+
await engine.removeByIds(coll, ids);
|
|
1085
|
+
for (const d of docs) await engine.insert(coll, d);
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
_nonDollarQuery(q) {
|
|
1089
|
+
const out = {};
|
|
1090
|
+
for (const [k, v] of Object.entries(q || {})) if (!k.startsWith('$')) out[k] = v;
|
|
1091
|
+
return out;
|
|
893
1092
|
}
|
|
894
1093
|
|
|
895
1094
|
async _removeRows(engine, coll, rows) {
|
|
@@ -916,6 +1115,13 @@ class MongoServer {
|
|
|
916
1115
|
|
|
917
1116
|
// 单个字段值的操作符求值;$not 递归取反。返回 false 表示不匹配。
|
|
918
1117
|
_fieldMatch(rv, cond) {
|
|
1118
|
+
// #219:条件是不含任何 $ 运算符的普通子文档时,按整文档深相等匹配
|
|
1119
|
+
if (cond && typeof cond === 'object' && !Array.isArray(cond) && !(cond instanceof ObjectId)) {
|
|
1120
|
+
const keys = Object.keys(cond);
|
|
1121
|
+
if (keys.length > 0 && keys.every(k => k[0] !== '$')) {
|
|
1122
|
+
return deepEqualMongo(rv, cond);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
919
1125
|
for (const [op, v] of Object.entries(cond)) {
|
|
920
1126
|
if (op === '$not') { if (this._fieldMatch(rv, v)) return false; continue; }
|
|
921
1127
|
if (op === '$options') continue;
|
|
@@ -932,14 +1138,26 @@ class MongoServer {
|
|
|
932
1138
|
if (op === '$exists' && (v ? (rv === undefined) : (rv !== undefined))) return false;
|
|
933
1139
|
if (op === '$all' && !(Array.isArray(v) && v.every(t => orAnyElement(rv, el => valuesEqual(el, t))))) return false;
|
|
934
1140
|
if (op === '$regex') {
|
|
935
|
-
|
|
936
|
-
const
|
|
1141
|
+
// #217:$options 支持 i/m/s/x,全部映射到 JS RegExp 标志
|
|
1142
|
+
const opt = String(cond.$options || '');
|
|
1143
|
+
const flags = [...new Set(opt.match(/[ims]/g) || [])].join('');
|
|
1144
|
+
let pat = String(v);
|
|
1145
|
+
if (opt.includes('x')) pat = pat.replace(/\s+/g, '');
|
|
1146
|
+
const re = new RegExp(pat, flags);
|
|
937
1147
|
if (!orAnyElement(rv, el => typeof el === 'string' && re.test(el))) return false;
|
|
938
1148
|
}
|
|
939
1149
|
if (op === '$type') {
|
|
940
1150
|
if (v === 'objectId') { if (!(rv instanceof ObjectId)) return false; continue; }
|
|
941
|
-
|
|
942
|
-
|
|
1151
|
+
// #220:按 BSON 类型判别,不能直接用 typeof(数组/null 永远对不上)
|
|
1152
|
+
const actual = bsonTypeOf(rv);
|
|
1153
|
+
const wanted = String(v).toLowerCase();
|
|
1154
|
+
const aliases = {
|
|
1155
|
+
bool: 'bool', boolean: 'bool',
|
|
1156
|
+
int: 'number', long: 'number', double: 'number', number: 'number',
|
|
1157
|
+
null: 'null', array: 'array', object: 'object',
|
|
1158
|
+
string: 'string', date: 'date', objectid: 'objectId',
|
|
1159
|
+
};
|
|
1160
|
+
if (actual !== (aliases[wanted] || wanted)) return false;
|
|
943
1161
|
}
|
|
944
1162
|
if (op === '$size' && !(Array.isArray(rv) && rv.length === v)) return false;
|
|
945
1163
|
if (op === '$elemMatch' && !(Array.isArray(rv) && rv.some((el) => this._elemMatches(el, v)))) return false;
|
package/lib/mysql_server.js
CHANGED
|
@@ -94,7 +94,8 @@ const CLIENT_MULTI_RESULTS = 0x00020000;
|
|
|
94
94
|
const CLIENT_LONG_FLAG = 0x00000004;
|
|
95
95
|
const CLIENT_DEPRECATE_EOF = 0x01000000;
|
|
96
96
|
const SERVER_STATUS_AUTOCOMMIT = 0x0002;
|
|
97
|
-
const
|
|
97
|
+
const SERVER_MORE_RESULTS_EXISTS = 0x0008;
|
|
98
|
+
const CHARSET_UTF8 = 0x2d; // #212: 45 = utf8mb4_general_ci(33 是 utf8mb3/cesu8)
|
|
98
99
|
|
|
99
100
|
const MYSQL_TYPE_TINY = 1, MYSQL_TYPE_LONG = 3, MYSQL_TYPE_LONGLONG = 8,
|
|
100
101
|
MYSQL_TYPE_DATE = 10, MYSQL_TYPE_DATETIME = 12, MYSQL_TYPE_DOUBLE = 5,
|
|
@@ -202,11 +203,11 @@ function okPacket(affectedRows = 0, insertId = 0, status = SERVER_STATUS_AUTOCOM
|
|
|
202
203
|
return b.build();
|
|
203
204
|
}
|
|
204
205
|
|
|
205
|
-
function eofPacket() {
|
|
206
|
+
function eofPacket(status = SERVER_STATUS_AUTOCOMMIT) {
|
|
206
207
|
const b = new PacketBuilder();
|
|
207
208
|
b.byte(0xfe);
|
|
208
209
|
b.int16(0);
|
|
209
|
-
b.int16(
|
|
210
|
+
b.int16(status);
|
|
210
211
|
return b.build();
|
|
211
212
|
}
|
|
212
213
|
|
|
@@ -281,9 +282,9 @@ function columnDefinition(column) {
|
|
|
281
282
|
|
|
282
283
|
function columnTypeFromSchema(def) {
|
|
283
284
|
const type = def && def.type ? String(def.type).toLowerCase() : 'string';
|
|
285
|
+
if (type === 'bigint' || (def && def.intType === 'bigint')) return MYSQL_TYPE_LONGLONG;
|
|
284
286
|
if (type === 'integer' || type === 'int') return MYSQL_TYPE_LONG;
|
|
285
|
-
if (type === '
|
|
286
|
-
if (type === 'number' || type === 'float' || type === 'double' || type === 'real') return MYSQL_TYPE_DOUBLE;
|
|
287
|
+
if (type === 'number' || type === 'float' || type === 'double' || type === 'real' || type === 'numeric' || type === 'decimal') return MYSQL_TYPE_DOUBLE;
|
|
287
288
|
if (type === 'boolean' || type === 'bool') return MYSQL_TYPE_TINY;
|
|
288
289
|
if (type === 'date') return MYSQL_TYPE_DATE;
|
|
289
290
|
if (type === 'datetime' || type === 'timestamp') return MYSQL_TYPE_DATETIME;
|
|
@@ -292,7 +293,7 @@ function columnTypeFromSchema(def) {
|
|
|
292
293
|
return MYSQL_TYPE_VAR_STRING;
|
|
293
294
|
}
|
|
294
295
|
|
|
295
|
-
function resultSetPacket(result, tableSchema, baseSeq, rawRow) {
|
|
296
|
+
function resultSetPacket(result, tableSchema, baseSeq, rawRow, status = SERVER_STATUS_AUTOCOMMIT) {
|
|
296
297
|
const packets = [];
|
|
297
298
|
let sequence = baseSeq || 0;
|
|
298
299
|
const push = (buf) => {
|
|
@@ -331,7 +332,7 @@ function resultSetPacket(result, tableSchema, baseSeq, rawRow) {
|
|
|
331
332
|
};
|
|
332
333
|
push(columnDefinition(col));
|
|
333
334
|
}
|
|
334
|
-
push(eofPacket());
|
|
335
|
+
push(eofPacket(status));
|
|
335
336
|
|
|
336
337
|
for (const row of result.rows || []) {
|
|
337
338
|
if (!row) continue;
|
|
@@ -350,11 +351,11 @@ function resultSetPacket(result, tableSchema, baseSeq, rawRow) {
|
|
|
350
351
|
}
|
|
351
352
|
push(Buffer.concat(parts));
|
|
352
353
|
}
|
|
353
|
-
push(eofPacket());
|
|
354
|
+
push(eofPacket(status));
|
|
354
355
|
return { packets, sequence };
|
|
355
356
|
}
|
|
356
357
|
|
|
357
|
-
function binaryResultSetPacket(result, tableSchema, baseSeq) {
|
|
358
|
+
function binaryResultSetPacket(result, tableSchema, baseSeq, status = SERVER_STATUS_AUTOCOMMIT) {
|
|
358
359
|
const packets = [];
|
|
359
360
|
let sequence = baseSeq || 0;
|
|
360
361
|
const push = (buf) => {
|
|
@@ -389,7 +390,7 @@ function binaryResultSetPacket(result, tableSchema, baseSeq) {
|
|
|
389
390
|
types.push(t);
|
|
390
391
|
push(columnDefinition({ name: String(cols[ci]), table: result.table || '', type: t, length: 1024 }));
|
|
391
392
|
}
|
|
392
|
-
push(eofPacket());
|
|
393
|
+
push(eofPacket(status));
|
|
393
394
|
|
|
394
395
|
const encodeValue = (v, type) => {
|
|
395
396
|
if (v === null || v === undefined) return null;
|
|
@@ -409,7 +410,7 @@ function binaryResultSetPacket(result, tableSchema, baseSeq) {
|
|
|
409
410
|
const b = Buffer.alloc(4); b.writeInt32LE(n, 0); return b;
|
|
410
411
|
}
|
|
411
412
|
// MYSQL_TYPE_LONGLONG:始终写 8 字节,超出 int64 范围时钳位(保持行布局)。
|
|
412
|
-
let bi = BigInt(Math.trunc(num));
|
|
413
|
+
let bi = typeof v === 'bigint' ? v : BigInt(Math.trunc(num));
|
|
413
414
|
const INT64_MAX = 9223372036854775807n;
|
|
414
415
|
const INT64_MIN = -9223372036854775808n;
|
|
415
416
|
if (bi > INT64_MAX) bi = INT64_MAX;
|
|
@@ -849,7 +850,8 @@ class MysqlConnection {
|
|
|
849
850
|
off = parsed.off;
|
|
850
851
|
}
|
|
851
852
|
}
|
|
852
|
-
|
|
853
|
+
// #211:COM_STMT_EXECUTE 的结果集恒为二进制格式,即使没有占位符
|
|
854
|
+
this._handleQuery(stmt.sql, values !== null && values.length > 0 ? values : undefined, true);
|
|
853
855
|
} catch (e) {
|
|
854
856
|
this._send(errPacket(toMysqlErrno(e), e.message));
|
|
855
857
|
}
|
|
@@ -906,7 +908,7 @@ class MysqlConnection {
|
|
|
906
908
|
}
|
|
907
909
|
}
|
|
908
910
|
|
|
909
|
-
_handleQuery(sql, values) {
|
|
911
|
+
_handleQuery(sql, values, binary = false) {
|
|
910
912
|
(async () => {
|
|
911
913
|
try {
|
|
912
914
|
const hasParams = values !== undefined && values !== null && values.length > 0;
|
|
@@ -915,7 +917,10 @@ class MysqlConnection {
|
|
|
915
917
|
if (statements.length > 1 && !this.multiStatements) {
|
|
916
918
|
throw new Error(`too many statements (${statements.length} > 1)`);
|
|
917
919
|
}
|
|
918
|
-
for (
|
|
920
|
+
for (let si = 0; si < statements.length; si++) {
|
|
921
|
+
const stmtSql = statements[si];
|
|
922
|
+
// #210:非末结果必须置 SERVER_MORE_RESULTS_EXISTS,客户端才会继续读下一个结果集
|
|
923
|
+
const status = SERVER_STATUS_AUTOCOMMIT | (si < statements.length - 1 ? SERVER_MORE_RESULTS_EXISTS : 0);
|
|
919
924
|
const stmt = parseSQL(stmtSql);
|
|
920
925
|
switch (stmt.type) {
|
|
921
926
|
case 'use': {
|
|
@@ -926,7 +931,7 @@ class MysqlConnection {
|
|
|
926
931
|
try {
|
|
927
932
|
await this.server._getDatabase(stmt.database);
|
|
928
933
|
this.currentDb = stmt.database;
|
|
929
|
-
this._send(okPacket());
|
|
934
|
+
this._send(okPacket(0, 0, status));
|
|
930
935
|
} catch (e) {
|
|
931
936
|
this._send(errPacket(1049, `Unknown database '${stmt.database}'`));
|
|
932
937
|
}
|
|
@@ -938,7 +943,7 @@ class MysqlConnection {
|
|
|
938
943
|
const names = allowed === null ? dbs : dbs.filter(n => allowed.includes(n));
|
|
939
944
|
const { packets, sequence } = resultSetPacket({
|
|
940
945
|
type: 'showDatabases', columns: ['Database'], rows: names.map(n => [n]),
|
|
941
|
-
}, null, this.sequence);
|
|
946
|
+
}, null, this.sequence, null, status);
|
|
942
947
|
this.sequence = sequence;
|
|
943
948
|
this.socket.write(Buffer.concat(packets));
|
|
944
949
|
continue;
|
|
@@ -949,7 +954,7 @@ class MysqlConnection {
|
|
|
949
954
|
continue;
|
|
950
955
|
}
|
|
951
956
|
await this.server.createDatabase(stmt.database, { ifNotExists: stmt.ifNotExists });
|
|
952
|
-
this._send(okPacket());
|
|
957
|
+
this._send(okPacket(0, 0, status));
|
|
953
958
|
continue;
|
|
954
959
|
}
|
|
955
960
|
case 'dropDatabase': {
|
|
@@ -958,7 +963,7 @@ class MysqlConnection {
|
|
|
958
963
|
continue;
|
|
959
964
|
}
|
|
960
965
|
await this.server.dropDatabase(stmt.database, { ifExists: stmt.ifExists });
|
|
961
|
-
this._send(okPacket());
|
|
966
|
+
this._send(okPacket(0, 0, status));
|
|
962
967
|
continue;
|
|
963
968
|
}
|
|
964
969
|
default:
|
|
@@ -983,9 +988,9 @@ class MysqlConnection {
|
|
|
983
988
|
? await engine.getTableSchema(r.table)
|
|
984
989
|
: (engine._schemas ? engine._schemas[r.table] : null);
|
|
985
990
|
}
|
|
986
|
-
const { packets, sequence } =
|
|
987
|
-
? binaryResultSetPacket(r, schema, this.sequence)
|
|
988
|
-
: resultSetPacket(r, schema, this.sequence);
|
|
991
|
+
const { packets, sequence } = binary
|
|
992
|
+
? binaryResultSetPacket(r, schema, this.sequence, status)
|
|
993
|
+
: resultSetPacket(r, schema, this.sequence, null, status);
|
|
989
994
|
this.sequence = sequence;
|
|
990
995
|
this.socket.write(Buffer.concat(packets));
|
|
991
996
|
} else {
|
|
@@ -995,7 +1000,7 @@ class MysqlConnection {
|
|
|
995
1000
|
} else if (r.type === 'update' || r.type === 'delete' || r.type === 'truncate') {
|
|
996
1001
|
this.session.rowCount = r.affectedRows;
|
|
997
1002
|
}
|
|
998
|
-
this._send(okPacket(r.affectedRows || 0, r.insertId || 0));
|
|
1003
|
+
this._send(okPacket(r.affectedRows || 0, r.insertId || 0, status));
|
|
999
1004
|
}
|
|
1000
1005
|
}
|
|
1001
1006
|
} catch (e) {
|