minor
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / update / TableDroper.java
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 java.sql.SQLException;
13
14 import org.apache.log4j.Logger;
15
16 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
17 import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
18 import eu.etaxonomy.cdm.database.ICdmDataSource;
19
20 /**
21 * @author a.mueller
22 * @date 16.09.2010
23 *
24 */
25 public class TableDroper extends SchemaUpdaterStepBase<TableDroper> implements ISchemaUpdaterStep {
26 @SuppressWarnings("unused")
27 private static final Logger logger = Logger.getLogger(TableDroper.class);
28
29 private String tableName;
30 private boolean includeAudTable;
31
32 public static final TableDroper NewInstance(String stepName, String tableName, boolean includeAudTable){
33 return new TableDroper(stepName, tableName, includeAudTable);
34 }
35
36
37 protected TableDroper(String stepName, String tableName, boolean includeAudTable) {
38 super(stepName);
39 this.tableName = tableName;
40 this.includeAudTable = includeAudTable;
41 }
42
43 /* (non-Javadoc)
44 * @see eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase#invoke(eu.etaxonomy.cdm.database.ICdmDataSource, eu.etaxonomy.cdm.common.IProgressMonitor)
45 */
46 @Override
47 public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException {
48 boolean result = true;
49 result &= removeTable(tableName, datasource, monitor);
50 if (includeAudTable){
51 String aud = "_AUD";
52 result &= removeTable(tableName + aud, datasource, monitor);
53 }
54 return (result == true )? 0 : null;
55 }
56
57 private boolean removeTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) {
58 boolean result = true;
59 try {
60 String updateQuery = getUpdateQueryString(tableName, datasource, monitor);
61 try {
62 datasource.executeUpdate(updateQuery);
63 } catch (SQLException e) {
64 logger.error(e);
65 result = false;
66 }
67 return result;
68 } catch ( DatabaseTypeNotSupportedException e) {
69 return false;
70 }
71 }
72
73 public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
74 String updateQuery;
75 DatabaseTypeEnum type = datasource.getDatabaseType();
76
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";
83 }else{
84 updateQuery = null;
85 String warning = "Update step '" + this.getStepName() + "' is not supported by " + type.getName();
86 monitor.warning(warning);
87 throw new DatabaseTypeNotSupportedException(warning);
88 }
89 updateQuery = updateQuery.replace("@tableName", tableName);
90
91 return updateQuery;
92 }
93
94
95 }