Project

General

Profile

Download (4.26 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
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 org.apache.log4j.Logger;
12

    
13
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
14
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
15
import eu.etaxonomy.cdm.database.ICdmDataSource;
16

    
17
/**
18
 * @author a.mueller
19
 * @since 16.09.2010
20
 *
21
 */
22
public class TableDroper
23
        extends AuditedSchemaUpdaterStepBase{
24

    
25
    private static final Logger logger = Logger.getLogger(TableDroper.class);
26

    
27
	private boolean ifExists = true;
28

    
29
	public static final TableDroper NewInstance(String stepName, String tableName, boolean includeAudTable){
30
		return new TableDroper(stepName, tableName, includeAudTable, true);
31
	}
32

    
33
	/**
34
	 * @param stepName
35
	 * @param tableName
36
	 * @param includeAudTable
37
	 * @param ifExists if false, and error will be thrown if the table does not exist and can therefore not be dropped.
38
	 * @see #NewInstance(String, String, boolean)
39
	 * @return
40
	 */
41
	public static final TableDroper NewInstance(String stepName, String tableName, boolean includeAudTable, boolean ifExists){
42
		return new TableDroper(stepName, tableName, includeAudTable, ifExists);
43
	}
44

    
45

    
46
	protected TableDroper(String stepName, String tableName, boolean includeAudTable, boolean ifExists) {
47
		super(stepName, tableName, includeAudTable);
48
		this.ifExists = ifExists;
49
	}
50

    
51
    @Override
52
    protected void invokeOnTable(String tableName, ICdmDataSource datasource,
53
            IProgressMonitor monitor, CaseType caseType, SchemaUpdateResult result) {
54
        try {
55
			String updateQuery = getUpdateQueryString(tableName, datasource, monitor);
56
			datasource.executeUpdate(updateQuery);
57
			if (! this.isAuditing){
58
				removeFromHibernateSequences(datasource, monitor, tableName, result);
59
			}
60
			return;
61
		} catch ( Exception e) {
62
		    String message = e.getMessage();
63
			monitor.warning(message, e);
64
			logger.error(e);
65
			result.addException(e, message, getStepName() + ", TableDropper.invokeOnTable");
66
			return;
67
		}
68
	}
69

    
70
	private void removeFromHibernateSequences(ICdmDataSource datasource, IProgressMonitor monitor,
71
	        String tableName, SchemaUpdateResult result) {
72
		try {
73
			//TODO do we need to "case" this table name?
74
			String sql = " DELETE FROM hibernate_sequences WHERE sequence_name = '%s'";
75
			sql = String.format(sql, tableName);
76
			datasource.executeUpdate(sql);
77
			return;
78
		} catch (Exception e) {
79
			String message = "Exception occurred when trying to read or update hibernate_sequences table for value " + this.tableName + ": " + e.getMessage();
80
			monitor.warning(message, e);
81
			logger.error(message);
82
			result.addWarning(message, (String)null, getStepName());
83
			return;
84
		}
85
	}
86

    
87
	/**
88
	 * @param tableName cased tableName
89
	 * @param datasource
90
	 * @param monitor
91
	 * @param caseType
92
	 * @return
93
	 * @throws DatabaseTypeNotSupportedException
94
	 */
95
	public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
96
		String updateQuery;
97
		DatabaseTypeEnum type = datasource.getDatabaseType();
98

    
99
		updateQuery = "DROP TABLE @ifExists @tableName ";
100
		if (type.equals(DatabaseTypeEnum.SqlServer2005)){
101
			//MySQL allows both syntaxes
102
			updateQuery = " if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME='@tableName') BEGIN drop table @tableName end ";
103
		}else if (type.equals(DatabaseTypeEnum.H2) || type.equals(DatabaseTypeEnum.PostgreSQL) || type.equals(DatabaseTypeEnum.MySQL)){
104
//			updateQuery = "ALTER TABLE @tableName @addSeparator @columnName @columnType";
105
		}else{
106
			updateQuery = null;
107
			String warning = "Update step '" + this.getStepName() + "' is not supported by " + type.getName();
108
			monitor.warning(warning);
109
			throw new DatabaseTypeNotSupportedException(warning);
110
		}
111
		updateQuery = updateQuery.replace("@tableName", tableName);
112
		if (ifExists == true){
113
			updateQuery = updateQuery.replace("@ifExists", "IF EXISTS");
114
		}else{
115
			updateQuery = updateQuery.replace("@ifExists", "");
116
		}
117

    
118
		return updateQuery;
119
	}
120

    
121

    
122
}
(28-28/35)