删除 SQL 构造器。暂不考虑相关工具。

This commit is contained in:
2024-12-28 22:27:00 +08:00
parent 9ccaa2d1d6
commit 40302d83cf
6 changed files with 4 additions and 979 deletions

View File

@@ -1,72 +0,0 @@
/*
* Copyright 2023-2024 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 java.util.Collection;
import xyz.zhouxy.plusone.commons.util.StringTools;
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) { // NOSONAR
return IN(col, c.size());
}
public static <T> String IN(String col, T[] c) { // NOSONAR
return IN(col, c.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) { // NOSONAR
return NOT_IN(col, c.size());
}
public static <T> String NOT_IN(String col, T[] c) { // NOSONAR
return NOT_IN(col, c.length);
}
private static String NOT_IN(String col, int length) { // NOSONAR
if (length == 0) {
return "true";
}
return col + " NOT IN (" + buildQuestionsList(length) + ')';
}
private static String buildQuestionsList(int times) {
final int length = times <= 0 ? 0 : (times * 3 - 2);
return StringTools.repeat("?, ", times, length);
}
}

View File

@@ -1,73 +0,0 @@
/*
* Copyright 2023-2024 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 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) { // NOSONAR
return " " + col + " IN" + buildForeach(col, paramName);
}
public static String NOT_IN(String col, String paramName) { // NOSONAR
return col + " NOT IN" + buildForeach(col, paramName);
}
private static String buildForeach(String col, String paramName) {
final String format = "<foreach" +
" item=\"%s\"" +
" index=\"index\"" +
" collection=\"%s\"" +
" open=\"(\"" +
" separator=\",\"" +
" close=\")\"" +
">" +
"#{%s}" +
"</foreach>";
return String.format(format, col, paramName, col);
}
@Override
public String toString() {
if (withScript) {
return "<script>\n" + super.toString() + "\n</script>";
}
return super.toString();
}
}

View File

@@ -1,47 +0,0 @@
/*
* Copyright 2023-2024 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 <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
@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) { // NOSONAR
if (condition) {
return WHERE(sqlCondition);
}
return getSelf();
}
public T WHERE(boolean condition, String ifSqlCondition, String elseSqlCondition) { // NOSONAR
return WHERE(condition ? ifSqlCondition : elseSqlCondition);
}
}