You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
986 B
34 lines
986 B
using Connected.Expressions.Visitors;
|
|
using System.Linq.Expressions;
|
|
|
|
namespace Connected.Expressions.Translation.Rewriters;
|
|
|
|
public class ParameterRewriter : DatabaseVisitor
|
|
{
|
|
private ParameterRewriter(ExpressionCompilationContext context)
|
|
{
|
|
Context = context;
|
|
}
|
|
|
|
private ExpressionCompilationContext Context { get; }
|
|
|
|
public static Expression Rewrite(ExpressionCompilationContext context, Expression expression)
|
|
{
|
|
return new ParameterRewriter(context).Visit(expression);
|
|
}
|
|
|
|
protected override Expression VisitBinary(BinaryExpression expression)
|
|
{
|
|
return base.VisitBinary(expression);
|
|
}
|
|
protected override Expression VisitConstant(ConstantExpression expression)
|
|
{
|
|
var parameter = Context.Parameters.FirstOrDefault(f => f.Value == expression);
|
|
|
|
if (parameter.Value is not null)
|
|
return Expression.Constant($"@{parameter.Key}");
|
|
|
|
return base.VisitConstant(expression);
|
|
}
|
|
}
|