2011-01-28 06:49:47 -06:00
|
|
|
-module(ts_common).
|
2011-02-02 16:57:58 -06:00
|
|
|
-export([list/3, order_datetimes/2]).
|
2011-01-28 06:49:47 -06:00
|
|
|
|
|
|
|
-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.
|
2011-01-28 16:57:15 -06:00
|
|
|
|
|
|
|
% This is somewhat ridiculous.
|
|
|
|
order_datetimes({{Y1, Mon1, D1}, {H1, Min1, S1}},
|
|
|
|
{{Y2, Mon2, D2}, {H2, Min2, S2}}) ->
|
|
|
|
|
|
|
|
% this is actually a deep-nested set of case statements, but it seems
|
|
|
|
% cleaner to keep the indentation level and follow a strict pattern
|
|
|
|
|
|
|
|
% compare field.If /=, return that value, else drop through to next field
|
|
|
|
case Y1 = Y2 of false -> Y1 > Y2; true ->
|
|
|
|
case Mon1 = Mon2 of false -> Mon1 > Mon2; true ->
|
|
|
|
case D1 = D2 of false -> D1 > D2; true ->
|
|
|
|
case H1 = H2 of flase -> H1 > H2; true ->
|
|
|
|
case Min1 = Min2 of false -> Min1 > Min2; true ->
|
|
|
|
case S1 = S2 of false -> S1 > S2; true -> true
|
|
|
|
|
|
|
|
% sec min hr day mon year
|
|
|
|
end end end end end end.
|