Jelajahi Sumber

sql拼接相关

MaiXinRong 5 tahun lalu
induk
melakukan
c0be8c4511
2 mengubah file dengan 15 tambahan dan 5 penghapusan
  1. 8 5
      app/extend/helper.js
  2. 7 0
      test/app/extend/helper.test.js

+ 8 - 5
app/extend/helper.js

@@ -687,10 +687,13 @@ module.exports = {
      * @returns {*}
      */
     getInArrStrSqlFilter(arr) {
-        const self = this;
-        const arrEs = _.forIn(arr, function (a) {
-            return self.ctx.app.mysql.escape(a);
-        });
-        return arrEs.join(',');
+        let result = '';
+        for (const a of arr) {
+            if (result !== '') {
+                result = result + ','
+            }
+            result = result + this.ctx.app.mysql.escape(a);
+        }
+        return result;
     }
 };

+ 7 - 0
test/app/extend/helper.test.js

@@ -133,4 +133,11 @@ describe('test/app/extend/helper.test.js', () => {
         assert(ctx.helper.mul(0.07, 10) === 0.7);
         assert(ctx.helper.div(0.32, 0.2) === 1.6);
     });
+
+    it('test getInArrStrSqlFilter', function () {
+        const ctx = app.mockContext();
+        const id = ['abc', 'efj'];
+        const str = 'And id in (' + ctx.helper.getInArrStrSqlFilter(id) + ')';
+        assert(str === "And id in ('abc','efj')");
+    })
 });