mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 17:18:24 +08:00
feat: add SqlOperator
This commit is contained in:
parent
a31d33011f
commit
35717f6160
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.mybatisflex.core.constant;
|
||||
|
||||
/**
|
||||
* @author michael
|
||||
*/
|
||||
public enum SqlOperator {
|
||||
|
||||
// >
|
||||
GT(SqlConsts.GT),
|
||||
|
||||
// >=
|
||||
GE(SqlConsts.GE),
|
||||
|
||||
// <
|
||||
LT(SqlConsts.LT),
|
||||
|
||||
// <=
|
||||
LE(SqlConsts.LE),
|
||||
|
||||
// like
|
||||
LIKE(SqlConsts.LIKE),
|
||||
|
||||
// =
|
||||
EQUALS(SqlConsts.EQUALS),
|
||||
|
||||
// is null
|
||||
IS_NULL(SqlConsts.IS_NULL),
|
||||
|
||||
// is not null
|
||||
IS_NOT_NULL(SqlConsts.IS_NOT_NULL),
|
||||
|
||||
// in
|
||||
IN(SqlConsts.IN),
|
||||
|
||||
// not in
|
||||
NOT_IN(SqlConsts.NOT_IN),
|
||||
|
||||
// between
|
||||
BETWEEN(SqlConsts.BETWEEN),
|
||||
|
||||
// not between
|
||||
NOT_BETWEEN(SqlConsts.NOT_BETWEEN),
|
||||
;
|
||||
|
||||
private final String value;
|
||||
|
||||
|
||||
SqlOperator(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@ -17,6 +17,7 @@ package com.mybatisflex.core.query;
|
||||
|
||||
import com.mybatisflex.core.FlexConsts;
|
||||
import com.mybatisflex.core.constant.SqlConsts;
|
||||
import com.mybatisflex.core.constant.SqlOperator;
|
||||
import com.mybatisflex.core.dialect.DialectFactory;
|
||||
import com.mybatisflex.core.table.TableDef;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
@ -167,7 +168,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
if (CollectionUtil.isEmpty(queryTables)) {
|
||||
throw new IllegalArgumentException("query table must not be empty.");
|
||||
}
|
||||
|
||||
queryTables.get(queryTables.size() - 1).alias = alias;
|
||||
return this;
|
||||
}
|
||||
@ -188,12 +188,14 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
}
|
||||
|
||||
public QueryWrapper where(Map<String, Object> whereConditions) {
|
||||
if (whereConditions != null) {
|
||||
whereConditions.forEach((s, o) -> and(QueryCondition.create(new QueryColumn(s), o)));
|
||||
}
|
||||
return this;
|
||||
return and(whereConditions);
|
||||
}
|
||||
|
||||
public QueryWrapper where(Map<String, Object> whereConditions, Map<String, SqlOperator> operators) {
|
||||
return and(whereConditions, operators);
|
||||
}
|
||||
|
||||
|
||||
public <T> QueryConditionBuilder where(LambdaGetter<T> fn) {
|
||||
return new QueryConditionBuilder(this, LambdaUtil.getQueryColumn(fn), SqlConnector.AND);
|
||||
}
|
||||
@ -226,6 +228,15 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper and(Map<String, Object> whereConditions) {
|
||||
return and(whereConditions, Collections.emptyMap());
|
||||
}
|
||||
|
||||
public QueryWrapper and(Map<String, Object> whereConditions, Map<String, SqlOperator> operators) {
|
||||
return connectMap(whereConditions, operators, true, true);
|
||||
}
|
||||
|
||||
public QueryWrapper or(QueryCondition queryCondition) {
|
||||
return addWhereQueryCondition(queryCondition, SqlConnector.OR);
|
||||
}
|
||||
@ -254,6 +265,45 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper or(Map<String, Object> whereConditions) {
|
||||
return or(whereConditions, Collections.emptyMap());
|
||||
}
|
||||
|
||||
public QueryWrapper or(Map<String, Object> whereConditions, Map<String, SqlOperator> operators) {
|
||||
return connectMap(whereConditions, operators, false, true);
|
||||
}
|
||||
|
||||
|
||||
private QueryWrapper connectMap(Map<String, Object> mapConditions, Map<String, SqlOperator> operators, boolean outerAnd, boolean innerAnd) {
|
||||
if (mapConditions != null) {
|
||||
QueryCondition condition = null;
|
||||
for (Map.Entry<String, Object> entry : mapConditions.entrySet()) {
|
||||
SqlOperator operator = operators.get(entry.getKey());
|
||||
if (operator == null) {
|
||||
operator = SqlOperator.EQUALS;
|
||||
}
|
||||
QueryCondition cond = QueryCondition.create(new QueryColumn(entry.getKey()), operator.getValue(), entry.getValue());
|
||||
if (condition == null) {
|
||||
condition = cond;
|
||||
} else {
|
||||
if (innerAnd) {
|
||||
condition.and(cond);
|
||||
} else {
|
||||
condition.or(cond);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (outerAnd) {
|
||||
and(condition);
|
||||
} else {
|
||||
or(condition);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public Joiner<QueryWrapper> leftJoin(String table) {
|
||||
return joining(SqlConsts.LEFT_JOIN, new QueryTable(table), true);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user