1
|
/**
|
2
|
* Copyright (C) 2019 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 24.07.2019
|
22
|
*
|
23
|
*/
|
24
|
public class AllowNullUpdater
|
25
|
extends AuditedSchemaUpdaterStepBase{
|
26
|
|
27
|
private static final Logger logger = LogManager.getLogger(AllowNullUpdater.class);
|
28
|
|
29
|
private String columnName;
|
30
|
private Datatype datatype;
|
31
|
private Integer size; //only required for MySQL
|
32
|
|
33
|
|
34
|
public static AllowNullUpdater NewStringInstance(List<ISchemaUpdaterStep> stepList, String stepName,
|
35
|
String tableName, String columnName, boolean includeAudTable){
|
36
|
return new AllowNullUpdater(stepList, stepName, tableName, columnName, includeAudTable, Datatype.VARCHAR, 255);
|
37
|
}
|
38
|
|
39
|
public static AllowNullUpdater NewStringInstance(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String columnName,
|
40
|
int size, boolean includeAudTable){
|
41
|
return new AllowNullUpdater(stepList, stepName, tableName, columnName, includeAudTable, Datatype.VARCHAR, size);
|
42
|
}
|
43
|
|
44
|
// **************************************** Constructor ***************************************/
|
45
|
|
46
|
protected AllowNullUpdater(List<ISchemaUpdaterStep> stepList, String stepName, String tableName, String columnName,
|
47
|
boolean includeAudTable, Datatype datatype, Integer size) {
|
48
|
super(stepList, stepName, tableName, includeAudTable);
|
49
|
this.columnName = columnName;
|
50
|
this.datatype = datatype;
|
51
|
this.size = size;
|
52
|
}
|
53
|
|
54
|
@Override
|
55
|
protected void invokeOnTable(String tableName, ICdmDataSource datasource,
|
56
|
IProgressMonitor monitor, CaseType caseType, SchemaUpdateResult result) {
|
57
|
try {
|
58
|
DatabaseTypeEnum type = datasource.getDatabaseType();
|
59
|
String updateQuery1;
|
60
|
|
61
|
if (type.equals(DatabaseTypeEnum.SqlServer2005)){
|
62
|
String message = "SQLServer column name changer syntax not yet tested. Table name: " + this.tableName + "; column name: " + columnName;
|
63
|
monitor.warning(message);
|
64
|
result.addWarning(message);
|
65
|
updateQuery1 = "ALTER TABLE @tableName ALTER COLUMN @columnName @definition";
|
66
|
}else if ( type.equals(DatabaseTypeEnum.MySQL)){
|
67
|
//FIXME MySQL column name changer
|
68
|
//logger.warn("Changing column name not yet supported for MySQL");
|
69
|
updateQuery1 = "ALTER TABLE @tableName MODIFY @columnName @definition";
|
70
|
}else if (type.equals(DatabaseTypeEnum.H2)){
|
71
|
updateQuery1 = "ALTER TABLE @tableName ALTER COLUMN @columnName DROP NOT NULL";
|
72
|
}else if ( type.equals(DatabaseTypeEnum.PostgreSQL) ){
|
73
|
updateQuery1 = "ALTER TABLE @tableName ALTER COLUMN @columnName DROP NOT NULL";
|
74
|
}else{
|
75
|
String message = "Update step '" + this.getStepName() + "' is not supported by " + type.getName();
|
76
|
monitor.warning(message);
|
77
|
result.addError(message, getStepName() + ", ColumnNameChanger.invokeOnTable");
|
78
|
return;
|
79
|
}
|
80
|
updateQuery1 = updateQuery1.replace("@tableName", tableName);
|
81
|
updateQuery1 = updateQuery1.replace("@columnName", columnName);
|
82
|
updateQuery1 = updateQuery1.replace("@definition", getDefinition(datasource));
|
83
|
datasource.executeUpdate(updateQuery1);
|
84
|
|
85
|
|
86
|
return;
|
87
|
} catch (Exception e) {
|
88
|
String message = e.getMessage();
|
89
|
monitor.warning(message, e);
|
90
|
logger.error(e);
|
91
|
result.addException(e, message, getStepName() + ", ColumnNameChanger.invokeOnTable");
|
92
|
return;
|
93
|
}
|
94
|
|
95
|
}
|
96
|
|
97
|
private CharSequence getDefinition(ICdmDataSource datasource) {
|
98
|
return datatype.format(datasource,size);
|
99
|
}
|
100
|
|
101
|
|
102
|
}
|