Skip to content

Commit

Permalink
filter date without casting ecto type #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Colombo committed Dec 3, 2024
1 parent 9a93dd5 commit 071a0ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
17 changes: 9 additions & 8 deletions lib/query_builders/ecto_query_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -501,15 +501,15 @@ defmodule FIQLEx.QueryBuilders.EctoQueryBuilder do
subquery_where =
dynamic(
[],
field(as(^association), ^assoc_selector) >= fragment("?::date", ^to_string(value))
field(as(^association), ^assoc_selector) >= ^to_string(value)
)

build_association_where(association, subquery_where, opts)

false ->
selector_name = string_to_atom(selector_name)

dynamic([q], field(q, ^selector_name) >= fragment("?::date", ^to_string(value)))
dynamic([q], field(q, ^selector_name) >= ^to_string(value))
end
end

Expand Down Expand Up @@ -542,15 +542,15 @@ defmodule FIQLEx.QueryBuilders.EctoQueryBuilder do
subquery_where =
dynamic(
[],
field(as(^association), ^assoc_selector) > fragment("?::date", ^to_string(value))
field(as(^association), ^assoc_selector) > ^to_string(value)
)

build_association_where(association, subquery_where, opts)

false ->
selector_name = string_to_atom(selector_name)

dynamic([q], field(q, ^selector_name) > fragment("?::date", ^to_string(value)))
dynamic([q], field(q, ^selector_name) > ^to_string(value))
end
end

Expand Down Expand Up @@ -583,15 +583,15 @@ defmodule FIQLEx.QueryBuilders.EctoQueryBuilder do
subquery_where =
dynamic(
[],
field(as(^association), ^assoc_selector) <= fragment("?::date", ^to_string(value))
field(as(^association), ^assoc_selector) <= ^to_string(value)
)

build_association_where(association, subquery_where, opts)

false ->
selector_name = string_to_atom(selector_name)

dynamic([q], field(q, ^selector_name) <= fragment("?::date", ^to_string(value)))
dynamic([q], field(q, ^selector_name) <= ^to_string(value))
end
end

Expand Down Expand Up @@ -624,15 +624,15 @@ defmodule FIQLEx.QueryBuilders.EctoQueryBuilder do
subquery_where =
dynamic(
[],
field(as(^association), ^assoc_selector) < fragment("?::date", ^to_string(value))
field(as(^association), ^assoc_selector) < ^to_string(value)
)

build_association_where(association, subquery_where, opts)

false ->
selector_name = string_to_atom(selector_name)

dynamic([q], field(q, ^selector_name) < fragment("?::date", ^to_string(value)))
dynamic([q], field(q, ^selector_name) < ^to_string(value))
end
end

Expand Down Expand Up @@ -683,6 +683,7 @@ defmodule FIQLEx.QueryBuilders.EctoQueryBuilder do
# owner_key = String.to_existing_atom("#{association}_id")
# related_key = :id
schema = Keyword.fetch!(opts, :schema)

case schema.__schema__(:association, association) do
%{owner_key: owner_key, related_key: related_key} ->
case related_key do
Expand Down
16 changes: 8 additions & 8 deletions test/ecto_query_builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -512,15 +512,15 @@ defmodule EctoQueryBuilderTest do
from(u0 in FIQLEx.Test.Support.User,
join: g1 in assoc(u0, :groups),
as: :groups,
where: as(:groups).inserted_at > fragment("?::date", ^"2022-10-02T18:23:03Z"),
where: as(:groups).inserted_at > ^"2022-10-02T18:23:03Z",
select: u0.id
)
) and
u0.id in subquery(
from(u0 in FIQLEx.Test.Support.User,
join: g1 in assoc(u0, :groups),
as: :groups,
where: as(:groups).inserted_at < fragment("?::date", ^"2022-10-31T18:23:03Z"),
where: as(:groups).inserted_at < ^"2022-10-31T18:23:03Z",
select: u0.id
)
),
Expand Down Expand Up @@ -548,15 +548,15 @@ defmodule EctoQueryBuilderTest do
from(u0 in FIQLEx.Test.Support.User,
join: g1 in assoc(u0, :groups),
as: :groups,
where: as(:groups).inserted_at >= fragment("?::date", ^"2022-10-02T18:23:03Z"),
where: as(:groups).inserted_at >= ^"2022-10-02T18:23:03Z",
select: u0.id
)
) and
u0.id in subquery(
from(u0 in FIQLEx.Test.Support.User,
join: g1 in assoc(u0, :groups),
as: :groups,
where: as(:groups).inserted_at <= fragment("?::date", ^"2022-10-31T18:23:03Z"),
where: as(:groups).inserted_at <= ^"2022-10-31T18:23:03Z",
select: u0.id
)
),
Expand Down Expand Up @@ -894,8 +894,8 @@ defmodule EctoQueryBuilderTest do
expected =
from(u0 in FIQLEx.Test.Support.User,
where:
u0.inserted_at > fragment("?::date", ^"2022-10-02T18:23:03Z") and
u0.inserted_at < fragment("?::date", ^"2022-10-31T18:23:03Z"),
u0.inserted_at > ^"2022-10-02T18:23:03Z" and
u0.inserted_at < ^"2022-10-31T18:23:03Z",
order_by: [],
select: [:inserted_at]
)
Expand All @@ -915,8 +915,8 @@ defmodule EctoQueryBuilderTest do
expected =
from(u0 in FIQLEx.Test.Support.User,
where:
u0.inserted_at >= fragment("?::date", ^"2022-10-02T18:23:03Z") and
u0.inserted_at <= fragment("?::date", ^"2022-10-31T18:23:03Z"),
u0.inserted_at >= ^"2022-10-02T18:23:03Z" and
u0.inserted_at <= ^"2022-10-31T18:23:03Z",
order_by: [],
select: [:inserted_at]
)
Expand Down

0 comments on commit 071a0ac

Please sign in to comment.