mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
FIX: M2M Where Formula joins
This commit is contained in:
@@ -456,7 +456,8 @@ public class BeanProperty implements ElPropertyValue, Property, STreeProperty {
|
||||
@Override
|
||||
public void appendFrom(DbSqlContext ctx, SqlJoinType joinType, String manyWhere) {
|
||||
if (formula && sqlFormulaJoin != null) {
|
||||
ctx.appendFormulaJoin(sqlFormulaJoin, joinType, manyWhere);
|
||||
String alias = ctx.tableAliasManyWhere(manyWhere);
|
||||
ctx.appendFormulaJoin(sqlFormulaJoin, joinType, alias);
|
||||
} else if (secondaryTableJoin != null) {
|
||||
String relativePrefix = ctx.relativePrefix(secondaryTableJoinPrefix);
|
||||
secondaryTableJoin.addJoin(joinType, relativePrefix, ctx);
|
||||
|
||||
@@ -613,12 +613,24 @@ public class BeanPropertyAssocOne<T> extends BeanPropertyAssoc<T> implements STr
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add table join with explicit table alias.
|
||||
*/
|
||||
@Override
|
||||
public SqlJoinType addJoin(SqlJoinType joinType, String a1, String a2, DbSqlContext ctx) {
|
||||
if (sqlFormulaJoin != null) {
|
||||
ctx.appendFormulaJoin(sqlFormulaJoin, joinType, a1);
|
||||
}
|
||||
return super.addJoin(joinType, a1, a2, ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendFrom(DbSqlContext ctx, SqlJoinType joinType, String manyWhere) {
|
||||
if (!isTransient && !primaryKeyExport) {
|
||||
localHelp.appendFrom(ctx, joinType);
|
||||
if (sqlFormulaJoin != null) {
|
||||
ctx.appendFormulaJoin(sqlFormulaJoin, joinType, manyWhere);
|
||||
String alias = ctx.tableAliasManyWhere(manyWhere);
|
||||
ctx.appendFormulaJoin(sqlFormulaJoin, joinType, alias);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ public interface DbSqlContext {
|
||||
* Append a Sql Formula join. This converts the "${ta}" keyword to the current
|
||||
* table alias.
|
||||
*/
|
||||
void appendFormulaJoin(String sqlFormulaJoin, SqlJoinType joinType, String manyWhere);
|
||||
void appendFormulaJoin(String sqlFormulaJoin, SqlJoinType joinType, String tableAlias);
|
||||
|
||||
/**
|
||||
* Return the current content length.
|
||||
|
||||
@@ -197,7 +197,7 @@ final class DefaultDbSqlContext implements DbSqlContext {
|
||||
|
||||
@Override
|
||||
public String tableAliasManyWhere(String prefix) {
|
||||
return alias.tableAliasManyWhere(prefix);
|
||||
return prefix == null ? tableAliasStack.peek() : alias.tableAliasManyWhere(prefix);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -225,9 +225,8 @@ final class DefaultDbSqlContext implements DbSqlContext {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendFormulaJoin(String sqlFormulaJoin, SqlJoinType joinType, String manyWhere) {
|
||||
public void appendFormulaJoin(String sqlFormulaJoin, SqlJoinType joinType, String tableAlias) {
|
||||
// replace ${ta} placeholder with the real table alias...
|
||||
String tableAlias = manyWhere == null ? tableAliasStack.peek() : tableAliasManyWhere(manyWhere);
|
||||
String converted = sqlFormulaJoin.replace(tableAliasPlaceHolder, tableAlias);
|
||||
if (formulaJoins == null) {
|
||||
formulaJoins = new HashSet<>();
|
||||
@@ -240,11 +239,17 @@ final class DefaultDbSqlContext implements DbSqlContext {
|
||||
formulaJoins.add(converted);
|
||||
sb.append(" ");
|
||||
if (joinType == SqlJoinType.OUTER) {
|
||||
if ("join".equalsIgnoreCase(sqlFormulaJoin.substring(0, 4))) {
|
||||
if ("join".equalsIgnoreCase(converted.substring(0, 4))) {
|
||||
// prepend left as we are in the 'many' part
|
||||
sb.append("left ");
|
||||
}
|
||||
}
|
||||
if (joinType == SqlJoinType.INNER) {
|
||||
if ("left join".equalsIgnoreCase(converted.substring(0, 9))) {
|
||||
// remove left as we do not need it
|
||||
converted = converted.substring(5);
|
||||
}
|
||||
}
|
||||
sb.append(converted);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user