新增datasource关键字

This commit is contained in:
iceewei 2021-06-14 22:59:40 +08:00
parent 2a506fdc21
commit b39dda60ae
7 changed files with 148 additions and 24 deletions

View File

@ -137,9 +137,10 @@ public class JSONObject extends com.alibaba.fastjson.JSONObject {
public static final String KEY_ROLE = "@role"; //角色拥有对某些数据的某些操作的权限
public static final String KEY_DATABASE = "@database"; //数据库类型默认为MySQL
public static final String KEY_SCHEMA = "@schema"; //数据库Table在非默认schema内时需要声明
public static final String KEY_DATASOURCE = "@datasource"; //数据源
public static final String KEY_EXPLAIN = "@explain"; //分析 true/false
public static final String KEY_CACHE = "@cache"; //缓存 RAM/ROM/ALL
public static final String KEY_SCHEMA = "@schema"; //数据库Table在非默认schema内时需要声明
public static final String KEY_COLUMN = "@column"; //查询的Table字段或SQL函数
public static final String KEY_FROM = "@from"; //FROM语句
public static final String KEY_COMBINE = "@combine"; //条件组合每个条件key前面可以放&,|,!逻辑关系 "id!{},&sex,!name&$"
@ -154,9 +155,10 @@ public class JSONObject extends com.alibaba.fastjson.JSONObject {
TABLE_KEY_LIST = new ArrayList<String>();
TABLE_KEY_LIST.add(KEY_ROLE);
TABLE_KEY_LIST.add(KEY_DATABASE);
TABLE_KEY_LIST.add(KEY_SCHEMA);
TABLE_KEY_LIST.add(KEY_DATASOURCE);
TABLE_KEY_LIST.add(KEY_EXPLAIN);
TABLE_KEY_LIST.add(KEY_CACHE);
TABLE_KEY_LIST.add(KEY_SCHEMA);
TABLE_KEY_LIST.add(KEY_COLUMN);
TABLE_KEY_LIST.add(KEY_FROM);
TABLE_KEY_LIST.add(KEY_COMBINE);
@ -216,6 +218,20 @@ public class JSONObject extends com.alibaba.fastjson.JSONObject {
public JSONObject setDatabase(String database) {
return puts(KEY_DATABASE, database);
}
/**set schema where table was puts
* @param schema
* @return this
*/
public JSONObject setSchema(String schema) {
return puts(KEY_SCHEMA, schema);
}
/**set datasource where table was puts
* @param datasource
* @return this
*/
public JSONObject setDatasource(String datasource) {
return puts(KEY_DATASOURCE, datasource);
}
/**set if return explain informations
* @param explain
* @return
@ -243,13 +259,6 @@ public class JSONObject extends com.alibaba.fastjson.JSONObject {
public JSONObject setCache(String cache) {
return puts(KEY_CACHE, cache);
}
/**set schema where table was puts
* @param schema
* @return this
*/
public JSONObject setSchema(String schema) {
return puts(KEY_SCHEMA, schema);
}
/**set keys need to be returned
* @param keys key0, key1, key2 ...

View File

@ -287,6 +287,10 @@ public abstract class AbstractObjectParser implements ObjectParser {
if (parser.getGlobleSchema() != null && sqlRequest.get(JSONRequest.KEY_SCHEMA) == null) {
sqlRequest.put(JSONRequest.KEY_SCHEMA, parser.getGlobleSchema());
}
if (parser.getGlobleDatasource() != null && sqlRequest.get(JSONRequest.KEY_DATASOURCE) == null) {
sqlRequest.put(JSONRequest.KEY_DATASOURCE, parser.getGlobleDatasource());
}
if (isSubquery == false) { //解决 SQL 语法报错子查询不能 EXPLAIN
if (parser.getGlobleExplain() != null && sqlRequest.get(JSONRequest.KEY_EXPLAIN) == null) {
sqlRequest.put(JSONRequest.KEY_EXPLAIN, parser.getGlobleExplain());

View File

@ -199,6 +199,16 @@ public abstract class AbstractParser<T> implements Parser<T>, ParserCreator<T>,
public String getGlobleSchema() {
return globleSchema;
}
protected String globleDatasource;
@Override
public String getGlobleDatasource() {
return globleDatasource;
}
public AbstractParser<T> setGlobleDatasource(String globleDatasource) {
this.globleDatasource = globleDatasource;
return this;
}
protected Boolean globleExplain;
public AbstractParser<T> setGlobleExplain(Boolean globleExplain) {
this.globleExplain = globleExplain;
@ -361,12 +371,14 @@ public abstract class AbstractParser<T> implements Parser<T>, ParserCreator<T>,
setGlobleFormat(requestObject.getBoolean(JSONRequest.KEY_FORMAT));
setGlobleDatabase(requestObject.getString(JSONRequest.KEY_DATABASE));
setGlobleSchema(requestObject.getString(JSONRequest.KEY_SCHEMA));
setGlobleDatasource(requestObject.getString(JSONRequest.KEY_DATASOURCE));
setGlobleExplain(requestObject.getBoolean(JSONRequest.KEY_EXPLAIN));
setGlobleCache(requestObject.getString(JSONRequest.KEY_CACHE));
requestObject.remove(JSONRequest.KEY_FORMAT);
requestObject.remove(JSONRequest.KEY_DATABASE);
requestObject.remove(JSONRequest.KEY_SCHEMA);
requestObject.remove(JSONRequest.KEY_DATASOURCE);
requestObject.remove(JSONRequest.KEY_EXPLAIN);
requestObject.remove(JSONRequest.KEY_CACHE);
} catch (Exception e) {
@ -1245,6 +1257,7 @@ public abstract class AbstractParser<T> implements Parser<T>, ParserCreator<T>,
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_ROLE);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_DATABASE);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_SCHEMA);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_DATASOURCE);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_COLUMN);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_COMBINE);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_GROUP);
@ -1464,7 +1477,7 @@ public abstract class AbstractParser<T> implements Parser<T>, ParserCreator<T>,
//取出key被valuePath包含的result再从里面获取key对应的value
JSONObject parent = null;
String[] keys = null;
for (Map.Entry<String,Object> entry : queryResultMap.entrySet()){
for (Entry<String,Object> entry : queryResultMap.entrySet()){
String path = entry.getKey();
if (valuePath.startsWith(path + "/")) {
try {

View File

@ -9,6 +9,7 @@ import static apijson.JSONObject.KEY_CACHE;
import static apijson.JSONObject.KEY_COLUMN;
import static apijson.JSONObject.KEY_COMBINE;
import static apijson.JSONObject.KEY_DATABASE;
import static apijson.JSONObject.KEY_DATASOURCE;
import static apijson.JSONObject.KEY_EXPLAIN;
import static apijson.JSONObject.KEY_FROM;
import static apijson.JSONObject.KEY_GROUP;
@ -335,6 +336,7 @@ public abstract class AbstractSQLConfig implements SQLConfig {
private boolean distinct = false;
private String database; //表所在的数据库类型
private String schema; //表所在的数据库名
private String datasource; //数据源
private String table; //表名
private String alias; //表别名
private String group; //分组方式的字符串数组','分隔
@ -549,6 +551,17 @@ public abstract class AbstractSQLConfig implements SQLConfig {
this.schema = schema;
return this;
}
@Override
public String getDatasource() {
return datasource;
}
@Override
public SQLConfig setDatasource(String datasource) {
this.datasource = datasource;
return this;
}
/**请求传进来的Table名
* @return
* @see {@link #getSQLTable()}
@ -1547,7 +1560,7 @@ public abstract class AbstractSQLConfig implements SQLConfig {
synchronized (where) {
if (where != null) {
int index;
for (Map.Entry<String,Object> entry : where.entrySet()) {
for (Entry<String,Object> entry : where.entrySet()) {
String k = entry.getKey();
index = k.indexOf(key);
if (index >= 0 && StringUtil.isName(k.substring(index)) == false) {
@ -2502,7 +2515,7 @@ public abstract class AbstractSQLConfig implements SQLConfig {
Object value;
String idKey = getIdKey();
for (Map.Entry<String,Object> entry : content.entrySet()) {
for (Entry<String,Object> entry : content.entrySet()) {
String key = entry.getKey();
//避免筛选到全部 value = key == null ? null : content.get(key);
if (key == null || idKey.equals(key)) {
@ -2811,12 +2824,14 @@ public abstract class AbstractSQLConfig implements SQLConfig {
}
String schema = request.getString(KEY_SCHEMA);
String datasource = request.getString(KEY_DATASOURCE);
SQLConfig config = callback.getSQLConfig(method, database, schema, table);
config.setAlias(alias);
config.setDatabase(database); //不删后面表对象还要用的必须放在 parseJoin
config.setSchema(schema); //不删后面表对象还要用的
config.setDatasource(datasource); //不删后面表对象还要用的
if (isProcedure) {
return config;
@ -3387,21 +3402,39 @@ public abstract class AbstractSQLConfig implements SQLConfig {
*/
Object newId(RequestMethod method, String database, String schema, String table);
/**已废弃最早 5.0.0 移除改用 {@link #getIdKey(String, String, String, String)}
* @param database
* @param schema
* @param table
* @return
*/
@Deprecated
String getIdKey(String database, String schema, String table);
/**获取主键名
* @param database
* @param schema
* @param table
* @return
*/
String getIdKey(String database, String schema, String table);
String getIdKey(String database, String schema, String datasource, String table);
/**已废弃最早 5.0.0 移除改用 {@link #getUserIdKey(String, String, String, String)}
* @param database
* @param schema
* @param table
* @return
*/
@Deprecated
String getUserIdKey(String database, String schema, String table);
/**获取 User 的主键名
* @param database
* @param schema
* @param table
* @return
*/
String getUserIdKey(String database, String schema, String table);
String getUserIdKey(String database, String schema, String datasource, String table);
}
public static interface Callback extends IdCallback {
@ -3434,11 +3467,21 @@ public abstract class AbstractSQLConfig implements SQLConfig {
public String getIdKey(String database, String schema, String table) {
return KEY_ID;
}
@Override
public String getIdKey(String database, String schema, String datasource, String table) {
return getIdKey(database, schema, table);
}
@Override
public String getUserIdKey(String database, String schema, String table) {
return KEY_USER_ID;
}
@Override
public String getUserIdKey(String database, String schema, String datasource, String table) {
return getUserIdKey(database, schema, table);
}
@Override
public void onMissingKey4Combine(String name, JSONObject request, String combine, String item, String key) throws Exception {

View File

@ -109,8 +109,8 @@ public abstract class AbstractVerifier<T> implements Verifier<T>, IdCallback {
OPERATION_KEY_LIST.add(REMOVE.name());
OPERATION_KEY_LIST.add(MUST.name());
OPERATION_KEY_LIST.add(REFUSE.name());
SYSTEM_ACCESS_MAP = new HashMap<String, Map<RequestMethod, RequestRole[]>>();
SYSTEM_ACCESS_MAP.put(Access.class.getSimpleName(), getAccessMap(Access.class.getAnnotation(MethodAccess.class)));
@ -170,10 +170,18 @@ public abstract class AbstractVerifier<T> implements Verifier<T>, IdCallback {
return apijson.JSONObject.KEY_ID;
}
@Override
public String getIdKey(String database, String schema, String datasource, String table) {
return getIdKey(database, schema, table);
}
@Override
public String getUserIdKey(String database, String schema, String table) {
return apijson.JSONObject.KEY_USER_ID;
}
@Override
public String getUserIdKey(String database, String schema, String datasource, String table) {
return getUserIdKey(database, schema, table);
}
@Override
public Object newId(RequestMethod method, String database, String schema, String table) {
return System.currentTimeMillis();
}
@ -515,6 +523,23 @@ public abstract class AbstractVerifier<T> implements Verifier<T>, IdCallback {
public static JSONObject verifyRequest(@NotNull final RequestMethod method, final String name
, final JSONObject target, final JSONObject request, final int maxUpdateCount
, final String database, final String schema, final IdCallback idCallback, final SQLCreator creator) throws Exception {
return verifyRequest(method, name, target, request, maxUpdateCount, database, schema, null, idCallback, creator);
}
/**从request提取target指定的内容
* @param method
* @param name
* @param target
* @param request
* @param maxUpdateCount
* @param idKey
* @param userIdKey
* @param creator
* @return
* @throws Exception
*/
public static JSONObject verifyRequest(@NotNull final RequestMethod method, final String name
, final JSONObject target, final JSONObject request, final int maxUpdateCount
, final String database, final String schema, final String datasource, final IdCallback idCallback, final SQLCreator creator) throws Exception {
Log.i(TAG, "verifyRequest method = " + method + "; name = " + name
+ "; target = \n" + JSON.toJSONString(target)
@ -546,14 +571,18 @@ public abstract class AbstractVerifier<T> implements Verifier<T>, IdCallback {
} else if (apijson.JSONObject.isTableKey(key)) {
String db = request.getString(apijson.JSONObject.KEY_DATABASE);
String sh = request.getString(apijson.JSONObject.KEY_SCHEMA);
String ds = request.getString(apijson.JSONObject.KEY_DATASOURCE);
if (StringUtil.isEmpty(db, false)) {
db = database;
}
if (StringUtil.isEmpty(sh, false)) {
sh = schema;
}
if (StringUtil.isEmpty(ds, false)) {
ds = datasource;
}
String idKey = idCallback == null ? null : idCallback.getIdKey(db, sh, key);
String idKey = idCallback == null ? null : idCallback.getIdKey(db, sh, ds, key);
String finalIdKey = StringUtil.isEmpty(idKey, false) ? apijson.JSONObject.KEY_ID : idKey;
if (method == RequestMethod.POST) {
@ -564,7 +593,7 @@ public abstract class AbstractVerifier<T> implements Verifier<T>, IdCallback {
if (RequestMethod.isQueryMethod(method) == false) {
verifyId(method.name(), name, key, robj, finalIdKey, maxUpdateCount, true);
String userIdKey = idCallback == null ? null : idCallback.getUserIdKey(db, sh, key);
String userIdKey = idCallback == null ? null : idCallback.getUserIdKey(db, sh, ds, key);
String finalUserIdKey = StringUtil.isEmpty(userIdKey, false) ? apijson.JSONObject.KEY_USER_ID : userIdKey;
verifyId(method.name(), name, key, robj, finalUserIdKey, maxUpdateCount, false);
}
@ -742,14 +771,14 @@ public abstract class AbstractVerifier<T> implements Verifier<T>, IdCallback {
, SQLCreator creator, @NotNull OnParseCallback callback) throws Exception {
return parse(method, name, target, real, null, null, null, creator, callback);
}
/**对request和response不同的解析用callback返回
* @param method
* @param name
* @param target
* @param real
* @param idKey
* @param userIdKey
* @param database
* @param schema
* @param idCallback
* @param creator
* @param callback
* @return
@ -757,6 +786,24 @@ public abstract class AbstractVerifier<T> implements Verifier<T>, IdCallback {
*/
public static JSONObject parse(@NotNull final RequestMethod method, String name, JSONObject target, JSONObject real
, final String database, final String schema, final IdCallback idCallback, SQLCreator creator, @NotNull OnParseCallback callback) throws Exception {
return parse(method, name, target, real, database, schema, null, idCallback, creator, callback);
}
/**对request和response不同的解析用callback返回
* @param method
* @param name
* @param target
* @param real
* @param database
* @param schema
* @param datasource
* @param idCallback
* @param creator
* @param callback
* @return
* @throws Exception
*/
public static JSONObject parse(@NotNull final RequestMethod method, String name, JSONObject target, JSONObject real
, final String database, final String schema, final String datasource, final IdCallback idCallback, SQLCreator creator, @NotNull OnParseCallback callback) throws Exception {
if (target == null) {
return null;
}
@ -913,12 +960,16 @@ public abstract class AbstractVerifier<T> implements Verifier<T>, IdCallback {
String db = real.getString(apijson.JSONObject.KEY_DATABASE);
String sh = real.getString(apijson.JSONObject.KEY_SCHEMA);
String ds = real.getString(apijson.JSONObject.KEY_DATASOURCE);
if (StringUtil.isEmpty(db, false)) {
db = database;
}
if (StringUtil.isEmpty(sh, false)) {
sh = schema;
}
if (StringUtil.isEmpty(ds, false)) {
ds = datasource;
}
String idKey = idCallback == null ? null : idCallback.getIdKey(db, sh, name);
String finalIdKey = StringUtil.isEmpty(idKey, false) ? apijson.JSONObject.KEY_ID : idKey;
@ -977,7 +1028,7 @@ public abstract class AbstractVerifier<T> implements Verifier<T>, IdCallback {
if (tk == null || OPERATION_KEY_LIST.contains(tk)) {
continue;
}
tv = e.getValue();
if (opt == TYPE) {

View File

@ -124,6 +124,7 @@ public interface Parser<T> {
RequestRole getGlobleRole();
String getGlobleDatabase();
String getGlobleSchema();
String getGlobleDatasource();
Boolean getGlobleExplain();
String getGlobleCache();

View File

@ -120,10 +120,13 @@ public interface SQLConfig {
String getDatabase();
SQLConfig setDatabase(String database);
String getQuote();
String getSchema();
SQLConfig setSchema(String schema);
String getDatasource();
SQLConfig setDatasource(String datasource);
String getQuote();
/**请求传进来的Table名
* @return