Exemplo n.º 1
0
        protected override SqlGenerate Select(MemberExpression expression, SqlGenerate sqlGenerate)
        {
            if (IsEnumerable(expression))
            {
                var result = SqlExpressionCompiler.Evaluate(expression);
                var fields = (result as IEnumerable).Flatten();
                if (fields?.Count > 0)
                {
                    sqlGenerate.SelectFields.AddRange(fields.Select(field => field.ToString().ParamSql(sqlGenerate)));
                }
                else
                {
                    sqlGenerate.SelectFields = new List <string>()
                    {
                        "*"
                    }
                };

                return(sqlGenerate);
            }

            string fieldName = expression.Member.GetFieldName();

            if (string.IsNullOrEmpty(fieldName))
            {
                return(sqlGenerate);
            }

            sqlGenerate.SelectFields.Add(fieldName.ParamSql(sqlGenerate));
            return(sqlGenerate);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取Value
        /// </summary>
        /// <param name="binding"></param>
        /// <returns></returns>
        public static object GetValue(this MemberBinding binding)
        {
            switch (binding.BindingType)
            {
            case MemberBindingType.Assignment:
                var assign = (MemberAssignment)binding;
                if (assign.Expression is ConstantExpression constant)
                {
                    return(constant.Value);
                }

                try
                {
                    return(SqlExpressionCompiler.Evaluate(assign.Expression));
                }
                catch
                {
                    var member = Expression.Convert(assign.Expression, typeof(object));
                    var lambda = Expression.Lambda <Func <object> >(member);
                    var getter = lambda.Compile();
                    return(getter());
                }
            }
            return(null);
        }
Exemplo n.º 3
0
        protected override SqlGenerate In(MemberExpression expression, SqlGenerate sqlGenerate)
        {
            var result = SqlExpressionCompiler.Evaluate(expression);
            var inArgs = (result as IEnumerable).Flatten();

            sqlGenerate.AddDbParameter(inArgs);
            return(sqlGenerate);
        }
Exemplo n.º 4
0
        private static void StartsWith(MethodCallExpression expression, SqlGenerate sqlGenerate)
        {
            SqlExpressionProvider.Where(expression.Object, sqlGenerate);
            SqlExpressionProvider.Where(expression.Arguments[0], sqlGenerate);
            sqlGenerate += " like ";

            var val = SqlExpressionCompiler.Evaluate(expression.Arguments[0]);

            sqlGenerate.AddDbParameter($"{val}%");
        }
Exemplo n.º 5
0
        protected override SqlGenerate In(NewArrayExpression expression, SqlGenerate sqlGenerate)
        {
            var list = new List <object>();

            foreach (var expressionItem in expression.Expressions)
            {
                var obj = SqlExpressionCompiler.Evaluate(expressionItem);
                list.Add(obj);
            }
            sqlGenerate.AddDbParameter(list);
            return(sqlGenerate);
        }
Exemplo n.º 6
0
        protected override SqlGenerate In(ListInitExpression expression, SqlGenerate sqlGenerate)
        {
            var list = new List <object>();

            foreach (var elementInit in expression.Initializers)
            {
                foreach (var expre in elementInit.Arguments)
                {
                    var obj = SqlExpressionCompiler.Evaluate(expre);
                    list.Add(obj);
                }
            }
            sqlGenerate.AddDbParameter(list);
            return(sqlGenerate);
        }
Exemplo n.º 7
0
        protected override SqlGenerate Update(MemberExpression expression, SqlGenerate sqlGenerate)
        {
            var obj = SqlExpressionCompiler.Evaluate(expression);

            if (obj == null)
            {
                throw new ArgumentException($"Expression '{expression}' accesses unsupported property '{expression.Member}' of Nullable<T>");
            }

            if (obj.GetType().IsValueType)
            {
                throw new ArgumentException($"Expression '{expression}' accesses unsupported valuetype");
            }

            if (obj is IDictionary dictionary)
            {
                foreach (string key in dictionary.Keys)
                {
                    sqlGenerate += $"{key.ParamSql(sqlGenerate)} = ";
                    sqlGenerate.AddDbParameter(dictionary[key]);
                    sqlGenerate += ",";
                }
            }
            else
            {
                var pis = obj.GetType().GetProperties();
                foreach (var p in pis)
                {
                    string fieldName = p.GetFieldName();
                    if (string.IsNullOrEmpty(fieldName))
                    {
                        continue;
                    }

                    sqlGenerate += $"{fieldName.ParamSql(sqlGenerate)} = ";
                    sqlGenerate.AddDbParameter(p.GetValue(obj));
                    sqlGenerate += ",";
                }
            }

            if (sqlGenerate[sqlGenerate.Length - 1] == ',')
            {
                sqlGenerate.Sql.Remove(sqlGenerate.Length - 1, 1);
            }

            return(sqlGenerate);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 获取Fields
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expr"></param>
        /// <returns></returns>
        public static string[] GetFieldNames <T>(this Expression <Func <T, object> > expr)
        {
            if (expr == null)
            {
                return(null);
            }

            if (expr.Body is MemberExpression member)
            {
                if (member.Member.DeclaringType.IsAssignableFrom(typeof(T)))
                {
                    return new[] { member.Member.Name }
                }
                ;

                var array = SqlExpressionCompiler.Evaluate(member);
                if (array is IEnumerable <string> strEnum)
                {
                    return(strEnum.ToArray());
                }
            }

            if (expr.Body is NewExpression newExpr)
            {
                return(newExpr.Arguments.OfType <MemberExpression>().Select(x => x.Member.Name).ToArray());
            }

            if (expr.Body is MemberInitExpression init)
            {
                return(init.Bindings.Select(x => x.Member.Name).ToArray());
            }

            if (expr.Body is UnaryExpression unary)
            {
                member = unary.Operand as MemberExpression;
                if (member != null)
                {
                    return new[] { member.Member.Name }
                }
                ;
            }

            throw new ArgumentException("Invalid Fields List Expression: " + expr);
        }
Exemplo n.º 9
0
        protected override SqlGenerate Update(NewExpression expression, SqlGenerate sqlGenerate)
        {
            for (int i = 0; i < expression.Members.Count; i++)
            {
                var m = expression.Members[i];

                string fieldName = m.GetFieldName();
                if (string.IsNullOrEmpty(fieldName))
                {
                    continue;
                }

                sqlGenerate += $"{fieldName.ParamSql(sqlGenerate)} = ";

                var val = SqlExpressionCompiler.Evaluate(expression.Arguments[i]);
                sqlGenerate.AddDbParameter(val);
                sqlGenerate += ",";
            }
            if (sqlGenerate[^ 1] == ',')
Exemplo n.º 10
0
        private static object ResolveMember(MemberExpression expression)
        {
            if (expression.Expression != null)
            {
                if (expression.Member.DeclaringType.IsNullableType())
                {
                    return(null);
                }

                if (expression.IsParameterOrConvertAccess())
                {
                    return(expression.Member.Name);
                }
            }

            var val = SqlExpressionCompiler.Evaluate(expression);

            return(val);
        }
Exemplo n.º 11
0
        private static void Contains(MethodCallExpression expression, SqlGenerate sqlGenerate)
        {
            if (IsStaticArrayMethod(expression))
            {
                DoStaticArrayMethodCall(expression, sqlGenerate);
                return;
            }
            if (IsEnumerableMethod(expression))
            {
                DoEnumerableMethodCall(expression, sqlGenerate);
                return;
            }

            SqlExpressionProvider.Where(expression.Object, sqlGenerate);
            sqlGenerate += " like ";
            var val = SqlExpressionCompiler.Evaluate(expression.Arguments[0]);

            sqlGenerate.AddDbParameter($"%{val}%");
        }
Exemplo n.º 12
0
        protected override SqlGenerate Select(ListInitExpression expression, SqlGenerate sqlGenerate)
        {
            foreach (var elementInit in expression.Initializers)
            {
                foreach (var expre in elementInit.Arguments)
                {
                    var obj = SqlExpressionCompiler.Evaluate(expre);
                    if (obj == null)
                    {
                        continue;
                    }

                    var fieldName = obj.ToString();
                    if (string.IsNullOrEmpty(fieldName))
                    {
                        continue;
                    }
                    sqlGenerate.SelectFields.Add(fieldName.ParamSql(sqlGenerate));
                }
            }
            return(sqlGenerate);
        }
Exemplo n.º 13
0
        protected override SqlGenerate Where(MemberExpression expression, SqlGenerate sqlGenerate)
        {
            if (expression.Expression != null)
            {
                if (expression.Member.DeclaringType.IsNullableType())
                {
                    if (expression.Member.Name == "Value") //Can't use C# 6 yet: nameof(Nullable<bool>.Value)
                    {
                        SqlExpressionProvider.Where(expression.Expression, sqlGenerate);
                        return(sqlGenerate);
                    }
                    if (expression.Member.Name == "HasValue")
                    {
                        var doesNotEqualNull = Expression.MakeBinary(ExpressionType.NotEqual, expression.Expression, Expression.Constant(null));
                        SqlExpressionProvider.Where(doesNotEqualNull, sqlGenerate);
                        return(sqlGenerate);
                    }
                    throw new ArgumentException($"Expression '{expression}' accesses unsupported property '{expression.Member}' of Nullable<T>");
                }

                if (expression.IsParameterOrConvertAccess())
                {
                    string fieldName = expression.Member.GetFieldName();
                    if (string.IsNullOrEmpty(fieldName))
                    {
                        return(sqlGenerate);
                    }

                    sqlGenerate += $" {fieldName.ParamSql(sqlGenerate)}";
                    return(sqlGenerate);
                }
            }

            var val = SqlExpressionCompiler.Evaluate(expression);

            sqlGenerate.AddDbParameter(val);
            return(sqlGenerate);
        }