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);
|
47
|
this.tableName = tableName;
|
48
|
this.includeAudTable = includeAudTable;
|
49
|
this.ifExists = ifExists;
|
50
|
}
|
51
|
|
52
|
@Override
|
53
|
protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) {
|
54
|
boolean result = true;
|
55
|
try {
|
56
|
String updateQuery = getUpdateQueryString(tableName, datasource, monitor);
|
57
|
datasource.executeUpdate(updateQuery);
|
58
|
if (! this.isAuditing){
|
59
|
removeFromHibernateSequences(datasource, monitor, tableName);
|
60
|
}
|
61
|
return result;
|
62
|
} catch ( Exception e) {
|
63
|
monitor.warning(e.getMessage(), e);
|
64
|
logger.error(e);
|
65
|
return false;
|
66
|
}
|
67
|
}
|
68
|
|
69
|
private boolean removeFromHibernateSequences(ICdmDataSource datasource, IProgressMonitor monitor, String tableName) {
|
70
|
try {
|
71
|
String sql = " DELETE FROM hibernate_sequences WHERE sequence_name = '%s'";
|
72
|
sql = String.format(sql, tableName);
|
73
|
datasource.executeUpdate(sql);
|
74
|
return true;
|
75
|
} catch (Exception e) {
|
76
|
String message = "Exception occurred when trying to read or update hibernate_sequences table for value " + this.tableName + ": " + e.getMessage();
|
77
|
monitor.warning(message, e);
|
78
|
logger.error(message);
|
79
|
return false;
|
80
|
}
|
81
|
|
82
|
}
|
83
|
|
84
|
public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
|
85
|
String updateQuery;
|
86
|
DatabaseTypeEnum type = datasource.getDatabaseType();
|
87
|
|
88
|
updateQuery = "DROP TABLE @ifExists @tableName ";
|
89
|
if (type.equals(DatabaseTypeEnum.SqlServer2005)){
|
90
|
//MySQL allows both syntaxes
|
91
|
updateQuery = " if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME='@tableName') BEGIN drop table @tableName end ";
|
92
|
}else if (type.equals(DatabaseTypeEnum.H2) || type.equals(DatabaseTypeEnum.PostgreSQL) || type.equals(DatabaseTypeEnum.MySQL)){
|
93
|
// updateQuery = "ALTER TABLE @tableName @addSeparator @columnName @columnType";
|
94
|
}else{
|
95
|
updateQuery = null;
|
96
|
String warning = "Update step '" + this.getStepName() + "' is not supported by " + type.getName();
|
97
|
monitor.warning(warning);
|
98
|
throw new DatabaseTypeNotSupportedException(warning);
|
99
|
}
|
100
|
updateQuery = updateQuery.replace("@tableName", tableName);
|
101
|
if (ifExists == true){
|
102
|
updateQuery = updateQuery.replace("@ifExists", "IF EXISTS");
|
103
|
}else{
|
104
|
updateQuery = updateQuery.replace("@ifExists", "");
|
105
|
}
|
106
|
|
107
|
return updateQuery;
|
108
|
}
|
109
|
|
110
|
|
111
|
}
|