Commit f659fed2 authored by alex yao's avatar alex yao

feat: database数据表查询

parent ce52568b
......@@ -19,6 +19,8 @@ public class DatabaseUtil {
private static Logger logger = LoggerFactory.getLogger(DatabaseUtil.class);
private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
private static final int CONNECT_TIMEOUT = 5000; // 连接超时时间(毫秒)
private static final int SOCKET_TIMEOUT = 30000; // socket超时时间(毫秒)
......@@ -33,7 +35,7 @@ public class DatabaseUtil {
" AND TABLE_NAME = t.TABLE_NAME) AS TABLE_COLUMN " +
"FROM INFORMATION_SCHEMA.TABLES t " +
"WHERE TABLE_SCHEMA = ?";
;
/**
* 测试数据库连接
......@@ -49,7 +51,10 @@ public class DatabaseUtil {
String url = "jdbc:mysql://" + host + ":" + port + "/" + database
+ "?connectTimeout=" + CONNECT_TIMEOUT // 连接超时(毫秒)
+ "&socketTimeout=" + SOCKET_TIMEOUT; // socket超时(毫秒)
try (Connection connection = DriverManager.getConnection(url, username, password)) {
try {
Class.forName(DRIVER_NAME);
Connection connection = DriverManager.getConnection(url, username, password);
connection.close();
return true;
} catch (Exception e) {
return false;
......@@ -63,7 +68,9 @@ public class DatabaseUtil {
String url = "jdbc:mysql://" + host + ":" + port + "/" + database
+ "?connectTimeout=" + CONNECT_TIMEOUT // 连接超时(毫秒)
+ "&socketTimeout=" + SOCKET_TIMEOUT; // socket超时(毫秒)
try (Connection connection = DriverManager.getConnection(url, username, password)) {
try {
Class.forName(DRIVER_NAME);
Connection connection = DriverManager.getConnection(url, username, password);
PreparedStatement preparedStatement = connection.prepareStatement(SQL_TABLE_INFO);
preparedStatement.setString(1, database);
ResultSet resultSet = preparedStatement.executeQuery();
......@@ -76,6 +83,7 @@ public class DatabaseUtil {
tableInfo.setTABLE_COLUMN(resultSet.getInt("TABLE_COLUMN"));
tableInfoList.add(tableInfo);
}
connection.close();
return tableInfoList;
} catch (Exception e) {
logger.error("连接数据库失败", e);
......
......@@ -142,7 +142,28 @@ public class DatabaseRestImpl implements DatabaseRest {
List<KnowledgeDatabaseQueryInfoQueryItem> items = bizKnowledgeDatabaseService.queryKnowledgeDatabaseQueryInfo(condition, pagingInfo);
List<BizKnowledgeDatabaseDto> result = new ArrayList<>();
if (CollectionUtils.isNotEmpty(items)) {
result = items.stream().map(BizKnowledgeDatabaseConvert::itemToDto).collect(Collectors.toList());
result = items.stream().map(
item -> {
BizKnowledgeDatabaseDto bizKnowledgeDatabaseDto =
BizKnowledgeDatabaseConvert.itemToDto(item);
List<DatabaseUtil.TableInfo> tableInfos = DatabaseTableInfoCache.getCache(item.getId().intValue());
if (tableInfos == null) {
tableInfos = DatabaseUtil.getTableInfo(item.getDbHost(),
item.getDbPort(),
item.getDbUsername(),
item.getDbPassword(),
item.getDbName());
if (tableInfos == null) {
return null;
}
bizKnowledgeDatabaseDto.setTableInfos(tableInfos);
DatabaseTableInfoCache.updateOrSaveCache(item.getId().intValue(), tableInfos);
}
return bizKnowledgeDatabaseDto;
}
).collect(Collectors.toList());
}
return result;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment