Search in sources :

Example 11 with RootHibernateQuery

use of com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery in project midpoint by Evolveum.

the class PropertyRestriction method createPropertyVsPropertyCondition.

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);
        return hibernateQuery.createOr(condition, hibernateQuery.createAnd(hibernateQuery.createIsNull(leftPropertyValuePath), hibernateQuery.createIsNull(rightHqlPath)));
    } else if (filter instanceof ComparativeFilter) {
        ItemRestrictionOperation operation = findOperationForFilter(filter);
        return hibernateQuery.createCompareXY(leftPropertyValuePath, rightHqlPath, operation.symbol(), false);
    } else {
        throw new QueryException("Right-side ItemPath is supported currently only for EqualFilter or ComparativeFilter, not for " + filter);
    }
}
Also used : ConstantCondition(com.evolveum.midpoint.repo.sql.query.hqm.condition.ConstantCondition) Condition(com.evolveum.midpoint.repo.sql.query.hqm.condition.Condition) HqlDataInstance(com.evolveum.midpoint.repo.sql.query.resolution.HqlDataInstance) QueryException(com.evolveum.midpoint.repo.sqlbase.QueryException) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery) ComparativeFilter(com.evolveum.midpoint.prism.query.ComparativeFilter) EqualFilter(com.evolveum.midpoint.prism.query.EqualFilter) PolyString(com.evolveum.midpoint.prism.polystring.PolyString)

Example 12 with RootHibernateQuery

use of com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery in project midpoint by Evolveum.

the class QueryInterpreter method addGrouping.

private void addGrouping(InterpretationContext context, ObjectGrouping grouping) throws QueryException {
    ItemPath groupByPath = grouping.getGroupBy();
    // TODO if we'd like to have group-by extension properties, we'd need to provide itemDefinition for them
    ProperDataSearchResult<?> result = context.getItemPathResolver().findProperDataDefinition(context.getRootEntityDefinition(), groupByPath, null, JpaDataNodeDefinition.class, context.getPrismContext());
    if (result == null) {
        LOGGER.error("Unknown path '" + groupByPath + "', couldn't find definition for it, " + "list will not be grouped by it.");
        return;
    }
    JpaDataNodeDefinition targetDefinition = result.getLinkDefinition().getTargetDefinition();
    if (targetDefinition instanceof JpaAnyContainerDefinition) {
        throw new QueryException("Grouping based on extension item or attribute is not supported yet: " + groupByPath);
    } else if (targetDefinition instanceof JpaReferenceDefinition) {
        throw new QueryException("Grouping based on reference is not supported: " + groupByPath);
    } else if (result.getLinkDefinition().isMultivalued()) {
        throw new QueryException("Grouping based on multi-valued item is not supported: " + groupByPath);
    } else if (targetDefinition instanceof JpaEntityDefinition) {
        throw new QueryException("Grouping based on entity is not supported: " + groupByPath);
    } else if (!(targetDefinition instanceof JpaPropertyDefinition)) {
        throw new IllegalStateException("Unknown item definition type: " + result.getClass());
    }
    JpaEntityDefinition baseEntityDefinition = result.getEntityDefinition();
    JpaPropertyDefinition groupByDefinition = (JpaPropertyDefinition) targetDefinition;
    String hqlPropertyPath = context.getItemPathResolver().resolveItemPath(groupByPath, null, context.getPrimaryEntityAlias(), baseEntityDefinition, true).getHqlPath();
    if (RPolyString.class.equals(groupByDefinition.getJpaClass())) {
        hqlPropertyPath += ".orig";
    }
    RootHibernateQuery hibernateQuery = context.getHibernateQuery();
    hibernateQuery.addGrouping(hqlPropertyPath);
}
Also used : QueryException(com.evolveum.midpoint.repo.sqlbase.QueryException) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) RPolyString(com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 13 with RootHibernateQuery

use of com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery in project midpoint by Evolveum.

the class ItemPathResolver method addTextInfoJoin.

public String addTextInfoJoin(String currentHqlPath) {
    RootHibernateQuery hibernateQuery = context.getHibernateQuery();
    String joinedItemJpaName = RObject.F_TEXT_INFO_ITEMS;
    String joinedItemFullPath = currentHqlPath + "." + joinedItemJpaName;
    String joinedItemAlias = hibernateQuery.createAlias(joinedItemJpaName, false);
    hibernateQuery.getPrimaryEntity().addJoin(new JoinSpecification(joinedItemAlias, joinedItemFullPath, null));
    return joinedItemAlias;
}
Also used : RootHibernateQuery(com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery) JoinSpecification(com.evolveum.midpoint.repo.sql.query.hqm.JoinSpecification)

Example 14 with RootHibernateQuery

use of com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery in project midpoint by Evolveum.

the class ItemPathResolver method reuseOrAddJoin.

String reuseOrAddJoin(JpaLinkDefinition<?> joinedItemDefinition, String currentHqlPath, boolean reuseMultivaluedJoins) throws QueryException {
    RootHibernateQuery hibernateQuery = context.getHibernateQuery();
    String joinedItemJpaName = joinedItemDefinition.getJpaName();
    String joinedItemFullPath = currentHqlPath + "." + joinedItemJpaName;
    String joinedItemAlias;
    if (joinedItemDefinition.isMultivalued()) {
        if (reuseMultivaluedJoins) {
            String existingAlias = findExistingAlias(hibernateQuery, joinedItemDefinition, joinedItemFullPath);
            if (existingAlias != null) {
                return existingAlias;
            } else {
                // TODO better explanation
                throw new QueryException("Couldn't reuse existing join for " + joinedItemDefinition + " in the context of " + currentHqlPath);
            }
        }
    } else {
        /*
             * Let's check if we were already here i.e. if we had already created this join.
             * This is to avoid useless creation of redundant joins for singleton items.
             *
             * But how can we be sure that item is singleton if we look only at the last segment (which is single-valued)?
             * Imagine we are creating join for single-valued entity of u.abc.(...).xyz.ent where
             * ent is single-valued and not embedded (so we are to create something like "left join u.abc.(...).xyz.ent e").
             * Then "u" is certainly a single value: either the root object, or some value pointed to by Exists restriction.
             * Also, abc, ..., xyz are surely single-valued: otherwise they would be connected by a join. So,
             * u.abc.(...).xyz.ent is a singleton.
             *
             * Look at it in other way: if e.g. xyz was multivalued, then we would have something like:
             * left join u.abc.(...).uvw.xyz x
             * left join x.ent e
             * And, therefore we would not be looking for u.abc.(...).xyz.ent.
             */
        String existingAlias = findExistingAlias(hibernateQuery, joinedItemDefinition, joinedItemFullPath);
        if (existingAlias != null) {
            return existingAlias;
        }
    }
    joinedItemAlias = hibernateQuery.createAlias(joinedItemDefinition);
    Condition condition = createJoinCondition(joinedItemAlias, joinedItemDefinition, hibernateQuery);
    hibernateQuery.getPrimaryEntity().addJoin(new JoinSpecification(joinedItemAlias, joinedItemFullPath, condition));
    return joinedItemAlias;
}
Also used : AndCondition(com.evolveum.midpoint.repo.sql.query.hqm.condition.AndCondition) Condition(com.evolveum.midpoint.repo.sql.query.hqm.condition.Condition) QueryException(com.evolveum.midpoint.repo.sqlbase.QueryException) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery) JoinSpecification(com.evolveum.midpoint.repo.sql.query.hqm.JoinSpecification)

Aggregations

RootHibernateQuery (com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery)14 QueryException (com.evolveum.midpoint.repo.sqlbase.QueryException)8 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)5 RPolyString (com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString)4 Condition (com.evolveum.midpoint.repo.sql.query.hqm.condition.Condition)4 QName (javax.xml.namespace.QName)3 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)2 QueryInterpreter (com.evolveum.midpoint.repo.sql.query.QueryInterpreter)2 JoinSpecification (com.evolveum.midpoint.repo.sql.query.hqm.JoinSpecification)2 AndCondition (com.evolveum.midpoint.repo.sql.query.hqm.condition.AndCondition)2 HqlDataInstance (com.evolveum.midpoint.repo.sql.query.resolution.HqlDataInstance)2 PrismReferenceValue (com.evolveum.midpoint.prism.PrismReferenceValue)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 RObjectType (com.evolveum.midpoint.repo.sql.data.common.other.RObjectType)1 InterpretationContext (com.evolveum.midpoint.repo.sql.query.InterpretationContext)1 RQuery (com.evolveum.midpoint.repo.sql.query.RQuery)1 RQueryImpl (com.evolveum.midpoint.repo.sql.query.RQueryImpl)1 JpaDataNodeDefinition (com.evolveum.midpoint.repo.sql.query.definition.JpaDataNodeDefinition)1