From b39dda60aedc32c0d41fad81df0fa966dd59e0c9 Mon Sep 17 00:00:00 2001 From: iceewei Date: Mon, 14 Jun 2021 22:59:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Edatasource=E5=85=B3=E9=94=AE?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/apijson/JSONObject.java | 27 +++++--- .../apijson/orm/AbstractObjectParser.java | 4 ++ .../main/java/apijson/orm/AbstractParser.java | 15 ++++- .../java/apijson/orm/AbstractSQLConfig.java | 51 ++++++++++++-- .../java/apijson/orm/AbstractVerifier.java | 67 ++++++++++++++++--- .../src/main/java/apijson/orm/Parser.java | 1 + .../src/main/java/apijson/orm/SQLConfig.java | 7 +- 7 files changed, 148 insertions(+), 24 deletions(-) diff --git a/APIJSONORM/src/main/java/apijson/JSONObject.java b/APIJSONORM/src/main/java/apijson/JSONObject.java index 026d63efb..925735bb9 100755 --- a/APIJSONORM/src/main/java/apijson/JSONObject.java +++ b/APIJSONORM/src/main/java/apijson/JSONObject.java @@ -137,9 +137,10 @@ public JSONObject setUserIdIn(List list) { 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 JSONObject setUserIdIn(List list) { TABLE_KEY_LIST = new ArrayList(); 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 JSONObject setRole(String role) { 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 JSONObject setCache(Integer cache) { 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 ... diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java b/APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java index f83c9e8bb..5244abc9a 100755 --- a/APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java +++ b/APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java @@ -287,6 +287,10 @@ else if (method == PUT && value instanceof JSONArray 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()); diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractParser.java b/APIJSONORM/src/main/java/apijson/orm/AbstractParser.java index c46e82e4e..84bbca2b7 100755 --- a/APIJSONORM/src/main/java/apijson/orm/AbstractParser.java +++ b/APIJSONORM/src/main/java/apijson/orm/AbstractParser.java @@ -199,6 +199,16 @@ public AbstractParser setGlobleSchema(String globleSchema) { public String getGlobleSchema() { return globleSchema; } + protected String globleDatasource; + @Override + public String getGlobleDatasource() { + return globleDatasource; + } + public AbstractParser setGlobleDatasource(String globleDatasource) { + this.globleDatasource = globleDatasource; + return this; + } + protected Boolean globleExplain; public AbstractParser setGlobleExplain(Boolean globleExplain) { this.globleExplain = globleExplain; @@ -361,12 +371,14 @@ public JSONObject parseResponse(JSONObject request) { 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 @@ else if (join != null){ 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 Object getValueByPath(String valuePath) { //取出key被valuePath包含的result,再从里面获取key对应的value JSONObject parent = null; String[] keys = null; - for (Map.Entry entry : queryResultMap.entrySet()){ + for (Entry entry : queryResultMap.entrySet()){ String path = entry.getKey(); if (valuePath.startsWith(path + "/")) { try { diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java b/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java index efedbe3e8..d963fea55 100755 --- a/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java +++ b/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java @@ -9,6 +9,7 @@ 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 String getUserIdKey() { 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 AbstractSQLConfig setSchema(String schema) { 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 Object getWhere(String key, boolean exactMatch) { synchronized (where) { if (where != null) { int index; - for (Map.Entry entry : where.entrySet()) { + for (Entry 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 String getSetString(RequestMethod method, Map content, bo Object value; String idKey = getIdKey(); - for (Map.Entry entry : content.entrySet()) { + for (Entry 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 static SQLConfig newSQLConfig(RequestMethod method, String table, String } 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 static interface IdCallback { */ 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 datasource, String table); - /**获取 User 的主键名 + /**已废弃,最早 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 datasource, String table); } public static interface Callback extends IdCallback { @@ -3434,11 +3467,21 @@ public Object newId(RequestMethod method, String database, String schema, String 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 { diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java b/APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java index 926a037f8..30aeb5f15 100755 --- a/APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java +++ b/APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java @@ -109,8 +109,8 @@ public abstract class AbstractVerifier implements Verifier, IdCallback { OPERATION_KEY_LIST.add(REMOVE.name()); OPERATION_KEY_LIST.add(MUST.name()); OPERATION_KEY_LIST.add(REFUSE.name()); - - + + SYSTEM_ACCESS_MAP = new HashMap>(); SYSTEM_ACCESS_MAP.put(Access.class.getSimpleName(), getAccessMap(Access.class.getAnnotation(MethodAccess.class))); @@ -170,10 +170,18 @@ public String getIdKey(String database, String schema, String table) { 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 static JSONObject verifyRequest(@NotNull final RequestMethod method, fina 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 JSONObject onParseJSONObject(String key, JSONObject tobj, JSONObject robj } 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 JSONObject onParseJSONObject(String key, JSONObject tobj, JSONObject robj 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 static JSONObject parse(@NotNull final RequestMethod method, String name, , 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 static JSONObject parse(@NotNull final RequestMethod method, String name, */ 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 static JSONObject parse(@NotNull final RequestMethod method, String name, 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 @@ private static JSONObject operate(Operation opt, JSONObject targetChild, JSONObj if (tk == null || OPERATION_KEY_LIST.contains(tk)) { continue; } - + tv = e.getValue(); if (opt == TYPE) { diff --git a/APIJSONORM/src/main/java/apijson/orm/Parser.java b/APIJSONORM/src/main/java/apijson/orm/Parser.java index dee098b29..6e0af5368 100755 --- a/APIJSONORM/src/main/java/apijson/orm/Parser.java +++ b/APIJSONORM/src/main/java/apijson/orm/Parser.java @@ -124,6 +124,7 @@ JSONObject parseCorrectRequest(RequestMethod method, String tag, int version, St RequestRole getGlobleRole(); String getGlobleDatabase(); String getGlobleSchema(); + String getGlobleDatasource(); Boolean getGlobleExplain(); String getGlobleCache(); diff --git a/APIJSONORM/src/main/java/apijson/orm/SQLConfig.java b/APIJSONORM/src/main/java/apijson/orm/SQLConfig.java index 7e73bdd18..9abce83b9 100755 --- a/APIJSONORM/src/main/java/apijson/orm/SQLConfig.java +++ b/APIJSONORM/src/main/java/apijson/orm/SQLConfig.java @@ -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