1
|
/**
|
2
|
* Copyright (C) 2020 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.math.BigDecimal;
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.List;
|
16
|
|
17
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
18
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
19
|
import eu.etaxonomy.cdm.hibernate.BigDecimalUserType;
|
20
|
|
21
|
/**
|
22
|
* Changes a float column into a BigDecimal (2 columns) for usage in {@link BigDecimalUserType}.
|
23
|
* First renames the old column and creates the 2 new columns via {@link #getInnerSteps() inner steps}
|
24
|
* and then fills the new columns by using the mapping algorithm defined in
|
25
|
* {@link #invokeOnTable(String, ICdmDataSource, IProgressMonitor, CaseType, SchemaUpdateResult)}.
|
26
|
*
|
27
|
* Does not yet delete the old column. This should be done in a follow up model update.
|
28
|
*
|
29
|
* @author a.mueller
|
30
|
* @since 27.04.2020
|
31
|
*/
|
32
|
public class Float2BigDecimalTypeChanger extends AuditedSchemaUpdaterStepBase {
|
33
|
|
34
|
private static final String OLD_POSTFIX = "_old";
|
35
|
|
36
|
private final String valueColumn;
|
37
|
private final String scaleColumn;
|
38
|
private final Integer newScale;
|
39
|
private final Integer newPrecision;
|
40
|
private final boolean isNotNull = false;
|
41
|
private boolean isRounder = false;
|
42
|
|
43
|
public static Float2BigDecimalTypeChanger NewInstance (List<? extends ISchemaUpdaterStep> stepList,
|
44
|
String stepName, String tableName, String valueColumn, String scaleColumn, Integer newPrecision, Integer newScale,
|
45
|
boolean includedAudTable){
|
46
|
return new Float2BigDecimalTypeChanger(stepList, stepName, tableName, valueColumn,
|
47
|
scaleColumn, newPrecision, newScale, includedAudTable);
|
48
|
}
|
49
|
|
50
|
protected Float2BigDecimalTypeChanger(List<? extends ISchemaUpdaterStep> stepList,
|
51
|
String stepName, String tableName, String valueColumn, String scaleColumn,
|
52
|
Integer newPrecision, Integer newScale, boolean includedAudTable) {
|
53
|
super(stepList, stepName, tableName, includedAudTable);
|
54
|
this.valueColumn = valueColumn;
|
55
|
this.scaleColumn = scaleColumn;
|
56
|
this.newPrecision = newPrecision;
|
57
|
this.newScale = newScale;
|
58
|
}
|
59
|
|
60
|
@Override
|
61
|
protected void invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor,
|
62
|
CaseType caseType, SchemaUpdateResult result) {
|
63
|
if(!isRounder){
|
64
|
return;
|
65
|
}
|
66
|
String sql = "SELECT id, %s as value_float FROM %s as t ";
|
67
|
sql = String.format(sql, valueColumn+OLD_POSTFIX, tableName);
|
68
|
try {
|
69
|
ResultSet rs = datasource.executeQuery(sql);
|
70
|
while(rs.next()){
|
71
|
Float floatValue = rs.getFloat("value_float");
|
72
|
BigDecimal bd = new BigDecimal(floatValue.toString());
|
73
|
int id = rs.getInt("id");
|
74
|
sql = "UPDATE %s SET %s = %s, %s = %s WHERE id = %s ";
|
75
|
sql = String.format(sql, tableName, valueColumn, bd.toString(), scaleColumn, bd.scale(), String.valueOf(id));
|
76
|
datasource.executeUpdate(sql);
|
77
|
}
|
78
|
} catch (SQLException e) {
|
79
|
e.printStackTrace();
|
80
|
}
|
81
|
}
|
82
|
|
83
|
|
84
|
@Override
|
85
|
public List<ISchemaUpdaterStep> getInnerSteps() {
|
86
|
if(!isRounder){
|
87
|
List<ISchemaUpdaterStep> result = new ArrayList<>();
|
88
|
ColumnNameChanger.NewFloatInstance(result, stepName + " - rename old column", tableName, valueColumn, valueColumn + OLD_POSTFIX, includeAudTable);
|
89
|
ColumnAdder.NewDecimalInstance(result, stepName + " - add value column", tableName, valueColumn, newPrecision, newScale, includeAudTable, 0, isNotNull);
|
90
|
ColumnAdder.NewIntegerInstance(result, stepName + " - add scale column", tableName, scaleColumn, includeAudTable, newScale, isNotNull);
|
91
|
Float2BigDecimalTypeChanger rounder = Float2BigDecimalTypeChanger.NewInstance(result, stepName + " - round", tableName, valueColumn, scaleColumn, newPrecision, newScale, this.includeAudTable);
|
92
|
rounder.isRounder = true;
|
93
|
return result;
|
94
|
}else{
|
95
|
return super.getInnerSteps();
|
96
|
}
|
97
|
}
|
98
|
|
99
|
}
|