Commit 3c5309b14e6ac44dcef5007aa7d67d2c50ce825f
1 parent
355e47c3
feat: FindInSetEnum、NameToTypeEnum 融合为DbTypeEnum
Showing
4 changed files
with
31 additions
and
75 deletions
yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/NameToTypeEnum.java renamed to yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/DbTypeEnum.java
| ... | ... | @@ -7,61 +7,67 @@ import lombok.Getter; |
| 7 | 7 | |
| 8 | 8 | import java.util.Arrays; |
| 9 | 9 | import java.util.Map; |
| 10 | +import java.util.Optional; | |
| 11 | +import java.util.function.Function; | |
| 10 | 12 | import java.util.stream.Collectors; |
| 11 | 13 | |
| 12 | -// TODO @dhb52:是不是把 FindInSetEnum、NameToTypeEnum 融合,搞成 DbTypeEnum?然后里面 type、productName,findInSet | |
| 13 | -/** | |
| 14 | - * 数据库产品名 => mp DbType 的映射关系 | |
| 15 | - * | |
| 16 | - * @author dhb52 | |
| 17 | - */ | |
| 14 | + | |
| 18 | 15 | @Getter |
| 19 | 16 | @AllArgsConstructor |
| 20 | -public enum NameToTypeEnum { | |
| 21 | - | |
| 17 | +public enum DbTypeEnum { | |
| 22 | 18 | /** |
| 23 | 19 | * MySQL |
| 24 | 20 | */ |
| 25 | - MY_SQL("MySQL", DbType.MYSQL), | |
| 21 | + MY_SQL("MySQL", "FIND_IN_SET('#{value}', #{column}) <> 0", DbType.MYSQL), | |
| 26 | 22 | |
| 27 | 23 | /** |
| 28 | 24 | * Oracle |
| 29 | 25 | */ |
| 30 | - ORACLE("Oracle", DbType.ORACLE), | |
| 26 | + ORACLE("Oracle", "FIND_IN_SET('#{value}', #{column}) <> 0", DbType.ORACLE), | |
| 31 | 27 | |
| 32 | 28 | /** |
| 33 | 29 | * PostgreSQL |
| 34 | 30 | */ |
| 35 | - POSTGRE_SQL("PostgreSQL", DbType.POSTGRE_SQL), | |
| 31 | + POSTGRE_SQL("PostgreSQL", "POSITION('#{value}' IN #{column}) <> 0", DbType.POSTGRE_SQL), | |
| 36 | 32 | |
| 37 | 33 | /** |
| 38 | 34 | * SQL Server |
| 39 | 35 | */ |
| 40 | - SQL_SERVER("Microsoft SQL Server", DbType.SQL_SERVER), | |
| 36 | + SQL_SERVER("Microsoft SQL Server", "CHARINDEX(',' + #{value} + ',', ',' + #{column} + ',') <> 0", DbType.SQL_SERVER), | |
| 41 | 37 | |
| 42 | 38 | /** |
| 43 | 39 | * 达梦 |
| 44 | 40 | */ |
| 45 | - DM("DM DBMS", DbType.DM), | |
| 41 | + DM("DM DBMS", "FIND_IN_SET('#{value}', #{column}) <> 0", DbType.DM), | |
| 46 | 42 | |
| 47 | 43 | /** |
| 48 | 44 | * 人大金仓 |
| 49 | 45 | */ |
| 50 | - KINGBASE_ES("KingbaseES", DbType.KINGBASE_ES), | |
| 46 | + KINGBASE_ES("KingbaseES", "POSITION('#{value}' IN #{column}) <> 0", DbType.KINGBASE_ES), | |
| 51 | 47 | |
| 52 | - // 华为openGauss ProductName 与 PostgreSQL相同 | |
| 48 | + // 华为openGauss 使用ProductName 与 PostgreSQL相同 | |
| 53 | 49 | ; |
| 54 | 50 | |
| 55 | - private final String name; | |
| 56 | - private final DbType type; | |
| 51 | + public static final Map<String, DbTypeEnum> MAP_BY_NAME = Arrays.stream(values()) | |
| 52 | + .collect(Collectors.toMap(DbTypeEnum::getName, Function.identity())); | |
| 53 | + | |
| 54 | + public static final Map<DbType, DbTypeEnum> MAP_BY_MP = Arrays.stream(values()) | |
| 55 | + .collect(Collectors.toMap(DbTypeEnum::getMpDbType, Function.identity())); | |
| 57 | 56 | |
| 58 | - public static final Map<String, DbType> MAPS = Arrays.stream(values()) | |
| 59 | - .collect(Collectors.toMap(NameToTypeEnum::getName, NameToTypeEnum::getType)); | |
| 57 | + | |
| 58 | + private final String name; | |
| 59 | + private final String findInSetTemplate; | |
| 60 | + private final DbType mpDbType; | |
| 60 | 61 | |
| 61 | 62 | public static DbType find(String databaseProductName) { |
| 62 | 63 | if (StrUtil.isBlank(databaseProductName)) { |
| 63 | 64 | return null; |
| 64 | 65 | } |
| 65 | - return MAPS.get(databaseProductName); | |
| 66 | + return MAP_BY_NAME.get(databaseProductName).getMpDbType(); | |
| 67 | + } | |
| 68 | + | |
| 69 | + public static String getFindInSetTemplate(DbType dbType) { | |
| 70 | + return Optional.of(MAP_BY_MP.get(dbType).getFindInSetTemplate()) | |
| 71 | + .orElseThrow(() -> new IllegalArgumentException("FIND_IN_SET not supported")); | |
| 66 | 72 | } |
| 67 | -} | |
| 68 | 73 | \ No newline at end of file |
| 74 | +} | ... | ... |
yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/FindInSetEnum.java deleted
| 1 | -package cn.iocoder.yudao.framework.mybatis.core.enums; | |
| 2 | - | |
| 3 | - | |
| 4 | -import com.baomidou.mybatisplus.annotation.DbType; | |
| 5 | -import lombok.AllArgsConstructor; | |
| 6 | -import lombok.Getter; | |
| 7 | - | |
| 8 | -import java.util.Arrays; | |
| 9 | -import java.util.Map; | |
| 10 | -import java.util.Optional; | |
| 11 | -import java.util.stream.Collectors; | |
| 12 | - | |
| 13 | -/** | |
| 14 | - * find_in_set函数的跨数据库实现 | |
| 15 | - * | |
| 16 | - * @author dhb52 | |
| 17 | - */ | |
| 18 | -@Getter | |
| 19 | -@AllArgsConstructor | |
| 20 | -public enum FindInSetEnum { | |
| 21 | - | |
| 22 | - // FIND_IN_SET: MySQL 类型 | |
| 23 | - MYSQL("FIND_IN_SET('#{value}', #{column}) <> 0", DbType.MYSQL), | |
| 24 | - DM("FIND_IN_SET('#{value}', #{column}) <> 0", DbType.DM), | |
| 25 | - | |
| 26 | - // INSTR: Oracle 类型 | |
| 27 | - ORACLE("INSTR(','||#{column}||',' , ',#{value},') <> 0", DbType.ORACLE), | |
| 28 | - | |
| 29 | - // CHARINDEX: SQLServer | |
| 30 | - SQLSERVER("CHARINDEX(',' + #{value} + ',', ',' + #{column} + ',')", DbType.SQL_SERVER), | |
| 31 | - | |
| 32 | - // POSITION: PostgreSQL 类型 | |
| 33 | - POSTGRE_SQL("POSITION('#{value}' IN #{column})", DbType.POSTGRE_SQL), | |
| 34 | - KINGBASE_ES("POSITION('#{value}' IN #{column})", DbType.KINGBASE_ES), | |
| 35 | - | |
| 36 | - // LOCATE: 其他 | |
| 37 | - H2("LOCATE('#{value}' IN #{column})", DbType.H2), | |
| 38 | - ; | |
| 39 | - | |
| 40 | - public static final Map<DbType, String> MAPS = Arrays.stream(values()) | |
| 41 | - .collect(Collectors.toMap(FindInSetEnum::getDbType, FindInSetEnum::getSqlTemplate)); | |
| 42 | - | |
| 43 | - private String sqlTemplate; | |
| 44 | - private DbType dbType; | |
| 45 | - | |
| 46 | - public static String getTemplate(DbType dbType) { | |
| 47 | - return Optional.of(MAPS.get(dbType)) | |
| 48 | - .orElseThrow(() -> new IllegalArgumentException("FIND_IN_SET not supported")); | |
| 49 | - } | |
| 50 | -} |
yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/JdbcUtils.java
| 1 | 1 | package cn.iocoder.yudao.framework.mybatis.core.util; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.util.spring.SpringUtils; |
| 4 | -import cn.iocoder.yudao.framework.mybatis.core.enums.NameToTypeEnum; | |
| 4 | +import cn.iocoder.yudao.framework.mybatis.core.enums.DbTypeEnum; | |
| 5 | 5 | import com.baomidou.dynamic.datasource.DynamicRoutingDataSource; |
| 6 | 6 | import com.baomidou.mybatisplus.annotation.DbType; |
| 7 | 7 | |
| ... | ... | @@ -52,7 +52,7 @@ public class JdbcUtils { |
| 52 | 52 | DynamicRoutingDataSource dynamicRoutingDataSource = SpringUtils.getBean(DynamicRoutingDataSource.class); |
| 53 | 53 | DataSource dataSource = dynamicRoutingDataSource.determineDataSource(); |
| 54 | 54 | try (Connection conn = dataSource.getConnection()) { |
| 55 | - return NameToTypeEnum.find(conn.getMetaData().getDatabaseProductName()); | |
| 55 | + return DbTypeEnum.find(conn.getMetaData().getDatabaseProductName()); | |
| 56 | 56 | } catch (SQLException e) { |
| 57 | 57 | throw new IllegalArgumentException(e.getMessage()); |
| 58 | 58 | } | ... | ... |
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; |
| 4 | 4 | import cn.hutool.core.util.StrUtil; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageParam; |
| 6 | 6 | import cn.iocoder.yudao.framework.common.pojo.SortingField; |
| 7 | -import cn.iocoder.yudao.framework.mybatis.core.enums.FindInSetEnum; | |
| 7 | +import cn.iocoder.yudao.framework.mybatis.core.enums.DbTypeEnum; | |
| 8 | 8 | import com.baomidou.mybatisplus.annotation.DbType; |
| 9 | 9 | import com.baomidou.mybatisplus.core.metadata.OrderItem; |
| 10 | 10 | import com.baomidou.mybatisplus.core.toolkit.StringPool; |
| ... | ... | @@ -98,7 +98,7 @@ public class MyBatisUtils { |
| 98 | 98 | public static String findInSet(String column, Object value) { |
| 99 | 99 | // 这里不用SqlConstants.DB_TYPE,因为它是使用 primary 数据源的 url 推断出来的类型 |
| 100 | 100 | DbType dbType = JdbcUtils.getDbType(); |
| 101 | - return FindInSetEnum.getTemplate(dbType) | |
| 101 | + return DbTypeEnum.getFindInSetTemplate(dbType) | |
| 102 | 102 | .replace("#{column}", column) |
| 103 | 103 | .replace("#{value}", StrUtil.toString(value)); |
| 104 | 104 | } | ... | ... |