Skip to content

Commit fa79888

Browse files
committed
Server:JSONObject 新增对象关键词 @Explain 和 @cache 替代 JSONRequest 里的数组关键词 explain 和 cache
1 parent 57920dc commit fa79888

File tree

5 files changed

+83
-11
lines changed

5 files changed

+83
-11
lines changed

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/JSONObject.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,14 @@ public JSONObject setUserIdIn(List<Object> list) {
121121

122122

123123
//@key关键字都放这个类 <<<<<<<<<<<<<<<<<<<<<<
124-
public static final String KEY_ROLE = "@role"; //角色,拥有对某些数据的某些操作的权限
125124
public static final String KEY_TRY = "@try"; //尝试,忽略异常
126125
public static final String KEY_DROP = "@drop"; //丢弃,不返回
127126
public static final String KEY_CORRECT = "@correct"; //字段校正
128127

128+
public static final String KEY_ROLE = "@role"; //角色,拥有对某些数据的某些操作的权限
129129
public static final String KEY_DATABASE = "@database"; //数据库类型,默认为MySQL
130+
public static final String KEY_EXPLAIN = "@explain"; //分析 true/false
131+
public static final String KEY_CACHE = "@cache"; //缓存 RAM/ROM/ALL
130132
public static final String KEY_SCHEMA = "@schema"; //数据库,Table在非默认schema内时需要声明
131133
public static final String KEY_COLUMN = "@column"; //查询的Table字段或SQL函数
132134
public static final String KEY_FROM = "@from"; //FROM语句
@@ -140,6 +142,8 @@ public JSONObject setUserIdIn(List<Object> list) {
140142
TABLE_KEY_LIST = new ArrayList<String>();
141143
TABLE_KEY_LIST.add(KEY_ROLE);
142144
TABLE_KEY_LIST.add(KEY_DATABASE);
145+
TABLE_KEY_LIST.add(KEY_EXPLAIN);
146+
TABLE_KEY_LIST.add(KEY_CACHE);
143147
TABLE_KEY_LIST.add(KEY_SCHEMA);
144148
TABLE_KEY_LIST.add(KEY_COLUMN);
145149
TABLE_KEY_LIST.add(KEY_FROM);

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/AbstractObjectParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -766,9 +766,9 @@ public void parseFunction(JSONObject json, String key, String value) throws Exce
766766

767767
private SQLConfig newSQLConfig(SQLConfig arrayConfig, boolean isProcedure) throws Exception {
768768
SQLConfig cfg = newSQLConfig(isProcedure);
769-
if (arrayConfig != null) {
770-
cfg.setCache(arrayConfig.getCache()).setExplain(arrayConfig.isExplain());
771-
}
769+
// if (arrayConfig != null) {
770+
// cfg.setCache(arrayConfig.getCache()).setExplain(arrayConfig.isExplain());
771+
// }
772772
return cfg;
773773
}
774774

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/AbstractParser.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package zuo.biao.apijson.server;
1616

17+
import static zuo.biao.apijson.JSONObject.KEY_EXPLAIN;
1718
import static zuo.biao.apijson.RequestMethod.GET;
1819

1920
import java.io.UnsupportedEncodingException;
@@ -1349,7 +1350,29 @@ public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Except
13491350
}
13501351

13511352
try {
1352-
return parseCorrectResponse(config.getTable(), sqlExecutor.execute(config, false));
1353+
boolean explain = config.isExplain();
1354+
JSONObject result;
1355+
if (explain) { //如果先执行 explain,则 execute 会死循环,所以只能先执行非 explain
1356+
config.setExplain(false); //对下面 config.getSQL(false); 生效
1357+
JSONObject res = sqlExecutor.execute(config, false);
1358+
1359+
config.setExplain(explain);
1360+
JSONObject explainResult = config.isMain() && config.getPosition() != 0 ? null : sqlExecutor.execute(config, false);
1361+
1362+
if (explainResult == null) {
1363+
result = res;
1364+
}
1365+
else {
1366+
result = new JSONObject(true);
1367+
result.put(KEY_EXPLAIN, explainResult);
1368+
result.putAll(res);
1369+
}
1370+
}
1371+
else {
1372+
result = sqlExecutor.execute(config, false);
1373+
}
1374+
1375+
return parseCorrectResponse(config.getTable(), result);
13531376
}
13541377
catch (Exception e) {
13551378
if (Log.DEBUG == false && e instanceof SQLException) {

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/AbstractSQLConfig.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import static zuo.biao.apijson.JSONObject.KEY_COLUMN;
1818
import static zuo.biao.apijson.JSONObject.KEY_COMBINE;
1919
import static zuo.biao.apijson.JSONObject.KEY_DATABASE;
20+
import static zuo.biao.apijson.JSONObject.KEY_EXPLAIN;
21+
import static zuo.biao.apijson.JSONObject.KEY_CACHE;
2022
import static zuo.biao.apijson.JSONObject.KEY_FROM;
2123
import static zuo.biao.apijson.JSONObject.KEY_GROUP;
2224
import static zuo.biao.apijson.JSONObject.KEY_HAVING;
@@ -881,6 +883,37 @@ public AbstractSQLConfig setCache(int cache) {
881883
this.cache = cache;
882884
return this;
883885
}
886+
887+
public AbstractSQLConfig setCache(String cache) {
888+
int cache2;
889+
if (cache == null) {
890+
cache2 = JSONRequest.CACHE_ALL;
891+
}
892+
else {
893+
// if (isSubquery) {
894+
// throw new IllegalArgumentException("子查询内不支持传 " + JSONRequest.KEY_CACHE + "!");
895+
// }
896+
897+
switch (cache) {
898+
case "0":
899+
case JSONRequest.CACHE_ALL_STRING:
900+
cache2 = JSONRequest.CACHE_ALL;
901+
break;
902+
case "1":
903+
case JSONRequest.CACHE_ROM_STRING:
904+
cache2 = JSONRequest.CACHE_ROM;
905+
break;
906+
case "2":
907+
case JSONRequest.CACHE_RAM_STRING:
908+
cache2 = JSONRequest.CACHE_RAM;
909+
break;
910+
default:
911+
throw new IllegalArgumentException(getTable() + "/" + JSONRequest.KEY_CACHE + ":value 中 value 的值不合法!必须在 [0,1,2] 或 [ALL, ROM, RAM] 内 !");
912+
}
913+
}
914+
setCache(cache2);
915+
return this;
916+
}
884917

885918
@Override
886919
public boolean isExplain() {
@@ -2082,6 +2115,9 @@ public String getJoinString() throws Exception {
20822115
/**新建SQL配置
20832116
* @param table
20842117
* @param request
2118+
* @param joinList
2119+
* @param isProcedure
2120+
* @param callback
20852121
* @return
20862122
* @throws Exception
20872123
*/
@@ -2163,6 +2199,8 @@ else if (id instanceof Subquery) {}
21632199

21642200

21652201
String role = request.getString(KEY_ROLE);
2202+
boolean explain = request.getBooleanValue(KEY_EXPLAIN);
2203+
String cache = request.getString(KEY_CACHE);
21662204
String combine = request.getString(KEY_COMBINE);
21672205
Subquery from = (Subquery) request.get(KEY_FROM);
21682206
String column = request.getString(KEY_COLUMN);
@@ -2175,6 +2213,8 @@ else if (id instanceof Subquery) {}
21752213
request.remove(idInKey);
21762214
//关键词
21772215
request.remove(KEY_ROLE);
2216+
request.remove(KEY_EXPLAIN);
2217+
request.remove(KEY_CACHE);
21782218
request.remove(KEY_DATABASE);
21792219
request.remove(KEY_SCHEMA);
21802220
request.remove(KEY_COMBINE);
@@ -2355,6 +2395,8 @@ else if (whereList != null && whereList.contains(key)) {
23552395
}
23562396
}
23572397

2398+
config.setExplain(explain);
2399+
config.setCache(cache);
23582400
config.setFrom(from);
23592401
config.setColumn(column == null ? null : cs); //解决总是 config.column != null,总是不能得到 *
23602402
config.setWhere(tableWhere);
@@ -2374,8 +2416,10 @@ else if (whereList != null && whereList.contains(key)) {
23742416
request.put(idKey, id);
23752417
request.put(idInKey, idIn);
23762418
//关键词
2377-
request.put(KEY_ROLE, role);
23782419
request.put(KEY_DATABASE, database);
2420+
request.put(KEY_ROLE, role);
2421+
request.put(KEY_EXPLAIN, explain);
2422+
request.put(KEY_CACHE, cache);
23792423
request.put(KEY_SCHEMA, schema);
23802424
request.put(KEY_COMBINE, combine);
23812425
request.put(KEY_FROM, from);
@@ -2387,7 +2431,8 @@ else if (whereList != null && whereList.contains(key)) {
23872431
return config;
23882432
}
23892433

2390-
2434+
2435+
23912436
/**
23922437
* @param method
23932438
* @param config

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/AbstractSQLExecutor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
174174

175175
long startTime = System.currentTimeMillis();
176176
Log.d(TAG, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
177-
+ "\n 已生成 " + generatedSQLCount + " 条 SQL"
178-
+ "\n select startTime = " + startTime
179-
+ "\n sql = \n " + sql
177+
+ "\n已生成 " + generatedSQLCount + " 条 SQL"
178+
+ "\nselect startTime = " + startTime
179+
+ "\nsql = \n" + sql
180180
+ "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
181181

182182
ResultSet rs = null;
@@ -229,7 +229,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws
229229

230230
case GET:
231231
case GETS:
232-
noCache = config.isExplain() || config.isTest();
232+
noCache = config.isTest() || config.isExplain(); // TODO explain 照样 cache,但下面的 noCache 涉及的地方要改改,尤其是 JOIN
233233

234234
result = noCache ? null : getCacheItem(sql, position, config.getCache());
235235
Log.i(TAG, ">>> select result = getCache('" + sql + "', " + position + ") = " + result);

0 commit comments

Comments
 (0)