From 35717f6160cacb912ef093b227182bfb8a8f1746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Fri, 21 Jul 2023 20:09:40 +0800 Subject: [PATCH] feat: add SqlOperator --- .../core/constant/SqlOperator.java | 70 +++++++++++++++++++ .../mybatisflex/core/query/QueryWrapper.java | 60 ++++++++++++++-- 2 files changed, 125 insertions(+), 5 deletions(-) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlOperator.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlOperator.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlOperator.java new file mode 100644 index 00000000..fb114f65 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlOperator.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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; + } +} diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java index c7feebb5..af92075a 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java @@ -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 { 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 { } public QueryWrapper where(Map whereConditions) { - if (whereConditions != null) { - whereConditions.forEach((s, o) -> and(QueryCondition.create(new QueryColumn(s), o))); - } - return this; + return and(whereConditions); } + public QueryWrapper where(Map whereConditions, Map operators) { + return and(whereConditions, operators); + } + + public QueryConditionBuilder where(LambdaGetter fn) { return new QueryConditionBuilder(this, LambdaUtil.getQueryColumn(fn), SqlConnector.AND); } @@ -226,6 +228,15 @@ public class QueryWrapper extends BaseQueryWrapper { return this; } + + public QueryWrapper and(Map whereConditions) { + return and(whereConditions, Collections.emptyMap()); + } + + public QueryWrapper and(Map whereConditions, Map 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 { return this; } + + public QueryWrapper or(Map whereConditions) { + return or(whereConditions, Collections.emptyMap()); + } + + public QueryWrapper or(Map whereConditions, Map operators) { + return connectMap(whereConditions, operators, false, true); + } + + + private QueryWrapper connectMap(Map mapConditions, Map operators, boolean outerAnd, boolean innerAnd) { + if (mapConditions != null) { + QueryCondition condition = null; + for (Map.Entry 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 leftJoin(String table) { return joining(SqlConsts.LEFT_JOIN, new QueryTable(table), true); }