mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-08-02 21:50:43 +08:00
Driver configuration is configured through json,
Support driver attribute configuration.
This commit is contained in:
@ -77,7 +77,7 @@
|
||||
<dependency>
|
||||
<groupId>org.locationtech.jts</groupId>
|
||||
<artifactId>jts-core</artifactId>
|
||||
<version>1.18.1</version> <!-- 确保使用最新的版本 -->
|
||||
<version>1.19.0</version> <!-- 确保使用最新的版本 -->
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -1,7 +1,7 @@
|
||||
|
||||
package ai.chat2db.spi.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -9,7 +9,6 @@ import java.util.List;
|
||||
* @author jipengfei
|
||||
* @version : DBConfig.java
|
||||
*/
|
||||
@Data
|
||||
public class DBConfig {
|
||||
|
||||
/**
|
||||
@ -32,7 +31,6 @@ public class DBConfig {
|
||||
*/
|
||||
private List<DriverConfig> driverConfigList;
|
||||
|
||||
|
||||
/**
|
||||
* 建表语句
|
||||
*/
|
||||
@ -42,4 +40,73 @@ public class DBConfig {
|
||||
* 修改表结构
|
||||
*/
|
||||
private String simpleAlterTable;
|
||||
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public DriverConfig getDefaultDriverConfig() {
|
||||
if (this.defaultDriverConfig != null) {
|
||||
return this.defaultDriverConfig;
|
||||
} else {
|
||||
if (!CollectionUtils.isEmpty(driverConfigList)) {
|
||||
for (DriverConfig driverConfig : driverConfigList) {
|
||||
if (driverConfig.isDefaultDriver()) {
|
||||
return driverConfig;
|
||||
}
|
||||
}
|
||||
return driverConfigList.get(0);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setDefaultDriverConfig(DriverConfig defaultDriverConfig) {
|
||||
this.defaultDriverConfig = defaultDriverConfig;
|
||||
}
|
||||
|
||||
public List<DriverConfig> getDriverConfigList() {
|
||||
return driverConfigList;
|
||||
}
|
||||
|
||||
public void setDriverConfigList(List<DriverConfig> driverConfigList) {
|
||||
this.driverConfigList = driverConfigList;
|
||||
if (!CollectionUtils.isEmpty(driverConfigList)) {
|
||||
for (DriverConfig driverConfig : driverConfigList) {
|
||||
if (driverConfig.isDefaultDriver()) {
|
||||
this.defaultDriverConfig = driverConfig;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getSimpleCreateTable() {
|
||||
return simpleCreateTable;
|
||||
}
|
||||
|
||||
public void setSimpleCreateTable(String simpleCreateTable) {
|
||||
this.simpleCreateTable = simpleCreateTable;
|
||||
}
|
||||
|
||||
public String getSimpleAlterTable() {
|
||||
return simpleAlterTable;
|
||||
}
|
||||
|
||||
public void setSimpleAlterTable(String simpleAlterTable) {
|
||||
this.simpleAlterTable = simpleAlterTable;
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package ai.chat2db.spi.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import ai.chat2db.spi.model.KeyValue;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
@ -12,6 +13,11 @@ import org.apache.commons.lang3.StringUtils;
|
||||
*/
|
||||
@Data
|
||||
public class DriverConfig {
|
||||
|
||||
/**
|
||||
* url
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* jdbcDriver
|
||||
*/
|
||||
@ -22,17 +28,14 @@ public class DriverConfig {
|
||||
*/
|
||||
private String jdbcDriverClass;
|
||||
|
||||
///**
|
||||
// * name
|
||||
// */
|
||||
//private String name;
|
||||
|
||||
/**
|
||||
* downloadJdbcDriverUrls
|
||||
*/
|
||||
private List<String> downloadJdbcDriverUrls;
|
||||
|
||||
|
||||
/**
|
||||
* dbType
|
||||
*/
|
||||
private String dbType;
|
||||
|
||||
/**
|
||||
@ -40,6 +43,13 @@ public class DriverConfig {
|
||||
*/
|
||||
private boolean custom;
|
||||
|
||||
/**
|
||||
* properties
|
||||
*/
|
||||
private List<KeyValue> extendInfo;
|
||||
|
||||
|
||||
private boolean defaultDriver;
|
||||
|
||||
public boolean notEmpty() {
|
||||
return StringUtils.isNotBlank(getJdbcDriver()) && StringUtils.isNotBlank(
|
||||
|
@ -23,7 +23,17 @@ public class KeyValue implements Serializable {
|
||||
/**
|
||||
* 属性值
|
||||
*/
|
||||
private Object value;
|
||||
private String value;
|
||||
|
||||
/**
|
||||
* 是否必填
|
||||
*/
|
||||
private boolean required;
|
||||
|
||||
/**
|
||||
* 选项
|
||||
*/
|
||||
private List<String> choices;
|
||||
|
||||
public static Map<String, Object> toMap(List<KeyValue> keyValues) {
|
||||
if (CollectionUtils.isEmpty(keyValues)) {
|
||||
|
@ -7,6 +7,7 @@ import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.sql.Connection;
|
||||
import java.sql.Driver;
|
||||
import java.sql.DriverPropertyInfo;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
@ -39,7 +40,7 @@ public class IDriverManager {
|
||||
}
|
||||
|
||||
public static Connection getConnection(String url, String user, String password, DriverConfig driver)
|
||||
throws SQLException {
|
||||
throws SQLException {
|
||||
Properties info = new Properties();
|
||||
if (user != null) {
|
||||
info.put("user", user);
|
||||
@ -53,8 +54,8 @@ public class IDriverManager {
|
||||
}
|
||||
|
||||
public static Connection getConnection(String url, String user, String password, DriverConfig driver,
|
||||
Map<String, Object> properties)
|
||||
throws SQLException {
|
||||
Map<String, Object> properties)
|
||||
throws SQLException {
|
||||
Properties info = new Properties();
|
||||
if (StringUtils.isNotEmpty(user)) {
|
||||
info.put("user", user);
|
||||
@ -74,7 +75,7 @@ public class IDriverManager {
|
||||
}
|
||||
|
||||
public static Connection getConnection(String url, Properties info, DriverConfig driver)
|
||||
throws SQLException {
|
||||
throws SQLException {
|
||||
if (url == null) {
|
||||
throw new SQLException("The url cannot be null", "08001");
|
||||
}
|
||||
@ -84,8 +85,8 @@ public class IDriverManager {
|
||||
}
|
||||
try {
|
||||
Connection connection = driverEntry.getDriver().connect(url, info);
|
||||
if(connection == null){
|
||||
throw new SQLException("driver.connect return null , No suitable driver found for url " +url ,"08001");
|
||||
if (connection == null) {
|
||||
throw new SQLException("driver.connect return null , No suitable driver found for url " + url, "08001");
|
||||
}
|
||||
return connection;
|
||||
} catch (SQLException var7) {
|
||||
@ -94,13 +95,31 @@ public class IDriverManager {
|
||||
return con;
|
||||
} else {
|
||||
throw new SQLException("Cannot create connection (" + var7.getMessage() + ")", "08001",
|
||||
var7);
|
||||
var7);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static DriverPropertyInfo[] getProperty(DriverConfig driver)
|
||||
throws SQLException {
|
||||
if (driver == null) {
|
||||
return null;
|
||||
}
|
||||
DriverEntry driverEntry = DRIVER_ENTRY_MAP.get(driver.getJdbcDriver());
|
||||
if (driverEntry == null) {
|
||||
driverEntry = getJDBCDriver(driver);
|
||||
}
|
||||
try {
|
||||
String url = driver.getUrl() == null ? "" : driver.getUrl();
|
||||
return driverEntry.getDriver().getPropertyInfo(url, null);
|
||||
} catch (Exception var7) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static Connection tryConnectionAgain(DriverEntry driverEntry, String url,
|
||||
Properties info) throws SQLException {
|
||||
Properties info) throws SQLException {
|
||||
if (url.contains("mysql")) {
|
||||
if (!info.containsKey("useSSL")) {
|
||||
info.put("useSSL", "false");
|
||||
@ -111,14 +130,14 @@ public class IDriverManager {
|
||||
}
|
||||
|
||||
private static DriverEntry getJDBCDriver(DriverConfig driver)
|
||||
throws SQLException {
|
||||
throws SQLException {
|
||||
synchronized (driver) {
|
||||
try {
|
||||
if (DRIVER_ENTRY_MAP.containsKey(driver.getJdbcDriver())) {
|
||||
return DRIVER_ENTRY_MAP.get(driver.getJdbcDriver());
|
||||
}
|
||||
ClassLoader cl = getClassLoader(driver);
|
||||
Driver d = (Driver)cl.loadClass(driver.getJdbcDriverClass()).newInstance();
|
||||
Driver d = (Driver) cl.loadClass(driver.getJdbcDriverClass()).newInstance();
|
||||
DriverEntry driverEntry = DriverEntry.builder().driverConfig(driver).driver(d).build();
|
||||
DRIVER_ENTRY_MAP.put(driver.getJdbcDriver(), driverEntry);
|
||||
return driverEntry;
|
||||
|
@ -0,0 +1,21 @@
|
||||
package ai.chat2db.spi.util;
|
||||
|
||||
import ai.chat2db.spi.config.DBConfig;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
public static <T> T readJsonValue(Class<?> loaderClass, String path, Class<T> clazz) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
T value = null;
|
||||
try {
|
||||
value = mapper.readValue(loaderClass.getResourceAsStream(path), clazz);
|
||||
// 使用obj中的数据
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
@ -1,11 +1,10 @@
|
||||
package ai.chat2db.spi.util;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.Map;
|
||||
import java.sql.*;
|
||||
import java.text.Collator;
|
||||
import java.util.*;
|
||||
|
||||
import ai.chat2db.spi.model.KeyValue;
|
||||
import com.alibaba.druid.DbType;
|
||||
|
||||
import ai.chat2db.spi.config.DriverConfig;
|
||||
@ -14,8 +13,11 @@ import ai.chat2db.spi.model.DataSourceConnect;
|
||||
import ai.chat2db.spi.model.SSHInfo;
|
||||
import ai.chat2db.spi.sql.IDriverManager;
|
||||
import ai.chat2db.spi.ssh.SSHManager;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.jcraft.jsch.Session;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
@ -138,11 +140,11 @@ public class JdbcUtils {
|
||||
* @return
|
||||
*/
|
||||
public static DataSourceConnect testConnect(String url, String host, String port,
|
||||
String userName, String password, String dbType,
|
||||
DriverConfig driverConfig, SSHInfo ssh, Map<String, Object> properties) {
|
||||
String userName, String password, String dbType,
|
||||
DriverConfig driverConfig, SSHInfo ssh, Map<String, Object> properties) {
|
||||
DataSourceConnect dataSourceConnect = DataSourceConnect.builder()
|
||||
.success(Boolean.TRUE)
|
||||
.build();
|
||||
.success(Boolean.TRUE)
|
||||
.build();
|
||||
Session session = null;
|
||||
Connection connection = null;
|
||||
// 加载驱动
|
||||
@ -155,7 +157,7 @@ public class JdbcUtils {
|
||||
}
|
||||
// 创建连接
|
||||
connection = IDriverManager.getConnection(url, userName, password,
|
||||
driverConfig, properties);
|
||||
driverConfig, properties);
|
||||
} catch (Exception e) {
|
||||
log.error("connection fail:", e);
|
||||
dataSourceConnect.setSuccess(Boolean.FALSE);
|
||||
@ -203,4 +205,96 @@ public class JdbcUtils {
|
||||
|
||||
}
|
||||
|
||||
public static void setDriverDefaultProperty(DriverConfig driverConfig) {
|
||||
if(driverConfig == null){
|
||||
return;
|
||||
}
|
||||
List<KeyValue> defaultKeyValues = driverConfig.getExtendInfo();
|
||||
Map<String, KeyValue> valueMap = Maps.newHashMap();
|
||||
if (!CollectionUtils.isEmpty(defaultKeyValues)) {
|
||||
for (KeyValue keyValue : defaultKeyValues) {
|
||||
if (keyValue == null || StringUtils.isBlank(keyValue.getKey())) {
|
||||
continue;
|
||||
}
|
||||
valueMap.put(keyValue.getKey(), keyValue);
|
||||
}
|
||||
}
|
||||
try {
|
||||
DriverPropertyInfo[] propertyInfos = IDriverManager.getProperty(driverConfig);
|
||||
if (propertyInfos == null) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < propertyInfos.length; i++) {
|
||||
DriverPropertyInfo propertyInfo = propertyInfos[i];
|
||||
if (propertyInfo == null) {
|
||||
continue;
|
||||
}
|
||||
KeyValue keyValue = valueMap.get(propertyInfo.name);
|
||||
if (keyValue != null) {
|
||||
String[] choices = propertyInfo.choices;
|
||||
if (CollectionUtils.isEmpty(keyValue.getChoices()) && choices != null && choices.length > 0) {
|
||||
keyValue.setChoices(Lists.newArrayList(choices));
|
||||
}
|
||||
} else {
|
||||
keyValue = new KeyValue();
|
||||
keyValue.setKey(propertyInfo.name);
|
||||
keyValue.setValue(propertyInfo.value);
|
||||
keyValue.setRequired(propertyInfo.required);
|
||||
String[] choices = propertyInfo.choices;
|
||||
if (choices != null && choices.length > 0) {
|
||||
keyValue.setChoices(Lists.newArrayList(choices));
|
||||
}
|
||||
valueMap.put(keyValue.getKey(), keyValue);
|
||||
}
|
||||
}
|
||||
if (!valueMap.isEmpty()) {
|
||||
Comparator comparator = Collator.getInstance(Locale.ENGLISH);
|
||||
List<KeyValue> result = new ArrayList<>(valueMap.values());
|
||||
Collections.sort(result, (o1, o2) -> comparator.compare(o1.getKey(), o2.getKey()));
|
||||
driverConfig.setExtendInfo(result);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
log.error("get property error:", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void removePropertySameAsDefault(DriverConfig driverConfig) {
|
||||
if(driverConfig == null){
|
||||
return;
|
||||
}
|
||||
List<KeyValue> customValue = driverConfig.getExtendInfo();
|
||||
if (CollectionUtils.isEmpty(customValue)) {
|
||||
return ;
|
||||
}
|
||||
Map<String, String> map = Maps.newHashMap();
|
||||
List<KeyValue> result = new ArrayList<>();
|
||||
try {
|
||||
DriverPropertyInfo[] propertyInfos = IDriverManager.getProperty(driverConfig);
|
||||
if (propertyInfos == null) {
|
||||
return ;
|
||||
}
|
||||
for (int i = 0; i < propertyInfos.length; i++) {
|
||||
DriverPropertyInfo propertyInfo = propertyInfos[i];
|
||||
if (propertyInfo == null) {
|
||||
continue;
|
||||
}
|
||||
map.put(propertyInfo.name, propertyInfo.value);
|
||||
}
|
||||
for (KeyValue keyValue : customValue) {
|
||||
if (keyValue == null || StringUtils.isBlank(keyValue.getKey())) {
|
||||
continue;
|
||||
}
|
||||
String value = map.get(keyValue.getKey());
|
||||
if (!StringUtils.equals(value, keyValue.getValue())) {
|
||||
result.add(keyValue);
|
||||
}
|
||||
}
|
||||
Comparator comparator = Collator.getInstance(Locale.ENGLISH);
|
||||
Collections.sort(result, (o1, o2) -> comparator.compare(o1.getKey(), o2.getKey()));
|
||||
driverConfig.setExtendInfo(result);
|
||||
} catch (SQLException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user