1、新增自定义异常捕获处理类
2、新增保存“业务数据”逻辑:
This commit is contained in:
22
src/main/java/net/tccn/base/CfgException.java
Normal file
22
src/main/java/net/tccn/base/CfgException.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package net.tccn.base;
|
||||
|
||||
import org.redkale.net.http.HttpRequest;
|
||||
|
||||
/**
|
||||
* 自定义异常处理
|
||||
* @author: liangxianyou
|
||||
*/
|
||||
public class CfgException extends IllegalArgumentException {
|
||||
|
||||
public CfgException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public CfgException(String msgTpl, Object ... objects) {
|
||||
super(String.format(msgTpl, objects));
|
||||
}
|
||||
|
||||
public void log(HttpRequest request) {
|
||||
System.out.printf("CfgException: {uri: %s,%n Parameters:%s,%n body: %s} %n", request.getRequestURI(), request.getParameters().toString(), request.getBody());
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,7 @@ public class Kv<K,V> extends LinkedHashMap<K,V> {
|
||||
filedS= arr[1];
|
||||
}
|
||||
|
||||
Method method = m.getClass().getDeclaredMethod("get" + Liangs.toUpperCaseFirst(filedS));
|
||||
Method method = m.getClass().getDeclaredMethod("get" + X.toUpperCaseFirst(filedS));
|
||||
if (method != null) {
|
||||
kv.set(filedT, method.invoke(m));
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package net.tccn.base;
|
||||
|
||||
import lombok.Getter;
|
||||
import net.tccn.base.arango.Doc;
|
||||
import net.tccn.dbq.Field;
|
||||
import net.tccn.dbq.table.Field;
|
||||
import net.tccn.dbq.fbean.FilterType;
|
||||
import net.tccn.dbq.jdbc.api.DbAccount;
|
||||
import net.tccn.dbq.jdbc.api.DbKit;
|
||||
@@ -140,6 +140,17 @@ public final class MetaKit {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到业务主表
|
||||
* @param serviceName
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
public static MetaTable getMainTable(String serviceName, String token) {
|
||||
MetaService metaService = MetaKit.getMetaService(serviceName, token);
|
||||
return MetaKit.getMetaTableByAlias(metaService.getTable());
|
||||
}
|
||||
|
||||
// -----------------------------------
|
||||
public void cleanData() {
|
||||
|
||||
@@ -434,7 +445,7 @@ public final class MetaKit {
|
||||
Set<String> allAlias;
|
||||
|
||||
if (!all) {
|
||||
allAlias = Liangs.concat(
|
||||
allAlias = X.concat(
|
||||
metaService.getFilters().stream().map(f -> {
|
||||
String col = (String) f.getName();
|
||||
String alias = col.split("[.]")[0];
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.util.stream.Stream;
|
||||
/**
|
||||
* Created by liangxianyou at 2019/3/19 18:01.
|
||||
*/
|
||||
public class Liangs {
|
||||
public class X {
|
||||
|
||||
/**
|
||||
* 将集合数组合并到一个Set<T> 集合中
|
||||
@@ -85,4 +85,18 @@ public class Liangs {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isEmpty(Object obj) {
|
||||
if (obj == null)
|
||||
return true;
|
||||
if (obj instanceof List)
|
||||
return ((List) obj).isEmpty();
|
||||
if (obj instanceof String)
|
||||
return ((String) obj).isEmpty();
|
||||
if (obj instanceof Map)
|
||||
return ((Map) obj).isEmpty();
|
||||
if (obj instanceof Collection)
|
||||
return ((Collection) obj).isEmpty();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,18 +13,19 @@ import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* Db 调度层
|
||||
*/
|
||||
public class DbExecutors {
|
||||
private final static ParseMysql PARSER = new ParseMysql();
|
||||
|
||||
public static PageBean findPage(FBean fBean) throws ExecutionException, InterruptedException {
|
||||
//sql解析
|
||||
String[] sqls = PARSER.parse(fBean);
|
||||
String[] sqls = PARSER.parseList(fBean);
|
||||
|
||||
//当前的业务 => 获取主表 信息 => 数据源信息 => 数据源对象 => 创建数据工具对象 => 查询数据
|
||||
MetaService metaService = MetaKit.getMetaService(fBean.getName(), fBean.getPlatToken());
|
||||
|
||||
MetaTable mainTable = MetaKit.getMetaTableByAlias(metaService.getTable());
|
||||
|
||||
DbKit dbKit = MetaKit.getDbKit(mainTable.getDbPlatId(), mainTable.getCatalog());
|
||||
//System.out.printf("----------------%n countSql:%s%n findSql:%s%n----------------%n", sqls[0], sqls[1]);
|
||||
|
||||
@@ -39,12 +40,18 @@ public class DbExecutors {
|
||||
|
||||
public static void del(String name, Map data, String token) {
|
||||
MetaService metaService = MetaKit.getMetaService(name, token);
|
||||
|
||||
MetaTable mainTable = MetaKit.getMetaTableByAlias(metaService.getTable());
|
||||
|
||||
DbKit dbKit = MetaKit.getDbKit(mainTable.getDbPlatId(), mainTable.getCatalog());
|
||||
|
||||
String delSql = PARSER.parseDel(name, data, token);
|
||||
dbKit.exetute(delSql);
|
||||
}
|
||||
|
||||
public static void save(String name, Map data, String token) {
|
||||
MetaTable mainTable = MetaKit.getMainTable(name, token);
|
||||
DbKit dbKit = MetaKit.getDbKit(mainTable.getDbPlatId(), mainTable.getCatalog());
|
||||
|
||||
String sql = PARSER.parseSave(name, data, token);
|
||||
dbKit.exetute(sql);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package net.tccn.dbq.jdbc.api;
|
||||
|
||||
import net.tccn.base.Liangs;
|
||||
import net.tccn.base.X;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* Db 最终执行层
|
||||
* Created by liangxianyou at 2019/3/12 14:11.
|
||||
*/
|
||||
public class DbKit implements DbSource{
|
||||
@@ -17,7 +18,7 @@ public class DbKit implements DbSource{
|
||||
/*public DbKit(DbAccount dbAccount) {
|
||||
this.dbAccount = dbAccount;
|
||||
try {
|
||||
DbSource dbSource = Liangs.getDbSource(DbSource.class, dbAccount.getCate());
|
||||
DbSource dbSource = X.getDbSource(DbSource.class, dbAccount.getCate());
|
||||
dbSource.setDbAccount(dbAccount);
|
||||
|
||||
this.dbSource = dbSource;
|
||||
@@ -30,7 +31,7 @@ public class DbKit implements DbSource{
|
||||
this.catalog = catalog;
|
||||
|
||||
try {
|
||||
DbSource dbSource = Liangs.getDbSource(DbSource.class, dbAccount.getCate());
|
||||
DbSource dbSource = X.getDbSource(DbSource.class, dbAccount.getCate());
|
||||
dbSource.setDbAccount(dbAccount);
|
||||
dbSource.setCatalog(catalog);
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import net.tccn.dbq.fbean.FBean;
|
||||
public class ParseArango implements Parser {
|
||||
|
||||
@Override
|
||||
public String[] parse(FBean fBean) {
|
||||
public String[] parseList(FBean fBean) {
|
||||
return new String[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ public class ParseEs implements Parser {
|
||||
|
||||
|
||||
@Override
|
||||
public String[] parse(FBean fBean) {
|
||||
public String[] parseList(FBean fBean) {
|
||||
return new String[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package net.tccn.dbq.parser;
|
||||
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.base.MetaKit;
|
||||
import net.tccn.base.TplKit;
|
||||
import net.tccn.base.*;
|
||||
import net.tccn.dbq.fbean.*;
|
||||
import net.tccn.meta.MetaLink;
|
||||
import net.tccn.meta.MetaService;
|
||||
@@ -48,8 +46,13 @@ public class ParseMysql implements Parser {
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* 查询数据解析
|
||||
* @param fBean
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String[] parse(FBean fBean) {
|
||||
public String[] parseList(FBean fBean) {
|
||||
MetaService metaService = MetaKit.getMetaService(fBean.getName(), fBean.getPlatToken());
|
||||
|
||||
Kv<String, MetaTable> tables = MetaKit.getMetaTables(metaService, false);//所有的关联表信息
|
||||
@@ -134,6 +137,13 @@ public class ParseMysql implements Parser {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除执行语句解析
|
||||
* @param name
|
||||
* @param data
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
public String parseDel(String name, Map data, String token) {
|
||||
MetaService metaService = MetaKit.getMetaService(name, token);
|
||||
Map<String, String> dels = metaService.getDels();
|
||||
@@ -151,4 +161,67 @@ public class ParseMysql implements Parser {
|
||||
|
||||
return sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存数据解析逻辑:
|
||||
* 1.得到主表信息
|
||||
* 2.根据主表信息 使用数据创建sql
|
||||
* @param serviceName 业务名称
|
||||
* @param data 待保存的数据
|
||||
* @param token 平台token
|
||||
* @return sql 待执行的sql语句
|
||||
*/
|
||||
public String parseSave(String serviceName, Map<String, String> data, String token) {
|
||||
MetaTable mainTable = MetaKit.getMainTable(serviceName, token);
|
||||
String alias = mainTable.getAlias();
|
||||
String[] pks = mainTable.pk();
|
||||
|
||||
// 取出有效的数据key
|
||||
List<String> keys = data.keySet()
|
||||
.stream()
|
||||
.filter(x ->
|
||||
x.startsWith(alias + ".") || !X.isEmpty(data.get(alias + "." + x))
|
||||
).collect(Collectors.toList());
|
||||
if (pks.length == 0) {
|
||||
throw new CfgException("保存数据失败,检查业务主表[%s-%S]主键配置", mainTable.getName(), mainTable.getComment());
|
||||
} else if (keys.size() == 0) {
|
||||
throw new CfgException("保存数据失败,提交数据不能改空");
|
||||
}
|
||||
|
||||
|
||||
//单主键
|
||||
String pv = data.get(alias + "." + pks[0]);
|
||||
if (X.isEmpty(pv)) { //新增
|
||||
String sqlTpl = "INSERT INTO `%s` (%s) VALUES %s;"; // para: table、 ks、 vs
|
||||
StringBuffer ks = new StringBuffer();// `k1`,`k2`,`k3`, ...
|
||||
StringBuffer vs = new StringBuffer();// `v1`,`v2`,`v3`, ...
|
||||
|
||||
for (String k : keys) {
|
||||
ks.append(String.format("`%s`,", k));
|
||||
vs.append(String.format("'%s',", data.get(alias + "." + k)));
|
||||
}
|
||||
if (ks.length() > 0) {
|
||||
ks.deleteCharAt(ks.length() - 1);
|
||||
vs.deleteCharAt(vs.length() - 1);
|
||||
}
|
||||
|
||||
return String.format(sqlTpl, mainTable.getName(), ks, vs);
|
||||
}
|
||||
|
||||
else { //修改
|
||||
String sqlTpl = "update `%s` set %s where `%s` = '%s';"; // para: table、 kvs、 pk、 pv
|
||||
StringBuilder kvs = new StringBuilder(); // `k1`='v1',`k2`='v2', ...
|
||||
String pk = pks[0];
|
||||
|
||||
for (String k : keys) {
|
||||
kvs.append(String.format("`%s`='%s',", k, data.get(alias + "." + k)));
|
||||
}
|
||||
|
||||
if (kvs.length() > 0) {
|
||||
kvs.deleteCharAt(kvs.length() - 1);
|
||||
}
|
||||
|
||||
return String.format(sqlTpl, mainTable.getName(), kvs, pk, pv);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package net.tccn.dbq.parser;
|
||||
import net.tccn.dbq.fbean.FBean;
|
||||
|
||||
/**
|
||||
* Db 执行解释层
|
||||
* Created by liangxianyou at 2018/12/24 15:47.
|
||||
*/
|
||||
public interface Parser {
|
||||
@@ -12,13 +13,8 @@ public interface Parser {
|
||||
* @param fBean
|
||||
* @return [countSql, listSql]
|
||||
*/
|
||||
String[] parse(FBean fBean);
|
||||
String[] parseList(FBean fBean);
|
||||
|
||||
/**
|
||||
* 解析一个查询条件
|
||||
* @param filter
|
||||
*/
|
||||
//String parse(Filter filter);
|
||||
|
||||
/**
|
||||
* 解析入库语句
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package net.tccn.dbq;
|
||||
package net.tccn.dbq.table;
|
||||
|
||||
import lombok.Data;
|
||||
import net.tccn.dbq.table.Column;
|
||||
|
||||
/**
|
||||
* @author: liangxianyou at 2018/10/17 17:24.
|
||||
@@ -2,11 +2,12 @@ package net.tccn.meta;
|
||||
|
||||
import lombok.Data;
|
||||
import net.tccn.base.arango.Doc;
|
||||
import net.tccn.dbq.Field;
|
||||
import net.tccn.dbq.table.Field;
|
||||
|
||||
import javax.persistence.Table;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -40,4 +41,17 @@ public class MetaTable extends Doc<MetaTable> implements Serializable {
|
||||
|
||||
return _bean;
|
||||
}
|
||||
|
||||
// 方法名getPK 报错,
|
||||
public String[] pk() {
|
||||
List<String> pks = items.stream().filter(x -> x.getPk() != null && x.getPk()).map(x -> x.getName()).collect(Collectors.toList());
|
||||
|
||||
if (pks.size() > 0) {
|
||||
return pks.toArray(new String[pks.size()]);
|
||||
}
|
||||
|
||||
//存在id字段,取id
|
||||
Optional<Field> any = items.stream().filter(x -> x.getName().equalsIgnoreCase("id")).findAny();
|
||||
return any.isPresent() ? new String[]{"id"} : new String[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package net.tccn.service;
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.base.MetaKit;
|
||||
import net.tccn.dbq.Field;
|
||||
import net.tccn.dbq.table.Field;
|
||||
import net.tccn.meta.*;
|
||||
import net.tccn.plat.SysPlat;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
|
||||
@@ -4,7 +4,7 @@ import net.tccn.base.ExcelKit;
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.base.MetaKit;
|
||||
import net.tccn.dbq.Field;
|
||||
import net.tccn.dbq.table.Field;
|
||||
import net.tccn.meta.MetaTable;
|
||||
import net.tccn.plat.SysPlat;
|
||||
import org.redkale.net.http.RestMapping;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.tccn.servlet;
|
||||
|
||||
import com.arangodb.ArangoDBException;
|
||||
import net.tccn.base.CfgException;
|
||||
import net.tccn.base.JBean;
|
||||
import net.tccn.base.Kv;
|
||||
import net.tccn.base.arango.ArangoSource;
|
||||
@@ -73,6 +74,12 @@ public class BaseServlet extends HttpServlet {
|
||||
} catch (UnsupportedOperationException e) {
|
||||
e.printStackTrace();
|
||||
response.finish(JBean.by(-1, e.getMessage()));
|
||||
} catch (CfgException e) { // 系统元数据配置异常
|
||||
e.printStackTrace();
|
||||
System.out.println("-------------------------------------------");
|
||||
e.log(request);
|
||||
System.out.println("-------------------------------------------");
|
||||
response.finish(JBean.by(-1, e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ public class RunTest<T> {
|
||||
//FBean fBean = new Gson().fromJson(str, FBean.class);
|
||||
FBean fBean = convert.convertFrom(FBean.class, str);
|
||||
|
||||
String[] parse = parser.parse(fBean);
|
||||
String[] parse = parser.parseList(fBean);
|
||||
|
||||
System.out.println("count:" + parse[0]);
|
||||
System.out.println("list:" + parse[1]);
|
||||
@@ -370,4 +370,14 @@ public class RunTest<T> {
|
||||
System.out.println("end");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exceptionTest() {
|
||||
|
||||
try {
|
||||
throw new CfgException("hello exception: %s - %s - %s", 1, 2, "x");
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user