forked from plusone/plusone-commons
【优化】将 Jdbc 的 SQL 构建放在 JdbcSql 中,后续可添加 SQL 的其它子类,用于构建 MyBatis 等不同的 SQL。
This commit is contained in:
62
src/main/java/xyz/zhouxy/plusone/commons/jdbc/JdbcSql.java
Normal file
62
src/main/java/xyz/zhouxy/plusone/commons/jdbc/JdbcSql.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package xyz.zhouxy.plusone.commons.jdbc;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class JdbcSql extends SQL<JdbcSql> {
|
||||
|
||||
JdbcSql() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JdbcSql getSelf() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public static String IN(String col, Collection<?> c) {
|
||||
return IN(col, c.size());
|
||||
}
|
||||
|
||||
public static <T> 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 <T> 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user