Skip to content

Commit d0130bc

Browse files
committed
对 SQLConfig 传入 Parser, ObjectParser, version, tag 等,拿到上下文信息来实现某些需求
1 parent d325d63 commit d0130bc

File tree

4 files changed

+83
-8
lines changed

4 files changed

+83
-8
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,9 @@ public void onTableArrayParse(String key, JSONArray valueArray) throws Exception
799799
@Override
800800
public JSONObject parseResponse(RequestMethod method, String table, String alias
801801
, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception {
802-
SQLConfig config = newSQLConfig(method, table, alias, request, joinList, isProcedure);
802+
SQLConfig config = newSQLConfig(method, table, alias, request, joinList, isProcedure)
803+
.setParser(parser)
804+
.setObjectParser(this);
803805
return parseResponse(config, isProcedure);
804806
}
805807
@Override
@@ -813,7 +815,9 @@ public JSONObject parseResponse(SQLConfig config, boolean isProcedure) throws Ex
813815

814816
@Override
815817
public SQLConfig newSQLConfig(boolean isProcedure) throws Exception {
816-
return newSQLConfig(method, table, alias, sqlRequest, joinList, isProcedure);
818+
return newSQLConfig(method, table, alias, sqlRequest, joinList, isProcedure)
819+
.setParser(parser)
820+
.setObjectParser(this);
817821
}
818822

819823
/**SQL 配置,for single object
@@ -836,10 +840,10 @@ public AbstractObjectParser setSQLConfig(int count, int page, int position) thro
836840
sqlConfig = newSQLConfig(false);
837841
}
838842
catch (Exception e) {
839-
if (e instanceof NotExistException || (e instanceof CommonException && e.getCause() instanceof NotExistException)) {
840-
return this;
841-
}
842-
throw e;
843+
if (e instanceof NotExistException || (e instanceof CommonException && e.getCause() instanceof NotExistException)) {
844+
return this;
845+
}
846+
throw e;
843847
}
844848
}
845849
sqlConfig.setCount(sqlConfig.getCount() <= 0 ? count : sqlConfig.getCount()).setPage(page).setPosition(position);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,10 @@ public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Except
18951895
return null;
18961896
}
18971897

1898+
config.setParser(this);
1899+
config.setVersion(getVersion());
1900+
config.setTag(getTag());
1901+
18981902
if (isSubquery) {
18991903
JSONObject sqlObj = new JSONObject(true);
19001904
sqlObj.put(KEY_CONFIG, config);
@@ -1940,7 +1944,7 @@ public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Except
19401944
return result;
19411945
}
19421946
catch (Exception e) {
1943-
throw CommonException.wrap(e, config);
1947+
throw CommonException.wrap(e, config);
19441948
}
19451949
finally {
19461950
if (config.getPosition() == 0 && config.limitSQLCount()) {

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,56 @@ public abstract class AbstractSQLConfig implements SQLConfig {
771771

772772
}
773773

774+
private Parser<?> parser;
775+
@Override
776+
public Parser<?> getParser() {
777+
return parser;
778+
}
779+
@Override
780+
public AbstractSQLConfig setParser(Parser<?> parser) {
781+
this.parser = parser;
782+
return this;
783+
}
784+
785+
private ObjectParser objectParser;
786+
@Override
787+
public ObjectParser getObjectParser() {
788+
return objectParser;
789+
}
790+
@Override
791+
public AbstractSQLConfig setObjectParser(ObjectParser objectParser) {
792+
this.objectParser = objectParser;
793+
return this;
794+
}
795+
796+
private int version;
797+
@Override
798+
public int getVersion() {
799+
if (version <= 0 && parser != null) {
800+
version = parser.getVersion();
801+
}
802+
return version;
803+
}
804+
@Override
805+
public AbstractSQLConfig setVersion(int version) {
806+
this.version = version;
807+
return this;
808+
}
809+
810+
private String tag;
811+
@Override
812+
public String getTag() {
813+
if (StringUtil.isEmpty(tag) && parser != null) {
814+
tag = parser.getTag();
815+
}
816+
return tag;
817+
}
818+
@Override
819+
public AbstractSQLConfig setTag(String tag) {
820+
this.tag = tag;
821+
return this;
822+
}
823+
774824
// mysql8版本以上,子查询支持with as表达式
775825
private List<String> withAsExprSqlList = null;
776826
protected List<Object> withAsExprPreparedValueList = new ArrayList<>();
@@ -4734,7 +4784,8 @@ protected void onGetCrossJoinString(Join j) throws UnsupportedOperationException
47344784
* @return
47354785
* @throws Exception
47364786
*/
4737-
public static <T extends Object> SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure, Callback<T> callback) throws Exception {
4787+
public static <T extends Object> SQLConfig newSQLConfig(RequestMethod method, String table, String alias
4788+
, JSONObject request, List<Join> joinList, boolean isProcedure, Callback<T> callback) throws Exception {
47384789
if (request == null) { // User:{} 这种空内容在查询时也有效
47394790
throw new NullPointerException(TAG + ": newSQLConfig request == null!");
47404791
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ public interface SQLConfig {
4848
int TYPE_ITEM = 1;
4949
int TYPE_ITEM_CHILD_0 = 2;
5050

51+
Parser<?> getParser();
52+
53+
AbstractSQLConfig setParser(Parser<?> parser);
54+
55+
ObjectParser getObjectParser();
56+
57+
AbstractSQLConfig setObjectParser(ObjectParser objectParser);
58+
59+
int getVersion();
60+
61+
AbstractSQLConfig setVersion(int version);
62+
63+
String getTag();
64+
65+
AbstractSQLConfig setTag(String tag);
66+
5167
boolean isMySQL();
5268
boolean isPostgreSQL();
5369
boolean isSQLServer();

0 commit comments

Comments
 (0)