Skip to content

Commit

Permalink
Re: #78, Generalize query filtering creation in Table so that it ma…
Browse files Browse the repository at this point in the history
…tches new generic signatures
  • Loading branch information
acupofjose committed Jan 12, 2024
1 parent 9f9f72d commit e00a2e0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
8 changes: 5 additions & 3 deletions Postgrest/QueryFilter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json;
using Postgrest.Exceptions;
using Postgrest.Interfaces;
Expand Down Expand Up @@ -62,7 +64,7 @@ public QueryFilter(string property, Operator op, object? criteria)
/// <param name="property">Column name</param>
/// <param name="op">Operation: In, Contains, ContainedIn, or Overlap</param>
/// <param name="criteria"></param>
public QueryFilter(string property, Operator op, List<object> criteria)
public QueryFilter(string property, Operator op, IList criteria)
{
switch (op)
{
Expand All @@ -85,7 +87,7 @@ public QueryFilter(string property, Operator op, List<object> criteria)
/// <param name="property">Column name</param>
/// <param name="op">Operation: In, Contains, ContainedIn, or Overlap</param>
/// <param name="criteria"></param>
public QueryFilter(string property, Operator op, Dictionary<string, object> criteria)
public QueryFilter(string property, Operator op, IDictionary criteria)
{
switch (op)
{
Expand Down
24 changes: 13 additions & 11 deletions Postgrest/Table.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
Expand Down Expand Up @@ -146,12 +147,12 @@ public Table<TModel> Filter<TCriterion>(string columnName, Operator op, TCriteri
case float floatCriterion:
_filters.Add(new QueryFilter(columnName, op, floatCriterion));
return this;
case List<object> listCriteria:
_filters.Add(new QueryFilter(columnName, op, listCriteria));
return this;
case Dictionary<string, object> dictCriteria:
case IDictionary dictCriteria:
_filters.Add(new QueryFilter(columnName, op, dictCriteria));
return this;
case IList listCriteria:
_filters.Add(new QueryFilter(columnName, op, listCriteria));
return this;
case IntRange rangeCriteria:
_filters.Add(new QueryFilter(columnName, op, rangeCriteria));
return this;
Expand Down Expand Up @@ -810,18 +811,19 @@ internal KeyValuePair<string, string> PrepareFilter(QueryFilter filter)

break;
case Operator.In:
if (filter.Criteria is List<object> inCriteria && filter.Property != null)
if (filter is { Criteria: IList inCriteria, Property: not null })
{
foreach (var item in inCriteria)
strBuilder.Append($"\"{item}\",");

return new KeyValuePair<string, string>(filter.Property,
$"{asAttribute.Mapping}.({strBuilder.ToString().Trim(',')})");
}
else if (filter.Criteria is Dictionary<string, object> dictCriteria && filter.Property != null)

if (filter is { Criteria: IDictionary inDictCriteria, Property: not null })
{
return new KeyValuePair<string, string>(filter.Property,
$"{asAttribute.Mapping}.{JsonConvert.SerializeObject(dictCriteria)}");
$"{asAttribute.Mapping}.{JsonConvert.SerializeObject(inDictCriteria)}");
}

break;
Expand All @@ -830,15 +832,15 @@ internal KeyValuePair<string, string> PrepareFilter(QueryFilter filter)
case Operator.Overlap:
switch (filter.Criteria)
{
case List<object> listCriteria when filter.Property != null:
case IList listCriteria when filter.Property != null:
{
foreach (var item in listCriteria)
strBuilder.Append($"{item},");

return new KeyValuePair<string, string>(filter.Property,
$"{asAttribute.Mapping}.{{{strBuilder.ToString().Trim(',')}}}");
}
case Dictionary<string, object> dictCriteria when filter.Property != null:
case IDictionary dictCriteria when filter.Property != null:
return new KeyValuePair<string, string>(filter.Property,
$"{asAttribute.Mapping}.{JsonConvert.SerializeObject(dictCriteria)}");
case IntRange rangeCriteria when filter.Property != null:
Expand All @@ -852,7 +854,7 @@ internal KeyValuePair<string, string> PrepareFilter(QueryFilter filter)
case Operator.NotRightOf:
case Operator.NotLeftOf:
case Operator.Adjacent:
if (filter.Criteria is IntRange rangeCriterion && filter.Property != null)
if (filter is { Criteria: IntRange rangeCriterion, Property: not null })
{
return new KeyValuePair<string, string>(filter.Property,
$"{asAttribute.Mapping}.{rangeCriterion.ToPostgresString()}");
Expand All @@ -863,7 +865,7 @@ internal KeyValuePair<string, string> PrepareFilter(QueryFilter filter)
case Operator.PHFTS:
case Operator.PLFTS:
case Operator.WFTS:
if (filter.Criteria is FullTextSearchConfig searchConfig && filter.Property != null)
if (filter is { Criteria: FullTextSearchConfig searchConfig, Property: not null })
{
return new KeyValuePair<string, string>(filter.Property,
$"{asAttribute.Mapping}({searchConfig.Config}).{searchConfig.QueryText}");
Expand Down

0 comments on commit e00a2e0

Please sign in to comment.