feat():新增pgsql数据源
parent
b7f3be1963
commit
36cb2598f3
|
@ -1,31 +0,0 @@
|
||||||
package com.muyu.data.source.domain.req;
|
|
||||||
|
|
||||||
import com.muyu.data.source.domain.AssetDataSource;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import lombok.experimental.SuperBuilder;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询表结构
|
|
||||||
*
|
|
||||||
* @author HuFangMing
|
|
||||||
* @ClassName: ShowTableReq
|
|
||||||
* @createTime: 2024/4/23 20:45
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@SuperBuilder
|
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class ShowTableReq {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 资产数据源
|
|
||||||
*/
|
|
||||||
private AssetDataSource assetStructure;
|
|
||||||
/**
|
|
||||||
* 表名
|
|
||||||
*/
|
|
||||||
private String tableName;
|
|
||||||
}
|
|
|
@ -28,6 +28,10 @@
|
||||||
<groupId>com.alibaba.cloud</groupId>
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.postgresql</groupId>
|
||||||
|
<artifactId>postgresql</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- SpringCloud Alibaba Nacos Config -->
|
<!-- SpringCloud Alibaba Nacos Config -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -2,9 +2,7 @@ package com.muyu.data.source.controller;
|
||||||
|
|
||||||
import com.muyu.data.source.domain.AssetDataSource;
|
import com.muyu.data.source.domain.AssetDataSource;
|
||||||
import com.muyu.data.source.domain.Children;
|
import com.muyu.data.source.domain.Children;
|
||||||
import com.muyu.data.source.domain.DatabaseType;
|
|
||||||
import com.muyu.data.source.domain.TableData;
|
import com.muyu.data.source.domain.TableData;
|
||||||
import com.muyu.data.source.domain.req.ShowTableReq;
|
|
||||||
import com.muyu.data.source.domain.resp.CountResp;
|
import com.muyu.data.source.domain.resp.CountResp;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
@ -156,22 +154,22 @@ public class DataSourceController extends BaseController {
|
||||||
* 获取数据库下表描述
|
* 获取数据库下表描述
|
||||||
*/
|
*/
|
||||||
@RequiresPermissions("data:source:childrenlist")
|
@RequiresPermissions("data:source:childrenlist")
|
||||||
@PostMapping("/getChildrenList")
|
@GetMapping("/getChildrenList/{id}")
|
||||||
public Result<List<Children>> getChildrenList(@RequestBody AssetDataSource assetDataSource){
|
public Result getChildrenList(@PathVariable("id") Integer id){
|
||||||
|
List<Children> list = dataSourceService.getChildrenList(id);
|
||||||
return Result.success( dataSourceService.getChildrenList(assetDataSource));
|
return Result.success(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取表下的字段描述
|
* 获取表下的字段描述
|
||||||
*/
|
*/
|
||||||
@RequiresPermissions("data:source:addtbledate")
|
// @RequiresPermissions("data:source:addtbledate")
|
||||||
@PostMapping("/addTableData")
|
// @PostMapping("/addTableData")
|
||||||
public Result addTbleDate(@RequestBody ShowTableReq showTableReq){
|
// public Result addTbleDate(@RequestBody ShowTableReq showTableReq){
|
||||||
|
//
|
||||||
dataSourceService.addTbleDate(showTableReq);
|
// dataSourceService.addTbleDate(showTableReq);
|
||||||
return Result.success();
|
// return Result.success();
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ import com.muyu.common.core.domain.Result;
|
||||||
import com.muyu.data.source.domain.AssetDataSource;
|
import com.muyu.data.source.domain.AssetDataSource;
|
||||||
import com.muyu.data.source.domain.Children;
|
import com.muyu.data.source.domain.Children;
|
||||||
import com.muyu.data.source.domain.TableData;
|
import com.muyu.data.source.domain.TableData;
|
||||||
import com.muyu.data.source.domain.req.ShowTableReq;
|
|
||||||
import com.muyu.data.source.domain.resp.CountResp;
|
import com.muyu.data.source.domain.resp.CountResp;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import com.muyu.data.source.domain.DataSource;
|
import com.muyu.data.source.domain.DataSource;
|
||||||
|
@ -31,9 +30,9 @@ public interface DataSourceService extends IService<DataSource> {
|
||||||
|
|
||||||
List<AssetDataSource> getAssetList();
|
List<AssetDataSource> getAssetList();
|
||||||
|
|
||||||
List<Children> getChildrenList(AssetDataSource assetDataSource );
|
List<Children> getChildrenList(Integer id);
|
||||||
|
|
||||||
|
|
||||||
void addTbleDate(ShowTableReq showTableReq);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,12 +5,12 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.muyu.common.core.domain.Result;
|
import com.muyu.common.core.domain.Result;
|
||||||
import com.muyu.common.core.utils.ObjUtils;
|
import com.muyu.common.core.utils.ObjUtils;
|
||||||
|
import com.muyu.common.core.utils.StringUtils;
|
||||||
import com.muyu.data.source.domain.AssetDataSource;
|
import com.muyu.data.source.domain.AssetDataSource;
|
||||||
import com.muyu.data.source.domain.Children;
|
import com.muyu.data.source.domain.Children;
|
||||||
import com.muyu.data.source.domain.DataSource;
|
import com.muyu.data.source.domain.DataSource;
|
||||||
import com.muyu.data.source.domain.DatabaseType;
|
import com.muyu.data.source.domain.DatabaseType;
|
||||||
import com.muyu.data.source.domain.TableData;
|
import com.muyu.data.source.domain.TableData;
|
||||||
import com.muyu.data.source.domain.req.ShowTableReq;
|
|
||||||
import com.muyu.data.source.domain.resp.CountResp;
|
import com.muyu.data.source.domain.resp.CountResp;
|
||||||
import com.muyu.data.source.mapper.DataSourceMapper;
|
import com.muyu.data.source.mapper.DataSourceMapper;
|
||||||
import com.muyu.data.source.service.AssetDataSourceService;
|
import com.muyu.data.source.service.AssetDataSourceService;
|
||||||
|
@ -108,6 +108,10 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
||||||
jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/"
|
jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/"
|
||||||
+ dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
|
+ dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
|
||||||
}
|
}
|
||||||
|
if ("PostgreSql".equals(dataType.getDatabaseName())) {
|
||||||
|
driveClass = dataType.getDriverManager();
|
||||||
|
jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/" + dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
|
||||||
|
}
|
||||||
if ("oracle".equals(dataType.getDatabaseName())) {
|
if ("oracle".equals(dataType.getDatabaseName())) {
|
||||||
driveClass = dataType.getDriverManager();
|
driveClass = dataType.getDriverManager();
|
||||||
jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + ":"
|
jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + ":"
|
||||||
|
@ -151,22 +155,43 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Result syncConnection(DataSource dataSource) {
|
public Result syncConnection(DataSource dataSource) {
|
||||||
AssetDataSource assetDataSourceOne=assetDataSourceService.getOne(new LambdaQueryWrapper<AssetDataSource>(){{
|
AssetDataSource assetDataSourceOne=assetDataSourceService.getOne(new LambdaQueryWrapper<>() {{
|
||||||
eq(AssetDataSource::getId, dataSource.getId());
|
eq(AssetDataSource::getId, dataSource.getId());
|
||||||
}});
|
}});
|
||||||
if (assetDataSourceOne==null){
|
// 如果存在 删除
|
||||||
AssetDataSource assetDataSource=AssetDataSource.builder()
|
if (StringUtils.isNotNull(assetDataSourceOne)) {
|
||||||
.id(Long.valueOf(Math.toIntExact(dataSource.getId())))
|
List<Children> childrenList = childrenService.list(new LambdaQueryWrapper<>() {{
|
||||||
.name(dataSource.getName())
|
eq(Children::getAssetId, assetDataSourceOne.getId());
|
||||||
.systemName(dataSource.getSystemName())
|
}});
|
||||||
.databaseName(dataSource.getDatabaseName())
|
// 同步表结构
|
||||||
.type("dataSoure")
|
childrenList.forEach(children -> {
|
||||||
.build();
|
tableDataService.remove(new LambdaQueryWrapper<>() {{
|
||||||
return Result.success(assetDataSourceService.save(assetDataSource));
|
eq(TableData::getChildrenId, children.getId());
|
||||||
|
}});
|
||||||
|
});
|
||||||
|
childrenService.remove(new LambdaQueryWrapper<>() {{
|
||||||
|
eq(Children::getAssetId, assetDataSourceOne.getId());
|
||||||
|
}});
|
||||||
|
assetDataSourceService.removeById(assetDataSourceOne.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return Result.error("数据源已存在");
|
AssetDataSource assetDataSource = AssetDataSource.builder()
|
||||||
|
.id((long) Math.toIntExact(dataSource.getId()))
|
||||||
|
.name(dataSource.getName())
|
||||||
|
.systemName(dataSource.getSystemName())
|
||||||
|
.databaseName(dataSource.getDatabaseName())
|
||||||
|
.type("dataSource")
|
||||||
|
.build();
|
||||||
|
assetDataSourceService.save(assetDataSource);
|
||||||
|
// 同步数据库结构
|
||||||
|
getChildrenList(assetDataSource);
|
||||||
|
List<Children> childrenList = childrenService.list();
|
||||||
|
// 同步表结构
|
||||||
|
childrenList.forEach(children -> {
|
||||||
|
addTableData(assetDataSource, children.getName());
|
||||||
|
});
|
||||||
|
return Result.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -176,7 +201,14 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Children> getChildrenList(AssetDataSource assetDataSource) {
|
public List<Children> getChildrenList(Integer id) {
|
||||||
|
List<Children> childrenList = childrenService.list(new LambdaQueryWrapper<Children>() {{
|
||||||
|
eq(Children::getAssetId, id);
|
||||||
|
}});
|
||||||
|
return childrenList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getChildrenList(AssetDataSource assetDataSource) {
|
||||||
//查询数据源对象
|
//查询数据源对象
|
||||||
DataSource dataSource = this.getOne(new LambdaQueryWrapper<DataSource>() {{
|
DataSource dataSource = this.getOne(new LambdaQueryWrapper<DataSource>() {{
|
||||||
eq(DataSource::getName, assetDataSource.getName());
|
eq(DataSource::getName, assetDataSource.getName());
|
||||||
|
@ -185,93 +217,91 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
||||||
DatabaseType databaseType = databaseTypeService.getOne(new LambdaQueryWrapper<DatabaseType>() {{
|
DatabaseType databaseType = databaseTypeService.getOne(new LambdaQueryWrapper<DatabaseType>() {{
|
||||||
eq(DatabaseType::getDatabaseName, dataSource.getDataType());
|
eq(DatabaseType::getDatabaseName, dataSource.getDataType());
|
||||||
}});
|
}});
|
||||||
|
|
||||||
|
String jdbcUrl = "";
|
||||||
|
String sql = "";
|
||||||
|
|
||||||
|
jdbcUrl = databaseType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/" +
|
||||||
|
dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
|
||||||
//数据库类型为mysql
|
//数据库类型为mysql
|
||||||
if ("mysql".equals(dataSource.getDataType())) {
|
if ("mysql".equals(dataSource.getDataType())) {
|
||||||
// 根据id查询表描述
|
// 根据id查询表描述
|
||||||
List<Children> childrenList = childrenService.list(new LambdaQueryWrapper<Children>() {{
|
List<Children> childrenList = childrenService.list(new LambdaQueryWrapper<Children>() {{
|
||||||
eq(Children::getAssetId, assetDataSource.getId());
|
eq(Children::getAssetId, assetDataSource.getId());
|
||||||
}});
|
}});
|
||||||
if (childrenList == null || childrenList.size() == 0) {
|
sql = "select TABLE_NAME,TABLE_COMMENT from INFORMATION_SCHEMA.Tables where table_schema = " + "'" + dataSource.getDatabaseName() + "'";
|
||||||
String jdbcUrl = databaseType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/"
|
} else if ("PostgreSql".equals(dataSource.getDataType())) {
|
||||||
+ dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
|
sql = "select tb.table_name AS TABLE_NAME, d.description AS TABLE_COMMENT\n" +
|
||||||
try {
|
"from information_schema.tables tb\n" +
|
||||||
Class.forName(databaseType.getDriverManager());
|
"join pg_class c on c.relname = tb.table_name\n" +
|
||||||
|
"left join pg_description d on d.objoid = c.oid and d.objsubid = '0'\n" +
|
||||||
Connection conn = DriverManager.getConnection(jdbcUrl, dataSource.getUser(),
|
"where tb.table_schema = '" + dataSource.getDatabaseName() + "'";
|
||||||
dataSource.getPassword());
|
}
|
||||||
DatabaseMetaData metaData = conn.getMetaData();
|
try {
|
||||||
Statement statement = conn.createStatement();
|
Class.forName(databaseType.getDriverManager());
|
||||||
ResultSet rs = statement.executeQuery(
|
Connection connection = DriverManager.getConnection(jdbcUrl, dataSource.getUser(), dataSource.getPassword());
|
||||||
"select * from information_schema.tables where TABLE_SCHEMA = " +
|
PreparedStatement ps = connection.prepareStatement(sql);
|
||||||
"'" + dataSource.getDatabaseName() + "'");
|
ResultSet rs = ps.executeQuery();
|
||||||
ResultSet tableRet = metaData.getTables(conn.getCatalog(), "%", "%", new String[]{"TABLE"});
|
while (rs.next()) {
|
||||||
while (rs.next()) {
|
// 获取表名
|
||||||
String tableName = rs.getString("TABLE_NAME");
|
String tableName = rs.getString("TABLE_NAME");
|
||||||
String tableComment = rs.getString("TABLE_COMMENT");
|
// 表注释
|
||||||
|
String tableComment = rs.getString("TABLE_COMMENT");
|
||||||
|
Children children = Children.builder()
|
||||||
Children children = Children.builder()
|
.name(tableName)
|
||||||
.name(tableName)
|
.annotation(tableComment)
|
||||||
.annotation(tableComment)
|
.type("dataTable")
|
||||||
.type("dataTable")
|
.assetId(assetDataSource.getId())
|
||||||
.build();
|
.build();
|
||||||
children.setAssetId(assetDataSource.getId());
|
childrenService.save(children);
|
||||||
childrenService.save(children);
|
|
||||||
}
|
|
||||||
conn.close();
|
|
||||||
rs.close();
|
|
||||||
tableRet.close();
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
childrenService.list(new LambdaQueryWrapper<Children>(){{
|
|
||||||
eq(Children::getAssetId, assetDataSource.getId());
|
|
||||||
}});
|
|
||||||
try {
|
|
||||||
Class.forName(databaseType.getDriverManager());
|
|
||||||
Connection connection = DriverManager.getConnection(jdbcUrl, dataSource.getUser(), dataSource.getPassword());
|
|
||||||
Statement statement = connection.createStatement();
|
|
||||||
childrenList.forEach(children -> {
|
|
||||||
String sql="select count(*) as tableNum from `" + dataSource.getDatabaseName() + "`." + children.getName();
|
|
||||||
try {
|
|
||||||
ResultSet rs = statement.executeQuery(sql);
|
|
||||||
while (rs.next()){
|
|
||||||
Long tableNum = rs.getLong("tableNum");
|
|
||||||
children.setDataTotal(tableNum);
|
|
||||||
childrenService.updateById(children);
|
|
||||||
}
|
|
||||||
rs.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
//返回表描述
|
connection.close();
|
||||||
return childrenList;
|
rs.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
List<Children> childrenList = childrenService.list(new LambdaQueryWrapper<>() {{
|
||||||
|
eq(Children::getAssetId, assetDataSource.getId());
|
||||||
|
}});
|
||||||
|
try {
|
||||||
|
Class.forName(databaseType.getDriverManager());
|
||||||
|
Connection connection = DriverManager.getConnection(jdbcUrl, dataSource.getUser(), dataSource.getPassword());
|
||||||
|
Statement statement = connection.createStatement();
|
||||||
|
childrenList.forEach(children -> {
|
||||||
|
String sql1 = "select count(*) as tableNum from " + dataSource.getDatabaseName() + "." + children.getName();
|
||||||
|
try {
|
||||||
|
ResultSet rs = statement.executeQuery(sql1);
|
||||||
|
while (rs.next()) {
|
||||||
|
int tableNum = rs.getInt("tableNum");
|
||||||
|
children.setDataTotal(Long.valueOf(tableNum));
|
||||||
|
childrenService.updateById(children);;
|
||||||
|
}
|
||||||
|
|
||||||
|
rs.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addTbleDate(ShowTableReq showTableReq) {
|
public void addTableData(AssetDataSource assetDataSource, String tableName) {
|
||||||
// if (version == 1){
|
// if (version == 1){
|
||||||
DataSource dataSource = this.getOne(new LambdaQueryWrapper<>() {{
|
DataSource dataSource = this.getOne(new LambdaQueryWrapper<>() {{
|
||||||
eq(DataSource::getName, showTableReq.getAssetStructure().getName());
|
eq(DataSource::getName, assetDataSource.getName());
|
||||||
eq(DataSource::getDatabaseName, showTableReq.getAssetStructure().getDatabaseName());
|
eq(DataSource::getDatabaseName, assetDataSource.getDatabaseName());
|
||||||
}});
|
}});
|
||||||
Children children = childrenService.getOne(new LambdaQueryWrapper<>() {{
|
Children children = childrenService.getOne(new LambdaQueryWrapper<>() {{
|
||||||
eq(Children::getAssetId, showTableReq.getAssetStructure().getId());
|
eq(Children::getAssetId, assetDataSource.getId());
|
||||||
eq(Children::getName, showTableReq.getTableName());
|
eq(Children::getName, tableName);
|
||||||
}});
|
}});
|
||||||
// 获取数据类型对象
|
// 获取数据类型对象
|
||||||
DatabaseType dataType = databaseTypeService.getOne(new LambdaQueryWrapper<>() {
|
DatabaseType dataType = databaseTypeService.getOne(new LambdaQueryWrapper<>() {
|
||||||
|
@ -279,60 +309,56 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
||||||
eq(DatabaseType::getDatabaseName, dataSource.getDataType());
|
eq(DatabaseType::getDatabaseName, dataSource.getDataType());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
String jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/"
|
|
||||||
+ dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
|
|
||||||
|
|
||||||
List<TableData> tableDataList = tableDataService.list(new LambdaQueryWrapper<>() {{
|
List<TableData> tableDataList = tableDataService.list(new LambdaQueryWrapper<>() {{
|
||||||
eq(TableData::getChildrenId, children.getId());
|
eq(TableData::getChildrenId, children.getId());
|
||||||
}});
|
}});
|
||||||
|
|
||||||
if (tableDataList == null || tableDataList.size() == 0) {
|
String jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/" + dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
|
||||||
try {
|
Connection connection = null;
|
||||||
Class.forName(dataType.getDriverManager());
|
try {
|
||||||
Connection connection = DriverManager.getConnection(jdbcUrl, dataSource.getUser(), dataSource.getPassword());
|
Class.forName(dataType.getDriverManager());
|
||||||
Statement statement = connection.createStatement();
|
connection = DriverManager.getConnection(jdbcUrl, dataSource.getUser(), dataSource.getPassword());
|
||||||
String sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS where table_schema ='" + dataSource.getDatabaseName() + "' AND table_name = '" + showTableReq.getTableName() + "'";
|
} catch (Exception e) {
|
||||||
ResultSet rs = statement.executeQuery(sql);
|
throw new RuntimeException(e);
|
||||||
while (rs.next()) {
|
}
|
||||||
String columnName = rs.getString("COLUMN_NAME");
|
try {
|
||||||
String columnComment = rs.getString("COLUMN_COMMENT");
|
DatabaseMetaData metaData = connection.getMetaData();
|
||||||
String columnKey = rs.getString("COLUMN_KEY");
|
ResultSet columnsRs = metaData.getColumns(dataSource.getDatabaseName(), dataSource.getDatabaseName(), tableName, "%");
|
||||||
String columnType = rs.getString("DATA_TYPE");
|
ResultSet primaryKeysRs = metaData.getPrimaryKeys(dataSource.getDatabaseName(), dataSource.getDatabaseName(), tableName);
|
||||||
String javaType = getJavaType(dataType.getDriverManager(), jdbcUrl, dataSource.getUser(), dataSource.getPassword(), showTableReq.getTableName(), columnName);
|
String primaryKeyColumnName = "";
|
||||||
String type = rs.getString("COLUMN_TYPE");
|
while (primaryKeysRs.next()) {
|
||||||
int length = 0;
|
primaryKeyColumnName = primaryKeysRs.getString("COLUMN_NAME");
|
||||||
Pattern pattern = Pattern.compile("\\d+");
|
|
||||||
Matcher matcher = pattern.matcher(type);
|
|
||||||
if (matcher.find()) {
|
|
||||||
String numberString = matcher.group();
|
|
||||||
length = Integer.parseInt(numberString);
|
|
||||||
}
|
|
||||||
int numericScale = rs.getInt("NUMERIC_SCALE");
|
|
||||||
String isNullable = rs.getString("IS_NULLABLE");
|
|
||||||
String columnDefault = rs.getString("COLUMN_DEFAULT");
|
|
||||||
TableData tableData = TableData.builder()
|
|
||||||
.name(columnName)
|
|
||||||
.comment(columnComment)
|
|
||||||
.isPrimaryKey(columnKey.equals("PRI") ? "Y" : "N")
|
|
||||||
.type(columnType)
|
|
||||||
.mappingType(javaType)
|
|
||||||
.length(length)
|
|
||||||
.decimalPlaces(Long.valueOf(numericScale))
|
|
||||||
.isNull(isNullable.equals("YES") ? "Y" : "N")
|
|
||||||
.defaultValue(columnDefault)
|
|
||||||
.isDict("N")
|
|
||||||
.dictKey(null)
|
|
||||||
.childrenId(children.getId())
|
|
||||||
.build();
|
|
||||||
tableDataService.save(tableData);
|
|
||||||
}
|
|
||||||
connection.close();
|
|
||||||
rs.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
// version ++;
|
while (columnsRs.next()) {
|
||||||
// }
|
String columnName = columnsRs.getString("COLUMN_NAME");
|
||||||
|
String columnComment = columnsRs.getString("REMARKS");
|
||||||
|
String columnType = columnsRs.getString("DATA_TYPE");
|
||||||
|
String javaType = getJavaType(dataType.getDriverManager(), jdbcUrl, dataSource, tableName, columnName);
|
||||||
|
int columnSize = columnsRs.getInt("COLUMN_SIZE");
|
||||||
|
int decimalDigits = columnsRs.getInt("DECIMAL_DIGITS");
|
||||||
|
String isNullable = columnsRs.getString("IS_NULLABLE");
|
||||||
|
String columnDefault = columnsRs.getString("COLUMN_DEF");
|
||||||
|
TableData tableData = TableData.builder()
|
||||||
|
.name(columnName)
|
||||||
|
.comment(columnComment)
|
||||||
|
.isPrimaryKey(columnName.equals(primaryKeyColumnName) ? "Y" : "N")
|
||||||
|
.type(columnType)
|
||||||
|
.mappingType(javaType)
|
||||||
|
.length(columnSize)
|
||||||
|
.decimalPlaces((long) decimalDigits)
|
||||||
|
.isNull(isNullable.equals("YES") ? "Y" : "N")
|
||||||
|
.defaultValue(columnDefault)
|
||||||
|
.isDict("N")
|
||||||
|
.dictKey(null)
|
||||||
|
.childrenId(children.getId())
|
||||||
|
.build();
|
||||||
|
tableDataService.save(tableData);
|
||||||
|
}
|
||||||
|
columnsRs.close();
|
||||||
|
primaryKeysRs.close();
|
||||||
|
connection.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -380,13 +406,23 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String getJavaType(String driveClass, String url, String username, String password, String tableName,
|
public static String getJavaType(String driveClass, String jdbcUrl, DataSource dataSource, String tableName, String columnName) {
|
||||||
String columnName) {
|
String sql = "";
|
||||||
Connection connection = buildConnection(driveClass, url, username, password);
|
Connection connection = null;
|
||||||
PreparedStatement pst = null;
|
|
||||||
String javaType = null;
|
String javaType = null;
|
||||||
|
if ("MySql".equals(dataSource.getDataType())) {
|
||||||
|
sql = "select * from " + tableName + " where 2=1";
|
||||||
|
} else if ("PostgreSql".equals(dataSource.getDataType())) {
|
||||||
|
sql = "select * from " + dataSource.getDatabaseName() + "." + tableName + " where 2=1";
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
String sql = "select * from " + tableName;
|
Class.forName(driveClass);
|
||||||
|
connection = DriverManager.getConnection(jdbcUrl, dataSource.getUser(), dataSource.getPassword());
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
PreparedStatement pst = null;
|
||||||
pst = connection.prepareStatement(sql);
|
pst = connection.prepareStatement(sql);
|
||||||
ResultSetMetaData rsd = pst.executeQuery().getMetaData();
|
ResultSetMetaData rsd = pst.executeQuery().getMetaData();
|
||||||
for (int i = 1; i <= rsd.getColumnCount(); i++) {
|
for (int i = 1; i <= rsd.getColumnCount(); i++) {
|
||||||
|
@ -398,14 +434,10 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pst.close();
|
||||||
|
pst = null;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
pst.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return javaType;
|
return javaType;
|
||||||
}
|
}
|
||||||
|
|
8
pom.xml
8
pom.xml
|
@ -35,6 +35,7 @@
|
||||||
<minio.version>8.2.2</minio.version>
|
<minio.version>8.2.2</minio.version>
|
||||||
<poi.version>4.1.2</poi.version>
|
<poi.version>4.1.2</poi.version>
|
||||||
<jedis.version>2.9.0</jedis.version>
|
<jedis.version>2.9.0</jedis.version>
|
||||||
|
<postgresql.version>42.5.0</postgresql.version>
|
||||||
<transmittable-thread-local.version>2.14.3</transmittable-thread-local.version>
|
<transmittable-thread-local.version>2.14.3</transmittable-thread-local.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
@ -207,6 +208,13 @@
|
||||||
<version>${muyu.version}</version>
|
<version>${muyu.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- PostgreSQL JDBC 驱动 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.postgresql</groupId>
|
||||||
|
<artifactId>postgresql</artifactId>
|
||||||
|
<version>${postgresql.version}</version> <!-- 使用当前最新稳定版本 -->
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>redis.clients</groupId>
|
<groupId>redis.clients</groupId>
|
||||||
<artifactId>jedis</artifactId>
|
<artifactId>jedis</artifactId>
|
||||||
|
|
Loading…
Reference in New Issue