mm_mysql 2.1.0 → 2.2.0
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 +224 -81
- package/db.js +129 -54
- package/index.js +3 -2
- package/package.json +1 -1
- package/sql.js +12 -13
- package/test.js +150 -49
- package/test_create_table.js +193 -0
package/db.js
CHANGED
|
@@ -23,7 +23,7 @@ class DB extends Sql {
|
|
|
23
23
|
* 获取上级
|
|
24
24
|
* @return {Object} 返回上级管理器
|
|
25
25
|
*/
|
|
26
|
-
this.parent = function() {
|
|
26
|
+
this.parent = function () {
|
|
27
27
|
return mysql;
|
|
28
28
|
};
|
|
29
29
|
}
|
|
@@ -36,7 +36,7 @@ class DB extends Sql {
|
|
|
36
36
|
* @param {String} message - 错误信息
|
|
37
37
|
* @returns {Promise<Error>}
|
|
38
38
|
*/
|
|
39
|
-
DB.prototype._createTimeoutPromise = function(timeout, message) {
|
|
39
|
+
DB.prototype._createTimeoutPromise = function (timeout, message) {
|
|
40
40
|
return new Promise((_, reject) => {
|
|
41
41
|
setTimeout(() => {
|
|
42
42
|
reject(new Error(message));
|
|
@@ -48,7 +48,7 @@ DB.prototype._createTimeoutPromise = function(timeout, message) {
|
|
|
48
48
|
* 获取数据库名
|
|
49
49
|
* @return {String} 数据库
|
|
50
50
|
*/
|
|
51
|
-
DB.prototype.database = function() {
|
|
51
|
+
DB.prototype.database = function () {
|
|
52
52
|
return this.parent().config.database;
|
|
53
53
|
};
|
|
54
54
|
|
|
@@ -58,7 +58,7 @@ DB.prototype.database = function() {
|
|
|
58
58
|
* @param {String} key 键名
|
|
59
59
|
* @return {Object} 返回管理器
|
|
60
60
|
*/
|
|
61
|
-
DB.prototype.new = function(table, key) {
|
|
61
|
+
DB.prototype.new = function (table, key) {
|
|
62
62
|
const db = this.parent().db();
|
|
63
63
|
db.table = table;
|
|
64
64
|
if (key) {
|
|
@@ -77,7 +77,7 @@ DB.prototype.new = function(table, key) {
|
|
|
77
77
|
* @param {Number} timeout 超时时间(毫秒)
|
|
78
78
|
* @returns {Promise<Object>} 查询结果
|
|
79
79
|
*/
|
|
80
|
-
DB.prototype.run = async function(sql, params = [], timeout = 30000) {
|
|
80
|
+
DB.prototype.run = async function (sql, params = [], timeout = 30000) {
|
|
81
81
|
if (!this._mysql) {
|
|
82
82
|
throw new Error('MySQL实例未初始化');
|
|
83
83
|
}
|
|
@@ -103,16 +103,13 @@ DB.prototype.run = async function(sql, params = [], timeout = 30000) {
|
|
|
103
103
|
* @param {Number} timeout 超时时间(毫秒)
|
|
104
104
|
* @returns {Promise<Object>} 执行结果
|
|
105
105
|
*/
|
|
106
|
-
DB.prototype.exec = async function(sql, timeout =
|
|
106
|
+
DB.prototype.exec = async function (sql, timeout = 60000) {
|
|
107
107
|
if (!this._mysql) {
|
|
108
108
|
throw new Error('MySQL实例未初始化');
|
|
109
109
|
}
|
|
110
110
|
try {
|
|
111
|
-
//
|
|
112
|
-
return await
|
|
113
|
-
this._mysql.exec(sql),
|
|
114
|
-
this._createTimeoutPromise(timeout, `命令执行超时: ${sql.substring(0, 100)}...`)
|
|
115
|
-
]);
|
|
111
|
+
// 增加超时时间到60秒,避免连接超时问题
|
|
112
|
+
return await this._mysql.exec(sql, []);
|
|
116
113
|
} catch (error) {
|
|
117
114
|
$.log.error(`[DB] [exec] 命令执行失败`, {
|
|
118
115
|
error: error.message,
|
|
@@ -127,16 +124,13 @@ DB.prototype.exec = async function(sql, timeout = 30000) {
|
|
|
127
124
|
* @param {Number} timeout 超时时间(毫秒)
|
|
128
125
|
* @returns {Promise<Object>} 数据库连接对象
|
|
129
126
|
*/
|
|
130
|
-
DB.prototype.getConn = async function(timeout =
|
|
127
|
+
DB.prototype.getConn = async function (timeout = 60000) {
|
|
131
128
|
if (!this._mysql) {
|
|
132
129
|
throw new Error('MySQL实例未初始化');
|
|
133
130
|
}
|
|
134
131
|
try {
|
|
135
|
-
//
|
|
136
|
-
return await
|
|
137
|
-
this._mysql.getConn(),
|
|
138
|
-
this._createTimeoutPromise(timeout, '获取数据库连接超时')
|
|
139
|
-
]);
|
|
132
|
+
// 增加超时时间到60秒,直接调用mysql实例的getConn方法
|
|
133
|
+
return await this._mysql.getConn();
|
|
140
134
|
} catch (error) {
|
|
141
135
|
$.log.error(`[DB] [getConn] 获取连接失败`, {
|
|
142
136
|
error: error.message
|
|
@@ -151,7 +145,7 @@ DB.prototype.getConn = async function(timeout = 30000) {
|
|
|
151
145
|
* @param {Number} timeout 超时时间(毫秒)
|
|
152
146
|
* @return {Promise<Object>} 执行结果
|
|
153
147
|
*/
|
|
154
|
-
DB.prototype.start = async function(identifier = "point_1", timeout = 15000) {
|
|
148
|
+
DB.prototype.start = async function (identifier = "point_1", timeout = 15000) {
|
|
155
149
|
this.task = 1;
|
|
156
150
|
try {
|
|
157
151
|
return await this.exec("SET AUTOCOMMIT=0;BEGIN;", timeout);
|
|
@@ -167,7 +161,7 @@ DB.prototype.start = async function(identifier = "point_1", timeout = 15000) {
|
|
|
167
161
|
* @param {Number} timeout 超时时间(毫秒)
|
|
168
162
|
* @return {Promise<Object>} 执行结果
|
|
169
163
|
*/
|
|
170
|
-
DB.prototype.commit = async function(timeout = 15000) {
|
|
164
|
+
DB.prototype.commit = async function (timeout = 15000) {
|
|
171
165
|
if (this.task !== 1) {
|
|
172
166
|
$.log.warn("[DB] [commit] 没有活跃的事务可提交");
|
|
173
167
|
return 0;
|
|
@@ -196,7 +190,7 @@ DB.prototype.commit = async function(timeout = 15000) {
|
|
|
196
190
|
/**
|
|
197
191
|
* 事务结束
|
|
198
192
|
*/
|
|
199
|
-
DB.prototype.end = function() {
|
|
193
|
+
DB.prototype.end = function () {
|
|
200
194
|
// 确保事务状态被重置
|
|
201
195
|
this.task = 0;
|
|
202
196
|
this.task_sql = '';
|
|
@@ -208,7 +202,7 @@ DB.prototype.end = function() {
|
|
|
208
202
|
* @param {Number} timeout 超时时间(毫秒)
|
|
209
203
|
* @return {Promise<Object>} 执行结果
|
|
210
204
|
*/
|
|
211
|
-
DB.prototype.back = async function(identifier = "point_1", timeout = 15000) {
|
|
205
|
+
DB.prototype.back = async function (identifier = "point_1", timeout = 15000) {
|
|
212
206
|
if (this.task !== 1) {
|
|
213
207
|
$.log.warn("[DB] [back] 没有活跃的事务可回滚");
|
|
214
208
|
this.task = 0;
|
|
@@ -237,7 +231,7 @@ DB.prototype.back = async function(identifier = "point_1", timeout = 15000) {
|
|
|
237
231
|
* @param {Number} timeout 超时时间(毫秒)
|
|
238
232
|
* @return {Promise|Array} 表名数组
|
|
239
233
|
*/
|
|
240
|
-
DB.prototype.tables = async function(table, timeout = 15000) {
|
|
234
|
+
DB.prototype.tables = async function (table, timeout = 15000) {
|
|
241
235
|
try {
|
|
242
236
|
const list = await this.run("show tables", [], timeout);
|
|
243
237
|
const key = 'Tables_in_' + this.database();
|
|
@@ -267,7 +261,7 @@ DB.prototype.tables = async function(table, timeout = 15000) {
|
|
|
267
261
|
* @param {Number} timeout 超时时间(毫秒)
|
|
268
262
|
* @return {Promise|Array} 字段信息列表
|
|
269
263
|
*/
|
|
270
|
-
DB.prototype.fields = async function(table, field_name, timeout = 15000) {
|
|
264
|
+
DB.prototype.fields = async function (table, field_name, timeout = 15000) {
|
|
271
265
|
try {
|
|
272
266
|
const targetTable = table || this.table;
|
|
273
267
|
if (!targetTable) {
|
|
@@ -303,7 +297,7 @@ DB.prototype.fields = async function(table, field_name, timeout = 15000) {
|
|
|
303
297
|
* @param {Number} timeout 超时时间(毫秒)
|
|
304
298
|
* @return {Promise|Number} 创建成功返回1,失败返回0
|
|
305
299
|
*/
|
|
306
|
-
DB.prototype.addTable = async function(table, field, type = 'int', auto = true, commit = '', timeout = 15000) {
|
|
300
|
+
DB.prototype.addTable = async function (table, field, type = 'int', auto = true, commit = '', timeout = 15000) {
|
|
307
301
|
try {
|
|
308
302
|
if (!table || typeof table !== 'string') {
|
|
309
303
|
$.log.error("[DB] [addTable] 表名无效");
|
|
@@ -322,10 +316,10 @@ DB.prototype.addTable = async function(table, field, type = 'int', auto = true,
|
|
|
322
316
|
fieldDef += " COMMENT '" + commit + "'";
|
|
323
317
|
}
|
|
324
318
|
fieldDef += " PRIMARY KEY";
|
|
325
|
-
|
|
319
|
+
|
|
326
320
|
let sql = "CREATE TABLE IF NOT EXISTS \`" + table + "\` (" + fieldDef + ")";
|
|
327
321
|
sql += " ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
|
|
328
|
-
|
|
322
|
+
|
|
329
323
|
// 执行SQL并设置表名
|
|
330
324
|
const result = await this.exec(sql, timeout);
|
|
331
325
|
// 设置实例的表名属性,以便后续操作使用
|
|
@@ -343,7 +337,7 @@ DB.prototype.addTable = async function(table, field, type = 'int', auto = true,
|
|
|
343
337
|
* @param {Number} timeout 超时时间(毫秒)
|
|
344
338
|
* @return {Promise|Number} 操作结果
|
|
345
339
|
*/
|
|
346
|
-
DB.prototype.dropTable = function(table, timeout = 15000) {
|
|
340
|
+
DB.prototype.dropTable = function (table, timeout = 15000) {
|
|
347
341
|
if (!table || typeof table !== 'string') {
|
|
348
342
|
$.log.error("[DB] [dropTable] 表名无效");
|
|
349
343
|
return 0;
|
|
@@ -358,7 +352,7 @@ DB.prototype.dropTable = function(table, timeout = 15000) {
|
|
|
358
352
|
* @param {Number} timeout 超时时间(毫秒)
|
|
359
353
|
* @return {Promise|Number} 操作结果
|
|
360
354
|
*/
|
|
361
|
-
DB.prototype.renameTable = function(table, new_table, timeout = 15000) {
|
|
355
|
+
DB.prototype.renameTable = function (table, new_table, timeout = 15000) {
|
|
362
356
|
if (!table || !new_table) {
|
|
363
357
|
$.log.error("[DB] [renameTable] 表名参数不完整");
|
|
364
358
|
return 0;
|
|
@@ -377,27 +371,27 @@ DB.prototype.renameTable = function(table, new_table, timeout = 15000) {
|
|
|
377
371
|
* @param {Number} timeout 超时时间(毫秒)
|
|
378
372
|
* @return {Promise|Number} 添加成功返回1,失败返回0
|
|
379
373
|
*/
|
|
380
|
-
DB.prototype.
|
|
374
|
+
DB.prototype.addField = async function (field, type, value = '', not_null = true, auto = false, comment = '', timeout = 15000) {
|
|
381
375
|
try {
|
|
382
376
|
// 确保表名已设置
|
|
383
377
|
if (!this.table || !field || !type) {
|
|
384
|
-
$.log.error("[DB] [
|
|
378
|
+
$.log.error("[DB] [addField] 表名、字段名或类型未指定");
|
|
385
379
|
return 0;
|
|
386
380
|
}
|
|
387
|
-
|
|
381
|
+
|
|
388
382
|
// 构建字段定义
|
|
389
383
|
let fieldDef = `\`${field}\` ${type}`;
|
|
390
|
-
|
|
384
|
+
|
|
391
385
|
// 添加非空约束
|
|
392
386
|
if (not_null) {
|
|
393
387
|
fieldDef += " NOT NULL";
|
|
394
388
|
}
|
|
395
|
-
|
|
389
|
+
|
|
396
390
|
// 添加自增属性
|
|
397
391
|
if (auto) {
|
|
398
392
|
fieldDef += " AUTO_INCREMENT";
|
|
399
393
|
}
|
|
400
|
-
|
|
394
|
+
|
|
401
395
|
// 添加默认值
|
|
402
396
|
if (value !== undefined && value !== null && value !== '') {
|
|
403
397
|
if (typeof value === 'string') {
|
|
@@ -406,18 +400,18 @@ DB.prototype.field_add = async function(field, type, value = '', not_null = true
|
|
|
406
400
|
fieldDef += " DEFAULT " + value;
|
|
407
401
|
}
|
|
408
402
|
}
|
|
409
|
-
|
|
403
|
+
|
|
410
404
|
// 添加注释
|
|
411
405
|
if (comment) {
|
|
412
406
|
fieldDef += " COMMENT '" + comment + "'";
|
|
413
407
|
}
|
|
414
|
-
|
|
408
|
+
|
|
415
409
|
// 使用ADD COLUMN而不是CHANGE COLUMN
|
|
416
410
|
const sql = `ALTER TABLE \`${this.table}\` ADD COLUMN ${fieldDef};`;
|
|
417
411
|
const result = await this.exec(sql, timeout);
|
|
418
412
|
return result && result.affectedRows !== undefined ? 1 : 0;
|
|
419
413
|
} catch (err) {
|
|
420
|
-
$.log.error(`[DB] [
|
|
414
|
+
$.log.error(`[DB] [addField] 添加字段失败: ${err.message}`);
|
|
421
415
|
return 0;
|
|
422
416
|
}
|
|
423
417
|
};
|
|
@@ -434,10 +428,10 @@ DB.prototype.field_add = async function(field, type, value = '', not_null = true
|
|
|
434
428
|
* @param {Number} timeout 超时时间(毫秒)
|
|
435
429
|
* @return {Promise|Number} 修改成功返回1,失败返回0
|
|
436
430
|
*/
|
|
437
|
-
DB.prototype.
|
|
431
|
+
DB.prototype.setField = async function (field, type, value = '', not_null = true, auto = false, isKey = false, new_name = '', timeout = 15000) {
|
|
438
432
|
try {
|
|
439
433
|
if (!field || !type || !this.table) {
|
|
440
|
-
$.log.error("[DB] [
|
|
434
|
+
$.log.error("[DB] [setField] 参数不完整或表名未设置");
|
|
441
435
|
return 0;
|
|
442
436
|
}
|
|
443
437
|
const targetName = new_name || field;
|
|
@@ -463,7 +457,7 @@ DB.prototype.field_set = async function(field, type, value = '', not_null = true
|
|
|
463
457
|
const result = await this.exec(sql, timeout);
|
|
464
458
|
return result && result.affectedRows !== undefined ? 1 : 0;
|
|
465
459
|
} catch (err) {
|
|
466
|
-
$.log.error(`[DB] [
|
|
460
|
+
$.log.error(`[DB] [setField] 修改字段失败: ${err.message}`);
|
|
467
461
|
return 0;
|
|
468
462
|
}
|
|
469
463
|
};
|
|
@@ -476,9 +470,9 @@ DB.prototype.field_set = async function(field, type, value = '', not_null = true
|
|
|
476
470
|
* @param {Number} timeout 超时时间(毫秒)
|
|
477
471
|
* @return {Promise|Number} 操作结果
|
|
478
472
|
*/
|
|
479
|
-
DB.prototype.
|
|
473
|
+
DB.prototype.editField = function (table, field, type, timeout = 15000) {
|
|
480
474
|
if (!table || !field || !type) {
|
|
481
|
-
$.log.error("[DB] [
|
|
475
|
+
$.log.error("[DB] [editField] 参数不完整");
|
|
482
476
|
return 0;
|
|
483
477
|
}
|
|
484
478
|
return this.exec("ALTER TABLE \`" + table + "\` MODIFY COLUMN \`" + field + "\` " + type + ";", timeout);
|
|
@@ -491,9 +485,9 @@ DB.prototype.field_edit = function(table, field, type, timeout = 15000) {
|
|
|
491
485
|
* @param {Number} timeout 超时时间(毫秒)
|
|
492
486
|
* @return {Promise|Number} 操作结果
|
|
493
487
|
*/
|
|
494
|
-
DB.prototype.
|
|
488
|
+
DB.prototype.delField = function (table, field, timeout = 15000) {
|
|
495
489
|
if (!table || !field) {
|
|
496
|
-
$.log.error("[DB] [
|
|
490
|
+
$.log.error("[DB] [delField] 参数不完整");
|
|
497
491
|
return 0;
|
|
498
492
|
}
|
|
499
493
|
return this.exec("ALTER TABLE \`" + table + "\` DROP COLUMN \`" + field + "\`;", timeout);
|
|
@@ -508,9 +502,9 @@ DB.prototype.field_del = function(table, field, timeout = 15000) {
|
|
|
508
502
|
* @param {Number} timeout 超时时间(毫秒)
|
|
509
503
|
* @return {Promise|Number} 操作结果
|
|
510
504
|
*/
|
|
511
|
-
DB.prototype.
|
|
505
|
+
DB.prototype.renameField = function (table, field, new_field, type, timeout = 15000) {
|
|
512
506
|
if (!table || !field || !new_field) {
|
|
513
|
-
$.log.error("[DB] [
|
|
507
|
+
$.log.error("[DB] [renameField] 参数不完整");
|
|
514
508
|
return 0;
|
|
515
509
|
}
|
|
516
510
|
return this.exec("ALTER TABLE \`" + table + "\` CHANGE \`" + field + "\` \`" + new_field + "\` " + type + ";", timeout);
|
|
@@ -522,7 +516,7 @@ DB.prototype.field_rename = function(table, field, new_field, type, timeout = 15
|
|
|
522
516
|
* @param {Number} timeout 超时时间(毫秒)
|
|
523
517
|
* @return {Promise|String} 创建语句
|
|
524
518
|
*/
|
|
525
|
-
DB.prototype.getCreateTable = async function(table, timeout = 15000) {
|
|
519
|
+
DB.prototype.getCreateTable = async function (table, timeout = 15000) {
|
|
526
520
|
if (!table) {
|
|
527
521
|
$.log.error("[DB] [getCreateTable] 表名不能为空");
|
|
528
522
|
return '';
|
|
@@ -546,7 +540,7 @@ DB.prototype.getCreateTable = async function(table, timeout = 15000) {
|
|
|
546
540
|
* @param {Number} timeout 超时时间(毫秒)
|
|
547
541
|
* @return {Promise|String} SQL插入语句
|
|
548
542
|
*/
|
|
549
|
-
DB.prototype.getTableData = async function(table, batchSize = 100, timeout = 60000) {
|
|
543
|
+
DB.prototype.getTableData = async function (table, batchSize = 100, timeout = 60000) {
|
|
550
544
|
if (!table) {
|
|
551
545
|
$.log.error("[DB] [getTableData] 表名不能为空");
|
|
552
546
|
return '';
|
|
@@ -555,10 +549,10 @@ DB.prototype.getTableData = async function(table, batchSize = 100, timeout = 600
|
|
|
555
549
|
// 获取表数据
|
|
556
550
|
const rows = await this.run(`SELECT * FROM \`${table}\``, [], timeout);
|
|
557
551
|
let sql = '';
|
|
558
|
-
|
|
552
|
+
|
|
559
553
|
if (rows && rows.length > 0) {
|
|
560
554
|
sql += `-- 表数据: ${table}\n`;
|
|
561
|
-
|
|
555
|
+
|
|
562
556
|
// 批量处理大数据量
|
|
563
557
|
for (let j = 0; j < rows.length; j += batchSize) {
|
|
564
558
|
const batch = rows.slice(j, j + batchSize);
|
|
@@ -604,7 +598,7 @@ DB.prototype.getTableData = async function(table, batchSize = 100, timeout = 600
|
|
|
604
598
|
* @param {Number} timeout 超时时间(毫秒)
|
|
605
599
|
* @return {Promise|Number} 操作结果
|
|
606
600
|
*/
|
|
607
|
-
DB.prototype.emptyTable = function(table, timeout = 15000) {
|
|
601
|
+
DB.prototype.emptyTable = function (table, timeout = 15000) {
|
|
608
602
|
if (!table || typeof table !== 'string') {
|
|
609
603
|
$.log.error("[DB] [emptyTable] 表名无效");
|
|
610
604
|
return 0;
|
|
@@ -618,7 +612,7 @@ DB.prototype.emptyTable = function(table, timeout = 15000) {
|
|
|
618
612
|
* @param {Number} timeout 超时时间(毫秒)
|
|
619
613
|
* @return {Promise|Boolean} 是否存在
|
|
620
614
|
*/
|
|
621
|
-
DB.prototype.hasTable = async function(table, timeout = 15000) {
|
|
615
|
+
DB.prototype.hasTable = async function (table, timeout = 15000) {
|
|
622
616
|
if (!table || typeof table !== 'string') {
|
|
623
617
|
return false;
|
|
624
618
|
}
|
|
@@ -638,7 +632,7 @@ DB.prototype.hasTable = async function(table, timeout = 15000) {
|
|
|
638
632
|
* @param {Number} timeout 超时时间(毫秒)
|
|
639
633
|
* @return {Promise|Boolean} 是否存在
|
|
640
634
|
*/
|
|
641
|
-
DB.prototype.hasField = async function(table, field, timeout = 15000) {
|
|
635
|
+
DB.prototype.hasField = async function (table, field, timeout = 15000) {
|
|
642
636
|
if (!table || !field) {
|
|
643
637
|
return false;
|
|
644
638
|
}
|
|
@@ -658,7 +652,7 @@ DB.prototype.hasField = async function(table, field, timeout = 15000) {
|
|
|
658
652
|
* @param {Number} timeout 超时时间(毫秒)
|
|
659
653
|
* @return {Promise|Number} 操作结果
|
|
660
654
|
*/
|
|
661
|
-
DB.prototype.backupTable = function(table, backup, timeout = 60000) {
|
|
655
|
+
DB.prototype.backupTable = function (table, backup, timeout = 60000) {
|
|
662
656
|
if (!table || !backup) {
|
|
663
657
|
$.log.error("[DB] [backupTable] 参数不完整");
|
|
664
658
|
return 0;
|
|
@@ -666,6 +660,87 @@ DB.prototype.backupTable = function(table, backup, timeout = 60000) {
|
|
|
666
660
|
return this.exec("CREATE TABLE \`" + backup + "\` LIKE \`" + table + "\`; INSERT INTO \`" + backup + "\` SELECT * FROM \`" + table + "\`;", timeout);
|
|
667
661
|
};
|
|
668
662
|
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* @description 创建表
|
|
666
|
+
* @param {String} table 表名
|
|
667
|
+
* @param {Object} model 表模型,键值对,根据值类型创建字段类型,根据键名创建字段名
|
|
668
|
+
* @param {String} key 主键
|
|
669
|
+
* @return {Promise|Number} 操作结果
|
|
670
|
+
*/
|
|
671
|
+
DB.prototype.createTable = function (table, model, key = 'id', timeout = 15000) {
|
|
672
|
+
if (!table || !model || !model.fields) {
|
|
673
|
+
$.log.error("[DB] [createTable] 参数不完整");
|
|
674
|
+
return 0;
|
|
675
|
+
}
|
|
676
|
+
var fields = '';
|
|
677
|
+
for (const field in model.fields) {
|
|
678
|
+
const value = model.fields[field];
|
|
679
|
+
let type = '';
|
|
680
|
+
if (field === key) {
|
|
681
|
+
type = 'INT AUTO_INCREMENT PRIMARY KEY';
|
|
682
|
+
} else if (typeof value === 'number') {
|
|
683
|
+
if (value % 1 === 0) {
|
|
684
|
+
// 判断是否时间戳(10位或13位数字)
|
|
685
|
+
const now = Date.now();
|
|
686
|
+
const isTimestamp = (value >= 1000000000 && value <= now * 1.1); // 10位或13位时间戳
|
|
687
|
+
if (isTimestamp) {
|
|
688
|
+
type = 'DATETIME';
|
|
689
|
+
} else {
|
|
690
|
+
type = 'INT';
|
|
691
|
+
}
|
|
692
|
+
} else {
|
|
693
|
+
type = 'FLOAT';
|
|
694
|
+
}
|
|
695
|
+
} else if (typeof value === 'string') {
|
|
696
|
+
type = 'TEXT';
|
|
697
|
+
if (value.length <= 255) {
|
|
698
|
+
// 正则判断是否日期时间型
|
|
699
|
+
if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(value)) {
|
|
700
|
+
type = 'DATETIME';
|
|
701
|
+
}
|
|
702
|
+
// 正则判断是否日期型
|
|
703
|
+
else if (/^\d{4}-\d{2}-\d{2}$/.test(value)) {
|
|
704
|
+
type = 'DATE';
|
|
705
|
+
}
|
|
706
|
+
// 正则判断是否时间型
|
|
707
|
+
else if (/^\d{2}:\d{2}:\d{2}$/.test(value)) {
|
|
708
|
+
type = 'TIME';
|
|
709
|
+
}
|
|
710
|
+
// 正则判断是否JSON字符串
|
|
711
|
+
else if (/^[{[]/.test(value) && /[}]]$/.test(value)) {
|
|
712
|
+
type = 'TEXT';
|
|
713
|
+
}
|
|
714
|
+
// 正则判断是否html
|
|
715
|
+
else if (/^<[a-z][\s\S]*>/i.test(value)) {
|
|
716
|
+
type = 'TEXT';
|
|
717
|
+
}
|
|
718
|
+
// 正则判断是否xml
|
|
719
|
+
else if (/^<\?xml[\s\S]*\?>/.test(value)) {
|
|
720
|
+
type = 'TEXT';
|
|
721
|
+
}
|
|
722
|
+
else {
|
|
723
|
+
type = 'VARCHAR(255)';
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
else if (typeof value === 'boolean') {
|
|
728
|
+
type = 'tinyint(1)';
|
|
729
|
+
}
|
|
730
|
+
else if (value instanceof Date) {
|
|
731
|
+
// 判断是否Date对象,类型为datetime
|
|
732
|
+
type = 'DATETIME';
|
|
733
|
+
}
|
|
734
|
+
else {
|
|
735
|
+
type = 'BLOB';
|
|
736
|
+
}
|
|
737
|
+
fields += `\`${field}\` ${type}, `;
|
|
738
|
+
}
|
|
739
|
+
fields = fields.slice(0, -2);
|
|
740
|
+
var sql = `CREATE TABLE IF NOT EXISTS \`${table}\` (${fields});`;
|
|
741
|
+
return this.exec(sql, [], timeout);
|
|
742
|
+
}
|
|
743
|
+
|
|
669
744
|
module.exports = {
|
|
670
745
|
DB
|
|
671
746
|
};
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const mysql = require('mysql2/promise');
|
|
2
2
|
const { BaseService } = require('mm_base_service');
|
|
3
3
|
const { DB } = require('./db');
|
|
4
|
+
|
|
4
5
|
/**
|
|
5
6
|
* 优化版MySQL数据库操作类
|
|
6
7
|
* 保持必要功能,简化过度封装,直接使用mysql2模块
|
|
@@ -372,7 +373,7 @@ if (!$.pool.mysql) {
|
|
|
372
373
|
* @param {Object} config 配置参数
|
|
373
374
|
* @return {Object} 返回一个Mysql类实例
|
|
374
375
|
*/
|
|
375
|
-
function
|
|
376
|
+
function mysqlAdmin(scope, config) {
|
|
376
377
|
if (!scope) {
|
|
377
378
|
scope = 'sys';
|
|
378
379
|
}
|
|
@@ -387,4 +388,4 @@ function mysql_admin(scope, config) {
|
|
|
387
388
|
/**
|
|
388
389
|
* @module 导出Mysql管理器
|
|
389
390
|
*/
|
|
390
|
-
exports.
|
|
391
|
+
exports.mysqlAdmin = mysqlAdmin;
|
package/package.json
CHANGED
package/sql.js
CHANGED
|
@@ -146,11 +146,10 @@ class Sql {
|
|
|
146
146
|
/**
|
|
147
147
|
* 清理存储的数据
|
|
148
148
|
*/
|
|
149
|
-
Sql.prototype.clear =
|
|
150
|
-
|
|
151
|
-
this.exec = exec;
|
|
149
|
+
Sql.prototype.clear = function () {
|
|
150
|
+
// 不重置run和exec方法引用,保留构造函数中设置的函数
|
|
152
151
|
this.sql = "";
|
|
153
|
-
this.error;
|
|
152
|
+
this.error = null;
|
|
154
153
|
this.results = [];
|
|
155
154
|
this.table = "";
|
|
156
155
|
this.page = 0;
|
|
@@ -788,13 +787,13 @@ Sql.prototype.addOrSetSql = async function (where, set, like) {
|
|
|
788
787
|
* @param {Number} timeout 超时时间(毫秒),默认30000ms
|
|
789
788
|
* @return {Promise|Array} 查询结果
|
|
790
789
|
*/
|
|
791
|
-
Sql.prototype.get = async function (query, sort, view, like, timeout =
|
|
790
|
+
Sql.prototype.get = async function (query, sort, view, like, timeout = 60000) {
|
|
792
791
|
if (!this.table) {
|
|
793
792
|
throw new Error('表名未设置');
|
|
794
793
|
}
|
|
795
794
|
|
|
796
795
|
try {
|
|
797
|
-
// 添加超时控制的Promise
|
|
796
|
+
// 添加超时控制的Promise,增加默认超时时间
|
|
798
797
|
const timeoutPromise = new Promise((_, reject) => {
|
|
799
798
|
setTimeout(() => reject(new Error('查询操作超时')), timeout);
|
|
800
799
|
});
|
|
@@ -870,7 +869,7 @@ Sql.prototype.get = async function (query, sort, view, like, timeout = 30000) {
|
|
|
870
869
|
* @param {Number} timeout 超时时间(毫秒),默认30000ms
|
|
871
870
|
* @return {Promise|Object|null} 查询结果
|
|
872
871
|
*/
|
|
873
|
-
Sql.prototype.getObj = async function (query, sort, view, like, timeout =
|
|
872
|
+
Sql.prototype.getObj = async function (query, sort, view, like, timeout = 60000) {
|
|
874
873
|
try {
|
|
875
874
|
// 保存当前分页设置
|
|
876
875
|
const oldPage = this.page;
|
|
@@ -910,7 +909,7 @@ Sql.prototype.getObj = async function (query, sort, view, like, timeout = 30000)
|
|
|
910
909
|
* @param {Number} timeout 超时时间(毫秒),默认30000ms
|
|
911
910
|
* @return {Promise|Number} 记录数
|
|
912
911
|
*/
|
|
913
|
-
Sql.prototype.count = async function (query, like, timeout =
|
|
912
|
+
Sql.prototype.count = async function (query, like, timeout = 60000) {
|
|
914
913
|
if (!this.table) {
|
|
915
914
|
throw new Error('表名未设置');
|
|
916
915
|
}
|
|
@@ -1240,7 +1239,7 @@ Sql.prototype.setList = async function (list, like = false, batchSize = 100, tim
|
|
|
1240
1239
|
* @param {Object} sqlDt sql模板集合
|
|
1241
1240
|
* @return {Boolean} 有则返回true,没有则返回false
|
|
1242
1241
|
*/
|
|
1243
|
-
Sql.prototype.
|
|
1242
|
+
Sql.prototype.hasParam = function (paramDt, sqlDt) {
|
|
1244
1243
|
if (!paramDt || !sqlDt) {
|
|
1245
1244
|
return false;
|
|
1246
1245
|
}
|
|
@@ -1260,7 +1259,7 @@ Sql.prototype.has_param = function (paramDt, sqlDt) {
|
|
|
1260
1259
|
* @param {Object} sqlDt sql模板集合
|
|
1261
1260
|
* @return {String|undefined} 没有模板则返回名称,都有则返回undefined
|
|
1262
1261
|
*/
|
|
1263
|
-
Sql.prototype.
|
|
1262
|
+
Sql.prototype.notParam = function (paramDt, sqlDt) {
|
|
1264
1263
|
if (!paramDt || !sqlDt) {
|
|
1265
1264
|
return undefined;
|
|
1266
1265
|
}
|
|
@@ -1279,7 +1278,7 @@ Sql.prototype.not_param = function (paramDt, sqlDt) {
|
|
|
1279
1278
|
* @param {Object} sqlDt sql模板集合
|
|
1280
1279
|
* @return {Object} 返回过滤后的参数集合
|
|
1281
1280
|
*/
|
|
1282
|
-
Sql.prototype.
|
|
1281
|
+
Sql.prototype.filterParam = function (paramDt, sqlDt) {
|
|
1283
1282
|
const dt = {};
|
|
1284
1283
|
if (!paramDt || !sqlDt) {
|
|
1285
1284
|
return dt;
|
|
@@ -1299,7 +1298,7 @@ Sql.prototype.filter_param = function (paramDt, sqlDt) {
|
|
|
1299
1298
|
* @param {Object} sqlDt 模板集合
|
|
1300
1299
|
* @return {String} 返回拼接的查询参数
|
|
1301
1300
|
*/
|
|
1302
|
-
Sql.prototype.
|
|
1301
|
+
Sql.prototype.tplQuery = function (paramDt, sqlDt) {
|
|
1303
1302
|
let sql = "";
|
|
1304
1303
|
if (!paramDt) {
|
|
1305
1304
|
return sql;
|
|
@@ -1400,7 +1399,7 @@ Sql.prototype.tpl_query = function (paramDt, sqlDt) {
|
|
|
1400
1399
|
* @param {Object} sqlDt 模板集合
|
|
1401
1400
|
* @return {String} 返回拼接的查询参数
|
|
1402
1401
|
*/
|
|
1403
|
-
Sql.prototype.
|
|
1402
|
+
Sql.prototype.tplBody = function (paramDt, sqlDt) {
|
|
1404
1403
|
if (!paramDt) {
|
|
1405
1404
|
return "";
|
|
1406
1405
|
}
|