mm_mysql 2.5.3 → 2.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 +53 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -245,6 +245,59 @@ Mysql.prototype.exec = async function (sql, params = []) {
|
|
|
245
245
|
return ret;
|
|
246
246
|
};
|
|
247
247
|
|
|
248
|
+
/**
|
|
249
|
+
* 执行多语句SQL脚本(专用于导入)
|
|
250
|
+
* 通过独立连接执行,不影响连接池默认配置
|
|
251
|
+
* @param {string} sql - SQL脚本内容,可含多条以分号分隔的语句
|
|
252
|
+
* @returns {Promise<object>} 执行结果统计对象
|
|
253
|
+
* @throws {TypeError} 当sql参数无效时
|
|
254
|
+
*/
|
|
255
|
+
Mysql.prototype.runSql = async function (sql) {
|
|
256
|
+
// 参数校验
|
|
257
|
+
if (typeof sql !== 'string' || sql.trim() === '') {
|
|
258
|
+
throw new TypeError('sql must be non-empty string');
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
let conn = null;
|
|
262
|
+
let ret = { success: false, statements: 0, affected_rows: 0, error: null };
|
|
263
|
+
try {
|
|
264
|
+
// 创建专用连接,仅导入连接开启多语句支持
|
|
265
|
+
let valid_config = this._getValidConfig(false);
|
|
266
|
+
valid_config.multipleStatements = true;
|
|
267
|
+
conn = await mysql.createConnection(valid_config);
|
|
268
|
+
|
|
269
|
+
// 执行整段SQL脚本
|
|
270
|
+
let [results] = await conn.query(sql);
|
|
271
|
+
// 统计语句数和受影响行数
|
|
272
|
+
if (Array.isArray(results)) {
|
|
273
|
+
ret.statements = results.length;
|
|
274
|
+
for (let item of results) {
|
|
275
|
+
ret.affected_rows += item.affectedRows || 0;
|
|
276
|
+
}
|
|
277
|
+
} else {
|
|
278
|
+
ret.statements = 1;
|
|
279
|
+
ret.affected_rows = results.affectedRows || 0;
|
|
280
|
+
}
|
|
281
|
+
ret.success = true;
|
|
282
|
+
} catch (error) {
|
|
283
|
+
this.error = {
|
|
284
|
+
code: error.errno,
|
|
285
|
+
message: error.sqlMessage || error.message
|
|
286
|
+
};
|
|
287
|
+
this.sql = error.sql || sql;
|
|
288
|
+
ret.error = this.error.message;
|
|
289
|
+
} finally {
|
|
290
|
+
if (conn) {
|
|
291
|
+
try {
|
|
292
|
+
await conn.end();
|
|
293
|
+
} catch (end_error) {
|
|
294
|
+
this.log('error', '关闭导入连接失败', end_error);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return ret;
|
|
299
|
+
};
|
|
300
|
+
|
|
248
301
|
/**
|
|
249
302
|
* 开始事务(优化版)
|
|
250
303
|
* @returns {Promise<object>} 事务连接对象
|