|
@@ -26,6 +26,7 @@ import lombok.Data;
|
|
import lombok.experimental.Accessors;
|
|
import lombok.experimental.Accessors;
|
|
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.Connection;
|
|
|
|
+import java.sql.Driver;
|
|
import java.sql.DriverManager;
|
|
import java.sql.DriverManager;
|
|
import java.sql.SQLException;
|
|
import java.sql.SQLException;
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
@@ -79,6 +80,142 @@ public class DataSourceConfig {
|
|
*/
|
|
*/
|
|
private String password;
|
|
private String password;
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 后续不再公开次构造方法
|
|
|
|
+ *
|
|
|
|
+ * @see Builder#Builder(String, String, String)
|
|
|
|
+ * @deprecated 3.4.1
|
|
|
|
+ */
|
|
|
|
+ @Deprecated
|
|
|
|
+ public DataSourceConfig() {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置表数据查询实现类
|
|
|
|
+ *
|
|
|
|
+ * @param dbQuery 表数据查询
|
|
|
|
+ * @return this
|
|
|
|
+ * @see Builder#dbQuery(IDbQuery)
|
|
|
|
+ * @deprecated 3.4.1
|
|
|
|
+ */
|
|
|
|
+ @Deprecated
|
|
|
|
+ public DataSourceConfig setDbQuery(IDbQuery dbQuery) {
|
|
|
|
+ this.dbQuery = dbQuery;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置数据库类型
|
|
|
|
+ *
|
|
|
|
+ * @param dbType 数据库类型
|
|
|
|
+ * @return this
|
|
|
|
+ * @see Builder#dbType(DbType)
|
|
|
|
+ * @deprecated 3.4.1
|
|
|
|
+ */
|
|
|
|
+ @Deprecated
|
|
|
|
+ public DataSourceConfig setDbType(DbType dbType) {
|
|
|
|
+ this.dbType = dbType;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置数据库schema
|
|
|
|
+ *
|
|
|
|
+ * @param schemaName 指定schema
|
|
|
|
+ * @return this
|
|
|
|
+ * @see Builder#schema(String)
|
|
|
|
+ * @deprecated 3.4.1
|
|
|
|
+ */
|
|
|
|
+ @Deprecated
|
|
|
|
+ public DataSourceConfig setSchemaName(String schemaName) {
|
|
|
|
+ this.schemaName = schemaName;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置数据库字段转换实现
|
|
|
|
+ *
|
|
|
|
+ * @param typeConvert 数据库字段转换实现
|
|
|
|
+ * @return this
|
|
|
|
+ * @see Builder#typeConvert(ITypeConvert)
|
|
|
|
+ * @deprecated 3.4.1
|
|
|
|
+ */
|
|
|
|
+ @Deprecated
|
|
|
|
+ public DataSourceConfig setTypeConvert(ITypeConvert typeConvert) {
|
|
|
|
+ this.typeConvert = typeConvert;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置关键字处理器
|
|
|
|
+ *
|
|
|
|
+ * @param keyWordsHandler 关键字处理器
|
|
|
|
+ * @return this
|
|
|
|
+ * @see Builder#keyWordsHandler(IKeyWordsHandler)
|
|
|
|
+ * @deprecated 3.4.1
|
|
|
|
+ */
|
|
|
|
+ @Deprecated
|
|
|
|
+ public DataSourceConfig setKeyWordsHandler(IKeyWordsHandler keyWordsHandler) {
|
|
|
|
+ this.keyWordsHandler = keyWordsHandler;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置数据库连接地址
|
|
|
|
+ *
|
|
|
|
+ * @param url 数据库连接地址
|
|
|
|
+ * @return this
|
|
|
|
+ * @see Builder#Builder(java.lang.String, java.lang.String, java.lang.String)
|
|
|
|
+ * @deprecated 3.4.1
|
|
|
|
+ */
|
|
|
|
+ @Deprecated
|
|
|
|
+ public DataSourceConfig setUrl(String url) {
|
|
|
|
+ this.url = url;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置驱动名称
|
|
|
|
+ *
|
|
|
|
+ * @param driverName 驱动名
|
|
|
|
+ * @return this
|
|
|
|
+ * @see Builder#driver(String)
|
|
|
|
+ * @deprecated 3.4.1
|
|
|
|
+ */
|
|
|
|
+ @Deprecated
|
|
|
|
+ public DataSourceConfig setDriverName(String driverName) {
|
|
|
|
+ this.driverName = driverName;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置数据库账号
|
|
|
|
+ *
|
|
|
|
+ * @param username 数据库账号
|
|
|
|
+ * @return this
|
|
|
|
+ * @see Builder#Builder(java.lang.String, java.lang.String, java.lang.String)
|
|
|
|
+ * @deprecated 3.4.1
|
|
|
|
+ */
|
|
|
|
+ @Deprecated
|
|
|
|
+ public DataSourceConfig setUsername(String username) {
|
|
|
|
+ this.username = username;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置数据库密码
|
|
|
|
+ *
|
|
|
|
+ * @param password 数据库密码
|
|
|
|
+ * @return this
|
|
|
|
+ * @see Builder#Builder(java.lang.String, java.lang.String, java.lang.String)
|
|
|
|
+ * @deprecated 3.4.1
|
|
|
|
+ */
|
|
|
|
+ @Deprecated
|
|
|
|
+ public DataSourceConfig setPassword(String password) {
|
|
|
|
+ this.password = password;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
public IDbQuery getDbQuery() {
|
|
public IDbQuery getDbQuery() {
|
|
if (null == dbQuery) {
|
|
if (null == dbQuery) {
|
|
DbType dbType = getDbType();
|
|
DbType dbType = getDbType();
|
|
@@ -97,9 +234,9 @@ public class DataSourceConfig {
|
|
*/
|
|
*/
|
|
public DbType getDbType() {
|
|
public DbType getDbType() {
|
|
if (null == this.dbType) {
|
|
if (null == this.dbType) {
|
|
- this.dbType = this.getDbType(this.driverName);
|
|
|
|
|
|
+ this.dbType = this.getDbType(this.url.toLowerCase());
|
|
if (null == this.dbType) {
|
|
if (null == this.dbType) {
|
|
- this.dbType = this.getDbType(this.url.toLowerCase());
|
|
|
|
|
|
+ this.dbType = this.getDbType(this.driverName);
|
|
if (null == this.dbType) {
|
|
if (null == this.dbType) {
|
|
throw ExceptionUtils.mpe("Unknown type of database!");
|
|
throw ExceptionUtils.mpe("Unknown type of database!");
|
|
}
|
|
}
|
|
@@ -116,34 +253,33 @@ public class DataSourceConfig {
|
|
* @return 类型枚举值,如果没找到,则返回 null
|
|
* @return 类型枚举值,如果没找到,则返回 null
|
|
*/
|
|
*/
|
|
private DbType getDbType(String str) {
|
|
private DbType getDbType(String str) {
|
|
- if (str.contains("mysql")) {
|
|
|
|
|
|
+ if (url.contains(":mysql:") || url.contains(":cobar:")) {
|
|
return DbType.MYSQL;
|
|
return DbType.MYSQL;
|
|
- } else if (str.contains("oracle")) {
|
|
|
|
|
|
+ } else if (str.contains(":oracle:")) {
|
|
return DbType.ORACLE;
|
|
return DbType.ORACLE;
|
|
- } else if (str.contains("postgresql")) {
|
|
|
|
|
|
+ } else if (str.contains(":postgresql:")) {
|
|
return DbType.POSTGRE_SQL;
|
|
return DbType.POSTGRE_SQL;
|
|
- } else if (str.contains("sqlserver")) {
|
|
|
|
|
|
+ } else if (str.contains(":sqlserver:")) {
|
|
return DbType.SQL_SERVER;
|
|
return DbType.SQL_SERVER;
|
|
- } else if (str.contains("db2")) {
|
|
|
|
|
|
+ } else if (str.contains(":db2:")) {
|
|
return DbType.DB2;
|
|
return DbType.DB2;
|
|
- } else if (str.contains("mariadb")) {
|
|
|
|
|
|
+ } else if (str.contains(":mariadb:")) {
|
|
return DbType.MARIADB;
|
|
return DbType.MARIADB;
|
|
- } else if (str.contains("sqlite")) {
|
|
|
|
|
|
+ } else if (str.contains(":sqlite:")) {
|
|
return DbType.SQLITE;
|
|
return DbType.SQLITE;
|
|
- } else if (str.contains("h2")) {
|
|
|
|
|
|
+ } else if (str.contains(":h2:")) {
|
|
return DbType.H2;
|
|
return DbType.H2;
|
|
- } else if (str.contains("kingbase") || str.contains("kingbase8")) {
|
|
|
|
|
|
+ } else if (str.contains(":kingbase:") || str.contains(":kingbase8:")) {
|
|
return DbType.KINGBASE_ES;
|
|
return DbType.KINGBASE_ES;
|
|
- } else if (str.contains("dm")) {
|
|
|
|
|
|
+ } else if (str.contains(":dm:")) {
|
|
return DbType.DM;
|
|
return DbType.DM;
|
|
- } else if (str.contains("zenith")) {
|
|
|
|
|
|
+ } else if (str.contains(":zenith:")) {
|
|
return DbType.GAUSS;
|
|
return DbType.GAUSS;
|
|
- } else if (str.contains("oscar")) {
|
|
|
|
|
|
+ } else if (str.contains(":oscar:")) {
|
|
return DbType.OSCAR;
|
|
return DbType.OSCAR;
|
|
- } else if (str.contains("firebird")) {
|
|
|
|
|
|
+ } else if (str.contains(":firebird:")) {
|
|
return DbType.FIREBIRD;
|
|
return DbType.FIREBIRD;
|
|
- }
|
|
|
|
- else {
|
|
|
|
|
|
+ } else {
|
|
return DbType.OTHER;
|
|
return DbType.OTHER;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -170,7 +306,9 @@ public class DataSourceConfig {
|
|
public Connection getConn() {
|
|
public Connection getConn() {
|
|
Connection conn;
|
|
Connection conn;
|
|
try {
|
|
try {
|
|
- Class.forName(this.driverName);
|
|
|
|
|
|
+ if (StringUtils.isNotBlank(this.driverName)) {
|
|
|
|
+ Class.forName(this.driverName);
|
|
|
|
+ }
|
|
conn = DriverManager.getConnection(this.url, this.username, this.password);
|
|
conn = DriverManager.getConnection(this.url, this.username, this.password);
|
|
String schema = StringUtils.isNotBlank(this.schemaName) ? this.schemaName : getDefaultSchema();
|
|
String schema = StringUtils.isNotBlank(this.schemaName) ? this.schemaName : getDefaultSchema();
|
|
if (StringUtils.isNotBlank(schema)) {
|
|
if (StringUtils.isNotBlank(schema)) {
|
|
@@ -207,4 +345,113 @@ public class DataSourceConfig {
|
|
}
|
|
}
|
|
return schema;
|
|
return schema;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 数据库配置构建者
|
|
|
|
+ *
|
|
|
|
+ * @author nieqiurong 2020/10/10.
|
|
|
|
+ * @since 3.4.1
|
|
|
|
+ */
|
|
|
|
+ public static class Builder {
|
|
|
|
+
|
|
|
|
+ private final DataSourceConfig dataSourceConfig = new DataSourceConfig();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 构造初始化方法
|
|
|
|
+ *
|
|
|
|
+ * @param url 数据库连接地址
|
|
|
|
+ * @param username 数据库账号
|
|
|
|
+ * @param password 数据库密码
|
|
|
|
+ */
|
|
|
|
+ public Builder(String url, String username, String password) {
|
|
|
|
+ this.dataSourceConfig.url = url;
|
|
|
|
+ this.dataSourceConfig.username = username;
|
|
|
|
+ this.dataSourceConfig.password = password;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置数据库查询实现
|
|
|
|
+ *
|
|
|
|
+ * @param dbQuery 数据库查询实现
|
|
|
|
+ * @return this
|
|
|
|
+ */
|
|
|
|
+ public Builder dbQuery(IDbQuery dbQuery) {
|
|
|
|
+ this.dataSourceConfig.dbQuery = dbQuery;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置数据库类型
|
|
|
|
+ *
|
|
|
|
+ * @param dbType 数据库类型
|
|
|
|
+ * @return this
|
|
|
|
+ */
|
|
|
|
+ public Builder dbType(DbType dbType) {
|
|
|
|
+ this.dataSourceConfig.dbType = dbType;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置数据库schema
|
|
|
|
+ *
|
|
|
|
+ * @param schemaName 数据库schema
|
|
|
|
+ * @return this
|
|
|
|
+ */
|
|
|
|
+ public Builder schema(String schemaName) {
|
|
|
|
+ this.dataSourceConfig.schemaName = schemaName;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置数据库驱动
|
|
|
|
+ *
|
|
|
|
+ * @param driverName 驱动名
|
|
|
|
+ * @return this
|
|
|
|
+ */
|
|
|
|
+ public Builder driver(String driverName) {
|
|
|
|
+ this.dataSourceConfig.driverName = driverName;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置数据库驱动
|
|
|
|
+ *
|
|
|
|
+ * @param driver 驱动类
|
|
|
|
+ * @return this
|
|
|
|
+ */
|
|
|
|
+ public Builder driver(Class<? extends Driver> driver) {
|
|
|
|
+ return driver(driver.getName());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置类型转换器
|
|
|
|
+ *
|
|
|
|
+ * @param typeConvert 类型转换器
|
|
|
|
+ * @return this
|
|
|
|
+ */
|
|
|
|
+ public Builder typeConvert(ITypeConvert typeConvert) {
|
|
|
|
+ this.dataSourceConfig.typeConvert = typeConvert;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 设置数据库关键字处理器
|
|
|
|
+ *
|
|
|
|
+ * @param keyWordsHandler 关键字处理器
|
|
|
|
+ * @return this
|
|
|
|
+ */
|
|
|
|
+ public Builder keyWordsHandler(IKeyWordsHandler keyWordsHandler) {
|
|
|
|
+ this.dataSourceConfig.keyWordsHandler = keyWordsHandler;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 构建数据库配置
|
|
|
|
+ *
|
|
|
|
+ * @return 数据库配置
|
|
|
|
+ */
|
|
|
|
+ public DataSourceConfig build() {
|
|
|
|
+ return this.dataSourceConfig;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|