From be252ca22bc4397fce99713a46f82c15553d5c52 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Sat, 28 Dec 2013 14:21:03 +0100 Subject: [PATCH] [#2734] Add support for lateral derived tables --- .../configuration/lukas/postgres/library.xml | 2 +- .../org/jooq/test/_/testcases/JoinTests.java | 66 +++++++++++- .../src/org/jooq/test/jOOQAbstractTest.java | 5 + jOOQ/src/main/java/org/jooq/impl/DSL.java | 17 +++ jOOQ/src/main/java/org/jooq/impl/Lateral.java | 101 ++++++++++++++++++ 5 files changed, 188 insertions(+), 3 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/Lateral.java diff --git a/jOOQ-test/configuration/org/jooq/configuration/lukas/postgres/library.xml b/jOOQ-test/configuration/org/jooq/configuration/lukas/postgres/library.xml index 092bf06237..4622c648ff 100644 --- a/jOOQ-test/configuration/org/jooq/configuration/lukas/postgres/library.xml +++ b/jOOQ-test/configuration/org/jooq/configuration/lukas/postgres/library.xml @@ -2,7 +2,7 @@ org.postgresql.Driver - jdbc:postgresql:postgres + jdbc:postgresql://localhost:5434/postgres public postgres test diff --git a/jOOQ-test/src/org/jooq/test/_/testcases/JoinTests.java b/jOOQ-test/src/org/jooq/test/_/testcases/JoinTests.java index f3fa26a2da..bbc8bd821d 100644 --- a/jOOQ-test/src/org/jooq/test/_/testcases/JoinTests.java +++ b/jOOQ-test/src/org/jooq/test/_/testcases/JoinTests.java @@ -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 BaseTestLATERAL joined table. + *

+ * Example: + *

+     * SELECT *
+     * FROM employees e,
+     *      LATERAL(SELECT * FROM departments d
+     *              WHERE e.department_id = d.department_id);
+     * 
+ */ + @Support({ POSTGRES }) + public static Table lateral(TableLike table) { + return new Lateral(table.asTable()); + } + // ------------------------------------------------------------------------- // XXX SQL keywords // ------------------------------------------------------------------------- diff --git a/jOOQ/src/main/java/org/jooq/impl/Lateral.java b/jOOQ/src/main/java/org/jooq/impl/Lateral.java new file mode 100644 index 0000000000..c2ba4f00ae --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Lateral.java @@ -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 extends AbstractTable { + + /** + * Generated UID + */ + private static final long serialVersionUID = -3665347156501299297L; + private final Table table; + + Lateral(Table table) { + super(table.getName(), table.getSchema()); + + this.table = table; + } + + @Override + public final boolean declaresTables() { + return true; + } + + @Override + public final Class getRecordType() { + return table.getRecordType(); + } + + @Override + public final Table as(String alias) { + return new Lateral(table.as(alias)); + } + + @Override + public final Table as(String alias, String... fieldAliases) { + return new Lateral(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 fields0() { + return new Fields(table.fields()); + } +}