Skip to content

Commit

Permalink
added support for ref+target filter and ownedBy for old repo
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Jun 24, 2022
1 parent 86d05d5 commit 01d10d2
Show file tree
Hide file tree
Showing 35 changed files with 1,075 additions and 474 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import com.evolveum.midpoint.repo.sql.query.QueryEngine;
import com.evolveum.midpoint.repo.sql.query.RQuery;
import com.evolveum.midpoint.repo.sql.query.RQueryImpl;
import com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery;
import com.evolveum.midpoint.repo.sql.query.hqm.HibernateQuery;
import com.evolveum.midpoint.repo.sql.type.XMLGregorianCalendarType;
import com.evolveum.midpoint.repo.sql.util.RUtil;
import com.evolveum.midpoint.repo.sqlbase.QueryException;
Expand Down Expand Up @@ -489,7 +489,7 @@ public void test018QueryGenericLongTwice() throws Exception {
.build();

RQuery realQuery = getInterpretedQueryWhole(session, GenericObjectType.class, query, false, null);
RootHibernateQuery source = ((RQueryImpl) realQuery).getQuerySource();
HibernateQuery source = ((RQueryImpl) realQuery).getQuerySource();
String real = ((RQueryImpl) realQuery).getQuery().getQueryString();

// note l and l2 cannot be merged as they point to different extension properties (intType, longType)
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.repo.sql.data.common.dictionary.ExtItemDictionary;
import com.evolveum.midpoint.repo.sql.query.definition.JpaEntityDefinition;
import com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery;
import com.evolveum.midpoint.repo.sql.query.hqm.HibernateQuery;
import com.evolveum.midpoint.repo.sql.query.resolution.ItemPathResolver;
import com.evolveum.midpoint.repo.sqlbase.QueryException;
import com.evolveum.midpoint.repo.sqlbase.SupportedDatabase;
Expand All @@ -34,7 +34,7 @@ public class InterpretationContext {

private final Class<? extends Containerable> type;

private final RootHibernateQuery hibernateQuery;
private HibernateQuery hibernateQuery; // de-facto final

/**
* Definition of the root entity. Root entity corresponds to the ObjectType class that was requested
Expand All @@ -47,6 +47,24 @@ public InterpretationContext(QueryInterpreter interpreter, Class<? extends Conta
PrismContext prismContext, RelationRegistry relationRegistry,
ExtItemDictionary extItemDictionary, Session session, SupportedDatabase databaseType)
throws QueryException {
this(interpreter, type, prismContext, relationRegistry, extItemDictionary, session);

this.hibernateQuery = new HibernateQuery(rootEntityDefinition, databaseType);
}

public InterpretationContext(QueryInterpreter interpreter, Class<? extends Containerable> type,
PrismContext prismContext, RelationRegistry relationRegistry,
ExtItemDictionary extItemDictionary, Session session, HibernateQuery parentQuery)
throws QueryException {
this(interpreter, type, prismContext, relationRegistry, extItemDictionary, session);

this.hibernateQuery = parentQuery.createSubquery(rootEntityDefinition);
}

private InterpretationContext(QueryInterpreter interpreter, Class<? extends Containerable> type,
PrismContext prismContext, RelationRegistry relationRegistry,
ExtItemDictionary extItemDictionary, Session session)
throws QueryException {

Objects.requireNonNull(interpreter, "interpreter");
Objects.requireNonNull(type, "type");
Expand All @@ -64,13 +82,11 @@ public InterpretationContext(QueryInterpreter interpreter, Class<? extends Conta

QueryDefinitionRegistry registry = QueryDefinitionRegistry.getInstance();

// This is a preliminary information. It can change (be narrowed) during filter interpretation, e.g. from RObject to RUser.
// This is preliminary information. It can change (be narrowed) during filter interpretation, e.g. from RObject to RUser.
// Unfortunately, it's not that easy to postpone HibernateQuery creation, because individual filters may require it -
// even before some ItemValueRestriction requests the narrowing.

rootEntityDefinition = registry.findEntityDefinition(type);

this.hibernateQuery = new RootHibernateQuery(rootEntityDefinition, databaseType);
}

public PrismContext getPrismContext() {
Expand All @@ -93,7 +109,7 @@ public Class<? extends Containerable> getType() {
return type;
}

public RootHibernateQuery getHibernateQuery() {
public HibernateQuery getHibernateQuery() {
return hibernateQuery;
}

Expand All @@ -116,4 +132,9 @@ public String getPrimaryEntityAlias() {
public ExtItemDictionary getExtItemDictionary() {
return extItemDictionary;
}

public InterpretationContext createSubcontext(Class<? extends Containerable> type) throws QueryException {
return new InterpretationContext(interpreter, type, prismContext,
relationRegistry, extItemDictionary, session, hibernateQuery);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.repo.sql.SqlRepositoryConfiguration;
import com.evolveum.midpoint.repo.sql.data.common.dictionary.ExtItemDictionary;
import com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery;
import com.evolveum.midpoint.repo.sql.query.hqm.HibernateQuery;
import com.evolveum.midpoint.repo.sqlbase.QueryException;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.RelationRegistry;
Expand Down Expand Up @@ -53,7 +53,7 @@ public RQuery interpret(ObjectQuery query, Class<? extends Containerable> type,
query = refineAssignmentHolderQuery(type, query);

QueryInterpreter interpreter = new QueryInterpreter(repoConfiguration, extItemDictionary);
RootHibernateQuery hibernateQuery = interpreter.interpret(query, type, options, prismContext, relationRegistry, countingObjects, session);
HibernateQuery hibernateQuery = interpreter.interpret(query, type, options, prismContext, relationRegistry, countingObjects, session);
Query<?> hqlQuery = hibernateQuery.getAsHqlQuery(session);

if (LOGGER.isTraceEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.evolveum.midpoint.repo.sql.helpers.ObjectRetriever;
import com.evolveum.midpoint.repo.sql.query.definition.*;
import com.evolveum.midpoint.repo.sql.query.hqm.CountProjectionElement;
import com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery;
import com.evolveum.midpoint.repo.sql.query.hqm.HibernateQuery;
import com.evolveum.midpoint.repo.sql.query.hqm.condition.Condition;
import com.evolveum.midpoint.repo.sql.query.matcher.DefaultMatcher;
import com.evolveum.midpoint.repo.sql.query.matcher.Matcher;
Expand Down Expand Up @@ -97,7 +97,7 @@ public QueryInterpreter(SqlRepositoryConfiguration repoConfiguration, ExtItemDic
this.extItemDictionary = extItemDictionary;
}

public RootHibernateQuery interpret(ObjectQuery query, @NotNull Class<? extends Containerable> type,
public HibernateQuery interpret(ObjectQuery query, @NotNull Class<? extends Containerable> type,
Collection<SelectorOptions<GetOperationOptions>> options, @NotNull PrismContext prismContext,
@NotNull RelationRegistry relationRegistry, boolean countingObjects, @NotNull Session session) throws QueryException {
boolean distinctRequested = GetOperationOptions.isDistinct(SelectorOptions.findRootOptions(options));
Expand All @@ -112,7 +112,7 @@ public RootHibernateQuery interpret(ObjectQuery query, @NotNull Class<? extends

if (countingObjects) {
interpretPagingAndSorting(context, query, true);
RootHibernateQuery hibernateQuery = context.getHibernateQuery();
HibernateQuery hibernateQuery = context.getHibernateQuery();
boolean distinct = distinctRequested && !hibernateQuery.isDistinctNotNecessary();
hibernateQuery.addProjectionElement(new CountProjectionElement(resultStyle.getCountString(rootAlias), distinct));
return hibernateQuery;
Expand All @@ -128,7 +128,7 @@ public RootHibernateQuery interpret(ObjectQuery query, @NotNull Class<? extends
u.oid in (select distinct u.oid from RUser u where ...)
*/
boolean distinctBlobCapable = !repoConfiguration.isUsingOracle() && !repoConfiguration.isUsingSQLServer();
RootHibernateQuery hibernateQuery = context.getHibernateQuery();
HibernateQuery hibernateQuery = context.getHibernateQuery();
boolean distinct = distinctRequested && !hibernateQuery.isDistinctNotNecessary();
hibernateQuery.setDistinct(distinct);
hibernateQuery.addProjectionElementsFor(resultStyle.getIdentifiers(rootAlias));
Expand All @@ -147,7 +147,7 @@ u.oid in (select distinct u.oid from RUser u where ...)
interpretQueryFilter(wrapperContext, query);
interpretPagingAndSorting(wrapperContext, query, false);
}
RootHibernateQuery wrapperQuery = wrapperContext.getHibernateQuery();
HibernateQuery wrapperQuery = wrapperContext.getHibernateQuery();
if (repoConfiguration.isUsingSQLServer() && resultStyle.getIdentifiers("").size() > 1) {
// using 'where exists' clause
// FIXME refactor this ugly code
Expand Down Expand Up @@ -295,6 +295,8 @@ Restriction findAndCreateRestrictionInternal(T filter, InterpretationContext con
} else {
return new PropertyRestriction(context, valFilter, propDefRes.getEntityDefinition(), parent, propDefRes.getLinkDefinition());
}
} else if (filter instanceof OwnedByFilter) {
return OwnedByRestriction.create(context, (OwnedByFilter) filter, baseEntityDefinition);
} 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());
Expand All @@ -304,7 +306,7 @@ Restriction findAndCreateRestrictionInternal(T filter, InterpretationContext con
}

private void interpretPagingAndSorting(InterpretationContext context, ObjectQuery query, boolean countingObjects) throws QueryException {
RootHibernateQuery hibernateQuery = context.getHibernateQuery();
HibernateQuery hibernateQuery = context.getHibernateQuery();
String rootAlias = hibernateQuery.getPrimaryEntityAlias();

//noinspection StringEquality
Expand All @@ -323,7 +325,7 @@ private void interpretPagingAndSorting(InterpretationContext context, ObjectQuer
}
}

private void updatePagingAndSortingByOid(RootHibernateQuery hibernateQuery, ObjectPaging paging) {
private void updatePagingAndSortingByOid(HibernateQuery hibernateQuery, ObjectPaging paging) {
String rootAlias = hibernateQuery.getPrimaryEntityAlias();
if (paging.getPrimaryOrderingPath() != null || paging.getPrimaryOrderingDirection() != null || paging.getOffset() != null) {
throw new IllegalArgumentException("orderBy, direction nor offset is allowed on ObjectPaging with cookie");
Expand All @@ -342,7 +344,7 @@ private void updatePagingAndSorting(InterpretationContext context, ObjectPaging
if (paging == null) {
return;
}
RootHibernateQuery hibernateQuery = context.getHibernateQuery();
HibernateQuery hibernateQuery = context.getHibernateQuery();
if (paging.getOffset() != null) {
hibernateQuery.setFirstResult(paging.getOffset());
}
Expand Down Expand Up @@ -397,7 +399,7 @@ private void addOrdering(InterpretationContext context, ObjectOrdering ordering)
hqlPropertyPath += ".orig";
}

RootHibernateQuery hibernateQuery = context.getHibernateQuery();
HibernateQuery hibernateQuery = context.getHibernateQuery();
if (ordering.getDirection() != null) {
switch (ordering.getDirection()) {
case ASCENDING:
Expand Down Expand Up @@ -447,7 +449,7 @@ private void addGrouping(InterpretationContext context, ObjectGrouping grouping)
hqlPropertyPath += ".orig";
}

RootHibernateQuery hibernateQuery = context.getHibernateQuery();
HibernateQuery hibernateQuery = context.getHibernateQuery();

hibernateQuery.addGrouping(hqlPropertyPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
import org.hibernate.ScrollableResults;
import org.hibernate.query.Query;

import com.evolveum.midpoint.repo.sql.query.hqm.RootHibernateQuery;
import com.evolveum.midpoint.repo.sql.query.hqm.HibernateQuery;

/**
* @author lazyman
*/
public class RQueryImpl implements RQuery {

private final RootHibernateQuery querySource; // only for diagnostic purposes
private final HibernateQuery querySource; // only for diagnostic purposes
private final Query<?> query;

public RQueryImpl(Query<?> query, RootHibernateQuery querySource) {
public RQueryImpl(Query<?> query, HibernateQuery querySource) {
Objects.requireNonNull(query, "Query must not be null.");
this.query = query;
this.querySource = querySource;
Expand All @@ -51,7 +51,7 @@ public Query<?> getQuery() {
return query;
}

public RootHibernateQuery getQuerySource() {
public HibernateQuery getQuerySource() {
return querySource;
}
}

0 comments on commit 01d10d2

Please sign in to comment.