Jelajahi Sumber

优化咩咩的重构

hubin 7 tahun lalu
induk
melakukan
b7359e32be

+ 1 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractWrapper.java

@@ -50,11 +50,11 @@ import com.baomidou.mybatisplus.core.conditions.interfaces.Compare;
 import com.baomidou.mybatisplus.core.conditions.interfaces.Func;
 import com.baomidou.mybatisplus.core.conditions.interfaces.Join;
 import com.baomidou.mybatisplus.core.conditions.interfaces.Nested;
+import com.baomidou.mybatisplus.core.conditions.segments.JoinSegment;
 import com.baomidou.mybatisplus.core.enums.SqlKeyword;
 import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
-import com.baomidou.mybatisplus.core.toolkit.sql.segments.JoinSegment;
 
 
 /**
@@ -65,7 +65,6 @@ import com.baomidou.mybatisplus.core.toolkit.sql.segments.JoinSegment;
  * @author hubin miemie HCL
  * @since 2017-05-26
  */
-@SuppressWarnings("unchecked,serial")
 public abstract class AbstractWrapper<T, R, This extends AbstractWrapper<T, R, This>> extends Wrapper<T>
     implements Compare<This, R>, Nested<This>, Join<This>, Func<This, R>, Serializable {
 

+ 5 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/segments/GroupBySegment.java → mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/segments/GroupBySegment.java

@@ -13,7 +13,7 @@
  * License for the specific language governing permissions and limitations under
  * the License.
  */
-package com.baomidou.mybatisplus.core.toolkit.sql.segments;
+package com.baomidou.mybatisplus.core.conditions.segments;
 
 import static com.baomidou.mybatisplus.core.enums.SqlKeyword.GROUP_BY;
 import static java.util.stream.Collectors.joining;
@@ -25,6 +25,10 @@ import java.util.List;
 import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
 
 /**
+ * <p>
+ * Group By SQL 片段
+ * </p>
+ *
  * @author miemie
  * @since 2018-06-27
  */

+ 7 - 6
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/segments/JoinSegment.java → mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/segments/JoinSegment.java

@@ -13,10 +13,7 @@
  * License for the specific language governing permissions and limitations under
  * the License.
  */
-package com.baomidou.mybatisplus.core.toolkit.sql.segments;
-
-import static com.baomidou.mybatisplus.core.toolkit.sql.segments.MatchSegment.GROUP_BY;
-import static com.baomidou.mybatisplus.core.toolkit.sql.segments.MatchSegment.ORDER_BY;
+package com.baomidou.mybatisplus.core.conditions.segments;
 
 import java.util.Arrays;
 import java.util.List;
@@ -24,6 +21,10 @@ import java.util.List;
 import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
 
 /**
+ * <p>
+ * 合并 SQL 片段
+ * </p>
+ *
  * @author miemie
  * @since 2018-06-27
  */
@@ -38,9 +39,9 @@ public class JoinSegment implements ISqlSegment {
     public void add(ISqlSegment... iSqlSegments) {
         List<ISqlSegment> list = Arrays.asList(iSqlSegments);
         ISqlSegment sqlSegment = list.get(0);
-        if (MatchSegment.match(ORDER_BY, sqlSegment)) {
+        if (MatchSegment.ORDER_BY.match(sqlSegment)) {
             orderBySegment.addAll(list);
-        } else if (MatchSegment.match(GROUP_BY, sqlSegment)) {
+        } else if (MatchSegment.GROUP_BY.match(sqlSegment)) {
             groupBySegment.addAll(list);
         } else {
             normalSegment.addAll(list);

+ 52 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/segments/MatchSegment.java

@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.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.baomidou.mybatisplus.core.conditions.segments;
+
+import java.util.function.Predicate;
+
+import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
+import com.baomidou.mybatisplus.core.enums.SqlKeyword;
+
+/**
+ * <p>
+ * 匹配片段
+ * </p>
+ *
+ * @author miemie
+ * @since 2018-06-27
+ */
+public enum MatchSegment {
+    GROUP_BY(i -> i == SqlKeyword.GROUP_BY),
+    ORDER_BY(i -> i == SqlKeyword.ORDER_BY),
+    NOT(i -> i == SqlKeyword.NOT),
+    AND(i -> i == SqlKeyword.AND),
+    OR(i -> i == SqlKeyword.OR),
+    AND_OR(i -> i == SqlKeyword.AND || i == SqlKeyword.OR);
+
+    private final Predicate<ISqlSegment> predicate;
+
+    MatchSegment(Predicate<ISqlSegment> predicate) {
+        this.predicate = predicate;
+    }
+
+    public boolean match(ISqlSegment segment) {
+        return getPredicate().test(segment);
+    }
+
+    protected Predicate<ISqlSegment> getPredicate() {
+        return predicate;
+    }
+}

+ 11 - 12
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/segments/NormalSegment.java → mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/segments/NormalSegment.java

@@ -13,12 +13,7 @@
  * License for the specific language governing permissions and limitations under
  * the License.
  */
-package com.baomidou.mybatisplus.core.toolkit.sql.segments;
-
-import static com.baomidou.mybatisplus.core.toolkit.sql.segments.MatchSegment.AND;
-import static com.baomidou.mybatisplus.core.toolkit.sql.segments.MatchSegment.AND_OR;
-import static com.baomidou.mybatisplus.core.toolkit.sql.segments.MatchSegment.NOT;
-import static com.baomidou.mybatisplus.core.toolkit.sql.segments.MatchSegment.OR;
+package com.baomidou.mybatisplus.core.conditions.segments;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -29,6 +24,10 @@ import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
 import com.baomidou.mybatisplus.core.enums.SqlKeyword;
 
 /**
+ * <p>
+ * 普通片段
+ * </p>
+ *
  * @author miemie
  * @since 2018-06-27
  */
@@ -48,19 +47,19 @@ public class NormalSegment extends ArrayList<ISqlSegment> implements ISqlSegment
             /**
              * 只有 and() 以及 or() 以及 not() 会进入
              */
-            if (!MatchSegment.match(NOT, sqlSegment)) {
+            if (!MatchSegment.NOT.match(sqlSegment)) {
                 //不是 not
                 if (isEmpty()) {
                     //sqlSegment是 and 或者 or 并且在第一位,不继续执行
                     return false;
                 }
-                boolean matchLastAnd = MatchSegment.match(AND, lastValue);
-                boolean matchLastOr = MatchSegment.match(OR, lastValue);
+                boolean matchLastAnd = MatchSegment.AND.match(lastValue);
+                boolean matchLastOr = MatchSegment.OR.match(lastValue);
                 if (matchLastAnd || matchLastOr) {
                     //上次最后一个值是 and 或者 or
-                    if (matchLastAnd && MatchSegment.match(AND, sqlSegment)) {
+                    if (matchLastAnd && MatchSegment.AND.match(sqlSegment)) {
                         return false;
-                    } else if (matchLastOr && MatchSegment.match(OR, sqlSegment)) {
+                    } else if (matchLastOr && MatchSegment.OR.match(sqlSegment)) {
                         return false;
                     } else {
                         //和上次的不一样
@@ -68,7 +67,7 @@ public class NormalSegment extends ArrayList<ISqlSegment> implements ISqlSegment
                     }
                 }
             }
-        } else if (!MatchSegment.match(AND_OR, lastValue) && !isEmpty()) {
+        } else if (!MatchSegment.AND_OR.match(lastValue) && !isEmpty()) {
             add(SqlKeyword.AND);
         }
         //后置处理

+ 5 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/segments/OrderBySegment.java → mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/segments/OrderBySegment.java

@@ -13,7 +13,7 @@
  * License for the specific language governing permissions and limitations under
  * the License.
  */
-package com.baomidou.mybatisplus.core.toolkit.sql.segments;
+package com.baomidou.mybatisplus.core.conditions.segments;
 
 import static com.baomidou.mybatisplus.core.enums.SqlKeyword.ORDER_BY;
 import static java.util.stream.Collectors.joining;
@@ -25,6 +25,10 @@ import java.util.List;
 import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
 
 /**
+ * <p>
+ * Order By SQL 片段
+ * </p>
+ *
  * @author miemie
  * @since 2018-06-27
  */

+ 4 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/segments/package-info.java

@@ -0,0 +1,4 @@
+/**
+ * SQL 片段相关类
+ */
+package com.baomidou.mybatisplus.core.conditions.segments;

+ 0 - 33
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/segments/MatchSegment.java

@@ -1,33 +0,0 @@
-package com.baomidou.mybatisplus.core.toolkit.sql.segments;
-
-import java.util.function.Predicate;
-
-import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
-import com.baomidou.mybatisplus.core.enums.SqlKeyword;
-
-/**
- * @author miemie
- * @since 2018-06-27
- */
-enum MatchSegment {
-    GROUP_BY(i -> i == SqlKeyword.GROUP_BY),
-    ORDER_BY(i -> i == SqlKeyword.ORDER_BY),
-    NOT(i -> i == SqlKeyword.NOT),
-    AND(i -> i == SqlKeyword.AND),
-    OR(i -> i == SqlKeyword.OR),
-    AND_OR(i -> i == SqlKeyword.AND || i == SqlKeyword.OR);
-
-    private final Predicate<ISqlSegment> predicate;
-
-    MatchSegment(Predicate<ISqlSegment> predicate) {
-        this.predicate = predicate;
-    }
-
-    protected static boolean match(MatchSegment matchSegment, ISqlSegment segment) {
-        return matchSegment.getPredicate().test(segment);
-    }
-
-    protected Predicate<ISqlSegment> getPredicate() {
-        return predicate;
-    }
-}