许鹏程
2023-06-29 e7ccc6e4243d7d7bfb4a950367b2cdc26428915f
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.product.datasource.config;
 
import com.product.core.exception.BaseException;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.data.config.DatabaseType;
import com.product.data.config.DbValueConfig;
import com.product.datasource.dao.Dao;
import com.product.datasource.dao.impl.MysqlDaoImpl;
import com.product.datasource.dao.impl.OracleDaoImpl;
import com.product.datasource.entity.DataBaseEntity;
 
import java.lang.reflect.Constructor;
 
/**
 * @Author cheng
 * @Date 2022/7/6 16:42
 * @Desc 数据源类型
 */
public enum DataBaseType {
 
    ORACLE(DbValueConfig.Oracle, "oracle.jdbc.driver.OracleDriver", "select 1 from dual", OracleDaoImpl.class),
 
    MYSQL(DbValueConfig.MySql, "com.mysql.cj.jdbc.Driver", "SELECT 1", MysqlDaoImpl.class),
 
    SQLSERVER(DbValueConfig.SqlServer, "com.microsoft.sqlserver.jdbc.SQLServerDriver", "SELECT 1", null),
 
    INFORMIX(DbValueConfig.Informix, "com.informix.jdbc.IfxDriver", "select count(*) from systables", null),
 
    PSQL(DbValueConfig.PSQL, "org.postgresql.Driver", "select 1 ", null),
 
    ;
 
 
    private DbValueConfig value;
    private String validationQuery;
 
    private String driver;
 
    private Class implClass;
 
    public Dao getDao(DataBaseEntity dataBaseEntity) throws BaseException {
        try {
            Constructor cons = implClass.getConstructor(DataBaseEntity.class);
            return (Dao) cons.newInstance(dataBaseEntity);
        } catch (Exception e) {
            e.printStackTrace();
            SpringMVCContextHolder.getSystemLogger().error(e);
            throw new BaseException(ErrorCode.GET_DAO_IMPL_ERROR);
        }
    }
 
    /**
     * 转换为标准产品中的DatabaseType 枚举
     * @return
     */
    public DatabaseType getStandardDatabaseType(){
        return DatabaseType.getDataBaseType(this.value.getValue());
    }
    public static DataBaseType getDataBaseType(int type) {
        DataBaseType[] values = DataBaseType.values();
        for (DataBaseType value : values) {
            if (value.value.getValue() == type) {
                return value;
            }
        }
        return null;
    }
 
 
    DataBaseType(DbValueConfig value, String driver, String validationQuery, Class implClass) {
        this.driver = driver;
        this.value = value;
        this.validationQuery = validationQuery;
        this.implClass = implClass;
    }
 
    public int getValue() {
        return value.getValue();
    }
 
    public String getValidationQuery() {
        return validationQuery;
    }
 
    public String getDriver() {
        return driver;
    }
}