Search in sources :

Example 6 with RootHibernateQuery

use of com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery 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 7 with RootHibernateQuery

use of com.evolveum.midpoint.repo.sql.query2.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);
        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 8 with RootHibernateQuery

use of com.evolveum.midpoint.repo.sql.query2.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.
     *
     * Such a filter has to be treated like
     *
     *      NOT (PROPERTY=VALUE & PROPERTY IS NOT NULL)
     *
     * TODO implement for restrictions other than PropertyRestriction.
     */
protected 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.query2.hqm.condition.IsNotNullCondition) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery) IsNullCondition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.IsNullCondition) AndCondition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.AndCondition)

Example 9 with RootHibernateQuery

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

the class QueryInterpreter2Test method test071QueryGenericLongTwice.

@Test
public void test071QueryGenericLongTwice() throws Exception {
    Session session = open();
    try {
        ObjectQuery query = QueryBuilder.queryFor(GenericObjectType.class, prismContext).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 = getInterpretedQuery2Whole(session, GenericObjectType.class, query, false, null);
        RootHibernateQuery source = ((RQueryImpl) realQuery).getQuerySource();
        String real = ((RQueryImpl) realQuery).getQuery().getQueryString();
        String expected = "select\n" + "  g.oid, g.fullObject, g.stringsCount, g.longsCount, g.datesCount, g.referencesCount, g.polysCount, g.booleansCount\n" + "from\n" + "  RGenericObject g\n" + "    left join g.longs l with ( l.ownerType = :ownerType and l.name = :name )\n" + "    left join g.longs l2 with ( l2.ownerType = :ownerType2 and l2.name = :name2 )\n" + "where\n" + "  (\n" + "    g.name.norm = :norm and\n" + "    l.value >= :value and\n" + "    l.value < :value2 and\n" + "    l2.value = :value3\n" + "  )";
        // note l and l2 cannot be merged as they point to different extension properties (intType, longType)
        assertEqualsIgnoreWhitespace(expected, real);
        assertEquals("Wrong property URI for 'intType'", "http://example.com/p#intType", source.getParameters().get("name").getValue());
        assertEquals("Wrong property URI for 'longType'", "http://example.com/p#longType", source.getParameters().get("name2").getValue());
    } finally {
        close(session);
    }
}
Also used : RootHibernateQuery(com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery) QName(javax.xml.namespace.QName) RQueryImpl(com.evolveum.midpoint.repo.sql.query2.RQueryImpl) Session(org.hibernate.Session) RQuery(com.evolveum.midpoint.repo.sql.query.RQuery) Test(org.testng.annotations.Test)

Example 10 with RootHibernateQuery

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

the class QueryInterpreter2 method interpretPagingAndSorting.

private void interpretPagingAndSorting(InterpretationContext context, ObjectQuery query, boolean countingObjects) throws QueryException {
    RootHibernateQuery hibernateQuery = context.getHibernateQuery();
    String rootAlias = hibernateQuery.getPrimaryEntityAlias();
    if (query != null && query.getPaging() instanceof ObjectPagingAfterOid) {
        ObjectPagingAfterOid paging = (ObjectPagingAfterOid) query.getPaging();
        if (paging.getOidGreaterThan() != null) {
            Condition c = hibernateQuery.createSimpleComparisonCondition(rootAlias + ".oid", paging.getOidGreaterThan(), ">");
            hibernateQuery.addCondition(c);
        }
    }
    if (!countingObjects && query != null && query.getPaging() != null) {
        if (query.getPaging() instanceof ObjectPagingAfterOid) {
            // very special case - ascending ordering by OID (nothing more)
            updatePagingAndSortingByOid(hibernateQuery, (ObjectPagingAfterOid) query.getPaging());
        } else {
            updatePagingAndSorting(context, query.getPaging());
        }
    }
}
Also used : Condition(com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery) ObjectPagingAfterOid(com.evolveum.midpoint.repo.sql.ObjectPagingAfterOid) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) RPolyString(com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString)

Aggregations

RootHibernateQuery (com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery)13 QueryException (com.evolveum.midpoint.repo.sql.query.QueryException)7 Condition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition)6 AndCondition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.AndCondition)5 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)4 RPolyString (com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString)3 QName (javax.xml.namespace.QName)3 QueryInterpreter2 (com.evolveum.midpoint.repo.sql.query2.QueryInterpreter2)2 JoinSpecification (com.evolveum.midpoint.repo.sql.query2.hqm.JoinSpecification)2 OrCondition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.OrCondition)2 HqlDataInstance (com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance)2 RUtil.qnameToString (com.evolveum.midpoint.repo.sql.util.RUtil.qnameToString)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 ObjectPagingAfterOid (com.evolveum.midpoint.repo.sql.ObjectPagingAfterOid)1 RObject (com.evolveum.midpoint.repo.sql.data.common.RObject)1 RObjectType (com.evolveum.midpoint.repo.sql.data.common.other.RObjectType)1