feat(core): 添加新函数并增强数据库类型判断能力

- 在 FuncName 中添加 STRING_AGG 和 LISTAGG 函数
- 在 DbType 中添加方法判断数据库类型是否支持 MySQL、Oracle 或 PostgreSQL
- 在 DbTypeUtil 中添加获取当前数据库类型的方法
- 在 QueryMethods 中添加 STRING_AGG 和 LISTAGG聚合函数的支持
This commit is contained in:
ZhengJin 2025-08-07 17:09:24 +08:00
parent 354f9d11a3
commit aaa2566b12
No known key found for this signature in database
4 changed files with 72 additions and 0 deletions

View File

@ -144,6 +144,8 @@ 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() {
} }

View File

@ -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();
}
} }

View File

@ -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
*/ */

View File

@ -2678,6 +2678,20 @@ 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));
}
/**
* LISTAGG 聚合函数
*/
public static QueryColumn listAgg(QueryColumn columnX, String separator) {
return new FunctionQueryColumn(LISTAGG, columnX, string(separator));
}
/** /**
* date 函数 * date 函数
* @return * @return