.
This commit is contained in:
@@ -6,6 +6,7 @@ import net.tccn.dbq.Field;
|
||||
import javax.persistence.Table;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 元数据
|
||||
@@ -24,6 +25,7 @@ public class MetaTable extends Doc<MetaTable> implements Serializable {
|
||||
private String dbPlatId; //所属数据平台
|
||||
private String catalog; //所在database
|
||||
|
||||
private Integer hv;//临时
|
||||
//=============== getter/setter ============
|
||||
|
||||
public String getName() {
|
||||
@@ -81,4 +83,25 @@ public class MetaTable extends Doc<MetaTable> implements Serializable {
|
||||
public void setCatalog(String catalog) {
|
||||
this.catalog = catalog;
|
||||
}
|
||||
|
||||
public Integer getHv() {
|
||||
return hv;
|
||||
}
|
||||
|
||||
public void setHv(Integer hv) {
|
||||
this.hv = hv;
|
||||
}
|
||||
|
||||
// ------------------------------------------------
|
||||
public static MetaTable toAs(net.tccn.dbq.table.Table table) {
|
||||
List<Field> fields = table.getColumns().stream().map(Field::toAs).collect(Collectors.toList());
|
||||
|
||||
MetaTable _bean = new MetaTable();
|
||||
_bean.setName(table.getName());
|
||||
_bean.setComment(table.getComment());
|
||||
_bean.setCatalog(table.getCatalog());
|
||||
_bean.setItems(fields);
|
||||
|
||||
return _bean;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,13 +37,13 @@ public class _DbService extends BaseService {
|
||||
}
|
||||
|
||||
@RestMapping(name = "table_list", comment = "数据库表列表")
|
||||
public JBean tableList(DbAccount dbAccount, String dbPlatId, String[] catalogs) {
|
||||
JBean jBean = new JBean();
|
||||
public List<Table> tableList(DbAccount dbAccount, String dbPlatId, String[] catalogs) {
|
||||
|
||||
DbKit dbKit = MetaKit.getDbKit(dbPlatId);
|
||||
|
||||
StringBuffer sqlBuf = new StringBuffer("SELECT TABLE_NAME 'name',TABLE_COMMENT 'comment',table_schema 'catalog' FROM INFORMATION_SCHEMA.TABLES");
|
||||
if (catalogs != null && catalogs.length > 0) {
|
||||
sqlBuf.append("WHERE TABLE_SCHEMA in (");
|
||||
sqlBuf.append(" WHERE TABLE_SCHEMA in (");
|
||||
for (String catalog : catalogs) {
|
||||
sqlBuf.append("'").append(catalog).append("',");
|
||||
}
|
||||
@@ -52,7 +52,7 @@ public class _DbService extends BaseService {
|
||||
}
|
||||
List<Table> list = dbKit.findList(sqlBuf.toString(), Table.class);
|
||||
|
||||
return jBean.setBody(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
@RestMapping(name = "table_info", comment = "数据库表详情")
|
||||
@@ -62,7 +62,7 @@ public class _DbService extends BaseService {
|
||||
DbKit dbKit = MetaKit.getDbKit(dbPlatId);
|
||||
|
||||
String sql = String.format("SELECT TABLE_NAME 'name',TABLE_COMMENT 'comment',table_schema 'catalog' FROM INFORMATION_SCHEMA.TABLES where TABLE_NAME='%s'", tableName);
|
||||
String columnSql = String.format("SHOW FULL COLUMNS FROM '%s.%s'", catalog, tableName);
|
||||
String columnSql = String.format("SHOW FULL COLUMNS FROM %s.`%s`", catalog, tableName);
|
||||
|
||||
CompletableFuture<Table> tableFuture = CompletableFuture.supplyAsync(() -> dbKit.findfirst(sql, Table.class));
|
||||
CompletableFuture<List<Column>> columnFuture = CompletableFuture.supplyAsync(() -> dbKit.findList(columnSql, Column.class));
|
||||
@@ -70,7 +70,9 @@ public class _DbService extends BaseService {
|
||||
try {
|
||||
Table table = tableFuture.get();
|
||||
table.setColumns(columnFuture.get());
|
||||
jBean.setBody(table);
|
||||
|
||||
//jBean.setBody(table);
|
||||
jBean.setBody(MetaTable.toAs(table)); //todo: 将此转换提取到 单独方法中<依据六边形架构原则>
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
jBean.set(-1, "查询表信息失败");
|
||||
new IllegalArgumentException("查询表信息失败", e);
|
||||
@@ -88,4 +90,12 @@ public class _DbService extends BaseService {
|
||||
|
||||
return jBean;
|
||||
}
|
||||
|
||||
@RestMapping(ignore = true, comment = "查询表信息")
|
||||
public List<Table> tableList(String dbPlatId, String catalog, String ... tables) {
|
||||
//todo:
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
131
src/main/java/net/tccn/meta/_TableService.java
Normal file
131
src/main/java/net/tccn/meta/_TableService.java
Normal file
@@ -0,0 +1,131 @@
|
||||
package net.tccn.meta;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.dbq.jdbc.api.DbAccount;
|
||||
import net.tccn.dbq.table.Table;
|
||||
import net.tccn.service.BaseService;
|
||||
import net.tccn.service._FileService;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.net.http.RestParam;
|
||||
import org.redkale.net.http.RestService;
|
||||
import org.redkale.util.Comment;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* 元数据实体管理
|
||||
* 1、实体导入,
|
||||
* 2、实体信息维护
|
||||
* 3、数据库表管理
|
||||
*/
|
||||
@RestService(automapping = true, comment = "元数据实体管理")
|
||||
public class _TableService extends BaseService {
|
||||
|
||||
@Resource
|
||||
_FileService fileService;
|
||||
@Resource
|
||||
_DbService dbService;
|
||||
|
||||
|
||||
@RestMapping(name = "sheets", comment = "导入选择列表数据准备")
|
||||
public JBean sheets(String cate, //类型
|
||||
//excel {文件地址}
|
||||
String filePath,
|
||||
|
||||
//mysql {数据库连接账号、数据源id、数据库database数组}
|
||||
DbAccount dbAccount, String dbPlatId, String[] catalogs,
|
||||
@RestParam(name = "platToken") String token) {
|
||||
|
||||
JBean jBean = new JBean();
|
||||
|
||||
if ("excel".equals(cate)) {
|
||||
jBean.setBody(fileService.data(filePath, token));
|
||||
|
||||
} else if ("mysql".equals(cate)){
|
||||
List<Table> list = dbService.tableList(dbAccount, dbPlatId, catalogs);
|
||||
|
||||
String[] tableArr = list.stream().map(Table::getName).toArray(String[]::new);
|
||||
CompletableFuture<List<String>> hvFuture = tableExist(tableArr, token);
|
||||
|
||||
try {
|
||||
List<String> _hv = hvFuture.get();
|
||||
|
||||
List<MetaTable> sheets = new ArrayList<>();
|
||||
list.forEach(x -> {
|
||||
MetaTable bean = MetaTable.toAs(x);
|
||||
bean.setHv(_hv.contains(x.getName()) ? 1 : 0);
|
||||
sheets.add(bean);
|
||||
});
|
||||
|
||||
//对数据分组后返回
|
||||
Kv<String, MetaTable> data = Kv.of();
|
||||
sheets.forEach(x -> data.set(x.getName(), x));
|
||||
|
||||
jBean.setBody(data);
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return jBean;
|
||||
}
|
||||
|
||||
|
||||
/*public JBean tableInfo(DbAccount dbAccount,
|
||||
String dbPlatId, String catalog, String tableName) {
|
||||
|
||||
dbService.tableInfo(dbAccount, dbPlatId, catalog, tableName)
|
||||
|
||||
return null;
|
||||
}*/
|
||||
|
||||
@RestMapping(name = "table_save", comment = "保存数据源实体到元数据实体表")
|
||||
public void tableSave(String dbPlatId,
|
||||
String catalog,
|
||||
String[] tableArr,
|
||||
@RestParam(name = "platToken") String token) {
|
||||
|
||||
CompletableFuture<List<String>> hvfuture = tableExist(tableArr, token);
|
||||
List<Table> tables = dbService.tableList(dbPlatId, catalog, tableArr);
|
||||
|
||||
try {
|
||||
List<String> hvs = hvfuture.get();
|
||||
tables.forEach(t -> {
|
||||
if (!hvs.contains(t.getName())) {
|
||||
MetaTable metaTable = MetaTable.toAs(t);
|
||||
metaTable.setCatalog(catalog);
|
||||
metaTable.setDbPlatId(dbPlatId);
|
||||
metaTable.setAlias("");//todo: 表别名
|
||||
|
||||
//保存数据到元数据表
|
||||
metaTable.save();
|
||||
}
|
||||
});
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Comment("查询元数据中存在的表")
|
||||
private CompletableFuture<List<String>> tableExist(String[] tableArr, String token) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("for d in MetaTable\n" +
|
||||
" filter d.name in [");
|
||||
for (String x : tableArr) {
|
||||
buf.append("'").append(x).append("',");
|
||||
}
|
||||
buf.deleteCharAt(buf.length() - 1);
|
||||
buf.append("] and d.sysPlatId=='" + platId(token) + "'\n" +
|
||||
" return d.name");
|
||||
List<String> hv = MetaTable.dao.find(buf.toString(), String.class); //在元数据中已经存在的sheet
|
||||
return hv;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user