|
@@ -15,8 +15,8 @@ class SqlBuilder {
|
|
|
* @return {void}
|
|
|
*/
|
|
|
constructor() {
|
|
|
- this.andWhere = {};
|
|
|
- this.orWhere = {};
|
|
|
+ this.andWhere = [];
|
|
|
+ this.orWhere = [];
|
|
|
this.columns = [];
|
|
|
this.limit = -1;
|
|
|
this.offset = -1;
|
|
@@ -34,7 +34,7 @@ class SqlBuilder {
|
|
|
if (Object.keys(data).length <= 0) {
|
|
|
return;
|
|
|
}
|
|
|
- this.andWhere[field] = data;
|
|
|
+ this.andWhere.push({ field, data });
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -47,21 +47,21 @@ class SqlBuilder {
|
|
|
let sql = this.columns.length === 0 ? 'SELECT * FROM ??' : 'SELECT ?? FROM ??';
|
|
|
const sqlParam = this.columns.length === 0 ? [tableName] : [this.columns, tableName];
|
|
|
|
|
|
- if (Object.keys(this.andWhere).length > 0) {
|
|
|
+ if (this.andWhere.length > 0) {
|
|
|
const whereArr = [];
|
|
|
- for (const index in this.andWhere) {
|
|
|
- whereArr.push(' ?? ' + this.andWhere[index].operate + ' ' + this.andWhere[index].value);
|
|
|
- sqlParam.push(index);
|
|
|
+ for (const where of this.andWhere) {
|
|
|
+ whereArr.push(' ?? ' + where.data.operate + ' ' + where.data.value);
|
|
|
+ sqlParam.push(where.field);
|
|
|
}
|
|
|
const whereString = whereArr.join(' AND ');
|
|
|
sql += ' WHERE ' + whereString;
|
|
|
}
|
|
|
|
|
|
- if (Object.keys(this.orWhere).length > 0) {
|
|
|
+ if (this.orWhere.length > 0) {
|
|
|
const whereArr = [];
|
|
|
- for (const index in this.orWhere) {
|
|
|
- whereArr.push(' ?? ' + this.orWhere[index].operate + ' ' + this.orWhere[index].value);
|
|
|
- sqlParam.push(index);
|
|
|
+ for (const where in this.orWhere) {
|
|
|
+ whereArr.push(' ?? ' + where.data.operate + ' ' + where.data.value);
|
|
|
+ sqlParam.push(where.field);
|
|
|
}
|
|
|
const whereString = whereArr.join(' OR ');
|
|
|
// 如果前面已经有设置过WHERE则不需要再重复添加
|