This commit is contained in:
Lukas Eder 2020-07-02 15:40:51 +02:00
parent 5c54196097
commit db093b3fc9
3 changed files with 32 additions and 13 deletions

View File

@ -265,6 +265,8 @@ public interface Table<R extends Record> extends TableLike<R>, Qualified {
/**
* Get a list of <code>FOREIGN KEY</code>'s of a specific table, referencing
* a this table.
* <p>
* This will recurse into joined tables.
*
* @param <O> The other table's record type
* @param other The other table of the foreign key relationship
@ -287,6 +289,8 @@ public interface Table<R extends Record> extends TableLike<R>, Qualified {
/**
* Get a list of <code>FOREIGN KEY</code>'s of this table, referencing a
* specific table.
* <p>
* This will recurse into joined tables.
*
* @param <O> The other table's record type
* @param other The other table of the foreign key relationship

View File

@ -520,22 +520,39 @@ abstract class AbstractTable<R extends Record> extends AbstractNamed implements
List<ForeignKey<R, O>> result = new ArrayList<>();
for (ForeignKey<R, ?> reference : getReferences()) {
if (other.equals(reference.getKey().getTable())) {
result.add((ForeignKey<R, O>) reference);
}
// [#1460] [#6304] In case the other table was aliased
else {
Table<O> aliased = Tools.aliased(other);
if (aliased != null && aliased.equals(reference.getKey().getTable())) {
for (Table<?> o : flattenJoins(other)) {
if (o.equals(reference.getKey().getTable())) {
result.add((ForeignKey<R, O>) reference);
}
// [#1460] [#6304] In case the other table was aliased
else {
Table<?> aliased = Tools.aliased(o);
if (aliased != null && aliased.equals(reference.getKey().getTable()))
result.add((ForeignKey<R, O>) reference);
}
}
}
return Collections.unmodifiableList(result);
}
private final List<Table<?>> flattenJoins(Table<?> other) {
return flattenJoins(other, new ArrayList<>());
}
private final List<Table<?>> flattenJoins(Table<?> other, List<Table<?>> list) {
if (other instanceof JoinTable) {
flattenJoins(((JoinTable) other).lhs, list);
flattenJoins(((JoinTable) other).rhs, list);
}
else
list.add(other);
return list;
}
/**
* {@inheritDoc}
* <p>

View File

@ -645,12 +645,10 @@ implements
List<?> leftToRight = lhs.getReferencesTo(rhs);
List<?> rightToLeft = rhs.getReferencesTo(lhs);
if (leftToRight.size() == 1 && rightToLeft.size() == 0) {
if (leftToRight.size() == 1 && rightToLeft.size() == 0)
return onKey((ForeignKey<?, ?>) leftToRight.get(0), lhs, rhs);
}
else if (rightToLeft.size() == 1 && leftToRight.size() == 0) {
else if (rightToLeft.size() == 1 && leftToRight.size() == 0)
return onKey((ForeignKey<?, ?>) rightToLeft.get(0), rhs, lhs);
}
if (rightToLeft.isEmpty() && leftToRight.isEmpty())
throw onKeyException(OnKeyExceptionReason.NOT_FOUND, leftToRight, rightToLeft);