Skip to content

Commit

Permalink
Test fixes for eclipselink and datanucleus
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Apr 14, 2017
1 parent 7ac770d commit f0b9e79
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,30 +210,40 @@ protected String treatRoot(String path, Class<?> type, String property) {
}

protected String treatRootWhereFragment(String alias, Class<?> treatType, String after, boolean negatedContext) {
if (jpaProvider.supportsTreatJoin()) {
return "TREAT(" + alias + " AS " + treatType.getSimpleName() + ")" + after;
} else if (jpaProvider.supportsSubtypePropertyResolving()) {
String operator;
String logicalOperator;
String predicate;
String operator;
String logicalOperator;
String predicate;

if (negatedContext) {
operator = " = ";
logicalOperator = " AND ";
} else {
operator = " <> ";
logicalOperator = " OR ";
}
if (negatedContext) {
operator = " = ";
logicalOperator = " AND ";
} else {
operator = " <> ";
logicalOperator = " OR ";
}

if (jpaProvider.supportsRootTreat()) {
predicate = "TREAT(" + alias + " AS " + treatType.getSimpleName() + ")" + after;
} else if (jpaProvider.supportsSubtypePropertyResolving()) {
predicate = alias + after;
return "(TYPE(" + alias + ")" + operator + treatType.getSimpleName() + logicalOperator + predicate + ")";

} else {
throw new IllegalArgumentException("Treat should not be used as the JPA provider does not support subtype property access!");
}

throw new IllegalArgumentException("Treat should not be used as the JPA provider does not support subtype property access!");
return "(TYPE(" + alias + ")" + operator + treatType.getSimpleName() + logicalOperator + predicate + ")";
}

protected String treatJoinedConstraintFragment(String alias, Class<?> treatType, String after, boolean negatedContext) {
if (jpaProvider.supportsTreatJoin()) {
return alias + after;
}
return treatRootWhereFragment(alias, treatType, after, negatedContext);
}

protected String treatJoinWhereFragment(String alias, Class<?> type, JoinType joinType, String whereFragment) {
if (jpaProvider.supportsTreatJoin() || joinType != JoinType.INNER) {
protected String treatJoinWhereFragment(Class<?> sourceType, String attribute, String alias, Class<?> type, JoinType joinType, String whereFragment) {
JpaProvider.ConstraintType constraintType = jpaProvider.requiresTreatFilter(em.getMetamodel().managedType(sourceType), attribute, joinType);
if (constraintType != JpaProvider.ConstraintType.WHERE) {
return whereFragment == null ? "" : whereFragment;
}
String constraint = "TYPE(" + alias + ") = " + type.getSimpleName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@
import com.blazebit.persistence.testsuite.base.category.NoDatanucleus4;
import com.blazebit.persistence.testsuite.base.category.NoEclipselink;
import com.blazebit.persistence.testsuite.base.category.NoHibernate;
import com.blazebit.persistence.testsuite.base.category.NoHibernate43;
import com.blazebit.persistence.testsuite.base.category.NoHibernate50;
import com.blazebit.persistence.testsuite.base.category.NoHibernate51;
import com.blazebit.persistence.testsuite.base.category.NoHibernate52;
import com.blazebit.persistence.testsuite.entity.*;
import com.googlecode.catchexception.CatchException;
import org.junit.Test;
import org.junit.experimental.categories.Category;

Expand Down Expand Up @@ -179,7 +174,7 @@ public void joinTreatedRelation() {
criteria.from(PolymorphicBase.class, "p");
criteria.select("polymorphicSub1.sub1Value");
criteria.innerJoin("TREAT(p.parent AS PolymorphicSub1)", "polymorphicSub1");
String treatJoinWhereFragment = treatJoinWhereFragment("polymorphicSub1", PolymorphicSub1.class, JoinType.INNER, null);
String treatJoinWhereFragment = treatJoinWhereFragment(PolymorphicBase.class, "parent", "polymorphicSub1", PolymorphicSub1.class, JoinType.INNER, null);
assertEquals("SELECT polymorphicSub1.sub1Value FROM PolymorphicBase p JOIN " + treatJoin("p.parent", PolymorphicSub1.class) + " polymorphicSub1" + treatJoinWhereFragment, criteria.getQueryString());
criteria.getResultList();
}
Expand Down Expand Up @@ -230,7 +225,7 @@ public void treatJoinTreatedRootRelation() {
criteria.from(PolymorphicBase.class, "p");
criteria.select("polymorphicSub1.sub1Value");
criteria.innerJoin("TREAT(TREAT(p AS PolymorphicSub1).parent1 AS PolymorphicSub1)", "polymorphicSub1");
String treatJoinWhereFragment = treatJoinWhereFragment("polymorphicSub1", PolymorphicSub1.class, JoinType.INNER, null);
String treatJoinWhereFragment = treatJoinWhereFragment(PolymorphicSub1.class, "parent1", "polymorphicSub1", PolymorphicSub1.class, JoinType.INNER, null);
assertEquals("SELECT polymorphicSub1.sub1Value FROM PolymorphicBase p "
+ treatRootTreatJoin(JoinType.INNER, "p", PolymorphicSub1.class, "parent1", PolymorphicSub1.class, "polymorphicSub1") + treatJoinWhereFragment, criteria.getQueryString());
criteria.getResultList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import com.blazebit.persistence.CriteriaBuilder;
import com.blazebit.persistence.criteria.impl.BlazeCriteria;
import com.blazebit.persistence.testsuite.AbstractCoreTest;
import com.blazebit.persistence.testsuite.base.category.NoDatanucleus;
import com.blazebit.persistence.testsuite.base.category.NoEclipselink;
import com.blazebit.persistence.testsuite.entity.*;
import com.googlecode.catchexception.CatchException;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import javax.persistence.Tuple;

Expand Down Expand Up @@ -87,6 +90,8 @@ public void treatRoot() {
}

@Test
// Eclipselink does not support dereferencing of TREAT join path elements
@Category({ NoDatanucleus.class, NoEclipselink.class })
public void treatRootJoin() {
BlazeCriteriaQuery<PolymorphicBase> cq = BlazeCriteria.get(em, cbf, PolymorphicBase.class);
BlazeCriteriaBuilder cb = cq.getCriteriaBuilder();
Expand Down Expand Up @@ -132,12 +137,12 @@ public void treatJoin() {

CriteriaBuilder<?> criteriaBuilder = cq.createCriteriaBuilder();
String whereFragment = null;
whereFragment = treatJoinWhereFragment("child", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment("list", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment("map", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment("parent", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment("parent2", PolymorphicSub2.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
assertEquals("SELECT parent.sub1Value, list.sub1Value, child.sub1Value, map.sub1Value, INDEX(list), KEY(map)" +
whereFragment = treatJoinWhereFragment(PolymorphicBase.class, "children", "child", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment(PolymorphicBase.class, "list", "list", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment(PolymorphicBase.class, "map", "map", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment(PolymorphicBase.class, "parent", "parent", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment(PolymorphicBase.class, "parent2", "parent2", PolymorphicSub2.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
assertEquals("SELECT parent.sub1Value, list.sub1Value, child.sub1Value, " + joinAliasValue("map", "sub1Value") + ", INDEX(list), KEY(map)" +
" FROM PolymorphicBase base" +
" JOIN " + treatJoin("base.children", PolymorphicSub1.class) + " child" +
" JOIN " + treatJoin("base.list", PolymorphicSub1.class) + " list" +
Expand All @@ -147,6 +152,8 @@ public void treatJoin() {
}

@Test
// Eclipselink does not support dereferencing of TREAT join path elements
@Category({ NoDatanucleus.class, NoEclipselink.class })
public void joinTreatedJoinWithOnClause() {
BlazeCriteriaQuery<Tuple> cq = BlazeCriteria.get(em, cbf, Tuple.class);
BlazeCriteriaBuilder cb = cq.getCriteriaBuilder();
Expand Down Expand Up @@ -175,27 +182,27 @@ public void joinTreatedJoinWithOnClause() {

CriteriaBuilder<?> criteriaBuilder = cq.createCriteriaBuilder();
String whereFragment = null;
whereFragment = treatJoinWhereFragment("child", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment("list", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment("map", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment("parent", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment("parent2", PolymorphicSub2.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment(PolymorphicBase.class, "children", "child", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment(PolymorphicBase.class, "list", "list", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment(PolymorphicBase.class, "map", "map", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment(PolymorphicBase.class, "parent", "parent", PolymorphicSub1.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
whereFragment = treatJoinWhereFragment(PolymorphicBase.class, "parent2", "parent2", PolymorphicSub2.class, com.blazebit.persistence.JoinType.INNER, whereFragment);
assertEquals("SELECT TYPE(parent), TYPE(list), TYPE(child), TYPE(map), TYPE(KEY(map)), TYPE(parent_1), TYPE(relation1_1) " +
"FROM PolymorphicBase base" +
" JOIN " + treatJoin("base.children", PolymorphicSub1.class) + " child" +
onClause(treatRootWhereFragment("child", PolymorphicSub1.class, ".sub1Value IS NOT NULL", false)) +
onClause(treatJoinedConstraintFragment("child", PolymorphicSub1.class, ".sub1Value IS NOT NULL", false)) +
" JOIN child.relation1 setRelation1" +
" JOIN " + treatJoin("base.list", PolymorphicSub1.class) + " list" +
onClause(treatRootWhereFragment("list", PolymorphicSub1.class, ".sub1Value IS NOT NULL", false)) +
onClause(treatJoinedConstraintFragment("list", PolymorphicSub1.class, ".sub1Value IS NOT NULL", false)) +
" JOIN list.relation1 listRelation1" +
" JOIN " + treatJoin("base.map", PolymorphicSub1.class) + " map" +
onClause(treatRootWhereFragment("map", PolymorphicSub1.class, ".sub1Value IS NOT NULL", false)) +
onClause(treatJoinedConstraintFragment("map", PolymorphicSub1.class, ".sub1Value IS NOT NULL", false)) +
" JOIN map.relation1 mapRelation1" +
" JOIN " + treatJoin("base.parent", PolymorphicSub1.class) + " parent" +
onClause(treatRootWhereFragment("parent", PolymorphicSub1.class, ".sub1Value IS NOT NULL", false)) +
onClause(treatJoinedConstraintFragment("parent", PolymorphicSub1.class, ".sub1Value IS NOT NULL", false)) +
" JOIN parent.relation1 parentRelation1" +
" JOIN " + treatJoin("base.parent", PolymorphicSub1.class) + " parent2" +
onClause(treatRootWhereFragment("parent2", PolymorphicSub2.class, ".sub2Value IS NOT NULL", false)) +
" JOIN " + treatJoin("base.parent", PolymorphicSub2.class) + " parent2" +
onClause(treatJoinedConstraintFragment("parent2", PolymorphicSub2.class, ".sub2Value IS NOT NULL", false)) +
" JOIN parent2.relation2 parent2Relation2" +
" LEFT JOIN base.parent parent_1" +
" LEFT JOIN parent_1.relation1 relation1_1" + whereFragment, criteriaBuilder.getQueryString());
Expand Down

0 comments on commit f0b9e79

Please sign in to comment.