mm_sqlite 1.0.7 → 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/mm.db CHANGED
Binary file
package/db.js CHANGED
@@ -18,7 +18,9 @@ class DB extends Sql {
18
18
 
19
19
  // 事务SQL
20
20
  this.task_sql = "";
21
-
21
+ // 表名
22
+ this.table = "";
23
+
22
24
  /**
23
25
  * 获取上级
24
26
  * @return {Object} 返回上级管理器
@@ -83,7 +85,7 @@ DB.prototype.run = async function(sql, params = [], timeout = 30000) {
83
85
  try {
84
86
  // 使用Promise.race实现超时控制
85
87
  return await Promise.race([
86
- this._mysql.run(sql, params),
88
+ this._mysql.run(sql, params, timeout),
87
89
  this._createTimeoutPromise(timeout, `查询执行超时: ${sql.substring(0, 100)}...`)
88
90
  ]);
89
91
  } catch (error) {
@@ -99,23 +101,25 @@ DB.prototype.run = async function(sql, params = [], timeout = 30000) {
99
101
  /**
100
102
  * 执行SQL命令
101
103
  * @param {string} sql SQL语句
104
+ * @param {Array} params 参数数组
102
105
  * @param {Number} timeout 超时时间(毫秒)
103
106
  * @returns {Promise<Object>} 执行结果
104
107
  */
105
- DB.prototype.exec = async function(sql, timeout = 30000) {
108
+ DB.prototype.exec = async function(sql, params = [], timeout = 30000) {
106
109
  if (!this._mysql) {
107
110
  throw new Error('SQLite实例未初始化');
108
111
  }
109
112
  try {
110
113
  // 使用Promise.race实现超时控制
111
114
  return await Promise.race([
112
- this._mysql.exec(sql),
115
+ this._mysql.exec(sql, params, timeout),
113
116
  this._createTimeoutPromise(timeout, `命令执行超时: ${sql.substring(0, 100)}...`)
114
117
  ]);
115
118
  } catch (error) {
116
119
  $.log.error(`[DB] [exec] 命令执行失败`, {
117
120
  error: error.message,
118
- sql: sql.substring(0, 200)
121
+ sql: sql.substring(0, 200),
122
+ params: JSON.stringify(params.slice(0, 5))
119
123
  });
120
124
  throw error;
121
125
  }
@@ -153,7 +157,7 @@ DB.prototype.getConn = async function(timeout = 15000) {
153
157
  DB.prototype.start = async function(identifier = "point_1", timeout = 15000) {
154
158
  this.task = 1;
155
159
  try {
156
- return await this.exec("BEGIN;", timeout);
160
+ return await this.exec("BEGIN;", [], timeout);
157
161
  } catch (err) {
158
162
  this.task = 0;
159
163
  $.log.error(`[DB] [start] 事务开始失败: ${err.message}`);
@@ -173,7 +177,7 @@ DB.prototype.commit = async function(timeout = 15000) {
173
177
  }
174
178
  this.task = 2;
175
179
  try {
176
- const result = await this.exec("COMMIT;", timeout);
180
+ const result = await this.exec("COMMIT;", [], timeout);
177
181
  // 重置事务状态
178
182
  this.task = 0;
179
183
  this.task_sql = '';
@@ -182,7 +186,7 @@ DB.prototype.commit = async function(timeout = 15000) {
182
186
  $.log.error(`[DB] [commit] 事务提交失败: ${err.message}`);
183
187
  // 尝试回滚
184
188
  try {
185
- await this.exec("ROLLBACK;", timeout);
189
+ await this.exec("ROLLBACK;", [], timeout);
186
190
  } catch (rollbackErr) {
187
191
  $.log.error(`[DB] [commit] 事务回滚也失败: ${rollbackErr.message}`);
188
192
  }
@@ -216,8 +220,8 @@ DB.prototype.back = async function(identifier = "point_1", timeout = 15000) {
216
220
  }
217
221
  this.task = 3;
218
222
  try {
219
- await this.exec("ROLLBACK;", timeout);
220
- const result = await this.exec("COMMIT;", timeout);
223
+ await this.exec("ROLLBACK;", [], timeout);
224
+ const result = await this.exec("COMMIT;", [], timeout);
221
225
  // 重置事务状态
222
226
  this.task = 0;
223
227
  this.task_sql = '';
@@ -437,14 +441,28 @@ DB.prototype.addTable = async function(table, field, type = 'int', auto = true,
437
441
  if (!field) {
438
442
  field = "id";
439
443
  }
440
- var sql = "CREATE TABLE IF NOT EXISTS `{0}` (`{1}` {2})".replace('{0}', table).replace(
441
- '{1}', field).replace('{2}', this.setType(field, type, null, true, auto) + ' PRIMARY KEY');
442
- if (commit) {
443
- sql += " COMMENT = '" + commit + "';"
444
+ // SQLite的自增主键要求字段类型必须是INTEGER(大写且不带长度)
445
+ let fieldType;
446
+ if (auto) {
447
+ // 自增字段使用简单的INTEGER类型,由后面的PRIMARY KEY AUTOINCREMENT决定自增行为
448
+ fieldType = "INTEGER";
449
+ } else {
450
+ // 非自增字段使用正常的类型设置
451
+ fieldType = this.setType(field, type, null, true, auto);
452
+ }
453
+ // 根据是否自增来构造不同的SQL语句
454
+ let sqlTemplate;
455
+ if (auto) {
456
+ // 自增字段:使用INTEGER PRIMARY KEY AUTOINCREMENT语法
457
+ sqlTemplate = "CREATE TABLE IF NOT EXISTS `{0}` (`{1}` {2} PRIMARY KEY AUTOINCREMENT)";
444
458
  } else {
445
- sql += ";"
459
+ // 非自增字段:使用正常的字段类型和主键定义
460
+ sqlTemplate = "CREATE TABLE IF NOT EXISTS `{0}` (`{1}` {2} PRIMARY KEY)";
446
461
  }
447
- var bl = await this.exec(sql, timeout);
462
+ var sql = sqlTemplate.replace('{0}', table).replace(
463
+ '{1}', field).replace('{2}', fieldType) + ";";
464
+ // SQLite不支持表级COMMENT语法,所以忽略commit参数
465
+ var bl = await this.exec(sql, [], timeout);
448
466
  // 设置实例的表名属性,以便后续操作使用
449
467
  this.table = table;
450
468
  return bl && bl.affectedRows !== undefined ? 1 : 0;
@@ -486,22 +504,25 @@ DB.prototype.addField = async function(field, type, value = '', not_null = true,
486
504
  return 0;
487
505
  }
488
506
 
489
- var sql =
490
- "select count(*) as `count` from sqlite_master where `type` = 'table' and `name` = '{1}' and `sql` like '%{2}%'";
491
- sql = sql.replace('{1}', this.table).replace('{2}', field);
492
- var arr = await this.run(sql, [], timeout);
493
- if (arr && arr.length > 0) {
494
- if (arr[0].count == 0) {
495
- var type = this.setType(field, type, value, not_null, auto);
496
- var sql = "ALTER Table `{0}` ADD `{1}` {2}";
497
- sql = sql.replace('{0}', this.table).replace('{1}', field).replace('{2}', type);
498
- if (comment) {
499
- sql += " COMMENT '" + comment + "'";
500
- }
501
- sql += ";";
502
- 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;
503
516
  }
504
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
+ }
505
526
  return 0;
506
527
  } catch (err) {
507
528
  $.log.error(`[DB] [addField] 添加字段失败: ${err.message}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_sqlite",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "高性能SQLite数据库操作模块,提供与mm_mysql完全兼容的API接口,支持异步操作、事务处理和批量操作",
5
5
  "main": "index.js",
6
6
  "scripts": {
Binary file
Binary file
Binary file
package/db/test.db DELETED
Binary file