From 3c5309b14e6ac44dcef5007aa7d67d2c50ce825f Mon Sep 17 00:00:00 2001 From: dhb52 Date: Sat, 22 Jun 2024 16:56:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20FindInSetEnum=E3=80=81NameToTypeEnum=20?= =?UTF-8?q?=E8=9E=8D=E5=90=88=E4=B8=BADbTypeEnum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatis/core/enums/DbTypeEnum.java | 73 +++++++++++++++++++ .../mybatis/core/enums/FindInSetEnum.java | 50 ------------- .../mybatis/core/enums/NameToTypeEnum.java | 67 ----------------- .../mybatis/core/util/JdbcUtils.java | 4 +- .../mybatis/core/util/MyBatisUtils.java | 4 +- 5 files changed, 77 insertions(+), 121 deletions(-) create mode 100644 yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/DbTypeEnum.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/FindInSetEnum.java delete mode 100644 yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/NameToTypeEnum.java diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/DbTypeEnum.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/DbTypeEnum.java new file mode 100644 index 0000000000..9ea09168df --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/DbTypeEnum.java @@ -0,0 +1,73 @@ +package cn.iocoder.yudao.framework.mybatis.core.enums; + +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.annotation.DbType; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.Arrays; +import java.util.Map; +import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collectors; + + +@Getter +@AllArgsConstructor +public enum DbTypeEnum { + /** + * MySQL + */ + MY_SQL("MySQL", "FIND_IN_SET('#{value}', #{column}) <> 0", DbType.MYSQL), + + /** + * Oracle + */ + ORACLE("Oracle", "FIND_IN_SET('#{value}', #{column}) <> 0", DbType.ORACLE), + + /** + * PostgreSQL + */ + POSTGRE_SQL("PostgreSQL", "POSITION('#{value}' IN #{column}) <> 0", DbType.POSTGRE_SQL), + + /** + * SQL Server + */ + SQL_SERVER("Microsoft SQL Server", "CHARINDEX(',' + #{value} + ',', ',' + #{column} + ',') <> 0", DbType.SQL_SERVER), + + /** + * 达梦 + */ + DM("DM DBMS", "FIND_IN_SET('#{value}', #{column}) <> 0", DbType.DM), + + /** + * 人大金仓 + */ + KINGBASE_ES("KingbaseES", "POSITION('#{value}' IN #{column}) <> 0", DbType.KINGBASE_ES), + + // 华为openGauss 使用ProductName 与 PostgreSQL相同 + ; + + public static final Map MAP_BY_NAME = Arrays.stream(values()) + .collect(Collectors.toMap(DbTypeEnum::getName, Function.identity())); + + public static final Map MAP_BY_MP = Arrays.stream(values()) + .collect(Collectors.toMap(DbTypeEnum::getMpDbType, Function.identity())); + + + private final String name; + private final String findInSetTemplate; + private final DbType mpDbType; + + public static DbType find(String databaseProductName) { + if (StrUtil.isBlank(databaseProductName)) { + return null; + } + return MAP_BY_NAME.get(databaseProductName).getMpDbType(); + } + + public static String getFindInSetTemplate(DbType dbType) { + return Optional.of(MAP_BY_MP.get(dbType).getFindInSetTemplate()) + .orElseThrow(() -> new IllegalArgumentException("FIND_IN_SET not supported")); + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/FindInSetEnum.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/FindInSetEnum.java deleted file mode 100644 index 298d0dd9d1..0000000000 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/FindInSetEnum.java +++ /dev/null @@ -1,50 +0,0 @@ -package cn.iocoder.yudao.framework.mybatis.core.enums; - - -import com.baomidou.mybatisplus.annotation.DbType; -import lombok.AllArgsConstructor; -import lombok.Getter; - -import java.util.Arrays; -import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; - -/** - * find_in_set函数的跨数据库实现 - * - * @author dhb52 - */ -@Getter -@AllArgsConstructor -public enum FindInSetEnum { - - // FIND_IN_SET: MySQL 类型 - MYSQL("FIND_IN_SET('#{value}', #{column}) <> 0", DbType.MYSQL), - DM("FIND_IN_SET('#{value}', #{column}) <> 0", DbType.DM), - - // INSTR: Oracle 类型 - ORACLE("INSTR(','||#{column}||',' , ',#{value},') <> 0", DbType.ORACLE), - - // CHARINDEX: SQLServer - SQLSERVER("CHARINDEX(',' + #{value} + ',', ',' + #{column} + ',')", DbType.SQL_SERVER), - - // POSITION: PostgreSQL 类型 - POSTGRE_SQL("POSITION('#{value}' IN #{column})", DbType.POSTGRE_SQL), - KINGBASE_ES("POSITION('#{value}' IN #{column})", DbType.KINGBASE_ES), - - // LOCATE: 其他 - H2("LOCATE('#{value}' IN #{column})", DbType.H2), - ; - - public static final Map MAPS = Arrays.stream(values()) - .collect(Collectors.toMap(FindInSetEnum::getDbType, FindInSetEnum::getSqlTemplate)); - - private String sqlTemplate; - private DbType dbType; - - public static String getTemplate(DbType dbType) { - return Optional.of(MAPS.get(dbType)) - .orElseThrow(() -> new IllegalArgumentException("FIND_IN_SET not supported")); - } -} diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/NameToTypeEnum.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/NameToTypeEnum.java deleted file mode 100644 index fe6e99830d..0000000000 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/NameToTypeEnum.java +++ /dev/null @@ -1,67 +0,0 @@ -package cn.iocoder.yudao.framework.mybatis.core.enums; - -import cn.hutool.core.util.StrUtil; -import com.baomidou.mybatisplus.annotation.DbType; -import lombok.AllArgsConstructor; -import lombok.Getter; - -import java.util.Arrays; -import java.util.Map; -import java.util.stream.Collectors; - -// TODO @dhb52:是不是把 FindInSetEnum、NameToTypeEnum 融合,搞成 DbTypeEnum?然后里面 type、productName,findInSet -/** - * 数据库产品名 => mp DbType 的映射关系 - * - * @author dhb52 - */ -@Getter -@AllArgsConstructor -public enum NameToTypeEnum { - - /** - * MySQL - */ - MY_SQL("MySQL", DbType.MYSQL), - - /** - * Oracle - */ - ORACLE("Oracle", DbType.ORACLE), - - /** - * PostgreSQL - */ - POSTGRE_SQL("PostgreSQL", DbType.POSTGRE_SQL), - - /** - * SQL Server - */ - SQL_SERVER("Microsoft SQL Server", DbType.SQL_SERVER), - - /** - * 达梦 - */ - DM("DM DBMS", DbType.DM), - - /** - * 人大金仓 - */ - KINGBASE_ES("KingbaseES", DbType.KINGBASE_ES), - - // 华为openGauss ProductName 与 PostgreSQL相同 - ; - - private final String name; - private final DbType type; - - public static final Map MAPS = Arrays.stream(values()) - .collect(Collectors.toMap(NameToTypeEnum::getName, NameToTypeEnum::getType)); - - public static DbType find(String databaseProductName) { - if (StrUtil.isBlank(databaseProductName)) { - return null; - } - return MAPS.get(databaseProductName); - } -} \ No newline at end of file diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/JdbcUtils.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/JdbcUtils.java index c7e9e89cdb..c4894cad00 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/JdbcUtils.java +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/JdbcUtils.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.framework.mybatis.core.util; import cn.iocoder.yudao.framework.common.util.spring.SpringUtils; -import cn.iocoder.yudao.framework.mybatis.core.enums.NameToTypeEnum; +import cn.iocoder.yudao.framework.mybatis.core.enums.DbTypeEnum; import com.baomidou.dynamic.datasource.DynamicRoutingDataSource; import com.baomidou.mybatisplus.annotation.DbType; @@ -52,7 +52,7 @@ public class JdbcUtils { DynamicRoutingDataSource dynamicRoutingDataSource = SpringUtils.getBean(DynamicRoutingDataSource.class); DataSource dataSource = dynamicRoutingDataSource.determineDataSource(); try (Connection conn = dataSource.getConnection()) { - return NameToTypeEnum.find(conn.getMetaData().getDatabaseProductName()); + return DbTypeEnum.find(conn.getMetaData().getDatabaseProductName()); } catch (SQLException e) { throw new IllegalArgumentException(e.getMessage()); } diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/MyBatisUtils.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/MyBatisUtils.java index 789bf75c9d..384b49d9a3 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/MyBatisUtils.java +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/MyBatisUtils.java @@ -4,7 +4,7 @@ import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.SortingField; -import cn.iocoder.yudao.framework.mybatis.core.enums.FindInSetEnum; +import cn.iocoder.yudao.framework.mybatis.core.enums.DbTypeEnum; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.core.metadata.OrderItem; import com.baomidou.mybatisplus.core.toolkit.StringPool; @@ -98,7 +98,7 @@ public class MyBatisUtils { public static String findInSet(String column, Object value) { // 这里不用SqlConstants.DB_TYPE,因为它是使用 primary 数据源的 url 推断出来的类型 DbType dbType = JdbcUtils.getDbType(); - return FindInSetEnum.getTemplate(dbType) + return DbTypeEnum.getFindInSetTemplate(dbType) .replace("#{column}", column) .replace("#{value}", StrUtil.toString(value)); }