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