From f5e742cdd272bbf9277325b0e5673921feb0d524 Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Sun, 20 Jul 2025 23:07:01 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20=E5=88=A9=E7=94=A8jsqlpa?= =?UTF-8?q?rser=E6=96=B0=E5=A2=9E=E4=B8=80=E4=B8=AA=E6=96=B9=E4=BE=BF?= =?UTF-8?q?=E6=8B=BC=E8=A3=85sql=E7=9A=84=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1469817729548288]利用jsqlparser新增一个方便拼装sql的工具类 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1469817729548288 --- .../framework/sqlgenerator/$sql.java | 748 ++++++++++++++++++ .../framework/sqlgenerator/$sqlTest.java | 710 +++++++++++++++++ .../framework/sqlgenerator/ExpressionVo.java | 153 ++++ .../framework/sqlgenerator/FunctionVo.java | 72 ++ .../framework/sqlgenerator/JoinVo.java | 98 +++ .../framework/sqlgenerator/ValueVo.java | 113 +++ 6 files changed, 1894 insertions(+) create mode 100644 src/main/java/neatlogic/framework/sqlgenerator/$sql.java create mode 100644 src/main/java/neatlogic/framework/sqlgenerator/$sqlTest.java create mode 100644 src/main/java/neatlogic/framework/sqlgenerator/ExpressionVo.java create mode 100644 src/main/java/neatlogic/framework/sqlgenerator/FunctionVo.java create mode 100644 src/main/java/neatlogic/framework/sqlgenerator/JoinVo.java create mode 100644 src/main/java/neatlogic/framework/sqlgenerator/ValueVo.java diff --git a/src/main/java/neatlogic/framework/sqlgenerator/$sql.java b/src/main/java/neatlogic/framework/sqlgenerator/$sql.java new file mode 100644 index 000000000..ba52355ae --- /dev/null +++ b/src/main/java/neatlogic/framework/sqlgenerator/$sql.java @@ -0,0 +1,748 @@ +/* + * Copyright (C) 2025 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.framework.sqlgenerator; + +import neatlogic.framework.util.TimeUtil; +import net.sf.jsqlparser.expression.*; +import net.sf.jsqlparser.expression.operators.conditional.AndExpression; +import net.sf.jsqlparser.expression.operators.conditional.OrExpression; +import net.sf.jsqlparser.expression.operators.relational.*; +import net.sf.jsqlparser.schema.Column; +import net.sf.jsqlparser.schema.Table; +import net.sf.jsqlparser.statement.select.*; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Objects; + +/** + *

基本用法

+ *

PlainSelect plainSelect = $sql.from("tenant", "a");

+ *

$sql.addSelectColumn(plainSelect, "a.name");

+ *

$sql.addSelectColumn(plainSelect, $sql.fun("count", "b.module_group"), "moduleGroupCount");

+ *

$sql.addJoin(plainSelect, "left join", "tenant_modulegroup", "b", $sql.exp("b.tenant_uuid", "=", "a.uuid"));

+ *

$sql.addWhereExpression(plainSelect, $sql.exp("a.is_active", "=", 1));

+ *

$sql.addGroupBy(plainSelect, "a.name");

+ *

$sql.addOrderBy(plainSelect, "a.name", "desc");

+ *

$sql.setLimit(plainSelect, 3, 20);

+ *

输出结果:

+ *

SELECT

+ *

a.name,

+ *

count(b.module_group) AS moduleGroupCount

+ *

FROM tenant a

+ *

LEFT JOIN tenant_modulegroup b ON b.tenant_uuid = a.uuid

+ *

WHERE a.is_active = 1

+ *

GROUP BY a.name

+ *

ORDER BY a.name DESC

+ *

LIMIT 3, 20

+ */ +public class $sql { + // 布尔运算 + public static final String BOOLEAN_OPERATION = "booleanOperation"; + // 逻辑运算 + public static final String COMPARATIVE_OPERATION = "comparativeOperation"; + + public static JoinVo join(String operationSymbol, String schemaName, String tableName, String alias, ExpressionVo on) { + return new JoinVo(operationSymbol, schemaName, tableName, alias, on); + } + + public static JoinVo join(String operationSymbol, String tableName, String alias, ExpressionVo on) { + return new JoinVo(operationSymbol, null, tableName, alias, on); + } + + public static JoinVo join(String operationSymbol, String tableName, String alias) { + return new JoinVo(operationSymbol,null, tableName, alias, null); + } + + public static ExpressionVo exp(String leftParenthesis, ExpressionVo leftExpressionVo, String operationSymbol, ExpressionVo rightExpressionVo, String rightParenthesis) { + return new ExpressionVo(leftParenthesis, leftExpressionVo, operationSymbol, rightExpressionVo, rightParenthesis); + } + + public static ExpressionVo exp(ExpressionVo leftExpressionVo, String operationType, ExpressionVo rightExpressionVo) { + return exp(null, leftExpressionVo, operationType, rightExpressionVo, null); + } + + public static ExpressionVo exp(String leftColumn, String operationSymbol, String rightColumn) { + return new ExpressionVo(leftColumn, operationSymbol, rightColumn); + } + + public static ExpressionVo exp(String leftColumn, String operationSymbol, Integer rightValue) { + return new ExpressionVo(leftColumn, operationSymbol, new ValueVo(rightValue)); + } + + public static ExpressionVo exp(String leftColumn, String operationSymbol, Long rightValue) { + return new ExpressionVo(leftColumn, operationSymbol, new ValueVo(rightValue)); + } + + public static ExpressionVo exp(String leftColumn, String operationSymbol, Double rightValue) { + return new ExpressionVo(leftColumn, operationSymbol, new ValueVo(rightValue)); + } + + public static ExpressionVo exp(String leftColumn, String operationSymbol, List rightValue) { + return new ExpressionVo(leftColumn, operationSymbol, new ValueVo(rightValue)); + } + + public static ExpressionVo exp(String leftColumn, String operationSymbol, Date date) { + return new ExpressionVo(leftColumn, operationSymbol, new ValueVo(date)); + } + + public static ExpressionVo exp(String leftColumn, String operationSymbol, ValueVo rightValueExpression) { + return new ExpressionVo(leftColumn, operationSymbol, rightValueExpression); + } + + public static ExpressionVo exp(String leftColumn, String operationSymbol) { + return new ExpressionVo(leftColumn, operationSymbol, new ValueVo("")); + } + + public static ExpressionVo exp(Integer leftValue, String operationSymbol, Integer rightValue) { + return new ExpressionVo(new ValueVo(leftValue), operationSymbol, new ValueVo(rightValue)); + } + + public static ExpressionVo exp(Integer leftValue, String operationSymbol, Long rightValue) { + return new ExpressionVo(new ValueVo(leftValue), operationSymbol, new ValueVo(rightValue)); + } + + public static ExpressionVo exp(Integer leftValue, String operationSymbol, Double rightValue) { + return new ExpressionVo(new ValueVo(leftValue), operationSymbol, new ValueVo(rightValue)); + } + + public static ExpressionVo exp(Long leftValue, String operationSymbol, Integer rightValue) { + return new ExpressionVo(new ValueVo(leftValue), operationSymbol, new ValueVo(rightValue)); + } + + public static ExpressionVo exp(Long leftValue, String operationSymbol, Long rightValue) { + return new ExpressionVo(new ValueVo(leftValue), operationSymbol, new ValueVo(rightValue)); + } + + public static ExpressionVo exp(Long leftValue, String operationSymbol, Double rightValue) { + return new ExpressionVo(new ValueVo(leftValue), operationSymbol, new ValueVo(rightValue)); + } + + public static ExpressionVo exp(Double leftValue, String operationSymbol, Integer rightValue) { + return new ExpressionVo(new ValueVo(leftValue), operationSymbol, new ValueVo(rightValue)); + } + + public static ExpressionVo exp(Double leftValue, String operationSymbol, Long rightValue) { + return new ExpressionVo(new ValueVo(leftValue), operationSymbol, new ValueVo(rightValue)); + } + + public static ExpressionVo exp(Double leftValue, String operationSymbol, Double rightValue) { + return new ExpressionVo(new ValueVo(leftValue), operationSymbol, new ValueVo(rightValue)); + } + + public static ExpressionVo exp(ValueVo leftValueExpression, String operationSymbol, ValueVo rightValueExpression) { + return new ExpressionVo(leftValueExpression, operationSymbol, rightValueExpression); + } + + public static ValueVo value(String strValue) { + return new ValueVo(strValue); + } + + public static ValueVo value(Integer intValue) { + return new ValueVo(intValue); + } + + public static ValueVo value(Long longValue) { + return new ValueVo(longValue); + } + + public static ValueVo value(Double doubleValue) { + return new ValueVo(doubleValue); + } + + public static ValueVo value(List list) { + return new ValueVo(list); + } + + public static FunctionVo fun(String funName, Object ... parameters) { + return new FunctionVo(funName, parameters); + } + + public static void from(PlainSelect plainSelect, String tableName) { + if (StringUtils.isNotBlank(tableName)) { + String schemaName = null; + String alias = null; + tableName = tableName.trim(); + if (tableName.contains(".")) { + String[] split = tableName.split("\\."); + schemaName = split[0]; + tableName = split[1]; + } + if (tableName.contains(" ")) { + String[] split = tableName.split(" "); + tableName = split[0]; + alias = split[1]; + } + from(plainSelect, schemaName, tableName, alias); + } + } + + public static void from(PlainSelect plainSelect, String tableName, String alias) { + if (StringUtils.isNotBlank(tableName)) { + String schemaName = null; + tableName = tableName.trim(); + if (tableName.contains(".")) { + String[] split = tableName.split("\\."); + schemaName = split[0]; + tableName = split[1]; + } + from(plainSelect, schemaName, tableName, alias); + } + } + + public static void from(PlainSelect plainSelect, String schemaName, String tableName, String alias) { + if (StringUtils.isNotBlank(tableName)) { + Table table = new Table(tableName); + if (StringUtils.isNotBlank(schemaName)) { + table.withSchemaName(schemaName.trim()); + } + if (StringUtils.isNotBlank(alias)) { + table.withAlias(new Alias(alias.trim()).withUseAs(false)); + } + plainSelect.withFromItem(table); + } + } + + public static PlainSelect from(String tableName) { + PlainSelect plainSelect = new PlainSelect(); + from(plainSelect, tableName); + return plainSelect; + } + + public static PlainSelect from(String tableName, String alias) { + PlainSelect plainSelect = new PlainSelect(); + from(plainSelect, tableName, alias); + return plainSelect; + } + + public static PlainSelect from(String schemaName, String tableName, String alias) { + PlainSelect plainSelect = new PlainSelect(); + from(plainSelect, schemaName, tableName, alias); + return plainSelect; + } + + public static void addSelectColumn(PlainSelect plainSelect, String columnName) { + addSelectColumn(plainSelect, columnName, null); + } + + public static void addSelectColumn(PlainSelect plainSelect, String columnName, String alias) { + if (plainSelect == null || columnName == null) { + return; + } + SelectExpressionItem selectExpressionItem = new SelectExpressionItem(new Column(columnName.trim())); + if (StringUtils.isNotBlank(alias)) { + selectExpressionItem.withAlias(new Alias(alias)); + } + plainSelect.addSelectItems(selectExpressionItem); + } + + public static void setSelectColumn(PlainSelect plainSelect, String columnName) { + setSelectColumn(plainSelect, columnName, null); + } + + public static void setSelectColumn(PlainSelect plainSelect, String columnName, String alias) { + if (plainSelect == null || columnName == null) { + return; + } + SelectExpressionItem selectExpressionItem = new SelectExpressionItem(new Column(columnName.trim())); + if (StringUtils.isNotBlank(alias)) { + selectExpressionItem.withAlias(new Alias(alias)); + } + List selectItemList = new ArrayList<>(); + selectItemList.add(selectExpressionItem); + plainSelect.setSelectItems(selectItemList); + } + + public static void addSelectColumn(PlainSelect plainSelect, ValueVo valueVo) { + addSelectColumn(plainSelect, valueVo, null); + } + + public static void addSelectColumn(PlainSelect plainSelect, ValueVo valueVo, String alias) { + if (plainSelect == null || valueVo == null) { + return; + } + Object obj = parseValue(valueVo); + if (obj instanceof Expression) { + SelectExpressionItem selectExpressionItem = new SelectExpressionItem((Expression) obj); + if (StringUtils.isNotBlank(alias)) { + selectExpressionItem.withAlias(new Alias(alias)); + } + plainSelect.addSelectItems(selectExpressionItem); + } else if (obj instanceof ExpressionList) { + ExpressionList expressionList = (ExpressionList) obj; + List expressions = expressionList.getExpressions(); + if (CollectionUtils.isNotEmpty(expressions)) { + List selectItemList = new ArrayList<>(); + for (Expression expression : expressions) { + SelectExpressionItem selectExpressionItem = new SelectExpressionItem(expression); + if (StringUtils.isNotBlank(alias)) { + selectExpressionItem.withAlias(new Alias(alias)); + } + selectItemList.add(selectExpressionItem); + } + plainSelect.addSelectItems(selectItemList); + } + } + } + + public static void setSelectColumn(PlainSelect plainSelect, ValueVo valueVo) { + setSelectColumn(plainSelect, valueVo, null); + } + + public static void setSelectColumn(PlainSelect plainSelect, ValueVo valueVo, String alias) { + if (plainSelect == null || valueVo == null) { + return; + } + Object obj = parseValue(valueVo); + if (obj != null) { + if (obj instanceof Expression) { + SelectExpressionItem selectExpressionItem = new SelectExpressionItem((Expression) obj); + if (StringUtils.isNotBlank(alias)) { + selectExpressionItem.withAlias(new Alias(alias)); + } + List selectItemList = new ArrayList<>(); + selectItemList.add(selectExpressionItem); + plainSelect.setSelectItems(selectItemList); + } else if (obj instanceof ExpressionList) { + ExpressionList expressionList = (ExpressionList) obj; + List expressions = expressionList.getExpressions(); + if (CollectionUtils.isNotEmpty(expressions)) { + List selectItemList = new ArrayList<>(); + for (Expression expression : expressions) { + SelectExpressionItem selectExpressionItem = new SelectExpressionItem(expression); + if (StringUtils.isNotBlank(alias)) { + selectExpressionItem.withAlias(new Alias(alias)); + } + selectItemList.add(selectExpressionItem); + } + plainSelect.setSelectItems(selectItemList); + } + } + } + } + + public static void addSelectColumn(PlainSelect plainSelect, FunctionVo functionVo) { + addSelectColumn(plainSelect, functionVo, null); + } + + public static void addSelectColumn(PlainSelect plainSelect, FunctionVo functionVo, String alias) { + if (plainSelect == null || functionVo == null) { + return; + } + SelectExpressionItem selectExpressionItem = new SelectExpressionItem(parseFunction(functionVo)); + if (StringUtils.isNotBlank(alias)) { + selectExpressionItem.withAlias(new Alias(alias)); + } + plainSelect.addSelectItems(selectExpressionItem); + } + + public static void setSelectColumn(PlainSelect plainSelect, FunctionVo functionVo) { + setSelectColumn(plainSelect, functionVo, null); + } + + public static void setSelectColumn(PlainSelect plainSelect, FunctionVo functionVo, String alias) { + if (plainSelect == null || functionVo == null) { + return; + } + SelectExpressionItem selectExpressionItem = new SelectExpressionItem(parseFunction(functionVo)); + if (StringUtils.isNotBlank(alias)) { + selectExpressionItem.withAlias(new Alias(alias)); + } + List selectItemList = new ArrayList<>(); + selectItemList.add(selectExpressionItem); + plainSelect.setSelectItems(selectItemList); + } + + public static void addJoin(PlainSelect plainSelect, String operationSymbol, String schemaName, String tableName, String alias, ExpressionVo on) { + addJoin(plainSelect, new JoinVo(operationSymbol, schemaName, tableName, alias, on)); + } + + public static void addJoin(PlainSelect plainSelect, String operationSymbol, String tableName, String alias, ExpressionVo on) { + addJoin(plainSelect, new JoinVo(operationSymbol, null, tableName, alias, on)); + } + + public static void addJoin(PlainSelect plainSelect, JoinVo joinVo) { + if (plainSelect == null || joinVo == null) { + return; + } + Join join = new Join(); + if (Objects.equals(joinVo.getOperationSymbol().toLowerCase().trim(), "left join")) { + join.withLeft(true); + } + Table table = new Table(joinVo.getTableName().trim()); + if (StringUtils.isNotBlank(joinVo.getSchemaName())) { + table.withSchemaName(joinVo.getSchemaName().trim()); + } + if (StringUtils.isNotBlank(joinVo.getAlias())) { + table.withAlias(new Alias(joinVo.getAlias().trim()).withUseAs(false)); + } + join.withRightItem(table); + ExpressionVo on = joinVo.getOn(); + if (on != null) { + Expression expression = parseExpression(on); + if (expression != null) { + join.addOnExpression(expression); + } + } + plainSelect.addJoins(join); + } + + public static void addJoinList(PlainSelect plainSelect, List joinList) { + if (plainSelect == null || CollectionUtils.isEmpty(joinList)) { + return; + } + for (JoinVo joinVo : joinList) { + addJoin(plainSelect, joinVo); + } + } + + public static void addWhereExpression(PlainSelect plainSelect, ExpressionVo expressionVo) { + addWhereExpression(plainSelect, "and", expressionVo); + } + + public static void addWhereExpression(PlainSelect plainSelect, String operationSymbol, ExpressionVo expressionVo) { + if (plainSelect == null || expressionVo == null || operationSymbol == null) { + return; + } + Expression expression = parseExpression(expressionVo); + if (expression != null) { + Expression where = plainSelect.getWhere(); + if (where != null) { + if (Objects.equals(operationSymbol.toLowerCase().trim(), "or")) { + plainSelect.setWhere(new OrExpression(where, expression)); + } else { + plainSelect.setWhere(new AndExpression(where, expression)); + } + } else { + plainSelect.setWhere(expression); + } + } + } + + public static void addWhereExpressionList(PlainSelect plainSelect, String operationSymbol, List expressionList) { + if (plainSelect == null || CollectionUtils.isEmpty(expressionList) || operationSymbol == null) { + return; + } + for (ExpressionVo expressionVo : expressionList) { + addWhereExpression(plainSelect, operationSymbol, expressionVo); + } + } + + public static void addWhereExpressionList(PlainSelect plainSelect, List expressionList) { + addWhereExpressionList(plainSelect, "and", expressionList); + } + + public static void addGroupBy(PlainSelect plainSelect, String columnName) { + if (plainSelect == null || columnName == null) { + return; + } + plainSelect.addGroupByColumnReference(new Column(columnName)); + } + + public static void addGroupBy(PlainSelect plainSelect, Column column) { + if (plainSelect == null || column == null) { + return; + } + plainSelect.addGroupByColumnReference(column); + } + + public static void addOrderBy(PlainSelect plainSelect, String columnName) { + addOrderBy(plainSelect, columnName, null); + } + public static void addOrderBy(PlainSelect plainSelect, String columnName, String asc) { + if (plainSelect == null || columnName == null) { + return; + } + OrderByElement orderByElement = new OrderByElement().withExpression(new Column(columnName.trim())); + if (StringUtils.isNotBlank(asc) && Objects.equals(asc.trim().toLowerCase(), "desc")) { + orderByElement.withAsc(false); + } + plainSelect.addOrderByElements(orderByElement); + } + + public static void addOrderBy(PlainSelect plainSelect, FunctionVo functionVo) { + addOrderBy(plainSelect, functionVo, "asc"); + } + + public static void addOrderBy(PlainSelect plainSelect, FunctionVo functionVo, String asc) { + if (plainSelect == null || functionVo == null || asc == null) { + return; + } + OrderByElement orderByElement = new OrderByElement().withExpression(parseFunction(functionVo)); + if (Objects.equals(asc.trim().toLowerCase(), "desc")) { + orderByElement.withAsc(false); + } + plainSelect.addOrderByElements(orderByElement); + } + + public static void setLimit(PlainSelect plainSelect, int startNum, int pageSize) { + if (plainSelect == null) { + return; + } + plainSelect.setLimit(new Limit().withOffset(new LongValue(startNum)).withRowCount(new LongValue(pageSize))); + } + + public static void setLimit(PlainSelect plainSelect, int pageSize) { + setLimit(plainSelect, 0, pageSize); + } + + private static Expression parseExpression(ExpressionVo expressionVo) { + Expression resultExpression = null; + String operationSymbol = expressionVo.getOperationSymbol().toLowerCase().trim(); + if (Objects.equals(expressionVo.getType(), BOOLEAN_OPERATION)) { + if (Objects.equals(operationSymbol, "and")) { + ExpressionVo leftExpression = expressionVo.getLeftExpression(); + ExpressionVo rightExpression = expressionVo.getRightExpression(); + if (leftExpression != null && rightExpression != null) { + Expression expression1 = parseExpression(leftExpression); + Expression expression2 = parseExpression(rightExpression); + resultExpression = new AndExpression(expression1, expression2); + } else if (leftExpression != null) { + resultExpression = parseExpression(leftExpression); + } else if (rightExpression != null) { + resultExpression = parseExpression(rightExpression); + } + } else if (Objects.equals(operationSymbol, "or")) { + ExpressionVo leftExpression = expressionVo.getLeftExpression(); + ExpressionVo rightExpression = expressionVo.getRightExpression(); + if (leftExpression != null && rightExpression != null) { + Expression expression1 = parseExpression(leftExpression); + Expression expression2 = parseExpression(rightExpression); + resultExpression = new OrExpression(expression1, expression2); + } else if (leftExpression != null) { + resultExpression = parseExpression(leftExpression); + } else if (rightExpression != null) { + resultExpression = parseExpression(rightExpression); + } + } + } else if (Objects.equals(expressionVo.getType(), COMPARATIVE_OPERATION)) { + Expression leftExpression = null; + if (StringUtils.isNotBlank(expressionVo.getLeftColumn())) { + leftExpression = new Column(expressionVo.getLeftColumn()); + } else if (expressionVo.getLeftValueExpression() != null) { + Object leftObj = parseValue(expressionVo.getLeftValueExpression()); + if (leftObj instanceof Expression) { + leftExpression = (Expression) leftObj; + } + } + Expression rightExpression = null; + ExpressionList rightExpressionList = null; + String rightColumn = expressionVo.getRightColumn(); + if (StringUtils.isNotBlank(rightColumn)) { + rightExpression = new Column(rightColumn); + } else if (expressionVo.getRightValueExpression() != null) { + Object rightObj = parseValue(expressionVo.getRightValueExpression()); + if (rightObj instanceof Expression) { + rightExpression = (Expression) rightObj; + } else if (rightObj instanceof ExpressionList) { + rightExpressionList = (ExpressionList) rightObj; + } + } + if (Objects.equals(operationSymbol, "=")) { + resultExpression = new EqualsTo(leftExpression, rightExpression); + } else if (Objects.equals(operationSymbol, "!=")) { + resultExpression = new NotEqualsTo(leftExpression, rightExpression); + } else if (Objects.equals(operationSymbol, ">")) { + resultExpression = new GreaterThan().withLeftExpression(leftExpression).withRightExpression(rightExpression); + } else if (Objects.equals(operationSymbol, ">=")) { + resultExpression = new GreaterThanEquals().withLeftExpression(leftExpression).withRightExpression(rightExpression); + } else if (Objects.equals(operationSymbol, "<")) { + resultExpression = new MinorThan().withLeftExpression(leftExpression).withRightExpression(rightExpression); + } else if (Objects.equals(operationSymbol, "<=")) { + resultExpression = new MinorThanEquals().withLeftExpression(leftExpression).withRightExpression(rightExpression); + } else if (Objects.equals(operationSymbol, "like")) { + resultExpression = new LikeExpression().withLeftExpression(leftExpression).withRightExpression(rightExpression); + } else if (Objects.equals(operationSymbol, "not like")) { + resultExpression = new LikeExpression().withNot(true).withLeftExpression(leftExpression).withRightExpression(rightExpression); + } else if (Objects.equals(operationSymbol, "in")) { + if (rightExpressionList == null) { + if (StringUtils.isNotBlank(rightColumn)) { + rightExpressionList = string2ExpressionList(rightColumn); + } else if (rightExpression != null) { + rightExpressionList = new ExpressionList(); + rightExpressionList.addExpressions(rightExpression); + } + } + resultExpression = new InExpression(leftExpression, rightExpressionList); + } else if (Objects.equals(operationSymbol, "not in")) { + if (rightExpressionList == null) { + if (StringUtils.isNotBlank(rightColumn)) { + rightExpressionList = string2ExpressionList(rightColumn); + } else if (rightExpression != null) { + rightExpressionList = new ExpressionList(); + rightExpressionList.addExpressions(rightExpression); + } + } + resultExpression = new InExpression(leftExpression, rightExpressionList).withNot(true); + } else if (Objects.equals(operationSymbol, "is null")) { + resultExpression = new IsNullExpression().withLeftExpression(leftExpression); + } else if (Objects.equals(operationSymbol, "is not null")) { + resultExpression = new IsNullExpression().withLeftExpression(leftExpression).withNot(true); + } + } + if (resultExpression != null) { + if (expressionVo.getLeftParenthesis() != null && expressionVo.getRightParenthesis() != null) { + if (Objects.equals(expressionVo.getLeftParenthesis().trim(), "(") + && Objects.equals(expressionVo.getRightParenthesis().trim(), ")")) { + return new Parenthesis(resultExpression); + } + } + return resultExpression; + } + return null; + } + + private static ExpressionList string2ExpressionList(String expressionListStr) { + if (StringUtils.isNotBlank(expressionListStr)) { + ExpressionList expressionList = new ExpressionList(); + expressionListStr = expressionListStr.trim(); + if (expressionListStr.startsWith("(") && expressionListStr.endsWith(")")) { + expressionListStr = expressionListStr.substring(1, expressionListStr.length() - 1); + if (expressionListStr.contains(",")) { + String[] split = expressionListStr.split(","); + for (String str : split) { + str = str.trim(); + if (NumberUtils.isCreatable(str)) { + expressionList.addExpressions(new DoubleValue(str)); + } else { + expressionList.addExpressions(new StringValue(str)); + } + } + } else { + if (NumberUtils.isCreatable(expressionListStr)) { + expressionList.addExpressions(new DoubleValue(expressionListStr)); + } else { + expressionList.addExpressions(new StringValue(expressionListStr)); + } + } + } else { + expressionList.addExpressions(new StringValue(expressionListStr)); + } + return expressionList; + } + return null; + } + + private static Object parseValue(ValueVo valueVo) { + if (valueVo.getStrValue() != null) { + return new StringValue(valueVo.getStrValue()); + } else if (valueVo.getIntValue() != null) { + return new LongValue(valueVo.getIntValue()); + } else if (valueVo.getLongValue() != null) { + return new LongValue(valueVo.getLongValue()); + } else if (valueVo.getDoubleValue() != null) { + return new DoubleValue(valueVo.getDoubleValue().toString()); + } else if (valueVo.getDate() != null) { + return new StringValue(new SimpleDateFormat(TimeUtil.YYYY_MM_DD_HH_MM_SS).format(valueVo.getDate())); + } else if (valueVo.getList() != null) { + ExpressionList expressionList = new ExpressionList(); + List list = valueVo.getList(); + for (Object obj : list) { + if (obj != null) { + if (obj instanceof String) { + expressionList.addExpressions(new StringValue((String) obj)); + } else if (obj instanceof Integer) { + expressionList.addExpressions(new LongValue((Integer) obj)); + } else if (obj instanceof Long) { + expressionList.addExpressions(new LongValue((Long) obj)); + } else if (obj instanceof Double) { + expressionList.addExpressions(new DoubleValue().withValue((Double) obj)); + } + } + } + return expressionList; + } + return null; + } + + private static Function parseFunction(FunctionVo functionVo) { + if (functionVo != null) { + Function function = new Function(); + function.withName(functionVo.getName()); + function.withDistinct(functionVo.isDistinct()); + Object[] parameters = functionVo.getParameters(); + if (parameters != null && parameters.length > 0) { + ExpressionList expressionList = new ExpressionList(); + for (Object parameter : parameters) { + if (parameter != null) { + if (parameter instanceof String) { + expressionList.addExpressions(new Column((String) parameter)); + } else if (parameter instanceof Integer) { + Object obj = parseValue(new ValueVo((Integer) parameter)); + if (obj instanceof Expression) { + expressionList.addExpressions((Expression) obj); + } + } else if (parameter instanceof Long) { + Object obj = parseValue(new ValueVo((Long) parameter)); + if (obj instanceof Expression) { + expressionList.addExpressions((Expression) obj); + } + } else if (parameter instanceof Double) { + Object obj = parseValue(new ValueVo((Double) parameter)); + if (obj instanceof Expression) { + expressionList.addExpressions((Expression) obj); + } + } else if (parameter instanceof Date) { + Object obj = parseValue(new ValueVo((Date) parameter)); + if (obj instanceof Expression) { + expressionList.addExpressions((Expression) obj); + } + } else if (parameter instanceof List) { + Object obj = parseValue(new ValueVo((List) parameter)); + if (obj instanceof ExpressionList) { + expressionList.addExpressions(((ExpressionList) obj).getExpressions()); + } + } else if (parameter instanceof ValueVo) { + Object obj = parseValue((ValueVo) parameter); + if (obj instanceof Expression) { + expressionList.addExpressions((Expression) obj); + } else if (obj instanceof ExpressionList) { + expressionList.addExpressions(((ExpressionList) obj).getExpressions()); + } + } else if (parameter instanceof ExpressionVo) { + Expression expression = parseExpression((ExpressionVo) parameter); + if (expression != null) { + expressionList.addExpressions(expression); + } + } else if (parameter instanceof FunctionVo) { + Function function1 = parseFunction((FunctionVo) parameter); + if (function1 != null) { + expressionList.addExpressions(function1); + } + } else if (parameter instanceof Expression) { + expressionList.addExpressions((Expression) parameter); + } else { + Object obj = parseValue(new ValueVo(parameter.toString())); + if (obj instanceof Expression) { + expressionList.addExpressions((Expression) obj); + } + } + } + } + function.setParameters(expressionList); + } + return function; + } + return null; + } +} diff --git a/src/main/java/neatlogic/framework/sqlgenerator/$sqlTest.java b/src/main/java/neatlogic/framework/sqlgenerator/$sqlTest.java new file mode 100644 index 000000000..590be9068 --- /dev/null +++ b/src/main/java/neatlogic/framework/sqlgenerator/$sqlTest.java @@ -0,0 +1,710 @@ +/* + * Copyright (C) 2025 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.framework.sqlgenerator; + +import net.sf.jsqlparser.schema.Column; +import net.sf.jsqlparser.statement.select.PlainSelect; +import org.apache.commons.collections4.CollectionUtils; +import org.springframework.util.Assert; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.*; + +public class $sqlTest { + + public static void main(String[] args) { + List methodNameList = getMethodNameList($sql.class); + testForm(methodNameList); + testSetSelectColumn(methodNameList); + testAddSelectColumn(methodNameList); + testAddJoin(methodNameList); + testAddWhereExpressionList(methodNameList); + testAddGroupBy(methodNameList); + testAddOrderBy(methodNameList); + testSetLimit(methodNameList); + testValue(methodNameList); + testExp(methodNameList); + testFun(methodNameList); + if (CollectionUtils.isNotEmpty(methodNameList)) { + System.out.println("$sql类中以下方法没有被测试:"); + for (String methodName : methodNameList) { + System.out.println(methodName); + } + } else { + System.out.println("$sql类中所有public static方法测试成功"); + } +// PlainSelect plainSelect = $sql.from("tenant", "a"); +// $sql.addSelectColumn(plainSelect, "a.name"); +// $sql.addSelectColumn(plainSelect, $sql.fun("count", "b.module_group"), "moduleGroupCount"); +// $sql.addJoin(plainSelect, "left join", "tenant_modulegroup", "b", $sql.exp("b.tenant_uuid", "=", "a.uuid")); +// $sql.addWhereExpression(plainSelect, $sql.exp("a.is_active", "=", 1)); +// $sql.addGroupBy(plainSelect, "a.name"); +// $sql.addOrderBy(plainSelect, "a.name", "desc"); +// $sql.setLimit(plainSelect, 3, 20); +// System.out.println("plainSelect = " + plainSelect); + } + + private static void testForm(List methodNameList) { + // public static PlainSelect from(String) + Assert.isTrue(Objects.equals($sql.from("tenant").toString(), "SELECT FROM tenant"), "测试失败"); + Assert.isTrue(Objects.equals($sql.from("neatlogic.tenant").toString(), "SELECT FROM neatlogic.tenant"), "测试失败"); + Assert.isTrue(Objects.equals($sql.from("tenant a").toString(), "SELECT FROM tenant a"), "测试失败"); + Assert.isTrue(Objects.equals($sql.from("neatlogic.tenant a").toString(), "SELECT FROM neatlogic.tenant a"), "测试失败"); + + methodNameList.remove("public static PlainSelect from(String)"); + + // public static PlainSelect from(String, String) + Assert.isTrue(Objects.equals($sql.from("tenant", "a").toString(), "SELECT FROM tenant a"), "测试失败"); + Assert.isTrue(Objects.equals($sql.from("neatlogic.tenant", "a").toString(), "SELECT FROM neatlogic.tenant a"), "测试失败"); + + methodNameList.remove("public static PlainSelect from(String, String)"); + + // public static PlainSelect from(String, String, String) + Assert.isTrue(Objects.equals($sql.from("neatlogic", "tenant", "a").toString(), "SELECT FROM neatlogic.tenant a"), "测试失败"); + methodNameList.remove("public static PlainSelect from(String, String, String)"); + + PlainSelect plainSelect = new PlainSelect(); + // public static void from(PlainSelect, String) + $sql.from(plainSelect, "tenant"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant"), "测试失败"); + + $sql.from(plainSelect, "neatlogic.tenant"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM neatlogic.tenant"), "测试失败"); + + $sql.from(plainSelect, "tenant a"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant a"), "测试失败"); + + $sql.from(plainSelect, "neatlogic.tenant a"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM neatlogic.tenant a"), "测试失败"); + + methodNameList.remove("public static void from(PlainSelect, String)"); + + // public static void from(PlainSelect, String, String) + $sql.from(plainSelect, "tenant", "a"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant a"), "测试失败"); + + $sql.from(plainSelect, "neatlogic.tenant", "a"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM neatlogic.tenant a"), "测试失败"); + + methodNameList.remove("public static void from(PlainSelect, String, String)"); + + // public static void from(PlainSelect, String, String, String) + $sql.from(plainSelect, "neatlogic", "tenant", "a"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM neatlogic.tenant a"), "测试失败"); + + methodNameList.remove("public static void from(PlainSelect, String, String, String)"); + } + + private static void testSetSelectColumn(List methodNameList) { + PlainSelect plainSelect = $sql.from("tenant"); +// public static void setSelectColumn(PlainSelect, String) + $sql.setSelectColumn(plainSelect, "name"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT name FROM tenant"), "测试失败"); + + methodNameList.remove("public static void setSelectColumn(PlainSelect, String)"); + +// public static void setSelectColumn(PlainSelect, String, String) + $sql.setSelectColumn(plainSelect, "name", "tenantName"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT name AS tenantName FROM tenant"), "测试失败"); + + methodNameList.remove("public static void setSelectColumn(PlainSelect, String, String)"); + +// public static void setSelectColumn(PlainSelect, ValueVo) + $sql.setSelectColumn(plainSelect, $sql.value("abc")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 'abc' FROM tenant"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.value(10)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 10 FROM tenant"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.value(100L)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 100 FROM tenant"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.value(0.25)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 0.25 FROM tenant"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.value(Arrays.asList("bcd", "cde"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 'bcd', 'cde' FROM tenant"), "测试失败"); + + methodNameList.remove("public static void setSelectColumn(PlainSelect, ValueVo)"); + +// public static void setSelectColumn(PlainSelect, ValueVo, String) + $sql.setSelectColumn(plainSelect, $sql.value("abc"), "constant"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 'abc' AS constant FROM tenant"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.value(10), "constant"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 10 AS constant FROM tenant"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.value(100L), "constant"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 100 AS constant FROM tenant"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.value(0.25), "constant"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 0.25 AS constant FROM tenant"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.value(Arrays.asList("bcd", "cde")), "constant"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 'bcd' AS constant, 'cde' AS constant FROM tenant"), "测试失败"); + methodNameList.remove("public static void setSelectColumn(PlainSelect, ValueVo, String)"); +// public static void setSelectColumn(PlainSelect, FunctionVo) + $sql.setSelectColumn(plainSelect, $sql.fun("count", "name")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT count(name) FROM tenant"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("count", $sql.value(1))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT count(1) FROM tenant"), "测试失败"); + methodNameList.remove("public static void setSelectColumn(PlainSelect, FunctionVo)"); +// public static void setSelectColumn(PlainSelect, FunctionVo, String) + $sql.setSelectColumn(plainSelect, $sql.fun("count", "name"), "countName"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT count(name) AS countName FROM tenant"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("count", $sql.value(1)), "count"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT count(1) AS count FROM tenant"), "测试失败"); + methodNameList.remove("public static void setSelectColumn(PlainSelect, FunctionVo, String)"); + } + + private static void testAddSelectColumn(List methodNameList) { +// public static void addSelectColumn(PlainSelect, String) + PlainSelect plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, "name"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, name FROM tenant"), "测试失败"); + + methodNameList.remove("public static void addSelectColumn(PlainSelect, String)"); +// public static void addSelectColumn(PlainSelect, String, String) + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, "name", "tenantName"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, name AS tenantName FROM tenant"), "测试失败"); + + methodNameList.remove("public static void addSelectColumn(PlainSelect, String, String)"); +// public static void addSelectColumn(PlainSelect, ValueVo) + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.value("abc")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, 'abc' FROM tenant"), "测试失败"); + + + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.value(10)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, 10 FROM tenant"), "测试失败"); + + + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.value(100L)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, 100 FROM tenant"), "测试失败"); + + + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.value(0.25)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, 0.25 FROM tenant"), "测试失败"); + + + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.value(Arrays.asList("bcd", "cde"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, 'bcd', 'cde' FROM tenant"), "测试失败"); + + methodNameList.remove("public static void addSelectColumn(PlainSelect, ValueVo)"); +// public static void addSelectColumn(PlainSelect, ValueVo, String) + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.value("abc"), "constant"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, 'abc' AS constant FROM tenant"), "测试失败"); + + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.value(10), "constant"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, 10 AS constant FROM tenant"), "测试失败"); + + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.value(100L), "constant"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, 100 AS constant FROM tenant"), "测试失败"); + + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.value(0.25), "constant"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, 0.25 AS constant FROM tenant"), "测试失败"); + + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.value(Arrays.asList("bcd", "cde")), "constant"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, 'bcd' AS constant, 'cde' AS constant FROM tenant"), "测试失败"); + methodNameList.remove("public static void addSelectColumn(PlainSelect, ValueVo, String)"); + +// public static void addSelectColumn(PlainSelect, FunctionVo) + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.fun("count", "name")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, count(name) FROM tenant"), "测试失败"); + + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.fun("count", $sql.value(1))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, count(1) FROM tenant"), "测试失败"); + methodNameList.remove("public static void addSelectColumn(PlainSelect, FunctionVo)"); +// public static void addSelectColumn(PlainSelect, FunctionVo, String) + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.fun("count", "name"), "countName"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, count(name) AS countName FROM tenant"), "测试失败"); + + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.fun("count", $sql.value(1)), "count"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, count(1) AS count FROM tenant"), "测试失败"); + methodNameList.remove("public static void addSelectColumn(PlainSelect, FunctionVo, String)"); + } + + private static void testAddJoin(List methodNameList) { +// public static void addJoin(PlainSelect, String, String, String, String, ExpressionVo) + PlainSelect plainSelect = $sql.from("tenant", "a"); + $sql.addJoin(plainSelect, "left join", "neatlogic", "tenant_modulegroup", "b", $sql.exp("b.tenant_uuid", "=", "a.uuid")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant a LEFT JOIN neatlogic.tenant_modulegroup b ON b.tenant_uuid = a.uuid"), "测试失败"); + methodNameList.remove("public static void addJoin(PlainSelect, String, String, String, String, ExpressionVo)"); +// public static JoinVo join(String, String, String, String, ExpressionVo) + plainSelect = $sql.from("tenant", "a"); + $sql.addJoin(plainSelect, $sql.join("left join", "neatlogic", "tenant_modulegroup", "b", $sql.exp("b.tenant_uuid", "=", "a.uuid"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant a LEFT JOIN neatlogic.tenant_modulegroup b ON b.tenant_uuid = a.uuid"), "测试失败"); + methodNameList.remove("public static JoinVo join(String, String, String, String, ExpressionVo)"); + +// public static void addJoin(PlainSelect, String, String, String, ExpressionVo) + plainSelect = $sql.from("tenant", "a"); + $sql.addJoin(plainSelect, "left join", "tenant_modulegroup", "b", $sql.exp("b.tenant_uuid", "=", "a.uuid")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant a LEFT JOIN tenant_modulegroup b ON b.tenant_uuid = a.uuid"), "测试失败"); + methodNameList.remove("public static void addJoin(PlainSelect, String, String, String, ExpressionVo)"); +// public static JoinVo join(String, String, String, ExpressionVo) +// public static void addJoin(PlainSelect, JoinVo) + plainSelect = $sql.from("tenant", "a"); + $sql.addJoin(plainSelect, $sql.join("left join", "tenant_modulegroup", "b", $sql.exp("b.tenant_uuid", "=", "a.uuid"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant a LEFT JOIN tenant_modulegroup b ON b.tenant_uuid = a.uuid"), "测试失败"); + methodNameList.remove("public static JoinVo join(String, String, String, ExpressionVo)"); + methodNameList.remove("public static void addJoin(PlainSelect, JoinVo)"); +// public static JoinVo join(String, String, String) +// public static void addJoinList(PlainSelect, List) + plainSelect = $sql.from("tenant", "a"); + List joinList = new ArrayList<>(); + joinList.add($sql.join("left join", "tenant_modulegroup", "b")); + joinList.add($sql.join("left join", "tenant_module", "c")); + $sql.addJoinList(plainSelect, joinList); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant a LEFT JOIN tenant_modulegroup b LEFT JOIN tenant_module c"), "测试失败"); + methodNameList.remove("public static JoinVo join(String, String, String)"); + methodNameList.remove("public static void addJoinList(PlainSelect, List)"); + } + + private static void testAddWhereExpressionList(List methodNameList) { +// public static void addWhereExpression(PlainSelect, ExpressionVo) + PlainSelect plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("name", "=", $sql.value("demo"))); + $sql.addWhereExpression(plainSelect, $sql.exp("is_active", "=", $sql.value(1))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE name = 'demo' AND is_active = 1"), "测试失败"); + methodNameList.remove("public static void addWhereExpression(PlainSelect, ExpressionVo)"); +// public static void addWhereExpression(PlainSelect, String, ExpressionVo) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("name", "=", $sql.value("demo"))); + $sql.addWhereExpression(plainSelect, "or", $sql.exp("is_active", "=", $sql.value(1))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE name = 'demo' OR is_active = 1"), "测试失败"); + methodNameList.remove("public static void addWhereExpression(PlainSelect, String, ExpressionVo)"); + +// public static void addWhereExpressionList(PlainSelect, List) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("name", "=", $sql.value("demo"))); + List expressionList = new ArrayList<>(); + expressionList.add($sql.exp("is_active", "=", $sql.value(1))); + expressionList.add($sql.exp("status", "=", $sql.value("built"))); + $sql.addWhereExpressionList(plainSelect, expressionList); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE name = 'demo' AND is_active = 1 AND status = 'built'"), "测试失败"); + methodNameList.remove("public static void addWhereExpressionList(PlainSelect, List)"); + +// public static void addWhereExpressionList(PlainSelect, String, List) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("name", "=", $sql.value("demo"))); + expressionList = new ArrayList<>(); + expressionList.add($sql.exp("is_active", "=", $sql.value(1))); + expressionList.add($sql.exp("status", "=", $sql.value("built"))); + $sql.addWhereExpressionList(plainSelect, "or", expressionList); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE name = 'demo' OR is_active = 1 OR status = 'built'"), "测试失败"); + methodNameList.remove("public static void addWhereExpressionList(PlainSelect, String, List)"); + } + + private static void testAddGroupBy(List methodNameList) { +// public static void addGroupBy(PlainSelect, String) + PlainSelect plainSelect = $sql.from("tenant"); + $sql.addGroupBy(plainSelect, "name"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant GROUP BY name"), "测试失败"); + $sql.addGroupBy(plainSelect, "status"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant GROUP BY name, status"), "测试失败"); + methodNameList.remove("public static void addGroupBy(PlainSelect, String)"); +// public static void addGroupBy(PlainSelect, Column) + plainSelect = $sql.from("tenant"); + $sql.addGroupBy(plainSelect, new Column("name")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant GROUP BY name"), "测试失败"); + $sql.addGroupBy(plainSelect, new Column("status")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant GROUP BY name, status"), "测试失败"); + methodNameList.remove("public static void addGroupBy(PlainSelect, Column)"); + } + + private static void testAddOrderBy(List methodNameList) { +// public static void addOrderBy(PlainSelect, String) + PlainSelect plainSelect = $sql.from("tenant"); + $sql.addOrderBy(plainSelect, "name"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant ORDER BY name"), "测试失败"); + $sql.addOrderBy(plainSelect, "status"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant ORDER BY name, status"), "测试失败"); + methodNameList.remove("public static void addOrderBy(PlainSelect, String)"); +// public static void addOrderBy(PlainSelect, String, String) + plainSelect = $sql.from("tenant"); + $sql.addOrderBy(plainSelect, "name", "asc"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant ORDER BY name"), "测试失败"); + $sql.addOrderBy(plainSelect, "status", "desc"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant ORDER BY name, status DESC"), "测试失败"); + methodNameList.remove("public static void addOrderBy(PlainSelect, String, String)"); +// public static void addOrderBy(PlainSelect, FunctionVo) + plainSelect = $sql.from("tenant", "a"); + $sql.addSelectColumn(plainSelect, "a.name"); + $sql.addSelectColumn(plainSelect, $sql.fun("count", "a.name")); + $sql.addGroupBy(plainSelect, "a.name"); + $sql.addOrderBy(plainSelect, $sql.fun("count", "a.name")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT a.name, count(a.name) FROM tenant a GROUP BY a.name ORDER BY count(a.name)"), "测试失败"); + methodNameList.remove("public static void addOrderBy(PlainSelect, FunctionVo)"); +// public static void addOrderBy(PlainSelect, FunctionVo, String) + plainSelect = $sql.from("tenant", "a"); + $sql.addSelectColumn(plainSelect, "a.name"); + $sql.addSelectColumn(plainSelect, $sql.fun("count", "a.name")); + $sql.addGroupBy(plainSelect, "a.name"); + $sql.addOrderBy(plainSelect, $sql.fun("count", "a.name"), "desc"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT a.name, count(a.name) FROM tenant a GROUP BY a.name ORDER BY count(a.name) DESC"), "测试失败"); + methodNameList.remove("public static void addOrderBy(PlainSelect, FunctionVo, String)"); + + } + + private static void testSetLimit(List methodNameList) { + PlainSelect plainSelect = $sql.from("tenant"); + $sql.addSelectColumn(plainSelect, "*"); +// public static void setLimit(PlainSelect, int) + $sql.setLimit(plainSelect, 20); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT * FROM tenant LIMIT 0, 20"), "测试失败"); + methodNameList.remove("public static void setLimit(PlainSelect, int)"); +// public static void setLimit(PlainSelect, int, int) + $sql.setLimit(plainSelect, 10, 20); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT * FROM tenant LIMIT 10, 20"), "测试失败"); + methodNameList.remove("public static void setLimit(PlainSelect, int, int)"); + + } + + private static void testValue(List methodNameList) { + PlainSelect plainSelect = $sql.from("tenant"); +// public static ValueVo value(String) + $sql.setSelectColumn(plainSelect, $sql.value("abc")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 'abc' FROM tenant"), "测试失败"); + methodNameList.remove("public static ValueVo value(String)"); +// public static ValueVo value(Integer) + $sql.setSelectColumn(plainSelect, $sql.value(10)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 10 FROM tenant"), "测试失败"); + methodNameList.remove("public static ValueVo value(Integer)"); +// public static ValueVo value(Long) + $sql.setSelectColumn(plainSelect, $sql.value(100L)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 100 FROM tenant"), "测试失败"); + methodNameList.remove("public static ValueVo value(Long)"); +// public static ValueVo value(Double) + $sql.setSelectColumn(plainSelect, $sql.value(0.25)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 0.25 FROM tenant"), "测试失败"); + methodNameList.remove("public static ValueVo value(Double)"); +// public static ValueVo value(List) + $sql.setSelectColumn(plainSelect, $sql.value(Arrays.asList("bcd", "cde"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT 'bcd', 'cde' FROM tenant"), "测试失败"); + methodNameList.remove("public static ValueVo value(List)"); + } + + private static void testExp(List methodNameList) { +// public static ExpressionVo exp(String, String) + PlainSelect plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("name", "is null")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE name IS NULL"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(String, String)"); +// public static ExpressionVo exp(String, String, String) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("name", "=", "uuid")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE name = uuid"), "测试失败"); + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("name", "=", "'demo'")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE name = 'demo'"), "测试失败"); + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("is_active", "=", "1")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE is_active = 1"), "测试失败"); + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("is_active", "=", "0.99")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE is_active = 0.99"), "测试失败"); + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("is_active", "in", "(0, 1)")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE is_active IN (0, 1)"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(String, String, String)"); +// public static ExpressionVo exp(String, String, Integer) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("is_active", "=", 0)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE is_active = 0"), "测试失败"); + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("is_active", "in", 0)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE is_active IN (0)"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(String, String, Integer)"); +// public static ExpressionVo exp(String, String, Long) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("is_active", "=", 1L)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE is_active = 1"), "测试失败"); + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("is_active", "in", 1L)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE is_active IN (1)"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(String, String, Long)"); +// public static ExpressionVo exp(String, String, Double) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("is_active", "=", 0.99)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE is_active = 0.99"), "测试失败"); + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("is_active", "in", 0.99)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE is_active IN (0.99)"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(String, String, Double)"); +// public static ExpressionVo exp(String, String, List) + plainSelect = $sql.from("tenant"); + List list = new ArrayList<>(); + list.add(0); + list.add(1); + $sql.addWhereExpression(plainSelect, $sql.exp("is_active", "in", list)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE is_active IN (0, 1)"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(String, String, List)"); +// public static ExpressionVo exp(String, String, Date) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("fcd", "=", new Date(1752993694062L))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE fcd = '2025-07-20 14:41:34'"), "测试失败"); + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("fcd", "in", new Date(1752993694062L))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE fcd IN ('2025-07-20 14:41:34')"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(String, String, Date)"); +// public static ExpressionVo exp(Integer, String, Integer) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp(0, "=", 1)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE 0 = 1"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(Integer, String, Integer)"); +// public static ExpressionVo exp(Integer, String, Long) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp(0, "=", 2L)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE 0 = 2"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(Integer, String, Long)"); +// public static ExpressionVo exp(Integer, String, Double) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp(0, "=", 0.99)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE 0 = 0.99"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(Integer, String, Double)"); +// public static ExpressionVo exp(Long, String, Integer) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp(3L, "=", 0)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE 3 = 0"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(Long, String, Integer)"); +// public static ExpressionVo exp(Long, String, Long) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp(3L, "=", 4L)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE 3 = 4"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(Long, String, Long)"); +// public static ExpressionVo exp(Long, String, Double) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp(3L, "=", 1.68)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE 3 = 1.68"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(Long, String, Double)"); +// public static ExpressionVo exp(Double, String, Integer) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp(1.68, "=", 1)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE 1.68 = 1"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(Double, String, Integer)"); +// public static ExpressionVo exp(Double, String, Long) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp(1.68, "=", 5L)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE 1.68 = 5"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(Double, String, Long)"); +// public static ExpressionVo exp(Double, String, Double) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp(1.68, "=", 1.68)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE 1.68 = 1.68"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(Double, String, Double)"); +// public static ExpressionVo exp(String, String, ValueVo) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("name", "=", $sql.value("demo"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE name = 'demo'"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(String, String, ValueVo)"); +// public static ExpressionVo exp(ValueVo, String, ValueVo) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp($sql.value(1), "=", $sql.value(0))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE 1 = 0"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(ValueVo, String, ValueVo)"); +// public static ExpressionVo exp(ExpressionVo, String, ExpressionVo) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp($sql.exp("uuid", "=", "name"), "and", $sql.exp("name", "=", "uuid"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE uuid = name AND name = uuid"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(ExpressionVo, String, ExpressionVo)"); +// public static ExpressionVo exp(String, ExpressionVo, String, ExpressionVo, String) + plainSelect = $sql.from("tenant"); + $sql.addWhereExpression(plainSelect, $sql.exp("(", $sql.exp("uuid", "=", "name"), "and", $sql.exp("name", "=", "uuid"), ")")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FROM tenant WHERE (uuid = name AND name = uuid)"), "测试失败"); + methodNameList.remove("public static ExpressionVo exp(String, ExpressionVo, String, ExpressionVo, String)"); + + } + + private static void testFun(List methodNameList) { + PlainSelect plainSelect = new PlainSelect(); +// public static transient FunctionVo fun(String, Object[]) + $sql.setSelectColumn(plainSelect, $sql.fun("count", $sql.value(1))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT count(1)"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("count", 1)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT count(1)"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("CONCAT", $sql.value("Hello"), $sql.value(" "), $sql.value("MySQL"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT CONCAT('Hello', ' ', 'MySQL')"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("CONCAT", "'Hello'", "' '", "'MySQL'")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT CONCAT('Hello', ' ', 'MySQL')"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("SUBSTRING", $sql.value("MySQL"), $sql.value(2), $sql.value(3))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT SUBSTRING('MySQL', 2, 3)"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("SUBSTRING", "'MySQL'", 2, 3)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT SUBSTRING('MySQL', 2, 3)"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("SUBSTRING", $sql.value("MySQL"), $sql.value(-3))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT SUBSTRING('MySQL', -3)"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("SUBSTRING", "'MySQL'", -3)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT SUBSTRING('MySQL', -3)"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("LENGTH", $sql.value("MySQL"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT LENGTH('MySQL')"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("LENGTH", "'MySQL'")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT LENGTH('MySQL')"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("CHAR_LENGTH", $sql.value("你好"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT CHAR_LENGTH('你好')"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("CHAR_LENGTH", "'你好'")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT CHAR_LENGTH('你好')"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("UPPER", $sql.value("mysql"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT UPPER('mysql')"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("UPPER", "'mysql'")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT UPPER('mysql')"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("LOWER", $sql.value("MySQL"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT LOWER('MySQL')"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("LOWER", "'MySQL'")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT LOWER('MySQL')"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("TRIM", $sql.value(" MySQL "))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT TRIM(' MySQL ')"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("TRIM", "' MySQL '")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT TRIM(' MySQL ')"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("TRIM", "BOTH 'x' FROM 'xxMySQLxx'")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT TRIM(BOTH 'x' FROM 'xxMySQLxx')"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("ROUND", $sql.value(3.14159), $sql.value(2))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT ROUND(3.14159, 2)"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("ROUND", 3.14159, 2)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT ROUND(3.14159, 2)"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("ROUND", $sql.value(123.456), $sql.value(-1))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT ROUND(123.456, -1)"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("ROUND", 123.456, -1)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT ROUND(123.456, -1)"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("FLOOR", $sql.value(3.7))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FLOOR(3.7)"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("FLOOR", 3.7)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT FLOOR(3.7)"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("CEIL", $sql.value(3.2))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT CEIL(3.2)"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("CEIL", 3.2)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT CEIL(3.2)"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("ABS", $sql.value(-10))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT ABS(-10)"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("ABS", -10)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT ABS(-10)"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("MOD", $sql.value(10), $sql.value(3))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT MOD(10, 3)"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("MOD", 10, 3)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT MOD(10, 3)"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("POWER", $sql.value(2), $sql.value(3))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT POWER(2, 3)"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("POWER", 2, 3)); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT POWER(2, 3)"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("NOW")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT NOW()"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("CURDATE")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT CURDATE()"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("CURTIME")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT CURTIME()"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("DATE_FORMAT", $sql.fun("NOW"), $sql.value("%Y年%m月%d日 %H:%i:%s"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT DATE_FORMAT(NOW(), '%Y年%m月%d日 %H:%i:%s')"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("DATE_FORMAT", $sql.fun("NOW"), "'%Y年%m月%d日 %H:%i:%s'")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT DATE_FORMAT(NOW(), '%Y年%m月%d日 %H:%i:%s')"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("DATEDIFF", $sql.value("2023-12-31"), $sql.value("2023-01-01"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT DATEDIFF('2023-12-31', '2023-01-01')"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("DATEDIFF", "'2023-12-31'", "'2023-01-01'")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT DATEDIFF('2023-12-31', '2023-01-01')"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("DATE_ADD", $sql.fun("NOW"), "INTERVAL 1 MONTH")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT DATE_ADD(NOW(), INTERVAL 1 MONTH)"), "测试失败"); + + $sql.setSelectColumn(plainSelect, $sql.fun("DATE_SUB", $sql.fun("NOW"), "INTERVAL 1 WEEK")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT DATE_SUB(NOW(), INTERVAL 1 WEEK)"), "测试失败"); + + plainSelect = $sql.from("tenant"); + $sql.setSelectColumn(plainSelect, $sql.fun("IF", $sql.exp("name", "=", $sql.value("demo")), $sql.value("及格"), $sql.value("不及格"))); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT IF(name = 'demo', '及格', '不及格') FROM tenant"), "测试失败"); + $sql.setSelectColumn(plainSelect, $sql.fun("IF", $sql.exp("name", "=", "'demo'"), "'及格'", "'不及格'")); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT IF(name = 'demo', '及格', '不及格') FROM tenant"), "测试失败"); + + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, $sql.fun("GROUP_CONCAT", "name SEPARATOR ', '")); + $sql.addGroupBy(plainSelect, "uuid"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, GROUP_CONCAT(name SEPARATOR ', ') FROM tenant GROUP BY uuid"), "测试失败"); + + $sql.setSelectColumn(plainSelect, "uuid"); + $sql.addSelectColumn(plainSelect, "CASE WHEN name >= 90 THEN '优秀' WHEN name >= 80 THEN '良好' WHEN name >= 60 THEN '及格' ELSE '不及格' END", "grade"); + Assert.isTrue(Objects.equals(plainSelect.toString(), "SELECT uuid, CASE WHEN name >= 90 THEN '优秀' WHEN name >= 80 THEN '良好' WHEN name >= 60 THEN '及格' ELSE '不及格' END AS grade FROM tenant GROUP BY uuid"), "测试失败"); + methodNameList.remove("public static transient FunctionVo fun(String, Object[])"); + } + + private static List getMethodNameList(Class clazz) { + List resultList = new ArrayList<>(); + for (Method method : clazz.getDeclaredMethods()) { + String modifierString = Modifier.toString(method.getModifiers()); + if (modifierString.startsWith("public") && modifierString.contains("static")) { + List paramTypeNameList = new ArrayList<>(); + Class[] paramTypes = method.getParameterTypes(); + for (Class paramType : paramTypes) { + paramTypeNameList.add(paramType.getSimpleName()); + } + String result = modifierString + " " + method.getReturnType().getSimpleName() + " " + method.getName() + "(" + String.join(", ", paramTypeNameList) + ")"; + resultList.add(result); + } + } + return resultList; + } +} diff --git a/src/main/java/neatlogic/framework/sqlgenerator/ExpressionVo.java b/src/main/java/neatlogic/framework/sqlgenerator/ExpressionVo.java new file mode 100644 index 000000000..d7edfa2d2 --- /dev/null +++ b/src/main/java/neatlogic/framework/sqlgenerator/ExpressionVo.java @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2025 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.framework.sqlgenerator; + +import org.apache.commons.lang3.StringUtils; + +import java.util.Objects; + +public class ExpressionVo { + + private String leftParenthesis; + private String rightParenthesis; + private String leftColumn; + private String rightColumn; + private ValueVo leftValueExpression; + private ValueVo rightValueExpression; + private ExpressionVo leftExpressionVo; + private ExpressionVo rightExpressionVo; + private final String operationSymbol; + private final String type; + + ExpressionVo(ExpressionVo leftExpressionVo, String operationType, ExpressionVo rightExpressionVo) { + this(null, leftExpressionVo, operationType, rightExpressionVo, null); + } + + ExpressionVo(String leftParenthesis, ExpressionVo leftExpressionVo, String operationSymbol, ExpressionVo rightExpressionVo, String rightParenthesis) { + this.leftParenthesis = leftParenthesis; + this.leftExpressionVo = leftExpressionVo; + this.operationSymbol = operationSymbol; + this.rightExpressionVo = rightExpressionVo; + this.rightParenthesis = rightParenthesis; + this.type = $sql.BOOLEAN_OPERATION; + } + + ExpressionVo(String leftColumn, String operationSymbol, String rightColumn) { + this.leftColumn = leftColumn; + this.operationSymbol = operationSymbol; + this.rightColumn = rightColumn; + this.type = $sql.COMPARATIVE_OPERATION; + } + + ExpressionVo(String leftColumn, String operationSymbol) { + this(leftColumn, operationSymbol, new ValueVo("")); + } + + ExpressionVo(String leftColumn, String operationSymbol, ValueVo rightValueExpression) { + this.leftColumn = leftColumn; + this.operationSymbol = operationSymbol; + this.rightValueExpression = rightValueExpression; + this.type = $sql.COMPARATIVE_OPERATION; + } + + ExpressionVo(ValueVo leftValueExpression, String operationSymbol, ValueVo rightValueExpression) { + this.leftValueExpression = leftValueExpression; + this.operationSymbol = operationSymbol; + this.rightValueExpression = rightValueExpression; + this.type = $sql.COMPARATIVE_OPERATION; + } + + public String getLeftParenthesis() { + return leftParenthesis; + } + + public ExpressionVo getLeftExpression() { + return leftExpressionVo; + } + + public ExpressionVo getRightExpression() { + return rightExpressionVo; + } + + public String getRightParenthesis() { + return rightParenthesis; + } + + public String getLeftColumn() { + return leftColumn; + } + + public String getRightColumn() { + return rightColumn; + } + + public ValueVo getLeftValueExpression() { + return leftValueExpression; + } + + public ValueVo getRightValueExpression() { + return rightValueExpression; + } + + public String getOperationSymbol() { + return operationSymbol; + } + + public String getType() { + return type; + } + + @Override + public String toString() { + String result = StringUtils.EMPTY; + if (Objects.equals(type, $sql.BOOLEAN_OPERATION)) { + if (this.leftParenthesis != null) { + result += this.leftParenthesis; + } + if (this.leftExpressionVo != null) { + result += this.leftExpressionVo.toString(); + } + if (this.operationSymbol != null) { + result += " " + this.operationSymbol + " "; + } + if (this.rightExpressionVo != null) { + result += this.rightExpressionVo.toString(); + } + if (this.rightParenthesis != null) { + result += this.rightParenthesis; + } + } else if (Objects.equals(type, $sql.COMPARATIVE_OPERATION)) { + if (this.leftColumn != null) { + result += this.leftColumn; + } + if (this.leftValueExpression != null) { + result += this.leftValueExpression.toString(); + } + if (this.operationSymbol != null) { + result += " " + this.operationSymbol + " "; + } + if (this.rightColumn != null) { + result += this.rightColumn; + } + if (this.rightValueExpression != null) { + result += this.rightValueExpression.toString(); + } + } + return result; + } +} diff --git a/src/main/java/neatlogic/framework/sqlgenerator/FunctionVo.java b/src/main/java/neatlogic/framework/sqlgenerator/FunctionVo.java new file mode 100644 index 000000000..1a70e1316 --- /dev/null +++ b/src/main/java/neatlogic/framework/sqlgenerator/FunctionVo.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2025 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.framework.sqlgenerator; + +import org.apache.commons.lang3.StringUtils; + +import java.util.ArrayList; +import java.util.List; + +public class FunctionVo { + private final String name; + private Boolean distinct = false; + + private final Object[] parameters; + + FunctionVo(String name, Object ... parameters) { + this.name = name; + this.parameters = parameters; + } + + public FunctionVo withDistinct(boolean distinct) { + this.distinct = distinct; + return this; + } + + public String getName() { + return name; + } + + public boolean isDistinct() { + return distinct; + } + + public Object[] getParameters() { + return parameters; + } + + @Override + public String toString() { + String result = StringUtils.EMPTY; + if (this.name != null) { + result += this.name.trim(); + result += "("; + if (this.parameters != null) { + List paramterList = new ArrayList<>(); + for (Object parameter : this.parameters) { + if (parameter != null) { + paramterList.add(parameter.toString()); + } + } + result += String.join(", ", paramterList); + } + result += ")"; + } + return result; + } +} diff --git a/src/main/java/neatlogic/framework/sqlgenerator/JoinVo.java b/src/main/java/neatlogic/framework/sqlgenerator/JoinVo.java new file mode 100644 index 000000000..f4ccfe048 --- /dev/null +++ b/src/main/java/neatlogic/framework/sqlgenerator/JoinVo.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2025 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.framework.sqlgenerator; + +import org.apache.commons.lang3.StringUtils; + +public class JoinVo { + // join类型,LEFT JOIN, JOIN + private final String operationSymbol; + private final String schemaName; + private final String tableName; + private final String alias; + private ExpressionVo on; + + JoinVo(String operationSymbol, String schemaName, String tableName, String alias, ExpressionVo on) { + this.operationSymbol = operationSymbol; + this.schemaName = schemaName; + this.tableName = tableName; + this.alias = alias; + this.on = on; + } + + JoinVo(String operationSymbol, String tableName, String alias, ExpressionVo on) { + this(operationSymbol, null, tableName, alias, on); + } + JoinVo(String operationType, String tableName, String alias) { + this(operationType, null, tableName, alias, null); + } + + JoinVo(String operationType, String tableName) { + this(operationType, null, tableName, null, null); + } + + public JoinVo withOn(ExpressionVo on) { + this.on = on; + return this; + } + + public String getOperationSymbol() { + return operationSymbol; + } + + public String getSchemaName() { + return schemaName; + } + + public String getTableName() { + return tableName; + } + + public String getAlias() { + return alias; + } + + public ExpressionVo getOn() { + return on; + } + + @Override + public String toString() { + String result = StringUtils.EMPTY; + if (operationSymbol != null) { + result += operationSymbol.trim(); + result += StringUtils.SPACE; + } + if (schemaName != null) { + result += schemaName.trim(); + result += "."; + } + if (tableName != null) { + result += tableName.trim(); + result += StringUtils.SPACE; + } + if (alias != null) { + result += alias.trim(); + } + if (on != null) { + result += " ON "; + result += on.toString(); + } + return result; + } +} diff --git a/src/main/java/neatlogic/framework/sqlgenerator/ValueVo.java b/src/main/java/neatlogic/framework/sqlgenerator/ValueVo.java new file mode 100644 index 000000000..bb6e820d6 --- /dev/null +++ b/src/main/java/neatlogic/framework/sqlgenerator/ValueVo.java @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2025 深圳极向量科技有限公司 All Rights Reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package neatlogic.framework.sqlgenerator; + +import neatlogic.framework.util.TimeUtil; +import org.apache.commons.lang3.StringUtils; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class ValueVo { + + private String strValue; + private Integer intValue; + private Long longValue; + private Double doubleValue; + private List list; + + private Date date; + + ValueVo(String strValue) { + this.strValue = strValue; + } + + ValueVo(Integer intValue) { + this.intValue = intValue; + } + + ValueVo(Long longValue) { + this.longValue = longValue; + } + + ValueVo(Double doubleValue) { + this.doubleValue = doubleValue; + } + + ValueVo(List list) { + this.list = list; + } + + public ValueVo(Date date) { + this.date = date; + } + + public String getStrValue() { + return strValue; + } + + public Integer getIntValue() { + return intValue; + } + + public Long getLongValue() { + return longValue; + } + + public Double getDoubleValue() { + return doubleValue; + } + + public List getList() { + return list; + } + + public Date getDate() { + return date; + } + + @Override + public String toString() { + if (strValue != null) { + return "'" + strValue + "'"; + } else if (intValue != null) { + return intValue.toString(); + } else if (longValue != null) { + return longValue.toString(); + } else if (doubleValue != null) { + return doubleValue.toString(); + } else if (date != null) { + return "'" + (new SimpleDateFormat(TimeUtil.YYYY_MM_DD_HH_MM_SS)).format(date) + "'"; + } else if (list != null) { + List resultList = new ArrayList<>(); + for (Object obj : list) { + if (obj != null) { + if (obj instanceof String) { + resultList.add("'" + obj + "'"); + } else { + resultList.add(obj.toString()); + } + } + } + return String.join(", ", resultList); + } + return StringUtils.EMPTY; + } +} -- Gitee