예제 #1
0
        public static void PrepareRawSqlArguments(Expression formatArg, Expression?parametersArg, out string format, out IReadOnlyList <Expression> arguments)
        {
            // Consider that FormattableString is used
            if (formatArg.NodeType == ExpressionType.Call)
            {
                var mc = (MethodCallExpression)formatArg;

                if (mc.Arguments[1].NodeType != ExpressionType.NewArrayInit)
                {
                    format = (string)mc.Arguments[0].EvaluateExpression() !;
                    var args = new Expression[mc.Arguments.Count - 1];

                    for (var i = 0; i < args.Length; i++)
                    {
                        args[i] = mc.Arguments[i + 1];
                    }

                    arguments = args;
                }
                else
                {
                    format    = (string)mc.Arguments[0].EvaluateExpression() !;
                    arguments = ((NewArrayExpression)mc.Arguments[1]).Expressions;
                }
            }
            else
            {
                var evaluatedSql = formatArg.EvaluateExpression() !;
#if !NET45
                if (evaluatedSql is FormattableString formattable)
                {
                    format = formattable.Format;

                    var array = formattable.GetArguments();
                    var args  = new Expression[array.Length];

                    for (var i = 0; i < array.Length; i++)
                    {
                        var type = array[i]?.GetType() ?? typeof(object);

                        if (typeof(ISqlExpression).IsAssignableFrom(type))
                        {
                            args[i] = Expression.Constant(array[i]);
                            continue;
                        }

                        Expression expr = Expression.Call(formatArg, ReflectionHelper.Functions.FormattableString.GetArguments, ExpressionInstances.Int32Array(i));

                        if (type != typeof(object))
                        {
                            expr = Expression.Convert(expr, type);
                        }

                        args[i] = expr;
                    }

                    arguments = args;
                }
                else
#endif
                {
                    var rawSqlString = (RawSqlString)evaluatedSql;

                    format = rawSqlString.Format;
                    var arrayExpr = parametersArg !;

                    if (arrayExpr.NodeType == ExpressionType.NewArrayInit)
                    {
                        arguments = ((NewArrayExpression)arrayExpr).Expressions;
                    }
                    else
                    {
                        var array = (object[])arrayExpr.EvaluateExpression() !;
                        var args  = new Expression[array.Length];
                        for (var i = 0; i < array.Length; i++)
                        {
                            var type = array[i]?.GetType() ?? typeof(object);

                            if (typeof(ISqlExpression).IsAssignableFrom(type))
                            {
                                args[i] = Expression.Constant(array[i]);
                                continue;
                            }

                            Expression expr = Expression.ArrayIndex(arrayExpr, ExpressionInstances.Int32(i));
                            if (type != typeof(object))
                            {
                                expr = Expression.Convert(expr, type);
                            }

                            args[i] = expr;
                        }

                        arguments = args;
                    }
                }
            }
        }
예제 #2
0
            public override Expression BuildExpression(Expression?expression, int level, bool enforceServerSide)
            {
                expression = SequenceHelper.CorrectExpression(expression, this, Sequence);

                var expr = Sequence.BuildExpression(expression, level, enforceServerSide);

                if (!Disabled && expression == null)
                {
                    var q =
                        from col in SelectQuery.Select.Columns
                        where !col.CanBeNull
                        select SelectQuery.Select.Columns.IndexOf(col);

                    var idx = q.DefaultIfEmpty(-1).First();

                    if (idx == -1)
                    {
                        idx = SelectQuery.Select.Add(new SqlValue((int?)1));
                        SelectQuery.Select.Columns[idx].RawAlias = "is_empty";
                    }

                    var n = ConvertToParentIndex(idx, this);

                    Expression e = Expression.Call(
                        ExpressionBuilder.DataReaderParam,
                        ReflectionHelper.DataReader.IsDBNull,
                        ExpressionInstances.Int32Array(n));

                    var defaultValue = DefaultValue ?? new DefaultValueExpression(Builder.MappingSchema, expr.Type);

                    if (expr.NodeType == ExpressionType.Parameter)
                    {
                        var par  = (ParameterExpression)expr;
                        var pidx = Builder.BlockVariables.IndexOf(par);

                        if (pidx >= 0)
                        {
                            var ex = Builder.BlockExpressions[pidx];

                            if (ex.NodeType == ExpressionType.Assign)
                            {
                                var bex = (BinaryExpression)ex;

                                if (bex.Left == expr)
                                {
                                    if (bex.Right.NodeType != ExpressionType.Conditional)
                                    {
                                        Builder.BlockExpressions[pidx] =
                                            Expression.Assign(
                                                bex.Left,
                                                Expression.Condition(e, defaultValue, bex.Right));
                                    }
                                }
                            }
                        }
                    }

                    expr = Expression.Condition(e, defaultValue, expr);
                }

                return(expr);
            }
예제 #3
0
        public static void PrepareRawSqlArguments(Expression formatArg, Expression?parametersArg, out string format, out IEnumerable <Expression> arguments)
        {
            // Consider that FormattableString is used
            if (formatArg.NodeType == ExpressionType.Call)
            {
                var mc = (MethodCallExpression)formatArg;

                if (mc.Arguments[1].NodeType != ExpressionType.NewArrayInit)
                {
                    format    = (string)mc.Arguments[0].EvaluateExpression() !;
                    arguments = mc.Arguments.Skip(1).ToArray();
                }
                else
                {
                    format    = (string)mc.Arguments[0].EvaluateExpression() !;
                    arguments = ((NewArrayExpression)mc.Arguments[1]).Expressions;
                }
            }
            else
            {
                var evaluatedSql = formatArg.EvaluateExpression() !;
#if !NET45
                if (evaluatedSql is FormattableString formattable)
                {
                    format = formattable.Format;

                    arguments = formattable.GetArguments().Select((a, i) =>
                    {
                        var type = a?.GetType() ?? typeof(object);

                        if (typeof(ISqlExpression).IsAssignableFrom(type))
                        {
                            return(Expression.Constant(a));
                        }

                        Expression expr = Expression.Call(formatArg, ReflectionHelper.Functions.FormattableString.GetArguments, ExpressionInstances.Int32Array(i));

                        if (type != typeof(object))
                        {
                            expr = Expression.Convert(expr, type);
                        }

                        return(expr);
                    });
                }
                else
#endif
                {
                    var rawSqlString = (RawSqlString)evaluatedSql;

                    format = rawSqlString.Format;
                    var arrayExpr = parametersArg !;

                    if (arrayExpr.NodeType == ExpressionType.NewArrayInit)
                    {
                        arguments = ((NewArrayExpression)arrayExpr).Expressions;
                    }
                    else
                    {
                        var array = (object[])arrayExpr.EvaluateExpression() !;
                        arguments = array.Select((a, i) =>
                        {
                            var type = a?.GetType() ?? typeof(object);

                            if (typeof(ISqlExpression).IsAssignableFrom(type))
                            {
                                return(Expression.Constant(a));
                            }

                            Expression expr = Expression.ArrayIndex(arrayExpr, ExpressionInstances.Int32(i));
                            if (type != typeof(object))
                            {
                                expr = Expression.Convert(expr, type);
                            }

                            return(expr);
                        });
                    }
                }
            }
        }