30 lines
756 B
Erlang
30 lines
756 B
Erlang
|
-module(ts_common).
|
||
|
-export([list/3]).
|
||
|
|
||
|
-include_lib("stdlib/include/qlc.hrl").
|
||
|
|
||
|
%% list <Length> number of records, skipping the first <Start>
|
||
|
list(Table, Start, Length)
|
||
|
when is_atom(Table) and is_integer(Start) and is_integer(Length) ->
|
||
|
list(qlc:q([A || A <- mnesia:table(Table)]), Start, Length);
|
||
|
|
||
|
list(Query, Start, Length) ->
|
||
|
{atomic, Result} = mnesia:transaction(fun() ->
|
||
|
% create a cursor for the query
|
||
|
C = qlc:cursor(Query),
|
||
|
|
||
|
% skip the first Start records
|
||
|
if Start > 0 -> qlc:next_answers(C, Start);
|
||
|
true -> ok
|
||
|
end,
|
||
|
|
||
|
% return Length records
|
||
|
List = qlc:next_answers(C, Length),
|
||
|
|
||
|
% free cursor
|
||
|
ok = qlc:delete_cursor(C),
|
||
|
|
||
|
List
|
||
|
end),
|
||
|
Result.
|