001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.DerbyDatabase;
005: import liquibase.database.sql.RawSqlStatement;
006: import liquibase.database.sql.SqlStatement;
007: import liquibase.database.structure.Column;
008: import liquibase.database.structure.DatabaseObject;
009: import liquibase.database.structure.Table;
010: import liquibase.exception.UnsupportedChangeException;
011: import org.w3c.dom.Document;
012: import org.w3c.dom.Element;
013:
014: import java.util.*;
015:
016: /**
017: * Combines data from two existing columns into a new column and drops the original columns.
018: */
019: public class MergeColumnChange extends AbstractChange {
020:
021: private String schemaName;
022: private String tableName;
023: private String column1Name;
024: private String joinString;
025: private String column2Name;
026: private String finalColumnName;
027: private String finalColumnType;
028:
029: public MergeColumnChange() {
030: super ("mergeColumns", "Merge Column");
031: }
032:
033: public String getSchemaName() {
034: return schemaName;
035: }
036:
037: public void setSchemaName(String schemaName) {
038: this .schemaName = schemaName;
039: }
040:
041: public String getTableName() {
042: return tableName;
043: }
044:
045: public void setTableName(String tableName) {
046: this .tableName = tableName;
047: }
048:
049: public String getColumn1Name() {
050: return column1Name;
051: }
052:
053: public void setColumn1Name(String column1Name) {
054: this .column1Name = column1Name;
055: }
056:
057: public String getJoinString() {
058: return joinString;
059: }
060:
061: public void setJoinString(String joinString) {
062: this .joinString = joinString;
063: }
064:
065: public String getColumn2Name() {
066: return column2Name;
067: }
068:
069: public void setColumn2Name(String column2Name) {
070: this .column2Name = column2Name;
071: }
072:
073: public String getFinalColumnName() {
074: return finalColumnName;
075: }
076:
077: public void setFinalColumnName(String finalColumnName) {
078: this .finalColumnName = finalColumnName;
079: }
080:
081: public String getFinalColumnType() {
082: return finalColumnType;
083: }
084:
085: public void setFinalColumnType(String finalColumnType) {
086: this .finalColumnType = finalColumnType;
087: }
088:
089: public SqlStatement[] generateStatements(Database database)
090: throws UnsupportedChangeException {
091:
092: List<SqlStatement> statements = new ArrayList<SqlStatement>();
093:
094: AddColumnChange addNewColumnChange = new AddColumnChange();
095: String schemaName = getSchemaName() == null ? database
096: .getDefaultSchemaName() : getSchemaName();
097: addNewColumnChange.setSchemaName(schemaName);
098: addNewColumnChange.setTableName(getTableName());
099: ColumnConfig columnConfig = new ColumnConfig();
100: columnConfig.setName(getFinalColumnName());
101: columnConfig.setType(getFinalColumnType());
102: addNewColumnChange.addColumn(columnConfig);
103: statements.addAll(Arrays.asList(addNewColumnChange
104: .generateStatements(database)));
105:
106: String updateStatement = "UPDATE "
107: + database.escapeTableName(schemaName, getTableName())
108: + " SET "
109: + getFinalColumnName()
110: + " = "
111: + database.getConcatSql(getColumn1Name(), "'"
112: + getJoinString() + "'", getColumn2Name());
113:
114: statements.add(new RawSqlStatement(updateStatement));
115:
116: DropColumnChange dropColumn1Change = new DropColumnChange();
117: dropColumn1Change.setSchemaName(schemaName);
118: dropColumn1Change.setTableName(getTableName());
119: dropColumn1Change.setColumnName(getColumn1Name());
120: statements.addAll(Arrays.asList(dropColumn1Change
121: .generateStatements(database)));
122:
123: DropColumnChange dropColumn2Change = new DropColumnChange();
124: dropColumn2Change.setSchemaName(schemaName);
125: dropColumn2Change.setTableName(getTableName());
126: dropColumn2Change.setColumnName(getColumn2Name());
127: statements.addAll(Arrays.asList(dropColumn2Change
128: .generateStatements(database)));
129:
130: return statements.toArray(new SqlStatement[statements.size()]);
131:
132: }
133:
134: public SqlStatement[] generateStatements(
135: @SuppressWarnings("unused")
136: DerbyDatabase database) throws UnsupportedChangeException {
137: throw new UnsupportedChangeException(
138: "Derby does not currently support merging columns");
139: }
140:
141: public String getConfirmationMessage() {
142: return "Columns " + getTableName() + "." + getColumn1Name()
143: + " and " + getTableName() + "." + getColumn2Name()
144: + " merged";
145: }
146:
147: public Element createNode(Document currentChangeLogFileDOM) {
148: Element element = currentChangeLogFileDOM
149: .createElement(getTagName());
150: if (getSchemaName() != null) {
151: element.setAttribute("schemaName", getSchemaName());
152: }
153:
154: element.setAttribute("tableName", getTableName());
155: element.setAttribute("column1Name", getColumn1Name());
156: element.setAttribute("joinString", getJoinString());
157: element.setAttribute("column2Name", getColumn2Name());
158: element.setAttribute("finalColumnName", getFinalColumnName());
159: element.setAttribute("finalColumnType", getFinalColumnType());
160:
161: return element;
162: }
163:
164: public Set<DatabaseObject> getAffectedDatabaseObjects() {
165: Set<DatabaseObject> returnSet = new HashSet<DatabaseObject>();
166: Table table = new Table(getTableName());
167: returnSet.add(table);
168:
169: Column column1 = new Column();
170: column1.setTable(table);
171: column1.setName(column1Name);
172: returnSet.add(column1);
173:
174: Column column2 = new Column();
175: column2.setTable(table);
176: column2.setName(column2Name);
177: returnSet.add(column2);
178:
179: return returnSet;
180: }
181:
182: }
|