Skip to content

Commit 2e6d116

Browse files
authored
Merge pull request #311 from LiXinnnnnn/master
修改聚合语句的拼接
2 parents 3a73d7d + 60a305c commit 2e6d116

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

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

+23-15
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@
2121
import static apijson.JSONObject.KEY_ROLE;
2222
import static apijson.JSONObject.KEY_SCHEMA;
2323
import static apijson.JSONObject.KEY_USER_ID;
24-
import static apijson.RequestMethod.DELETE;
25-
import static apijson.RequestMethod.GET;
26-
import static apijson.RequestMethod.GETS;
27-
import static apijson.RequestMethod.HEADS;
28-
import static apijson.RequestMethod.POST;
29-
import static apijson.RequestMethod.PUT;
24+
import static apijson.RequestMethod.*;
3025
import static apijson.SQL.AND;
3126
import static apijson.SQL.NOT;
3227
import static apijson.SQL.OR;
@@ -890,7 +885,7 @@ public String getOrderString(boolean hasPrefix) {
890885
// return (hasPrefix ? " ORDER BY " : "") + StringUtil.concat(order, joinOrder, ", ");
891886
// }
892887

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

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

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

26952695
/**获取条件SQL字符串
2696-
* @param page
26972696
* @param column
26982697
* @param table
2699-
* @param where
2698+
* @param config
27002699
* @return
27012700
* @throws Exception
27022701
*/
@@ -2708,11 +2707,21 @@ private static String getConditionString(String column, String table, AbstractSQ
27082707
table = config.getSubqueryString(from) + " AS " + config.getAliasWithQuote() + " ";
27092708
}
27102709

2711-
String condition = table + config.getJoinString() + where + (
2712-
RequestMethod.isGetMethod(config.getMethod(), true) == false ?
2713-
"" : config.getGroupString(true) + config.getHavingString(true) + config.getOrderString(true)
2714-
)
2715-
; //+ config.getLimitString();
2710+
//根据方法不同,聚合语句不同。GROUP BY 和 HAVING 可以加在 HEAD 上, HAVING 可以加在 PUT, DELETE 上,GET 全加,POST 全都不加
2711+
String aggregation = "";
2712+
if (RequestMethod.isGetMethod(config.getMethod(), true)){
2713+
aggregation = config.getGroupString(true) + config.getHavingString(true) +
2714+
config.getOrderString(true);
2715+
}
2716+
if (RequestMethod.isHeadMethod(config.getMethod(), true)){
2717+
aggregation = config.getGroupString(true) + config.getHavingString(true) ;
2718+
}
2719+
if (config.getMethod() == PUT || config.getMethod() == DELETE){
2720+
aggregation = config.getHavingString(true) ;
2721+
}
2722+
2723+
String condition = table + config.getJoinString() + where + aggregation;
2724+
; //+ config.getLimitString();
27162725

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

2752-
27532761
private boolean keyPrefix;
27542762
@Override
27552763
public boolean isKeyPrefix() {

0 commit comments

Comments
 (0)