-module(ts_common). -export([list/3, compare_dates/2]). -include_lib("stdlib/include/qlc.hrl"). %% list number of records, skipping the first 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.