Skip to content

Commit

Permalink
0003389: dbcompare, timestamp and utf8. Added date-time-format option
Browse files Browse the repository at this point in the history
  • Loading branch information
klementinastojanovska committed Feb 1, 2018
1 parent aaa7e3d commit 01c5d99
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
Expand Up @@ -119,6 +119,15 @@ protected boolean executeWithOptions(CommandLine line) throws Exception {
throw new ParseException("Failed to parse arg [" + numericScaleArg + "] " + ex);
}
}

String dateTimeFormatArg = getOptionValue(OPTION_DATE_TIME_FORMAT, "dateTimeFormat", line, config);
if (!StringUtils.isEmpty(dateTimeFormatArg)) {
try {
config.setDateTimeFormat(dateTimeFormatArg);
} catch (Exception ex) {
throw new ParseException("Failed to parse arg [" + dateTimeFormatArg + "]" + ex);
}
}

ISymmetricEngine sourceEngine = new ClientSymmetricEngine(sourceProperies);
ISymmetricEngine targetEngine = new ClientSymmetricEngine(targetProperties);
Expand All @@ -133,10 +142,10 @@ protected String getOptionValue(String optionName, String internalName, CommandL
String optionValue = line.hasOption(optionName) ? line.getOptionValue(optionName) : null;
if (optionValue == null) {
Properties props = getConfigProperties(line);
optionValue = props.getProperty(optionName);
optionValue = props != null ? props.getProperty(optionName) : null;
if (optionValue == null) {
String optionNameUnderScore = optionName.replace('-', '_');
optionValue = props.getProperty(optionNameUnderScore);
optionValue = props != null ? props.getProperty(optionNameUnderScore) : null;
if (optionValue != null) {
config.setConfigSource(internalName, line.getOptionValue(OPTION_CONFIG_PROPERTIES));
}
Expand Down Expand Up @@ -171,6 +180,8 @@ protected static void initFromServerProperties() {
private static final String OPTION_OUTPUT_SQL = "output-sql";

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

private static final String OPTION_DATE_TIME_FORMAT = "date-time-format";

private static final String OPTION_CONFIG_PROPERTIES = "config";

Expand All @@ -191,6 +202,7 @@ protected void buildOptions(Options options) {
addOption(options, null, OPTION_USE_SYM_CONFIG, true);
addOption(options, null, OPTION_OUTPUT_SQL, true);
addOption(options, null, OPTION_NUMERIC_SCALE, true);
addOption(options, null, OPTION_DATE_TIME_FORMAT, true);
addOption(options, null, OPTION_CONFIG_PROPERTIES, true);
}

Expand Down
Expand Up @@ -79,6 +79,7 @@ public DbCompare(ISymmetricEngine sourceEngine, ISymmetricEngine targetEngine, D

public DbCompareReport compare() {
dbValueComparator.setNumericScale(config.getNumericScale());
dbValueComparator.setDateTimeFormat(config.getDateTimeFormat());

log.info("Starting DBCompare with config:\n{}", config.report());

Expand Down
Expand Up @@ -42,6 +42,7 @@ public class DbCompareConfig {
private List<String> excludedTableNames;
private boolean useSymmetricConfig = true;
private int numericScale = 3;
private String dateTimeFormat;
private Map<String, String> whereClauses = new LinkedHashMap<String, String>();
private Map<String, List<String>> tablesToExcludedColumns = new LinkedHashMap<String, List<String>>();
private String outputSql;
Expand Down Expand Up @@ -133,6 +134,12 @@ public int getNumericScale() {
public void setNumericScale(int numericScale) {
this.numericScale = numericScale;
}
public String getDateTimeFormat() {
return dateTimeFormat;
}
public void setDateTimeFormat(String format) {
this.dateTimeFormat= format;
}
public Map<String, String> getWhereClauses() {
return whereClauses;
}
Expand Down
Expand Up @@ -47,6 +47,7 @@ public class DbValueComparator {
private boolean stringNullEqualsEmptyString = true;
private List<SimpleDateFormat> dateFormats = new ArrayList<SimpleDateFormat>();
private int numericScale = -1;
private String dateTimeFormat;

public DbValueComparator(ISymmetricEngine sourceEngine, ISymmetricEngine targetEngine) {
this.sourceEngine = sourceEngine;
Expand Down Expand Up @@ -142,15 +143,26 @@ public int compareDateTime(Column sourceColumn, Column targetColumn, String sour
Date sourceDate = parseDate(sourceEngine, sourceColumn, sourceValue);
Date targetDate = parseDate(targetEngine, targetColumn, targetValue);

// if either column is a simple date, clear the time for comparison purposes.
if (sourceColumn.getJdbcTypeCode() == Types.DATE
|| targetColumn.getJdbcTypeCode() == Types.DATE) {
if (sourceColumn.getJdbcTypeCode() != Types.DATE
&& targetColumn.getJdbcTypeCode() != Types.DATE) {
if (dateTimeFormat != null) {
String sourceDateFormatted = formatDateTime(sourceDate);
String targetDateFormatted = formatDateTime(targetDate);
return compareDefault(sourceColumn, targetColumn, sourceDateFormatted, targetDateFormatted);
}
} else { // if either column is a simple date, clear the time for comparison purposes.
sourceDate = DateUtils.truncate(sourceDate, Calendar.DATE);
targetDate = DateUtils.truncate(targetDate, Calendar.DATE);
}

return compareDefault(sourceColumn, targetColumn, sourceDate, targetDate);
}

public String formatDateTime(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat(dateTimeFormat);

String formattedDate = formatter.format(date);
return formattedDate;
}

@SuppressWarnings("unchecked")
protected int compareDefault(Column sourceColumn, Column targetColumn, Object sourceValue, Object targetValue) {
Expand Down Expand Up @@ -204,6 +216,13 @@ public int getNumericScale() {
public void setNumericScale(int numericScale) {
this.numericScale = numericScale;
}


public String getDateTimeFormat() {
return dateTimeFormat;
}

public void setDateTimeFormat(String format) {
this.dateTimeFormat = format;
}

}

0 comments on commit 01c5d99

Please sign in to comment.