[jOOQ/jOOQ#7775] Add support for ALTER TYPE to modify enum types in PostgreSQL

This commit is contained in:
Lukas Eder 2019-09-30 10:35:39 +02:00
parent 3633b53f1e
commit da750890d3
8 changed files with 508 additions and 1 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;
/**
* A {@link Query} that can alter types.
* <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 AlterTypeFinalStep extends DDLQuery {
}

View File

@ -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.
* <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 AlterTypeRenameValueToStep {
/**
* Add the <code>ALTER TYPE .. RENAME VALUE .. TO ..</code> clause.
*/
@Support({ POSTGRES })
AlterTypeFinalStep to(String newEnumValue);
/**
* Add the <code>ALTER TYPE .. RENAME VALUE .. TO ..</code> clause.
*/
@Support({ POSTGRES })
AlterTypeFinalStep to(Field<String> newEnumValue);
}

View File

@ -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.
* <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 AlterTypeStep {
/**
* Add the <code>ALTER TYPE .. RENAME TO ..</code> clause.
*/
@Support({ POSTGRES })
AlterTypeFinalStep renameTo(String newName);
/**
* Add the <code>ALTER TYPE .. RENAME TO ..</code> clause.
*/
@Support({ POSTGRES })
AlterTypeFinalStep renameTo(Name newName);
/**
* Add the <code>ALTER TYPE .. SET SCHEMA ..</code> clause.
*/
@Support({ POSTGRES })
AlterTypeFinalStep setSchema(String newSchema);
/**
* Add the <code>ALTER TYPE .. SET SCHEMA ..</code> clause.
*/
@Support({ POSTGRES })
AlterTypeFinalStep setSchema(Name newSchema);
/**
* Add the <code>ALTER TYPE .. SET SCHEMA ..</code> clause.
*/
@Support({ POSTGRES })
AlterTypeFinalStep setSchema(Schema newSchema);
/**
* Add the <code>ALTER TYPE .. ADD VALUE ..</code> clause.
*/
@Support({ POSTGRES })
AlterTypeFinalStep addValue(String newEnumValue);
/**
* Add the <code>ALTER TYPE .. ADD VALUE ..</code> clause.
*/
@Support({ POSTGRES })
AlterTypeFinalStep addValue(Field<String> newEnumValue);
/**
* Add the <code>ALTER TYPE .. RENAME VALUE ..</code> clause.
*/
@Support({ POSTGRES })
AlterTypeRenameValueToStep renameValue(String existingEnumValue);
/**
* Add the <code>ALTER TYPE .. RENAME VALUE ..</code> clause.
*/
@Support({ POSTGRES })
AlterTypeRenameValueToStep renameValue(Field<String> existingEnumValue);
}

View File

@ -9028,6 +9028,22 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support({ H2, POSTGRES })
CreateTypeStep createType(Name type);
/**
* Create a new DSL <code>ALTER TYPE</code> statement.
*
* @see DSL#alterType(String)
*/
@Support({ POSTGRES })
AlterTypeStep alterType(String type);
/**
* Create a new DSL <code>ALTER TYPE</code> statement.
*
* @see DSL#alterType(Name)
*/
@Support({ POSTGRES })
AlterTypeStep alterType(Name type);
/**
* Create a new DSL <code>DROP TYPE</code> statement.
*

View File

@ -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<String> addValue;
private Field<String> renameValueFrom;
private Field<String> 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<String> newEnumValue) {
this.addValue = newEnumValue;
return this;
}
@Override
public final AlterTypeImpl renameValue(String existingEnumValue) {
return renameValue(Tools.field(existingEnumValue));
}
@Override
public final AlterTypeImpl renameValue(Field<String> existingEnumValue) {
this.renameValueFrom = existingEnumValue;
return this;
}
@Override
public final AlterTypeImpl to(String newEnumValue) {
return to(Tools.field(newEnumValue));
}
@Override
public final AlterTypeImpl to(Field<String> 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);
}
}

View File

@ -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 <code>ALTER TYPE</code> statement.
*
* @see DSLContext#alterType(String)
*/
@Support({ POSTGRES })
public static AlterTypeStep alterType(String type) {
return dsl().alterType(type);
}
/**
* Create a new DSL <code>ALTER TYPE</code> statement.
*
* @see DSLContext#alterType(Name)
*/
@Support({ POSTGRES })
public static AlterTypeStep alterType(Name type) {
return dsl().alterType(type);
}
/**
* Create a new DSL <code>DROP TYPE</code> statement.
*

View File

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

View File

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