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; } } } }
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); }); } } } }