diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/ConverterController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/ConverterController.java
index ad9fdfe56..b9d6905ac 100644
--- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/ConverterController.java
+++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/ConverterController.java
@@ -4,6 +4,7 @@
import ai.chat2db.server.tools.common.util.ConfigUtils;
import ai.chat2db.server.web.api.controller.ncx.service.ConverterService;
import ai.chat2db.server.web.api.controller.ncx.vo.UploadVO;
+import ai.chat2db.server.web.api.util.FileUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -12,7 +13,6 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
-import ai.chat2db.server.web.api.util.FileUtils;
import java.io.File;
import java.util.Objects;
@@ -33,10 +33,10 @@ public class ConverterController {
/**
* 导出教程
- * @see
*
* @param file file
* @return DataResult
+ * @see
**/
@SneakyThrows
@PostMapping("/ncx/upload")
@@ -67,7 +67,7 @@ public DataResult dbpUploadFile(@RequestParam("file") MultipartFile fi
/**
* 导入datagrip的连接信息,通过 ctrl/cmd + c(shift多选)复制连接,再导入进来
- * 目前复制的连接信息里面是没有密码的
+ * 目前复制的连接信息里面是没有密码的、ssh连接信息也没有
*
* @param text text
* @return DataResult
diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/service/impl/ConverterServiceImpl.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/service/impl/ConverterServiceImpl.java
index 2fab62892..4546a79a9 100644
--- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/service/impl/ConverterServiceImpl.java
+++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ncx/service/impl/ConverterServiceImpl.java
@@ -16,25 +16,21 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
-import org.w3c.dom.Document;
-import org.w3c.dom.NamedNodeMap;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
+import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.SAXParser;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.Objects;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
* ConverterServiceImpl
@@ -58,13 +54,17 @@ public class ConverterServiceImpl implements ConverterService {
* xml连接信息开始标志位
**/
private static final String BEGIN = "#BEGIN#";
- /**
- * xml连接信息结束标志位
- **/
- private static final String END = "#END#";
@Autowired
private DataSourceMapper dataSourceMapper;
+ /**
+ * jdbc通用匹配ip和端口
+ */
+ public static final Pattern IP_PORT = Pattern.compile("jdbc:(?[a-z]+)://(?[a-zA-Z0-9-//.]+):(?[0-9]+)");
+ /**
+ * oracle匹配ip和端口
+ */
+ public static final Pattern ORACLE_IP_PORT = Pattern.compile("jdbc:(?[a-z]+):(?[a-z]+):@(?[a-zA-Z0-9-//.]+):(?[0-9]+)");
@Override
public UploadVO uploadFile(File file) {
@@ -137,7 +137,7 @@ public UploadVO datagripUploadFile(String text) {
List configs = new ArrayList<>();
for (int i = 0; i < items.length; i++) {
if (items[i].equals(BEGIN)) {
- configs.add(items[i + 1]);
+ configs.add(XML_HEADER + items[i + 1]);
}
}
for (String config : configs) {
@@ -149,19 +149,48 @@ public UploadVO datagripUploadFile(String text) {
//3、通过DocumentBuilder对象的parser方法加载xml文件到当前项目下
try (InputStream inputStream = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8))) {
Document document = db.parse(inputStream);
- //获取Connection节点的集合
- NodeList connectList = document.getElementsByTagName("data-source");
- //选中第一个节点
- NamedNodeMap itemMap = connectList.item(0).getAttributes();
+ // 获取根元素
+ Element rootElement = document.getDocumentElement();
//创建datasource
DataSourceDO dataSourceDO = new DataSourceDO();
- dataSourceDO.setAlias(itemMap.getNamedItem("name").getNodeValue());
- for (int i = 0; i < connectList.getLength(); i++) {
- //通过 item(i)方法 获取一个Connection节点,nodeList的索引值从0开始
- Node connect = connectList.item(i);
- //获取Connection节点的所有属性集合
- NamedNodeMap attrs = connect.getAttributes();
+ LocalDateTime dateTime = LocalDateTime.now();
+ dataSourceDO.setGmtCreate(dateTime);
+ dataSourceDO.setGmtModified(dateTime);
+ dataSourceDO.setAlias(rootElement.getAttribute("name"));
+ // 获取子元素 database-info
+ Element databaseInfoElement = (Element) rootElement.getElementsByTagName("database-info").item(0);
+
+ // 获取连接相关信息
+ String type = databaseInfoElement.getAttribute("dbms");
+ String jdbcUrl = rootElement.getElementsByTagName("jdbc-url").item(0).getTextContent();
+ String username = rootElement.getElementsByTagName("user-name").item(0).getTextContent();
+ String driverName = rootElement.getElementsByTagName("jdbc-driver").item(0).getTextContent();
+ String host = "";
+ String port = "";
+ if (type.equals(DataBaseType.ORACLE.name())) {
+ // 创建 Matcher 对象
+ Matcher matcher = ORACLE_IP_PORT.matcher(jdbcUrl);
+ // 查找匹配的 IP 地址和端口号
+ if (matcher.find()) {
+ host = matcher.group("host");
+ port = matcher.group("port");
+ }
+ } else {
+ // 创建 Matcher 对象
+ Matcher matcher = IP_PORT.matcher(jdbcUrl);
+ // 查找匹配的 IP 地址和端口号
+ if (matcher.find()) {
+ host = matcher.group("host");
+ port = matcher.group("port");
+
+ }
}
+ dataSourceDO.setHost(host);
+ dataSourceDO.setPort(port);
+ dataSourceDO.setUrl(jdbcUrl);
+ dataSourceDO.setUserName(username);
+ dataSourceDO.setDriver(driverName);
+ dataSourceDO.setType(type);
dataSourceMapper.insert(dataSourceDO);
}
}