Search in sources :

Example 1 with RQuery

use of com.evolveum.midpoint.repo.sql.query.RQuery 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 2 with RQuery

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

the class ObjectRetriever method countObjectsAttempt.

public <T extends ObjectType> int countObjectsAttempt(Class<T> type, ObjectQuery query, Collection<SelectorOptions<GetOperationOptions>> options, OperationResult result) {
    LOGGER_PERFORMANCE.debug("> count objects {}", type.getSimpleName());
    int count = 0;
    Session session = null;
    try {
        Class<? extends RObject> hqlType = ClassMapper.getHQLTypeClass(type);
        session = baseHelper.beginReadOnlyTransaction();
        Number longCount;
        if (query == null || query.getFilter() == null) {
            if (GetOperationOptions.isDistinct(SelectorOptions.findRootOptions(options))) {
                // TODO
                throw new UnsupportedOperationException("Distinct option is not supported here");
            }
            // this is 5x faster than count with 3 inner joins, it can probably improved also for queries which
            // filters uses only properties from concrete entities like RUser, RRole by improving interpreter [lazyman]
            SQLQuery sqlQuery = session.createSQLQuery("SELECT COUNT(*) FROM " + RUtil.getTableName(hqlType));
            longCount = (Number) sqlQuery.uniqueResult();
        } else {
            RQuery rQuery;
            QueryEngine2 engine = new QueryEngine2(getConfiguration(), prismContext);
            rQuery = engine.interpret(query, type, options, true, session);
            longCount = (Number) rQuery.uniqueResult();
        }
        LOGGER.trace("Found {} objects.", longCount);
        count = longCount != null ? longCount.intValue() : 0;
        session.getTransaction().commit();
    } catch (QueryException | RuntimeException ex) {
        baseHelper.handleGeneralException(ex, session, result);
    } finally {
        baseHelper.cleanupSessionAndResult(session, result);
    }
    return count;
}
Also used : QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) QueryEngine2(com.evolveum.midpoint.repo.sql.query2.QueryEngine2) RQuery(com.evolveum.midpoint.repo.sql.query.RQuery)

Example 3 with RQuery

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

the class ObjectRetriever method searchObjectsIterativeAttempt.

public <T extends ObjectType> void searchObjectsIterativeAttempt(Class<T> type, ObjectQuery query, ResultHandler<T> handler, Collection<SelectorOptions<GetOperationOptions>> options, OperationResult result) throws SchemaException {
    Session session = null;
    try {
        session = baseHelper.beginReadOnlyTransaction();
        RQuery rQuery;
        QueryEngine2 engine = new QueryEngine2(getConfiguration(), prismContext);
        rQuery = engine.interpret(query, type, options, false, session);
        ScrollableResults results = rQuery.scroll(ScrollMode.FORWARD_ONLY);
        try {
            Iterator<GetObjectResult> iterator = new ScrollableResultsIterator(results);
            while (iterator.hasNext()) {
                GetObjectResult object = iterator.next();
                // TODO treat exceptions encountered within the next call
                PrismObject<T> prismObject = updateLoadedObject(object, type, null, options, null, session, result);
                if (!handler.handle(prismObject, result)) {
                    break;
                }
            }
        } finally {
            if (results != null) {
                results.close();
            }
        }
        session.getTransaction().commit();
    } catch (SchemaException | QueryException | RuntimeException ex) {
        baseHelper.handleGeneralException(ex, session, result);
    } finally {
        baseHelper.cleanupSessionAndResult(session, result);
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) RQuery(com.evolveum.midpoint.repo.sql.query.RQuery) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) QueryEngine2(com.evolveum.midpoint.repo.sql.query2.QueryEngine2)

Example 4 with RQuery

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

the class CertificationCaseHelper method updateLoadedCertificationWorkItem.

public AccessCertificationWorkItemType updateLoadedCertificationWorkItem(GetCertificationWorkItemResult result, // key=OID:ID
Map<String, PrismContainerValue<AccessCertificationCaseType>> casesCache, // key=OID
Map<String, PrismObject<AccessCertificationCampaignType>> campaignsCache, Collection<SelectorOptions<GetOperationOptions>> options, QueryEngine2 engine, Session session, OperationResult operationResult) throws SchemaException, QueryException {
    String campaignOid = result.getCampaignOid();
    Integer caseId = result.getCaseId();
    Integer workItemId = result.getId();
    String caseKey = campaignOid + ":" + caseId;
    PrismContainerValue<AccessCertificationCaseType> casePcv = casesCache.get(caseKey);
    if (casePcv == null) {
        ObjectQuery query = QueryBuilder.queryFor(AccessCertificationCaseType.class, prismContext).ownerId(campaignOid).and().id(caseId).build();
        RQuery caseQuery = engine.interpret(query, AccessCertificationCaseType.class, null, false, session);
        List<GetContainerableResult> cases = caseQuery.list();
        if (cases.size() > 1) {
            throw new IllegalStateException("More than one certification case found for campaign " + campaignOid + ", ID " + caseId);
        } else if (cases.isEmpty()) {
            // we need it, because otherwise we have only identifiers for the work item, no data
            throw new IllegalStateException("No certification case found for campaign " + campaignOid + ", ID " + caseId);
        }
        AccessCertificationCaseType _case = updateLoadedCertificationCase(cases.get(0), campaignsCache, null, session, operationResult);
        casePcv = _case.asPrismContainerValue();
        casesCache.put(caseKey, casePcv);
    }
    @SuppressWarnings({ "raw", "unchecked" }) PrismContainerValue<AccessCertificationWorkItemType> workItemPcv = (PrismContainerValue<AccessCertificationWorkItemType>) casePcv.find(new ItemPath(AccessCertificationCaseType.F_WORK_ITEM, workItemId));
    if (workItemPcv == null) {
        throw new IllegalStateException("No work item " + workItemId + " in " + casePcv);
    } else {
        return workItemPcv.asContainerable();
    }
}
Also used : AccessCertificationCaseType(com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationCaseType) PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) RQuery(com.evolveum.midpoint.repo.sql.query.RQuery) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) AccessCertificationWorkItemType(com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationWorkItemType)

Example 5 with RQuery

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

the class QueryInterpreter2Test method getInterpretedQuery2Whole.

@NotNull
private <T extends Containerable> RQuery getInterpretedQuery2Whole(Session session, Class<T> type, ObjectQuery query, boolean interpretCount, Collection<SelectorOptions<GetOperationOptions>> options) throws QueryException {
    if (query != null) {
        LOGGER.info("QUERY TYPE TO CONVERT :\n{}", (query.getFilter() != null ? query.getFilter().debugDump(3) : null));
    }
    QueryEngine2 engine = new QueryEngine2(baseHelper.getConfiguration(), prismContext);
    RQuery rQuery = engine.interpret(query, type, options, interpretCount, session);
    //just test if DB will handle it or throws some exception
    if (interpretCount) {
        rQuery.uniqueResult();
    } else {
        rQuery.list();
    }
    return rQuery;
}
Also used : QueryEngine2(com.evolveum.midpoint.repo.sql.query2.QueryEngine2) RQuery(com.evolveum.midpoint.repo.sql.query.RQuery) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

RQuery (com.evolveum.midpoint.repo.sql.query.RQuery)8 QueryEngine2 (com.evolveum.midpoint.repo.sql.query2.QueryEngine2)6 QueryException (com.evolveum.midpoint.repo.sql.query.QueryException)5 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)2 RQueryImpl (com.evolveum.midpoint.repo.sql.query2.RQueryImpl)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 NotNull (org.jetbrains.annotations.NotNull)2 PrismContainerValue (com.evolveum.midpoint.prism.PrismContainerValue)1 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)1 QueryParameterValue (com.evolveum.midpoint.repo.sql.query2.hqm.QueryParameterValue)1 RootHibernateQuery (com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery)1 AccessCertificationCaseType (com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationCaseType)1 AccessCertificationWorkItemType (com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationWorkItemType)1 QName (javax.xml.namespace.QName)1 Session (org.hibernate.Session)1 Test (org.testng.annotations.Test)1