mirror of https://gitee.com/stylefeng/roses
【7.1.5】整合两个dbType
parent
ac7afdc2a1
commit
c2eca322dd
|
@ -17,30 +17,47 @@ public enum DbTypeEnum {
|
|||
/**
|
||||
* mysql
|
||||
*/
|
||||
MYSQL("mysql", "mysql"),
|
||||
MYSQL("mysql", "mysql", "select 1"),
|
||||
|
||||
/**
|
||||
* pgsql
|
||||
*/
|
||||
PG_SQL("pgsql", "postgresql"),
|
||||
PG_SQL("postgresql", "pgsql", "select version()"),
|
||||
|
||||
/**
|
||||
* oracle
|
||||
*/
|
||||
ORACLE("oracle", "oracle"),
|
||||
ORACLE("oracle", "oracle", "select 1 from dual"),
|
||||
|
||||
/**
|
||||
* 达梦(使用oracle的mapping.xml)
|
||||
*/
|
||||
DM("dm", "oracle", "select 1 from dual"),
|
||||
|
||||
/**
|
||||
* mssql
|
||||
*/
|
||||
MS_SQL("mssql", "sqlserver");
|
||||
MS_SQL("sqlserver", "mssql", "select 1");
|
||||
|
||||
private final String code;
|
||||
/**
|
||||
* spring.datasource.url中包含的关键字
|
||||
*/
|
||||
private final String urlWords;
|
||||
|
||||
private final String name;
|
||||
/**
|
||||
* mapping.xml使用databaseId="xxx"来标识的关键字
|
||||
*/
|
||||
private final String xmlDatabaseId;
|
||||
|
||||
DbTypeEnum(String code, String name) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
/**
|
||||
* validateQuery所使用的语句
|
||||
*/
|
||||
private final String validateQuery;
|
||||
|
||||
DbTypeEnum(String urlWords, String xmlDatabaseId, String validateQuery) {
|
||||
this.urlWords = urlWords;
|
||||
this.xmlDatabaseId = xmlDatabaseId;
|
||||
this.validateQuery = validateQuery;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -46,15 +46,14 @@ public class DatabaseTypeUtil {
|
|||
return DbTypeEnum.MYSQL;
|
||||
}
|
||||
|
||||
if (jdbcUrl.contains(DbTypeEnum.ORACLE.getName())) {
|
||||
return DbTypeEnum.ORACLE;
|
||||
} else if (jdbcUrl.contains(DbTypeEnum.PG_SQL.getName())) {
|
||||
return DbTypeEnum.PG_SQL;
|
||||
} else if (jdbcUrl.contains(DbTypeEnum.MS_SQL.getName())) {
|
||||
return DbTypeEnum.MS_SQL;
|
||||
} else {
|
||||
return DbTypeEnum.MYSQL;
|
||||
// url字符串中包含了dbTypeEnum的name,则判定为该类型
|
||||
for (DbTypeEnum dbTypeEnum : DbTypeEnum.values()) {
|
||||
if (jdbcUrl.contains(dbTypeEnum.getUrlWords())) {
|
||||
return dbTypeEnum;
|
||||
}
|
||||
}
|
||||
|
||||
return DbTypeEnum.MYSQL;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,6 +32,8 @@ public class SysConfigDataFactory {
|
|||
return new MssqlSysConfigData();
|
||||
} else if (DbTypeEnum.ORACLE.equals(dbType)) {
|
||||
return new OracleSysConfigData();
|
||||
} else if (DbTypeEnum.DM.equals(dbType)) {
|
||||
return new OracleSysConfigData();
|
||||
}
|
||||
return new MysqlSysConfigData();
|
||||
}
|
||||
|
|
|
@ -1,75 +0,0 @@
|
|||
/*
|
||||
* Copyright [2020-2030] [https://www.stylefeng.cn]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Guns源码头部的版权声明。
|
||||
* 3.请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 6.若您的项目无法满足以上几点,可申请商业授权
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.db.api.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 不同数据库类型的枚举
|
||||
* <p>
|
||||
* 用于标识mapping.xml中不同数据库的标识
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/6/20 21:08
|
||||
*/
|
||||
@Getter
|
||||
public enum DbTypeEnum {
|
||||
|
||||
/**
|
||||
* mysql
|
||||
*/
|
||||
MYSQL("mysql", "select 1"),
|
||||
|
||||
/**
|
||||
* pgsql
|
||||
*/
|
||||
PG_SQL("postgresql", "select version()"),
|
||||
|
||||
/**
|
||||
* oracle
|
||||
*/
|
||||
ORACLE("oracle", "select 1 from dual"),
|
||||
|
||||
/**
|
||||
* mssql
|
||||
*/
|
||||
MS_SQL("sqlserver", "select 1");
|
||||
|
||||
/**
|
||||
* 数据库编码,也是datasource url上会有的关键字
|
||||
*/
|
||||
private final String code;
|
||||
|
||||
/**
|
||||
* 检测数据库是否运行的sql语句
|
||||
*/
|
||||
private final String validateQuery;
|
||||
|
||||
DbTypeEnum(String code, String validateQuery) {
|
||||
this.code = code;
|
||||
this.validateQuery = validateQuery;
|
||||
}
|
||||
|
||||
}
|
|
@ -25,8 +25,8 @@
|
|||
package cn.stylefeng.roses.kernel.db.api.factory;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.stylefeng.roses.kernel.db.api.enums.DbTypeEnum;
|
||||
import cn.stylefeng.roses.kernel.db.api.pojo.druid.DruidProperties;
|
||||
import cn.stylefeng.roses.kernel.rule.enums.DbTypeEnum;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
@ -117,14 +117,10 @@ public class DruidDatasourceFactory {
|
|||
* @date 2020/10/16 16:12
|
||||
*/
|
||||
private static String getValidateQueryByUrl(String url) {
|
||||
if (url.contains(DbTypeEnum.ORACLE.getCode())) {
|
||||
return DbTypeEnum.ORACLE.getValidateQuery();
|
||||
}
|
||||
if (url.contains(DbTypeEnum.MS_SQL.getCode())) {
|
||||
return DbTypeEnum.MS_SQL.getValidateQuery();
|
||||
}
|
||||
if (url.contains(DbTypeEnum.PG_SQL.getCode())) {
|
||||
return DbTypeEnum.PG_SQL.getValidateQuery();
|
||||
for (DbTypeEnum value : DbTypeEnum.values()) {
|
||||
if (url.contains(value.getUrlWords())) {
|
||||
return value.getValidateQuery();
|
||||
}
|
||||
}
|
||||
|
||||
return DbTypeEnum.MYSQL.getValidateQuery();
|
||||
|
|
|
@ -24,7 +24,8 @@
|
|||
*/
|
||||
package cn.stylefeng.roses.kernel.db.api.sqladapter;
|
||||
|
||||
import cn.stylefeng.roses.kernel.db.api.enums.DbTypeEnum;
|
||||
|
||||
import cn.stylefeng.roses.kernel.rule.enums.DbTypeEnum;
|
||||
|
||||
/**
|
||||
* 异构sql获取基类,通过继承此类,编写使用不同数据库的sql
|
||||
|
@ -43,13 +44,16 @@ public abstract class AbstractSql {
|
|||
* @date 2020/10/31 23:44
|
||||
*/
|
||||
public String getSql(String jdbcUrl) {
|
||||
if (jdbcUrl.contains(DbTypeEnum.ORACLE.getCode())) {
|
||||
if (jdbcUrl.contains(DbTypeEnum.ORACLE.getUrlWords())) {
|
||||
return oracle();
|
||||
}
|
||||
if (jdbcUrl.contains(DbTypeEnum.MS_SQL.getCode())) {
|
||||
if (jdbcUrl.contains(DbTypeEnum.DM.getUrlWords())) {
|
||||
return oracle();
|
||||
}
|
||||
if (jdbcUrl.contains(DbTypeEnum.MS_SQL.getUrlWords())) {
|
||||
return sqlServer();
|
||||
}
|
||||
if (jdbcUrl.contains(DbTypeEnum.PG_SQL.getCode())) {
|
||||
if (jdbcUrl.contains(DbTypeEnum.PG_SQL.getUrlWords())) {
|
||||
return pgSql();
|
||||
}
|
||||
return mysql();
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
*/
|
||||
package cn.stylefeng.roses.kernel.db.mp.dbid;
|
||||
|
||||
import cn.stylefeng.roses.kernel.db.api.enums.DbTypeEnum;
|
||||
import cn.stylefeng.roses.kernel.rule.enums.DbTypeEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.mapping.DatabaseIdProvider;
|
||||
|
||||
|
@ -66,21 +66,26 @@ public class CustomDatabaseIdProvider implements DatabaseIdProvider {
|
|||
|
||||
} catch (Exception e2) {
|
||||
log.warn("CustomDatabaseIdProvider无法判断当前数据源类型,默认选择Mysql类型");
|
||||
return DbTypeEnum.MYSQL.getCode();
|
||||
return DbTypeEnum.MYSQL.getUrlWords();
|
||||
}
|
||||
}
|
||||
|
||||
if (url.contains(DbTypeEnum.ORACLE.getCode())) {
|
||||
return DbTypeEnum.ORACLE.getCode();
|
||||
// 达梦和oracle使用同一种
|
||||
if (url.contains(DbTypeEnum.ORACLE.getUrlWords())) {
|
||||
return DbTypeEnum.ORACLE.getUrlWords();
|
||||
}
|
||||
if (url.contains(DbTypeEnum.MS_SQL.getCode())) {
|
||||
return DbTypeEnum.MS_SQL.getCode();
|
||||
}
|
||||
if (url.contains(DbTypeEnum.PG_SQL.getCode())) {
|
||||
return DbTypeEnum.PG_SQL.getCode();
|
||||
if (url.contains(DbTypeEnum.DM.getUrlWords())) {
|
||||
return DbTypeEnum.ORACLE.getUrlWords();
|
||||
}
|
||||
|
||||
return DbTypeEnum.MYSQL.getCode();
|
||||
if (url.contains(DbTypeEnum.MS_SQL.getUrlWords())) {
|
||||
return DbTypeEnum.MS_SQL.getUrlWords();
|
||||
}
|
||||
if (url.contains(DbTypeEnum.PG_SQL.getUrlWords())) {
|
||||
return DbTypeEnum.PG_SQL.getUrlWords();
|
||||
}
|
||||
|
||||
return DbTypeEnum.MYSQL.getUrlWords();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue