diff --git a/jOOQ/src/main/java/org/jooq/Clause.java b/jOOQ/src/main/java/org/jooq/Clause.java index 703dea361e..05a4aa4a3b 100644 --- a/jOOQ/src/main/java/org/jooq/Clause.java +++ b/jOOQ/src/main/java/org/jooq/Clause.java @@ -1123,6 +1123,22 @@ public enum Clause { */ ALTER_INDEX_RENAME, + /** + * A complete DROP SCHEMA statement. + */ + DROP_SCHEMA, + + /** + * A SCHEMA clause within an {@link #DROP_SCHEMA} statement. + *

+ * This clause surrounds + *

+ */ + DROP_SCHEMA_SCHEMA, + /** * A complete DROP VIEW statement. */ diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index 07ae0e52de..53578be49b 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -6953,6 +6953,30 @@ public interface DSLContext extends Scope , AutoCloseable { @Support({ POSTGRES }) CreateSchemaFinalStep createSchema(Schema schema); + /** + * Create a new DSL CREATE SCHEMA statement. + * + * @see DSL#createSchemaIfNotExists(String) + */ + @Support({ POSTGRES }) + CreateSchemaFinalStep createSchemaIfNotExists(String schema); + + /** + * Create a new DSL CREATE SCHEMA statement. + * + * @see DSL#createSchemaIfNotExists(Name) + */ + @Support({ POSTGRES }) + CreateSchemaFinalStep createSchemaIfNotExists(Name schema); + + /** + * Create a new DSL CREATE SCHEMA statement. + * + * @see DSL#createSchemaIfNotExists(Schema) + */ + @Support({ POSTGRES }) + CreateSchemaFinalStep createSchemaIfNotExists(Schema schema); + /** * Create a new DSL CREATE TABLE statement. * @@ -7385,6 +7409,54 @@ public interface DSLContext extends Scope , AutoCloseable { @Support({ POSTGRES }) AlterIndexStep alterIndexIfExists(Name index); + /** + * Create a new DSL DROP SCHEMA statement. + * + * @see DSL#dropSchema(String) + */ + @Support({ POSTGRES }) + DropSchemaStep dropSchema(String schema); + + /** + * Create a new DSL DROP SCHEMA statement. + * + * @see DSL#dropSchema(Name) + */ + @Support({ POSTGRES }) + DropSchemaStep dropSchema(Name schema); + + /** + * Create a new DSL DROP SCHEMA statement. + * + * @see DSL#dropSchema(Schema) + */ + @Support({ POSTGRES }) + DropSchemaStep dropSchema(Schema schema); + + /** + * Create a new DSL DROP SCHEMA statement. + * + * @see DSL#dropSchemaIfExists(String) + */ + @Support({ POSTGRES }) + DropSchemaStep dropSchemaIfExists(String schema); + + /** + * Create a new DSL DROP SCHEMA statement. + * + * @see DSL#dropSchemaIfExists(Name) + */ + @Support({ POSTGRES }) + DropSchemaStep dropSchemaIfExists(Name schema); + + /** + * Create a new DSL DROP SCHEMA statement. + * + * @see DSL#dropSchemaIfExists(Schema) + */ + @Support({ POSTGRES }) + DropSchemaStep dropSchemaIfExists(Schema schema); + /** * Create a new DSL DROP VIEW statement. * diff --git a/jOOQ/src/main/java/org/jooq/DropSchemaFinalStep.java b/jOOQ/src/main/java/org/jooq/DropSchemaFinalStep.java new file mode 100644 index 0000000000..3352c2f79b --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/DropSchemaFinalStep.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq; + +/** + * The final step in the DROP SCHEMA DSL. + * + * @author Lukas Eder + */ +public interface DropSchemaFinalStep extends DDLQuery { + +} diff --git a/jOOQ/src/main/java/org/jooq/DropSchemaStep.java b/jOOQ/src/main/java/org/jooq/DropSchemaStep.java new file mode 100644 index 0000000000..008eb0efe9 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/DropSchemaStep.java @@ -0,0 +1,64 @@ +/** + * Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq; + +/** + * The step in the DROP SCHEMA DSL used to specify + * DROP behaviour. + * + * @author Lukas Eder + */ +public interface DropSchemaStep extends DropSchemaFinalStep { + + /** + * Add a CASCADE clause to the DROP SCHEMA + * statement. + */ + @Support + DropSchemaFinalStep cascade(); + + /** + * Add a RESTRICT clause to the DROP SCHEMA + * statement. + */ + @Support + DropSchemaFinalStep restrict(); +} diff --git a/jOOQ/src/main/java/org/jooq/impl/CreateSchemaImpl.java b/jOOQ/src/main/java/org/jooq/impl/CreateSchemaImpl.java index 9a4e7a0b35..861d727813 100644 --- a/jOOQ/src/main/java/org/jooq/impl/CreateSchemaImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/CreateSchemaImpl.java @@ -66,11 +66,13 @@ final class CreateSchemaImpl extends AbstractQuery implements private static final Clause[] CLAUSES = { CREATE_SCHEMA }; private final Schema schema; + private final boolean ifNotExists; - CreateSchemaImpl(Configuration configuration, Schema schema) { + CreateSchemaImpl(Configuration configuration, Schema schema, boolean ifNotExists) { super(configuration); this.schema = schema; + this.ifNotExists = ifNotExists; } // ------------------------------------------------------------------------ @@ -84,8 +86,12 @@ final class CreateSchemaImpl extends AbstractQuery implements @Override public final void accept(Context ctx) { ctx.start(CREATE_SCHEMA_NAME) - .keyword("create schema") - .sql(' ').visit(schema) + .keyword("create schema"); + + if (ifNotExists) + ctx.sql(' ').keyword("if not exists"); + + ctx.sql(' ').visit(schema) .end(CREATE_SCHEMA_NAME); } diff --git a/jOOQ/src/main/java/org/jooq/impl/DDLStatementType.java b/jOOQ/src/main/java/org/jooq/impl/DDLStatementType.java index 7b8ec268f4..67af8e3217 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DDLStatementType.java +++ b/jOOQ/src/main/java/org/jooq/impl/DDLStatementType.java @@ -48,9 +48,11 @@ enum DDLStatementType { CREATE_SEQUENCE, CREATE_TABLE, CREATE_VIEW, + CREATE_SCHEMA, DROP_INDEX, DROP_SEQUENCE, DROP_TABLE, - DROP_VIEW + DROP_VIEW, + DROP_SCHEMA } diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 77676b1797..8c6579380b 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -126,6 +126,7 @@ import org.jooq.Delete; import org.jooq.DeleteWhereStep; import org.jooq.DerivedColumnList; import org.jooq.DropIndexOnStep; +import org.jooq.DropSchemaStep; import org.jooq.DropSequenceFinalStep; import org.jooq.DropTableStep; import org.jooq.DropViewFinalStep; @@ -4828,6 +4829,36 @@ public class DSL { return using(new DefaultConfiguration()).createSchema(schema); } + /** + * Create a new DSL CREATE SCHEMA statement. + * + * @see DSLContext#createSchemaIfNotExists(String) + */ + @Support({ POSTGRES }) + public static CreateSchemaFinalStep createSchemaIfNotExists(String schema) { + return using(new DefaultConfiguration()).createSchemaIfNotExists(schema); + } + + /** + * Create a new DSL CREATE SCHEMA statement. + * + * @see DSLContext#createSchemaIfNotExists(Name) + */ + @Support({ POSTGRES }) + public static CreateSchemaFinalStep createSchemaIfNotExists(Name table) { + return using(new DefaultConfiguration()).createSchemaIfNotExists(table); + } + + /** + * Create a new DSL CREATE SCHEMA statement. + * + * @see DSLContext#createSchemaIfNotExists(Schema) + */ + @Support({ POSTGRES }) + public static CreateSchemaFinalStep createSchemaIfNotExists(Schema schema) { + return using(new DefaultConfiguration()).createSchemaIfNotExists(schema); + } + /** * Create a new DSL CREATE TABLE statement. @@ -5369,6 +5400,66 @@ public class DSL { return using(new DefaultConfiguration()).alterIndexIfExists(index); } + /** + * Create a new DSL DROP SCHEMA statement. + * + * @see DSLContext#dropSchema(String) + */ + @Support({ POSTGRES }) + public static DropSchemaStep dropSchema(String schema){ + return using(new DefaultConfiguration()).dropSchema(schema); + } + + /** + * Create a new DSL DROP SCHEMA statement. + * + * @see DSLContext#dropSchema(Name) + */ + @Support({ POSTGRES }) + public static DropSchemaStep dropSchema(Name schema){ + return using(new DefaultConfiguration()).dropSchema(schema); + } + + /** + * Create a new DSL DROP SCHEMA statement. + * + * @see DSLContext#dropSchema(Schema) + */ + @Support({ POSTGRES }) + public static DropSchemaStep dropSchema(Schema schema){ + return using(new DefaultConfiguration()).dropSchema(schema); + } + + /** + * Create a new DSL DROP SCHEMA statement. + * + * @see DSLContext#dropSchemaIfExists(String) + */ + @Support({ POSTGRES }) + public static DropSchemaStep dropSchemaIfExists(String schema){ + return using(new DefaultConfiguration()).dropSchemaIfExists(schema); + } + + /** + * Create a new DSL DROP SCHEMA statement. + * + * @see DSLContext#dropSchemaIfExists(Name) + */ + @Support({ POSTGRES }) + public static DropSchemaStep dropSchemaIfExists(Name schema){ + return using(new DefaultConfiguration()).dropSchemaIfExists(schema); + } + + /** + * Create a new DSL DROP SCHEMA statement. + * + * @see DSLContext#dropSchemaIfExists(Schema) + */ + @Support({ POSTGRES }) + public static DropSchemaStep dropSchemaIfExists(Schema schema){ + return using(new DefaultConfiguration()).dropSchemaIfExists(schema); + } + /** * Create a new DSL DROP VIEW statement. * diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index f8c7e6a9dc..b747b71a8d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -106,6 +106,7 @@ import org.jooq.DataType; import org.jooq.DeleteQuery; import org.jooq.DeleteWhereStep; import org.jooq.DropIndexOnStep; +import org.jooq.DropSchemaStep; import org.jooq.DropSequenceFinalStep; import org.jooq.DropTableStep; import org.jooq.DropViewFinalStep; @@ -2395,7 +2396,22 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri @Override public CreateSchemaFinalStep createSchema(Schema schema) { - return new CreateSchemaImpl(configuration(), schema); + return new CreateSchemaImpl(configuration(), schema, false); + } + + @Override + public CreateSchemaFinalStep createSchemaIfNotExists(String schema) { + return createSchemaIfNotExists(name(schema)); + } + + @Override + public CreateSchemaFinalStep createSchemaIfNotExists(Name schema) { + return createSchemaIfNotExists(schema(schema)); + } + + @Override + public CreateSchemaFinalStep createSchemaIfNotExists(Schema schema) { + return new CreateSchemaImpl(configuration(), schema, true); } @Override @@ -2638,6 +2654,36 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return new AlterIndexImpl(configuration(), index, true); } + @Override + public DropSchemaStep dropSchema(String schema) { + return dropSchema(name(schema)); + } + + @Override + public DropSchemaStep dropSchema(Name schema) { + return dropSchema(schema(schema)); + } + + @Override + public DropSchemaStep dropSchema(Schema schema) { + return new DropSchemaImpl(configuration(), schema); + } + + @Override + public DropSchemaStep dropSchemaIfExists(String schema) { + return dropSchemaIfExists(name(schema)); + } + + @Override + public DropSchemaStep dropSchemaIfExists(Name schema) { + return dropSchemaIfExists(schema(schema)); + } + + @Override + public DropSchemaStep dropSchemaIfExists(Schema schema) { + return new DropSchemaImpl(configuration(), schema, true); + } + @Override public DropViewFinalStep dropView(String view) { return dropView(name(view)); diff --git a/jOOQ/src/main/java/org/jooq/impl/DropSchemaImpl.java b/jOOQ/src/main/java/org/jooq/impl/DropSchemaImpl.java new file mode 100644 index 0000000000..0cec39335a --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/DropSchemaImpl.java @@ -0,0 +1,148 @@ +/** + * Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com) + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.impl; + +import static java.util.Arrays.asList; +import static org.jooq.Clause.DROP_SCHEMA; +import static org.jooq.Clause.DROP_SCHEMA_SCHEMA; +// ... +// ... +// ... +import static org.jooq.SQLDialect.DERBY; +import static org.jooq.SQLDialect.FIREBIRD; +// ... +// ... +// ... + +import org.jooq.Clause; +import org.jooq.Configuration; +import org.jooq.Context; +import org.jooq.DropSchemaFinalStep; +import org.jooq.DropSchemaStep; +import org.jooq.Schema; + + +/** + * @author Lukas Eder + */ +final class DropSchemaImpl extends AbstractQuery implements + + // Cascading interface implementations for DROP VIEW behaviour + DropSchemaStep { + + /** + * Generated UID + */ + private static final long serialVersionUID = 8904572826501186329L; + private static final Clause[] CLAUSES = { DROP_SCHEMA }; + + private final Schema schema; + private final boolean ifExists; + private boolean cascade; + + DropSchemaImpl(Configuration configuration, Schema schema) { + this(configuration, schema, false); + } + + DropSchemaImpl(Configuration configuration, Schema schema, boolean ifExists) { + super(configuration); + + this.schema = schema; + this.ifExists = ifExists; + } + + // ------------------------------------------------------------------------ + // XXX: DSL API + // ------------------------------------------------------------------------ + + @Override + public final DropSchemaFinalStep cascade() { + cascade = true; + return this; + } + + @Override + public final DropSchemaFinalStep restrict() { + cascade = false; + return this; + } + + // ------------------------------------------------------------------------ + // XXX: QueryPart API + // ------------------------------------------------------------------------ + + private final boolean supportsIfExists(Context ctx) { + return !asList(DERBY, FIREBIRD).contains(ctx.family()); + } + + @Override + public final void accept(Context ctx) { + if (ifExists && !supportsIfExists(ctx)) { + Tools.executeImmediateBegin(ctx, DDLStatementType.DROP_SCHEMA); + accept0(ctx); + Tools.executeImmediateEnd(ctx, DDLStatementType.DROP_SCHEMA); + } + else { + accept0(ctx); + } + } + + private void accept0(Context ctx) { + ctx.start(DROP_SCHEMA_SCHEMA) + .keyword("drop schema"); + + if (ifExists && supportsIfExists(ctx)) + ctx.sql(' ').keyword("if exists"); + + ctx.sql(' ').visit(schema); + + if (cascade) + ctx.sql(' ').keyword("cascade"); + + ctx.end(DROP_SCHEMA_SCHEMA); + } + + + @Override + public final Clause[] clauses(Context ctx) { + return CLAUSES; + } +}