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 java
.sql
.SQLException
;
14 import org
.apache
.log4j
.Logger
;
16 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
17 import eu
.etaxonomy
.cdm
.database
.DatabaseTypeEnum
;
18 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
25 public class TableDroper
extends SchemaUpdaterStepBase
<TableDroper
> implements ISchemaUpdaterStep
{
26 @SuppressWarnings("unused")
27 private static final Logger logger
= Logger
.getLogger(TableDroper
.class);
29 private String tableName
;
30 private boolean includeAudTable
;
32 public static final TableDroper
NewInstance(String stepName
, String tableName
, boolean includeAudTable
){
33 return new TableDroper(stepName
, tableName
, includeAudTable
);
37 protected TableDroper(String stepName
, String tableName
, boolean includeAudTable
) {
39 this.tableName
= tableName
;
40 this.includeAudTable
= includeAudTable
;
44 * @see eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase#invoke(eu.etaxonomy.cdm.database.ICdmDataSource, eu.etaxonomy.cdm.common.IProgressMonitor)
47 public Integer
invoke(ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
48 boolean result
= true;
49 result
&= removeTable(tableName
, datasource
, monitor
);
52 result
&= removeTable(tableName
+ aud
, datasource
, monitor
);
54 return (result
== true )?
0 : null;
57 private boolean removeTable(String tableName
, ICdmDataSource datasource
, IProgressMonitor monitor
) {
58 boolean result
= true;
60 String updateQuery
= getUpdateQueryString(tableName
, datasource
, monitor
);
62 datasource
.executeUpdate(updateQuery
);
63 } catch (SQLException e
) {
68 } catch ( DatabaseTypeNotSupportedException e
) {
73 public String
getUpdateQueryString(String tableName
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws DatabaseTypeNotSupportedException
{
75 DatabaseTypeEnum type
= datasource
.getDatabaseType();
77 updateQuery
= "DROP TABLE @tableName";
78 if (type
.equals(DatabaseTypeEnum
.SqlServer2005
)){
79 //MySQL allows both syntaxes
80 // updateQuery = "ALTER TABLE @tableName ADD @columnName @columnType";
81 }else if (type
.equals(DatabaseTypeEnum
.H2
) || type
.equals(DatabaseTypeEnum
.PostgreSQL
) || type
.equals(DatabaseTypeEnum
.MySQL
)){
82 // updateQuery = "ALTER TABLE @tableName @addSeparator @columnName @columnType";
85 String warning
= "Update step '" + this.getStepName() + "' is not supported by " + type
.getName();
86 monitor
.warning(warning
);
87 throw new DatabaseTypeNotSupportedException(warning
);
89 updateQuery
= updateQuery
.replace("@tableName", tableName
);