Browse Source

1.sqlbuilder新增对in的支持
2.helper新增拆解path

olym 7 years ago
parent
commit
e75795fe03
4 changed files with 87 additions and 4 deletions
  1. 30 0
      app/extend/helper.js
  2. 19 2
      app/lib/sql_builder.js
  3. 20 2
      test/app/extend/helper.test.js
  4. 18 0
      test/app/lib/sql_builder.test.js

+ 30 - 0
app/extend/helper.js

@@ -185,4 +185,34 @@ module.exports = {
         }
     },
 
+    /**
+     * 拆分path
+     *
+     * @param {String|Array} paths - 拆分字符
+     * @param {String} symbol - 拆分符号
+     * @return {Array} - 拆分结果
+     */
+    explodePath(paths, symbol = '.') {
+        const result = [];
+        paths = paths instanceof Array ? paths : [paths];
+        for (const path of paths) {
+            // 拆分数据
+            const pathArray = path.split(symbol);
+            // 用户缓存循环的数据
+            const tmpArray = [];
+            for (const tmp of pathArray) {
+                // 每次循环都追加一个数据进去
+                tmpArray.push(tmp);
+                const tmpPathString = tmpArray.join(symbol);
+                // 判断是否已经存在有对应数据
+                if (result.indexOf(tmpPathString) >= 0) {
+                    continue;
+                }
+                result.push(tmpPathString);
+            }
+        }
+
+        return result;
+    },
+
 };

+ 19 - 2
app/lib/sql_builder.js

@@ -123,8 +123,25 @@ class SqlBuilder {
 
         const whereArr = [];
         for (const where of this.andWhere) {
-            whereArr.push(' ?? ' + where.data.operate + ' ' + where.data.value);
-            this.sqlParam.push(where.field);
+            if (where.data.operate === 'in') {
+                // in操作
+                const valueLength = where.data.value instanceof Array ? where.data.value.length : 1;
+                // 生成参数集
+                const inArr = [];
+                for (let i = 0; i < valueLength; i++) {
+                    inArr.push('?');
+                }
+                const inData = inArr.join(',');
+                whereArr.push(' ?? IN (' + inData + ')');
+                this.sqlParam.push(where.field);
+                // 赋值参数
+                this.sqlParam.push.apply(this.sqlParam, where.data.value);
+            } else {
+                // 普通操作
+                whereArr.push(' ?? ' + where.data.operate + ' ' + where.data.value);
+                this.sqlParam.push(where.field);
+            }
+
         }
         const whereString = whereArr.join(' AND ');
         this.sql += this.sql.indexOf('WHERE') > 0 ? whereString : ' WHERE ' + whereString;

+ 20 - 2
test/app/extend/helper.test.js

@@ -30,8 +30,8 @@ describe('test/app/extend/helper.test.js', () => {
             group_id: { required: true, min: 4, max: 10 },
             password: { required: true, minlength: 4 },
             confirm_password: { required: true, minlength: 4, equalTo: '#password' },
-            ip: { type: 'ip', allowEmpty: false },
-            telephone: { type: 'mobile', allowEmpty: false },
+            ip: { ip: true, required: true },
+            telephone: { mobile: true, required: true },
         };
         expectRule = JSON.stringify(expectRule);
         assert(convertRule === expectRule);
@@ -80,4 +80,22 @@ describe('test/app/extend/helper.test.js', () => {
         assert(responseData.data.ip === '116.7.222.12');
     });
 
+    it('explode paths', function* () {
+        // 创建 ctx
+        const ctx = app.mockContext();
+        const paths = ['1.2.3.4.5', '1.2.3.5.7'];
+        const normal = ctx.helper.explodePath(paths, '.');
+        const normalExpect = ['1', '1.2', '1.2.3', '1.2.3.4', '1.2.3.4.5', '1.2.3.5', '1.2.3.5.7'];
+        let result = true;
+        try {
+            for (const index in normal) {
+                if (normal[index] !== normalExpect[index]) {
+                    throw '数据错误';
+                }
+            }
+        } catch (error) {
+            result = false;
+        }
+        assert(result);
+    });
 });

+ 18 - 0
test/app/lib/sql_builder.test.js

@@ -40,6 +40,24 @@ describe('test/app/lib/sql_builder.test.js', () => {
         assert(finalSql === matchSql);
     });
 
+    it('where in sql', function* () {
+        const sqlBuilder = new SqlBuilder();
+        sqlBuilder.columns = ['id'];
+        sqlBuilder.limit = 10;
+        sqlBuilder.offset = 1;
+        sqlBuilder.orderBy = [['id', 'DESC']];
+        sqlBuilder.setAndWhere('id', {
+            value: [1, 2, 3, 4],
+            operate: 'in',
+        });
+
+        const [sql, sqlParam] = sqlBuilder.build('table');
+        const finalSql = app.mysql.format(sql, sqlParam);
+
+        const matchSql = 'SELECT `id` FROM `table` WHERE  `id` IN (1,2,3,4) LIMIT 1,10 ORDER BY  `id` DESC';
+        assert(matchSql === finalSql);
+    });
+
     it('update sql', function* () {
         const sqlBuilder = new SqlBuilder();
         sqlBuilder.setUpdateData('create_time', {