001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.sql.RenameColumnStatement;
005: import liquibase.database.sql.SqlStatement;
006: import liquibase.database.structure.Column;
007: import liquibase.database.structure.DatabaseObject;
008: import liquibase.database.structure.Table;
009: import liquibase.exception.UnsupportedChangeException;
010: import org.w3c.dom.Document;
011: import org.w3c.dom.Element;
012:
013: import java.util.Arrays;
014: import java.util.HashSet;
015: import java.util.Set;
016:
017: /**
018: * Renames an existing column.
019: */
020: public class RenameColumnChange extends AbstractChange {
021:
022: private String schemaName;
023: private String tableName;
024: private String oldColumnName;
025: private String newColumnName;
026: private String columnDataType;
027:
028: public RenameColumnChange() {
029: super ("renameColumn", "Rename Column");
030: }
031:
032: public String getSchemaName() {
033: return schemaName;
034: }
035:
036: public void setSchemaName(String schemaName) {
037: this .schemaName = schemaName;
038: }
039:
040: public String getTableName() {
041: return tableName;
042: }
043:
044: public void setTableName(String tableName) {
045: this .tableName = tableName;
046: }
047:
048: public String getOldColumnName() {
049: return oldColumnName;
050: }
051:
052: public void setOldColumnName(String oldColumnName) {
053: this .oldColumnName = oldColumnName;
054: }
055:
056: public String getNewColumnName() {
057: return newColumnName;
058: }
059:
060: public void setNewColumnName(String newColumnName) {
061: this .newColumnName = newColumnName;
062: }
063:
064: public String getColumnDataType() {
065: return columnDataType;
066: }
067:
068: public void setColumnDataType(String columnDataType) {
069: this .columnDataType = columnDataType;
070: }
071:
072: public SqlStatement[] generateStatements(Database database)
073: throws UnsupportedChangeException {
074: return new SqlStatement[] { new RenameColumnStatement(
075: getSchemaName() == null ? database
076: .getDefaultSchemaName() : getSchemaName(),
077: getTableName(), getOldColumnName(), getNewColumnName(),
078: getColumnDataType()) };
079: }
080:
081: protected Change[] createInverses() {
082: RenameColumnChange inverse = new RenameColumnChange();
083: inverse.setSchemaName(getSchemaName());
084: inverse.setTableName(getTableName());
085: inverse.setOldColumnName(getNewColumnName());
086: inverse.setNewColumnName(getOldColumnName());
087: inverse.setColumnDataType(getColumnDataType());
088:
089: return new Change[] { inverse };
090: }
091:
092: public String getConfirmationMessage() {
093: return "Column " + tableName + "." + oldColumnName
094: + " renamed to " + newColumnName;
095: }
096:
097: public Element createNode(Document currentChangeLogFileDOM) {
098: Element node = currentChangeLogFileDOM
099: .createElement("renameColumn");
100: if (getSchemaName() != null) {
101: node.setAttribute("schemaName", getSchemaName());
102: }
103:
104: node.setAttribute("tableName", getTableName());
105: node.setAttribute("oldColumnName", getOldColumnName());
106: node.setAttribute("newColumnName", getNewColumnName());
107:
108: return node;
109: }
110:
111: public Set<DatabaseObject> getAffectedDatabaseObjects() {
112:
113: Table table = new Table(getTableName());
114:
115: Column oldColumn = new Column();
116: oldColumn.setTable(table);
117: oldColumn.setName(getOldColumnName());
118:
119: Column newColumn = new Column();
120: newColumn.setTable(table);
121: newColumn.setName(getNewColumnName());
122:
123: return new HashSet<DatabaseObject>(Arrays.asList(table,
124: oldColumn, newColumn));
125:
126: }
127:
128: }
|