[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.
This commit is contained in:
parent
a73ec779a3
commit
350c19ce6e
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -89,17 +89,25 @@ final class DDL {
|
||||
|
||||
private final Query createTable(Table<?> table, Collection<? extends Constraint> 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()) {
|
||||
|
||||
@ -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<MutableTable> tables() {
|
||||
// TODO: Make this lazy
|
||||
List<MutableTable> 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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user