Skip to content

Commit afca7ea

Browse files
authored
Merge pull request #72 from elrhomariyounes/fixWhereEnum
Fix Where clause with enum supabase-community/supabase-csharp#66
2 parents c383e12 + 23c42c2 commit afca7ea

File tree

5 files changed

+55
-11
lines changed

5 files changed

+55
-11
lines changed

Postgrest/Linq/WhereExpressionVisitor.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ protected override Expression VisitBinary(BinaryExpression node)
5959
var left = Visit(node.Left);
6060
var right = Visit(node.Right);
6161

62-
var column = left is MemberExpression leftMember ? GetColumnFromMemberExpression(leftMember) : null;
62+
string? column = null;
63+
if (left is MemberExpression leftMember)
64+
{
65+
column = GetColumnFromMemberExpression(leftMember);
66+
}//To handle properly if it's a Convert ExpressionType generally with nullable properties
67+
else if (left is UnaryExpression leftUnary && leftUnary.NodeType == ExpressionType.Convert && leftUnary.Operand is MemberExpression leftOperandMember)
68+
{
69+
column = GetColumnFromMemberExpression(leftOperandMember);
70+
}
6371

6472
if (column == null)
6573
throw new ArgumentException($"Left side of expression: '{node}' is expected to be property with a ColumnAttribute or PrimaryKeyAttribute");
@@ -129,7 +137,15 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
129137
/// <param name="constantExpression"></param>
130138
private void HandleConstantExpression(string column, Operator op, ConstantExpression constantExpression)
131139
{
132-
Filter = new QueryFilter(column, op, constantExpression.Value);
140+
if (constantExpression.Type.IsEnum)
141+
{
142+
var enumValue = constantExpression.Value;
143+
Filter = new QueryFilter(column, op, enumValue);
144+
}
145+
else
146+
{
147+
Filter = new QueryFilter(column, op, constantExpression.Value);
148+
}
133149
}
134150

135151
/// <summary>
@@ -140,8 +156,8 @@ private void HandleConstantExpression(string column, Operator op, ConstantExpres
140156
/// <param name="memberExpression"></param>
141157
private void HandleMemberExpression(string column, Operator op, MemberExpression memberExpression)
142158
{
143-
Filter = new QueryFilter(column, op, GetMemberExpressionValue(memberExpression));
144-
}
159+
Filter = new QueryFilter(column, op, GetMemberExpressionValue(memberExpression));
160+
}
145161

146162
/// <summary>
147163
/// A unary expression parser (i.e. => x.Id == 1 &lt;- where both `1` is considered unary)
@@ -192,6 +208,10 @@ private void HandleNewExpression(string column, Operator op, NewExpression newEx
192208
{
193209
Filter = new QueryFilter(column, op, guid.ToString());
194210
}
211+
else if (instance.GetType().IsEnum)
212+
{
213+
Filter = new QueryFilter(column, op, instance);
214+
}
195215
}
196216

197217
/// <summary>

PostgrestTests/LinqTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ public async Task TestLinqWhere()
9898
foreach (var q in query7.Models)
9999
Assert.IsNotNull(q.DateTimeValue);
100100

101+
//Testing where condition with Enum as constant
102+
var query8 = await client.Table<Movie>()
103+
.Where(x => x.Status == MovieStatus.OnDisplay)
104+
.Get();
105+
foreach (var q in query8.Models)
106+
Assert.IsTrue(q.Status == MovieStatus.OnDisplay);
107+
108+
//Test where condition with Enum as Memeber expression
109+
var testMovie = new Movie { Status = MovieStatus.OnDisplay };
110+
var query9 = await client.Table<Movie>()
111+
.Where(x => x.Status == testMovie.Status)
112+
.Get();
113+
foreach (var q in query9.Models)
114+
Assert.IsTrue(q.Status == MovieStatus.OnDisplay);
115+
101116
await client.Table<KitchenSink>()
102117
.Where(x => x.DateTimeValue == DateTime.Now)
103118
.Get();

PostgrestTests/Models/LinkedModels.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ public class Movie : BaseModel
1212

1313
[Column("name")] public string? Name { get; set; }
1414

15+
[Column("status")] public MovieStatus? Status { get; set; }
16+
1517
[Reference(typeof(Person), ReferenceAttribute.JoinType.Left)]
1618
public List<Person> People { get; set; } = new();
1719

1820
[Column("created_at")] public DateTime CreatedAt { get; set; }
1921
}
2022

23+
public enum MovieStatus
24+
{
25+
OnDisplay,
26+
OffDisplay
27+
}
28+
2129
[Table("person")]
2230
public class Person : BaseModel
2331
{

PostgrestTests/db/00-schema.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ CREATE TABLE public.movie
7575
(
7676
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
7777
created_at timestamp without time zone NOT NULL DEFAULT now(),
78-
name character varying(255) NULL
78+
name character varying(255) NULL,
79+
status character varying(255) NULL
7980
);
8081

8182
CREATE TABLE public.person

PostgrestTests/db/01-dummy-data.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ VALUES ('f3ff356d-5803-43a7-b125-ba10cf10fdcd',
5353
'[20,50]'::int4range);
5454

5555

56-
insert into "public"."movie" ("created_at", "id", "name")
57-
values ('2022-08-20 00:29:45.400188', 'ea07bd86-a507-4c68-9545-b848bfe74c90', 'Top Gun: Maverick');
58-
insert into "public"."movie" ("created_at", "id", "name")
59-
values ('2022-08-22 00:29:45.400188', 'a972a8f6-2e23-4172-be8d-7b65470ca0f4', 'Mad Max: Fury Road');
60-
insert into "public"."movie" ("created_at", "id", "name")
61-
values ('2022-08-28 00:29:45.400188', '42fd15b1-3bff-431d-9fa5-314289beb246', 'Guns Away');
56+
insert into "public"."movie" ("created_at", "id", "name", "status")
57+
values ('2022-08-20 00:29:45.400188', 'ea07bd86-a507-4c68-9545-b848bfe74c90', 'Top Gun: Maverick', 'OnDisplay');
58+
insert into "public"."movie" ("created_at", "id", "name", "status")
59+
values ('2022-08-22 00:29:45.400188', 'a972a8f6-2e23-4172-be8d-7b65470ca0f4', 'Mad Max: Fury Road', 'OnDisplay');
60+
insert into "public"."movie" ("created_at", "id", "name", "status")
61+
values ('2022-08-28 00:29:45.400188', '42fd15b1-3bff-431d-9fa5-314289beb246', 'Guns Away', 'OffDisplay');
6262

6363

6464
insert into "public"."person" ("created_at", "first_name", "id", "last_name")

0 commit comments

Comments
 (0)