Project

General

Profile

Download (3.33 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 java.util.List;
12

    
13
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
14

    
15
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
16
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
17
import eu.etaxonomy.cdm.database.ICdmDataSource;
18

    
19
/**
20
 * @author a.mueller
21
 * @since 16.09.2010
22
 *
23
 */
24
public class ColumnRemover
25
        extends AuditedSchemaUpdaterStepBase{
26

    
27
    private static final Logger logger = LogManager.getLogger(ColumnRemover.class);
28

    
29
	private final String oldColumnName;
30

    
31
	public static final ColumnRemover NewInstance(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String oldColumnName, boolean includeAudTable){
32
		return new ColumnRemover(stepList, stepName, tableName, oldColumnName, includeAudTable);
33
	}
34

    
35

    
36
	protected ColumnRemover(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String oldColumnName, boolean includeAudTable) {
37
		super(stepList, stepName, tableName, includeAudTable);
38
		this.oldColumnName = oldColumnName;
39
	}
40

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

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

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

    
76
		return updateQuery;
77
	}
78

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

    
90
}
(9-9/41)