[#5319] Add support for ALTER TABLE .. DROP (...) to allow for dropping multiple objects at once
This commit is contained in:
parent
7171af618c
commit
178606ce44
@ -57,6 +57,8 @@ import static org.jooq.SQLDialect.POSTGRES;
|
||||
// ...
|
||||
// ...
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.jooq.impl.DSL;
|
||||
|
||||
|
||||
@ -399,6 +401,70 @@ public interface AlterTableStep {
|
||||
@Support({ H2, POSTGRES })
|
||||
AlterTableDropStep dropColumnIfExists(String field);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
* <p>
|
||||
* This is an alias for {@link #dropColumns(Collection)}.
|
||||
*/
|
||||
@Support({ H2 })
|
||||
AlterTableDropStep drop(Field<?>... fields);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
* <p>
|
||||
* This is an alias for {@link #dropColumns(Collection)}.
|
||||
*/
|
||||
@Support({ H2 })
|
||||
AlterTableDropStep drop(Name... fields);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
* <p>
|
||||
* This is an alias for {@link #dropColumns(Collection)}.
|
||||
*/
|
||||
@Support({ H2 })
|
||||
AlterTableDropStep drop(String... fields);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
*/
|
||||
@Support({ H2 })
|
||||
AlterTableDropStep dropColumns(Field<?>... fields);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
*/
|
||||
@Support({ H2 })
|
||||
AlterTableDropStep dropColumns(Name... fields);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
*/
|
||||
@Support({ H2 })
|
||||
AlterTableDropStep dropColumns(String... fields);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
* <p>
|
||||
* This is an alias for {@link #dropColumns(Collection)}.
|
||||
*/
|
||||
@Support({ H2 })
|
||||
AlterTableDropStep drop(Collection<? extends Field<?>> fields);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
*/
|
||||
@Support({ H2 })
|
||||
AlterTableDropStep dropColumns(Collection<? extends Field<?>> fields);
|
||||
|
||||
/**
|
||||
* Add a <code>DROP CONSTRAINT</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
|
||||
@ -58,6 +58,7 @@ import static org.jooq.SQLDialect.CUBRID;
|
||||
// ...
|
||||
import static org.jooq.SQLDialect.DERBY;
|
||||
import static org.jooq.SQLDialect.FIREBIRD;
|
||||
import static org.jooq.SQLDialect.H2;
|
||||
// ...
|
||||
import static org.jooq.SQLDialect.HSQLDB;
|
||||
// ...
|
||||
@ -112,12 +113,15 @@ import static org.jooq.impl.Tools.beginTryCatch;
|
||||
import static org.jooq.impl.Tools.end;
|
||||
import static org.jooq.impl.Tools.endExecuteImmediate;
|
||||
import static org.jooq.impl.Tools.endTryCatch;
|
||||
import static org.jooq.impl.Tools.fieldsByName;
|
||||
import static org.jooq.impl.Tools.toSQLDDLTypeDeclaration;
|
||||
import static org.jooq.impl.Tools.toSQLDDLTypeDeclarationForAddition;
|
||||
import static org.jooq.impl.Tools.toSQLDDLTypeDeclarationIdentityAfterNull;
|
||||
import static org.jooq.impl.Tools.toSQLDDLTypeDeclarationIdentityBeforeNull;
|
||||
import static org.jooq.impl.Tools.DataKey.DATA_CONSTRAINT_REFERENCE;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.EnumSet;
|
||||
|
||||
import org.jooq.AlterTableAlterStep;
|
||||
@ -190,7 +194,7 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
private Nullability alterColumnNullability;
|
||||
private DataType<?> alterColumnType;
|
||||
private Field<?> alterColumnDefault;
|
||||
private Field<?> dropColumn;
|
||||
private QueryPartList<Field<?>> dropColumns;
|
||||
private boolean dropColumnCascade;
|
||||
private Constraint dropConstraint;
|
||||
|
||||
@ -510,8 +514,7 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropColumn(Field<?> field) {
|
||||
dropColumn = field;
|
||||
return this;
|
||||
return dropColumns(new Field[] { field });
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -530,6 +533,47 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
return dropColumn(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl drop(Field<?>... fields) {
|
||||
return dropColumns(fields);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl drop(Name... fields) {
|
||||
return dropColumns(fields);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl drop(String... fields) {
|
||||
return dropColumns(fields);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropColumns(Field<?>... fields) {
|
||||
return dropColumns(Arrays.asList(fields));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropColumns(Name... fields) {
|
||||
return dropColumns(fieldsByName(fields));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropColumns(String... fields) {
|
||||
return dropColumns(fieldsByName(fields));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl drop(Collection<? extends Field<?>> fields) {
|
||||
return dropColumns(fields);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropColumns(Collection<? extends Field<?>> fields) {
|
||||
dropColumns = new QueryPartList<Field<?>>(fields);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl drop(Constraint constraint) {
|
||||
dropConstraint = constraint;
|
||||
@ -911,7 +955,7 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
ctx.end(ALTER_TABLE_ALTER);
|
||||
}
|
||||
else if (dropColumn != null) {
|
||||
else if (dropColumns != null) {
|
||||
ctx.start(ALTER_TABLE_DROP);
|
||||
|
||||
switch (family) {
|
||||
@ -926,6 +970,13 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
default:
|
||||
ctx.visit(K_DROP);
|
||||
break;
|
||||
@ -949,21 +1000,21 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
}
|
||||
}
|
||||
|
||||
ctx.sql(' ')
|
||||
.qualify(false)
|
||||
.visit(dropColumn)
|
||||
// [#5319] Older versions of H2 don't support parentheses around dropped columns
|
||||
if (ctx.family() == H2)
|
||||
ctx.sql(' ');
|
||||
else if (dropColumns.size() != 1 )
|
||||
ctx.sql(" (");
|
||||
else
|
||||
ctx.sql(' ');
|
||||
|
||||
ctx.qualify(false)
|
||||
.visit(dropColumns)
|
||||
.qualify(true);
|
||||
|
||||
switch (family) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (ctx.family() != H2)
|
||||
if (dropColumns.size() != 1 )
|
||||
ctx.sql(')');
|
||||
|
||||
if (dropColumnCascade)
|
||||
ctx.sql(' ').visit(K_CASCADE);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user