1 19 20 package org.apache.cayenne.query; 21 22 import java.util.Iterator ; 23 24 import org.apache.cayenne.CayenneRuntimeException; 25 import org.apache.cayenne.exp.Expression; 26 import org.apache.cayenne.map.EntityInheritanceTree; 27 import org.apache.cayenne.map.EntityResolver; 28 import org.apache.cayenne.map.ObjEntity; 29 import org.apache.cayenne.map.ObjRelationship; 30 31 37 class SelectQueryPrefetchRouterAction implements PrefetchProcessor { 38 39 SelectQuery query; 40 QueryRouter router; 41 EntityResolver resolver; 42 ObjEntity entity; 43 EntityInheritanceTree inheritanceTree; 44 45 48 void route(SelectQuery query, QueryRouter router, EntityResolver resolver) { 49 if (!query.isFetchingDataRows() && query.getPrefetchTree() != null) { 50 51 this.query = query; 52 this.router = router; 53 this.resolver = resolver; 54 this.entity = query.getMetaData(resolver).getObjEntity(); 55 this.inheritanceTree = resolver.lookupInheritanceTree(entity); 56 57 query.getPrefetchTree().traverse(this); 58 } 59 } 60 61 public boolean startPhantomPrefetch(PrefetchTreeNode node) { 62 return true; 63 } 64 65 public boolean startDisjointPrefetch(PrefetchTreeNode node) { 66 if (node == query.getPrefetchTree()) { 68 return true; 69 } 70 71 String prefetchPath = node.getPath(); 72 73 Iterator it = entity.resolvePathComponents(prefetchPath); 75 76 ObjRelationship relationship = null; 77 while (it.hasNext()) { 78 relationship = (ObjRelationship) it.next(); 79 } 80 81 if (relationship == null) { 82 throw new CayenneRuntimeException("Invalid prefetch '" 83 + prefetchPath 84 + "' for entity: " 85 + entity.getName()); 86 } 87 88 Expression queryQualifier = query.getQualifier(); 90 91 Expression entityQualifier = (inheritanceTree != null) ? inheritanceTree 92 .qualifierForEntityAndSubclasses() : entity.getDeclaredQualifier(); 93 94 if (entityQualifier != null) { 95 queryQualifier = (queryQualifier != null) ? queryQualifier 96 .andExp(entityQualifier) : entityQualifier; 97 } 98 99 PrefetchSelectQuery prefetchQuery = new PrefetchSelectQuery( 101 query, 102 prefetchPath, 103 relationship); 104 105 prefetchQuery.setQualifier(entity.translateToRelatedEntity( 106 queryQualifier, 107 prefetchPath)); 108 109 if (relationship.isFlattened() 112 || (relationship.isToMany() && relationship.getReverseRelationship() == null)) { 113 114 prefetchQuery.addResultPath("db:" 115 + relationship.getReverseDbRelationshipPath()); 116 } 117 118 prefetchQuery.setPrefetchTree(node); 120 121 prefetchQuery.route(router, resolver, null); 123 return true; 124 } 125 126 public boolean startJointPrefetch(PrefetchTreeNode node) { 127 return true; 129 } 130 131 public boolean startUnknownPrefetch(PrefetchTreeNode node) { 132 if (node == query.getPrefetchTree()) { 134 return true; 135 } 136 137 return startDisjointPrefetch(node); 139 } 140 141 public void finishPrefetch(PrefetchTreeNode node) { 142 } 143 } 144 | Popular Tags |