[#454] Added CTE / LIMIT / OFFSET integration tests

This commit is contained in:
Lukas Eder 2014-06-03 16:20:32 +02:00
parent 8a2f3281d5
commit 06574b9d4c
4 changed files with 81 additions and 4 deletions

View File

@ -236,10 +236,6 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
assertEquals(String.class, result1.field(1).getType());
assertEquals(1, (int) result1.get(0).getValue(0, Integer.class));
assertEquals("a", result1.getValue(0, 1));
// TODO: Test CTE with UNIONs (may not work due to #1658)
// TODO: Test CTE with LIMIT .. OFFSET (may not work due to ROWNUM emulation, etc)
// TODO: Test CTE with complex subqueries
}
public void testRecursiveCTESimple() throws Exception {
@ -343,4 +339,58 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
.collect(toList()),
result.getValues(3));
}
public void testCTEWithLimit() throws Exception {
assumeFamilyNotIn(CUBRID, DERBY, H2, MARIADB, MYSQL, SQLITE);
CommonTableExpression<Record3<String, String, Integer>> t1 = name("t1").as(
select(TAuthor_FIRST_NAME(), TAuthor_LAST_NAME(), TBook_ID())
.from(TAuthor())
.join(TBook())
.on(TBook_AUTHOR_ID().eq(TAuthor_ID()))
.orderBy(TBook_ID())
.limit(3)
);
Result<Record> result =
create().with(t1)
.select(t1.fields())
.from(t1)
.orderBy(t1.field(2).desc())
.limit(2)
.fetch();
assertEquals(2, result.size());
assertEquals(asList("Paulo", "George"), result.getValues(0));
assertEquals(asList("Coelho", "Orwell"), result.getValues(1));
assertEquals(asList(3, 2), result.getValues(2, int.class));
}
public void testCTEWithLimitOffset() throws Exception {
assumeFamilyNotIn(CUBRID, DERBY, H2, MARIADB, MYSQL, SQLITE);
CommonTableExpression<Record3<String, String, Integer>> t1 = name("t1").as(
select(TAuthor_FIRST_NAME(), TAuthor_LAST_NAME(), TBook_ID())
.from(TAuthor())
.join(TBook())
.on(TBook_AUTHOR_ID().eq(TAuthor_ID()))
.orderBy(TBook_ID())
.limit(3)
.offset(1)
);
Result<Record> result =
create().with(t1)
.select(t1.fields())
.from(t1)
.orderBy(t1.field(2).desc())
.limit(2)
.offset(1)
.fetch();
assertEquals(2, result.size());
assertEquals(asList("Paulo", "George"), result.getValues(0));
assertEquals(asList("Coelho", "Orwell"), result.getValues(1));
assertEquals(asList(3, 2), result.getValues(2, int.class));
}
}

View File

@ -2267,6 +2267,16 @@ public abstract class jOOQAbstractTest<
new CTETests(this).testRecursiveCTEMultiple();
}
@Test
public void testCTEWithLimit() throws Exception {
new CTETests(this).testCTEWithLimit();
}
@Test
public void testCTEWithLimitOffset() throws Exception {
new CTETests(this).testCTEWithLimitOffset();
}
@Test
public void testJoinDuplicateFieldNames() throws Exception {
new JoinTests(this).testJoinDuplicateFieldNames();

View File

@ -70,6 +70,15 @@ public interface Name extends QueryPart {
*/
WindowDefinition as(WindowSpecification window);
/**
* Specify a subselect to refer to by the <code>Name</code> to form a common
* table expression.
* <p>
* Column names are implicitly inherited from the <code>SELECT</code>
* statement.
*/
<R extends Record> CommonTableExpression<R> as(Select<R> select);
/**
* Add a list of fields to this name to make this name a
* {@link DerivedColumnList}.

View File

@ -43,9 +43,12 @@ package org.jooq.impl;
import java.util.Arrays;
import org.jooq.Clause;
import org.jooq.CommonTableExpression;
import org.jooq.Context;
import org.jooq.DerivedColumnList;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Select;
import org.jooq.WindowDefinition;
import org.jooq.WindowSpecification;
import org.jooq.tools.StringUtils;
@ -95,6 +98,11 @@ class NameImpl extends AbstractQueryPart implements Name {
return new WindowDefinitionImpl(this, window);
}
@Override
public final <R extends Record> CommonTableExpression<R> as(Select<R> select) {
return fields(new String[0]).as(select);
}
@Override
public final DerivedColumnList fields(String... fieldNames) {
if (qualifiedName.length != 1)