Skip to content

Commit 38b09a8

Browse files
committed
Server:新增数组关键词 cache 和 explain;解决 id{} 和 id 同时存在有时候 contains 因为类型不一致导致判断错误而 throw NotExistException;SQLConfig 删除多余的 cacheStatic
1 parent 7d33b02 commit 38b09a8

File tree

7 files changed

+196
-89
lines changed

7 files changed

+196
-89
lines changed

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,40 @@ public JSONRequest setFormat(Boolean format) {
8888
public static final int QUERY_TOTAL = 1;
8989
public static final int QUERY_ALL = 2;
9090

91+
public static final String QUERY_TABLE_STRING = "TABLE";
92+
public static final String QUERY_TOTAL_STRING = "TOTAL";
93+
public static final String QUERY_ALL_STRING = "ALL";
94+
95+
public static final int CACHE_ALL = 0;
96+
public static final int CACHE_ROM = 1;
97+
public static final int CACHE_RAM = 2;
98+
99+
public static final String CACHE_ALL_STRING = "ALL";
100+
public static final String CACHE_ROM_STRING = "ROM";
101+
public static final String CACHE_RAM_STRING = "RAM";
102+
91103
public static final String SUBQUERY_RANGE_ALL = "ALL";
92104
public static final String SUBQUERY_RANGE_ANY = "ANY";
93105

94-
public static final String KEY_QUERY = "query";
106+
public static final String KEY_QUERY = "query"; // 0-
95107
public static final String KEY_COUNT = "count";
96108
public static final String KEY_PAGE = "page";
97109
public static final String KEY_JOIN = "join";
110+
public static final String KEY_CACHE = "cache"; // RAM/ROM/ALL ?
111+
public static final String KEY_EXPLAIN = "explain"; // true explain:true/false 为了兼容,就不在 query 里加一个值了
98112
public static final String KEY_SUBQUERY_RANGE = "range";
99113
public static final String KEY_SUBQUERY_FROM = "from";
100-
114+
115+
101116
public static final List<String> ARRAY_KEY_LIST;
102117
static {
103118
ARRAY_KEY_LIST = new ArrayList<String>();
104119
ARRAY_KEY_LIST.add(KEY_QUERY);
105120
ARRAY_KEY_LIST.add(KEY_COUNT);
106121
ARRAY_KEY_LIST.add(KEY_PAGE);
107122
ARRAY_KEY_LIST.add(KEY_JOIN);
123+
ARRAY_KEY_LIST.add(KEY_CACHE);
124+
ARRAY_KEY_LIST.add(KEY_EXPLAIN);
108125
ARRAY_KEY_LIST.add(KEY_SUBQUERY_RANGE);
109126
ARRAY_KEY_LIST.add(KEY_SUBQUERY_FROM);
110127
}
@@ -142,6 +159,36 @@ public JSONRequest setJoin(String... joins) {
142159
return puts(KEY_JOIN, StringUtil.getString(joins));
143160
}
144161

162+
/**set cache type
163+
* @param cache
164+
* @return
165+
* @see {@link #CACHE_ALL}
166+
* @see {@link #CACHE_RAM}
167+
* @see {@link #CACHE_ROM}
168+
*/
169+
public JSONRequest setCache(int cache) {
170+
return puts(KEY_CACHE, cache);
171+
}
172+
173+
/**set cache type
174+
* @param cache
175+
* @return
176+
* @see {@link #CACHE_ALL_STRING}
177+
* @see {@link #CACHE_RAM_STRING}
178+
* @see {@link #CACHE_ROM_STRING}
179+
*/
180+
public JSONRequest setCache(String cache) {
181+
return puts(KEY_CACHE, cache);
182+
}
183+
184+
/**set joins of Main Table and it's Vice Tables in Array layer
185+
* @param joins "@/User/id@", "&/User/id@,>/Comment/momentId@" ...
186+
* @return
187+
*/
188+
public JSONRequest setExplain(boolean explain) {
189+
return puts(KEY_EXPLAIN, explain);
190+
}
191+
145192
/**set range for Subquery
146193
* @param range
147194
* @return

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ public AbstractObjectParser parse() throws Exception {
291291
}
292292
else {//直接解析并替换原来的,[]:{} 内必须直接解析,否则会因为丢掉count等属性,并且total@:"/[]/total"必须在[]:{} 后!
293293
response.put(key, onChildParse(index, key, (JSONObject)value));
294+
//导致不返回任何字段 response.put(isTable == false && arrayConfig != null && arrayConfig.isExplain()
295+
// ? JSONRequest.KEY_EXPLAIN : key, onChildParse(index, key, (JSONObject)value));
294296
index ++;
295297
}
296298
}
@@ -624,9 +626,10 @@ public AbstractObjectParser setSQLConfig(int count, int page, int position) thro
624626

625627
if (sqlConfig == null) {
626628
try {
627-
sqlConfig = newSQLConfig(false);
629+
sqlConfig = newSQLConfig(arrayConfig, false);
628630
}
629631
catch (NotExistException e) {
632+
e.printStackTrace();
630633
return this;
631634
}
632635
}
@@ -737,7 +740,7 @@ public void onFunctionResponse(String type) throws Exception {
737740
public void parseFunction(JSONObject json, String key, String value) throws Exception {
738741
Object result;
739742
if (key.startsWith("@")) {
740-
SQLConfig config = newSQLConfig(true);
743+
SQLConfig config = newSQLConfig(arrayConfig, true);
741744
config.setProcedure(value);
742745

743746
SQLExecutor executor = null;
@@ -761,6 +764,14 @@ public void parseFunction(JSONObject json, String key, String value) throws Exce
761764
}
762765
}
763766

767+
private SQLConfig newSQLConfig(SQLConfig arrayConfig, boolean isProcedure) throws Exception {
768+
SQLConfig cfg = newSQLConfig(isProcedure);
769+
if (arrayConfig != null) {
770+
cfg.setCache(arrayConfig.getCache()).setExplain(arrayConfig.isExplain());
771+
}
772+
return cfg;
773+
}
774+
764775
@Override
765776
public void onChildResponse() throws Exception {
766777
//把isTable时取出去child解析后重新添加回来

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

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ public JSONObject getStructure(@NotNull String table, String key, String value,
629629

630630
//too many connections error: 不try-catch,可以让客户端看到是服务器内部异常
631631
try {
632-
JSONObject result = executor.execute(config.setCacheStatic(true), false);
632+
JSONObject result = executor.execute(config, false);
633633
return getJSONObject(result, "structure");//解决返回值套了一层 "structure":{}
634634
} finally {
635635
executor.close();
@@ -751,34 +751,59 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
751751
final Integer count = request.getInteger(JSONRequest.KEY_COUNT); //TODO 如果不想用默认数量可以改成 getIntValue(JSONRequest.KEY_COUNT);
752752
final int page = request.getIntValue(JSONRequest.KEY_PAGE);
753753
final Object join = request.get(JSONRequest.KEY_JOIN);
754+
final String cache = request.getString(JSONRequest.KEY_CACHE);
755+
final boolean explain = request.getBooleanValue(JSONRequest.KEY_EXPLAIN);
754756

755757
int query2;
756758
if (query == null) {
757759
query2 = JSONRequest.QUERY_TABLE;
758760
}
759761
else {
760-
// if (isSubquery) {
761-
// throw new IllegalArgumentException("子查询内不支持传 " + JSONRequest.KEY_QUERY + "!");
762-
// }
763-
764762
switch (query) {
765763
case "0":
766-
case "TABLE":
764+
case JSONRequest.QUERY_TABLE_STRING:
767765
query2 = JSONRequest.QUERY_TABLE;
768766
break;
769767
case "1":
770-
case "TOTAL":
768+
case JSONRequest.QUERY_TOTAL_STRING:
771769
query2 = JSONRequest.QUERY_TOTAL;
772770
break;
773771
case "2":
774-
case "ALL":
772+
case JSONRequest.QUERY_ALL_STRING:
775773
query2 = JSONRequest.QUERY_ALL;
776774
break;
777775
default:
778776
throw new IllegalArgumentException(path + "/" + JSONRequest.KEY_QUERY + ":value 中 value 的值不合法!必须在 [0,1,2] 或 [TABLE, TOTAL, ALL] 内 !");
779777
}
780778
}
781779

780+
int cache2;
781+
if (cache == null) {
782+
cache2 = JSONRequest.CACHE_ALL;
783+
}
784+
else {
785+
if (isSubquery) {
786+
throw new IllegalArgumentException("子查询内不支持传 " + JSONRequest.KEY_CACHE + "!");
787+
}
788+
789+
switch (cache) {
790+
case "0":
791+
case JSONRequest.CACHE_ALL_STRING:
792+
cache2 = JSONRequest.CACHE_ALL;
793+
break;
794+
case "1":
795+
case JSONRequest.CACHE_ROM_STRING:
796+
cache2 = JSONRequest.CACHE_ROM;
797+
break;
798+
case "2":
799+
case JSONRequest.CACHE_RAM_STRING:
800+
cache2 = JSONRequest.CACHE_RAM;
801+
break;
802+
default:
803+
throw new IllegalArgumentException(path + "/" + JSONRequest.KEY_CACHE + ":value 中 value 的值不合法!必须在 [0,1,2] 或 [ALL, ROM, RAM] 内 !");
804+
}
805+
}
806+
782807
int maxPage = getMaxQueryPage();
783808
if (page < 0 || page > maxPage) {
784809
throw new IllegalArgumentException(path + "/" + JSONRequest.KEY_PAGE + ":value 中 value 的值不合法!必须在 0-" + maxPage + " 内 !");
@@ -796,7 +821,9 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
796821
request.remove(JSONRequest.KEY_COUNT);
797822
request.remove(JSONRequest.KEY_PAGE);
798823
request.remove(JSONRequest.KEY_JOIN);
799-
Log.d(TAG, "onArrayParse query = " + query + "; count = " + count + "; page = " + page + "; join = " + join);
824+
request.remove(JSONRequest.KEY_CACHE);
825+
request.remove(JSONRequest.KEY_EXPLAIN);
826+
Log.d(TAG, "onArrayParse query = " + query + "; count = " + count + "; page = " + page + "; join = " + join + "; cache = " + cache + "; explain = " + explain);
800827

801828
if (request.isEmpty()) {//如果条件成立,说明所有的 parentPath/name:request 中request都无效!!!
802829
Log.e(TAG, "onArrayParse request.isEmpty() >> return null;");
@@ -826,11 +853,13 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
826853
.setCount(size)
827854
.setPage(page)
828855
.setQuery(query2)
856+
.setCache(cache2)
857+
.setExplain(explain)
829858
.setJoinList(onJoinParse(join, request));
830859

860+
int len = explain || isSubquery ? 1 : size;
831861
JSONObject parent;
832-
//生成size个
833-
for (int i = 0; i < (isSubquery ? 1 : size); i++) {
862+
for (int i = 0; i < len; i++) { //生成len个
834863
parent = onObjectParse(request, path, "" + i, config.setType(SQLConfig.TYPE_ITEM).setPosition(i), isSubquery);
835864
if (parent == null || parent.isEmpty()) {
836865
break;
@@ -867,6 +896,8 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
867896
request.put(JSONRequest.KEY_COUNT, count);
868897
request.put(JSONRequest.KEY_PAGE, page);
869898
request.put(JSONRequest.KEY_JOIN, join);
899+
request.put(JSONRequest.KEY_CACHE, cache);
900+
request.put(JSONRequest.KEY_EXPLAIN, explain);
870901

871902
if (Log.DEBUG) {
872903
Log.i(TAG, "onArrayParse return response = \n" + JSON.toJSONString(response) + "\n>>>>>>>>>>>>>>>\n\n\n");

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

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,15 @@ public String getUserIdKey() {
124124
private int position; //Table在[]中的位置
125125
private int query; //JSONRequest.query
126126
private int type; //ObjectParser.type
127+
private int cache;
128+
private boolean explain;
129+
127130
private List<Join> joinList; //连表 配置列表
128131
//array item >>>>>>>>>>
129132
private boolean test; //测试
130-
private boolean cacheStatic; //静态缓存
131133

132134
private String procedure;
135+
133136
public SQLConfig setProcedure(String procedure) {
134137
this.procedure = procedure;
135138
return this;
@@ -868,6 +871,27 @@ public AbstractSQLConfig setType(int type) {
868871
this.type = type;
869872
return this;
870873
}
874+
875+
@Override
876+
public int getCache() {
877+
return cache;
878+
}
879+
@Override
880+
public AbstractSQLConfig setCache(int cache) {
881+
this.cache = cache;
882+
return this;
883+
}
884+
885+
@Override
886+
public boolean isExplain() {
887+
return explain;
888+
}
889+
@Override
890+
public AbstractSQLConfig setExplain(boolean explain) {
891+
this.explain = explain;
892+
return this;
893+
}
894+
871895
@Override
872896
public List<Join> getJoinList() {
873897
return joinList;
@@ -892,16 +916,6 @@ public AbstractSQLConfig setTest(boolean test) {
892916
this.test = test;
893917
return this;
894918
}
895-
@Override
896-
public boolean isCacheStatic() {
897-
return cacheStatic;
898-
}
899-
@Override
900-
public AbstractSQLConfig setCacheStatic(boolean cacheStatic) {
901-
this.cacheStatic = cacheStatic;
902-
return this;
903-
}
904-
905919

906920
/**获取初始位置offset
907921
* @return
@@ -1895,7 +1909,7 @@ public static String getSQL(AbstractSQLConfig config) throws Exception {
18951909
String sch = config.getSQLSchema(config.getSQLTable());
18961910
if (StringUtil.isNotEmpty(config.getProcedure(), true)) {
18971911
String q = config.getQuote();
1898-
return "CALL " + q + sch + q + "."+ config.getProcedure();
1912+
return "CALL " + q + sch + q + "."+ config.getProcedure(); // EXPLIAN 不能用于存储过程,和 CALL 语法冲突
18991913
}
19001914

19011915
String tablePath = config.getTablePath();
@@ -1914,7 +1928,7 @@ public static String getSQL(AbstractSQLConfig config) throws Exception {
19141928
default:
19151929
config.setPreparedValueList(new ArrayList<Object>());
19161930
String column = config.getColumnString();
1917-
return "SELECT " + column + " FROM " + getConditionString(column, tablePath, config);
1931+
return (config.isExplain() ? "EXPLAIN " : "") + "SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config);
19181932
}
19191933
}
19201934

@@ -2131,8 +2145,18 @@ else if (id instanceof Subquery) {}
21312145
}
21322146

21332147
if (idIn instanceof List) { //共用idIn场景少性能差
2134-
if (((List<?>) idIn).contains(id) == false) {//empty有效 BaseModel.isEmpty(idIn) == false) {
2135-
throw new NotExistException(TAG + ": newSQLConfig idIn != null && ((JSONArray) idIn).contains(id) == false");
2148+
boolean contains = false;
2149+
List<?> ids = ((List<?>) idIn);
2150+
Object d;
2151+
for (int i = 0; i < ids.size(); i++) { //不用 idIn.contains(id) 因为 idIn 里存到很可能是 Integer,id 又是 Long!
2152+
d = ids.get(i);
2153+
if (d != null && id.toString().equals(d.toString())) {
2154+
contains = true;
2155+
break;
2156+
}
2157+
}
2158+
if (contains == false) {//empty有效 BaseModel.isEmpty(idIn) == false) {
2159+
throw new NotExistException(TAG + ": newSQLConfig idIn != null && (((List<?>) idIn).contains(id) == false");
21362160
}
21372161
}
21382162
}

0 commit comments

Comments
 (0)