[#1460] Table.getReferencesTo(Table) doesn't work correctly for aliased

tables - Canonical implementation
This commit is contained in:
Lukas Eder 2012-05-28 11:24:38 +02:00
parent 531c5486bd
commit f65f05e8d5
4 changed files with 50 additions and 2 deletions

View File

@ -658,17 +658,24 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
assertEquals(0, a.getReferences().size());
assertEquals(Arrays.asList(), a.getReferencesTo(b));
// This should work with both types of meta-models (static, non-static)
assertEquals(TBook().getReferencesTo(TAuthor()), TBook().getReferencesTo(a));
assertEquals(TBook().getReferencesTo(TAuthor()), b.getReferencesTo(a));
assertEquals(TBook().getReferencesTo(TAuthor()), b.getReferencesTo(TAuthor()));
// Only with a non-static meta model
if (a instanceof UpdatableTable && b instanceof UpdatableTable) {
UpdatableTable<A> ua = (UpdatableTable<A>) a;
UpdatableTable<B> ub = (UpdatableTable<B>) b;
assertEquals(2, ua.getMainKey().getReferences().size());
assertEquals(b, ua.getMainKey().getReferences().get(0).getTable());
assertEquals(b, ua.getMainKey().getReferences().get(1).getTable());
assertEquals(TBook(), ua.getMainKey().getReferences().get(0).getTable());
assertEquals(TBook(), ua.getMainKey().getReferences().get(1).getTable());
assertTrue(b.getReferences().containsAll(ua.getReferencesFrom(b)));
assertTrue(b.getReferences().containsAll(ub.getReferencesFrom(a)));
assertEquals(b.getReferencesTo(a), ua.getReferencesFrom(b));
assertEquals(TBook().getReferencesTo(a), ua.getReferencesFrom(b));
assertEquals(b.getReferencesTo(a), TAuthor().getReferencesFrom(b));
}
}
else {

View File

@ -121,6 +121,25 @@ abstract class AbstractTable<R extends Record> extends AbstractType<R> implement
if (other.equals(reference.getKey().getTable())) {
result.add((ForeignKey<R, O>) reference);
}
// TODO: Refactor the following two blocks and make things more OO
// [#1460] In case the other table was aliased using
else if (other instanceof TableImpl) {
Table<O> aliased = ((TableImpl<O>) other).getAliasedTable();
if (aliased != null && aliased.equals(reference.getKey().getTable())) {
result.add((ForeignKey<R, O>) reference);
}
}
// [#1460] In case the other table was aliased using
else if (other instanceof TableAlias) {
Table<O> aliased = ((TableAlias<O>) other).getAliasedTable();
if (aliased != null && aliased.equals(reference.getKey().getTable())) {
result.add((ForeignKey<R, O>) reference);
}
}
}
return Collections.unmodifiableList(result);

View File

@ -66,6 +66,17 @@ class TableAlias<R extends Record> extends AbstractTable<R> {
this.aliasProvider = new AliasProviderImpl<Table<R>>(table, alias, wrapInParentheses);
}
/**
* Get the aliased table wrapped by this table
*/
Table<R> getAliasedTable() {
if (aliasProvider != null) {
return aliasProvider.getAliasProvider();
}
return null;
}
@Override
public final List<ForeignKey<R, ?>> getReferences() {
return aliasProvider.getAliasProvider().getReferences();

View File

@ -80,6 +80,17 @@ public class TableImpl<R extends Record> extends AbstractTable<R> {
}
}
/**
* Get the aliased table wrapped by this table
*/
Table<R> getAliasedTable() {
if (alias != null) {
return alias.getAliasProvider();
}
return null;
}
@Override
public final List<Attachable> getAttachables0() {
if (alias != null) {