diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/OrderByTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/OrderByTests.java index 9a5af7df0e..73be90f998 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/OrderByTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/OrderByTests.java @@ -129,25 +129,36 @@ extends BaseTest authors = + Result authors1 = create().selectFrom(TAuthor()) .orderBy( TAuthor_FIRST_NAME().asc().nullsFirst()) .fetch(); - assertNull(authors.getValue(0, TAuthor_FIRST_NAME())); - assertEquals("George", authors.getValue(1, TAuthor_FIRST_NAME())); - assertEquals("Paulo", authors.getValue(2, TAuthor_FIRST_NAME())); + assertNull(authors1.getValue(0, TAuthor_FIRST_NAME())); + assertEquals("George", authors1.getValue(1, TAuthor_FIRST_NAME())); + assertEquals("Paulo", authors1.getValue(2, TAuthor_FIRST_NAME())); - authors = + Result authors2 = create().selectFrom(TAuthor()) .orderBy( TAuthor_FIRST_NAME().asc().nullsLast()) .fetch(); - assertEquals("George", authors.getValue(0, TAuthor_FIRST_NAME())); - assertEquals("Paulo", authors.getValue(1, TAuthor_FIRST_NAME())); - assertNull(authors.getValue(2, TAuthor_FIRST_NAME())); + assertEquals("George", authors2.getValue(0, TAuthor_FIRST_NAME())); + assertEquals("Paulo", authors2.getValue(1, TAuthor_FIRST_NAME())); + assertNull(authors2.getValue(2, TAuthor_FIRST_NAME())); + + // [#1667] Check correct behaviour when bind values are involved + Result authors3 = + create().selectFrom(TAuthor()) + .orderBy( + TAuthor_FIRST_NAME().substring(3).asc().nullsLast()) + .fetch(); + + assertEquals("George", authors3.getValue(0, TAuthor_FIRST_NAME())); + assertEquals("Paulo", authors3.getValue(1, TAuthor_FIRST_NAME())); + assertNull(authors3.getValue(2, TAuthor_FIRST_NAME())); } @Test diff --git a/jOOQ/src/main/java/org/jooq/impl/SortFieldImpl.java b/jOOQ/src/main/java/org/jooq/impl/SortFieldImpl.java index 9c730fac4e..c5afa87590 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SortFieldImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/SortFieldImpl.java @@ -166,6 +166,24 @@ class SortFieldImpl extends AbstractNamedTypeProviderQueryPart implements @Override public final void bind(BindContext context) { + + // [#1667] Some dialects simulate NULLS { FIRST | LAST } clauses. They + // will need to bind the sort field twice + if (nullsFirst || nullsLast) { + switch (context.getDialect()) { + case DB2: + case ASE: + case CUBRID: + case INGRES: + case MYSQL: + case SQLITE: + case SQLSERVER: + case SYBASE: { + context.bind(field); + } + } + } + context.bind(field); } }