新增支持达梦数据库;优化调试信息及日志打印,解决异常栈不够精准

This commit is contained in:
TommyLemon 2022-10-09 18:57:04 +08:00
parent a3dffa8fba
commit b248b42b00
9 changed files with 357 additions and 177 deletions

View File

@ -5,7 +5,7 @@
<groupId>com.github.Tencent</groupId>
<artifactId>APIJSON</artifactId>
<version>5.2.0</version>
<version>5.3.0</version>
<packaging>jar</packaging>
<name>APIJSONORM</name>

View File

@ -14,18 +14,30 @@ public class Log {
public static boolean DEBUG = true;
public static final String VERSION = "5.2.0";
public static final String KEY_SYSTEM_INFO_DIVIDER = "---|-----APIJSON SYSTEM INFO-----|---";
public static final String VERSION = "5.3.0";
public static final String KEY_SYSTEM_INFO_DIVIDER = "\n---|-----APIJSON SYSTEM INFO-----|---\n";
//默认的时间格式
public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
public static final String OS_NAME;
public static final String OS_VERSION;
public static final String OS_ARCH;
public static final String JAVA_VERSION;
static {
OS_NAME = System.getProperty("os.name");
OS_VERSION = System.getProperty("os.version");
OS_ARCH = System.getProperty("os.arch");
JAVA_VERSION = System.getProperty("java.version");
}
//默认的时间格式
public static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
/**
* modify date format
* @param dateFormatString
*/
public static void setDateFormat(String dateFormatString) {
dateFormat = new SimpleDateFormat(dateFormatString);
DATE_FORMAT = new SimpleDateFormat(dateFormatString);
}
/**
@ -36,10 +48,10 @@ public class Log {
*/
public static void logInfo(String TAG, String msg, String level){
if(level.equals("DEBUG") || level .equals("ERROR") ||level.equals("WARN")){
System.err.println(dateFormat.format(System.currentTimeMillis()) + ": " + TAG + "." + level + ": " + msg);
System.err.println(DATE_FORMAT.format(System.currentTimeMillis()) + ": " + TAG + "." + level + ": " + msg);
}
else if(level.equals("VERBOSE") || level .equals("INFO") ){
System.out.println(dateFormat.format(System.currentTimeMillis()) + ": " + TAG + "." + level + ": " + msg);
System.out.println(DATE_FORMAT.format(System.currentTimeMillis()) + ": " + TAG + "." + level + ": " + msg);
}
}

View File

@ -12,6 +12,7 @@ import apijson.RequestMethod;
import apijson.StringUtil;
import apijson.orm.AbstractFunctionParser.FunctionBean;
import apijson.orm.exception.ConflictException;
import apijson.orm.exception.CommonException;
import apijson.orm.exception.NotExistException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
@ -19,6 +20,7 @@ import com.alibaba.fastjson.JSONObject;
import javax.activation.UnsupportedDataTypeException;
import java.rmi.ServerException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
@ -282,46 +284,7 @@ public abstract class AbstractObjectParser implements ObjectParser {
}
} catch (Exception e) {
if (tri == false) {
if (Log.DEBUG && sqlConfig != null && e.getMessage().contains(Log.KEY_SYSTEM_INFO_DIVIDER) == false) {
try {
String db = sqlConfig.getDatabase();
if (db == null) {
if (sqlConfig.isMySQL()) {
db = SQLConfig.DATABASE_MYSQL;
}
else if (sqlConfig.isPostgreSQL()) {
db = SQLConfig.DATABASE_POSTGRESQL;
}
else if (sqlConfig.isSQLServer()) {
db = SQLConfig.DATABASE_SQLSERVER;
}
else if (sqlConfig.isOracle()) {
db = SQLConfig.DATABASE_ORACLE;
}
else if (sqlConfig.isDb2()) {
db = SQLConfig.DATABASE_DB2;
}
else if (sqlConfig.isClickHouse()) {
db = SQLConfig.DATABASE_CLICKHOUSE;
}
else {
db = AbstractSQLConfig.DEFAULT_DATABASE;
}
}
Class<? extends Exception> clazz = e.getClass();
e = clazz.getConstructor(String.class).newInstance(
e.getMessage()
+ " " + Log.KEY_SYSTEM_INFO_DIVIDER + " \n **环境信息** "
+ " \n 系统: " + System.getProperty("os.name") + " " + System.getProperty("os.version")
+ " \n 数据库: " + db + " " + sqlConfig.getDBVersion()
+ " \n JDK: " + System.getProperty("java.version") + " " + System.getProperty("os.arch")
+ " \n APIJSON: " + Log.VERSION
);
} catch (Throwable e2) {}
}
throw e; // 不忽略错误抛异常
throw CommonException.wrap(e, sqlConfig); // 不忽略错误抛异常
}
invalidate(); // 忽略错误还原request
}
@ -515,9 +478,9 @@ public abstract class AbstractObjectParser implements ObjectParser {
/**
* @param index
* @param key
* @param value
* @param isFirst
* @return
* @throws Exception
*/
@ -746,9 +709,6 @@ public abstract class AbstractObjectParser implements ObjectParser {
protected SQLConfig sqlConfig = null;//array item复用
/**SQL查询for array item
* @param count
* @param page
* @param position
* @return this
* @throws Exception
*/

View File

@ -6,7 +6,6 @@ This source code is licensed under the Apache License Version 2.0.*/
package apijson.orm;
import static apijson.JSONObject.KEY_EXPLAIN;
import static apijson.JSONObject.KEY_JSON;
import static apijson.RequestMethod.GET;
import java.io.UnsupportedEncodingException;
@ -44,6 +43,7 @@ import apijson.RequestMethod;
import apijson.StringUtil;
import apijson.orm.exception.ConditionErrorException;
import apijson.orm.exception.ConflictException;
import apijson.orm.exception.CommonException;
import apijson.orm.exception.NotExistException;
import apijson.orm.exception.NotLoggedInException;
import apijson.orm.exception.OutOfRangeException;
@ -493,14 +493,18 @@ public abstract class AbstractParser<T extends Object> implements Parser<T>, Par
res.put("time:start|duration|end|parse|sql", startTime + "|" + duration + "|" + endTime + "|" + parseDuration + "|" + executedSQLDuration);
if (error != null) {
res.put("trace:throw", error.getClass().getName());
res.put("trace:stack", error.getStackTrace());
// String msg = error.getMessage();
// if (msg != null && msg.contains(Log.KEY_SYSTEM_INFO_DIVIDER)) {
// }
Throwable t = error instanceof CommonException && error.getCause() != null ? error.getCause() : error;
res.put("trace:throw", t.getClass().getName());
res.put("trace:stack", t.getStackTrace());
}
}
onClose();
//CS304 Issue link: https://github.com/Tencent/APIJSON/issues/232
// CS304 Issue link: https://github.com/Tencent/APIJSON/issues/232
if (IS_PRINT_REQUEST_STRING_LOG || Log.DEBUG || error != null) {
Log.sl("\n\n\n", '<', "");
Log.fd(TAG, requestMethod + "/parseResponse request = \n" + requestString + "\n\n");
@ -691,18 +695,18 @@ public abstract class AbstractParser<T extends Object> implements Parser<T>, Par
public static JSONObject extendResult(JSONObject object, int code, String msg, boolean isRoot) {
int index = Log.DEBUG == false || isRoot == false || msg == null ? -1 : msg.lastIndexOf(Log.KEY_SYSTEM_INFO_DIVIDER);
String debug = Log.DEBUG == false || isRoot == false ? null : (index >= 0 ? msg.substring(index + Log.KEY_SYSTEM_INFO_DIVIDER.length()).trim()
: " \n 提 bug 请发请求和响应的【完整截屏】,没图的自行解决!"
+ " \n 开发者有限的时间和精力主要放在【维护项目源码和文档】上!"
+ " \n 【描述不详细】 或 【文档/常见问题 已有答案】 的问题可能会被忽略!!"
+ " \n 【态度 不文明/不友善】的可能会被踢出群,问题也可能不予解答!!!"
: " \n提 bug 请发请求和响应的【完整截屏】,没图的自行解决!"
+ " \n开发者有限的时间和精力主要放在【维护项目源码和文档】上!"
+ " \n【描述不详细】 或 【文档/常见问题 已有答案】 的问题可能会被忽略!!"
+ " \n【态度 不文明/不友善】的可能会被踢出群,问题也可能不予解答!!!"
+ " \n\n **环境信息** "
+ " \n 系统: " + System.getProperty("os.name") + " " + System.getProperty("os.version")
+ " \n 数据库: DEFAULT_DATABASE = " + AbstractSQLConfig.DEFAULT_DATABASE
+ " \n JDK: " + System.getProperty("java.version") + " " + System.getProperty("os.arch")
+ " \n APIJSON: " + Log.VERSION
+ " \n | \n 常见问题https://github.com/Tencent/APIJSON/issues/36"
+ " \n 通用文档https://github.com/Tencent/APIJSON/blob/master/Document.md"
+ " \n 视频教程https://search.bilibili.com/all?keyword=APIJSON");
+ " \n系统: " + Log.OS_NAME + " " + Log.OS_VERSION
+ " \n数据库: DEFAULT_DATABASE = " + AbstractSQLConfig.DEFAULT_DATABASE
+ " \nJDK: " + Log.JAVA_VERSION + " " + Log.OS_ARCH
+ " \nAPIJSON: " + Log.VERSION
+ " \n \n【常见问题】https://github.com/Tencent/APIJSON/issues/36"
+ " \n【通用文档】https://github.com/Tencent/APIJSON/blob/master/Document.md"
+ " \n【视频教程】https://search.bilibili.com/all?keyword=APIJSON");
msg = index >= 0 ? msg.substring(0, index) : msg;
@ -710,10 +714,10 @@ public abstract class AbstractParser<T extends Object> implements Parser<T>, Par
object = new JSONObject(true);
}
if (object.containsKey(JSONResponse.KEY_OK) == false) {
if (object.get(JSONResponse.KEY_OK) == null) {
object.put(JSONResponse.KEY_OK, JSONResponse.isSuccess(code));
}
if (object.containsKey(JSONResponse.KEY_CODE) == false) {
if (object.get(JSONResponse.KEY_CODE) == null) {
object.put(JSONResponse.KEY_CODE, code);
}
@ -763,7 +767,6 @@ public abstract class AbstractParser<T extends Object> implements Parser<T>, Par
/**添加请求成功的状态内容
* @param object
* @param e
* @param isRoot
* @return
*/
public static JSONObject extendErrorResult(JSONObject object, Exception e) {
@ -783,20 +786,25 @@ public abstract class AbstractParser<T extends Object> implements Parser<T>, Par
* @return
*/
public static JSONObject extendErrorResult(JSONObject object, Exception e, RequestMethod requestMethod, String url, boolean isRoot) {
String msg = e.getMessage();
String msg = CommonException.getMsg(e);
if (Log.DEBUG && isRoot) {
try {
int index = msg.lastIndexOf(Log.KEY_SYSTEM_INFO_DIVIDER);
String info = index >= 0 ? msg.substring(index + Log.KEY_SYSTEM_INFO_DIVIDER.length()).trim()
: " \n **环境信息** "
+ " \n 系统: " + System.getProperty("os.name") + " " + System.getProperty("os.version")
+ " \n 数据库: <!-- 请填写,例如 MySQL 5.7DEFAULT_DATABASE = " + AbstractSQLConfig.DEFAULT_DATABASE + " -->"
+ " \n JDK: " + System.getProperty("java.version") + " " + System.getProperty("os.arch")
+ " \n APIJSON: " + Log.VERSION;
boolean isCommon = e instanceof CommonException;
String env = isCommon ? ((CommonException) e).getEnvironment() : null;
if (StringUtil.isEmpty(env)) {
//int index = msg.lastIndexOf(Log.KEY_SYSTEM_INFO_DIVIDER);
//env = index >= 0 ? msg.substring(index + Log.KEY_SYSTEM_INFO_DIVIDER.length()).trim()
env = " \n **环境信息** "
+ " \n 系统: " + Log.OS_NAME + " " + Log.OS_VERSION
+ " \n 数据库: <!-- 请填写,例如 MySQL 5.7。默认数据库为 " + AbstractSQLConfig.DEFAULT_DATABASE + " -->"
+ " \n JDK: " + Log.JAVA_VERSION + " " + Log.OS_ARCH
+ " \n APIJSON: " + Log.VERSION;
msg = index < 0 ? msg : msg.substring(0, index).trim();
String encodedMsg = URLEncoder.encode(msg, "UTF-8");
//msg = index < 0 ? msg : msg.substring(0, index).trim();
}
String encodedMsg = URLEncoder.encode(msg, "UTF-8");
if (StringUtil.isEmpty(url, true)) {
String host = "localhost";
@ -824,12 +832,12 @@ public abstract class AbstractParser<T extends Object> implements Parser<T>, Par
req = URLEncoder.encode(req, "UTF-8");
} catch (Throwable e2) {}
boolean isSQLException = e instanceof SQLException; // SQL 报错一般都是通用问题优先搜索引擎
String apiatuoAndGitHubLink = "\n【APIAuto】 \n http://apijson.cn/api?type=JSON&url=" + URLEncoder.encode(url, "UTF-8") + "&json=" + req
Throwable t = isCommon ? e.getCause() : e;
boolean isSQLException = t instanceof SQLException; // SQL 报错一般都是通用问题优先搜索引擎
String apiatuoAndGitHubLink = "\n\n【APIAuto】 \n http://apijson.cn/api?type=JSON&url=" + URLEncoder.encode(url, "UTF-8") + "&json=" + req
+ " \n\n【GitHub】 \n https://www.google.com/search?q=site%3Agithub.com%2FTencent%2FAPIJSON+++" + encodedMsg;
msg += Log.KEY_SYSTEM_INFO_DIVIDER + " \n | \n 浏览器打开以下链接查看解答"
msg += Log.KEY_SYSTEM_INFO_DIVIDER + " 浏览器打开以下链接查看解答"
+ (isSQLException ? "" : apiatuoAndGitHubLink)
// GitHub Issue 搜索貌似是精准包含不易找到答案 + " \n\nGitHub \n https://github.com/Tencent/APIJSON/issues?q=is%3Aissue+" + encodedMsg
+ " \n\n【Google】\n https://www.google.com/search?q=" + encodedMsg
@ -838,10 +846,10 @@ public abstract class AbstractParser<T extends Object> implements Parser<T>, Par
+ " \n\n都没找到答案打开这个链接 \n https://github.com/Tencent/APIJSON/issues/new?assignees=&labels=&template=--bug.md "
+ " \n然后提交问题推荐用以下模板修改注意要换行保持清晰可读。"
+ " \n【标题】" + msg
+ " \n【内容】" + info + "\n\n**问题描述**\n" + msg
+ " \n【内容】" + env + "\n\n**问题描述**\n" + msg
+ " \n\n<!-- 尽量完整截屏(至少包含请求和回包结果,还可以加上控制台报错日志),然后复制粘贴到这里 -->"
+ " \n\nPOST " + url
+ " \n请求 Request JSON\n ```js"
+ " \n发送请求 Request JSON\n ```js"
+ " \n 请填写,例如 { \"Users\":{} }"
+ " \n```"
+ " \n\n返回结果 Response JSON\n ```js"
@ -850,8 +858,8 @@ public abstract class AbstractParser<T extends Object> implements Parser<T>, Par
} catch (Throwable e2) {}
}
JSONObject error = newErrorResult(e, isRoot);
return extendResult(object, error.getIntValue(JSONResponse.KEY_CODE), msg, isRoot);
int code = CommonException.getCode(e);
return extendResult(object, code, msg, isRoot);
}
/**新建错误状态内容
@ -868,50 +876,14 @@ public abstract class AbstractParser<T extends Object> implements Parser<T>, Par
*/
public static JSONObject newErrorResult(Exception e, boolean isRoot) {
if (e != null) {
e.printStackTrace();
// if (Log.DEBUG) {
e.printStackTrace();
// }
int code;
if (e instanceof UnsupportedEncodingException) {
code = JSONResponse.CODE_UNSUPPORTED_ENCODING;
}
else if (e instanceof IllegalAccessException) {
code = JSONResponse.CODE_ILLEGAL_ACCESS;
}
else if (e instanceof UnsupportedOperationException) {
code = JSONResponse.CODE_UNSUPPORTED_OPERATION;
}
else if (e instanceof NotExistException) {
code = JSONResponse.CODE_NOT_FOUND;
}
else if (e instanceof IllegalArgumentException) {
code = JSONResponse.CODE_ILLEGAL_ARGUMENT;
}
else if (e instanceof NotLoggedInException) {
code = JSONResponse.CODE_NOT_LOGGED_IN;
}
else if (e instanceof TimeoutException) {
code = JSONResponse.CODE_TIME_OUT;
}
else if (e instanceof ConflictException) {
code = JSONResponse.CODE_CONFLICT;
}
else if (e instanceof ConditionErrorException) {
code = JSONResponse.CODE_CONDITION_ERROR;
}
else if (e instanceof UnsupportedDataTypeException) {
code = JSONResponse.CODE_UNSUPPORTED_TYPE;
}
else if (e instanceof OutOfRangeException) {
code = JSONResponse.CODE_OUT_OF_RANGE;
}
else if (e instanceof NullPointerException) {
code = JSONResponse.CODE_NULL_POINTER;
}
else {
code = JSONResponse.CODE_SERVER_ERROR;
}
String msg = CommonException.getMsg(e);
Integer code = CommonException.getCode(e);
return newResult(code, e.getMessage(), isRoot);
return newResult(code, msg, isRoot);
}
return newResult(JSONResponse.CODE_SERVER_ERROR, JSONResponse.MSG_SERVER_ERROR, isRoot);
@ -1987,50 +1959,7 @@ public abstract class AbstractParser<T extends Object> implements Parser<T>, Par
return result;
}
catch (Exception e) {
String msg = e.getMessage();
if (Log.DEBUG && msg != null && msg.contains(Log.KEY_SYSTEM_INFO_DIVIDER) == false) {
try {
String db = config.getDatabase();
if (db == null) {
if (config.isMySQL()) {
db = SQLConfig.DATABASE_MYSQL;
}
else if (config.isPostgreSQL()) {
db = SQLConfig.DATABASE_POSTGRESQL;
}
else if (config.isSQLServer()) {
db = SQLConfig.DATABASE_SQLSERVER;
}
else if (config.isOracle()) {
db = SQLConfig.DATABASE_ORACLE;
}
else if (config.isDb2()) {
db = SQLConfig.DATABASE_DB2;
}
else if (config.isClickHouse()) {
db = SQLConfig.DATABASE_CLICKHOUSE;
}
else {
db = AbstractSQLConfig.DEFAULT_DATABASE;
}
}
Class<? extends Exception> clazz = e.getClass();
e = clazz.getConstructor(String.class).newInstance(
msg
+ " " + Log.KEY_SYSTEM_INFO_DIVIDER + " \n **环境信息** "
+ " \n 系统: " + System.getProperty("os.name") + " " + System.getProperty("os.version")
+ " \n 数据库: " + db + " " + config.getDBVersion()
+ " \n JDK: " + System.getProperty("java.version") + " " + System.getProperty("os.arch")
+ " \n APIJSON: " + Log.VERSION
);
} catch (Throwable e2) {}
}
if (Log.DEBUG == false && e instanceof SQLException) {
throw new SQLException("数据库驱动执行异常SQLException非 Log.DEBUG 模式下不显示详情,避免泄漏真实模式名、表名等隐私信息", e);
}
throw e;
throw CommonException.wrap(e, config);
}
finally {
if (config.getPosition() == 0 && config.limitSQLCount()) {

View File

@ -63,6 +63,8 @@ import apijson.orm.Join.On;
import apijson.orm.exception.NotExistException;
import apijson.orm.model.Access;
import apijson.orm.model.Column;
import apijson.orm.model.DbaColumn;
import apijson.orm.model.DbaTable;
import apijson.orm.model.Document;
import apijson.orm.model.ExtendedProperty;
import apijson.orm.model.Function;
@ -131,6 +133,8 @@ public abstract class AbstractSQLConfig implements SQLConfig {
TABLE_KEY_MAP.put(SysTable.class.getSimpleName(), SysTable.TABLE_NAME);
TABLE_KEY_MAP.put(SysColumn.class.getSimpleName(), SysColumn.TABLE_NAME);
TABLE_KEY_MAP.put(ExtendedProperty.class.getSimpleName(), ExtendedProperty.TABLE_NAME);
TABLE_KEY_MAP.put(DbaTable.class.getSimpleName(), DbaTable.TABLE_NAME);
TABLE_KEY_MAP.put(DbaColumn.class.getSimpleName(), DbaColumn.TABLE_NAME);
CONFIG_TABLE_LIST = new ArrayList<>(); // Table, Column 等是系统表 AbstractVerifier.SYSTEM_ACCESS_MAP.keySet());
CONFIG_TABLE_LIST.add(Function.class.getSimpleName());
@ -146,6 +150,7 @@ public abstract class AbstractSQLConfig implements SQLConfig {
DATABASE_LIST.add(DATABASE_SQLSERVER);
DATABASE_LIST.add(DATABASE_ORACLE);
DATABASE_LIST.add(DATABASE_DB2);
DATABASE_LIST.add(DATABASE_DAMENG);
DATABASE_LIST.add(DATABASE_CLICKHOUSE);
DATABASE_LIST.add(DATABASE_HIVE);
DATABASE_LIST.add(DATABASE_TDENGINE);
@ -982,6 +987,14 @@ public abstract class AbstractSQLConfig implements SQLConfig {
public static boolean isDb2(String db) {
return DATABASE_DB2.equals(db);
}
@Override
public boolean isDameng() {
return isDameng(getSQLDatabase());
}
public static boolean isDameng(String db) {
return DATABASE_DAMENG.equals(db);
}
@Override
public boolean isClickHouse() {
return isClickHouse(getSQLDatabase());
@ -1029,6 +1042,9 @@ public abstract class AbstractSQLConfig implements SQLConfig {
if (SysTable.TAG.equals(table) || SysColumn.TAG.equals(table) || ExtendedProperty.TAG.equals(table)) {
return SCHEMA_SYS; //SQL Server sys 中的属性比 information_schema 中的要全能拿到注释
}
if (DbaTable.TAG.equals(table) || DbaColumn.TAG.equals(table)) {
return ""; //Oracle, Dameng dba_tables all_tab_columns 表好像不属于任何 Schema
}
String sch = getSchema();
return sch == null ? DEFAULT_SCHEMA : sch;
@ -1071,9 +1087,10 @@ public abstract class AbstractSQLConfig implements SQLConfig {
@JSONField(serialize = false)
@Override
public String getSQLTable() {
// String t = TABLE_KEY_MAP.containsKey(table) ? TABLE_KEY_MAP.get(table) : table;
//如果要强制小写则可在子类重写这个方法再 toLowerCase return DATABASE_POSTGRESQL.equals(getDatabase()) ? t.toLowerCase() : t;
return TABLE_KEY_MAP.containsKey(table) ? TABLE_KEY_MAP.get(table) : table;
// 如果要强制小写则可在子类重写这个方法再 toLowerCase return DATABASE_POSTGRESQL.equals(getDatabase()) ? t.toLowerCase() : t;
String ot = getTable();
String nt = TABLE_KEY_MAP.get(ot);
return StringUtil.isEmpty(nt) ? ot : nt;
}
@JSONField(serialize = false)
@Override

View File

@ -22,6 +22,7 @@ public interface SQLConfig {
String DATABASE_SQLSERVER = "SQLSERVER";
String DATABASE_ORACLE = "ORACLE";
String DATABASE_DB2 = "DB2";
String DATABASE_DAMENG = "DAMENG";
String DATABASE_CLICKHOUSE = "CLICKHOUSE";
String DATABASE_HIVE = "HIVE";
String DATABASE_TDENGINE = "TDENGINE";
@ -40,6 +41,7 @@ public interface SQLConfig {
boolean isSQLServer();
boolean isOracle();
boolean isDb2();
boolean isDameng();
boolean isClickHouse();
boolean isHive();
boolean isTDengine();

View File

@ -0,0 +1,224 @@
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package apijson.orm.exception;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.concurrent.TimeoutException;
import javax.activation.UnsupportedDataTypeException;
import apijson.JSONResponse;
import apijson.Log;
import apijson.StringUtil;
import apijson.orm.AbstractSQLConfig;
import apijson.orm.SQLConfig;
/**异常包装器主要用来包装构造器没有 Throwable 参数的 Exception
* @author Lemon
*/
public class CommonException extends Exception {
private static final long serialVersionUID = 1L;
private Integer code;
private String environment;
public CommonException setCode(Integer code) {
this.code = code;
return this;
}
public Integer getCode() {
if (code == null) {
code = getCode(getCause());
}
return code;
}
public static int getCode(Throwable e) {
boolean isCommon = Log.DEBUG && e instanceof CommonException;
Integer code = isCommon ? ((CommonException) e).getCode() : null;
if (code != null) {
return code;
}
Throwable t = isCommon ? e.getCause() : e;
if (t == null) {
return JSONResponse.CODE_SERVER_ERROR;
}
if (t instanceof UnsupportedEncodingException) {
return JSONResponse.CODE_UNSUPPORTED_ENCODING;
}
if (t instanceof IllegalAccessException) {
return JSONResponse.CODE_ILLEGAL_ACCESS;
}
if (t instanceof UnsupportedOperationException) {
return JSONResponse.CODE_UNSUPPORTED_OPERATION;
}
if (t instanceof NotExistException) {
return JSONResponse.CODE_NOT_FOUND;
}
if (t instanceof IllegalArgumentException) {
return JSONResponse.CODE_ILLEGAL_ARGUMENT;
}
if (t instanceof NotLoggedInException) {
return JSONResponse.CODE_NOT_LOGGED_IN;
}
if (t instanceof TimeoutException) {
return JSONResponse.CODE_TIME_OUT;
}
if (t instanceof ConflictException) {
return JSONResponse.CODE_CONFLICT;
}
if (t instanceof ConditionErrorException) {
return JSONResponse.CODE_CONDITION_ERROR;
}
if (t instanceof UnsupportedDataTypeException) {
return JSONResponse.CODE_UNSUPPORTED_TYPE;
}
if (t instanceof OutOfRangeException) {
return JSONResponse.CODE_OUT_OF_RANGE;
}
if (t instanceof NullPointerException) {
return JSONResponse.CODE_NULL_POINTER;
}
return JSONResponse.CODE_SERVER_ERROR;
}
public static String getMsg(Throwable e) {
if (e == null) {
return null;
}
String msg = e.getMessage();
if (msg != null) {
return msg;
}
Throwable t = e.getCause();
return t == null ? null : t.getMessage();
}
public CommonException setEnvironment(String environment) {
this.environment = environment;
return this;
}
public String getEnvironment() {
return environment;
}
public CommonException(Throwable t) {
this(null, t);
}
public CommonException(String msg, Throwable t) {
super(msg == null && t != null ? t.getMessage() : null, t);
}
public CommonException(int code, String msg) {
this(code, msg, null);
}
public CommonException(int code, String msg, Throwable t) {
this(msg, t);
setCode(code);
}
public CommonException(Throwable t, String environment) {
this(null, t);
setEnvironment(environment);
}
public static Exception wrap(Exception e, SQLConfig config) {
if (Log.DEBUG == false && e instanceof SQLException) {
return new SQLException("数据库驱动执行异常SQLException非 Log.DEBUG 模式下不显示详情,避免泄漏真实模式名、表名等隐私信息", e);
}
// String msg = e.getMessage();
if (Log.DEBUG && (e instanceof CommonException == false || ((CommonException) e).getEnvironment() == null)) {
// msg != null && msg.contains(Log.KEY_SYSTEM_INFO_DIVIDER) == false) {
try {
String db = config == null ? AbstractSQLConfig.DEFAULT_DATABASE : (config instanceof AbstractSQLConfig
? ((AbstractSQLConfig) config).getSQLDatabase() : config.getDatabase()
);
String dbVersion = config.getDBVersion();
if (StringUtil.isEmpty(dbVersion)) {
dbVersion = "<!-- 请填写版本号,例如 8.0 -->";
}
if (db != null) {
db += " " + dbVersion;
}
else if (config.isMySQL()) {
db = SQLConfig.DATABASE_MYSQL + " " + dbVersion;
}
else if (config.isPostgreSQL()) {
db = SQLConfig.DATABASE_POSTGRESQL + " " + dbVersion;
}
else if (config.isSQLServer()) {
db = SQLConfig.DATABASE_SQLSERVER + " " + dbVersion;
}
else if (config.isOracle()) {
db = SQLConfig.DATABASE_ORACLE + " " + dbVersion;
}
else if (config.isDb2()) {
db = SQLConfig.DATABASE_DB2 + " " + dbVersion;
}
else if (config.isDameng()) {
db = SQLConfig.DATABASE_DAMENG + " " + dbVersion;
}
else if (config.isClickHouse()) {
db = SQLConfig.DATABASE_CLICKHOUSE + " " + dbVersion;
}
else if (config.isTDengine()) {
db = SQLConfig.DATABASE_TDENGINE + " " + dbVersion;
}
else {
db = "<!-- 请填写,例如 MySQL 5.7。获取到的默认数据库为 " + AbstractSQLConfig.DEFAULT_DATABASE + " -->";
}
// Class<? extends Exception> clazz = e.getClass();
// msg = msg
// + " " + Log.KEY_SYSTEM_INFO_DIVIDER + " **环境信息** "
String env = " **环境信息** "
+ " \n 系统: " + Log.OS_NAME + " " + Log.OS_VERSION
+ " \n 数据库: " + db
+ " \n JDK: " + Log.JAVA_VERSION + " " + Log.OS_ARCH
+ " \n APIJSON: " + Log.VERSION;
if (e instanceof CommonException) {
((CommonException) e).setEnvironment(env);
return e;
}
// try {
// e = clazz.getConstructor(String.class, Throwable.class).newInstance(msg, e);
// }
// catch (Throwable e2) {
return new CommonException(e, env); // e = clazz.getConstructor(String.class).newInstance(msg);
// }
} catch (Throwable e2) {}
}
return e;
}
}

View File

@ -0,0 +1,18 @@
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package apijson.orm.model;
import apijson.MethodAccess;
/**SQL Server sys 下的字段(列名)
* @author Lemon
*/
@MethodAccess(POST = {}, PUT = {}, DELETE = {})
public class DbaColumn {
public static final String TAG = "DbaColumn";
public static final String TABLE_NAME = "all_tab_columns";
}

View File

@ -0,0 +1,18 @@
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package apijson.orm.model;
import apijson.MethodAccess;
/**SQL Server 表属性
* @author Lemon
*/
@MethodAccess(POST = {}, PUT = {}, DELETE = {})
public class DbaTable {
public static final String TAG = "DbaTable";
public static final String TABLE_NAME = "dba_tables";
}