01: package liquibase.change;
02:
03: import liquibase.database.Database;
04: import liquibase.database.sql.DropForeignKeyConstraintStatement;
05: import liquibase.database.sql.SqlStatement;
06: import liquibase.database.structure.DatabaseObject;
07: import liquibase.database.structure.ForeignKey;
08: import liquibase.database.structure.Table;
09: import liquibase.exception.UnsupportedChangeException;
10: import org.w3c.dom.Document;
11: import org.w3c.dom.Element;
12:
13: import java.util.HashSet;
14: import java.util.Set;
15:
16: /**
17: * Drops an existing foreign key constraint.
18: */
19: public class DropForeignKeyConstraintChange extends AbstractChange {
20: private String baseTableSchemaName;
21: private String baseTableName;
22: private String constraintName;
23:
24: public DropForeignKeyConstraintChange() {
25: super ("dropForeignKeyConstraint", "Drop Foreign Key Constraint");
26: }
27:
28: public String getBaseTableSchemaName() {
29: return baseTableSchemaName;
30: }
31:
32: public void setBaseTableSchemaName(String baseTableSchemaName) {
33: this .baseTableSchemaName = baseTableSchemaName;
34: }
35:
36: public String getBaseTableName() {
37: return baseTableName;
38: }
39:
40: public void setBaseTableName(String baseTableName) {
41: this .baseTableName = baseTableName;
42: }
43:
44: public String getConstraintName() {
45: return constraintName;
46: }
47:
48: public void setConstraintName(String constraintName) {
49: this .constraintName = constraintName;
50: }
51:
52: public SqlStatement[] generateStatements(Database database)
53: throws UnsupportedChangeException {
54: return new SqlStatement[] { new DropForeignKeyConstraintStatement(
55: getBaseTableSchemaName() == null ? database
56: .getDefaultSchemaName()
57: : getBaseTableSchemaName(), getBaseTableName(),
58: getConstraintName()), };
59: }
60:
61: public String getConfirmationMessage() {
62: return "Foreign key " + getConstraintName() + " dropped";
63: }
64:
65: public Element createNode(Document currentChangeLogFileDOM) {
66: Element node = currentChangeLogFileDOM
67: .createElement(getTagName());
68:
69: if (getBaseTableSchemaName() != null) {
70: node.setAttribute("baseTableSchemaName",
71: getBaseTableSchemaName());
72: }
73:
74: node.setAttribute("baseTableName", getBaseTableName());
75: node.setAttribute("constraintName", getConstraintName());
76:
77: return node;
78: }
79:
80: public Set<DatabaseObject> getAffectedDatabaseObjects() {
81: Set<DatabaseObject> returnSet = new HashSet<DatabaseObject>();
82:
83: Table baseTable = new Table(getBaseTableName());
84: returnSet.add(baseTable);
85:
86: ForeignKey fk = new ForeignKey();
87: fk.setName(constraintName);
88: fk.setForeignKeyTable(baseTable);
89: returnSet.add(fk);
90:
91: return returnSet;
92:
93: }
94:
95: }
|