mm_mysql 1.9.1 → 1.9.3
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 +154 -47
- package/package.json +8 -12
- package/test.js +33 -6
- package/data.sql +0 -4963
package/index.js
CHANGED
|
@@ -11,6 +11,9 @@ const {
|
|
|
11
11
|
} = require('./db');
|
|
12
12
|
|
|
13
13
|
const Link_model = require('./link_model');
|
|
14
|
+
const {
|
|
15
|
+
table
|
|
16
|
+
} = require('console');
|
|
14
17
|
|
|
15
18
|
var pool = {};
|
|
16
19
|
|
|
@@ -87,6 +90,10 @@ class Mysql {
|
|
|
87
90
|
$.log.debug("SQL:", sql);
|
|
88
91
|
}
|
|
89
92
|
|
|
93
|
+
if (!$this.conn) {
|
|
94
|
+
$this.open();
|
|
95
|
+
}
|
|
96
|
+
|
|
90
97
|
// 返回一个 Promise
|
|
91
98
|
return new Promise((resolve, reject) => {
|
|
92
99
|
$this.conn.getConnection(function(err, db) {
|
|
@@ -151,6 +158,11 @@ class Mysql {
|
|
|
151
158
|
if ($this.config.log) {
|
|
152
159
|
$.log.debug("SQL:", sql);
|
|
153
160
|
}
|
|
161
|
+
|
|
162
|
+
if (!$this.conn) {
|
|
163
|
+
$this.open();
|
|
164
|
+
}
|
|
165
|
+
|
|
154
166
|
// 返回一个 Promise
|
|
155
167
|
return new Promise((resolve, reject) => {
|
|
156
168
|
$this.conn.getConnection(function(err, db) {
|
|
@@ -216,66 +228,161 @@ class Mysql {
|
|
|
216
228
|
this.db = function() {
|
|
217
229
|
return new DB($this);
|
|
218
230
|
};
|
|
231
|
+
}
|
|
232
|
+
}
|
|
219
233
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
if (this.error.code == 1045 || this.error.code == 2003) {
|
|
249
|
-
break;
|
|
250
|
-
}
|
|
251
|
-
} else {
|
|
252
|
-
bl = true;
|
|
253
|
-
}
|
|
234
|
+
/**
|
|
235
|
+
* 导入数据库
|
|
236
|
+
* @param {Object} file sql文件
|
|
237
|
+
* @param {Function} func 回调函数
|
|
238
|
+
* @return {Promise} 异步构造器, 当await时返回执行结果
|
|
239
|
+
*/
|
|
240
|
+
Mysql.prototype.load = async function(file, func) {
|
|
241
|
+
var count = 0;
|
|
242
|
+
var progress = 0;
|
|
243
|
+
var errors = [];
|
|
244
|
+
var index = 0;
|
|
245
|
+
try {
|
|
246
|
+
var data = file.loadText();
|
|
247
|
+
// 将SQL文件内容分割成单独的语句
|
|
248
|
+
const arr = data.split(';\r\n');
|
|
249
|
+
count = arr.length;
|
|
250
|
+
for (var i = 0; i < arr.length; i++) {
|
|
251
|
+
var sql_str = arr[i].trim();
|
|
252
|
+
if (sql_str !== '') {
|
|
253
|
+
await this.run(sql_str + ";");
|
|
254
|
+
if (this.error) {
|
|
255
|
+
errors.push({
|
|
256
|
+
sql_str: sql_str.slice(0, 512),
|
|
257
|
+
error: this.error
|
|
258
|
+
});
|
|
259
|
+
// 如果数据库链接失败(密码错误或无法连接),则退出循环
|
|
260
|
+
if (this.error.code == 1045 || this.error.code == 2003) {
|
|
261
|
+
break;
|
|
254
262
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
var p = i / arr.length;
|
|
266
|
+
progress = Math.ceil(p * 100);
|
|
267
|
+
index = i;
|
|
268
|
+
if (func) {
|
|
269
|
+
func(progress, index, this.error, sql_str);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
} catch (err) {
|
|
273
|
+
$.log.error("导入SQL文件失败!", err);
|
|
274
|
+
}
|
|
275
|
+
return {
|
|
276
|
+
index,
|
|
277
|
+
count,
|
|
278
|
+
progress,
|
|
279
|
+
errors
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* 保存数据库为文件
|
|
285
|
+
* @param {String} file 文件对象
|
|
286
|
+
* @param {Function} func 回调函数
|
|
287
|
+
* @return {Promise} 异步构造器, 当await时返回执行结果
|
|
288
|
+
*/
|
|
289
|
+
Mysql.prototype.save = async function(file, func, tables = []) {
|
|
290
|
+
const fs = require('fs');
|
|
291
|
+
const stream = fs.createWriteStream(file);
|
|
292
|
+
let count = 0;
|
|
293
|
+
let progress = 0;
|
|
294
|
+
let errors = [];
|
|
295
|
+
let index = 0;
|
|
296
|
+
let tableCount = 0;
|
|
297
|
+
|
|
298
|
+
try {
|
|
299
|
+
// 开始导出数据库
|
|
300
|
+
stream.write('SET FOREIGN_KEY_CHECKS = 0;\r\n\r\n');
|
|
301
|
+
count++;
|
|
302
|
+
|
|
303
|
+
if (!tables.length) {
|
|
304
|
+
// 获取所有表
|
|
305
|
+
var tbs = await this.run('SHOW TABLES');
|
|
306
|
+
tables = tbs.map((item) => {
|
|
307
|
+
return item[Object.keys(item)[0]];
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
tableCount = tables.length;
|
|
312
|
+
console.log("tables", tables);
|
|
313
|
+
for (var i = 0; i < tables.length; i++) {
|
|
314
|
+
var tableName = tables[i];
|
|
315
|
+
try {
|
|
316
|
+
// 导出表结构
|
|
317
|
+
const createTable = await this.run(`SHOW CREATE TABLE ${tableName}`);
|
|
318
|
+
stream.write(`-- 表结构: ${tableName}\r\n`);
|
|
319
|
+
stream.write(`DROP TABLE IF EXISTS \`${tableName}\`;\r\n`);
|
|
320
|
+
count++;
|
|
321
|
+
stream.write(`${createTable[0]['Create Table']};\r\n\r\n`);
|
|
322
|
+
count++;
|
|
323
|
+
// 导出表数据
|
|
324
|
+
const rows = await this.run(`SELECT * FROM ${tableName}`);
|
|
325
|
+
if (rows.length > 0) {
|
|
326
|
+
stream.write(`-- 表数据: ${tableName}\r\n`);
|
|
327
|
+
for (const row of rows) {
|
|
328
|
+
const rowValues = Object.values(row)
|
|
329
|
+
.map(value => {
|
|
330
|
+
if (value === null) return 'NULL';
|
|
331
|
+
if (typeof value === 'boolean') return value ? 1 : 0;
|
|
332
|
+
if (typeof value === 'number') return value;
|
|
333
|
+
if (value instanceof Date) {
|
|
334
|
+
return "'" + value.toStr('yyyy-MM-dd hh:mm:ss') + "'";
|
|
335
|
+
}
|
|
336
|
+
// 处理字符串,转义特殊字符
|
|
337
|
+
return "'" + value.toString()
|
|
338
|
+
.replace(/[\\']/g, '\\$&')
|
|
339
|
+
.replace(/\n/g, '\\n')
|
|
340
|
+
.replace(/\r/g, '\\r')
|
|
341
|
+
.replace(/\t/g, '\\t')
|
|
342
|
+
.replace(/\u0000/g, '\\0') + "'";
|
|
343
|
+
})
|
|
344
|
+
.join(',');
|
|
345
|
+
stream.write(`INSERT INTO ${tableName} VALUES (${rowValues});\r\n`);
|
|
346
|
+
count++;
|
|
260
347
|
}
|
|
348
|
+
stream.write('\r\n');
|
|
261
349
|
}
|
|
262
350
|
} catch (err) {
|
|
263
|
-
|
|
351
|
+
errors.push({
|
|
352
|
+
table: tableName,
|
|
353
|
+
error: err.message
|
|
354
|
+
});
|
|
264
355
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
356
|
+
|
|
357
|
+
index++;
|
|
358
|
+
progress = Math.ceil((index / tableCount) * 100);
|
|
359
|
+
if (func) {
|
|
360
|
+
func(progress, index, this.error, tableName);
|
|
270
361
|
}
|
|
271
362
|
}
|
|
363
|
+
|
|
364
|
+
// 完成导出
|
|
365
|
+
stream.write('SET FOREIGN_KEY_CHECKS = 1;\r\n');
|
|
366
|
+
count++;
|
|
367
|
+
stream.end();
|
|
368
|
+
} catch (err) {
|
|
369
|
+
$.log.error("导出SQL文件失败!", err);
|
|
370
|
+
stream.end();
|
|
272
371
|
}
|
|
273
|
-
|
|
372
|
+
|
|
373
|
+
return {
|
|
374
|
+
tableCount,
|
|
375
|
+
index,
|
|
376
|
+
count,
|
|
377
|
+
progress,
|
|
378
|
+
errors
|
|
379
|
+
}
|
|
380
|
+
};
|
|
274
381
|
|
|
275
382
|
/**
|
|
276
383
|
* 获取数据库管理器
|
|
277
384
|
* @param {String} key 主键
|
|
278
|
-
* @param {String|Number} value 对象值
|
|
385
|
+
* @param {String|Number} value 对象值
|
|
279
386
|
* @param {Boolean} clear_prefix 清除前缀
|
|
280
387
|
* @param {Array} arr_table 关联的数据表
|
|
281
388
|
* @return {Object} 管理模型
|
package/package.json
CHANGED
|
@@ -1,21 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
{
|
|
2
2
|
"name": "mm_mysql",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.3",
|
|
4
4
|
"description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"
|
|
8
|
-
"
|
|
7
|
+
"mm_expand": "^1.8.0",
|
|
8
|
+
"mm_logs": "^1.1.7"
|
|
9
9
|
},
|
|
10
10
|
"scripts": {
|
|
11
11
|
"test": "node test.js",
|
|
12
12
|
"dev": "nodemon test.js"
|
|
13
13
|
},
|
|
14
|
-
"author": "邱文武",
|
|
15
|
-
"license": "ISC",
|
|
16
14
|
"repository": {
|
|
17
15
|
"type": "git",
|
|
18
|
-
"url": "
|
|
16
|
+
"url": "https://gitee.com/qiuwenwu91/mm_mysql.git"
|
|
19
17
|
},
|
|
20
18
|
"keywords": [
|
|
21
19
|
"mysql",
|
|
@@ -30,8 +28,6 @@
|
|
|
30
28
|
"run",
|
|
31
29
|
"exec"
|
|
32
30
|
],
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"homepage": "https://github.com/qiuwenwu/mm_mysql#readme"
|
|
37
|
-
}
|
|
31
|
+
"author": "qww",
|
|
32
|
+
"license": "ISC"
|
|
33
|
+
}
|
package/test.js
CHANGED
|
@@ -13,13 +13,40 @@ async function test() {
|
|
|
13
13
|
password: "Asd159357",
|
|
14
14
|
multipleStatements: true
|
|
15
15
|
});
|
|
16
|
-
await sql.open();
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
// await sql.open();
|
|
17
|
+
|
|
18
|
+
// db = sql.db();
|
|
19
|
+
// db.table = 'user_account';
|
|
20
|
+
|
|
21
|
+
// var obj = await db.getObj();
|
|
22
|
+
// console.log(obj);
|
|
23
|
+
|
|
24
|
+
// // 导入
|
|
25
|
+
// var ret = await sql.load("./data.sql".fullname(__dirname), function(progress, index, error, sql_str) {
|
|
26
|
+
// console.log("导入", progress, index, error, sql_str);
|
|
27
|
+
// });
|
|
28
|
+
// console.log("导入结果", ret);
|
|
29
|
+
|
|
30
|
+
// 导出指定表'user_account', 'user_info'
|
|
31
|
+
// var ret2 = await sql.save("./bat.sql".fullname(__dirname), function(progress, index, error, sql_str) {
|
|
32
|
+
// console.log("导出", progress, index, error, sql_str);
|
|
33
|
+
// }, ['user_account', 'user_info']);
|
|
34
|
+
// console.log("导出结果", ret2);
|
|
35
|
+
|
|
36
|
+
// // 导出
|
|
37
|
+
// var ret2 = await sql.save("./bat.sql".fullname(__dirname), function(progress, index, error, sql_str) {
|
|
38
|
+
// // console.log("导出", progress, index, error, sql_str);
|
|
39
|
+
// });
|
|
40
|
+
// console.log("导出结果", ret2);
|
|
41
|
+
|
|
42
|
+
// // 导入
|
|
43
|
+
// var ret = await sql.load("./bat.sql".fullname(__dirname), function(progress, index, error, sql_str) {
|
|
44
|
+
// // console.log("导入", progress, index, error, sql_str);
|
|
45
|
+
// });
|
|
46
|
+
// console.log("导入结果", ret);
|
|
47
|
+
|
|
21
48
|
sql.close();
|
|
22
|
-
|
|
49
|
+
|
|
23
50
|
}
|
|
24
51
|
test();
|
|
25
52
|
|