timestamper/src/ts_common.erl
Jonathan Bernard c04b73d9cc More implementation.
Modified intent of ts_db_records. timestamp value is no longer a
  {Date, Time} value, it is now gregorian seconds since year 0.
  This was done to make queries that are based on date comparisons
  easier to write and more efficient.
Added date time comparison function to ts_common.
Implemented update/1 and list/3 for ts_entry.
2011-01-28 16:57:15 -06:00

48 lines
1.5 KiB
Erlang

-module(ts_common).
-export([list/3, compare_dates/2]).
-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.
% 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.