diff --git a/jOOQ-test/src/org/jooq/test/PostgresTest.java b/jOOQ-test/src/org/jooq/test/PostgresTest.java index 51aec2ce4e..d2950de233 100644 --- a/jOOQ-test/src/org/jooq/test/PostgresTest.java +++ b/jOOQ-test/src/org/jooq/test/PostgresTest.java @@ -42,6 +42,7 @@ package org.jooq.test; import static java.util.Arrays.asList; +import static org.jooq.impl.DSL.selectOne; import static org.jooq.impl.DSL.val; import static org.jooq.test.postgres.generatedclasses.Routines.fSearchBook; import static org.jooq.test.postgres.generatedclasses.Tables.T_639_NUMBERS_TABLE; @@ -58,6 +59,7 @@ import static org.jooq.test.postgres.generatedclasses.Tables.T_DATES; import static org.jooq.test.postgres.generatedclasses.Tables.T_EXOTIC_TYPES; import static org.jooq.test.postgres.generatedclasses.Tables.T_IDENTITY; import static org.jooq.test.postgres.generatedclasses.Tables.T_IDENTITY_PK; +import static org.jooq.test.postgres.generatedclasses.Tables.T_INHERITANCE_CITIES; import static org.jooq.test.postgres.generatedclasses.Tables.T_PG_EXTENSIONS; import static org.jooq.test.postgres.generatedclasses.Tables.T_TRIGGERS; import static org.jooq.test.postgres.generatedclasses.Tables.T_UNSIGNED; @@ -68,6 +70,7 @@ import static org.jooq.util.postgres.PostgresDSL.arrayAppend; import static org.jooq.util.postgres.PostgresDSL.arrayCat; import static org.jooq.util.postgres.PostgresDSL.arrayPrepend; import static org.jooq.util.postgres.PostgresDSL.arrayToString; +import static org.jooq.util.postgres.PostgresDSL.only; import static org.jooq.util.postgres.PostgresDSL.stringToArray; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -1059,4 +1062,10 @@ public class PostgresTest extends jOOQAbstractTest< } + + @Test + public void testPostgresOnlyClause() throws Exception { + assertEquals(3, create().fetchCount(selectOne().from(T_INHERITANCE_CITIES))); + assertEquals(2, create().fetchCount(selectOne().from(only(T_INHERITANCE_CITIES)))); + } } diff --git a/jOOQ-test/src/org/jooq/test/postgres/create.sql b/jOOQ-test/src/org/jooq/test/postgres/create.sql index ef028dab0e..6d35f502dd 100644 --- a/jOOQ-test/src/org/jooq/test/postgres/create.sql +++ b/jOOQ-test/src/org/jooq/test/postgres/create.sql @@ -64,6 +64,8 @@ DROP TABLE IF EXISTS t_booleans/ DROP TABLE IF EXISTS t_identity/ DROP TABLE IF EXISTS t_identity_pk/ DROP TABLE IF EXISTS t_pg_extensions/ +DROP TABLE IF EXISTS t_inheritance_capitals/ +DROP TABLE IF EXISTS t_inheritance_cities/ DROP TYPE IF EXISTS u_address_type/ DROP TYPE IF EXISTS u_street_type/ @@ -108,6 +110,18 @@ CREATE TYPE u_address_type AS ( ) / +CREATE TABLE t_inheritance_cities ( + name text, + population int, + altitude int +) +/ + +CREATE TABLE t_inheritance_capitals ( + state char(2) +) INHERITS (t_inheritance_cities) +/ + CREATE TABLE t_pg_extensions ( id serial not null, pg_interval interval, diff --git a/jOOQ-test/src/org/jooq/test/postgres/reset.sql b/jOOQ-test/src/org/jooq/test/postgres/reset.sql index b46e5a0238..27c7f7746b 100644 --- a/jOOQ-test/src/org/jooq/test/postgres/reset.sql +++ b/jOOQ-test/src/org/jooq/test/postgres/reset.sql @@ -52,4 +52,8 @@ INSERT INTO t_book_to_book_store VALUES ('Buchhandlung im Volkshaus', 3, 1)/ INSERT INTO t_arrays VALUES (1, null, null, null, null, null, null)/ INSERT INTO t_arrays VALUES (2, '{}', '{}', '{}', '{}', '{}', '{}')/ INSERT INTO t_arrays VALUES (3, '{"a"}', '{1}', ARRAY[TO_DATE('1981-07-10', 'YYYY-MM-DD')], ARRAY[ROW('Downing Street', '10', null, E'\\x6969')]::u_street_type[], '{"England"}', ARRAY[ARRAY[1]])/ -INSERT INTO t_arrays VALUES (4, '{"a", "b"}', '{1, 2}', ARRAY[TO_DATE('1981-07-10', 'YYYY-MM-DD'), TO_DATE('2000-01-01', 'YYYY-MM-DD')], ARRAY[ROW('Downing Street', '10', '{}', E'\\x6969'), ROW('Bahnhofstrasse', '12', '{1, 2}', E'\\x6969')]::u_street_type[], '{"England", "Germany"}', ARRAY[ARRAY[1], ARRAY[2]])/ \ No newline at end of file +INSERT INTO t_arrays VALUES (4, '{"a", "b"}', '{1, 2}', ARRAY[TO_DATE('1981-07-10', 'YYYY-MM-DD'), TO_DATE('2000-01-01', 'YYYY-MM-DD')], ARRAY[ROW('Downing Street', '10', '{}', E'\\x6969'), ROW('Bahnhofstrasse', '12', '{1, 2}', E'\\x6969')]::u_street_type[], '{"England", "Germany"}', ARRAY[ARRAY[1], ARRAY[2]])/ + +INSERT INTO t_inheritance_capitals VALUES ('Zurich', 396389, 408, 'ZH')/ +INSERT INTO t_inheritance_cities VALUES ('Winterthur', 103075, 439)/ +INSERT INTO t_inheritance_cities VALUES ('Uster', 32577, 464)/ \ No newline at end of file diff --git a/jOOQ/src/main/java/org/jooq/util/postgres/PostgresDSL.java b/jOOQ/src/main/java/org/jooq/util/postgres/PostgresDSL.java index de67d0f28c..06c8aba553 100644 --- a/jOOQ/src/main/java/org/jooq/util/postgres/PostgresDSL.java +++ b/jOOQ/src/main/java/org/jooq/util/postgres/PostgresDSL.java @@ -43,8 +43,10 @@ package org.jooq.util.postgres; import static org.jooq.SQLDialect.POSTGRES; import org.jooq.Field; +import org.jooq.Record; import org.jooq.SQLDialect; import org.jooq.Support; +import org.jooq.Table; import org.jooq.impl.DSL; import org.jooq.impl.SQLDataType; @@ -303,4 +305,21 @@ public class PostgresDSL extends DSL { public static Field stringToArray(Field string, Field delimiter) { return field("{string_to_array}({0}, {1})", SQLDataType.VARCHAR.getArrayDataType(), nullSafe(string), nullSafe(delimiter)); } + + // ------------------------------------------------------------------------- + // Other PostgreSQL-specific functions / clauses + // ------------------------------------------------------------------------- + + /** + * Get the PostgreSQL-specific ONLY [table] clause for use with + * table inheritance. + *

+ * Example:

+     * SELECT * FROM ONLY parent_table
+     * 
+ */ + @Support({ POSTGRES }) + public static Table only(Table table) { + return table("{only} {0}", table); + } }