Project

General

Profile

Download (3.18 KB) Statistics
| Branch: | Tag: | Revision:
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 ColumnRemover extends AuditedSchemaUpdaterStepBase<ColumnRemover> implements ISchemaUpdaterStep {
24
	private static final Logger logger = Logger.getLogger(ColumnRemover.class);
25

    
26
	private final String oldColumnName;
27

    
28
	public static final ColumnRemover NewInstance(String stepName, String tableName, String oldColumnName, boolean includeAudTable){
29
		return new ColumnRemover(stepName, tableName, oldColumnName, includeAudTable);
30
	}
31

    
32

    
33
	protected ColumnRemover(String stepName, String tableName, String oldColumnName, boolean includeAudTable) {
34
		super(stepName, tableName, includeAudTable);
35
		this.oldColumnName = oldColumnName;
36
	}
37

    
38
	@Override
39
	protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) {
40
		boolean result = true;
41
		try {
42
			String updateQuery = getUpdateQueryString(tableName, datasource, monitor);
43
			datasource.executeUpdate(updateQuery);
44
			return result;
45
		} catch ( Exception e) {
46
			monitor.warning(e.getMessage(), e);
47
			logger.error(e);
48
			return false;
49
		}
50
	}
51

    
52
	public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
53
		String updateQuery;
54
		DatabaseTypeEnum type = datasource.getDatabaseType();
55

    
56
		updateQuery = "ALTER TABLE @tableName DROP COLUMN @columnName";
57
		if (type.equals(DatabaseTypeEnum.SqlServer2005)){
58
			//MySQL allows both syntaxes
59
//			updateQuery = "ALTER TABLE @tableName ADD @columnName @columnType";
60
		}else if (type.equals(DatabaseTypeEnum.H2) || type.equals(DatabaseTypeEnum.PostgreSQL) || type.equals(DatabaseTypeEnum.MySQL)){
61
//			updateQuery = "ALTER TABLE @tableName @addSeparator @columnName @columnType";
62
		}else{
63
			updateQuery = null;
64
			String warning = "Update step '" + this.getStepName() + "' is not supported by " + type.getName();
65
			monitor.warning(warning);
66
			throw new DatabaseTypeNotSupportedException(warning);
67
		}
68
		updateQuery = updateQuery.replace("@tableName", tableName);
69
		updateQuery = updateQuery.replace("@columnName", oldColumnName);
70

    
71
		return updateQuery;
72
	}
73

    
74
//	public static String getDropColumnSeperator(ICdmDataSource datasource) throws DatabaseTypeNotSupportedException {
75
//		DatabaseTypeEnum type = datasource.getDatabaseType();
76
//		if (type.equals(DatabaseTypeEnum.SqlServer2005)){
77
//			return "DROP ";
78
//		}else if (type.equals(DatabaseTypeEnum.H2) || type.equals(DatabaseTypeEnum.PostgreSQL) || type.equals(DatabaseTypeEnum.MySQL)){
79
//			return "DROP COLUMN ";
80
//		}else{
81
//			throw new DatabaseTypeNotSupportedException(datasource.getName());
82
//		}
83
//	}
84

    
85
}
(8-8/34)