1 | |
package liquibase.change.core; |
2 | |
|
3 | |
import liquibase.change.AbstractChange; |
4 | |
import liquibase.change.ChangeMetaData; |
5 | |
import liquibase.change.ColumnConfig; |
6 | |
import liquibase.database.Database; |
7 | |
import liquibase.database.core.DerbyDatabase; |
8 | |
import liquibase.database.core.SQLiteDatabase; |
9 | |
import liquibase.database.core.SQLiteDatabase.AlterTableVisitor; |
10 | |
import liquibase.database.structure.Index; |
11 | |
import liquibase.exception.UnsupportedChangeException; |
12 | |
import liquibase.statement.SqlStatement; |
13 | |
import liquibase.statement.core.RawSqlStatement; |
14 | |
import liquibase.util.StringUtils; |
15 | |
|
16 | |
import java.util.ArrayList; |
17 | |
import java.util.Arrays; |
18 | |
import java.util.List; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
public class MergeColumnChange extends AbstractChange { |
24 | |
|
25 | |
private String schemaName; |
26 | |
private String tableName; |
27 | |
private String column1Name; |
28 | |
private String joinString; |
29 | |
private String column2Name; |
30 | |
private String finalColumnName; |
31 | |
private String finalColumnType; |
32 | |
|
33 | |
public MergeColumnChange() { |
34 | 10 | super("mergeColumns", "Merge Column", ChangeMetaData.PRIORITY_DEFAULT); |
35 | 10 | } |
36 | |
|
37 | |
public String getSchemaName() { |
38 | 0 | return schemaName; |
39 | |
} |
40 | |
|
41 | |
public void setSchemaName(String schemaName) { |
42 | 0 | this.schemaName = StringUtils.trimToNull(schemaName); |
43 | 0 | } |
44 | |
|
45 | |
public String getTableName() { |
46 | 0 | return tableName; |
47 | |
} |
48 | |
|
49 | |
public void setTableName(String tableName) { |
50 | 0 | this.tableName = tableName; |
51 | 0 | } |
52 | |
|
53 | |
public String getColumn1Name() { |
54 | 0 | return column1Name; |
55 | |
} |
56 | |
|
57 | |
public void setColumn1Name(String column1Name) { |
58 | 0 | this.column1Name = column1Name; |
59 | 0 | } |
60 | |
|
61 | |
public String getJoinString() { |
62 | 0 | return joinString; |
63 | |
} |
64 | |
|
65 | |
public void setJoinString(String joinString) { |
66 | 0 | this.joinString = joinString; |
67 | 0 | } |
68 | |
|
69 | |
public String getColumn2Name() { |
70 | 0 | return column2Name; |
71 | |
} |
72 | |
|
73 | |
public void setColumn2Name(String column2Name) { |
74 | 0 | this.column2Name = column2Name; |
75 | 0 | } |
76 | |
|
77 | |
public String getFinalColumnName() { |
78 | 0 | return finalColumnName; |
79 | |
} |
80 | |
|
81 | |
public void setFinalColumnName(String finalColumnName) { |
82 | 0 | this.finalColumnName = finalColumnName; |
83 | 0 | } |
84 | |
|
85 | |
public String getFinalColumnType() { |
86 | 0 | return finalColumnType; |
87 | |
} |
88 | |
|
89 | |
public void setFinalColumnType(String finalColumnType) { |
90 | 0 | this.finalColumnType = finalColumnType; |
91 | 0 | } |
92 | |
|
93 | |
public SqlStatement[] generateStatements(Database database) { |
94 | |
|
95 | 0 | List<SqlStatement> statements = new ArrayList<SqlStatement>(); |
96 | |
|
97 | 0 | AddColumnChange addNewColumnChange = new AddColumnChange(); |
98 | 0 | String schemaName = getSchemaName() == null ? database.getDefaultSchemaName() : getSchemaName(); |
99 | 0 | addNewColumnChange.setSchemaName(schemaName); |
100 | 0 | addNewColumnChange.setTableName(getTableName()); |
101 | 0 | ColumnConfig columnConfig = new ColumnConfig(); |
102 | 0 | columnConfig.setName(getFinalColumnName()); |
103 | 0 | columnConfig.setType(getFinalColumnType()); |
104 | 0 | addNewColumnChange.addColumn(columnConfig); |
105 | 0 | statements.addAll(Arrays.asList(addNewColumnChange.generateStatements(database))); |
106 | |
|
107 | 0 | String updateStatement = "UPDATE " + database.escapeTableName(schemaName, getTableName()) + " SET " |
108 | |
+ getFinalColumnName() + " = " |
109 | |
+ database.getConcatSql(getColumn1Name(), "'" + getJoinString() + "'", getColumn2Name()); |
110 | |
|
111 | 0 | statements.add(new RawSqlStatement(updateStatement)); |
112 | |
|
113 | 0 | if (database instanceof SQLiteDatabase) { |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | 0 | AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() { |
120 | |
public ColumnConfig[] getColumnsToAdd() { |
121 | 0 | ColumnConfig[] new_columns = new ColumnConfig[1]; |
122 | 0 | ColumnConfig new_column = new ColumnConfig(); |
123 | 0 | new_column.setName(getFinalColumnName()); |
124 | 0 | new_column.setType(getFinalColumnType()); |
125 | 0 | new_columns[0] = new ColumnConfig(new_column); |
126 | 0 | return new_columns; |
127 | |
} |
128 | |
|
129 | |
public boolean copyThisColumn(ColumnConfig column) { |
130 | 0 | return !(column.getName().equals(getColumn1Name()) || column.getName().equals(getColumn2Name())); |
131 | |
} |
132 | |
|
133 | |
public boolean createThisColumn(ColumnConfig column) { |
134 | 0 | return !(column.getName().equals(getColumn1Name()) || column.getName().equals(getColumn2Name())); |
135 | |
} |
136 | |
|
137 | |
public boolean createThisIndex(Index index) { |
138 | 0 | return !(index.getColumns().contains(getColumn1Name()) || index.getColumns().contains( |
139 | |
getColumn2Name())); |
140 | |
} |
141 | |
}; |
142 | |
|
143 | |
try { |
144 | |
|
145 | 0 | statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, |
146 | |
getSchemaName(), getTableName())); |
147 | 0 | } catch (Exception e) { |
148 | 0 | e.printStackTrace(); |
149 | 0 | } |
150 | |
|
151 | 0 | } else { |
152 | |
|
153 | |
|
154 | 0 | DropColumnChange dropColumn1Change = new DropColumnChange(); |
155 | 0 | dropColumn1Change.setSchemaName(schemaName); |
156 | 0 | dropColumn1Change.setTableName(getTableName()); |
157 | 0 | dropColumn1Change.setColumnName(getColumn1Name()); |
158 | 0 | statements.addAll(Arrays.asList(dropColumn1Change.generateStatements(database))); |
159 | |
|
160 | 0 | DropColumnChange dropColumn2Change = new DropColumnChange(); |
161 | 0 | dropColumn2Change.setSchemaName(schemaName); |
162 | 0 | dropColumn2Change.setTableName(getTableName()); |
163 | 0 | dropColumn2Change.setColumnName(getColumn2Name()); |
164 | 0 | statements.addAll(Arrays.asList(dropColumn2Change.generateStatements(database))); |
165 | |
|
166 | |
} |
167 | 0 | return statements.toArray(new SqlStatement[statements.size()]); |
168 | |
|
169 | |
} |
170 | |
|
171 | |
public SqlStatement[] generateStatements(@SuppressWarnings("unused") DerbyDatabase database) |
172 | |
throws UnsupportedChangeException { |
173 | 0 | throw new UnsupportedChangeException("Derby does not currently support merging columns"); |
174 | |
} |
175 | |
|
176 | |
public String getConfirmationMessage() { |
177 | 0 | return "Columns " + getTableName() + "." + getColumn1Name() + " and " + getTableName() + "." + getColumn2Name() |
178 | |
+ " merged"; |
179 | |
} |
180 | |
} |