|
@@ -33,6 +33,20 @@ class SqlBuilder {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 设置新的orWhere数据
|
|
|
+ *
|
|
|
+ * @param {String} field - where中的字段名称
|
|
|
+ * @param {Object} data - where中的value部分
|
|
|
+ * @return {void}
|
|
|
+ */
|
|
|
+ setNewOrWhere(data) {
|
|
|
+ if (Object.keys(data).length <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.newOrWhere.push(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 更新字段设置
|
|
|
*
|
|
|
* @param {String} field - set的字段
|
|
@@ -54,6 +68,7 @@ class SqlBuilder {
|
|
|
resetCondition() {
|
|
|
this.andWhere = [];
|
|
|
this.orWhere = [];
|
|
|
+ this.newOrWhere = [];
|
|
|
this.columns = [];
|
|
|
this.limit = -1;
|
|
|
this.offset = -1;
|
|
@@ -147,6 +162,9 @@ class SqlBuilder {
|
|
|
this.sqlParam.push(where.field);
|
|
|
// 赋值参数
|
|
|
this.sqlParam.push.apply(this.sqlParam, where.data.value);
|
|
|
+ } else if (where.data.operate === 'find_in_set') {
|
|
|
+ whereArr.push(' find_in_set (' + where.data.value + ', ?? )');
|
|
|
+ this.sqlParam.push(where.field);
|
|
|
} else {
|
|
|
// 普通操作
|
|
|
whereArr.push(' ?? ' + where.data.operate + ' ' + where.data.value);
|
|
@@ -179,6 +197,51 @@ class SqlBuilder {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * orWhere设置-旧的不好用,独立写了一个包含括号的or兼容,传对象数组,
|
|
|
+ * 例:
|
|
|
+ * [{ field: 'office_share', value: managerSession.office, operate: 'find_in_set'},
|
|
|
+ * {field: 'office',value: managerSession.office,operate: '='},......]
|
|
|
+ *
|
|
|
+ * @return {void}
|
|
|
+ */
|
|
|
+ _newOrWhereBuild() {
|
|
|
+ if (this.newOrWhere.length <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const whereArr = [];
|
|
|
+ for (const oneOw in this.newOrWhere) {
|
|
|
+ for (const where of this.newOrWhere[oneOw]) {
|
|
|
+ if (where.operate === 'in') {
|
|
|
+ // in操作
|
|
|
+ const valueLength = where.value instanceof Array ? where.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.value);
|
|
|
+ } else if (where.operate === 'find_in_set') {
|
|
|
+ whereArr.push(' find_in_set (' + where.value + ', ?? )');
|
|
|
+ this.sqlParam.push(where.field);
|
|
|
+ } else {
|
|
|
+ // 普通操作
|
|
|
+ whereArr.push(' ?? ' + where.operate + ' ' + where.value);
|
|
|
+ this.sqlParam.push(where.field);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let whereString = whereArr.join(' OR ');
|
|
|
+ whereString = '(' + whereString + ')';
|
|
|
+ // 如果前面已经有设置过WHERE则不需要再重复添加
|
|
|
+ this.sql += this.sql.indexOf('WHERE') > 0 ? ' AND ' + whereString : ' WHERE ' + whereString;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* limit设置
|
|
|
*
|
|
|
* @return {void}
|
|
@@ -230,6 +293,7 @@ class SqlBuilder {
|
|
|
this._setDataBuild();
|
|
|
this._andWhereBuild();
|
|
|
this._orWhereBuild();
|
|
|
+ this._newOrWhereBuild();
|
|
|
|
|
|
this._orderByBuild();
|
|
|
this._limitBuild();
|
|
@@ -242,5 +306,32 @@ class SqlBuilder {
|
|
|
return [sql, sqlParam];
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 构建sql
|
|
|
+ *
|
|
|
+ * @param {String} tableName - 表名
|
|
|
+ * @param {String} type - 类型
|
|
|
+ * @return {Array} - 返回数组,第一个元素为sql语句,第二个元素为sql中问号部分的param
|
|
|
+ */
|
|
|
+ buildCount(tableName, type = 'select') {
|
|
|
+ this.type = type;
|
|
|
+ this._typeBuild(tableName);
|
|
|
+ if (this.sql === '') {
|
|
|
+ throw '类型错误';
|
|
|
+ }
|
|
|
+
|
|
|
+ this._setDataBuild();
|
|
|
+ this._andWhereBuild();
|
|
|
+ this._orWhereBuild();
|
|
|
+ this._newOrWhereBuild();
|
|
|
+
|
|
|
+ const sql = this.sql;
|
|
|
+ const sqlParam = this.sqlParam;
|
|
|
+ // 重置数据
|
|
|
+ // this.resetCondition();
|
|
|
+
|
|
|
+ return [sql, sqlParam];
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
module.exports = SqlBuilder;
|