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); } }