public DefaultOrmQuery(Class<T> beanType, EbeanServer server, ExpressionFactory expressionFactory, String query) { this.beanType = beanType; this.server = server; this.expressionFactory = expressionFactory; this.detail = new OrmQueryDetail(); this.name = ""; if (query != null){ setQuery(query); } }
/** * Return true if select all properties was used to ensure the * property invoking a lazy load was included in the query. */ public boolean selectAllForLazyLoadProperty() { if (lazyLoadProperty != null){ if (!detail.containsProperty(lazyLoadProperty)){ detail.select("*"); return true; } } return false; }
public void setFilterMany(String prop, ExpressionList<?> filterMany){ if (filterMany != null){ OrmQueryProperties chunk = detail.getChunk(prop, true); chunk.setFilterMany((SpiExpressionList<?>)filterMany); } }
/** * Return true if this path is a candidate for converting to a query join. */ private boolean isQueryJoinCandidate(String lazyLoadManyPath, OrmQueryProperties chunk) { return chunk.isFetchJoin() && !isLazyLoadManyRoot(lazyLoadManyPath, chunk) && !hasParentSecJoin(lazyLoadManyPath, chunk); }
boolean tuneFetchProperties(OrmQueryDetail tunedDetail) { boolean tuned = false; OrmQueryProperties tunedRoot = tunedDetail.getChunk(null, false); if (tunedRoot != null) { tuned = true; baseProps = tunedRoot; for (OrmQueryProperties tunedChunk : tunedDetail.fetchPaths.values()) { fetch(tunedChunk.copy()); } } return tuned; }
sortFetchPaths(beanDescriptor); && !isLazyLoadManyRoot(lazyLoadManyPath, chunk) && !hasParentSecJoin(lazyLoadManyPath, chunk)) {
public OrmQueryDetail buildTunedFetch(BeanDescriptor<?> rootDesc){ synchronized (monitor) { if (nodeUsageMap.isEmpty()){ return null; } PathProperties pathProps = new PathProperties(); Iterator<StatisticsNodeUsage> it = nodeUsageMap.values().iterator(); while (it.hasNext()) { StatisticsNodeUsage statsNode = it.next(); statsNode.buildTunedFetch(pathProps, rootDesc); } OrmQueryDetail detail = new OrmQueryDetail(); Collection<Props> pathProperties = pathProps.getPathProps(); for (Props props : pathProperties) { if (!props.isEmpty()){ detail.addFetch(props.getPath(), props.getPropertiesAsString(), null); } } detail.sortFetchPaths(rootDesc); return detail; } }
/** * If the chunk has a parent that is a query or lazy join. In this case it * does not need to be converted. */ private boolean hasParentSecJoin(String lazyLoadManyPath, OrmQueryProperties chunk) { OrmQueryProperties parent = getParent(chunk); if (parent == null) { return false; } else { if (lazyLoadManyPath != null && lazyLoadManyPath.equals(parent.getPath())) { return false; } else if (!parent.isFetchJoin()) { return true; } else { return hasParentSecJoin(lazyLoadManyPath, parent); } } }
OrmQueryDetail detail = new OrmQueryDetail(); String path = pathIt.next(); Set<String> props = pathProps.get(path); detail.getChunk(path, true).setDefaultProperties(null, props);
private OrmQueryDetail buildDetail(BeanDescriptor<?> rootDesc) { PathProperties pathProps = new PathProperties(); for (ProfileOriginNodeUsage statsNode : nodeUsageMap.values()) { statsNode.buildTunedFetch(pathProps, rootDesc, queryTuningAddVersion); } OrmQueryDetail detail = new OrmQueryDetail(); Collection<Props> pathProperties = pathProps.getPathProps(); for (Props props : pathProperties) { if (!props.isEmpty()) { detail.fetch(props.getPath(), props.getPropertiesAsString(), null); } } detail.sortFetchPaths(rootDesc); return detail; }
/** * Tune the query by replacing its OrmQueryDetail with a tuned one. * * @return true if the query was tuned, otherwise false. */ public boolean tuneQuery(SpiQuery<?> query) { if (tunedDetail == null) { return false; } boolean tuned; if (query.isDetailEmpty()) { tuned = true; // tune by 'replacement' query.setDetail(tunedDetail.copy()); } else { // tune by 'addition' tuned = query.tuneFetchProperties(tunedDetail); } if (tuned) { query.setAutoTuned(true); } return tuned; }
public boolean tuneFetchProperties(OrmQueryDetail tunedDetail) { boolean tuned = false; OrmQueryProperties tunedRoot = tunedDetail.getChunk(null, false); if (tunedRoot != null && tunedRoot.hasProperties()) { tuned = true; baseProps.setTunedProperties(tunedRoot); for (OrmQueryProperties tunedChunk : tunedDetail.fetchPaths.values()) { OrmQueryProperties chunk = getChunk(tunedChunk.getPath(), false); if (chunk != null) { // set the properties to select chunk.setTunedProperties(tunedChunk); } else { // add a missing join putFetchPath(tunedChunk.copy()); } } } return tuned; }
/** * Test to see if we are including this node into the query. * <p> * Return true if this node is FULLY included resulting in table join. If * the node is not included but its parent has been included then a "bean * proxy" is added and false is returned. * </p> */ private boolean isIncludeBean(String prefix, BeanPropertyAssocOne<?> prop) { if (queryDetail.includes(prefix)) { // explicitly included summary.append(", ").append(prefix); String[] splitNames = SplitName.split(prefix); queryDetail.includeBeanJoin(splitNames[0], splitNames[1]); return true; } return false; }
/** * Return true if equal in terms of autoTune (select and fetch without property ordering). */ public boolean isAutoTuneEqual(OrmQueryDetail otherDetail) { if (!isSameByAutoTune(baseProps, otherDetail.baseProps)) { return false; } if (fetchPaths == null) { return otherDetail.fetchPaths == null; } if (fetchPaths.size() != otherDetail.fetchPaths.size()) { return false; } // check without regard to ordering for (Map.Entry<String, OrmQueryProperties> entry : fetchPaths.entrySet()) { OrmQueryProperties chunk = otherDetail.getChunk(entry.getKey(), false); if (!isSameByAutoTune(entry.getValue(), chunk)) { return false; } } return true; }
/** * Return true if the query detail has neither select or joins specified. */ @Override public boolean isDetailEmpty() { return detail.isEmpty(); }
/** * Set the select clause to select the Id property. */ public void setSelectId() { // clear select and fetch joins.. detail.clear(); select(beanDescriptor.getIdBinder().getIdProperty()); }
/** * Test to see if we are including this node into the query. * <p> * Return true if this node is FULLY included resulting in table join. If the * node is not included but its parent has been included then a "bean proxy" * is added and false is returned. * </p> */ private boolean isIncludeBean(String prefix) { if (queryDetail.includesPath(prefix)) { // explicitly included summary.append(", ").append(prefix); String[] splitNames = SplitName.split(prefix); queryDetail.includeBeanJoin(splitNames[0], splitNames[1]); return true; } return false; }
/** * Set any default select clauses for the main bean and any joins that have not explicitly defined * a select clause. * <p> * That is this will use FetchType.LAZY to exclude some properties by default. * </p> */ public void setDefaultSelectClause(BeanDescriptor<?> desc) { if (desc.hasDefaultSelectClause() && !hasSelectClause()) { baseProps = new OrmQueryProperties(null, desc.getDefaultSelectClause()); } for (OrmQueryProperties joinProps : fetchPaths.values()) { if (!joinProps.hasSelectClause()) { BeanDescriptor<?> assocDesc = desc.getBeanDescriptor(joinProps.getPath()); if (assocDesc.hasDefaultSelectClause()) { fetch(joinProps.getPath(), assocDesc.getDefaultSelectClause(), joinProps.getFetchConfig()); } } } }
/** * Set any default select clauses for the main bean and any joins that have * not explicitly defined a select clause. * <p> * That is this will use FetchType.LAZY to exclude some properties by * default. * </p> */ public void setDefaultSelectClause(BeanDescriptor<?> desc) { if (desc.hasDefaultSelectClause() && !hasSelectClause()) { if (baseProps == null) { baseProps = new OrmQueryProperties(); } baseProps.setDefaultProperties(desc.getDefaultSelectClause(), desc.getDefaultSelectClauseSet()); } for (OrmQueryProperties joinProps : fetchPaths.values()) { if (!joinProps.hasSelectClause()) { BeanDescriptor<?> assocDesc = desc.getBeanDescriptor(joinProps.getPath()); if (assocDesc.hasDefaultSelectClause()) { // use the default select clause joinProps.setDefaultProperties(assocDesc.getDefaultSelectClause(), assocDesc.getDefaultSelectClauseSet()); } } } }
OrmQueryDetail detail = new OrmQueryDetail(); detail.fetch(props.getPath(), props.getProperties());