Search in sources :

Example 1 with Condition

use of com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition in project midpoint by Evolveum.

the class ReferenceRestriction method interpretInternal.

@Override
public Condition interpretInternal() throws QueryException {
    String hqlPath = hqlDataInstance.getHqlPath();
    LOGGER.trace("interpretInternal starting with hqlPath = {}", hqlPath);
    RootHibernateQuery hibernateQuery = context.getHibernateQuery();
    List<PrismReferenceValue> values = filter.getValues();
    if (CollectionUtils.isEmpty(values)) {
        return hibernateQuery.createIsNull(hqlDataInstance.getHqlPath());
    }
    Set<String> oids = new HashSet<>();
    Set<QName> relations = new HashSet<>();
    Set<QName> targetTypes = new HashSet<>();
    for (PrismReferenceValue value : values) {
        if (value.getOid() == null) {
            throw new QueryException("Null OID is not allowed in the reference query. Use empty reference list if needed.");
        }
        oids.add(value.getOid());
        if (value.getRelation() == null) {
            relations.add(SchemaConstants.ORG_DEFAULT);
        } else {
            // we intentionally don't normalize relations namespaces, to be able to do namespace-insensitive searches
            // so the caller is responsible to unify namespaces if he needs to optimize queries (use IN instead of OR)
            relations.add(value.getRelation());
        }
        targetTypes.add(qualifyTypeName(value.getTargetType()));
    }
    if (relations.size() > 1 || targetTypes.size() > 1) {
        // we must use 'OR' clause
        OrCondition rootOr = hibernateQuery.createOr();
        values.forEach(prv -> rootOr.add(createRefCondition(hibernateQuery, Collections.singleton(prv.getOid()), prv.getRelation(), prv.getTargetType())));
        return rootOr;
    } else {
        return createRefCondition(hibernateQuery, oids, MiscUtil.extractSingleton(relations), MiscUtil.extractSingleton(targetTypes));
    }
}
Also used : QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) PrismReferenceValue(com.evolveum.midpoint.prism.PrismReferenceValue) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery) QName(javax.xml.namespace.QName) RUtil.qnameToString(com.evolveum.midpoint.repo.sql.util.RUtil.qnameToString) OrCondition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.OrCondition)

Example 2 with Condition

use of com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition in project midpoint by Evolveum.

the class Matcher method basicMatch.

protected Condition basicMatch(RootHibernateQuery hibernateQuery, ItemRestrictionOperation operation, String propertyPath, Object value, boolean ignoreCase) throws QueryException {
    Validate.notNull(hibernateQuery, "hibernateQuery");
    if (ignoreCase && !(value instanceof String)) {
        LOGGER.warn("Ignoring ignoreCase setting for non-string value of {}", value);
        ignoreCase = false;
    }
    Condition condition;
    switch(operation) {
        case EQ:
            if (value == null) {
                condition = hibernateQuery.createIsNull(propertyPath);
            } else {
                condition = hibernateQuery.createEq(propertyPath, value, ignoreCase);
            }
            break;
        case GT:
        case GE:
        case LT:
        case LE:
            condition = hibernateQuery.createSimpleComparisonCondition(propertyPath, value, operation.symbol(), ignoreCase);
            break;
        case NOT_NULL:
            condition = hibernateQuery.createIsNotNull(propertyPath);
            break;
        case NULL:
            condition = hibernateQuery.createIsNull(propertyPath);
            break;
        case STARTS_WITH:
            condition = hibernateQuery.createLike(propertyPath, (String) value, MatchMode.START, ignoreCase);
            break;
        case ENDS_WITH:
            condition = hibernateQuery.createLike(propertyPath, (String) value, MatchMode.END, ignoreCase);
            break;
        case SUBSTRING:
            condition = hibernateQuery.createLike(propertyPath, (String) value, MatchMode.ANYWHERE, ignoreCase);
            break;
        default:
            throw new QueryException("Unknown operation '" + operation + "'.");
    }
    return condition;
}
Also used : Condition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException)

Example 3 with Condition

use of com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition in project midpoint by Evolveum.

the class PolyStringMatcher method match.

@Override
public Condition match(RootHibernateQuery hibernateQuery, ItemRestrictionOperation operation, String propertyName, PolyString value, String matcher) throws QueryException {
    boolean ignoreCase = STRICT_IGNORE_CASE.equals(matcher) || ORIG_IGNORE_CASE.equals(matcher) || NORM_IGNORE_CASE.equals(matcher);
    if (StringUtils.isEmpty(matcher) || DEFAULT.equals(matcher) || STRICT.equals(matcher) || STRICT_IGNORE_CASE.equals(matcher)) {
        AndCondition conjunction = hibernateQuery.createAnd();
        conjunction.add(createOrigMatch(hibernateQuery, operation, propertyName, value, ignoreCase));
        conjunction.add(createNormMatch(hibernateQuery, operation, propertyName, value, ignoreCase));
        return conjunction;
    } else if (ORIG.equals(matcher) || ORIG_IGNORE_CASE.equals(matcher)) {
        return createOrigMatch(hibernateQuery, operation, propertyName, value, ignoreCase);
    } else if (NORM.equals(matcher) || NORM_IGNORE_CASE.equals(matcher)) {
        return createNormMatch(hibernateQuery, operation, propertyName, value, ignoreCase);
    } else {
        throw new QueryException("Unknown matcher '" + matcher + "'.");
    }
}
Also used : QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) AndCondition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.AndCondition)

Example 4 with Condition

use of com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition in project midpoint by Evolveum.

the class ItemPathResolver method addJoin.

String addJoin(JpaLinkDefinition joinedItemDefinition, String currentHqlPath) throws QueryException {
    RootHibernateQuery hibernateQuery = context.getHibernateQuery();
    String joinedItemJpaName = joinedItemDefinition.getJpaName();
    String joinedItemFullPath = currentHqlPath + "." + joinedItemJpaName;
    String joinedItemAlias;
    if (!joinedItemDefinition.isMultivalued()) {
        /*
             * 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.
             */
        JoinSpecification existingJoin = hibernateQuery.getPrimaryEntity().findJoinFor(joinedItemFullPath);
        if (existingJoin != null) {
            // but let's check the condition as well
            String existingAlias = existingJoin.getAlias();
            // we have to create condition for existing alias, to be matched to existing condition
            Condition conditionForExistingAlias = createJoinCondition(existingAlias, joinedItemDefinition, hibernateQuery);
            if (ObjectUtils.equals(conditionForExistingAlias, existingJoin.getCondition())) {
                LOGGER.trace("Reusing alias '{}' for joined path '{}'", existingAlias, joinedItemFullPath);
                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.query2.hqm.condition.AndCondition) Condition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery) JoinSpecification(com.evolveum.midpoint.repo.sql.query2.hqm.JoinSpecification)

Example 5 with Condition

use of com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition in project midpoint by Evolveum.

the class AndRestriction method interpret.

@Override
public Condition interpret() throws QueryException {
    validateFilter();
    AndCondition conjunction = getContext().getHibernateQuery().createAnd();
    updateJunction(filter.getConditions(), conjunction);
    return conjunction;
}
Also used : AndCondition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.AndCondition)

Aggregations

Condition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition)12 QueryException (com.evolveum.midpoint.repo.sql.query.QueryException)8 RootHibernateQuery (com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery)8 AndCondition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.AndCondition)7 QueryInterpreter2 (com.evolveum.midpoint.repo.sql.query2.QueryInterpreter2)5 InterpretationContext (com.evolveum.midpoint.repo.sql.query2.InterpretationContext)4 OrCondition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.OrCondition)4 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)3 HqlDataInstance (com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance)3 IsNotNullCondition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.IsNotNullCondition)2 IsNullCondition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.IsNullCondition)2 RUtil.qnameToString (com.evolveum.midpoint.repo.sql.util.RUtil.qnameToString)2 QName (javax.xml.namespace.QName)2 PrismReferenceValue (com.evolveum.midpoint.prism.PrismReferenceValue)1 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)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 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)1 ObjectPagingAfterOid (com.evolveum.midpoint.repo.sql.ObjectPagingAfterOid)1