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

尝试修复 oracle select 分页语法问题 #223

Merged
merged 1 commit into from
Apr 23, 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
18 changes: 11 additions & 7 deletions APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2613,8 +2613,12 @@ public static String getSQL(AbstractSQLConfig config) throws Exception {

config.setPreparedValueList(new ArrayList<Object>());
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字符串
Expand All @@ -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();
// }
//
//
Expand Down