Exemplo n.º 1
0
        protected override Expression VisitUnary(UnaryExpression unaryExpression)
        {
            Check.NotNull(unaryExpression, nameof(unaryExpression));

            if (unaryExpression.NodeType == ExpressionType.ArrayLength &&
                unaryExpression.Operand.Type == typeof(byte[]))
            {
                return(base.Visit(unaryExpression.Operand) is SqlExpression sqlExpression
                    ? SqlExpressionFactory.Function("length", new[] { sqlExpression }, typeof(int))
                    : null);
            }

            var visitedExpression = base.VisitUnary(unaryExpression);

            if (visitedExpression == null)
            {
                return(null);
            }

            if (visitedExpression is SqlUnaryExpression sqlUnary &&
                sqlUnary.OperatorType == ExpressionType.Negate)
            {
                var operandType = GetProviderType(sqlUnary.Operand);
                if (operandType == typeof(decimal) ||
                    operandType == typeof(TimeSpan))
                {
                    return(null);
                }
            }

            return(visitedExpression);
        }
Exemplo n.º 2
0
        protected override Expression VisitUnary(UnaryExpression unaryExpression)
        {
            if (unaryExpression.NodeType == ExpressionType.ArrayLength &&
                unaryExpression.Operand.Type == typeof(byte[]))
            {
                var sqlExpression = base.Visit(unaryExpression.Operand) as SqlExpression;

                if (sqlExpression == null)
                {
                    return(null);
                }

                var isBinaryMaxDataType   = GetProviderType(sqlExpression) == "varbinary(max)" || sqlExpression is SqlParameterExpression;
                var dataLengthSqlFunction = SqlExpressionFactory.Function(
                    "DATALENGTH",
                    new[] { sqlExpression },
                    nullable: true,
                    argumentsPropagateNullability: new bool[] { true },
                    isBinaryMaxDataType ? typeof(long) : typeof(int));

                return(isBinaryMaxDataType
                    ? (Expression)SqlExpressionFactory.Convert(dataLengthSqlFunction, typeof(int))
                    : dataLengthSqlFunction);
            }

            return(base.VisitUnary(unaryExpression));
        }
        public override SqlExpression TranslateLongCount(Expression expression = null)
        {
            if (expression != null)
            {
                // TODO: Translate Count with predicate for GroupBy
                return(null);
            }

            return(SqlExpressionFactory.ApplyDefaultTypeMapping(
                       SqlExpressionFactory.Function("COUNT_BIG", new[] { SqlExpressionFactory.Fragment("*") }, typeof(long))));
        }
Exemplo n.º 4
0
        public override SqlExpression TranslateLongCount(Expression expression = null)
        {
            if (expression != null)
            {
                // TODO: Translate Count with predicate for GroupBy
                return(null);
            }

            return(SqlExpressionFactory.ApplyDefaultTypeMapping(
                       SqlExpressionFactory.Function(
                           "COUNT_BIG",
                           new[] { SqlExpressionFactory.Fragment("*") },
                           nullResultAllowed: true,
                           argumentsPropagateNullability: new[] { false },
                           typeof(long))));
        }
        private Expression DoDecimalCompare(SqlExpression visitedExpression, ExpressionType op, SqlExpression left, SqlExpression right)
        {
            var actual = SqlExpressionFactory.Function(
                name: "ef_compare",
                new[] { left, right },
                nullable: true,
                new[] { true, true },
                typeof(int));
            var oracle = SqlExpressionFactory.Constant(value: 0);

            return(op switch
            {
                ExpressionType.GreaterThan => SqlExpressionFactory.GreaterThan(left: actual, right: oracle),
                ExpressionType.GreaterThanOrEqual => SqlExpressionFactory.GreaterThanOrEqual(left: actual, right: oracle),
                ExpressionType.LessThan => SqlExpressionFactory.LessThan(left: actual, right: oracle),
                ExpressionType.LessThanOrEqual => SqlExpressionFactory.LessThanOrEqual(left: actual, right: oracle),
                _ => visitedExpression
            });
        protected override Expression VisitBinary(BinaryExpression binaryExpression)
        {
            Check.NotNull(binaryExpression, nameof(binaryExpression));

            var visitedExpression = (SqlExpression)base.VisitBinary(binaryExpression);

            if (visitedExpression == null)
            {
                return(null);
            }

            if (visitedExpression is SqlBinaryExpression sqlBinary)
            {
                if (sqlBinary.OperatorType == ExpressionType.Modulo &&
                    (_functionModuloTypes.Contains(GetProviderType(sqlBinary.Left)) ||
                     _functionModuloTypes.Contains(GetProviderType(sqlBinary.Right))))
                {
                    return(SqlExpressionFactory.Function(
                               "ef_mod",
                               new[] { sqlBinary.Left, sqlBinary.Right },
                               nullable: true,
                               argumentsPropagateNullability: new[] { true, true },
                               visitedExpression.Type,
                               visitedExpression.TypeMapping));
                }

                if (AttemptDecimalCompare(sqlBinary))
                {
                    return(DoDecimalCompare(visitedExpression, sqlBinary.OperatorType, sqlBinary.Left, sqlBinary.Right));
                }

                if (_restrictedBinaryExpressions.TryGetValue(sqlBinary.OperatorType, out var restrictedTypes) &&
                    (restrictedTypes.Contains(GetProviderType(sqlBinary.Left)) ||
                     restrictedTypes.Contains(GetProviderType(sqlBinary.Right))))
                {
                    return(null);
                }
            }

            return(visitedExpression);
        }