using System.Diagnostics;
namespace Strata.SqlTools.Rules.Rule.Expression;
///
/// Represents a comparison operation between two expressions.
///
[DebuggerDisplay("{Left} {Type} {Right}")]
public abstract class Comparison : BoolExpr, IBinary
{
///
/// Gets the left operand of the comparison.
///
public Expression Left { get; }
///
/// Gets the right operand of the comparison.
///
public Expression Right { get; }
///
/// Gets the type of comparison operation.
///
public abstract Type Type { get; }
///
/// Initializes a new instance of the class.
///
/// The left operand.
/// The right operand.
protected Comparison(Expression left, Expression right)
{
Left = left;
Right = right;
}
///
/// Creates a new comparison expression with updated operands.
///
/// The new left operand.
/// The new right operand.
/// A new comparison expression or this instance if operands are unchanged.
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")
};
}
}