Files
sql-utilities/src/Strata.SqlTools.Rules/Rule/Expression/Comparison.cs
T

65 lines
1.9 KiB
C#

using System.Diagnostics;
namespace Strata.SqlTools.Rules.Rule.Expression;
/// <summary>
/// Represents a comparison operation between two expressions.
/// </summary>
[DebuggerDisplay("{Left} {Type} {Right}")]
public abstract class Comparison : BoolExpr, IBinary
{
/// <summary>
/// Gets the left operand of the comparison.
/// </summary>
public Expression Left { get; }
/// <summary>
/// Gets the right operand of the comparison.
/// </summary>
public Expression Right { get; }
/// <summary>
/// Gets the type of comparison operation.
/// </summary>
public abstract Type Type { get; }
/// <summary>
/// Initializes a new instance of the <see cref="Comparison"/> class.
/// </summary>
/// <param name="left">The left operand.</param>
/// <param name="right">The right operand.</param>
protected Comparison(Expression left, Expression right)
{
Left = left;
Right = right;
}
/// <summary>
/// Creates a new comparison expression with updated operands.
/// </summary>
/// <param name="left">The new left operand.</param>
/// <param name="right">The new right operand.</param>
/// <returns>A new comparison expression or this instance if operands are unchanged.</returns>
public Expression Update(Expression left, Expression right)
{
if (ReferenceEquals(left, Left) && ReferenceEquals(right, Right))
{
return this;
}
return Create(left, right, Type);
}
private static Comparison Create(Expression left, Expression right, Type Type)
{
return Type switch
{
Type.Equal => new Equal(left, right),
Type.NotEqual => new NotEqual(left, right),
Type.GreaterThan => new GreaterThan(left, right),
_ => throw new NotImplementedException("not yet")
};
}
}