Browse Source

重构 wrapper 部分逻辑 query 演变

hubin 7 years ago
parent
commit
2fa70fc14b

+ 270 - 0
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/Condition.java

@@ -0,0 +1,270 @@
+package com.baomidou.mybatisplus.core.test.query;
+
+import static com.baomidou.mybatisplus.core.test.query.Keywords.AND;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.BETWEEN;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.EQ;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.EXISTS;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.GE;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.GROUP_BY;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.GT;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.HAVING;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.IN;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.IS_NOT_NULL;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.IS_NULL;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.LE;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.LIKE;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.LT;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.NE;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.NOT;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.OR;
+import static com.baomidou.mybatisplus.core.test.query.Keywords.ORDER_BY;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+
+/**
+ * <p>
+ * 查询条件封装
+ * </p>
+ *
+ * @author hubin
+ * @since 2017-05-26
+ */
+public class Condition implements ISqlSegment {
+
+    List<ISqlSegment> expression = new ArrayList<>();
+
+    public Condition apply(String condition) {
+        expression.add(() -> condition);
+        return this;
+    }
+
+    public Condition and(String condition) {
+        return this.addCondition(condition, AND);
+    }
+
+    public Condition and(Function<Condition, Condition> condition) {
+        return addNestedCondition(condition, AND);
+    }
+
+    public Condition or(String condition) {
+        return this.addCondition(condition, OR);
+    }
+
+    public Condition or(Function<Condition, Condition> condition) {
+        return addNestedCondition(condition, OR);
+    }
+
+    public Condition in(String condition) {
+        return this.addNestedCondition(condition, IN);
+    }
+
+    /**
+     * NOT 关键词
+     */
+    public Condition not() {
+        expression.add(NOT);
+        return this;
+    }
+
+    /**
+     * HAVING 关键词
+     */
+    public Condition having() {
+        expression.add(HAVING);
+        return this;
+    }
+
+    /**
+     * LIKE '%值%'
+     */
+    public Condition like(String condition) {
+        expression.add(LIKE);
+        expression.add(() -> "'%");
+        expression.add(() -> condition);
+        expression.add(() -> "%'");
+        return this;
+    }
+
+    /**
+     * LIKE '%值'
+     */
+    public Condition likeLeft(String condition) {
+        expression.add(LIKE);
+        expression.add(() -> "'%");
+        expression.add(() -> condition);
+        expression.add(() -> "'");
+        return this;
+    }
+
+    /**
+     * LIKE '值%'
+     */
+    public Condition likeRight(String condition) {
+        expression.add(LIKE);
+        expression.add(() -> "'");
+        expression.add(() -> condition);
+        expression.add(() -> "%'");
+        return this;
+    }
+
+    /**
+     * 等于 =
+     */
+    public Condition eq(String condition) {
+        return this.addCondition(condition, EQ);
+    }
+
+    /**
+     * 不等于 <>
+     */
+    public Condition NE(String condition) {
+        return this.addCondition(condition, NE);
+    }
+
+    /**
+     * 大于 >
+     */
+    public Condition gt(String condition) {
+        return this.addCondition(condition, GT);
+    }
+
+    /**
+     * 大于等于 >=
+     */
+    public Condition ge(String condition) {
+        return this.addCondition(condition, GE);
+    }
+
+    /**
+     * 小于 <
+     */
+    public Condition lt(String condition) {
+        return this.addCondition(condition, LT);
+    }
+
+    /**
+     * 小于等于 <=
+     */
+    public Condition le(String condition) {
+        return this.addCondition(condition, LE);
+    }
+
+    /**
+     * 字段 IS NULL
+     */
+    public Condition isNull(String condition) {
+        expression.add(() -> condition);
+        expression.add(IS_NULL);
+        return this;
+    }
+
+    /**
+     * 字段 IS NOT NULL
+     */
+    public Condition isNotNull(String condition) {
+        expression.add(() -> condition);
+        expression.add(IS_NOT_NULL);
+        return this;
+    }
+
+    /**
+     * 分组:GROUP BY 字段, ...
+     */
+    public Condition groupBy(String condition) {
+        return this.addCondition(condition, GROUP_BY);
+    }
+
+    /**
+     * 排序:ORDER BY 字段, ...
+     */
+    public Condition orderBy(String condition) {
+        return this.addCondition(condition, ORDER_BY);
+    }
+
+    /**
+     * exists ( sql 语句 )
+     */
+    public Condition exists(String condition) {
+        return this.addNestedCondition(condition, EXISTS);
+    }
+
+    /**
+     * BETWEEN 值1 AND 值2
+     */
+    public Condition BETWEEN(String condition, Object val1, Object val2) {
+        expression.add(() -> condition);
+        expression.add(BETWEEN);
+        expression.add(() -> "val1");
+        expression.add(AND);
+        expression.add(() -> "val2");
+        return this;
+    }
+
+    /**
+     * LAST 拼接在 SQL 末尾
+     */
+    public Condition last(String condition) {
+        expression.add(() -> condition);
+        return this;
+    }
+
+    /**
+     * <p>
+     * 普通查询条件
+     * </p>
+     *
+     * @param condition 查询条件值
+     * @param keyword   SQL 关键词
+     * @return
+     */
+    private Condition addCondition(String condition, Keywords keyword) {
+        expression.add(keyword);
+        expression.add(() -> condition);
+        return this;
+    }
+
+    /**
+     * <p>
+     * 嵌套查询条件
+     * </p>
+     *
+     * @param condition 查询条件值
+     * @param keyword   SQL 关键词
+     * @return
+     */
+    private Condition addNestedCondition(String condition, Keywords keyword) {
+        expression.add(keyword);
+        expression.add(() -> "(");
+        expression.add(() -> condition);
+        expression.add(() -> ")");
+        return this;
+    }
+
+    /**
+     * <p>
+     * 多重嵌套查询条件
+     * </p>
+     *
+     * @param condition 查询条件值
+     * @param keyword   SQL 关键词
+     * @return
+     */
+    private Condition addNestedCondition(Function<Condition, Condition> condition, Keywords keyword) {
+        expression.add(keyword);
+        expression.add(() -> "(");
+        expression.add(condition.apply(new Condition()));
+        expression.add(() -> ")");
+        return this;
+    }
+
+    @Override
+    public String sqlSegment() {
+        return String.join(" ", expression.stream()
+            .map(ISqlSegment::sqlSegment)
+            .collect(Collectors.toList()));
+    }
+}

+ 1 - 1
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/struct/ISqlQueue.java → mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/ISqlQueue.java

@@ -1,4 +1,4 @@
-package com.baomidou.mybatisplus.core.test.query.struct;
+package com.baomidou.mybatisplus.core.test.query;
 
 
 public interface ISqlQueue<T> {

+ 32 - 2
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/Keywords.java

@@ -1,12 +1,42 @@
 package com.baomidou.mybatisplus.core.test.query;
 
 
+/**
+ * <p>
+ * SQL 保留关键字枚举
+ * </p>
+ *
+ * @author hubin
+ * @since 2017-05-26
+ */
 public enum Keywords implements ISqlSegment {
+    AND("AND"),
+    OR("OR"),
+    IN("IN"),
+    NOT("NOT"),
+    LIKE("LIKE"),
+    EQ("="),
+    NE("<>"),
+    GT(">"),
+    GE(">="),
+    LT("<"),
+    LE("<="),
+    IS_NULL("IS NULL"),
+    IS_NOT_NULL("IS NOT NULL"),
+    GROUP_BY("GROUP BY"),
+    HAVING("HAVING"),
+    ORDER_BY("ORDER BY"),
+    EXISTS("EXISTS"),
+    BETWEEN("BETWEEN");
 
-    AND, OR;
+    private String condition;
+
+    Keywords(final String condition) {
+        this.condition = condition;
+    }
 
     @Override
     public String sqlSegment() {
-        return this.toString();
+        return this.condition;
     }
 }

+ 0 - 25
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/Query.java

@@ -1,9 +1,5 @@
 package com.baomidou.mybatisplus.core.test.query;
 
-import com.baomidou.mybatisplus.core.test.query.clause.*;
-import com.baomidou.mybatisplus.core.test.query.predicate.*;
-import com.baomidou.mybatisplus.core.test.query.struct.SqlStatement;
-
 import java.util.function.Function;
 
 
@@ -34,27 +30,6 @@ public class Query implements ISqlSegment {
       return add(new Select(columns));
    }
 
-   /**
-    * SELECT table(s)
-    * @param tables table or tables from which to select
-    * @return this
-    */
-   public Query from(String... tables) {
-      return add(new From(tables));
-   }
-
-   /**
-    * FROM table JOIN(s)
-    * @param table from which to select
-    * @param joins function to populate join clauses
-    * @return this
-    */
-   public Query from(String table, Function<Table, Table> joins) {
-      From from = new From(new String[]{ table });
-      from.joins(joins.apply(new Table()));
-      return add(from);
-   }
-
    /**
     * WHERE expression
     * @param expression to

+ 5 - 79
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/QueryTest.java

@@ -15,89 +15,15 @@ public class QueryTest {
     public void test() {
         String sql = new Query()
             .select("b.*")
-            .from("bunnies b")
             .where("b.age > 18", condition ->
                 condition.and("b.type = 'rabid'")
-                    .or("b.abc=12"))
-            .sqlSegment();
+                    .or(nested -> nested.apply("name='12'").and("age=1"))
+                    .not().in("ads,2112,212")
+                    .last("LIMIT 1")
+            ).sqlSegment();
 
         log(sql);
-    }
-
-    @Test
-    public void testBunnies() {
-
-        String sql = new Query()
-            .select("*")
-            .from("bunnies")
-            .sqlSegment();
-
-        log(sql);
-        assertEquals("SELECT * FROM bunnies", sql);
-    }
-
-    @Test
-    public void testCrazyManlyBunnies() {
-
-        String sql = new Query()
-            .select("b.*")
-            .from("bunnies b")
-            .where("b.age > 18", condition ->
-                condition.and("b.type = 'rabid'"))
-            .sqlSegment();
-
-        log(sql);
-        assertEquals("SELECT b.* FROM bunnies b WHERE b.age > 18 AND b.type = 'rabid'", sql);
-    }
-
-    @Test
-    public void testLigers() {
-
-        String sql = new Query()
-            .select("t.id", "l.id")
-            .from("tigers t", table ->
-                table.innerJoin("lions l").on("t.lion_id = l.id"))
-            .sqlSegment();
-
-        log(sql);
-        assertEquals("SELECT t.id, l.id FROM tigers t INNER JOIN lions l ON t.lion_id = l.id", sql);
-    }
-
-    class Sloth {
-
-        private Integer slownessLevel;
-
-        public Sloth(Integer slownessLevel) {
-            this.slownessLevel = slownessLevel;
-        }
-
-        public String repeat(String message) {
-
-            StringBuilder toRepeat = new StringBuilder();
-            char[] chars = message.toCharArray();
-
-            for (char aChar : chars)
-                if (aChar == ' ') toRepeat.append(aChar);
-                else for (int j = 0; j < slownessLevel; j++)
-                    toRepeat.append(aChar);
-
-            return toRepeat.toString();
-        }
-    }
-
-    @Test
-    public void testSpecificSloth() {
-
-        String sql = new Query()
-            .select("s.*")
-            .from("sloths s", "slowness n")
-            .where("s.slowness_id = n.id", condition ->
-                condition.and(nested ->
-                    nested.apply("s.name = 'Kristen Bell'").or("n.level = 1.5")))
-            .sqlSegment();
-
-        log(sql);
-        assertEquals("SELECT s.* FROM sloths s, slowness n WHERE s.slowness_id = n.id AND ( s.name = 'Kristen Bell' OR n.level = 1.5 )", sql);
+        assertEquals("SELECT b.* WHERE b.age > 18 AND b.type = 'rabid' OR ( name='12' AND age=1 ) NOT IN ( ads,2112,212 ) LIMIT 1", sql);
     }
 
 }

+ 1 - 3
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/struct/SqlStatement.java → mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/SqlStatement.java

@@ -1,12 +1,10 @@
-package com.baomidou.mybatisplus.core.test.query.struct;
+package com.baomidou.mybatisplus.core.test.query;
 
 import static java.util.stream.Collectors.toList;
 
 import java.util.ArrayDeque;
 import java.util.Deque;
 
-import com.baomidou.mybatisplus.core.test.query.ISqlSegment;
-
 
 public class SqlStatement implements ISqlQueue<ISqlSegment>, ISqlSegment {
 

+ 1 - 4
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/clause/Where.java → mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/Where.java

@@ -1,7 +1,4 @@
-package com.baomidou.mybatisplus.core.test.query.clause;
-
-import com.baomidou.mybatisplus.core.test.query.ISqlSegment;
-import com.baomidou.mybatisplus.core.test.query.predicate.Condition;
+package com.baomidou.mybatisplus.core.test.query;
 
 
 public class Where implements ISqlSegment {

+ 0 - 19
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/clause/Column.java

@@ -1,19 +0,0 @@
-package com.baomidou.mybatisplus.core.test.query.clause;
-
-
-public class Column {
-
-   private String name;
-
-   public Column(String name) {
-      this.name = name;
-   }
-
-   public Column eq(String anotherColumn) {
-      return this;
-   }
-
-   public Column greaterThan(String anotherColumn) {
-      return this;
-   }
-}

+ 0 - 36
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/clause/From.java

@@ -1,36 +0,0 @@
-package com.baomidou.mybatisplus.core.test.query.clause;
-
-
-import static java.lang.String.format;
-import static java.lang.String.join;
-
-import com.baomidou.mybatisplus.core.test.query.ISqlSegment;
-import com.baomidou.mybatisplus.core.test.query.predicate.Table;
-
-
-public class From implements ISqlSegment {
-
-   private String[] tables;
-   private Table table;
-
-   public From(String[] tables) {
-      this.tables = tables;
-   }
-
-   public void joins(Table table) {
-      this.table = table;
-   }
-
-    @Override
-    public String sqlSegment() {
-      String sql = format("FROM %s", join(", ", (CharSequence[]) tables));
-
-      if (table != null) {
-         sql = sql
-            .concat(" ")
-            .concat(table.sqlSegment());
-      }
-
-      return sql;
-   }
-}

+ 0 - 41
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/clause/Join.java

@@ -1,41 +0,0 @@
-package com.baomidou.mybatisplus.core.test.query.clause;
-
-import static java.lang.String.format;
-
-import com.baomidou.mybatisplus.core.test.query.ISqlSegment;
-
-
-public class Join implements ISqlSegment {
-
-    public enum JoinType {
-
-        DEFAULT,
-        INNER,
-        LEFT_OUTER,
-        RIGHT_OUTER,
-        FULL_OUTER,
-        CROSS;
-
-        protected String toSQLString() {
-
-            if (this == DEFAULT) {
-                return "JOIN";
-            } else {
-                return format("%s %s", this.toString(), "JOIN");
-            }
-        }
-    }
-
-    private String table;
-    private JoinType joinType;
-
-    public Join(String table, JoinType type) {
-        this.table = table;
-        this.joinType = type;
-    }
-
-    @Override
-    public String sqlSegment() {
-        return format("%s %s", joinType.toSQLString(), table);
-    }
-}

+ 0 - 20
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/clause/Select.java

@@ -1,20 +0,0 @@
-package com.baomidou.mybatisplus.core.test.query.clause;
-
-import static java.lang.String.format;
-
-import com.baomidou.mybatisplus.core.test.query.ISqlSegment;
-
-
-public class Select implements ISqlSegment {
-
-    private String[] columns;
-
-    public Select(String[] columns) {
-        this.columns = columns;
-    }
-
-    @Override
-    public String sqlSegment() {
-        return format("SELECT %s", String.join(", ", (CharSequence[]) columns));
-    }
-}

+ 0 - 58
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/predicate/Condition.java

@@ -1,58 +0,0 @@
-package com.baomidou.mybatisplus.core.test.query.predicate;
-
-import com.baomidou.mybatisplus.core.test.query.Keywords;
-import com.baomidou.mybatisplus.core.test.query.ISqlSegment;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.function.Function;
-import java.util.stream.Collectors;
-
-import static com.baomidou.mybatisplus.core.test.query.Keywords.AND;
-import static com.baomidou.mybatisplus.core.test.query.Keywords.OR;
-
-
-public class Condition implements ISqlSegment {
-
-    List<ISqlSegment> expression = new ArrayList<>();
-
-    public Condition apply(String condition) {
-        expression.add(() -> condition);
-        return this;
-    }
-
-    public Condition and(String condition) {
-        expression.add(AND);
-        expression.add(() -> condition);
-        return this;
-    }
-
-    public Condition and(Function<Condition, Condition> condition) {
-        return addNestedCondition(condition, AND);
-    }
-
-    public Condition or(String condition) {
-        expression.add(OR);
-        expression.add(() -> condition);
-        return this;
-    }
-
-    public Condition or(Function<Condition, Condition> condition) {
-        return addNestedCondition(condition, OR);
-    }
-
-    private Condition addNestedCondition(Function<Condition, Condition> condition, Keywords keyword) {
-        expression.add(keyword);
-        expression.add(() -> "(");
-        expression.add(condition.apply(new Condition()));
-        expression.add(() -> ")");
-        return this;
-    }
-
-    @Override
-    public String sqlSegment() {
-        return String.join(" ", expression.stream()
-            .map(ISqlSegment::sqlSegment)
-            .collect(Collectors.toList()));
-    }
-}

+ 0 - 18
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/predicate/On.java

@@ -1,18 +0,0 @@
-package com.baomidou.mybatisplus.core.test.query.predicate;
-
-import com.baomidou.mybatisplus.core.test.query.ISqlSegment;
-
-
-public class On implements ISqlSegment {
-
-    private String criteria;
-
-    public On(String criteria) {
-        this.criteria = criteria;
-    }
-
-    @Override
-    public String sqlSegment() {
-        return String.format("ON %s", criteria);
-    }
-}

+ 0 - 100
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/query/predicate/Table.java

@@ -1,100 +0,0 @@
-package com.baomidou.mybatisplus.core.test.query.predicate;
-
-import static java.util.stream.Collectors.toList;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import com.baomidou.mybatisplus.core.test.query.ISqlSegment;
-import com.baomidou.mybatisplus.core.test.query.clause.Join;
-
-
-public class Table implements ISqlSegment {
-
-    List<ISqlSegment> stringables = new ArrayList<>();
-
-    /**
-     * INNER JOIN table
-     *
-     * @param table table to join
-     * @return this
-     */
-    public Table innerJoin(String table) {
-        stringables.add(new Join(table, Join.JoinType.INNER));
-        return this;
-    }
-
-    /**
-     * CROSS JOIN table
-     *
-     * @param table table to join
-     * @return this
-     */
-    public Table crossJoin(String table) {
-        stringables.add(new Join(table, Join.JoinType.CROSS));
-        return this;
-    }
-
-    /**
-     * JOIN table
-     *
-     * @param table table to join
-     * @return this
-     */
-    public Table join(String table) {
-        stringables.add(new Join(table, Join.JoinType.DEFAULT));
-        return this;
-    }
-
-    /**
-     * LEFT OUTER JOIN table
-     *
-     * @param table table to join
-     * @return this
-     */
-    public Table leftOuterJoin(String table) {
-        stringables.add(new Join(table, Join.JoinType.LEFT_OUTER));
-        return this;
-    }
-
-    /**
-     * RIGHT OUTER JOIN table
-     *
-     * @param table table to join
-     * @return this
-     */
-    public Table rightOuterJoin(String table) {
-        stringables.add(new Join(table, Join.JoinType.RIGHT_OUTER));
-        return this;
-    }
-
-    /**
-     * FULL OUTER JOIN table
-     *
-     * @param table table to join
-     * @return this
-     */
-    public Table fullOuterJoin(String table) {
-        stringables.add(new Join(table, Join.JoinType.FULL_OUTER));
-        return this;
-    }
-
-    /**
-     * ON expression
-     *
-     * @param expression expression on which to join
-     * @return this
-     */
-    public Table on(String expression) {
-        stringables.add(new On(expression));
-        return this;
-    }
-
-    @Override
-    public String sqlSegment() {
-        return String.join(" ", stringables
-            .stream()
-            .map(ISqlSegment::sqlSegment)
-            .collect(toList()));
-    }
-}