1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.database.update;
|
11
|
|
12
|
import org.apache.log4j.Logger;
|
13
|
|
14
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
15
|
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
|
16
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
17
|
|
18
|
/**
|
19
|
* @author a.mueller
|
20
|
* @date 16.09.2010
|
21
|
*
|
22
|
*/
|
23
|
public class ColumnNameChanger extends AuditedSchemaUpdaterStepBase<ColumnNameChanger> implements ISchemaUpdaterStep {
|
24
|
private static final Logger logger = Logger.getLogger(ColumnNameChanger.class);
|
25
|
|
26
|
private final String newColumnName;
|
27
|
private final String oldColumnName;
|
28
|
private final Datatype datatype; //TODO make enum
|
29
|
|
30
|
private enum Datatype{
|
31
|
integer,
|
32
|
clob
|
33
|
}
|
34
|
|
35
|
public static ColumnNameChanger NewIntegerInstance(String stepName, String tableName, String oldColumnName, String newColumnName, boolean includeAudTable){
|
36
|
return new ColumnNameChanger(stepName, tableName, oldColumnName, newColumnName, includeAudTable, null, Datatype.integer);
|
37
|
}
|
38
|
|
39
|
public static ColumnNameChanger NewClobInstance(String stepName, String tableName, String oldColumnName, String newColumnName, boolean includeAudTable){
|
40
|
return new ColumnNameChanger(stepName, tableName, oldColumnName, newColumnName, includeAudTable, null, Datatype.clob);
|
41
|
}
|
42
|
|
43
|
protected ColumnNameChanger(String stepName, String tableName, String oldColumnName, String newColumnName, boolean includeAudTable, Object defaultValue, Datatype datatype) {
|
44
|
super(stepName, tableName, includeAudTable);
|
45
|
this.newColumnName = newColumnName;
|
46
|
this.oldColumnName = oldColumnName;
|
47
|
this.datatype = datatype;
|
48
|
}
|
49
|
|
50
|
@Override
|
51
|
protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) {
|
52
|
try {
|
53
|
boolean result = true;
|
54
|
DatabaseTypeEnum type = datasource.getDatabaseType();
|
55
|
String updateQuery;
|
56
|
|
57
|
if (type.equals(DatabaseTypeEnum.SqlServer2005)){
|
58
|
logger.warn("SQLServer column name changer syntax not yet tested");
|
59
|
updateQuery = "EXEC sp_rename '@oldName', '@newName'";
|
60
|
}else if (type.equals(DatabaseTypeEnum.H2)){
|
61
|
updateQuery = "ALTER TABLE @tableName ALTER COLUMN @oldColumnName RENAME TO @newColumnName";
|
62
|
}else if ( type.equals(DatabaseTypeEnum.MySQL)){
|
63
|
//FIXME MySQL column name changer
|
64
|
// logger.warn("Changing column name not yet supported for MySQL");
|
65
|
updateQuery = "ALTER TABLE @tableName CHANGE COLUMN @oldColumnName @newColumnName @definition";
|
66
|
}else if ( type.equals(DatabaseTypeEnum.PostgreSQL) ){
|
67
|
updateQuery = "ALTER TABLE @tableName RENAME COLUMN @oldColumnName TO @newColumnName;";
|
68
|
}else{
|
69
|
updateQuery = null;
|
70
|
monitor.warning("Update step '" + this.getStepName() + "' is not supported by " + type.getName());
|
71
|
return false;
|
72
|
}
|
73
|
updateQuery = updateQuery.replace("@tableName", tableName);
|
74
|
updateQuery = updateQuery.replace("@oldColumnName", oldColumnName);
|
75
|
updateQuery = updateQuery.replace("@newColumnName", newColumnName);
|
76
|
updateQuery = updateQuery.replace("@definition", getDefinition());
|
77
|
datasource.executeUpdate(updateQuery);
|
78
|
|
79
|
return result;
|
80
|
} catch (Exception e) {
|
81
|
monitor.warning(e.getMessage(), e);
|
82
|
logger.error(e);
|
83
|
return false;
|
84
|
}
|
85
|
}
|
86
|
|
87
|
|
88
|
//TODO use same code as ColumnTypeChanger or ColumnAdder
|
89
|
private CharSequence getDefinition() {
|
90
|
if (this.datatype == Datatype.integer){
|
91
|
return "integer";
|
92
|
}else if (this.datatype == Datatype.clob){
|
93
|
return "longtext";
|
94
|
}else{
|
95
|
throw new RuntimeException("Definition type not supported");
|
96
|
}
|
97
|
}
|
98
|
|
99
|
}
|