Skip to content

Commit

Permalink
解决 JOIN ON 中用 @combine 等情况下预编译值与 SQL 中 ? 占位符顺序对不上导致的异常
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyLemon committed Mar 20, 2022
1 parent 9776408 commit 12738bf
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2463,8 +2463,10 @@ protected String parseCombineExpression(RequestMethod method, String quote, Stri

int n = s.length();
if (n > 0) {
setPreparedValueList(new ArrayList<>());

if (isHaving == false) {
setPreparedValueList(new ArrayList<>()); // 必须反过来,否则 JOIN ON 内部 @combine 拼接后顺序错误
}

int maxDepth = getMaxCombineDepth();
int maxCombineCount = getMaxCombineCount();
int maxCombineKeyCount = getMaxCombineKeyCount();
Expand Down Expand Up @@ -2675,13 +2677,17 @@ else if (c == ')') {
if (StringUtil.isEmpty(result, true)) {
result = andCond;
}
else if (StringUtil.isNotEmpty(andCond, true)) { // andWhere 必须放后面,否则 prepared 值顺序错误
// result = "( " + result + " )" + AND + andCond;
result = andCond + AND + "( " + result + " )"; // 先暂存之前的 prepared 值,然后反向整合
if (n > 0) {
prepreadValues.addAll(getPreparedValueList());
setPreparedValueList(prepreadValues);
}
else if (StringUtil.isNotEmpty(andCond, true)) { // andCond 必须放后面,否则 prepared 值顺序错误
if (isHaving) { // HAVING 前 WHERE 已经有条件 ? 占位,不能反过来,想优化 AND 连接在最前,需要多遍历一次内部的 key,也可以 newSQLConfig 时存到 andList
result = "( " + result + " )" + AND + andCond;
}
else {
result = andCond + AND + "( " + result + " )"; // 先暂存之前的 prepared 值,然后反向整合
if (n > 0) {
prepreadValues.addAll(getPreparedValueList());
setPreparedValueList(prepreadValues);
}
}
}

return result;
Expand Down

0 comments on commit 12738bf

Please sign in to comment.