Skip to content

Commit

Permalink
fixed: duplicate foreign key issue (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbesimi authored Jul 6, 2023
1 parent 3467818 commit 1de0075
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.adaptivescale.rosetta.common.models;

import java.util.Objects;

public class ForeignKey {
private String name;
private String schema;
Expand Down Expand Up @@ -74,4 +76,17 @@ public String getColumnName() {
public void setColumnName(String columnName) {
this.columnName = columnName;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ForeignKey that = (ForeignKey) o;
return Objects.equals(name, that.name) && Objects.equals(tableName, that.tableName) && Objects.equals(columnName, that.columnName);
}

@Override
public int hashCode() {
return Objects.hash(name, tableName, columnName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

public class ColumnsExtractor implements ColumnExtractor<java.sql.Connection, Collection<Table>> {

Expand Down Expand Up @@ -79,7 +80,7 @@ protected void extract(ResultSet resultSet, Column column) throws SQLException {

private Map<String, List<ForeignKey>> extractForeignKeys(java.sql.Connection connection, Table table) throws SQLException {
ResultSet exportedKeys = connection.getMetaData().getImportedKeys(this.connection.getDatabaseName(), table.getSchema(), table.getName());
Map<String, List<ForeignKey>> result = new HashMap<>();
Map<String, Set<ForeignKey>> result = new HashMap<>();

while (exportedKeys.next()) {
ForeignKey foreignKey = new ForeignKey();
Expand All @@ -101,11 +102,16 @@ private Map<String, List<ForeignKey>> extractForeignKeys(java.sql.Connection con
foreignKey.setPrimaryTableName(exportedKeys.getString("PKTABLE_NAME"));
foreignKey.setPrimaryColumnName(exportedKeys.getString("PKCOLUMN_NAME"));

List<ForeignKey> foreignKeys = result.computeIfAbsent(foreignKey.getColumnName(), k -> new ArrayList<>());
Set<ForeignKey> foreignKeys = result.computeIfAbsent(foreignKey.getColumnName(), k -> new HashSet<>());
foreignKeys.add(foreignKey);
}

return result;
return result.entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> new ArrayList<>(entry.getValue())
));
}

private Map<String, Integer> extractPrimaryKeys(java.sql.Connection connection, Table table) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

@RosettaModule(
name = "db2",
Expand Down Expand Up @@ -61,7 +62,7 @@ protected void extract(ResultSet resultSet, Column column) throws SQLException {

private Map<String, List<ForeignKey>> extractForeignKeys(java.sql.Connection connection, Table table) throws SQLException {
ResultSet exportedKeys = connection.getMetaData().getImportedKeys(null, table.getSchema(), table.getName());
Map<String, List<ForeignKey>> result = new HashMap<>();
Map<String, Set<ForeignKey>> result = new HashMap<>();

while (exportedKeys.next()) {
ForeignKey foreignKey = new ForeignKey();
Expand All @@ -83,11 +84,16 @@ private Map<String, List<ForeignKey>> extractForeignKeys(java.sql.Connection con
foreignKey.setPrimaryTableName(exportedKeys.getString("PKTABLE_NAME"));
foreignKey.setPrimaryColumnName(exportedKeys.getString("PKCOLUMN_NAME"));

List<ForeignKey> foreignKeys = result.computeIfAbsent(foreignKey.getColumnName(), k -> new ArrayList<>());
Set<ForeignKey> foreignKeys = result.computeIfAbsent(foreignKey.getColumnName(), k -> new HashSet<>());
foreignKeys.add(foreignKey);
}

return result;
return result.entrySet()
.stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> new ArrayList<>(entry.getValue())
));
}

private Map<String, Integer> extractPrimaryKeys(java.sql.Connection connection, Table table) throws SQLException {
Expand Down

0 comments on commit 1de0075

Please sign in to comment.