Esempio n. 1
0
        protected override SqlGenerate In(MemberExpression expression, SqlGenerate sqlGenerate)
        {
            var result = SqlExpressionCompiler.Evaluate(expression);
            var inArgs = (result as IEnumerable).Flatten();

            sqlGenerate += "(";
            if (inArgs?.Count > 0)
            {
                foreach (var item in inArgs)
                {
                    sqlGenerate.AddDbParameter(item);
                    sqlGenerate += ",";
                }
                if (sqlGenerate.Sql.ToString().EndsWith(","))
                {
                    sqlGenerate.Sql.Remove(sqlGenerate.Sql.Length - 1, 1);
                }
            }
            else
            {
                sqlGenerate += "NULL";
            }
            sqlGenerate += ")";
            return(sqlGenerate);
        }
        /// <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 (Exception ex)
                {
                    var member = Expression.Convert(assign.Expression, typeof(object));
                    var lambda = Expression.Lambda <Func <object> >(member);
                    var getter = lambda.Compile();
                    return(getter());
                }
            }
            return(null);
        }
Esempio n. 3
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())
                {
                    sqlGenerate += $" {expression.Member.Name.ParamSql(sqlGenerate)}";
                    return(sqlGenerate);
                }
            }

            var val = SqlExpressionCompiler.Evaluate(expression);

            sqlGenerate.AddDbParameter(val);
            return(sqlGenerate);
        }
        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}%");
        }
        /// <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);
        }
    }
}
Esempio n. 6
0
        protected override SqlGenerate Update(NewExpression expression, SqlGenerate sqlGenerate)
        {
            for (int i = 0; i < expression.Members.Count; i++)
            {
                var m = expression.Members[i];
                sqlGenerate += $"{m.Name.ParamSql(sqlGenerate)} = ";

                var val = SqlExpressionCompiler.Evaluate(expression.Arguments[i]);
                sqlGenerate.AddDbParameter(val);
                sqlGenerate += ",";
            }
            if (sqlGenerate[sqlGenerate.Length - 1] == ',')
            {
                sqlGenerate.Sql.Remove(sqlGenerate.Length - 1, 1);
            }

            return(sqlGenerate);
        }
Esempio n. 7
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);
        }
        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}%");
        }
Esempio n. 9
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)
            {
                IDictionary dictionary = (IDictionary)obj;
                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)
                {
                    sqlGenerate += $"{p.Name.ParamSql(sqlGenerate)} = ";
                    sqlGenerate.AddDbParameter(p.GetValue(obj));
                    sqlGenerate += ",";
                }
            }

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

            return(sqlGenerate);
        }