Skip to content

Commit

Permalink
datagrip导入实现
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhuoyuan1 committed Sep 24, 2023
1 parent 3fdc512 commit 739a2d5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -33,10 +33,10 @@ public class ConverterController {

/**
* 导出教程
* @see <a href="https://blog.csdn.net/kkk123445/article/details/122514124?spm=1001.2014.3001.5502" />
*
* @param file file
* @return DataResult<UploadVO>
* @see <a href="https://blog.csdn.net/kkk123445/article/details/122514124?spm=1001.2014.3001.5502" />
**/
@SneakyThrows
@PostMapping("/ncx/upload")
Expand Down Expand Up @@ -67,7 +67,7 @@ public DataResult<UploadVO> dbpUploadFile(@RequestParam("file") MultipartFile fi

/**
* 导入datagrip的连接信息,通过 ctrl/cmd + c(shift多选)复制连接,再导入进来
* 目前复制的连接信息里面是没有密码的
* 目前复制的连接信息里面是没有密码的、ssh连接信息也没有
*
* @param text text
* @return DataResult<UploadVO>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:(?<type>[a-z]+)://(?<host>[a-zA-Z0-9-//.]+):(?<port>[0-9]+)");
/**
* oracle匹配ip和端口
*/
public static final Pattern ORACLE_IP_PORT = Pattern.compile("jdbc:(?<type>[a-z]+):(?<child>[a-z]+):@(?<host>[a-zA-Z0-9-//.]+):(?<port>[0-9]+)");

@Override
public UploadVO uploadFile(File file) {
Expand Down Expand Up @@ -137,7 +137,7 @@ public UploadVO datagripUploadFile(String text) {
List<String> 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) {
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 739a2d5

Please sign in to comment.