1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.database.update;
|
10
|
|
11
|
import java.sql.SQLException;
|
12
|
import java.util.List;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
|
16
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
17
|
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
|
18
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
19
|
|
20
|
/**
|
21
|
* @author a.mueller
|
22
|
* @since 16.09.2010
|
23
|
*/
|
24
|
public class ColumnTypeChanger
|
25
|
extends AuditedSchemaUpdaterStepBase {
|
26
|
|
27
|
private static final String _OLDXXX = "_oldxxx";
|
28
|
|
29
|
private static final Logger logger = Logger.getLogger(ColumnTypeChanger.class);
|
30
|
|
31
|
private final String columnName;
|
32
|
private final Datatype newColumnType;
|
33
|
private final Integer newSize;
|
34
|
private final Object defaultValue;
|
35
|
private final boolean isNotNull;
|
36
|
private final String referencedTable;
|
37
|
|
38
|
|
39
|
|
40
|
public static final ColumnTypeChanger NewStringSizeInstance(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String columnName, int newSize, boolean includeAudTable){
|
41
|
return new ColumnTypeChanger(stepList, stepName, tableName, columnName, Datatype.VARCHAR, newSize, includeAudTable, null, false, null);
|
42
|
}
|
43
|
|
44
|
public static final ColumnTypeChanger NewClobInstance(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String columnName, boolean includeAudTable){
|
45
|
return new ColumnTypeChanger(stepList, stepName, tableName, columnName, Datatype.CLOB, null, includeAudTable, null, false, null);
|
46
|
}
|
47
|
|
48
|
public static final ColumnTypeChanger NewInt2DoubleInstance(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String columnName, boolean includeAudTable){
|
49
|
return new ColumnTypeChanger(stepList, stepName, tableName, columnName, Datatype.DOUBLE, null, includeAudTable, null, false, null);
|
50
|
}
|
51
|
|
52
|
public static final ColumnTypeChanger NewInt2StringInstance(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String columnName, int size, boolean includeAudTable, Integer defaultValue, boolean notNull){
|
53
|
return new ColumnTypeChanger(stepList, stepName, tableName, columnName, Datatype.VARCHAR, size, includeAudTable, defaultValue, notNull, null);
|
54
|
}
|
55
|
|
56
|
public static final ColumnTypeChanger NewChangeAllowNullOnIntChanger(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String columnName, boolean includeAudTable){
|
57
|
return new ColumnTypeChanger(stepList, stepName, tableName, columnName, Datatype.INTEGER, null, includeAudTable, null, false, null);
|
58
|
}
|
59
|
|
60
|
public static final ColumnTypeChanger NewChangeAllowNullOnStringChanger(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String columnName, Integer size, boolean includeAudTable){
|
61
|
return new ColumnTypeChanger(stepList, stepName, tableName, columnName, Datatype.VARCHAR, size, includeAudTable, null, false, null);
|
62
|
}
|
63
|
|
64
|
protected ColumnTypeChanger(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String columnName, Datatype newColumnType, Integer size,
|
65
|
boolean includeAudTable, Object defaultValue, boolean notNull, String referencedTable) {
|
66
|
|
67
|
super(stepList, stepName, tableName, includeAudTable);
|
68
|
this.columnName = columnName;
|
69
|
this.newColumnType = newColumnType;
|
70
|
this.newSize = size;
|
71
|
this.defaultValue = defaultValue;
|
72
|
this.isNotNull = notNull;
|
73
|
this.referencedTable = referencedTable;
|
74
|
}
|
75
|
|
76
|
@Override
|
77
|
protected void invokeOnTable(String tableName, ICdmDataSource datasource,
|
78
|
IProgressMonitor monitor, CaseType caseType, SchemaUpdateResult result) {
|
79
|
try {
|
80
|
|
81
|
String updateQuery;
|
82
|
|
83
|
//set null values to default value if NOT NULL
|
84
|
if (this.isNotNull){
|
85
|
updateQuery = getNotNullUpdateQuery(tableName);
|
86
|
datasource.executeUpdate(updateQuery);
|
87
|
}
|
88
|
|
89
|
//update
|
90
|
changeType(tableName, datasource, monitor, caseType, result);
|
91
|
|
92
|
if (defaultValue instanceof Boolean){
|
93
|
updateQuery = "UPDATE @tableName SET @columnName = " + (defaultValue == null ? "null" : getBoolean((Boolean) defaultValue, datasource)) +
|
94
|
" WHERE @columnName IS NULL ";
|
95
|
updateQuery = updateQuery.replace("@tableName", tableName);
|
96
|
updateQuery = updateQuery.replace("@columnName", columnName);
|
97
|
datasource.executeUpdate(updateQuery);
|
98
|
}
|
99
|
|
100
|
//foreign keys
|
101
|
if (referencedTable != null){
|
102
|
TableCreator.makeForeignKey(tableName, datasource, monitor, columnName, referencedTable, caseType, result);
|
103
|
}
|
104
|
|
105
|
return;
|
106
|
} catch ( Exception e) {
|
107
|
String message = "Unhandled exception when trying to change column type for " +
|
108
|
columnName + " for table " + tableName;
|
109
|
monitor.warning(message, e);
|
110
|
logger.error(e);
|
111
|
result.addException(e, message, getStepName());
|
112
|
return;
|
113
|
}
|
114
|
}
|
115
|
|
116
|
|
117
|
protected void changeType(String tableName, ICdmDataSource datasource, IProgressMonitor monitor,
|
118
|
CaseType caseType, SchemaUpdateResult result)
|
119
|
throws DatabaseTypeNotSupportedException, SQLException {
|
120
|
DatabaseTypeEnum type = datasource.getDatabaseType();
|
121
|
if (type.equals(DatabaseTypeEnum.PostgreSQL)){
|
122
|
handlePostgres(tableName, datasource, monitor, caseType, result);
|
123
|
}else{
|
124
|
String updateQuery = getUpdateQueryString(tableName, datasource, monitor);
|
125
|
datasource.executeUpdate(updateQuery);
|
126
|
}
|
127
|
}
|
128
|
|
129
|
private String getNotNullUpdateQuery(String tableName) {
|
130
|
String query = " UPDATE %s SET %s = %s WHERE %s IS NULL ";
|
131
|
String defaultValueStr = String.valueOf(this.defaultValue);
|
132
|
if (this.defaultValue instanceof Integer){
|
133
|
//OK
|
134
|
}else{
|
135
|
defaultValueStr = "'" + defaultValueStr + "'";
|
136
|
}
|
137
|
query = String.format(query, tableName, this.columnName, defaultValueStr, this.columnName);
|
138
|
return query;
|
139
|
}
|
140
|
|
141
|
public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
|
142
|
String updateQuery;
|
143
|
DatabaseTypeEnum type = datasource.getDatabaseType();
|
144
|
String databaseColumnType = getDatabaseColumnType(datasource, this.newColumnType, newSize, null);
|
145
|
|
146
|
if (type.equals(DatabaseTypeEnum.SqlServer2005)){
|
147
|
//MySQL allows both syntaxes
|
148
|
updateQuery = "ALTER TABLE @tableName ALTER COLUMN @columnName @columnType";
|
149
|
}else if (type.equals(DatabaseTypeEnum.H2)){
|
150
|
updateQuery = "ALTER TABLE @tableName ALTER COLUMN @columnName @columnType";
|
151
|
}else if (type.equals(DatabaseTypeEnum.PostgreSQL)){
|
152
|
updateQuery = "ALTER TABLE @tableName ALTER COLUMN @columnName TYPE @columnType";
|
153
|
}else if (type.equals(DatabaseTypeEnum.MySQL)){
|
154
|
updateQuery = "ALTER TABLE @tableName MODIFY COLUMN @columnName @columnType";
|
155
|
}else{
|
156
|
updateQuery = null;
|
157
|
String warning = "Update step '" + this.getStepName() + "' is not supported by " + type.getName();
|
158
|
monitor.warning(warning);
|
159
|
throw new DatabaseTypeNotSupportedException(warning);
|
160
|
}
|
161
|
if (isNotNull && !isAuditing){
|
162
|
if (datasource.getDatabaseType().equals(DatabaseTypeEnum.PostgreSQL)){
|
163
|
logger.warn("NOT NULL not implementd for POSTGRES");
|
164
|
}else{
|
165
|
updateQuery += " NOT NULL";
|
166
|
}
|
167
|
} else{
|
168
|
if (! datasource.getDatabaseType().equals(DatabaseTypeEnum.PostgreSQL)){
|
169
|
updateQuery += " NULL ";
|
170
|
}
|
171
|
}
|
172
|
updateQuery = updateQuery.replace("@tableName", tableName);
|
173
|
updateQuery = updateQuery.replace("@columnName", columnName);
|
174
|
updateQuery = updateQuery.replace("@columnType", databaseColumnType);
|
175
|
// updateQuery = updateQuery.replace("@addSeparator", getAddColumnSeperator(datasource));
|
176
|
|
177
|
return updateQuery;
|
178
|
}
|
179
|
|
180
|
|
181
|
/**
|
182
|
* Postgres has problems with datatype changes as casting does often not really work even if using " ... USING ...."
|
183
|
* resulting in errors like "operator does not exist: character varying >= integer".
|
184
|
* Therefore we better create a new column here and transfer the data from the old column to the new column.
|
185
|
* @param tableName
|
186
|
* @param datasource
|
187
|
* @param monitor
|
188
|
* @param result
|
189
|
* @param caseType
|
190
|
* @throws SQLException
|
191
|
*/
|
192
|
private void handlePostgres(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType, SchemaUpdateResult result) throws SQLException {
|
193
|
//
|
194
|
boolean includeAuditing = false;
|
195
|
String colNameChanged = this.columnName + _OLDXXX;
|
196
|
String databaseColumnType = getDatabaseColumnType(datasource, this.newColumnType, newSize, null);
|
197
|
|
198
|
//change old column name
|
199
|
//note data type is not relevant for ColumnNameChanger with Postgres
|
200
|
ISchemaUpdaterStep step = ColumnNameChanger.NewIntegerInstance(null, this.stepName + " - Change column name",
|
201
|
tableName, this.columnName, colNameChanged, includeAuditing);
|
202
|
step.invoke(datasource, monitor, caseType, result);
|
203
|
|
204
|
//create new column
|
205
|
// step = ColumnAdder.NewStringInstance(this.stepName + " - Add new column", tableName, this.columnName, includeAuditing);
|
206
|
Object defaultValue = null;
|
207
|
step = new ColumnAdder(null, this.stepName + " - Add new column", tableName, this.columnName, newColumnType, newSize, null, includeAuditing, defaultValue, false, null);
|
208
|
step.invoke(datasource, monitor, caseType, result);
|
209
|
|
210
|
//move data
|
211
|
String updateQuery = " UPDATE @tableName SET @columnName = @columnOld::@type ";
|
212
|
String casedTableName = caseType.transformTo(tableName);
|
213
|
updateQuery = updateQuery.replace("@tableName", casedTableName);
|
214
|
updateQuery = updateQuery.replace("@columnName", columnName);
|
215
|
updateQuery = updateQuery.replace("@columnOld", colNameChanged);
|
216
|
updateQuery = updateQuery.replace("@type", databaseColumnType);
|
217
|
// if (this.isAuditing){
|
218
|
// step = SimpleSchemaUpdaterStep.NewAuditedInstance(this.stepName + " - Move data", updateQuery, casedTableName, -99);
|
219
|
// }else{
|
220
|
step = SimpleSchemaUpdaterStep.NewNonAuditedInstance(null, this.stepName + " - Move data", updateQuery, -99);
|
221
|
// }
|
222
|
step.invoke(datasource, monitor, caseType, result);
|
223
|
|
224
|
//delete old column
|
225
|
step = ColumnRemover.NewInstance(null, this.stepName + " - Remove old column", tableName, colNameChanged, includeAuditing);
|
226
|
step.invoke(datasource, monitor, caseType, result);
|
227
|
}
|
228
|
|
229
|
private String getDatabaseColumnType(ICdmDataSource datasource, Datatype columnType, Integer size, Integer scale) {
|
230
|
return columnType.format(datasource, size, scale);
|
231
|
}
|
232
|
|
233
|
public String getReferencedTable() {
|
234
|
return referencedTable;
|
235
|
}
|
236
|
|
237
|
}
|