[#5279] Add support for DROP SCHEMA [ IF EXISTS ] .. [ CASCADE | RESTRICT ]

This commit is contained in:
lukaseder 2016-05-17 14:08:54 +02:00
parent 0d7d85a286
commit 7c697937b5
9 changed files with 500 additions and 5 deletions

View File

@ -1123,6 +1123,22 @@ public enum Clause {
*/
ALTER_INDEX_RENAME,
/**
* A complete <code>DROP SCHEMA</code> statement.
*/
DROP_SCHEMA,
/**
* A <code>SCHEMA</code> clause within an {@link #DROP_SCHEMA} statement.
* <p>
* This clause surrounds
* <ul>
* <li>the <code>DROP SCHEMA</code> keywords</li>
* <li>the schema that is being dropped</li>
* </ul>
*/
DROP_SCHEMA_SCHEMA,
/**
* A complete <code>DROP VIEW</code> statement.
*/

View File

@ -6953,6 +6953,30 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ POSTGRES })
CreateSchemaFinalStep createSchema(Schema schema);
/**
* Create a new DSL <code>CREATE SCHEMA</code> statement.
*
* @see DSL#createSchemaIfNotExists(String)
*/
@Support({ POSTGRES })
CreateSchemaFinalStep createSchemaIfNotExists(String schema);
/**
* Create a new DSL <code>CREATE SCHEMA</code> statement.
*
* @see DSL#createSchemaIfNotExists(Name)
*/
@Support({ POSTGRES })
CreateSchemaFinalStep createSchemaIfNotExists(Name schema);
/**
* Create a new DSL <code>CREATE SCHEMA</code> statement.
*
* @see DSL#createSchemaIfNotExists(Schema)
*/
@Support({ POSTGRES })
CreateSchemaFinalStep createSchemaIfNotExists(Schema schema);
/**
* Create a new DSL <code>CREATE TABLE</code> statement.
*
@ -7385,6 +7409,54 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ POSTGRES })
AlterIndexStep alterIndexIfExists(Name index);
/**
* Create a new DSL <code>DROP SCHEMA</code> statement.
*
* @see DSL#dropSchema(String)
*/
@Support({ POSTGRES })
DropSchemaStep dropSchema(String schema);
/**
* Create a new DSL <code>DROP SCHEMA</code> statement.
*
* @see DSL#dropSchema(Name)
*/
@Support({ POSTGRES })
DropSchemaStep dropSchema(Name schema);
/**
* Create a new DSL <code>DROP SCHEMA</code> statement.
*
* @see DSL#dropSchema(Schema)
*/
@Support({ POSTGRES })
DropSchemaStep dropSchema(Schema schema);
/**
* Create a new DSL <code>DROP SCHEMA</code> statement.
*
* @see DSL#dropSchemaIfExists(String)
*/
@Support({ POSTGRES })
DropSchemaStep dropSchemaIfExists(String schema);
/**
* Create a new DSL <code>DROP SCHEMA</code> statement.
*
* @see DSL#dropSchemaIfExists(Name)
*/
@Support({ POSTGRES })
DropSchemaStep dropSchemaIfExists(Name schema);
/**
* Create a new DSL <code>DROP SCHEMA</code> statement.
*
* @see DSL#dropSchemaIfExists(Schema)
*/
@Support({ POSTGRES })
DropSchemaStep dropSchemaIfExists(Schema schema);
/**
* Create a new DSL <code>DROP VIEW</code> statement.
*

View File

@ -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 <code>DROP SCHEMA</code> DSL.
*
* @author Lukas Eder
*/
public interface DropSchemaFinalStep extends DDLQuery {
}

View File

@ -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 <code>DROP SCHEMA</code> DSL used to specify
* <code>DROP</code> behaviour.
*
* @author Lukas Eder
*/
public interface DropSchemaStep extends DropSchemaFinalStep {
/**
* Add a <code>CASCADE</code> clause to the <code>DROP SCHEMA</code>
* statement.
*/
@Support
DropSchemaFinalStep cascade();
/**
* Add a <code>RESTRICT</code> clause to the <code>DROP SCHEMA</code>
* statement.
*/
@Support
DropSchemaFinalStep restrict();
}

View File

@ -66,11 +66,13 @@ final class CreateSchemaImpl<R extends Record> 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<R extends Record> 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);
}

View File

@ -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
}

View File

@ -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 <code>CREATE SCHEMA</code> statement.
*
* @see DSLContext#createSchemaIfNotExists(String)
*/
@Support({ POSTGRES })
public static CreateSchemaFinalStep createSchemaIfNotExists(String schema) {
return using(new DefaultConfiguration()).createSchemaIfNotExists(schema);
}
/**
* Create a new DSL <code>CREATE SCHEMA</code> statement.
*
* @see DSLContext#createSchemaIfNotExists(Name)
*/
@Support({ POSTGRES })
public static CreateSchemaFinalStep createSchemaIfNotExists(Name table) {
return using(new DefaultConfiguration()).createSchemaIfNotExists(table);
}
/**
* Create a new DSL <code>CREATE SCHEMA</code> statement.
*
* @see DSLContext#createSchemaIfNotExists(Schema)
*/
@Support({ POSTGRES })
public static CreateSchemaFinalStep createSchemaIfNotExists(Schema schema) {
return using(new DefaultConfiguration()).createSchemaIfNotExists(schema);
}
/**
* Create a new DSL <code>CREATE TABLE</code> statement.
@ -5369,6 +5400,66 @@ public class DSL {
return using(new DefaultConfiguration()).alterIndexIfExists(index);
}
/**
* Create a new DSL <code>DROP SCHEMA</code> statement.
*
* @see DSLContext#dropSchema(String)
*/
@Support({ POSTGRES })
public static DropSchemaStep dropSchema(String schema){
return using(new DefaultConfiguration()).dropSchema(schema);
}
/**
* Create a new DSL <code>DROP SCHEMA</code> statement.
*
* @see DSLContext#dropSchema(Name)
*/
@Support({ POSTGRES })
public static DropSchemaStep dropSchema(Name schema){
return using(new DefaultConfiguration()).dropSchema(schema);
}
/**
* Create a new DSL <code>DROP SCHEMA</code> statement.
*
* @see DSLContext#dropSchema(Schema)
*/
@Support({ POSTGRES })
public static DropSchemaStep dropSchema(Schema schema){
return using(new DefaultConfiguration()).dropSchema(schema);
}
/**
* Create a new DSL <code>DROP SCHEMA</code> statement.
*
* @see DSLContext#dropSchemaIfExists(String)
*/
@Support({ POSTGRES })
public static DropSchemaStep dropSchemaIfExists(String schema){
return using(new DefaultConfiguration()).dropSchemaIfExists(schema);
}
/**
* Create a new DSL <code>DROP SCHEMA</code> statement.
*
* @see DSLContext#dropSchemaIfExists(Name)
*/
@Support({ POSTGRES })
public static DropSchemaStep dropSchemaIfExists(Name schema){
return using(new DefaultConfiguration()).dropSchemaIfExists(schema);
}
/**
* Create a new DSL <code>DROP SCHEMA</code> statement.
*
* @see DSLContext#dropSchemaIfExists(Schema)
*/
@Support({ POSTGRES })
public static DropSchemaStep dropSchemaIfExists(Schema schema){
return using(new DefaultConfiguration()).dropSchemaIfExists(schema);
}
/**
* Create a new DSL <code>DROP VIEW</code> statement.
*

View File

@ -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));

View File

@ -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;
}
}