Skip to content

Commit

Permalink
Bugfix: Union queries
Browse files Browse the repository at this point in the history
  • Loading branch information
tmthn committed Jun 3, 2022
1 parent 7362ad7 commit 685419d
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lib/scrivener/paginater/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ defimpl Scrivener.Paginater, for: Ecto.Query do
|> all(repo, caller, prefix)
end

defp total_entries(%{combinations: [_|_]} = query, repo, _caller, _options) do
simpler_query =
query
|> exclude(:preload)
|> exclude(:order_by)

{sql_query, params} = Ecto.Adapters.SQL.to_sql(:all, repo, simpler_query)
%Postgrex.Result{
columns: ["count"],
command: :select,
num_rows: 1,
rows: [[total_entries]]
}
= Ecto.Adapters.SQL.query!(
repo, "select count(*) from (#{sql_query}) as count_me", params
)

total_entries || 0
end

defp total_entries(query, repo, caller, options) do
prefix = options[:prefix]

Expand All @@ -60,10 +80,15 @@ defimpl Scrivener.Paginater, for: Ecto.Query do

defp aggregate(%{distinct: %{expr: expr}} = query) when expr == true or is_list(expr) do
query
|> exclude(:select)
|> count()
end

defp aggregate(%{order_bys: %{expr: expr}} = query) do
query
|> exclude(:preload)
|> select(count("*"))
end

defp aggregate(
%{
group_bys: [
Expand Down

0 comments on commit 685419d

Please sign in to comment.