Search in sources :

Example 16 with QueryException

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

Example 17 with QueryException

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

the class ObjectRetriever method searchObjectsAttempt.

@NotNull
public <T extends ObjectType> SearchResultList<PrismObject<T>> searchObjectsAttempt(Class<T> type, ObjectQuery query, Collection<SelectorOptions<GetOperationOptions>> options, OperationResult result) throws SchemaException {
    LOGGER_PERFORMANCE.debug("> search objects {}", new Object[] { type.getSimpleName() });
    Session session = null;
    try {
        session = baseHelper.beginReadOnlyTransaction();
        RQuery rQuery;
        QueryEngine2 engine = new QueryEngine2(getConfiguration(), prismContext);
        rQuery = engine.interpret(query, type, options, false, session);
        List<GetObjectResult> queryResult = rQuery.list();
        LOGGER.trace("Found {} objects, translating to JAXB.", queryResult != null ? queryResult.size() : 0);
        List<PrismObject<T>> list = queryResultToPrismObjects(queryResult, type, options, session, result);
        session.getTransaction().commit();
        return new SearchResultList<>(list);
    } catch (QueryException | RuntimeException ex) {
        baseHelper.handleGeneralException(ex, session, result);
        throw new IllegalStateException("shouldn't get here");
    } finally {
        baseHelper.cleanupSessionAndResult(session, result);
    }
}
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) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with QueryException

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

the class QueryInterpreter2 method findAndCreateRestrictionInternal.

private <T extends ObjectFilter> Restriction findAndCreateRestrictionInternal(T filter, InterpretationContext context, Restriction parent, ItemPathResolver resolver, JpaEntityDefinition baseEntityDefinition) throws QueryException {
    if (filter instanceof AndFilter) {
        return new AndRestriction(context, (AndFilter) filter, baseEntityDefinition, parent);
    } else if (filter instanceof OrFilter) {
        return new OrRestriction(context, (OrFilter) filter, baseEntityDefinition, parent);
    } else if (filter instanceof NotFilter) {
        return new NotRestriction(context, (NotFilter) filter, baseEntityDefinition, parent);
    } else if (filter instanceof FullTextFilter) {
        return new FullTextRestriction(context, (FullTextFilter) filter, baseEntityDefinition, parent);
    } else if (filter instanceof InOidFilter) {
        return new InOidRestriction(context, (InOidFilter) filter, baseEntityDefinition, parent);
    } else if (filter instanceof OrgFilter) {
        return new OrgRestriction(context, (OrgFilter) filter, baseEntityDefinition, parent);
    } else if (filter instanceof TypeFilter) {
        TypeFilter typeFilter = (TypeFilter) filter;
        JpaEntityDefinition refinedEntityDefinition = resolver.findRestrictedEntityDefinition(baseEntityDefinition, typeFilter.getType());
        return new TypeRestriction(context, typeFilter, refinedEntityDefinition, parent);
    } else if (filter instanceof ExistsFilter) {
        ExistsFilter existsFilter = (ExistsFilter) filter;
        ItemPath path = existsFilter.getFullPath();
        ItemDefinition definition = existsFilter.getDefinition();
        ProperDataSearchResult<JpaDataNodeDefinition> searchResult = resolver.findProperDataDefinition(baseEntityDefinition, path, definition, JpaDataNodeDefinition.class, context.getPrismContext());
        if (searchResult == null) {
            throw new QueryException("Path for ExistsFilter (" + path + ") doesn't point to a hibernate entity or property within " + baseEntityDefinition);
        }
        return new ExistsRestriction(context, existsFilter, searchResult.getEntityDefinition(), parent);
    } else if (filter instanceof RefFilter) {
        RefFilter refFilter = (RefFilter) filter;
        ItemPath path = refFilter.getFullPath();
        ItemDefinition definition = refFilter.getDefinition();
        ProperDataSearchResult<JpaReferenceDefinition> searchResult = resolver.findProperDataDefinition(baseEntityDefinition, path, definition, JpaReferenceDefinition.class, context.getPrismContext());
        if (searchResult == null) {
            throw new QueryException("Path for RefFilter (" + path + ") doesn't point to a reference item within " + baseEntityDefinition);
        }
        return new ReferenceRestriction(context, refFilter, searchResult.getEntityDefinition(), parent, searchResult.getLinkDefinition());
    } else if (filter instanceof PropertyValueFilter) {
        PropertyValueFilter valFilter = (PropertyValueFilter) filter;
        ItemPath path = valFilter.getFullPath();
        ItemDefinition definition = valFilter.getDefinition();
        ProperDataSearchResult<JpaPropertyDefinition> propDefRes = resolver.findProperDataDefinition(baseEntityDefinition, path, definition, JpaPropertyDefinition.class, context.getPrismContext());
        if (propDefRes == null) {
            throw new QueryException("Couldn't find a proper data item to query, given base entity " + baseEntityDefinition + " and this filter: " + valFilter.debugDump());
        }
        // TODO can't be unified?
        if (propDefRes.getTargetDefinition() instanceof JpaAnyPropertyDefinition) {
            return new AnyPropertyRestriction(context, valFilter, propDefRes.getEntityDefinition(), parent, propDefRes.getLinkDefinition());
        } else {
            return new PropertyRestriction(context, valFilter, propDefRes.getEntityDefinition(), parent, propDefRes.getLinkDefinition());
        }
    } else if (filter instanceof NoneFilter || filter instanceof AllFilter || filter instanceof UndefinedFilter) {
        // these should be filtered out by the client
        throw new IllegalStateException("Trivial filters are not supported by QueryInterpreter: " + filter.debugDump());
    } else {
        throw new IllegalStateException("Unknown filter: " + filter.debugDump());
    }
}
Also used : ItemDefinition(com.evolveum.midpoint.prism.ItemDefinition) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) ProperDataSearchResult(com.evolveum.midpoint.repo.sql.query2.resolution.ProperDataSearchResult) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 19 with QueryException

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

the class JpaEntityDefinition method nextLinkDefinition.

@Override
public DataSearchResult<?> nextLinkDefinition(ItemPath path, ItemDefinition<?> itemDefinition, PrismContext prismContext) throws QueryException {
    if (ItemPath.isNullOrEmpty(path)) {
        // doesn't fulfill precondition
        return null;
    }
    ItemPathSegment first = path.first();
    if (first instanceof IdItemPathSegment) {
        throw new QueryException("ID item path segments are not allowed in query: " + path);
    } else if (first instanceof ObjectReferencePathSegment) {
        throw new QueryException("'@' path segment cannot be used in the context of an entity " + this);
    }
    JpaLinkDefinition<?> link = findRawLinkDefinition(path, JpaDataNodeDefinition.class, false);
    if (link == null) {
        return null;
    } else {
        link.resolveEntityPointer();
        return new DataSearchResult<>(link, path.tail(link.getItemPath().size()));
    }
}
Also used : ItemPathSegment(com.evolveum.midpoint.prism.path.ItemPathSegment) IdItemPathSegment(com.evolveum.midpoint.prism.path.IdItemPathSegment) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) IdItemPathSegment(com.evolveum.midpoint.prism.path.IdItemPathSegment) DataSearchResult(com.evolveum.midpoint.repo.sql.query2.resolution.DataSearchResult) ObjectReferencePathSegment(com.evolveum.midpoint.prism.path.ObjectReferencePathSegment)

Example 20 with QueryException

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

the class OrgRestriction method interpret.

@Override
public Condition interpret() throws QueryException {
    RootHibernateQuery hibernateQuery = getContext().getHibernateQuery();
    if (filter.isRoot()) {
        // oid in (select descendantOid from ROrgClosure group by descendantOid having count(descendantOid) = 1)
        return hibernateQuery.createIn(getBaseHqlEntity().getHqlPath() + ".oid", "select descendantOid from ROrgClosure group by descendantOid having count(descendantOid) = 1");
    }
    if (filter.getOrgRef() == null) {
        throw new QueryException("No organization reference defined in the search query.");
    }
    if (filter.getOrgRef().getOid() == null) {
        throw new QueryException("No oid specified in organization reference " + filter.getOrgRef().debugDump());
    }
    String orgOidParamName = hibernateQuery.addParameter("orgOid", filter.getOrgRef().getOid());
    // oid in ...
    String oidQueryText;
    switch(filter.getScope()) {
        case ONE_LEVEL:
            oidQueryText = // TODO distinct(ref.ownerOid) ? (was in original QueryInterpreter)
            "select ref.ownerOid " + "from RObjectReference ref " + "where " + "ref.referenceType = " + nameOf(RReferenceOwner.OBJECT_PARENT_ORG) + " and " + "ref.targetOid = :" + orgOidParamName;
            break;
        case ANCESTORS:
            oidQueryText = "select c.ancestorOid " + "from ROrgClosure c " + "where " + "c.ancestorOid != :" + orgOidParamName + " and " + "c.descendantOid = :" + orgOidParamName;
            break;
        case SUBTREE:
        default:
            oidQueryText = "select ref.ownerOid " + "from RObjectReference ref " + "where " + "ref.referenceType = " + nameOf(RReferenceOwner.OBJECT_PARENT_ORG) + " and " + "ref.targetOid in (" + "select descendantOid from ROrgClosure where ancestorOid = :" + orgOidParamName + ")";
    }
    return hibernateQuery.createIn(getBaseHqlEntity().getHqlPath() + ".oid", oidQueryText);
}
Also used : QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) RootHibernateQuery(com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery)

Aggregations

QueryException (com.evolveum.midpoint.repo.sql.query.QueryException)26 RQuery (com.evolveum.midpoint.repo.sql.query.RQuery)5 QueryEngine2 (com.evolveum.midpoint.repo.sql.query2.QueryEngine2)5 RootHibernateQuery (com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery)5 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)4 Condition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.Condition)4 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 QName (javax.xml.namespace.QName)4 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)3 JpaEntityDefinition (com.evolveum.midpoint.repo.sql.query2.definition.JpaEntityDefinition)3 OrCondition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.OrCondition)3 HqlDataInstance (com.evolveum.midpoint.repo.sql.query2.resolution.HqlDataInstance)3 EqualFilter (com.evolveum.midpoint.prism.query.EqualFilter)2 QueryDefinitionRegistry2 (com.evolveum.midpoint.repo.sql.query2.QueryDefinitionRegistry2)2 JpaPropertyDefinition (com.evolveum.midpoint.repo.sql.query2.definition.JpaPropertyDefinition)2 AndCondition (com.evolveum.midpoint.repo.sql.query2.hqm.condition.AndCondition)2 DataSearchResult (com.evolveum.midpoint.repo.sql.query2.resolution.DataSearchResult)2 ItemDefinition (com.evolveum.midpoint.prism.ItemDefinition)1 PrismReferenceValue (com.evolveum.midpoint.prism.PrismReferenceValue)1 IdItemPathSegment (com.evolveum.midpoint.prism.path.IdItemPathSegment)1