格式化 SQL Builder 相关代码

pull/1/head
ZhouXY108 2024-12-23 00:36:01 +08:00
parent 275a156184
commit fbef9bd005
3 changed files with 23 additions and 23 deletions

View File

@ -18,6 +18,8 @@ package xyz.zhouxy.plusone.commons.sql;
import java.util.Collection; import java.util.Collection;
import xyz.zhouxy.plusone.commons.util.StringTools;
public class JdbcSql extends SQL<JdbcSql> { public class JdbcSql extends SQL<JdbcSql> {
JdbcSql() { JdbcSql() {
@ -33,40 +35,38 @@ public class JdbcSql extends SQL<JdbcSql> {
return this; return this;
} }
public static String IN(String col, Collection<?> c) { public static String IN(String col, Collection<?> c) { // NOSONAR
return IN(col, c.size()); return IN(col, c.size());
} }
public static <T> String IN(String col, T[] c) { public static <T> String IN(String col, T[] c) { // NOSONAR
return IN(col, c.length); return IN(col, c.length);
} }
private static String IN(String col, int length) { private static String IN(String col, int length) { // NOSONAR
return col + " IN (" + String.valueOf(buildQuestionsList(length)) + ')'; 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()); return NOT_IN(col, c.size());
} }
public static <T> String NOT_IN(String col, T[] c) { public static <T> String NOT_IN(String col, T[] c) { // NOSONAR
return NOT_IN(col, c.length); return NOT_IN(col, c.length);
} }
private static String NOT_IN(String col, int length) { private static String NOT_IN(String col, int length) { // NOSONAR
return col + " NOT IN (" + String.valueOf(buildQuestionsList(length)) + ')'; if (length == 0) {
return "true";
}
return col + " NOT IN (" + buildQuestionsList(length) + ')';
} }
private static char[] buildQuestionsList(int times) { private static String buildQuestionsList(int times) {
char[] arr = new char[times * 3 - 2]; final int length = times <= 0 ? 0 : (times * 3 - 2);
int i = 0; return StringTools.repeat("?, ", times, length);
for (int t = 1; t <= times; t++) {
arr[i++] = '?';
if (t < times) {
arr[i++] = ',';
arr[i++] = ' ';
}
}
return arr;
} }
} }

View File

@ -41,11 +41,11 @@ public class MyBatisSql extends SQL<MyBatisSql> {
return this; 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); 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); return col + " NOT IN" + buildForeach(col, paramName);
} }

View File

@ -34,14 +34,14 @@ public abstract class SQL<T> extends AbstractSQL<T> {
return new MyBatisSql(withScript); return new MyBatisSql(withScript);
} }
public T WHERE(boolean condition, String sqlCondition) { public T WHERE(boolean condition, String sqlCondition) { // NOSONAR
if (condition) { if (condition) {
return WHERE(sqlCondition); return WHERE(sqlCondition);
} }
return getSelf(); 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); return WHERE(condition ? ifSqlCondition : elseSqlCondition);
} }
} }