Skip to content

Commit

Permalink
improve numerical pagination, based on #28
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb committed Apr 4, 2024
1 parent 7de307c commit ae56c63
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lib/modules/mnesia_assistant/pagination.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,31 @@ defmodule MnesiaAssistant.Pagination do
# [{{tab,'$1','_','_','_','_','_','_','_','_','_','_','_','_','_'},
# [{'andalso', {'>', '$1', 20}, {'<', '$1', 30}}], ['$_']}], 11, read)
# end).
def numerical(table, keys, strc, start: start, last: last) do
fields = {table, :"$1", :_, :_, :_, :_, :_, :_, :_, :_, :_, :_, :_, :_, :_}
conds = [{eg(:and), {eg(:>), :"$1", start}, {eg(:<), :"$1", last}}]
spec = [{fields, conds, er(:all)}]
def numerical(table, keys, strc, start: start, last: last, at: at) do
fields =
([table] ++
Enum.map(1..length(keys), fn x -> if(x === at, do: :"$1", else: :_) end))
|> List.to_tuple()

Transaction.transaction(fn -> Query.select(table, spec, :read) end)
|> case do
{:atomic, res} ->
tuple_to_map(res, keys, strc, [])

{:aborted, reason} ->
Transaction.transaction_error(reason, __MODULE__, "listing", :global, :database)
end
conds = [{eg(:and), {eg(:>=), :"$1", start}, {eg(:<=), :"$1", last}}]
spec = [{fields, conds, er(:all)}]
run_numerical_query(table, spec, keys, strc)
end

def numerical(table, keys, strc, page, limit) when page > 0 and limit > 0 do
def numerical(table, keys, strc, page, limit, at) when page > 0 and limit > 0 do
start = page * limit - limit
fields = {table, :"$1", :_, :_, :_, :_, :_, :_, :_, :_, :_, :_, :_, :_, :_}
conds = [{eg(:and), {eg(:>), :"$1", start}, {eg(:<), :"$1", page * limit}}]

fields =
([table] ++
Enum.map(1..length(keys), fn x -> if(x === at, do: :"$1", else: :_) end))
|> List.to_tuple()

conds = [{eg(:and), {eg(:>), :"$1", start}, {eg(:<=), :"$1", page * limit}}]
spec = [{fields, conds, er(:all)}]
run_numerical_query(table, spec, keys, strc)
end

defp run_numerical_query(table, spec, keys, strc) do
Transaction.transaction(fn -> Query.select(table, spec, :read) end)
|> case do
{:atomic, res} ->
Expand Down

0 comments on commit ae56c63

Please sign in to comment.