use of com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery in project midpoint by Evolveum.
the class ItemPathResolver method addJoin.
String addJoin(JpaLinkDefinition joinedItemDefinition, String currentHqlPath) throws QueryException {
RootHibernateQuery hibernateQuery = context.getHibernateQuery();
String joinedItemJpaName = joinedItemDefinition.getJpaName();
String joinedItemFullPath = currentHqlPath + "." + joinedItemJpaName;
String joinedItemAlias;
if (!joinedItemDefinition.isMultivalued()) {
/*
* Let's check if we were already here i.e. if we had already created this join.
* This is to avoid useless creation of redundant joins for singleton items.
*
* But how can we be sure that item is singleton if we look only at the last segment (which is single-valued)?
* Imagine we are creating join for single-valued entity of u.abc.(...).xyz.ent where
* ent is single-valued and not embedded (so we are to create something like "left join u.abc.(...).xyz.ent e").
* Then "u" is certainly a single value: either the root object, or some value pointed to by Exists restriction.
* Also, abc, ..., xyz are surely single-valued: otherwise they would be connected by a join. So,
* u.abc.(...).xyz.ent is a singleton.
*
* Look at it in other way: if e.g. xyz was multivalued, then we would have something like:
* left join u.abc.(...).uvw.xyz x
* left join x.ent e
* And, therefore we would not be looking for u.abc.(...).xyz.ent.
*/
JoinSpecification existingJoin = hibernateQuery.getPrimaryEntity().findJoinFor(joinedItemFullPath);
if (existingJoin != null) {
// but let's check the condition as well
String existingAlias = existingJoin.getAlias();
// we have to create condition for existing alias, to be matched to existing condition
Condition conditionForExistingAlias = createJoinCondition(existingAlias, joinedItemDefinition, hibernateQuery);
if (ObjectUtils.equals(conditionForExistingAlias, existingJoin.getCondition())) {
LOGGER.trace("Reusing alias '{}' for joined path '{}'", existingAlias, joinedItemFullPath);
return existingAlias;
}
}
}
joinedItemAlias = hibernateQuery.createAlias(joinedItemDefinition);
Condition condition = createJoinCondition(joinedItemAlias, joinedItemDefinition, hibernateQuery);
hibernateQuery.getPrimaryEntity().addJoin(new JoinSpecification(joinedItemAlias, joinedItemFullPath, condition));
return joinedItemAlias;
}
use of com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery in project midpoint by Evolveum.
the class ExistsRestriction method interpret.
@Override
public Condition interpret() throws QueryException {
HqlDataInstance dataInstance = getItemPathResolver().resolveItemPath(filter.getFullPath(), filter.getDefinition(), getBaseHqlEntity(), false);
boolean isAll = filter.getFilter() == null || filter.getFilter() instanceof AllFilter;
JpaDataNodeDefinition jpaDefinition = dataInstance.getJpaDefinition();
if (!isAll) {
if (!(jpaDefinition instanceof JpaEntityDefinition)) {
// partially checked already (for non-null-ness)
throw new QueryException("ExistsRestriction with non-empty subfilter points to non-entity node: " + jpaDefinition);
}
setHqlDataInstance(dataInstance);
QueryInterpreter2 interpreter = context.getInterpreter();
return interpreter.interpretFilter(context, filter.getFilter(), this);
} else if (jpaDefinition instanceof JpaPropertyDefinition && (((JpaPropertyDefinition) jpaDefinition).isCount())) {
RootHibernateQuery hibernateQuery = context.getHibernateQuery();
return hibernateQuery.createSimpleComparisonCondition(dataInstance.getHqlPath(), 0, ">");
} else {
// TODO support exists also for other properties (single valued or multi valued)
throw new UnsupportedOperationException("Exists filter with 'all' subfilter is currently not supported");
}
}
use of com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery in project midpoint by Evolveum.
the class ItemValueRestriction method addIsNotNullIfNecessary.
/**
* Filter of type NOT(PROPERTY=VALUE) causes problems when there are entities with PROPERTY set to NULL.
*
* Such a filter has to be treated like
*
* NOT (PROPERTY=VALUE & PROPERTY IS NOT NULL)
*
* TODO implement for restrictions other than PropertyRestriction.
*/
protected Condition addIsNotNullIfNecessary(Condition condition, String propertyPath) {
if (condition instanceof IsNullCondition || condition instanceof IsNotNullCondition) {
return condition;
}
if (!isNegated()) {
return condition;
}
RootHibernateQuery hibernateQuery = getContext().getHibernateQuery();
AndCondition conjunction = hibernateQuery.createAnd();
conjunction.add(condition);
conjunction.add(hibernateQuery.createIsNotNull(propertyPath));
return conjunction;
}
use of com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery 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);
}
}
use of com.evolveum.midpoint.repo.sql.query2.hqm.RootHibernateQuery in project midpoint by Evolveum.
the class QueryInterpreter2 method interpretPagingAndSorting.
private void interpretPagingAndSorting(InterpretationContext context, ObjectQuery query, boolean countingObjects) throws QueryException {
RootHibernateQuery hibernateQuery = context.getHibernateQuery();
String rootAlias = hibernateQuery.getPrimaryEntityAlias();
if (query != null && query.getPaging() instanceof ObjectPagingAfterOid) {
ObjectPagingAfterOid paging = (ObjectPagingAfterOid) query.getPaging();
if (paging.getOidGreaterThan() != null) {
Condition c = hibernateQuery.createSimpleComparisonCondition(rootAlias + ".oid", paging.getOidGreaterThan(), ">");
hibernateQuery.addCondition(c);
}
}
if (!countingObjects && query != null && query.getPaging() != null) {
if (query.getPaging() instanceof ObjectPagingAfterOid) {
// very special case - ascending ordering by OID (nothing more)
updatePagingAndSortingByOid(hibernateQuery, (ObjectPagingAfterOid) query.getPaging());
} else {
updatePagingAndSorting(context, query.getPaging());
}
}
}
Aggregations