timestamper/src/ts_timeline.erl
Jonathan Bernard 495336fc58 Starting DB implementation.
Added Makefile, id_counter.erl, and yaws.conf.
Created ts_common module. Will contain common DB code. So far only list/3.
Created ts_timeline module. DB code for storing timeline entries.
Created ts_entry module, DB code for storing timelin entries.
2011-01-28 06:49:47 -06:00

35 lines
1000 B
Erlang

-module(ts_timeline).
-export([create_table/1, new/1, update/1, list/3]).
-include("ts_db_records.hrl").
-include_lib("stdlib/include/qlc.hrl").
create_table(TableOpts) ->
mnesia:create_table(ts_timeline,
TableOpts ++ [{attributes, record_info(fields, ts_timeline)},
{type, ordered_set}]).
new(TR = #ts_timeline{}) ->
case mnesia:dirty_read(ts_timeline, TR#ts_timeline.ref) of
[ExistingRecord] -> {error, {record_exists, ExistingRecord}};
[] -> mnesia:dirty_write(TR)
end.
update(TR = #ts_timeline{}) ->
% look for the existing record
case mnesia:dirty_read(ts_timeline, TR#ts_timeline.ref) of
% record does not exist
[] -> no_record;
% record exists, update
[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).