throw new NullPointerException("The entity class for a search cannot be null"); SearchContext ctx = new SearchContext(entityClass, rootAlias, paramList); String prop = fields.get(0).getProperty(); if (prop == null || "".equals(prop)) { sb.append(ctx.getRootAlias()); } else { sb.append(getPathRef(ctx, prop));
SearchContext ctx2 = new SearchContext(); ctx2.rootClass = metadataUtil.get(ctx.rootClass, property).getJavaClass(); ctx2.setRootAlias(rootAlias + (ctx.nextSubqueryNum++)); ctx2.paramList = ctx.paramList; ctx2.nextAliasNum = ctx.nextAliasNum; sb.append(getPathRef(ctx, property)); sb.append(" "); sb.append(ctx2.getRootAlias()); sb.append(joins); sb.append(where);
prop = ctx.getRootAlias(); } else { AliasNode aliasNodeForProperty = getAliasForPathIfItExists(ctx, field.getProperty()); return "select distinct " + ctx.getRootAlias(); else return "select " + ctx.getRootAlias();
/** * Generate the QL string for a given search. Fill paramList with the values * to be used for the query. All parameters within the query string are * specified as named parameters ":pX", where X is the index of the * parameter value in paramList. */ public String generateQL(Class<?> entityClass, ISearch search, List<Object> paramList) { if (entityClass == null) throw new NullPointerException("The entity class for a search cannot be null"); SearchContext ctx = new SearchContext(entityClass, rootAlias, paramList); List<Field> fields = checkAndCleanFields(search.getFields()); applyFetches(ctx, checkAndCleanFetches(search.getFetches()), fields); String select = generateSelectClause(ctx, fields, search.isDistinct()); String where = generateWhereClause(ctx, checkAndCleanFilters(search.getFilters()), search.isDisjunction()); String orderBy = generateOrderByClause(ctx, checkAndCleanSorts(search.getSorts())); String from = generateFromClause(ctx, true); StringBuilder sb = new StringBuilder(); sb.append(select); sb.append(from); sb.append(where); sb.append(orderBy); String query = sb.toString(); if (logger.isDebugEnabled()) logger.debug("generateQL:\n " + query); return query; }
/** * Internal method for generating from clause. This method should be called * after generating other clauses because it relies on the aliases they * create. This method takes every path that is called for in the other * clauses and makes it available as an alias using left joins. It also adds * join fetching for properties specified by <code>fetches</code> if * <code>doEagerFetching</code> is <code>true</code>. <b>NOTE:</b> When * using eager fetching, <code>applyFetches()</code> must be executed first. */ protected String generateFromClause(SearchContext ctx, boolean doEagerFetching) { StringBuilder sb = new StringBuilder(" from "); sb.append(getMetadataUtil().get(ctx.rootClass).getEntityName()); sb.append(" "); sb.append(ctx.getRootAlias()); sb.append(generateJoins(ctx, doEagerFetching)); return sb.toString(); }
/** * Given a full path to a property (ex. department.manager.salary), return * the reference to that property that uses the appropriate alias (ex. * a4_manager.salary). */ protected String getPathRef(SearchContext ctx, String path) { if (path == null || "".equals(path)) { return ctx.getRootAlias(); } String[] parts = splitPath(ctx, path); return getOrCreateAlias(ctx, parts[0], false).alias + "." + parts[1]; }
public SearchContext(Class<?> rootClass, String rootAlias, List<Object> paramList) { this.rootClass = rootClass; setRootAlias(rootAlias); this.paramList = paramList; }