Esempio n. 1
0
        protected HqlTreeNode VisitBinaryExpression(BinaryExpression expression)
        {
            if (expression.NodeType == ExpressionType.Equal)
            {
                return(TranslateEqualityComparison(expression));
            }
            if (expression.NodeType == ExpressionType.NotEqual)
            {
                return(TranslateInequalityComparison(expression));
            }

            _nullableExpressionDetector.SearchForNotNullMemberChecks(expression);

            var lhs = VisitExpression(expression.Left).AsExpression();
            var rhs = VisitExpression(expression.Right).AsExpression();

            switch (expression.NodeType)
            {
            case ExpressionType.And:
                return(_hqlTreeBuilder.BitwiseAnd(lhs, rhs));

            case ExpressionType.AndAlso:
                return(_hqlTreeBuilder.BooleanAnd(lhs.ToBooleanExpression(), rhs.ToBooleanExpression()));

            case ExpressionType.Or:
                return(_hqlTreeBuilder.BitwiseOr(lhs, rhs));

            case ExpressionType.OrElse:
                return(_hqlTreeBuilder.BooleanOr(lhs.ToBooleanExpression(), rhs.ToBooleanExpression()));

            case ExpressionType.Add:
                if (expression.Left.Type == typeof(string) && expression.Right.Type == typeof(string))
                {
                    return(_hqlTreeBuilder.MethodCall("concat", lhs, rhs));
                }
                return(_hqlTreeBuilder.Add(lhs, rhs));

            case ExpressionType.Subtract:
                return(_hqlTreeBuilder.Subtract(lhs, rhs));

            case ExpressionType.Multiply:
                return(_hqlTreeBuilder.Multiply(lhs, rhs));

            case ExpressionType.Divide:
                return(_hqlTreeBuilder.Divide(lhs, rhs));

            case ExpressionType.Modulo:
                return(_hqlTreeBuilder.MethodCall("mod", lhs, rhs));

            case ExpressionType.LessThan:
                return(_hqlTreeBuilder.LessThan(lhs, rhs));

            case ExpressionType.LessThanOrEqual:
                return(_hqlTreeBuilder.LessThanOrEqual(lhs, rhs));

            case ExpressionType.GreaterThan:
                return(_hqlTreeBuilder.GreaterThan(lhs, rhs));

            case ExpressionType.GreaterThanOrEqual:
                return(_hqlTreeBuilder.GreaterThanOrEqual(lhs, rhs));

            case ExpressionType.Coalesce:
                return(_hqlTreeBuilder.Coalesce(lhs.ToArithmeticExpression(), rhs.ToArithmeticExpression()));
            }

            throw new InvalidOperationException();
        }
Esempio n. 2
0
        protected HqlTreeNode VisitBinaryExpression(BinaryExpression expression)
        {
            // There are some cases where we do not want to add a sql cast:
            // - When comparing numeric types that do not have their own operator (e.g. short == short)
            // - When comparing a member expression with a parameter of similar type (e.g. o.Short == intParameter)
            var leftType  = GetExpressionType(expression.Left);
            var rightType = GetExpressionType(expression.Right);

            if (leftType != null && leftType == rightType)
            {
                _notCastableExpressions[expression.Left]  = leftType;
                _notCastableExpressions[expression.Right] = rightType;
            }

            if (expression.NodeType == ExpressionType.Equal)
            {
                return(TranslateEqualityComparison(expression));
            }
            if (expression.NodeType == ExpressionType.NotEqual)
            {
                return(TranslateInequalityComparison(expression));
            }

            _nullableExpressionDetector.SearchForNotNullMemberChecks(expression);

            var lhs = VisitExpression(expression.Left).AsExpression();
            var rhs = VisitExpression(expression.Right).AsExpression();

            switch (expression.NodeType)
            {
            case ExpressionType.And:
                return(_hqlTreeBuilder.BitwiseAnd(lhs, rhs));

            case ExpressionType.AndAlso:
                return(_hqlTreeBuilder.BooleanAnd(lhs.ToBooleanExpression(), rhs.ToBooleanExpression()));

            case ExpressionType.Or:
                return(_hqlTreeBuilder.BitwiseOr(lhs, rhs));

            case ExpressionType.OrElse:
                return(_hqlTreeBuilder.BooleanOr(lhs.ToBooleanExpression(), rhs.ToBooleanExpression()));

            case ExpressionType.Add:
                if (expression.Left.Type == typeof(string) && expression.Right.Type == typeof(string))
                {
                    return(_hqlTreeBuilder.MethodCall("concat", lhs, rhs));
                }
                return(_hqlTreeBuilder.Add(lhs, rhs));

            case ExpressionType.Subtract:
                return(_hqlTreeBuilder.Subtract(lhs, rhs));

            case ExpressionType.Multiply:
                return(_hqlTreeBuilder.Multiply(lhs, rhs));

            case ExpressionType.Divide:
                return(_hqlTreeBuilder.Divide(lhs, rhs));

            case ExpressionType.Modulo:
                return(_hqlTreeBuilder.MethodCall("mod", lhs, rhs));

            case ExpressionType.LessThan:
                return(_hqlTreeBuilder.LessThan(lhs, rhs));

            case ExpressionType.LessThanOrEqual:
                return(_hqlTreeBuilder.LessThanOrEqual(lhs, rhs));

            case ExpressionType.GreaterThan:
                return(_hqlTreeBuilder.GreaterThan(lhs, rhs));

            case ExpressionType.GreaterThanOrEqual:
                return(_hqlTreeBuilder.GreaterThanOrEqual(lhs, rhs));

            case ExpressionType.Coalesce:
                return(_hqlTreeBuilder.Coalesce(lhs.ToArithmeticExpression(), rhs.ToArithmeticExpression()));
            }

            throw new InvalidOperationException();
        }