Search in sources :

Example 1 with HqlDataInstance

use of com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance in project midpoint by Evolveum.

the class ItemPathResolutionState method nextState.

/**
     * Executes transition to next state. Modifies query context by adding joins as necessary.
     *
     * Precondition: !isFinal()
     * Precondition: adequate transition exists
     *
     * @param itemDefinition Target item definition (used/required only for "any" properties)
     * @param singletonOnly Collections are forbidden
     * @return destination state - always not null
     */
public ItemPathResolutionState nextState(ItemDefinition itemDefinition, boolean singletonOnly, PrismContext prismContext) throws QueryException {
    // This is brutal hack, to be thought again.
    if (remainingItemPath.startsWith(ParentPathSegment.class) && hqlDataInstance.getParentItem() != null) {
        return new ItemPathResolutionState(remainingItemPath.tail(), hqlDataInstance.getParentItem(), itemPathResolver);
    }
    DataSearchResult<?> result = hqlDataInstance.getJpaDefinition().nextLinkDefinition(remainingItemPath, itemDefinition, prismContext);
    LOGGER.trace("nextLinkDefinition on '{}' returned '{}'", remainingItemPath, result != null ? result.getLinkDefinition() : "(null)");
    if (result == null) {
        // sorry we failed (however, this should be caught before -> so IllegalStateException)
        throw new IllegalStateException("Couldn't find '" + remainingItemPath + "' in " + hqlDataInstance.getJpaDefinition());
    }
    JpaLinkDefinition linkDefinition = result.getLinkDefinition();
    String newHqlPath = hqlDataInstance.getHqlPath();
    if (linkDefinition.hasJpaRepresentation()) {
        if (singletonOnly && linkDefinition.isMultivalued()) {
            // TODO better message + context
            throw new QueryException("Collections are not allowable for right-side paths");
        }
        if (!linkDefinition.isEmbedded() || linkDefinition.isMultivalued()) {
            LOGGER.trace("Adding join for '{}' to context", linkDefinition);
            newHqlPath = itemPathResolver.addJoin(linkDefinition, hqlDataInstance.getHqlPath());
        } else {
            newHqlPath += "." + linkDefinition.getJpaName();
        }
    }
    HqlDataInstance<?> parentDataInstance;
    if (!remainingItemPath.startsWith(ParentPathSegment.class)) {
        // TODO what about other special cases? (@, ...)
        parentDataInstance = hqlDataInstance;
    } else {
        parentDataInstance = null;
    }
    return new ItemPathResolutionState(result.getRemainder(), new HqlDataInstance<>(newHqlPath, result.getTargetDefinition(), parentDataInstance), itemPathResolver);
}
Also used : JpaLinkDefinition(com.evolveum.midpoint.repo.sql.query2.definition.JpaLinkDefinition) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) ParentPathSegment(com.evolveum.midpoint.prism.path.ParentPathSegment)

Example 2 with HqlDataInstance

use of com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance in project midpoint by Evolveum.

the class ExistsRestriction method interpret.

@Override
public Condition interpret() throws QueryException {
    HqlDataInstance dataInstance = getItemPathResolver().resolveItemPath(filter.getFullPath(), filter.getDefinition(), getBaseHqlEntity(), false);
    boolean isAll = filter.getFilter() == null || filter.getFilter() instanceof AllFilter;
    JpaDataNodeDefinition jpaDefinition = dataInstance.getJpaDefinition();
    if (!isAll) {
        if (!(jpaDefinition instanceof JpaEntityDefinition)) {
            // partially checked already (for non-null-ness)
            throw new QueryException("ExistsRestriction with non-empty subfilter points to non-entity node: " + jpaDefinition);
        }
        setHqlDataInstance(dataInstance);
        QueryInterpreter2 interpreter = context.getInterpreter();
        return interpreter.interpretFilter(context, filter.getFilter(), this);
    } else if (jpaDefinition instanceof JpaPropertyDefinition && (((JpaPropertyDefinition) jpaDefinition).isCount())) {
        RootHibernateQuery hibernateQuery = context.getHibernateQuery();
        return hibernateQuery.createSimpleComparisonCondition(dataInstance.getHqlPath(), 0, ">");
    } else {
        // TODO support exists also for other properties (single valued or multi valued)
        throw new UnsupportedOperationException("Exists filter with 'all' subfilter is currently not supported");
    }
}
Also used : JpaDataNodeDefinition(com.evolveum.midpoint.repo.sql.query2.definition.JpaDataNodeDefinition) HqlDataInstance(com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) AllFilter(com.evolveum.midpoint.prism.query.AllFilter) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery) JpaEntityDefinition(com.evolveum.midpoint.repo.sql.query2.definition.JpaEntityDefinition) JpaPropertyDefinition(com.evolveum.midpoint.repo.sql.query2.definition.JpaPropertyDefinition) QueryInterpreter2(com.evolveum.midpoint.repo.sql.query2.QueryInterpreter2)

Example 3 with HqlDataInstance

use of com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance in project midpoint by Evolveum.

the class PropertyRestriction method createPropertyVsPropertyCondition.

protected Condition createPropertyVsPropertyCondition(String leftPropertyValuePath) throws QueryException {
    HqlDataInstance rightItem = getItemPathResolver().resolveItemPath(filter.getRightHandSidePath(), filter.getRightHandSideDefinition(), getBaseHqlEntityForChildren(), true);
    String rightHqlPath = rightItem.getHqlPath();
    RootHibernateQuery hibernateQuery = context.getHibernateQuery();
    if (filter instanceof EqualFilter) {
        // left = right OR (left IS NULL AND right IS NULL)
        Condition condition = hibernateQuery.createCompareXY(leftPropertyValuePath, rightHqlPath, "=", false);
        OrCondition orCondition = hibernateQuery.createOr(condition, hibernateQuery.createAnd(hibernateQuery.createIsNull(leftPropertyValuePath), hibernateQuery.createIsNull(rightHqlPath)));
        return orCondition;
    } else if (filter instanceof ComparativeFilter) {
        ItemRestrictionOperation operation = findOperationForFilter(filter);
        Condition condition = hibernateQuery.createCompareXY(leftPropertyValuePath, rightHqlPath, operation.symbol(), false);
        return condition;
    } else {
        throw new QueryException("Right-side ItemPath is supported currently only for EqualFilter or ComparativeFilter, not for " + filter);
    }
}
Also used : Condition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition) OrCondition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.OrCondition) HqlDataInstance(com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery) ComparativeFilter(com.evolveum.midpoint.prism.query.ComparativeFilter) EqualFilter(com.evolveum.midpoint.prism.query.EqualFilter) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) OrCondition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.OrCondition)

Example 4 with HqlDataInstance

use of com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance in project midpoint by Evolveum.

the class ItemValueRestriction method interpret.

@Override
public Condition interpret() throws QueryException {
    ItemPath path = getItemPath();
    if (ItemPath.isNullOrEmpty(path)) {
        throw new QueryException("Null or empty path for ItemValueRestriction in " + filter.debugDump());
    }
    HqlDataInstance dataInstance = getItemPathResolver().resolveItemPath(path, itemDefinition, getBaseHqlEntity(), false);
    setHqlDataInstance(dataInstance);
    Condition condition = interpretInternal();
    return condition;
}
Also used : AndCondition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.AndCondition) Condition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition) IsNotNullCondition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.IsNotNullCondition) IsNullCondition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.IsNullCondition) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) HqlDataInstance(com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Aggregations

QueryException (com.evolveum.midpoint.repo.sql.query.QueryException)4 HqlDataInstance (com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance)3 RootHibernateQuery (com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery)2 Condition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition)2 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)1 ParentPathSegment (com.evolveum.midpoint.prism.path.ParentPathSegment)1 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1 AllFilter (com.evolveum.midpoint.prism.query.AllFilter)1 ComparativeFilter (com.evolveum.midpoint.prism.query.ComparativeFilter)1 EqualFilter (com.evolveum.midpoint.prism.query.EqualFilter)1 QueryInterpreter2 (com.evolveum.midpoint.repo.sql.query2.QueryInterpreter2)1 JpaDataNodeDefinition (com.evolveum.midpoint.repo.sql.query2.definition.JpaDataNodeDefinition)1 JpaEntityDefinition (com.evolveum.midpoint.repo.sql.query2.definition.JpaEntityDefinition)1 JpaLinkDefinition (com.evolveum.midpoint.repo.sql.query2.definition.JpaLinkDefinition)1 JpaPropertyDefinition (com.evolveum.midpoint.repo.sql.query2.definition.JpaPropertyDefinition)1 AndCondition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.AndCondition)1 IsNotNullCondition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.IsNotNullCondition)1 IsNullCondition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.IsNullCondition)1 OrCondition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.OrCondition)1