diff --git a/jOOQ/src/main/java/org/jooq/impl/Interpreter.java b/jOOQ/src/main/java/org/jooq/impl/Interpreter.java index 63cbb1110b..1b87659670 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Interpreter.java +++ b/jOOQ/src/main/java/org/jooq/impl/Interpreter.java @@ -312,7 +312,7 @@ final class Interpreter { private final void accept0(CreateSchemaImpl query) { Schema schema = query.$schema(); - if (getSchema(schema, false) != null) { + if (getSchema(schema, false, false) != null) { if (!query.$ifNotExists()) throw alreadyExists(schema); @@ -325,18 +325,14 @@ final class Interpreter { private final void accept0(AlterSchemaImpl query) { Schema schema = query.$schema(); - MutableSchema oldSchema = getSchema(schema); - if (oldSchema == null) { - if (!query.$ifExists()) - throw notExists(schema); - + MutableSchema oldSchema = getSchema(schema, false, !query.$ifExists()); + if (oldSchema == null) return; - } if (query.$renameTo() != null) { Schema renameTo = query.$renameTo(); - if (getSchema(renameTo, false) != null) + if (getSchema(renameTo, false, false) != null) throw alreadyExists(renameTo); oldSchema.name((UnqualifiedName) renameTo.getUnqualifiedName()); @@ -348,16 +344,11 @@ final class Interpreter { private final void accept0(DropSchemaImpl query) { Schema schema = query.$schema(); - MutableSchema mutableSchema = getSchema(schema); - - if (mutableSchema == null) { - if (!query.$ifExists()) - throw notExists(schema); + MutableSchema mutableSchema = getSchema(schema, false, !query.$ifExists()); + if (mutableSchema == null) return; - } - - if (mutableSchema.isEmpty() || query.$cascade() == Cascade.CASCADE) + else if (mutableSchema.isEmpty() || query.$cascade() == Cascade.CASCADE) mutableSchema.catalog.schemas.remove(mutableSchema); else throw schemaNotEmpty(schema); @@ -420,10 +411,6 @@ final class Interpreter { private final void addForeignKey0(MutableTable mt, QOM.ForeignKey key) { MutableSchema ms = getSchema(key.$referencesTable().getSchema()); - - if (ms == null) - throw notExists(key.$referencesTable().getSchema()); - MutableTable mrf = ms.table(key.$referencesTable(), true); MutableUniqueKey mu = null; @@ -551,7 +538,6 @@ final class Interpreter { private final void accept0(AlterTableImpl query) { Table table = query.$table(); MutableSchema schema = getSchema(table.getSchema()); - MutableTable existing = schema.table(table, true); if (existing == null) { if (!query.$ifExists()) @@ -1330,11 +1316,7 @@ final class Interpreter { } private final void accept0(SetSchema query) { - MutableSchema schema = getSchema(query.$schema()); - if (schema == null) - throw notExists(query.$schema()); - - currentSchema = schema; + currentSchema = getSchema(query.$schema()); } private final void accept0(SetCommand query) { @@ -1436,6 +1418,10 @@ final class Interpreter { } private final MutableSchema getSchema(Schema input, boolean create) { + return getSchema(input, create, true); + } + + private final MutableSchema getSchema(Schema input, boolean create, boolean throwIfNotExists) { if (input == null) return currentSchema(create); @@ -1450,9 +1436,14 @@ final class Interpreter { return null; MutableSchema schema = defaultSchema; - if ((schema = find(catalog.schemas, input)) == null && create) + if ((schema = find(catalog.schemas, input)) == null) { + // TODO createSchemaIfNotExists should probably be configurable - schema = new MutableSchema((UnqualifiedName) input.getUnqualifiedName(), catalog, true); + if (create) + schema = new MutableSchema((UnqualifiedName) input.getUnqualifiedName(), catalog, true); + else if (throwIfNotExists) + throw notExists(input); + } return schema; } @@ -1471,7 +1462,7 @@ final class Interpreter { return defaultSchema; InterpreterSearchSchema schema = searchPath.get(0); - return getSchema(schema(name(schema.getCatalog(), schema.getSchema())), create); + return getSchema(schema(name(schema.getCatalog(), schema.getSchema())), create, false); } private final MutableTable newTable( @@ -1515,7 +1506,8 @@ final class Interpreter { } private final MutableTable table(Table table, boolean throwIfNotExists) { - MutableTable result = getSchema(table.getSchema()).table(table, true); + MutableTable result = getSchema(table.getSchema(), false, throwIfNotExists).table(table, true); + if (result == null && throwIfNotExists) throw notExists(table); @@ -1597,7 +1589,10 @@ final class Interpreter { MutableTable table = table(DSL.table(field.getQualifiedName().qualifier()), throwIfNotExists); if (table == null) - return null; + if (throwIfNotExists) + throw notExists(field); + else + return null; MutableField result = find(table.fields, field);