From 60a305c9aad2dc4f1620c8b084c4ac398fb1c951 Mon Sep 17 00:00:00 2001 From: lixin Date: Thu, 16 Sep 2021 12:58:45 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=A0=B9=E6=8D=AE=E6=96=B9=E6=B3=95=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E6=8B=BC=E6=8E=A5=E8=81=9A=E5=90=88=E8=AF=AD=E5=8F=A5?= =?UTF-8?q?=202.=E4=BF=AE=E6=94=B9Oracle=E5=88=86=E7=BB=84=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/apijson/orm/AbstractSQLConfig.java | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java b/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java index 82bdbf5b4..856c9e8d7 100755 --- a/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java +++ b/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java @@ -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; @@ -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 @@ -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(); } @@ -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 */ @@ -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())) { @@ -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() {