From aaa2566b126fc77b27354fe45b37b49ce582afbd Mon Sep 17 00:00:00 2001 From: ZhengJin Date: Thu, 7 Aug 2025 17:09:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20=E6=B7=BB=E5=8A=A0=E6=96=B0?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=B9=B6=E5=A2=9E=E5=BC=BA=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E7=B1=BB=E5=9E=8B=E5=88=A4=E6=96=AD=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 FuncName 中添加 STRING_AGG 和 LISTAGG 函数 - 在 DbType 中添加方法判断数据库类型是否支持 MySQL、Oracle 或 PostgreSQL - 在 DbTypeUtil 中添加获取当前数据库类型的方法 - 在 QueryMethods 中添加 STRING_AGG 和 LISTAGG聚合函数的支持 --- .../mybatisflex/core/constant/FuncName.java | 2 + .../com/mybatisflex/core/dialect/DbType.java | 41 +++++++++++++++++++ .../mybatisflex/core/dialect/DbTypeUtil.java | 15 +++++++ .../mybatisflex/core/query/QueryMethods.java | 14 +++++++ 4 files changed, 72 insertions(+) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/FuncName.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/FuncName.java index 2b2c63ba..a302e432 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/FuncName.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/FuncName.java @@ -144,6 +144,8 @@ public class FuncName { public static final String WEEKOFYEAR = "WEEKOFYEAR"; public static final String YEAR = "YEAR"; public static final String GROUP_CONCAT = "GROUP_CONCAT"; + public static final String STRING_AGG = "STRING_AGG"; + public static final String LISTAGG = "LISTAGG"; private FuncName() { } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/DbType.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/DbType.java index 61f5a262..9c880f05 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/DbType.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/DbType.java @@ -17,6 +17,7 @@ package com.mybatisflex.core.dialect; import com.mybatisflex.core.util.StringUtil; import java.util.Arrays; +import java.util.List; public enum DbType { @@ -208,4 +209,44 @@ public enum DbType { .findFirst() .orElse(null); } + + /** + * 获取所有数据库类型 + * + * @return 包含所有数据库类型的列表 + */ + public static List 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(); + } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/DbTypeUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/DbTypeUtil.java index 1ceafbae..6bb58b3a 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/DbTypeUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/DbTypeUtil.java @@ -16,6 +16,7 @@ package com.mybatisflex.core.dialect; +import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.exception.locale.LocalizedFormats; import com.mybatisflex.core.util.StringUtil; @@ -37,6 +38,20 @@ public class DbTypeUtil { private DbTypeUtil() { } + /** + * 获取当前数据库类型 + *

首先从全局配置中获取数据库类型,如果全局配置中未设置,则尝试从方言工厂中获取线程局部变量设置的数据库类型 + * + * @return 当前数据库类型,可能为null + */ + public static DbType getCurrentDbType() { + DbType dbType = FlexGlobalConfig.getDefaultConfig().getDbType(); + if (dbType == null) { + dbType = DialectFactory.getHintDbType(); + } + return dbType; + } + /** * 获取当前配置的 DbType */ diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java index 67f8c08a..caddb467 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java @@ -2678,6 +2678,20 @@ public class QueryMethods { 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 函数 * @return