Search in sources :

Example 6 with RootHibernateQuery

use of com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery 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);
        QueryInterpreter 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.query.definition.JpaDataNodeDefinition) HqlDataInstance(com.evolveum.midpoint.repo.sql.query.resolution.HqlDataInstance) QueryException(com.evolveum.midpoint.repo.sqlbase.QueryException) AllFilter(com.evolveum.midpoint.prism.query.AllFilter) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery) JpaEntityDefinition(com.evolveum.midpoint.repo.sql.query.definition.JpaEntityDefinition) QueryInterpreter(com.evolveum.midpoint.repo.sql.query.QueryInterpreter) JpaPropertyDefinition(com.evolveum.midpoint.repo.sql.query.definition.JpaPropertyDefinition)

Example 7 with RootHibernateQuery

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

the class QueryEngine method interpret.

public RQuery interpret(ObjectQuery query, Class<? extends Containerable> type, Collection<SelectorOptions<GetOperationOptions>> options, boolean countingObjects, Session session) throws QueryException {
    query = refineAssignmentHolderQuery(type, query);
    QueryInterpreter interpreter = new QueryInterpreter(repoConfiguration, extItemDictionary);
    RootHibernateQuery hibernateQuery = interpreter.interpret(query, type, options, prismContext, relationRegistry, countingObjects, session);
    Query<?> hqlQuery = hibernateQuery.getAsHqlQuery(session);
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Query interpretation result:\n--- Query:\n{}\n--- with options: {}\n--- resulted in HQL:\n{}", DebugUtil.debugDump(query), options, hqlQuery.getQueryString());
    }
    return new RQueryImpl(hqlQuery, hibernateQuery);
}
Also used : RootHibernateQuery(com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery)

Example 8 with RootHibernateQuery

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

the class QueryInterpreterTest method test018QueryGenericLongTwice.

@Test
public void test018QueryGenericLongTwice() throws Exception {
    Session session = open();
    try {
        ObjectQuery query = prismContext.queryFor(GenericObjectType.class).item(F_NAME).eqPoly("generic object", "generic object").matchingNorm().and().item(F_EXTENSION, new QName("intType")).ge(100).and().item(F_EXTENSION, new QName("intType")).lt(200).and().item(F_EXTENSION, new QName("longType")).eq(335).build();
        RQuery realQuery = getInterpretedQueryWhole(session, GenericObjectType.class, query, false, null);
        RootHibernateQuery source = ((RQueryImpl) realQuery).getQuerySource();
        String real = ((RQueryImpl) realQuery).getQuery().getQueryString();
        // note l and l2 cannot be merged as they point to different extension properties (intType, longType)
        assertThat(real).isEqualToIgnoringWhitespace("select\n" + "  g.oid, g.fullObject\n" + "from\n" + "  RGenericObject g\n" + "    left join g.longs l with ( l.ownerType = :ownerType and l.itemId = :itemId )\n" + "    left join g.longs l2 with ( l2.ownerType = :ownerType2 and l2.itemId = :itemId2 )\n" + "where\n" + "  (\n" + "    g.nameCopy.norm = :norm and\n" + "    l.value >= :value and\n" + "    l.value < :value2 and\n" + "    l2.value = :value3\n" + "  )");
        assertEquals("Wrong property ID for 'intType'", intTypeDefinition.getId(), source.getParameters().get("itemId").getValue());
        assertEquals("Wrong property ID for 'longType'", longTypeDefinition.getId(), source.getParameters().get("itemId2").getValue());
    } finally {
        close(session);
    }
}
Also used : RootHibernateQuery(com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery) QName(javax.xml.namespace.QName) RQueryImpl(com.evolveum.midpoint.repo.sql.query.RQueryImpl) Session(org.hibernate.Session) RQuery(com.evolveum.midpoint.repo.sql.query.RQuery) Test(org.testng.annotations.Test)

Example 9 with RootHibernateQuery

use of com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery 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<>();
    boolean valuesWithWildcardOid = false;
    boolean valuesWithSpecifiedOid = false;
    for (PrismReferenceValue value : values) {
        if (value.getOid() != null) {
            oids.add(value.getOid());
            valuesWithSpecifiedOid = true;
        } else {
            if (filter.isOidNullAsAny()) {
                valuesWithWildcardOid = true;
            } else {
                throw new QueryException("Null OID is not allowed in the reference query. " + "If you'd like to search for missing reference, use empty list of values.");
            }
        }
        if (value.getRelation() == null) {
            relations.add(context.getPrismContext().getDefaultRelation());
        } 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 (valuesWithWildcardOid && valuesWithSpecifiedOid || relations.size() > 1 || targetTypes.size() > 1) {
        // We must use 'OR' clause
        OrCondition rootOr = hibernateQuery.createOr();
        values.forEach(prv -> rootOr.add(createRefCondition(hibernateQuery, MiscUtil.singletonOrEmptySet(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.sqlbase.QueryException) PrismReferenceValue(com.evolveum.midpoint.prism.PrismReferenceValue) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery) QName(javax.xml.namespace.QName) OrCondition(com.evolveum.midpoint.repo.sql.query.hqm.condition.OrCondition)

Example 10 with RootHibernateQuery

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

the class ItemValueRestriction method addIsNotNullIfNecessary.

/**
 * Filter of type NOT(PROPERTY=VALUE) causes problems when there are entities with PROPERTY set to NULL.
 * <p>
 * Such a filter has to be treated like
 * <p>
 * NOT (PROPERTY=VALUE & PROPERTY IS NOT NULL)
 * <p>
 * TODO implement for restrictions other than PropertyRestriction.
 */
Condition addIsNotNullIfNecessary(Condition condition, String propertyPath) {
    if (condition instanceof IsNullCondition || condition instanceof IsNotNullCondition) {
        return condition;
    }
    if (!isNegated()) {
        return condition;
    }
    RootHibernateQuery hibernateQuery = getContext().getHibernateQuery();
    AndCondition conjunction = hibernateQuery.createAnd();
    conjunction.add(condition);
    conjunction.add(hibernateQuery.createIsNotNull(propertyPath));
    return conjunction;
}
Also used : IsNotNullCondition(com.evolveum.midpoint.repo.sql.query.hqm.condition.IsNotNullCondition) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery) IsNullCondition(com.evolveum.midpoint.repo.sql.query.hqm.condition.IsNullCondition) AndCondition(com.evolveum.midpoint.repo.sql.query.hqm.condition.AndCondition)

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