-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity_db.erl
76 lines (67 loc) · 2.71 KB
/
entity_db.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
%% Copyright 2010 Ulf Angermann
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%% -------------------------------------------------------------------
%%% Author : Ulf [email protected]
%%%
%%% Description :
%%% Created :
%%% -------------------------------------------------------------------
-module({{appid}}_db).
%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------
-include_lib("stdlib/include/qlc.hrl").
-include("../include/{{appid}}.hrl").
%% --------------------------------------------------------------------
%% External exports
%% --------------------------------------------------------------------
-export([create_db/0]).
-export([create/2, update/1, delete/1, find_by_id/1, find_free_id/1]).
create_db() ->
case mnesia:create_schema([node()]) of
{error, Reason} -> error_logger:info_msg(Reason),
false;
_ -> application:start(mnesia),
mnesia:create_table({{entity}},[{disc_copies, [node()]}, {attributes, record_info(fields, {{entity}})}]),
mnesia:wait_for_tables([{{entity}}], 100000),
application:stop(mnesia)
end.
create(json, Properties) ->
Entity = #{{entity}}{id=proplists:get_value("id", Properties), created = erlang:universaltime(), lastmodified = erlang:universaltime(), version = 0, data = Properties},
mnesia:activity(transaction, fun() -> mnesia:write({{entity}}, Entity, write) end),
Entity;
create(xml, Entity) ->
mnesia:activity(transaction, fun() -> mnesia:write({{entity}}, Entity, write) end).
find_by_id(Id) ->
mnesia:activity(transaction, fun() ->mnesia:read({{entity}}, Id) end).
update(Entity) ->
ok.
delete(Id) ->
mnesia:activity(transaction, fun() -> mnesia:delete({{entity}}, Id, write) end).
find_free_id(Id) ->
case find_by_id(Id) of
[] -> Id;
_ -> find_free_id(Id, 0)
end.
find_free_id(Id, Count) ->
case find_by_id(Id ++ integer_to_list(Count)) of
[] -> Id ++ integer_to_list(Count);
_ -> find_free_id(Id, Count + 1)
end.
%% --------------------------------------------------------------------
%%% Test functions
%% --------------------------------------------------------------------
-include_lib("eunit/include/eunit.hrl").
-ifdef(TEST).
-endif.