Project

General

Profile

Download (4.94 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 ColumnNameChanger
23
        extends AuditedSchemaUpdaterStepBase{
24

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

    
27
	private String newColumnName;
28
	private String oldColumnName;
29
	private Datatype datatype;
30
	private Integer size;  //only required for MySQL
31

    
32
	private enum Datatype{
33
		integer,
34
		clob,
35
		varchar,
36
		date
37
	}
38

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

    
43
	public static ColumnNameChanger NewClobInstance(String stepName, String tableName, String oldColumnName,
44
	        String newColumnName, boolean includeAudTable){
45
		return new ColumnNameChanger(stepName, tableName, oldColumnName, newColumnName, includeAudTable, null, Datatype.clob, null);
46
	}
47

    
48
    public static ColumnNameChanger NewVarCharInstance(String stepName, String tableName, String oldColumnName,
49
            String newColumnName, int size, boolean includeAudTable){
50
        return new ColumnNameChanger(stepName, tableName, oldColumnName, newColumnName, includeAudTable, null, Datatype.varchar, size);
51
    }
52

    
53

    
54
    public static ColumnNameChanger NewDateTimeInstance(String stepName, String tableName, String oldColumnName,
55
            String newColumnName, boolean includeAudTable){
56
        return new ColumnNameChanger(stepName, tableName, oldColumnName, newColumnName, includeAudTable, null, Datatype.date, null);
57
    }
58

    
59
// **************************************** Constructor ***************************************/
60

    
61
	protected ColumnNameChanger(String stepName, String tableName, String oldColumnName,
62
	        String newColumnName, boolean includeAudTable, Object defaultValue, Datatype datatype, Integer size) {
63
		super(stepName, tableName, includeAudTable);
64
		this.newColumnName = newColumnName;
65
		this.oldColumnName = oldColumnName;
66
		this.datatype = datatype;
67
		this.size = size;
68
	}
69

    
70
    @Override
71
    protected void invokeOnTable(String tableName, ICdmDataSource datasource,
72
            IProgressMonitor monitor, CaseType caseType, SchemaUpdateResult result) {
73
        try {
74
			DatabaseTypeEnum type = datasource.getDatabaseType();
75
			String updateQuery;
76

    
77
			if (type.equals(DatabaseTypeEnum.SqlServer2005)){
78
				logger.warn("SQLServer column name changer syntax not yet tested");
79
				updateQuery = "EXEC sp_rename '@oldName', '@newName'";
80
			}else if (type.equals(DatabaseTypeEnum.H2)){
81
				updateQuery = "ALTER TABLE @tableName ALTER COLUMN @oldColumnName RENAME TO @newColumnName";
82
			}else if ( type.equals(DatabaseTypeEnum.MySQL)){
83
				//FIXME MySQL column name changer
84
//			logger.warn("Changing column name not yet supported for MySQL");
85
				updateQuery = "ALTER TABLE @tableName CHANGE COLUMN @oldColumnName @newColumnName @definition";
86
			}else if ( type.equals(DatabaseTypeEnum.PostgreSQL) ){
87
				updateQuery = "ALTER TABLE @tableName RENAME COLUMN @oldColumnName TO @newColumnName;";
88
			}else{
89
				updateQuery = null;
90
				String message = "Update step '" + this.getStepName() + "' is not supported by " + type.getName();
91
				monitor.warning(message);
92
				result.addError(message, getStepName() + ", ColumnNameChanger.invokeOnTable");
93
				return;
94
			}
95
			updateQuery = updateQuery.replace("@tableName", tableName);
96
			updateQuery = updateQuery.replace("@oldColumnName", oldColumnName);
97
			updateQuery = updateQuery.replace("@newColumnName", newColumnName);
98
			updateQuery = updateQuery.replace("@definition", getDefinition());
99
			datasource.executeUpdate(updateQuery);
100

    
101
			return;
102
		} catch (Exception e) {
103
		    String message = e.getMessage();
104
			monitor.warning(message, e);
105
			logger.error(e);
106
			result.addException(e, message, getStepName() + ", ColumnNameChanger.invokeOnTable");
107
			return;
108
		}
109
	}
110

    
111

    
112
	//TODO use same code as ColumnTypeChanger or ColumnAdder
113
	private CharSequence getDefinition() {
114
		if (this.datatype == Datatype.integer){
115
			return "integer";
116
		}else if (this.datatype == Datatype.clob){
117
			return "longtext";
118
		}else if (this.datatype == Datatype.varchar){
119
            return "nvarchar("+size+")";
120
		}else if (this.datatype == Datatype.date){
121
            return "datetime";
122
        }else{
123
			throw new RuntimeException("Definition type not supported");
124
		}
125
	}
126

    
127
}
(7-7/35)