API数据表更新
This commit is contained in:
@@ -1,18 +1,222 @@
|
||||
package com.mini.capi.sys.controller;
|
||||
|
||||
import com.mini.capi.biz.domain.DbConfig;
|
||||
import com.mini.capi.biz.domain.SyncTask;
|
||||
import com.mini.capi.biz.service.DbConfigService;
|
||||
import com.mini.capi.biz.service.SyncTaskService;
|
||||
import com.mini.capi.config.DataSourceConfig;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.Data;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
@Controller
|
||||
public class dataController {
|
||||
|
||||
|
||||
@Resource
|
||||
private SyncTaskService syncTaskService;
|
||||
|
||||
|
||||
@Resource
|
||||
private DbConfigService dbConfigService;
|
||||
|
||||
@GetMapping("/Sys/data/list")
|
||||
public String listPage() {
|
||||
return "views/data/list";
|
||||
}
|
||||
|
||||
|
||||
@Data
|
||||
static class TabDetail implements Serializable {
|
||||
private String createTime;
|
||||
private String dbName;
|
||||
private String tableName;
|
||||
private String tableDesc;
|
||||
private String dataLength;
|
||||
private String updateTime;
|
||||
|
||||
public TabDetail(String createTime, String dbName, String tableName, String tableDesc, String dataLength, String updateTime) {
|
||||
this.createTime = createTime;
|
||||
this.dbName = dbName;
|
||||
this.tableName = tableName;
|
||||
this.tableDesc = tableDesc;
|
||||
this.dataLength = dataLength;
|
||||
this.updateTime = updateTime;
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
static class TabColumns implements Serializable {
|
||||
private String tableName;
|
||||
private String sort;
|
||||
private String colName;
|
||||
private String colType;
|
||||
private String colDesc;
|
||||
private String keyType;
|
||||
|
||||
public TabColumns(String tableName, String sort, String colName, String colType, String colDesc, String keyType) {
|
||||
this.tableName = tableName;
|
||||
this.sort = sort;
|
||||
this.colName = colName;
|
||||
this.colType = colType;
|
||||
this.colDesc = colDesc;
|
||||
this.keyType = keyType;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static String buildSelect(List<TabColumns> columns, TabDetail table) {
|
||||
if (columns == null || columns.isEmpty() || table == null) {
|
||||
return "-- no columns or table info";
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder("SELECT\n");
|
||||
for (int i = 0; i < columns.size(); i++) {
|
||||
TabColumns c = columns.get(i);
|
||||
sb.append(" ");
|
||||
if (i > 0) sb.append(",");
|
||||
sb.append(quote(c.getColName()))
|
||||
.append(" -- ")
|
||||
.append(nullToEmpty(c.getColDesc()))
|
||||
.append('\n');
|
||||
}
|
||||
sb.append("FROM ")
|
||||
.append(quote(table.getDbName()))
|
||||
.append('.')
|
||||
.append(quote(table.getTableName()))
|
||||
.append(" -- ")
|
||||
.append(nullToEmpty(table.getTableDesc()))
|
||||
.append(";\n");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/* ---------- 生成 CREATE TABLE ---------- */
|
||||
public static String buildDDL(List<TabColumns> columns, TabDetail table) {
|
||||
if (columns == null || columns.isEmpty() || table == null) {
|
||||
return "-- no columns or table info";
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder("CREATE TABLE ")
|
||||
.append(quote(table.getDbName()))
|
||||
.append('.')
|
||||
.append(quote(table.getTableName()))
|
||||
.append(" (\n");
|
||||
|
||||
for (TabColumns c : columns) {
|
||||
sb.append(" ")
|
||||
.append(leftPad(quote(c.getColName()), 20)) // 左对齐宽度可自行调整
|
||||
.append(leftPad(mapType(c.getColType()), 18))
|
||||
.append(" NOT NULL");
|
||||
|
||||
String comment = nullToEmpty(c.getColDesc());
|
||||
if (!comment.isEmpty()) {
|
||||
sb.append(" COMMENT '").append(comment).append('\'');
|
||||
}
|
||||
sb.append(",\n");
|
||||
}
|
||||
|
||||
// 主键:约定 id 字段
|
||||
boolean hasId = columns.stream().anyMatch(c -> "id".equalsIgnoreCase(c.getColName()));
|
||||
if (hasId) {
|
||||
sb.append(" PRIMARY KEY (").append(quote("id")).append(")\n");
|
||||
} else {
|
||||
sb.setLength(sb.length() - 2); // 去掉最后一个逗号
|
||||
sb.append("\n");
|
||||
}
|
||||
|
||||
sb.append(")\nCOMMENT='")
|
||||
.append(nullToEmpty(table.getTableDesc()))
|
||||
.append("';\n");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/* ---------- 工具 ---------- */
|
||||
private static String nullToEmpty(String s) {
|
||||
return s == null ? "" : s;
|
||||
}
|
||||
|
||||
private static String quote(String name) {
|
||||
return "`" + name + "`";
|
||||
}
|
||||
|
||||
private static String leftPad(String val, int len) {
|
||||
return String.format("%-" + len + "s", val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单类型映射,可根据需要扩充
|
||||
**/
|
||||
private static String mapType(String colType) {
|
||||
if (colType == null) return "varchar(50)";
|
||||
return colType.trim();
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/Sys/data/getTableDetail")
|
||||
public String getTableDetail(String taskId) {
|
||||
public String getTableDetail(String taskId, Model model) {
|
||||
SyncTask task = syncTaskService.getById(taskId);
|
||||
DbConfig dbConfig = dbConfigService.getById(task.getSourceDbId());
|
||||
List<TabDetail> data;
|
||||
List<TabColumns> columns;
|
||||
try {
|
||||
JdbcTemplate jdbcTemplate = DataSourceConfig.createJdbcTemplate(dbConfig);
|
||||
// 补充参数传递
|
||||
String querySql = "SELECT CREATE_TIME,TABLE_NAME,TABLE_COMMENT,MAX_DATA_LENGTH,UPDATE_TIME FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?";
|
||||
data = jdbcTemplate.query(
|
||||
querySql,
|
||||
(rs, rowNum) -> new TabDetail(
|
||||
rs.getString("CREATE_TIME"),
|
||||
dbConfig.getDbName(),
|
||||
rs.getString("TABLE_NAME"),
|
||||
rs.getString("TABLE_COMMENT"),
|
||||
rs.getString("MAX_DATA_LENGTH"),
|
||||
rs.getString("UPDATE_TIME")
|
||||
),
|
||||
dbConfig.getDbName(),
|
||||
task.getSourceTable()
|
||||
);
|
||||
data.sort(Comparator.comparing(TabDetail::getTableName));
|
||||
} catch (Exception e) {
|
||||
data = Collections.emptyList();
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
JdbcTemplate jdbcTemplate = DataSourceConfig.createJdbcTemplate(dbConfig);
|
||||
// 补充参数传递
|
||||
String querySql = "SELECT TABLE_NAME,ORDINAL_POSITION,COLUMN_NAME,COLUMN_TYPE,COLUMN_COMMENT,COLUMN_KEY FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?";
|
||||
columns = jdbcTemplate.query(
|
||||
querySql,
|
||||
(rs, rowNum) -> new TabColumns(
|
||||
rs.getString("TABLE_NAME"),
|
||||
rs.getString("ORDINAL_POSITION"),
|
||||
rs.getString("COLUMN_NAME"),
|
||||
rs.getString("COLUMN_TYPE"),
|
||||
rs.getString("COLUMN_COMMENT"),
|
||||
rs.getString("COLUMN_KEY")
|
||||
),
|
||||
dbConfig.getDbName(),
|
||||
task.getSourceTable()
|
||||
);
|
||||
columns.sort(Comparator.comparing(TabColumns::getSort));
|
||||
} catch (Exception e) {
|
||||
columns = Collections.emptyList();
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
model.addAttribute("data", data);
|
||||
model.addAttribute("columns", columns);
|
||||
model.addAttribute("ddlSql", buildDDL(columns, data.get(0)));
|
||||
model.addAttribute("selectSql", buildSelect(columns, data.get(0)));
|
||||
return "views/data/detail";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.mini.capi.sys.controller;
|
||||
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Controller
|
||||
public class sysController {
|
||||
|
||||
|
||||
@GetMapping("/Sys/sys/icon")
|
||||
public String getIcon() {
|
||||
return "views/icon";
|
||||
}
|
||||
}
|
||||
10
src/main/java/com/mini/capi/utils/vSql.java
Normal file
10
src/main/java/com/mini/capi/utils/vSql.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.mini.capi.utils;
|
||||
|
||||
public class vSql {
|
||||
|
||||
|
||||
private vSql() {}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user