Search in sources :

Example 1 with QueryEngine2

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

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

use of com.evolveum.midpoint.repo.sql.query2.QueryEngine2 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)

Example 4 with QueryEngine2

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

the class ObjectRetriever method searchContainersAttempt.

public <C extends Containerable> SearchResultList<C> searchContainersAttempt(Class<C> type, ObjectQuery query, Collection<SelectorOptions<GetOperationOptions>> options, OperationResult result) throws SchemaException {
    boolean cases = AccessCertificationCaseType.class.equals(type);
    boolean workItems = AccessCertificationWorkItemType.class.equals(type);
    if (!cases && !workItems) {
        throw new UnsupportedOperationException("Only AccessCertificationCaseType or AccessCertificationWorkItemType is supported here now.");
    }
    LOGGER_PERFORMANCE.debug("> search containers {}", type.getSimpleName());
    List<C> list = new ArrayList<>();
    Session session = null;
    try {
        session = baseHelper.beginReadOnlyTransaction();
        QueryEngine2 engine = new QueryEngine2(getConfiguration(), prismContext);
        RQuery rQuery = engine.interpret(query, type, options, false, session);
        if (cases) {
            List<GetContainerableResult> items = rQuery.list();
            LOGGER.trace("Found {} items (cases), translating to JAXB.", items.size());
            Map<String, PrismObject<AccessCertificationCampaignType>> campaignsCache = new HashMap<>();
            for (GetContainerableResult item : items) {
                @SuppressWarnings({ "raw", "unchecked" }) C value = (C) caseHelper.updateLoadedCertificationCase(item, campaignsCache, options, session, result);
                list.add(value);
            }
        } else {
            assert workItems;
            List<GetCertificationWorkItemResult> items = rQuery.list();
            LOGGER.trace("Found {} work items, translating to JAXB.", items.size());
            Map<String, PrismContainerValue<AccessCertificationCaseType>> casesCache = new HashMap<>();
            Map<String, PrismObject<AccessCertificationCampaignType>> campaignsCache = new HashMap<>();
            for (GetCertificationWorkItemResult item : items) {
                //LOGGER.trace("- {}", item);
                @SuppressWarnings({ "raw", "unchecked" }) C value = (C) caseHelper.updateLoadedCertificationWorkItem(item, casesCache, campaignsCache, options, engine, session, result);
                list.add(value);
            }
        }
        nameResolutionHelper.resolveNamesIfRequested(session, PrismContainerValue.asPrismContainerValues(list), options);
        session.getTransaction().commit();
    } catch (QueryException | RuntimeException ex) {
        baseHelper.handleGeneralException(ex, session, result);
    } finally {
        baseHelper.cleanupSessionAndResult(session, result);
    }
    list.forEach(c -> ObjectTypeUtil.normalizeAllRelations(c.asPrismContainerValue()));
    return new SearchResultList<>(list);
}
Also used : 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 5 with QueryEngine2

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

the class ObjectRetriever method executeQueryDiagnosticsRequest.

public RepositoryQueryDiagResponse executeQueryDiagnosticsRequest(RepositoryQueryDiagRequest request, OperationResult result) {
    LOGGER_PERFORMANCE.debug("> execute query diagnostics {}", request);
    Session session = null;
    try {
        // beware, not all databases support read-only transactions!
        session = baseHelper.beginReadOnlyTransaction();
        final String implementationLevelQuery;
        final Map<String, RepositoryQueryDiagResponse.ParameterValue> implementationLevelQueryParameters;
        final Query query;
        final boolean isMidpointQuery = request.getImplementationLevelQuery() == null;
        if (isMidpointQuery) {
            QueryEngine2 engine = new QueryEngine2(getConfiguration(), prismContext);
            RQueryImpl rQuery = (RQueryImpl) engine.interpret(request.getQuery(), request.getType(), null, false, session);
            query = rQuery.getQuery();
            implementationLevelQuery = query.getQueryString();
            implementationLevelQueryParameters = new HashMap<>();
            for (Map.Entry<String, QueryParameterValue> entry : rQuery.getQuerySource().getParameters().entrySet()) {
                implementationLevelQueryParameters.put(entry.getKey(), new RepositoryQueryDiagResponse.ParameterValue(entry.getValue().getValue(), entry.getValue().toString()));
            }
        } else {
            implementationLevelQuery = (String) request.getImplementationLevelQuery();
            implementationLevelQueryParameters = new HashMap<>();
            query = session.createQuery(implementationLevelQuery);
        }
        List<?> objects = request.isTranslateOnly() ? null : query.list();
        if (isMidpointQuery && objects != null) {
            // raw GetObjectResult instances are useless outside repo-sql-impl module, so we'll convert them to objects
            @SuppressWarnings("unchecked") List<GetObjectResult> listOfGetObjectResults = (List<GetObjectResult>) objects;
            objects = queryResultToPrismObjects(listOfGetObjectResults, request.getType(), null, session, result);
        }
        RepositoryQueryDiagResponse response = new RepositoryQueryDiagResponse(objects, implementationLevelQuery, implementationLevelQueryParameters);
        session.getTransaction().rollback();
        return response;
    } catch (SchemaException | QueryException | RuntimeException ex) {
        baseHelper.handleGeneralException(ex, session, result);
        throw new IllegalStateException("shouldn't get here");
    } finally {
        baseHelper.cleanupSessionAndResult(session, result);
    }
}
Also used : QueryParameterValue(com.evolveum.midpoint.repo.sql.query2.hqm.QueryParameterValue) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) QueryParameterValue(com.evolveum.midpoint.repo.sql.query2.hqm.QueryParameterValue) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) RQuery(com.evolveum.midpoint.repo.sql.query.RQuery) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) QueryEngine2(com.evolveum.midpoint.repo.sql.query2.QueryEngine2) RQueryImpl(com.evolveum.midpoint.repo.sql.query2.RQueryImpl)

Aggregations

RQuery (com.evolveum.midpoint.repo.sql.query.RQuery)6 QueryEngine2 (com.evolveum.midpoint.repo.sql.query2.QueryEngine2)6 QueryException (com.evolveum.midpoint.repo.sql.query.QueryException)5 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 NotNull (org.jetbrains.annotations.NotNull)2 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)1 RQueryImpl (com.evolveum.midpoint.repo.sql.query2.RQueryImpl)1 QueryParameterValue (com.evolveum.midpoint.repo.sql.query2.hqm.QueryParameterValue)1