Building a Filter class for WHERE clauses
Previously, we built a data structure capable of organizing filters into groups.
With that framework in place, we must now devise some construct to represent filters. To do that, let's first identify some basic requirements for a filter:
- Chainable using logical operators AND/OR and optionally NONE for the first filter in a chain
- A name on which the filter acts (such as a column name)
- A value to be compared against
- A comparator (such as >, =, <, etc)
- The ability to inverse the filter (NOT)
(A OR (B AND C AND D))
Here we have an inner group (B AND C AND D). If we were to name this group X, this could be reduced to:
(A OR (X))
With the inner group simplified as X, it's easier to abstract it away and focus on the final requirement: a filter must be able to be ANDed or ORed with a group. With that in mind, we'll create some InnerGroup property so that group can later be evaluated.
There is one more knot to untangle, however. A basic filter -- A < 10 for example -- is easy to implement, but it's very common to run functions on the LHS operator such as LENGTH(A) < 10. Then there are functions we can apply to the RHS such as A IN (1,2,3). With this in mind, we've created a single abstract filter that takes care of the chaining of filters, and left the implementation of the comparators to a future concrete class.
public abstract class Filter { /// <summary> /// Name of the column to which the filter is applied /// </summary> internal string Name { get; set; } /// <summary> /// Value to which the column is being compared /// </summary> internal object Value { get; set; } /// <summary> /// The logical operator preceding the filter (and/or). If a filter /// has no preceding filter, this is expected to be none /// </summary> internal LogicalOperator LogicalOperator { get; set; } /// <summary> /// The next filter that has been ANDed or ORed with this filter /// </summary> internal Filter Next { get; set; } /// <summary> /// True if this filter has been notted /// </summary> internal bool Not { get; set; } /// <summary> /// The group to which the filter has been added /// </summary> internal FilterGroup MemberOfGroup { get; set; } /// <summary> /// A group embedded within the filter's group: (A < B OR (innergroup)) /// </summary> internal FilterGroup InnerGroup { get; set; } /// <summary> /// Translator determining to which language the filter group should be translated /// </summary> internal readonly IQueryTranslator _translator; protected Filter(FilterGroup group, string name, object value, LogicalOperator logicalOp, IQueryTranslator translator, bool not) { _translator = translator; Name = name; Value = value; LogicalOperator = logicalOp; Not = not; MemberOfGroup = group; } /// <summary> /// Creates a new comparison filter, ANDs it with the current filter, and returns the newly /// added filter /// </summary> /// <param name="name">Value name</param> /// <param name="comparisonOperator">Comparison operator (greater than, less than, etc)</param> /// <param name="value">Value</param> /// <param name="not">True if the filter should be notted</param> /// <returns>Filter that has been added</returns> public Filter And(string name, ComparisonOperator comparisonOperator, object value, bool notted = false) => And(name, ComparisonFunction.None, comparisonOperator, value, notted); /// <summary> /// Creates a new comparison filter, ANDs it with the current filter, and returns the newly /// added filter /// </summary> /// <param name="name">Value name</param> /// /// <param name="function">Function, if applicable (in, starts with, etc)</param> /// <param name="comparisonOperator">Comparison operator (greater than, less than, etc)</param> /// <param name="value">Value</param> /// <param name="not">True if the filter should be notted</param> /// <returns>Filter that has been added</returns> public Filter And(string name, ComparisonFunction function, ComparisonOperator comparisonOperator, object value, bool not = false) { var filter = new ComparisonFilter(MemberOfGroup, name, comparisonOperator, value, function, _translator, not); return And(filter); } /// <summary> /// Creates a new comparison filter, ANDs it with the current filter, and returns the newly /// added filter /// </summary> /// <param name="name">Value name</param> /// <param name="function">Function, if applicable (in, starts with, etc)</param> /// <param name="value">Value</param> /// <param name="not">True if the filter should be notted</param> /// <returns>Filter that has been added</returns> public Filter And(string name, ValueFunction function, object value, bool not = false) { var filter = new FunctionFilter(MemberOfGroup, name, function, value, _translator, not); return And(filter); } /// <summary> /// Creates a new comparison filter, ORs it with the current filter, and returns the newly /// added filter /// </summary> /// <param name="name">Value name</param> /// <param name="comparisonOperator">Comparison operator (greater than, less than, etc)</param> /// <param name="value">Value</param> /// <param name="not">True if the filter should be notted</param> /// <returns>Filter that has been added</returns> public Filter Or(string name, ComparisonOperator comparisonOperator, object value, bool not = false) => Or(name, ComparisonFunction.None, comparisonOperator, value, not); /// <summary> /// Creates a new comparison filter, ORs it with the current filter, and returns the newly /// added filter /// </summary> /// <param name="name">Value name</param> /// /// <param name="function">Function, if applicable (in, starts with, etc)</param> /// <param name="comparisonOperator">Comparison operator (greater than, less than, etc)</param> /// <param name="value">Value</param> /// <param name="not">True if the filter should be notted</param> /// <returns>Filter that has been added</returns> public Filter Or(string name, ComparisonFunction function, ComparisonOperator comparisonOperator, object value, bool not = false) { var filter = new ComparisonFilter(MemberOfGroup, name, comparisonOperator, value, function, _translator, not); return Or(filter); } /// <summary> /// Creates a new comparison filter, ANDs it with the current filter, and returns the newly /// added filter /// </summary> /// <param name="name">Value name</param> /// <param name="function">Function, if applicable (in, starts with, etc)</param> /// <param name="value">Value</param> /// <param name="not">True if the filter should be notted</param> /// <returns>Filter that has been added</returns> public Filter Or(string name, ValueFunction function, object value, bool not = false) { var filter = new FunctionFilter(MemberOfGroup, name, function, value, _translator, not); return Or(filter); } /// <summary> /// Creates a new filter group, ANDs it with the group the filter is a member of, /// and returns that new group (ex: (A < B ) OR ({newgroup}) /// </summary> /// <returns>Newly added group</returns> public FilterGroup AndGroup() { var nextGroup = new FilterGroup(_translator) { LogicalOperator = LogicalOperator.And }; MemberOfGroup.Next = nextGroup; return MemberOfGroup.Next; } /// <summary> /// Creates a new filter group, ANDs it with the current filter, and returns that /// new group as a member of the current group (ex: (A < B OR ({newgroup})) /// </summary> /// <returns>Newly added group</returns> public FilterGroup AndInnerGroup() { InnerGroup = new FilterGroup(_translator) { LogicalOperator = LogicalOperator.And }; return InnerGroup; } /// <summary> /// Creates a new filter group, ORs it with the group the filter is a member of, /// and returns that new group /// </summary> /// <returns>Newly added group</returns> public FilterGroup OrGroup() { var nextGroup = new FilterGroup(_translator) { LogicalOperator = LogicalOperator.Or }; MemberOfGroup.Next = nextGroup; return MemberOfGroup.Next; } /// <summary> /// Creates a new filter group, ORs it with the current filter, and returns that /// new group as a member of the current group (ex: (A < B OR ({newgroup})) /// </summary> /// <returns>Newly added group</returns> public FilterGroup OrInnerGroup() { InnerGroup = new FilterGroup(_translator) { LogicalOperator = LogicalOperator.Or }; return InnerGroup; } /// <summary> /// ANDs the provided filter with the current filter /// </summary> /// <param name="filter">Filter to chain with the current filter</param> /// <returns>Newly added filter</returns> internal Filter And(Filter filter) { filter.LogicalOperator = LogicalOperator.And; Next = filter; MemberOfGroup.LastFilter = filter; return Next; } /// <summary> /// ORs the provided filter with the current filter /// </summary> /// <param name="filter">Filter to chain with the current filter</param> /// <returns>Newly added filter</returns> internal Filter Or(Filter filter) { filter.LogicalOperator = LogicalOperator.Or; Next = filter; MemberOfGroup.LastFilter = filter; return Next; } }
Above, we've implement methods to chain filters with one another. Note the And() and Or() methods which set the Next member equal to a new filter and then return that same filter. This finally allows us to chain filters like below:
myFilter.And("A", ComparisonOperator.LessThan, 10).Or("B", ComparisonOperator.GreaterThan, 20)
There is one other thing to take note of. We have also introduced the ability to add an innergroup. Because inner groups are treated like any other filter, we must have some 'And' method and some 'Or' method. From there, the group can be added onto just like the original group.
Lastly, we have once again used dependency injection to set a QueryTranslator. And, once again, this will be covered in a later post!

Comments
Post a Comment