Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5013713
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类:

2012-09-23 11:57:47

使用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.  -include("defined.hrl").
  4.  -include_lib("stdlib/include/qlc.hrl").


  5.  start() ->
  6.      mnesia:start(),
  7.      %% 等待表的加载
  8.      mnesia:wait_for_tables([shop,cost],20000).

  9.  %% 初始化mnesia表结构
  10.  init() ->
  11.      mnesia:create_schema([node()]),
  12.      mnesia:start(),
  13.      %% 表创建 mnesia:create_talbe(TableName,[Args])
  14.      %% {type,Type} set,ordered_set,bag 表类型
  15.      %% {ram_copies,NodeList} NodeList每个节点都有内存备份 默认为这个{ram_copies,[node()]}
  16.      %% {disc_copies,NodeList} NodeList每个节点都有内存备份和磁盘备份
  17.      %% {disc_only_copies,NodeList} NodeList每个节点有磁盘备份
  18.      %% {attributes,AtomList} 要保存的列名称 一般和record有关 record_info(fields,RecordName)
  19.      mnesia:create_table(shop,[{attributes,record_info(fields,shop)}]), %% 创建shop表
  20.      mnesia:create_table(cost,[{attributes,record_info(fields,cost)}]),
  21.      mnesia:stop().

  22.  %% 加载测试数据
  23.  reset_tables() ->
  24.      mnesia:clear_table(shop),
  25.      mnesia:clear_table(cost),
  26.      F = fun() ->
  27.              lists:foreach(fun mnesia:write/1,example_tables())
  28.      end,
  29.      mnesia:transaction(F).


  30.  %% 测试数据
  31.  example_tables() ->
  32.      [
  33.          %% shop table
  34.          {shop,apple,20,2.3},
  35.          {shop,orange,100,3.8},
  36.          {shop,pear,200,3.6},
  37.          {shop,banana,420,4.5},
  38.          {shop,potato,2456,1.2},
  39.          %% cost table
  40.          {cost,apple,1.5},
  41.          {cost,orange,2.4},
  42.          {cost,pear,2.2},
  43.          {cost,banana,1.6},
  44.          {cost,potato,0.6}
  45.      ].

common_query.erl


 

  1. -module(common_query).
  2.  -compile(export_all).

  3.  -include("defined.hrl").
  4.  -include_lib("stdlib/include/qlc.hrl").


  5.  %% 指定表的数据量
  6.  count(Table) ->
  7.      case list(Table) of
  8.          {error,Msg} -> {error,Msg};
  9.          {ok,[]} -> 0;
  10.          {ok,L} -> length(L)
  11.      end.

  12.  %% 按条件查询指定表满足条件的数据的数量
  13.  count(Table,Where) ->
  14.      case list(Table,Where) of
  15.          {error,Msg} -> {error,Msg};
  16.          {ok,[]} -> 0;
  17.          {ok,L} -> length(L)
  18.      end.


  19.  %% 查看指定Table中的所有数据
  20.  list(Table) ->
  21.      F = fun() ->
  22.          qlc:e(qlc:q([X||X<-mnesia:table(Table)]))
  23.      end,
  24.      {atomic,L} = mnesia:transaction(F),
  25.      {ok,L}.

  26.  %% 按条件查询指定表满足条件的所有数据
  27.  list(Table,Where) ->
  28.      case query_func(Table,Where) of
  29.          {error,Msg} -> {error,Msg};
  30.          {ok,F} ->
  31.              {atomic,L} = mnesia:transaction(F),
  32.              {ok,L}
  33.      end.

  34.  %% 分页取表信息
  35.  %% @type Table = atom()
  36.  %% @type Offset = integer() > 0
  37.  %% @type Limit = integer() > 0
  38.  %% @spec list(site,1,20) -> [L] | {error,badarg}
  39.  list(Table,Offset,Limit) ->
  40.      if
  41.          is_integer(Offset) and is_integer(Limit) and (Offset > 0) and (Limit > 0) ->
  42.              F=fun() ->
  43.                    QH=qlc:q([X||X<-mnesia:table(Table)]),
  44.                    Qc=qlc:cursor(QH),
  45.                    case Offset of
  46.                        1 -> skip;
  47.                        _ -> qlc:next_answers(Qc,Offset-1)
  48.                    end,
  49.                    qlc:next_answers(Qc,Limit)
  50.              end,
  51.              {atomic,L} = mnesia:transaction(F),
  52.              {ok,L};
  53.          true -> {error,badarg}
  54.      end.
  55.   
  56.  %% 按Where条件搜索分页取表信息
  57.  %% @type Table = atom()
  58.  %% @type Offset = integer() > 0
  59.  %% @type Limit = integer() > 0
  60.  %% @type Where = [{agent,Agent}] | [{creator,Creator}]
  61.  %% @spec list(site,1,20,[{agent,shopex}]) -> [L] | {error,badarg}
  62.  list(Table,Offset,Limit,Where) ->
  63.      if
  64.          is_integer(Offset) and is_integer(Limit) and (Offset > 0) and (Limit > 0) ->
  65.              case query_func(Table,Offset,Limit,Where) of
  66.                  {error,Msg} -> {error,Msg};
  67.                  {ok,F} ->
  68.                      {atomic,L} = mnesia:transaction(F),
  69.                      L
  70.              end;
  71.          true -> {error,badarg}
  72.      end.

  73.  %% 查询方法 翻页使用
  74.  query_func(Table,Offset,Limit,Where) ->
  75.      case query_cond(Table,Where) of
  76.          {ok,QH} ->
  77.              {ok,fun() ->
  78.                    Qc=qlc:cursor(QH),
  79.                    case Offset of
  80.                        1 -> skip;
  81.                        _ -> qlc:next_answers(Qc,Offset-1)
  82.                    end,
  83.                    qlc:next_answers(Qc,Limit)
  84.              end};
  85.          {error,Msg} -> {error,Msg}
  86.      end.

  87.  %% 查询方法(count使用)
  88.  query_func(Table,Where) ->
  89.      case query_cond(Table,Where) of
  90.          {ok,QH} ->
  91.              {ok,fun() -> qlc:e(QH) end};
  92.          {error,Msg} -> {error,Msg}
  93.      end.

  94.  %% 查询商品(shop)
  95.  query_cond(shop,Where) ->
  96.      case Where of
  97.              %% 按Item查询
  98.              [{item,Item}] ->
  99.                  QH=qlc:q([X || X <- mnesia:table(shop),
  100.                                      X#shop.item =:= Item]),
  101.                  {ok,QH};
  102.              [_] ->
  103.                  {error,badarg}
  104.          end;


  105.  %% 没有相关表的query_cond 都是报错的
  106.  query_cond(_,_) ->
  107.      {error,badarg}.

原文来自:

http://www.cnblogs.com/bluefrog/archive/2012/06/13/2548405.html


阅读(852) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~