mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
Merge pull request #570 from fangzhengjin/feat-QueryMethods
feat: 增加STRING_AGG、LISTAGG、CAST函数支持
This commit is contained in:
commit
be7d089264
@ -144,6 +144,9 @@ public class FuncName {
|
|||||||
public static final String WEEKOFYEAR = "WEEKOFYEAR";
|
public static final String WEEKOFYEAR = "WEEKOFYEAR";
|
||||||
public static final String YEAR = "YEAR";
|
public static final String YEAR = "YEAR";
|
||||||
public static final String GROUP_CONCAT = "GROUP_CONCAT";
|
public static final String GROUP_CONCAT = "GROUP_CONCAT";
|
||||||
|
public static final String STRING_AGG = "STRING_AGG";
|
||||||
|
public static final String LISTAGG = "LISTAGG";
|
||||||
|
|
||||||
private FuncName() {
|
private FuncName() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -17,6 +17,7 @@ package com.mybatisflex.core.dialect;
|
|||||||
|
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public enum DbType {
|
public enum DbType {
|
||||||
|
|
||||||
@ -208,4 +209,44 @@ public enum DbType {
|
|||||||
.findFirst()
|
.findFirst()
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取所有数据库类型
|
||||||
|
*
|
||||||
|
* @return 包含所有数据库类型的列表
|
||||||
|
*/
|
||||||
|
public static List<DbType> all() {
|
||||||
|
return Arrays.asList(DbType.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断当前数据库语法是否与MySQL属于同一类型
|
||||||
|
*/
|
||||||
|
public boolean mysqlSameType() {
|
||||||
|
return this == MYSQL || this == MARIADB || this == GBASE || this == OSCAR || this == XUGU || this == CLICK_HOUSE || this == OCEAN_BASE || this == CUBRID || this == SUNDB || this == GOLDENDB || this == YASDB;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断当前数据库语法是否与Oracle属于同一类型
|
||||||
|
*/
|
||||||
|
public boolean oracleSameType() {
|
||||||
|
return this == ORACLE || this == DM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断当前数据库语法是否与PostgreSQL属于同一类型
|
||||||
|
*/
|
||||||
|
public boolean postgresqlSameType() {
|
||||||
|
return this == POSTGRE_SQL || this == H2 || this == LEALONE || this == SQLITE || this == HSQL || this == KINGBASE_ES || this == PHOENIX || this == SAP_HANA || this == IMPALA || this == HIGH_GO || this == VERTICA || this == REDSHIFT || this == GAUSS || this == OPENGAUSS || this == TDENGINE || this == UXDB || this == GBASE_8S_PG || this == GBASE_8C || this == VASTBASE || this == DUCKDB;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否为已兼容的数据库类型
|
||||||
|
* 允许的数据库类型包括MySQL系列、Oracle系列和PostgreSQL系列
|
||||||
|
*
|
||||||
|
* @return 如果是允许的数据库类型返回true,否则返回false
|
||||||
|
*/
|
||||||
|
public boolean isSupportDb() {
|
||||||
|
return mysqlSameType() || oracleSameType() || postgresqlSameType();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
package com.mybatisflex.core.dialect;
|
package com.mybatisflex.core.dialect;
|
||||||
|
|
||||||
|
|
||||||
|
import com.mybatisflex.core.FlexGlobalConfig;
|
||||||
import com.mybatisflex.core.exception.FlexExceptions;
|
import com.mybatisflex.core.exception.FlexExceptions;
|
||||||
import com.mybatisflex.core.exception.locale.LocalizedFormats;
|
import com.mybatisflex.core.exception.locale.LocalizedFormats;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
@ -37,6 +38,20 @@ public class DbTypeUtil {
|
|||||||
private DbTypeUtil() {
|
private DbTypeUtil() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前数据库类型
|
||||||
|
* <p>首先从全局配置中获取数据库类型,如果全局配置中未设置,则尝试从方言工厂中获取线程局部变量设置的数据库类型
|
||||||
|
*
|
||||||
|
* @return 当前数据库类型,可能为null
|
||||||
|
*/
|
||||||
|
public static DbType getCurrentDbType() {
|
||||||
|
DbType dbType = FlexGlobalConfig.getDefaultConfig().getDbType();
|
||||||
|
if (dbType == null) {
|
||||||
|
dbType = DialectFactory.getHintDbType();
|
||||||
|
}
|
||||||
|
return dbType;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前配置的 DbType
|
* 获取当前配置的 DbType
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
import com.mybatisflex.core.dialect.IDialect;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CAST函数查询列
|
||||||
|
*/
|
||||||
|
public class CastQueryColumn extends QueryColumn implements HasParamsColumn {
|
||||||
|
|
||||||
|
private QueryColumn column;
|
||||||
|
private final String dataType;
|
||||||
|
|
||||||
|
public CastQueryColumn(QueryColumn column, String dataType) {
|
||||||
|
this.column = column;
|
||||||
|
this.dataType = dataType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CastQueryColumn(String column, String dataType) {
|
||||||
|
this.column = new QueryColumn(column);
|
||||||
|
this.dataType = dataType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
|
||||||
|
return " CAST(" + column.toConditionSql(queryTables, dialect) + " AS " + dataType + ") ";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String toSelectSql(List<QueryTable> queryTables, IDialect dialect) {
|
||||||
|
return " CAST(" + column.toSelectSql(queryTables, dialect) + " AS " + dataType + ") " + WrapperUtil.buildColumnAlias(alias, dialect);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CastQueryColumn clone() {
|
||||||
|
CastQueryColumn clone = (CastQueryColumn) super.clone();
|
||||||
|
clone.column = column.clone();
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "CastQueryColumn{" +
|
||||||
|
"column=" + column +
|
||||||
|
", dataType='" + dataType + '\'' +
|
||||||
|
", alias='" + alias + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object[] getParamValues() {
|
||||||
|
if (column instanceof HasParamsColumn) {
|
||||||
|
return ((HasParamsColumn) column).getParamValues();
|
||||||
|
}
|
||||||
|
return FlexConsts.EMPTY_ARRAY;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -955,6 +955,10 @@ public class QueryColumn implements CloneSupport<QueryColumn>, Conditional<Query
|
|||||||
return new ArithmeticQueryColumn(this).divide(number);
|
return new ArithmeticQueryColumn(this).divide(number);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public QueryColumn cast(String dataType) {
|
||||||
|
return new CastQueryColumn(this, dataType);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成列用于构建查询条件的 SQL 语句。
|
* 生成列用于构建查询条件的 SQL 语句。
|
||||||
*
|
*
|
||||||
|
|||||||
@ -2678,6 +2678,51 @@ public class QueryMethods {
|
|||||||
return new FunctionQueryColumn(GROUP_CONCAT, columnX);
|
return new FunctionQueryColumn(GROUP_CONCAT, columnX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* STRING_AGG 聚合函数
|
||||||
|
*/
|
||||||
|
public static QueryColumn stringAgg(QueryColumn columnX, String separator) {
|
||||||
|
return new FunctionQueryColumn(STRING_AGG, columnX, string(separator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static QueryColumn stringAgg(String columnX, String separator) {
|
||||||
|
return new FunctionQueryColumn(STRING_AGG, columnX, separator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> QueryColumn stringAgg(LambdaGetter<T> columnX, String separator) {
|
||||||
|
return new FunctionQueryColumn(STRING_AGG, LambdaUtil.getQueryColumn(columnX), string(separator));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LISTAGG 聚合函数
|
||||||
|
*/
|
||||||
|
public static QueryColumn listAgg(QueryColumn column, String separator) {
|
||||||
|
return new FunctionQueryColumn(LISTAGG, column, string(separator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static QueryColumn listAgg(String column, String separator) {
|
||||||
|
return new FunctionQueryColumn(STRING_AGG, column, separator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> QueryColumn listAgg(LambdaGetter<T> column, String separator) {
|
||||||
|
return new FunctionQueryColumn(STRING_AGG, LambdaUtil.getQueryColumn(column), string(separator));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CAST函数查询列
|
||||||
|
*/
|
||||||
|
public static <T> QueryColumn cast(QueryColumn column, String dataType) {
|
||||||
|
return new CastQueryColumn(column, dataType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> QueryColumn cast(String column, String dataType) {
|
||||||
|
return new CastQueryColumn(column, dataType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> QueryColumn cast(LambdaGetter<T> column, String dataType) {
|
||||||
|
return new CastQueryColumn(LambdaUtil.getQueryColumn(column), dataType);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* date 函数
|
* date 函数
|
||||||
* @return
|
* @return
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user