Merge branch 'FOCONIS-extrawhere-on-join'

This commit is contained in:
rob bygrave
2018-11-13 20:51:55 +13:00
4 changed files with 30 additions and 26 deletions
@@ -518,6 +518,17 @@ class SqlTreeNodeBean implements SqlTreeNode {
ctx.append(inheritInfo.getWhere()).append(" ");
}
}
appendExtraWhere(ctx);
for (SqlTreeNode aChildren : children) {
// recursively add to the where clause any
// fixed predicates (extraWhere etc)
aChildren.appendWhere(ctx);
}
}
protected void appendExtraWhere(DbSqlContext ctx) {
if (extraWhere != null) {
if (ctx.length() > 0) {
ctx.append(" and");
@@ -526,12 +537,6 @@ class SqlTreeNodeBean implements SqlTreeNode {
String ew = StringHelper.replaceString(extraWhere, "${ta}", ta);
ctx.append(" ").append(ew).append(" ");
}
for (SqlTreeNode aChildren : children) {
// recursively add to the where clause any
// fixed predicates (extraWhere etc)
aChildren.appendWhere(ctx);
}
}
/**
@@ -612,7 +617,7 @@ class SqlTreeNodeBean implements SqlTreeNode {
return sqlJoinType;
}
private SqlJoinType appendFromAsJoin(DbSqlContext ctx, SqlJoinType joinType) {
protected SqlJoinType appendFromAsJoin(DbSqlContext ctx, SqlJoinType joinType) {
if (nodeBeanProp instanceof STreePropertyAssocMany) {
STreePropertyAssocMany manyProp = (STreePropertyAssocMany) nodeBeanProp;
@@ -41,6 +41,22 @@ final class SqlTreeNodeManyRoot extends SqlTreeNodeBean {
return detailBean;
}
/**
* append extraWhere to the join.
*/
@Override
protected SqlJoinType appendFromAsJoin(DbSqlContext ctx, SqlJoinType joinType) {
SqlJoinType join = super.appendFromAsJoin(ctx, joinType);
super.appendExtraWhere(ctx);
return join;
}
@Override
protected void appendExtraWhere(DbSqlContext ctx) {
// extraWhere is already appended to the tableJoin
}
/**
* Force outer join for everything after the many property.
*/
@@ -65,6 +65,6 @@ public class TestViewBaseEntity extends BaseTestCase {
assertThat(details).isNotEmpty();
}
assertThat(query.getGeneratedSql()).contains("from order_agg_vw t0 left join o_order t1 on t1.id = t0.order_id left join o_customer t3 on t3.id = t1.kcustomer_id left join o_order_detail t2 on t2.order_id = t1.id where t2.id > 0 and t0.order_total > ?");
assertThat(query.getGeneratedSql()).contains("from order_agg_vw t0 left join o_order t1 on t1.id = t0.order_id left join o_customer t3 on t3.id = t1.kcustomer_id left join o_order_detail t2 on t2.order_id = t1.id and t2.id > 0 where t0.order_total > ?");
}
}
@@ -174,24 +174,7 @@ public class TestOuterJoin extends BaseTestCase {
LoggedSql.stop();
// Note: You would expect, that the two lists are equal.
// but there is a @Where(clause = "${ta}.id > 0") on the details
// property, that will filter orders, where details is zero.
// The produced SQL is:
// select * from o_order t0
// join o_customer t2 on t2.id = t0.kcustomer_id
// left join o_order_detail t1 on t1.order_id = t0.id
// where t1.id > 0
// order by t0.id, t1.id asc, t1.order_qty asc, t1.cretime desc; --bind()
//
// I think this is NOT correct as it may exclude orders without details or with negative ID
//
// Correct way could/would be, to add that extra where to the join
//
//
// assertThat(orders2).isEqualTo(orders1);
assertThat(orders2).isEqualTo(orders1);
}
}