[#1954] Bad SQL rendered when combining ORDER BY [ some-function ] with
LIMIT .. OFFSET in DB2, SQL Server - Workaround for DB2: Avoid ORDER BY clause in ROW_NUMBER() ranking function
This commit is contained in:
parent
4af0a57485
commit
81d18ecf06
@ -45,6 +45,7 @@ import static org.jooq.SQLDialect.HSQLDB;
|
||||
import static org.jooq.SQLDialect.INGRES;
|
||||
import static org.jooq.SQLDialect.SYBASE;
|
||||
import static org.jooq.impl.Factory.count;
|
||||
import static org.jooq.impl.Factory.inline;
|
||||
import static org.jooq.impl.Factory.lower;
|
||||
import static org.jooq.impl.Factory.param;
|
||||
import static org.jooq.impl.Factory.table;
|
||||
@ -260,7 +261,9 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T725,
|
||||
.orderBy(
|
||||
TBook_AUTHOR_ID().mul(2).asc(),
|
||||
lower(TBook_TITLE()).asc())
|
||||
.limit(2)
|
||||
|
||||
// Force Sybase to simulate TOP .. START AT
|
||||
.limit(inline(2))
|
||||
.offset(1)
|
||||
.fetch(TBook_ID());
|
||||
|
||||
|
||||
@ -46,6 +46,7 @@ import static org.jooq.SQLDialect.MYSQL;
|
||||
import static org.jooq.SQLDialect.POSTGRES;
|
||||
import static org.jooq.SQLDialect.SQLITE;
|
||||
import static org.jooq.SQLDialect.SQLSERVER;
|
||||
import static org.jooq.SQLDialect.SYBASE;
|
||||
import static org.jooq.impl.Factory.inline;
|
||||
import static org.jooq.impl.Factory.one;
|
||||
|
||||
@ -322,21 +323,26 @@ abstract class AbstractSubSelect<R extends Record> extends AbstractSelect<R> imp
|
||||
.keyword("select ")
|
||||
.sql(subqueryName)
|
||||
.sql(".*, row_number() ")
|
||||
.keyword("over (order by ");
|
||||
.keyword("over (");
|
||||
|
||||
if (getOrderBy().isEmpty()) {
|
||||
context.literal(getSelect().get(0).getName());
|
||||
}
|
||||
else {
|
||||
String separator = "";
|
||||
// [#1954] DB2 can do without ORDER BY clause in ranking functions
|
||||
if (asList(SQLSERVER, SYBASE).contains(context.getDialect())) {
|
||||
context.keyword("order by ");
|
||||
|
||||
for (SortField<?> field : getOrderBy()) {
|
||||
context.sql(separator)
|
||||
.literal(field.getName())
|
||||
.sql(" ")
|
||||
.keyword(field.getOrder().toSQL());
|
||||
if (getOrderBy().isEmpty()) {
|
||||
context.literal(getSelect().get(0).getName());
|
||||
}
|
||||
else {
|
||||
String separator = "";
|
||||
|
||||
separator = ", ";
|
||||
for (SortField<?> field : getOrderBy()) {
|
||||
context.sql(separator)
|
||||
.literal(field.getName())
|
||||
.sql(" ")
|
||||
.keyword(field.getOrder().toSQL());
|
||||
|
||||
separator = ", ";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user