| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 53f13ff commit e424085
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,7 +34,6 @@ | |||
| 34 | 34 | import apijson.RequestMethod; | |
| 35 | 35 | import apijson.RequestRole; | |
| 36 | 36 | import apijson.StringUtil; | |
| 37 | - import apijson.orm.AbstractSQLConfig.IdCallback; | ||
| 38 | 37 | import apijson.orm.exception.ConditionErrorException; | |
| 39 | 38 | import apijson.orm.exception.ConflictException; | |
| 40 | 39 | import apijson.orm.exception.NotExistException; | |
@@ -44,7 +43,7 @@ | |||
| 44 | 43 | /**parser for parsing request to JSONObject | |
| 45 | 44 | * @author Lemon | |
| 46 | 45 | */ | |
| 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 { | ||
| 48 | 47 | protected static final String TAG = "AbstractParser"; | |
| 49 | 48 | ||
| 50 | 49 | ||
@@ -248,10 +247,16 @@ public AbstractParser<T> setNeedVerifyContent(boolean needVerifyContent) { | |||
| 248 | 247 | ||
| 249 | 248 | @Override | |
| 250 | 249 | public SQLExecutor getSQLExecutor() { | |
| 250 | + if (sqlExecutor == null) { | ||
| 251 | + sqlExecutor = createSQLExecutor(); | ||
| 252 | + } | ||
| 251 | 253 | return sqlExecutor; | |
| 252 | 254 | } | |
| 253 | 255 | @Override | |
| 254 | 256 | public Verifier<T> getVerifier() { | |
| 257 | + if (verifier == null) { | ||
| 258 | + verifier = createVerifier().setVisitor(getVisitor()); | ||
| 259 | + } | ||
| 255 | 260 | return verifier; | |
| 256 | 261 | } | |
| 257 | 262 | ||
@@ -377,7 +382,7 @@ public JSONObject parseResponse(JSONObject request) { | |||
| 377 | 382 | long duration = endTime - startTime; | |
| 378 | 383 | ||
| 379 | 384 | 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()); | ||
| 381 | 386 | requestObject.put("depth:count|max", queryDepth + "|" + getMaxQueryDepth()); | |
| 382 | 387 | requestObject.put("time:start|duration|end", startTime + "|" + duration + "|" + endTime); | |
| 383 | 388 | } | |
@@ -402,7 +407,7 @@ public JSONObject parseResponse(JSONObject request) { | |||
| 402 | 407 | ||
| 403 | 408 | @Override | |
| 404 | 409 | public void onVerifyLogin() throws Exception { | |
| 405 | - verifier.verifyLogin(); | ||
| 410 | + getVerifier().verifyLogin(); | ||
| 406 | 411 | } | |
| 407 | 412 | @Override | |
| 408 | 413 | public void onVerifyContent() throws Exception { | |
@@ -427,7 +432,7 @@ public void onVerifyRole(@NotNull SQLConfig config) throws Exception { | |||
| 427 | 432 | config.setRole(getVisitor().getId() == null ? RequestRole.UNKNOWN : RequestRole.LOGIN); | |
| 428 | 433 | } | |
| 429 | 434 | } | |
| 430 | - verifier.verify(config); | ||
| 435 | + getVerifier().verifyAccess(config); | ||
| 431 | 436 | } | |
| 432 | 437 | ||
| 433 | 438 | } | |
@@ -493,20 +498,7 @@ public JSONObject parseCorrectRequest(RequestMethod method, String tag, int vers | |||
| 493 | 498 | ||
| 494 | 499 | ||
| 495 | 500 | //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); | ||
| 510 | 502 | } | |
| 511 | 503 | ||
| 512 | 504 | ||
@@ -694,12 +686,8 @@ public JSONObject getStructure(@NotNull String table, String key, String value, | |||
| 694 | 686 | config.setOrder(JSONRequest.KEY_VERSION + (version > 0 ? "+" : "-")); | |
| 695 | 687 | config.setCount(1); | |
| 696 | 688 | ||
| 697 | - if (sqlExecutor == null) { | ||
| 698 | - sqlExecutor = createSQLExecutor(); | ||
| 699 | - } | ||
| 700 | - | ||
| 701 | 689 | //too many connections error: 不try-catch,可以让客户端看到是服务器内部异常 | |
| 702 | - JSONObject result = sqlExecutor.execute(config, false); | ||
| 690 | + JSONObject result = getSQLExecutor().execute(config, false); | ||
| 703 | 691 | return getJSONObject(result, "structure");//解决返回值套了一层 "structure":{} | |
| 704 | 692 | } | |
| 705 | 693 | ||
@@ -1405,10 +1393,10 @@ public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Except | |||
| 1405 | 1393 | JSONObject result; | |
| 1406 | 1394 | if (explain) { //如果先执行 explain,则 execute 会死循环,所以只能先执行非 explain | |
| 1407 | 1395 | config.setExplain(false); //对下面 config.getSQL(false); 生效 | |
| 1408 | - JSONObject res = sqlExecutor.execute(config, false); | ||
| 1396 | + JSONObject res = getSQLExecutor().execute(config, false); | ||
| 1409 | 1397 | ||
| 1410 | 1398 | 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); | ||
| 1412 | 1400 | ||
| 1413 | 1401 | if (explainResult == null) { | |
| 1414 | 1402 | result = res; | |
@@ -1420,7 +1408,7 @@ public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Except | |||
| 1420 | 1408 | } | |
| 1421 | 1409 | } | |
| 1422 | 1410 | else { | |
| 1423 | - result = sqlExecutor.execute(config, false); | ||
| 1411 | + result = getSQLExecutor().execute(config, false); | ||
| 1424 | 1412 | } | |
| 1425 | 1413 | ||
| 1426 | 1414 | return parseCorrectResponse(config.getTable(), result); | |
@@ -1434,7 +1422,7 @@ public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Except | |||
| 1434 | 1422 | finally { | |
| 1435 | 1423 | if (config.getPosition() == 0 && config.limitSQLCount()) { | |
| 1436 | 1424 | int maxSQLCount = getMaxSQLCount(); | |
| 1437 | - int sqlCount = sqlExecutor.getExecutedSQLCount(); | ||
| 1425 | + int sqlCount = getSQLExecutor().getExecutedSQLCount(); | ||
| 1438 | 1426 | Log.d(TAG, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< \n\n\n 已执行 " + sqlCount + "/" + maxSQLCount + " 条 SQL \n\n\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); | |
| 1439 | 1427 | if (sqlCount > maxSQLCount) { | |
| 1440 | 1428 | throw new IllegalArgumentException("截至 " + config.getTable() + " 已执行 " + sqlCount + " 条 SQL,数量已超限,必须在 0-" + maxSQLCount + " 内 !"); | |
@@ -1458,27 +1446,27 @@ public void setTransactionIsolation(int transactionIsolation) { | |||
| 1458 | 1446 | @Override | |
| 1459 | 1447 | public void begin(int transactionIsolation) { | |
| 1460 | 1448 | 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); | ||
| 1462 | 1450 | } | |
| 1463 | 1451 | @Override | |
| 1464 | 1452 | public void rollback() throws SQLException { | |
| 1465 | 1453 | Log.d("\n\n" + TAG, "<<<<<<<<<<<<<<<<<<<<<<< rollback >>>>>>>>>>>>>>>>>>>>>>> \n\n"); | |
| 1466 | - sqlExecutor.rollback(); | ||
| 1454 | + getSQLExecutor().rollback(); | ||
| 1467 | 1455 | } | |
| 1468 | 1456 | @Override | |
| 1469 | 1457 | public void rollback(Savepoint savepoint) throws SQLException { | |
| 1470 | 1458 | Log.d("\n\n" + TAG, "<<<<<<<<<<<<<<<<<<<<<<< rollback savepoint " + (savepoint == null ? "" : "!") + "= null >>>>>>>>>>>>>>>>>>>>>>> \n\n"); | |
| 1471 | - sqlExecutor.rollback(savepoint); | ||
| 1459 | + getSQLExecutor().rollback(savepoint); | ||
| 1472 | 1460 | } | |
| 1473 | 1461 | @Override | |
| 1474 | 1462 | public void commit() throws SQLException { | |
| 1475 | 1463 | Log.d("\n\n" + TAG, "<<<<<<<<<<<<<<<<<<<<<<< commit >>>>>>>>>>>>>>>>>>>>>>> \n\n"); | |
| 1476 | - sqlExecutor.commit(); | ||
| 1464 | + getSQLExecutor().commit(); | ||
| 1477 | 1465 | } | |
| 1478 | 1466 | @Override | |
| 1479 | 1467 | public void close() { | |
| 1480 | 1468 | Log.d("\n\n" + TAG, "<<<<<<<<<<<<<<<<<<<<<<< close >>>>>>>>>>>>>>>>>>>>>>> \n\n"); | |
| 1481 | - sqlExecutor.close(); | ||
| 1469 | + getSQLExecutor().close(); | ||
| 1482 | 1470 | } | |
| 1483 | 1471 | ||
| 1484 | 1472 | /**开始事务 | |
@@ -1535,6 +1523,7 @@ protected void onClose() { | |||
| 1535 | 1523 | // Log.d(TAG, "onClose >>"); | |
| 1536 | 1524 | ||
| 1537 | 1525 | close(); | |
| 1526 | + verifier = null; | ||
| 1538 | 1527 | sqlExecutor = null; | |
| 1539 | 1528 | queryResultMap.clear(); | |
| 1540 | 1529 | queryResultMap = null; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments