|
|
|
@ -26,7 +26,14 @@ public class DataBaseHelper {
|
|
|
|
private static final DynamicRoutingDataSource DS = SpringUtils.getBean(DynamicRoutingDataSource.class);
|
|
|
|
private static final DynamicRoutingDataSource DS = SpringUtils.getBean(DynamicRoutingDataSource.class);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取当前数据库类型
|
|
|
|
* 获取当前数据源对应的数据库类型
|
|
|
|
|
|
|
|
* <p>
|
|
|
|
|
|
|
|
* 通过 DynamicRoutingDataSource 获取当前线程绑定的数据源,
|
|
|
|
|
|
|
|
* 然后从数据源获取数据库连接,利用连接的元数据获取数据库产品名称,
|
|
|
|
|
|
|
|
* 最后调用 DataBaseType.find 方法将数据库名称转换为对应的枚举类型
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return 当前数据库对应的 DataBaseType 枚举,找不到时默认返回 MY_SQL
|
|
|
|
|
|
|
|
* @throws ServiceException 当获取数据库连接或元数据出现异常时抛出业务异常
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static DataBaseType getDataBaseType() {
|
|
|
|
public static DataBaseType getDataBaseType() {
|
|
|
|
DataSource dataSource = DS.determineDataSource();
|
|
|
|
DataSource dataSource = DS.determineDataSource();
|
|
|
|
@ -39,37 +46,31 @@ public class DataBaseHelper {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean isMySql() {
|
|
|
|
/**
|
|
|
|
return DataBaseType.MY_SQL == getDataBaseType();
|
|
|
|
* 根据当前数据库类型,生成兼容的 FIND_IN_SET 语句片段
|
|
|
|
}
|
|
|
|
* <p>
|
|
|
|
|
|
|
|
* 用于判断指定值是否存在于逗号分隔的字符串列中,SQL写法根据不同数据库方言自动切换:
|
|
|
|
public static boolean isOracle() {
|
|
|
|
* - Oracle 使用 instr 函数
|
|
|
|
return DataBaseType.ORACLE == getDataBaseType();
|
|
|
|
* - PostgreSQL 使用 strpos 函数
|
|
|
|
}
|
|
|
|
* - SQL Server 使用 charindex 函数
|
|
|
|
|
|
|
|
* - 其他默认使用 MySQL 的 find_in_set 函数
|
|
|
|
public static boolean isPostgerSql() {
|
|
|
|
*
|
|
|
|
return DataBaseType.POSTGRE_SQL == getDataBaseType();
|
|
|
|
* @param var1 要查找的值(支持任意类型,内部会转换成字符串)
|
|
|
|
}
|
|
|
|
* @param var2 存储逗号分隔值的数据库列名
|
|
|
|
|
|
|
|
* @return 适用于当前数据库的 SQL 条件字符串,通常用于 where 或 apply 中拼接
|
|
|
|
public static boolean isSqlServer() {
|
|
|
|
*/
|
|
|
|
return DataBaseType.SQL_SERVER == getDataBaseType();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String findInSet(Object var1, String var2) {
|
|
|
|
public static String findInSet(Object var1, String var2) {
|
|
|
|
DataBaseType dataBasyType = getDataBaseType();
|
|
|
|
|
|
|
|
String var = Convert.toStr(var1);
|
|
|
|
String var = Convert.toStr(var1);
|
|
|
|
if (dataBasyType == DataBaseType.SQL_SERVER) {
|
|
|
|
return switch (getDataBaseType()) {
|
|
|
|
// 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) {
|
|
|
|
|
|
|
|
// instr(',0,100,101,' , ',100,') <> 0
|
|
|
|
// 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
|
|
|
|
// find_in_set('100' , '0,100,101')
|
|
|
|
case POSTGRE_SQL -> "(select strpos(','||%s||',' , ',%s,')) <> 0".formatted(var2, var);
|
|
|
|
return "find_in_set('%s' , %s) <> 0".formatted(var, var2);
|
|
|
|
// charindex(',100,' , ',0,100,101,') <> 0
|
|
|
|
|
|
|
|
case SQL_SERVER -> "charindex(',%s,' , ','+%s+',') <> 0".formatted(var, var2);
|
|
|
|
|
|
|
|
// find_in_set(100 , '0,100,101')
|
|
|
|
|
|
|
|
default -> "find_in_set('%s' , %s) <> 0".formatted(var, var2);
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
|