From 350c19ce6e0f59ca125dec41d5c72ba4e3c45fe2 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 31 Oct 2019 17:27:30 +0100 Subject: [PATCH] [jOOQ/jOOQ#2370] [jOOQ/jOOQ#8528] Correctly interpret CREATE VIEW So far, there was no way for the jOOQ runtime meta model to remember what SELECT statement was used in the creation of a view. We now have this model in TableOptions, so we can start using it, e.g. in the interpreter. --- jOOQ/src/main/java/org/jooq/TableOptions.java | 16 +++++++++++- jOOQ/src/main/java/org/jooq/impl/DDL.java | 26 ++++++++++++------- .../java/org/jooq/impl/DDLInterpreter.java | 22 +++++----------- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/TableOptions.java b/jOOQ/src/main/java/org/jooq/TableOptions.java index e77ecc66ea..725eb1dc51 100644 --- a/jOOQ/src/main/java/org/jooq/TableOptions.java +++ b/jOOQ/src/main/java/org/jooq/TableOptions.java @@ -193,7 +193,21 @@ public final class TableOptions implements Serializable { /** * A table type that is unknown to jOOQ. */ - UNKNOWN + UNKNOWN; + + /** + * Whether the type is a view. + */ + public final boolean isView() { + return this == VIEW || this == MATERIALIZED_VIEW; + } + + /** + * Whether the type is a view. + */ + public final boolean isTable() { + return this == TABLE || this == TEMPORARY; + } } /** diff --git a/jOOQ/src/main/java/org/jooq/impl/DDL.java b/jOOQ/src/main/java/org/jooq/impl/DDL.java index 6e8a5c35b6..3cb7bd21f0 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DDL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DDL.java @@ -89,17 +89,25 @@ final class DDL { private final Query createTable(Table table, Collection constraints) { boolean temporary = table.getType() == TableType.TEMPORARY; + boolean view = table.getType().isView(); OnCommit onCommit = table.getOptions().onCommit(); - CreateTableOnCommitStep s0 = (configuration.createTableIfNotExists() - ? temporary - ? ctx.createTemporaryTableIfNotExists(table) - : ctx.createTableIfNotExists(table) - : temporary - ? ctx.createTemporaryTable(table) - : ctx.createTable(table)) - .columns(sortIf(Arrays.asList(table.fields()), !configuration.respectColumnOrder())) - .constraints(constraints); + if (view) + return (configuration.createTableIfNotExists() + ? ctx.createViewIfNotExists(table, table.fields()) + : ctx.createView(table, table.fields())) + .as(table.getOptions().select()); + + CreateTableOnCommitStep s0 = + (configuration.createTableIfNotExists() + ? temporary + ? ctx.createTemporaryTableIfNotExists(table) + : ctx.createTableIfNotExists(table) + : temporary + ? ctx.createTemporaryTable(table) + : ctx.createTable(table)) + .columns(sortIf(Arrays.asList(table.fields()), !configuration.respectColumnOrder())) + .constraints(constraints); if (temporary && onCommit != null) { switch (table.getOptions().onCommit()) { diff --git a/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java b/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java index b0d57a5fe7..67125383c2 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java +++ b/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java @@ -380,7 +380,7 @@ final class DDLInterpreter { return; } - else if (!table(existing)) + else if (!existing.options.type().isTable()) throw objectNotTable(table); // TODO: Multi-add statements @@ -542,7 +542,7 @@ final class DDLInterpreter { return; } - else if (!table(existing)) + else if (!existing.options.type().isTable()) throw objectNotTable(table); else if (query.$temporary() && existing.options.type() != TableType.TEMPORARY) throw objectNotTemporaryTable(table); @@ -556,7 +556,7 @@ final class DDLInterpreter { MutableTable existing = schema.table(table); if (existing != null) { - if (!view(existing)) + if (!existing.options.type().isView()) throw objectNotView(table); else if (query.$orReplace()) drop(schema.tables, existing, RESTRICT); @@ -584,7 +584,7 @@ final class DDLInterpreter { return; } - else if (!view(existing)) + else if (!existing.options.type().isView()) throw objectNotView(table); Table renameTo = query.$renameTo(); @@ -605,7 +605,7 @@ final class DDLInterpreter { return; } - else if (!view(existing)) + else if (!existing.options.type().isView()) throw objectNotView(table); drop(schema.tables, existing, RESTRICT); @@ -827,16 +827,6 @@ final class DDLInterpreter { // Auxiliary methods // ------------------------------------------------------------------------- - private static final boolean view(MutableTable mt) { - TableType type = mt.options.type(); - return type == TableType.VIEW || type == TableType.MATERIALIZED_VIEW; - } - - private static final boolean table(MutableTable mt) { - TableType type = mt.options.type(); - return type == TableType.TABLE || type == TableType.TEMPORARY; - } - private final Iterable tables() { // TODO: Make this lazy List result = new ArrayList<>(); @@ -956,7 +946,7 @@ final class DDLInterpreter { } private static final DataDefinitionException alreadyExists(Table t, MutableTable mt) { - if (view(mt)) + if (mt.options.type().isView()) return viewAlreadyExists(t); else return tableAlreadyExists(t);