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.util.List;
|
12
|
|
13
|
import org.apache.log4j.Logger;
|
14
|
|
15
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
16
|
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
|
17
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
18
|
|
19
|
/**
|
20
|
* @author a.mueller
|
21
|
* @since 16.09.2010
|
22
|
*
|
23
|
*/
|
24
|
public class ColumnNameChanger
|
25
|
extends AuditedSchemaUpdaterStepBase{
|
26
|
|
27
|
private static final Logger logger = Logger.getLogger(ColumnNameChanger.class);
|
28
|
|
29
|
private String newColumnName;
|
30
|
private String oldColumnName;
|
31
|
private Datatype datatype;
|
32
|
private Integer size; //only required for MySQL
|
33
|
|
34
|
public static ColumnNameChanger NewIntegerInstance(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String oldColumnName, String newColumnName, boolean includeAudTable){
|
35
|
return new ColumnNameChanger(stepList, stepName, tableName, oldColumnName, newColumnName, includeAudTable, null, Datatype.INTEGER, null);
|
36
|
}
|
37
|
|
38
|
public static ColumnNameChanger NewFloatInstance(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String oldColumnName, String newColumnName, boolean includeAudTable){
|
39
|
return new ColumnNameChanger(stepList, stepName, tableName, oldColumnName, newColumnName, includeAudTable, null, Datatype.FLOAT, null);
|
40
|
}
|
41
|
|
42
|
public static ColumnNameChanger NewClobInstance(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String oldColumnName,
|
43
|
String newColumnName, boolean includeAudTable){
|
44
|
return new ColumnNameChanger(stepList, stepName, tableName, oldColumnName, newColumnName, includeAudTable, null, Datatype.CLOB, null);
|
45
|
}
|
46
|
|
47
|
public static ColumnNameChanger NewVarCharInstance(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String oldColumnName,
|
48
|
String newColumnName, int size, boolean includeAudTable){
|
49
|
return new ColumnNameChanger(stepList, stepName, tableName, oldColumnName, newColumnName, includeAudTable, null, Datatype.VARCHAR, size);
|
50
|
}
|
51
|
|
52
|
|
53
|
public static ColumnNameChanger NewDateTimeInstance(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String oldColumnName,
|
54
|
String newColumnName, boolean includeAudTable){
|
55
|
return new ColumnNameChanger(stepList, stepName, tableName, oldColumnName, newColumnName, includeAudTable, null, Datatype.DATETIME, null);
|
56
|
}
|
57
|
|
58
|
// **************************************** Constructor ***************************************/
|
59
|
|
60
|
protected ColumnNameChanger(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String oldColumnName,
|
61
|
String newColumnName, boolean includeAudTable, Object defaultValue, Datatype datatype, Integer size) {
|
62
|
super(stepList, stepName, tableName, includeAudTable);
|
63
|
this.newColumnName = newColumnName;
|
64
|
this.oldColumnName = oldColumnName;
|
65
|
this.datatype = datatype;
|
66
|
this.size = size;
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
protected void invokeOnTable(String tableName, ICdmDataSource datasource,
|
71
|
IProgressMonitor monitor, CaseType caseType, SchemaUpdateResult result) {
|
72
|
try {
|
73
|
DatabaseTypeEnum type = datasource.getDatabaseType();
|
74
|
String updateQuery;
|
75
|
|
76
|
if (type.equals(DatabaseTypeEnum.SqlServer2005)){
|
77
|
result.addWarning("SQLServer column name changer syntax not yet tested. Table name: " + this.tableName + "; old column name: " + oldColumnName + "; new column name: " + newColumnName);
|
78
|
updateQuery = "EXEC sp_rename '@oldName', '@newName'";
|
79
|
}else if (type.equals(DatabaseTypeEnum.H2)){
|
80
|
updateQuery = "ALTER TABLE @tableName ALTER COLUMN @oldColumnName RENAME TO @newColumnName";
|
81
|
}else if ( type.equals(DatabaseTypeEnum.MySQL)){
|
82
|
//FIXME MySQL column name changer
|
83
|
// logger.warn("Changing column name not yet supported for MySQL");
|
84
|
updateQuery = "ALTER TABLE @tableName CHANGE COLUMN @oldColumnName @newColumnName @definition";
|
85
|
}else if ( type.equals(DatabaseTypeEnum.PostgreSQL) ){
|
86
|
updateQuery = "ALTER TABLE @tableName RENAME COLUMN @oldColumnName TO @newColumnName;";
|
87
|
}else{
|
88
|
updateQuery = null;
|
89
|
String message = "Update step '" + this.getStepName() + "' is not supported by " + type.getName();
|
90
|
monitor.warning(message);
|
91
|
result.addError(message, getStepName() + ", ColumnNameChanger.invokeOnTable");
|
92
|
return;
|
93
|
}
|
94
|
updateQuery = updateQuery.replace("@tableName", tableName);
|
95
|
updateQuery = updateQuery.replace("@oldColumnName", oldColumnName);
|
96
|
updateQuery = updateQuery.replace("@newColumnName", newColumnName);
|
97
|
updateQuery = updateQuery.replace("@definition", getDefinition(datasource));
|
98
|
datasource.executeUpdate(updateQuery);
|
99
|
|
100
|
return;
|
101
|
} catch (Exception e) {
|
102
|
String message = e.getMessage();
|
103
|
monitor.warning(message, e);
|
104
|
logger.error(e);
|
105
|
result.addException(e, message, getStepName() + ", ColumnNameChanger.invokeOnTable");
|
106
|
return;
|
107
|
}
|
108
|
}
|
109
|
|
110
|
private CharSequence getDefinition(ICdmDataSource datasource) {
|
111
|
return datatype.format(datasource, size);
|
112
|
}
|
113
|
|
114
|
}
|