使用mneisa存储数据不想每次mneisa:table的调用 所以写了个简单的orm只有查询
defined.hrl
- %% 定义记录结构
- -record(shop,{item,quantity,cost}).
- -record(cost,{name,price}).
init_data.erl
- -module(init_data).
- -compile(export_all).
- -include("defined.hrl").
- -include_lib("stdlib/include/qlc.hrl").
- start() ->
- mnesia:start(),
- %% 等待表的加载
- mnesia:wait_for_tables([shop,cost],20000).
- %% 初始化mnesia表结构
- init() ->
- mnesia:create_schema([node()]),
- mnesia:start(),
- %% 表创建 mnesia:create_talbe(TableName,[Args])
- %% {type,Type} set,ordered_set,bag 表类型
- %% {ram_copies,NodeList} NodeList每个节点都有内存备份 默认为这个{ram_copies,[node()]}
- %% {disc_copies,NodeList} NodeList每个节点都有内存备份和磁盘备份
- %% {disc_only_copies,NodeList} NodeList每个节点有磁盘备份
- %% {attributes,AtomList} 要保存的列名称 一般和record有关 record_info(fields,RecordName)
- mnesia:create_table(shop,[{attributes,record_info(fields,shop)}]), %% 创建shop表
- mnesia:create_table(cost,[{attributes,record_info(fields,cost)}]),
- mnesia:stop().
- %% 加载测试数据
- reset_tables() ->
- mnesia:clear_table(shop),
- mnesia:clear_table(cost),
- F = fun() ->
- lists:foreach(fun mnesia:write/1,example_tables())
- end,
- mnesia:transaction(F).
- %% 测试数据
- example_tables() ->
- [
- %% shop table
- {shop,apple,20,2.3},
- {shop,orange,100,3.8},
- {shop,pear,200,3.6},
- {shop,banana,420,4.5},
- {shop,potato,2456,1.2},
- %% cost table
- {cost,apple,1.5},
- {cost,orange,2.4},
- {cost,pear,2.2},
- {cost,banana,1.6},
- {cost,potato,0.6}
- ].
common_query.erl
- -module(common_query).
- -compile(export_all).
- -include("defined.hrl").
- -include_lib("stdlib/include/qlc.hrl").
- %% 指定表的数据量
- count(Table) ->
- case list(Table) of
- {error,Msg} -> {error,Msg};
- {ok,[]} -> 0;
- {ok,L} -> length(L)
- end.
- %% 按条件查询指定表满足条件的数据的数量
- count(Table,Where) ->
- case list(Table,Where) of
- {error,Msg} -> {error,Msg};
- {ok,[]} -> 0;
- {ok,L} -> length(L)
- end.
- %% 查看指定Table中的所有数据
- list(Table) ->
- F = fun() ->
- qlc:e(qlc:q([X||X<-mnesia:table(Table)]))
- end,
- {atomic,L} = mnesia:transaction(F),
- {ok,L}.
- %% 按条件查询指定表满足条件的所有数据
- list(Table,Where) ->
- case query_func(Table,Where) of
- {error,Msg} -> {error,Msg};
- {ok,F} ->
- {atomic,L} = mnesia:transaction(F),
- {ok,L}
- end.
- %% 分页取表信息
- %% @type Table = atom()
- %% @type Offset = integer() > 0
- %% @type Limit = integer() > 0
- %% @spec list(site,1,20) -> [L] | {error,badarg}
- list(Table,Offset,Limit) ->
- if
- is_integer(Offset) and is_integer(Limit) and (Offset > 0) and (Limit > 0) ->
- F=fun() ->
- QH=qlc:q([X||X<-mnesia:table(Table)]),
- Qc=qlc:cursor(QH),
- case Offset of
- 1 -> skip;
- _ -> qlc:next_answers(Qc,Offset-1)
- end,
- qlc:next_answers(Qc,Limit)
- end,
- {atomic,L} = mnesia:transaction(F),
- {ok,L};
- true -> {error,badarg}
- end.
-
- %% 按Where条件搜索分页取表信息
- %% @type Table = atom()
- %% @type Offset = integer() > 0
- %% @type Limit = integer() > 0
- %% @type Where = [{agent,Agent}] | [{creator,Creator}]
- %% @spec list(site,1,20,[{agent,shopex}]) -> [L] | {error,badarg}
- list(Table,Offset,Limit,Where) ->
- if
- is_integer(Offset) and is_integer(Limit) and (Offset > 0) and (Limit > 0) ->
- case query_func(Table,Offset,Limit,Where) of
- {error,Msg} -> {error,Msg};
- {ok,F} ->
- {atomic,L} = mnesia:transaction(F),
- L
- end;
- true -> {error,badarg}
- end.
- %% 查询方法 翻页使用
- query_func(Table,Offset,Limit,Where) ->
- case query_cond(Table,Where) of
- {ok,QH} ->
- {ok,fun() ->
- Qc=qlc:cursor(QH),
- case Offset of
- 1 -> skip;
- _ -> qlc:next_answers(Qc,Offset-1)
- end,
- qlc:next_answers(Qc,Limit)
- end};
- {error,Msg} -> {error,Msg}
- end.
- %% 查询方法(count使用)
- query_func(Table,Where) ->
- case query_cond(Table,Where) of
- {ok,QH} ->
- {ok,fun() -> qlc:e(QH) end};
- {error,Msg} -> {error,Msg}
- end.
- %% 查询商品(shop)
- query_cond(shop,Where) ->
- case Where of
- %% 按Item查询
- [{item,Item}] ->
- QH=qlc:q([X || X <- mnesia:table(shop),
- X#shop.item =:= Item]),
- {ok,QH};
- [_] ->
- {error,badarg}
- end;
- %% 没有相关表的query_cond 都是报错的
- query_cond(_,_) ->
- {error,badarg}.
原文来自:
http://www.cnblogs.com/bluefrog/archive/2012/06/13/2548405.html
阅读(886) | 评论(0) | 转发(0) |