From da750890d3752f37dab64b88d97a76d1f89b860c Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Mon, 30 Sep 2019 10:35:39 +0200 Subject: [PATCH] [jOOQ/jOOQ#7775] Add support for ALTER TYPE to modify enum types in PostgreSQL --- .../java/org/jooq/AlterTypeFinalStep.java | 65 +++++++ .../org/jooq/AlterTypeRenameValueToStep.java | 79 ++++++++ .../src/main/java/org/jooq/AlterTypeStep.java | 122 ++++++++++++ jOOQ/src/main/java/org/jooq/DSLContext.java | 16 ++ .../java/org/jooq/impl/AlterTypeImpl.java | 173 ++++++++++++++++++ jOOQ/src/main/java/org/jooq/impl/DSL.java | 21 +++ .../java/org/jooq/impl/DefaultDSLContext.java | 11 ++ .../main/java/org/jooq/impl/ParserImpl.java | 22 ++- 8 files changed, 508 insertions(+), 1 deletion(-) create mode 100644 jOOQ/src/main/java/org/jooq/AlterTypeFinalStep.java create mode 100644 jOOQ/src/main/java/org/jooq/AlterTypeRenameValueToStep.java create mode 100644 jOOQ/src/main/java/org/jooq/AlterTypeStep.java create mode 100644 jOOQ/src/main/java/org/jooq/impl/AlterTypeImpl.java diff --git a/jOOQ/src/main/java/org/jooq/AlterTypeFinalStep.java b/jOOQ/src/main/java/org/jooq/AlterTypeFinalStep.java new file mode 100644 index 0000000000..5a5c393122 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/AlterTypeFinalStep.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 alter types. + *

+ *

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 AlterTypeFinalStep extends DDLQuery { + +} diff --git a/jOOQ/src/main/java/org/jooq/AlterTypeRenameValueToStep.java b/jOOQ/src/main/java/org/jooq/AlterTypeRenameValueToStep.java new file mode 100644 index 0000000000..20ee11844d --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/AlterTypeRenameValueToStep.java @@ -0,0 +1,79 @@ +/* + * 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; + +/** + * A {@link Query} that can alter types. + *

+ *

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 AlterTypeRenameValueToStep { + + /** + * Add the ALTER TYPE .. RENAME VALUE .. TO .. clause. + */ + @Support({ POSTGRES }) + AlterTypeFinalStep to(String newEnumValue); + + /** + * Add the ALTER TYPE .. RENAME VALUE .. TO .. clause. + */ + @Support({ POSTGRES }) + AlterTypeFinalStep to(Field newEnumValue); + +} diff --git a/jOOQ/src/main/java/org/jooq/AlterTypeStep.java b/jOOQ/src/main/java/org/jooq/AlterTypeStep.java new file mode 100644 index 0000000000..b898632252 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/AlterTypeStep.java @@ -0,0 +1,122 @@ +/* + * 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; + +/** + * A {@link Query} that can alter types. + *

+ *

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 AlterTypeStep { + + /** + * Add the ALTER TYPE .. RENAME TO .. clause. + */ + @Support({ POSTGRES }) + AlterTypeFinalStep renameTo(String newName); + + /** + * Add the ALTER TYPE .. RENAME TO .. clause. + */ + @Support({ POSTGRES }) + AlterTypeFinalStep renameTo(Name newName); + + /** + * Add the ALTER TYPE .. SET SCHEMA .. clause. + */ + @Support({ POSTGRES }) + AlterTypeFinalStep setSchema(String newSchema); + + /** + * Add the ALTER TYPE .. SET SCHEMA .. clause. + */ + @Support({ POSTGRES }) + AlterTypeFinalStep setSchema(Name newSchema); + + /** + * Add the ALTER TYPE .. SET SCHEMA .. clause. + */ + @Support({ POSTGRES }) + AlterTypeFinalStep setSchema(Schema newSchema); + + /** + * Add the ALTER TYPE .. ADD VALUE .. clause. + */ + @Support({ POSTGRES }) + AlterTypeFinalStep addValue(String newEnumValue); + + /** + * Add the ALTER TYPE .. ADD VALUE .. clause. + */ + @Support({ POSTGRES }) + AlterTypeFinalStep addValue(Field newEnumValue); + + /** + * Add the ALTER TYPE .. RENAME VALUE .. clause. + */ + @Support({ POSTGRES }) + AlterTypeRenameValueToStep renameValue(String existingEnumValue); + + /** + * Add the ALTER TYPE .. RENAME VALUE .. clause. + */ + @Support({ POSTGRES }) + AlterTypeRenameValueToStep renameValue(Field existingEnumValue); + + +} diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index 456e774fc9..e4b36e7334 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -9028,6 +9028,22 @@ public interface DSLContext extends Scope , AutoCloseable { @Support({ H2, POSTGRES }) CreateTypeStep createType(Name type); + /** + * Create a new DSL ALTER TYPE statement. + * + * @see DSL#alterType(String) + */ + @Support({ POSTGRES }) + AlterTypeStep alterType(String type); + + /** + * Create a new DSL ALTER TYPE statement. + * + * @see DSL#alterType(Name) + */ + @Support({ POSTGRES }) + AlterTypeStep alterType(Name type); + /** * Create a new DSL DROP TYPE statement. * diff --git a/jOOQ/src/main/java/org/jooq/impl/AlterTypeImpl.java b/jOOQ/src/main/java/org/jooq/impl/AlterTypeImpl.java new file mode 100644 index 0000000000..91d431d8c7 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/AlterTypeImpl.java @@ -0,0 +1,173 @@ +/* + * 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.impl.DSL.name; +import static org.jooq.impl.DSL.schema; +import static org.jooq.impl.Keywords.K_ADD; +import static org.jooq.impl.Keywords.K_ALTER; +import static org.jooq.impl.Keywords.K_RENAME; +import static org.jooq.impl.Keywords.K_RENAME_TO; +import static org.jooq.impl.Keywords.K_SCHEMA; +import static org.jooq.impl.Keywords.K_SET; +import static org.jooq.impl.Keywords.K_TO; +import static org.jooq.impl.Keywords.K_TYPE; +import static org.jooq.impl.Keywords.K_VALUE; + +import org.jooq.AlterTypeFinalStep; +import org.jooq.AlterTypeRenameValueToStep; +import org.jooq.AlterTypeStep; +import org.jooq.Configuration; +import org.jooq.Context; +import org.jooq.Field; +import org.jooq.Name; +import org.jooq.Schema; +import org.jooq.conf.ParamType; + +/** + * @author Lukas Eder + */ +final class AlterTypeImpl extends AbstractRowCountQuery implements + + // Cascading interface implementations for CREATE TYPE behaviour + AlterTypeStep, + AlterTypeRenameValueToStep, + AlterTypeFinalStep { + + private static final long serialVersionUID = -5018375056147329888L; + private final Name type; + private Name renameTo; + private Schema setSchema; + private Field addValue; + private Field renameValueFrom; + private Field renameValueTo; + + AlterTypeImpl(Configuration configuration, Name type) { + super(configuration); + + this.type = type; + } + + // ------------------------------------------------------------------------ + // XXX: DSL API + // ------------------------------------------------------------------------ + + @Override + public final AlterTypeImpl renameTo(String newName) { + return renameTo(DSL.name(newName)); + } + + @Override + public final AlterTypeImpl renameTo(Name newName) { + this.renameTo = newName; + return this; + } + + @Override + public final AlterTypeImpl setSchema(String newSchema) { + return setSchema(name(newSchema)); + } + + @Override + public final AlterTypeImpl setSchema(Name newSchema) { + return setSchema(schema(newSchema)); + } + + @Override + public final AlterTypeImpl setSchema(Schema newSchema) { + this.setSchema = newSchema; + return this; + } + + @Override + public final AlterTypeImpl addValue(String newEnumValue) { + return addValue(Tools.field(newEnumValue)); + } + + @Override + public final AlterTypeImpl addValue(Field newEnumValue) { + this.addValue = newEnumValue; + return this; + } + + @Override + public final AlterTypeImpl renameValue(String existingEnumValue) { + return renameValue(Tools.field(existingEnumValue)); + } + + @Override + public final AlterTypeImpl renameValue(Field existingEnumValue) { + this.renameValueFrom = existingEnumValue; + return this; + } + + @Override + public final AlterTypeImpl to(String newEnumValue) { + return to(Tools.field(newEnumValue)); + } + + @Override + public final AlterTypeImpl to(Field newEnumValue) { + this.renameValueTo = newEnumValue; + return this; + } + + // ------------------------------------------------------------------------ + // XXX: QueryPart API + // ------------------------------------------------------------------------ + + @Override + public final void accept(Context ctx) { + ParamType previous = ctx.paramType(); + boolean qualified = ctx.qualify(); + + ctx.visit(K_ALTER).sql(' ').visit(K_TYPE).sql(' ') + .visit(type).sql(' '); + + if (renameTo != null) + ctx.visit(K_RENAME_TO).sql(' ').qualify(false).visit(renameTo).qualify(qualified); + else if (setSchema != null) + ctx.visit(K_SET).sql(' ').visit(K_SCHEMA).sql(' ').visit(setSchema); + else if (addValue != null) + ctx.visit(K_ADD).sql(' ').visit(K_VALUE).sql(' ').visit(addValue); + else if (renameValueFrom != null) + ctx.visit(K_RENAME).sql(' ').visit(K_VALUE).sql(' ').visit(renameValueFrom).sql(' ').visit(K_TO).sql(' ').visit(renameValueTo); + + ctx.paramType(previous); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 940daa3e33..4bbed50aa6 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -131,6 +131,7 @@ import org.jooq.AlterIndexStep; import org.jooq.AlterSchemaStep; import org.jooq.AlterSequenceStep; import org.jooq.AlterTableStep; +import org.jooq.AlterTypeStep; import org.jooq.AlterViewStep; import org.jooq.ArrayAggOrderByStep; // ... @@ -6993,6 +6994,26 @@ public class DSL { return dsl().createType(type); } + /** + * Create a new DSL ALTER TYPE statement. + * + * @see DSLContext#alterType(String) + */ + @Support({ POSTGRES }) + public static AlterTypeStep alterType(String type) { + return dsl().alterType(type); + } + + /** + * Create a new DSL ALTER TYPE statement. + * + * @see DSLContext#alterType(Name) + */ + @Support({ POSTGRES }) + public static AlterTypeStep alterType(Name type) { + return dsl().alterType(type); + } + /** * Create a new DSL DROP TYPE statement. * diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index df198b5998..8a33507223 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -98,6 +98,7 @@ import org.jooq.AlterIndexStep; import org.jooq.AlterSchemaStep; import org.jooq.AlterSequenceStep; import org.jooq.AlterTableStep; +import org.jooq.AlterTypeStep; import org.jooq.AlterViewStep; import org.jooq.Attachable; import org.jooq.Batch; @@ -3151,6 +3152,16 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return new CreateTypeImpl(configuration(), type); } + @Override + public AlterTypeStep alterType(String type) { + return alterType(name(type)); + } + + @Override + public AlterTypeStep alterType(Name type) { + return new AlterTypeImpl(configuration(), type); + } + @Override public DropTypeStep dropType(String type) { return dropType(name(type)); diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index cdda179099..c2549d00d6 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -339,6 +339,7 @@ import org.jooq.AlterSequenceStep; import org.jooq.AlterTableDropStep; import org.jooq.AlterTableFinalStep; import org.jooq.AlterTableStep; +import org.jooq.AlterTypeStep; import org.jooq.ArrayAggOrderByStep; import org.jooq.Block; import org.jooq.CaseConditionStep; @@ -2192,10 +2193,12 @@ final class ParserImpl implements Parser { return parseAlterSession(ctx); else if (parseKeywordIf(ctx, "TABLE")) return parseAlterTable(ctx); + else if (parseKeywordIf(ctx, "TYPE")) + return parseAlterType(ctx); else if (parseKeywordIf(ctx, "VIEW")) return parseAlterView(ctx); else - throw ctx.expected("DOMAIN", "INDEX", "SCHEMA", "SEQUENCE", "SESSION", "TABLE", "VIEW"); + throw ctx.expected("DOMAIN", "INDEX", "SCHEMA", "SEQUENCE", "SESSION", "TABLE", "TYPE", "VIEW"); } private static final DDLQuery parseDrop(ParserContext ctx) { @@ -4043,6 +4046,23 @@ final class ParserImpl implements Parser { return s1.alter(field).set(type); } + private static final DDLQuery parseAlterType(ParserContext ctx) { + AlterTypeStep s1 = ctx.dsl.alterType(parseName(ctx)); + + + if (parseKeywordIf(ctx, "ADD VALUE")) + return s1.addValue(parseStringLiteral(ctx)); + else if (parseKeywordIf(ctx, "RENAME TO")) + return s1.renameTo(parseIdentifier(ctx)); + else if (parseKeywordIf(ctx, "RENAME VALUE")) + return s1.renameValue(parseStringLiteral(ctx)).to(parseKeyword(ctx, "TO") ? parseStringLiteral(ctx) : null); + else if (parseKeywordIf(ctx, "SET SCHEMA")) + return s1.setSchema(parseIdentifier(ctx)); + + + throw ctx.expected("ADD VALUE", "RENAME TO", "RENAME VALUE", "SET SCHEMA"); + } + private static final DDLQuery parseRename(ParserContext ctx) { parseKeyword(ctx, "RENAME");