mm_mysql 1.7.3 → 1.7.5

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 +44 -17
  3. package/test.js +9 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_mysql",
3
- "version": "1.7.3",
3
+ "version": "1.7.5",
4
4
  "description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/sql.js CHANGED
@@ -389,7 +389,6 @@ Sql.prototype.groupCountSql = async function(where, groupby, view, sort) {
389
389
  return await this.groupMathSql(where, groupby, view, sort, "COUNT");
390
390
  };
391
391
 
392
-
393
392
  /**
394
393
  * @description 统计学
395
394
  * @param {Object} query 查询条件
@@ -456,18 +455,21 @@ Sql.prototype.toWhere = function(obj, like) {
456
455
  if (val && typeof(val) === "string") {
457
456
  val = val.trim("'");
458
457
  }
458
+ val = escape(val);
459
459
  if (k.endWith('_min')) {
460
- where += " and " + escapeId(k.replace('_min', '')) + " >= " + escape(val);
460
+ where += " and " + escapeId(k.replace('_min', '')) + " >= " + val;
461
461
  } else if (k.endWith('_max')) {
462
- where += " and " + escapeId(k.replace('_max', '')) + " <= " + escape(val);
462
+ where += " and " + escapeId(k.replace('_max', '')) + " <= " + val;
463
463
  } else if (k.endWith('_not')) {
464
- where += " and " + escapeId(k.replace('_not', '')) + " != " + escape(val);
464
+ where += " and " + escapeId(k.replace('_not', '')) + " != " + val;
465
465
  } else if (k.endWith('_has')) {
466
- where += " and " + escapeId(k.replace('_has', '')) + " in (" + val + ")";
466
+ where += " and " + escapeId(k.replace('_has', '')) + " in (" + val.trim("'") + ")";
467
+ } else if (k.endWith('_like')) {
468
+ where += " and " + escapeId(k) + " LIKE '%" + val.trim("'") + "%'";
467
469
  } else if (typeof(val) === "string" && !/^[0-9]+$/.test(val)) {
468
- where += " and " + escapeId(k) + " LIKE '%" + escape(val).trim("'") + "%'"
470
+ where += " and " + escapeId(k) + " LIKE '%" + val.trim("'") + "%'";
469
471
  } else {
470
- where += " and " + escapeId(k) + " = " + val
472
+ where += " and " + escapeId(k) + " = " + val;
471
473
  }
472
474
  }
473
475
  } else {
@@ -476,16 +478,19 @@ Sql.prototype.toWhere = function(obj, like) {
476
478
  if (val && typeof(val) === "string") {
477
479
  val = val.trim("'");
478
480
  }
481
+ val = escape(val);
479
482
  if (k.endWith('_min')) {
480
- where += " and " + escapeId(k.replace('_min', '')) + " >= " + escape(val);
483
+ where += " and " + escapeId(k.replace('_min', '')) + " >= " + val;
481
484
  } else if (k.endWith('_max')) {
482
- where += " and " + escapeId(k.replace('_max', '')) + " <= " + escape(val);
485
+ where += " and " + escapeId(k.replace('_max', '')) + " <= " + val;
483
486
  } else if (k.endWith('_not')) {
484
- where += " and " + escapeId(k.replace('_not', '')) + " != " + escape(val);
487
+ where += " and " + escapeId(k.replace('_not', '')) + " != " + val;
485
488
  } else if (k.endWith('_has')) {
486
- where += " and " + escapeId(k.replace('_has', '')) + " in (" + val.replace(/`/gi, "") + ")";
489
+ where += " and " + escapeId(k.replace('_has', '')) + " in (" + val.trim("'") + ")";
490
+ } else if (k.endWith('_like')) {
491
+ where += " and " + escapeId(k) + " LIKE '%" + val.trim("'") + "%'";
487
492
  } else {
488
- where += " and " + escapeId(k) + "=" + escape(val);
493
+ where += " and " + escapeId(k) + " = " + val;
489
494
  }
490
495
  }
491
496
  }
@@ -525,13 +530,18 @@ Sql.prototype.toSet = function(obj) {
525
530
  */
526
531
  Sql.prototype.toAddSql = function(item) {
527
532
  var key = "";
528
- var val = "";
533
+ var value = "";
529
534
  for (var k in item) {
530
535
  key += "," + escapeId(k);
531
- val += "," + escape(item[k]);
536
+ var val = item[k];
537
+ if (val && typeof(val) == "string") {
538
+ val = val.trim("'");
539
+ }
540
+ value += "," + escape(val);
532
541
  }
533
542
  var sql = "INSERT INTO `{0}` ({1}) VALUES ({2});";
534
- return sql.replace("{0}", this.table).replace("{1}", key.replace(",", "")).replace("{2}", val.replace(",", ""));
543
+ return sql.replace("{0}", this.table).replace("{1}", key.replace(",", "")).replace("{2}", value.replace(",",
544
+ ""));
535
545
  };
536
546
 
537
547
  /**
@@ -764,11 +774,18 @@ Sql.prototype.tpl_query = function(paramDt, sqlDt) {
764
774
  var sl = "(";
765
775
  var len = arr.length;
766
776
  for (var i = 0; i < len; i++) {
767
- sl += " || " + tpl.replaceAll("{0}", escape(arr[i]).trim("'"));
777
+ var val = arr[i];
778
+ if (val && typeof(val) == "string") {
779
+ val = val.trim("'");
780
+ }
781
+ sl += " || " + tpl.replaceAll("{0}", escape(val).trim("'"));
768
782
  }
769
783
  sl = sl.replace(" || ", "") + ")";
770
784
  sql += " && " + sl;
771
785
  } else {
786
+ if (value && typeof(value) == "string") {
787
+ value = value.trim("'");
788
+ }
772
789
  sql += " && " + tpl.replaceAll("{0}", escape(value).trim("'"));
773
790
  }
774
791
  } else {
@@ -777,11 +794,18 @@ Sql.prototype.tpl_query = function(paramDt, sqlDt) {
777
794
  var sl = "(";
778
795
  var len = arr.length;
779
796
  for (var i = 0; i < len; i++) {
780
- sl += " || " + escapeId(key) + " = " + escape(arr[i]);
797
+ var val = arr[i];
798
+ if (val && typeof(val) == "string") {
799
+ val = val.trim("'");
800
+ }
801
+ sl += " || " + escapeId(key) + " = " + escape(val);
781
802
  }
782
803
  sl = sl.replace(" || ", "") + ")";
783
804
  sql += " && " + sl;
784
805
  } else {
806
+ if (value && typeof(value) == "string") {
807
+ value = value.trim("'");
808
+ }
785
809
  sql += " && " + escapeId(key) + " = " + escape(value);
786
810
  }
787
811
  }
@@ -789,6 +813,9 @@ Sql.prototype.tpl_query = function(paramDt, sqlDt) {
789
813
  } else {
790
814
  for (var key in paramDt) {
791
815
  var value = paramDt[key];
816
+ if (value && typeof(value) == "string") {
817
+ value = value.trim("'");
818
+ }
792
819
  value = escape(value);
793
820
  if (sqlDt[key]) {
794
821
  sql += " && " + sqlDt[key].replaceAll("{0}", value.trim("'"));
package/test.js CHANGED
@@ -81,7 +81,7 @@ async function test_tpl_get() {
81
81
  db.table = 'user_account';
82
82
  var query = {
83
83
  gm_min: 2,
84
- username: '\'ad%m'
84
+ username: 'ad%m|882'
85
85
  };
86
86
  db.page = 1;
87
87
  db.size = 5;
@@ -92,13 +92,16 @@ async function test_tpl_get() {
92
92
  console.log('SQL语句', db.sql);
93
93
 
94
94
  var o = ret.list[0];
95
- console.log("查询结果", o);
96
- o.user_id = await db.count() + 1;
97
- o.nickname = "'广东'小伙";
98
- // await db.add(o);
99
- await db.addOrSet({
95
+ // console.log("查询结果", o);
96
+ // // o.user_id = await db.count() + 1;
97
+ o.gm = "'5";
98
+ // // await db.add(o);
99
+ await db.set({
100
100
  user_id: o.user_id
101
101
  }, o);
102
+ // await db.addOrSet({
103
+ // user_id: o.user_id
104
+ // }, o);
102
105
  console.log(db.sql);
103
106
 
104
107
  ret = await db.groupSumSql(query_str, "mc", "gm");