[#454] Fixed CTE and aliasing with derived column lists

This commit is contained in:
Lukas Eder 2014-04-11 14:07:39 +02:00
parent 1fe69f3079
commit 24d1b421ae
4 changed files with 49 additions and 9 deletions

View File

@ -94,7 +94,7 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
}
@Test
public void testCTE() throws Exception {
public void testCTESimple() throws Exception {
switch (dialect().family()) {
case MARIADB:
case MYSQL:
@ -116,9 +116,11 @@ 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, result1.getValue(0, 0));
assertEquals("a", result1.getValue(0, 1));
}
@Test
public void testCTEMultiple() throws Exception {
CommonTableExpression<Record2<Integer, String>> t1 = name("t1").fields("f1", "f2").as(select(val(1), val("a")));
CommonTableExpression<Record2<Integer, String>> t2 = name("t2").fields("f3", "f4").as(select(val(2), val("b")));
@ -150,8 +152,14 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
.from(t1, t2)
.fetch()
);
}
@Test
public void testCTEAliasing() throws Exception {
CommonTableExpression<Record2<Integer, String>> t1 = name("t1").fields("f1", "f2").as(select(val(1), val("a")));
CommonTableExpression<Record2<Integer, String>> t2 = name("t2").fields("f3", "f4").as(select(val(2), val("b")));
// Try renaming the CTEs when referencing them
Table<Record2<Integer, String>> a1 = t1.as("a1");
Table<Record2<Integer, String>> a2 = t2.as("a2");
@ -174,9 +182,28 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
assertEquals(3, result3.getValue(0, 0));
assertEquals("ab", result3.getValue(0, 1));
// Try renaming the CTEs and their columns when referencing them
Table<Record2<Integer, String>> b1 = t1.as("a1", "i1", "s1");
Table<Record2<Integer, String>> b2 = t2.as("a2", "i2", "s2");
Result<?> result4 =
create().with(t1)
.with(t2)
.select(
b1.field("i1").add(b2.field("i2")).as("add"),
b1.field("s1").concat(b2.field("s2")).as("concat"))
.from(b1, b2)
.fetch();
assertEquals(1, result4.size());
assertEquals(2, result4.fields().length);
assertEquals("add", result4.field(0).getName());
assertEquals("concat", result4.field(1).getName());
assertEquals(Integer.class, result4.field(0).getType());
assertEquals(String.class, result4.field(1).getType());
assertEquals(3, result4.getValue(0, 0));
assertEquals("ab", result4.getValue(0, 1));
// TODO: Test aliasing CTE with derived column lists : a1(x1, x2)
// TODO: Test CTE with no explicit column lists
// TODO: Test recursive CTE
// TODO: Test CTE with UNIONs (may not work due to #1658)

View File

@ -2023,8 +2023,18 @@ public abstract class jOOQAbstractTest<
}
@Test
public void testCTE() throws Exception {
new CTETests(this).testCTE();
public void testCTESimple() throws Exception {
new CTETests(this).testCTESimple();
}
@Test
public void testCTEMultiple() throws Exception {
new CTETests(this).testCTEMultiple();
}
@Test
public void testCTEAliasing() throws Exception {
new CTETests(this).testCTEAliasing();
}
@Test

View File

@ -2,4 +2,5 @@ eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//target/generated-sources/xjc=UTF-8
encoding/<project>=UTF-8

View File

@ -122,10 +122,12 @@ class Alias<Q extends QueryPart> extends AbstractQueryPart {
SQLDialect dialect = context.configuration().dialect();
boolean simulateDerivedColumnList = false;
// [#1801] Some databases don't allow "derived column names" in
// "simple class specifications". Hence, wrap the table reference in
// a subselect
if (fieldAliases != null && asList(CUBRID, FIREBIRD).contains(dialect.family()) && wrapped instanceof TableImpl) {
// [#454] [#1801] Some databases don't allow "derived column names" in
// "simple class specifications", or "common table expression references".
// Hence, wrap the table reference in a subselect
if (fieldAliases != null
&& asList(CUBRID, FIREBIRD).contains(dialect.family())
&& (wrapped instanceof TableImpl || wrapped instanceof CommonTableExpressionImpl)) {
@SuppressWarnings("unchecked")
Select<Record> select =