[#1502] Support implicit self joins, generating aliases for implicit join path

This commit is contained in:
lukaseder 2018-02-09 12:57:00 +01:00
parent 967560fb1b
commit 8e34cd0417
3 changed files with 19 additions and 2 deletions

View File

@ -738,7 +738,7 @@ abstract class AbstractContext<C extends Context<C>> extends AbstractScope imple
Table<?> result = table;
for (Entry<ForeignKey<?, ?>, JoinNode> e : children.entrySet())
result = result.leftJoin(e.getValue().table).onKey(e.getKey());
result = result.leftJoin(e.getValue().joinTree()).onKey(e.getKey());
return result;
}

View File

@ -208,6 +208,8 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
next = new JoinNode(t);
childNode.children.put(k, next);
}
childNode = next;
}
}
}

View File

@ -156,7 +156,22 @@ public class TableImpl<R extends Record> extends AbstractTable<R> {
}
public <O extends Record> TableImpl(Table<O> child, ForeignKey<O, R> path, Table<R> parent) {
this(parent.getQualifiedName(), parent.getSchema(), child, path, null, null, DSL.comment(parent.getComment()));
this(pathAlias(child, path), null, child, path, parent, null, DSL.comment(parent.getComment()));
}
private static final Name pathAlias(Table<?> child, ForeignKey<?, ?> path) {
Name name = DSL.name(path.getName());
if (child instanceof TableImpl) {
Table<?> ancestor = ((TableImpl<?>) child).child;
if (ancestor != null)
name = pathAlias(ancestor, ((TableImpl<?>) child).childPath).append(name);
else
name = child.getQualifiedName().append(name);
}
return DSL.name("alias_" + Tools.hash(name));
}
public <O extends Record> TableImpl(Name name, Schema schema, Table<O> child, ForeignKey<O, R> path, Table<R> aliased, Field<?>[] parameters, Comment comment) {