21
21
import static apijson .JSONObject .KEY_ROLE ;
22
22
import static apijson .JSONObject .KEY_SCHEMA ;
23
23
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 .*;
30
25
import static apijson .SQL .AND ;
31
26
import static apijson .SQL .NOT ;
32
27
import static apijson .SQL .OR ;
@@ -890,7 +885,7 @@ public String getOrderString(boolean hasPrefix) {
890
885
// return (hasPrefix ? " ORDER BY " : "") + StringUtil.concat(order, joinOrder, ", ");
891
886
// }
892
887
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关键字
894
889
895
890
// String[] ss = StringUtil.split(order);
896
891
if (StringUtil .isEmpty (order , true )) { //SQL Server 子查询内必须指定 OFFSET 才能用 ORDER BY
@@ -2685,6 +2680,11 @@ public static String getSQL(AbstractSQLConfig config) throws Exception {
2685
2680
String column = config .getColumnString ();
2686
2681
if (config .isOracle ()) {
2687
2682
//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
+ }
2688
2688
return explain + "SELECT * FROM (SELECT " + (config .getCache () == JSONRequest .CACHE_RAM ? "SQL_NO_CACHE " : "" ) + column + " FROM " + getConditionString (column , tablePath , config ) + ") " + config .getLimitString ();
2689
2689
}
2690
2690
@@ -2693,10 +2693,9 @@ public static String getSQL(AbstractSQLConfig config) throws Exception {
2693
2693
}
2694
2694
2695
2695
/**获取条件SQL字符串
2696
- * @param page
2697
2696
* @param column
2698
2697
* @param table
2699
- * @param where
2698
+ * @param config
2700
2699
* @return
2701
2700
* @throws Exception
2702
2701
*/
@@ -2708,11 +2707,21 @@ private static String getConditionString(String column, String table, AbstractSQ
2708
2707
table = config .getSubqueryString (from ) + " AS " + config .getAliasWithQuote () + " " ;
2709
2708
}
2710
2709
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();
2716
2725
2717
2726
//no need to optimize
2718
2727
// if (config.getPage() <= 0 || ID.equals(column.trim())) {
@@ -2749,7 +2758,6 @@ private static String getConditionString(String column, String table, AbstractSQ
2749
2758
// return table + " AS t0 INNER JOIN (SELECT id FROM " + condition + ") AS t1 ON t0.id = t1.id";
2750
2759
}
2751
2760
2752
-
2753
2761
private boolean keyPrefix ;
2754
2762
@ Override
2755
2763
public boolean isKeyPrefix () {
0 commit comments