Skip to content

Commit 1774237

Browse files
committed
Server:executeSQL.executeSQL内抽取出onVerifyRole;ObjectParser.executeSQL内抽取出setSQLConfig
1 parent 82abf30 commit 1774237

6 files changed

Lines changed: 99 additions & 57 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public JSONObject parseResponse(JSONRequest request) throws Exception {
122122

123123

124124
@Override
125-
protected void onVerifyContent() throws Exception {
125+
public void onVerifyContent() throws Exception {
126126
//补充全局缺省版本号 //可能在默认为1的前提下这个请求version就需要为0 requestObject.getIntValue(JSONRequest.KEY_VERSION) <= 0) {
127127
if (session != null && requestObject.get(JSONRequest.KEY_VERSION) == null) {
128128
requestObject.put(JSONRequest.KEY_VERSION, session.getAttribute(JSONRequest.KEY_VERSION));

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ public JSONObject setUserIdIn(List<Object> list) {
129129
public static final String KEY_DATABASE = "@database"; //数据库类型,默认为MySQL
130130
public static final String KEY_SCHEMA = "@schema"; //数据库,Table在非默认schema内时需要声明
131131
public static final String KEY_COLUMN = "@column"; //查询的Table字段或SQL函数
132+
public static final String KEY_FROM = "@from"; //FROM语句
133+
public static final String KEY_SQL = "@sql"; //SQL语句
132134
public static final String KEY_COMBINE = "@combine"; //条件组合,每个条件key前面可以放&,|,!逻辑关系 "id!{},&sex,!name&$"
133135
public static final String KEY_GROUP = "@group"; //分组方式
134136
public static final String KEY_HAVING = "@having"; //聚合函数条件,一般和@group一起用
@@ -141,6 +143,8 @@ public JSONObject setUserIdIn(List<Object> list) {
141143
TABLE_KEY_LIST.add(KEY_DATABASE);
142144
TABLE_KEY_LIST.add(KEY_SCHEMA);
143145
TABLE_KEY_LIST.add(KEY_COLUMN);
146+
TABLE_KEY_LIST.add(KEY_FROM);
147+
TABLE_KEY_LIST.add(KEY_SQL);
144148
TABLE_KEY_LIST.add(KEY_COMBINE);
145149
TABLE_KEY_LIST.add(KEY_GROUP);
146150
TABLE_KEY_LIST.add(KEY_HAVING);

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

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

1717
import static zuo.biao.apijson.JSONObject.KEY_COMBINE;
18+
import static zuo.biao.apijson.JSONObject.KEY_SQL;
1819
import static zuo.biao.apijson.JSONObject.KEY_CORRECT;
1920
import static zuo.biao.apijson.JSONObject.KEY_DROP;
2021
import static zuo.biao.apijson.JSONObject.KEY_TRY;
@@ -49,7 +50,7 @@
4950
* @author Lemon
5051
*/
5152
public abstract class AbstractObjectParser implements ObjectParser {
52-
private static final String TAG = "ObjectParser";
53+
private static final String TAG = "AbstractObjectParser";
5354

5455
@NotNull
5556
protected Parser<?> parser;
@@ -75,7 +76,8 @@ public AbstractObjectParser setParser(Parser<?> parser) {
7576
* TODO Parser内要不因为 非 TYPE_ITEM_CHILD_0 的Table 为空导致后续中断。
7677
*/
7778
protected final boolean drop;
78-
protected JSONObject correct;
79+
protected final JSONObject correct;
80+
protected final JSONObject sql;
7981

8082
/**for single object
8183
* @param parentPath
@@ -101,14 +103,19 @@ public AbstractObjectParser(@NotNull JSONObject request, String parentPath, Stri
101103
if (isEmpty) {
102104
this.tri = false;
103105
this.drop = false;
104-
} else {
106+
this.correct = null;
107+
this.sql = null;
108+
}
109+
else {
105110
this.tri = request.getBooleanValue(KEY_TRY);
106111
this.drop = request.getBooleanValue(KEY_DROP);
107112
this.correct = request.getJSONObject(KEY_CORRECT);
113+
this.sql = request.getJSONObject(KEY_SQL);
108114

109115
request.remove(KEY_TRY);
110116
request.remove(KEY_DROP);
111117
request.remove(KEY_CORRECT);
118+
request.remove(KEY_SQL);
112119

113120
try {
114121
parseCorrect();
@@ -121,8 +128,8 @@ public AbstractObjectParser(@NotNull JSONObject request, String parentPath, Stri
121128
}
122129

123130

124-
Log.d(TAG, "ObjectParser table = " + table + "; isTable = " + isTable);
125-
Log.d(TAG, "ObjectParser isEmpty = " + isEmpty + "; tri = " + tri + "; drop = " + drop);
131+
Log.d(TAG, "AbstractObjectParser table = " + table + "; isTable = " + isTable);
132+
Log.d(TAG, "AbstractObjectParser isEmpty = " + isEmpty + "; tri = " + tri + "; drop = " + drop);
126133
}
127134

128135
public static final Map<String, Pattern> COMPILE_MAP;
@@ -162,8 +169,7 @@ public AbstractObjectParser parseCorrect() throws Exception {
162169
}
163170

164171
if (rk == null) {
165-
throw new IllegalArgumentException(
166-
"格式错误!找不到 " + k + ":" + value + " 对应[" + v + "]内的任何一项!");
172+
throw new IllegalArgumentException("格式错误!找不到 " + k + ":" + value + " 对应[" + v + "]内的任何一项!");
167173
}
168174
request.put(rk, request.remove(k));
169175
corrected.put(k, rk);
@@ -523,16 +529,32 @@ public void onPUTArrayParse(@NotNull String key, @NotNull JSONArray array) throw
523529

524530
}
525531

526-
527-
/**SQL查询,for single object
528-
* @return {@link #executeSQL(int, int, int)}
532+
/**SQL 配置,for single object
533+
* @return {@link #setSQLConfig(int, int, int)}
529534
* @throws Exception
530535
*/
531536
@Override
532-
public AbstractObjectParser executeSQL() throws Exception {
533-
return executeSQL(1, 0, 0);
537+
public AbstractObjectParser setSQLConfig() throws Exception {
538+
return setSQLConfig(1, 0, 0);
539+
}
540+
541+
@Override
542+
public AbstractObjectParser setSQLConfig(int count, int page, int position) throws Exception {
543+
if (isTable == false) {
544+
return this;
545+
}
546+
547+
if (sqlConfig == null) {
548+
sqlConfig = newSQLConfig();
549+
}
550+
sqlConfig.setCount(count).setPage(page).setPosition(position);
551+
552+
parser.onVerifyRole(sqlConfig);
553+
554+
return this;
534555
}
535556

557+
536558
protected SQLConfig sqlConfig = null;//array item复用
537559
/**SQL查询,for array item
538560
* @param count
@@ -542,17 +564,12 @@ public AbstractObjectParser executeSQL() throws Exception {
542564
* @throws Exception
543565
*/
544566
@Override
545-
public AbstractObjectParser executeSQL(int count, int page, int position) throws Exception {
567+
public AbstractObjectParser executeSQL() throws Exception {
546568
//执行SQL操作数据库
547569
if (isTable == false) {//提高性能
548570
sqlReponse = new JSONObject(sqlRequest);
549571
} else {
550-
551572
try {
552-
if (sqlConfig == null) {
553-
sqlConfig = newSQLConfig();
554-
}
555-
sqlConfig.setCount(count).setPage(page).setPosition(position);
556573
sqlReponse = onSQLExecute();
557574
} catch (Exception e) {
558575
Log.e(TAG, "getObject try { response = getSQLObject(config2); } catch (Exception e) {");
@@ -621,11 +638,11 @@ public void onFunctionResponse(String type) throws Exception {
621638
//解析函数function
622639
Set<Entry<String, String>> functionSet = map == null ? null : map.entrySet();
623640
if (functionSet != null && functionSet.isEmpty() == false) {
624-
// JSONObject json = "-".equals(type) ? request : response; // key-():function 是实时执行,而不是在这里批量执行
641+
// JSONObject json = "-".equals(type) ? request : response; // key-():function 是实时执行,而不是在这里批量执行
625642

626643
for (Entry<String, String> entry : functionSet) {
627644

628-
// parseFunction(json, entry.getKey(), entry.getValue());
645+
// parseFunction(json, entry.getKey(), entry.getValue());
629646
parseFunction(response, entry.getKey(), entry.getValue());
630647
}
631648
}
@@ -696,9 +713,11 @@ public void recycle() {
696713
if (correct != null) {
697714
request.put(KEY_CORRECT, correct);
698715
}
716+
if (sql != null) {
717+
request.put(KEY_SQL, sql);
718+
}
699719

700720

701-
correct = null;
702721
corrected = null;
703722
method = null;
704723
parentPath = null;

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

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,38 @@ public JSONObject parseResponse(JSONObject request) {
319319
}
320320

321321

322-
protected void onVerifyLogin() throws Exception {
322+
@Override
323+
public void onVerifyLogin() throws Exception {
323324
verifier.verifyLogin();
324325
}
325-
protected void onVerifyContent() throws Exception {
326+
@Override
327+
public void onVerifyContent() throws Exception {
326328
requestObject = parseCorrectRequest();
327329
}
330+
/**校验角色及对应操作的权限
331+
* @param config
332+
* @return
333+
* @throws Exception
334+
*/
335+
@Override
336+
public void onVerifyRole(@NotNull SQLConfig config) throws Exception {
337+
Log.i(TAG, "executeSQL config = " + JSON.toJSONString(config));
338+
if (config.getDatabase() == null && globleDatabase != null) {
339+
config.setDatabase(globleDatabase);
340+
}
341+
342+
if (noVerifyRole == false) {
343+
if (config.getRole() == null) {
344+
if (globleRole != null) {
345+
config.setRole(globleRole);
346+
} else {
347+
config.setRole(getVisitor().getId() == null ? RequestRole.UNKNOWN : RequestRole.LOGIN);
348+
}
349+
}
350+
verifier.verify(config);
351+
}
352+
353+
}
328354

329355

330356
/**解析请求JSONObject
@@ -594,14 +620,14 @@ public JSONObject onObjectParse(final JSONObject request
594620
JSONObject response = null;
595621
if (op != null) {//TODO SQL查询结果为空时,functionMap和customMap还有没有意义?
596622
if (arrayConfig == null) {//Common
597-
response = op.executeSQL().response();
623+
response = op.setSQLConfig().executeSQL().response();
598624
} else {//Array Item Child
599625
int query = arrayConfig.getQuery();
600626

601627
//total 这里不能用arrayConfig.getType(),因为在createObjectParser.onChildParse传到onObjectParse时已被改掉
602628
if (type == SQLConfig.TYPE_ITEM_CHILD_0 && query != JSONRequest.QUERY_TABLE
603629
&& arrayConfig.getPosition() == 0) {
604-
JSONObject rp = op.setMethod(RequestMethod.HEAD).executeSQL().getSqlReponse();
630+
JSONObject rp = op.setMethod(RequestMethod.HEAD).setSQLConfig().executeSQL().getSqlReponse();
605631
if (rp != null) {
606632
int index = parentPath.lastIndexOf("]/");
607633
if (index >= 0) {
@@ -621,9 +647,10 @@ public JSONObject onObjectParse(final JSONObject request
621647
if (query == JSONRequest.QUERY_TOTAL) {
622648
response = null;//不再往后查询
623649
} else {
624-
response = op.executeSQL(
625-
arrayConfig.getCount(), arrayConfig.getPage(), arrayConfig.getPosition()
626-
).response();
650+
response = op
651+
.setSQLConfig(arrayConfig.getCount(), arrayConfig.getPage(), arrayConfig.getPosition())
652+
.executeSQL()
653+
.response();
627654
// itemConfig = op.getConfig();
628655
}
629656
}
@@ -1112,29 +1139,13 @@ public static JSONObject getJSONObject(JSONObject object, String key) {
11121139
}
11131140

11141141

1115-
/**获取数据库返回的String
1142+
/**执行 SQL 并返回 JSONObject
11161143
* @param config
11171144
* @return
11181145
* @throws Exception
11191146
*/
11201147
@Override
1121-
public synchronized JSONObject executeSQL(SQLConfig config) throws Exception {
1122-
Log.i(TAG, "executeSQL config = " + JSON.toJSONString(config));
1123-
if (noVerifyRole == false) {
1124-
if (config.getRole() == null) {
1125-
if (globleRole != null) {
1126-
config.setRole(globleRole);
1127-
} else {
1128-
config.setRole(getVisitor().getId() == null ? RequestRole.UNKNOWN : RequestRole.LOGIN);
1129-
}
1130-
}
1131-
verifier.verify(config);
1132-
}
1133-
1134-
if (config.getDatabase() == null && globleDatabase != null) {
1135-
config.setDatabase(globleDatabase);
1136-
}
1137-
1148+
public synchronized JSONObject executeSQL(@NotNull SQLConfig config) throws Exception {
11381149
return parseCorrectResponse(config.getTable(), sqlExecutor.execute(config));
11391150
}
11401151

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,25 @@ public interface ObjectParser {
8181
*/
8282
void onPUTArrayParse(@NotNull String key, @NotNull JSONArray array) throws Exception;
8383

84-
85-
/**SQL查询,for single object
86-
* @return {@link #executeSQL(int, int, int)}
84+
/**SQL 配置,for single object
85+
* @return {@link #setSQLConfig(int, int, int)}
8786
* @throws Exception
8887
*/
89-
ObjectParser executeSQL() throws Exception;
88+
ObjectParser setSQLConfig() throws Exception;
9089

91-
/**SQL查询,for array item
92-
* @param count
93-
* @param page
94-
* @param position
95-
* @return this
90+
/**SQL 配置
91+
* @return
92+
* @throws Exception
93+
*/
94+
ObjectParser setSQLConfig(int count, int page, int position) throws Exception;
95+
96+
97+
/**执行 SQL
98+
* @return
9699
* @throws Exception
97100
*/
98-
ObjectParser executeSQL(int count, int page, int position) throws Exception;
101+
ObjectParser executeSQL() throws Exception;
102+
99103

100104
/**
101105
* @return

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ public interface Parser<T> {
105105
Object getValueByPath(String valuePath);
106106

107107

108+
void onVerifyLogin() throws Exception;
109+
void onVerifyContent() throws Exception;
110+
void onVerifyRole(SQLConfig config) throws Exception;
111+
108112
JSONObject executeSQL(SQLConfig config) throws Exception;
109113

110114
}

0 commit comments

Comments
 (0)