[erlang-questions] Re: How to reduce the repetitious code in create mnesia table?
    Ulf Wiger 
    ulf.wiger@REDACTED
       
    Tue Mar 30 18:58:06 CEST 2010
    
    
  
Magnus Henoch wrote:
> 钱晓明 <kyleqian@REDACTED> writes:
> 
>> Is there any way I can do to reduce the repetitious code? I have
>> tried defining a new function accepting the name of record to do the same
>> thing above, but can not compile code because of record_info.
> 
> You could do it with a macro.  Something like:
> 
> -define(CHECK_CREATE_TABLE(Name, Attrs),
>         case mnesia:create_table(Name, [{attributes, record_info(fields, Name)}|Attrs]) of
>             {atomic, ok} -> ok;
>             {aborted, {already_exists, Name}} -> ok
>         end).
Having spent far too much time digging into mnesia internals, I prefer
to do it this way:
mktab(Name, Opts) ->
    mnesia_schema:do_create_table(
       mnesia_schema:list2cs([{name,Name}|Opts])).
mnesia_schema:schema_transaction(
    fun() ->
      mktab(t1, [{disc_copies, ...},...]),
      mktab(t2, [{ram_copies, ...},...]),
      ...
    end)
No need for lots of checks, and all tables are created, or
none are, which is usually what you want.
And, no, this isn't documented.
BR,
Ulf W
-- 
Ulf Wiger
CTO, Erlang Solutions Ltd, formerly Erlang Training & Consulting Ltd
http://www.erlang-solutions.com
---------------------------------------------------
---------------------------------------------------
WE'VE CHANGED NAMES!
Since January 1st 2010 Erlang Training and Consulting Ltd. has become ERLANG SOLUTIONS LTD.
www.erlang-solutions.com
    
    
More information about the erlang-questions
mailing list