[jOOQ/jOOQ#13639] Fix ambiguities of to-many and to-one self path joins

This commit is contained in:
Lukas Eder 2023-05-01 19:14:49 +02:00
parent ffb9414667
commit 55a74c04d7
5 changed files with 21 additions and 13 deletions

View File

@ -37,14 +37,9 @@
*/
package org.jooq;
import java.util.Collection;
import java.util.List;
import org.jooq.exception.DataAccessException;
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* An <code>InverseForeignKey</code> is an inverse {@link ForeignKey}. It

View File

@ -49,6 +49,7 @@ import static org.jooq.JoinType.LEFT_OUTER_JOIN;
import static org.jooq.conf.InvocationOrder.REVERSE;
import static org.jooq.conf.ParamType.INDEXED;
import static org.jooq.impl.DSL.noCondition;
import static org.jooq.impl.JoinTable.onKey0;
import static org.jooq.impl.Tools.DATAKEY_RESET_IN_SUBQUERY_SCOPE;
import static org.jooq.impl.Tools.EMPTY_CLAUSE;
import static org.jooq.impl.Tools.EMPTY_QUERYPART;
@ -1209,7 +1210,10 @@ abstract class AbstractContext<C extends Context<C>> extends AbstractScope imple
// [#14985] Once explicit join paths stabilise, it should be possible
// to omit the ON clause here, and have it generated in JoinTable
result = result.join(e.getValue().joinTree(), type).onKey(e.getKey());
Table<?> t = e.getValue().joinTree();
result = result
.join(t, type)
.on(onKey0(e.getKey(), result, t));
}
for (Entry<InverseForeignKey<?, ?>, JoinNode> e : pathsToMany.entrySet()) {
@ -1226,7 +1230,10 @@ abstract class AbstractContext<C extends Context<C>> extends AbstractScope imple
break;
}
result = result.join(e.getValue().joinTree(), type).onKey(e.getKey().getForeignKey());
Table<?> t = e.getValue().joinTree();
result = result
.join(t, type)
.on(onKey0(e.getKey().getForeignKey(), t, result));
}
return result;

View File

@ -292,13 +292,21 @@ public final class Internal {
*/
@NotNull
public static final Name createPathAlias(Table<?> child, ForeignKey<?, ?> path) {
Name name = DSL.name(path.getName());
return createPathAlias(child, path, "");
}
/**
* Factory method for path aliases.
*/
@NotNull
static final Name createPathAlias(Table<?> child, ForeignKey<?, ?> path, String suffix) {
Name name = DSL.name(path.getName() + suffix);
if (child instanceof TableImpl<?> t) {
if (t.childPath != null)
name = createPathAlias(t.path, t.childPath).append(name);
else if (t.parentPath != null)
name = createPathAlias(t.path, t.parentPath.getForeignKey()).append(name);
name = createPathAlias(t.path, t.parentPath.getForeignKey(), ".inverse").append(name);
else
name = child.getQualifiedName().append(name);
}

View File

@ -40,14 +40,12 @@ package org.jooq.impl;
import java.util.List;
import org.jooq.ConstraintEnforcementStep;
import org.jooq.Record;
import org.jooq.ForeignKey;
import org.jooq.InverseForeignKey;
import org.jooq.Record;
import org.jooq.TableField;
import org.jooq.impl.QOM.UTransient;
import org.jetbrains.annotations.NotNull;
/**
* @author Lukas Eder
*/

View File

@ -211,7 +211,7 @@ implements
}
public TableImpl(Table<?> parent, InverseForeignKey<?, R> parentPath, Table<R> child) {
this(createPathAlias(parent, parentPath.getForeignKey()), null, parent, parentPath, child, null, child.getCommentPart());
this(createPathAlias(parent, parentPath.getForeignKey(), ".inverse"), null, parent, parentPath, child, null, child.getCommentPart());
}
public TableImpl(Name name, Schema schema, Table<?> path, ForeignKey<?, R> childPath, Table<R> aliased, Field<?>[] parameters, Comment comment) {