Search in sources :

Example 6 with Expression

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

the class DB2SqlAstTranslator method visitAnsiCaseSearchedExpression.

// DB2 does not allow CASE expressions where all result arms contain plain parameters.
// At least one result arm must provide some type context for inference,
// so we cast the first result arm if we encounter this condition
@Override
protected void visitAnsiCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression, Consumer<Expression> resultRenderer) {
    if (getParameterRenderingMode() == SqlAstNodeRenderingMode.DEFAULT && areAllResultsParameters(caseSearchedExpression)) {
        final List<CaseSearchedExpression.WhenFragment> whenFragments = caseSearchedExpression.getWhenFragments();
        final Expression firstResult = whenFragments.get(0).getResult();
        super.visitAnsiCaseSearchedExpression(caseSearchedExpression, e -> {
            if (e == firstResult) {
                renderCasted(e);
            } else {
                resultRenderer.accept(e);
            }
        });
    } else {
        super.visitAnsiCaseSearchedExpression(caseSearchedExpression, resultRenderer);
    }
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)

Example 7 with Expression

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

the class DB2SqlAstTranslator method visitAnsiCaseSimpleExpression.

@Override
protected void visitAnsiCaseSimpleExpression(CaseSimpleExpression caseSimpleExpression, Consumer<Expression> resultRenderer) {
    if (getParameterRenderingMode() == SqlAstNodeRenderingMode.DEFAULT && areAllResultsParameters(caseSimpleExpression)) {
        final List<CaseSimpleExpression.WhenFragment> whenFragments = caseSimpleExpression.getWhenFragments();
        final Expression firstResult = whenFragments.get(0).getResult();
        super.visitAnsiCaseSimpleExpression(caseSimpleExpression, e -> {
            if (e == firstResult) {
                renderCasted(e);
            } else {
                resultRenderer.accept(e);
            }
        });
    } else {
        super.visitAnsiCaseSimpleExpression(caseSimpleExpression, resultRenderer);
    }
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)

Example 8 with Expression

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

the class DerbySqlAstTranslator method visitAnsiCaseSearchedExpression.

// Derby does not allow CASE expressions where all result arms contain plain parameters.
// At least one result arm must provide some type context for inference,
// so we cast the first result arm if we encounter this condition
@Override
protected void visitAnsiCaseSearchedExpression(CaseSearchedExpression caseSearchedExpression, Consumer<Expression> resultRenderer) {
    if (getParameterRenderingMode() == SqlAstNodeRenderingMode.DEFAULT && areAllResultsParameters(caseSearchedExpression)) {
        final List<CaseSearchedExpression.WhenFragment> whenFragments = caseSearchedExpression.getWhenFragments();
        final Expression firstResult = whenFragments.get(0).getResult();
        super.visitAnsiCaseSearchedExpression(caseSearchedExpression, e -> {
            if (e == firstResult) {
                renderCasted(e);
            } else {
                resultRenderer.accept(e);
            }
        });
    } else {
        super.visitAnsiCaseSearchedExpression(caseSearchedExpression, resultRenderer);
    }
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)

Example 9 with Expression

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

the class AbstractCompositeIdentifierMapping method toSqlExpression.

@Override
public SqlTuple toSqlExpression(TableGroup tableGroup, Clause clause, SqmToSqlAstConverter walker, SqlAstCreationState sqlAstCreationState) {
    final SelectableMappings selectableMappings = getEmbeddableTypeDescriptor();
    final List<ColumnReference> columnReferences = CollectionHelper.arrayList(selectableMappings.getJdbcTypeCount());
    final NavigablePath navigablePath = tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName());
    final TableReference defaultTableReference = tableGroup.resolveTableReference(navigablePath, getContainingTableExpression());
    getEmbeddableTypeDescriptor().forEachSelectable((columnIndex, selection) -> {
        final TableReference tableReference = defaultTableReference.resolveTableReference(selection.getContainingTableExpression()) != null ? defaultTableReference : tableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
        final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference.getIdentificationVariable(), selection, sqlAstCreationState.getCreationContext().getSessionFactory()));
        columnReferences.add((ColumnReference) columnReference);
    });
    return new SqlTuple(columnReferences, this);
}
Also used : TableReference(org.hibernate.sql.ast.tree.from.TableReference) SelectableMappings(org.hibernate.metamodel.mapping.SelectableMappings) NavigablePath(org.hibernate.query.spi.NavigablePath) Expression(org.hibernate.sql.ast.tree.expression.Expression) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 10 with Expression

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

the class AbstractSqlAstTranslator method visitNullnessPredicate.

@Override
public void visitNullnessPredicate(NullnessPredicate nullnessPredicate) {
    final Expression expression = nullnessPredicate.getExpression();
    final String predicateValue;
    if (nullnessPredicate.isNegated()) {
        predicateValue = " is not null";
    } else {
        predicateValue = " is null";
    }
    final SqlTuple tuple;
    if ((tuple = SqlTupleContainer.getSqlTuple(expression)) != null) {
        String separator = NO_SEPARATOR;
        // as the embeddable is not considered as null, if at least one sub-part is not null
        if (nullnessPredicate.isNegated() && expression.getExpressionType() instanceof AttributeMapping) {
            appendSql('(');
            for (Expression exp : tuple.getExpressions()) {
                appendSql(separator);
                exp.accept(this);
                appendSql(predicateValue);
                separator = " or ";
            }
            appendSql(')');
        } else // For the is null check, and also for tuples in SQL in general,
        // the semantics is that all sub-parts must match the predicate
        {
            for (Expression exp : tuple.getExpressions()) {
                appendSql(separator);
                exp.accept(this);
                appendSql(predicateValue);
                separator = " and ";
            }
        }
    } else {
        expression.accept(this);
        appendSql(predicateValue);
    }
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) OrderedSetAggregateFunctionExpression(org.hibernate.sql.ast.tree.expression.OrderedSetAggregateFunctionExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) SqlTuple(org.hibernate.sql.ast.tree.expression.SqlTuple)

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