Skip to content

Commit

Permalink
Extract duplicated subquery count logic
Browse files Browse the repository at this point in the history
  • Loading branch information
drewolson committed Aug 17, 2017
1 parent 8ec0a6a commit eade4b5
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/scrivener/paginater/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ defimpl Scrivener.Paginater, for: Ecto.Query do
|> exclude(:preload)
|> exclude(:order_by)
|> prepare_select
|> subquery
|> select(count("*"))
|> count
|> repo.one(caller: caller)

total_entries || 0
Expand All @@ -43,9 +42,8 @@ defimpl Scrivener.Paginater, for: Ecto.Query do
defp prepare_select(query) do
try do
query
|> subquery
|> select(count("*"))
|> Ecto.Query.Planner.prepare_sources(_adapter = nil)
|> count
|> Ecto.Query.Planner.prepare_sources(nil)

query
rescue
Expand All @@ -64,6 +62,12 @@ defimpl Scrivener.Paginater, for: Ecto.Query do
end
end

defp count(query) do
query
|> subquery
|> select(count("*"))
end

defp total_pages(0, _), do: 1

defp total_pages(total_entries, page_size) do
Expand Down

0 comments on commit eade4b5

Please sign in to comment.