diff --git a/lib/modules/mnesia_assistant/pagination.ex b/lib/modules/mnesia_assistant/pagination.ex index 85d2bf7..f7b1606 100644 --- a/lib/modules/mnesia_assistant/pagination.ex +++ b/lib/modules/mnesia_assistant/pagination.ex @@ -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} ->