From fbef9bd00593a8d8a9274421603e11b18225027d Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 23 Dec 2024 00:36:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=20SQL=20Builder=20?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/sql/JdbcSql.java | 38 +++++++++---------- .../plusone/commons/sql/MyBatisSql.java | 4 +- .../xyz/zhouxy/plusone/commons/sql/SQL.java | 4 +- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/sql/JdbcSql.java b/src/main/java/xyz/zhouxy/plusone/commons/sql/JdbcSql.java index d24ed8a..5844d28 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/sql/JdbcSql.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/sql/JdbcSql.java @@ -18,6 +18,8 @@ package xyz.zhouxy.plusone.commons.sql; import java.util.Collection; +import xyz.zhouxy.plusone.commons.util.StringTools; + public class JdbcSql extends SQL { JdbcSql() { @@ -33,40 +35,38 @@ public class JdbcSql extends SQL { return this; } - public static String IN(String col, Collection c) { + public static String IN(String col, Collection c) { // NOSONAR return IN(col, c.size()); } - public static String IN(String col, T[] c) { + public static String IN(String col, T[] c) { // NOSONAR return IN(col, c.length); } - private static String IN(String col, int length) { - return col + " IN (" + String.valueOf(buildQuestionsList(length)) + ')'; + private static String IN(String col, int length) { // NOSONAR + if (length == 0) { + return "false"; + } + return col + " IN (" + buildQuestionsList(length) + ')'; } - public static String NOT_IN(String col, Collection c) { + public static String NOT_IN(String col, Collection c) { // NOSONAR return NOT_IN(col, c.size()); } - public static String NOT_IN(String col, T[] c) { + public static String NOT_IN(String col, T[] c) { // NOSONAR return NOT_IN(col, c.length); } - private static String NOT_IN(String col, int length) { - return col + " NOT IN (" + String.valueOf(buildQuestionsList(length)) + ')'; + private static String NOT_IN(String col, int length) { // NOSONAR + if (length == 0) { + return "true"; + } + return col + " NOT IN (" + buildQuestionsList(length) + ')'; } - 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; + private static String buildQuestionsList(int times) { + final int length = times <= 0 ? 0 : (times * 3 - 2); + return StringTools.repeat("?, ", times, length); } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/sql/MyBatisSql.java b/src/main/java/xyz/zhouxy/plusone/commons/sql/MyBatisSql.java index 3592205..0382d43 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/sql/MyBatisSql.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/sql/MyBatisSql.java @@ -41,11 +41,11 @@ public class MyBatisSql extends SQL { return this; } - public static String IN(String col, String paramName) { + public static String IN(String col, String paramName) { // NOSONAR return " " + col + " IN" + buildForeach(col, paramName); } - public static String NOT_IN(String col, String paramName) { + public static String NOT_IN(String col, String paramName) { // NOSONAR return col + " NOT IN" + buildForeach(col, paramName); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/sql/SQL.java b/src/main/java/xyz/zhouxy/plusone/commons/sql/SQL.java index b0ef318..a699639 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/sql/SQL.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/sql/SQL.java @@ -34,14 +34,14 @@ public abstract class SQL extends AbstractSQL { return new MyBatisSql(withScript); } - public T WHERE(boolean condition, String sqlCondition) { + public T WHERE(boolean condition, String sqlCondition) { // NOSONAR if (condition) { return WHERE(sqlCondition); } return getSelf(); } - public T WHERE(boolean condition, String ifSqlCondition, String elseSqlCondition) { + public T WHERE(boolean condition, String ifSqlCondition, String elseSqlCondition) { // NOSONAR return WHERE(condition ? ifSqlCondition : elseSqlCondition); } }