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 java.sql.Types;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
|
16
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
17
|
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
|
18
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
19
|
|
20
|
/**
|
21
|
* @author a.mueller
|
22
|
* @date 16.09.2010
|
23
|
*
|
24
|
*/
|
25
|
public class ColumnAdder extends AuditedSchemaUpdaterStepBase<ColumnAdder> implements ISchemaUpdaterStep {
|
26
|
private static final Logger logger = Logger.getLogger(ColumnAdder.class);
|
27
|
|
28
|
private final String newColumnName;
|
29
|
private final String columnType;
|
30
|
private final Object defaultValue;
|
31
|
private boolean isNotNull;
|
32
|
|
33
|
private final String referencedTable;
|
34
|
|
35
|
public static final ColumnAdder NewIntegerInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable, boolean notNull, String referencedTable){
|
36
|
return new ColumnAdder(stepName, tableName, newColumnName, "int", includeAudTable, null, notNull, referencedTable);
|
37
|
}
|
38
|
|
39
|
public static final ColumnAdder NewIntegerInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable, Integer defaultValue, boolean notNull){
|
40
|
return new ColumnAdder(stepName, tableName, newColumnName, "int", includeAudTable, defaultValue, notNull, null);
|
41
|
}
|
42
|
|
43
|
public static final ColumnAdder NewTinyIntegerInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable, boolean notNull){
|
44
|
return new ColumnAdder(stepName, tableName, newColumnName, "tinyint", includeAudTable, null, notNull, null);
|
45
|
}
|
46
|
|
47
|
public static final ColumnAdder NewDoubleInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable, boolean notNull){
|
48
|
return new ColumnAdder(stepName, tableName, newColumnName, "double", includeAudTable, null, notNull, null);
|
49
|
}
|
50
|
|
51
|
public static final ColumnAdder NewBooleanInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable, Boolean defaultValue){
|
52
|
return new ColumnAdder(stepName, tableName, newColumnName, "bit", includeAudTable, defaultValue, false, null);
|
53
|
}
|
54
|
|
55
|
public static final ColumnAdder NewStringInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable){
|
56
|
return new ColumnAdder(stepName, tableName, newColumnName, "nvarchar(255)", includeAudTable, null, false, null);
|
57
|
}
|
58
|
|
59
|
public static final ColumnAdder NewStringInstance(String stepName, String tableName, String newColumnName, int length, boolean includeAudTable){
|
60
|
return new ColumnAdder(stepName, tableName, newColumnName, "nvarchar("+length+")", includeAudTable, null, false, null);
|
61
|
}
|
62
|
|
63
|
public static final ColumnAdder NewClobInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable){
|
64
|
return new ColumnAdder(stepName, tableName, newColumnName, "clob", includeAudTable, null, false, null);
|
65
|
}
|
66
|
|
67
|
public static final ColumnAdder NewDateTimeInstance(String stepName, String tableName, String newColumnName, boolean includeAudTable, boolean notNull){
|
68
|
return new ColumnAdder(stepName, tableName, newColumnName, "datetime", includeAudTable, null, notNull, null);
|
69
|
}
|
70
|
|
71
|
protected ColumnAdder(String stepName, String tableName, String newColumnName, String columnType, boolean includeAudTable, Object defaultValue, boolean notNull, String referencedTable) {
|
72
|
super(stepName, tableName, includeAudTable);
|
73
|
this.newColumnName = newColumnName;
|
74
|
this.columnType = columnType;
|
75
|
this.defaultValue = defaultValue;
|
76
|
this.isNotNull = notNull;
|
77
|
this.referencedTable = referencedTable;
|
78
|
}
|
79
|
|
80
|
public ColumnAdder setNotNull(boolean isNotNull) {
|
81
|
this.isNotNull = isNotNull;
|
82
|
return this;
|
83
|
}
|
84
|
|
85
|
@Override
|
86
|
protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) {
|
87
|
boolean result = true;
|
88
|
try {
|
89
|
String updateQuery = getUpdateQueryString(tableName, datasource, monitor);
|
90
|
datasource.executeUpdate(updateQuery);
|
91
|
|
92
|
if (defaultValue instanceof Boolean){
|
93
|
String defaultValueQuery = "UPDATE @tableName SET @columnName = " + (defaultValue == null ? "NULL" : getBoolean((Boolean) defaultValue, datasource));
|
94
|
defaultValueQuery = defaultValueQuery.replace("@tableName", tableName);
|
95
|
defaultValueQuery = defaultValueQuery.replace("@columnName", newColumnName);
|
96
|
datasource.executeUpdate(defaultValueQuery);
|
97
|
}else if (defaultValue instanceof Integer){
|
98
|
String defaultValueQuery = "UPDATE @tableName SET @columnName = " + (defaultValue == null ? "NULL" : defaultValue);
|
99
|
defaultValueQuery = defaultValueQuery.replace("@tableName", tableName);
|
100
|
defaultValueQuery = defaultValueQuery.replace("@columnName", newColumnName);
|
101
|
datasource.executeUpdate(defaultValueQuery);
|
102
|
}else if (defaultValue != null){
|
103
|
logger.warn("Default Value not implemented for type " + defaultValue.getClass().getName());
|
104
|
}
|
105
|
if (referencedTable != null){
|
106
|
result &= TableCreator.makeForeignKey(tableName, datasource, monitor, newColumnName, referencedTable, caseType);
|
107
|
}
|
108
|
|
109
|
return result;
|
110
|
} catch ( Exception e) {
|
111
|
monitor.warning(e.getMessage(), e);
|
112
|
logger.error(e);
|
113
|
return false;
|
114
|
}
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* Returns the update query string. tableName must already be cased correctly. See {@link CaseType}.
|
119
|
* @param tableName correctly cased table name
|
120
|
* @param datasource data source
|
121
|
* @param monitor monitor
|
122
|
* @return the query string
|
123
|
* @throws DatabaseTypeNotSupportedException
|
124
|
*/
|
125
|
public String getUpdateQueryString(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) throws DatabaseTypeNotSupportedException {
|
126
|
String updateQuery;
|
127
|
DatabaseTypeEnum type = datasource.getDatabaseType();
|
128
|
String databaseColumnType = getDatabaseColumnType(datasource, this.columnType);
|
129
|
|
130
|
if (type.equals(DatabaseTypeEnum.SqlServer2005)){
|
131
|
//MySQL allows both syntaxes
|
132
|
updateQuery = "ALTER TABLE @tableName ADD @columnName @columnType";
|
133
|
}else if (type.equals(DatabaseTypeEnum.H2) || type.equals(DatabaseTypeEnum.PostgreSQL) || type.equals(DatabaseTypeEnum.MySQL)){
|
134
|
updateQuery = "ALTER TABLE @tableName @addSeparator @columnName @columnType";
|
135
|
}else{
|
136
|
updateQuery = null;
|
137
|
String warning = "Update step '" + this.getStepName() + "' is not supported by " + type.getName();
|
138
|
monitor.warning(warning);
|
139
|
throw new DatabaseTypeNotSupportedException(warning);
|
140
|
}
|
141
|
if (isNotNull){
|
142
|
updateQuery += " NOT NULL";
|
143
|
}
|
144
|
updateQuery = updateQuery.replace("@tableName", tableName);
|
145
|
updateQuery = updateQuery.replace("@columnName", newColumnName);
|
146
|
updateQuery = updateQuery.replace("@columnType", databaseColumnType);
|
147
|
updateQuery = updateQuery.replace("@addSeparator", getAddColumnSeperator(datasource));
|
148
|
|
149
|
return updateQuery;
|
150
|
}
|
151
|
|
152
|
protected static String getDatabaseColumnType(ICdmDataSource datasource, String columnType) {
|
153
|
String result = columnType;
|
154
|
DatabaseTypeEnum dbType = datasource.getDatabaseType();
|
155
|
//nvarchar
|
156
|
if (dbType.equals(DatabaseTypeEnum.PostgreSQL)){ //TODO use PostgeSQL82 Dialect infos
|
157
|
result = result.replace("nvarchar", "varchar");
|
158
|
result = result.replace("double", "float8");
|
159
|
result = result.replace("bit", DatabaseTypeEnum.PostgreSQL.getHibernateDialect().getTypeName(Types.BIT));
|
160
|
result = result.replace("datetime", DatabaseTypeEnum.PostgreSQL.getHibernateDialect().getTypeName(Types.TIMESTAMP));
|
161
|
result = result.replace("tinyint", DatabaseTypeEnum.PostgreSQL.getHibernateDialect().getTypeName(Types.TINYINT));
|
162
|
}
|
163
|
//CLOB
|
164
|
if (columnType.equalsIgnoreCase("clob")){
|
165
|
//TODO use hibernate dialects
|
166
|
if (dbType.equals(DatabaseTypeEnum.MySQL)){
|
167
|
result = "longtext";
|
168
|
}else if (dbType.equals(DatabaseTypeEnum.H2)){
|
169
|
result = "CLOB"; //or NVARCHAR
|
170
|
}else if (dbType.equals(DatabaseTypeEnum.PostgreSQL)){
|
171
|
result = "text";
|
172
|
}else if (dbType.equals(DatabaseTypeEnum.SqlServer2005)){
|
173
|
result = "NVARCHAR(MAX)";
|
174
|
}
|
175
|
}
|
176
|
return result;
|
177
|
}
|
178
|
|
179
|
|
180
|
/**
|
181
|
* Returns the sql keywords for adding a column. This is usually 'ADD' or 'ADD COLUMN'
|
182
|
* @param datasource
|
183
|
* @return
|
184
|
* @throws DatabaseTypeNotSupportedException
|
185
|
*/
|
186
|
public static String getAddColumnSeperator(ICdmDataSource datasource) throws DatabaseTypeNotSupportedException {
|
187
|
DatabaseTypeEnum type = datasource.getDatabaseType();
|
188
|
if (type.equals(DatabaseTypeEnum.SqlServer2005)){
|
189
|
return "ADD ";
|
190
|
}else if (type.equals(DatabaseTypeEnum.H2) || type.equals(DatabaseTypeEnum.PostgreSQL) || type.equals(DatabaseTypeEnum.MySQL)){
|
191
|
return "ADD COLUMN ";
|
192
|
}else{
|
193
|
throw new DatabaseTypeNotSupportedException(datasource.getName());
|
194
|
}
|
195
|
}
|
196
|
|
197
|
public String getReferencedTable() {
|
198
|
return referencedTable;
|
199
|
}
|
200
|
|
201
|
|
202
|
public String getNewColumnName() {
|
203
|
return newColumnName;
|
204
|
}
|
205
|
|
206
|
}
|