疯狂的狮子li
2022-03-11 333a38978e9f46b0768e7c19ea3e870bb0cf8508
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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";
    }
}