From 9e30ef55806c591690cdd57ed4ccc1aeac2feea2 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 5 Jun 2023 18:31:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20IN=20=E5=92=8C=20NOT=5FIN?= =?UTF-8?q?=20=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xyz/zhouxy/plusone/commons/jdbc/SQL.java | 53 ++++++++++++++++++- .../commons/util/SimpleJdbcTemplateTests.java | 18 +++++-- 2 files changed, 65 insertions(+), 6 deletions(-) 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 1dabb9d..b23f9ff 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SQL.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/SQL.java @@ -16,6 +16,8 @@ package xyz.zhouxy.plusone.commons.jdbc; +import java.util.Collection; + import org.apache.ibatis.jdbc.AbstractSQL; import com.google.common.annotations.Beta; @@ -31,11 +33,60 @@ public class SQL extends AbstractSQL { return this; } - public SQL WHERE_IF(boolean condition, String sqlConditions) { + public static SQL newSql() { + return new SQL(); + } + + public SQL 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]; + for (int t = 1, i = 0; t <= times; t++) { + arr[i++] = '?'; + if (t < times) { + arr[i++] = ','; + arr[i++] = ' '; + } + } + return String.valueOf(arr); + } } 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 ec55599..8502654 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/SimpleJdbcTemplateTests.java @@ -1,7 +1,9 @@ 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 java.io.File; import java.io.IOException; @@ -53,13 +55,19 @@ class SimpleJdbcTemplateTests { @Test void testQuery() throws SQLException { try (Connection conn = this.dataSource.getConnection()) { + Object[] params = MoreArrays.asObjectArray("501533", "501554", "544599"); + String sql = newSql() + .SELECT("*") + .FROM("test_table") + .WHERE(NOT_IN("id", params)) + .toString(); + log.info(sql); List rs = SimpleJdbcTemplate.connect(conn) - .queryToRecordList( - "SELECT * FROM public.base_table WHERE id IN (?, ?, ?)", - 501533, 501554, 544599); - assertTrue(3 > rs.size()); + .queryToRecordList(sql, params); + assertNotNull(rs); for (DbRecord baseEntity : rs) { - log.info("id: {}", baseEntity.getValueAsLong("id")); + // log.info("id: {}", baseEntity.getValueAsString("id")); + log.info(baseEntity.toString()); assertEquals(Optional.empty(), baseEntity.getValueAsString("updated_by")); } }