From 08a3125adf96509ae8361095c18e54818e2e4711 Mon Sep 17 00:00:00 2001 From: kxlv2000 <49295281+kxlv2000@users.noreply.github.com> Date: Fri, 23 Apr 2021 15:26:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E4=BF=AE=E5=A4=8D=20oracle?= =?UTF-8?q?=20select=20=E5=88=86=E9=A1=B5=E8=AF=AD=E6=B3=95=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/apijson/orm/AbstractSQLConfig.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java b/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java index d4ac822f8..4f036900e 100755 --- a/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java +++ b/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java @@ -1473,17 +1473,17 @@ public String getLimitString() { if (count <= 0 || RequestMethod.isHeadMethod(getMethod(), true)) { return ""; } - return getLimitString(getPage(), getCount(), isOracle() || isSQLServer() || isDb2()); + return getLimitString(getPage(), getCount(), isOracle() || isSQLServer() || isDb2(), isOracle()); } /**获取限制数量 * @param limit * @return */ - public static String getLimitString(int page, int count, boolean isTSQL) { + public static String getLimitString(int page, int count, boolean isTSQL, boolean isOracle) { int offset = getOffset(page, count); - if (isTSQL) { // OFFSET FECTH 中所有关键词都不可省略 - return " OFFSET " + offset + " ROWS FETCH FIRST " + count + " ROWS ONLY"; + if (isTSQL) { // OFFSET FECTH 中所有关键词都不可省略, 另外 Oracle 数据库使用子查询加 where 分页 + return isOracle? " WHERE ROWNUM BETWEEN "+ offset +" AND "+ (offset + count): " OFFSET " + offset + " ROWS FETCH FIRST " + count + " ROWS ONLY"; } return " LIMIT " + count + (offset <= 0 ? "" : " OFFSET " + offset); // DELETE, UPDATE 不支持 OFFSET @@ -2613,8 +2613,12 @@ public static String getSQL(AbstractSQLConfig config) throws Exception { config.setPreparedValueList(new ArrayList()); String column = config.getColumnString(); - return explain + "SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config); - } + if(config.isOracle()){ + //When config's database is oracle,Using subquery since Oracle12 below does not support OFFSET FETCH paging syntax. + return explain + "SELECT * FROM (SELECT"+ (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM "+getConditionString(column, tablePath, config)+ ") "+config.getLimitString(); + }else + return explain + "SELECT " + (config.getCache() == JSONRequest.CACHE_RAM ? "SQL_NO_CACHE " : "") + column + " FROM " + getConditionString(column, tablePath, config); + } } /**获取条件SQL字符串 @@ -2641,7 +2645,7 @@ private static String getConditionString(String column, String table, AbstractSQ //no need to optimize // if (config.getPage() <= 0 || ID.equals(column.trim())) { - return condition + config.getLimitString(); + return config.isOracle()? condition:condition + config.getLimitString(); // } // //