[#5318] Add support for ALTER TABLE .. ADD (...) to add multiple objects to a table at once

This commit is contained in:
lukaseder 2018-03-01 16:53:38 +01:00
parent 44ce8b3f23
commit c0af9354d9
5 changed files with 123 additions and 2 deletions

View File

@ -229,6 +229,22 @@ public interface AlterTableStep {
@Support
AlterTableFinalStep add(Field<?> field);
/**
* Add an <code>ADD</code> clause with multiple columns or constraints to
* the <code>ALTER TABLE</code> statement.
*/
// @Support({ H2, FIREBIRD, MARIADB, MYSQL, POSTGRES })
@Support({ H2 })
AlterTableFinalStep add(FieldOrConstraint... fields);
/**
* Add an <code>ADD</code> clause with multiple columns or constraints to
* the <code>ALTER TABLE</code> statement.
*/
// @Support({ H2, FIREBIRD, MARIADB, MYSQL, POSTGRES })
@Support({ H2 })
AlterTableFinalStep add(Collection<? extends FieldOrConstraint> fields);
/**
* Add an <code>ADD COLUMN</code> clause to the <code>ALTER TABLE</code>
* statement.

View File

@ -42,6 +42,6 @@ package org.jooq;
*
* @author Lukas Eder
*/
public interface Constraint extends QueryPart {
public interface Constraint extends FieldOrConstraint {
}

View File

@ -79,7 +79,14 @@ import org.jooq.types.Interval;
* @param <T> The field type
* @author Lukas Eder
*/
public interface Field<T> extends SelectField<T>, GroupField, OrderField<T>, FieldOrRow, Named {
public interface Field<T>
extends
SelectField<T>,
GroupField,
OrderField<T>,
FieldOrRow,
FieldOrConstraint,
Named {
// ------------------------------------------------------------------------
// API

View File

@ -0,0 +1,50 @@
/*
* 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 common base type for {@link Field} and {@link Constraint} where DSL API accepts
* both types alike.
* <p>
* This is useful for DDL statements.
*
* @author Lukas Eder
*/
public interface FieldOrConstraint extends QueryPart {
}

View File

@ -144,6 +144,7 @@ import org.jooq.Context;
import org.jooq.DSLContext;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.FieldOrConstraint;
import org.jooq.Index;
import org.jooq.Name;
import org.jooq.Nullability;
@ -192,6 +193,7 @@ final class AlterTableImpl extends AbstractQuery implements
private Index renameIndexTo;
private Constraint renameConstraint;
private Constraint renameConstraintTo;
private QueryPartList<FieldOrConstraint> add;
private Field<?> addColumn;
private DataType<?> addColumnType;
private Constraint addConstraint;
@ -349,6 +351,17 @@ final class AlterTableImpl extends AbstractQuery implements
return addColumn(field);
}
@Override
public final AlterTableImpl add(FieldOrConstraint... fields) {
return add(Arrays.asList(fields));
}
@Override
public final AlterTableImpl add(Collection<? extends FieldOrConstraint> fields) {
add = new QueryPartList<FieldOrConstraint>(fields);
return this;
}
@Override
public final <T> AlterTableImpl add(Field<T> field, DataType<T> type) {
return addColumn(field, type);
@ -867,6 +880,41 @@ final class AlterTableImpl extends AbstractQuery implements
ctx.data().remove(DATA_CONSTRAINT_REFERENCE);
ctx.end(ALTER_TABLE_RENAME_CONSTRAINT);
}
else if (add != null) {
boolean qualify = ctx.qualify();
ctx.start(ALTER_TABLE_ADD)
.visit(K_ADD)
.qualify(false)
.sql(" (");
boolean indent = add.size() > 1;
if (indent)
ctx.formatIndentStart()
.formatNewLine();
for (int i = 0; i < add.size(); i++) {
if (i > 0)
ctx.sql(',').formatSeparator();
FieldOrConstraint part = add.get(i);
ctx.visit(part);
if (part instanceof Field) {
ctx.sql(' ');
toSQLDDLTypeDeclarationForAddition(ctx, ((Field<?>) part).getDataType());
}
}
if (indent)
ctx.formatIndentEnd()
.formatNewLine();
ctx.sql(')')
.qualify(qualify)
.end(ALTER_TABLE_ADD);
}
else if (addColumn != null) {
boolean qualify = ctx.qualify();