[#5315] Add support for ALTER TABLE .. DROP IF EXISTS
This commit is contained in:
parent
0e5c1a9311
commit
18acc1dbcd
@ -279,6 +279,33 @@ public interface AlterTableStep {
|
||||
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
|
||||
AlterTableDropStep drop(String field);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN IF EXISTS</code> clause to the
|
||||
* <code>ALTER TABLE</code> statement.
|
||||
* <p>
|
||||
* This is an alias for {@link #dropColumnIfExists(Field)}.
|
||||
*/
|
||||
@Support({ H2, POSTGRES })
|
||||
AlterTableDropStep dropIfExists(Field<?> field);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN IF EXISTS</code> clause to the
|
||||
* <code>ALTER TABLE</code> statement.
|
||||
* <p>
|
||||
* This is an alias for {@link #dropColumnIfExists(Name)}.
|
||||
*/
|
||||
@Support({ H2, POSTGRES })
|
||||
AlterTableDropStep dropIfExists(Name field);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN IF EXISTS</code> clause to the
|
||||
* <code>ALTER TABLE</code> statement.
|
||||
* <p>
|
||||
* This is an alias for {@link #dropColumnIfExists(String)}.
|
||||
*/
|
||||
@Support({ H2, POSTGRES })
|
||||
AlterTableDropStep dropIfExists(String field);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
@ -300,6 +327,27 @@ public interface AlterTableStep {
|
||||
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
|
||||
AlterTableDropStep dropColumn(String field);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN IF EXISTS</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
*/
|
||||
@Support({ H2, POSTGRES })
|
||||
AlterTableDropStep dropColumnIfExists(Field<?> field);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN IF EXISTS</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
*/
|
||||
@Support({ H2, POSTGRES })
|
||||
AlterTableDropStep dropColumnIfExists(Name field);
|
||||
|
||||
/**
|
||||
* Add an <code>DROP COLUMN IF EXISTS</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
*/
|
||||
@Support({ H2, POSTGRES })
|
||||
AlterTableDropStep dropColumnIfExists(String field);
|
||||
|
||||
/**
|
||||
* Add a <code>DROP CONSTRAINT</code> clause to the <code>ALTER TABLE</code>
|
||||
* statement.
|
||||
|
||||
@ -161,6 +161,7 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
private final Table<?> table;
|
||||
private final boolean ifExists;
|
||||
private boolean ifExistsColumn;
|
||||
private Table<?> renameTo;
|
||||
private Field<?> renameColumn;
|
||||
private Field<?> renameColumnTo;
|
||||
@ -440,6 +441,21 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
return dropColumn(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropIfExists(Field<?> field) {
|
||||
return dropColumnIfExists(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropIfExists(Name field) {
|
||||
return dropColumnIfExists(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropIfExists(String field) {
|
||||
return dropColumnIfExists(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropColumn(Name field) {
|
||||
return dropColumn(field(field));
|
||||
@ -456,6 +472,22 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropColumnIfExists(Name field) {
|
||||
return dropColumnIfExists(field(field));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropColumnIfExists(String field) {
|
||||
return dropColumnIfExists(name(field));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl dropColumnIfExists(Field<?> field) {
|
||||
ifExistsColumn = true;
|
||||
return dropColumn(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final AlterTableImpl drop(Constraint constraint) {
|
||||
dropConstraint = constraint;
|
||||
@ -505,6 +537,19 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
}
|
||||
|
||||
private final void accept0(Context<?> ctx) {
|
||||
if (ifExistsColumn && !supportsIfExists(ctx)) {
|
||||
Field<?> field = field(table.getQualifiedName().append(dropColumn.getUnqualifiedName()));
|
||||
|
||||
Tools.beginTryCatchIfExistsColumn(ctx, DDLStatementType.ALTER_TABLE, field);
|
||||
accept1(ctx);
|
||||
Tools.endTryCatchIfExistsColumn(ctx, DDLStatementType.ALTER_TABLE, field);
|
||||
}
|
||||
else {
|
||||
accept1(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
private final void accept1(Context<?> ctx) {
|
||||
SQLDialect family = ctx.family();
|
||||
|
||||
|
||||
@ -558,10 +603,10 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
}
|
||||
}
|
||||
|
||||
accept1(ctx);
|
||||
accept2(ctx);
|
||||
}
|
||||
|
||||
private final void accept1(Context<?> ctx) {
|
||||
private final void accept2(Context<?> ctx) {
|
||||
SQLDialect family = ctx.family();
|
||||
|
||||
boolean omitAlterTable =
|
||||
@ -835,6 +880,21 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
break;
|
||||
}
|
||||
|
||||
if (ifExistsColumn) {
|
||||
switch (family) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
case H2:
|
||||
case POSTGRES:
|
||||
default:
|
||||
ctx.sql(' ').visit(K_IF_EXISTS);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ctx.sql(' ')
|
||||
.qualify(false)
|
||||
.visit(dropColumn)
|
||||
@ -949,7 +1009,7 @@ final class AlterTableImpl extends AbstractQuery implements
|
||||
|
||||
|
||||
|
||||
accept1(ctx);
|
||||
accept2(ctx);
|
||||
|
||||
|
||||
|
||||
|
||||
@ -3778,6 +3778,54 @@ final class Tools {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
default:
|
||||
beginTryCatch(ctx, type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static final void beginTryCatchIfExistsColumn(Context<?> ctx, DDLStatementType type, QueryPart object) {
|
||||
switch (ctx.family()) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -3817,6 +3865,24 @@ final class Tools {
|
||||
|
||||
|
||||
|
||||
default:
|
||||
endTryCatch(ctx, type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static final void endTryCatchIfExistsColumn(Context<?> ctx, DDLStatementType type, QueryPart object) {
|
||||
switch (ctx.family()) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
default:
|
||||
endTryCatch(ctx, type);
|
||||
break;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user