feat():新增pgsql数据源

ruoyi_test
sunshine7058 2024-04-28 20:51:14 +08:00
parent b7f3be1963
commit 36cb2598f3
6 changed files with 201 additions and 191 deletions

View File

@ -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;
}

View File

@ -28,6 +28,10 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!-- SpringCloud Alibaba Nacos Config -->
<dependency>

View File

@ -2,9 +2,7 @@ package com.muyu.data.source.controller;
import com.muyu.data.source.domain.AssetDataSource;
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.req.ShowTableReq;
import com.muyu.data.source.domain.resp.CountResp;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
@ -156,22 +154,22 @@ public class DataSourceController extends BaseController {
*
*/
@RequiresPermissions("data:source:childrenlist")
@PostMapping("/getChildrenList")
public Result<List<Children>> getChildrenList(@RequestBody AssetDataSource assetDataSource){
return Result.success( dataSourceService.getChildrenList(assetDataSource));
@GetMapping("/getChildrenList/{id}")
public Result getChildrenList(@PathVariable("id") Integer id){
List<Children> list = dataSourceService.getChildrenList(id);
return Result.success(list);
}
/**
*
*/
@RequiresPermissions("data:source:addtbledate")
@PostMapping("/addTableData")
public Result addTbleDate(@RequestBody ShowTableReq showTableReq){
dataSourceService.addTbleDate(showTableReq);
return Result.success();
}
// @RequiresPermissions("data:source:addtbledate")
// @PostMapping("/addTableData")
// public Result addTbleDate(@RequestBody ShowTableReq showTableReq){
//
// dataSourceService.addTbleDate(showTableReq);
// return Result.success();
// }

View File

@ -4,7 +4,6 @@ import com.muyu.common.core.domain.Result;
import com.muyu.data.source.domain.AssetDataSource;
import com.muyu.data.source.domain.Children;
import com.muyu.data.source.domain.TableData;
import com.muyu.data.source.domain.req.ShowTableReq;
import com.muyu.data.source.domain.resp.CountResp;
import java.util.List;
import com.muyu.data.source.domain.DataSource;
@ -31,9 +30,9 @@ public interface DataSourceService extends IService<DataSource> {
List<AssetDataSource> getAssetList();
List<Children> getChildrenList(AssetDataSource assetDataSource );
List<Children> getChildrenList(Integer id);
void addTbleDate(ShowTableReq showTableReq);

View File

@ -5,12 +5,12 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.domain.Result;
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.Children;
import com.muyu.data.source.domain.DataSource;
import com.muyu.data.source.domain.DatabaseType;
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.mapper.DataSourceMapper;
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() + "/"
+ 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())) {
driveClass = dataType.getDriverManager();
jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + ":"
@ -151,22 +155,43 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
*/
@Override
public Result syncConnection(DataSource dataSource) {
AssetDataSource assetDataSourceOne=assetDataSourceService.getOne(new LambdaQueryWrapper<AssetDataSource>(){{
AssetDataSource assetDataSourceOne=assetDataSourceService.getOne(new LambdaQueryWrapper<>() {{
eq(AssetDataSource::getId, dataSource.getId());
}});
if (assetDataSourceOne==null){
AssetDataSource assetDataSource=AssetDataSource.builder()
.id(Long.valueOf(Math.toIntExact(dataSource.getId())))
.name(dataSource.getName())
.systemName(dataSource.getSystemName())
.databaseName(dataSource.getDatabaseName())
.type("dataSoure")
.build();
return Result.success(assetDataSourceService.save(assetDataSource));
// 如果存在 删除
if (StringUtils.isNotNull(assetDataSourceOne)) {
List<Children> childrenList = childrenService.list(new LambdaQueryWrapper<>() {{
eq(Children::getAssetId, assetDataSourceOne.getId());
}});
// 同步表结构
childrenList.forEach(children -> {
tableDataService.remove(new LambdaQueryWrapper<>() {{
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
@ -176,7 +201,14 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
}
@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>() {{
eq(DataSource::getName, assetDataSource.getName());
@ -185,93 +217,91 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
DatabaseType databaseType = databaseTypeService.getOne(new LambdaQueryWrapper<DatabaseType>() {{
eq(DatabaseType::getDatabaseName, dataSource.getDataType());
}});
String jdbcUrl = "";
String sql = "";
jdbcUrl = databaseType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/" +
dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
//数据库类型为mysql
if ("mysql".equals(dataSource.getDataType())) {
// 根据id查询表描述
List<Children> childrenList = childrenService.list(new LambdaQueryWrapper<Children>() {{
eq(Children::getAssetId, assetDataSource.getId());
}});
if (childrenList == null || childrenList.size() == 0) {
String jdbcUrl = databaseType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/"
+ dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
try {
Class.forName(databaseType.getDriverManager());
Connection conn = DriverManager.getConnection(jdbcUrl, dataSource.getUser(),
dataSource.getPassword());
DatabaseMetaData metaData = conn.getMetaData();
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(
"select * from information_schema.tables where TABLE_SCHEMA = " +
"'" + dataSource.getDatabaseName() + "'");
ResultSet tableRet = metaData.getTables(conn.getCatalog(), "%", "%", new String[]{"TABLE"});
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME");
String tableComment = rs.getString("TABLE_COMMENT");
Children children = Children.builder()
.name(tableName)
.annotation(tableComment)
.type("dataTable")
.build();
children.setAssetId(assetDataSource.getId());
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);
}
sql = "select TABLE_NAME,TABLE_COMMENT from INFORMATION_SCHEMA.Tables where table_schema = " + "'" + dataSource.getDatabaseName() + "'";
} else if ("PostgreSql".equals(dataSource.getDataType())) {
sql = "select tb.table_name AS TABLE_NAME, d.description AS TABLE_COMMENT\n" +
"from information_schema.tables tb\n" +
"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" +
"where tb.table_schema = '" + dataSource.getDatabaseName() + "'";
}
try {
Class.forName(databaseType.getDriverManager());
Connection connection = DriverManager.getConnection(jdbcUrl, dataSource.getUser(), dataSource.getPassword());
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
// 获取表名
String tableName = rs.getString("TABLE_NAME");
// 表注释
String tableComment = rs.getString("TABLE_COMMENT");
Children children = Children.builder()
.name(tableName)
.annotation(tableComment)
.type("dataTable")
.assetId(assetDataSource.getId())
.build();
childrenService.save(children);
}
//返回表描述
return childrenList;
connection.close();
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){
DataSource dataSource = this.getOne(new LambdaQueryWrapper<>() {{
eq(DataSource::getName, showTableReq.getAssetStructure().getName());
eq(DataSource::getDatabaseName, showTableReq.getAssetStructure().getDatabaseName());
eq(DataSource::getName, assetDataSource.getName());
eq(DataSource::getDatabaseName, assetDataSource.getDatabaseName());
}});
Children children = childrenService.getOne(new LambdaQueryWrapper<>() {{
eq(Children::getAssetId, showTableReq.getAssetStructure().getId());
eq(Children::getName, showTableReq.getTableName());
eq(Children::getAssetId, assetDataSource.getId());
eq(Children::getName, tableName);
}});
// 获取数据类型对象
DatabaseType dataType = databaseTypeService.getOne(new LambdaQueryWrapper<>() {
@ -279,60 +309,56 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
eq(DatabaseType::getDatabaseName, dataSource.getDataType());
}
});
String jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/"
+ dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
List<TableData> tableDataList = tableDataService.list(new LambdaQueryWrapper<>() {{
eq(TableData::getChildrenId, children.getId());
}});
if (tableDataList == null || tableDataList.size() == 0) {
try {
Class.forName(dataType.getDriverManager());
Connection connection = DriverManager.getConnection(jdbcUrl, dataSource.getUser(), dataSource.getPassword());
Statement statement = connection.createStatement();
String sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS where table_schema ='" + dataSource.getDatabaseName() + "' AND table_name = '" + showTableReq.getTableName() + "'";
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
String columnName = rs.getString("COLUMN_NAME");
String columnComment = rs.getString("COLUMN_COMMENT");
String columnKey = rs.getString("COLUMN_KEY");
String columnType = rs.getString("DATA_TYPE");
String javaType = getJavaType(dataType.getDriverManager(), jdbcUrl, dataSource.getUser(), dataSource.getPassword(), showTableReq.getTableName(), columnName);
String type = rs.getString("COLUMN_TYPE");
int length = 0;
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);
String jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/" + dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
Connection connection = null;
try {
Class.forName(dataType.getDriverManager());
connection = DriverManager.getConnection(jdbcUrl, dataSource.getUser(), dataSource.getPassword());
} catch (Exception e) {
throw new RuntimeException(e);
}
try {
DatabaseMetaData metaData = connection.getMetaData();
ResultSet columnsRs = metaData.getColumns(dataSource.getDatabaseName(), dataSource.getDatabaseName(), tableName, "%");
ResultSet primaryKeysRs = metaData.getPrimaryKeys(dataSource.getDatabaseName(), dataSource.getDatabaseName(), tableName);
String primaryKeyColumnName = "";
while (primaryKeysRs.next()) {
primaryKeyColumnName = primaryKeysRs.getString("COLUMN_NAME");
}
// 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,
String columnName) {
Connection connection = buildConnection(driveClass, url, username, password);
PreparedStatement pst = null;
public static String getJavaType(String driveClass, String jdbcUrl, DataSource dataSource, String tableName, String columnName) {
String sql = "";
Connection connection = 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 {
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);
ResultSetMetaData rsd = pst.executeQuery().getMetaData();
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) {
throw new RuntimeException(e);
} finally {
try {
pst.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return javaType;
}

View File

@ -35,6 +35,7 @@
<minio.version>8.2.2</minio.version>
<poi.version>4.1.2</poi.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>
</properties>
@ -207,6 +208,13 @@
<version>${muyu.version}</version>
</dependency>
<!-- PostgreSQL JDBC 驱动 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version> <!-- 使用当前最新稳定版本 -->
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>