1 |
c97c2e65
|
Andreas Müller
|
/**
|
2 |
|
|
* Copyright (C) 2021 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.log4j.Logger;
|
14 |
|
|
|
15 |
|
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
16 |
|
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
17 |
|
|
|
18 |
|
|
/**
|
19 |
|
|
* Updates a value for a single table column.
|
20 |
|
|
*
|
21 |
|
|
* @author a.mueller
|
22 |
|
|
* @since 18.03.2021
|
23 |
|
|
*/
|
24 |
|
|
public class ColumnValueUpdater
|
25 |
|
|
extends AuditedSchemaUpdaterStepBase {
|
26 |
|
|
|
27 |
|
|
private static final Logger logger = Logger.getLogger(ColumnValueUpdater.class);
|
28 |
|
|
|
29 |
|
|
private final String columnName;
|
30 |
|
|
private final String newValueStr;
|
31 |
|
|
private final Integer newValueInt;
|
32 |
|
|
private final String where;
|
33 |
|
|
|
34 |
|
|
public static ColumnValueUpdater NewIntegerInstance(List<ISchemaUpdaterStep> stepList, String stepName, String tableName,
|
35 |
|
|
String columnName, Integer newValue, String where, boolean includeAudTable){
|
36 |
|
|
return new ColumnValueUpdater(stepList, stepName, tableName, columnName, null, newValue, where, includeAudTable);
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
public static ColumnValueUpdater NewStringInstance(List<ISchemaUpdaterStep> stepList, String stepName, String tableName,
|
40 |
|
|
String columnName, String newValue, String where, boolean includeAudTable){
|
41 |
|
|
return new ColumnValueUpdater(stepList, stepName, tableName, columnName, newValue, null, where, includeAudTable);
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
// **************************************** Constructor ***************************************/
|
46 |
|
|
|
47 |
|
|
protected ColumnValueUpdater(List<ISchemaUpdaterStep> stepList, String stepName, String tableName,
|
48 |
|
|
String columnName, String newValueStr, Integer newValueInt, String where, boolean includeAudTable) {
|
49 |
|
|
super(stepList, stepName, tableName, includeAudTable);
|
50 |
|
|
this.columnName = columnName;
|
51 |
|
|
this.newValueStr = newValueStr;
|
52 |
|
|
this.newValueInt = newValueInt;
|
53 |
|
|
this.where = isBlank(where)? " (1=1) " : where;
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
@Override
|
57 |
|
|
protected void invokeOnTable(String tableName, ICdmDataSource datasource,
|
58 |
|
|
IProgressMonitor monitor, CaseType caseType, SchemaUpdateResult result) {
|
59 |
|
|
try {
|
60 |
|
|
String value = (newValueStr == null && newValueInt == null) ?
|
61 |
|
|
" = NULL " : newValueInt != null ? String.valueOf(newValueInt) :
|
62 |
|
|
"'"+newValueStr+"'";
|
63 |
|
|
|
64 |
|
|
String updateQuery = "UPDATE %s "
|
65 |
|
|
+ " SET %s = %s "
|
66 |
|
|
+ " WHERE %s ";
|
67 |
|
|
updateQuery = String.format(updateQuery, caseType.transformTo(tableName),
|
68 |
|
|
columnName, value, where);
|
69 |
|
|
|
70 |
|
|
datasource.executeUpdate(updateQuery);
|
71 |
|
|
|
72 |
|
|
return;
|
73 |
|
|
} catch (Exception e) {
|
74 |
|
|
String message = e.getMessage();
|
75 |
|
|
monitor.warning(message, e);
|
76 |
|
|
logger.error(e);
|
77 |
|
|
result.addException(e, message, getStepName() + ", ColumnNameChanger.invokeOnTable");
|
78 |
|
|
return;
|
79 |
|
|
}
|
80 |
|
|
}
|
81 |
|
|
}
|