Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改聚合语句的拼接 #311

Merged
merged 1 commit into from
Sep 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@
import static apijson.JSONObject.KEY_ROLE;
import static apijson.JSONObject.KEY_SCHEMA;
import static apijson.JSONObject.KEY_USER_ID;
import static apijson.RequestMethod.DELETE;
import static apijson.RequestMethod.GET;
import static apijson.RequestMethod.GETS;
import static apijson.RequestMethod.HEADS;
import static apijson.RequestMethod.POST;
import static apijson.RequestMethod.PUT;
import static apijson.RequestMethod.*;
import static apijson.SQL.AND;
import static apijson.SQL.NOT;
import static apijson.SQL.OR;
Expand Down Expand Up @@ -890,7 +885,7 @@ public String getOrderString(boolean hasPrefix) {
// return (hasPrefix ? " ORDER BY " : "") + StringUtil.concat(order, joinOrder, ", ");
// }

if (getCount() > 0 && (isOracle() || isSQLServer() || isDb2())) { // Oracle, SQL Server, DB2 的 OFFSET 必须加 ORDER BY
if (getCount() > 0 && (isSQLServer() || isDb2())) { // Oracle, SQL Server, DB2 的 OFFSET 必须加 ORDER BY.去掉Oracle,Oracle里面没有offset关键字

// String[] ss = StringUtil.split(order);
if (StringUtil.isEmpty(order, true)) { //SQL Server 子查询内必须指定 OFFSET 才能用 ORDER BY
Expand Down Expand Up @@ -2685,6 +2680,11 @@ public static String getSQL(AbstractSQLConfig config) throws Exception {
String column = config.getColumnString();
if (config.isOracle()) {
//When config's database is oracle,Using subquery since Oracle12 below does not support OFFSET FETCH paging syntax.
//针对oracle分组后条数的统计
if ((config.getMethod() == HEAD || config.getMethod() == HEADS)
&& StringUtil.isNotEmpty(config.getGroup(),true)){
return explain + "SELECT count(*) FROM (SELECT "+ (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config) + ") " + config.getLimitString();
}
return explain + "SELECT * FROM (SELECT "+ (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config) + ") " + config.getLimitString();
}

Expand All @@ -2693,10 +2693,9 @@ public static String getSQL(AbstractSQLConfig config) throws Exception {
}

/**获取条件SQL字符串
* @param page
* @param column
* @param table
* @param where
* @param config
* @return
* @throws Exception
*/
Expand All @@ -2708,11 +2707,21 @@ private static String getConditionString(String column, String table, AbstractSQ
table = config.getSubqueryString(from) + " AS " + config.getAliasWithQuote() + " ";
}

String condition = table + config.getJoinString() + where + (
RequestMethod.isGetMethod(config.getMethod(), true) == false ?
"" : config.getGroupString(true) + config.getHavingString(true) + config.getOrderString(true)
)
; //+ config.getLimitString();
//根据方法不同,聚合语句不同。GROUP BY 和 HAVING 可以加在 HEAD 上, HAVING 可以加在 PUT, DELETE 上,GET 全加,POST 全都不加
String aggregation = "";
if (RequestMethod.isGetMethod(config.getMethod(), true)){
aggregation = config.getGroupString(true) + config.getHavingString(true) +
config.getOrderString(true);
}
if (RequestMethod.isHeadMethod(config.getMethod(), true)){
aggregation = config.getGroupString(true) + config.getHavingString(true) ;
}
if (config.getMethod() == PUT || config.getMethod() == DELETE){
aggregation = config.getHavingString(true) ;
}

String condition = table + config.getJoinString() + where + aggregation;
; //+ config.getLimitString();

//no need to optimize
// if (config.getPage() <= 0 || ID.equals(column.trim())) {
Expand Down Expand Up @@ -2749,7 +2758,6 @@ private static String getConditionString(String column, String table, AbstractSQ
// return table + " AS t0 INNER JOIN (SELECT id FROM " + condition + ") AS t1 ON t0.id = t1.id";
}


private boolean keyPrefix;
@Override
public boolean isKeyPrefix() {
Expand Down