Skip to content

Fix Where clause with enum supabase-community/supabase-csharp#66 #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions Postgrest/Linq/WhereExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ protected override Expression VisitBinary(BinaryExpression node)
var left = Visit(node.Left);
var right = Visit(node.Right);

var column = left is MemberExpression leftMember ? GetColumnFromMemberExpression(leftMember) : null;
string? column = null;
if (left is MemberExpression leftMember)
{
column = GetColumnFromMemberExpression(leftMember);
}//To handle properly if it's a Convert ExpressionType generally with nullable properties
else if (left is UnaryExpression leftUnary && leftUnary.NodeType == ExpressionType.Convert && leftUnary.Operand is MemberExpression leftOperandMember)
{
column = GetColumnFromMemberExpression(leftOperandMember);
}

if (column == null)
throw new ArgumentException($"Left side of expression: '{node}' is expected to be property with a ColumnAttribute or PrimaryKeyAttribute");
Expand Down Expand Up @@ -129,7 +137,15 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
/// <param name="constantExpression"></param>
private void HandleConstantExpression(string column, Operator op, ConstantExpression constantExpression)
{
Filter = new QueryFilter(column, op, constantExpression.Value);
if (constantExpression.Type.IsEnum)
{
var enumValue = constantExpression.Value;
Filter = new QueryFilter(column, op, enumValue);
}
else
{
Filter = new QueryFilter(column, op, constantExpression.Value);
}
}

/// <summary>
Expand All @@ -140,8 +156,8 @@ private void HandleConstantExpression(string column, Operator op, ConstantExpres
/// <param name="memberExpression"></param>
private void HandleMemberExpression(string column, Operator op, MemberExpression memberExpression)
{
Filter = new QueryFilter(column, op, GetMemberExpressionValue(memberExpression));
}
Filter = new QueryFilter(column, op, GetMemberExpressionValue(memberExpression));
}

/// <summary>
/// A unary expression parser (i.e. => x.Id == 1 &lt;- where both `1` is considered unary)
Expand Down Expand Up @@ -192,6 +208,10 @@ private void HandleNewExpression(string column, Operator op, NewExpression newEx
{
Filter = new QueryFilter(column, op, guid.ToString());
}
else if (instance.GetType().IsEnum)
{
Filter = new QueryFilter(column, op, instance);
}
}

/// <summary>
Expand Down
15 changes: 15 additions & 0 deletions PostgrestTests/LinqTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ public async Task TestLinqWhere()
foreach (var q in query7.Models)
Assert.IsNotNull(q.DateTimeValue);

//Testing where condition with Enum as constant
var query8 = await client.Table<Movie>()
.Where(x => x.Status == MovieStatus.OnDisplay)
.Get();
foreach (var q in query8.Models)
Assert.IsTrue(q.Status == MovieStatus.OnDisplay);

//Test where condition with Enum as Memeber expression
var testMovie = new Movie { Status = MovieStatus.OnDisplay };
var query9 = await client.Table<Movie>()
.Where(x => x.Status == testMovie.Status)
.Get();
foreach (var q in query9.Models)
Assert.IsTrue(q.Status == MovieStatus.OnDisplay);

await client.Table<KitchenSink>()
.Where(x => x.DateTimeValue == DateTime.Now)
.Get();
Expand Down
8 changes: 8 additions & 0 deletions PostgrestTests/Models/LinkedModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ public class Movie : BaseModel

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

[Column("status")] public MovieStatus? Status { get; set; }

[Reference(typeof(Person), ReferenceAttribute.JoinType.Left)]
public List<Person> People { get; set; } = new();

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

public enum MovieStatus
{
OnDisplay,
OffDisplay
}

[Table("person")]
public class Person : BaseModel
{
Expand Down
3 changes: 2 additions & 1 deletion PostgrestTests/db/00-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ CREATE TABLE public.movie
(
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
created_at timestamp without time zone NOT NULL DEFAULT now(),
name character varying(255) NULL
name character varying(255) NULL,
status character varying(255) NULL
);

CREATE TABLE public.person
Expand Down
12 changes: 6 additions & 6 deletions PostgrestTests/db/01-dummy-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ VALUES ('f3ff356d-5803-43a7-b125-ba10cf10fdcd',
'[20,50]'::int4range);


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


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