Project

General

Profile

Download (3.73 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 ColumnNameChanger extends AuditedSchemaUpdaterStepBase<ColumnNameChanger> implements ISchemaUpdaterStep {
23
	private static final Logger logger = Logger.getLogger(ColumnNameChanger.class);
24

    
25
	private final String newColumnName;
26
	private final String oldColumnName;
27
	private final Datatype datatype; //TODO make enum
28

    
29
	private enum Datatype{
30
		integer,
31
		clob
32
	}
33

    
34
	public static ColumnNameChanger NewIntegerInstance(String stepName, String tableName, String oldColumnName, String newColumnName, boolean includeAudTable){
35
		return new ColumnNameChanger(stepName, tableName, oldColumnName, newColumnName, includeAudTable, null, Datatype.integer);
36
	}
37

    
38
	public static ColumnNameChanger NewClobInstance(String stepName, String tableName, String oldColumnName, String newColumnName, boolean includeAudTable){
39
		return new ColumnNameChanger(stepName, tableName, oldColumnName, newColumnName, includeAudTable, null, Datatype.clob);
40
	}
41

    
42
	protected ColumnNameChanger(String stepName, String tableName, String oldColumnName, String newColumnName, boolean includeAudTable, Object defaultValue, Datatype datatype) {
43
		super(stepName, tableName, includeAudTable);
44
		this.newColumnName = newColumnName;
45
		this.oldColumnName = oldColumnName;
46
		this.datatype = datatype;
47
	}
48

    
49
	@Override
50
	protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) {
51
		try {
52
			boolean result = true;
53
			DatabaseTypeEnum type = datasource.getDatabaseType();
54
			String updateQuery;
55

    
56
			if (type.equals(DatabaseTypeEnum.SqlServer2005)){
57
				logger.warn("SQLServer column name changer syntax not yet tested");
58
				updateQuery = "EXEC sp_rename '@oldName', '@newName'";
59
			}else if (type.equals(DatabaseTypeEnum.H2)){
60
				updateQuery = "ALTER TABLE @tableName ALTER COLUMN @oldColumnName RENAME TO @newColumnName";
61
			}else if ( type.equals(DatabaseTypeEnum.MySQL)){
62
				//FIXME MySQL column name changer
63
//			logger.warn("Changing column name not yet supported for MySQL");
64
				updateQuery = "ALTER TABLE @tableName CHANGE COLUMN @oldColumnName @newColumnName @definition";
65
			}else if ( type.equals(DatabaseTypeEnum.PostgreSQL) ){
66
				updateQuery = "ALTER TABLE @tableName RENAME COLUMN @oldColumnName TO @newColumnName;";
67
			}else{
68
				updateQuery = null;
69
				monitor.warning("Update step '" + this.getStepName() + "' is not supported by " + type.getName());
70
				return false;
71
			}
72
			updateQuery = updateQuery.replace("@tableName", tableName);
73
			updateQuery = updateQuery.replace("@oldColumnName", oldColumnName);
74
			updateQuery = updateQuery.replace("@newColumnName", newColumnName);
75
			updateQuery = updateQuery.replace("@definition", getDefinition());
76
			datasource.executeUpdate(updateQuery);
77

    
78
			return result;
79
		} catch (Exception e) {
80
			monitor.warning(e.getMessage(), e);
81
			logger.error(e);
82
			return false;
83
		}
84
	}
85

    
86

    
87
	//TODO use same code as ColumnTypeChanger or ColumnAdder
88
	private CharSequence getDefinition() {
89
		if (this.datatype == Datatype.integer){
90
			return "integer";
91
		}else if (this.datatype == Datatype.clob){
92
			return "longtext";
93
		}else{
94
			throw new RuntimeException("Definition type not supported");
95
		}
96
	}
97

    
98
}
(7-7/36)