mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-09-17 16:56:39 +08:00
update 优化数据库类型获取和判断逻辑,增强代码可读性
This commit is contained in:
parent
ae5bec994d
commit
84fd02e7d8
@ -42,17 +42,46 @@ public enum DataBaseType {
|
||||
* 根据数据库产品名称查找对应的数据库类型
|
||||
*
|
||||
* @param databaseProductName 数据库产品名称
|
||||
* @return 对应的数据库类型枚举值,如果未找到则返回 null
|
||||
* @return 对应的数据库类型枚举值
|
||||
*/
|
||||
public static DataBaseType find(String databaseProductName) {
|
||||
if (StringUtils.isBlank(databaseProductName)) {
|
||||
return null;
|
||||
return MY_SQL;
|
||||
}
|
||||
for (DataBaseType type : values()) {
|
||||
if (type.getType().equals(databaseProductName)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return MY_SQL;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为 MySQL 类型
|
||||
*/
|
||||
public boolean isMySql() {
|
||||
return this == MY_SQL;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为 Oracle 类型
|
||||
*/
|
||||
public boolean isOracle() {
|
||||
return this == ORACLE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为 PostgreSQL 类型
|
||||
*/
|
||||
public boolean isPostgreSql() {
|
||||
return this == POSTGRE_SQL;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为 SQL Server 类型
|
||||
*/
|
||||
public boolean isSqlServer() {
|
||||
return this == SQL_SERVER;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,7 +26,14 @@ public class DataBaseHelper {
|
||||
private static final DynamicRoutingDataSource DS = SpringUtils.getBean(DynamicRoutingDataSource.class);
|
||||
|
||||
/**
|
||||
* 获取当前数据库类型
|
||||
* 获取当前数据源对应的数据库类型
|
||||
* <p>
|
||||
* 通过 DynamicRoutingDataSource 获取当前线程绑定的数据源,
|
||||
* 然后从数据源获取数据库连接,利用连接的元数据获取数据库产品名称,
|
||||
* 最后调用 DataBaseType.find 方法将数据库名称转换为对应的枚举类型
|
||||
*
|
||||
* @return 当前数据库对应的 DataBaseType 枚举,找不到时默认返回 MY_SQL
|
||||
* @throws ServiceException 当获取数据库连接或元数据出现异常时抛出业务异常
|
||||
*/
|
||||
public static DataBaseType getDataBaseType() {
|
||||
DataSource dataSource = DS.determineDataSource();
|
||||
@ -39,37 +46,31 @@ public class DataBaseHelper {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isMySql() {
|
||||
return DataBaseType.MY_SQL == getDataBaseType();
|
||||
}
|
||||
|
||||
public static boolean isOracle() {
|
||||
return DataBaseType.ORACLE == getDataBaseType();
|
||||
}
|
||||
|
||||
public static boolean isPostgerSql() {
|
||||
return DataBaseType.POSTGRE_SQL == getDataBaseType();
|
||||
}
|
||||
|
||||
public static boolean isSqlServer() {
|
||||
return DataBaseType.SQL_SERVER == getDataBaseType();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据当前数据库类型,生成兼容的 FIND_IN_SET 语句片段
|
||||
* <p>
|
||||
* 用于判断指定值是否存在于逗号分隔的字符串列中,SQL写法根据不同数据库方言自动切换:
|
||||
* - Oracle 使用 instr 函数
|
||||
* - PostgreSQL 使用 strpos 函数
|
||||
* - SQL Server 使用 charindex 函数
|
||||
* - 其他默认使用 MySQL 的 find_in_set 函数
|
||||
*
|
||||
* @param var1 要查找的值(支持任意类型,内部会转换成字符串)
|
||||
* @param var2 存储逗号分隔值的数据库列名
|
||||
* @return 适用于当前数据库的 SQL 条件字符串,通常用于 where 或 apply 中拼接
|
||||
*/
|
||||
public static String findInSet(Object var1, String var2) {
|
||||
DataBaseType dataBasyType = getDataBaseType();
|
||||
String var = Convert.toStr(var1);
|
||||
if (dataBasyType == DataBaseType.SQL_SERVER) {
|
||||
// charindex(',100,' , ',0,100,101,') <> 0
|
||||
return "charindex(',%s,' , ','+%s+',') <> 0".formatted(var, var2);
|
||||
} else if (dataBasyType == DataBaseType.POSTGRE_SQL) {
|
||||
// (select strpos(',0,100,101,' , ',100,')) <> 0
|
||||
return "(select strpos(','||%s||',' , ',%s,')) <> 0".formatted(var2, var);
|
||||
} else if (dataBasyType == DataBaseType.ORACLE) {
|
||||
return switch (getDataBaseType()) {
|
||||
// instr(',0,100,101,' , ',100,') <> 0
|
||||
return "instr(','||%s||',' , ',%s,') <> 0".formatted(var2, var);
|
||||
}
|
||||
case ORACLE -> "instr(','||%s||',' , ',%s,') <> 0".formatted(var2, var);
|
||||
// (select strpos(',0,100,101,' , ',100,')) <> 0
|
||||
case POSTGRE_SQL -> "(select strpos(','||%s||',' , ',%s,')) <> 0".formatted(var2, var);
|
||||
// charindex(',100,' , ',0,100,101,') <> 0
|
||||
case SQL_SERVER -> "charindex(',%s,' , ','+%s+',') <> 0".formatted(var, var2);
|
||||
// find_in_set(100 , '0,100,101')
|
||||
return "find_in_set('%s' , %s) <> 0".formatted(var, var2);
|
||||
default -> "find_in_set('%s' , %s) <> 0".formatted(var, var2);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3,6 +3,7 @@ package org.dromara.generator.util;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import org.dromara.common.mybatis.enums.DataBaseType;
|
||||
import org.dromara.generator.constant.GenConstants;
|
||||
import org.dromara.common.core.utils.DateUtils;
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
@ -118,11 +119,12 @@ public class VelocityUtils {
|
||||
templates.add("vm/java/serviceImpl.java.vm");
|
||||
templates.add("vm/java/controller.java.vm");
|
||||
templates.add("vm/xml/mapper.xml.vm");
|
||||
if (DataBaseHelper.isOracle()) {
|
||||
DataBaseType dataBaseType = DataBaseHelper.getDataBaseType();
|
||||
if (dataBaseType.isOracle()) {
|
||||
templates.add("vm/sql/oracle/sql.vm");
|
||||
} else if (DataBaseHelper.isPostgerSql()) {
|
||||
} else if (dataBaseType.isPostgreSql()) {
|
||||
templates.add("vm/sql/postgres/sql.vm");
|
||||
} else if (DataBaseHelper.isSqlServer()) {
|
||||
} else if (dataBaseType.isSqlServer()) {
|
||||
templates.add("vm/sql/sqlserver/sql.vm");
|
||||
} else {
|
||||
templates.add("vm/sql/sql.vm");
|
||||
|
Loading…
Reference in New Issue
Block a user