Project

General

Profile

Download (3.28 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 ColumnRemover
23
        extends AuditedSchemaUpdaterStepBase{
24

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

    
27
	private final String oldColumnName;
28

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

    
33

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

    
39
    @Override
40
    protected void invokeOnTable(String tableName, ICdmDataSource datasource,
41
            IProgressMonitor monitor, CaseType caseType, SchemaUpdateResult result) {
42
        try {
43
			String updateQuery = getUpdateQueryString(tableName, datasource, monitor);
44
			datasource.executeUpdate(updateQuery);
45
			return;
46
		} catch ( Exception e) {
47
		    String message = e.getMessage();
48
			monitor.warning(message, e);
49
			logger.error(e);
50
            result.addException(e, message, getStepName() + ", ColumnRemove.invokeOnTable");
51
            return;
52
		}
53
	}
54

    
55
	public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
56
		String updateQuery;
57
		DatabaseTypeEnum type = datasource.getDatabaseType();
58

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

    
74
		return updateQuery;
75
	}
76

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

    
88
}
(8-8/35)