Project

General

Profile

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

    
26
	private final String columnName;
27
	private final String newColumnType;
28
	private final Object defaultValue;
29
	private final boolean isNotNull;
30
	private final String referencedTable;
31

    
32

    
33
	public static final ColumnTypeChanger NewStringSizeInstance(String stepName, String tableName, String columnName, int newSize, boolean includeAudTable){
34
		return new ColumnTypeChanger(stepName, tableName, columnName, "nvarchar("+newSize+")", includeAudTable, null, false, null);
35
	}
36

    
37
	public static final ColumnTypeChanger NewClobInstance(String stepName, String tableName, String columnName, boolean includeAudTable){
38
		return new ColumnTypeChanger(stepName, tableName, columnName, "clob", includeAudTable, null, false, null);
39
	}
40

    
41
	public static final ColumnTypeChanger NewInt2DoubleInstance(String stepName, String tableName, String columnName, boolean includeAudTable){
42
		return new ColumnTypeChanger(stepName, tableName, columnName, "double", includeAudTable, null, false, null);
43
	}
44

    
45
	public static final ColumnTypeChanger NewInt2StringInstance(String stepName, String tableName, String columnName, int size, boolean includeAudTable, Integer defaultValue, boolean notNull){
46
		return new ColumnTypeChanger(stepName, tableName, columnName, "nvarchar("+size+")", includeAudTable, defaultValue, notNull, null);
47
	}
48

    
49
//	public static final ColumnTypeChanger NewChangeAllowNullOnStringChanger(){
50
//
51
//	}
52

    
53

    
54
	protected ColumnTypeChanger(String stepName, String tableName, String columnName, String newColumnType, boolean includeAudTable, Object defaultValue, boolean notNull, String referencedTable) {
55
		super(stepName, tableName, includeAudTable);
56
		this.columnName = columnName;
57
		this.newColumnType = newColumnType;
58
		this.defaultValue = defaultValue;
59
		this.isNotNull = notNull;
60
		this.referencedTable = referencedTable;
61
	}
62

    
63
	@Override
64
	protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) {
65
		boolean result = true;
66
		try {
67

    
68
			String updateQuery;
69
			if (this.isNotNull){
70
				updateQuery = getNotNullUpdateQuery(tableName, datasource, monitor);
71
				datasource.executeUpdate(updateQuery);
72
			}
73

    
74
			updateQuery = getUpdateQueryString(tableName, datasource, monitor);
75
			datasource.executeUpdate(updateQuery);
76

    
77
			if (defaultValue instanceof Boolean){
78
				updateQuery = "UPDATE @tableName SET @columnName = " + (defaultValue == null ? "null" : getBoolean((Boolean) defaultValue, datasource));
79
				updateQuery = updateQuery.replace("@tableName", tableName);
80
				updateQuery = updateQuery.replace("@columnName", columnName);
81
				datasource.executeUpdate(updateQuery);
82
			}
83
			if (referencedTable != null){
84
				result &= TableCreator.makeForeignKey(tableName, datasource, monitor, columnName, referencedTable, caseType);
85
			}
86

    
87
			return result;
88
		} catch ( Exception e) {
89
			monitor.warning(e.getMessage(), e);
90
			logger.error(e);
91
			return false;
92
		}
93
	}
94

    
95
	private String getNotNullUpdateQuery(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) {
96
		String query = " UPDATE %s SET %s = %S WHERE %s IS NULL ";
97
		String defaultValueStr = String.valueOf(this.defaultValue);
98
		if (this.defaultValue instanceof Integer){
99
			//OK
100
		}else{
101
			defaultValueStr = "'" + defaultValueStr + "'";
102
		}
103
		query = String.format(query, tableName, this.columnName, defaultValueStr, this.columnName);
104
		return query;
105
	}
106

    
107
	public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
108
		String updateQuery;
109
		DatabaseTypeEnum type = datasource.getDatabaseType();
110
		String databaseColumnType = getDatabaseColumnType(datasource, this.newColumnType);
111

    
112
		if (type.equals(DatabaseTypeEnum.SqlServer2005)){
113
			//MySQL allows both syntaxes
114
			updateQuery = "ALTER TABLE @tableName ALTER COLUMN @columnName @columnType";
115
		}else if (type.equals(DatabaseTypeEnum.H2)){
116
			updateQuery = "ALTER TABLE @tableName ALTER COLUMN @columnName @columnType";
117
		}else if (type.equals(DatabaseTypeEnum.PostgreSQL)){
118
			updateQuery = "ALTER TABLE @tableName ALTER COLUMN @columnName TYPE @columnType";
119
		}else if (type.equals(DatabaseTypeEnum.MySQL)){
120
			updateQuery = "ALTER TABLE @tableName MODIFY COLUMN @columnName @columnType";
121
		}else{
122
			updateQuery = null;
123
			String warning = "Update step '" + this.getStepName() + "' is not supported by " + type.getName();
124
			monitor.warning(warning);
125
			throw new DatabaseTypeNotSupportedException(warning);
126
		}
127
		if (isNotNull){
128
			if (datasource.getDatabaseType().equals(DatabaseTypeEnum.PostgreSQL)){
129
				logger.warn("NOT NULL not implementd for POSTGRES");
130
			}else{
131
				updateQuery += " NOT NULL";
132
			}
133
		} else{
134
			if (! datasource.getDatabaseType().equals(DatabaseTypeEnum.PostgreSQL)){
135
				updateQuery += " NULL ";
136
			}
137
		}
138
		updateQuery = updateQuery.replace("@tableName", tableName);
139
		updateQuery = updateQuery.replace("@columnName", columnName);
140
		updateQuery = updateQuery.replace("@columnType", databaseColumnType);
141
//		updateQuery = updateQuery.replace("@addSeparator", getAddColumnSeperator(datasource));
142

    
143
		return updateQuery;
144
	}
145

    
146
	private String getDatabaseColumnType(ICdmDataSource datasource, String columnType) {
147
		return ColumnAdder.getDatabaseColumnType(datasource, columnType);
148
	}
149

    
150
	public String getReferencedTable() {
151
		return referencedTable;
152
	}
153
//
154
//	public String getNewColumnName() {
155
//		return columnName;
156
//	}
157

    
158
}
(9-9/34)