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.
This commit is contained in:
Jonathan Bernard 2011-01-28 16:57:15 -06:00
parent 495336fc58
commit c04b73d9cc
4 changed files with 61 additions and 6 deletions

View File

@ -1,5 +1,5 @@
-module(ts_common).
-export([list/3]).
-export([list/3, compare_dates/2]).
-include_lib("stdlib/include/qlc.hrl").
@ -27,3 +27,21 @@ list(Query, Start, Length) ->
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.

View File

@ -6,7 +6,7 @@
-record(ts_entry, {
ref, % {username, timelineid, eventid}
timestamp, % {{year, month, day}, {hour, minute, second}}
timestamp, % gregorian seconds
mark, % String description of entry
notes % String with further notes about the entry
}).

View File

@ -17,3 +17,41 @@ new(ER = #ts_entry()) ->
ok = mnesia:write(NewRow),
NewRow end),
{ok, NewRow}.
update(ER = #ts_entry()) ->
% look for existing record
case mnesia:dirty_read(ts_entry, ER#ts_entry.ref) of
% record does not exist
[] -> no_record;
% record exists, update it
[_Record] -> mnesia:dirty_write(ER)
end.
list({Username, Timeline}, Start, Length)
when is_integer(Start) and is_integer(Length) ->
ts_common:list(
qlc:q([E || E <- mnesia:table(ts_entry),
E#ts_entry.ref =:= {Username, Timeline, _}]_,
Start, Length).
list({Username, Timeline}, StartDateTime, EndDateTime) ->
{atomic, Entries} = mnesia:transaction(fun() ->
StartSeconds = calendar:datetime_to_gregorian_seconds(StartDateTime),
EndSeconds = calendar:datetime_to_gregorian_seconds(EndDateTime),
% create the query that will select these records
Q = qlc:q([E || E <- mnesia:table(ts_entry),
E#ts_entry.timestamp >= StartSeconds,
E#ts_entry.timestamp < EndSeconds]),
% sort by timestamp
SortedQ = qlc:sort(Q, {order, fun(A, B) ->
A#ts_entry.timestamp > B#ts_entry.timestamp end}),
% return
qlc:e(SortedQ)
end),
Entries.

View File

@ -22,13 +22,12 @@ update(TR = #ts_timeline{}) ->
% record does not exist
[] -> no_record;
% record exists, update
[Record] -> mnesia:dirty_write(TR)
[_Record] -> mnesia:dirty_write(TR)
end.
list(Username, Start, Length) ->
ts_common:list(
qlc:q([T || T <- mnesia:table(ts_timeline),
T#ts_timeline.ref = {Username, _}]),
Start, Length).
T#ts_timeline.ref =:= {Username, _}]),
Start, Length).