Search in sources :

Example 1 with FromClause

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

the class HqlSqlWalker method beforeSelectClause.

@Override
protected void beforeSelectClause() throws SemanticException {
    // Turn off includeSubclasses on all FromElements.
    FromClause from = getCurrentFromClause();
    List fromElements = from.getFromElements();
    for (Iterator iterator = fromElements.iterator(); iterator.hasNext(); ) {
        FromElement fromElement = (FromElement) iterator.next();
        fromElement.setIncludeSubclasses(false);
    }
}
Also used : FromClause(org.hibernate.hql.internal.ast.tree.FromClause) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) EntityJoinFromElement(org.hibernate.hql.internal.ast.tree.EntityJoinFromElement) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with FromClause

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

the class HqlSqlWalker method createFromFilterElement.

@Override
protected AST createFromFilterElement(AST filterEntity, AST alias) throws SemanticException {
    FromElement fromElement = currentFromClause.addFromElement(filterEntity.getText(), alias);
    FromClause fromClause = fromElement.getFromClause();
    QueryableCollection persister = sessionFactoryHelper.getCollectionPersister(collectionFilterRole);
    // Get the names of the columns used to link between the collection
    // owner and the collection elements.
    String[] keyColumnNames = persister.getKeyColumnNames();
    String fkTableAlias = persister.isOneToMany() ? fromElement.getTableAlias() : fromClause.getAliasGenerator().createName(collectionFilterRole);
    JoinSequence join = sessionFactoryHelper.createJoinSequence();
    join.setRoot(persister, fkTableAlias);
    if (!persister.isOneToMany()) {
        join.addJoin((AssociationType) persister.getElementType(), fromElement.getTableAlias(), JoinType.INNER_JOIN, persister.getElementColumnNames(fkTableAlias));
    }
    join.addCondition(fkTableAlias, keyColumnNames, " = ?");
    fromElement.setJoinSequence(join);
    fromElement.setFilter(true);
    LOG.debug("createFromFilterElement() : processed filter FROM element.");
    return fromElement;
}
Also used : FromClause(org.hibernate.hql.internal.ast.tree.FromClause) FromElement(org.hibernate.hql.internal.ast.tree.FromElement) EntityJoinFromElement(org.hibernate.hql.internal.ast.tree.EntityJoinFromElement) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) JoinSequence(org.hibernate.engine.internal.JoinSequence)

Example 3 with FromClause

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

the class HqlSqlWalker method pushFromClause.

/**
 * Sets the current 'FROM' context.
 *
 * @param fromNode The new 'FROM' context.
 * @param inputFromNode The from node from the input AST.
 */
@Override
protected void pushFromClause(AST fromNode, AST inputFromNode) {
    FromClause newFromClause = (FromClause) fromNode;
    newFromClause.setParentFromClause(currentFromClause);
    currentFromClause = newFromClause;
}
Also used : FromClause(org.hibernate.hql.internal.ast.tree.FromClause)

Example 4 with FromClause

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

the class HqlSqlWalker method prepareVersioned.

@Override
protected void prepareVersioned(AST updateNode, AST versioned) throws SemanticException {
    UpdateStatement updateStatement = (UpdateStatement) updateNode;
    FromClause fromClause = updateStatement.getFromClause();
    if (versioned != null) {
        // Make sure that the persister is versioned
        Queryable persister = fromClause.getFromElement().getQueryable();
        if (!persister.isVersioned()) {
            throw new SemanticException("increment option specified for update of non-versioned entity");
        }
        VersionType versionType = persister.getVersionType();
        if (versionType instanceof UserVersionType) {
            throw new SemanticException("user-defined version types not supported for increment option");
        }
        AST eq = getASTFactory().create(HqlSqlTokenTypes.EQ, "=");
        AST versionPropertyNode = generateVersionPropertyNode(persister);
        eq.setFirstChild(versionPropertyNode);
        AST versionIncrementNode = null;
        if (isTimestampBasedVersion(versionType)) {
            versionIncrementNode = getASTFactory().create(HqlSqlTokenTypes.PARAM, "?");
            ParameterSpecification paramSpec = new VersionTypeSeedParameterSpecification(versionType);
            ((ParameterNode) versionIncrementNode).setHqlParameterSpecification(paramSpec);
            parameterSpecs.add(0, paramSpec);
        } else {
            // Not possible to simply re-use the versionPropertyNode here as it causes
            // OOM errors due to circularity :(
            versionIncrementNode = getASTFactory().create(HqlSqlTokenTypes.PLUS, "+");
            versionIncrementNode.setFirstChild(generateVersionPropertyNode(persister));
            versionIncrementNode.addChild(getASTFactory().create(HqlSqlTokenTypes.IDENT, "1"));
        }
        eq.addChild(versionIncrementNode);
        evaluateAssignment(eq, persister, 0);
        AST setClause = updateStatement.getSetClause();
        AST currentFirstSetElement = setClause.getFirstChild();
        setClause.setFirstChild(eq);
        eq.setNextSibling(currentFirstSetElement);
    }
}
Also used : UpdateStatement(org.hibernate.hql.internal.ast.tree.UpdateStatement) UserVersionType(org.hibernate.usertype.UserVersionType) AST(antlr.collections.AST) CollectionFilterKeyParameterSpecification(org.hibernate.param.CollectionFilterKeyParameterSpecification) ParameterSpecification(org.hibernate.param.ParameterSpecification) PositionalParameterSpecification(org.hibernate.param.PositionalParameterSpecification) VersionTypeSeedParameterSpecification(org.hibernate.param.VersionTypeSeedParameterSpecification) NamedParameterSpecification(org.hibernate.param.NamedParameterSpecification) VersionTypeSeedParameterSpecification(org.hibernate.param.VersionTypeSeedParameterSpecification) ParameterNode(org.hibernate.hql.internal.ast.tree.ParameterNode) FromClause(org.hibernate.hql.internal.ast.tree.FromClause) Queryable(org.hibernate.persister.entity.Queryable) UserVersionType(org.hibernate.usertype.UserVersionType) VersionType(org.hibernate.type.VersionType) SemanticException(antlr.SemanticException)

Example 5 with FromClause

use of org.hibernate.hql.internal.ast.tree.FromClause 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)

Aggregations

FromClause (org.hibernate.hql.internal.ast.tree.FromClause)5 FromElement (org.hibernate.hql.internal.ast.tree.FromElement)3 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 JoinSequence (org.hibernate.engine.internal.JoinSequence)2 EntityJoinFromElement (org.hibernate.hql.internal.ast.tree.EntityJoinFromElement)2 SemanticException (antlr.SemanticException)1 AST (antlr.collections.AST)1 ListIterator (java.util.ListIterator)1 ImpliedFromElement (org.hibernate.hql.internal.ast.tree.ImpliedFromElement)1 ParameterNode (org.hibernate.hql.internal.ast.tree.ParameterNode)1 UpdateStatement (org.hibernate.hql.internal.ast.tree.UpdateStatement)1 CollectionFilterKeyParameterSpecification (org.hibernate.param.CollectionFilterKeyParameterSpecification)1 NamedParameterSpecification (org.hibernate.param.NamedParameterSpecification)1 ParameterSpecification (org.hibernate.param.ParameterSpecification)1 PositionalParameterSpecification (org.hibernate.param.PositionalParameterSpecification)1 VersionTypeSeedParameterSpecification (org.hibernate.param.VersionTypeSeedParameterSpecification)1 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)1 Queryable (org.hibernate.persister.entity.Queryable)1