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