[#794] Add support for ORDER BY [int value] in order to reference a column index for sorting

This commit is contained in:
Lukas Eder 2011-10-29 16:40:29 +00:00
parent 5ac32186db
commit 09c3406180
7 changed files with 72 additions and 0 deletions

View File

@ -4284,6 +4284,32 @@ public abstract class jOOQAbstractTest<
assertNull(authors.getValue(2, TAuthor_FIRST_NAME()));
}
@Test
public void testOrderByIndexes() throws Exception {
assertEquals(Arrays.asList(1, 2, 3, 4),
create().selectFrom(TBook())
.orderBy(1)
.fetch(TBook_ID()));
assertEquals(Arrays.asList(1, 2, 3, 4),
create().select(TBook_ID(), TBook_TITLE())
.from(TBook())
.orderBy(1)
.fetch(TBook_ID()));
assertEquals(Arrays.asList(1, 2, 3, 4),
create().select(TBook_TITLE(), TBook_ID())
.from(TBook())
.orderBy(2)
.fetch(TBook_ID()));
assertEquals(Arrays.asList(1, 1, 2, 2),
create().select(TBook_AUTHOR_ID(), TBook_ID())
.from(TBook())
.orderBy(2, 1)
.fetch(TBook_AUTHOR_ID()));
}
@Test
public void testOrderByIndirection() throws Exception {
assertEquals(BOOK_IDS,

View File

@ -65,6 +65,15 @@ public interface OrderProvider {
*/
void addOrderBy(Collection<SortField<?>> fields);
/**
* Adds ordering fields
* <p>
* Indexes start at <code>1</code> in SQL!
*
* @param fields The ordering fields
*/
void addOrderBy(int... fieldIndexes);
/**
* Limit the results of this select
* <p>

View File

@ -95,4 +95,11 @@ public interface SelectOrderByStep extends SelectLimitStep {
* Add an <code>ORDER BY</code> clause to the query
*/
SelectLimitStep orderBy(Collection<SortField<?>> fields);
/**
* Add an <code>ORDER BY</code> clause to the query
* <p>
* Indexes start at <code>1</code> in SQL!
*/
SelectLimitStep orderBy(int... fieldIndexes);
}

View File

@ -72,4 +72,11 @@ public interface SimpleSelectOrderByStep<R extends Record> extends SimpleSelectL
* Add an <code>ORDER BY</code> clause to the query
*/
SimpleSelectLimitStep<R> orderBy(Collection<SortField<?>> fields);
/**
* Add an <code>ORDER BY</code> clause to the query
* <p>
* Indexes start at <code>1</code> in SQL!
*/
SimpleSelectLimitStep<R> orderBy(int... fieldIndexes);
}

View File

@ -727,6 +727,17 @@ implements
addOrderBy(Arrays.asList(fields));
}
@Override
public final void addOrderBy(int... fieldIndexes) {
Field<?>[] fields = new Field[fieldIndexes.length];
for (int i = 0; i < fieldIndexes.length; i++) {
fields[i] = literal(fieldIndexes[i]);
}
addOrderBy(fields);
}
@Override
public final void addConditions(Condition... conditions) {
condition.addConditions(conditions);

View File

@ -366,6 +366,12 @@ class SelectImpl extends AbstractDelegatingSelect<Record> implements
return this;
}
@Override
public final SelectImpl orderBy(int... fieldIndexes) {
getQuery().addOrderBy(fieldIndexes);
return this;
}
@Override
public final SelectImpl limit(int numberOfRows) {
this.limit = numberOfRows;

View File

@ -209,6 +209,12 @@ class SimpleSelectImpl<R extends Record> extends AbstractDelegatingSelect<R>
return this;
}
@Override
public final SimpleSelectImpl<R> orderBy(int... fieldIndexes) {
getQuery().addOrderBy(fieldIndexes);
return this;
}
@Override
public final SimpleSelectImpl<R> limit(int numberOfRows) {
this.limit = numberOfRows;