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 TableDroper extends AuditedSchemaUpdaterStepBase<TableDroper> implements ISchemaUpdaterStep {
|
24
|
private static final Logger logger = Logger.getLogger(TableDroper.class);
|
25
|
|
26
|
private boolean ifExists = true;
|
27
|
|
28
|
public static final TableDroper NewInstance(String stepName, String tableName, boolean includeAudTable){
|
29
|
return new TableDroper(stepName, tableName, includeAudTable, true);
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* @param stepName
|
34
|
* @param tableName
|
35
|
* @param includeAudTable
|
36
|
* @param ifExists if false, and error will be thrown if the table does not exist and can therefore not be dropped.
|
37
|
* @see #NewInstance(String, String, boolean)
|
38
|
* @return
|
39
|
*/
|
40
|
public static final TableDroper NewInstance(String stepName, String tableName, boolean includeAudTable, boolean ifExists){
|
41
|
return new TableDroper(stepName, tableName, includeAudTable, ifExists);
|
42
|
}
|
43
|
|
44
|
|
45
|
protected TableDroper(String stepName, String tableName, boolean includeAudTable, boolean ifExists) {
|
46
|
super(stepName, tableName, includeAudTable);
|
47
|
this.ifExists = ifExists;
|
48
|
}
|
49
|
|
50
|
@Override
|
51
|
protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) {
|
52
|
boolean result = true;
|
53
|
try {
|
54
|
String updateQuery = getUpdateQueryString(tableName, datasource, monitor);
|
55
|
datasource.executeUpdate(updateQuery);
|
56
|
if (! this.isAuditing){
|
57
|
removeFromHibernateSequences(datasource, monitor, tableName);
|
58
|
}
|
59
|
return result;
|
60
|
} catch ( Exception e) {
|
61
|
monitor.warning(e.getMessage(), e);
|
62
|
logger.error(e);
|
63
|
return false;
|
64
|
}
|
65
|
}
|
66
|
|
67
|
private boolean removeFromHibernateSequences(ICdmDataSource datasource, IProgressMonitor monitor, String tableName) {
|
68
|
try {
|
69
|
//TODO do we need to "case" this table name?
|
70
|
String sql = " DELETE FROM hibernate_sequences WHERE sequence_name = '%s'";
|
71
|
sql = String.format(sql, tableName);
|
72
|
datasource.executeUpdate(sql);
|
73
|
return true;
|
74
|
} catch (Exception e) {
|
75
|
String message = "Exception occurred when trying to read or update hibernate_sequences table for value " + this.tableName + ": " + e.getMessage();
|
76
|
monitor.warning(message, e);
|
77
|
logger.error(message);
|
78
|
return false;
|
79
|
}
|
80
|
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* @param tableName cased tableName
|
85
|
* @param datasource
|
86
|
* @param monitor
|
87
|
* @param caseType
|
88
|
* @return
|
89
|
* @throws DatabaseTypeNotSupportedException
|
90
|
*/
|
91
|
public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
|
92
|
String updateQuery;
|
93
|
DatabaseTypeEnum type = datasource.getDatabaseType();
|
94
|
|
95
|
updateQuery = "DROP TABLE @ifExists @tableName ";
|
96
|
if (type.equals(DatabaseTypeEnum.SqlServer2005)){
|
97
|
//MySQL allows both syntaxes
|
98
|
updateQuery = " if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME='@tableName') BEGIN drop table @tableName end ";
|
99
|
}else if (type.equals(DatabaseTypeEnum.H2) || type.equals(DatabaseTypeEnum.PostgreSQL) || type.equals(DatabaseTypeEnum.MySQL)){
|
100
|
// updateQuery = "ALTER TABLE @tableName @addSeparator @columnName @columnType";
|
101
|
}else{
|
102
|
updateQuery = null;
|
103
|
String warning = "Update step '" + this.getStepName() + "' is not supported by " + type.getName();
|
104
|
monitor.warning(warning);
|
105
|
throw new DatabaseTypeNotSupportedException(warning);
|
106
|
}
|
107
|
updateQuery = updateQuery.replace("@tableName", tableName);
|
108
|
if (ifExists == true){
|
109
|
updateQuery = updateQuery.replace("@ifExists", "IF EXISTS");
|
110
|
}else{
|
111
|
updateQuery = updateQuery.replace("@ifExists", "");
|
112
|
}
|
113
|
|
114
|
return updateQuery;
|
115
|
}
|
116
|
|
117
|
|
118
|
}
|