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
|
|
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 TableNameChanger
|
25
|
extends SchemaUpdaterStepBase{
|
26
|
|
27
|
private static final Logger logger = Logger.getLogger(TableNameChanger.class);
|
28
|
|
29
|
private String oldName;
|
30
|
private String newName;
|
31
|
private boolean includeAudTable;
|
32
|
|
33
|
public static final TableNameChanger NewInstance(String stepName, String oldName, String newName, boolean includeAudTable){
|
34
|
return new TableNameChanger(stepName, oldName, newName, includeAudTable);
|
35
|
}
|
36
|
|
37
|
protected TableNameChanger(String stepName, String oldName, String newName, boolean includeAudTable) {
|
38
|
super(stepName);
|
39
|
this.oldName = oldName;
|
40
|
this.newName = newName;
|
41
|
this.includeAudTable = includeAudTable;
|
42
|
}
|
43
|
|
44
|
@Override
|
45
|
public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
|
46
|
CaseType caseType, SchemaUpdateResult result) throws SQLException {
|
47
|
invokeOnTable(caseType.transformTo(oldName), caseType.transformTo(newName),
|
48
|
datasource, monitor, result);
|
49
|
updateHibernateSequence(datasource, monitor, newName, oldName); //no result&= as hibernateSequence problems may not lead to a complete fail
|
50
|
if (includeAudTable){
|
51
|
String aud = "_AUD";
|
52
|
invokeOnTable(caseType.transformTo(oldName + aud), caseType.transformTo(newName + aud),
|
53
|
datasource, monitor, result);
|
54
|
}
|
55
|
return;
|
56
|
}
|
57
|
|
58
|
//does not support AuditedSchemaUpdaterStepBase signature
|
59
|
private void invokeOnTable(String oldName, String newName, ICdmDataSource datasource,
|
60
|
IProgressMonitor monitor, SchemaUpdateResult result) {
|
61
|
DatabaseTypeEnum type = datasource.getDatabaseType();
|
62
|
String updateQuery;
|
63
|
if (type.equals(DatabaseTypeEnum.MySQL)){
|
64
|
//MySQL allows both syntaxes
|
65
|
updateQuery = "RENAME TABLE @oldName TO @newName";
|
66
|
}else if (type.equals(DatabaseTypeEnum.H2) || type.equals(DatabaseTypeEnum.PostgreSQL) || type.equals(DatabaseTypeEnum.MySQL)){
|
67
|
updateQuery = "ALTER TABLE @oldName RENAME TO @newName";
|
68
|
}else if (type.equals(DatabaseTypeEnum.SqlServer2005)){
|
69
|
updateQuery = "EXEC sp_rename '@oldName', '@newName'";
|
70
|
}else{
|
71
|
updateQuery = null;
|
72
|
String message ="Update step '" + this.getStepName() + "' is not supported by " + type.getName();
|
73
|
monitor.warning(message);
|
74
|
result.addError(message, getStepName() + ", TableNameChanger.invokeOnTable");
|
75
|
return;
|
76
|
}
|
77
|
updateQuery = updateQuery.replace("@oldName", oldName);
|
78
|
updateQuery = updateQuery.replace("@newName", newName);
|
79
|
try {
|
80
|
datasource.executeUpdate(updateQuery);
|
81
|
} catch (SQLException e) {
|
82
|
String message = "Could not perform rename table operation";
|
83
|
monitor.warning("Could not perform rename table operation", e);
|
84
|
logger.warn(message+ ": " + e.getMessage());
|
85
|
result.addException(e, message, getStepName() + ", TableNameChanger.invokeOnTable");
|
86
|
return;
|
87
|
}
|
88
|
return;
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
*
|
93
|
* @param datasource
|
94
|
* @param monitor
|
95
|
* @param table
|
96
|
* @param oldVal
|
97
|
* @return
|
98
|
*/
|
99
|
private boolean updateHibernateSequence(ICdmDataSource datasource, IProgressMonitor monitor, String newName, String oldName){
|
100
|
try{
|
101
|
//TODO do we need to "case" this table name?
|
102
|
String sql = " UPDATE hibernate_sequences SET sequence_name = '%s' WHERE sequence_name = '%s'";
|
103
|
datasource.executeUpdate(String.format(sql, newName ,oldName));
|
104
|
return true;
|
105
|
} catch (Exception e) {
|
106
|
String message = "Exception occurred when trying to read or update hibernate_sequences table for value " + this.newName + ": " + e.getMessage();
|
107
|
monitor.warning(message, e);
|
108
|
logger.error(message);
|
109
|
return false;
|
110
|
}
|
111
|
|
112
|
}
|
113
|
|
114
|
}
|