Search in sources :

Example 1 with Expression

use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.

the class EmbeddedCollectionPart method toSqlExpression.

@Override
public SqlTuple toSqlExpression(TableGroup tableGroup, Clause clause, SqmToSqlAstConverter walker, SqlAstCreationState sqlAstCreationState) {
    final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
    final List<Expression> expressions = new ArrayList<>();
    getEmbeddableTypeDescriptor().forEachSelectable((columnIndex, selection) -> {
        assert containingTableExpression.equals(selection.getContainingTableExpression());
        final TableReference tableReference = tableGroup.resolveTableReference(tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName()), selection.getContainingTableExpression());
        expressions.add(sqlExpressionResolver.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, selection, sqlAstCreationState.getCreationContext().getSessionFactory())));
    });
    return new SqlTuple(expressions, this);
}
Also used : DomainResultCreationState(org.hibernate.sql.results.graph.DomainResultCreationState) SqlAliasBaseGenerator(org.hibernate.sql.ast.spi.SqlAliasBaseGenerator) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference) JavaType(org.hibernate.type.descriptor.java.JavaType) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) Clause(org.hibernate.sql.ast.Clause) PropertyAccess(org.hibernate.property.access.spi.PropertyAccess) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) ArrayList(java.util.ArrayList) MappingType(org.hibernate.metamodel.mapping.MappingType) SqlAstCreationContext(org.hibernate.sql.ast.spi.SqlAstCreationContext) TableReference(org.hibernate.sql.ast.tree.from.TableReference) ModelPart(org.hibernate.metamodel.mapping.ModelPart) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) FetchOptions(org.hibernate.sql.results.graph.FetchOptions) BiConsumer(java.util.function.BiConsumer) EmbeddableResultImpl(org.hibernate.sql.results.graph.embeddable.internal.EmbeddableResultImpl) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) FetchTiming(org.hibernate.engine.FetchTiming) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) NavigablePath(org.hibernate.query.spi.NavigablePath) DomainResult(org.hibernate.sql.results.graph.DomainResult) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqmToSqlAstConverter(org.hibernate.query.sqm.sql.SqmToSqlAstConverter) EmbeddableValuedFetchable(org.hibernate.sql.results.graph.embeddable.EmbeddableValuedFetchable) Fetch(org.hibernate.sql.results.graph.Fetch) EmbeddableFetchImpl(org.hibernate.sql.results.graph.embeddable.internal.EmbeddableFetchImpl) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Consumer(java.util.function.Consumer) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) FetchStyle(org.hibernate.engine.FetchStyle) List(java.util.List) FromClauseAccess(org.hibernate.sql.ast.spi.FromClauseAccess) StandardVirtualTableGroup(org.hibernate.sql.ast.tree.from.StandardVirtualTableGroup) PropertyAccessStrategyBasicImpl(org.hibernate.property.access.internal.PropertyAccessStrategyBasicImpl) SqlAstJoinType(org.hibernate.sql.ast.SqlAstJoinType) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) FetchParent(org.hibernate.sql.results.graph.FetchParent) TableReference(org.hibernate.sql.ast.tree.from.TableReference) SqlExpressionResolver(org.hibernate.sql.ast.spi.SqlExpressionResolver) Expression(org.hibernate.sql.ast.tree.expression.Expression) ArrayList(java.util.ArrayList) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 2 with Expression

use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.

the class EmbeddedForeignKeyDescriptor method isSimpleJoinPredicate.

@Override
public boolean isSimpleJoinPredicate(Predicate predicate) {
    if (!(predicate instanceof Junction)) {
        return false;
    }
    final Junction junction = (Junction) predicate;
    if (junction.getNature() != Junction.Nature.CONJUNCTION) {
        return false;
    }
    final List<Predicate> predicates = junction.getPredicates();
    if (predicates.size() != keySelectableMappings.getJdbcTypeCount()) {
        return false;
    }
    Boolean lhsIsKey = null;
    for (int i = 0; i < predicates.size(); i++) {
        final Predicate p = predicates.get(i);
        if (!(p instanceof ComparisonPredicate)) {
            return false;
        }
        final ComparisonPredicate comparisonPredicate = (ComparisonPredicate) p;
        if (comparisonPredicate.getOperator() != ComparisonOperator.EQUAL) {
            return false;
        }
        final Expression lhsExpr = comparisonPredicate.getLeftHandExpression();
        final Expression rhsExpr = comparisonPredicate.getRightHandExpression();
        if (!(lhsExpr instanceof ColumnReference) || !(rhsExpr instanceof ColumnReference)) {
            return false;
        }
        final ColumnReference lhs = (ColumnReference) lhsExpr;
        final ColumnReference rhs = (ColumnReference) rhsExpr;
        if (lhsIsKey == null) {
            final String keyExpression = keySelectableMappings.getSelectable(i).getSelectionExpression();
            final String targetExpression = targetSelectableMappings.getSelectable(i).getSelectionExpression();
            if (keyExpression.equals(targetExpression)) {
                if (!lhs.getColumnExpression().equals(keyExpression) || !rhs.getColumnExpression().equals(keyExpression)) {
                    return false;
                }
            } else {
                if (keyExpression.equals(lhs.getColumnExpression())) {
                    if (!targetExpression.equals(rhs.getColumnExpression())) {
                        return false;
                    }
                    lhsIsKey = true;
                } else if (keyExpression.equals(rhs.getColumnExpression())) {
                    if (!targetExpression.equals(lhs.getColumnExpression())) {
                        return false;
                    }
                    lhsIsKey = false;
                } else {
                    return false;
                }
            }
        } else {
            final String lhsSelectionExpression;
            final String rhsSelectionExpression;
            if (lhsIsKey) {
                lhsSelectionExpression = keySelectableMappings.getSelectable(i).getSelectionExpression();
                rhsSelectionExpression = targetSelectableMappings.getSelectable(i).getSelectionExpression();
            } else {
                lhsSelectionExpression = targetSelectableMappings.getSelectable(i).getSelectionExpression();
                rhsSelectionExpression = keySelectableMappings.getSelectable(i).getSelectionExpression();
            }
            if (!lhs.getColumnExpression().equals(lhsSelectionExpression) || !rhs.getColumnExpression().equals(rhsSelectionExpression)) {
                return false;
            }
        }
    }
    return true;
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) Junction(org.hibernate.sql.ast.tree.predicate.Junction) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 3 with Expression

use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.

the class SimpleForeignKeyDescriptor method isSimpleJoinPredicate.

@Override
public boolean isSimpleJoinPredicate(Predicate predicate) {
    if (!(predicate instanceof ComparisonPredicate)) {
        return false;
    }
    final ComparisonPredicate comparisonPredicate = (ComparisonPredicate) predicate;
    if (comparisonPredicate.getOperator() != ComparisonOperator.EQUAL) {
        return false;
    }
    final Expression lhsExpr = comparisonPredicate.getLeftHandExpression();
    final Expression rhsExpr = comparisonPredicate.getRightHandExpression();
    if (!(lhsExpr instanceof ColumnReference) || !(rhsExpr instanceof ColumnReference)) {
        return false;
    }
    final String lhs = ((ColumnReference) lhsExpr).getColumnExpression();
    final String rhs = ((ColumnReference) rhsExpr).getColumnExpression();
    final String keyExpression = keySide.getModelPart().getSelectionExpression();
    final String targetExpression = targetSide.getModelPart().getSelectionExpression();
    return (lhs.equals(keyExpression) && rhs.equals(targetExpression)) || (lhs.equals(targetExpression) && rhs.equals(keyExpression));
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 4 with Expression

use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.

the class OrderingExpression method applyCollation.

static Expression applyCollation(Expression expression, String collation, SqlAstCreationState creationState) {
    final Expression sortExpression;
    if (collation == null) {
        sortExpression = expression;
    } else {
        final QueryEngine queryEngine = creationState.getCreationContext().getSessionFactory().getQueryEngine();
        final SqmToSqlAstConverter converter;
        if (creationState instanceof SqmToSqlAstConverter) {
            converter = (SqmToSqlAstConverter) creationState;
        } else {
            converter = new FakeSqmToSqlAstConverter(creationState);
        }
        sortExpression = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("collate").generateSqmExpression(new SqmSelfRenderingExpression<>(walker -> expression, null, null), null, queryEngine, queryEngine.getTypeConfiguration()).convertToSqlAst(converter);
    }
    return sortExpression;
}
Also used : QueryEngine(org.hibernate.query.spi.QueryEngine) SqmSelfRenderingExpression(org.hibernate.query.sqm.tree.expression.SqmSelfRenderingExpression) FakeSqmToSqlAstConverter(org.hibernate.query.sqm.sql.FakeSqmToSqlAstConverter) SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqmToSqlAstConverter(org.hibernate.query.sqm.sql.SqmToSqlAstConverter) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) SortOrder(org.hibernate.query.sqm.SortOrder) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) NullPrecedence(org.hibernate.query.sqm.NullPrecedence) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) FakeSqmToSqlAstConverter(org.hibernate.query.sqm.sql.FakeSqmToSqlAstConverter) SqmSelfRenderingExpression(org.hibernate.query.sqm.tree.expression.SqmSelfRenderingExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) QueryEngine(org.hibernate.query.spi.QueryEngine) FakeSqmToSqlAstConverter(org.hibernate.query.sqm.sql.FakeSqmToSqlAstConverter) SqmToSqlAstConverter(org.hibernate.query.sqm.sql.SqmToSqlAstConverter)

Example 5 with Expression

use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.

the class OracleSqlAstTranslator method visitOver.

@Override
public void visitOver(Over<?> over) {
    final Expression expression = over.getExpression();
    if (expression instanceof FunctionExpression && "row_number".equals(((FunctionExpression) expression).getFunctionName())) {
        if (over.getPartitions().isEmpty() && over.getOrderList().isEmpty() && over.getStartKind() == FrameKind.UNBOUNDED_PRECEDING && over.getEndKind() == FrameKind.CURRENT_ROW && over.getExclusion() == FrameExclusion.NO_OTHERS) {
            // Oracle doesn't allow an empty over clause for the row_number() function
            append("rownum");
            return;
        }
    }
    super.visitOver(over);
}
Also used : FunctionExpression(org.hibernate.sql.ast.tree.expression.FunctionExpression) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) FunctionExpression(org.hibernate.sql.ast.tree.expression.FunctionExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)

Aggregations

Expression (org.hibernate.sql.ast.tree.expression.Expression)130 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)68 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)67 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)62 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)57 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)54 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)54 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)42 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)37 SqlTuple (org.hibernate.sql.ast.tree.expression.SqlTuple)35 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)34 SelfRenderingSqlFragmentExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression)33 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)32 ArrayList (java.util.ArrayList)31 SqmModifiedSubQueryExpression (org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression)31 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)28 TableReference (org.hibernate.sql.ast.tree.from.TableReference)25 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)23 ComparisonPredicate (org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)23 NavigablePath (org.hibernate.query.spi.NavigablePath)21