[jOOQ/jOOQ#10210] Support CREATE, ALTER, DROP DATABASE

This commit is contained in:
Lukas Eder 2020-05-20 15:42:17 +02:00
parent 4971d8aad9
commit c29f2dbac7
7 changed files with 706 additions and 0 deletions

View File

@ -0,0 +1,65 @@
/*
* 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.
* <p>
* <h3>Referencing <code>XYZ*Step</code> types directly from client code</h3>
* <p>
* It is usually not recommended to reference any <code>XYZ*Step</code> types
* directly from client code, or assign them to local variables. When writing
* dynamic SQL, creating a statement's components dynamically, and passing them
* to the DSL API statically is usually a better choice. See the manual's
* section about dynamic SQL for details: <a href=
* "https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql">https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql</a>.
* <p>
* Drawbacks of referencing the <code>XYZ*Step</code> types directly:
* <ul>
* <li>They're operating on mutable implementations (as of jOOQ 3.x)</li>
* <li>They're less composable and not easy to get right when dynamic SQL gets
* complex</li>
* <li>They're less readable</li>
* <li>They might have binary incompatible changes between minor releases</li>
* </ul>
*
* @author Lukas Eder
*/
public interface AlterDatabaseFinalStep extends DDLQuery {
}

View File

@ -0,0 +1,92 @@
/*
* 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;
// ...
// ...
// ...
import static org.jooq.SQLDialect.POSTGRES;
/**
* The step in the <code>ALTER DATABASE</code> DSL used to specify
* <code>ALTER</code> behaviour.
* <p>
* <h3>Referencing <code>XYZ*Step</code> types directly from client code</h3>
* <p>
* It is usually not recommended to reference any <code>XYZ*Step</code> types
* directly from client code, or assign them to local variables. When writing
* dynamic SQL, creating a statement's components dynamically, and passing them
* to the DSL API statically is usually a better choice. See the manual's
* section about dynamic SQL for details: <a href=
* "https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql">https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql</a>.
* <p>
* Drawbacks of referencing the <code>XYZ*Step</code> types directly:
* <ul>
* <li>They're operating on mutable implementations (as of jOOQ 3.x)</li>
* <li>They're less composable and not easy to get right when dynamic SQL gets
* complex</li>
* <li>They're less readable</li>
* <li>They might have binary incompatible changes between minor releases</li>
* </ul>
*
* @author Lukas Eder
*/
public interface AlterDatabaseStep {
/**
* Add a <code>RENAME TO</code> clause to the <code>ALTER DATABASE</code>
* statement.
*/
@Support({ POSTGRES })
AlterDatabaseFinalStep renameTo(Catalog newName);
/**
* Add a <code>RENAME TO</code> clause to the <code>ALTER DATABASE</code>
* statement.
*/
@Support({ POSTGRES })
AlterDatabaseFinalStep renameTo(Name newName);
/**
* Add a <code>RENAME TO</code> clause to the <code>ALTER DATABASE</code>
* statement.
*/
@Support({ POSTGRES })
AlterDatabaseFinalStep renameTo(String newName);
}

View File

@ -0,0 +1,65 @@
/*
* 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;
/**
* A {@link Query} that can create databases.
* <p>
* <h3>Referencing <code>XYZ*Step</code> types directly from client code</h3>
* <p>
* It is usually not recommended to reference any <code>XYZ*Step</code> types
* directly from client code, or assign them to local variables. When writing
* dynamic SQL, creating a statement's components dynamically, and passing them
* to the DSL API statically is usually a better choice. See the manual's
* section about dynamic SQL for details: <a href=
* "https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql">https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql</a>.
* <p>
* Drawbacks of referencing the <code>XYZ*Step</code> types directly:
* <ul>
* <li>They're operating on mutable implementations (as of jOOQ 3.x)</li>
* <li>They're less composable and not easy to get right when dynamic SQL gets
* complex</li>
* <li>They're less readable</li>
* <li>They might have binary incompatible changes between minor releases</li>
* </ul>
*
* @author Lukas Eder
*/
public interface CreateDatabaseFinalStep extends DDLQuery {
}

View File

@ -0,0 +1,65 @@
/*
* 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.
* <p>
* <h3>Referencing <code>XYZ*Step</code> types directly from client code</h3>
* <p>
* It is usually not recommended to reference any <code>XYZ*Step</code> types
* directly from client code, or assign them to local variables. When writing
* dynamic SQL, creating a statement's components dynamically, and passing them
* to the DSL API statically is usually a better choice. See the manual's
* section about dynamic SQL for details: <a href=
* "https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql">https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql</a>.
* <p>
* Drawbacks of referencing the <code>XYZ*Step</code> types directly:
* <ul>
* <li>They're operating on mutable implementations (as of jOOQ 3.x)</li>
* <li>They're less composable and not easy to get right when dynamic SQL gets
* complex</li>
* <li>They're less readable</li>
* <li>They might have binary incompatible changes between minor releases</li>
* </ul>
*
* @author Lukas Eder
*/
public interface DropDatabaseFinalStep extends DDLQuery {
}

View File

@ -0,0 +1,172 @@
/*
* 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 org.jooq.SQLDialect.POSTGRES;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.Keywords.K_ALTER;
import static org.jooq.impl.Keywords.K_DATABASE;
import static org.jooq.impl.Keywords.K_IF_EXISTS;
import static org.jooq.impl.Keywords.K_RENAME;
import static org.jooq.impl.Keywords.K_RENAME_TO;
import static org.jooq.impl.Keywords.K_TO;
import java.util.Set;
import org.jooq.AlterDatabaseFinalStep;
import org.jooq.AlterDatabaseStep;
import org.jooq.Catalog;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.Name;
// ...
import org.jooq.SQLDialect;
/**
* @author Lukas Eder
*/
final class AlterDatabaseImpl extends AbstractRowCountQuery implements
// Cascading interface implementations for ALTER DATABASE behaviour
AlterDatabaseStep,
AlterDatabaseFinalStep {
/**
* Generated UID
*/
private static final long serialVersionUID = 8904572826501186329L;
private final Catalog database;
private final boolean ifExists;
private Catalog renameTo;
AlterDatabaseImpl(Configuration configuration, Catalog database) {
this(configuration, database, false);
}
AlterDatabaseImpl(Configuration configuration, Catalog database, boolean ifExists) {
super(configuration);
this.database = database;
this.ifExists = ifExists;
}
final Catalog $database() { return database; }
final boolean $ifExists() { return ifExists; }
final Catalog $renameTo() { return renameTo; }
// ------------------------------------------------------------------------
// XXX: DSL API
// ------------------------------------------------------------------------
@Override
public final AlterDatabaseImpl renameTo(Catalog newName) {
this.renameTo = newName;
return this;
}
@Override
public final AlterDatabaseImpl renameTo(Name newName) {
return renameTo(DSL.catalog(newName));
}
@Override
public final AlterDatabaseImpl renameTo(String newName) {
return renameTo(name(newName));
}
// ------------------------------------------------------------------------
// XXX: QueryPart API
// ------------------------------------------------------------------------
@Override
public final void accept(Context<?> ctx) {
accept0(ctx);
}
private final void accept0(Context<?> ctx) {
boolean supportRename = false;
if (supportRename)
ctx.visit(K_RENAME);
else
ctx.visit(K_ALTER);
ctx.sql(' ').visit(K_DATABASE);
if (ifExists)
ctx.sql(' ').visit(K_IF_EXISTS);
ctx.sql(' ').visit(database);
if (renameTo != null) {
boolean qualify = ctx.qualify();
ctx.sql(' ')
.qualify(false)
.visit(supportRename ? K_TO : K_RENAME_TO).sql(' ').visit(renameTo)
.qualify(qualify);
}
}
}

View File

@ -0,0 +1,123 @@
/*
* 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 org.jooq.SQLDialect.DERBY;
import static org.jooq.SQLDialect.FIREBIRD;
// ...
// ...
import static org.jooq.SQLDialect.POSTGRES;
// ...
// ...
import static org.jooq.impl.Keywords.K_CREATE;
import static org.jooq.impl.Keywords.K_DATABASE;
import static org.jooq.impl.Keywords.K_IF_NOT_EXISTS;
import java.util.Set;
import org.jooq.Catalog;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.CreateDatabaseFinalStep;
import org.jooq.SQLDialect;
/**
* @author Lukas Eder
*/
final class CreateDatabaseImpl extends AbstractRowCountQuery implements
// Cascading interface implementations for CREATE DATABASE behaviour
CreateDatabaseFinalStep {
/**
* Generated UID
*/
private static final long serialVersionUID = 8904572826501186329L;
private static final Set<SQLDialect> NO_SUPPORT_IF_NOT_EXISTS = SQLDialect.supportedBy(DERBY, FIREBIRD, POSTGRES);
private final Catalog database;
private final boolean ifNotExists;
CreateDatabaseImpl(Configuration configuration, Catalog database, boolean ifNotExists) {
super(configuration);
this.database = database;
this.ifNotExists = ifNotExists;
}
final Catalog $database() { return database; }
final boolean $ifNotExists() { return ifNotExists; }
// ------------------------------------------------------------------------
// XXX: DSL API
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// XXX: QueryPart API
// ------------------------------------------------------------------------
private final boolean supportsIfNotExists(Context<?> ctx) {
return !NO_SUPPORT_IF_NOT_EXISTS.contains(ctx.family());
}
@Override
public final void accept(Context<?> ctx) {
if (ifNotExists && !supportsIfNotExists(ctx)) {
Tools.beginTryCatch(ctx, DDLStatementType.CREATE_DATABASE);
accept0(ctx);
Tools.endTryCatch(ctx, DDLStatementType.CREATE_DATABASE);
}
else {
accept0(ctx);
}
}
private final void accept0(Context<?> ctx) {
ctx.visit(K_CREATE).sql(' ').visit(K_DATABASE);
if (ifNotExists && supportsIfNotExists(ctx))
ctx.sql(' ').visit(K_IF_NOT_EXISTS);
ctx.sql(' ').visit(database);
}
}

View File

@ -0,0 +1,124 @@
/*
* 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 org.jooq.Clause.DROP_SCHEMA;
// ...
// ...
// ...
import static org.jooq.SQLDialect.DERBY;
import static org.jooq.SQLDialect.FIREBIRD;
// ...
// ...
// ...
// ...
import static org.jooq.impl.Keywords.K_DATABASE;
import static org.jooq.impl.Keywords.K_DROP;
import static org.jooq.impl.Keywords.K_IF_EXISTS;
import java.util.Set;
import org.jooq.Catalog;
import org.jooq.Clause;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.DropDatabaseFinalStep;
import org.jooq.SQLDialect;
/**
* @author Lukas Eder
*/
final class DropDatabaseImpl extends AbstractRowCountQuery implements
// Cascading interface implementations for DROP DATABASE behaviour
DropDatabaseFinalStep {
/**
* Generated UID
*/
private static final long serialVersionUID = 8904572826501186329L;
private static final Clause[] CLAUSES = { DROP_SCHEMA };
private static final Set<SQLDialect> NO_SUPPORT_IF_EXISTS = SQLDialect.supportedBy(DERBY, FIREBIRD);
private final Catalog database;
private final boolean ifExists;
DropDatabaseImpl(Configuration configuration, Catalog database) {
this(configuration, database, false);
}
DropDatabaseImpl(Configuration configuration, Catalog database, boolean ifExists) {
super(configuration);
this.database = database;
this.ifExists = ifExists;
}
final Catalog $database() { return database; }
final boolean $ifExists() { return ifExists; }
// ------------------------------------------------------------------------
// XXX: QueryPart API
// ------------------------------------------------------------------------
private final boolean supportsIfExists(Context<?> ctx) {
return !NO_SUPPORT_IF_EXISTS.contains(ctx.family());
}
@Override
public final void accept(Context<?> ctx) {
if (ifExists && !supportsIfExists(ctx)) {
Tools.beginTryCatch(ctx, DDLStatementType.DROP_DATABASE);
accept0(ctx);
Tools.endTryCatch(ctx, DDLStatementType.DROP_DATABASE);
}
else {
accept0(ctx);
}
}
private void accept0(Context<?> ctx) {
ctx.visit(K_DROP).sql(' ').visit(K_DATABASE);
if (ifExists && supportsIfExists(ctx))
ctx.sql(' ').visit(K_IF_EXISTS);
ctx.sql(' ').visit(database);
}
}