Skip to content

Commit 2113afd

Browse files
Support long, DateTime, and DateTimeOffset criteria in filter expressions (#101)
* Add "long" type column to kitchen sink test table. * Support long, DateTime, and DateTimeOffset criteria in filter expressions.
1 parent e708b36 commit 2113afd

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

Postgrest/Table.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ public IPostgrestTable<TModel> Filter<TCriterion>(string columnName, Operator op
144144
case int intCriterion:
145145
_filters.Add(new QueryFilter(columnName, op, intCriterion));
146146
return this;
147+
case long longCriterion:
148+
_filters.Add(new QueryFilter(columnName, op, longCriterion));
149+
return this;
147150
case float floatCriterion:
148151
_filters.Add(new QueryFilter(columnName, op, floatCriterion));
149152
return this;
@@ -159,9 +162,15 @@ public IPostgrestTable<TModel> Filter<TCriterion>(string columnName, Operator op
159162
case FullTextSearchConfig fullTextSearchCriteria:
160163
_filters.Add(new QueryFilter(columnName, op, fullTextSearchCriteria));
161164
return this;
165+
case DateTime dtSearchCriteria:
166+
_filters.Add(new QueryFilter(columnName, op, dtSearchCriteria));
167+
return this;
168+
case DateTimeOffset dtoSearchCriteria:
169+
_filters.Add(new QueryFilter(columnName, op, dtoSearchCriteria));
170+
return this;
162171
default:
163172
throw new PostgrestException(
164-
"Unknown criterion type, is it of type `string`, `int`, `float`, `List`, `Dictionary<string, object>`, `FullTextSearchConfig`, or `Range`?")
173+
"Unknown criterion type, is it of type `string`, `int`, `long`, `float`, `List`, `DateTime`, `DateTimeOffset`, `Dictionary<string, object>`, `FullTextSearchConfig`, or `Range`?")
165174
{
166175
Reason = FailureHint.Reason.InvalidArgument
167176
};

PostgrestTests/ClientTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,37 @@ public async Task TestMatchFilter()
959959
CollectionAssert.AreEqual(expected, filteredResponse.Models);
960960
}
961961

962+
[TestMethod("filters: dt")]
963+
public async Task TestDateTimeFilter()
964+
{
965+
var client = new Client(BaseUrl);
966+
var filteredResponse = await client.Table<Movie>().Filter("created_at", Operator.GreaterThan, new DateTime(2022, 08, 20))
967+
.Filter("created_at", Operator.LessThan, new DateTime(2022, 08, 21))
968+
.Get();
969+
Assert.AreEqual(1, filteredResponse.Models.Count);
970+
Assert.AreEqual("ea07bd86-a507-4c68-9545-b848bfe74c90", filteredResponse.Models[0].Id);
971+
}
972+
973+
[TestMethod("filters: dto")]
974+
public async Task TestDateTimeOffsetFilter()
975+
{
976+
var client = new Client(BaseUrl);
977+
var filteredResponse = await client.Table<Movie>().Filter("created_at", Operator.GreaterThan, new DateTimeOffset(new DateTime(2022, 08, 20)))
978+
.Filter("created_at", Operator.LessThan, new DateTimeOffset(new DateTime(2022, 08, 21)))
979+
.Get();
980+
Assert.AreEqual(1, filteredResponse.Models.Count);
981+
Assert.AreEqual("ea07bd86-a507-4c68-9545-b848bfe74c90", filteredResponse.Models[0].Id);
982+
}
983+
984+
[TestMethod("filters: long")]
985+
public async Task TestLongIntFilter()
986+
{
987+
var client = new Client(BaseUrl);
988+
var filteredResponse = await client.Table<KitchenSink>().Filter("long_value", Operator.Equals, 2147483648L)
989+
.Get();
990+
Assert.AreEqual(1, filteredResponse.Models.Count);
991+
}
992+
962993
[TestMethod("select: basic")]
963994
public async Task TestSelect()
964995
{

PostgrestTests/Models/KitchenSink.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class KitchenSink : BaseModel
2121

2222
[Column("int_value")] public int? IntValue { get; set; }
2323

24+
[Column("long_value")] public long? LongValue { get; set; }
25+
2426
[Column("float_value")] public float FloatValue { get; set; }
2527

2628
[Column("double_value")] public double DoubleValue { get; set; }

PostgrestTests/db/00-schema.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ create table "public"."kitchen_sink"
5757
"bool_value" BOOL DEFAULT false,
5858
"unique_value" varchar(255) UNIQUE,
5959
"int_value" INT null,
60+
"long_value" BIGINT null,
6061
"float_value" FLOAT null,
6162
"double_value" DOUBLE PRECISION null,
6263
"datetime_value" timestamp null,

PostgrestTests/db/01-dummy-data.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ INSERT INTO public.kitchen_sink (id,
2525
string_value,
2626
bool_value,
2727
int_value,
28+
long_value,
2829
float_value,
2930
double_value,
3031
datetime_value,
@@ -41,6 +42,7 @@ VALUES ('f3ff356d-5803-43a7-b125-ba10cf10fdcd',
4142
'Im the Kitchen Sink!',
4243
false,
4344
99999,
45+
2147483648,
4446
'99999.0'::float4,
4547
'99999.0'::float8,
4648
'Tue May 24 06:30:00 2022'::timestamp,

0 commit comments

Comments
 (0)