refactor: 使 QueryColumn 子类可以自定义 SQL 生成,关闭 https://gitee.com/mybatis-flex/mybatis-flex/issues/IAJOFJ

This commit is contained in:
Suomm 2024-10-02 11:06:37 +08:00
parent d533d2e5d6
commit 8447d5e4c2
12 changed files with 76 additions and 34 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
* Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -89,7 +89,7 @@ public class ArithmeticQueryColumn extends QueryColumn implements HasParamsColum
@Override
String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
StringBuilder sql = new StringBuilder();
for (int i = 0; i < arithmeticInfos.size(); i++) {
sql.append(arithmeticInfos.get(i).toSql(queryTables, dialect, i));
@ -110,7 +110,7 @@ public class ArithmeticQueryColumn extends QueryColumn implements HasParamsColum
@Override
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
StringBuilder sql = new StringBuilder();
for (int i = 0; i < arithmeticInfos.size(); i++) {
sql.append(arithmeticInfos.get(i).toSql(queryTables, dialect, i));

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
* Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -41,7 +41,7 @@ public class CaseQueryColumn extends QueryColumn implements HasParamsColumn {
@Override
String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
String sql = buildSql(queryTables, dialect);
if (StringUtil.isNotBlank(alias)) {
return WrapperUtil.withAlias(sql, alias, dialect);
@ -60,7 +60,7 @@ public class CaseQueryColumn extends QueryColumn implements HasParamsColumn {
@Override
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
return WrapperUtil.withBracket(buildSql(queryTables, dialect));
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
* Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -34,7 +34,7 @@ public class CaseSearchQueryColumn extends QueryColumn implements HasParamsColum
private Object elseValue;
@Override
String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
String sql = buildSql(queryTables, dialect);
if (StringUtil.isNotBlank(alias)) {
return WrapperUtil.withAlias(sql, alias, dialect);
@ -68,7 +68,7 @@ public class CaseSearchQueryColumn extends QueryColumn implements HasParamsColum
@Override
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
return WrapperUtil.withBracket(buildSql(queryTables, dialect));
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
* Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -42,7 +42,7 @@ public class DistinctQueryColumn extends QueryColumn implements HasParamsColumn
}
@Override
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
if (CollectionUtil.isEmpty(queryTables)) {
return SqlConsts.EMPTY;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
* Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -99,7 +99,7 @@ public class FunctionQueryColumn extends QueryColumn implements HasParamsColumn
}
@Override
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
String sql = getSql(queryTables, dialect);
return fnName + WrapperUtil.withBracket(sql);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
* Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -40,7 +40,7 @@ public class IfFunctionQueryColumn extends QueryColumn implements HasParamsColum
}
@Override
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
return "IF(" + condition.toSql(queryTables, dialect) + ", " +
trueValue.toConditionSql(queryTables, dialect) + ", " +
falseValue.toConditionSql(queryTables, dialect) + ")";

View File

@ -1,3 +1,19 @@
/*
* Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 com.mybatisflex.core.query;
import com.mybatisflex.core.FlexConsts;
@ -28,12 +44,12 @@ public class NegativeQueryColumn extends QueryColumn implements HasParamsColumn
}
@Override
String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
return toConditionSql(queryTables, dialect) + WrapperUtil.buildColumnAlias(alias, dialect);
}
@Override
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
return "-" + queryColumn.toConditionSql(queryTables, dialect);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
* Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -45,7 +45,6 @@ public class QueryColumn implements CloneSupport<QueryColumn>, Conditional<Query
private boolean returnCopyByAsMethod = false;
public QueryColumn() {
}
@ -514,7 +513,7 @@ public class QueryColumn implements CloneSupport<QueryColumn>, Conditional<Query
return QueryCondition.createEmpty();
}
return between(values[0], values[values.length - 1]);
return between(values[0], values[values.length - 1]);
}
@Override
@ -935,8 +934,14 @@ public class QueryColumn implements CloneSupport<QueryColumn>, Conditional<Query
return new ArithmeticQueryColumn(this).divide(number);
}
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
/**
* 生成列用于构建查询条件的 SQL 语句
*
* @param queryTables 查询表
* @param dialect 方言
* @return SQL 语句
*/
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
QueryTable selectTable = getSelectTable(queryTables, table);
if (selectTable == null) {
return dialect.wrap(name);
@ -955,12 +960,17 @@ public class QueryColumn implements CloneSupport<QueryColumn>, Conditional<Query
}
}
String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
/**
* 生成列用于构建查询列的 SQL 语句
*
* @param queryTables 查询表
* @param dialect 方言
* @return SQL 语句
*/
protected String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
return toConditionSql(queryTables, dialect) + WrapperUtil.buildColumnAlias(alias, dialect);
}
QueryTable getSelectTable(List<QueryTable> queryTables, QueryTable selfTable) {
// 未查询任何表
if (queryTables == null || queryTables.isEmpty()) {

View File

@ -1,3 +1,19 @@
/*
* Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 com.mybatisflex.core.query;
import com.mybatisflex.core.dialect.IDialect;
@ -30,12 +46,12 @@ public class QueryColumnAdapter extends QueryColumn implements HasParamsColumn {
}
@Override
String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
return condition.toSql(queryTables, dialect) + WrapperUtil.buildColumnAlias(alias, dialect);
}
@Override
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
return condition.toSql(queryTables, dialect);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
* Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -38,12 +38,12 @@ public class RawQueryColumn extends QueryColumn implements HasParamsColumn {
}
@Override
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
return content;
}
@Override
String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
return content + WrapperUtil.buildColumnAlias(alias, dialect);
}

View File

@ -40,12 +40,12 @@ public class SelectQueryColumn extends QueryColumn implements HasParamsColumn {
}
@Override
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
return dialect.forSelectByQuery(queryWrapper);
}
@Override
String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
String selectSql = dialect.forSelectByQuery(queryWrapper);
if (StringUtil.isNotBlank(selectSql) && StringUtil.isNotBlank(alias)) {
selectSql = WrapperUtil.withAlias(selectSql, alias, dialect);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
* Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -69,7 +69,7 @@ public class StringFunctionQueryColumn extends QueryColumn {
}
@Override
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
String sql = StringUtil.join(SqlConsts.DELIMITER, params);
if (StringUtil.isBlank(sql)) {
return SqlConsts.EMPTY;