diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java index 229232144e9..43c940063a1 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/SearchBuilder.java @@ -129,7 +129,6 @@ public class SearchBuilder implements ISearchBuilder { private BaseHapiFhirDao myCallingDao; private SearchParameterMap myParams; private String myResourceName; - private AbstractQuery myResourceTableQuery; private Class myResourceType; private String mySearchUuid; private int myFetchSize; @@ -231,8 +230,7 @@ public class SearchBuilder implements ISearchBuilder { assert !theCount; outerQuery = myBuilder.createQuery(Long.class); - myResourceTableQuery = outerQuery; - myQueryRoot.push(myResourceTableQuery.from(ResourceTable.class)); + myQueryRoot.push(outerQuery); if (theCount) { outerQuery.multiselect(myBuilder.countDistinct(myQueryRoot.getRoot())); } else { @@ -249,8 +247,7 @@ public class SearchBuilder implements ISearchBuilder { } else { outerQuery = myBuilder.createQuery(Long.class); - myResourceTableQuery = outerQuery; - myQueryRoot.push(myResourceTableQuery.from(ResourceTable.class)); + myQueryRoot.push(outerQuery); if (theCount) { outerQuery.multiselect(myBuilder.countDistinct(myQueryRoot.getRoot())); } else { @@ -327,7 +324,7 @@ public class SearchBuilder implements ISearchBuilder { List lastUpdatedPredicates = createLastUpdatedPredicates(lu, myBuilder, myQueryRoot.getRoot()); myQueryRoot.addPredicates(lastUpdatedPredicates); - myResourceTableQuery.where(myBuilder.and(myQueryRoot.getPredicateArray())); + myQueryRoot.where(myBuilder.and(myQueryRoot.getPredicateArray())); /* * Now perform the search @@ -895,10 +892,6 @@ public class SearchBuilder implements ISearchBuilder { return myQueryRoot; } - public AbstractQuery getResourceTableQuery() { - return myResourceTableQuery; - } - public Class getResourceType() { return myResourceType; } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/BasePredicateBuilder.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/BasePredicateBuilder.java index f0689df948d..6889f275466 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/BasePredicateBuilder.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/BasePredicateBuilder.java @@ -37,7 +37,6 @@ abstract class BasePredicateBuilder { final QueryRoot myQueryRoot; final Class myResourceType; final String myResourceName; - final AbstractQuery myResourceTableQuery; final SearchParameterMap myParams; // FIXME KHS autowire with lookup @@ -47,7 +46,6 @@ abstract class BasePredicateBuilder { myQueryRoot = theSearchBuilder.getQueryRoot(); myResourceType = theSearchBuilder.getResourceType(); myResourceName = theSearchBuilder.getResourceName(); - myResourceTableQuery = theSearchBuilder.getResourceTableQuery(); myParams = theSearchBuilder.getParams(); } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/PredicateBuilderReference.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/PredicateBuilderReference.java index 75006b03623..68e9618b26e 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/PredicateBuilderReference.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/PredicateBuilderReference.java @@ -363,7 +363,7 @@ class PredicateBuilderReference extends BasePredicateBuilder { } Subquery createLinkSubquery(boolean theFoundChainMatch, String theChain, String theSubResourceName, List theOrValues, RequestDetails theRequest) { - Subquery subQ = myResourceTableQuery.subquery(Long.class); + Subquery subQ = myQueryRoot.subquery(Long.class); Root subQfrom = subQ.from(ResourceTable.class); subQ.select(subQfrom.get("myId").as(Long.class)); @@ -376,7 +376,7 @@ class PredicateBuilderReference extends BasePredicateBuilder { * stack and run a subquery */ - myQueryRoot.push(subQfrom); + myQueryRoot.push(subQ); // FIXME KHS stack in all predicates // Create the subquery predicates diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/PredicateBuilderTag.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/PredicateBuilderTag.java index 5799c1cb614..aaf157de92f 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/PredicateBuilderTag.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/PredicateBuilderTag.java @@ -135,7 +135,7 @@ class PredicateBuilderTag extends BasePredicateBuilder { if (paramInverted) { ourLog.debug("Searching for _tag:not"); - Subquery subQ = myResourceTableQuery.subquery(Long.class); + Subquery subQ = myQueryRoot.subquery(Long.class); Root subQfrom = subQ.from(ResourceTag.class); subQ.select(subQfrom.get("myResourceId").as(Long.class)); diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/QueryRoot.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/QueryRoot.java index ae01d24a80e..d5e63b3e7e3 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/QueryRoot.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/QueryRoot.java @@ -10,8 +10,8 @@ import java.util.Stack; public class QueryRoot { private final Stack myQueryRootStack = new Stack<>(); - public void push(Root theResourceTableRoot) { - myQueryRootStack.push(new QueryRootEntry(theResourceTableRoot)); + public void push(AbstractQuery theResourceTableQuery) { + myQueryRootStack.push(new QueryRootEntry(theResourceTableQuery)); } private QueryRootEntry top() { @@ -62,4 +62,12 @@ public class QueryRoot { List getPredicates() { return top().getPredicates(); } + + public void where(Predicate theAnd) { + top().where(theAnd); + } + + Subquery subquery(Class theClass) { + return top().subquery(theClass); + } } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/QueryRootEntry.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/QueryRootEntry.java index f0a84adbb23..4d65ce27cd2 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/QueryRootEntry.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/predicate/QueryRootEntry.java @@ -8,12 +8,14 @@ import java.util.ArrayList; import java.util.List; public class QueryRootEntry { + private final AbstractQuery myResourceTableQuery; private final Root myResourceTableRoot; private final ArrayList myPredicates = new ArrayList<>(); private final IndexJoins myIndexJoins = new IndexJoins(); - public QueryRootEntry(Root theResourceTableRoot) { - myResourceTableRoot = theResourceTableRoot; + public QueryRootEntry(AbstractQuery theResourceTableQuery) { + myResourceTableQuery = theResourceTableQuery; + myResourceTableRoot = theResourceTableQuery.from(ResourceTable.class); } public Root getRoot() { @@ -56,4 +58,12 @@ public class QueryRootEntry { List getPredicates() { return myPredicates; } + + public void where(Predicate theAnd) { + myResourceTableQuery.where(theAnd); + } + + Subquery subquery(Class theClass) { + return myResourceTableQuery.subquery(theClass); + } }