[jOOQ/jOOQ#9380] Support DROP CONSTRAINT .. [ CASCADE | RESTRICT ]

Added support in:
- API
- Object model
- Parser
- Interpreter

Still missing:
- SQL generation
- Integration tests
This commit is contained in:
Lukas Eder 2019-10-16 12:48:07 +02:00
parent fb7e46f452
commit e1900b6419
5 changed files with 38 additions and 39 deletions

View File

@ -40,7 +40,7 @@ package org.jooq;
/**
* The step in the <code>ALTER TABLE</code> DSL used to <code>DROP</code>
* columns.
* columns or constraints.
* <p>
* <h3>Referencing <code>XYZ*Step</code> types directly from client code</h3>
* <p>
@ -66,14 +66,14 @@ public interface AlterTableDropStep extends AlterTableFinalStep {
/**
* Add a <code>CASCADE</code> clause to the
* <code>ALTER TABLE .. DROP COLUMN</code> statement.
* <code>ALTER TABLE .. DROP</code> statement.
*/
@Support
AlterTableFinalStep cascade();
/**
* Add a <code>RESTRICT</code> clause to the
* <code>ALTER TABLE .. DROP COLUMN</code> statement.
* <code>ALTER TABLE .. DROP</code> statement.
*/
@Support
AlterTableFinalStep restrict();

View File

@ -558,7 +558,7 @@ public interface AlterTableStep {
* statement.
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES })
AlterTableFinalStep drop(Constraint constraint);
AlterTableDropStep drop(Constraint constraint);
/**
* Add a <code>DROP CONSTRAINT</code> clause to the <code>ALTER TABLE</code>
@ -567,7 +567,7 @@ public interface AlterTableStep {
* @see DSL#constraint(String)
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES })
AlterTableFinalStep dropConstraint(Constraint constraint);
AlterTableDropStep dropConstraint(Constraint constraint);
/**
* Add a <code>DROP CONSTRAINT</code> clause to the <code>ALTER TABLE</code>
@ -576,7 +576,7 @@ public interface AlterTableStep {
* @see DSL#constraint(String)
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES })
AlterTableFinalStep dropConstraint(Name constraint);
AlterTableDropStep dropConstraint(Name constraint);
/**
* Add a <code>DROP CONSTRAINT</code> clause to the <code>ALTER TABLE</code>
@ -585,14 +585,14 @@ public interface AlterTableStep {
* @see DSL#constraint(String)
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, POSTGRES })
AlterTableFinalStep dropConstraint(String constraint);
AlterTableDropStep dropConstraint(String constraint);
/**
* Add a <code>DROP PRIMARY KEY</code> clause to the <code>ALTER TABLE</code>
* statement.
*/
@Support({ DERBY, H2, HSQLDB, MARIADB, MYSQL })
AlterTableFinalStep dropPrimaryKey();
AlterTableDropStep dropPrimaryKey();
/**
* Add a <code>DROP PRIMARY KEY</code> clause to the
@ -605,7 +605,7 @@ public interface AlterTableStep {
* {@link #dropConstraint(Constraint)}.
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
AlterTableFinalStep dropPrimaryKey(Constraint constraint);
AlterTableDropStep dropPrimaryKey(Constraint constraint);
/**
* Add a <code>DROP PRIMARY KEY</code> clause to the <code>ALTER TABLE</code>
@ -620,7 +620,7 @@ public interface AlterTableStep {
* @see DSL#constraint(Name)
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
AlterTableFinalStep dropPrimaryKey(Name constraint);
AlterTableDropStep dropPrimaryKey(Name constraint);
/**
* Add a <code>DROP PRIMARY KEY</code> clause to the <code>ALTER TABLE</code>
@ -635,14 +635,14 @@ public interface AlterTableStep {
* @see DSL#constraint(String)
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
AlterTableFinalStep dropPrimaryKey(String constraint);
AlterTableDropStep dropPrimaryKey(String constraint);
/**
* Add a <code>DROP FOREIGN KEY</code> clause to the <code>ALTER TABLE</code>
* statement.
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
AlterTableFinalStep dropForeignKey(Constraint constraint);
AlterTableDropStep dropForeignKey(Constraint constraint);
/**
* Add a <code>DROP FOREIGN KEY</code> clause to the <code>ALTER TABLE</code>
@ -651,7 +651,7 @@ public interface AlterTableStep {
* @see DSL#constraint(Name)
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
AlterTableFinalStep dropForeignKey(Name constraint);
AlterTableDropStep dropForeignKey(Name constraint);
/**
* Add a <code>DROP FOREIGN KEY</code> clause to the <code>ALTER TABLE</code>
@ -660,5 +660,5 @@ public interface AlterTableStep {
* @see DSL#constraint(String)
*/
@Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES })
AlterTableFinalStep dropForeignKey(String constraint);
AlterTableDropStep dropForeignKey(String constraint);
}

View File

@ -257,9 +257,9 @@ final class AlterTableImpl extends AbstractRowCountQuery implements
private Field<?> alterColumnDefault;
private boolean alterColumnDropDefault;
private QueryPartList<Field<?>> dropColumns;
private boolean dropColumnCascade;
private Constraint dropConstraint;
private ConstraintType dropConstraintType;
private boolean dropCascade;
AlterTableImpl(Configuration configuration, Table<?> table) {
this(configuration, table, false);
@ -293,7 +293,7 @@ final class AlterTableImpl extends AbstractRowCountQuery implements
final Constraint $renameConstraint() { return renameConstraint; }
final Constraint $renameConstraintTo() { return renameConstraintTo; }
final List<Field<?>> $dropColumns() { return dropColumns; }
final boolean $dropColumnsCascade() { return dropColumnCascade; }
final boolean $dropCascade() { return dropCascade; }
final Constraint $dropConstraint() { return dropConstraint; }
final ConstraintType $dropConstraintType() { return dropConstraintType; }
@ -839,13 +839,13 @@ final class AlterTableImpl extends AbstractRowCountQuery implements
@Override
public final AlterTableFinalStep cascade() {
dropColumnCascade = true;
dropCascade = true;
return this;
}
@Override
public final AlterTableFinalStep restrict() {
dropColumnCascade = false;
dropCascade = false;
return this;
}
@ -1450,7 +1450,7 @@ final class AlterTableImpl extends AbstractRowCountQuery implements
if (dropColumnCascade)
if (dropCascade)
ctx.sql(' ').visit(K_CASCADE);
}

View File

@ -433,7 +433,7 @@ final class DDLInterpreter {
if (fields.size() < query.$dropColumns().size() && !query.$ifExistsColumn())
existing.fields(query.$dropColumns().toArray(EMPTY_FIELD), true);
dropColumns(existing, fields, query.$dropColumnsCascade());
dropColumns(existing, fields, query.$dropCascade());
}
else if (query.$dropConstraint() != null) {
ConstraintImpl impl = (ConstraintImpl) query.$dropConstraint();
@ -454,7 +454,7 @@ final class DDLInterpreter {
MutableUniqueKey key = uks.next();
if (key.name.equals(impl.getUnqualifiedName())) {
cascade(key, null, false);
cascade(key, null, query.$dropCascade());
uks.remove();
break removal;
}
@ -462,7 +462,7 @@ final class DDLInterpreter {
if (existing.primaryKey != null) {
if (existing.primaryKey.name.equals(impl.getUnqualifiedName())) {
cascade(existing.primaryKey, null, false);
cascade(existing.primaryKey, null, query.$dropCascade());
existing.primaryKey = null;
break removal;
}

View File

@ -3774,11 +3774,11 @@ final class ParserImpl implements Parser {
case 'D':
if (parseKeywordIf(ctx, "DROP")) {
if (parseKeywordIf(ctx, "CONSTRAINT")) {
return s1.dropConstraint(parseIdentifier(ctx));
return parseCascadeRestrictIf(ctx, s1.dropConstraint(parseIdentifier(ctx)));
}
else if (parseKeywordIf(ctx, "PRIMARY KEY")) {
Name identifier = parseIdentifierIf(ctx);
return identifier == null ? s1.dropPrimaryKey() : s1.dropPrimaryKey(identifier);
return parseCascadeRestrictIf(ctx, identifier == null ? s1.dropPrimaryKey() : s1.dropPrimaryKey(identifier));
}
else if (parseKeywordIf(ctx, "FOREIGN KEY")) {
return s1.dropForeignKey(parseIdentifier(ctx));
@ -3809,24 +3809,12 @@ final class ParserImpl implements Parser {
if (parens)
parse(ctx, ')');
boolean cascade = parseKeywordIf(ctx, "CASCADE");
boolean restrict = !cascade && parseKeywordIf(ctx, "RESTRICT");
AlterTableDropStep s2 =
fields == null
return parseCascadeRestrictIf(ctx, fields == null
? ifColumnExists
? s1.dropColumnIfExists(field)
: s1.dropColumn(field)
: s1.dropColumns(fields);
AlterTableFinalStep s3 =
cascade
? s2.cascade()
: restrict
? s2.restrict()
: s2;
return s3;
: s1.dropColumns(fields)
);
}
}
@ -3879,6 +3867,17 @@ final class ParserImpl implements Parser {
throw ctx.expected("ADD", "ALTER", "COMMENT", "DROP", "MODIFY", "RENAME");
}
private static final AlterTableFinalStep parseCascadeRestrictIf(ParserContext ctx, AlterTableDropStep step) {
boolean cascade = parseKeywordIf(ctx, "CASCADE");
boolean restrict = !cascade && parseKeywordIf(ctx, "RESTRICT");
return cascade
? step.cascade()
: restrict
? step.restrict()
: step;
}
private static final DDLQuery parseAlterTableAdd(ParserContext ctx, AlterTableStep s1, Table<?> tableName) {
List<FieldOrConstraint> list = new ArrayList<>();