mm_mysql 1.6.6 → 1.7.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/config.json +1 -1
- package/db.js +30 -19
- package/package.json +3 -2
- package/sql.js +67 -10
- package/test.js +72 -63
package/config.json
CHANGED
package/db.js
CHANGED
|
@@ -11,18 +11,30 @@ class DB extends Sql {
|
|
|
11
11
|
*/
|
|
12
12
|
constructor(mysql) {
|
|
13
13
|
super(mysql.run, mysql.exec);
|
|
14
|
-
|
|
15
|
-
// 数据库名
|
|
16
|
-
this.mysql = mysql;
|
|
17
|
-
|
|
18
14
|
// 事务中
|
|
19
15
|
this.task = 0;
|
|
20
16
|
|
|
21
17
|
// 事务SQL
|
|
22
18
|
this.task_sql = "";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 获取上级
|
|
22
|
+
* @return {Object} 返回上级管理器
|
|
23
|
+
*/
|
|
24
|
+
this.parent = function() {
|
|
25
|
+
return mysql;
|
|
26
|
+
};
|
|
23
27
|
}
|
|
24
28
|
}
|
|
25
29
|
|
|
30
|
+
/**
|
|
31
|
+
* 获取数据库名
|
|
32
|
+
* @return {String} 数据库
|
|
33
|
+
*/
|
|
34
|
+
DB.prototype.database = function() {
|
|
35
|
+
return this.parent().config.database;
|
|
36
|
+
};
|
|
37
|
+
|
|
26
38
|
/**
|
|
27
39
|
* 构建新管理器
|
|
28
40
|
* @param {String} table 表名
|
|
@@ -30,7 +42,7 @@ class DB extends Sql {
|
|
|
30
42
|
* @return {Object} 返回管理器
|
|
31
43
|
*/
|
|
32
44
|
DB.prototype.new = function(table, key) {
|
|
33
|
-
var db = this.
|
|
45
|
+
var db = this.parent().db();
|
|
34
46
|
db.table = table;
|
|
35
47
|
if (key) {
|
|
36
48
|
db.key = key;
|
|
@@ -41,14 +53,6 @@ DB.prototype.new = function(table, key) {
|
|
|
41
53
|
return db;
|
|
42
54
|
};
|
|
43
55
|
|
|
44
|
-
/**
|
|
45
|
-
* 获取上级
|
|
46
|
-
* @return {Object} 返回上级管理器
|
|
47
|
-
*/
|
|
48
|
-
DB.prototype.parent = function() {
|
|
49
|
-
return this.mysql;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
56
|
/**
|
|
53
57
|
* 事务开始
|
|
54
58
|
*/
|
|
@@ -88,7 +92,7 @@ DB.prototype.back = async function(identifier = "point_1") {
|
|
|
88
92
|
*/
|
|
89
93
|
DB.prototype.tables = async function(table) {
|
|
90
94
|
var list = await this.run("show tables");
|
|
91
|
-
var key = 'Tables_in_' + this.
|
|
95
|
+
var key = 'Tables_in_' + this.database();
|
|
92
96
|
if (table) {
|
|
93
97
|
list = list.search(table, key);
|
|
94
98
|
}
|
|
@@ -107,7 +111,7 @@ DB.prototype.fields = async function(table, field_name) {
|
|
|
107
111
|
var field =
|
|
108
112
|
'COLUMN_NAME as `name`,ORDINAL_POSITION as `cid`,COLUMN_DEFAULT as `dflt_value`,IS_NULLABLE as `notnull`,COLUMN_TYPE as `type`,COLUMN_KEY as `pk`,EXTRA as `auto`,COLUMN_COMMENT as `note`';
|
|
109
113
|
var sql = "select " + field + " from information_schema.COLUMNS where `table_name` = '" + table +
|
|
110
|
-
"' and `table_schema` = '" + this.
|
|
114
|
+
"' and `table_schema` = '" + this.database() + "'";
|
|
111
115
|
if (field_name) {
|
|
112
116
|
sql += " AND COLUMN_NAME='" + field_name + "'";
|
|
113
117
|
}
|
|
@@ -217,7 +221,14 @@ DB.prototype.setType = function(field, type, value, not_null, auto) {
|
|
|
217
221
|
type += " DEFAULT 0";
|
|
218
222
|
}
|
|
219
223
|
break;
|
|
224
|
+
case "longtext":
|
|
220
225
|
case "text":
|
|
226
|
+
if (type == "text") {
|
|
227
|
+
type = "text NULL"
|
|
228
|
+
}
|
|
229
|
+
else if (type == "longtext") {
|
|
230
|
+
type = "longtext NULL"
|
|
231
|
+
}
|
|
221
232
|
break;
|
|
222
233
|
default:
|
|
223
234
|
if (type.indexOf('var') !== -1) {
|
|
@@ -294,7 +305,7 @@ DB.prototype.clearTable = async function(reset = true, table = '') {
|
|
|
294
305
|
DB.prototype.field_add = async function(field, type, value, not_null, auto, isKey) {
|
|
295
306
|
var sql =
|
|
296
307
|
"SELECT COUNT(*) as `count` FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='{0}' AND table_name='{1}' AND COLUMN_NAME='{2}'";
|
|
297
|
-
sql = sql.replace('{0}', this.
|
|
308
|
+
sql = sql.replace('{0}', this.database()).replace('{1}', this.table).replace('{2}', field);
|
|
298
309
|
var arr = await this.run(sql);
|
|
299
310
|
if (arr && arr.length > 0) {
|
|
300
311
|
if (arr[0].count == 0) {
|
|
@@ -326,7 +337,7 @@ DB.prototype.field_add = async function(field, type, value, not_null, auto, isKe
|
|
|
326
337
|
DB.prototype.field_set = async function(field, type, value, not_null, auto, isKey, new_name) {
|
|
327
338
|
var sql =
|
|
328
339
|
"SELECT COUNT(*) as `count` FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='{0}' AND table_name='{1}' AND COLUMN_NAME='{2}'";
|
|
329
|
-
sql = sql.replace('{0}', this.
|
|
340
|
+
sql = sql.replace('{0}', this.database()).replace('{1}', this.table).replace('{2}', field);
|
|
330
341
|
var arr = await this.run(sql);
|
|
331
342
|
if (arr && arr.length > 0) {
|
|
332
343
|
if (arr[0].count == 0) {
|
|
@@ -336,7 +347,7 @@ DB.prototype.field_set = async function(field, type, value, not_null, auto, isKe
|
|
|
336
347
|
|
|
337
348
|
var type = this.setType(field, type, value, not_null, auto);
|
|
338
349
|
if (type.has('text')) {
|
|
339
|
-
type = type.replace('NOT NULL', '');
|
|
350
|
+
type = type.replace('NOT NULL', '').replace("DEFAULT ''", "");
|
|
340
351
|
}
|
|
341
352
|
|
|
342
353
|
if (!new_name) {
|
|
@@ -361,7 +372,7 @@ DB.prototype.field_set = async function(field, type, value, not_null, auto, isKe
|
|
|
361
372
|
DB.prototype.field_del = async function(field) {
|
|
362
373
|
var sql =
|
|
363
374
|
"SELECT COUNT(*) as `count` FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='{0}' AND table_name='{1}' AND COLUMN_NAME='{2}'";
|
|
364
|
-
sql = sql.replace('{0}', this.
|
|
375
|
+
sql = sql.replace('{0}', this.database()).replace('{1}', this.table).replace('{2}', field);
|
|
365
376
|
var arr = await this.run(sql);
|
|
366
377
|
if (arr && arr.length > 0) {
|
|
367
378
|
if (arr[0].count > 0) {
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_mysql",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "node test.js"
|
|
7
|
+
"test": "node test.js",
|
|
8
|
+
"dev": "nodemon test.js"
|
|
8
9
|
},
|
|
9
10
|
"repository": {
|
|
10
11
|
"type": "git",
|
package/sql.js
CHANGED
|
@@ -138,6 +138,8 @@ class Sql {
|
|
|
138
138
|
"count_ret": "count_ret"
|
|
139
139
|
}
|
|
140
140
|
};
|
|
141
|
+
|
|
142
|
+
this.like = false;
|
|
141
143
|
}
|
|
142
144
|
}
|
|
143
145
|
|
|
@@ -318,8 +320,7 @@ Sql.prototype.getCountSql = async function(where, sort, view) {
|
|
|
318
320
|
* @param {String} sort 排序方式
|
|
319
321
|
* @return {Promise|Object} 查询到的内容列表和符合条件总数
|
|
320
322
|
*/
|
|
321
|
-
Sql.prototype.
|
|
322
|
-
var where = this.toWhere(where, false);
|
|
323
|
+
Sql.prototype.groupMathSql = async function(where, groupby, view, sort, method) {
|
|
323
324
|
if (!view) {
|
|
324
325
|
view = "*"
|
|
325
326
|
}
|
|
@@ -350,7 +351,8 @@ Sql.prototype.groupMath = async function(where, groupby, view, sort, method) {
|
|
|
350
351
|
sql += " limit " + start + ',' + this.size;
|
|
351
352
|
}
|
|
352
353
|
return await this.run(sql);
|
|
353
|
-
}
|
|
354
|
+
}
|
|
355
|
+
|
|
354
356
|
|
|
355
357
|
/**
|
|
356
358
|
* @description 分组求平均值
|
|
@@ -360,8 +362,8 @@ Sql.prototype.groupMath = async function(where, groupby, view, sort, method) {
|
|
|
360
362
|
* @param {String} sort 排序方式
|
|
361
363
|
* @return {Promise|Object} 查询到的内容列表和符合条件总数
|
|
362
364
|
*/
|
|
363
|
-
Sql.prototype.
|
|
364
|
-
return await this.
|
|
365
|
+
Sql.prototype.groupAvgSql = async function(where, groupby, view, sort) {
|
|
366
|
+
return await this.groupMathSql(where, groupby, view, sort, "AVG");
|
|
365
367
|
};
|
|
366
368
|
|
|
367
369
|
/**
|
|
@@ -372,8 +374,8 @@ Sql.prototype.groupAvg = async function(where, groupby, view, sort) {
|
|
|
372
374
|
* @param {String} sort 排序方式
|
|
373
375
|
* @return {Promise|Object} 查询到的内容列表和符合条件总数
|
|
374
376
|
*/
|
|
375
|
-
Sql.prototype.
|
|
376
|
-
return await this.
|
|
377
|
+
Sql.prototype.groupSumSql = async function(where, groupby, view, sort) {
|
|
378
|
+
return await this.groupMathSql(where, groupby, view, sort, "SUM");
|
|
377
379
|
};
|
|
378
380
|
|
|
379
381
|
/**
|
|
@@ -383,8 +385,57 @@ Sql.prototype.groupSum = async function(where, groupby, view, sort) {
|
|
|
383
385
|
* @param {String} view 返回的字段
|
|
384
386
|
* @return {Promise|Object} 查询到的内容列表和符合条件总数
|
|
385
387
|
*/
|
|
386
|
-
Sql.prototype.
|
|
387
|
-
return await this.
|
|
388
|
+
Sql.prototype.groupCountSql = async function(where, groupby, view, sort) {
|
|
389
|
+
return await this.groupMathSql(where, groupby, view, sort, "COUNT");
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* @description 统计学
|
|
395
|
+
* @param {Object} query 查询条件
|
|
396
|
+
* @param {String} groupby 分组的字段
|
|
397
|
+
* @param {String} view 返回的字段
|
|
398
|
+
* @param {String} sort 排序方式
|
|
399
|
+
* @return {Promise|Object} 查询到的内容列表和符合条件总数
|
|
400
|
+
*/
|
|
401
|
+
Sql.prototype.groupMath = async function(query, groupby, view, sort, method) {
|
|
402
|
+
var where = this.toWhere(query, this.like);
|
|
403
|
+
return await this.groupMathSql(where, groupby, view, sort, method);
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* @description 分组求平均值
|
|
408
|
+
* @param {Object} query 查询条件
|
|
409
|
+
* @param {String} groupby 分组的字段
|
|
410
|
+
* @param {String} view 返回的字段
|
|
411
|
+
* @param {String} sort 排序方式
|
|
412
|
+
* @return {Promise|Object} 查询到的内容列表和符合条件总数
|
|
413
|
+
*/
|
|
414
|
+
Sql.prototype.groupAvg = async function(query, groupby, view, sort) {
|
|
415
|
+
return await this.groupMath(query, groupby, view, sort, "AVG");
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* @description 分组合计数值
|
|
420
|
+
* @param {Object} query 查询条件
|
|
421
|
+
* @param {String} groupby 分组的字段
|
|
422
|
+
* @param {String} view 返回的字段
|
|
423
|
+
* @param {String} sort 排序方式
|
|
424
|
+
* @return {Promise|Object} 查询到的内容列表和符合条件总数
|
|
425
|
+
*/
|
|
426
|
+
Sql.prototype.groupSum = async function(query, groupby, view, sort) {
|
|
427
|
+
return await this.groupMath(query, groupby, view, sort, "SUM");
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* @description 分组合计不同条数
|
|
432
|
+
* @param {Object} query 查询条件
|
|
433
|
+
* @param {String} groupby 分组的字段
|
|
434
|
+
* @param {String} view 返回的字段
|
|
435
|
+
* @return {Promise|Object} 查询到的内容列表和符合条件总数
|
|
436
|
+
*/
|
|
437
|
+
Sql.prototype.groupCount = async function(query, groupby, view, sort) {
|
|
438
|
+
return await this.groupMath(query, groupby, view, sort, "COUNT");
|
|
388
439
|
};
|
|
389
440
|
|
|
390
441
|
/* === sql语句拼接函数 === */
|
|
@@ -396,6 +447,9 @@ Sql.prototype.groupCount = async function(where, groupby, view, sort) {
|
|
|
396
447
|
*/
|
|
397
448
|
Sql.prototype.toWhere = function(obj, like) {
|
|
398
449
|
var where = "";
|
|
450
|
+
if (like === undefined) {
|
|
451
|
+
like = this.like;
|
|
452
|
+
}
|
|
399
453
|
if (like) {
|
|
400
454
|
for (var k in obj) {
|
|
401
455
|
var val = obj[k];
|
|
@@ -832,7 +886,7 @@ Sql.prototype.model = function(model) {
|
|
|
832
886
|
* @param {Boolean} like 是否like匹配
|
|
833
887
|
* @return {Promise|Array} 查询结果
|
|
834
888
|
*/
|
|
835
|
-
Sql.prototype.getObj = async function(query, sort, view, like
|
|
889
|
+
Sql.prototype.getObj = async function(query, sort, view, like) {
|
|
836
890
|
this.page = 1;
|
|
837
891
|
this.size = 1;
|
|
838
892
|
var key = this.key;
|
|
@@ -841,6 +895,9 @@ Sql.prototype.getObj = async function(query, sort, view, like = false) {
|
|
|
841
895
|
view += "," + this.escapeId(key);
|
|
842
896
|
}
|
|
843
897
|
}
|
|
898
|
+
if (like === undefined) {
|
|
899
|
+
like = this.like;
|
|
900
|
+
}
|
|
844
901
|
var sql = this.toGetSql(query, sort, view, like);
|
|
845
902
|
var list = await this.run(sql);
|
|
846
903
|
if (list.length > 0) {
|
package/test.js
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
1
|
const Mysql = require('./index.js').Mysql;
|
|
2
2
|
|
|
3
|
-
var sql = new Mysql();
|
|
4
|
-
sql.setConfig({
|
|
5
|
-
|
|
6
|
-
});
|
|
7
|
-
sql.open();
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
3
|
+
// var sql = new Mysql();
|
|
4
|
+
// sql.setConfig({
|
|
5
|
+
// multipleStatements: true
|
|
6
|
+
// });
|
|
7
|
+
// sql.open();
|
|
8
|
+
|
|
9
|
+
var tpl = {
|
|
10
|
+
"query": {
|
|
11
|
+
"state_min": "`state` >= '{0}'",
|
|
12
|
+
"state_max": "`state` <= '{0}'",
|
|
13
|
+
"vip_min": "`vip` >= '{0}'",
|
|
14
|
+
"vip_max": "`vip` <= '{0}'",
|
|
15
|
+
"gm_min": "`gm` >= '{0}'",
|
|
16
|
+
"gm_max": "`gm` <= '{0}'",
|
|
17
|
+
"mc_min": "`mc` >= '{0}'",
|
|
18
|
+
"mc_max": "`mc` <= '{0}'",
|
|
19
|
+
"create_time_min": "`create_time` >= '{0}'",
|
|
20
|
+
"create_time_max": "`create_time` <= '{0}'",
|
|
21
|
+
"login_time_min": "`login_time` >= '{0}'",
|
|
22
|
+
"login_time_max": "`login_time` <= '{0}'",
|
|
23
|
+
"salt": "`salt` like '%{0}%'",
|
|
24
|
+
"invite_code": "`invite_code` like '%{0}%'",
|
|
25
|
+
"phone": "`phone` like '%{0}%'",
|
|
26
|
+
"username": "`username` like '%{0}%'",
|
|
27
|
+
"nickname": "`nickname` like '%{0}%'",
|
|
28
|
+
"password": "`password` like '%{0}%'",
|
|
29
|
+
"email": "`email` like '%{0}%'",
|
|
30
|
+
"user_group": "`user_group` like '%{0}%'",
|
|
31
|
+
"user_admin": "`user_admin` like '%{0}%'",
|
|
32
|
+
"login_ip": "`login_ip` like '%{0}%'",
|
|
33
|
+
"signature": "`signature` like '%{0}%'",
|
|
34
|
+
"avatar": "`avatar` like '%{0}%'",
|
|
35
|
+
"friends": "`friends` like '%{0}%'",
|
|
36
|
+
"admin_group": "`admin_group` like '%{0}%'"
|
|
37
|
+
},
|
|
38
|
+
"update": {
|
|
39
|
+
"state_add": "`state` = `state` + '{0}'",
|
|
40
|
+
"vip_add": "`vip` = `vip` + '{0}'",
|
|
41
|
+
"gm_add": "`gm` = `gm` + '{0}'",
|
|
42
|
+
"mc_add": "`mc` = `mc` + '{0}'"
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
45
|
|
|
46
46
|
// // 测试模板修改
|
|
47
47
|
// async function test_tpl_set() {
|
|
@@ -68,25 +68,28 @@ sql.open();
|
|
|
68
68
|
// }
|
|
69
69
|
// test_tpl_set();
|
|
70
70
|
|
|
71
|
-
//
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
71
|
+
// 测试模板查询
|
|
72
|
+
async function test_tpl_get() {
|
|
73
|
+
var sql = new Mysql();
|
|
74
|
+
sql.open();
|
|
75
|
+
db = sql.db();
|
|
76
|
+
var num = 0;
|
|
77
|
+
db.table = 'user_account';
|
|
78
|
+
var query = {
|
|
79
|
+
gm_min: 2,
|
|
80
|
+
username: 'ad%m'
|
|
81
|
+
};
|
|
82
|
+
db.page = 1;
|
|
83
|
+
db.size = 5;
|
|
84
|
+
var query_str = db.tpl_query(query, tpl.query);
|
|
85
|
+
ret = await db.getCountSql(query_str, "`user_id` desc", "*");
|
|
86
|
+
console.log('查询结果', ret);
|
|
87
|
+
console.log('SQL语句', db.sql);
|
|
88
|
+
|
|
89
|
+
ret = await db.groupSumSql(query_str, "mc", "gm");
|
|
90
|
+
console.log('求和查询结果', ret);
|
|
91
|
+
}
|
|
92
|
+
test_tpl_get();
|
|
90
93
|
|
|
91
94
|
// async function addField() {
|
|
92
95
|
// var sql = new Mysql();
|
|
@@ -167,12 +170,18 @@ sql.open();
|
|
|
167
170
|
// console.log("SQL:" + db.sql);
|
|
168
171
|
// }
|
|
169
172
|
|
|
170
|
-
// num = await db.field_add("note", 'text',
|
|
173
|
+
// num = await db.field_add("note", 'text', '123123', false);
|
|
171
174
|
// console.log("note结果:" + num);
|
|
172
175
|
// if (num < 0) {
|
|
173
176
|
// console.log("SQL:" + db.sql);
|
|
174
177
|
// }
|
|
175
|
-
|
|
178
|
+
|
|
179
|
+
// num = await db.field_add("content", 'longtext', '123123', false);
|
|
180
|
+
// console.log("content结果:" + num);
|
|
181
|
+
// if (num < 0) {
|
|
182
|
+
// console.log("SQL:" + db.sql);
|
|
183
|
+
// }
|
|
184
|
+
|
|
176
185
|
// num = await db.field_add("cycle", 'time', null, true);
|
|
177
186
|
// console.log("cycle结果:" + num);
|
|
178
187
|
// if (num < 0) {
|