[erlang] mnesia orm

使用mneisa存储数据不想每次mneisa:table的调用 所以写了个简单的orm只有查询

defined.hrl

1 %% 定义记录结构
2 -record(shop,{item,quantity,cost}).
3 -record(cost,{name,price}).

init_data.erl

 1 -module(init_data).
 2 -compile(export_all).
 3 
 4 -include("defined.hrl").
 5 -include_lib("stdlib/include/qlc.hrl").
 6 
 7 
 8 start() ->
 9     mnesia:start(),
10     %% 等待表的加载
11     mnesia:wait_for_tables([shop,cost],20000).
12 
13 %% 初始化mnesia表结构
14 init() ->
15     mnesia:create_schema([node()]),
16     mnesia:start(),
17     %% 表创建 mnesia:create_talbe(TableName,[Args])
18     %% {type,Type}  set,ordered_set,bag 表类型
19     %% {ram_copies,NodeList} NodeList每个节点都有内存备份 默认为这个{ram_copies,[node()]}
20     %% {disc_copies,NodeList} NodeList每个节点都有内存备份和磁盘备份 
21     %% {disc_only_copies,NodeList} NodeList每个节点有磁盘备份 
22     %% {attributes,AtomList} 要保存的列名称 一般和record有关 record_info(fields,RecordName)
23     mnesia:create_table(shop,[{attributes,record_info(fields,shop)}]), %% 创建shop表
24     mnesia:create_table(cost,[{attributes,record_info(fields,cost)}]),
25     mnesia:stop().
26 
27 %% 加载测试数据
28 reset_tables() ->
29     mnesia:clear_table(shop),
30     mnesia:clear_table(cost),
31     F = fun() ->
32             lists:foreach(fun mnesia:write/1,example_tables())
33     end,
34     mnesia:transaction(F).
35 
36 
37 %% 测试数据
38 example_tables() ->
39     [
40         %% shop table
41         {shop,apple,20,2.3},
42         {shop,orange,100,3.8},
43         {shop,pear,200,3.6},
44         {shop,banana,420,4.5},
45         {shop,potato,2456,1.2},
46         %% cost table
47         {cost,apple,1.5},
48         {cost,orange,2.4},
49         {cost,pear,2.2},
50         {cost,banana,1.6},
51         {cost,potato,0.6}
52     ].

common_query.erl

  1 -module(common_query).
  2 -compile(export_all).
  3 
  4 -include("defined.hrl").
  5 -include_lib("stdlib/include/qlc.hrl").
  6 
  7 
  8 %% 指定表的数据量
  9 count(Table) ->
 10     case list(Table) of
 11         {error,Msg} -> {error,Msg};
 12         {ok,[]}     -> 0;
 13         {ok,L}      -> length(L)
 14     end.
 15 
 16 %% 按条件查询指定表满足条件的数据的数量
 17 count(Table,Where) ->
 18     case list(Table,Where) of
 19         {error,Msg} -> {error,Msg};
 20         {ok,[]}     -> 0;
 21         {ok,L}      -> length(L)
 22     end.
 23 
 24 
 25 %% 查看指定Table中的所有数据
 26 list(Table) ->
 27     F = fun() ->
 28         qlc:e(qlc:q([X||X<-mnesia:table(Table)]))
 29     end,
 30     {atomic,L} = mnesia:transaction(F),
 31     {ok,L}.
 32 
 33 %% 按条件查询指定表满足条件的所有数据
 34 list(Table,Where) ->
 35     case query_func(Table,Where) of
 36         {error,Msg} -> {error,Msg};
 37         {ok,F}      ->
 38             {atomic,L} = mnesia:transaction(F),
 39             {ok,L}
 40     end.
 41 
 42 %% 分页取表信息
 43 %% @type Table  = atom()
 44 %% @type Offset = integer() > 0
 45 %% @type Limit  = integer() > 0
 46 %% @spec list(site,1,20) -> [L] | {error,badarg} 
 47 list(Table,Offset,Limit) ->
 48     if 
 49         is_integer(Offset) and is_integer(Limit) and (Offset > 0) and (Limit > 0) -> 
 50             F=fun() ->
 51                   QH=qlc:q([X||X<-mnesia:table(Table)]),
 52                   Qc=qlc:cursor(QH),          
 53                   case Offset of
 54                       1 -> skip;
 55                       _ -> qlc:next_answers(Qc,Offset-1)
 56                   end,
 57                   qlc:next_answers(Qc,Limit)
 58             end,
 59             {atomic,L} = mnesia:transaction(F),
 60             {ok,L};
 61         true -> {error,badarg}
 62     end.
 63  
 64 %% 按Where条件搜索分页取表信息
 65 %% @type Table  = atom()
 66 %% @type Offset = integer() > 0
 67 %% @type Limit  = integer() > 0
 68 %% @type Where  = [{agent,Agent}] | [{creator,Creator}]
 69 %% @spec list(site,1,20,[{agent,shopex}]) -> [L] | {error,badarg} 
 70 list(Table,Offset,Limit,Where) ->
 71     if 
 72         is_integer(Offset) and is_integer(Limit) and (Offset > 0) and (Limit > 0) -> 
 73             case query_func(Table,Offset,Limit,Where) of
 74                 {error,Msg} -> {error,Msg};
 75                 {ok,F}      ->
 76                     {atomic,L} = mnesia:transaction(F),
 77                     L
 78             end;
 79         true -> {error,badarg}
 80     end.
 81 
 82 %% 查询方法 翻页使用
 83 query_func(Table,Offset,Limit,Where) ->
 84     case query_cond(Table,Where) of
 85         {ok,QH} ->
 86             {ok,fun() ->
 87                   Qc=qlc:cursor(QH),          
 88                   case Offset of
 89                       1 -> skip;
 90                       _ -> qlc:next_answers(Qc,Offset-1)
 91                   end,
 92                   qlc:next_answers(Qc,Limit)
 93             end};
 94         {error,Msg} -> {error,Msg}
 95     end.
 96 
 97 %% 查询方法(count使用)
 98 query_func(Table,Where) ->
 99     case query_cond(Table,Where) of
100         {ok,QH} ->
101             {ok,fun() -> qlc:e(QH) end};
102         {error,Msg} -> {error,Msg}
103     end.
104 
105 %% 查询商品(shop) 
106 query_cond(shop,Where) ->
107     case Where of
108             %% 按Item查询
109             [{item,Item}] ->
110                 QH=qlc:q([X || X <- mnesia:table(shop),
111                                     X#shop.item =:= Item]),
112                 {ok,QH};
113             [_] ->
114                 {error,badarg}
115         end;
116 
117 
118 %% 没有相关表的query_cond 都是报错的
119 query_cond(_,_) ->
120     {error,badarg}.

 

posted on 2012-06-13 19:41  bluefrog  阅读(1527)  评论(1编辑  收藏  举报