mm_sqlite 1.0.8 → 1.0.9
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/db.js +31 -30
- package/package.json +1 -1
package/db.js
CHANGED
|
@@ -85,7 +85,7 @@ DB.prototype.run = async function(sql, params = [], timeout = 30000) {
|
|
|
85
85
|
try {
|
|
86
86
|
// 使用Promise.race实现超时控制
|
|
87
87
|
return await Promise.race([
|
|
88
|
-
this._mysql.run(sql, params),
|
|
88
|
+
this._mysql.run(sql, params, timeout),
|
|
89
89
|
this._createTimeoutPromise(timeout, `查询执行超时: ${sql.substring(0, 100)}...`)
|
|
90
90
|
]);
|
|
91
91
|
} catch (error) {
|
|
@@ -101,23 +101,25 @@ DB.prototype.run = async function(sql, params = [], timeout = 30000) {
|
|
|
101
101
|
/**
|
|
102
102
|
* 执行SQL命令
|
|
103
103
|
* @param {string} sql SQL语句
|
|
104
|
+
* @param {Array} params 参数数组
|
|
104
105
|
* @param {Number} timeout 超时时间(毫秒)
|
|
105
106
|
* @returns {Promise<Object>} 执行结果
|
|
106
107
|
*/
|
|
107
|
-
DB.prototype.exec = async function(sql, timeout = 30000) {
|
|
108
|
+
DB.prototype.exec = async function(sql, params = [], timeout = 30000) {
|
|
108
109
|
if (!this._mysql) {
|
|
109
110
|
throw new Error('SQLite实例未初始化');
|
|
110
111
|
}
|
|
111
112
|
try {
|
|
112
113
|
// 使用Promise.race实现超时控制
|
|
113
114
|
return await Promise.race([
|
|
114
|
-
this._mysql.exec(sql),
|
|
115
|
+
this._mysql.exec(sql, params, timeout),
|
|
115
116
|
this._createTimeoutPromise(timeout, `命令执行超时: ${sql.substring(0, 100)}...`)
|
|
116
117
|
]);
|
|
117
118
|
} catch (error) {
|
|
118
119
|
$.log.error(`[DB] [exec] 命令执行失败`, {
|
|
119
120
|
error: error.message,
|
|
120
|
-
sql: sql.substring(0, 200)
|
|
121
|
+
sql: sql.substring(0, 200),
|
|
122
|
+
params: JSON.stringify(params.slice(0, 5))
|
|
121
123
|
});
|
|
122
124
|
throw error;
|
|
123
125
|
}
|
|
@@ -155,7 +157,7 @@ DB.prototype.getConn = async function(timeout = 15000) {
|
|
|
155
157
|
DB.prototype.start = async function(identifier = "point_1", timeout = 15000) {
|
|
156
158
|
this.task = 1;
|
|
157
159
|
try {
|
|
158
|
-
return await this.exec("BEGIN;", timeout);
|
|
160
|
+
return await this.exec("BEGIN;", [], timeout);
|
|
159
161
|
} catch (err) {
|
|
160
162
|
this.task = 0;
|
|
161
163
|
$.log.error(`[DB] [start] 事务开始失败: ${err.message}`);
|
|
@@ -175,7 +177,7 @@ DB.prototype.commit = async function(timeout = 15000) {
|
|
|
175
177
|
}
|
|
176
178
|
this.task = 2;
|
|
177
179
|
try {
|
|
178
|
-
const result = await this.exec("COMMIT;", timeout);
|
|
180
|
+
const result = await this.exec("COMMIT;", [], timeout);
|
|
179
181
|
// 重置事务状态
|
|
180
182
|
this.task = 0;
|
|
181
183
|
this.task_sql = '';
|
|
@@ -184,7 +186,7 @@ DB.prototype.commit = async function(timeout = 15000) {
|
|
|
184
186
|
$.log.error(`[DB] [commit] 事务提交失败: ${err.message}`);
|
|
185
187
|
// 尝试回滚
|
|
186
188
|
try {
|
|
187
|
-
await this.exec("ROLLBACK;", timeout);
|
|
189
|
+
await this.exec("ROLLBACK;", [], timeout);
|
|
188
190
|
} catch (rollbackErr) {
|
|
189
191
|
$.log.error(`[DB] [commit] 事务回滚也失败: ${rollbackErr.message}`);
|
|
190
192
|
}
|
|
@@ -218,8 +220,8 @@ DB.prototype.back = async function(identifier = "point_1", timeout = 15000) {
|
|
|
218
220
|
}
|
|
219
221
|
this.task = 3;
|
|
220
222
|
try {
|
|
221
|
-
await this.exec("ROLLBACK;", timeout);
|
|
222
|
-
const result = await this.exec("COMMIT;", timeout);
|
|
223
|
+
await this.exec("ROLLBACK;", [], timeout);
|
|
224
|
+
const result = await this.exec("COMMIT;", [], timeout);
|
|
223
225
|
// 重置事务状态
|
|
224
226
|
this.task = 0;
|
|
225
227
|
this.task_sql = '';
|
|
@@ -458,13 +460,9 @@ DB.prototype.addTable = async function(table, field, type = 'int', auto = true,
|
|
|
458
460
|
sqlTemplate = "CREATE TABLE IF NOT EXISTS `{0}` (`{1}` {2} PRIMARY KEY)";
|
|
459
461
|
}
|
|
460
462
|
var sql = sqlTemplate.replace('{0}', table).replace(
|
|
461
|
-
'{1}', field).replace('{2}', fieldType);
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
} else {
|
|
465
|
-
sql += ";"
|
|
466
|
-
}
|
|
467
|
-
var bl = await this.exec(sql, timeout);
|
|
463
|
+
'{1}', field).replace('{2}', fieldType) + ";";
|
|
464
|
+
// SQLite不支持表级COMMENT语法,所以忽略commit参数
|
|
465
|
+
var bl = await this.exec(sql, [], timeout);
|
|
468
466
|
// 设置实例的表名属性,以便后续操作使用
|
|
469
467
|
this.table = table;
|
|
470
468
|
return bl && bl.affectedRows !== undefined ? 1 : 0;
|
|
@@ -506,22 +504,25 @@ DB.prototype.addField = async function(field, type, value = '', not_null = true,
|
|
|
506
504
|
return 0;
|
|
507
505
|
}
|
|
508
506
|
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
sql = sql.replace('{
|
|
512
|
-
var
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
if (comment) {
|
|
519
|
-
sql += " COMMENT '" + comment + "'";
|
|
520
|
-
}
|
|
521
|
-
sql += ";";
|
|
522
|
-
return await this.exec(sql, timeout);
|
|
507
|
+
// 在SQLite中,使用PRAGMA table_info检查表中是否已存在字段
|
|
508
|
+
var sql = "PRAGMA table_info(`{0}`)";
|
|
509
|
+
sql = sql.replace('{0}', this.table);
|
|
510
|
+
var columns = await this.run(sql, [], timeout);
|
|
511
|
+
var fieldExists = false;
|
|
512
|
+
for (var i = 0; i < columns.length; i++) {
|
|
513
|
+
if (columns[i].name === field) {
|
|
514
|
+
fieldExists = true;
|
|
515
|
+
break;
|
|
523
516
|
}
|
|
524
517
|
}
|
|
518
|
+
|
|
519
|
+
if (!fieldExists) {
|
|
520
|
+
var fieldType = this.setType(field, type, value, not_null, auto);
|
|
521
|
+
var sql = "ALTER Table `{0}` ADD `{1}` {2}";
|
|
522
|
+
sql = sql.replace('{0}', this.table).replace('{1}', field).replace('{2}', fieldType) + ";";
|
|
523
|
+
// SQLite不支持字段级COMMENT语法,所以忽略comment参数
|
|
524
|
+
return await this.exec(sql, [], timeout);
|
|
525
|
+
}
|
|
525
526
|
return 0;
|
|
526
527
|
} catch (err) {
|
|
527
528
|
$.log.error(`[DB] [addField] 添加字段失败: ${err.message}`);
|