From 48bc47380f451357e31a67aba232f91597dd407c Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Mon, 17 Apr 2023 11:57:07 +0200 Subject: [PATCH] [jOOQ/jOOQ#14930] Implement this for PostgreSQL --- .../jooq/meta/firebird/FirebirdDatabase.java | 2 +- .../org/jooq/meta/mysql/MySQLDatabase.java | 2 +- .../jooq/meta/postgres/PostgresDatabase.java | 18 ++++++------------ jOOQ/src/main/java/org/jooq/impl/MetaSQL.java | 10 +++++----- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/firebird/FirebirdDatabase.java b/jOOQ-meta/src/main/java/org/jooq/meta/firebird/FirebirdDatabase.java index 99ceb0f577..ad629489d3 100644 --- a/jOOQ-meta/src/main/java/org/jooq/meta/firebird/FirebirdDatabase.java +++ b/jOOQ-meta/src/main/java/org/jooq/meta/firebird/FirebirdDatabase.java @@ -393,7 +393,7 @@ public class FirebirdDatabase extends AbstractDatabase implements ResultQueryDat inline(null, VARCHAR).as("schema"), RDB$RELATIONS.RDB$RELATION_NAME.trim(), when(RDB$RELATIONS.RDB$VIEW_SOURCE.lower().like(inline("create%")), RDB$RELATIONS.RDB$VIEW_SOURCE.trim()) - .else_(inline("create view \"").concat(RDB$RELATIONS.RDB$RELATION_NAME.trim()).concat("\" as ").concat(RDB$RELATIONS.RDB$VIEW_SOURCE)).as("view_source")) + .else_(inline("create view \"").concat(RDB$RELATIONS.RDB$RELATION_NAME.trim()).concat(inline("\" as ")).concat(RDB$RELATIONS.RDB$VIEW_SOURCE)).as("view_source")) .from(RDB$RELATIONS) .orderBy(RDB$RELATIONS.RDB$RELATION_NAME.trim()); } diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/mysql/MySQLDatabase.java b/jOOQ-meta/src/main/java/org/jooq/meta/mysql/MySQLDatabase.java index ad4ed4a5cb..deb35e5d80 100644 --- a/jOOQ-meta/src/main/java/org/jooq/meta/mysql/MySQLDatabase.java +++ b/jOOQ-meta/src/main/java/org/jooq/meta/mysql/MySQLDatabase.java @@ -431,7 +431,7 @@ public class MySQLDatabase extends AbstractDatabase implements ResultQueryDataba VIEWS.TABLE_SCHEMA, VIEWS.TABLE_NAME, when(VIEWS.VIEW_DEFINITION.lower().like(inline("create%")), VIEWS.VIEW_DEFINITION) - .else_(inline("create view `").concat(VIEWS.TABLE_NAME).concat("` as ").concat(VIEWS.VIEW_DEFINITION)).as(VIEWS.VIEW_DEFINITION)) + .else_(inline("create view `").concat(VIEWS.TABLE_NAME).concat(inline("` as ")).concat(VIEWS.VIEW_DEFINITION)).as(VIEWS.VIEW_DEFINITION)) .from(VIEWS) .where(VIEWS.TABLE_SCHEMA.in(schemas)) .orderBy( diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/postgres/PostgresDatabase.java b/jOOQ-meta/src/main/java/org/jooq/meta/postgres/PostgresDatabase.java index 529491f215..e9035dc808 100644 --- a/jOOQ-meta/src/main/java/org/jooq/meta/postgres/PostgresDatabase.java +++ b/jOOQ-meta/src/main/java/org/jooq/meta/postgres/PostgresDatabase.java @@ -127,7 +127,7 @@ import org.jooq.Record12; import org.jooq.Record4; import org.jooq.Record5; import org.jooq.Record6; -import org.jooq.Record8; +import org.jooq.Record7; import org.jooq.Result; import org.jooq.ResultQuery; import org.jooq.SQLDialect; @@ -468,8 +468,8 @@ public class PostgresDatabase extends AbstractDatabase implements ResultQueryDat List result = new ArrayList<>(); Map map = new HashMap<>(); - Select> empty = - select(inline(""), inline(""), inline(""), inline(""), inline(""), inline(""), inline(""), inline("")) + Select> empty = + select(inline(""), inline(""), inline(""), inline(""), inline(""), inline(""), inline("")) .where(falseCondition()); for (Record record : create() @@ -482,7 +482,6 @@ public class PostgresDatabase extends AbstractDatabase implements ResultQueryDat PG_DESCRIPTION.DESCRIPTION, when(TABLES.TABLE_TYPE.eq(inline("VIEW")), inline(TableType.VIEW.name())) .else_(inline(TableType.TABLE.name())).as("table_type"), - VIEWS.VIEW_DEFINITION, inline("").as(ROUTINES.TYPE_UDT_SCHEMA), inline("").as(ROUTINES.TYPE_UDT_NAME)) .from(TABLES) @@ -521,7 +520,6 @@ public class PostgresDatabase extends AbstractDatabase implements ResultQueryDat PG_DESCRIPTION.DESCRIPTION, inline(TableType.MATERIALIZED_VIEW.name()).as("table_type"), inline(""), - inline(""), inline("")) .from(PG_CLASS) .leftOuterJoin(PG_DESCRIPTION) @@ -540,7 +538,6 @@ public class PostgresDatabase extends AbstractDatabase implements ResultQueryDat ROUTINES.SPECIFIC_NAME, inline(""), inline(TableType.FUNCTION.name()).as("table_type"), - inline(""), ROUTINES.TYPE_UDT_SCHEMA, ROUTINES.TYPE_UDT_NAME) .from(ROUTINES) @@ -558,10 +555,6 @@ public class PostgresDatabase extends AbstractDatabase implements ResultQueryDat String name = record.get(TABLES.TABLE_NAME); String comment = record.get(PG_DESCRIPTION.DESCRIPTION, String.class); TableType tableType = record.get("table_type", TableType.class); - String source = record.get(VIEWS.VIEW_DEFINITION); - - if (source != null && !source.toLowerCase().startsWith("create")) - source = "create view \"" + name + "\" as " + source; switch (tableType) { case FUNCTION: { @@ -579,7 +572,7 @@ public class PostgresDatabase extends AbstractDatabase implements ResultQueryDat break; } default: { - PostgresTableDefinition t = new PostgresTableDefinition(schema, name, comment, tableType, source); + PostgresTableDefinition t = new PostgresTableDefinition(schema, name, comment, tableType, null); result.add(t); map.put(name(schema.getName(), name), t); break; @@ -660,7 +653,8 @@ public class PostgresDatabase extends AbstractDatabase implements ResultQueryDat VIEWS.TABLE_CATALOG, VIEWS.TABLE_SCHEMA, VIEWS.TABLE_NAME, - VIEWS.VIEW_DEFINITION) + when(VIEWS.VIEW_DEFINITION.lower().like(inline("create%")), VIEWS.VIEW_DEFINITION) + .else_(inline("create view \"").concat(VIEWS.TABLE_NAME).concat(inline("\" as ")).concat(VIEWS.VIEW_DEFINITION)).as(VIEWS.VIEW_DEFINITION)) .from(VIEWS) .where(VIEWS.TABLE_SCHEMA.in(schemas)) .orderBy( diff --git a/jOOQ/src/main/java/org/jooq/impl/MetaSQL.java b/jOOQ/src/main/java/org/jooq/impl/MetaSQL.java index c336c6a73d..7db59e7e8e 100644 --- a/jOOQ/src/main/java/org/jooq/impl/MetaSQL.java +++ b/jOOQ/src/main/java/org/jooq/impl/MetaSQL.java @@ -209,15 +209,15 @@ final class MetaSQL { M_SOURCES.put(DERBY, "select cast(null as varchar(32672)) as catalog, alias_57844683.SCHEMANAME, SYS.SYSTABLES.TABLENAME, SYS.SYSVIEWS.VIEWDEFINITION from (SYS.SYSTABLES join SYS.SYSSCHEMAS as alias_57844683 on SYS.SYSTABLES.SCHEMAID = alias_57844683.SCHEMAID) left outer join SYS.SYSVIEWS on SYS.SYSTABLES.TABLEID = SYS.SYSVIEWS.TABLEID where cast(alias_57844683.SCHEMANAME as varchar(32672)) in (cast(? as varchar(32672))) order by alias_57844683.SCHEMANAME, SYS.SYSTABLES.TABLENAME"); - M_SOURCES.put(FIREBIRD, "select null catalog, null schema, trim(RDB$RELATIONS.RDB$RELATION_NAME), trim(RDB$RELATIONS.RDB$VIEW_SOURCE) from RDB$RELATIONS order by trim(RDB$RELATIONS.RDB$RELATION_NAME)"); + M_SOURCES.put(FIREBIRD, "select null catalog, null schema, trim(RDB$RELATIONS.RDB$RELATION_NAME), case when lower(RDB$RELATIONS.RDB$VIEW_SOURCE) like 'create%' then trim(RDB$RELATIONS.RDB$VIEW_SOURCE) else ((('create view \"' || trim(RDB$RELATIONS.RDB$RELATION_NAME)) || '\" as ') || RDB$RELATIONS.RDB$VIEW_SOURCE) end view_source from RDB$RELATIONS order by trim(RDB$RELATIONS.RDB$RELATION_NAME)"); M_SOURCES.put(H2, "select INFORMATION_SCHEMA.VIEWS.TABLE_CATALOG, INFORMATION_SCHEMA.VIEWS.TABLE_SCHEMA, INFORMATION_SCHEMA.VIEWS.TABLE_NAME, ((('create view \"' || INFORMATION_SCHEMA.VIEWS.TABLE_NAME) || '\" as ') || INFORMATION_SCHEMA.VIEWS.VIEW_DEFINITION) VIEW_DEFINITION from INFORMATION_SCHEMA.VIEWS where INFORMATION_SCHEMA.VIEWS.TABLE_SCHEMA in (cast(? as varchar)) order by INFORMATION_SCHEMA.VIEWS.TABLE_SCHEMA, INFORMATION_SCHEMA.VIEWS.TABLE_NAME"); M_SOURCES.put(HSQLDB, "select INFORMATION_SCHEMA.VIEWS.TABLE_CATALOG, INFORMATION_SCHEMA.VIEWS.TABLE_SCHEMA, INFORMATION_SCHEMA.VIEWS.TABLE_NAME, INFORMATION_SCHEMA.VIEWS.VIEW_DEFINITION from INFORMATION_SCHEMA.VIEWS where INFORMATION_SCHEMA.VIEWS.TABLE_SCHEMA in (cast(? as varchar(128))) order by INFORMATION_SCHEMA.VIEWS.TABLE_SCHEMA, INFORMATION_SCHEMA.VIEWS.TABLE_NAME"); - M_SOURCES.put(MARIADB, "select information_schema.VIEWS.TABLE_CATALOG, information_schema.VIEWS.TABLE_SCHEMA, information_schema.VIEWS.TABLE_NAME, information_schema.VIEWS.VIEW_DEFINITION from information_schema.VIEWS where information_schema.VIEWS.TABLE_SCHEMA in (?) order by information_schema.VIEWS.TABLE_SCHEMA, information_schema.VIEWS.TABLE_NAME"); - M_SOURCES.put(MYSQL, "select information_schema.VIEWS.TABLE_CATALOG, information_schema.VIEWS.TABLE_SCHEMA, information_schema.VIEWS.TABLE_NAME, information_schema.VIEWS.VIEW_DEFINITION from information_schema.VIEWS where information_schema.VIEWS.TABLE_SCHEMA in (?) order by information_schema.VIEWS.TABLE_SCHEMA, information_schema.VIEWS.TABLE_NAME"); - M_SOURCES.put(POSTGRES, "select information_schema.views.table_catalog, information_schema.views.table_schema, information_schema.views.table_name, information_schema.views.view_definition from information_schema.views where information_schema.views.table_schema in (?) order by information_schema.views.table_schema, information_schema.views.table_name"); + M_SOURCES.put(MARIADB, "select information_schema.VIEWS.TABLE_CATALOG, information_schema.VIEWS.TABLE_SCHEMA, information_schema.VIEWS.TABLE_NAME, case when lower(information_schema.VIEWS.VIEW_DEFINITION) like 'create%' then information_schema.VIEWS.VIEW_DEFINITION else concat(concat(concat('create view `', information_schema.VIEWS.TABLE_NAME), '` as '), information_schema.VIEWS.VIEW_DEFINITION) end as VIEW_DEFINITION from information_schema.VIEWS where information_schema.VIEWS.TABLE_SCHEMA in (?) order by information_schema.VIEWS.TABLE_SCHEMA, information_schema.VIEWS.TABLE_NAME"); + M_SOURCES.put(MYSQL, "select information_schema.VIEWS.TABLE_CATALOG, information_schema.VIEWS.TABLE_SCHEMA, information_schema.VIEWS.TABLE_NAME, case when lower(information_schema.VIEWS.VIEW_DEFINITION) like 'create%' then information_schema.VIEWS.VIEW_DEFINITION else concat(concat(concat('create view `', information_schema.VIEWS.TABLE_NAME), '` as '), information_schema.VIEWS.VIEW_DEFINITION) end as VIEW_DEFINITION from information_schema.VIEWS where information_schema.VIEWS.TABLE_SCHEMA in (?) order by information_schema.VIEWS.TABLE_SCHEMA, information_schema.VIEWS.TABLE_NAME"); + M_SOURCES.put(POSTGRES, "select information_schema.views.table_catalog, information_schema.views.table_schema, information_schema.views.table_name, case when lower(information_schema.views.view_definition) like 'create%' then information_schema.views.view_definition else ((('create view \"' || information_schema.views.table_name) || '\" as ') || information_schema.views.view_definition) end as view_definition from information_schema.views where information_schema.views.table_schema in (?) order by information_schema.views.table_schema, information_schema.views.table_name"); M_SOURCES.put(SQLITE, "select null as catalog, null as schema, sqlite_master.name, sqlite_master.sql from sqlite_master order by sqlite_master.name"); M_SOURCES.put(TRINO, "select '' TABLE_CATALOG, INFORMATION_SCHEMA.VIEWS.TABLE_SCHEMA, INFORMATION_SCHEMA.VIEWS.TABLE_NAME, case when lower(INFORMATION_SCHEMA.VIEWS.VIEW_DEFINITION) like 'create%' then INFORMATION_SCHEMA.VIEWS.VIEW_DEFINITION else ((('create view \"' || INFORMATION_SCHEMA.VIEWS.TABLE_NAME) || '\" as ') || INFORMATION_SCHEMA.VIEWS.VIEW_DEFINITION) end VIEW_DEFINITION from INFORMATION_SCHEMA.VIEWS where INFORMATION_SCHEMA.VIEWS.TABLE_SCHEMA in (?) order by INFORMATION_SCHEMA.VIEWS.TABLE_SCHEMA, INFORMATION_SCHEMA.VIEWS.TABLE_NAME"); - M_SOURCES.put(YUGABYTEDB, "select information_schema.views.table_catalog, information_schema.views.table_schema, information_schema.views.table_name, information_schema.views.view_definition from information_schema.views where information_schema.views.table_schema in (?) order by information_schema.views.table_schema, information_schema.views.table_name"); + M_SOURCES.put(YUGABYTEDB, "select information_schema.views.table_catalog, information_schema.views.table_schema, information_schema.views.table_name, case when lower(information_schema.views.view_definition) like 'create%' then information_schema.views.view_definition else ((('create view \"' || information_schema.views.table_name) || '\" as ') || information_schema.views.view_definition) end as view_definition from information_schema.views where information_schema.views.table_schema in (?) order by information_schema.views.table_schema, information_schema.views.table_name");