mm_mysql 1.9.9 → 2.0.1
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 +15 -12
- package/package.json +1 -1
- package/test.js +29 -27
- package/upgrade.sql +34 -0
package/index.js
CHANGED
|
@@ -94,7 +94,7 @@ class Mysql {
|
|
|
94
94
|
var list = [];
|
|
95
95
|
try {
|
|
96
96
|
// 关于连接池初始化,请参阅上文
|
|
97
|
-
conn = await $this.
|
|
97
|
+
conn = await $this.getConn();
|
|
98
98
|
const [rows] = await conn.query(sql, val);
|
|
99
99
|
// 查询解析时,连接会自动释放
|
|
100
100
|
list = rows;
|
|
@@ -118,7 +118,7 @@ class Mysql {
|
|
|
118
118
|
*/
|
|
119
119
|
this.exec = async function(sql, val) {
|
|
120
120
|
if (this.task) {
|
|
121
|
-
this.task_sql += sql + "\
|
|
121
|
+
this.task_sql += sql + "\n";
|
|
122
122
|
if (this.task === 1) {
|
|
123
123
|
return this.task_sql;
|
|
124
124
|
} else if (this.task === 2) {
|
|
@@ -138,7 +138,7 @@ class Mysql {
|
|
|
138
138
|
var conn;
|
|
139
139
|
var bl = -1;
|
|
140
140
|
try {
|
|
141
|
-
conn = await $this.
|
|
141
|
+
conn = await $this.getConn();
|
|
142
142
|
var [rows] = await conn.execute(sql, val);
|
|
143
143
|
if (rows.constructor == Array) {
|
|
144
144
|
if (rows.length > 0) {
|
|
@@ -188,6 +188,9 @@ class Mysql {
|
|
|
188
188
|
Mysql.prototype.getConn = async function() {
|
|
189
189
|
let conn;
|
|
190
190
|
try {
|
|
191
|
+
if (!this.pool) {
|
|
192
|
+
await this.open();
|
|
193
|
+
}
|
|
191
194
|
conn = await this.pool.getConnection();
|
|
192
195
|
} catch (err) {
|
|
193
196
|
console.error('Connection failed - retrying: ' + err.message);
|
|
@@ -212,7 +215,7 @@ Mysql.prototype.load = async function(file, func) {
|
|
|
212
215
|
try {
|
|
213
216
|
var data = file.loadText();
|
|
214
217
|
// 将SQL文件内容分割成单独的语句
|
|
215
|
-
const arr = data.split(';\
|
|
218
|
+
const arr = data.replace(/\r\n/g, '\n').split(';\n');
|
|
216
219
|
count = arr.length;
|
|
217
220
|
for (var i = 0; i < arr.length; i++) {
|
|
218
221
|
var sql_str = arr[i].trim();
|
|
@@ -264,7 +267,7 @@ Mysql.prototype.save = async function(file, func, tables = []) {
|
|
|
264
267
|
|
|
265
268
|
try {
|
|
266
269
|
// 开始导出数据库
|
|
267
|
-
stream.write('SET FOREIGN_KEY_CHECKS = 0;\
|
|
270
|
+
stream.write('SET FOREIGN_KEY_CHECKS = 0;\n\n');
|
|
268
271
|
count++;
|
|
269
272
|
|
|
270
273
|
if (!tables.length) {
|
|
@@ -281,15 +284,15 @@ Mysql.prototype.save = async function(file, func, tables = []) {
|
|
|
281
284
|
try {
|
|
282
285
|
// 导出表结构
|
|
283
286
|
const createTable = await this.run(`SHOW CREATE TABLE ${tableName}`);
|
|
284
|
-
stream.write(`-- 表结构: ${tableName}\
|
|
285
|
-
stream.write(`DROP TABLE IF EXISTS \`${tableName}\`;\
|
|
287
|
+
stream.write(`-- 表结构: ${tableName}\n`);
|
|
288
|
+
stream.write(`DROP TABLE IF EXISTS \`${tableName}\`;\n`);
|
|
286
289
|
count++;
|
|
287
|
-
stream.write(`${createTable[0]['Create Table']};\
|
|
290
|
+
stream.write(`${createTable[0]['Create Table']};\n\n`);
|
|
288
291
|
count++;
|
|
289
292
|
// 导出表数据
|
|
290
293
|
const rows = await this.run(`SELECT * FROM ${tableName}`);
|
|
291
294
|
if (rows.length > 0) {
|
|
292
|
-
stream.write(`-- 表数据: ${tableName}\
|
|
295
|
+
stream.write(`-- 表数据: ${tableName}\n`);
|
|
293
296
|
for (const row of rows) {
|
|
294
297
|
const rowValues = Object.values(row)
|
|
295
298
|
.map(value => {
|
|
@@ -308,10 +311,10 @@ Mysql.prototype.save = async function(file, func, tables = []) {
|
|
|
308
311
|
.replace(/\u0000/g, '\\0') + "'";
|
|
309
312
|
})
|
|
310
313
|
.join(',');
|
|
311
|
-
stream.write(`INSERT INTO ${tableName} VALUES (${rowValues});\
|
|
314
|
+
stream.write(`INSERT INTO ${tableName} VALUES (${rowValues});\n`);
|
|
312
315
|
count++;
|
|
313
316
|
}
|
|
314
|
-
stream.write('\
|
|
317
|
+
stream.write('\n');
|
|
315
318
|
}
|
|
316
319
|
} catch (err) {
|
|
317
320
|
errors.push({
|
|
@@ -328,7 +331,7 @@ Mysql.prototype.save = async function(file, func, tables = []) {
|
|
|
328
331
|
}
|
|
329
332
|
|
|
330
333
|
// 完成导出
|
|
331
|
-
stream.write('SET FOREIGN_KEY_CHECKS = 1;\
|
|
334
|
+
stream.write('SET FOREIGN_KEY_CHECKS = 1;\n');
|
|
332
335
|
count++;
|
|
333
336
|
stream.end();
|
|
334
337
|
} catch (err) {
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -13,32 +13,34 @@ async function test() {
|
|
|
13
13
|
password: "Asd159357",
|
|
14
14
|
multipleStatements: true
|
|
15
15
|
});
|
|
16
|
-
await sql.open();
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
db
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
16
|
+
// await sql.open();
|
|
17
|
+
|
|
18
|
+
await sql.load("./upgrade.sql");
|
|
19
|
+
|
|
20
|
+
// db = sql.db();
|
|
21
|
+
// db.table = 'user_account';
|
|
22
|
+
// db.key = "user_id";
|
|
23
|
+
// var obj = await db.getObj({
|
|
24
|
+
// "user_id": "1"
|
|
25
|
+
// });
|
|
26
|
+
// console.log("查询对象", obj);
|
|
27
|
+
|
|
28
|
+
// var i = 0;
|
|
29
|
+
// var timer = setInterval(async () => {
|
|
30
|
+
// var now = new Date();
|
|
31
|
+
// var start = now.getTime();
|
|
32
|
+
// console.log(start);
|
|
33
|
+
// var bl = await db.set({
|
|
34
|
+
// "user_id": "1"
|
|
35
|
+
// }, {
|
|
36
|
+
// "nickname": "hao" + (i++)
|
|
37
|
+
// });
|
|
38
|
+
// now = new Date();
|
|
39
|
+
// var end = now.getTime();
|
|
40
|
+
// console.log(end);
|
|
41
|
+
// console.log(end - start);
|
|
42
|
+
// console.log("测试", bl);
|
|
43
|
+
// }, 3000);
|
|
42
44
|
|
|
43
45
|
// obj.nickname = "hao";
|
|
44
46
|
// console.log("查询对象", obj, db.error, db.sql);
|
|
@@ -67,7 +69,7 @@ async function test() {
|
|
|
67
69
|
// });
|
|
68
70
|
// console.log("导入结果", ret);
|
|
69
71
|
|
|
70
|
-
|
|
72
|
+
sql.close();
|
|
71
73
|
|
|
72
74
|
}
|
|
73
75
|
test();
|
package/upgrade.sql
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Navicat Premium Data Transfer
|
|
3
|
+
|
|
4
|
+
Source Server : 本地
|
|
5
|
+
Source Server Type : MySQL
|
|
6
|
+
Source Server Version : 50726
|
|
7
|
+
Source Host : localhost:3306
|
|
8
|
+
Source Schema : card
|
|
9
|
+
|
|
10
|
+
Target Server Type : MySQL
|
|
11
|
+
Target Server Version : 50726
|
|
12
|
+
File Encoding : 65001
|
|
13
|
+
|
|
14
|
+
Date: 08/04/2025 09:01:37
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
SET NAMES utf8mb4;
|
|
18
|
+
SET FOREIGN_KEY_CHECKS = 0;
|
|
19
|
+
|
|
20
|
+
-- ----------------------------
|
|
21
|
+
-- Table structure for test
|
|
22
|
+
-- ----------------------------
|
|
23
|
+
DROP TABLE IF EXISTS `test`;
|
|
24
|
+
CREATE TABLE `test` (
|
|
25
|
+
`test_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
26
|
+
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
|
|
27
|
+
PRIMARY KEY (`test_id`) USING BTREE
|
|
28
|
+
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
|
|
29
|
+
|
|
30
|
+
-- ----------------------------
|
|
31
|
+
-- Records of test
|
|
32
|
+
-- ----------------------------
|
|
33
|
+
|
|
34
|
+
SET FOREIGN_KEY_CHECKS = 1;
|