mm_sqlite 1.5.3 → 1.5.4
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/index.js +73 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -257,6 +257,79 @@ SQLite.prototype.exec = async function (sql, params = [], timeout) {
|
|
|
257
257
|
return ret;
|
|
258
258
|
};
|
|
259
259
|
|
|
260
|
+
/**
|
|
261
|
+
* 执行多语句SQL脚本(专用于导入)
|
|
262
|
+
* 逐条执行DML语句统计变更行数,其他语句使用exec执行
|
|
263
|
+
* @param {string} sql - SQL脚本内容,可含多条以分号分隔的语句
|
|
264
|
+
* @returns {Promise<object>} 执行结果统计对象
|
|
265
|
+
* @throws {TypeError} 当sql参数无效时
|
|
266
|
+
*/
|
|
267
|
+
SQLite.prototype.runSql = async function (sql) {
|
|
268
|
+
// 参数校验
|
|
269
|
+
if (typeof sql !== 'string' || sql.trim() === '') {
|
|
270
|
+
throw new TypeError('sql must be non-empty string');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// 注意:同 scope 下为单例共享,this.error/this.sql 并发时以最后一次写入为准
|
|
274
|
+
this.error = null;
|
|
275
|
+
this.sql = '';
|
|
276
|
+
let conn = null;
|
|
277
|
+
let ret = { success: false, statements: 0, affected_rows: 0, error: null };
|
|
278
|
+
try {
|
|
279
|
+
// 获取连接
|
|
280
|
+
conn = await this.getConn();
|
|
281
|
+
this.sql = sql;
|
|
282
|
+
|
|
283
|
+
// 拆分语句列表
|
|
284
|
+
let stmts = splitStatements(sql);
|
|
285
|
+
ret.statements = stmts.length;
|
|
286
|
+
// 逐条执行,DML语句统计变更行数
|
|
287
|
+
for (let stmt_sql of stmts) {
|
|
288
|
+
if (/^(INSERT|UPDATE|DELETE|REPLACE)\b/i.test(stmt_sql)) {
|
|
289
|
+
let info = conn.prepare(stmt_sql).run();
|
|
290
|
+
ret.affected_rows += info.changes || 0;
|
|
291
|
+
} else {
|
|
292
|
+
conn.exec(stmt_sql);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
ret.success = true;
|
|
296
|
+
} catch (error) {
|
|
297
|
+
console.error('SQL执行失败', error);
|
|
298
|
+
this.sql = sql;
|
|
299
|
+
this.error = {
|
|
300
|
+
code: error.code || error.errno,
|
|
301
|
+
message: error.message
|
|
302
|
+
};
|
|
303
|
+
ret.error = error.message;
|
|
304
|
+
} finally {
|
|
305
|
+
await releaseConn(conn);
|
|
306
|
+
}
|
|
307
|
+
return ret;
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* 拆分SQL脚本为语句列表
|
|
312
|
+
* 去除注释后按分号分割,过滤空语句
|
|
313
|
+
* @private
|
|
314
|
+
* @param {string} sql - SQL脚本内容
|
|
315
|
+
* @returns {Array} 语句列表
|
|
316
|
+
*/
|
|
317
|
+
function splitStatements(sql) {
|
|
318
|
+
// 去除行注释和块注释
|
|
319
|
+
let clean_sql = sql
|
|
320
|
+
.replace(/--[^\n]*/g, '')
|
|
321
|
+
.replace(/\/\*[\s\S]*?\*\//g, '')
|
|
322
|
+
.trim();
|
|
323
|
+
let stmts = [];
|
|
324
|
+
for (let part of clean_sql.split(';')) {
|
|
325
|
+
let stmt = part.trim();
|
|
326
|
+
if (stmt !== '') {
|
|
327
|
+
stmts.push(stmt);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return stmts;
|
|
331
|
+
}
|
|
332
|
+
|
|
260
333
|
/**
|
|
261
334
|
* 读取整张表的数据
|
|
262
335
|
* @param {string} table - 表名
|