-
Notifications
You must be signed in to change notification settings - Fork 221
/
AdditiveColumnTransform.java
91 lines (77 loc) · 3.72 KB
/
AdditiveColumnTransform.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package org.jumpmind.symmetric.io.data.transform;
import java.math.BigDecimal;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.extension.IBuiltInExtensionPoint;
import org.jumpmind.symmetric.io.data.DataContext;
public class AdditiveColumnTransform implements ISingleValueColumnTransform, IBuiltInExtensionPoint {
public static final String NAME = "additive";
public String getName() {
return NAME;
}
public boolean isExtractColumnTransform() {
return false;
}
public boolean isLoadColumnTransform() {
return true;
}
public String getFullyQualifiedTableName(IDatabasePlatform platform, String schema, String catalog, String tableName) {
String quote = platform.getDdlBuilder().isDelimitedIdentifierModeOn() ? platform
.getPlatformInfo().getDelimiterToken() : "";
tableName = quote + tableName + quote;
if (!StringUtils.isBlank(schema)) {
tableName = schema + "." + tableName;
}
if (!StringUtils.isBlank(catalog)) {
tableName = catalog + "." + tableName;
}
return tableName;
}
public String transform(IDatabasePlatform platform, DataContext context,
TransformColumn column, TransformedData data, Map<String, String> sourceValues, String newValue, String oldValue) throws IgnoreColumnException,
IgnoreRowException {
if (StringUtils.isNotBlank(newValue)) {
BigDecimal numericValue = new BigDecimal(newValue);
Table table = platform.getTableFromCache(data.getCatalogName(), data.getSchemaName(),
data.getTableName(), false);
if (table != null) {
if (StringUtils.isNotBlank(oldValue)) {
numericValue = numericValue.subtract(new BigDecimal(oldValue));
newValue = numericValue.toString();
}
String quote = platform.getDdlBuilder().isDelimitedIdentifierModeOn() ? platform
.getPlatformInfo().getDelimiterToken() : "";
StringBuilder sql = new StringBuilder(String.format("update %s set %s=%s+%s where ",
getFullyQualifiedTableName(platform, data.getSchemaName(), data.getCatalogName(), data.getTableName()),
quote + column.getTargetColumnName() + quote,
quote + column.getTargetColumnName() + quote,
newValue));
String[] keyNames = data.getKeyNames();
Column[] columns = new Column[keyNames.length];
for (int i = 0; i < keyNames.length; i++) {
if (i > 0) {
sql.append("and ");
}
columns[i] = table.getColumnWithName(keyNames[i]);
if (columns[i] == null) {
throw new NullPointerException("Could not find a column named: " + keyNames[i] + " on the target table: " + table.getName());
}
sql.append(quote);
sql.append(keyNames[i]);
sql.append(quote);
sql.append("=? ");
}
if (0 < platform.getSqlTemplate().update(
sql.toString(),
platform.getObjectValues(context.getBatch().getBinaryEncoding(),
data.getKeyValues(), columns))) {
throw new IgnoreColumnException();
}
}
}
return newValue;
}
}