From 58e085277bb7dba27045f3af3a4a215296cdaf27 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 7 Jun 2023 11:51:32 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91=E5=B0=86?= =?UTF-8?q?=20Jdbc=20=E7=9A=84=20SQL=20=E6=9E=84=E5=BB=BA=E6=94=BE?= =?UTF-8?q?=E5=9C=A8=20JdbcSql=20=E4=B8=AD=EF=BC=8C=E5=90=8E=E7=BB=AD?= =?UTF-8?q?=E5=8F=AF=E6=B7=BB=E5=8A=A0=20SQL=20=E7=9A=84=E5=85=B6=E5=AE=83?= =?UTF-8?q?=E5=AD=90=E7=B1=BB=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=20MyBatis=20=E7=AD=89=E4=B8=8D=E5=90=8C=E7=9A=84=20SQL?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/jdbc/JdbcSql.java | 62 ++++++++++++++++++ .../xyz/zhouxy/plusone/commons/jdbc/SQL.java | 64 ++----------------- .../commons/util/SimpleJdbcTemplateTests.java | 5 +- 3 files changed, 70 insertions(+), 61 deletions(-) create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/jdbc/JdbcSql.java 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))