Erlang中ets模块中的fun2ms/1。

问题来源:

http://stackoverflow.com/questions/16660955/mnesia-check-for-a-value-in-either-of-the-keys-in-the-table/16769468#16769468

关于此函数的分析:

http://blog.yufeng.info/archives/482#more-482


自己的代码实现:

-module(wy).
-compile(export_all).

-record(users, {username, nickname, age}).
-include_lib("stdlib/include/ms_transform.hrl").

create_table()->
    mnesia:create_schema(node()),
    mnesia:start(),
    mnesia:create_table(users, [{attributes, record_info(fields, users)}]).

test()->
    Fun = fun()->
                  mnesia:write(#users{username = "John", nickname = "baddy", age = 25}),
                  mnesia:write(#users{username = "DaDa", nickname = "KaKa", age = 24}),
                  mnesia:write(#users{username = "Monk", nickname = "John", age = 24})
          end,
    mnesia:transaction(Fun),

    Fun2 = ets:fun2ms(fun({users, UserName, NickName, Age}) when
        UserName == "John" orelse NickName == "John" ->
                              {users, UserName, NickName, Age}
                      end),

    mnesia:transaction(fun() -> mnesia:select(users, Fun2) end).


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页