Search in sources :

Example 1 with CollectionFilterKeyParameterSpecification

use of org.hibernate.param.CollectionFilterKeyParameterSpecification in project hibernate-orm by hibernate.

the class HqlSqlWalker method prepareFromClauseInputTree.

@Override
protected void prepareFromClauseInputTree(AST fromClauseInput) {
    if (!isSubQuery()) {
        if (isFilter()) {
            // Handle collection-filter compilation.
            // IMPORTANT NOTE: This is modifying the INPUT (HQL) tree, not the output tree!
            QueryableCollection persister = sessionFactoryHelper.getCollectionPersister(collectionFilterRole);
            Type collectionElementType = persister.getElementType();
            if (!collectionElementType.isEntityType()) {
                throw new QueryException("collection of values in filter: this");
            }
            String collectionElementEntityName = persister.getElementPersister().getEntityName();
            ASTFactory inputAstFactory = hqlParser.getASTFactory();
            AST fromElement = inputAstFactory.create(HqlTokenTypes.FILTER_ENTITY, collectionElementEntityName);
            ASTUtil.createSibling(inputAstFactory, HqlTokenTypes.ALIAS, "this", fromElement);
            fromClauseInput.addChild(fromElement);
            // Show the modified AST.
            LOG.debug("prepareFromClauseInputTree() : Filter - Added 'this' as a from element...");
            queryTranslatorImpl.showHqlAst(hqlParser.getAST());
            // Create a parameter specification for the collection filter...
            Type collectionFilterKeyType = sessionFactoryHelper.requireQueryableCollection(collectionFilterRole).getKeyType();
            ParameterNode collectionFilterKeyParameter = (ParameterNode) astFactory.create(PARAM, "?");
            CollectionFilterKeyParameterSpecification collectionFilterKeyParameterSpec = new CollectionFilterKeyParameterSpecification(collectionFilterRole, collectionFilterKeyType, positionalParameterCount++);
            collectionFilterKeyParameter.setHqlParameterSpecification(collectionFilterKeyParameterSpec);
            parameters.add(collectionFilterKeyParameterSpec);
        }
    }
}
Also used : UserVersionType(org.hibernate.usertype.UserVersionType) JoinType(org.hibernate.sql.JoinType) CompositeType(org.hibernate.type.CompositeType) DbTimestampType(org.hibernate.type.DbTimestampType) VersionType(org.hibernate.type.VersionType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) AST(antlr.collections.AST) ASTFactory(antlr.ASTFactory) ParameterNode(org.hibernate.hql.internal.ast.tree.ParameterNode) CollectionFilterKeyParameterSpecification(org.hibernate.param.CollectionFilterKeyParameterSpecification) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Example 2 with CollectionFilterKeyParameterSpecification

use of org.hibernate.param.CollectionFilterKeyParameterSpecification in project hibernate-orm by hibernate.

the class SyntheticAndFactory method addWhereFragment.

public void addWhereFragment(JoinFragment joinFragment, String whereFragment, QueryNode query, FromElement fromElement, HqlSqlWalker hqlSqlWalker) {
    if (whereFragment == null) {
        return;
    }
    if (!fromElement.useWhereFragment() && !joinFragment.hasThetaJoins()) {
        return;
    }
    whereFragment = whereFragment.trim();
    if (StringHelper.isEmpty(whereFragment)) {
        return;
    }
    // handle adding them
    if (whereFragment.startsWith("and")) {
        whereFragment = whereFragment.substring(4);
    }
    LOG.debugf("Using unprocessed WHERE-fragment [%s]", whereFragment);
    SqlFragment fragment = (SqlFragment) create(SQL_TOKEN, whereFragment);
    fragment.setJoinFragment(joinFragment);
    fragment.setFromElement(fromElement);
    if (fromElement.getIndexCollectionSelectorParamSpec() != null) {
        fragment.addEmbeddedParameter(fromElement.getIndexCollectionSelectorParamSpec());
        fromElement.setIndexCollectionSelectorParamSpec(null);
    }
    if (hqlSqlWalker.isFilter()) {
        if (whereFragment.indexOf('?') >= 0) {
            Type collectionFilterKeyType = hqlSqlWalker.getSessionFactoryHelper().requireQueryableCollection(hqlSqlWalker.getCollectionFilterRole()).getKeyType();
            CollectionFilterKeyParameterSpecification paramSpec = new CollectionFilterKeyParameterSpecification(hqlSqlWalker.getCollectionFilterRole(), collectionFilterKeyType, 0);
            fragment.addEmbeddedParameter(paramSpec);
        }
    }
    JoinProcessor.processDynamicFilterParameters(whereFragment, fragment, hqlSqlWalker);
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Using processed WHERE-fragment [%s]", fragment.getText());
    }
    // then it binds all the HQL query parameters, see org.hibernate.loader.Loader.processFilterParameters().
    if (fragment.getFromElement().isFilter() || fragment.hasFilterCondition()) {
        if (filters == null) {
            // Find or create the WHERE clause
            AST where = query.getWhereClause();
            // Create a new FILTERS node as a parent of all filters
            filters = create(FILTERS, "{filter conditions}");
            // Put the FILTERS node beforeQuery the HQL condition and theta joins
            ASTUtil.insertChild(where, filters);
        }
        // add the current fragment to the FILTERS node
        filters.addChild(fragment);
    } else {
        if (thetaJoins == null) {
            // Find or create the WHERE clause
            AST where = query.getWhereClause();
            // Create a new THETA_JOINS node as a parent of all filters
            thetaJoins = create(THETA_JOINS, "{theta joins}");
            // Put the THETA_JOINS node beforeQuery the HQL condition, afterQuery the filters.
            if (filters == null) {
                ASTUtil.insertChild(where, thetaJoins);
            } else {
                ASTUtil.insertSibling(thetaJoins, filters);
            }
        }
        // add the current fragment to the THETA_JOINS node
        thetaJoins.addChild(fragment);
    }
}
Also used : SqlFragment(org.hibernate.hql.internal.ast.tree.SqlFragment) Type(org.hibernate.type.Type) AST(antlr.collections.AST) CollectionFilterKeyParameterSpecification(org.hibernate.param.CollectionFilterKeyParameterSpecification)

Aggregations

AST (antlr.collections.AST)2 CollectionFilterKeyParameterSpecification (org.hibernate.param.CollectionFilterKeyParameterSpecification)2 Type (org.hibernate.type.Type)2 ASTFactory (antlr.ASTFactory)1 QueryException (org.hibernate.QueryException)1 ParameterNode (org.hibernate.hql.internal.ast.tree.ParameterNode)1 SqlFragment (org.hibernate.hql.internal.ast.tree.SqlFragment)1 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)1 JoinType (org.hibernate.sql.JoinType)1 AssociationType (org.hibernate.type.AssociationType)1 CompositeType (org.hibernate.type.CompositeType)1 DbTimestampType (org.hibernate.type.DbTimestampType)1 VersionType (org.hibernate.type.VersionType)1 UserVersionType (org.hibernate.usertype.UserVersionType)1