[#2734] Add support for lateral derived tables

This commit is contained in:
Lukas Eder 2013-12-28 14:21:03 +01:00
parent a06fd8d56f
commit be252ca22b
5 changed files with 188 additions and 3 deletions

View File

@ -2,7 +2,7 @@
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-2.1.0.xsd">
<jdbc>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql:postgres</url>
<url>jdbc:postgresql://localhost:5434/postgres</url>
<schema>public</schema>
<user>postgres</user>
<password>test</password>

View File

@ -42,9 +42,7 @@ package org.jooq.test._.testcases;
import static java.util.Arrays.asList;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static org.jooq.SQLDialect.CUBRID;
// ...
import static org.jooq.SQLDialect.HSQLDB;
@ -54,12 +52,15 @@ import static org.jooq.SQLDialect.SQLITE;
import static org.jooq.impl.DSL.count;
import static org.jooq.impl.DSL.falseCondition;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.lateral;
import static org.jooq.impl.DSL.lower;
import static org.jooq.impl.DSL.one;
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.selectOne;
import static org.jooq.impl.DSL.val;
import static org.jooq.impl.DSL.zero;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.sql.Date;
import java.util.List;
@ -400,6 +401,67 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
xx [/pro] */
}
@Test
public void testLateralJoin() throws Exception {
switch (dialect()) {
/* [pro] xx
xxxx xxxxxxxxxx
xx [/pro] */
case POSTGRES:
break;
default:
log.info("SKIPPING", "LATERAL tests");
return;
}
assertEquals(
asList(2, 2),
create().select()
.from(TAuthor(),
lateral(select(count().as("c"))
.from(TBook())
.where(TBook_AUTHOR_ID().eq(TAuthor_ID())))
)
.fetch("c", int.class)
);
assertEquals(
asList(2, 2),
create().select()
.from(TAuthor().crossJoin(
lateral(select(count().as("c"))
.from(TBook())
.where(TBook_AUTHOR_ID().eq(TAuthor_ID())))
)
)
.fetch("c", int.class)
);
assertEquals(
asList(2, 2),
create().select()
.from(TAuthor(),
lateral(select(count())
.from(TBook())
.where(TBook_AUTHOR_ID().eq(TAuthor_ID()))).as("x", "c")
)
.fetch("c", int.class)
);
assertEquals(
asList(2, 2),
create().select()
.from(TAuthor(),
lateral(select(count())
.from(TBook())
.where(TBook_AUTHOR_ID().eq(TAuthor_ID())).asTable("x", "c"))
)
.fetch("c", int.class)
);
}
@Test
public void testNaturalJoin() throws Exception {
boolean unqualified = false;

View File

@ -1966,6 +1966,11 @@ public abstract class jOOQAbstractTest<
new JoinTests(this).testCrossApply();
}
@Test
public void testLateralJoin() throws Exception {
new JoinTests(this).testLateralJoin();
}
@Test
public void testNaturalJoin() throws Exception {
new JoinTests(this).testNaturalJoin();

View File

@ -202,6 +202,7 @@ import org.jooq.SelectWhereStep;
import org.jooq.SortField;
import org.jooq.Support;
import org.jooq.Table;
import org.jooq.TableLike;
import org.jooq.TruncateIdentityStep;
import org.jooq.UDTRecord;
import org.jooq.Update;
@ -4771,6 +4772,22 @@ public class DSL {
return new GenerateSeries(nullSafe(from), nullSafe(to));
}
/**
* Create a <code>LATERAL</code> joined table.
* <p>
* Example:
* <code><pre>
* SELECT *
* FROM employees e,
* LATERAL(SELECT * FROM departments d
* WHERE e.department_id = d.department_id);
* </pre></code>
*/
@Support({ POSTGRES })
public static <R extends Record> Table<R> lateral(TableLike<R> table) {
return new Lateral<R>(table.asTable());
}
// -------------------------------------------------------------------------
// XXX SQL keywords
// -------------------------------------------------------------------------

View File

@ -0,0 +1,101 @@
/**
* Copyright (c) 2009-2013, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* This work is dual-licensed
* - under the Apache Software License 2.0 (the "ASL")
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
* =============================================================================
* You may choose which license applies to you:
*
* - If you're using this work with Open Source databases, you may choose
* either ASL or jOOQ License.
* - If you're using this work with at least one commercial database, you must
* choose jOOQ License
*
* For more information, please visit http://www.jooq.org/licenses
*
* Apache Software License 2.0:
* -----------------------------------------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* jOOQ License and Maintenance Agreement:
* -----------------------------------------------------------------------------
* Data Geekery grants the Customer the non-exclusive, timely limited and
* non-transferable license to install and use the Software under the terms of
* the jOOQ License and Maintenance Agreement.
*
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
*/
package org.jooq.impl;
import org.jooq.BindContext;
import org.jooq.Record;
import org.jooq.RenderContext;
import org.jooq.Table;
/**
* @author Lukas Eder
*/
class Lateral<R extends Record> extends AbstractTable<R> {
/**
* Generated UID
*/
private static final long serialVersionUID = -3665347156501299297L;
private final Table<R> table;
Lateral(Table<R> table) {
super(table.getName(), table.getSchema());
this.table = table;
}
@Override
public final boolean declaresTables() {
return true;
}
@Override
public final Class<? extends R> getRecordType() {
return table.getRecordType();
}
@Override
public final Table<R> as(String alias) {
return new Lateral<R>(table.as(alias));
}
@Override
public final Table<R> as(String alias, String... fieldAliases) {
return new Lateral<R>(table.as(alias, fieldAliases));
}
@Override
public final void toSQL(RenderContext ctx) {
ctx.keyword("lateral")
.sql(" ")
.visit(table);
}
@Override
public final void bind(BindContext ctx) {
ctx.visit(table);
}
@Override
final Fields<R> fields0() {
return new Fields<R>(table.fields());
}
}