This commit is contained in:
2019-04-11 23:04:13 +08:00
parent 6e7388ddf4
commit 14795814e3
24 changed files with 384 additions and 66 deletions

View File

@@ -218,10 +218,8 @@ public class Kv<K,V> extends LinkedHashMap<K,V> {
Object obj = null;
try {
obj = clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException | IllegalAccessException e) {
new IllegalArgumentException("创建对象实列失败", e); // 检查clazz是否有无参构造
}
for (String k : (Set<String>)map.keySet()) {

View File

@@ -10,6 +10,7 @@ import net.tccn.meta.MetaService;
import net.tccn.meta.MetaTable;
import net.tccn.plat.DbPlat;
import net.tccn.plat.SysPlat;
import org.redkale.util.Comment;
import java.util.*;
import java.util.function.BiFunction;
@@ -470,6 +471,12 @@ public class MetaKit {
return dbAccount.get();
}
@Comment("通过平台token 得到平台id")
public static String getPlatId(String platToken) { //
Optional<SysPlat> plat = sysPlats.stream().filter(x -> x.getToken().equals(platToken)).findAny();
return plat.get().getKey();
}
public String nextAlias(String x) {
return next(x, "");
}

View File

@@ -1,5 +1,7 @@
package net.tccn.dbq;
import net.tccn.dbq.table.Column;
/**
* @author: liangxianyou at 2018/10/17 17:24.
*/
@@ -11,6 +13,7 @@ public class Field {
private String inType;
private String inExt;
public Field() {}
//============== getter/setter =============
public String getName() {
@@ -92,6 +95,15 @@ public class Field {
return InType.SELECT_EXT.name.equalsIgnoreCase(inType);
}
public static Field toAs(Column column) {
Field _bean = new Field();
_bean.setName(column.getField());
_bean.setType(column.getType());
_bean.setLabel(column.getComment());
return _bean;
}
@Override
public boolean equals(Object name) {
return (this.name == null && name == null) || this.name.equals(name);

View File

@@ -125,7 +125,7 @@ public class JdbcService {
connection.setCatalog(table.getCatalog());
}
String tableDdl = table.getTableDdl();
String tableDdl = ""; // table.getTableDdl();
System.out.println(tableDdl);
try (PreparedStatement ps = connection.prepareStatement(tableDdl)) {
return ps.execute();

View File

@@ -137,9 +137,6 @@ public class DbSourceMysql implements DbSource {
System.out.println("创建新的连接:" + x);
} else {
conn = queue.take();
if (conn != null) {
System.out.println("获取已有连接" + conn);
}
}
} catch (SQLException | InterruptedException e) {
if (e instanceof InterruptedException) {
@@ -167,7 +164,7 @@ public class DbSourceMysql implements DbSource {
if (connection != null) {
queue.put(connection);
conns.put(accountKey, queue);
System.out.println("还回连接:" + connection);
//System.out.println("还回连接:" + connection);
}
} catch (InterruptedException e) {
e.printStackTrace();

View File

@@ -56,5 +56,4 @@ public class Column {
public void setNull(String notNull) {
this.notNull = "NO".equalsIgnoreCase(notNull) ? true : false;
}
}

View File

@@ -1,6 +1,7 @@
package net.tccn.dbq.table;
import java.util.ArrayList;
import java.util.List;
/**
@@ -11,8 +12,9 @@ public class Table {
private String catalog; //库名称
private String name; //表名称
private String comment; //表备注
private List<Column> columns; //表的字段列
private List<Column> columns = new ArrayList<>(); //表的字段列
public Table() {}
public Table(String name, String comment) {
this.name = name;
this.comment = comment;
@@ -55,7 +57,7 @@ public class Table {
//------------------------------
//Dev
public String getTableDdl() {
public String _getTableDdl() {
StringBuilder buf = new StringBuilder();
buf.append("CREATE TABLE " + name + "(");

View File

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

View File

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

View 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;
});
}
}

View File

@@ -2,6 +2,7 @@ package net.tccn.service;
import com.arangodb.Predicate;
import com.google.gson.Gson;
import net.tccn.base.MetaKit;
import org.redkale.net.http.RestMapping;
import org.redkale.service.Service;
import org.redkale.source.CacheSource;
@@ -88,4 +89,8 @@ public class BaseService implements Service {
return prop.getProperty(k);
}
public String platId(String token) {
return MetaKit.getPlatId(token);
}
}

View File

@@ -23,7 +23,7 @@ public class DataService extends BaseService {
private MetadataService metadataService;
@RestMapping(name = "list", comment = "数据分页列表")
@RestMapping(name = "list", auth = false, comment = "数据分页列表")
public JBean findList(FBean fBean, @RestParam(name = "platToken") String token) {
JBean jBean = new JBean();
try {

View File

@@ -1,12 +1,13 @@
import com.google.gson.Gson;
import net.tccn.base.Kv;
import net.tccn.base.MetaKit;
import net.tccn.dbq.fbean.FBean;
import net.tccn.dbq.parser.ParseMysql;
import net.tccn.dbq.jdbc.api.DbAccount;
import net.tccn.dbq.jdbc.api.DbKit;
import net.tccn.base.MetaKit;
import net.tccn.dbq.parser.ParseMysql;
import net.tccn.qtask.QRuner;
import net.tccn.qtask.Task;
import net.tccn.user.User;
import org.junit.Test;
import java.util.Date;
@@ -138,5 +139,15 @@ public class RunTest<T> {
System.out.println(xx);
}
//@Test
public void userCreate() {
User user = new User();
user.setUsername("admin");
user.setCreateTime(System.currentTimeMillis());
user.setPwd(User.md5IfNeed("123456"));
user.setStatus(1);
user.save();
}
}