Skip to content

Commit

Permalink
0005113: Data mapping is incorrect on an insert fallback to update
Browse files Browse the repository at this point in the history
potentially when column count on target is not the same as row data
  • Loading branch information
joshahicks committed Nov 3, 2021
1 parent 57cf9e8 commit abc7f75
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,25 @@ public Map<String, String> toColumnNameValuePairs(String[] keyNames, String key)
return new HashMap<String, String>(0);
}
}

public Map<String, String> toColumnNameValuePairsWithExactMatch(String[] primaryKeyNames, String[] secondaryKeyNames,String key) {
String[] values = getParsedData(key);
if (values != null && primaryKeyNames != null && values.length == primaryKeyNames.length) {
Map<String, String> map = new LinkedCaseInsensitiveMap<String>(primaryKeyNames.length);
for (int i = 0; i < primaryKeyNames.length; i++) {
map.put(primaryKeyNames[i], values[i]);
}
return map;
} else if (values != null && secondaryKeyNames != null && values.length == secondaryKeyNames.length) {
Map<String, String> map = new LinkedCaseInsensitiveMap<String>(secondaryKeyNames.length);
for (int i = 0; i < secondaryKeyNames.length; i++) {
map.put(secondaryKeyNames[i], values[i]);
}
return map;
} else {
return new HashMap<String, String>(0);
}
}

public boolean requiresTable() {
return dataEventType != null && dataEventType != DataEventType.CREATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ protected boolean checkForUniqueKeyViolation(AbstractDatabaseWriter writer, CsvD
int count = 0;
if (e != null && sqlTemplate.isUniqueKeyViolation(e)) {
Table targetTable = writer.getTargetTable();
Map<String, String> values = data.toColumnNameValuePairs(targetTable.getColumnNames(), CsvData.ROW_DATA);
Map<String, String> values = data.toColumnNameValuePairsWithExactMatch(targetTable.getColumnNames(),
writer.getSourceTable().getColumnNames(), CsvData.ROW_DATA);
List<Column> whereColumns = targetTable.getPrimaryKeyColumnsAsList();
List<String> whereValues = new ArrayList<String>();
for (Column column : whereColumns) {
Expand Down

0 comments on commit abc7f75

Please sign in to comment.