Skip to content

Commit

Permalink
Fixes for new DbCompare feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmichalek committed Feb 29, 2016
1 parent f3f32ac commit 30eb3f0
Show file tree
Hide file tree
Showing 8 changed files with 452 additions and 173 deletions.
Expand Up @@ -28,6 +28,7 @@
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.model.Table;
Expand Down Expand Up @@ -95,6 +96,9 @@ protected boolean executeWithOptions(CommandLine line) throws Exception {
if (line.hasOption(OPTION_EXCLUDE)) {
dbCompare.setExcludedTableNames(Arrays.asList(line.getOptionValue(OPTION_EXCLUDE).split(",")));
}
if (!CollectionUtils.isEmpty(line.getArgList())) {
dbCompare.setIncludedTableNames(Arrays.asList(line.getArgList().get(0).toString().split(",")));
}

DbCompareReport report = dbCompare.compare();
for (TableReport tableReport : report.getTableReports()) {
Expand All @@ -117,9 +121,9 @@ protected static void initFromServerProperties() {

private static final String OPTION_EXCLUDE = "exclude";

private static final String OPTION_USE_SYM_CONFIG = "use_sym_config";
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_OUTPUT_SQL = "output-sql";

@Override
protected void printHelp(CommandLine cmd, Options options) {
Expand Down
248 changes: 119 additions & 129 deletions symmetric-core/src/main/java/org/jumpmind/symmetric/io/DbCompare.java

Large diffs are not rendered by default.

Expand Up @@ -30,76 +30,80 @@
import org.jumpmind.symmetric.ISymmetricEngine;

public class DbCompareRow {

private DbValueComparator dbValueComparator;
private ISymmetricEngine engine;
private Table table;
private Row row;
private Map<String, String> rowValues;

private Map<String, String> rowPkValues;

public DbCompareRow(ISymmetricEngine engine, DbValueComparator dbValueComparator, Table table, Row row) {
this.engine = engine;
this.table = table;
this.row = row;
this.dbValueComparator = dbValueComparator;
this.rowValues = loadRowValues();
loadRowValues();
}

public int comparePks(DbCompareRow targetRow) {
Table targetTable = targetRow.getTable();

for (int i = 0; i < table.getPrimaryKeyColumnCount(); i++) {
String sourcePkColumn = table.getPrimaryKeyColumnNames()[i];
String targetPkColumn = targetTable.getPrimaryKeyColumnNames()[i];
public int comparePks(DbCompareTables tables, DbCompareRow targetRow) {
for (Column sourcePkColumn : table.getPrimaryKeyColumns()) {
Column targetPkColumn = tables.getColumnMapping().get(sourcePkColumn);

int result = dbValueComparator.compareValues(sourcePkColumn, targetPkColumn,
rowValues.get(sourcePkColumn.getName()), targetRow.getRowValues().get(targetPkColumn.getName()));

int result = dbValueComparator.compareValues(table.getPrimaryKeyColumns()[i], targetTable.getPrimaryKeyColumns()[i],
rowValues.get(sourcePkColumn), targetRow.getRowValues().get(targetPkColumn));

if (result != 0) {
return result;
}
}
}

return 0;
}
public Map<Column, String> compareTo(DbCompareRow targetRow) {

public Map<Column, String> compareTo(DbCompareTables tables, DbCompareRow targetRow) {

Map<Column, String> deltas = new LinkedHashMap<Column, String>();
// TODO maybe should operate on non-pk columns here.
for (int i = 0; i < table.getColumnCount(); i++) {
Table targetTable = targetRow.getTable();

String sourceColumn = table.getColumnNames()[i];
String targetColumn = targetTable.getColumnNames()[i];
int result = dbValueComparator.compareValues(table.getColumns()[i], targetTable.getColumns()[i],
rowValues.get(sourceColumn), targetRow.getRowValues().get(targetColumn));
for (Column sourceColumn : table.getColumns()) {
Column targetColumn = tables.getColumnMapping().get(sourceColumn);
if (targetColumn == null) {
continue;
}

int result = dbValueComparator.compareValues(sourceColumn, targetColumn,
rowValues.get(sourceColumn.getName()), targetRow.getRowValues().get(targetColumn.getName()));

if (result != 0) {
deltas.put(targetTable.getColumns()[i], rowValues.get(sourceColumn));
}
deltas.put(targetColumn, rowValues.get(sourceColumn.getName()));
}
}

return deltas;
}
protected Map<String, String> loadRowValues() {

protected void loadRowValues() {
String[] stringValues = engine.getDatabasePlatform().
getStringValues(BinaryEncoding.HEX, table.getColumns(), row, false, false);

Map<String, String> localRowValues = new LinkedHashMap<String, String>();

Map<String, String> localPkRowValues = new LinkedHashMap<String, String>();

for (int i = 0; i < stringValues.length; i++) {
String columnName;
if (i < table.getColumnCount()) {
columnName = table.getColumn(i).getName();
String columnName = table.getColumn(i).getName();
String stringValue = stringValues[i];
localRowValues.put(columnName, stringValue);
}
if (i < table.getPrimaryKeyColumnCount()) {
String columnName = table.getPrimaryKeyColumns()[i].getName();
String stringValue = stringValues[i];
localPkRowValues.put(columnName, stringValue);
}
}

return localRowValues;

this.rowValues = localRowValues;
this.rowPkValues = localPkRowValues;
}

public DbValueComparator getDbValueComparator() {
Expand Down Expand Up @@ -142,5 +146,13 @@ public void setRowValues(Map<String, String> rowValues) {
this.rowValues = rowValues;
}

public Map<String, String> getRowPkValues() {
return rowPkValues;
}

public void setRowPkValues(Map<String, String> rowPkValues) {
this.rowPkValues = rowPkValues;
}


}
@@ -0,0 +1,111 @@
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.io;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.Table;
import org.jumpmind.symmetric.io.data.transform.TransformColumn;
import org.jumpmind.symmetric.service.impl.TransformService.TransformTableNodeGroupLink;

public class DbCompareTables {
private Table sourceTable;
private Table targetTable;
private TransformTableNodeGroupLink transform;
private Map<Column, Column> columnMapping = new LinkedHashMap<Column, Column>();

public DbCompareTables(Table sourceTable, Table targetTable) {
this.sourceTable = sourceTable;
this.targetTable = targetTable;
}

public Map<Column, Column> getColumnMapping() {
return columnMapping;
}

public void setColumnMapping(Map<Column, Column> columnMapping) {
this.columnMapping = columnMapping;
}

public void addColumnMapping(Column sourceColumn, Column targetColumn) {
columnMapping.put(sourceColumn,targetColumn);
}

public void applyColumnMappings() {
columnMapping.clear();

if (transform != null && !CollectionUtils.isEmpty(transform.getTransformColumns())) {
applyColumnMappingsFromTransform();
} else {
applyColumnMappingsDefault();
}
}

protected void applyColumnMappingsFromTransform() {
for (Column sourceColumn : sourceTable.getColumns()) {
List<TransformColumn> sourceTransformColumns = transform.getTransformColumnFor(sourceColumn.getName());
if (!sourceTransformColumns.isEmpty()) {
TransformColumn transformColumn = sourceTransformColumns.get(0);
Column targetColumn = targetTable.getColumnWithName(transformColumn.getTargetColumnName());
columnMapping.put(sourceColumn, targetColumn);
}
}
}

protected void applyColumnMappingsDefault() {
for (Column sourceColumn : sourceTable.getColumns()) {
for (Column targetColumn : targetTable.getColumns()) {
if (StringUtils.equalsIgnoreCase(sourceColumn.getName(), targetColumn.getName())) {
columnMapping.put(sourceColumn, targetColumn);
}
}
}
}

public TransformTableNodeGroupLink getTransform() {
return transform;
}

public void setTransform(TransformTableNodeGroupLink transform) {
this.transform = transform;
}

public Table getSourceTable() {
return sourceTable;
}

public void setSourceTable(Table sourceTable) {
this.sourceTable = sourceTable;
}

public Table getTargetTable() {
return targetTable;
}

public void setTargetTable(Table targetTable) {
this.targetTable = targetTable;
}
}
Expand Up @@ -23,6 +23,7 @@
import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.TypeMap;
Expand Down Expand Up @@ -80,13 +81,29 @@ public int compareText(Column sourceColumn, Column targetColumn, String source,
}

public int compareNumeric(Column sourceColumn, Column targetColumn, String sourceValue, String targetValue) {
if (sourceValue != null && targetValue != null) {
BigDecimal source = NumberUtils.createBigDecimal(sourceValue.toString());
BigDecimal target = NumberUtils.createBigDecimal(targetValue.toString());
return source.compareTo(target);
} else {
return compareDefault(sourceColumn, targetColumn, sourceValue, targetValue);
if (StringUtils.isBlank(sourceValue) && StringUtils.isBlank(targetValue)) {
return 0;
}
if (!StringUtils.isBlank(sourceValue) && StringUtils.isBlank(targetValue)) {
return 1;
}
if (StringUtils.isBlank(sourceValue) && !StringUtils.isBlank(targetValue)) {
return -1;
}

try {
BigDecimal source = NumberUtils.createBigDecimal(sourceValue);
BigDecimal target = NumberUtils.createBigDecimal(targetValue);
return source.compareTo(target);
} catch (NumberFormatException ex) {
System.out.println(sourceColumn + " " + sourceValue + " " + targetColumn + " " + targetValue + " " + ex);
}

return sourceValue.compareTo(targetValue);




}

public int compareDateTime(Column sourceColumn, Column targetColumn, String sourceValue, String targetValue) {
Expand Down
Expand Up @@ -22,6 +22,7 @@

import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.platform.db2.Db2zOsDmlStatement;
import org.jumpmind.db.platform.mysql.MySqlDmlStatement;
import org.jumpmind.db.platform.oracle.OracleDmlStatement;
import org.jumpmind.db.platform.postgresql.PostgreSqlDmlStatement;
Expand Down Expand Up @@ -84,6 +85,10 @@ public static DmlStatement createDmlStatement(String databaseName, DmlType dmlTy
return new SqlAnywhereDmlStatement(dmlType, catalogName, schemaName, tableName, keys, columns,
nullKeyValues, ddlBuilder.getDatabaseInfo(),
ddlBuilder.isDelimitedIdentifierModeOn(), textColumnExpression);
} else if (DatabaseNamesConstants.DB2ZOS.equals(databaseName)) {
return new Db2zOsDmlStatement(dmlType, catalogName, schemaName, tableName, keys, columns,
nullKeyValues, ddlBuilder.getDatabaseInfo(),
ddlBuilder.isDelimitedIdentifierModeOn(), textColumnExpression);
} else {
return new DmlStatement(dmlType, catalogName, schemaName, tableName, keys, columns,
nullKeyValues, ddlBuilder.getDatabaseInfo(),
Expand Down

0 comments on commit 30eb3f0

Please sign in to comment.