Skip to content

Commit

Permalink
Fixed Blazebit#257 - Added fetches attribute that invokes fetch() on …
Browse files Browse the repository at this point in the history
…the query builder. Fix a join manager issue related to MapValueExpressions
  • Loading branch information
beikov committed Mar 21, 2017
1 parent dbea7da commit 35c0aa8
Show file tree
Hide file tree
Showing 53 changed files with 697 additions and 250 deletions.
43 changes: 43 additions & 0 deletions core/api/src/main/java/com/blazebit/persistence/FetchBuilder.java
@@ -0,0 +1,43 @@
/*
* Copyright 2014 - 2017 Blazebit.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blazebit.persistence;

/**
* An interface for builders that support fetching.
*
* @param <X> The concrete builder type
* @author Christian Beikov
* @since 1.2.0
*/
public interface FetchBuilder<X extends FetchBuilder<X>> {

/**
* Adds an implicit join fetch to the query.
*
* @param path The path to join fetch
* @return The query builder for chaining calls
*/
public X fetch(String path);

/**
* Adds an implicit join fetch for every given path to the query.
*
* @param paths The paths to join fetch
* @return The query builder for chaining calls
*/
public X fetch(String... paths);
}
Expand Up @@ -27,7 +27,7 @@
* @author Christian Beikov
* @since 1.1.0
*/
public interface FullQueryBuilder<T, X extends FullQueryBuilder<T, X>> extends QueryBuilder<T, X> {
public interface FullQueryBuilder<T, X extends FullQueryBuilder<T, X>> extends QueryBuilder<T, X>, FetchBuilder<X> {

/**
* Copies this query builder into a new one, using it's projection as an overridable default.
Expand Down Expand Up @@ -142,22 +142,6 @@ public interface FullQueryBuilder<T, X extends FullQueryBuilder<T, X>> extends Q
*/
public X joinDefault(String path, String alias, JoinType type, boolean fetch);

/**
* Adds an implicit join fetch to the query.
*
* @param path The path to join fetch
* @return The query builder for chaining calls
*/
public X fetch(String path);

/**
* Adds an implicit join fetch for every given path to the query.
*
* @param paths The paths to join fetch
* @return The query builder for chaining calls
*/
public X fetch(String... paths);

/**
* Like {@link FullQueryBuilder#join(java.lang.String, java.lang.String, com.blazebit.persistence.JoinType, boolean) } but with
* {@link JoinType#INNER} and fetch set to true.
Expand Down
Expand Up @@ -34,6 +34,7 @@ public interface ObjectBuilder<T> {
* @param selectBuilder The selectBuilder on which to apply the selects
* @param <X> The type of the select builder
*/
// TODO: Create a special subtype "ObjectBuilderTarget" that extends SelectBuilder<X> & FetchBuilder<X>
public <X extends SelectBuilder<X>> void applySelects(X selectBuilder);

/**
Expand Down
Expand Up @@ -92,6 +92,7 @@
import java.util.Date;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -160,7 +161,7 @@ public abstract class AbstractCommonQueryBuilder<QueryResultType, BuilderType, S
protected boolean hasGroupBy = false;
protected boolean needsCheck = true;
// Fetch owner's are evaluated during implicit joining
protected Map<JoinNode, Boolean> fetchOwners = new HashMap<>();
protected Set<JoinNode> nodesToFetch;

private boolean checkSetBuilderEnded = true;
private boolean implicitJoinsApplied = false;
Expand Down Expand Up @@ -1376,12 +1377,16 @@ protected void applyImplicitJoins() {
}

final JoinVisitor joinVisitor = new JoinVisitor(joinManager);
final List<JoinNode> fetchableNodes = new ArrayList<>();
final JoinNodeVisitor joinNodeVisitor = new OnClauseJoinNodeVisitor(joinVisitor) {

@Override
public void visit(JoinNode node) {
super.visit(node);
node.registerDependencies();
if (node.isFetch()) {
fetchableNodes.add(node);
}
}

};
Expand All @@ -1394,10 +1399,36 @@ public void visit(JoinNode node) {
selectManager.acceptVisitor(joinVisitor);
joinVisitor.setJoinRequired(true);

// Only the main query does has fetch owners
// Only the main query has fetch owners
if (isMainQuery) {
fetchOwners.clear();
selectManager.collectFetchOwners(fetchOwners);
StringBuilder sb = null;
Set<JoinNode> fetchOwners = selectManager.collectFetchOwners();
nodesToFetch = new HashSet<>();
// Add all parents of the fetchable nodes to nodesToFetch until the fetch owner is reached
// If we reach a root before a fetch owner, the fetch owner is missing
for (int i = 0; i < fetchableNodes.size(); i++) {
JoinNode fetchableNode = fetchableNodes.get(i);
while (!fetchOwners.contains(fetchableNode)) {
nodesToFetch.add(fetchableNode);
if (fetchableNode.getParent() == null) {
if (sb == null) {
sb = new StringBuilder();
sb.append("Some join nodes specified fetch joining but their fetch owners weren't included in the select clause! Missing fetch owners: [");
} else {
sb.append(", ");
}
sb.append(fetchableNode.getAlias());
break;
}
fetchableNode = fetchableNode.getParent();
}
}
if (sb != null) {
sb.append("]");
throw new IllegalStateException(sb.toString());
}
} else {
nodesToFetch = Collections.emptySet();
}

joinVisitor.setFromClause(ClauseType.WHERE);
Expand Down Expand Up @@ -1826,7 +1857,7 @@ protected void appendSelectClause(StringBuilder sbSelectFrom) {

protected List<String> appendFromClause(StringBuilder sbSelectFrom, boolean externalRepresentation) {
List<String> whereClauseConjuncts = new ArrayList<>();
joinManager.buildClause(sbSelectFrom, EnumSet.noneOf(ClauseType.class), null, false, externalRepresentation, whereClauseConjuncts, explicitVersionEntities, fetchOwners);
joinManager.buildClause(sbSelectFrom, EnumSet.noneOf(ClauseType.class), null, false, externalRepresentation, whereClauseConjuncts, explicitVersionEntities, nodesToFetch);
return whereClauseConjuncts;
}

Expand Down

0 comments on commit 35c0aa8

Please sign in to comment.