diff --git a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/JdbcSql.java b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/JdbcSql.java index 030699c..0f725f5 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/JdbcSql.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/JdbcSql.java @@ -8,6 +8,10 @@ public class JdbcSql extends SQL { super(); } + public static JdbcSql newSql() { + return new JdbcSql(); + } + @Override public JdbcSql getSelf() { return this; diff --git a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/MyBatisSql.java b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/MyBatisSql.java new file mode 100644 index 0000000..76d141b --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/MyBatisSql.java @@ -0,0 +1,65 @@ +package xyz.zhouxy.plusone.commons.jdbc; + +import com.google.common.annotations.Beta; + +@Beta +public class MyBatisSql extends SQL { + + private final boolean withScript; + + MyBatisSql(boolean withScript) { + super(); + this.withScript = withScript; + } + + public static MyBatisSql newSql() { + return new MyBatisSql(false); + } + + public static MyBatisSql newScriptSql() { + return new MyBatisSql(true); + } + + @Override + public MyBatisSql getSelf() { + return this; + } + + public static String IN(String col, String paramName) { + return new StringBuilder(" ") + .append(col) + .append(" IN") + .append(buildQuestionsList(col, paramName)) + .toString(); + } + + public static String NOT_IN(String col, String paramName) { + return new StringBuilder() + .append(col) + .append(" NOT IN") + .append(buildQuestionsList(col, paramName)) + .toString(); + } + + private static String buildQuestionsList(String col, String paramName) { + return new StringBuilder() + .append("") + .append("#{") + .append(col) + .append("}") + .toString(); + } + + @Override + public String toString() { + String str = super.toString(); + if (withScript) { + str = ""; + } + return str; + } +} \ No newline at end of file diff --git a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SQL.java b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SQL.java index bba238d..4bc41ac 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SQL.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SQL.java @@ -30,10 +30,18 @@ public abstract class SQL extends AbstractSQL { return new JdbcSql(); } - public T WHERE(boolean condition, String sqlConditions) { + public static MyBatisSql newMyBatisSql(boolean withScript) { + return new MyBatisSql(withScript); + } + + public T WHERE(boolean condition, String sqlCondition) { if (condition) { - return WHERE(sqlConditions); + return WHERE(sqlCondition); } return getSelf(); } + + public T WHERE(boolean condition, String ifSqlCondition, String elseSqlCondition) { + return WHERE(condition ? ifSqlCondition : elseSqlCondition); + } } \ No newline at end of file diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java b/src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java deleted file mode 100644 index 4291478..0000000 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/MoreArrays.java +++ /dev/null @@ -1,29 +0,0 @@ -package xyz.zhouxy.plusone.commons.util; - -import java.util.Arrays; - -import com.google.common.annotations.Beta; - -@Beta -public class MoreArrays { - - public static Object[] asObjectArray(final Object... objects) { - return objects; - } - - public static char[] newCharArrayWith(int length, char c) { - char[] arr = new char[length]; - Arrays.fill(arr, c); - return arr; - } - - public static String[] newStringArrayWith(int length, String c) { - String[] arr = new String[length]; - Arrays.fill(arr, c); - return arr; - } - - private MoreArrays() { - throw new IllegalStateException("Utility class"); - } -} diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/MoreArraysTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/MoreArraysTests.java deleted file mode 100644 index bca55a7..0000000 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/MoreArraysTests.java +++ /dev/null @@ -1,20 +0,0 @@ -package xyz.zhouxy.plusone.commons.util; - -import java.util.Arrays; -import java.util.Date; - -import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -class MoreArraysTests { - - private static final Logger log = LoggerFactory.getLogger(MoreArraysTests.class); - - @Test - void testAsObjectArray() { - Object[] arr = MoreArrays.asObjectArray("1", "2", 1, 2, new Date()); - log.info("arr: {}", arr.toString()); - log.info("arr: {}", Arrays.toString(arr)); - } -} diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/MyBatisSqlBuilderTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/MyBatisSqlBuilderTests.java new file mode 100644 index 0000000..ef2f778 --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/MyBatisSqlBuilderTests.java @@ -0,0 +1,23 @@ +package xyz.zhouxy.plusone.commons.util; + +import static xyz.zhouxy.plusone.commons.jdbc.MyBatisSql.IN; + +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import xyz.zhouxy.plusone.commons.jdbc.MyBatisSql; + +class MyBatisSqlBuilderTests { + private static final Logger log = LoggerFactory.getLogger(MyBatisSqlBuilderTests.class); + + @Test + void test() { + // List ids = Arrays.asList("2333", "4501477"); + MyBatisSql sql = MyBatisSql.newScriptSql() + .SELECT("*") + .FROM("test_table") + .WHERE(IN("id", "ids")); + log.info("sql: {}", sql); + } +}