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; import java.lang.reflect.InvocationTargetException; /** * @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; } }