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 java.sql.SQLException;
|
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
|
* @date 16.09.2010
|
23
|
*
|
24
|
*/
|
25
|
public class ColumnRemover extends SchemaUpdaterStepBase<ColumnRemover> implements ISchemaUpdaterStep {
|
26
|
@SuppressWarnings("unused")
|
27
|
private static final Logger logger = Logger.getLogger(ColumnRemover.class);
|
28
|
|
29
|
private String tableName;
|
30
|
private String oldColumnName;
|
31
|
private boolean includeAudTable;
|
32
|
|
33
|
public static final ColumnRemover NewInstance(String stepName, String tableName, String oldColumnName, boolean includeAudTable){
|
34
|
return new ColumnRemover(stepName, tableName, oldColumnName, includeAudTable);
|
35
|
}
|
36
|
|
37
|
|
38
|
protected ColumnRemover(String stepName, String tableName, String oldColumnName, boolean includeAudTable) {
|
39
|
super(stepName);
|
40
|
this.tableName = tableName;
|
41
|
this.oldColumnName = oldColumnName;
|
42
|
this.includeAudTable = includeAudTable;
|
43
|
}
|
44
|
|
45
|
/* (non-Javadoc)
|
46
|
* @see eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase#invoke(eu.etaxonomy.cdm.database.ICdmDataSource, eu.etaxonomy.cdm.common.IProgressMonitor)
|
47
|
*/
|
48
|
@Override
|
49
|
public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {
|
50
|
boolean result = true;
|
51
|
result &= removeColumn(tableName, datasource, monitor);
|
52
|
if (includeAudTable){
|
53
|
String aud = "_AUD";
|
54
|
result &= removeColumn(tableName + aud, datasource, monitor);
|
55
|
}
|
56
|
return (result == true )? 0 : null;
|
57
|
}
|
58
|
|
59
|
private boolean removeColumn(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) {
|
60
|
boolean result = true;
|
61
|
try {
|
62
|
String updateQuery = getUpdateQueryString(tableName, datasource, monitor);
|
63
|
try {
|
64
|
datasource.executeUpdate(updateQuery);
|
65
|
} catch (SQLException e) {
|
66
|
logger.error(e);
|
67
|
result = false;
|
68
|
}
|
69
|
return result;
|
70
|
} catch ( DatabaseTypeNotSupportedException e) {
|
71
|
return false;
|
72
|
}
|
73
|
}
|
74
|
|
75
|
public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
|
76
|
String updateQuery;
|
77
|
DatabaseTypeEnum type = datasource.getDatabaseType();
|
78
|
|
79
|
updateQuery = "ALTER TABLE @tableName DROP COLUMN @columnName";
|
80
|
if (type.equals(DatabaseTypeEnum.SqlServer2005)){
|
81
|
//MySQL allows both syntaxes
|
82
|
// updateQuery = "ALTER TABLE @tableName ADD @columnName @columnType";
|
83
|
}else if (type.equals(DatabaseTypeEnum.H2) || type.equals(DatabaseTypeEnum.PostgreSQL) || type.equals(DatabaseTypeEnum.MySQL)){
|
84
|
// updateQuery = "ALTER TABLE @tableName @addSeparator @columnName @columnType";
|
85
|
}else{
|
86
|
updateQuery = null;
|
87
|
String warning = "Update step '" + this.getStepName() + "' is not supported by " + type.getName();
|
88
|
monitor.warning(warning);
|
89
|
throw new DatabaseTypeNotSupportedException(warning);
|
90
|
}
|
91
|
updateQuery = updateQuery.replace("@tableName", tableName);
|
92
|
updateQuery = updateQuery.replace("@columnName", oldColumnName);
|
93
|
|
94
|
return updateQuery;
|
95
|
}
|
96
|
|
97
|
// public static String getDropColumnSeperator(ICdmDataSource datasource) throws DatabaseTypeNotSupportedException {
|
98
|
// DatabaseTypeEnum type = datasource.getDatabaseType();
|
99
|
// if (type.equals(DatabaseTypeEnum.SqlServer2005)){
|
100
|
// return "DROP ";
|
101
|
// }else if (type.equals(DatabaseTypeEnum.H2) || type.equals(DatabaseTypeEnum.PostgreSQL) || type.equals(DatabaseTypeEnum.MySQL)){
|
102
|
// return "DROP COLUMN ";
|
103
|
// }else{
|
104
|
// throw new DatabaseTypeNotSupportedException(datasource.getName());
|
105
|
// }
|
106
|
// }
|
107
|
|
108
|
}
|