FromElement createCollection( QueryableCollection queryableCollection, String role, int joinType, boolean fetchFlag, boolean indexed) throws SemanticException { if (!collection) { throw new IllegalStateException("FromElementFactory not initialized for collections!"); } this.inElementsFunction = indexed; FromElement elem; this.queryableCollection = queryableCollection; collectionType = queryableCollection.getCollectionType(); String roleAlias = fromClause.getAliasGenerator().createName(role); // Correlated subqueries create 'special' implied from nodes // because correlated subselects can't use an ANSI-style join boolean explicitSubqueryFromElement = fromClause.isSubQuery() && !implied; if (explicitSubqueryFromElement) { String pathRoot = StringHelper.root(path); FromElement origin = fromClause.getFromElement(pathRoot); if (origin == null || origin.getFromClause() != fromClause) { implied = true; } } // super-duper-classic-parser-regression-testing-mojo-magic... if (explicitSubqueryFromElement && DotNode.useThetaStyleImplicitJoins) { implied = true; } Type elementType = queryableCollection.getElementType(); if (elementType.isEntityType()) { // A collection of entities... elem = createEntityAssociation(role, roleAlias, joinType); } else if (elementType.isComponentType()) { // A collection of components... JoinSequence joinSequence = createJoinSequence(roleAlias, joinType); elem = createCollectionJoin(joinSequence, roleAlias); } else { // A collection of scalar elements... JoinSequence joinSequence = createJoinSequence(roleAlias, joinType); elem = createCollectionJoin(joinSequence, roleAlias); } elem.setRole(role); elem.setQueryableCollection(queryableCollection); // Don't include sub-classes for implied collection joins or subquery joins. if (implied) { elem.setIncludeSubclasses(false); } if (explicitSubqueryFromElement) { elem.setInProjectionList(true); // Treat explict from elements in sub-queries properly. } if (fetchFlag) { elem.setFetch(true); } return elem; }
public FromElement createEntityJoin( String entityClass, String tableAlias, JoinSequence joinSequence, boolean fetchFlag, boolean inFrom, EntityType type, String role, String joinPath) throws SemanticException { FromElement elem = createJoin(entityClass, tableAlias, joinSequence, type, false); elem.setFetch(fetchFlag); if (joinPath != null) { elem.applyTreatAsDeclarations(fromClause.getWalker().getTreatAsDeclarationsByPath(joinPath)); } EntityPersister entityPersister = elem.getEntityPersister(); int numberOfTables = entityPersister.getQuerySpaces().length; if (numberOfTables > 1 && implied && !elem.useFromFragment()) { LOG.debug("createEntityJoin() : Implied multi-table entity join"); elem.setUseFromFragment(true); } // If this is an implied join in a FROM clause, then use ANSI-style joining, and set the // flag on the FromElement that indicates that it was implied in the FROM clause itself. if (implied && inFrom) { joinSequence.setUseThetaStyle(false); elem.setUseFromFragment(true); elem.setImpliedInFromClause(true); } if (elem.getWalker().isSubQuery()) { // two conditions where we need to transform this to a theta-join syntax: // 1) 'elem' is the "root from-element" in correlated subqueries // 2) The DotNode.useThetaStyleImplicitJoins has been set to true // and 'elem' represents an implicit join if (elem.getFromClause() != elem.getOrigin().getFromClause() || // ( implied && DotNode.useThetaStyleImplicitJoins ) ) { PathSeparatorNode.useThetaStyleImplicitJoins) { // the "root from-element" in correlated subqueries do need this piece elem.setType(FROM_FRAGMENT); joinSequence.setUseThetaStyle(true); elem.setUseFromFragment(false); } } elem.setRole(role); return elem; }
private void renderNonScalarIdentifiers( FromElement fromElement, int nonscalarSize, int j, SelectExpression expr, ASTAppender appender) { String text = fromElement.renderIdentifierSelect(nonscalarSize, j); if (!fromElement.getFromClause().isSubQuery()) { if (!scalarSelect && !getWalker().isShallowQuery()) { // TODO: is this a bit ugly? expr.setText(text); } else { appender.append(SqlTokenTypes.SQL_TOKEN, text, false); } } }
private FromElement createFromElementInSubselect( String path, String pathAlias, FromElement parentFromElement, String classAlias) throws SemanticException { if (log.isDebugEnabled()) { log.debug("createFromElementInSubselect() : path = " + path); } // Create an DotNode AST for the path and resolve it. FromElement fromElement = evaluateFromElementPath(path, classAlias); EntityPersister entityPersister = fromElement.getEntityPersister(); // If the first identifier in the path referrs to the class alias (not the class name), then // this // is a correlated subselect. If it's a correlated sub-select, use the existing table alias. // Otherwise // generate a new one. String tableAlias = null; boolean correlatedSubselect = pathAlias.equals(parentFromElement.getClassAlias()); if (correlatedSubselect) { tableAlias = fromElement.getTableAlias(); } else { tableAlias = null; } // If the from element isn't in the same clause, create a new from element. if (fromElement.getFromClause() != fromClause) { if (log.isDebugEnabled()) { log.debug("createFromElementInSubselect() : creating a new FROM element..."); } fromElement = createFromElement(entityPersister); initializeAndAddFromElement( fromElement, path, classAlias, entityPersister, (EntityType) ((Queryable) entityPersister).getType(), tableAlias); } if (log.isDebugEnabled()) { log.debug("createFromElementInSubselect() : " + path + " -> " + fromElement); } return fromElement; }