diff --git a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/JdbcSql.java b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/JdbcSql.java new file mode 100644 index 0000000..030699c --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/JdbcSql.java @@ -0,0 +1,62 @@ +package xyz.zhouxy.plusone.commons.jdbc; + +import java.util.Collection; + +public class JdbcSql extends SQL { + + JdbcSql() { + super(); + } + + @Override + public JdbcSql getSelf() { + return this; + } + + public static String IN(String col, Collection c) { + return IN(col, c.size()); + } + + public static String IN(String col, T[] c) { + return IN(col, c.length); + } + + private static String IN(String col, int length) { + return new StringBuilder() + .append(col) + .append(" IN (") + .append(buildQuestionsList(length)) + .append(')') + .toString(); + } + + public static String NOT_IN(String col, Collection c) { + return NOT_IN(col, c.size()); + } + + public static String NOT_IN(String col, T[] c) { + return NOT_IN(col, c.length); + } + + private static String NOT_IN(String col, int length) { + return new StringBuilder() + .append(col) + .append(" NOT IN (") + .append(buildQuestionsList(length)) + .append(')') + .toString(); + } + + private static char[] buildQuestionsList(int times) { + char[] arr = new char[times * 3 - 2]; + int i = 0; + for (int t = 1; t <= times; t++) { + arr[i++] = '?'; + if (t < times) { + arr[i++] = ','; + arr[i++] = ' '; + } + } + return arr; + } +} \ 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 97192b6..bba238d 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SQL.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SQL.java @@ -16,8 +16,6 @@ package xyz.zhouxy.plusone.commons.jdbc; -import java.util.Collection; - import org.apache.ibatis.jdbc.AbstractSQL; import com.google.common.annotations.Beta; @@ -26,68 +24,16 @@ import com.google.common.annotations.Beta; * @author ZhouXY */ @Beta -public class SQL extends AbstractSQL { +public abstract class SQL extends AbstractSQL { - @Override - public SQL getSelf() { - return this; + public static JdbcSql newJdbcSql() { + return new JdbcSql(); } - public static SQL newSql() { - return new SQL(); - } - - public SQL WHERE(boolean condition, String sqlConditions) { + public T WHERE(boolean condition, String sqlConditions) { if (condition) { return WHERE(sqlConditions); } return getSelf(); } - - public static String IN(String col, Collection c) { - return IN(col, c.size()); - } - - public static String IN(String col, T[] c) { - return IN(col, c.length); - } - - private static String IN(String col, int length) { - return new StringBuilder() - .append(col) - .append(" IN (") - .append(buildQuestionsList(length)) - .append(')') - .toString(); - } - - public static String NOT_IN(String col, Collection c) { - return NOT_IN(col, c.size()); - } - - public static String NOT_IN(String col, T[] c) { - return NOT_IN(col, c.length); - } - - private static String NOT_IN(String col, int length) { - return new StringBuilder() - .append(col) - .append(" NOT IN (") - .append(buildQuestionsList(length)) - .append(')') - .toString(); - } - - private static String buildQuestionsList(int times) { - char[] arr = new char[times * 3 - 2]; - int i = 0; - for (int t = 1; t <= times; t++) { - arr[i++] = '?'; - if (t < times) { - arr[i++] = ','; - arr[i++] = ' '; - } - } - return String.valueOf(arr); - } -} +} \ No newline at end of file diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java index 1cc22af..8e38389 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java @@ -3,7 +3,7 @@ package xyz.zhouxy.plusone.commons.util; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import static xyz.zhouxy.plusone.commons.jdbc.SQL.*; +import static xyz.zhouxy.plusone.commons.jdbc.JdbcSql.*; import java.io.File; import java.io.IOException; @@ -27,6 +27,7 @@ import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import xyz.zhouxy.plusone.commons.jdbc.DbRecord; +import xyz.zhouxy.plusone.commons.jdbc.SQL; import xyz.zhouxy.plusone.commons.jdbc.SimpleJdbcTemplate; class SimpleJdbcTemplateTests { @@ -56,7 +57,7 @@ class SimpleJdbcTemplateTests { void testQuery() throws SQLException { try (Connection conn = this.dataSource.getConnection()) { Object[] params = SimpleJdbcTemplate.buildParams("501533", "501554", "544599"); - String sql = newSql() + String sql = SQL.newJdbcSql() .SELECT("*") .FROM("test_table") .WHERE(NOT_IN("id", params))