3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.database
.update
;
12 import org
.apache
.log4j
.Logger
;
14 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
15 import eu
.etaxonomy
.cdm
.database
.DatabaseTypeEnum
;
16 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
23 public class TableDroper
extends AuditedSchemaUpdaterStepBase
<TableDroper
> implements ISchemaUpdaterStep
{
24 private static final Logger logger
= Logger
.getLogger(TableDroper
.class);
26 private boolean ifExists
= true;
28 public static final TableDroper
NewInstance(String stepName
, String tableName
, boolean includeAudTable
){
29 return new TableDroper(stepName
, tableName
, includeAudTable
, true);
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)
40 public static final TableDroper
NewInstance(String stepName
, String tableName
, boolean includeAudTable
, boolean ifExists
){
41 return new TableDroper(stepName
, tableName
, includeAudTable
, ifExists
);
45 protected TableDroper(String stepName
, String tableName
, boolean includeAudTable
, boolean ifExists
) {
47 this.tableName
= tableName
;
48 this.includeAudTable
= includeAudTable
;
49 this.ifExists
= ifExists
;
53 protected boolean invokeOnTable(String tableName
, ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
) {
54 boolean result
= true;
56 String updateQuery
= getUpdateQueryString(tableName
, datasource
, monitor
);
57 datasource
.executeUpdate(updateQuery
);
58 if (! this.isAuditing
){
59 removeFromHibernateSequences(datasource
, monitor
, tableName
);
62 } catch ( Exception e
) {
63 monitor
.warning(e
.getMessage(), e
);
69 private boolean removeFromHibernateSequences(ICdmDataSource datasource
, IProgressMonitor monitor
, String tableName
) {
71 //TODO do we need to "case" this table name?
72 String sql
= " DELETE FROM hibernate_sequences WHERE sequence_name = '%s'";
73 sql
= String
.format(sql
, tableName
);
74 datasource
.executeUpdate(sql
);
76 } catch (Exception e
) {
77 String message
= "Exception occurred when trying to read or update hibernate_sequences table for value " + this.tableName
+ ": " + e
.getMessage();
78 monitor
.warning(message
, e
);
79 logger
.error(message
);
86 * @param tableName cased tableName
91 * @throws DatabaseTypeNotSupportedException
93 public String
getUpdateQueryString(String tableName
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws DatabaseTypeNotSupportedException
{
95 DatabaseTypeEnum type
= datasource
.getDatabaseType();
97 updateQuery
= "DROP TABLE @ifExists @tableName ";
98 if (type
.equals(DatabaseTypeEnum
.SqlServer2005
)){
99 //MySQL allows both syntaxes
100 updateQuery
= " if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME='@tableName') BEGIN drop table @tableName end ";
101 }else if (type
.equals(DatabaseTypeEnum
.H2
) || type
.equals(DatabaseTypeEnum
.PostgreSQL
) || type
.equals(DatabaseTypeEnum
.MySQL
)){
102 // updateQuery = "ALTER TABLE @tableName @addSeparator @columnName @columnType";
105 String warning
= "Update step '" + this.getStepName() + "' is not supported by " + type
.getName();
106 monitor
.warning(warning
);
107 throw new DatabaseTypeNotSupportedException(warning
);
109 updateQuery
= updateQuery
.replace("@tableName", tableName
);
110 if (ifExists
== true){
111 updateQuery
= updateQuery
.replace("@ifExists", "IF EXISTS");
113 updateQuery
= updateQuery
.replace("@ifExists", "");