/** * @return an empty String if there is no join clause, in other cases a String like the following * is returned " as e left join e.bank as alias_1" */ public String getJoinClause() { if (joinClause != null) { return joinClause; } // make sure that the join clauses are computed getWhereClause(); getOrderByClause(); if (getMainAlias() == null) { return ""; } final StringBuilder sb = new StringBuilder(); sb.append(" as " + getMainAlias() + " "); for (JoinDefinition joinDefinition : joinDefinitions) { sb.append(joinDefinition.getJoinStatement()); } sb.append(" "); joinClause = sb.toString(); return joinClause; }
// Resolves the list of properties against existing join definitions // creates new join definitions when necessary private String resolveJoins(List<Property> props, String originalPath) { String alias = getMainAlias(); if (alias == null) { return originalPath; } int index = 0; int joinedPropertyIndex = -1; for (Property prop : props) { boolean found = false; for (JoinDefinition joinDefinition : joinDefinitions) { if (joinDefinition.appliesTo(alias, prop)) { alias = joinDefinition.getJoinAlias(); joinedPropertyIndex = index; found = true; break; } } if (!found) { // no more joins, leave break; } index++; } // check if any new JoinDefinitions should be created for (int i = (joinedPropertyIndex + 1); i < props.size(); i++) { final Property prop = props.get(i); if (prop.isPrimitive()) { break; } // a joinable property final JoinDefinition joinDefinition = new JoinDefinition(); joinDefinition.setOwnerAlias(alias); joinDefinition.setJoinAlias(getNewUniqueAlias()); joinDefinition.setProperty(prop); joinDefinitions.add(joinDefinition); // move the result up to use the new JoinDefinition alias = joinDefinition.getJoinAlias(); joinedPropertyIndex = i; } if (joinedPropertyIndex == (props.size() - 1)) { return alias; } return alias + "." + props.get(props.size() - 1).getName(); }