separate Amplification and AmplificationResult.java #4541
[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 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, CaseType caseType) {
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 //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);
75 return true;
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);
80 return false;
81 }
82
83 }
84
85 /**
86 * @param tableName cased tableName
87 * @param datasource
88 * @param monitor
89 * @param caseType
90 * @return
91 * @throws DatabaseTypeNotSupportedException
92 */
93 public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
94 String updateQuery;
95 DatabaseTypeEnum type = datasource.getDatabaseType();
96
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";
103 }else{
104 updateQuery = null;
105 String warning = "Update step '" + this.getStepName() + "' is not supported by " + type.getName();
106 monitor.warning(warning);
107 throw new DatabaseTypeNotSupportedException(warning);
108 }
109 updateQuery = updateQuery.replace("@tableName", tableName);
110 if (ifExists == true){
111 updateQuery = updateQuery.replace("@ifExists", "IF EXISTS");
112 }else{
113 updateQuery = updateQuery.replace("@ifExists", "");
114 }
115
116 return updateQuery;
117 }
118
119
120 }