From c29f2dbac7f61d157f227cb2fb7f54fae050fe58 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 20 May 2020 15:42:17 +0200 Subject: [PATCH] [jOOQ/jOOQ#10210] Support CREATE, ALTER, DROP DATABASE --- .../java/org/jooq/AlterDatabaseFinalStep.java | 65 +++++++ .../main/java/org/jooq/AlterDatabaseStep.java | 92 ++++++++++ .../org/jooq/CreateDatabaseFinalStep.java | 65 +++++++ .../java/org/jooq/DropDatabaseFinalStep.java | 65 +++++++ .../java/org/jooq/impl/AlterDatabaseImpl.java | 172 ++++++++++++++++++ .../org/jooq/impl/CreateDatabaseImpl.java | 123 +++++++++++++ .../java/org/jooq/impl/DropDatabaseImpl.java | 124 +++++++++++++ 7 files changed, 706 insertions(+) create mode 100644 jOOQ/src/main/java/org/jooq/AlterDatabaseFinalStep.java create mode 100644 jOOQ/src/main/java/org/jooq/AlterDatabaseStep.java create mode 100644 jOOQ/src/main/java/org/jooq/CreateDatabaseFinalStep.java create mode 100644 jOOQ/src/main/java/org/jooq/DropDatabaseFinalStep.java create mode 100644 jOOQ/src/main/java/org/jooq/impl/AlterDatabaseImpl.java create mode 100644 jOOQ/src/main/java/org/jooq/impl/CreateDatabaseImpl.java create mode 100644 jOOQ/src/main/java/org/jooq/impl/DropDatabaseImpl.java diff --git a/jOOQ/src/main/java/org/jooq/AlterDatabaseFinalStep.java b/jOOQ/src/main/java/org/jooq/AlterDatabaseFinalStep.java new file mode 100644 index 0000000000..314c27abf1 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/AlterDatabaseFinalStep.java @@ -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 DROP SCHEMA DSL. + *

+ *

Referencing XYZ*Step types directly from client code

+ *

+ * It is usually not recommended to reference any XYZ*Step 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: https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql. + *

+ * Drawbacks of referencing the XYZ*Step types directly: + *

+ * + * @author Lukas Eder + */ +public interface AlterDatabaseFinalStep extends DDLQuery { + +} diff --git a/jOOQ/src/main/java/org/jooq/AlterDatabaseStep.java b/jOOQ/src/main/java/org/jooq/AlterDatabaseStep.java new file mode 100644 index 0000000000..17ae1793d2 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/AlterDatabaseStep.java @@ -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 ALTER DATABASE DSL used to specify + * ALTER behaviour. + *

+ *

Referencing XYZ*Step types directly from client code

+ *

+ * It is usually not recommended to reference any XYZ*Step 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: https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql. + *

+ * Drawbacks of referencing the XYZ*Step types directly: + *

+ * + * @author Lukas Eder + */ +public interface AlterDatabaseStep { + + /** + * Add a RENAME TO clause to the ALTER DATABASE + * statement. + */ + @Support({ POSTGRES }) + AlterDatabaseFinalStep renameTo(Catalog newName); + + /** + * Add a RENAME TO clause to the ALTER DATABASE + * statement. + */ + @Support({ POSTGRES }) + AlterDatabaseFinalStep renameTo(Name newName); + + /** + * Add a RENAME TO clause to the ALTER DATABASE + * statement. + */ + @Support({ POSTGRES }) + AlterDatabaseFinalStep renameTo(String newName); + +} diff --git a/jOOQ/src/main/java/org/jooq/CreateDatabaseFinalStep.java b/jOOQ/src/main/java/org/jooq/CreateDatabaseFinalStep.java new file mode 100644 index 0000000000..3486fd390c --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/CreateDatabaseFinalStep.java @@ -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. + *

+ *

Referencing XYZ*Step types directly from client code

+ *

+ * It is usually not recommended to reference any XYZ*Step 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: https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql. + *

+ * Drawbacks of referencing the XYZ*Step types directly: + *

+ * + * @author Lukas Eder + */ +public interface CreateDatabaseFinalStep extends DDLQuery { + +} diff --git a/jOOQ/src/main/java/org/jooq/DropDatabaseFinalStep.java b/jOOQ/src/main/java/org/jooq/DropDatabaseFinalStep.java new file mode 100644 index 0000000000..f1f037a73b --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/DropDatabaseFinalStep.java @@ -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 DROP SCHEMA DSL. + *

+ *

Referencing XYZ*Step types directly from client code

+ *

+ * It is usually not recommended to reference any XYZ*Step 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: https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql. + *

+ * Drawbacks of referencing the XYZ*Step types directly: + *

+ * + * @author Lukas Eder + */ +public interface DropDatabaseFinalStep extends DDLQuery { + +} diff --git a/jOOQ/src/main/java/org/jooq/impl/AlterDatabaseImpl.java b/jOOQ/src/main/java/org/jooq/impl/AlterDatabaseImpl.java new file mode 100644 index 0000000000..f3fe434932 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/AlterDatabaseImpl.java @@ -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); + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/CreateDatabaseImpl.java b/jOOQ/src/main/java/org/jooq/impl/CreateDatabaseImpl.java new file mode 100644 index 0000000000..25ad0ffa43 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/CreateDatabaseImpl.java @@ -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 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); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DropDatabaseImpl.java b/jOOQ/src/main/java/org/jooq/impl/DropDatabaseImpl.java new file mode 100644 index 0000000000..c1901f4e2f --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/DropDatabaseImpl.java @@ -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 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); + } +}