[#2776] Add support for the PostgreSQL ONLY clause

This commit is contained in:
Lukas Eder 2013-10-15 15:13:37 +02:00
parent 5e7f157f52
commit 095026dc1d
4 changed files with 47 additions and 1 deletions

View File

@ -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))));
}
}

View File

@ -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,

View File

@ -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]])/
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)/

View File

@ -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<String[]> stringToArray(Field<String> string, Field<String> delimiter) {
return field("{string_to_array}({0}, {1})", SQLDataType.VARCHAR.getArrayDataType(), nullSafe(string), nullSafe(delimiter));
}
// -------------------------------------------------------------------------
// Other PostgreSQL-specific functions / clauses
// -------------------------------------------------------------------------
/**
* Get the PostgreSQL-specific <code>ONLY [table]</code> clause for use with
* table inheritance.
* <p>
* Example: <code><pre>
* SELECT * FROM ONLY parent_table
* </pre></code>
*/
@Support({ POSTGRES })
public static Table<Record> only(Table<?> table) {
return table("{only} {0}", table);
}
}