forked from plusone/plusone-commons
移动类位置。
This commit is contained in:
66
src/main/java/xyz/zhouxy/plusone/commons/sql/JdbcSql.java
Normal file
66
src/main/java/xyz/zhouxy/plusone/commons/sql/JdbcSql.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package xyz.zhouxy.plusone.commons.sql;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class JdbcSql extends SQL<JdbcSql> {
|
||||
|
||||
JdbcSql() {
|
||||
super();
|
||||
}
|
||||
|
||||
public static JdbcSql newSql() {
|
||||
return new JdbcSql();
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
65
src/main/java/xyz/zhouxy/plusone/commons/sql/MyBatisSql.java
Normal file
65
src/main/java/xyz/zhouxy/plusone/commons/sql/MyBatisSql.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package xyz.zhouxy.plusone.commons.sql;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
@Beta
|
||||
public class MyBatisSql extends SQL<MyBatisSql> {
|
||||
|
||||
private final boolean withScript;
|
||||
|
||||
MyBatisSql(boolean withScript) {
|
||||
super();
|
||||
this.withScript = withScript;
|
||||
}
|
||||
|
||||
public static MyBatisSql newSql() {
|
||||
return new MyBatisSql(false);
|
||||
}
|
||||
|
||||
public static MyBatisSql newScriptSql() {
|
||||
return new MyBatisSql(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MyBatisSql getSelf() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public static String IN(String col, String paramName) {
|
||||
return new StringBuilder(" ")
|
||||
.append(col)
|
||||
.append(" IN")
|
||||
.append(buildQuestionsList(col, paramName))
|
||||
.toString();
|
||||
}
|
||||
|
||||
public static String NOT_IN(String col, String paramName) {
|
||||
return new StringBuilder()
|
||||
.append(col)
|
||||
.append(" NOT IN")
|
||||
.append(buildQuestionsList(col, paramName))
|
||||
.toString();
|
||||
}
|
||||
|
||||
private static String buildQuestionsList(String col, String paramName) {
|
||||
return new StringBuilder()
|
||||
.append("<foreach item=\"")
|
||||
.append(col)
|
||||
.append("\" index=\"index\" collection=\"")
|
||||
.append(paramName)
|
||||
.append("\" open=\"(\" separator=\",\" close=\")\">")
|
||||
.append("#{")
|
||||
.append(col)
|
||||
.append("}</foreach>")
|
||||
.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String str = super.toString();
|
||||
if (withScript) {
|
||||
str = "<script>\n" + str + "\n</script>";
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
47
src/main/java/xyz/zhouxy/plusone/commons/sql/SQL.java
Normal file
47
src/main/java/xyz/zhouxy/plusone/commons/sql/SQL.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2022-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.sql;
|
||||
|
||||
import org.apache.ibatis.jdbc.AbstractSQL;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
/**
|
||||
* @author ZhouXY
|
||||
*/
|
||||
@Beta
|
||||
public abstract class SQL<T> extends AbstractSQL<T> {
|
||||
|
||||
public static JdbcSql newJdbcSql() {
|
||||
return new JdbcSql();
|
||||
}
|
||||
|
||||
public static MyBatisSql newMyBatisSql(boolean withScript) {
|
||||
return new MyBatisSql(withScript);
|
||||
}
|
||||
|
||||
public T WHERE(boolean condition, String sqlCondition) {
|
||||
if (condition) {
|
||||
return WHERE(sqlCondition);
|
||||
}
|
||||
return getSelf();
|
||||
}
|
||||
|
||||
public T WHERE(boolean condition, String ifSqlCondition, String elseSqlCondition) {
|
||||
return WHERE(condition ? ifSqlCondition : elseSqlCondition);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user