mm_mysql 1.6.7 → 1.7.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/config.json +1 -1
- package/db.js +8 -1
- package/package.json +4 -2
- package/sql.js +67 -10
- package/test.js +79 -70
package/config.json
CHANGED
package/db.js
CHANGED
|
@@ -221,7 +221,14 @@ DB.prototype.setType = function(field, type, value, not_null, auto) {
|
|
|
221
221
|
type += " DEFAULT 0";
|
|
222
222
|
}
|
|
223
223
|
break;
|
|
224
|
+
case "longtext":
|
|
224
225
|
case "text":
|
|
226
|
+
if (type == "text") {
|
|
227
|
+
type = "text NULL"
|
|
228
|
+
}
|
|
229
|
+
else if (type == "longtext") {
|
|
230
|
+
type = "longtext NULL"
|
|
231
|
+
}
|
|
225
232
|
break;
|
|
226
233
|
default:
|
|
227
234
|
if (type.indexOf('var') !== -1) {
|
|
@@ -340,7 +347,7 @@ DB.prototype.field_set = async function(field, type, value, not_null, auto, isKe
|
|
|
340
347
|
|
|
341
348
|
var type = this.setType(field, type, value, not_null, auto);
|
|
342
349
|
if (type.has('text')) {
|
|
343
|
-
type = type.replace('NOT NULL', '');
|
|
350
|
+
type = type.replace('NOT NULL', '').replace("DEFAULT ''", "");
|
|
344
351
|
}
|
|
345
352
|
|
|
346
353
|
if (!new_name) {
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_mysql",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.1",
|
|
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",
|
|
@@ -30,6 +31,7 @@
|
|
|
30
31
|
},
|
|
31
32
|
"homepage": "https://github.com/qiuwenwu/mm_mysql#readme",
|
|
32
33
|
"dependencies": {
|
|
34
|
+
"mm_expand": "^1.5.1",
|
|
33
35
|
"mm_logs": "^1.0.7",
|
|
34
36
|
"mysql": "^2.17.1"
|
|
35
37
|
}
|
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) {
|
|
@@ -249,13 +258,13 @@ sql.open();
|
|
|
249
258
|
// }
|
|
250
259
|
|
|
251
260
|
// 测试获取字段
|
|
252
|
-
async function test3(){
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
}
|
|
258
|
-
test3();
|
|
261
|
+
// async function test3(){
|
|
262
|
+
// sql.config.log = true;
|
|
263
|
+
// var db = sql.db();
|
|
264
|
+
// var fields = await db.fields("cloud_user_flow");
|
|
265
|
+
// console.log(fields);
|
|
266
|
+
// }
|
|
267
|
+
// test3();
|
|
259
268
|
|
|
260
269
|
|
|
261
270
|
// 测试实例新对象获取
|