mm_sqlite 1.1.4 → 1.1.5
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 +168 -102
- package/db.js +94 -115
- package/index.js +741 -548
- package/package.json +2 -2
- package/sql.js +245 -372
- package/sql_builder.js +0 -375
package/db.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const Sql = require('./sql');
|
|
1
|
+
const { Sql } = require('./sql');
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @class 数据库操作类
|
|
@@ -20,12 +20,12 @@ class DB extends Sql {
|
|
|
20
20
|
this.task_sql = "";
|
|
21
21
|
// 表名
|
|
22
22
|
this.table = "";
|
|
23
|
-
|
|
23
|
+
|
|
24
24
|
/**
|
|
25
25
|
* 获取上级
|
|
26
26
|
* @return {Object} 返回上级管理器
|
|
27
27
|
*/
|
|
28
|
-
this.parent = function() {
|
|
28
|
+
this.parent = function () {
|
|
29
29
|
return mysql;
|
|
30
30
|
};
|
|
31
31
|
}
|
|
@@ -37,7 +37,7 @@ class DB extends Sql {
|
|
|
37
37
|
* @param {String} message - 超时提示信息
|
|
38
38
|
* @return {Promise} 超时Promise
|
|
39
39
|
*/
|
|
40
|
-
DB.prototype._createTimeoutPromise = function(timeout, message) {
|
|
40
|
+
DB.prototype._createTimeoutPromise = function (timeout, message) {
|
|
41
41
|
return new Promise((_, reject) => {
|
|
42
42
|
setTimeout(() => {
|
|
43
43
|
reject(new Error(message));
|
|
@@ -49,7 +49,7 @@ DB.prototype._createTimeoutPromise = function(timeout, message) {
|
|
|
49
49
|
* 获取数据库名
|
|
50
50
|
* @return {String} 数据库
|
|
51
51
|
*/
|
|
52
|
-
DB.prototype.database = function() {
|
|
52
|
+
DB.prototype.database = function () {
|
|
53
53
|
return this.parent().config.database;
|
|
54
54
|
};
|
|
55
55
|
|
|
@@ -59,7 +59,7 @@ DB.prototype.database = function() {
|
|
|
59
59
|
* @param {String} key 键名
|
|
60
60
|
* @return {Object} 返回管理器
|
|
61
61
|
*/
|
|
62
|
-
DB.prototype.new = function(table, key) {
|
|
62
|
+
DB.prototype.new = function (table, key) {
|
|
63
63
|
var db = this.parent().db();
|
|
64
64
|
db.table = table;
|
|
65
65
|
if (key) {
|
|
@@ -78,7 +78,7 @@ DB.prototype.new = function(table, key) {
|
|
|
78
78
|
* @param {Number} timeout 超时时间(毫秒)
|
|
79
79
|
* @returns {Promise<Object>} 查询结果
|
|
80
80
|
*/
|
|
81
|
-
DB.prototype.run = async function(sql, params = [], timeout = 30000) {
|
|
81
|
+
DB.prototype.run = async function (sql, params = [], timeout = 30000) {
|
|
82
82
|
if (!this._mysql) {
|
|
83
83
|
throw new Error('SQLite实例未初始化');
|
|
84
84
|
}
|
|
@@ -89,12 +89,9 @@ DB.prototype.run = async function(sql, params = [], timeout = 30000) {
|
|
|
89
89
|
this._createTimeoutPromise(timeout, `查询执行超时: ${sql.substring(0, 100)}...`)
|
|
90
90
|
]);
|
|
91
91
|
} catch (error) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
params: JSON.stringify(params.slice(0, 5))
|
|
96
|
-
});
|
|
97
|
-
throw error;
|
|
92
|
+
this.logger('error', '查询执行失败', error);
|
|
93
|
+
// 返回空数组作为默认值,保持返回值类型一致
|
|
94
|
+
return [];
|
|
98
95
|
}
|
|
99
96
|
};
|
|
100
97
|
|
|
@@ -105,7 +102,7 @@ DB.prototype.run = async function(sql, params = [], timeout = 30000) {
|
|
|
105
102
|
* @param {Number} timeout 超时时间(毫秒)
|
|
106
103
|
* @returns {Promise<Object>} 执行结果
|
|
107
104
|
*/
|
|
108
|
-
DB.prototype.exec = async function(sql, params = [], timeout = 30000) {
|
|
105
|
+
DB.prototype.exec = async function (sql, params = [], timeout = 30000) {
|
|
109
106
|
if (!this._mysql) {
|
|
110
107
|
throw new Error('SQLite实例未初始化');
|
|
111
108
|
}
|
|
@@ -116,12 +113,9 @@ DB.prototype.exec = async function(sql, params = [], timeout = 30000) {
|
|
|
116
113
|
this._createTimeoutPromise(timeout, `命令执行超时: ${sql.substring(0, 100)}...`)
|
|
117
114
|
]);
|
|
118
115
|
} catch (error) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
params: JSON.stringify(params.slice(0, 5))
|
|
123
|
-
});
|
|
124
|
-
throw error;
|
|
116
|
+
this.logger('error', '命令执行失败', error);
|
|
117
|
+
// 返回默认操作结果对象,保持返回值类型一致
|
|
118
|
+
return 0;
|
|
125
119
|
}
|
|
126
120
|
};
|
|
127
121
|
|
|
@@ -130,7 +124,7 @@ DB.prototype.exec = async function(sql, params = [], timeout = 30000) {
|
|
|
130
124
|
* @param {Number} timeout 超时时间(毫秒)
|
|
131
125
|
* @returns {Promise<Object>} 数据库连接对象
|
|
132
126
|
*/
|
|
133
|
-
DB.prototype.getConn = async function(timeout = 15000) {
|
|
127
|
+
DB.prototype.getConn = async function (timeout = 15000) {
|
|
134
128
|
if (!this._mysql) {
|
|
135
129
|
throw new Error('SQLite实例未初始化');
|
|
136
130
|
}
|
|
@@ -141,9 +135,8 @@ DB.prototype.getConn = async function(timeout = 15000) {
|
|
|
141
135
|
this._createTimeoutPromise(timeout, '获取数据库连接超时')
|
|
142
136
|
]);
|
|
143
137
|
} catch (error) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
});
|
|
138
|
+
this.logger('error', '获取连接失败', error);
|
|
139
|
+
// 重新抛出异常,因为调用方需要连接对象
|
|
147
140
|
throw error;
|
|
148
141
|
}
|
|
149
142
|
};
|
|
@@ -154,14 +147,15 @@ DB.prototype.getConn = async function(timeout = 15000) {
|
|
|
154
147
|
* @param {Number} timeout 超时时间(毫秒)
|
|
155
148
|
* @return {Promise<Object>} 执行结果
|
|
156
149
|
*/
|
|
157
|
-
DB.prototype.start = async function(identifier = "point_1", timeout = 15000) {
|
|
150
|
+
DB.prototype.start = async function (identifier = "point_1", timeout = 15000) {
|
|
158
151
|
this.task = 1;
|
|
159
152
|
try {
|
|
160
153
|
return await this.exec("BEGIN;", [], timeout);
|
|
161
154
|
} catch (err) {
|
|
162
155
|
this.task = 0;
|
|
163
|
-
|
|
164
|
-
|
|
156
|
+
this.logger('error', '事务开始失败', err);
|
|
157
|
+
// 返回默认操作结果对象,保持返回值类型一致
|
|
158
|
+
return 0;
|
|
165
159
|
}
|
|
166
160
|
};
|
|
167
161
|
|
|
@@ -170,7 +164,7 @@ DB.prototype.start = async function(identifier = "point_1", timeout = 15000) {
|
|
|
170
164
|
* @param {Number} timeout 超时时间(毫秒)
|
|
171
165
|
* @return {Promise<Object>} 执行结果
|
|
172
166
|
*/
|
|
173
|
-
DB.prototype.commit = async function(timeout = 15000) {
|
|
167
|
+
DB.prototype.commit = async function (timeout = 15000) {
|
|
174
168
|
if (this.task !== 1) {
|
|
175
169
|
$.log.warn("[DB] [commit] 没有活跃的事务可提交");
|
|
176
170
|
return 0;
|
|
@@ -183,23 +177,22 @@ DB.prototype.commit = async function(timeout = 15000) {
|
|
|
183
177
|
this.task_sql = '';
|
|
184
178
|
return result;
|
|
185
179
|
} catch (err) {
|
|
186
|
-
|
|
180
|
+
this.logger('error', '事务提交失败', err);
|
|
187
181
|
// 尝试回滚
|
|
188
182
|
try {
|
|
189
183
|
await this.exec("ROLLBACK;", [], timeout);
|
|
190
184
|
} catch (rollbackErr) {
|
|
191
|
-
|
|
185
|
+
this.logger('error', '事务回滚也失败', rollbackErr);
|
|
192
186
|
}
|
|
193
187
|
this.task = 0;
|
|
194
188
|
this.task_sql = '';
|
|
195
|
-
throw err;
|
|
196
189
|
}
|
|
197
190
|
};
|
|
198
191
|
|
|
199
192
|
/**
|
|
200
193
|
* 事务结束
|
|
201
194
|
*/
|
|
202
|
-
DB.prototype.end = function() {
|
|
195
|
+
DB.prototype.end = function () {
|
|
203
196
|
// 确保事务状态被重置
|
|
204
197
|
this.task = 0;
|
|
205
198
|
this.task_sql = '';
|
|
@@ -211,7 +204,7 @@ DB.prototype.end = function() {
|
|
|
211
204
|
* @param {Number} timeout 超时时间(毫秒)
|
|
212
205
|
* @return {Promise<Object>} 执行结果
|
|
213
206
|
*/
|
|
214
|
-
DB.prototype.back = async function(identifier = "point_1", timeout = 15000) {
|
|
207
|
+
DB.prototype.back = async function (identifier = "point_1", timeout = 15000) {
|
|
215
208
|
if (this.task !== 1) {
|
|
216
209
|
$.log.warn("[DB] [back] 没有活跃的事务可回滚");
|
|
217
210
|
this.task = 0;
|
|
@@ -227,10 +220,10 @@ DB.prototype.back = async function(identifier = "point_1", timeout = 15000) {
|
|
|
227
220
|
this.task_sql = '';
|
|
228
221
|
return result;
|
|
229
222
|
} catch (err) {
|
|
230
|
-
|
|
223
|
+
this.logger('error', '事务回滚失败', err);
|
|
231
224
|
this.task = 0;
|
|
232
225
|
this.task_sql = '';
|
|
233
|
-
|
|
226
|
+
return 0;
|
|
234
227
|
}
|
|
235
228
|
};
|
|
236
229
|
|
|
@@ -240,7 +233,7 @@ DB.prototype.back = async function(identifier = "point_1", timeout = 15000) {
|
|
|
240
233
|
* @param {Number} timeout 超时时间(毫秒)
|
|
241
234
|
* @return {Promise|Array} 表名数组
|
|
242
235
|
*/
|
|
243
|
-
DB.prototype.tables = async function(table, timeout = 15000) {
|
|
236
|
+
DB.prototype.tables = async function (table, timeout = 15000) {
|
|
244
237
|
try {
|
|
245
238
|
const list = await this.run("SELECT `name` FROM sqlite_master WHERE type='table';", [], timeout);
|
|
246
239
|
if (table) {
|
|
@@ -257,7 +250,7 @@ DB.prototype.tables = async function(table, timeout = 15000) {
|
|
|
257
250
|
// 使用原生方法提取表名
|
|
258
251
|
return list.map(item => item.name);
|
|
259
252
|
} catch (err) {
|
|
260
|
-
|
|
253
|
+
this.logger('error', '获取表名失败', err);
|
|
261
254
|
return [];
|
|
262
255
|
}
|
|
263
256
|
};
|
|
@@ -265,16 +258,14 @@ DB.prototype.tables = async function(table, timeout = 15000) {
|
|
|
265
258
|
/**
|
|
266
259
|
* @description 获取所有表字段
|
|
267
260
|
* @param {String} table 表名
|
|
268
|
-
* @param {String} field_name 字段名,可选
|
|
269
261
|
* @param {Number} timeout 超时时间(毫秒)
|
|
270
262
|
* @return {Promise|Array} 字段信息列表
|
|
271
263
|
*/
|
|
272
|
-
DB.prototype.fields = async function(table,
|
|
264
|
+
DB.prototype.fields = async function (table, timeout = 15000) {
|
|
273
265
|
try {
|
|
274
266
|
const targetTable = table || this.table;
|
|
275
|
-
if (!
|
|
276
|
-
|
|
277
|
-
return [];
|
|
267
|
+
if (!table) {
|
|
268
|
+
throw new TypeError('table must be specified');
|
|
278
269
|
}
|
|
279
270
|
var sql = "PRAGMA table_info(`{0}`);".replace('{0}', targetTable);
|
|
280
271
|
var list = await this.run(sql, [], timeout);
|
|
@@ -285,7 +276,7 @@ DB.prototype.fields = async function(table, field_name, timeout = 15000) {
|
|
|
285
276
|
}
|
|
286
277
|
return list;
|
|
287
278
|
} catch (err) {
|
|
288
|
-
|
|
279
|
+
this.logger('error', '获取字段信息失败', err);
|
|
289
280
|
return [];
|
|
290
281
|
}
|
|
291
282
|
};
|
|
@@ -299,7 +290,7 @@ DB.prototype.fields = async function(table, field_name, timeout = 15000) {
|
|
|
299
290
|
* @param {Boolean} auto 自动
|
|
300
291
|
* @return {String} 返回最终类型
|
|
301
292
|
*/
|
|
302
|
-
DB.prototype.setType = function(field, type, value, not_null, auto) {
|
|
293
|
+
DB.prototype.setType = function (field, type, value, not_null, auto) {
|
|
303
294
|
if (!type) {
|
|
304
295
|
type = 'integer';
|
|
305
296
|
}
|
|
@@ -432,11 +423,10 @@ DB.prototype.setType = function(field, type, value, not_null, auto) {
|
|
|
432
423
|
* @param {Number} timeout 超时时间(毫秒)
|
|
433
424
|
* @return {Promise|Number} 创建成功返回1,失败返回0
|
|
434
425
|
*/
|
|
435
|
-
DB.prototype.addTable = async function(table, field, type = 'int', auto = true, commit = '', timeout = 15000) {
|
|
426
|
+
DB.prototype.addTable = async function (table, field, type = 'int', auto = true, commit = '', timeout = 15000) {
|
|
436
427
|
try {
|
|
437
428
|
if (!table || typeof table !== 'string') {
|
|
438
|
-
|
|
439
|
-
return 0;
|
|
429
|
+
throw new TypeError('table must be non-empty string');
|
|
440
430
|
}
|
|
441
431
|
if (!field) {
|
|
442
432
|
field = "id";
|
|
@@ -465,9 +455,9 @@ DB.prototype.addTable = async function(table, field, type = 'int', auto = true,
|
|
|
465
455
|
var bl = await this.exec(sql, [], timeout);
|
|
466
456
|
// 设置实例的表名属性,以便后续操作使用
|
|
467
457
|
this.table = table;
|
|
468
|
-
return bl
|
|
458
|
+
return bl;
|
|
469
459
|
} catch (err) {
|
|
470
|
-
|
|
460
|
+
this.logger('error', '创建表失败', err);
|
|
471
461
|
return 0;
|
|
472
462
|
}
|
|
473
463
|
};
|
|
@@ -479,7 +469,7 @@ DB.prototype.addTable = async function(table, field, type = 'int', auto = true,
|
|
|
479
469
|
* @param {Number} timeout - 超时时间(毫秒)
|
|
480
470
|
* @return {Promise|Number} 清空成功返回1,失败返回0
|
|
481
471
|
*/
|
|
482
|
-
DB.prototype.clearTable = async function(reset = true, table = '', timeout = 15000) {
|
|
472
|
+
DB.prototype.clearTable = async function (reset = true, table = '', timeout = 15000) {
|
|
483
473
|
var sql = reset ? "TRUNCATE table `{0}`;" : "DELETE FROM `{0}`";
|
|
484
474
|
var bl = await this.exec(sql.replace('{0}', table || this.table), [], timeout);
|
|
485
475
|
return bl;
|
|
@@ -496,14 +486,13 @@ DB.prototype.clearTable = async function(reset = true, table = '', timeout = 150
|
|
|
496
486
|
* @param {Number} timeout 超时时间(毫秒)
|
|
497
487
|
* @return {Promise|Number} 添加成功返回1,失败返回0
|
|
498
488
|
*/
|
|
499
|
-
DB.prototype.addField = async function(field, type, value = '', not_null = true, auto = false, comment = '', timeout = 15000) {
|
|
489
|
+
DB.prototype.addField = async function (field, type, value = '', not_null = true, auto = false, comment = '', timeout = 15000) {
|
|
500
490
|
try {
|
|
501
491
|
// 确保表名已设置
|
|
502
492
|
if (!this.table || !field || !type) {
|
|
503
|
-
|
|
504
|
-
return 0;
|
|
493
|
+
throw new TypeError('table, field, and type must be specified');
|
|
505
494
|
}
|
|
506
|
-
|
|
495
|
+
|
|
507
496
|
// 在SQLite中,使用PRAGMA table_info检查表中是否已存在字段
|
|
508
497
|
var sql = "PRAGMA table_info(`{0}`)";
|
|
509
498
|
sql = sql.replace('{0}', this.table);
|
|
@@ -515,7 +504,7 @@ DB.prototype.addField = async function(field, type, value = '', not_null = true,
|
|
|
515
504
|
break;
|
|
516
505
|
}
|
|
517
506
|
}
|
|
518
|
-
|
|
507
|
+
|
|
519
508
|
if (!fieldExists) {
|
|
520
509
|
var fieldType = this.setType(field, type, value, not_null, auto);
|
|
521
510
|
var sql = "ALTER Table `{0}` ADD `{1}` {2}";
|
|
@@ -525,7 +514,7 @@ DB.prototype.addField = async function(field, type, value = '', not_null = true,
|
|
|
525
514
|
}
|
|
526
515
|
return 0;
|
|
527
516
|
} catch (err) {
|
|
528
|
-
|
|
517
|
+
this.logger('error', '添加字段失败', err);
|
|
529
518
|
return 0;
|
|
530
519
|
}
|
|
531
520
|
};
|
|
@@ -542,13 +531,12 @@ DB.prototype.addField = async function(field, type, value = '', not_null = true,
|
|
|
542
531
|
* @param {Number} timeout 超时时间(毫秒)
|
|
543
532
|
* @return {Promise|Number} 修改成功返回1,失败返回0
|
|
544
533
|
*/
|
|
545
|
-
DB.prototype.setField = async function(field, type, value = '', not_null = true, auto = false, isKey = false, new_name = '', timeout = 15000) {
|
|
534
|
+
DB.prototype.setField = async function (field, type, value = '', not_null = true, auto = false, isKey = false, new_name = '', timeout = 15000) {
|
|
546
535
|
try {
|
|
547
536
|
if (!field || !type || !this.table) {
|
|
548
|
-
|
|
549
|
-
return 0;
|
|
537
|
+
throw new TypeError('field, type, and table must be specified');
|
|
550
538
|
}
|
|
551
|
-
|
|
539
|
+
|
|
552
540
|
var sql =
|
|
553
541
|
"select count(*) as `count` from sqlite_master where `type` = 'table' and `name` = '{1}' and `sql` like '%{2}%'";
|
|
554
542
|
sql = sql.replace('{1}', this.table).replace('{2}', field);
|
|
@@ -588,7 +576,7 @@ DB.prototype.setField = async function(field, type, value = '', not_null = true,
|
|
|
588
576
|
}
|
|
589
577
|
return ret;
|
|
590
578
|
} catch (err) {
|
|
591
|
-
|
|
579
|
+
this.logger('error', '修改字段失败', err);
|
|
592
580
|
return 0;
|
|
593
581
|
}
|
|
594
582
|
};
|
|
@@ -600,18 +588,17 @@ DB.prototype.setField = async function(field, type, value = '', not_null = true,
|
|
|
600
588
|
* @param {Number} timeout 超时时间(毫秒)
|
|
601
589
|
* @return {Promise|Number} 删除成功返回1,失败返回0
|
|
602
590
|
*/
|
|
603
|
-
DB.prototype.delField = async function(table, field, timeout = 15000) {
|
|
591
|
+
DB.prototype.delField = async function (table, field, timeout = 15000) {
|
|
604
592
|
try {
|
|
605
593
|
if (!table || !field) {
|
|
606
|
-
|
|
607
|
-
return 0;
|
|
594
|
+
throw new TypeError('table and field must be specified');
|
|
608
595
|
}
|
|
609
|
-
|
|
596
|
+
|
|
610
597
|
var sql = "ALTER TABLE {0} DROP COLUMN {1};";
|
|
611
598
|
sql = sql.replace('{0}', table).replace('{1}', field);
|
|
612
599
|
return await this.exec(sql, [], timeout);
|
|
613
600
|
} catch (err) {
|
|
614
|
-
|
|
601
|
+
this.logger('error', '删除字段失败', err);
|
|
615
602
|
return 0;
|
|
616
603
|
}
|
|
617
604
|
};
|
|
@@ -621,7 +608,7 @@ DB.prototype.delField = async function(table, field, timeout = 15000) {
|
|
|
621
608
|
* @param {Object} fd 字段信息
|
|
622
609
|
* @return {String} sql语段
|
|
623
610
|
*/
|
|
624
|
-
DB.prototype.sqlField = function(fd) {
|
|
611
|
+
DB.prototype.sqlField = function (fd) {
|
|
625
612
|
var sql = "`{0}`".replace('{0}', fd.name);
|
|
626
613
|
sql += " " + fd.type;
|
|
627
614
|
if (fd.notnull) {
|
|
@@ -659,18 +646,17 @@ DB.prototype.sqlField = function(fd) {
|
|
|
659
646
|
* @param {Number} timeout 超时时间(毫秒)
|
|
660
647
|
* @return {Promise|Number} 重命名成功返回1,失败返回0
|
|
661
648
|
*/
|
|
662
|
-
DB.prototype.renameField = async function(table, field, new_field, type, timeout = 15000) {
|
|
649
|
+
DB.prototype.renameField = async function (table, field, new_field, type, timeout = 15000) {
|
|
663
650
|
try {
|
|
664
651
|
if (!table || !field || !new_field) {
|
|
665
|
-
|
|
666
|
-
return 0;
|
|
652
|
+
throw new TypeError('table, field, and new_field must be specified');
|
|
667
653
|
}
|
|
668
|
-
|
|
654
|
+
|
|
669
655
|
var sql = "ALTER TABLE {0} RENAME COLUMN {1} TO {2};";
|
|
670
656
|
sql = sql.replace('{0}', table).replace('{1}', field).replace('{2}', new_field);
|
|
671
657
|
return await this.exec(sql, [], timeout);
|
|
672
658
|
} catch (err) {
|
|
673
|
-
|
|
659
|
+
this.logger('error', '重命名字段失败', err);
|
|
674
660
|
return 0;
|
|
675
661
|
}
|
|
676
662
|
};
|
|
@@ -683,15 +669,14 @@ DB.prototype.renameField = async function(table, field, new_field, type, timeout
|
|
|
683
669
|
* @param {Number} timeout 超时时间(毫秒)
|
|
684
670
|
* @return {Promise|Number} 操作结果
|
|
685
671
|
*/
|
|
686
|
-
DB.prototype.editField = function(table, field, type, timeout = 15000) {
|
|
672
|
+
DB.prototype.editField = function (table, field, type, timeout = 15000) {
|
|
687
673
|
try {
|
|
688
674
|
if (!table || !field || !type) {
|
|
689
|
-
|
|
690
|
-
return 0;
|
|
675
|
+
throw new TypeError('table, field, and type must be specified');
|
|
691
676
|
}
|
|
692
677
|
return this.exec("ALTER TABLE `" + table + "` MODIFY COLUMN `" + field + "` " + type + ";", [], timeout);
|
|
693
678
|
} catch (err) {
|
|
694
|
-
|
|
679
|
+
this.logger('error', '修改字段类型失败', err);
|
|
695
680
|
return 0;
|
|
696
681
|
}
|
|
697
682
|
};
|
|
@@ -702,10 +687,9 @@ DB.prototype.editField = function(table, field, type, timeout = 15000) {
|
|
|
702
687
|
* @param {Number} timeout - 超时时间(毫秒)
|
|
703
688
|
* @return {Promise|Number} 删除成功返回1,失败返回0
|
|
704
689
|
*/
|
|
705
|
-
DB.prototype.dropTable = function(table, timeout = 15000) {
|
|
690
|
+
DB.prototype.dropTable = function (table, timeout = 15000) {
|
|
706
691
|
if (!table) {
|
|
707
|
-
|
|
708
|
-
return 0;
|
|
692
|
+
throw new TypeError('table must be specified');
|
|
709
693
|
}
|
|
710
694
|
return this.exec("DROP TABLE IF EXISTS `" + table + "`;", [], timeout);
|
|
711
695
|
};
|
|
@@ -717,10 +701,9 @@ DB.prototype.dropTable = function(table, timeout = 15000) {
|
|
|
717
701
|
* @param {Number} timeout - 超时时间(毫秒)
|
|
718
702
|
* @return {Promise|Number} 重命名成功返回1,失败返回0
|
|
719
703
|
*/
|
|
720
|
-
DB.prototype.renameTable = function(table, new_table, timeout = 15000) {
|
|
704
|
+
DB.prototype.renameTable = function (table, new_table, timeout = 15000) {
|
|
721
705
|
if (!table || !new_table) {
|
|
722
|
-
|
|
723
|
-
return 0;
|
|
706
|
+
throw new TypeError('table and new_table must be specified');
|
|
724
707
|
}
|
|
725
708
|
return this.exec("ALTER TABLE `" + table + "` RENAME TO `" + new_table + "`;", [], timeout);
|
|
726
709
|
};
|
|
@@ -731,10 +714,9 @@ DB.prototype.renameTable = function(table, new_table, timeout = 15000) {
|
|
|
731
714
|
* @param {Number} timeout - 超时时间(毫秒)
|
|
732
715
|
* @return {Promise|Number} 清空成功返回1,失败返回0
|
|
733
716
|
*/
|
|
734
|
-
DB.prototype.emptyTable = function(table, timeout = 15000) {
|
|
717
|
+
DB.prototype.emptyTable = function (table, timeout = 15000) {
|
|
735
718
|
if (!table) {
|
|
736
|
-
|
|
737
|
-
return 0;
|
|
719
|
+
throw new TypeError('table must be specified');
|
|
738
720
|
}
|
|
739
721
|
return this.exec("DELETE FROM `" + table + "`;", [], timeout);
|
|
740
722
|
};
|
|
@@ -745,7 +727,7 @@ DB.prototype.emptyTable = function(table, timeout = 15000) {
|
|
|
745
727
|
* @param {Number} timeout - 超时时间(毫秒)
|
|
746
728
|
* @return {Promise|Boolean} 存在返回true,不存在返回false
|
|
747
729
|
*/
|
|
748
|
-
DB.prototype.hasTable = async function(table, timeout = 15000) {
|
|
730
|
+
DB.prototype.hasTable = async function (table, timeout = 15000) {
|
|
749
731
|
try {
|
|
750
732
|
if (!table) {
|
|
751
733
|
return false;
|
|
@@ -753,7 +735,7 @@ DB.prototype.hasTable = async function(table, timeout = 15000) {
|
|
|
753
735
|
var list = await this.run("SELECT name FROM sqlite_master WHERE type='table' AND name=?;", [table], timeout);
|
|
754
736
|
return list && list.length > 0;
|
|
755
737
|
} catch (err) {
|
|
756
|
-
|
|
738
|
+
this.logger('error', '检查表是否存在失败', err);
|
|
757
739
|
return false;
|
|
758
740
|
}
|
|
759
741
|
};
|
|
@@ -765,7 +747,7 @@ DB.prototype.hasTable = async function(table, timeout = 15000) {
|
|
|
765
747
|
* @param {Number} timeout - 超时时间(毫秒)
|
|
766
748
|
* @return {Promise|Boolean} 存在返回true,不存在返回false
|
|
767
749
|
*/
|
|
768
|
-
DB.prototype.hasField = async function(table, field, timeout = 15000) {
|
|
750
|
+
DB.prototype.hasField = async function (table, field, timeout = 15000) {
|
|
769
751
|
try {
|
|
770
752
|
if (!table || !field) {
|
|
771
753
|
return false;
|
|
@@ -773,7 +755,7 @@ DB.prototype.hasField = async function(table, field, timeout = 15000) {
|
|
|
773
755
|
var fields = await this.fields(table, field, timeout);
|
|
774
756
|
return fields && fields.some(f => f.name === field);
|
|
775
757
|
} catch (err) {
|
|
776
|
-
|
|
758
|
+
this.logger('error', '检查字段是否存在失败', err);
|
|
777
759
|
return false;
|
|
778
760
|
}
|
|
779
761
|
};
|
|
@@ -785,10 +767,9 @@ DB.prototype.hasField = async function(table, field, timeout = 15000) {
|
|
|
785
767
|
* @param {Number} timeout - 超时时间(毫秒)
|
|
786
768
|
* @return {Promise|Number} 备份成功返回1,失败返回0
|
|
787
769
|
*/
|
|
788
|
-
DB.prototype.backupTable = function(table, backup, timeout = 60000) {
|
|
770
|
+
DB.prototype.backupTable = function (table, backup, timeout = 60000) {
|
|
789
771
|
if (!table || !backup) {
|
|
790
|
-
|
|
791
|
-
return 0;
|
|
772
|
+
throw new TypeError('table and backup must be specified');
|
|
792
773
|
}
|
|
793
774
|
var sql = "CREATE TABLE IF NOT EXISTS `" + backup + "` AS SELECT * FROM `" + table + "`;";
|
|
794
775
|
return this.exec(sql, [], timeout);
|
|
@@ -800,11 +781,10 @@ DB.prototype.backupTable = function(table, backup, timeout = 60000) {
|
|
|
800
781
|
* @param {Number} timeout 超时时间(毫秒)
|
|
801
782
|
* @return {Promise|String} 创建语句
|
|
802
783
|
*/
|
|
803
|
-
DB.prototype.getCreateTable = async function(table, timeout = 15000) {
|
|
784
|
+
DB.prototype.getCreateTable = async function (table, timeout = 15000) {
|
|
804
785
|
try {
|
|
805
786
|
if (!table) {
|
|
806
|
-
|
|
807
|
-
return '';
|
|
787
|
+
throw new TypeError('table must be specified');
|
|
808
788
|
}
|
|
809
789
|
const list = await this.run("SELECT sql FROM sqlite_master WHERE type='table' AND name=?;", [table], timeout);
|
|
810
790
|
if (list && list.length > 0) {
|
|
@@ -812,7 +792,7 @@ DB.prototype.getCreateTable = async function(table, timeout = 15000) {
|
|
|
812
792
|
}
|
|
813
793
|
return '';
|
|
814
794
|
} catch (err) {
|
|
815
|
-
|
|
795
|
+
this.logger('error', '获取表结构失败', err);
|
|
816
796
|
return '';
|
|
817
797
|
}
|
|
818
798
|
};
|
|
@@ -824,19 +804,18 @@ DB.prototype.getCreateTable = async function(table, timeout = 15000) {
|
|
|
824
804
|
* @param {Number} timeout 超时时间(毫秒)
|
|
825
805
|
* @return {Promise|String} SQL插入语句
|
|
826
806
|
*/
|
|
827
|
-
DB.prototype.getTableData = async function(table, batchSize = 100, timeout = 60000) {
|
|
807
|
+
DB.prototype.getTableData = async function (table, batchSize = 100, timeout = 60000) {
|
|
828
808
|
try {
|
|
829
809
|
if (!table) {
|
|
830
|
-
|
|
831
|
-
return '';
|
|
810
|
+
throw new TypeError('table must be specified');
|
|
832
811
|
}
|
|
833
812
|
// 获取表数据
|
|
834
813
|
const rows = await this.run(`SELECT * FROM \`${table}\``, [], timeout);
|
|
835
814
|
let sql = '';
|
|
836
|
-
|
|
815
|
+
|
|
837
816
|
if (rows && rows.length > 0) {
|
|
838
817
|
sql += `-- 表数据: ${table}\n`;
|
|
839
|
-
|
|
818
|
+
|
|
840
819
|
// 批量处理大数据量
|
|
841
820
|
for (let j = 0; j < rows.length; j += batchSize) {
|
|
842
821
|
const batch = rows.slice(j, j + batchSize);
|
|
@@ -861,17 +840,17 @@ DB.prototype.getTableData = async function(table, batchSize = 100, timeout = 600
|
|
|
861
840
|
}
|
|
862
841
|
return "'" + String(value || '') + "'";
|
|
863
842
|
}).join(',');
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
}
|
|
843
|
+
sql += `INSERT INTO \`${table}\` VALUES (${rowValues});\n`;
|
|
844
|
+
} catch (rowErr) {
|
|
845
|
+
this.logger('error', '处理行数据失败', rowErr);
|
|
868
846
|
}
|
|
869
847
|
}
|
|
870
|
-
|
|
848
|
+
}
|
|
849
|
+
sql += '\n';
|
|
871
850
|
}
|
|
872
851
|
return sql;
|
|
873
852
|
} catch (err) {
|
|
874
|
-
|
|
853
|
+
this.logger('error', '获取表数据失败', err);
|
|
875
854
|
return '';
|
|
876
855
|
}
|
|
877
856
|
};
|
|
@@ -883,15 +862,14 @@ DB.prototype.getTableData = async function(table, batchSize = 100, timeout = 600
|
|
|
883
862
|
* @param {Number} timeout 超时时间(毫秒)
|
|
884
863
|
* @return {Promise|Number} 操作结果
|
|
885
864
|
*/
|
|
886
|
-
DB.prototype.backup = function(table, backup, timeout = 60000) {
|
|
865
|
+
DB.prototype.backup = function (table, backup, timeout = 60000) {
|
|
887
866
|
try {
|
|
888
867
|
if (!table || !backup) {
|
|
889
|
-
|
|
890
|
-
return 0;
|
|
868
|
+
throw new TypeError('table and backup must be specified');
|
|
891
869
|
}
|
|
892
870
|
return this.backupTable(table, backup, timeout);
|
|
893
871
|
} catch (err) {
|
|
894
|
-
|
|
872
|
+
this.logger('error', '备份表失败', err);
|
|
895
873
|
return 0;
|
|
896
874
|
}
|
|
897
875
|
};
|
|
@@ -903,10 +881,9 @@ DB.prototype.backup = function(table, backup, timeout = 60000) {
|
|
|
903
881
|
* @param {String} key 主键
|
|
904
882
|
* @return {Promise|Number} 操作结果
|
|
905
883
|
*/
|
|
906
|
-
DB.prototype.createTable = function(table, model, key = 'id', timeout = 15000){
|
|
884
|
+
DB.prototype.createTable = function (table, model, key = 'id', timeout = 15000) {
|
|
907
885
|
if (!table || !model || !model.fields) {
|
|
908
|
-
|
|
909
|
-
return 0;
|
|
886
|
+
throw new TypeError('table, model, and model.fields must be specified');
|
|
910
887
|
}
|
|
911
888
|
var fields = '';
|
|
912
889
|
for (const field in model.fields) {
|
|
@@ -933,4 +910,6 @@ DB.prototype.createTable = function(table, model, key = 'id', timeout = 15000){
|
|
|
933
910
|
return this.exec(sql, [], timeout);
|
|
934
911
|
}
|
|
935
912
|
|
|
936
|
-
module.exports =
|
|
913
|
+
module.exports = {
|
|
914
|
+
DB
|
|
915
|
+
};
|