mm_mysql 1.8.0 → 1.8.2

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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/sql.js +30 -20
  3. package/test.js +52 -34
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_mysql",
3
- "version": "1.8.0",
3
+ "version": "1.8.2",
4
4
  "description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/sql.js CHANGED
@@ -250,6 +250,7 @@ Sql.prototype.getSql = function(where, sort, view) {
250
250
  * @description 添加或修改
251
251
  * @param {String} where 查询条件
252
252
  * @param {String} set 修改的键值
253
+ * @param {Boolean} like 是否使用like匹配, 为空使用默认方式
253
254
  * @return {Promise|Object} 执行结果
254
255
  */
255
256
  Sql.prototype.addOrSetSql = async function(where, set, like) {
@@ -257,7 +258,7 @@ Sql.prototype.addOrSetSql = async function(where, set, like) {
257
258
  return -1;
258
259
  }
259
260
  if (typeof(where) === "object") {
260
- where = await this.toWhere(where);
261
+ where = await this.toWhere(where, like);
261
262
  }
262
263
  var count = await this.countSql(where);
263
264
  if (count === 0) {
@@ -412,10 +413,11 @@ Sql.prototype.groupCountSql = async function(where, groupby, view, sort) {
412
413
  * @param {String} groupby 分组的字段
413
414
  * @param {String} view 返回的字段
414
415
  * @param {String} sort 排序方式
416
+ * @param {Boolean} like 是否使用like匹配, 为空使用默认方式
415
417
  * @return {Promise|Object} 查询到的内容列表和符合条件总数
416
418
  */
417
- Sql.prototype.groupMath = async function(query, groupby, view, sort, method) {
418
- var where = this.toWhere(query, this.like);
419
+ Sql.prototype.groupMath = async function(query, groupby, view, sort, method, like) {
420
+ var where = this.toWhere(query, like);
419
421
  return await this.groupMathSql(where, groupby, view, sort, method);
420
422
  };
421
423
 
@@ -458,7 +460,7 @@ Sql.prototype.groupCount = async function(query, groupby, view, sort) {
458
460
  /**
459
461
  * @description 转为where语句
460
462
  * @param {Object} obj 用作拼接的对象
461
- * @param {Boolean} like 是否使用like匹配, 默认不使用
463
+ * @param {Boolean} like 是否使用like匹配, 为空使用默认方式
462
464
  * @return {String} where格式sql语句字符串
463
465
  */
464
466
  Sql.prototype.toWhere = function(obj, like) {
@@ -480,7 +482,10 @@ Sql.prototype.toWhere = function(obj, like) {
480
482
  } else if (k.endWith('_not')) {
481
483
  where += " and " + escapeId(k.replace('_not', "")) + " != " + val;
482
484
  } else if (k.endWith('_has')) {
483
- where += " and " + escapeId(k.replace('_has', "")) + " in (" + val.trim("'") + ")";
485
+ var vals = val.trim("'").split(',').map((o) => {
486
+ return "'" + o + "'"
487
+ });
488
+ where += " and " + escapeId(k.replace('_has', "")) + " in (" + vals.join(',') + ")";
484
489
  } else if (k.endWith('_like')) {
485
490
  where += " and " + escapeId(k) + " LIKE '%" + val.trim("'") + "%'";
486
491
  } else if (typeof(val) === "string" && !/^[0-9]+$/.test(val)) {
@@ -503,7 +508,10 @@ Sql.prototype.toWhere = function(obj, like) {
503
508
  } else if (k.endWith('_not')) {
504
509
  where += " and " + escapeId(k.replace('_not', "")) + " != " + val;
505
510
  } else if (k.endWith('_has')) {
506
- where += " and " + escapeId(k.replace('_has', "")) + " in (" + val.trim("'") + ")";
511
+ var vals = val.trim("'").split(',').map((o) => {
512
+ return "'" + o + "'"
513
+ });
514
+ where += " and " + escapeId(k.replace('_has', "")) + " in (" + vals.join(',') + ")";
507
515
  } else if (k.endWith('_like')) {
508
516
  where += " and " + escapeId(k) + " LIKE '%" + val.trim("'") + "%'";
509
517
  } else {
@@ -564,10 +572,11 @@ Sql.prototype.toAddSql = function(item) {
564
572
  /**
565
573
  * @description 转删除sql语句
566
574
  * @param {Object} query 查询键值
575
+ * @param {Boolean} like 是否使用like匹配, 为空使用默认方式
567
576
  * @return {String} sql语句
568
577
  */
569
- Sql.prototype.toDelSql = function(query) {
570
- var where = this.toWhere(query);
578
+ Sql.prototype.toDelSql = function(query, like) {
579
+ var where = this.toWhere(query, like);
571
580
  var sql = "DELETE FROM `{0}` WHERE {1};";
572
581
  return sql.replace("{0}", this.table).replace("{1}", where);
573
582
  };
@@ -576,10 +585,11 @@ Sql.prototype.toDelSql = function(query) {
576
585
  * @description 转修改sql语句
577
586
  * @param {Object} query 查询的键值集合
578
587
  * @param {Object} item 修改的键值集合
588
+ * @param {Boolean} like 是否使用like匹配, 为空使用默认方式
579
589
  * @return {String} sql语句
580
590
  */
581
- Sql.prototype.toSetSql = function(query, item) {
582
- var where = this.toWhere(query);
591
+ Sql.prototype.toSetSql = function(query, item, like) {
592
+ var where = this.toWhere(query, like);
583
593
  var set = this.toSet(item);
584
594
  var sql = "UPDATE `{0}` SET {1} WHERE {2};";
585
595
  return sql.replace("{0}", this.table).replace("{1}", set).replace("{2}", where);
@@ -590,10 +600,10 @@ Sql.prototype.toSetSql = function(query, item) {
590
600
  * @param {Object} query 查询键值集合
591
601
  * @param {String} sort 排序规则
592
602
  * @param {String} view 显示的字段
593
- * @param {Boolean} like 是否使用like匹配, 默认使用
603
+ * @param {Boolean} like 是否使用like匹配, 为空使用默认方式
594
604
  * @return {String} sql语句
595
605
  */
596
- Sql.prototype.toGetSql = function(query, sort, view, like = true) {
606
+ Sql.prototype.toGetSql = function(query, sort, view, like) {
597
607
  var where = this.toWhere(query, like);
598
608
  var sql = this.toQuery(where, sort, view);
599
609
  return sql;
@@ -635,10 +645,10 @@ Sql.prototype.set = function(query, item) {
635
645
  * @param {Object} query 查询条件
636
646
  * @param {String} sort 排序
637
647
  * @param {String} view 返回的字段
638
- * @param {Boolean} like 是否使用like匹配, 默认使用
648
+ * @param {Boolean} like 是否使用like匹配, 为空使用默认方式
639
649
  * @return {Promise|Array} 查询结果
640
650
  */
641
- Sql.prototype.get = function(query, sort, view, like = true) {
651
+ Sql.prototype.get = function(query, sort, view, like) {
642
652
  var sql = this.toGetSql(query, sort, view, like);
643
653
  return this.run(sql);
644
654
  };
@@ -647,7 +657,7 @@ Sql.prototype.get = function(query, sort, view, like = true) {
647
657
  * @description 添加或修改
648
658
  * @param {Object} where 查询条件集合
649
659
  * @param {Object} set 修改的键值
650
- * @param {Boolean} like 是否使用like匹配, 默认不使用
660
+ * @param {Boolean} like 是否使用like匹配, 为空使用默认方式
651
661
  * @return {Promise|Object} 执行结果
652
662
  */
653
663
  Sql.prototype.addOrSet = async function(where, set, like) {
@@ -657,10 +667,10 @@ Sql.prototype.addOrSet = async function(where, set, like) {
657
667
  /**
658
668
  * @description 查询符合结果总数
659
669
  * @param {Object} query 查询条件集合
660
- * @param {Boolean} like 是否使用like匹配, 默认使用
670
+ * @param {Boolean} like 是否使用like匹配, 为空使用默认方式
661
671
  * @return {Promise|Number} 查询结果
662
672
  */
663
- Sql.prototype.count = function(query, like = true) {
673
+ Sql.prototype.count = function(query, like) {
664
674
  return this.countSql(this.toWhere(query, like));
665
675
  };
666
676
 
@@ -669,10 +679,10 @@ Sql.prototype.count = function(query, like = true) {
669
679
  * @param {Object} query 查询条件
670
680
  * @param {String} sort 排序
671
681
  * @param {String} view 返回的字段
672
- * @param {Boolean} like 是否使用like匹配, 默认使用
682
+ * @param {Boolean} like 是否使用like匹配, 为空使用默认方式
673
683
  * @return {Promise|Object} 查询到的内容列表和符合条件总数
674
684
  */
675
- Sql.prototype.getCount = async function(query, sort, view, like = true) {
685
+ Sql.prototype.getCount = async function(query, sort, view, like) {
676
686
  return this.getCountSql(this.toWhere(query, like), sort, view);
677
687
  };
678
688
 
@@ -938,7 +948,7 @@ Sql.prototype.model = function(model) {
938
948
  * @param {Object} query 查询条件
939
949
  * @param {String} sort 排序
940
950
  * @param {String} view 返回的字段
941
- * @param {Boolean} like 是否like匹配
951
+ * @param {Boolean} like 是否使用like匹配, 为空使用默认方式
942
952
  * @return {Promise|Array} 查询结果
943
953
  */
944
954
  Sql.prototype.getObj = async function(query, sort, view, like) {
package/test.js CHANGED
@@ -76,47 +76,65 @@ var tpl = {
76
76
  // }
77
77
  // test_tpl_set();
78
78
 
79
- // 测试模板查询
80
- async function test_tpl_get() {
79
+ async function test() {
81
80
  var sql = new Mysql();
81
+ var db = sql.db();
82
82
  sql.setConfig({
83
83
  "user": "root",
84
84
  "password": "Asd159357"
85
85
  });
86
86
  sql.open();
87
- db = sql.db();
88
- var num = 0;
89
- db.table = 'user_account';
90
- var query = {
91
- gm_min: 2,
92
- username: 'ad%m|882'
93
- };
94
- db.page = 1;
95
- db.size = 5;
96
- var query_str = db.tpl_query(query, tpl.query);
97
- ret = await db.getCountSql(query_str, "`user_id` desc", "*");
98
-
99
- console.log('查询结果', ret);
100
- console.log('SQL语句', db.sql);
101
-
102
- var o = ret.list[0];
103
- // console.log("查询结果", o);
104
- o.user_id = await db.count() + 1;
105
- o.nickname = o.nickname + ",6,7";
106
- o.gm = "'5";
107
- // // await db.add(o);
108
- // await db.set({
109
- // user_id: o.user_id
110
- // }, o);
111
- await db.addOrSet({
112
- user_id: o.user_id
113
- }, o);
114
- // console.log(db.sql);
115
-
116
- ret = await db.groupSumSql(query_str, "mc", "gm");
117
- console.log('求和查询结果', ret);
87
+
88
+ db.table = "user_account";
89
+ var list = await db.get({
90
+ user_id_has: "1,2,a-3,4,5"
91
+ });
92
+
93
+ console.log("语法", db.sql, list.length);
118
94
  }
119
- test_tpl_get();
95
+ test();
96
+
97
+ // 测试模板查询
98
+ // async function test_tpl_get() {
99
+ // var sql = new Mysql();
100
+ // sql.setConfig({
101
+ // "user": "root",
102
+ // "password": "Asd159357"
103
+ // });
104
+ // sql.open();
105
+ // db = sql.db();
106
+ // var num = 0;
107
+ // db.table = 'user_account';
108
+ // var query = {
109
+ // gm_min: 2,
110
+ // username: 'ad%m|882'
111
+ // };
112
+ // db.page = 1;
113
+ // db.size = 5;
114
+ // var query_str = db.tpl_query(query, tpl.query);
115
+ // ret = await db.getCountSql(query_str, "`user_id` desc", "*");
116
+
117
+ // console.log('查询结果', ret);
118
+ // console.log('SQL语句', db.sql);
119
+
120
+ // var o = ret.list[0];
121
+ // // console.log("查询结果", o);
122
+ // o.user_id = await db.count() + 1;
123
+ // o.nickname = o.nickname + ",6,7";
124
+ // o.gm = "'5";
125
+ // // // await db.add(o);
126
+ // // await db.set({
127
+ // // user_id: o.user_id
128
+ // // }, o);
129
+ // await db.addOrSet({
130
+ // user_id: o.user_id
131
+ // }, o);
132
+ // // console.log(db.sql);
133
+
134
+ // ret = await db.groupSumSql(query_str, "mc", "gm");
135
+ // console.log('求和查询结果', ret);
136
+ // }
137
+ // test_tpl_get();
120
138
 
121
139
  // async function addField() {
122
140
  // var sql = new Mysql();