Search in sources :

Example 21 with FromElement

use of org.hibernate.hql.internal.ast.tree.FromElement in project hibernate-orm by hibernate.

the class JoinProcessor method processJoins.

public void processJoins(QueryNode query) {
    final FromClause fromClause = query.getFromClause();
    final List fromElements;
    if (DotNode.useThetaStyleImplicitJoins) {
        // for regression testing against output from the old parser...
        // found it easiest to simply reorder the FromElements here into ascending order
        // in terms of injecting them into the resulting sql ast in orders relative to those
        // expected by the old parser; this is definitely another of those "only needed
        // for regression purposes". The SyntheticAndFactory, then, simply injects them as it
        // encounters them.
        fromElements = new ArrayList();
        ListIterator liter = fromClause.getFromElements().listIterator(fromClause.getFromElements().size());
        while (liter.hasPrevious()) {
            fromElements.add(liter.previous());
        }
    } else {
        fromElements = new ArrayList(fromClause.getFromElements().size());
        ListIterator<FromElement> liter = fromClause.getFromElements().listIterator();
        while (liter.hasNext()) {
            FromElement fromElement = liter.next();
            // We found an implied from element that is used in the WITH clause of another from element, so it need to become part of it's join sequence
            if (fromElement instanceof ImpliedFromElement && fromElement.getOrigin().getWithClauseFragment() != null && fromElement.getOrigin().getWithClauseFragment().contains(fromElement.getTableAlias())) {
                fromElement.getOrigin().getJoinSequence().addJoin((ImpliedFromElement) fromElement);
                // This from element will be rendered as part of the origins join sequence
                fromElement.setText("");
            } else {
                fromElements.add(fromElement);
            }
        }
    }
    // Iterate through the alias,JoinSequence pairs and generate SQL token nodes.
    Iterator iter = fromElements.iterator();
    while (iter.hasNext()) {
        final FromElement fromElement = (FromElement) iter.next();
        JoinSequence join = fromElement.getJoinSequence();
        join.setSelector(new JoinSequence.Selector() {

            public boolean includeSubclasses(String alias) {
                // The uber-rule here is that we need to include subclass joins if
                // the FromElement is in any way dereferenced by a property from
                // the subclass table; otherwise we end up with column references
                // qualified by a non-existent table reference in the resulting SQL...
                boolean containsTableAlias = fromClause.containsTableAlias(alias);
                if (fromElement.isDereferencedBySubclassProperty()) {
                    // TODO : or should we return 'containsTableAlias'??
                    LOG.tracev("Forcing inclusion of extra joins [alias={0}, containsTableAlias={1}]", alias, containsTableAlias);
                    return true;
                }
                boolean shallowQuery = walker.isShallowQuery();
                boolean includeSubclasses = fromElement.isIncludeSubclasses();
                boolean subQuery = fromClause.isSubQuery();
                return includeSubclasses && containsTableAlias && !subQuery && !shallowQuery;
            }
        });
        addJoinNodes(query, join, fromElement);
    }
}
Also used : FromClause(org.hibernate.hql.internal.ast.tree.FromClause) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) ImpliedFromElement(org.hibernate.hql.internal.ast.tree.ImpliedFromElement) ArrayList(java.util.ArrayList) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ImpliedFromElement(org.hibernate.hql.internal.ast.tree.ImpliedFromElement) ListIterator(java.util.ListIterator) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 22 with FromElement

use of org.hibernate.hql.internal.ast.tree.FromElement in project hibernate-orm by hibernate.

the class QueryLoader method initialize.

private void initialize(SelectClause selectClause) {
    List fromElementList = selectClause.getFromElementsForLoad();
    hasScalars = selectClause.isScalarSelect();
    scalarColumnNames = selectClause.getColumnNames();
    // sqlResultTypes = selectClause.getSqlResultTypes();
    queryReturnTypes = selectClause.getQueryReturnTypes();
    aggregatedSelectExpression = selectClause.getAggregatedSelectExpression();
    queryReturnAliases = selectClause.getQueryReturnAliases();
    List collectionFromElements = selectClause.getCollectionFromElements();
    if (collectionFromElements != null && collectionFromElements.size() != 0) {
        int length = collectionFromElements.size();
        collectionPersisters = new QueryableCollection[length];
        collectionOwners = new int[length];
        collectionSuffixes = new String[length];
        for (int i = 0; i < length; i++) {
            FromElement collectionFromElement = (FromElement) collectionFromElements.get(i);
            collectionPersisters[i] = collectionFromElement.getQueryableCollection();
            collectionOwners[i] = fromElementList.indexOf(collectionFromElement.getOrigin());
            // collectionSuffixes[i] = collectionFromElement.getColumnAliasSuffix();
            // collectionSuffixes[i] = Integer.toString( i ) + "_";
            collectionSuffixes[i] = collectionFromElement.getCollectionSuffix();
        }
    }
    int size = fromElementList.size();
    entityPersisters = new Queryable[size];
    entityEagerPropertyFetches = new boolean[size];
    entityAliases = new String[size];
    sqlAliases = new String[size];
    sqlAliasSuffixes = new String[size];
    includeInSelect = new boolean[size];
    owners = new int[size];
    ownerAssociationTypes = new EntityType[size];
    for (int i = 0; i < size; i++) {
        final FromElement element = (FromElement) fromElementList.get(i);
        entityPersisters[i] = (Queryable) element.getEntityPersister();
        if (entityPersisters[i] == null) {
            throw new IllegalStateException("No entity persister for " + element.toString());
        }
        entityEagerPropertyFetches[i] = element.isAllPropertyFetch();
        sqlAliases[i] = element.getTableAlias();
        entityAliases[i] = element.getClassAlias();
        sqlAliasByEntityAlias.put(entityAliases[i], sqlAliases[i]);
        // TODO should we just collect these like with the collections above?
        sqlAliasSuffixes[i] = (size == 1) ? "" : (Integer.toString(i) + "_").intern();
        // sqlAliasSuffixes[i] = element.getColumnAliasSuffix();
        includeInSelect[i] = !element.isFetch();
        if (includeInSelect[i]) {
            selectLength++;
        }
        // by default
        owners[i] = -1;
        if (element.isFetch()) {
            // noinspection StatementWithEmptyBody
            if (element.isCollectionJoin() || element.getQueryableCollection() != null) {
            // This is now handled earlier in this method.
            } else if (element.getDataType().isEntityType()) {
                EntityType entityType = (EntityType) element.getDataType();
                if (entityType.isOneToOne()) {
                    owners[i] = fromElementList.indexOf(element.getOrigin());
                }
                ownerAssociationTypes[i] = entityType;
            }
        }
    }
    // NONE, because its the requested lock mode, not the actual!
    defaultLockModes = ArrayHelper.fillArray(LockMode.NONE, size);
}
Also used : EntityType(org.hibernate.type.EntityType) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) List(java.util.List)

Aggregations

FromElement (org.hibernate.hql.internal.ast.tree.FromElement)22 EntityJoinFromElement (org.hibernate.hql.internal.ast.tree.EntityJoinFromElement)9 Queryable (org.hibernate.persister.entity.Queryable)8 ArrayList (java.util.ArrayList)5 List (java.util.List)5 QueryException (org.hibernate.QueryException)4 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)4 AST (antlr.collections.AST)3 Iterator (java.util.Iterator)3 JoinSequence (org.hibernate.engine.internal.JoinSequence)3 DeleteStatement (org.hibernate.hql.internal.ast.tree.DeleteStatement)3 FromClause (org.hibernate.hql.internal.ast.tree.FromClause)3 UpdateStatement (org.hibernate.hql.internal.ast.tree.UpdateStatement)3 FromElementFactory (org.hibernate.hql.internal.ast.tree.FromElementFactory)2 FromReferenceNode (org.hibernate.hql.internal.ast.tree.FromReferenceNode)2 ImpliedFromElement (org.hibernate.hql.internal.ast.tree.ImpliedFromElement)2 QueryNode (org.hibernate.hql.internal.ast.tree.QueryNode)2 TableBasedDeleteHandlerImpl (org.hibernate.hql.spi.id.TableBasedDeleteHandlerImpl)2 TableBasedUpdateHandlerImpl (org.hibernate.hql.spi.id.TableBasedUpdateHandlerImpl)2 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)2