jsql-neo 6.8.27 → 6.10.2
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 +9 -4
- package/bin/jsql-server-control.js +58 -14
- package/index.d.ts +3 -0
- package/index.js +1 -1
- package/lib/bigint_json.js +13 -1
- package/lib/btree.js +12 -3
- package/lib/cache.js +12 -1
- package/lib/database.js +272 -36
- package/lib/jsql_format.js +22 -9
- package/lib/migrate.js +184 -98
- package/lib/mod.js +48 -13
- package/lib/mongo_server.js +652 -246
- package/lib/mysql_compat.js +54 -11
- package/lib/mysql_server.js +248 -65
- package/lib/nedb_compat.js +7 -1
- package/lib/objectid.js +14 -6
- package/lib/pg_server.js +385 -9
- package/lib/plugin.js +34 -3
- package/lib/query.js +12 -6
- package/lib/redis_server.js +102 -42
- package/lib/sql.js +446 -48
- package/lib/table.js +111 -41
- package/lib/tui.js +28 -9
- package/lib/web_ui.js +33 -2
- package/package.json +4 -4
- package/test/alter-table.test.js +11 -5
- package/test/compat-v1.test.js +1 -1
- package/test/extra-coverage.test.js +6 -7
- package/test/issue-258-300.test.js +6 -0
- package/test/issue-312-320.test.js +11 -4
- package/test/issue-326.test.js +42 -0
- package/test/issue-327-418.test.js +1 -1
- package/test/issue-336.test.js +56 -0
- package/test/issue-353.test.js +34 -0
- package/test/issue-356.test.js +27 -0
- package/test/issue-357-365.test.js +1 -1
- package/test/issue-361.test.js +55 -0
- package/test/issue-370.test.js +43 -0
- package/test/issue-373.test.js +40 -0
- package/test/issue-374.test.js +62 -0
- package/test/issue-378.test.js +39 -0
- package/test/issue-379.test.js +39 -0
- package/test/issue-38.test.js +2 -2
- package/test/issue-380.test.js +27 -0
- package/test/issue-383-402.test.js +4 -3
- package/test/issue-384.test.js +41 -0
- package/test/issue-392.test.js +25 -0
- package/test/issue-397.test.js +44 -0
- package/test/issue-403.test.js +47 -0
- package/test/issue-404.test.js +58 -0
- package/test/issue-407.test.js +40 -0
- package/test/issue-408.test.js +92 -0
- package/test/issue-409.test.js +32 -0
- package/test/issue-410.test.js +36 -0
- package/test/issue-411.test.js +38 -0
- package/test/issue-412.test.js +50 -0
- package/test/issue-414.test.js +46 -0
- package/test/issue-417.test.js +56 -0
- package/test/issue-420.test.js +87 -0
- package/test/issue-421.test.js +30 -0
- package/test/issue-422.test.js +33 -0
- package/test/issue-424.test.js +44 -0
- package/test/issue-425.test.js +22 -0
- package/test/issue-426.test.js +22 -0
- package/test/issue-427.test.js +36 -0
- package/test/issue-429.test.js +38 -0
- package/test/issue-431.test.js +41 -0
- package/test/issue-432.test.js +37 -0
- package/test/issue-436.test.js +47 -0
- package/test/issue-445-449.test.js +14 -7
- package/test/issue-455-459.test.js +6 -3
- package/test/issue-474.test.js +59 -0
- package/test/issue-475.test.js +38 -0
- package/test/issue-476.test.js +50 -0
- package/test/issue-477.test.js +54 -0
- package/test/issue-478.test.js +52 -0
- package/test/issue-479.test.js +44 -0
- package/test/issue-480.test.js +42 -0
- package/test/issue-481.test.js +51 -0
- package/test/issue-482.test.js +36 -0
- package/test/issue-483.test.js +57 -0
- package/test/issue-485.test.js +47 -0
- package/test/issue-486.test.js +69 -0
- package/test/issue-487.test.js +54 -0
- package/test/issue-488.test.js +69 -0
- package/test/issue-490.test.js +20 -0
- package/test/issue-491.test.js +21 -0
- package/test/issue-492.test.js +19 -0
- package/test/issue-493.test.js +30 -0
- package/test/issue-494.test.js +82 -0
- package/test/issue-495.test.js +86 -0
- package/test/issue-496.test.js +98 -0
- package/test/issue-497.test.js +29 -0
- package/test/issue-498.test.js +32 -0
- package/test/issue-499.test.js +41 -0
- package/test/issue-500.test.js +39 -0
- package/test/issue-501-506.test.js +29 -0
- package/test/issue-502.test.js +18 -0
- package/test/issue-503.test.js +43 -0
- package/test/issue-504.test.js +83 -0
- package/test/issue-505.test.js +29 -0
- package/test/issue-529.test.js +39 -0
- package/test/issue-530.test.js +69 -0
- package/test/issue-536.test.js +46 -0
- package/test/issue-537.test.js +52 -0
- package/test/issue-539.test.js +65 -0
- package/test/issue-540.test.js +31 -0
- package/test/issue-545.test.js +38 -0
- package/test/issue-546.test.js +68 -0
- package/test/issue-547.test.js +27 -0
- package/test/issue-548.test.js +68 -0
- package/test/issue-549.test.js +26 -0
- package/test/issue-550.test.js +52 -0
- package/test/issue-551.test.js +34 -0
- package/test/issue-552.test.js +30 -0
- package/test/issue-553.test.js +29 -0
- package/test/issue-554.test.js +29 -0
- package/test/issue-555.test.js +22 -0
- package/test/issue-556.test.js +25 -0
- package/test/issue-565.test.js +32 -0
- package/test/issue-566.test.js +30 -0
- package/test/issue-567.test.js +56 -0
- package/test/issue-568.test.js +46 -0
- package/test/issue-569.test.js +25 -0
- package/test/issue-570.test.js +30 -0
- package/test/issue-571.test.js +32 -0
- package/test/issue-572.test.js +64 -0
- package/test/issue-573.test.js +71 -0
- package/test/issue-574.test.js +63 -0
- package/test/issue-581.test.js +46 -0
- package/test/issue-59.test.js +17 -4
- package/test/issue-69-70.test.js +1 -1
- package/test/issue-mysql-harness.js +260 -0
- package/test/issue-pg5-helper.js +135 -0
- package/test/mongo-arrays.test.js +63 -0
- package/test/mongo-objectid.test.js +2 -2
- package/test/mongo-persistence.test.js +3 -3
- package/test/mongo-update-dates.test.js +128 -0
- package/test/wal-recovery.test.js +17 -9
package/lib/migrate.js
CHANGED
|
@@ -28,11 +28,11 @@ function normalizeSchema(schema) {
|
|
|
28
28
|
const d = typeof def === 'string' ? parseFieldShorthand(def.toLowerCase()) : { ...def };
|
|
29
29
|
if (!d.type) d.type = typeof d === 'object' ? 'any' : 'string';
|
|
30
30
|
if (d.type === 'int' || d.type === 'bigint' || d.type === 'smallint' || d.type === 'tinyint') d.type = 'integer';
|
|
31
|
-
|
|
31
|
+
// #483:text/varchar/char 是导出 schema 携带的原始文本子类型,往返后必须可区分
|
|
32
32
|
if (d.type === 'double' || d.type === 'real' || d.type === 'decimal' || d.type === 'numeric') d.type = 'float';
|
|
33
33
|
if (d.type === 'bool') d.type = 'boolean';
|
|
34
|
-
|
|
35
|
-
if (d.maxLength) { d.length = d.maxLength; delete d.maxLength; }
|
|
34
|
+
// #483:列长度是 schema 的既有约束,不能无条件删除;maxLength 别名仅在缺失 length 时回迁
|
|
35
|
+
if (d.length == null && d.maxLength != null) { d.length = d.maxLength; delete d.maxLength; }
|
|
36
36
|
out[name] = d;
|
|
37
37
|
}
|
|
38
38
|
return out;
|
|
@@ -75,7 +75,11 @@ function parseValue(str, type) {
|
|
|
75
75
|
return str;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
/**
|
|
79
|
+
* #481:惰性逐行解析。importFromCSV 边取边分批写入,避免把整个文件物化成 rows 数组。
|
|
80
|
+
* 解析前的体积/行数护栏与 parseCSV 完全一致。
|
|
81
|
+
*/
|
|
82
|
+
function* parseCSVStream(text, opts = {}) {
|
|
79
83
|
if (typeof text !== 'string') {
|
|
80
84
|
throw new Error('parseCSV: csv must be a string');
|
|
81
85
|
}
|
|
@@ -91,17 +95,23 @@ function parseCSV(text, opts = {}) {
|
|
|
91
95
|
const maxRows = opts.maxRows == null ? DEFAULT_MAX_ROWS : opts.maxRows;
|
|
92
96
|
const isFiniteMaxRows = typeof maxRows === 'number' && Number.isFinite(maxRows);
|
|
93
97
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
let rowsSeen = 0;
|
|
99
|
+
const emitRow = () => {
|
|
100
|
+
if (isFiniteMaxRows && rowsSeen + 1 > maxRows) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`CSV too many rows: ${rowsSeen + 1} exceeds limit of ${maxRows} ` +
|
|
103
|
+
`(raise opts.maxRows or split the file)`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
rowsSeen++;
|
|
100
107
|
const r = row;
|
|
101
108
|
row = [];
|
|
102
|
-
rows.push(r);
|
|
103
109
|
return r;
|
|
104
110
|
};
|
|
111
|
+
// 把 `field += c` 改为数组收集中间 join —— 与 splitStatements 同修 O(n²)
|
|
112
|
+
let fieldBuf = [];
|
|
113
|
+
const flushField = () => { const s = fieldBuf.join(''); fieldBuf.length = 0; return s; };
|
|
114
|
+
let row = [];
|
|
105
115
|
let inQuotes = false;
|
|
106
116
|
let i = 0;
|
|
107
117
|
const n = text.length;
|
|
@@ -123,36 +133,30 @@ function parseCSV(text, opts = {}) {
|
|
|
123
133
|
if (c === '\n' || c === '\r') {
|
|
124
134
|
if (c === '\r' && text[i + 1] === '\n') i++;
|
|
125
135
|
row.push(flushField());
|
|
126
|
-
if (row.length > 1 || row[0] !== '')
|
|
127
|
-
|
|
128
|
-
throw new Error(
|
|
129
|
-
`CSV too many rows: ${rows.length + 1} exceeds limit of ${maxRows} ` +
|
|
130
|
-
`(raise opts.maxRows or split the file)`
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
flushRow();
|
|
134
|
-
} else {
|
|
135
|
-
row = [];
|
|
136
|
-
}
|
|
136
|
+
if (row.length > 1 || row[0] !== '') yield emitRow();
|
|
137
|
+
else row = [];
|
|
137
138
|
i++;
|
|
138
139
|
continue;
|
|
139
140
|
}
|
|
140
141
|
fieldBuf.push(c);
|
|
141
142
|
i++;
|
|
142
143
|
}
|
|
144
|
+
// #482:EOF 时引号仍未闭合属于格式错误,不能静默把换行并进字段
|
|
145
|
+
if (inQuotes) {
|
|
146
|
+
const line = text.slice(0, i).split('\n').length;
|
|
147
|
+
throw new Error(
|
|
148
|
+
`CSV unterminated quoted field at line ${line}, position ${i} ` +
|
|
149
|
+
`(a quoted field must be closed before end of input)`
|
|
150
|
+
);
|
|
151
|
+
}
|
|
143
152
|
if (fieldBuf.length > 0 || row.length > 0) {
|
|
144
153
|
row.push(flushField());
|
|
145
|
-
if (row.length > 1 || row[0] !== '')
|
|
146
|
-
if (isFiniteMaxRows && rows.length + 1 > maxRows) {
|
|
147
|
-
throw new Error(
|
|
148
|
-
`CSV too many rows: ${rows.length + 1} exceeds limit of ${maxRows} ` +
|
|
149
|
-
`(raise opts.maxRows or split the file)`
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
flushRow();
|
|
153
|
-
}
|
|
154
|
+
if (row.length > 1 || row[0] !== '') yield emitRow();
|
|
154
155
|
}
|
|
155
|
-
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function parseCSV(text, opts = {}) {
|
|
159
|
+
return [...parseCSVStream(text, opts)];
|
|
156
160
|
}
|
|
157
161
|
|
|
158
162
|
function toCSV(rows, columns) {
|
|
@@ -240,6 +244,10 @@ async function importFromJSON(engine, data, opts = {}) {
|
|
|
240
244
|
} else {
|
|
241
245
|
tables = data;
|
|
242
246
|
}
|
|
247
|
+
// #476:契约是「表名 → { schema, rows }」映射,顶层数组既不能用下标当表名,也不能静默零导入
|
|
248
|
+
if (Array.isArray(tables)) {
|
|
249
|
+
throw new Error('importFromJSON: invalid import payload: expected an object map of tables, got an array');
|
|
250
|
+
}
|
|
243
251
|
// 兼容单表形状:exportTableToJSON 返回 { table, schema, rows }
|
|
244
252
|
if (tables && !Array.isArray(tables) && typeof tables === 'object'
|
|
245
253
|
&& tables.table && tables.schema && !tables[tables.table]) {
|
|
@@ -248,44 +256,99 @@ async function importFromJSON(engine, data, opts = {}) {
|
|
|
248
256
|
tables[single.table] = single;
|
|
249
257
|
}
|
|
250
258
|
const created = [];
|
|
259
|
+
const errors = [];
|
|
260
|
+
let skipped = 0;
|
|
251
261
|
let inserted = 0;
|
|
252
|
-
const result = { created, inserted };
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
262
|
+
const result = { created, inserted, errors, skipped: 0 };
|
|
263
|
+
|
|
264
|
+
// #479/#478:先校验整批 payload,非法条目进 errors 通道(strict 下直接抛错,且尚未写入任何表)
|
|
265
|
+
const valid = [];
|
|
266
|
+
for (const [name, t] of Object.entries(tables || {})) {
|
|
267
|
+
if (!t || !t.schema) {
|
|
268
|
+
if (opts.strict) throw new Error(`importFromJSON: table '${name}': missing schema`);
|
|
269
|
+
errors.push({ table: name, reason: 'missing schema', type: t === null ? 'null' : typeof t });
|
|
270
|
+
skipped++;
|
|
271
|
+
continue;
|
|
258
272
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
273
|
+
if (t.rows !== undefined && !Array.isArray(t.rows)) {
|
|
274
|
+
if (opts.strict) throw new Error(`importFromJSON: table '${name}': rows must be an array (got ${typeof t.rows})`);
|
|
275
|
+
errors.push({ table: name, reason: 'rows must be an array', type: typeof t.rows });
|
|
276
|
+
skipped++;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
valid.push([name, t]);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// #361:整批导入包事务,任一张表失败即回滚本次全部 DDL/DML。
|
|
283
|
+
// 引擎不提供 begin/commit/rollback 时退回补偿清理(删本次新建表、还原被覆盖表)。
|
|
284
|
+
const canTx = typeof engine.begin === 'function' && typeof engine.commit === 'function'
|
|
285
|
+
&& typeof engine.rollback === 'function';
|
|
286
|
+
let txActive = false;
|
|
287
|
+
if (canTx) {
|
|
288
|
+
try { engine.begin(); txActive = true; }
|
|
289
|
+
catch (_) { /* 调用方已有事务,改用补偿清理 */ }
|
|
290
|
+
}
|
|
291
|
+
const touched = [];
|
|
292
|
+
try {
|
|
293
|
+
for (const [name, t] of valid) {
|
|
294
|
+
let backup = null;
|
|
295
|
+
if (engine.hasTable(name)) {
|
|
296
|
+
if (!overwrite) throw new Error(`Table '${name}' already exists; pass { overwrite: true } to replace it`);
|
|
297
|
+
if (!txActive) {
|
|
298
|
+
backup = { schema: engine.getTableSchema(name), rows: await engine.find(name, {}, { limit: 1e9, offset: 0 }) };
|
|
299
|
+
}
|
|
300
|
+
await engine.dropTable(name);
|
|
278
301
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
302
|
+
touched.push({ name, backup });
|
|
303
|
+
await engine.createTable(name, normalizeSchema(t.schema));
|
|
304
|
+
created.push(name);
|
|
305
|
+
if (Array.isArray(t.rows) && t.rows.length > 0) {
|
|
306
|
+
// #454:只保留目标 schema 内的列,schema 外的幽灵字段不得写进存储行。
|
|
307
|
+
const schemaCols = new Set(Object.keys(normalizeSchema(t.schema)));
|
|
308
|
+
const ghostCols = new Set();
|
|
309
|
+
// 分块 insert —— 避免全量 rows.map 先攒再 insert
|
|
310
|
+
const chunkSize = opts.chunkSize != null ? opts.chunkSize : DEFAULT_CHUNK_SIZE;
|
|
311
|
+
for (let i = 0; i < t.rows.length; i += chunkSize) {
|
|
312
|
+
const chunk = t.rows.slice(i, i + chunkSize).map(r => {
|
|
313
|
+
const out = {};
|
|
314
|
+
for (const k of Object.keys(r)) {
|
|
315
|
+
if (schemaCols.has(k)) out[k] = r[k];
|
|
316
|
+
else ghostCols.add(k);
|
|
317
|
+
}
|
|
318
|
+
return out;
|
|
319
|
+
});
|
|
320
|
+
const ids = await engine.insert(name, chunk);
|
|
321
|
+
inserted += Array.isArray(ids) ? ids.length : chunk.length;
|
|
322
|
+
}
|
|
323
|
+
if (ghostCols.size > 0) {
|
|
324
|
+
(result.warnings = result.warnings || []).push({
|
|
325
|
+
kind: 'unknownColumn',
|
|
326
|
+
columns: [...ghostCols].slice(0, 50)
|
|
327
|
+
});
|
|
328
|
+
if (opts.strictColumns) throw new Error(`importFromJSON: columns not in schema: ${[...ghostCols].join(', ')}`);
|
|
329
|
+
}
|
|
285
330
|
}
|
|
286
331
|
}
|
|
332
|
+
if (txActive) engine.commit();
|
|
333
|
+
result.inserted = inserted;
|
|
334
|
+
result.skipped = skipped;
|
|
335
|
+
return result;
|
|
336
|
+
} catch (e) {
|
|
337
|
+
if (txActive) {
|
|
338
|
+
try { engine.rollback(); } catch (_) { /* 不覆盖原始异常 */ }
|
|
339
|
+
} else {
|
|
340
|
+
for (const { name, backup } of touched.reverse()) {
|
|
341
|
+
try {
|
|
342
|
+
if (engine.hasTable(name)) await engine.dropTable(name);
|
|
343
|
+
if (backup) {
|
|
344
|
+
await engine.createTable(name, backup.schema);
|
|
345
|
+
if (backup.rows && backup.rows.length > 0) await engine.insert(name, backup.rows);
|
|
346
|
+
}
|
|
347
|
+
} catch (_) { /* 尽力补偿,不覆盖原始异常 */ }
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
throw e;
|
|
287
351
|
}
|
|
288
|
-
return result;
|
|
289
352
|
}
|
|
290
353
|
|
|
291
354
|
/* ---------- CSV ---------- */
|
|
@@ -300,18 +363,38 @@ async function exportTableToCSV(engine, table) {
|
|
|
300
363
|
|
|
301
364
|
async function importFromCSV(engine, table, csv, opts = {}) {
|
|
302
365
|
const schema = opts.schema || await engine.getTableSchema(table);
|
|
303
|
-
//
|
|
304
|
-
const
|
|
305
|
-
|
|
366
|
+
// #362/#454:行宽、幽灵列、重复表头统一走 warnings 通道,strictRows 决定是否中止
|
|
367
|
+
const warnings = [];
|
|
368
|
+
// #481:流式解析,不再 parseCSV 一次性物化整份文件
|
|
369
|
+
const iter = parseCSVStream(csv, opts);
|
|
306
370
|
let columns;
|
|
307
371
|
let start = 0;
|
|
308
372
|
if (opts.header !== false) {
|
|
309
|
-
|
|
373
|
+
const first = iter.next();
|
|
374
|
+
if (first.done) return { inserted: 0 };
|
|
375
|
+
columns = first.value;
|
|
310
376
|
start = 1;
|
|
311
377
|
} else if (schema) {
|
|
312
378
|
columns = Object.keys(schema);
|
|
313
379
|
} else {
|
|
314
|
-
|
|
380
|
+
const first = iter.next();
|
|
381
|
+
if (first.done) return { inserted: 0 };
|
|
382
|
+
columns = first.value.map((_, i) => 'col' + (i + 1));
|
|
383
|
+
}
|
|
384
|
+
// #477:重复表头让后列静默覆盖前列,必须显式告警(列名 + 出现位置)
|
|
385
|
+
const firstIndex = new Map();
|
|
386
|
+
const dupMap = new Map();
|
|
387
|
+
columns.forEach((c, j) => {
|
|
388
|
+
if (firstIndex.has(c)) {
|
|
389
|
+
if (!dupMap.has(c)) dupMap.set(c, [firstIndex.get(c)]);
|
|
390
|
+
dupMap.get(c).push(j);
|
|
391
|
+
} else firstIndex.set(c, j);
|
|
392
|
+
});
|
|
393
|
+
if (dupMap.size > 0) {
|
|
394
|
+
for (const [column, indices] of dupMap) warnings.push({ kind: 'duplicateHeader', column, indices });
|
|
395
|
+
if (opts.strictRows) {
|
|
396
|
+
throw new Error(`CSV duplicate header column(s): ${[...dupMap.keys()].join(', ')}`);
|
|
397
|
+
}
|
|
315
398
|
}
|
|
316
399
|
if (!engine.hasTable(table)) {
|
|
317
400
|
if (!schema) {
|
|
@@ -324,38 +407,38 @@ async function importFromCSV(engine, table, csv, opts = {}) {
|
|
|
324
407
|
const schemaCols = liveSchema ? new Set(Object.keys(liveSchema)) : new Set();
|
|
325
408
|
const chunkSize = opts.chunkSize != null ? opts.chunkSize : DEFAULT_CHUNK_SIZE;
|
|
326
409
|
let inserted = 0;
|
|
327
|
-
// #362:行宽与表头不一致此前完全静默 —— 多出来的字段被丢掉,缺的列补 NULL,
|
|
328
|
-
// 导入后行数对得上、数据却缺一块。这里统计并按 strictRows 决定是否中止。
|
|
329
|
-
const warnings = [];
|
|
330
410
|
let rowNo = start;
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
row
|
|
411
|
+
let batch = [];
|
|
412
|
+
const flush = async () => {
|
|
413
|
+
if (batch.length === 0) return;
|
|
414
|
+
const ids = await engine.insert(table, batch);
|
|
415
|
+
inserted += Array.isArray(ids) ? ids.length : batch.length;
|
|
416
|
+
batch = [];
|
|
417
|
+
};
|
|
418
|
+
for (const raw of iter) {
|
|
419
|
+
rowNo++;
|
|
420
|
+
if (raw.length !== columns.length) {
|
|
421
|
+
const kind = raw.length > columns.length ? 'extra' : 'missing';
|
|
422
|
+
const n = Math.abs(raw.length - columns.length);
|
|
423
|
+
if (warnings.length < 50) warnings.push({ row: rowNo, kind, count: n, expected: columns.length, actual: raw.length });
|
|
424
|
+
if (opts.strictRows) throw new Error(`CSV row ${rowNo}: expected ${columns.length} fields, got ${raw.length}`);
|
|
425
|
+
}
|
|
426
|
+
const row = {};
|
|
427
|
+
for (let j = 0; j < columns.length; j++) {
|
|
428
|
+
if (liveSchema && !schemaCols.has(columns[j])) {
|
|
429
|
+
// #454:schema 外的幽灵列丢弃,记入 warnings(与 #362 行宽告警同一通道)。
|
|
430
|
+
if (warnings.length < 50) warnings.push({ row: rowNo, kind: 'unknownColumn', column: columns[j] });
|
|
431
|
+
if (opts.strictColumns) throw new Error(`CSV row ${rowNo}: column '${columns[j]}' is not in table schema`);
|
|
432
|
+
continue;
|
|
352
433
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
|
|
434
|
+
const type = liveSchema && liveSchema[columns[j]] ? liveSchema[columns[j]].type : 'string';
|
|
435
|
+
row[columns[j]] = parseValue(raw[j], type);
|
|
436
|
+
}
|
|
437
|
+
if (row.id === null || row.id === undefined || row.id === '') delete row.id;
|
|
438
|
+
batch.push(row);
|
|
439
|
+
if (batch.length >= chunkSize) await flush();
|
|
358
440
|
}
|
|
441
|
+
await flush();
|
|
359
442
|
const out = { inserted };
|
|
360
443
|
if (warnings.length > 0) out.warnings = warnings;
|
|
361
444
|
return out;
|
|
@@ -423,6 +506,8 @@ async function exportToFile(engine, table, filePath, opts = {}) {
|
|
|
423
506
|
const schema = await engine.getTableSchema(table);
|
|
424
507
|
if (!schema) throw new Error(`Table '${table}' does not exist`);
|
|
425
508
|
const columns = Object.keys(schema);
|
|
509
|
+
// #480:与 Database#exportCSV 对齐,递归创建缺失的父目录
|
|
510
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
426
511
|
const stream = fs.createWriteStream(filePath, { encoding: 'utf8' });
|
|
427
512
|
// 防止磁盘写满等异步错误在 write 回调之外变成未捕获异常
|
|
428
513
|
stream.on('error', () => {});
|
|
@@ -468,6 +553,7 @@ async function exportToFile(engine, table, filePath, opts = {}) {
|
|
|
468
553
|
module.exports = {
|
|
469
554
|
normalizeSchema,
|
|
470
555
|
parseCSV,
|
|
556
|
+
parseCSVStream,
|
|
471
557
|
toCSV,
|
|
472
558
|
csvLine,
|
|
473
559
|
iterateTable,
|
package/lib/mod.js
CHANGED
|
@@ -105,13 +105,31 @@ class ModuleManager {
|
|
|
105
105
|
return this._modules.find(m => m.name === name) || null;
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
//
|
|
111
|
-
|
|
112
|
-
// 必须要求 cwd + path.sep 前缀,或 resolved 恰好等于 cwd(目录场景)
|
|
108
|
+
/** 防止 path traversal:resolved 必须落在 cwd 之下(复用同一边界规则) */
|
|
109
|
+
_assertInsideCwd(filePath) {
|
|
110
|
+
// startsWith(cwd) 不够——/etc/passwd 匹配 /etc 前缀;要求 cwd + sep 前缀
|
|
111
|
+
const resolved = path.resolve(filePath);
|
|
113
112
|
const cwdWithSep = process.cwd() + path.sep;
|
|
114
|
-
if (!resolved.startsWith(cwdWithSep))
|
|
113
|
+
if (!resolved.startsWith(cwdWithSep)) {
|
|
114
|
+
throw new Error('Module path outside working directory: ' + filePath);
|
|
115
|
+
}
|
|
116
|
+
return resolved;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** 从持久化配置恢复的条目在每次加载前都要重新校验边界;越界则告警并跳过 (#486) */
|
|
120
|
+
_loadStoredModule(entry, strict = false) {
|
|
121
|
+
try {
|
|
122
|
+
this._assertInsideCwd(entry.path);
|
|
123
|
+
} catch (e) {
|
|
124
|
+
if (strict) throw e;
|
|
125
|
+
console.warn(`[mod] Skipping module '${entry.name}': ${e.message}`);
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
return this._loadModuleFile(entry.path);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
add(address, opts = {}) {
|
|
132
|
+
const resolved = this._assertInsideCwd(address);
|
|
115
133
|
if (!fs.existsSync(resolved)) {
|
|
116
134
|
throw new Error(`Module file not found: ${resolved}`);
|
|
117
135
|
}
|
|
@@ -149,7 +167,7 @@ class ModuleManager {
|
|
|
149
167
|
enabling.add(name);
|
|
150
168
|
|
|
151
169
|
try {
|
|
152
|
-
const plugin = this.
|
|
170
|
+
const plugin = this._loadStoredModule(m, true);
|
|
153
171
|
|
|
154
172
|
if (plugin.depends && plugin.depends.length > 0) {
|
|
155
173
|
for (const dep of plugin.depends) {
|
|
@@ -177,7 +195,16 @@ class ModuleManager {
|
|
|
177
195
|
// P3-21: 不应全量 require 所有模块来检查依赖——只检查已启用的
|
|
178
196
|
for (const other of this._modules) {
|
|
179
197
|
if (!other.enabled) continue;
|
|
180
|
-
|
|
198
|
+
let plugin;
|
|
199
|
+
try {
|
|
200
|
+
plugin = this._loadStoredModule(other);
|
|
201
|
+
} catch (e) {
|
|
202
|
+
// #539: 回扫只读 depends 声明,个别损坏的依赖模块不应阻断
|
|
203
|
+
// 目标模块自身的禁用;跳过该模块,继续处理其余级联项。
|
|
204
|
+
console.warn(`[mod] Skipping broken module '${other.name}' during disable('${name}'): ${e.message}`);
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (!plugin) continue;
|
|
181
208
|
if (plugin.depends && plugin.depends.includes(name)) {
|
|
182
209
|
this.disable(other.name);
|
|
183
210
|
}
|
|
@@ -205,8 +232,15 @@ class ModuleManager {
|
|
|
205
232
|
applyTo(db) {
|
|
206
233
|
const enabled = this._sortEnabled();
|
|
207
234
|
for (const entry of enabled) {
|
|
208
|
-
|
|
209
|
-
|
|
235
|
+
// #384: 逐模块失败隔离,与 enableAll() 口径对齐——单个模块
|
|
236
|
+
// 加载/安装抛错只跳过该模块,不阻断排在后面的其它已启用模块。
|
|
237
|
+
try {
|
|
238
|
+
const plugin = this._loadStoredModule(entry);
|
|
239
|
+
if (!plugin) continue;
|
|
240
|
+
db.use(plugin);
|
|
241
|
+
} catch (e) {
|
|
242
|
+
if (process.env.JSQL_DEBUG) console.warn(`[mod] Skipping module '${entry.name}' in applyTo: ${e.message}`);
|
|
243
|
+
}
|
|
210
244
|
}
|
|
211
245
|
return db;
|
|
212
246
|
}
|
|
@@ -214,7 +248,7 @@ class ModuleManager {
|
|
|
214
248
|
api(name) {
|
|
215
249
|
const m = this.find(name);
|
|
216
250
|
if (!m) throw new Error(`Module not found: ${name}`);
|
|
217
|
-
const plugin = this.
|
|
251
|
+
const plugin = this._loadStoredModule(m, true);
|
|
218
252
|
return plugin.api || null;
|
|
219
253
|
}
|
|
220
254
|
|
|
@@ -234,7 +268,8 @@ class ModuleManager {
|
|
|
234
268
|
if (cache.has(entry.path)) {
|
|
235
269
|
plugin = cache.get(entry.path);
|
|
236
270
|
} else {
|
|
237
|
-
plugin = this.
|
|
271
|
+
plugin = this._loadStoredModule(entry);
|
|
272
|
+
if (!plugin) return 0; // 越界条目不参与排序与应用
|
|
238
273
|
cache.set(entry.path, plugin);
|
|
239
274
|
}
|
|
240
275
|
|
|
@@ -266,7 +301,7 @@ class ModuleManager {
|
|
|
266
301
|
get(name) {
|
|
267
302
|
const m = this.find(name);
|
|
268
303
|
if (!m) throw new Error(`Module not found: ${name}`);
|
|
269
|
-
return this.
|
|
304
|
+
return this._loadStoredModule(m, true);
|
|
270
305
|
}
|
|
271
306
|
|
|
272
307
|
_loadModuleFile(filePath) {
|