Skip to content

Commit f318d2b

Browse files
committed
Server:解决全局的@database@Schema对 JOIN 的副表在解析生成 JOIN 副表语句时未生效,导致 SQL 内主表和副表引号不一致而报错
1 parent 0e5ae86 commit f318d2b

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ public AbstractObjectParser parse() throws Exception {
234234
response = new JSONObject(true);//must init
235235

236236
sqlRequest = new JSONObject(true);//must init
237+
237238
sqlReponse = null;//must init
238239
customMap = null;//must init
239240
functionMap = null;//must init
@@ -312,6 +313,15 @@ else if (method == PUT && value instanceof JSONArray
312313

313314
//非Table内的函数会被滞后在onChildParse后调用! onFunctionResponse("-");
314315
}
316+
317+
if (isTable) {
318+
if (sqlRequest.get(JSONRequest.KEY_DATABASE) == null && parser.getGlobleDatabase() != null) {
319+
sqlRequest.put(JSONRequest.KEY_DATABASE, parser.getGlobleDatabase());
320+
}
321+
if (sqlRequest.get(JSONRequest.KEY_SCHEMA) == null && parser.getGlobleSchema() != null) {
322+
sqlRequest.put(JSONRequest.KEY_SCHEMA, parser.getGlobleSchema());
323+
}
324+
}
315325
}
316326

317327
if (isInvalidate()) {
@@ -488,7 +498,7 @@ public JSON onChildParse(int index, String key, JSONObject value) throws Excepti
488498
throw new IllegalArgumentException(parentPath + "/" + key + ":{} 不合法!"
489499
+ "数组 []:{} 中第一个 key:{} 必须是主表 TableKey:{} !不能为 arrayKey[]:{} !");
490500
}
491-
501+
492502
if (arrayConfig == null || arrayConfig.getPosition() == 0) {
493503
arrayCount ++;
494504
int maxArrayCount = parser.getMaxArrayCount();
@@ -515,7 +525,7 @@ public JSON onChildParse(int index, String key, JSONObject value) throws Excepti
515525
throw new IllegalArgumentException(path + " 内截至 " + key + ":{} 时对象 key:{} 的数量达到 " + objectCount + " 已超限,必须在 0-" + maxObjectCount + " 内 !");
516526
}
517527
}
518-
528+
519529
child = parser.onObjectParse(value, path, key, isMain ? arrayConfig.setType(SQLConfig.TYPE_ITEM_CHILD_0) : null, isSubquery);
520530

521531
isEmpty = child == null || ((JSONObject) child).isEmpty();

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,37 @@ public AbstractParser<T> setGlobleRole(RequestRole globleRole) {
134134
this.globleRole = globleRole;
135135
return this;
136136
}
137+
@Override
138+
public RequestRole getGlobleRole() {
139+
return globleRole;
140+
}
137141
protected String globleDatabase;
138142
public AbstractParser<T> setGlobleDatabase(String globleDatabase) {
139143
this.globleDatabase = globleDatabase;
140144
return this;
141145
}
146+
@Override
147+
public String getGlobleDatabase() {
148+
return globleDatabase;
149+
}
142150
protected String globleSchema;
143151
public AbstractParser<T> setGlobleSchema(String globleSchema) {
144152
this.globleSchema = globleSchema;
145153
return this;
146154
}
147-
protected boolean globleFormat;
155+
@Override
156+
public String getGlobleSchema() {
157+
return globleSchema;
158+
}
159+
protected Boolean globleFormat;
148160
public AbstractParser<T> setGlobleFormat(Boolean globleFormat) {
149161
this.globleFormat = globleFormat;
150162
return this;
151163
}
164+
@Override
165+
public Boolean getGlobleFormat() {
166+
return globleFormat;
167+
}
152168

153169
@Override
154170
public boolean isNoVerify() {
@@ -290,7 +306,7 @@ public JSONObject parseResponse(JSONObject request) {
290306
try {
291307
setGlobleDatabase(requestObject.getString(JSONRequest.KEY_DATABASE));
292308
setGlobleSchema(requestObject.getString(JSONRequest.KEY_SCHEMA));
293-
setGlobleFormat(requestObject.getBooleanValue(JSONRequest.KEY_FORMAT));
309+
setGlobleFormat(requestObject.getBoolean(JSONRequest.KEY_FORMAT));
294310

295311
requestObject.remove(JSONRequest.KEY_DATABASE);
296312
requestObject.remove(JSONRequest.KEY_SCHEMA);
@@ -316,7 +332,7 @@ public JSONObject parseResponse(JSONObject request) {
316332

317333
requestObject = error == null ? extendSuccessResult(requestObject) : extendErrorResult(requestObject, error);
318334

319-
JSONObject res = globleFormat && JSONResponse.isSuccess(requestObject) ? new JSONResponse(requestObject) : requestObject;
335+
JSONObject res = (globleFormat != null && globleFormat) && JSONResponse.isSuccess(requestObject) ? new JSONResponse(requestObject) : requestObject;
320336

321337
long endTime = System.currentTimeMillis();
322338
long duration = endTime - startTime;
@@ -363,14 +379,6 @@ public void onVerifyContent() throws Exception {
363379
*/
364380
@Override
365381
public void onVerifyRole(@NotNull SQLConfig config) throws Exception {
366-
//居然导致 @JSONField(serialize = false) 的方法也被执行了,然后 getTablePath 里 对 sch 赋值了 Log.i(TAG, "executeSQL config = " + JSON.toJSONString(config));
367-
if (config.getDatabase() == null && globleDatabase != null) {
368-
config.setDatabase(globleDatabase);
369-
}
370-
if (config.getSchema() == null && globleSchema != null) {
371-
config.setSchema(globleSchema);
372-
}
373-
374382
if (Log.DEBUG) {
375383
Log.i(TAG, "onVerifyRole config = " + JSON.toJSONString(config));
376384
}

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,11 @@ public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table,
20752075
}
20762076
AbstractSQLConfig config = callback.getSQLConfig(method, table);
20772077

2078+
String database = request.getString(KEY_DATABASE);
2079+
String schema = request.getString(KEY_SCHEMA);
2080+
config.setDatabase(database); //不删,后面表对象还要用的,必须放在 parseJoin 前
2081+
config.setSchema(schema); //不删,后面表对象还要用的
2082+
20782083
//放后面会导致主表是空对象时 joinList 未解析
20792084
if (isProcedure == false) {
20802085
config = parseJoin(method, config, joinList, callback);
@@ -2084,11 +2089,6 @@ public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table,
20842089
return config; //request.remove(key); 前都可以直接return,之后必须保证 put 回去
20852090
}
20862091

2087-
String database = request.getString(KEY_DATABASE);
2088-
String schema = request.getString(KEY_SCHEMA);
2089-
config.setDatabase(database); //不删,后面表对象还要用的
2090-
config.setSchema(schema); //不删,后面表对象还要用的
2091-
20922092
if (isProcedure) {
20932093
return config;
20942094
}
@@ -2388,6 +2388,18 @@ public static AbstractSQLConfig parseJoin(RequestMethod method, AbstractSQLConfi
23882388
SQLConfig cacheConfig = newSQLConfig(method, name, j.getTable(), null, false, callback).setCount(1);
23892389

23902390
if (j.isAppJoin() == false) { //除了 @ APP JOIN,其它都是 SQL JOIN,则副表要这样配置
2391+
if (joinConfig.getDatabase() == null) {
2392+
joinConfig.setDatabase(config.getDatabase()); //解决主表 JOIN 副表,引号不一致
2393+
}
2394+
else if (joinConfig.getDatabase().equals(config.getDatabase()) == false) {
2395+
throw new IllegalArgumentException("主表 " + config.getTable() + " 的 @database:" + config.getDatabase() + " 和它 SQL JOIN 的副表 " + name + " 的 @database:" + joinConfig.getDatabase() + " 不一致!");
2396+
}
2397+
if (joinConfig.getSchema() == null) {
2398+
joinConfig.setSchema(config.getSchema()); //主表 JOIN 副表,默认 schema 一致
2399+
}
2400+
cacheConfig.setDatabase(joinConfig.getDatabase()).setSchema(joinConfig.getSchema()); //解决主表 JOIN 副表,引号不一致
2401+
2402+
23912403
if (isQuery) {
23922404
config.setKeyPrefix(true);
23932405
}
@@ -2396,7 +2408,7 @@ public static AbstractSQLConfig parseJoin(RequestMethod method, AbstractSQLConfi
23962408

23972409
if (j.isLeftOrRightJoin()) {
23982410
SQLConfig outterConfig = newSQLConfig(method, name, j.getOutter(), null, false, callback);
2399-
outterConfig.setMain(false).setKeyPrefix(true);
2411+
outterConfig.setMain(false).setKeyPrefix(true).setDatabase(joinConfig.getDatabase()).setSchema(joinConfig.getSchema()); //解决主表 JOIN 副表,引号不一致
24002412
j.setOutterConfig(outterConfig);
24012413
}
24022414
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import zuo.biao.apijson.NotNull;
2121
import zuo.biao.apijson.RequestMethod;
22+
import zuo.biao.apijson.RequestRole;
2223

2324
/**解析器
2425
* @author Lemon
@@ -125,5 +126,11 @@ public interface Parser<T> {
125126

126127
SQLExecutor getSQLExecutor();
127128
Verifier<T> getVerifier();
129+
130+
131+
RequestRole getGlobleRole();
132+
String getGlobleDatabase();
133+
String getGlobleSchema();
134+
Boolean getGlobleFormat();
128135

129136
}

0 commit comments

Comments
 (0)