Skip to content

Commit e424085

Browse files
committed
数据和结构校验类 Structure 整合到 AbstractVerifier;AbstractParser 中 IdCallback 相关方法移到 AbstractVerifier;AbstractParser 优化代码避免 NPE;
1 parent 53f13ff commit e424085

File tree

5 files changed

+1104
-824
lines changed

5 files changed

+1104
-824
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import apijson.RequestMethod;
3535
import apijson.RequestRole;
3636
import apijson.StringUtil;
37-
import apijson.orm.AbstractSQLConfig.IdCallback;
3837
import apijson.orm.exception.ConditionErrorException;
3938
import apijson.orm.exception.ConflictException;
4039
import apijson.orm.exception.NotExistException;
@@ -44,7 +43,7 @@
4443
/**parser for parsing request to JSONObject
4544
* @author Lemon
4645
*/
47-
public abstract class AbstractParser<T> implements Parser<T>, ParserCreator<T>, VerifierCreator<T>, SQLCreator, IdCallback {
46+
public abstract class AbstractParser<T> implements Parser<T>, ParserCreator<T>, VerifierCreator<T>, SQLCreator {
4847
protected static final String TAG = "AbstractParser";
4948

5049

@@ -248,10 +247,16 @@ public AbstractParser<T> setNeedVerifyContent(boolean needVerifyContent) {
248247

249248
@Override
250249
public SQLExecutor getSQLExecutor() {
250+
if (sqlExecutor == null) {
251+
sqlExecutor = createSQLExecutor();
252+
}
251253
return sqlExecutor;
252254
}
253255
@Override
254256
public Verifier<T> getVerifier() {
257+
if (verifier == null) {
258+
verifier = createVerifier().setVisitor(getVisitor());
259+
}
255260
return verifier;
256261
}
257262

@@ -377,7 +382,7 @@ public JSONObject parseResponse(JSONObject request) {
377382
long duration = endTime - startTime;
378383

379384
if (Log.DEBUG) { //用 | 替代 /,避免 APIJSON ORM,APIAuto 等解析路径错误
380-
requestObject.put("sql:generate|cache|execute|maxExecute", sqlExecutor.getGeneratedSQLCount() + "|" + sqlExecutor.getCachedSQLCount() + "|" + sqlExecutor.getExecutedSQLCount() + "|" + getMaxSQLCount());
385+
requestObject.put("sql:generate|cache|execute|maxExecute", getSQLExecutor().getGeneratedSQLCount() + "|" + getSQLExecutor().getCachedSQLCount() + "|" + getSQLExecutor().getExecutedSQLCount() + "|" + getMaxSQLCount());
381386
requestObject.put("depth:count|max", queryDepth + "|" + getMaxQueryDepth());
382387
requestObject.put("time:start|duration|end", startTime + "|" + duration + "|" + endTime);
383388
}
@@ -402,7 +407,7 @@ public JSONObject parseResponse(JSONObject request) {
402407

403408
@Override
404409
public void onVerifyLogin() throws Exception {
405-
verifier.verifyLogin();
410+
getVerifier().verifyLogin();
406411
}
407412
@Override
408413
public void onVerifyContent() throws Exception {
@@ -427,7 +432,7 @@ public void onVerifyRole(@NotNull SQLConfig config) throws Exception {
427432
config.setRole(getVisitor().getId() == null ? RequestRole.UNKNOWN : RequestRole.LOGIN);
428433
}
429434
}
430-
verifier.verify(config);
435+
getVerifier().verifyAccess(config);
431436
}
432437

433438
}
@@ -493,20 +498,7 @@ public JSONObject parseCorrectRequest(RequestMethod method, String tag, int vers
493498

494499

495500
//JSONObject clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是 {}
496-
return Structure.parseRequest(method, name, target, request, maxUpdateCount, getGlobleDatabase(), getGlobleSchema(), this, creator);
497-
}
498-
499-
@Override
500-
public String getIdKey(String database, String schema, String table) {
501-
return apijson.JSONObject.KEY_ID;
502-
}
503-
@Override
504-
public String getUserIdKey(String database, String schema, String table) {
505-
return apijson.JSONObject.KEY_USER_ID;
506-
}
507-
@Override
508-
public Object newId(RequestMethod method, String database, String schema, String table) {
509-
return System.currentTimeMillis();
501+
return getVerifier().verifyRequest(method, name, target, request, maxUpdateCount, getGlobleDatabase(), getGlobleSchema(), creator);
510502
}
511503

512504

@@ -694,12 +686,8 @@ public JSONObject getStructure(@NotNull String table, String key, String value,
694686
config.setOrder(JSONRequest.KEY_VERSION + (version > 0 ? "+" : "-"));
695687
config.setCount(1);
696688

697-
if (sqlExecutor == null) {
698-
sqlExecutor = createSQLExecutor();
699-
}
700-
701689
//too many connections error: 不try-catch,可以让客户端看到是服务器内部异常
702-
JSONObject result = sqlExecutor.execute(config, false);
690+
JSONObject result = getSQLExecutor().execute(config, false);
703691
return getJSONObject(result, "structure");//解决返回值套了一层 "structure":{}
704692
}
705693

@@ -1405,10 +1393,10 @@ public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Except
14051393
JSONObject result;
14061394
if (explain) { //如果先执行 explain,则 execute 会死循环,所以只能先执行非 explain
14071395
config.setExplain(false); //对下面 config.getSQL(false); 生效
1408-
JSONObject res = sqlExecutor.execute(config, false);
1396+
JSONObject res = getSQLExecutor().execute(config, false);
14091397

14101398
config.setExplain(explain);
1411-
JSONObject explainResult = config.isMain() && config.getPosition() != 0 ? null : sqlExecutor.execute(config, false);
1399+
JSONObject explainResult = config.isMain() && config.getPosition() != 0 ? null : getSQLExecutor().execute(config, false);
14121400

14131401
if (explainResult == null) {
14141402
result = res;
@@ -1420,7 +1408,7 @@ public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Except
14201408
}
14211409
}
14221410
else {
1423-
result = sqlExecutor.execute(config, false);
1411+
result = getSQLExecutor().execute(config, false);
14241412
}
14251413

14261414
return parseCorrectResponse(config.getTable(), result);
@@ -1434,7 +1422,7 @@ public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Except
14341422
finally {
14351423
if (config.getPosition() == 0 && config.limitSQLCount()) {
14361424
int maxSQLCount = getMaxSQLCount();
1437-
int sqlCount = sqlExecutor.getExecutedSQLCount();
1425+
int sqlCount = getSQLExecutor().getExecutedSQLCount();
14381426
Log.d(TAG, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< \n\n\n 已执行 " + sqlCount + "/" + maxSQLCount + " 条 SQL \n\n\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
14391427
if (sqlCount > maxSQLCount) {
14401428
throw new IllegalArgumentException("截至 " + config.getTable() + " 已执行 " + sqlCount + " 条 SQL,数量已超限,必须在 0-" + maxSQLCount + " 内 !");
@@ -1458,27 +1446,27 @@ public void setTransactionIsolation(int transactionIsolation) {
14581446
@Override
14591447
public void begin(int transactionIsolation) {
14601448
Log.d("\n\n" + TAG, "<<<<<<<<<<<<<<<<<<<<<<< begin transactionIsolation = " + transactionIsolation + " >>>>>>>>>>>>>>>>>>>>>>> \n\n");
1461-
sqlExecutor.setTransactionIsolation(transactionIsolation); //不知道 connection 什么时候创建,不能在这里准确控制,sqlExecutor.begin(transactionIsolation);
1449+
getSQLExecutor().setTransactionIsolation(transactionIsolation); //不知道 connection 什么时候创建,不能在这里准确控制,getSqlExecutor().begin(transactionIsolation);
14621450
}
14631451
@Override
14641452
public void rollback() throws SQLException {
14651453
Log.d("\n\n" + TAG, "<<<<<<<<<<<<<<<<<<<<<<< rollback >>>>>>>>>>>>>>>>>>>>>>> \n\n");
1466-
sqlExecutor.rollback();
1454+
getSQLExecutor().rollback();
14671455
}
14681456
@Override
14691457
public void rollback(Savepoint savepoint) throws SQLException {
14701458
Log.d("\n\n" + TAG, "<<<<<<<<<<<<<<<<<<<<<<< rollback savepoint " + (savepoint == null ? "" : "!") + "= null >>>>>>>>>>>>>>>>>>>>>>> \n\n");
1471-
sqlExecutor.rollback(savepoint);
1459+
getSQLExecutor().rollback(savepoint);
14721460
}
14731461
@Override
14741462
public void commit() throws SQLException {
14751463
Log.d("\n\n" + TAG, "<<<<<<<<<<<<<<<<<<<<<<< commit >>>>>>>>>>>>>>>>>>>>>>> \n\n");
1476-
sqlExecutor.commit();
1464+
getSQLExecutor().commit();
14771465
}
14781466
@Override
14791467
public void close() {
14801468
Log.d("\n\n" + TAG, "<<<<<<<<<<<<<<<<<<<<<<< close >>>>>>>>>>>>>>>>>>>>>>> \n\n");
1481-
sqlExecutor.close();
1469+
getSQLExecutor().close();
14821470
}
14831471

14841472
/**开始事务
@@ -1535,6 +1523,7 @@ protected void onClose() {
15351523
// Log.d(TAG, "onClose >>");
15361524

15371525
close();
1526+
verifier = null;
15381527
sqlExecutor = null;
15391528
queryResultMap.clear();
15401529
queryResultMap = null;

0 commit comments

Comments
 (0)