use of org.hibernate.query.sqm.tree.select.SqmSelection in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitSelection.
@Override
public Void visitSelection(SqmSelection<?> sqmSelection) {
final List<Map.Entry<String, DomainResultProducer<?>>> resultProducers;
final SqmSelectableNode<?> selectionNode = sqmSelection.getSelectableNode();
if (selectionNode instanceof SqmJpaCompoundSelection<?>) {
final SqmJpaCompoundSelection<?> selectableNode = (SqmJpaCompoundSelection<?>) selectionNode;
resultProducers = new ArrayList<>(selectableNode.getSelectionItems().size());
for (SqmSelectableNode<?> selectionItem : selectableNode.getSelectionItems()) {
if (selectionItem instanceof SqmPath<?>) {
prepareForSelection((SqmPath<?>) selectionItem);
}
resultProducers.add(new AbstractMap.SimpleEntry<>(selectionItem.getAlias(), (DomainResultProducer<?>) selectionItem.accept(this)));
}
} else {
if (selectionNode instanceof SqmPath<?>) {
prepareForSelection((SqmPath<?>) selectionNode);
}
resultProducers = Collections.singletonList(new AbstractMap.SimpleEntry<>(sqmSelection.getAlias(), (DomainResultProducer<?>) selectionNode.accept(this)));
}
final Stack<SqlAstProcessingState> processingStateStack = getProcessingStateStack();
final boolean needsDomainResults = domainResults != null && currentClauseContributesToTopLevelSelectClause();
final boolean collectDomainResults;
if (processingStateStack.depth() == 1) {
collectDomainResults = needsDomainResults;
} else {
final SqlAstProcessingState current = processingStateStack.getCurrent();
// Since we only want to create domain results for the first/left-most query spec within query groups,
// we have to check if the current query spec is the left-most.
// This is the case when all upper level in-flight query groups are still empty
collectDomainResults = needsDomainResults && processingStateStack.findCurrentFirst(processingState -> {
if (!(processingState instanceof SqlAstQueryPartProcessingState)) {
return Boolean.FALSE;
}
if (processingState == current) {
return null;
}
final QueryPart part = ((SqlAstQueryPartProcessingState) processingState).getInflightQueryPart();
if (part instanceof QueryGroup) {
if (((QueryGroup) part).getQueryParts().isEmpty()) {
return null;
}
}
return Boolean.FALSE;
}) == null;
}
// arguments
if (collectDomainResults) {
resultProducers.forEach(entry -> {
if (!(entry.getValue() instanceof DynamicInstantiation<?>)) {
currentSqlSelectionCollector().next();
}
domainResults.add(entry.getValue().createDomainResult(entry.getKey(), this));
});
} else if (needsDomainResults) {
// We just create domain results for the purpose of creating selections
// This is necessary for top-level query specs within query groups to avoid cycles
resultProducers.forEach(entry -> {
if (!(entry.getValue() instanceof DynamicInstantiation<?>)) {
currentSqlSelectionCollector().next();
}
entry.getValue().createDomainResult(entry.getKey(), this);
});
} else {
resultProducers.forEach(entry -> {
if (!(entry.getValue() instanceof DynamicInstantiation<?>)) {
currentSqlSelectionCollector().next();
}
entry.getValue().applySqlSelections(this);
});
}
return null;
}
use of org.hibernate.query.sqm.tree.select.SqmSelection in project hibernate-orm by hibernate.
the class QuerySqmImpl method verifyInsertTypesMatch.
private void verifyInsertTypesMatch(String hqlString, SqmInsertStatement<R> sqmStatement) {
final List<SqmPath<?>> insertionTargetPaths = sqmStatement.getInsertionTargetPaths();
if (sqmStatement instanceof SqmInsertValuesStatement<?>) {
final SqmInsertValuesStatement<R> statement = (SqmInsertValuesStatement<R>) sqmStatement;
for (SqmValues sqmValues : statement.getValuesList()) {
verifyInsertTypesMatch(hqlString, insertionTargetPaths, sqmValues.getExpressions());
}
} else {
final SqmInsertSelectStatement<R> statement = (SqmInsertSelectStatement<R>) sqmStatement;
final List<SqmSelection<?>> selections = statement.getSelectQueryPart().getFirstQuerySpec().getSelectClause().getSelections();
verifyInsertTypesMatch(hqlString, insertionTargetPaths, selections);
statement.getSelectQueryPart().validateQueryStructureAndFetchOwners();
}
}
use of org.hibernate.query.sqm.tree.select.SqmSelection in project hibernate-orm by hibernate.
the class ASTParserLoadingTest method testAliases.
@Test
public void testAliases() {
Session s = openSession();
Transaction t = s.beginTransaction();
Animal a = new Animal();
a.setBodyWeight(12.4f);
a.setDescription("an animal");
s.persist(a);
Query<?> q = s.createQuery("select a.bodyWeight as abw, a.description from Animal a");
SqmSelectStatement<?> sqmStatement = (SqmSelectStatement<?>) q.unwrap(QuerySqmImpl.class).getSqmStatement();
List<SqmSelection<?>> selections = sqmStatement.getQuerySpec().getSelectClause().getSelections();
assertThat(selections.size(), is(2));
assertThat(selections.get(0).getAlias(), is("abw"));
assertThat(selections.get(1).getAlias(), nullValue());
q = s.createQuery("select count(*), avg(a.bodyWeight) as avg from Animal a");
sqmStatement = (SqmSelectStatement<?>) q.unwrap(QuerySqmImpl.class).getSqmStatement();
selections = sqmStatement.getQuerySpec().getSelectClause().getSelections();
assertThat(selections.size(), is(2));
assertThat(selections.get(0), notNullValue());
assertThat(selections.get(0).getAlias(), nullValue());
assertThat(selections.get(0).getSelectableNode(), instanceOf(SqmFunction.class));
assertThat(((SqmFunction) selections.get(0).getSelectableNode()).getFunctionName(), is("count"));
assertThat(selections.get(1), notNullValue());
assertThat(selections.get(1).getAlias(), notNullValue());
assertThat(selections.get(1).getAlias(), is("avg"));
assertThat(selections.get(1).getSelectableNode(), instanceOf(SqmFunction.class));
assertThat(((SqmFunction) selections.get(1).getSelectableNode()).getFunctionName(), is("avg"));
s.delete(a);
t.commit();
s.close();
}
use of org.hibernate.query.sqm.tree.select.SqmSelection in project hibernate-orm by hibernate.
the class AttributePathTests method testImplicitJoinReuse2.
@Test
public void testImplicitJoinReuse2() {
final SqmSelectStatement<?> statement = interpretSelect("select s.mate from Person s where s.mate.dob = ?1");
assertThat(statement.getQuerySpec().getFromClause().getRoots().size(), is(1));
final SqmRoot<?> sqmRoot = statement.getQuerySpec().getFromClause().getRoots().get(0);
assertThat(sqmRoot.getJoins().size(), is(0));
assertThat(sqmRoot.getReusablePaths().size(), is(1));
final SqmSelection selection = statement.getQuerySpec().getSelectClause().getSelections().get(0);
assertThat(selection.getSelectableNode(), instanceOf(SqmEntityValuedSimplePath.class));
final SqmPath selectExpression = (SqmPath) selection.getSelectableNode();
assertThat(selectExpression.getReferencedPathSource().getSqmPathType(), instanceOf(EntityDomainType.class));
final SqmComparisonPredicate predicate = (SqmComparisonPredicate) statement.getQuerySpec().getWhereClause().getPredicate();
final SqmPath predicateLhs = (SqmPath) predicate.getLeftHandExpression();
assertThat(predicateLhs.getLhs(), notNullValue());
// from-clause paths
// assertPropertyPath( space.getRoot(), "com.acme.Something(s)" );
// assertPropertyPath( space.getJoins().get( 0 ), "com.acme.Something(s).entity" );
// expression paths
assertPropertyPath((SqmExpression) selection.getSelectableNode(), Person.class.getName() + "(s).mate");
assertPropertyPath(predicateLhs, Person.class.getName() + "(s).mate.dob");
}
Aggregations