1、修改Mysql线程池逻辑
2、新增数据表信息查询接口 3、优化前端代码逻辑 4、新增业面业务功能提示信息窗口
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
package net.tccn.meta;
|
||||
package net.tccn.base;
|
||||
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.base.UtilityExt;
|
||||
import net.tccn.base.arango.Doc;
|
||||
import net.tccn.dbq.Field;
|
||||
import net.tccn.dbq.fbean.FilterType;
|
||||
import net.tccn.dbq.jdbc.api.DbAccount;
|
||||
import net.tccn.dbq.jdbc.api.DbKit;
|
||||
import net.tccn.meta.MetaLink;
|
||||
import net.tccn.meta.MetaService;
|
||||
import net.tccn.meta.MetaTable;
|
||||
import net.tccn.plat.DbPlat;
|
||||
import net.tccn.plat.SysPlat;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
@@ -23,6 +28,7 @@ public class MetaKit {
|
||||
private static List<MetaService> metaServices;
|
||||
|
||||
private static List<DbAccount> dbPlats;
|
||||
private static List<SysPlat> sysPlats;
|
||||
|
||||
static {
|
||||
metaTables = MetaTable.dao.find();
|
||||
@@ -30,6 +36,17 @@ public class MetaKit {
|
||||
metaServices = MetaService.dao.find();
|
||||
|
||||
dbPlats = DbAccount.dao.find();
|
||||
sysPlats = SysPlat.dao.find();
|
||||
}
|
||||
|
||||
public static <T extends Doc> void reload(Class<T> clazz) {
|
||||
|
||||
if (MetaTable.class == clazz) metaTables = MetaTable.dao.find();
|
||||
else if (MetaLink.class == clazz) metaLinks = MetaLink.dao.find();
|
||||
else if (MetaService.class == clazz) metaServices = MetaService.dao.find();
|
||||
else if (DbAccount.class == clazz) dbPlats = DbAccount.dao.find();
|
||||
else if (DbPlat.class == clazz) dbPlats = DbAccount.dao.find();
|
||||
else if (SysPlat.class == clazz) sysPlats = SysPlat.dao.find();
|
||||
}
|
||||
|
||||
//----- get/set ----
|
||||
@@ -61,6 +78,14 @@ public class MetaKit {
|
||||
MetaKit.dbPlats = dbPlats;
|
||||
}
|
||||
|
||||
public static List<SysPlat> getSysPlats() {
|
||||
return sysPlats;
|
||||
}
|
||||
|
||||
public static void setSysPlats(List<SysPlat> sysPlats) {
|
||||
MetaKit.sysPlats = sysPlats;
|
||||
}
|
||||
|
||||
//-----------------------------------
|
||||
public static void setMetaServices(List<MetaService> metaServices) {
|
||||
MetaKit.metaServices = metaServices;
|
||||
@@ -433,6 +458,18 @@ public class MetaKit {
|
||||
return tables;
|
||||
}
|
||||
|
||||
public static DbKit getDbKit(String dbPlatId) {
|
||||
Optional<DbAccount> dbAccount = dbPlats.stream().filter(x -> x.getKey().equals(dbPlatId)).findAny();
|
||||
|
||||
return new DbKit(dbAccount.get());
|
||||
}
|
||||
|
||||
public static DbAccount getDbPlat(String dbPlatId) {
|
||||
Optional<DbAccount> dbAccount = dbPlats.stream().filter(x -> x.getKey().equals(dbPlatId)).findFirst();
|
||||
|
||||
return dbAccount.get();
|
||||
}
|
||||
|
||||
public String nextAlias(String x) {
|
||||
return next(x, "");
|
||||
}
|
||||
@@ -206,7 +206,7 @@ public class ArangoSource {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
Map<String, Integer> order = t.getOrder();
|
||||
if (isEmpty.test(order)) {
|
||||
return buf;
|
||||
return buf.append(" sort d._key desc");
|
||||
}
|
||||
buf.append(" sort ");
|
||||
order.forEach((k, v) -> {
|
||||
|
||||
@@ -25,6 +25,7 @@ public abstract class Doc<T extends Doc> {
|
||||
|
||||
private Set<String> _shows;
|
||||
private Map _order;
|
||||
private List<Map> _filters;//[{col, value, expr}]
|
||||
|
||||
public String getId() {
|
||||
return _id;
|
||||
@@ -194,6 +195,9 @@ public abstract class Doc<T extends Doc> {
|
||||
if (flipper == null) {
|
||||
flipper = new Flipper();
|
||||
}
|
||||
if (t == null) {
|
||||
t = (T) this;
|
||||
}
|
||||
|
||||
List<T> list = find(t, flipper.getOffset(), flipper.getLimit());
|
||||
long count = count(t);
|
||||
@@ -251,7 +255,7 @@ public abstract class Doc<T extends Doc> {
|
||||
return (T) collection.getDocument(String.valueOf(key), this.getClass());
|
||||
}
|
||||
|
||||
//ok
|
||||
// ok todo: 将数据放入回收库
|
||||
public void delete() {
|
||||
collection.deleteDocument(getKey());
|
||||
}
|
||||
|
||||
@@ -1,23 +1,24 @@
|
||||
package net.tccn.dbq;
|
||||
|
||||
import net.tccn.base.MetaKit;
|
||||
import net.tccn.base.PageBean;
|
||||
import net.tccn.dbq.fbean.FBean;
|
||||
import net.tccn.dbq.jdbc.api.DbAccount;
|
||||
import net.tccn.dbq.jdbc.api.DbKit;
|
||||
import net.tccn.dbq.parser.ParseMysql;
|
||||
import net.tccn.dbq.parser.Parser;
|
||||
import net.tccn.meta.MetaKit;
|
||||
import net.tccn.meta.MetaService;
|
||||
import net.tccn.meta.MetaTable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class DbExecutors {
|
||||
private final static Parser PARSER = new ParseMysql();
|
||||
|
||||
public static PageBean findPage(FBean fBean) {
|
||||
|
||||
public static PageBean findPage(FBean fBean) throws ExecutionException, InterruptedException {
|
||||
//sql解析
|
||||
String[] sqls = PARSER.parse(fBean);
|
||||
|
||||
//当前的业务 => 获取主表 信息 => 数据源信息 => 数据源对象 => 创建数据工具对象 => 查询数据
|
||||
@@ -25,15 +26,13 @@ public class DbExecutors {
|
||||
|
||||
MetaTable mainTable = MetaKit.getMetaTableByAlias(metaService.getTable());
|
||||
|
||||
DbAccount dbAccount = DbAccount.dao.findByKey(mainTable.getDbPlatId());
|
||||
|
||||
DbKit dbKit = new DbKit(dbAccount);
|
||||
DbKit dbKit = MetaKit.getDbKit(mainTable.getDbPlatId());
|
||||
System.out.println("countSql: " + sqls[0]);
|
||||
System.out.println("findSql: " + sqls[1]);
|
||||
|
||||
Integer count = dbKit.queryColumn(sqls[0], int.class);
|
||||
List<Map> list = dbKit.findList(sqls[1], Map.class);
|
||||
CompletableFuture<Integer> countFuture = CompletableFuture.supplyAsync(() -> dbKit.queryColumn(sqls[0], int.class));
|
||||
CompletableFuture<List<Map>> listFuture = CompletableFuture.supplyAsync(() -> dbKit.findList(sqls[1], Map.class));
|
||||
|
||||
return PageBean.by(list, count);
|
||||
return PageBean.by(listFuture.get(), countFuture.get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,4 +34,14 @@ public class DbKit implements DbSource{
|
||||
public <T> T queryColumn(String sql, Class<T> type) {
|
||||
return dbSource.queryColumn(sql, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTable(String sql) {
|
||||
dbSource.createTable(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropTable(String tableName) {
|
||||
dbSource.dropTable(tableName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,4 +32,7 @@ public interface DbSource {
|
||||
default Date queryDate(String sql) {
|
||||
return queryColumn(sql, Date.class);
|
||||
}
|
||||
|
||||
void createTable(String sql);
|
||||
void dropTable(String tableName);
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReferenceArray;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2019/3/12 14:20.
|
||||
@@ -17,7 +17,7 @@ import java.util.concurrent.atomic.AtomicReferenceArray;
|
||||
@SuppressWarnings("Duplicates")
|
||||
public class DbSourceMysql implements DbSource {
|
||||
|
||||
private static ConcurrentHashMap<String, AtomicReferenceArray<Connection>> conns = new ConcurrentHashMap<>();
|
||||
private static ConcurrentHashMap<String, LinkedBlockingQueue<Connection>> conns = new ConcurrentHashMap<>();
|
||||
private static ConcurrentHashMap<String, AtomicInteger> counter = new ConcurrentHashMap<>();
|
||||
|
||||
private String accountKey;
|
||||
@@ -111,57 +111,66 @@ public class DbSourceMysql implements DbSource {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTable(String sql) {
|
||||
new RuntimeException("DbSourceMysql.createTable NOT SUPPORT right now" ); // todo:
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropTable(String tableName) {
|
||||
new RuntimeException("[DbSourceMysql.dropTable] NOT SUPPORT right now" ); // todo:
|
||||
}
|
||||
|
||||
private Connection connection() {
|
||||
return connection(0);
|
||||
}
|
||||
private Connection connection(int n) {
|
||||
AtomicReferenceArray<Connection> arr = conns.getOrDefault(accountKey, new AtomicReferenceArray<>(15));
|
||||
Connection connection = null;
|
||||
AtomicInteger num = counter.getOrDefault(accountKey, new AtomicInteger(0));
|
||||
for (int i = 0; num.get() > 0 && i < arr.length() && connection == null; i++) {
|
||||
try {
|
||||
connection = arr.getAndUpdate(i, null);
|
||||
} catch (Exception e) {
|
||||
System.out.println("getAndUpdate exception");
|
||||
}
|
||||
}
|
||||
if (connection == null) {
|
||||
try {
|
||||
if (num.get() < 15) {
|
||||
connection = DriverManager.getConnection(dbAccount.getUrl(), dbAccount.getUser(), dbAccount.getPwd());
|
||||
num.getAndIncrement();
|
||||
} else {
|
||||
//连接被全部使用中,等待1s后再次获取连接,直到得到连接 !!!
|
||||
Thread.sleep(1000);
|
||||
if (++n > 3)
|
||||
connection(n);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
throw new IllegalArgumentException("获取数据库连接失败");
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
LinkedBlockingQueue<Connection> queue = conns.getOrDefault(accountKey, new LinkedBlockingQueue<>(15));
|
||||
|
||||
return connection;
|
||||
Connection conn = null;
|
||||
AtomicInteger num = counter.getOrDefault(accountKey, new AtomicInteger(0));
|
||||
try {
|
||||
if (queue.size() == 0 && num.get() < 15) {
|
||||
conn = DriverManager.getConnection(dbAccount.getUrl(), dbAccount.getUser(), dbAccount.getPwd());
|
||||
int x = num.incrementAndGet();
|
||||
counter.put(accountKey, num);
|
||||
System.out.println("创建新的连接:" + x);
|
||||
} else {
|
||||
conn = queue.take();
|
||||
if (conn != null) {
|
||||
System.out.println("获取已有连接" + conn);
|
||||
}
|
||||
}
|
||||
} catch (SQLException | InterruptedException e) {
|
||||
if (e instanceof InterruptedException) {
|
||||
try {
|
||||
conn = DriverManager.getConnection(dbAccount.getUrl(), dbAccount.getUser(), dbAccount.getPwd());
|
||||
num.getAndIncrement();
|
||||
if (conn != null) {
|
||||
System.out.println("获取连接异常,并重新创建成功");
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
new IllegalArgumentException("创建连接失败", e);
|
||||
}
|
||||
num.getAndIncrement();
|
||||
counter.put(accountKey, num);
|
||||
} else {
|
||||
new IllegalArgumentException("获取连接失败", e);
|
||||
}
|
||||
}
|
||||
conns.put(accountKey, queue);
|
||||
return conn;
|
||||
}
|
||||
private void release(Connection connection) {
|
||||
AtomicReferenceArray<Connection> arr = conns.getOrDefault(accountKey, new AtomicReferenceArray<>(15));
|
||||
|
||||
int i = 0;
|
||||
boolean bool = false;
|
||||
while (i < arr.length() && !bool){
|
||||
bool = arr.compareAndSet(i++, null, connection);
|
||||
}
|
||||
|
||||
//如果没成功释放,关系连接
|
||||
if (!bool && connection != null) {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
LinkedBlockingQueue<Connection> queue = conns.getOrDefault(accountKey, new LinkedBlockingQueue<>(15));
|
||||
try {
|
||||
if (connection != null) {
|
||||
queue.put(connection);
|
||||
conns.put(accountKey, queue);
|
||||
System.out.println("还回连接:" + connection);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package net.tccn.dbq.parser;
|
||||
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.dbq.fbean.*;
|
||||
import net.tccn.meta.MetaKit;
|
||||
import net.tccn.base.MetaKit;
|
||||
import net.tccn.meta.MetaLink;
|
||||
import net.tccn.meta.MetaService;
|
||||
import net.tccn.meta.MetaTable;
|
||||
|
||||
@@ -5,7 +5,7 @@ package net.tccn.dbq.table;
|
||||
* @author: liangxianyou at 2018/10/8 10:59.
|
||||
*/
|
||||
public class Column {
|
||||
private String name; //列名称
|
||||
private String field; //列名称
|
||||
private String type; //列类型
|
||||
private boolean notNull; //不为null
|
||||
private String comment; //列说明
|
||||
@@ -14,18 +14,18 @@ public class Column {
|
||||
}
|
||||
|
||||
public Column(String name, String type, boolean notNull, String comment) {
|
||||
this.name = name;
|
||||
this.field = name;
|
||||
this.type = type;
|
||||
this.notNull = notNull;
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
public String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
public void setField(String field) {
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
@@ -52,4 +52,9 @@ public class Column {
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
//-----------------------
|
||||
public void setNull(String notNull) {
|
||||
this.notNull = "NO".equalsIgnoreCase(notNull) ? true : false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public class Table {
|
||||
|
||||
buf.append("CREATE TABLE " + name + "(");
|
||||
columns.forEach(x -> {
|
||||
buf.append("\n " + x.getName() + " " + x.getType() + ",");
|
||||
buf.append("\n " + x.getField() + " " + x.getType() + ",");
|
||||
});
|
||||
|
||||
buf.deleteCharAt(buf.length() - 1);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.tccn.meta;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.qtask.Task;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2019/3/7 16:13.
|
||||
|
||||
@@ -20,6 +20,7 @@ public class MetaTable extends Doc<MetaTable> implements Serializable {
|
||||
private String alias; //表别名:全库唯一,程序自动生成
|
||||
private String comment;
|
||||
private List<Field> items;
|
||||
private String sysPlatId; //所属系统平台
|
||||
private String dbPlatId; //所属数据平台
|
||||
private String catalog; //所在database
|
||||
|
||||
@@ -57,6 +58,14 @@ public class MetaTable extends Doc<MetaTable> implements Serializable {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
public String getSysPlatId() {
|
||||
return sysPlatId;
|
||||
}
|
||||
|
||||
public void setSysPlatId(String sysPlatId) {
|
||||
this.sysPlatId = sysPlatId;
|
||||
}
|
||||
|
||||
public String getDbPlatId() {
|
||||
return dbPlatId;
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package net.tccn.meta;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2019/3/7 16:24.
|
||||
*/
|
||||
public class Task {
|
||||
}
|
||||
91
src/main/java/net/tccn/meta/_DbService.java
Normal file
91
src/main/java/net/tccn/meta/_DbService.java
Normal file
@@ -0,0 +1,91 @@
|
||||
package net.tccn.meta;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.MetaKit;
|
||||
import net.tccn.dbq.jdbc.api.DbAccount;
|
||||
import net.tccn.dbq.jdbc.api.DbKit;
|
||||
import net.tccn.dbq.table.Column;
|
||||
import net.tccn.dbq.table.Table;
|
||||
import net.tccn.service.BaseService;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.net.http.RestService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RestService(automapping = true, comment = "数据库操作类")
|
||||
public class _DbService extends BaseService {
|
||||
|
||||
@RestMapping(name = "catalog_list", comment = "获取数据源的database")
|
||||
public JBean catalogList(DbAccount dbAccount, String dbPlatId) {
|
||||
JBean jBean = new JBean();
|
||||
DbKit dbKit = null;
|
||||
if (dbAccount != null) {
|
||||
dbKit = new DbKit(dbAccount);
|
||||
} else {
|
||||
dbKit = MetaKit.getDbKit(dbPlatId);
|
||||
}
|
||||
|
||||
List<Map> list = dbKit.findList("SHOW DATABASES;", Map.class);
|
||||
|
||||
Stream<String> database = list.stream().map(x -> String.valueOf(x.get("Database")));
|
||||
|
||||
return jBean.setBody(database.toArray());
|
||||
}
|
||||
|
||||
@RestMapping(name = "table_list", comment = "数据库表列表")
|
||||
public JBean tableList(DbAccount dbAccount, String dbPlatId, String[] catalogs) {
|
||||
JBean jBean = new JBean();
|
||||
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 (");
|
||||
for (String catalog : catalogs) {
|
||||
sqlBuf.append("'").append(catalog).append("',");
|
||||
}
|
||||
sqlBuf.deleteCharAt(sqlBuf.length() - 1);
|
||||
sqlBuf.append(")");
|
||||
}
|
||||
List<Table> list = dbKit.findList(sqlBuf.toString(), Table.class);
|
||||
|
||||
return jBean.setBody(list);
|
||||
}
|
||||
|
||||
@RestMapping(name = "table_info", comment = "数据库表详情")
|
||||
public JBean tableInfo(DbAccount dbAccount,
|
||||
String dbPlatId, String catalog, String tableName) {
|
||||
JBean jBean = new JBean();
|
||||
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);
|
||||
|
||||
CompletableFuture<Table> tableFuture = CompletableFuture.supplyAsync(() -> dbKit.findfirst(sql, Table.class));
|
||||
CompletableFuture<List<Column>> columnFuture = CompletableFuture.supplyAsync(() -> dbKit.findList(columnSql, Column.class));
|
||||
|
||||
try {
|
||||
Table table = tableFuture.get();
|
||||
table.setColumns(columnFuture.get());
|
||||
jBean.setBody(table);
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
jBean.set(-1, "查询表信息失败");
|
||||
new IllegalArgumentException("查询表信息失败", e);
|
||||
}
|
||||
|
||||
return jBean;
|
||||
}
|
||||
|
||||
@RestMapping(name = "table_create", comment = "新建表[mysql]")
|
||||
public JBean tableCreate(String dbPlatId, String catalog, String sql) {
|
||||
JBean jBean = new JBean();
|
||||
|
||||
DbKit dbKit = MetaKit.getDbKit(dbPlatId);
|
||||
dbKit.createTable(sql);
|
||||
|
||||
return jBean;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,17 @@
|
||||
package net.tccn.plat;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.MetaKit;
|
||||
import net.tccn.base.PageBean;
|
||||
import net.tccn.dbq.jdbc.api.DbAccount;
|
||||
import net.tccn.service.BaseService;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.net.http.RestService;
|
||||
import org.redkale.source.Flipper;
|
||||
import org.redkale.util.Comment;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestService(name = "plat", automapping = true, comment = "业务/数据平台")
|
||||
public class PlatService extends BaseService {
|
||||
|
||||
@@ -15,7 +19,9 @@ public class PlatService extends BaseService {
|
||||
public JBean list(SysPlat plat, Flipper flipper) {
|
||||
JBean jBean = new JBean();
|
||||
|
||||
PageBean<SysPlat> page = SysPlat.dao.findPage(plat, flipper);
|
||||
//PageBean<SysPlat> page = SysPlat.dao.findPage(plat, flipper);
|
||||
List<SysPlat> list = MetaKit.getSysPlats();
|
||||
PageBean page = PageBean.by(list, list.size());
|
||||
|
||||
return jBean.setBody(page);
|
||||
}
|
||||
@@ -42,19 +48,24 @@ public class PlatService extends BaseService {
|
||||
public JBean dbList(DbPlat plat, Flipper flipper) {
|
||||
JBean jBean = new JBean();
|
||||
|
||||
PageBean<DbPlat> page = DbPlat.dao.findPage(plat, flipper);
|
||||
//PageBean<DbPlat> page = DbPlat.dao.findPage(plat, flipper);
|
||||
|
||||
List<DbAccount> list = MetaKit.getDbPlats();
|
||||
PageBean page = PageBean.by(list, list.size());
|
||||
|
||||
return jBean.setBody(page);
|
||||
}
|
||||
|
||||
@RestMapping(name = "db_save", comment = "数据源信息保存")
|
||||
public JBean dbSave(DbPlat plat) {
|
||||
//DbAccount dbPlat = MetaKit.getDbPlat(plat.getKey());
|
||||
|
||||
if (plat.getKey() == null) {
|
||||
plat.save();
|
||||
} else {
|
||||
plat.update();
|
||||
}
|
||||
MetaKit.reload(DbPlat.class);
|
||||
|
||||
return new JBean();
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ package net.tccn.qtask;
|
||||
|
||||
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.base.arango.Doc;
|
||||
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
* cate:mysql
|
||||
@@ -22,7 +25,9 @@ import net.tccn.base.Kv;
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class Task {
|
||||
@Table(name = "qtask", catalog = "db_dev")
|
||||
public class Task extends Doc<Task> {
|
||||
public static Task dao = dao(Task.class);
|
||||
|
||||
public String cate;//MYSQL,ES,METHOD,HTTP
|
||||
public String queryId;
|
||||
|
||||
@@ -23,6 +23,9 @@ import java.util.logging.Logger;
|
||||
*/
|
||||
public class BaseService implements Service {
|
||||
|
||||
@Resource(name = "SERVER_ROOT")
|
||||
protected File webroot;
|
||||
|
||||
public static Gson gson = new Gson();
|
||||
public Logger logger = Logger.getLogger(this.getClass().getSimpleName());
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import net.tccn.base.Kv;
|
||||
import net.tccn.dbq.Field;
|
||||
import net.tccn.dbq.jdbc.api.DbAccount;
|
||||
import net.tccn.plat.SysPlat;
|
||||
import net.tccn.meta.MetaKit;
|
||||
import net.tccn.base.MetaKit;
|
||||
import net.tccn.meta.MetaService;
|
||||
import net.tccn.meta.MetaTable;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
|
||||
243
src/main/java/net/tccn/service/_FileService.java
Normal file
243
src/main/java/net/tccn/service/_FileService.java
Normal file
@@ -0,0 +1,243 @@
|
||||
package net.tccn.service;
|
||||
|
||||
import com.lxyer.excel.poi.ExcelKit;
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.dbq.Field;
|
||||
import net.tccn.meta.MetaTable;
|
||||
import net.tccn.plat.SysPlat;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
import org.redkale.net.http.RestParam;
|
||||
import org.redkale.net.http.RestService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/10/24 10:57.
|
||||
*/
|
||||
@RestService(automapping = true, comment = "文件服务")
|
||||
public class _FileService extends BaseService {
|
||||
|
||||
private final static String[] FIELDS = {"field", "cate", "must", "remark1", "remark2", "tag", "selects", "column", "filter", "ck", "edit"};
|
||||
|
||||
@Resource
|
||||
private QtaskService qtaskService;
|
||||
|
||||
|
||||
@RestMapping(name = "sheets", comment = "得到所有的sheetName")
|
||||
public List<String> sheets(String filePath) {
|
||||
List<String> sheets = new ArrayList<>();
|
||||
try {
|
||||
|
||||
File file = new File(webroot, filePath);
|
||||
if (file.exists()) {
|
||||
sheets = ExcelKit.getSheetNames(file);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return sheets.stream().filter(x -> {
|
||||
return !x.contains("版本记录") && !x.contains("表说明") && !x.contains("表名称");
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@RestMapping(name = "data", comment = "得到文件数据")
|
||||
public JBean data(String filePath, @RestParam(name = "platToken") String token) {
|
||||
JBean jBean = new JBean();
|
||||
SysPlat sysPlat = qtaskService.getSysPlat(token);
|
||||
File file = new File(webroot, filePath);
|
||||
if (file.exists()) {
|
||||
try {
|
||||
Map<String, List<Map>> map = ExcelKit.readExcelAll(file, FIELDS);
|
||||
|
||||
Kv<String, MetaTable> data = Kv.of();
|
||||
map.forEach((k, v) -> {
|
||||
if (v.size() > 2) {
|
||||
data.put(k.replace(" ", ""), toCols(v));
|
||||
}
|
||||
});
|
||||
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("for d in meta_cols\n" +
|
||||
" filter d.name in [");
|
||||
data.values().forEach(x -> {
|
||||
buf.append("'").append(x.getName()).append("',");
|
||||
});
|
||||
buf.deleteCharAt(buf.length() - 1);
|
||||
buf.append("] and d.sysPlatId=='" + sysPlat.getKey() + "'\n" +
|
||||
" return d.name");
|
||||
|
||||
List<String> hv = MetaTable.dao.find(buf.toString(), String.class);
|
||||
|
||||
Kv res = Kv.of();
|
||||
data.forEach((k, v) -> {
|
||||
Kv kv = Kv.of();
|
||||
res.put(k,
|
||||
kv.set("name", v.getName())
|
||||
.set("hv", hv.contains(v.getName()) ? 1 : 0)
|
||||
.set("comment", v.getComment())
|
||||
);
|
||||
});
|
||||
|
||||
jBean.setBody(res);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return jBean;
|
||||
}
|
||||
|
||||
@RestMapping(name = "sheet_data", comment = "得到sheet数据")
|
||||
public JBean sheetData(String filePath, String sheetName, @RestParam(name = "platToken") String token) {
|
||||
JBean jBean = new JBean();
|
||||
SysPlat sysPlat = qtaskService.getSysPlat(token);
|
||||
File file = new File(webroot, filePath);
|
||||
try {
|
||||
List<Map> list = ExcelKit.readExcel(file, FIELDS, sheetName);
|
||||
MetaTable metaTable = toCols(list);
|
||||
|
||||
jBean.setBody(metaTable);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
jBean.set(-1, String.format("读取sheet[%s]失败", sheetName));
|
||||
}
|
||||
|
||||
return jBean;
|
||||
}
|
||||
|
||||
@RestMapping(name = "import_metatable", comment = "导入excel数据到metatable")
|
||||
public JBean importMetaTable(@RestParam(name = "sheetArr", comment = "sheet名") String[] sheetArr,
|
||||
@RestParam(name = "filePath", comment = "文件路径") String filePath,
|
||||
@RestParam(name = "platToken") String token) {
|
||||
JBean jBean = new JBean();
|
||||
SysPlat sysPlat = qtaskService.getSysPlat(token);
|
||||
|
||||
File file = new File(webroot, filePath);
|
||||
String[] fields = {"field", "cate", "must", "remark1", "remark2", "tag", "selects", "column", "filter", "ck", "edit"};
|
||||
try {
|
||||
Map<String, List<Map>> map = ExcelKit.readExcelAll(file, fields);
|
||||
map.forEach((k, v) -> {
|
||||
if (v.size() > 2) {
|
||||
MetaTable table = toCols(v);
|
||||
for (String sn : sheetArr) {
|
||||
if (table.getName().equals(sn)) { //在选中列表中
|
||||
MetaTable metaTable = new MetaTable();
|
||||
metaTable.setSysPlatId(sysPlat.getKey());
|
||||
metaTable.setName(table.getName());
|
||||
|
||||
MetaTable _metaTable = MetaTable.dao.findFirst(metaTable);
|
||||
if (_metaTable == null) {//库里没有数据保存
|
||||
table.setSysPlatId(sysPlat.getKey());
|
||||
table.save();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return jBean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 组装元数据
|
||||
*/
|
||||
private MetaTable toCols(List<Map> list) {
|
||||
|
||||
//Kv col = Kv.of();
|
||||
MetaTable metaTable = new MetaTable();
|
||||
|
||||
list.remove(1);//list[0] head info
|
||||
Map rowHead = list.remove(0);
|
||||
|
||||
String comment = getComment(rowHead);//list[1] comment,
|
||||
String tableName = getTableName(rowHead);
|
||||
//col.set("name", tableName).set("comment", comment);
|
||||
metaTable.setName(tableName);
|
||||
metaTable.setComment(comment);
|
||||
|
||||
//所有字段
|
||||
List<Field> items = new ArrayList<>();
|
||||
|
||||
//展示的字段
|
||||
List<String> shows = new ArrayList<>();
|
||||
|
||||
//编辑的字段
|
||||
List<Map> edits = new ArrayList<>();
|
||||
|
||||
//查询过滤用字段
|
||||
List<Map> filters = new ArrayList<>();
|
||||
|
||||
|
||||
list.forEach(x -> {
|
||||
String colName = x.getOrDefault("field", "") + "";
|
||||
|
||||
Field item = new Field();
|
||||
item.setName(colName);
|
||||
item.setLabel(x.getOrDefault("remark1", "") + "");
|
||||
item.setType(x.getOrDefault("cate", "") + "");
|
||||
item.setInType(x.getOrDefault("tag", "") + "");
|
||||
item.setInExt(x.getOrDefault("selects", "") + "");
|
||||
|
||||
items.add(item);
|
||||
|
||||
if ("1".equals(x.getOrDefault("column", "") + "")) {
|
||||
shows.add(colName);
|
||||
}
|
||||
|
||||
if ("1".equals(x.getOrDefault("edit", "") + "")) {
|
||||
edits.add(Kv.of("col", colName));
|
||||
}
|
||||
|
||||
if (x.get("filter") != null && !"".equals(x.get("filter") + "")) {
|
||||
String filter = x.getOrDefault("filter", "") + "";
|
||||
filter = filter.replace("1", "EQUAL");
|
||||
filter = filter.replace("!1", "NOTEQUAL");
|
||||
filter = filter.replace("like", "LIKE");
|
||||
|
||||
filters.add(Kv.of("name", item).set("filterType", asList(filter.split(","))));
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
metaTable.setItems(items);
|
||||
//metaTable.setShows(shows);
|
||||
//metaTable.setEdits(edits);
|
||||
//metaTable.setFilters(filters);
|
||||
return metaTable;
|
||||
}
|
||||
|
||||
private String getTableName(Map rowHead) {
|
||||
String field = rowHead.get("field") + "";
|
||||
|
||||
int s = field.indexOf("(");
|
||||
if (s > 0) {
|
||||
return field.substring(0, s);
|
||||
}
|
||||
return field;
|
||||
}
|
||||
|
||||
private String getComment(Map rowHead) {
|
||||
String field = rowHead.get("field") + "";
|
||||
|
||||
int s = field.indexOf("(");
|
||||
int e = field.indexOf(")");
|
||||
if (s > 0) {
|
||||
return field.substring(s + 1, e > 0 ? e : field.length());
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
22
src/main/java/net/tccn/service/_QtaskService.java
Normal file
22
src/main/java/net/tccn/service/_QtaskService.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package net.tccn.service;
|
||||
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.PageBean;
|
||||
import net.tccn.qtask.Task;
|
||||
import org.redkale.net.http.RestParam;
|
||||
import org.redkale.net.http.RestService;
|
||||
import org.redkale.source.Flipper;
|
||||
import org.redkale.util.Comment;
|
||||
|
||||
@RestService(automapping = true)
|
||||
public class _QtaskService extends BaseService{
|
||||
|
||||
@Comment("qtask列表")
|
||||
public JBean list(Task task, Flipper flipper, @RestParam(name = "platToken") String token) {
|
||||
PageBean<Task> page = Task.dao.findPage(task, flipper);
|
||||
|
||||
|
||||
return JBean.by(0, "", page);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,7 @@ 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.meta.MetaKit;
|
||||
import net.tccn.base.MetaKit;
|
||||
import net.tccn.qtask.QRuner;
|
||||
import net.tccn.qtask.Task;
|
||||
import org.junit.Test;
|
||||
@@ -60,8 +60,11 @@ public class RunTest<T> {
|
||||
|
||||
DbKit dbKit = new DbKit(dbAccount);
|
||||
|
||||
//String sql = "select * from basic_concat limit 1";
|
||||
String sql = "show databases;";
|
||||
|
||||
// find list
|
||||
List<Map> list = dbKit.findList("select * from basic_concat limit 1", Map.class);
|
||||
List<Map> list = dbKit.findList(sql, Map.class);
|
||||
System.out.println(list.get(0));
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user