新增MySQL和pg数据库的同步

This commit is contained in:
2025-08-27 12:33:40 +08:00
parent db13de9fc5
commit 1442f08a86
2 changed files with 47 additions and 5 deletions

View File

@@ -0,0 +1,24 @@
package com.mini.capi.model;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@Data
public class TabResult implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private String tableName;
private String tableDesc;
public TabResult(String tableName,String tableDesc){
this.tableName = tableName;
this.tableDesc = tableDesc;
}
}

View File

@@ -4,6 +4,7 @@ import com.mini.capi.biz.domain.DbConfig;
import com.mini.capi.biz.service.DbConfigService; import com.mini.capi.biz.service.DbConfigService;
import com.mini.capi.config.DataSourceConfig; import com.mini.capi.config.DataSourceConfig;
import com.mini.capi.model.ApiResult; import com.mini.capi.model.ApiResult;
import com.mini.capi.model.TabResult;
import com.mini.capi.utils.vToken; import com.mini.capi.utils.vToken;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
@@ -11,6 +12,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -26,7 +28,7 @@ public class dbController {
* 获取MySQL的当前连接下的所有数据表 * 获取MySQL的当前连接下的所有数据表
*/ */
@GetMapping("/getApiSourceTables") @GetMapping("/getApiSourceTables")
public ApiResult<List<String>> listSourceTables(String token, String dbId) { public ApiResult<List<TabResult>> listSourceTables(String token, String dbId) {
// 1. 验证token有效性 // 1. 验证token有效性
if (!vToken.isValidToken(token)) { if (!vToken.isValidToken(token)) {
return ApiResult.error(401, "无效的访问令牌"); return ApiResult.error(401, "无效的访问令牌");
@@ -45,15 +47,31 @@ public class dbController {
try { try {
JdbcTemplate jdbcTemplate = DataSourceConfig.createJdbcTemplate(dbConfig); JdbcTemplate jdbcTemplate = DataSourceConfig.createJdbcTemplate(dbConfig);
// 补充参数传递修复SQL参数绑定问题 // 补充参数传递
String querySql = "SELECT table_name FROM information_schema.tables WHERE table_schema = ?"; String querySql = "SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.tables WHERE TABLE_SCHEMA = ?";
List<Map<String, Object>> result = jdbcTemplate.queryForList(querySql, dbConfig.getDbSchema()); List<Map<String, Object>> result = jdbcTemplate.queryForList(querySql, dbConfig.getDbSchema());
List<String> data = result.stream() List<TabResult> data = result.stream()
.map(row -> row.values().iterator().next().toString()) .map(row -> {
String tableName = row.get("TABLE_NAME") != null ? row.get("TABLE_NAME").toString() : "";
String tableDesc = row.get("TABLE_COMMENT") != null ? row.get("TABLE_COMMENT").toString() : "";
return new TabResult(tableName, getComment(tableName, tableDesc));
})
.sorted(Comparator.comparing(TabResult::getTableName)) // 按表名排序
.toList(); .toList();
return ApiResult.success(data); return ApiResult.success(data);
} catch (Exception e) { } catch (Exception e) {
return ApiResult.error(101, e.getMessage()); return ApiResult.error(101, e.getMessage());
} }
} }
private String getComment(String tableName, String tableDesc) {
boolean hasTableDesc = tableDesc != null && !tableDesc.trim().isEmpty();
// 根据表描述是否存在返回不同格式
if (hasTableDesc) {
return String.format("%s(%s)", tableDesc.trim(), tableName);
} else {
return tableName;
}
}
} }