Skip to content

Commit

Permalink
0002564: dbcompare feature - support rounding to a certain scale when
Browse files Browse the repository at this point in the history
comparing decimals.  The default scale is 3.
  • Loading branch information
mmichalek committed May 16, 2016
1 parent ac3cd97 commit 3770e8f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
Expand Up @@ -99,12 +99,16 @@ protected boolean executeWithOptions(CommandLine line) throws Exception {
dbCompare.setIncludedTableNames(Arrays.asList(line.getArgList().get(0).toString().split(",")));
}

String numericScaleArg = line.getOptionValue(OPTION_NUMERIC_SCALE);
if (!StringUtils.isEmpty(numericScaleArg)) {
try {
dbCompare.setNumericScale(Integer.parseInt(numericScaleArg.trim()));
} catch (Exception ex) {
throw new ParseException("Failed to parse arg [" + numericScaleArg + "] " + ex);
}
}

DbCompareReport report = dbCompare.compare();
// if (report.getTableReports() != null) {
// for (TableReport tableReport : report.getTableReports()) {
// System.out.println(tableReport);
// }
// }

return false;
}
Expand All @@ -125,6 +129,8 @@ protected static void initFromServerProperties() {
private static final String OPTION_USE_SYM_CONFIG = "use-sym-config";

private static final String OPTION_OUTPUT_SQL = "output-sql";

private static final String OPTION_NUMERIC_SCALE = "numeric-scale";

@Override
protected void printHelp(CommandLine cmd, Options options) {
Expand All @@ -141,6 +147,7 @@ protected void buildOptions(Options options) {
addOption(options, null, OPTION_EXCLUDE, true);
addOption(options, null, OPTION_USE_SYM_CONFIG, false);
addOption(options, null, OPTION_OUTPUT_SQL, true);
addOption(options, null, OPTION_NUMERIC_SCALE, true);
}

}
Expand Up @@ -67,6 +67,15 @@ public Row mapRow(Row row) {
private List<String> excludedTableNames;
private boolean useSymmetricConfig = true;
private DbValueComparator dbValueComparator;
private int numericScale = 3;

public int getNumericScale() {
return numericScale;
}

public void setNumericScale(int numericScale) {
this.numericScale = numericScale;
}

public DbCompare(ISymmetricEngine sourceEngine, ISymmetricEngine targetEngine) {
this.sourceEngine = sourceEngine;
Expand All @@ -75,6 +84,8 @@ public DbCompare(ISymmetricEngine sourceEngine, ISymmetricEngine targetEngine) {
}

public DbCompareReport compare() {
dbValueComparator.setNumericScale(numericScale);

DbCompareReport report = new DbCompareReport();
long start = System.currentTimeMillis();
List<DbCompareTables> tablesToCompare = getTablesToCompare();
Expand Down Expand Up @@ -119,6 +130,8 @@ protected TableReport compareTables(DbCompareTables tables) {
Row targetRow = targetCursor.next();




int counter = 0;
long startTime = System.currentTimeMillis();
DbCompareDiffWriter diffWriter = new DbCompareDiffWriter(targetEngine, tables, sqlDiffFileName);
Expand Down
Expand Up @@ -35,14 +35,19 @@
import org.jumpmind.db.model.TypeMap;
import org.jumpmind.symmetric.ISymmetricEngine;
import org.jumpmind.util.FormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DbValueComparator {

final Logger log = LoggerFactory.getLogger(getClass());

private ISymmetricEngine sourceEngine;
private ISymmetricEngine targetEngine;
private boolean stringIgnoreWhiteSpace = true;
private boolean stringNullEqualsEmptyString = true;
private List<SimpleDateFormat> dateFormats = new ArrayList<SimpleDateFormat>();
private int numericScale = -1;

public DbValueComparator(ISymmetricEngine sourceEngine, ISymmetricEngine targetEngine) {
this.sourceEngine = sourceEngine;
Expand Down Expand Up @@ -105,14 +110,29 @@ public int compareNumeric(Column sourceColumn, Column targetColumn, String sourc
return -1;
}

BigDecimal source = null;
BigDecimal target = null;

try {
BigDecimal source = NumberUtils.createBigDecimal(sourceValue);
BigDecimal target = NumberUtils.createBigDecimal(targetValue);
return source.compareTo(target);
source = NumberUtils.createBigDecimal(sourceValue);
} catch (NumberFormatException ex) {
System.out.println(sourceColumn + " " + sourceValue + " " + targetColumn + " " + targetValue + " " + ex);
log.debug("Failed to parse [" + sourceValue + "]", ex);
}

try {
target = NumberUtils.createBigDecimal(targetValue);
} catch (NumberFormatException ex) {
log.debug("Failed to parse [" + targetValue + "]", ex);
}

if (source != null && target != null) {
if (numericScale >= 0) {
source = source.setScale(numericScale, BigDecimal.ROUND_HALF_UP);
target = target.setScale(numericScale, BigDecimal.ROUND_HALF_UP);
}
return source.compareTo(target);
}

return sourceValue.compareTo(targetValue);
}

Expand Down Expand Up @@ -177,6 +197,14 @@ protected Date parseDate(ISymmetricEngine engine, Column column, String value) {
}
}
return date;
}

public int getNumericScale() {
return numericScale;
}

public void setNumericScale(int numericScale) {
this.numericScale = numericScale;
}


Expand Down

0 comments on commit 3770e8f

Please sign in to comment.