Project

General

Profile

Download (4.08 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
 * @date 16.09.2010
20
 *
21
 */
22
public class TableDroper extends AuditedSchemaUpdaterStepBase<TableDroper> implements ISchemaUpdaterStep {
23
	private static final Logger logger = Logger.getLogger(TableDroper.class);
24

    
25
	private boolean ifExists = true;
26

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

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

    
43

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

    
49
	@Override
50
	protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) {
51
		boolean result = true;
52
		try {
53
			String updateQuery = getUpdateQueryString(tableName, datasource, monitor);
54
			datasource.executeUpdate(updateQuery);
55
			if (! this.isAuditing){
56
				removeFromHibernateSequences(datasource, monitor, tableName);
57
			}
58
			return result;
59
		} catch ( Exception e) {
60
			monitor.warning(e.getMessage(), e);
61
			logger.error(e);
62
			return false;
63
		}
64
	}
65

    
66
	private boolean removeFromHibernateSequences(ICdmDataSource datasource, IProgressMonitor monitor, String tableName) {
67
		try {
68
			//TODO do we need to "case" this table name?
69
			String sql = " DELETE FROM hibernate_sequences WHERE sequence_name = '%s'";
70
			sql = String.format(sql, tableName);
71
			datasource.executeUpdate(sql);
72
			return true;
73
		} catch (Exception e) {
74
			String message = "Exception occurred when trying to read or update hibernate_sequences table for value " + this.tableName + ": " + e.getMessage();
75
			monitor.warning(message, e);
76
			logger.error(message);
77
			return false;
78
		}
79

    
80
	}
81

    
82
	/**
83
	 * @param tableName cased tableName
84
	 * @param datasource
85
	 * @param monitor
86
	 * @param caseType
87
	 * @return
88
	 * @throws DatabaseTypeNotSupportedException
89
	 */
90
	public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
91
		String updateQuery;
92
		DatabaseTypeEnum type = datasource.getDatabaseType();
93

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

    
113
		return updateQuery;
114
	}
115

    
116

    
117
}
(28-28/36)