Skip to content

Commit

Permalink
AdditiveColumnTransform now accepts a multiplier (default of 1), and …
Browse files Browse the repository at this point in the history
…if new value doesn't exist it's treated as zero.
  • Loading branch information
mhanes committed May 4, 2012
1 parent 480e08d commit 870ef7e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 39 deletions.
6 changes: 4 additions & 2 deletions symmetric/symmetric-assemble/src/docbook/configuration.xml
Expand Up @@ -1106,8 +1106,10 @@ insert into sym_trigger_router (TRIGGER_ID,ROUTER_ID,INITIAL_LOAD_ORDER,
</listitem>
<listitem>
Additive Transform ('additive'): This transformation type is used for numeric data. It computes the change between the old and new values on the source
and then adds (or subtracts) the value from the existing value in the target column. For example, if the source column changed from a 2 to a 4, and the target
column is currently 10, the effect of the transform will be to change the target column to a value of 12 ( 10+(4-2) => 12 ).
and then adds the change to the existing value in the target column. That is, target = target + multiplier (source_new - source_old), where multiplier
is a constant found in the transform_expression (default is 1 if not specified). For example, if the source column changed from a 2 to a 4, the target
column is currently 10, and the multiplier is 3, the effect of the transform will be to change the target column to a value of 16 ( 10+3*(4-2) => 16 ). Note that,
in the case of deletes, the new column value is considered 0 for the purposes of the calculation.
</listitem>
<listitem>
Substring Transform ('substr'): This transformation computes a substring of the source column data and uses the substring as the target column value. The transform_expression can
Expand Down
Expand Up @@ -42,49 +42,63 @@ public String getFullyQualifiedTableName(IDatabasePlatform platform, String sche
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
.getDatabaseInfo().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));

BigDecimal multiplier = new BigDecimal(1.00);

if (StringUtils.isNotBlank(column.getTransformExpression())) {
multiplier = new BigDecimal(column.getTransformExpression());
}

BigDecimal delta = new BigDecimal(newValue);

Table table = platform.getTableFromCache(data.getCatalogName(), data.getSchemaName(),
data.getTableName(), false);
if (table != null) {
if (!StringUtils.isNotBlank(newValue)) {
newValue="0.00";
}

if (!StringUtils.isNotBlank(oldValue)) {
oldValue="0.00";
}

delta = delta.subtract(new BigDecimal(oldValue));
delta = delta.multiply(multiplier);
newValue = delta.toString();

String quote = platform.getDdlBuilder().isDelimitedIdentifierModeOn() ? platform
.getDatabaseInfo().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("=? ");
String[] keyNames = data.getKeyNames();
Column[] columns = new Column[keyNames.length];
for (int i = 0; i < keyNames.length; i++) {
if (i > 0) {
sql.append("and ");
}

if (0 < platform.getSqlTemplate().update(
sql.toString(),
platform.getObjectValues(context.getBatch().getBinaryEncoding(),
data.getKeyValues(), columns))) {
throw new IgnoreColumnException();
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;
}

Expand Down

0 comments on commit 870ef7e

Please sign in to comment.