parent
8a89054c2f
commit
a68a32d9b6
@ -0,0 +1,49 @@
|
|||||||
|
package com.ruoyi.common.enums;
|
||||||
|
|
||||||
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库类型
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum DataBaseType {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MySQL
|
||||||
|
*/
|
||||||
|
MY_SQL("MySQL"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Oracle
|
||||||
|
*/
|
||||||
|
ORACLE("Oracle"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PostgreSQL
|
||||||
|
*/
|
||||||
|
POSTGRE_SQL("PostgreSQL"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SQL Server
|
||||||
|
*/
|
||||||
|
SQL_SERVER("Microsoft SQL Server");
|
||||||
|
|
||||||
|
private final String type;
|
||||||
|
|
||||||
|
public static DataBaseType find(String databaseProductName) {
|
||||||
|
if (StringUtils.isBlank(databaseProductName)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
for (DataBaseType type : values()) {
|
||||||
|
if (type.getType().equals(databaseProductName)) {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,61 @@
|
|||||||
|
package com.ruoyi.common.helper;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
||||||
|
import com.ruoyi.common.enums.DataBaseType;
|
||||||
|
import com.ruoyi.common.exception.ServiceException;
|
||||||
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.DatabaseMetaData;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库助手
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
|
public class DataBaseHelper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前数据库类型
|
||||||
|
*/
|
||||||
|
public static DataBaseType getDataBasyType() {
|
||||||
|
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) SpringUtils.getBean(DataSource.class);
|
||||||
|
DataSource dataSource = ds.determineDataSource();
|
||||||
|
try {
|
||||||
|
DatabaseMetaData metaData = dataSource.getConnection().getMetaData();
|
||||||
|
String databaseProductName = metaData.getDatabaseProductName();
|
||||||
|
return DataBaseType.find(databaseProductName);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new ServiceException(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isMySql() {
|
||||||
|
return DataBaseType.MY_SQL == getDataBasyType();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isOracle() {
|
||||||
|
return DataBaseType.ORACLE == getDataBasyType();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isPostgerSql() {
|
||||||
|
return DataBaseType.POSTGRE_SQL == getDataBasyType();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isSqlServer() {
|
||||||
|
return DataBaseType.SQL_SERVER == getDataBasyType();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String findInSet(Object var1, String var2) {
|
||||||
|
DataBaseType dataBasyType = getDataBasyType();
|
||||||
|
if (dataBasyType == DataBaseType.SQL_SERVER) {
|
||||||
|
return "charindex(" + Convert.toStr(var1) + ", " + var2 + ") <> 0";
|
||||||
|
}
|
||||||
|
return "find_in_set(" + Convert.toStr(var1) + ", " + var2 + ") <> 0";
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue