01: package liquibase.change;
02:
03: import liquibase.database.DB2Database;
04: import liquibase.database.Database;
05: import liquibase.database.sql.DropColumnStatement;
06: import liquibase.database.sql.ReorganizeTableStatement;
07: import liquibase.database.sql.SqlStatement;
08: import liquibase.database.structure.Column;
09: import liquibase.database.structure.DatabaseObject;
10: import liquibase.database.structure.Table;
11: import liquibase.exception.UnsupportedChangeException;
12: import org.w3c.dom.Document;
13: import org.w3c.dom.Element;
14:
15: import java.util.*;
16:
17: /**
18: * Drops an existing column from a table.
19: */
20: public class DropColumnChange extends AbstractChange {
21:
22: private String schemaName;
23: private String tableName;
24: private String columnName;
25:
26: public DropColumnChange() {
27: super ("dropColumn", "Drop Column");
28: }
29:
30: public String getColumnName() {
31: return columnName;
32: }
33:
34: public void setColumnName(String columnName) {
35: this .columnName = columnName;
36: }
37:
38: public String getSchemaName() {
39: return schemaName;
40: }
41:
42: public void setSchemaName(String schemaName) {
43: this .schemaName = schemaName;
44: }
45:
46: public String getTableName() {
47: return tableName;
48: }
49:
50: public void setTableName(String tableName) {
51: this .tableName = tableName;
52: }
53:
54: public SqlStatement[] generateStatements(Database database)
55: throws UnsupportedChangeException {
56: List<SqlStatement> statements = new ArrayList<SqlStatement>();
57: String schemaName = getSchemaName() == null ? database
58: .getDefaultSchemaName() : getSchemaName();
59:
60: statements.add(new DropColumnStatement(schemaName,
61: getTableName(), getColumnName()));
62: if (database instanceof DB2Database) {
63: statements.add(new ReorganizeTableStatement(schemaName,
64: getTableName()));
65: }
66: return statements.toArray(new SqlStatement[statements.size()]);
67: }
68:
69: public String getConfirmationMessage() {
70: return "Column " + getTableName() + "." + getColumnName()
71: + " dropped";
72: }
73:
74: public Element createNode(Document currentChangeLogFileDOM) {
75: Element element = currentChangeLogFileDOM
76: .createElement("dropColumn");
77: if (getSchemaName() != null) {
78: element.setAttribute("schemaName", getSchemaName());
79: }
80:
81: element.setAttribute("tableName", getTableName());
82: element.setAttribute("columnName", getColumnName());
83: return element;
84: }
85:
86: public Set<DatabaseObject> getAffectedDatabaseObjects() {
87:
88: Table table = new Table(getTableName());
89:
90: Column column = new Column();
91: column.setTable(table);
92: column.setName(columnName);
93:
94: return new HashSet<DatabaseObject>(Arrays.asList(table, column));
95:
96: }
97:
98: }
|