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); }
/// <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); }
protected override SqlGenerate In(MemberExpression expression, SqlGenerate sqlGenerate) { var result = SqlExpressionCompiler.Evaluate(expression); var inArgs = (result as IEnumerable).Flatten(); sqlGenerate.AddDbParameter(inArgs); 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}%"); }
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); }
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); }
private static void Format(MethodCallExpression expression, SqlGenerate sqlGenerate) { var formatString = SqlExpressionCompiler.Evaluate(expression.Arguments[0]); var formatArgs = new List <string>(); var args = expression.Arguments; if (args.Count > 1) { for (int i = 1; i < args.Count; i++) { var val = SqlExpressionCompiler.Evaluate(expression.Arguments[i]); formatArgs.Add(val?.ToString()); } } sqlGenerate += string.Format(formatString.ToString(), formatArgs.ToArray()); }
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))); } return(sqlGenerate); } sqlGenerate.SelectFields.Add(expression.Member.Name.ParamSql(sqlGenerate)); return(sqlGenerate); }
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.GetType() == typeof(string)) { sqlGenerate += obj.ToString(); } else 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) { 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); }
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); }
/// <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); }
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}%"); }
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); }
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); }