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);
}
}
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);
}
}
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());
}
}
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()));
}
}
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);
}
Aggregations