Search in sources :

Example 1 with UnaryOperation

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

the class BaseSqmToSqlAstConverter method applyScale.

Expression applyScale(Expression magnitude) {
    boolean negate = negativeAdjustment;
    if (magnitude instanceof UnaryOperation) {
        UnaryOperation unary = (UnaryOperation) magnitude;
        if (unary.getOperator() == UNARY_MINUS) {
            // if it's already negated, don't
            // wrap it in another unary minus,
            // just throw away the one we have
            // (OTOH, if it *is* negated, shift
            // the operator to left of scale)
            negate = !negate;
        }
        magnitude = unary.getOperand();
    }
    if (adjustmentScale != null) {
        if (isOne(adjustmentScale)) {
        // no work to do
        } else {
            if (isOne(magnitude)) {
                magnitude = adjustmentScale;
            } else {
                final BasicValuedMapping magnitudeType = (BasicValuedMapping) magnitude.getExpressionType();
                final BasicValuedMapping expressionType;
                if (magnitudeType.getJdbcMapping().getJdbcType().isInterval()) {
                    expressionType = magnitudeType;
                } else {
                    expressionType = widestNumeric((BasicValuedMapping) adjustmentScale.getExpressionType(), magnitudeType);
                }
                magnitude = new BinaryArithmeticExpression(adjustmentScale, MULTIPLY, magnitude, expressionType);
            }
        }
    }
    if (negate) {
        magnitude = new UnaryOperation(UNARY_MINUS, magnitude, (BasicValuedMapping) magnitude.getExpressionType());
    }
    return magnitude;
}
Also used : BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) UnaryOperation(org.hibernate.sql.ast.tree.expression.UnaryOperation) SqmUnaryOperation(org.hibernate.query.sqm.tree.expression.SqmUnaryOperation) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)

Aggregations

BasicValuedMapping (org.hibernate.metamodel.mapping.BasicValuedMapping)1 SqmUnaryOperation (org.hibernate.query.sqm.tree.expression.SqmUnaryOperation)1 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)1 UnaryOperation (org.hibernate.sql.ast.tree.expression.UnaryOperation)1