24282 před 1 rokem
rodič
revize
74b95756d1

+ 39 - 43
my-test/src/main/java/org/example/join/domain/QueryCondition.java

@@ -67,51 +67,47 @@ public class QueryCondition implements IFormat {
         StringJoiner joiner = new StringJoiner(StringPool.EMPTY);
         joiner.add(column.toSql());
 
-        if (value instanceof IQueryColumn iQueryColumn) {
-            joiner.add(logic);
-            joiner.add(iQueryColumn.toSql());
-        } else {
-            switch (logic) {
-                case SqlConstant.BETWEEN, SqlConstant.NOT_BETWEEN -> {
-                    joiner.add(logic);
-                    Object[] valueObj = (Object[]) value;
-                    SqlUtil.putValue(joiner, valueObj[0]);
-                    joiner.add(SqlConstant.AND);
-                    SqlUtil.putValue(joiner, valueObj[1]);
-                }
-                case SqlConstant.LIKE, SqlConstant.NOT_LIKE -> {
-                    joiner.add(logic);
-                    joiner.add("concat('%',");
-                    SqlUtil.putValue(joiner, value);
-                    joiner.add(",'%')");
-                }
-                case SqlConstant.LIKE_LEFT, SqlConstant.NOT_LIKE_LEFT -> {
-                    joiner.add(SqlConstant.LIKE);
-                    joiner.add("concat('%',");
-                    SqlUtil.putValue(joiner, value);
-                    joiner.add(")");
-                }
-                case SqlConstant.LIKE_RIGHT, SqlConstant.NOT_LIKE_RIGHT -> {
-                    joiner.add(SqlConstant.LIKE);
-                    joiner.add("concat(");
-                    SqlUtil.putValue(joiner, value);
-                    joiner.add(",'%')");
-                }
-                case SqlConstant.IS_NULL, SqlConstant.IS_NOT_NULL -> joiner.add(logic);
-                case SqlConstant.IN, SqlConstant.NOT_IN -> {
-                    joiner.add(logic);
-                    List<Object> inList = new ArrayList<>((Collection<?>) value);
-                    joiner.add("(");
-                    for (int i = 0; i < inList.size(); i++) {
-                        SqlUtil.putValue(joiner, inList.get(i));
-                        joiner.add(inList.size() == i + 1 ? ")" : ",");
-                    }
-                }
-                default -> {
-                    joiner.add(logic);
-                    SqlUtil.putParam(joiner, value);
+        switch (logic) {
+            case SqlConstant.EQ, SqlConstant.NE, SqlConstant.GT, SqlConstant.LT, SqlConstant.GE, SqlConstant.LE -> {
+                joiner.add(logic);
+                SqlUtil.putValue(joiner, value);
+            }
+            case SqlConstant.BETWEEN, SqlConstant.NOT_BETWEEN -> {
+                joiner.add(logic);
+                Object[] valueObj = (Object[]) value;
+                SqlUtil.putValue(joiner, valueObj[0]);
+                joiner.add(SqlConstant.AND);
+                SqlUtil.putValue(joiner, valueObj[1]);
+            }
+            case SqlConstant.LIKE, SqlConstant.NOT_LIKE -> {
+                joiner.add(logic);
+                joiner.add("concat('%',");
+                SqlUtil.putValue(joiner, value);
+                joiner.add(",'%')");
+            }
+            case SqlConstant.LIKE_LEFT, SqlConstant.NOT_LIKE_LEFT -> {
+                joiner.add(SqlConstant.LIKE);
+                joiner.add("concat('%',");
+                SqlUtil.putValue(joiner, value);
+                joiner.add(")");
+            }
+            case SqlConstant.LIKE_RIGHT, SqlConstant.NOT_LIKE_RIGHT -> {
+                joiner.add(SqlConstant.LIKE);
+                joiner.add("concat(");
+                SqlUtil.putValue(joiner, value);
+                joiner.add(",'%')");
+            }
+            case SqlConstant.IS_NULL, SqlConstant.IS_NOT_NULL -> joiner.add(logic);
+            case SqlConstant.IN, SqlConstant.NOT_IN -> {
+                joiner.add(logic);
+                List<Object> inList = new ArrayList<>((Collection<?>) value);
+                joiner.add("(");
+                for (int i = 0; i < inList.size(); i++) {
+                    SqlUtil.putValue(joiner, inList.get(i));
+                    joiner.add(inList.size() == i + 1 ? ")" : ",");
                 }
             }
+            default -> throw new IllegalArgumentException("未知方法");
         }
 
         String sql = joiner.toString();

+ 10 - 0
my-test/src/main/java/org/example/join/util/SqlConstant.java

@@ -3,27 +3,37 @@ package org.example.join.util;
 public interface SqlConstant {
 
     String SQL_PARAM = "joinSqlParam";
+
     String SELECT = "select ";
     String FROM = " from ";
     String WHERE = " where ";
+
     String INNER_JOIN = " inner join ";
     String LEFT_JOIN = " left join ";
     String RIGHT_JOIN = " right join ";
+
     String GROUP_BY = " group by ";
     String ORDER_BY = " order by ";
+
     String LIMIT = " limit ";
+
     String UNION = " union ";
     String UNION_ALL = " union all ";
+
     String AND = " and ";
     String OR = " or ";
+
     String ON = " on ";
+
     String ADD = " + ";
     String SUBTRACT = " - ";
     String MULTIPLY = " * ";
     String DIVIDE = " / ";
     String REMAINDER = " % ";
+
     String ASC = " asc";
     String DESC = " desc";
+
     String EQ = " = ";
     String NE = " <> ";
     String GT = " > ";

+ 3 - 7
my-test/src/main/java/org/example/join/util/SqlUtil.java

@@ -22,17 +22,13 @@ public class SqlUtil {
         return logicDeleteFieldInfo.getColumn();
     }
 
-    public static void putParam(StringJoiner joiner, Object value) {
-        String mapKey = SqlContext.getParamKey();
-        joiner.add(StringPool.HASH_LEFT_BRACE + mapKey + StringPool.RIGHT_BRACE);
-        SqlContext.getParamMap().put(mapKey, value);
-    }
-
     public static void putValue(StringJoiner joiner, Object value) {
         if (value instanceof IFormat iFormat) {
             joiner.add(iFormat.toSql());
         } else {
-            putParam(joiner, value);
+            String mapKey = SqlContext.getParamKey();
+            joiner.add(StringPool.HASH_LEFT_BRACE + mapKey + StringPool.RIGHT_BRACE);
+            SqlContext.getParamMap().put(mapKey, value);
         }
     }
 

+ 2 - 1
my-test/src/test/java/MySpringBootTest.java

@@ -54,7 +54,8 @@ public class MySpringBootTest {
                 .from(sys_dept)
                 .leftJoin(sys_user).on(user_id.eq(dept_id))
                 .where(
-                        //dept_id.eq(100)
+                        dept_id.isNull(),
+                        dept_id.eq(100)
                         //concat("777", "555").eq(create_by),
                         //create_by.eq(concat("777", "555").add(777)).and(concat("777", "555").eq("sss")),
                         //create_by.ne(98),