Skip to content

Commit 67478ee

Browse files
committed
Server:优化JOIN相关代码,配置封装到Join类
1 parent cfbb978 commit 67478ee

File tree

8 files changed

+182
-77
lines changed

8 files changed

+182
-77
lines changed

APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/server/DemoObjectParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public DemoObjectParser setParser(Parser parser) {
6464

6565
@Override
6666
public SQLConfig newSQLConfig() throws Exception {
67-
return DemoSQLConfig.newSQLConfig(method, table, sqlRequest, join);
67+
return DemoSQLConfig.newSQLConfig(method, table, sqlRequest, joinList);
6868
}
6969

7070

APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/server/DemoSQLConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package apijson.demo.server;
1616

1717
import java.util.List;
18-
import java.util.Map;
1918

2019
import com.alibaba.fastjson.JSONObject;
2120

@@ -24,6 +23,7 @@
2423
import zuo.biao.apijson.RequestMethod;
2524
import zuo.biao.apijson.StringUtil;
2625
import zuo.biao.apijson.server.AbstractSQLConfig;
26+
import zuo.biao.apijson.server.Join;
2727
import zuo.biao.apijson.server.SQLConfig;
2828

2929

@@ -77,8 +77,8 @@ public DemoSQLConfig(RequestMethod method, int count, int page) {
7777
* @return
7878
* @throws Exception
7979
*/
80-
public static SQLConfig newSQLConfig(RequestMethod method, String table, JSONObject request, List<Map<String, Object>> join) throws Exception {
81-
return newSQLConfig(method, table, request, join, new Callback() {
80+
public static SQLConfig newSQLConfig(RequestMethod method, String table, JSONObject request, List<Join> joinList) throws Exception {
81+
return newSQLConfig(method, table, request, joinList, new Callback() {
8282

8383
@Override
8484
public DemoSQLConfig getSQLConfig(RequestMethod method, String table) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public AbstractObjectParser setParser(Parser parser) {
6363
protected SQLConfig arrayConfig;//不用final是为了recycle
6464

6565
protected final int type;
66-
protected final List<Map<String, Object>> join;
66+
protected final List<Join> joinList;
6767
protected final boolean isTable;
6868
protected final String path;
6969
protected final String table;
@@ -91,7 +91,7 @@ public AbstractObjectParser(@NotNull JSONObject request, String parentPath, Stri
9191
this.arrayConfig = arrayConfig;
9292

9393
this.type = arrayConfig == null ? 0 : arrayConfig.getType();
94-
this.join = arrayConfig == null ? null : arrayConfig.getJoin();
94+
this.joinList = arrayConfig == null ? null : arrayConfig.getJoinList();
9595
this.path = AbstractParser.getAbsPath(parentPath, name);
9696
this.table = Pair.parseEntry(name, true).getKey();
9797
this.isTable = zuo.biao.apijson.JSONObject.isTableKey(table);

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.ArrayList;
2121
import java.util.HashMap;
2222
import java.util.HashSet;
23-
import java.util.LinkedHashMap;
2423
import java.util.List;
2524
import java.util.Map;
2625
import java.util.Set;
@@ -673,7 +672,7 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
673672
.setCount(size)
674673
.setPage(page)
675674
.setQuery(query)
676-
.setJoin(onJoinParse(join, request));
675+
.setJoinList(onJoinParse(join, request));
677676

678677
JSONObject parent;
679678
//生成size个
@@ -725,14 +724,14 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
725724
* @return
726725
* @throws Exception
727726
*/
728-
private List<Map<String, Object>> onJoinParse(String join, JSONObject request) throws Exception {
727+
private List<Join> onJoinParse(String join, JSONObject request) throws Exception {
729728
String[] sArr = request == null || request.isEmpty() ? null : StringUtil.split(join);
730729
if (sArr == null || sArr.length <= 0) {
731730
Log.e(TAG, "doJoin sArr == null || sArr.length <= 0 >> return request;");
732731
return null;
733732
}
734733

735-
List<Map<String, Object>> joinList = new ArrayList<>();
734+
List<Join> joinList = new ArrayList<>();
736735

737736

738737
JSONObject tableObj;
@@ -754,9 +753,9 @@ private List<Map<String, Object>> onJoinParse(String join, JSONObject request) t
754753
throw new IllegalArgumentException(JSONRequest.KEY_JOIN + ":value 中value不合法!"
755754
+ "必须为 &/Table0/key0,</Table1/key1,... 这种形式!");
756755
}
757-
String type = path.substring(0, index); //& | ! < > ( ) <> () *
758-
if (StringUtil.isEmpty(type, true)) {
759-
type = "|"; // FULL JOIN / UNIOIN
756+
String joinType = path.substring(0, index); //& | ! < > ( ) <> () *
757+
if (StringUtil.isEmpty(joinType, true)) {
758+
joinType = "|"; // FULL JOIN / UNIOIN
760759
}
761760
path = path.substring(index + 1);
762761

@@ -795,15 +794,15 @@ private List<Map<String, Object>> onJoinParse(String join, JSONObject request) t
795794
}
796795
targetObj.put(key+"@", targetObj.remove(key+"@")); //保证和SQLExcecutor缓存的Config里where顺序一致,生成的SQL也就一致
797796

798-
Map<String, Object> joinMap = new LinkedHashMap<>();
799-
joinMap.put("type", type); //TODO 要么减少数量,要么封装为一个对象
800-
joinMap.put("key", key);
801-
joinMap.put("targetKey", targetKey);
802-
joinMap.put("targetTable", targetTable);
803-
joinMap.put("name", table);
804-
joinMap.put("table", getJoinObject(table, tableObj, key));
797+
Join j = new Join();
798+
j.setJoinType(joinType);
799+
j.setTable(getJoinObject(table, tableObj, key));
800+
j.setName(table);
801+
j.setKey(key);
802+
j.setTargetName(targetTable);
803+
j.setTargetKey(targetKey);
805804

806-
joinList.add(joinMap);
805+
joinList.add(j);
807806

808807
// onList.add(table + "." + key + " = " + targetTable + "." + targetKey); // ON User.id = Moment.userId
809808

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

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public abstract class AbstractSQLConfig implements SQLConfig {
105105
private int position; //Table在[]中的位置
106106
private int query; //JSONRequest.query
107107
private int type; //ObjectParser.type
108-
private List<Map<String, Object>> join; //join
108+
private List<Join> joinList; //joinList
109109
//array item >>>>>>>>>>
110110
private boolean test; //测试
111111
private boolean cacheStatic; //静态缓存
@@ -215,7 +215,7 @@ public String getTable() {
215215
@Override
216216
public String getSQLTable() {
217217
return (TABLE_KEY_MAP.containsKey(table) ? TABLE_KEY_MAP.get(table) : table)
218-
+ ( join != null && join.isEmpty() == false //副表已经在 parseJoin 里加了 AS
218+
+ ( joinList != null && joinList.isEmpty() == false //副表已经在 parseJoin 里加了 AS
219219
&& isMain() && RequestMethod.isQueryMethod(method) ? " AS " + getAlias() : "");
220220
}
221221
@JSONField(serialize = false)
@@ -394,10 +394,10 @@ public String getColumnString() throws Exception {
394394
case GETS:
395395
boolean isQuery = RequestMethod.isQueryMethod(method);
396396
String joinColumn = "";
397-
if (isQuery && join != null) {
397+
if (isQuery && joinList != null) {
398398
SQLConfig c;
399-
for (Map<String, Object> map : join) {
400-
c = (SQLConfig) map.get("config");
399+
for (Join j : joinList) {
400+
c = j.getJoinConfig();
401401
c.setAlias(c.getTable());
402402
joinColumn += ((AbstractSQLConfig) c).getColumnString();
403403
}
@@ -541,12 +541,12 @@ public AbstractSQLConfig setType(int type) {
541541
return this;
542542
}
543543
@Override
544-
public List<Map<String, Object>> getJoin() {
545-
return join;
544+
public List<Join> getJoinList() {
545+
return joinList;
546546
}
547547
@Override
548-
public SQLConfig setJoin(List<Map<String, Object>> join) {
549-
this.join = join;
548+
public SQLConfig setJoinList(List<Join> joinList) {
549+
this.joinList = joinList;
550550
return this;
551551
}
552552

@@ -1301,13 +1301,13 @@ private static String getConditionString(String column, String table, AbstractSQ
13011301

13021302
public String getJoinString() {
13031303
String joinOns = "";
1304-
if (join != null) {
1305-
String j;
1304+
if (joinList != null) {
1305+
String sql;
13061306
boolean first = true;
1307-
for (Map<String, Object> map : join) {
1308-
j = (String) map.get("join");
1309-
if (j != null) {
1310-
joinOns += (first ? "" : AND) + j;
1307+
for (Join j: joinList) {
1308+
sql = j.getJoinSQL();
1309+
if (sql != null) {
1310+
joinOns += (first ? "" : AND) + sql;
13111311
first = false;
13121312
}
13131313
}
@@ -1321,7 +1321,7 @@ public String getJoinString() {
13211321
* @return
13221322
* @throws Exception
13231323
*/
1324-
public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table, JSONObject request, List<Map<String, Object>> join, Callback callback) throws Exception {
1324+
public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table, JSONObject request, List<Join> joinList, Callback callback) throws Exception {
13251325
if (request == null) { // User:{} 这种空内容在查询时也有效
13261326
throw new NullPointerException(TAG + ": newSQLConfig request == null!");
13271327
}
@@ -1545,7 +1545,7 @@ else if (whereList != null && whereList.contains(key)) {
15451545
config.setGroup(group);
15461546
config.setHaving(having);
15471547
config.setOrder(order);
1548-
config.setJoin(parseJoin(method, join, callback));
1548+
config.setJoinList(parseJoin(method, joinList, callback));
15491549

15501550
//TODO 解析JOIN,包括 @column,@group 等要合并
15511551

@@ -1565,49 +1565,35 @@ else if (whereList != null && whereList.contains(key)) {
15651565
return config;
15661566
}
15671567

1568-
public static List<Map<String, Object>> parseJoin(RequestMethod method, List<Map<String, Object>> join, Callback callback) throws Exception {
1568+
public static List<Join> parseJoin(RequestMethod method, List<Join> joinList, Callback callback) throws Exception {
15691569

15701570
//TODO 解析出 SQLConfig 再合并 column, order, group 等
1571-
if (join == null || join.isEmpty()) {
1571+
if (joinList == null || joinList.isEmpty()) {
15721572
return null;
15731573
}
15741574

15751575

15761576
String name;
1577-
for (Map<String, Object> map : join) {
1578-
name = (String) map.get("name");
1579-
SQLConfig childConfig = newSQLConfig(method, name, (JSONObject) map.get("table"), null, callback);
1580-
SQLConfig childConfig2 = newSQLConfig(method, name, (JSONObject) map.get("table"), null, callback);
1581-
childConfig.setMain(false).setCount(1).setPage(0).setPosition(0);
1582-
childConfig2.setCount(1).setPage(0).setPosition(0);
1583-
1584-
// <"INNER JOIN User ON User.id = Moment.userId", UserConfig>
1585-
map.put("join", getJoinType((String) map.get("type")) + " " + childConfig.getTablePath() + " AS "
1586-
+ name + " ON " + childConfig.getTable() + "." + (String) map.get("key") + " = "
1587-
+ (String) map.get("targetTable") + "." + (String) map.get("targetKey"));
1588-
map.put("config", childConfig);
1589-
map.put("config2", childConfig2);
1590-
}
1591-
1592-
return join;
1593-
}
1594-
1595-
private static String getJoinType(String type) {
1596-
switch (type) {
1597-
case "&":
1598-
return " INNER JOIN ";
1599-
case "<":
1600-
return " LEFT JOIN ";
1601-
case ">":
1602-
return " RIGIHT JOIN ";
1603-
default:
1604-
return " FULL JOIN ";
1577+
for (Join j : joinList) {
1578+
name = j.getName();
1579+
SQLConfig joinConfig = newSQLConfig(method, name, j.getTable(), null, callback).setMain(false);
1580+
SQLConfig cacheConfig = newSQLConfig(method, name, j.getTable(), null, callback).setCount(1);
1581+
1582+
j.setJoinConfig(joinConfig);
1583+
j.setCacheConfig(cacheConfig);
1584+
// <"INNER JOIN User ON User.id = Moment.userId", UserConfig> TODO AS 放 getSQLTable 内
1585+
j.setJoinSQL(j.getJoinTypeName() + " " + joinConfig.getTablePath() + " AS "
1586+
+ name + " ON " + joinConfig.getTable() + "." + j.getKey() + " = "
1587+
+ j.getTargetName() + "." + j.getTargetKey());
16051588
}
1589+
1590+
return joinList;
16061591
}
16071592

16081593

16091594

16101595

1596+
16111597
/**获取客户端实际需要的key
16121598
* verifyName = true
16131599
* @param method

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.sql.SQLException;
2020
import java.sql.Timestamp;
2121
import java.util.HashMap;
22-
import java.util.LinkedHashMap;
2322
import java.util.List;
2423
import java.util.Map;
2524
import java.util.Map.Entry;
@@ -282,14 +281,14 @@ protected JSONObject onPutColumn(@NotNull SQLConfig config, @NotNull ResultSet r
282281

283282
//<sql, Table>
284283

285-
List<Map<String, Object>> join = config.getJoin();
286-
if (join != null) {
287-
for (Map<String, Object> m : join) {
288-
if (childTable.equals(m.get("name"))) {
284+
List<Join> joinList = config.getJoinList();
285+
if (joinList != null) {
286+
for (Join j : joinList) {
287+
if (childTable.equals(j.getName())) {
289288

290-
childConfig = (SQLConfig) m.get("config2"); //这里用config改了getSQL后再还原很麻烦,所以提前给一个config2更好
289+
childConfig = j.getCacheConfig(); //这里用config改了getSQL后再还原很麻烦,所以提前给一个config2更好
291290

292-
childConfig.putWhere((String) m.get("key"), table.get(m.get("targetKey")));
291+
childConfig.putWhere(j.getKey(), table.get(j.getTargetKey()));
293292
childSql = childConfig.getSQL(false);
294293

295294
if (StringUtil.isEmpty(childSql, true)) {

0 commit comments

Comments
 (0)