feat(完成数据同步初版):
parent
6cb9dd439a
commit
8a493532a1
|
@ -0,0 +1,31 @@
|
|||
package com.muyu.data.source.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* 资产数据源
|
||||
*
|
||||
* @author HuFangMing
|
||||
* @ClassName: AssetDataSource
|
||||
* @createTime: 2024/4/23 20:30
|
||||
*/
|
||||
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class AssetDataSource {
|
||||
// 主键
|
||||
private Long id;
|
||||
// 名称
|
||||
private String name;
|
||||
// 系统名
|
||||
private String systemName;
|
||||
// 数据库名称
|
||||
private String databaseName;
|
||||
//类型
|
||||
private String type;
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.muyu.data.source.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* 数据库下的表数据展示
|
||||
*
|
||||
* @author HuFangMing
|
||||
* @ClassName: Children
|
||||
* @createTime: 2024/4/23 20:37
|
||||
*/
|
||||
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Children {
|
||||
private Long id;
|
||||
private String name;
|
||||
private String as;
|
||||
private Long dataTotal;
|
||||
private String type;
|
||||
private Long assetId;
|
||||
}
|
|
@ -3,6 +3,8 @@ package com.muyu.data.source.domain;
|
|||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.muyu.common.security.utils.SecurityUtils;
|
||||
import java.util.Date;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
@ -47,15 +49,10 @@ public class DataSource extends BaseEntity {
|
|||
@ApiModelProperty(name = "数据来源系统名称", value = "数据来源系统名称")
|
||||
private String systemName;
|
||||
|
||||
/** 数据库名称 */
|
||||
@Excel(name = "数据库名称")
|
||||
@ApiModelProperty(name = "数据库名称", value = "数据库名称")
|
||||
private String databaseName;
|
||||
|
||||
/** 数据库类型 */
|
||||
@Excel(name = "数据库类型")
|
||||
@ApiModelProperty(name = "数据库类型", value = "数据库类型")
|
||||
private Long databaseType;
|
||||
private String dataType;
|
||||
|
||||
/** 主机地址 */
|
||||
@Excel(name = "主机地址")
|
||||
|
@ -67,6 +64,11 @@ public class DataSource extends BaseEntity {
|
|||
@ApiModelProperty(name = "端口号", value = "端口号")
|
||||
private String port;
|
||||
|
||||
/** 数据库名称 */
|
||||
@Excel(name = "数据库名称")
|
||||
@ApiModelProperty(name = "数据库名称", value = "数据库名称")
|
||||
private String databaseName;
|
||||
|
||||
/** 用户名 */
|
||||
@Excel(name = "用户名")
|
||||
@ApiModelProperty(name = "用户名", value = "用户名")
|
||||
|
@ -113,61 +115,64 @@ public class DataSource extends BaseEntity {
|
|||
private Long maxWaitSize;
|
||||
|
||||
/**
|
||||
* 查询构造器
|
||||
*/
|
||||
* 查询构造器
|
||||
*/
|
||||
public static DataSource queryBuild( DataSourceQueryReq dataSourceQueryReq){
|
||||
return DataSource.builder()
|
||||
.name(dataSourceQueryReq.getName())
|
||||
.systemName(dataSourceQueryReq.getSystemName())
|
||||
.databaseName(dataSourceQueryReq.getDatabaseName())
|
||||
.build();
|
||||
.name(dataSourceQueryReq.getName())
|
||||
.systemName(dataSourceQueryReq.getSystemName())
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加构造器
|
||||
*/
|
||||
* 添加构造器
|
||||
*/
|
||||
public static DataSource saveBuild(DataSourceSaveReq dataSourceSaveReq){
|
||||
return DataSource.builder()
|
||||
.name(dataSourceSaveReq.getName())
|
||||
.systemName(dataSourceSaveReq.getSystemName())
|
||||
.databaseName(dataSourceSaveReq.getDatabaseName())
|
||||
.databaseType(dataSourceSaveReq.getDatabaseType())
|
||||
.host(dataSourceSaveReq.getHost())
|
||||
.port(dataSourceSaveReq.getPort())
|
||||
.user(dataSourceSaveReq.getUser())
|
||||
.password(dataSourceSaveReq.getPassword())
|
||||
.type(dataSourceSaveReq.getType())
|
||||
.connectionParam(dataSourceSaveReq.getConnectionParam())
|
||||
.status(dataSourceSaveReq.getStatus())
|
||||
.initNum(dataSourceSaveReq.getInitNum())
|
||||
.maxNum(dataSourceSaveReq.getMaxNum())
|
||||
.maxWaitTime(dataSourceSaveReq.getMaxWaitTime())
|
||||
.maxWaitSize(dataSourceSaveReq.getMaxWaitSize())
|
||||
.build();
|
||||
.name(dataSourceSaveReq.getName())
|
||||
.systemName(dataSourceSaveReq.getSystemName())
|
||||
.dataType(dataSourceSaveReq.getDatabaseType())
|
||||
.host(dataSourceSaveReq.getHost())
|
||||
.port(dataSourceSaveReq.getPort())
|
||||
.databaseName(dataSourceSaveReq.getDatabaseName())
|
||||
.user(dataSourceSaveReq.getUser())
|
||||
.password(dataSourceSaveReq.getPassword())
|
||||
.type(dataSourceSaveReq.getType())
|
||||
.connectionParam(dataSourceSaveReq.getConnectionParam())
|
||||
.initNum(dataSourceSaveReq.getInitNum())
|
||||
.maxNum(dataSourceSaveReq.getMaxNum())
|
||||
.maxWaitTime(dataSourceSaveReq.getMaxWaitTime())
|
||||
.maxWaitSize(dataSourceSaveReq.getMaxWaitSize())
|
||||
.remark(dataSourceSaveReq.getRemark())
|
||||
.createBy(SecurityUtils.getUsername())
|
||||
.createTime(new Date())
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改构造器
|
||||
*/
|
||||
* 修改构造器
|
||||
*/
|
||||
public static DataSource editBuild(Long id, DataSourceEditReq dataSourceEditReq){
|
||||
return DataSource.builder()
|
||||
.id(id)
|
||||
.name(dataSourceEditReq.getName())
|
||||
.systemName(dataSourceEditReq.getSystemName())
|
||||
.databaseName(dataSourceEditReq.getDatabaseName())
|
||||
.databaseType(dataSourceEditReq.getDatabaseType())
|
||||
.host(dataSourceEditReq.getHost())
|
||||
.port(dataSourceEditReq.getPort())
|
||||
.user(dataSourceEditReq.getUser())
|
||||
.password(dataSourceEditReq.getPassword())
|
||||
.type(dataSourceEditReq.getType())
|
||||
.connectionParam(dataSourceEditReq.getConnectionParam())
|
||||
.status(dataSourceEditReq.getStatus())
|
||||
.initNum(dataSourceEditReq.getInitNum())
|
||||
.maxNum(dataSourceEditReq.getMaxNum())
|
||||
.maxWaitTime(dataSourceEditReq.getMaxWaitTime())
|
||||
.maxWaitSize(dataSourceEditReq.getMaxWaitSize())
|
||||
.build();
|
||||
.name(dataSourceEditReq.getName())
|
||||
.systemName(dataSourceEditReq.getSystemName())
|
||||
.dataType(dataSourceEditReq.getDatabaseType())
|
||||
.host(dataSourceEditReq.getHost())
|
||||
.port(dataSourceEditReq.getPort())
|
||||
.databaseName(dataSourceEditReq.getDatabaseName())
|
||||
.user(dataSourceEditReq.getUser())
|
||||
.password(dataSourceEditReq.getPassword())
|
||||
.type(dataSourceEditReq.getType())
|
||||
.connectionParam(dataSourceEditReq.getConnectionParam())
|
||||
.initNum(dataSourceEditReq.getInitNum())
|
||||
.maxNum(dataSourceEditReq.getMaxNum())
|
||||
.maxWaitTime(dataSourceEditReq.getMaxWaitTime())
|
||||
.maxWaitSize(dataSourceEditReq.getMaxWaitSize())
|
||||
.remark(dataSourceEditReq.getRemark())
|
||||
.updateBy(SecurityUtils.getUsername())
|
||||
.updateTime(new Date())
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package com.muyu.data.source.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* 表结构
|
||||
*
|
||||
* @author HuFangMing
|
||||
* @ClassName: TableData
|
||||
* @createTime: 2024/4/23 20:39
|
||||
*/
|
||||
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class TableData {
|
||||
private Long id;
|
||||
private String name;
|
||||
private String comment;
|
||||
private String isPrimaryKey;
|
||||
private String type;
|
||||
private String mappingType;
|
||||
private Long length;
|
||||
private Long decimalPlaces;
|
||||
private String isNull;
|
||||
private String defaultValue;
|
||||
private String isDict;
|
||||
private String dictKey;
|
||||
private Long childrenId;
|
||||
}
|
|
@ -37,7 +37,7 @@ public class DataSourceEditReq extends BaseEntity {
|
|||
|
||||
/** 数据库类型 */
|
||||
@ApiModelProperty(name = "数据库类型", value = "数据库类型")
|
||||
private Long databaseType;
|
||||
private String databaseType;
|
||||
|
||||
/** 主机地址 */
|
||||
@ApiModelProperty(name = "主机地址", value = "主机地址")
|
||||
|
|
|
@ -46,7 +46,7 @@ public class DataSourceSaveReq extends BaseEntity {
|
|||
/** 数据库类型 */
|
||||
|
||||
@ApiModelProperty(name = "数据库类型", value = "数据库类型")
|
||||
private Long databaseType;
|
||||
private String databaseType;
|
||||
|
||||
/** 主机地址 */
|
||||
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
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 assetDataSource;
|
||||
private String tableName;
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.muyu.data.source.domain.resp;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* 字段描述
|
||||
*
|
||||
* @author HuFangMing
|
||||
* @ClassName: ColumnResp
|
||||
* @createTime: 2024/4/23 20:47
|
||||
*/
|
||||
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ColumnResp {
|
||||
private String columnName;
|
||||
private String columnComment;
|
||||
private String columnKey;
|
||||
private String columnType;
|
||||
private Long length;
|
||||
private Long numericScale;
|
||||
private String isNullable;
|
||||
private String columnDefault;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.muyu.data.source.domain.resp;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* 数据资产数量
|
||||
*
|
||||
* @author HuFangMing
|
||||
* @ClassName: CountResp
|
||||
* @createTime: 2024/4/23 21:15
|
||||
*/
|
||||
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class CountResp {
|
||||
private Long assetStructureCount;
|
||||
private Long assetStructureTableCount;
|
||||
private Long assetStructureTableDataCount;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.muyu.data.source.domain.resp;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* 表
|
||||
*
|
||||
* @author HuFangMing
|
||||
* @ClassName: Table
|
||||
* @createTime: 2024/4/23 21:21
|
||||
*/
|
||||
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Table {
|
||||
private String tableName;
|
||||
private String tableComment;
|
||||
private Long tableRows;
|
||||
}
|
|
@ -1,5 +1,9 @@
|
|||
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.req.ShowTableReq;
|
||||
import java.util.List;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
|
@ -113,9 +117,72 @@ public class DataSourceController extends BaseController {
|
|||
*/
|
||||
@RequiresPermissions("data:source:jdbc")
|
||||
@Log(title = "数据源类型", businessType = BusinessType.DELETE)
|
||||
@PostMapping("/test/{databaseId}")
|
||||
@PostMapping("/test")
|
||||
@ApiOperation("测试数据源")
|
||||
public Result testConnection(@PathVariable("databaseId") Long id) {
|
||||
return Result.success(dataSourceService.testConnection(id));
|
||||
public Result testConnection(@RequestBody DataSource dataSource) {
|
||||
return Result.success(dataSourceService.testConnection(dataSource));
|
||||
}
|
||||
/**
|
||||
* 同步数据
|
||||
*/
|
||||
@RequiresPermissions("data:source:sync")
|
||||
@PostMapping("/sync")
|
||||
public Result synchronous(@RequestBody DataSource dataSource){
|
||||
|
||||
return Result.success( dataSourceService.syncConnection(dataSource));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据库描述
|
||||
*/
|
||||
@RequiresPermissions("data:source:sync")
|
||||
@GetMapping("/getAssetList")
|
||||
public Result<List<AssetDataSource>> getAssetList(){
|
||||
|
||||
return Result.success( dataSourceService.getAssetList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据库下表描述
|
||||
*/
|
||||
@RequiresPermissions("data:source:childrenlist")
|
||||
@GetMapping("/getChildrenList")
|
||||
public Result<List<Children>> getChildrenList(@RequestBody AssetDataSource assetDataSource){
|
||||
|
||||
return Result.success( dataSourceService.getChildrenList(assetDataSource));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取表下的字段描述
|
||||
*/
|
||||
@RequiresPermissions("data:source:addtbledate")
|
||||
@GetMapping("/addTbleDate")
|
||||
public Result addTbleDate(@RequestBody ShowTableReq showTableReq){
|
||||
|
||||
return Result.success( dataSourceService.addTbleDate(showTableReq));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有表结构
|
||||
*/
|
||||
@RequiresPermissions("data:source:selecttablelist")
|
||||
@GetMapping("/selectTableList")
|
||||
public Result<List<DatabaseType>> selectTableList(){
|
||||
|
||||
return Result.success( dataSourceService.selectTableList());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 查询表的总数
|
||||
*/
|
||||
@RequiresPermissions("data:source:selecttabledatacount")
|
||||
@GetMapping("/selectTableDataCount")
|
||||
public Result selectTableDataCount(){
|
||||
|
||||
return Result.success( dataSourceService.selectTableDataCount());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,12 @@
|
|||
package com.muyu.data.source.mapper;
|
||||
|
||||
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.resp.ColumnResp;
|
||||
import com.muyu.data.source.domain.resp.Table;
|
||||
import feign.Param;
|
||||
import java.util.List;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.muyu.data.source.domain.DataSource;
|
||||
|
@ -12,4 +19,26 @@ import com.muyu.data.source.domain.DataSource;
|
|||
*/
|
||||
public interface DataSourceMapper extends BaseMapper<DataSource> {
|
||||
|
||||
|
||||
Result insertAsset(AssetDataSource assetDataSource);
|
||||
|
||||
List<AssetDataSource> selectAssetList();
|
||||
|
||||
List<Table> selectTable(String databaseName);
|
||||
|
||||
Result addChildren(Children children);
|
||||
|
||||
List<Children> getChildrenList(Long id);
|
||||
|
||||
List<ColumnResp> selectColumn(@Param("databaseName") String databaseName, @Param("tableName") String tableName);
|
||||
|
||||
Children selectChildren(@Param("id") Long id, @Param("tableName") String tableName);
|
||||
|
||||
void addTableData(TableData tableData);
|
||||
|
||||
List<TableData> getTableDataList(Long id);
|
||||
|
||||
List<TableData> selectTableData();
|
||||
|
||||
List<Children> selectChildrenList();
|
||||
}
|
||||
|
|
|
@ -1,5 +1,13 @@
|
|||
package com.muyu.data.source.service;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.common.core.web.page.TableDataInfo;
|
||||
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 com.muyu.data.source.domain.DataSource;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
@ -19,5 +27,18 @@ public interface DataSourceService extends IService<DataSource> {
|
|||
*/
|
||||
public List<DataSource> list(DataSource dataSource);
|
||||
|
||||
boolean testConnection(Long databaseId);
|
||||
Boolean testConnection(DataSource dataSource);
|
||||
|
||||
Boolean syncConnection(DataSource dataSource);
|
||||
|
||||
List<AssetDataSource> getAssetList();
|
||||
|
||||
List<Children> getChildrenList(AssetDataSource assetDataSource );
|
||||
|
||||
Result addTbleDate(ShowTableReq showTableReq);
|
||||
|
||||
List<TableData> selectTableList();
|
||||
|
||||
|
||||
CountResp selectTableDataCount();
|
||||
}
|
||||
|
|
|
@ -1,8 +1,21 @@
|
|||
package com.muyu.data.source.service.impl;
|
||||
|
||||
|
||||
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.DatabaseType;
|
||||
import com.muyu.data.source.domain.TableData;
|
||||
import com.muyu.data.source.domain.req.ShowTableReq;
|
||||
import com.muyu.data.source.domain.resp.ColumnResp;
|
||||
import com.muyu.data.source.domain.resp.CountResp;
|
||||
import com.muyu.data.source.domain.resp.Table;
|
||||
import com.muyu.data.source.service.DatabaseTypeService;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -29,6 +42,8 @@ import redis.clients.jedis.Jedis;
|
|||
@Service
|
||||
public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSource> implements DataSourceService {
|
||||
|
||||
@Autowired
|
||||
private DataSourceMapper dataSourceMapper;
|
||||
@Autowired
|
||||
private DataSourceService dataSourceService;
|
||||
|
||||
|
@ -63,21 +78,20 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
|||
/**
|
||||
* 测试连接
|
||||
*
|
||||
* @param id 数据源id
|
||||
* @param
|
||||
*/
|
||||
@Override
|
||||
public boolean testConnection(Long id) {
|
||||
DataSource dataSource=null;
|
||||
public Boolean testConnection(DataSource dataSource) {
|
||||
List<DataSource> dataSourceList = this.list(
|
||||
new LambdaQueryWrapper<DataSource>()
|
||||
.eq(DataSource::getDatabaseType, id)
|
||||
.eq(DataSource::getDataType, dataSource.getDataType())
|
||||
);
|
||||
|
||||
// 使用Set进行去重,
|
||||
Set<DataSource> uniqueDataSources = new HashSet<>(dataSourceList);
|
||||
|
||||
Optional<DataSource> source = uniqueDataSources.stream().distinct().findFirst();
|
||||
if (source.isPresent()){
|
||||
if (source.isPresent()) {
|
||||
dataSource = source.get();
|
||||
}
|
||||
|
||||
|
@ -86,34 +100,38 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
|||
boolean flag = false;
|
||||
DatabaseType dataType = databaseTypeService.getOne(
|
||||
new LambdaQueryWrapper<DatabaseType>()
|
||||
.eq(DatabaseType::getDatabaseId, dataSource.getDatabaseType())
|
||||
.eq(DatabaseType::getDatabaseId, dataSource.getDataType())
|
||||
);
|
||||
|
||||
if (dataType.getDatabaseName().equals("mysql")||dataType.equals("MYSQL")){
|
||||
driveClass=dataType.getDriverManager();
|
||||
if (dataType.getDatabaseName().equals("mysql") || dataType.equals("MYSQL")) {
|
||||
driveClass = dataType.getDriverManager();
|
||||
|
||||
}
|
||||
try {
|
||||
if (dataType.getDriverManager() != null && dataType.getUrlPre() != null){
|
||||
if ("mysql".equals(dataType.getDatabaseName())){
|
||||
if (dataType.getDriverManager() != null && dataType.getUrlPre() != null) {
|
||||
if ("mysql".equals(dataType.getDatabaseName())) {
|
||||
driveClass = dataType.getDriverManager();
|
||||
jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/" + dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
|
||||
jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/"
|
||||
+ dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
|
||||
}
|
||||
if ("oracle".equals(dataType.getDatabaseName())){
|
||||
if ("oracle".equals(dataType.getDatabaseName())) {
|
||||
driveClass = dataType.getDriverManager();
|
||||
jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + ":" + dataSource.getDatabaseName();
|
||||
jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + ":"
|
||||
+ dataSource.getDatabaseName();
|
||||
}
|
||||
if ("sqlserver".equals(dataType.getDatabaseName())){
|
||||
if ("sqlserver".equals(dataType.getDatabaseName())) {
|
||||
driveClass = dataType.getDriverManager();
|
||||
jdbcUrl = dataType.getUrlPre()+ dataSource.getHost() +":"+dataSource.getPort()+";databaseName="+dataSource.getDatabaseName();
|
||||
jdbcUrl =
|
||||
dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + ";databaseName="
|
||||
+ dataSource.getDatabaseName();
|
||||
}
|
||||
flag = testDatasource(driveClass, jdbcUrl, dataSource.getUser(), dataSource.getPassword());
|
||||
}else if(dataType.getDriverManager()== null && dataType.getUrlPre() == null){
|
||||
} else if (dataType.getDriverManager() == null && dataType.getUrlPre() == null) {
|
||||
// redis
|
||||
//连接指定的redis
|
||||
Jedis jedis = new Jedis(dataSource.getHost(), Integer.valueOf(dataSource.getPort()));
|
||||
//如果有密码则需要下面这一行
|
||||
if (dataSource.getPassword() != null && !dataSource.getPassword().equals("")){
|
||||
if (dataSource.getPassword() != null && !dataSource.getPassword().equals("")) {
|
||||
jedis.auth(dataSource.getPassword());
|
||||
}
|
||||
//查看服务是否运行,运行正常的话返回一个PONG,否则返回一个连接错误
|
||||
|
@ -125,13 +143,180 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
|||
}
|
||||
}
|
||||
return flag;
|
||||
}catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
return flag;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步数据
|
||||
*
|
||||
* @param dataSource
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Boolean syncConnection(DataSource dataSource) {
|
||||
if ("mysql".equals(dataSource.getDataType())) {
|
||||
AssetDataSource.builder()
|
||||
.name(dataSource.getName())
|
||||
.systemName(dataSource.getSystemName())
|
||||
.databaseName(dataSource.getDatabaseName())
|
||||
.type("dataSource")
|
||||
.build();
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AssetDataSource> getAssetList() {
|
||||
List<AssetDataSource> list = dataSourceMapper.selectAssetList();
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Children> getChildrenList(AssetDataSource assetDataSource) {
|
||||
//查询数据源对象
|
||||
DataSource dataSource = this.getOne(new LambdaQueryWrapper<DataSource>() {{
|
||||
eq(DataSource::getName, assetDataSource.getName());
|
||||
eq(DataSource::getDatabaseName, assetDataSource.getDatabaseName());
|
||||
}});
|
||||
//数据库类型为mysql
|
||||
if ("mysql".equals(dataSource.getDataType())) {
|
||||
//根据id查询表描述
|
||||
List<Children> childrenList = dataSourceMapper.getChildrenList(assetDataSource.getId());
|
||||
//获取表描述的集合
|
||||
List<Table> tableList = dataSourceMapper.selectTable(assetDataSource.getDatabaseName());
|
||||
//判断是否存在
|
||||
if (childrenList == null || childrenList.size() == 0) {
|
||||
tableList.forEach(table -> {
|
||||
Children children = Children.builder()
|
||||
.name(table.getTableName())
|
||||
.as(table.getTableComment())
|
||||
.dataTotal(table.getTableRows())
|
||||
.type("dataTble")
|
||||
.assetId(assetDataSource.getId())
|
||||
.build();
|
||||
//添加到数据库
|
||||
dataSourceMapper.addChildren(children);
|
||||
|
||||
});
|
||||
}
|
||||
//返回表描述
|
||||
return childrenList;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Result addTbleDate(ShowTableReq showTableReq) {
|
||||
// 查询数据源对象
|
||||
DataSource dataSource = this.getOne(new LambdaQueryWrapper<>() {{
|
||||
eq(DataSource::getName, showTableReq.getAssetDataSource().getName());
|
||||
eq(DataSource::getDatabaseName, showTableReq.getAssetDataSource().getDatabaseName());
|
||||
}});
|
||||
Children children = dataSourceMapper.selectChildren(showTableReq.getAssetDataSource().getId(),
|
||||
showTableReq.getTableName());
|
||||
// 获取数据类型对象
|
||||
Result<DatabaseType> dataTypeResult = dataTypeController.getByType(dataSource.getDataType());
|
||||
if (Result.isError(dataTypeResult)) {
|
||||
throw new RuntimeException("数据类型不存在");
|
||||
}
|
||||
DatabaseType dataType = dataTypeResult.getData();
|
||||
String jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/"
|
||||
+ dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
|
||||
List<ColumnResp> columnRespList = dataSourceMapper.selectColumn(dataSource.getDatabaseName(),
|
||||
showTableReq.getTableName());
|
||||
List<TableData> tableDataList = dataSourceMapper.getTableDataList(children.getId());
|
||||
|
||||
if (tableDataList == null || tableDataList.size() == 0) {
|
||||
columnRespList.forEach(columnResp -> {
|
||||
String javaType = getJavaType(dataType.getDriverManager(), jdbcUrl, dataSource.getUser(),
|
||||
dataSource.getPassword(), showTableReq.getTableName(), columnResp.getColumnName());
|
||||
TableData tableData = TableData.builder()
|
||||
.name(columnResp.getColumnName())
|
||||
.comment(columnResp.getColumnComment())
|
||||
.isPrimaryKey(columnResp.getColumnKey().equals("PRI") ? "Y" : "N")
|
||||
.type(columnResp.getColumnType())
|
||||
.mappingType(javaType)
|
||||
.length(columnResp.getLength())
|
||||
.decimalPlaces(columnResp.getNumericScale())
|
||||
.isNull(columnResp.getIsNullable().equals("YES") ? "Y" : "N")
|
||||
.defaultValue(columnResp.getColumnDefault())
|
||||
.isDict("N")
|
||||
.dictKey(null)
|
||||
.childrenId(children.getId())
|
||||
.build();
|
||||
dataSourceMapper.addTableData(tableData);
|
||||
});
|
||||
}
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TableData> selectTableList() {
|
||||
List<TableData> tableDataList = dataSourceMapper.selectTableData();
|
||||
return tableDataList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CountResp selectTableDataCount() {
|
||||
List<AssetDataSource> assetDataSourceList = dataSourceMapper.selectAssetList();
|
||||
long size = assetDataSourceList.size();
|
||||
List<Children> childrenList = dataSourceMapper.selectChildrenList();
|
||||
long size1 = childrenList.size();
|
||||
long sum = childrenList.stream().mapToLong(Children::getDataTotal).sum();
|
||||
CountResp countResp = CountResp.builder()
|
||||
.assetStructureCount(size)
|
||||
.assetStructureTableCount(size1)
|
||||
.assetStructureTableDataCount(sum)
|
||||
.build();
|
||||
return countResp;
|
||||
}
|
||||
|
||||
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;
|
||||
String javaType = null;
|
||||
try {
|
||||
String sql = "select * from " + tableName;
|
||||
pst = connection.prepareStatement(sql);
|
||||
ResultSetMetaData rsd = pst.executeQuery().getMetaData();
|
||||
for (int i = 1; i <= rsd.getColumnCount(); i++) {
|
||||
if (rsd.getColumnName(i).equals(columnName)) {
|
||||
if (rsd.getColumnClassName(i).equals("java.time.LocalDateTime")) {
|
||||
javaType = "LocalDateTime";
|
||||
} else {
|
||||
javaType = rsd.getColumnClassName(i).replace("java.lang.", "");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
try {
|
||||
pst.close();
|
||||
pst = null;
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return javaType;
|
||||
}
|
||||
|
||||
public static Connection buildConnection(String driveClass, String url, String username, String password) {
|
||||
try {
|
||||
Class.forName(driveClass);
|
||||
Connection connection = DriverManager.getConnection(url, username, password);
|
||||
return connection;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 建立连接
|
||||
public static boolean testDatasource(String driveClass, String url, String username, String password) {
|
||||
try {
|
||||
Class.forName(driveClass);
|
||||
|
|
|
@ -9,7 +9,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<result property="name" column="name" />
|
||||
<result property="systemName" column="system_name" />
|
||||
<result property="databaseName" column="database_name" />
|
||||
<result property="databaseType" column="database_type" />
|
||||
<result property="dataType" column="database_type" />
|
||||
<result property="host" column="host" />
|
||||
<result property="port" column="port" />
|
||||
<result property="user" column="user" />
|
||||
|
@ -31,4 +31,75 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<sql id="selectDataSourceVo">
|
||||
select id, name, system_name, database_name, database_type, host, port, user, password, type, connection_param, status, init_num, max_num, max_wait_time, max_wait_size, remark, create_by, create_time, update_by, update_time from data_source
|
||||
</sql>
|
||||
|
||||
<insert id="insertAsset">
|
||||
INSERT INTO `data_source`.`asset_data_source` (`name`, `system_name`, `database_name`, `type`)
|
||||
VALUES (#{name}, #{systemName}, #{databaseName}, #{type});
|
||||
</insert>
|
||||
|
||||
<insert id="addChildren">
|
||||
INSERT INTO `data_source`.`children` (`name`, `as`, `data_total`, `type`, `asset_id`)
|
||||
VALUES (#{name}, #{as}, #{dataTotal}, #{type}, #{assetId});
|
||||
</insert>
|
||||
|
||||
<insert id="addTableData">
|
||||
INSERT INTO `data_source`.`table_data` (`name`, `comment`, `is_primary_key`, `type`, `mapping_type`, `length`, `decimal_places`, `is_null`, `default_value`, `is_dict`, `dict_key`, `children_id`)
|
||||
VALUES (#{name}, #{comment}, #{isPrimaryKey}, #{type}, #{mappingType}, #{length}, #{decimalPlaces}, #{isNull}, #{defaultValue}, #{isDict}, #{dictKey}, #{childrenId});
|
||||
</insert>
|
||||
|
||||
<select id="selectAssetList" resultType="com.muyu.data.source.domain.AssetDataSource">
|
||||
select *
|
||||
from asset_data_source
|
||||
</select>
|
||||
|
||||
<select id="selectTable" resultType="com.muyu.data.source.domain.resp.Table">
|
||||
select table_name,
|
||||
table_comment,
|
||||
table_rows
|
||||
from INFORMATION_SCHEMA.Tables
|
||||
where table_schema = #{databaseName}
|
||||
</select>
|
||||
|
||||
<select id="getChildrenList" resultType="com.muyu.data.source.domain.Children">
|
||||
select *
|
||||
from children
|
||||
where asset_id = #{id}
|
||||
</select>
|
||||
|
||||
<select id="selectColumn" resultType="com.muyu.data.source.domain.resp.ColumnResp">
|
||||
SELECT COLUMN_NAME as column_name,
|
||||
COLUMN_COMMENT as column_comment,
|
||||
column_key as column_key,
|
||||
DATA_TYPE as column_type,
|
||||
CHARACTER_MAXIMUM_LENGTH as length,
|
||||
NUMERIC_SCALE as numeric_scale,
|
||||
IS_NULLABLE as is_nullable,
|
||||
COLUMN_DEFAULT as column_default
|
||||
FROM INFORMATION_SCHEMA.COLUMNS
|
||||
where table_schema = #{databaseName}
|
||||
AND table_name = #{tableName}
|
||||
</select>
|
||||
|
||||
<select id="getTableDataList" resultType="com.muyu.data.source.domain.TableData">
|
||||
select * from table_data where children_id=#{id}
|
||||
</select>
|
||||
|
||||
<select id="selectChildren" resultType="com.muyu.data.source.domain.Children">
|
||||
select * from children
|
||||
<where>
|
||||
<if test="id != null">
|
||||
and asset_id=#{id}
|
||||
</if>
|
||||
<if test="tableName != null">
|
||||
and name=#{tableName}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<select id="selectTableData" resultType="com.muyu.data.source.domain.TableData">
|
||||
select * from table_data
|
||||
</select>
|
||||
<select id="selectChildrenList" resultType="com.muyu.data.source.domain.Children">
|
||||
select * from children
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
Loading…
Reference in New Issue