jsql-neo 5.5.0 → 6.0.0
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/LICENSE +202 -202
- package/README.md +8155 -8097
- package/bin/jsql +320 -320
- package/bin/jsql-server +90 -90
- package/bin/jsql-server-control.js +177 -177
- package/index.d.ts +412 -385
- package/index.js +128 -124
- package/lib/ast.js +214 -214
- package/lib/btree.js +478 -413
- package/lib/cache.js +328 -328
- package/lib/client.js +125 -125
- package/lib/database.js +1741 -1744
- package/lib/date-types.js +182 -182
- package/lib/errors.js +89 -89
- package/lib/jsql_format.js +497 -497
- package/lib/migrate.js +254 -254
- package/lib/mod.js +292 -292
- package/lib/mongo_server.js +607 -607
- package/lib/multiserver.js +140 -140
- package/lib/mysql_compat.js +413 -413
- package/lib/mysql_server.js +1237 -1237
- package/lib/native_client.js +722 -732
- package/lib/nedb_compat.js +506 -506
- package/lib/pg_server.js +864 -864
- package/lib/plugin.js +259 -35
- package/lib/plugins/audit.js +37 -0
- package/lib/plugins/index.js +72 -0
- package/lib/plugins/logger.js +35 -0
- package/lib/plugins/metrics.js +38 -0
- package/lib/plugins/timestamps.js +57 -0
- package/lib/plugins/validation.js +128 -0
- package/lib/prepared.js +37 -0
- package/lib/query.js +834 -815
- package/lib/redis_server.js +571 -571
- package/lib/sql.js +4053 -3460
- package/lib/sqlite_compat.js +323 -323
- package/lib/sqlite_worker.js +264 -264
- package/lib/table.js +1351 -1304
- package/lib/tui.js +502 -502
- package/lib/wasm.js +1 -1
- package/lib/wasm_client.js +715 -718
- package/lib/web_ui.js +257 -257
- package/native/jsql-neo-native.node +0 -0
- package/nativesrc/jsql-neo-core/Cargo.lock +462 -455
- package/nativesrc/jsql-neo-core/Cargo.toml +26 -24
- package/nativesrc/jsql-neo-core/src/engine/hybrid.rs +492 -455
- package/nativesrc/jsql-neo-core/src/engine/memory.rs +191 -168
- package/nativesrc/jsql-neo-core/src/engine/mod.rs +74 -62
- package/nativesrc/jsql-neo-core/src/engine/table.rs +996 -700
- package/nativesrc/jsql-neo-core/src/lib.rs +2 -2
- package/nativesrc/jsql-neo-core/src/storage/mod.rs +2 -2
- package/nativesrc/jsql-neo-core/src/storage/persistent.rs +1 -1
- package/nativesrc/jsql-neo-core/src/storage/wal.rs +298 -85
- package/nativesrc/jsql-neo-core/src/types.rs +93 -93
- package/nativesrc/jsql-neo-native/Cargo.lock +613 -606
- package/nativesrc/jsql-neo-native/Cargo.toml +24 -16
- package/nativesrc/jsql-neo-native/build.rs +3 -3
- package/nativesrc/jsql-neo-native/package.json +7 -7
- package/nativesrc/jsql-neo-native/src/lib.rs +281 -269
- package/nativesrc/jsql-neo-wasm/Cargo.lock +472 -465
- package/nativesrc/jsql-neo-wasm/Cargo.toml +22 -15
- package/nativesrc/jsql-neo-wasm/src/lib.rs +463 -447
- package/package.json +87 -87
- package/postinstall.js +158 -158
- package/test/btree.test.js +144 -128
- package/test/coverage.js +187 -187
- package/test/join.test.js +110 -110
- package/test/native.test.js +158 -158
- package/test/query-opt.test.js +109 -0
- package/test/regress-5.1.0.js +271 -271
- package/test/smoke.js +58 -58
- package/test/sql-parser.test.js +450 -273
- package/test/wasm.test.js +110 -110
- package/wasm/browser.d.ts +89 -89
- package/wasm/browser.mjs +591 -569
- package/wasm/browser_bg.mjs +618 -597
- package/wasm/jsql_neo_wasm.d.ts +44 -38
- package/wasm/jsql_neo_wasm.js +641 -520
- package/wasm/jsql_neo_wasm_bg.wasm +0 -0
- package/wasm/jsql_neo_wasm_bg.wasm.d.ts +26 -26
- package/wasm/package.json +11 -11
package/lib/table.js
CHANGED
|
@@ -1,1305 +1,1352 @@
|
|
|
1
|
-
// © Vexify 2026 All Rights Reserved.
|
|
2
|
-
/**
|
|
3
|
-
* JSQL Table — 表操作引擎 v2.0
|
|
4
|
-
* B-Tree 自动索引、日期类型、错误码、外键 CASCADE、长度/精度校验
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const Query = require('./query');
|
|
8
|
-
const BTree = require('./btree');
|
|
9
|
-
const { validateDateType, now } = require('./date-types');
|
|
10
|
-
const { createError } = require('./errors');
|
|
11
|
-
|
|
12
|
-
function isSafeRegex(pattern) {
|
|
13
|
-
if (typeof pattern !== 'string') return false;
|
|
14
|
-
if (pattern.length > 256) return false;
|
|
15
|
-
const groups = [];
|
|
16
|
-
let i = 0;
|
|
17
|
-
const n = pattern.length;
|
|
18
|
-
while (i < n) {
|
|
19
|
-
const c = pattern[i];
|
|
20
|
-
if (c === '\\') { i += 2; continue; }
|
|
21
|
-
if (c === '[') {
|
|
22
|
-
while (i < n && pattern[i] !== ']') i++;
|
|
23
|
-
i++;
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
if (c === '(') {
|
|
27
|
-
const nxt = pattern[i + 1];
|
|
28
|
-
if (nxt === '?') {
|
|
29
|
-
const t = pattern[i + 2];
|
|
30
|
-
if (t === ':') {
|
|
31
|
-
groups.push({ hasQuant: false, hasAlt: false });
|
|
32
|
-
i += 3;
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
if (t === '=' || t === '!') {
|
|
36
|
-
let depth = 1, j = i + 3;
|
|
37
|
-
for (; j < n && depth > 0; j++) {
|
|
38
|
-
if (pattern[j] === '\\') { j++; continue; }
|
|
39
|
-
if (pattern[j] === '(') depth++;
|
|
40
|
-
else if (pattern[j] === ')') depth--;
|
|
41
|
-
}
|
|
42
|
-
i = j;
|
|
43
|
-
continue;
|
|
44
|
-
}
|
|
45
|
-
if (t === '<') {
|
|
46
|
-
const u = pattern[i + 3];
|
|
47
|
-
if (u === '=' || u === '!') {
|
|
48
|
-
let depth = 1, j = i + 4;
|
|
49
|
-
for (; j < n && depth > 0; j++) {
|
|
50
|
-
if (pattern[j] === '\\') { j++; continue; }
|
|
51
|
-
if (pattern[j] === '(') depth++;
|
|
52
|
-
else if (pattern[j] === ')') depth--;
|
|
53
|
-
}
|
|
54
|
-
i = j;
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
const gt = pattern.indexOf('>', i + 3);
|
|
58
|
-
if (gt === -1) return false;
|
|
59
|
-
groups.push({ hasQuant: false, hasAlt: false });
|
|
60
|
-
i = gt + 1;
|
|
61
|
-
continue;
|
|
62
|
-
}
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
groups.push({ hasQuant: false, hasAlt: false });
|
|
66
|
-
i++;
|
|
67
|
-
continue;
|
|
68
|
-
}
|
|
69
|
-
if (c === ')') {
|
|
70
|
-
const g = groups.pop();
|
|
71
|
-
if (!g) return false;
|
|
72
|
-
let qi = i + 1;
|
|
73
|
-
let quant = null;
|
|
74
|
-
if (pattern[qi] === '+' || pattern[qi] === '*' || pattern[qi] === '?') {
|
|
75
|
-
quant = pattern[qi];
|
|
76
|
-
} else if (pattern[qi] === '{') {
|
|
77
|
-
const close = pattern.indexOf('}', qi);
|
|
78
|
-
if (close === -1) return false;
|
|
79
|
-
quant = pattern.slice(qi, close + 1);
|
|
80
|
-
qi = close + 1;
|
|
81
|
-
}
|
|
82
|
-
if (quant && (g.hasQuant || g.hasAlt)) return false;
|
|
83
|
-
i = qi;
|
|
84
|
-
continue;
|
|
85
|
-
}
|
|
86
|
-
if (c === '+' || c === '*' || c === '?') {
|
|
87
|
-
if (groups.length > 0) groups[groups.length - 1].hasQuant = true;
|
|
88
|
-
i++;
|
|
89
|
-
continue;
|
|
90
|
-
}
|
|
91
|
-
if (c === '{' && /[0-9]/.test(pattern[i + 1] || '')) {
|
|
92
|
-
const close = pattern.indexOf('}', i);
|
|
93
|
-
if (close === -1) return false;
|
|
94
|
-
if (groups.length > 0) groups[groups.length - 1].hasQuant = true;
|
|
95
|
-
i = close + 1;
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
if (c === '|') {
|
|
99
|
-
if (groups.length > 0) groups[groups.length - 1].hasAlt = true;
|
|
100
|
-
i++;
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
i++;
|
|
104
|
-
}
|
|
105
|
-
return groups.length === 0;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
class Table {
|
|
109
|
-
constructor(name, schema, db) {
|
|
110
|
-
this._name = name;
|
|
111
|
-
this._schema = schema;
|
|
112
|
-
this._db = db;
|
|
113
|
-
this._rows = [];
|
|
114
|
-
this._indexes = {}; // 旧版 hash 索引(兼容)
|
|
115
|
-
this._btrees = {}; // B-Tree 索引 { fieldName: BTree }
|
|
116
|
-
this._autoIncrement = 0;
|
|
117
|
-
this._dirty = false;
|
|
118
|
-
this._hooks = {
|
|
119
|
-
beforeInsert: [], afterInsert: [],
|
|
120
|
-
beforeUpdate: [], afterUpdate: [],
|
|
121
|
-
beforeRemove: [], afterRemove: []
|
|
122
|
-
};
|
|
123
|
-
this._softDelete = false;
|
|
124
|
-
this._computedFields = {};
|
|
125
|
-
this._btreesDirty = false;
|
|
126
|
-
|
|
127
|
-
this._primaryKey = null;
|
|
128
|
-
this._pkIndex = null;
|
|
129
|
-
this._autoIncrementField = null;
|
|
130
|
-
this._foreignKeys = [];
|
|
131
|
-
this._checkConstraints = [];
|
|
132
|
-
this._defaults = {};
|
|
133
|
-
this._dateFields = {}; // { fieldName: 'date'|'datetime'|'timestamp' }
|
|
134
|
-
|
|
135
|
-
for (const [field, def] of Object.entries(schema)) {
|
|
136
|
-
const isPk = !!(def.primaryKey || def.primary === true);
|
|
137
|
-
if (isPk) this._primaryKey = field;
|
|
138
|
-
if (def.autoIncrement) {
|
|
139
|
-
this._autoIncrementField = field;
|
|
140
|
-
if (isPk) this._primaryKey = field;
|
|
141
|
-
}
|
|
142
|
-
if (def.default !== undefined) {
|
|
143
|
-
this._defaults[field] = def.default;
|
|
144
|
-
}
|
|
145
|
-
if (def.check) {
|
|
146
|
-
this._checkConstraints.push({ field, fn: def.check });
|
|
147
|
-
}
|
|
148
|
-
if (def.foreignKey) {
|
|
149
|
-
this._foreignKeys.push({
|
|
150
|
-
field,
|
|
151
|
-
table: def.foreignKey.table,
|
|
152
|
-
refField: def.foreignKey.field || 'id',
|
|
153
|
-
onDelete: def.foreignKey.onDelete || 'restrict',
|
|
154
|
-
onUpdate: def.foreignKey.onUpdate || 'restrict'
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
if (def.computed) {
|
|
158
|
-
this._computedFields[field] = def.computed;
|
|
159
|
-
}
|
|
160
|
-
// 日期类型
|
|
161
|
-
if (['date', 'datetime', 'timestamp', 'time'].includes(def.type)) {
|
|
162
|
-
this._dateFields[field] = def.type;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (schema._softDelete) {
|
|
167
|
-
this._softDelete = true;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
this._pkIndex = this._primaryKey ? new Map() : null;
|
|
171
|
-
this._cachedSchemaFields = Object.keys(schema).filter(f => f !== '_softDelete');
|
|
172
|
-
this._cachedDateFields = Object.keys(this._dateFields);
|
|
173
|
-
this._cachedUniqueFields = this._cachedSchemaFields.filter(f => schema[f].unique);
|
|
174
|
-
this._cachedRequiredFields = this._cachedSchemaFields.filter(f => schema[f].required);
|
|
175
|
-
this._autoCreateBTrees();
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
_autoCreateBTrees() {
|
|
179
|
-
for (const [field, def] of Object.entries(this._schema)) {
|
|
180
|
-
if (field === '_softDelete') continue;
|
|
181
|
-
if (def.primaryKey || def.primary === true || def.unique) {
|
|
182
|
-
this._btrees[field] = new BTree(64, true);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// ============================================================
|
|
188
|
-
// 插入
|
|
189
|
-
// ============================================================
|
|
190
|
-
|
|
191
|
-
insert(data) {
|
|
192
|
-
for (const hook of this._hooks.beforeInsert) {
|
|
193
|
-
data = hook({ ...data }) || data;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
data = this._applyDefaults(data);
|
|
197
|
-
data = this._applyComputed(data);
|
|
198
|
-
data = this._validateDataTypes(data);
|
|
199
|
-
this._validateConstraints(data, -1);
|
|
200
|
-
|
|
201
|
-
if (this._autoIncrementField && data[this._autoIncrementField] === undefined) {
|
|
202
|
-
this._autoIncrement++;
|
|
203
|
-
data[this._autoIncrementField] = this._autoIncrement;
|
|
204
|
-
} else if (this._autoIncrementField && data[this._autoIncrementField] > this._autoIncrement) {
|
|
205
|
-
this._autoIncrement = data[this._autoIncrementField];
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
const rowIndex = this._rows.length;
|
|
209
|
-
this._rows.push({ ...data });
|
|
210
|
-
this._addToIndexes(rowIndex, data);
|
|
211
|
-
this._dirty = true;
|
|
212
|
-
this._db._markDirty(this._name);
|
|
213
|
-
|
|
214
|
-
for (const hook of this._hooks.afterInsert) {
|
|
215
|
-
hook(data);
|
|
216
|
-
}
|
|
217
|
-
this._db._emitChange('insert', this._name, data);
|
|
218
|
-
|
|
219
|
-
return data;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
insertMany(items) {
|
|
223
|
-
if (items.length === 0) return [];
|
|
224
|
-
for (const hook of this._hooks.beforeInsert) {
|
|
225
|
-
const mapped = new Array(items.length);
|
|
226
|
-
for (let i = 0; i < items.length; i++) mapped[i] = hook({ ...items[i] }) || items[i];
|
|
227
|
-
items = mapped;
|
|
228
|
-
}
|
|
229
|
-
const N = items.length;
|
|
230
|
-
const uniqueTracker = {};
|
|
231
|
-
for (const field of this._cachedUniqueFields) {
|
|
232
|
-
uniqueTracker[field] = new Set();
|
|
233
|
-
for (const row of this._rows) {
|
|
234
|
-
if (row[field] !== undefined && row[field] !== null) uniqueTracker[field].add(row[field]);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
const dateFields = this._cachedDateFields;
|
|
238
|
-
const hasDateFields = dateFields.length > 0;
|
|
239
|
-
const autoIncField = this._autoIncrementField;
|
|
240
|
-
const defaults = this._defaults;
|
|
241
|
-
const computedFields = this._computedFields;
|
|
242
|
-
const schema = this._schema;
|
|
243
|
-
const checkConstraints = this._checkConstraints;
|
|
244
|
-
const foreignKeys = this._foreignKeys;
|
|
245
|
-
// 批量预分配自增 ID 范围:先扫描显式提供的最大值,一次性推进计数器,
|
|
246
|
-
// 再在循环内用本地序号递增,避免批量内重复读取/写入同一计数器字段。
|
|
247
|
-
const baseAutoInc = this._autoIncrement;
|
|
248
|
-
if (autoIncField) {
|
|
249
|
-
let maxExplicit = 0;
|
|
250
|
-
for (const it of items) {
|
|
251
|
-
const v = it[autoIncField];
|
|
252
|
-
if (v !== undefined && v !== null) {
|
|
253
|
-
const n = Number(v);
|
|
254
|
-
if (!isNaN(n) && n > maxExplicit) maxExplicit = n;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
if (maxExplicit > baseAutoInc) {
|
|
258
|
-
this._autoIncrement = maxExplicit;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
let autoIncSeq = 0;
|
|
262
|
-
for (let i = 0; i < N; i++) {
|
|
263
|
-
let data = items[i];
|
|
264
|
-
for (const [f, dv] of Object.entries(defaults)) {
|
|
265
|
-
if (data[f] === undefined) data[f] = typeof dv === 'function' ? dv() : dv === 'CURRENT_TIMESTAMP' ? now() : dv;
|
|
266
|
-
}
|
|
267
|
-
for (const f of Object.keys(computedFields)) data[f] = computedFields[f](data);
|
|
268
|
-
if (hasDateFields) {
|
|
269
|
-
for (const f of dateFields) {
|
|
270
|
-
if (data[f] !== undefined && data[f] !== null) data[f] = validateDateType(data[f], this._dateFields[f], f);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
for (const field of this._cachedSchemaFields) {
|
|
274
|
-
const def = schema[field];
|
|
275
|
-
if (def.required && (data[field] === undefined || data[field] === null)
|
|
276
|
-
&& !(def.autoIncrement && data[field] === undefined)) throw createError('ER_BAD_NULL_ERROR', field);
|
|
277
|
-
if (def.unique && data[field] !== undefined) {
|
|
278
|
-
if (uniqueTracker[field].has(data[field])) throw createError('ER_DUP_ENTRY', String(data[field]), field);
|
|
279
|
-
uniqueTracker[field].add(data[field]);
|
|
280
|
-
}
|
|
281
|
-
if (def.length && typeof data[field] === 'string' && data[field].length > def.length) throw createError('ER_DATA_TOO_LONG', field);
|
|
282
|
-
if (def.type === 'integer' || def.type === 'number') {
|
|
283
|
-
const v = def.type === 'integer' ? Number(data[field]) : data[field];
|
|
284
|
-
if (data[field] !== undefined && data[field] !== null && def.min !== undefined && v < def.min) throw createError('ER_OUT_OF_RANGE', field);
|
|
285
|
-
if (data[field] !== undefined && data[field] !== null && def.max !== undefined && v > def.max) throw createError('ER_OUT_OF_RANGE', field);
|
|
286
|
-
}
|
|
287
|
-
if (def.check && data[field] !== undefined && !def.check(data[field], data)) throw createError('ER_CHECK_CONSTRAINT', this._name + '.' + field);
|
|
288
|
-
}
|
|
289
|
-
for (const fk of foreignKeys) {
|
|
290
|
-
if (data[fk.field] !== undefined && data[fk.field] !== null) {
|
|
291
|
-
const refTable = this._resolveTable(fk.table);
|
|
292
|
-
if (refTable) {
|
|
293
|
-
const exists = refTable.findOne({ [fk.refField]: data[fk.field] });
|
|
294
|
-
if (!exists) throw createError('ER_NO_REFERENCED_ROW');
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
if (autoIncField && data[autoIncField] === undefined) {
|
|
299
|
-
autoIncSeq++;
|
|
300
|
-
data[autoIncField] = baseAutoInc + autoIncSeq;
|
|
301
|
-
this._autoIncrement = data[autoIncField];
|
|
302
|
-
} else if (autoIncField && data[autoIncField] > this._autoIncrement) {
|
|
303
|
-
this._autoIncrement = data[autoIncField];
|
|
304
|
-
}
|
|
305
|
-
const rowIdx = this._rows.length;
|
|
306
|
-
this._rows.push(Object.assign({}, data));
|
|
307
|
-
if (this._pkIndex && this._primaryKey) {
|
|
308
|
-
const pkVal = data[this._primaryKey];
|
|
309
|
-
if (pkVal !== undefined && pkVal !== null) this._pkIndex.set(pkVal, rowIdx);
|
|
310
|
-
}
|
|
311
|
-
for (const [field, tree] of Object.entries(this._btrees)) {
|
|
312
|
-
const val = data[field];
|
|
313
|
-
if (val !== undefined && val !== null) tree.insert(val, rowIdx);
|
|
314
|
-
}
|
|
315
|
-
items[i] = data;
|
|
316
|
-
}
|
|
317
|
-
this._dirty = true;
|
|
318
|
-
this._db._markDirty(this._name);
|
|
319
|
-
for (const hook of this._hooks.afterInsert) {
|
|
320
|
-
for (let i = 0; i < N; i++) hook(items[i]);
|
|
321
|
-
}
|
|
322
|
-
this._db._emitChange('insertMany', this._name, { count: N });
|
|
323
|
-
return items;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
upsert(data) {
|
|
327
|
-
if (this._primaryKey && data[this._primaryKey] !== undefined) {
|
|
328
|
-
const existing = this.findById(data[this._primaryKey]);
|
|
329
|
-
if (existing) {
|
|
330
|
-
this.updateById(data[this._primaryKey], data);
|
|
331
|
-
return this.findById(data[this._primaryKey]);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
return this.insert(data);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
// ============================================================
|
|
338
|
-
// 查询(支持 B-Tree 加速)
|
|
339
|
-
// ============================================================
|
|
340
|
-
|
|
341
|
-
find(query = {}) {
|
|
342
|
-
let rows = this._getVisibleRows();
|
|
343
|
-
if (Object.keys(query).length > 0) {
|
|
344
|
-
rows = this._applyFilterOptimized(rows, query);
|
|
345
|
-
}
|
|
346
|
-
return rows.map(r => ({ ...r }));
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
findOne(query = {}) {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
if (this.
|
|
367
|
-
const
|
|
368
|
-
if (
|
|
369
|
-
const row = this._rows[
|
|
370
|
-
if (row && !row._deleted) return { ...row };
|
|
371
|
-
}
|
|
372
|
-
return null;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
return this.
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
return
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
this.
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
this.
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
}
|
|
461
|
-
this.
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
this.
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
for (
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
this.
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
if (definition.
|
|
646
|
-
this.
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
}
|
|
654
|
-
if (definition.
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
this.
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
this.
|
|
683
|
-
this.
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
if (this.
|
|
690
|
-
this.
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
if (this.
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
this.
|
|
714
|
-
if (
|
|
715
|
-
this.
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
this.
|
|
722
|
-
this.
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
const
|
|
752
|
-
const
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
this.
|
|
798
|
-
|
|
799
|
-
}
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
this.
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
if (
|
|
931
|
-
|
|
932
|
-
} else {
|
|
933
|
-
|
|
934
|
-
}
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
if (
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
case '$
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
case '$
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
if (
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
if (
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
for (const
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
if (
|
|
1186
|
-
|
|
1187
|
-
else if (
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
}
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
const pkVal =
|
|
1272
|
-
if (pkVal !== undefined && pkVal !== null) this._pkIndex.set(pkVal,
|
|
1273
|
-
}
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
for (const field of Object.keys(this._indexes)) {
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
}
|
|
1304
|
-
|
|
1
|
+
// © Vexify 2026 All Rights Reserved.
|
|
2
|
+
/**
|
|
3
|
+
* JSQL Table — 表操作引擎 v2.0
|
|
4
|
+
* B-Tree 自动索引、日期类型、错误码、外键 CASCADE、长度/精度校验
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const Query = require('./query');
|
|
8
|
+
const BTree = require('./btree');
|
|
9
|
+
const { validateDateType, now } = require('./date-types');
|
|
10
|
+
const { createError } = require('./errors');
|
|
11
|
+
|
|
12
|
+
function isSafeRegex(pattern) {
|
|
13
|
+
if (typeof pattern !== 'string') return false;
|
|
14
|
+
if (pattern.length > 256) return false;
|
|
15
|
+
const groups = [];
|
|
16
|
+
let i = 0;
|
|
17
|
+
const n = pattern.length;
|
|
18
|
+
while (i < n) {
|
|
19
|
+
const c = pattern[i];
|
|
20
|
+
if (c === '\\') { i += 2; continue; }
|
|
21
|
+
if (c === '[') {
|
|
22
|
+
while (i < n && pattern[i] !== ']') i++;
|
|
23
|
+
i++;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (c === '(') {
|
|
27
|
+
const nxt = pattern[i + 1];
|
|
28
|
+
if (nxt === '?') {
|
|
29
|
+
const t = pattern[i + 2];
|
|
30
|
+
if (t === ':') {
|
|
31
|
+
groups.push({ hasQuant: false, hasAlt: false });
|
|
32
|
+
i += 3;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (t === '=' || t === '!') {
|
|
36
|
+
let depth = 1, j = i + 3;
|
|
37
|
+
for (; j < n && depth > 0; j++) {
|
|
38
|
+
if (pattern[j] === '\\') { j++; continue; }
|
|
39
|
+
if (pattern[j] === '(') depth++;
|
|
40
|
+
else if (pattern[j] === ')') depth--;
|
|
41
|
+
}
|
|
42
|
+
i = j;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (t === '<') {
|
|
46
|
+
const u = pattern[i + 3];
|
|
47
|
+
if (u === '=' || u === '!') {
|
|
48
|
+
let depth = 1, j = i + 4;
|
|
49
|
+
for (; j < n && depth > 0; j++) {
|
|
50
|
+
if (pattern[j] === '\\') { j++; continue; }
|
|
51
|
+
if (pattern[j] === '(') depth++;
|
|
52
|
+
else if (pattern[j] === ')') depth--;
|
|
53
|
+
}
|
|
54
|
+
i = j;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const gt = pattern.indexOf('>', i + 3);
|
|
58
|
+
if (gt === -1) return false;
|
|
59
|
+
groups.push({ hasQuant: false, hasAlt: false });
|
|
60
|
+
i = gt + 1;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
groups.push({ hasQuant: false, hasAlt: false });
|
|
66
|
+
i++;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (c === ')') {
|
|
70
|
+
const g = groups.pop();
|
|
71
|
+
if (!g) return false;
|
|
72
|
+
let qi = i + 1;
|
|
73
|
+
let quant = null;
|
|
74
|
+
if (pattern[qi] === '+' || pattern[qi] === '*' || pattern[qi] === '?') {
|
|
75
|
+
quant = pattern[qi];
|
|
76
|
+
} else if (pattern[qi] === '{') {
|
|
77
|
+
const close = pattern.indexOf('}', qi);
|
|
78
|
+
if (close === -1) return false;
|
|
79
|
+
quant = pattern.slice(qi, close + 1);
|
|
80
|
+
qi = close + 1;
|
|
81
|
+
}
|
|
82
|
+
if (quant && (g.hasQuant || g.hasAlt)) return false;
|
|
83
|
+
i = qi;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (c === '+' || c === '*' || c === '?') {
|
|
87
|
+
if (groups.length > 0) groups[groups.length - 1].hasQuant = true;
|
|
88
|
+
i++;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (c === '{' && /[0-9]/.test(pattern[i + 1] || '')) {
|
|
92
|
+
const close = pattern.indexOf('}', i);
|
|
93
|
+
if (close === -1) return false;
|
|
94
|
+
if (groups.length > 0) groups[groups.length - 1].hasQuant = true;
|
|
95
|
+
i = close + 1;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (c === '|') {
|
|
99
|
+
if (groups.length > 0) groups[groups.length - 1].hasAlt = true;
|
|
100
|
+
i++;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
i++;
|
|
104
|
+
}
|
|
105
|
+
return groups.length === 0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
class Table {
|
|
109
|
+
constructor(name, schema, db) {
|
|
110
|
+
this._name = name;
|
|
111
|
+
this._schema = schema;
|
|
112
|
+
this._db = db;
|
|
113
|
+
this._rows = [];
|
|
114
|
+
this._indexes = {}; // 旧版 hash 索引(兼容)
|
|
115
|
+
this._btrees = {}; // B-Tree 索引 { fieldName: BTree }
|
|
116
|
+
this._autoIncrement = 0;
|
|
117
|
+
this._dirty = false;
|
|
118
|
+
this._hooks = {
|
|
119
|
+
beforeInsert: [], afterInsert: [],
|
|
120
|
+
beforeUpdate: [], afterUpdate: [],
|
|
121
|
+
beforeRemove: [], afterRemove: []
|
|
122
|
+
};
|
|
123
|
+
this._softDelete = false;
|
|
124
|
+
this._computedFields = {};
|
|
125
|
+
this._btreesDirty = false;
|
|
126
|
+
|
|
127
|
+
this._primaryKey = null;
|
|
128
|
+
this._pkIndex = null;
|
|
129
|
+
this._autoIncrementField = null;
|
|
130
|
+
this._foreignKeys = [];
|
|
131
|
+
this._checkConstraints = [];
|
|
132
|
+
this._defaults = {};
|
|
133
|
+
this._dateFields = {}; // { fieldName: 'date'|'datetime'|'timestamp' }
|
|
134
|
+
|
|
135
|
+
for (const [field, def] of Object.entries(schema)) {
|
|
136
|
+
const isPk = !!(def.primaryKey || def.primary === true);
|
|
137
|
+
if (isPk) this._primaryKey = field;
|
|
138
|
+
if (def.autoIncrement) {
|
|
139
|
+
this._autoIncrementField = field;
|
|
140
|
+
if (isPk) this._primaryKey = field;
|
|
141
|
+
}
|
|
142
|
+
if (def.default !== undefined) {
|
|
143
|
+
this._defaults[field] = def.default;
|
|
144
|
+
}
|
|
145
|
+
if (def.check) {
|
|
146
|
+
this._checkConstraints.push({ field, fn: def.check });
|
|
147
|
+
}
|
|
148
|
+
if (def.foreignKey) {
|
|
149
|
+
this._foreignKeys.push({
|
|
150
|
+
field,
|
|
151
|
+
table: def.foreignKey.table,
|
|
152
|
+
refField: def.foreignKey.field || 'id',
|
|
153
|
+
onDelete: def.foreignKey.onDelete || 'restrict',
|
|
154
|
+
onUpdate: def.foreignKey.onUpdate || 'restrict'
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
if (def.computed) {
|
|
158
|
+
this._computedFields[field] = def.computed;
|
|
159
|
+
}
|
|
160
|
+
// 日期类型
|
|
161
|
+
if (['date', 'datetime', 'timestamp', 'time'].includes(def.type)) {
|
|
162
|
+
this._dateFields[field] = def.type;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (schema._softDelete) {
|
|
167
|
+
this._softDelete = true;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
this._pkIndex = this._primaryKey ? new Map() : null;
|
|
171
|
+
this._cachedSchemaFields = Object.keys(schema).filter(f => f !== '_softDelete');
|
|
172
|
+
this._cachedDateFields = Object.keys(this._dateFields);
|
|
173
|
+
this._cachedUniqueFields = this._cachedSchemaFields.filter(f => schema[f].unique);
|
|
174
|
+
this._cachedRequiredFields = this._cachedSchemaFields.filter(f => schema[f].required);
|
|
175
|
+
this._autoCreateBTrees();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
_autoCreateBTrees() {
|
|
179
|
+
for (const [field, def] of Object.entries(this._schema)) {
|
|
180
|
+
if (field === '_softDelete') continue;
|
|
181
|
+
if (def.primaryKey || def.primary === true || def.unique) {
|
|
182
|
+
this._btrees[field] = new BTree(64, true);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ============================================================
|
|
188
|
+
// 插入
|
|
189
|
+
// ============================================================
|
|
190
|
+
|
|
191
|
+
insert(data) {
|
|
192
|
+
for (const hook of this._hooks.beforeInsert) {
|
|
193
|
+
data = hook({ ...data }) || data;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
data = this._applyDefaults(data);
|
|
197
|
+
data = this._applyComputed(data);
|
|
198
|
+
data = this._validateDataTypes(data);
|
|
199
|
+
this._validateConstraints(data, -1);
|
|
200
|
+
|
|
201
|
+
if (this._autoIncrementField && data[this._autoIncrementField] === undefined) {
|
|
202
|
+
this._autoIncrement++;
|
|
203
|
+
data[this._autoIncrementField] = this._autoIncrement;
|
|
204
|
+
} else if (this._autoIncrementField && data[this._autoIncrementField] > this._autoIncrement) {
|
|
205
|
+
this._autoIncrement = data[this._autoIncrementField];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const rowIndex = this._rows.length;
|
|
209
|
+
this._rows.push({ ...data });
|
|
210
|
+
this._addToIndexes(rowIndex, data);
|
|
211
|
+
this._dirty = true;
|
|
212
|
+
this._db._markDirty(this._name);
|
|
213
|
+
|
|
214
|
+
for (const hook of this._hooks.afterInsert) {
|
|
215
|
+
hook(data);
|
|
216
|
+
}
|
|
217
|
+
this._db._emitChange('insert', this._name, data);
|
|
218
|
+
|
|
219
|
+
return data;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
insertMany(items) {
|
|
223
|
+
if (items.length === 0) return [];
|
|
224
|
+
for (const hook of this._hooks.beforeInsert) {
|
|
225
|
+
const mapped = new Array(items.length);
|
|
226
|
+
for (let i = 0; i < items.length; i++) mapped[i] = hook({ ...items[i] }) || items[i];
|
|
227
|
+
items = mapped;
|
|
228
|
+
}
|
|
229
|
+
const N = items.length;
|
|
230
|
+
const uniqueTracker = {};
|
|
231
|
+
for (const field of this._cachedUniqueFields) {
|
|
232
|
+
uniqueTracker[field] = new Set();
|
|
233
|
+
for (const row of this._rows) {
|
|
234
|
+
if (row[field] !== undefined && row[field] !== null) uniqueTracker[field].add(row[field]);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const dateFields = this._cachedDateFields;
|
|
238
|
+
const hasDateFields = dateFields.length > 0;
|
|
239
|
+
const autoIncField = this._autoIncrementField;
|
|
240
|
+
const defaults = this._defaults;
|
|
241
|
+
const computedFields = this._computedFields;
|
|
242
|
+
const schema = this._schema;
|
|
243
|
+
const checkConstraints = this._checkConstraints;
|
|
244
|
+
const foreignKeys = this._foreignKeys;
|
|
245
|
+
// 批量预分配自增 ID 范围:先扫描显式提供的最大值,一次性推进计数器,
|
|
246
|
+
// 再在循环内用本地序号递增,避免批量内重复读取/写入同一计数器字段。
|
|
247
|
+
const baseAutoInc = this._autoIncrement;
|
|
248
|
+
if (autoIncField) {
|
|
249
|
+
let maxExplicit = 0;
|
|
250
|
+
for (const it of items) {
|
|
251
|
+
const v = it[autoIncField];
|
|
252
|
+
if (v !== undefined && v !== null) {
|
|
253
|
+
const n = Number(v);
|
|
254
|
+
if (!isNaN(n) && n > maxExplicit) maxExplicit = n;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (maxExplicit > baseAutoInc) {
|
|
258
|
+
this._autoIncrement = maxExplicit;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
let autoIncSeq = 0;
|
|
262
|
+
for (let i = 0; i < N; i++) {
|
|
263
|
+
let data = items[i];
|
|
264
|
+
for (const [f, dv] of Object.entries(defaults)) {
|
|
265
|
+
if (data[f] === undefined) data[f] = typeof dv === 'function' ? dv() : dv === 'CURRENT_TIMESTAMP' ? now() : dv;
|
|
266
|
+
}
|
|
267
|
+
for (const f of Object.keys(computedFields)) data[f] = computedFields[f](data);
|
|
268
|
+
if (hasDateFields) {
|
|
269
|
+
for (const f of dateFields) {
|
|
270
|
+
if (data[f] !== undefined && data[f] !== null) data[f] = validateDateType(data[f], this._dateFields[f], f);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
for (const field of this._cachedSchemaFields) {
|
|
274
|
+
const def = schema[field];
|
|
275
|
+
if (def.required && (data[field] === undefined || data[field] === null)
|
|
276
|
+
&& !(def.autoIncrement && data[field] === undefined)) throw createError('ER_BAD_NULL_ERROR', field);
|
|
277
|
+
if (def.unique && data[field] !== undefined) {
|
|
278
|
+
if (uniqueTracker[field].has(data[field])) throw createError('ER_DUP_ENTRY', String(data[field]), field);
|
|
279
|
+
uniqueTracker[field].add(data[field]);
|
|
280
|
+
}
|
|
281
|
+
if (def.length && typeof data[field] === 'string' && data[field].length > def.length) throw createError('ER_DATA_TOO_LONG', field);
|
|
282
|
+
if (def.type === 'integer' || def.type === 'number') {
|
|
283
|
+
const v = def.type === 'integer' ? Number(data[field]) : data[field];
|
|
284
|
+
if (data[field] !== undefined && data[field] !== null && def.min !== undefined && v < def.min) throw createError('ER_OUT_OF_RANGE', field);
|
|
285
|
+
if (data[field] !== undefined && data[field] !== null && def.max !== undefined && v > def.max) throw createError('ER_OUT_OF_RANGE', field);
|
|
286
|
+
}
|
|
287
|
+
if (def.check && data[field] !== undefined && !def.check(data[field], data)) throw createError('ER_CHECK_CONSTRAINT', this._name + '.' + field);
|
|
288
|
+
}
|
|
289
|
+
for (const fk of foreignKeys) {
|
|
290
|
+
if (data[fk.field] !== undefined && data[fk.field] !== null) {
|
|
291
|
+
const refTable = this._resolveTable(fk.table);
|
|
292
|
+
if (refTable) {
|
|
293
|
+
const exists = refTable.findOne({ [fk.refField]: data[fk.field] });
|
|
294
|
+
if (!exists) throw createError('ER_NO_REFERENCED_ROW');
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (autoIncField && data[autoIncField] === undefined) {
|
|
299
|
+
autoIncSeq++;
|
|
300
|
+
data[autoIncField] = baseAutoInc + autoIncSeq;
|
|
301
|
+
this._autoIncrement = data[autoIncField];
|
|
302
|
+
} else if (autoIncField && data[autoIncField] > this._autoIncrement) {
|
|
303
|
+
this._autoIncrement = data[autoIncField];
|
|
304
|
+
}
|
|
305
|
+
const rowIdx = this._rows.length;
|
|
306
|
+
this._rows.push(Object.assign({}, data));
|
|
307
|
+
if (this._pkIndex && this._primaryKey) {
|
|
308
|
+
const pkVal = data[this._primaryKey];
|
|
309
|
+
if (pkVal !== undefined && pkVal !== null) this._pkIndex.set(pkVal, rowIdx);
|
|
310
|
+
}
|
|
311
|
+
for (const [field, tree] of Object.entries(this._btrees)) {
|
|
312
|
+
const val = data[field];
|
|
313
|
+
if (val !== undefined && val !== null) tree.insert(val, rowIdx);
|
|
314
|
+
}
|
|
315
|
+
items[i] = data;
|
|
316
|
+
}
|
|
317
|
+
this._dirty = true;
|
|
318
|
+
this._db._markDirty(this._name);
|
|
319
|
+
for (const hook of this._hooks.afterInsert) {
|
|
320
|
+
for (let i = 0; i < N; i++) hook(items[i]);
|
|
321
|
+
}
|
|
322
|
+
this._db._emitChange('insertMany', this._name, { count: N });
|
|
323
|
+
return items;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
upsert(data) {
|
|
327
|
+
if (this._primaryKey && data[this._primaryKey] !== undefined) {
|
|
328
|
+
const existing = this.findById(data[this._primaryKey]);
|
|
329
|
+
if (existing) {
|
|
330
|
+
this.updateById(data[this._primaryKey], data);
|
|
331
|
+
return this.findById(data[this._primaryKey]);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return this.insert(data);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// ============================================================
|
|
338
|
+
// 查询(支持 B-Tree 加速)
|
|
339
|
+
// ============================================================
|
|
340
|
+
|
|
341
|
+
find(query = {}) {
|
|
342
|
+
let rows = this._getVisibleRows();
|
|
343
|
+
if (Object.keys(query).length > 0) {
|
|
344
|
+
rows = this._applyFilterOptimized(rows, query);
|
|
345
|
+
}
|
|
346
|
+
return rows.map(r => ({ ...r }));
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
findOne(query = {}) {
|
|
350
|
+
let rows = this._getVisibleRows();
|
|
351
|
+
if (Object.keys(query).length > 0) {
|
|
352
|
+
rows = this._applyFilterOptimized(rows, query);
|
|
353
|
+
}
|
|
354
|
+
return rows.length > 0 ? { ...rows[0] } : null;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
exists(query = {}) {
|
|
358
|
+
if (Object.keys(query).length === 0) return this._getVisibleRows().length > 0;
|
|
359
|
+
const rows = this._applyFilterOptimized(this._getVisibleRows(), query);
|
|
360
|
+
return rows.length > 0;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
findById(id) {
|
|
364
|
+
if (!this._primaryKey) throw createError('ER_PARSE_ERROR', 'Table has no primary key');
|
|
365
|
+
// Hash 索引 O(1)
|
|
366
|
+
if (this._pkIndex) {
|
|
367
|
+
const rowIndex = this._pkIndex.get(id);
|
|
368
|
+
if (rowIndex !== undefined) {
|
|
369
|
+
const row = this._rows[rowIndex];
|
|
370
|
+
if (row && !row._deleted) return { ...row };
|
|
371
|
+
}
|
|
372
|
+
return null;
|
|
373
|
+
}
|
|
374
|
+
this._ensureBTrees();
|
|
375
|
+
if (this._btrees[this._primaryKey]) {
|
|
376
|
+
const indices = this._btrees[this._primaryKey].search(id);
|
|
377
|
+
if (indices.length > 0) {
|
|
378
|
+
const row = this._rows[indices[0]];
|
|
379
|
+
if (row && !row._deleted) return { ...row };
|
|
380
|
+
}
|
|
381
|
+
return null;
|
|
382
|
+
}
|
|
383
|
+
return this.findOne({ [this._primaryKey]: id });
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
findAll() {
|
|
387
|
+
return this._getVisibleRows().map(r => ({ ...r }));
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
count(query = {}) {
|
|
391
|
+
if (Object.keys(query).length === 0) return this._getVisibleRows().length;
|
|
392
|
+
return this._applyFilterOptimized(this._getVisibleRows(), query).length;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
where(conditions) {
|
|
396
|
+
return new Query(this, conditions);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// ============================================================
|
|
400
|
+
// 更新
|
|
401
|
+
// ============================================================
|
|
402
|
+
|
|
403
|
+
update(query, updates) {
|
|
404
|
+
for (const hook of this._hooks.beforeUpdate) {
|
|
405
|
+
updates = hook(query, { ...updates }) || updates;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
updates = this._validateDataTypes(updates, true);
|
|
409
|
+
const matched = this._applyFilterOptimized(this._getVisibleRows(), query);
|
|
410
|
+
let count = 0;
|
|
411
|
+
|
|
412
|
+
for (const row of matched) {
|
|
413
|
+
const index = this._rows.indexOf(row);
|
|
414
|
+
if (index === -1) continue;
|
|
415
|
+
|
|
416
|
+
const oldData = { ...this._rows[index] };
|
|
417
|
+
const merged = { ...this._rows[index], ...updates };
|
|
418
|
+
this._validateConstraints(merged, index);
|
|
419
|
+
|
|
420
|
+
this._removeFromIndexes(index, oldData);
|
|
421
|
+
Object.assign(this._rows[index], updates);
|
|
422
|
+
this._addToIndexes(index, this._rows[index]);
|
|
423
|
+
count++;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (count > 0) {
|
|
427
|
+
this._dirty = true;
|
|
428
|
+
this._db._markDirty(this._name);
|
|
429
|
+
for (const hook of this._hooks.afterUpdate) {
|
|
430
|
+
hook(query, updates, count);
|
|
431
|
+
}
|
|
432
|
+
this._db._emitChange('update', this._name, { query, updates, count });
|
|
433
|
+
}
|
|
434
|
+
return count;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
updateById(id, updates) {
|
|
438
|
+
if (!this._primaryKey) throw createError('ER_PARSE_ERROR', 'Table has no primary key');
|
|
439
|
+
for (const hook of this._hooks.beforeUpdate) {
|
|
440
|
+
updates = hook({ [this._primaryKey]: id }, { ...updates }) || updates;
|
|
441
|
+
}
|
|
442
|
+
updates = this._validateDataTypes(updates, true);
|
|
443
|
+
let rowIndex;
|
|
444
|
+
if (this._pkIndex) {
|
|
445
|
+
rowIndex = this._pkIndex.get(id);
|
|
446
|
+
} else {
|
|
447
|
+
this._ensureBTrees();
|
|
448
|
+
if (this._btrees[this._primaryKey]) {
|
|
449
|
+
const indices = this._btrees[this._primaryKey].search(id);
|
|
450
|
+
rowIndex = indices.length > 0 ? indices[0] : undefined;
|
|
451
|
+
} else {
|
|
452
|
+
rowIndex = this._rows.findIndex(r => r[this._primaryKey] === id);
|
|
453
|
+
if (rowIndex === -1) rowIndex = undefined;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
if (rowIndex === undefined) return 0;
|
|
457
|
+
const row = this._rows[rowIndex];
|
|
458
|
+
if (row && row._deleted) return 0;
|
|
459
|
+
const oldData = { ...row };
|
|
460
|
+
const merged = { ...row, ...updates };
|
|
461
|
+
this._validateConstraints(merged, rowIndex);
|
|
462
|
+
this._removeFromIndexes(rowIndex, oldData);
|
|
463
|
+
Object.assign(row, updates);
|
|
464
|
+
this._addToIndexes(rowIndex, row);
|
|
465
|
+
this._dirty = true;
|
|
466
|
+
this._db._markDirty(this._name);
|
|
467
|
+
for (const hook of this._hooks.afterUpdate) {
|
|
468
|
+
hook({ [this._primaryKey]: id }, updates, 1);
|
|
469
|
+
}
|
|
470
|
+
this._db._emitChange('update', this._name, { query: { [this._primaryKey]: id }, updates, count: 1 });
|
|
471
|
+
return 1;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// ============================================================
|
|
475
|
+
// 删除 & 外键 CASCADE
|
|
476
|
+
// ============================================================
|
|
477
|
+
|
|
478
|
+
remove(query = {}) {
|
|
479
|
+
for (const hook of this._hooks.beforeRemove) {
|
|
480
|
+
if (hook(query) === false) return 0;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
if (Object.keys(query).length === 0) {
|
|
484
|
+
return this._truncate();
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const toRemove = this._applyFilterOptimized(this._getVisibleRows(), query);
|
|
488
|
+
let count = 0;
|
|
489
|
+
const removeIndices = [];
|
|
490
|
+
|
|
491
|
+
for (const row of toRemove) {
|
|
492
|
+
const index = this._rows.indexOf(row);
|
|
493
|
+
if (index === -1) continue;
|
|
494
|
+
this._cascadeForeignKeys(row);
|
|
495
|
+
this._removeFromIndexes(index, row);
|
|
496
|
+
removeIndices.push(index);
|
|
497
|
+
count++;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
if (count > 0) {
|
|
501
|
+
removeIndices.sort((a, b) => b - a);
|
|
502
|
+
for (const idx of removeIndices) {
|
|
503
|
+
const lastIdx = this._rows.length - 1;
|
|
504
|
+
if (idx < lastIdx) {
|
|
505
|
+
const lastRow = this._rows[lastIdx];
|
|
506
|
+
this._rows[idx] = lastRow;
|
|
507
|
+
if (this._pkIndex && this._primaryKey) {
|
|
508
|
+
this._pkIndex.set(lastRow[this._primaryKey], idx);
|
|
509
|
+
}
|
|
510
|
+
for (const [field, tree] of Object.entries(this._btrees)) {
|
|
511
|
+
const val = lastRow[field];
|
|
512
|
+
if (val !== undefined && val !== null) {
|
|
513
|
+
tree.remove(val, lastIdx);
|
|
514
|
+
tree.insert(val, idx);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
for (const field of Object.keys(this._indexes)) {
|
|
518
|
+
const val = lastRow[field];
|
|
519
|
+
if (val !== undefined && this._indexes[field].has(val)) {
|
|
520
|
+
const positions = this._indexes[field].get(val);
|
|
521
|
+
const pi = positions.indexOf(lastIdx);
|
|
522
|
+
if (pi !== -1) positions[pi] = idx;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
this._rows.pop();
|
|
527
|
+
}
|
|
528
|
+
this._dirty = true;
|
|
529
|
+
this._db._markDirty(this._name);
|
|
530
|
+
for (const hook of this._hooks.afterRemove) {
|
|
531
|
+
hook(query, count);
|
|
532
|
+
}
|
|
533
|
+
this._db._emitChange('remove', this._name, { query, count });
|
|
534
|
+
}
|
|
535
|
+
return count;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
removeById(id) {
|
|
539
|
+
if (!this._primaryKey) throw createError('ER_PARSE_ERROR', 'Table has no primary key');
|
|
540
|
+
let rowIndex;
|
|
541
|
+
if (this._pkIndex) {
|
|
542
|
+
rowIndex = this._pkIndex.get(id);
|
|
543
|
+
} else {
|
|
544
|
+
this._ensureBTrees();
|
|
545
|
+
if (this._btrees[this._primaryKey]) {
|
|
546
|
+
const indices = this._btrees[this._primaryKey].search(id);
|
|
547
|
+
rowIndex = indices.length > 0 ? indices[0] : undefined;
|
|
548
|
+
} else {
|
|
549
|
+
rowIndex = this._rows.findIndex(r => r[this._primaryKey] === id);
|
|
550
|
+
if (rowIndex === -1) rowIndex = undefined;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
if (rowIndex === undefined) return 0;
|
|
554
|
+
const row = this._rows[rowIndex];
|
|
555
|
+
if (!row || row._deleted) return 0;
|
|
556
|
+
this._cascadeForeignKeys(row);
|
|
557
|
+
this._removeFromIndexes(rowIndex, row);
|
|
558
|
+
const lastIdx = this._rows.length - 1;
|
|
559
|
+
if (rowIndex < lastIdx) {
|
|
560
|
+
const lastRow = this._rows[lastIdx];
|
|
561
|
+
this._rows[rowIndex] = lastRow;
|
|
562
|
+
if (this._pkIndex && this._primaryKey) {
|
|
563
|
+
this._pkIndex.set(lastRow[this._primaryKey], rowIndex);
|
|
564
|
+
}
|
|
565
|
+
for (const [field, tree] of Object.entries(this._btrees)) {
|
|
566
|
+
const val = lastRow[field];
|
|
567
|
+
if (val !== undefined && val !== null) {
|
|
568
|
+
tree.remove(val, lastIdx);
|
|
569
|
+
tree.insert(val, rowIndex);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
for (const field of Object.keys(this._indexes)) {
|
|
573
|
+
const val = lastRow[field];
|
|
574
|
+
if (val !== undefined && this._indexes[field].has(val)) {
|
|
575
|
+
const positions = this._indexes[field].get(val);
|
|
576
|
+
const pi = positions.indexOf(lastIdx);
|
|
577
|
+
if (pi !== -1) positions[pi] = rowIndex;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
this._rows.pop();
|
|
582
|
+
this._dirty = true;
|
|
583
|
+
this._db._markDirty(this._name);
|
|
584
|
+
for (const hook of this._hooks.afterRemove) {
|
|
585
|
+
hook({ [this._primaryKey]: id }, 1);
|
|
586
|
+
}
|
|
587
|
+
this._db._emitChange('remove', this._name, { query: { [this._primaryKey]: id }, count: 1 });
|
|
588
|
+
return 1;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
removeSoft(query = {}) {
|
|
592
|
+
this._softDelete = true;
|
|
593
|
+
return this.remove(query);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
_findRowIndex(id) {
|
|
597
|
+
if (this._pkIndex && this._primaryKey) {
|
|
598
|
+
const idx = this._pkIndex.get(id);
|
|
599
|
+
if (idx !== undefined) return idx;
|
|
600
|
+
}
|
|
601
|
+
for (let i = 0; i < this._rows.length; i++) {
|
|
602
|
+
if (this._rows[i][this._primaryKey] === id) return i;
|
|
603
|
+
}
|
|
604
|
+
return -1;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
restore(query = {}) {
|
|
608
|
+
if (!this._softDelete) throw createError('ER_NOT_SUPPORTED', 'Soft delete is not enabled');
|
|
609
|
+
const matched = this._applyFilter(this._rows, { ...query, _deleted: true });
|
|
610
|
+
for (const row of matched) {
|
|
611
|
+
const index = this._rows.indexOf(row);
|
|
612
|
+
if (index !== -1) {
|
|
613
|
+
delete this._rows[index]._deleted;
|
|
614
|
+
delete this._rows[index]._deletedAt;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
return matched.length;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
truncate() {
|
|
621
|
+
return this._truncate();
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
_truncate() {
|
|
625
|
+
const count = this._rows.length;
|
|
626
|
+
this._rows = [];
|
|
627
|
+
if (this._pkIndex) this._pkIndex.clear();
|
|
628
|
+
for (const key of Object.keys(this._btrees)) {
|
|
629
|
+
this._btrees[key].clear();
|
|
630
|
+
}
|
|
631
|
+
this._indexes = {};
|
|
632
|
+
this._autoIncrement = 0;
|
|
633
|
+
this._dirty = true;
|
|
634
|
+
this._db._markDirty(this._name);
|
|
635
|
+
return count;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// ============================================================
|
|
639
|
+
// ALTER TABLE
|
|
640
|
+
// ============================================================
|
|
641
|
+
|
|
642
|
+
addColumn(field, definition) {
|
|
643
|
+
if (this._schema[field]) throw createError('ER_DUP_FIELDNAME', field);
|
|
644
|
+
this._schema[field] = definition;
|
|
645
|
+
if (definition.default !== undefined) {
|
|
646
|
+
this._defaults[field] = definition.default;
|
|
647
|
+
for (const row of this._rows) {
|
|
648
|
+
if (row[field] === undefined) {
|
|
649
|
+
row[field] = typeof definition.default === 'function' ? definition.default() : definition.default;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
if (definition.check) this._checkConstraints.push({ field, fn: definition.check });
|
|
654
|
+
if (definition.foreignKey) {
|
|
655
|
+
this._foreignKeys.push({
|
|
656
|
+
field,
|
|
657
|
+
table: definition.foreignKey.table,
|
|
658
|
+
refField: definition.foreignKey.field || 'id',
|
|
659
|
+
onDelete: definition.foreignKey.onDelete || 'restrict',
|
|
660
|
+
onUpdate: definition.foreignKey.onUpdate || 'restrict'
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
if (definition.computed) this._computedFields[field] = definition.computed;
|
|
664
|
+
if (['date', 'datetime', 'timestamp', 'time'].includes(definition.type)) {
|
|
665
|
+
this._dateFields[field] = definition.type;
|
|
666
|
+
}
|
|
667
|
+
if (definition.primaryKey || definition.unique) {
|
|
668
|
+
this._btrees[field] = new BTree(64, true);
|
|
669
|
+
this._rebuildBTree(field);
|
|
670
|
+
}
|
|
671
|
+
this._dirty = true;
|
|
672
|
+
this._db._markDirty(this._name);
|
|
673
|
+
return this;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
dropColumn(field) {
|
|
677
|
+
if (!this._schema[field]) throw createError('ER_CANT_DROP_FIELD', field);
|
|
678
|
+
delete this._schema[field];
|
|
679
|
+
delete this._defaults[field];
|
|
680
|
+
delete this._btrees[field];
|
|
681
|
+
delete this._indexes[field];
|
|
682
|
+
this._checkConstraints = this._checkConstraints.filter(c => c.field !== field);
|
|
683
|
+
this._foreignKeys = this._foreignKeys.filter(fk => fk.field !== field);
|
|
684
|
+
delete this._computedFields[field];
|
|
685
|
+
delete this._dateFields[field];
|
|
686
|
+
for (const row of this._rows) {
|
|
687
|
+
delete row[field];
|
|
688
|
+
}
|
|
689
|
+
if (this._primaryKey === field) this._primaryKey = null;
|
|
690
|
+
if (this._autoIncrementField === field) this._autoIncrementField = null;
|
|
691
|
+
this._dirty = true;
|
|
692
|
+
this._db._markDirty(this._name);
|
|
693
|
+
return this;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
renameColumn(oldName, newName) {
|
|
697
|
+
if (!this._schema[oldName]) throw createError('ER_BAD_FIELD_ERROR', oldName, this._name);
|
|
698
|
+
if (this._schema[newName]) throw createError('ER_DUP_FIELDNAME', newName);
|
|
699
|
+
this._schema[newName] = this._schema[oldName];
|
|
700
|
+
delete this._schema[oldName];
|
|
701
|
+
if (this._defaults[oldName] !== undefined) {
|
|
702
|
+
this._defaults[newName] = this._defaults[oldName];
|
|
703
|
+
delete this._defaults[oldName];
|
|
704
|
+
}
|
|
705
|
+
if (this._btrees[oldName]) {
|
|
706
|
+
this._btrees[newName] = this._btrees[oldName];
|
|
707
|
+
delete this._btrees[oldName];
|
|
708
|
+
}
|
|
709
|
+
for (const row of this._rows) {
|
|
710
|
+
row[newName] = row[oldName];
|
|
711
|
+
delete row[oldName];
|
|
712
|
+
}
|
|
713
|
+
if (this._primaryKey === oldName) this._primaryKey = newName;
|
|
714
|
+
if (this._autoIncrementField === oldName) this._autoIncrementField = newName;
|
|
715
|
+
this._dirty = true;
|
|
716
|
+
this._db._markDirty(this._name);
|
|
717
|
+
return this;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
modifyColumn(field, definition) {
|
|
721
|
+
if (!this._schema[field]) throw createError('ER_BAD_FIELD_ERROR', field, this._name);
|
|
722
|
+
this._schema[field] = { ...this._schema[field], ...definition };
|
|
723
|
+
if (definition.default !== undefined) this._defaults[field] = definition.default;
|
|
724
|
+
this._checkConstraints = this._checkConstraints.filter(c => c.field !== field);
|
|
725
|
+
if (definition.check) this._checkConstraints.push({ field, fn: definition.check });
|
|
726
|
+
if (definition.primaryKey || definition.unique) {
|
|
727
|
+
if (!this._btrees[field]) this._btrees[field] = new BTree(64, true);
|
|
728
|
+
this._rebuildBTree(field);
|
|
729
|
+
}
|
|
730
|
+
this._dirty = true;
|
|
731
|
+
this._db._markDirty(this._name);
|
|
732
|
+
return this;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
describe() {
|
|
736
|
+
return {
|
|
737
|
+
name: this._name,
|
|
738
|
+
columns: { ...this._schema },
|
|
739
|
+
primaryKey: this._primaryKey,
|
|
740
|
+
rowCount: this._getVisibleRows().length,
|
|
741
|
+
btreeIndexes: Object.keys(this._btrees),
|
|
742
|
+
hashIndexes: Object.keys(this._indexes),
|
|
743
|
+
softDelete: this._softDelete,
|
|
744
|
+
foreignKeys: this._foreignKeys.map(fk => ({ field: fk.field, table: fk.table }))
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
selectInto(newTableName, newSchema, query = {}) {
|
|
749
|
+
const rows = this._applyFilterOptimized(this._getVisibleRows(), query);
|
|
750
|
+
this._db.createTable(newTableName, newSchema);
|
|
751
|
+
const newTable = this._db._tables[newTableName];
|
|
752
|
+
for (const row of rows) {
|
|
753
|
+
newTable.insert(row);
|
|
754
|
+
}
|
|
755
|
+
return newTable;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
textSearch(field, terms) {
|
|
759
|
+
const searchTerms = Array.isArray(terms) ? terms : terms.split(/\s+/);
|
|
760
|
+
const rows = this._getVisibleRows();
|
|
761
|
+
const scored = [];
|
|
762
|
+
for (const row of rows) {
|
|
763
|
+
const value = String(row[field] || '').toLowerCase();
|
|
764
|
+
let score = 0;
|
|
765
|
+
for (const term of searchTerms) {
|
|
766
|
+
const t = term.toLowerCase();
|
|
767
|
+
const idx = value.indexOf(t);
|
|
768
|
+
if (idx !== -1) {
|
|
769
|
+
score += 10 + (idx === 0 ? 5 : 0) + (value === t ? 20 : 0)
|
|
770
|
+
+ (value.length - idx) / value.length * 5;
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
if (score > 0) scored.push({ ...row, _score: score });
|
|
774
|
+
}
|
|
775
|
+
return scored.sort((a, b) => b._score - a._score);
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
// ============================================================
|
|
779
|
+
// 索引
|
|
780
|
+
// ============================================================
|
|
781
|
+
|
|
782
|
+
createIndex(field) {
|
|
783
|
+
this._indexes[field] = new Map();
|
|
784
|
+
for (let i = 0; i < this._rows.length; i++) {
|
|
785
|
+
const value = this._rows[i][field];
|
|
786
|
+
if (value !== undefined) {
|
|
787
|
+
if (!this._indexes[field].has(value)) {
|
|
788
|
+
this._indexes[field].set(value, []);
|
|
789
|
+
}
|
|
790
|
+
this._indexes[field].get(value).push(i);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
return this._indexes[field];
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
dropIndex(field) {
|
|
797
|
+
delete this._indexes[field];
|
|
798
|
+
delete this._btrees[field];
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* 创建 B-Tree 索引(O(log n) 查询)
|
|
803
|
+
*/
|
|
804
|
+
createBTreeIndex(field, unique = false) {
|
|
805
|
+
this._btrees[field] = new BTree(64, unique);
|
|
806
|
+
this._rebuildBTree(field);
|
|
807
|
+
return this._btrees[field];
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
_rebuildBTree(field) {
|
|
811
|
+
if (this._btrees[field]) {
|
|
812
|
+
this._btrees[field].clear();
|
|
813
|
+
for (let i = 0; i < this._rows.length; i++) {
|
|
814
|
+
const val = this._rows[i][field];
|
|
815
|
+
if (val !== undefined && val !== null) {
|
|
816
|
+
this._btrees[field].insert(val, i);
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
_ensureBTrees() {
|
|
823
|
+
if (!this._btreesDirty) return;
|
|
824
|
+
this._btreesDirty = false;
|
|
825
|
+
for (const field of Object.keys(this._btrees)) {
|
|
826
|
+
this._btrees[field].clear();
|
|
827
|
+
}
|
|
828
|
+
for (let i = 0; i < this._rows.length; i++) {
|
|
829
|
+
const row = this._rows[i];
|
|
830
|
+
for (const [field, tree] of Object.entries(this._btrees)) {
|
|
831
|
+
const v = row[field];
|
|
832
|
+
if (v !== undefined && v !== null) tree.insert(v, i);
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
_rebuildAllBTrees() {
|
|
838
|
+
for (const field of Object.keys(this._btrees)) {
|
|
839
|
+
this._rebuildBTree(field);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
// ============================================================
|
|
844
|
+
// 事件钩子
|
|
845
|
+
// ============================================================
|
|
846
|
+
|
|
847
|
+
on(event, callback) {
|
|
848
|
+
if (this._hooks[event]) {
|
|
849
|
+
this._hooks[event].push(callback);
|
|
850
|
+
}
|
|
851
|
+
return this;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
off(event, callback) {
|
|
855
|
+
if (this._hooks[event]) {
|
|
856
|
+
this._hooks[event] = this._hooks[event].filter(cb => cb !== callback);
|
|
857
|
+
}
|
|
858
|
+
return this;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
// ============================================================
|
|
862
|
+
// 内部: 过滤器(B-Tree 加速)
|
|
863
|
+
// ============================================================
|
|
864
|
+
|
|
865
|
+
_rowsFromIndices(indices) {
|
|
866
|
+
const subset = [];
|
|
867
|
+
for (let i = 0; i < indices.length; i++) {
|
|
868
|
+
const row = this._rows[indices[i]];
|
|
869
|
+
if (row) subset.push(row);
|
|
870
|
+
}
|
|
871
|
+
return subset;
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
_likePrefix(pattern) {
|
|
875
|
+
if (typeof pattern !== 'string' || pattern.length === 0) return null;
|
|
876
|
+
if (pattern[0] === '%' || pattern[0] === '_') return null;
|
|
877
|
+
let end = pattern.length;
|
|
878
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
879
|
+
const c = pattern[i];
|
|
880
|
+
if (c === '%' || c === '_') { end = i; break; }
|
|
881
|
+
}
|
|
882
|
+
return end > 0 ? pattern.slice(0, end) : null;
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
_applyFilterOptimized(rows, query) {
|
|
886
|
+
this._ensureBTrees();
|
|
887
|
+
for (const [field, condition] of Object.entries(query)) {
|
|
888
|
+
if (field === '$or' || field === '$and' || field === '$not') continue;
|
|
889
|
+
if (typeof condition !== 'object' || condition === null || Array.isArray(condition) || condition instanceof RegExp) {
|
|
890
|
+
if (this._btrees[field]) {
|
|
891
|
+
const indices = this._btrees[field].search(condition);
|
|
892
|
+
if (indices.length === 0) return [];
|
|
893
|
+
return this._applyFilter(this._rowsFromIndices(indices), query);
|
|
894
|
+
}
|
|
895
|
+
if (this._indexes[field] && this._indexes[field].has(condition)) {
|
|
896
|
+
return this._applyFilter(this._rowsFromIndices(this._indexes[field].get(condition)), query);
|
|
897
|
+
}
|
|
898
|
+
if (this._indexes[field]) return [];
|
|
899
|
+
}
|
|
900
|
+
if (condition && typeof condition === 'object') {
|
|
901
|
+
if (this._btrees[field] && Array.isArray(condition.$in) && condition.$in.length > 0) {
|
|
902
|
+
const indices = this._btrees[field].searchMany(condition.$in);
|
|
903
|
+
if (indices.length === 0) return [];
|
|
904
|
+
return this._applyFilter(this._rowsFromIndices(indices), query);
|
|
905
|
+
}
|
|
906
|
+
if (this._btrees[field] && typeof condition.$like === 'string') {
|
|
907
|
+
const prefix = this._likePrefix(condition.$like);
|
|
908
|
+
if (prefix) {
|
|
909
|
+
const indices = this._btrees[field].prefix(prefix);
|
|
910
|
+
if (indices.length === 0) return [];
|
|
911
|
+
return this._applyFilter(this._rowsFromIndices(indices), query);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
if (this._btrees[field] && (condition.$gt !== undefined || condition.$gte !== undefined ||
|
|
915
|
+
condition.$lt !== undefined || condition.$lte !== undefined || condition.$between)) {
|
|
916
|
+
let min, max, exclusiveMin = false, exclusiveMax = false;
|
|
917
|
+
if (condition.$between && Array.isArray(condition.$between) && condition.$between.length === 2) {
|
|
918
|
+
min = condition.$between[0];
|
|
919
|
+
max = condition.$between[1];
|
|
920
|
+
} else {
|
|
921
|
+
if (condition.$gte !== undefined) min = condition.$gte;
|
|
922
|
+
else if (condition.$gt !== undefined) { min = condition.$gt; exclusiveMin = true; }
|
|
923
|
+
if (condition.$lte !== undefined) max = condition.$lte;
|
|
924
|
+
else if (condition.$lt !== undefined) { max = condition.$lt; exclusiveMax = true; }
|
|
925
|
+
}
|
|
926
|
+
if (min === undefined && max === undefined) continue;
|
|
927
|
+
let indices;
|
|
928
|
+
if (min !== undefined && max !== undefined) {
|
|
929
|
+
indices = this._btrees[field].range(min, max);
|
|
930
|
+
} else if (min !== undefined) {
|
|
931
|
+
indices = exclusiveMin ? this._btrees[field].greaterThan(min) : this._btrees[field].greaterThanEqual(min);
|
|
932
|
+
} else {
|
|
933
|
+
indices = exclusiveMax ? this._btrees[field].lessThan(max) : this._btrees[field].lessThanEqual(max);
|
|
934
|
+
}
|
|
935
|
+
return this._applyFilter(this._rowsFromIndices(indices), query);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
return this._applyFilter(rows, query);
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
_applyFilter(rows, query) {
|
|
943
|
+
const result = [];
|
|
944
|
+
for (let i = 0; i < rows.length; i++) {
|
|
945
|
+
if (this._matchRow(rows[i], query)) {
|
|
946
|
+
result.push(rows[i]);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
return result;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
_matchRow(row, query) {
|
|
953
|
+
for (const [field, condition] of Object.entries(query)) {
|
|
954
|
+
if (field === '$or') {
|
|
955
|
+
if (!Array.isArray(condition) || !condition.some(sub => this._matchRow(row, sub))) return false;
|
|
956
|
+
continue;
|
|
957
|
+
}
|
|
958
|
+
if (field === '$and') {
|
|
959
|
+
if (!Array.isArray(condition) || !condition.every(sub => this._matchRow(row, sub))) return false;
|
|
960
|
+
continue;
|
|
961
|
+
}
|
|
962
|
+
if (field === '$not') {
|
|
963
|
+
if (this._matchRow(row, condition)) return false;
|
|
964
|
+
continue;
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
const value = this._resolvePath(row, field);
|
|
968
|
+
|
|
969
|
+
if (condition !== null && typeof condition === 'object' && !Array.isArray(condition) && !(condition instanceof RegExp)) {
|
|
970
|
+
const keys = Object.keys(condition);
|
|
971
|
+
const isOperator = keys.every(k => k.startsWith('$'));
|
|
972
|
+
if (isOperator && keys.length > 0) {
|
|
973
|
+
for (const [op, target] of Object.entries(condition)) {
|
|
974
|
+
if (!this._matchOperator(value, op, target)) return false;
|
|
975
|
+
}
|
|
976
|
+
} else {
|
|
977
|
+
if (value !== null && typeof value === 'object') {
|
|
978
|
+
if (!this._matchRow(value, condition)) return false;
|
|
979
|
+
} else {
|
|
980
|
+
return false;
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
} else {
|
|
984
|
+
if (value !== condition) return false;
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
return true;
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
_matchOperator(value, op, target) {
|
|
991
|
+
switch (op) {
|
|
992
|
+
case '$eq': return value === target;
|
|
993
|
+
case '$ne': return value !== target;
|
|
994
|
+
case '$gt': return value > target;
|
|
995
|
+
case '$gte': return value >= target;
|
|
996
|
+
case '$lt': return value < target;
|
|
997
|
+
case '$lte': return value <= target;
|
|
998
|
+
case '$in': return Array.isArray(target) && target.includes(value);
|
|
999
|
+
case '$nin': return Array.isArray(target) && !target.includes(value);
|
|
1000
|
+
case '$like':
|
|
1001
|
+
if (typeof value !== 'string' || typeof target !== 'string') return false;
|
|
1002
|
+
{
|
|
1003
|
+
const escaped = target
|
|
1004
|
+
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
1005
|
+
.replace(/%/g, '.*')
|
|
1006
|
+
.replace(/_/g, '.');
|
|
1007
|
+
return new RegExp('^' + escaped + '$', 'i').test(value);
|
|
1008
|
+
}
|
|
1009
|
+
case '$regex':
|
|
1010
|
+
if (target instanceof RegExp) return target.test(String(value));
|
|
1011
|
+
if (!isSafeRegex(target)) throw createError('ER_BAD_REGEX', String(target));
|
|
1012
|
+
return new RegExp(target).test(String(value));
|
|
1013
|
+
case '$exists':
|
|
1014
|
+
return target ? value !== undefined : value === undefined;
|
|
1015
|
+
case '$between':
|
|
1016
|
+
return Array.isArray(target) && target.length === 2 && value >= target[0] && value <= target[1];
|
|
1017
|
+
case '$and':
|
|
1018
|
+
return Array.isArray(target) && target.every(sub => this._matchRow({ _val: value }, { _val: sub }));
|
|
1019
|
+
case '$or':
|
|
1020
|
+
return Array.isArray(target) && target.some(sub => this._matchRow({ _val: value }, { _val: sub }));
|
|
1021
|
+
case '$not':
|
|
1022
|
+
return !this._matchRow({ _val: value }, { _val: target });
|
|
1023
|
+
case '$contains':
|
|
1024
|
+
return Array.isArray(value) && value.includes(target);
|
|
1025
|
+
case '$type':
|
|
1026
|
+
return typeof value === target;
|
|
1027
|
+
case '$size':
|
|
1028
|
+
return Array.isArray(value) && value.length === target;
|
|
1029
|
+
case '$elemMatch':
|
|
1030
|
+
return Array.isArray(value) && value.some(el =>
|
|
1031
|
+
typeof el === 'object' ? this._matchRow(el, target) : el === target);
|
|
1032
|
+
case '$isNull':
|
|
1033
|
+
return target ? value === null || value === undefined : value !== null && value !== undefined;
|
|
1034
|
+
case '$startsWith':
|
|
1035
|
+
return typeof value === 'string' && value.startsWith(target);
|
|
1036
|
+
case '$endsWith':
|
|
1037
|
+
return typeof value === 'string' && value.endsWith(target);
|
|
1038
|
+
case '$search':
|
|
1039
|
+
if (typeof value !== 'string') return false;
|
|
1040
|
+
const terms = Array.isArray(target) ? target : [target];
|
|
1041
|
+
return terms.every(t => value.toLowerCase().includes(t.toLowerCase()));
|
|
1042
|
+
case '$textSearch':
|
|
1043
|
+
if (typeof value !== 'string') return false;
|
|
1044
|
+
const sTerms = Array.isArray(target) ? target : target.split(/\s+/);
|
|
1045
|
+
return sTerms.some(t => value.toLowerCase().includes(t.toLowerCase()));
|
|
1046
|
+
case '$inSub':
|
|
1047
|
+
if (!target || !target.table) return false;
|
|
1048
|
+
const subRows = target.table._applyFilter(target.table._rows, target.query || {});
|
|
1049
|
+
return subRows.some(r => r[target.field] === value);
|
|
1050
|
+
case '$notInSub':
|
|
1051
|
+
if (!target || !target.table) return false;
|
|
1052
|
+
const subRows2 = target.table._applyFilter(target.table._rows, target.query || {});
|
|
1053
|
+
return !subRows2.some(r => r[target.field] === value);
|
|
1054
|
+
case '$expr':
|
|
1055
|
+
if (typeof target === 'function') return target(value);
|
|
1056
|
+
return false;
|
|
1057
|
+
default:
|
|
1058
|
+
return false;
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
_resolvePath(row, field) {
|
|
1063
|
+
if (field.includes('.') && !field.startsWith('$')) {
|
|
1064
|
+
const parts = field.split('.');
|
|
1065
|
+
let value = row;
|
|
1066
|
+
for (const part of parts) {
|
|
1067
|
+
if (value === null || value === undefined) return undefined;
|
|
1068
|
+
value = value[part];
|
|
1069
|
+
}
|
|
1070
|
+
return value;
|
|
1071
|
+
}
|
|
1072
|
+
return row[field];
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
// ============================================================
|
|
1076
|
+
// 内部: 约束校验
|
|
1077
|
+
// ============================================================
|
|
1078
|
+
|
|
1079
|
+
_validateConstraints(data, excludeIndex, uniqueTracker) {
|
|
1080
|
+
for (const [field, def] of Object.entries(this._schema)) {
|
|
1081
|
+
if (field === '_softDelete') continue;
|
|
1082
|
+
|
|
1083
|
+
// NOT NULL
|
|
1084
|
+
if (def.required && (data[field] === undefined || data[field] === null)
|
|
1085
|
+
&& !(def.autoIncrement && data[field] === undefined)) {
|
|
1086
|
+
throw createError('ER_BAD_NULL_ERROR', field);
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
// PRIMARY KEY 唯一性
|
|
1090
|
+
if ((def.primaryKey || def.primary === true) && data[field] !== undefined && data[field] !== null) {
|
|
1091
|
+
let dup = false;
|
|
1092
|
+
if (uniqueTracker && uniqueTracker[field]) {
|
|
1093
|
+
dup = uniqueTracker[field].has(data[field]);
|
|
1094
|
+
} else if (this._pkIndex) {
|
|
1095
|
+
const idx = this._pkIndex.get(data[field]);
|
|
1096
|
+
dup = idx !== undefined && idx !== null && idx !== excludeIndex;
|
|
1097
|
+
} else {
|
|
1098
|
+
const idx = this._rows.findIndex(r => r[field] === data[field]);
|
|
1099
|
+
dup = idx !== -1 && idx !== excludeIndex;
|
|
1100
|
+
}
|
|
1101
|
+
if (dup) {
|
|
1102
|
+
throw createError('ER_DUP_ENTRY', String(data[field]), field);
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
// UNIQUE(B-Tree 优化 O(log n))
|
|
1107
|
+
if (def.unique && data[field] !== undefined) {
|
|
1108
|
+
if (uniqueTracker && uniqueTracker[field]) {
|
|
1109
|
+
if (uniqueTracker[field].has(data[field])) {
|
|
1110
|
+
throw createError('ER_DUP_ENTRY', String(data[field]), field);
|
|
1111
|
+
}
|
|
1112
|
+
uniqueTracker[field].add(data[field]);
|
|
1113
|
+
} else if (this._btrees[field]) {
|
|
1114
|
+
const found = this._btrees[field].search(data[field]);
|
|
1115
|
+
if (found.length > 0) {
|
|
1116
|
+
for (const idx of found) {
|
|
1117
|
+
if (idx !== excludeIndex && this._rows[idx] && this._rows[idx][field] === data[field]) {
|
|
1118
|
+
throw createError('ER_DUP_ENTRY', String(data[field]), field);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
} else {
|
|
1123
|
+
for (let i = 0; i < this._rows.length; i++) {
|
|
1124
|
+
if (i === excludeIndex) continue;
|
|
1125
|
+
if (this._rows[i][field] === data[field]) {
|
|
1126
|
+
throw createError('ER_DUP_ENTRY', String(data[field]), field);
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
// CHECK
|
|
1133
|
+
for (const { field: cf, fn } of this._checkConstraints) {
|
|
1134
|
+
if (cf === field && data[field] !== undefined && !fn(data[field], data)) {
|
|
1135
|
+
throw createError('ER_CHECK_CONSTRAINT', this._name + '.' + field);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
// 长度限制
|
|
1140
|
+
if (def.length && typeof data[field] === 'string' && data[field].length > def.length) {
|
|
1141
|
+
throw createError('ER_DATA_TOO_LONG', field);
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
// 数值范围
|
|
1145
|
+
if (def.type === 'integer' && data[field] !== undefined && data[field] !== null) {
|
|
1146
|
+
const v = Number(data[field]);
|
|
1147
|
+
if (def.min !== undefined && v < def.min) throw createError('ER_OUT_OF_RANGE', field);
|
|
1148
|
+
if (def.max !== undefined && v > def.max) throw createError('ER_OUT_OF_RANGE', field);
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
// FOREIGN KEY 校验
|
|
1153
|
+
for (const fk of this._foreignKeys) {
|
|
1154
|
+
if (data[fk.field] !== undefined && data[fk.field] !== null) {
|
|
1155
|
+
const refTable = this._resolveTable(fk.table);
|
|
1156
|
+
if (refTable) {
|
|
1157
|
+
const exists = refTable.findOne({ [fk.refField]: data[fk.field] });
|
|
1158
|
+
if (!exists) {
|
|
1159
|
+
throw createError('ER_NO_REFERENCED_ROW');
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
_resolveTable(tableRef) {
|
|
1167
|
+
if (typeof tableRef === 'string') {
|
|
1168
|
+
return this._db._tables[tableRef] || this._db._attached[tableRef]?._tables[tableRef];
|
|
1169
|
+
}
|
|
1170
|
+
return tableRef;
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
/**
|
|
1174
|
+
* 级联外键操作:当删除当前表的行时,查找所有引用此表的子表
|
|
1175
|
+
* 并执行 CASCADE 或 SET NULL
|
|
1176
|
+
*/
|
|
1177
|
+
_cascadeForeignKeys(row) {
|
|
1178
|
+
for (const [, table] of Object.entries(this._db._tables)) {
|
|
1179
|
+
if (table._name === this._name) continue;
|
|
1180
|
+
for (const fk of table._foreignKeys) {
|
|
1181
|
+
const refTable = table._resolveTable(fk.table);
|
|
1182
|
+
if (!refTable || refTable._name !== this._name) continue;
|
|
1183
|
+
|
|
1184
|
+
// 检查子表行是否引用当前行
|
|
1185
|
+
if (fk.onDelete === 'cascade') {
|
|
1186
|
+
table.remove({ [fk.field]: row[fk.refField] });
|
|
1187
|
+
} else if (fk.onDelete === 'set null') {
|
|
1188
|
+
table.update({ [fk.field]: row[fk.refField] }, { [fk.field]: null });
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
_validateDataTypes(data, partial = false) {
|
|
1195
|
+
const result = { ...data };
|
|
1196
|
+
for (const [field, type] of Object.entries(this._dateFields)) {
|
|
1197
|
+
if (partial && data[field] === undefined) continue;
|
|
1198
|
+
if (data[field] !== undefined && data[field] !== null) {
|
|
1199
|
+
result[field] = validateDateType(data[field], type, field);
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
for (const [field, def] of Object.entries(this._schema)) {
|
|
1203
|
+
if (field === '_softDelete') continue;
|
|
1204
|
+
if (partial && data[field] === undefined) continue;
|
|
1205
|
+
const v = data[field];
|
|
1206
|
+
if (v === undefined || v === null) continue;
|
|
1207
|
+
const t = String(def.type || '').toLowerCase();
|
|
1208
|
+
if (t === 'integer' || t === 'int' || t === 'bigint') {
|
|
1209
|
+
if (typeof v === 'boolean') throw createError('ER_TRUNCATED_WRONG_VALUE', t, v);
|
|
1210
|
+
if (typeof v === 'number') {
|
|
1211
|
+
if (Number.isInteger(v)) result[field] = v;
|
|
1212
|
+
else throw createError('ER_TRUNCATED_WRONG_VALUE', t, v);
|
|
1213
|
+
} else if (typeof v === 'string' && v.trim() !== '' && Number.isInteger(Number(v))) {
|
|
1214
|
+
result[field] = Number(v);
|
|
1215
|
+
} else {
|
|
1216
|
+
throw createError('ER_TRUNCATED_WRONG_VALUE', t, v);
|
|
1217
|
+
}
|
|
1218
|
+
} else if (t === 'float' || t === 'double' || t === 'number' || t === 'real') {
|
|
1219
|
+
if (typeof v === 'boolean') throw createError('ER_TRUNCATED_WRONG_VALUE', t, v);
|
|
1220
|
+
if (typeof v === 'number') result[field] = v;
|
|
1221
|
+
else if (typeof v === 'string' && v.trim() !== '' && !Number.isNaN(Number(v))) result[field] = Number(v);
|
|
1222
|
+
else throw createError('ER_TRUNCATED_WRONG_VALUE', t, v);
|
|
1223
|
+
} else if (t === 'boolean' || t === 'bool') {
|
|
1224
|
+
if (typeof v === 'boolean') result[field] = v;
|
|
1225
|
+
else if (v === 0 || v === 1) result[field] = !!v;
|
|
1226
|
+
else if (typeof v === 'string' && ['true', 'false', '1', '0'].includes(v.toLowerCase())) {
|
|
1227
|
+
result[field] = v.toLowerCase() === 'true' || v === '1';
|
|
1228
|
+
} else {
|
|
1229
|
+
throw createError('ER_TRUNCATED_WRONG_VALUE', t, v);
|
|
1230
|
+
}
|
|
1231
|
+
} else if (t === 'string' || t === 'text' || t === 'varchar' || t === 'char') {
|
|
1232
|
+
if (typeof v === 'string') result[field] = v;
|
|
1233
|
+
else if (typeof v === 'number' || typeof v === 'boolean' || v instanceof Date) result[field] = String(v);
|
|
1234
|
+
else if (Buffer.isBuffer(v)) result[field] = v.toString('utf8');
|
|
1235
|
+
else throw createError('ER_TRUNCATED_WRONG_VALUE', t, v);
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
return result;
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
// ============================================================
|
|
1242
|
+
// 内部: 辅助
|
|
1243
|
+
// ============================================================
|
|
1244
|
+
|
|
1245
|
+
_applyDefaults(data) {
|
|
1246
|
+
const result = { ...data };
|
|
1247
|
+
for (const [field, defaultValue] of Object.entries(this._defaults)) {
|
|
1248
|
+
if (result[field] === undefined) {
|
|
1249
|
+
result[field] = typeof defaultValue === 'function'
|
|
1250
|
+
? defaultValue()
|
|
1251
|
+
: (defaultValue === 'CURRENT_TIMESTAMP' ? now() : defaultValue);
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
return result;
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
_applyComputed(data) {
|
|
1258
|
+
for (const [field, fn] of Object.entries(this._computedFields)) {
|
|
1259
|
+
data[field] = fn(data);
|
|
1260
|
+
}
|
|
1261
|
+
return data;
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
_getVisibleRows() {
|
|
1265
|
+
if (this._softDelete) return this._rows.filter(r => !r._deleted);
|
|
1266
|
+
return this._rows;
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
_addToIndexes(rowIndex, data) {
|
|
1270
|
+
if (this._pkIndex && this._primaryKey) {
|
|
1271
|
+
const pkVal = data[this._primaryKey];
|
|
1272
|
+
if (pkVal !== undefined && pkVal !== null) this._pkIndex.set(pkVal, rowIndex);
|
|
1273
|
+
}
|
|
1274
|
+
for (const field of Object.keys(this._indexes)) {
|
|
1275
|
+
const value = data[field];
|
|
1276
|
+
if (value !== undefined) {
|
|
1277
|
+
if (!this._indexes[field].has(value)) {
|
|
1278
|
+
this._indexes[field].set(value, []);
|
|
1279
|
+
}
|
|
1280
|
+
this._indexes[field].get(value).push(rowIndex);
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
for (const [field, tree] of Object.entries(this._btrees)) {
|
|
1284
|
+
const value = data[field];
|
|
1285
|
+
if (value !== undefined && value !== null) {
|
|
1286
|
+
tree.insert(value, rowIndex);
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
_removeFromIndexes(rowIndex, data) {
|
|
1292
|
+
if (this._pkIndex && this._primaryKey) {
|
|
1293
|
+
this._pkIndex.delete(data[this._primaryKey]);
|
|
1294
|
+
}
|
|
1295
|
+
for (const field of Object.keys(this._indexes)) {
|
|
1296
|
+
const value = data[field];
|
|
1297
|
+
if (value !== undefined && this._indexes[field].has(value)) {
|
|
1298
|
+
const positions = this._indexes[field].get(value).filter(p => p !== rowIndex);
|
|
1299
|
+
if (positions.length === 0) {
|
|
1300
|
+
this._indexes[field].delete(value);
|
|
1301
|
+
} else {
|
|
1302
|
+
this._indexes[field].set(value, positions);
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
for (const [field, tree] of Object.entries(this._btrees)) {
|
|
1307
|
+
const value = data[field];
|
|
1308
|
+
if (value !== undefined && value !== null) {
|
|
1309
|
+
tree.remove(value, rowIndex);
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
_rebuildPKIndex() {
|
|
1315
|
+
if (!this._pkIndex || !this._primaryKey) return;
|
|
1316
|
+
this._pkIndex.clear();
|
|
1317
|
+
for (let i = 0; i < this._rows.length; i++) {
|
|
1318
|
+
const pkVal = this._rows[i][this._primaryKey];
|
|
1319
|
+
if (pkVal !== undefined && pkVal !== null) this._pkIndex.set(pkVal, i);
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
_adjustBTreeAfterRemove(removedIndex) {
|
|
1324
|
+
this._rebuildPKIndex();
|
|
1325
|
+
for (const field of Object.keys(this._btrees)) {
|
|
1326
|
+
this._rebuildBTree(field);
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
_loadRows(rows) {
|
|
1331
|
+
this._rows = rows;
|
|
1332
|
+
if (this._autoIncrementField) {
|
|
1333
|
+
for (const row of rows) {
|
|
1334
|
+
const val = row[this._autoIncrementField];
|
|
1335
|
+
if (typeof val === 'number' && val > this._autoIncrement) {
|
|
1336
|
+
this._autoIncrement = val;
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
this._rebuildPKIndex();
|
|
1341
|
+
this._rebuildAllBTrees();
|
|
1342
|
+
for (const field of Object.keys(this._indexes)) {
|
|
1343
|
+
this.createIndex(field);
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
toJSON() {
|
|
1348
|
+
return this._rows;
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1305
1352
|
module.exports = Table;
|