A Concrete Filter for Simple Comparisons


With the abstract Filter class complete, we can focus on implementing the actual comparator logic.

We can start with the simplest case: LHS Compator RHS

For this, we must first declare an enum to record the type of comparison being made:

public enum ComparisonOperator
{
    Equals,
    NotEquals,
    GreaterThan,
    GreaterThanOrEqual,
    LessThan,
    LessThanOrEqual
}

We can also create an enum to record a function being applied to the column name.  In this case, we're supporting only three.  However, because not all comparison filters will have a function applied, we've also added a None filter.

public enum ComparisonFunction
{
    Length,       
    ToUpper,
    ToLower,
    None
}

Finally, we can create our concrete ComparisonFilter.  Because the abstract Filter class takes care of all of the chaining and group organization, our ComparisonFilter is far smaller and more focused.  We need only to add two properties: one to record the comparison operator and the other to record the comparison function, if any.  (Note that because not every filter will have a function, we made this property optional with a default of none).

/// <summary>
/// Filter for applying basic comparisons (ex: A < 10).  Optionally, 
/// specific functions can be applied to the LHS (ex: LENGTH(A) < 10)
/// </summary>
public class ComparisonFilter : Filter
{
    internal readonly ComparisonOperator _comparisonOperator;
    internal readonly ComparisonFunction _function;

    internal ComparisonFilter(FilterGroup group, string name, ComparisonOperator comparisonOperator, object value, ComparisonFunction function, IQueryTranslator qt, bool not = false) 
        : base(group, name, value, LogicalOperator.None, qt, not)
    {
        _comparisonOperator = comparisonOperator;
        _function = function;
    }

    public override string ToString() => _translator.Translate(this);
}

We're pretty close to being complete, but there are still some missing elements.  What if we want to use something other than the basic comparison operators described here such as A StartsWith 'B'? What does this translator look like, anyway?  And how are the query strings actually built?  There's still quite a few more classes to examine before we have something that's actually capable of spitting out query strings!

Comments

Popular posts from this blog

ExcelDNA: Parameter Limit for Excel Functions Using a Variable Parameter Array (params and ParamArray)

Building a Filter class for WHERE clauses