From 32f0c9b051edffb93d3cc1262bfea078dce12fed Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Sat, 28 Dec 2013 12:15:41 +0100 Subject: [PATCH] [#2916] Improve code generator compatibility with PostgreSQL 8.x by avoiding the use of window functions --- .../jooq/util/postgres/PostgresDatabase.java | 82 ++++++++++++------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/jOOQ-meta/src/main/java/org/jooq/util/postgres/PostgresDatabase.java b/jOOQ-meta/src/main/java/org/jooq/util/postgres/PostgresDatabase.java index 7b5e922325..e7fd961f9c 100644 --- a/jOOQ-meta/src/main/java/org/jooq/util/postgres/PostgresDatabase.java +++ b/jOOQ-meta/src/main/java/org/jooq/util/postgres/PostgresDatabase.java @@ -80,6 +80,7 @@ import org.jooq.Record4; import org.jooq.Record5; import org.jooq.Result; import org.jooq.SQLDialect; +import org.jooq.exception.DataAccessException; import org.jooq.impl.DSL; import org.jooq.tools.JooqLogger; import org.jooq.util.AbstractDatabase; @@ -117,6 +118,8 @@ public class PostgresDatabase extends AbstractDatabase { private static final JooqLogger log = JooqLogger.getLogger(PostgresDatabase.class); + private static Boolean is84; + @Override protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException { for (Record record : fetchKeys("PRIMARY KEY")) { @@ -274,38 +277,42 @@ public class PostgresDatabase extends AbstractDatabase { PgClass pt = PG_CLASS.as("pt"); PgNamespace pn = PG_NAMESPACE.as("pn"); - for (Record5 inheritance : create() - .select( - cn.NSPNAME, - ct.RELNAME, - pn.NSPNAME, - pt.RELNAME, - max(i.INHSEQNO).over().partitionBy(i.INHRELID).as("m") - ) - .from(ct) - .join(cn).on(ct.RELNAMESPACE.eq(oid(cn))) - .join(i).on(i.INHRELID.eq(oid(ct))) - .join(pt).on(i.INHPARENT.eq(oid(pt))) - .join(pn).on(pt.RELNAMESPACE.eq(oid(pn))) - .fetch()) { + // [#2916] If window functions are not supported (prior to PostgreSQL 8.4), then + // don't execute the following query: + if (is84()) { + for (Record5 inheritance : create() + .select( + cn.NSPNAME, + ct.RELNAME, + pn.NSPNAME, + pt.RELNAME, + max(i.INHSEQNO).over().partitionBy(i.INHRELID).as("m") + ) + .from(ct) + .join(cn).on(ct.RELNAMESPACE.eq(oid(cn))) + .join(i).on(i.INHRELID.eq(oid(ct))) + .join(pt).on(i.INHPARENT.eq(oid(pt))) + .join(pn).on(pt.RELNAMESPACE.eq(oid(pn))) + .fetch()) { - Name child = name(inheritance.value1(), inheritance.value2()); - Name parent = name(inheritance.value3(), inheritance.value4()); + Name child = name(inheritance.value1(), inheritance.value2()); + Name parent = name(inheritance.value3(), inheritance.value4()); - if (inheritance.value5() > 1) { - log.info("Multiple inheritance", - "Multiple inheritance is not supported by jOOQ: " + - child + - " inherits from " + - parent); - } - else { - PostgresTableDefinition childTable = map.get(child); - PostgresTableDefinition parentTable = map.get(parent); + if (inheritance.value5() > 1) { + log.info("Multiple inheritance", + "Multiple inheritance is not supported by jOOQ: " + + child + + " inherits from " + + parent); + } + else { + PostgresTableDefinition childTable = map.get(child); + PostgresTableDefinition parentTable = map.get(parent); - if (childTable != null && parentTable != null) { - childTable.setParentTable(parentTable); - parentTable.getChildTables().add(childTable); + if (childTable != null && parentTable != null) { + childTable.setParentTable(parentTable); + parentTable.getChildTables().add(childTable); + } } } } @@ -520,4 +527,21 @@ public class PostgresDatabase extends AbstractDatabase { protected DSLContext create0() { return DSL.using(getConnection(), SQLDialect.POSTGRES); } + + private boolean is84() { + if (is84 == null) { + + // [#2916] Window functions were introduced with PostgreSQL 9.0 + try { + create().select(count().over()).fetch(); + + is84 = true; + } + catch (DataAccessException e) { + is84 = false; + } + } + + return is84; + } }