Skip to content
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

Fix where in array + dynamic limit and offset #171

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
53 changes: 49 additions & 4 deletions lib/mongo_ecto/normalized_query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,28 @@ defmodule Mongo.Ecto.NormalizedQuery do

defp offset_limit(nil, _params, _pk, _query, _where), do: nil

defp offset_limit(
%Query.QueryExpr{expr: {:^, l, [idx]}},
params,
pk,
%Query{wheres: wheres} = query,
"limit clause" = where
) do
where_params_offset = where_params_offset(wheres)
value({:^, l, [where_params_offset + 1]}, params, pk, query, where)
end

defp offset_limit(
%Query.QueryExpr{expr: {:^, l, [idx]}},
params,
pk,
%Query{wheres: wheres} = query,
"offset clause" = where
) do
where_params_offset = where_params_offset(wheres)
value({:^, l, [where_params_offset + 2]}, params, pk, query, where)
end

defp offset_limit(%Query.QueryExpr{expr: expr}, params, pk, query, where),
do: value(expr, params, pk, query, where)

Expand Down Expand Up @@ -466,9 +488,10 @@ defmodule Mongo.Ecto.NormalizedQuery do
{field(left, pk, query, place), ["$in": []]}
end

defp pair({:in, _, [left, {:^, _, [ix, len]}]}, params, pk, query, place) do
defp pair({:in, _, [left, _]} = expr, params, pk, query, place) do
args =
ix..(ix + len - 1)
expr
|> where_params_range()
|> Enum.map(&elem(params, &1))
|> Enum.map(&value(&1, params, pk, query, place))

Expand All @@ -483,9 +506,10 @@ defmodule Mongo.Ecto.NormalizedQuery do
{field(left, pk, query, place), [{binary_op(op), value(right, params, pk, query, place)}]}
end

defp pair({:not, _, [{:in, _, [left, {:^, _, [ix, len]}]}]}, params, pk, query, place) do
defp pair({:not, _, [{:in, _, [left, _]}]} = expr, params, pk, query, place) do
args =
ix..(ix + len - 1)
expr
|> where_params_range()
|> Enum.map(&elem(params, &1))
|> Enum.map(&value(&1, params, pk, query, place))

Expand Down Expand Up @@ -545,4 +569,25 @@ defmodule Mongo.Ecto.NormalizedQuery do
defp error(place) do
raise ArgumentError, "Invalid expression for MongoDB adapter in #{place}"
end

defp where_params_offset(wheres) do
wheres
|> Enum.map(fn %Query.BooleanExpr{expr: expr} ->
_from..to = where_params_range(expr)
to
end)
|> Enum.max(& &1)
end

defp where_params_range({_, _, [_, {:^, _, [0, 0]}]}), do: 0..0
defp where_params_range({_, _, [_, {:^, _, [idx, len]}]}), do: idx..(idx + len - 1)
defp where_params_range({_, _, [_, {:^, _, [idx]}]}), do: idx..idx
defp where_params_range({:not, _, [expr]}), do: where_params_range(expr)

defp where_params_range({_, _, exprs}) do
ranges = Enum.map(exprs, &where_params_range(&1)) |> Enum.filter(& &1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a function call when a pipeline is only one function long

from.._to = Enum.at(ranges, 0)
_from..to = Enum.at(ranges, -1)
from..to
end
end
21 changes: 21 additions & 0 deletions test/mongo_ecto_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ defmodule Mongo.EctoTest do
assert 10 == TestRepo.one(query)
end

test "where in ids + dynamic limit + dynamic offset" do
visits = 3
exclude_visits = [4, 5]
post1 = TestRepo.insert!(%Post{visits: visits})
post2 = TestRepo.insert!(%Post{visits: visits})
post3 = TestRepo.insert!(%Post{visits: visits})
ids = [post1.id, post2.id, post3.id]
limit = 1
offset = 2

query =
from(
p in Post,
where: p.visits == ^visits and not (p.visits in ^exclude_visits) and p.id in ^ids,
limit: ^limit,
offset: ^offset
)

assert TestRepo.all(query) == [post3]
end

# test "partial update in map" do
# post = TestRepo.insert!(%Post{meta: %{author: %{name: "michal"}, other: "value"}})
# TestRepo.update_all(Post, set: [meta: change_map("author.name", "michal")])
Expand Down