[#1502] Support implicit join on aliased root table
This commit is contained in:
parent
92cfbbb90c
commit
967560fb1b
@ -102,6 +102,7 @@ abstract class AbstractContext<C extends Context<C>> extends AbstractScope imple
|
||||
String stringLiteralEscapedApos = "'";
|
||||
int index;
|
||||
int scopeLevel = -1;
|
||||
int scopeMarking;
|
||||
final ScopeStack scopeStack;
|
||||
|
||||
// [#2665] VisitListener API
|
||||
@ -522,7 +523,11 @@ abstract class AbstractContext<C extends Context<C>> extends AbstractScope imple
|
||||
}
|
||||
|
||||
@Override
|
||||
public /* non-final */ C scopeMarkStart(QueryPart part) {
|
||||
public final C scopeMarkStart(QueryPart part) {
|
||||
if (scopeLevel >= 0 && scopeMarking == 0)
|
||||
scopeMarkStart0(part);
|
||||
|
||||
scopeMarking++;
|
||||
return (C) this;
|
||||
}
|
||||
|
||||
@ -532,7 +537,11 @@ abstract class AbstractContext<C extends Context<C>> extends AbstractScope imple
|
||||
}
|
||||
|
||||
@Override
|
||||
public /* non-final */ C scopeMarkEnd(QueryPart part) {
|
||||
public final C scopeMarkEnd(QueryPart part) {
|
||||
if (scopeLevel >= 0 && scopeMarking == 1)
|
||||
scopeMarkEnd0(part);
|
||||
|
||||
scopeMarking--;
|
||||
return (C) this;
|
||||
}
|
||||
|
||||
@ -546,6 +555,8 @@ abstract class AbstractContext<C extends Context<C>> extends AbstractScope imple
|
||||
}
|
||||
|
||||
void scopeStart0() {}
|
||||
void scopeMarkStart0(QueryPart part) {}
|
||||
void scopeMarkEnd0(QueryPart part) {}
|
||||
void scopeEnd0() {}
|
||||
|
||||
@Override
|
||||
@ -731,6 +742,11 @@ abstract class AbstractContext<C extends Context<C>> extends AbstractScope imple
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return joinTree().toString();
|
||||
}
|
||||
}
|
||||
|
||||
static class ScopeStackElement {
|
||||
|
||||
@ -201,9 +201,6 @@ final class Alias<Q extends QueryPart> extends AbstractQueryPart {
|
||||
toSQLWrapped(context);
|
||||
}
|
||||
|
||||
if (wrapped instanceof Table)
|
||||
context.scopeMarkEnd(wrapping);
|
||||
|
||||
// [#291] some aliases cause trouble, if they are not explicitly marked using "as"
|
||||
toSQLAs(context);
|
||||
|
||||
@ -244,6 +241,9 @@ final class Alias<Q extends QueryPart> extends AbstractQueryPart {
|
||||
}
|
||||
}
|
||||
|
||||
if (wrapped instanceof Table)
|
||||
context.scopeMarkEnd(wrapping);
|
||||
|
||||
context.declareAliases(true);
|
||||
}
|
||||
|
||||
|
||||
@ -167,26 +167,15 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public RenderContext scopeMarkStart(QueryPart part) {
|
||||
if (scopeLevel >= 0) {
|
||||
ScopeStackElement e = scopeStack.get(part);
|
||||
if (e.positions == null)
|
||||
e.positions = new int[2];
|
||||
|
||||
e.positions[0] = sql.length();
|
||||
}
|
||||
|
||||
return this;
|
||||
void scopeMarkStart0(QueryPart part) {
|
||||
ScopeStackElement e = scopeStack.get(part);
|
||||
e.positions = new int[] { sql.length(), -1 };
|
||||
}
|
||||
|
||||
@Override
|
||||
public RenderContext scopeMarkEnd(QueryPart part) {
|
||||
if (scopeLevel >= 0) {
|
||||
ScopeStackElement e = scopeStack.get(part);
|
||||
e.positions[1] = sql.length();
|
||||
}
|
||||
|
||||
return this;
|
||||
void scopeMarkEnd0(QueryPart part) {
|
||||
ScopeStackElement e = scopeStack.get(part);
|
||||
e.positions[1] = sql.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -200,7 +189,7 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
|
||||
List<Table<?>> tables = new ArrayList<Table<?>>();
|
||||
|
||||
while (root instanceof TableImpl && (child = ((TableImpl<?>) root).child) != null) {
|
||||
keys.add(((TableImpl<?>) root).path);
|
||||
keys.add(((TableImpl<?>) root).childPath);
|
||||
tables.add(root);
|
||||
root = child;
|
||||
}
|
||||
@ -226,6 +215,7 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
void scopeEnd0() {
|
||||
outer:
|
||||
@ -233,7 +223,7 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
|
||||
if (e1.positions == null || e1.joinNode == null)
|
||||
continue outer;
|
||||
|
||||
String replaced = "(" + DSL.using(configuration).render(e1.joinNode.joinTree()) + ")";
|
||||
String replaced = "(" + DSL.using(configuration).renderContext().declareTables(true).render(e1.joinNode.joinTree()) + ")";
|
||||
sql.replace(e1.positions[0], e1.positions[1], replaced);
|
||||
|
||||
int shift = replaced.length() - (e1.positions[1] - e1.positions[0]);
|
||||
|
||||
@ -80,7 +80,7 @@ public class TableImpl<R extends Record> extends AbstractTable<R> {
|
||||
|
||||
protected final Field<?>[] parameters;
|
||||
final Table<?> child;
|
||||
final ForeignKey<?, R> path;
|
||||
final ForeignKey<?, R> childPath;
|
||||
|
||||
/**
|
||||
* @deprecated - 3.10 - [#5996] - Use {@link #TableImpl(Name)} instead (or
|
||||
@ -164,7 +164,7 @@ public class TableImpl<R extends Record> extends AbstractTable<R> {
|
||||
|
||||
this.fields = new Fields<R>();
|
||||
this.child = child;
|
||||
this.path = path;
|
||||
this.childPath = path;
|
||||
|
||||
if (aliased != null) {
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user