Example #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);
        }
Example #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 FixCastErrorExpressionVisitor(
     QuerySqlGeneratorDependencies dependencies,
     RelationalQueryContext queryContext,
     SqlExpressionFactory factory)
 {
     this.sqlFactory   = factory;
     this.queryContext = queryContext;
 }
        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))));
        }
Example #5
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))));
        }
            protected override Expression VisitExtension(Expression extensionExpression)
            {
                // workaround for issue #18492
                var newExpression = base.VisitExtension(extensionExpression);

                if (newExpression is SelectExpression newSelectExpression)
                {
                    var changed      = false;
                    var newPredicate = newSelectExpression.Predicate;
                    var newHaving    = newSelectExpression.Having;
                    if (newSelectExpression.Predicate is SqlConstantExpression predicateConstantExpression &&
                        predicateConstantExpression.Value is bool predicateBoolValue &&
                        !predicateBoolValue)
                    {
                        changed      = true;
                        newPredicate = SqlExpressionFactory.Equal(
                            predicateConstantExpression,
                            SqlExpressionFactory.Constant(true, predicateConstantExpression.TypeMapping));
                    }

                    if (newSelectExpression.Having is SqlConstantExpression havingConstantExpression &&
                        havingConstantExpression.Value is bool havingBoolValue &&
                        !havingBoolValue)
                    {
                        changed   = true;
                        newHaving = SqlExpressionFactory.Equal(
                            havingConstantExpression,
                            SqlExpressionFactory.Constant(true, havingConstantExpression.TypeMapping));
                    }

                    return(changed
                        ? newSelectExpression.Update(
                               newSelectExpression.Projection.ToList(),
                               newSelectExpression.Tables.ToList(),
                               newPredicate,
                               newSelectExpression.GroupBy.ToList(),
                               newHaving,
                               newSelectExpression.Orderings.ToList(),
                               newSelectExpression.Limit,
                               newSelectExpression.Offset,
                               newSelectExpression.IsDistinct,
                               newSelectExpression.Alias)
                        : newSelectExpression);
                }

                return(newExpression);
            }
        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 VisitExtension(Expression extensionExpression)
            {
                if (extensionExpression is SelectExpression selectExpression)
                {
                    var newSelectExpression = (SelectExpression)base.VisitExtension(extensionExpression);

                    // if predicate is optimized to true, we can simply remove it
                    var newPredicate = newSelectExpression.Predicate is SqlConstantExpression newSelectPredicateConstant &&
                                       !(selectExpression.Predicate is SqlConstantExpression)
                            ? (bool)newSelectPredicateConstant.Value
                                ? null
                                : SqlExpressionFactory.Equal(
                        newSelectPredicateConstant,
                        SqlExpressionFactory.Constant(true, newSelectPredicateConstant.TypeMapping))
                            : newSelectExpression.Predicate;

                    var newHaving = newSelectExpression.Having is SqlConstantExpression newSelectHavingConstant &&
                                    !(selectExpression.Having is SqlConstantExpression)
                            ? (bool)newSelectHavingConstant.Value
                                ? null
                                : SqlExpressionFactory.Equal(
                        newSelectHavingConstant,
                        SqlExpressionFactory.Constant(true, newSelectHavingConstant.TypeMapping))
                            : newSelectExpression.Having;

                    return(!ReferenceEquals(newPredicate, newSelectExpression.Predicate) ||
                           !ReferenceEquals(newHaving, newSelectExpression.Having)
                            ? newSelectExpression.Update(
                               newSelectExpression.Projection.ToList(),
                               newSelectExpression.Tables.ToList(),
                               newPredicate,
                               newSelectExpression.GroupBy.ToList(),
                               newHaving,
                               newSelectExpression.Orderings.ToList(),
                               newSelectExpression.Limit,
                               newSelectExpression.Offset,
                               newSelectExpression.IsDistinct,
                               newSelectExpression.Alias)
                            : newSelectExpression);
                }

                return(base.VisitExtension(extensionExpression));
            }
        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);
        }
            protected override Expression VisitSqlUnaryExpression(SqlUnaryExpression sqlUnaryExpression)
            {
                var result = base.VisitSqlUnaryExpression(sqlUnaryExpression);

                if (result is SqlUnaryExpression newUnaryExpression &&
                    newUnaryExpression.Operand is SqlParameterExpression parameterOperand)
                {
                    var parameterValue = _parametersValues[parameterOperand.Name];
                    if (sqlUnaryExpression.OperatorType == ExpressionType.Equal)
                    {
                        return(SqlExpressionFactory.Constant(parameterValue == null, sqlUnaryExpression.TypeMapping));
                    }

                    if (sqlUnaryExpression.OperatorType == ExpressionType.NotEqual)
                    {
                        return(SqlExpressionFactory.Constant(parameterValue != null, sqlUnaryExpression.TypeMapping));
                    }
                }

                return(result);
            }
 public JetObjectToStringTranslator(SqlExpressionFactory sqlExpressionFactory)
 => _sqlExpressionFactory = (JetSqlExpressionFactory)sqlExpressionFactory;
Example #12
0
 public SqlExpression Translate(SqlExpression instance, MemberInfo member, Type returnType, IDiagnosticsLogger <DbLoggerCategory.Query> logger) =>
 instance.Type != ClrType
         ? null
         : SqlExpressionFactory.Convert(instance, returnType);