[erlang-questions] How best to extract value from mnesia query

lloyd@REDACTED lloyd@REDACTED
Thu Nov 16 00:34:18 CET 2017


Hi Robby,

Much appreciate your helpful response.

Lloyd

-----Original Message-----
From: "Robert Raschke" <rtrlists@REDACTED>
Sent: Wednesday, November 15, 2017 4:46pm
To: lloyd@REDACTED
Cc: "Erlang Questions" <erlang-questions@REDACTED>
Subject: Re: [erlang-questions] How best to extract value from mnesia query

If I know that I shall only receive a single value result, I write

{atomic, [Value]} = mnesia:transaction(...)

This has the side effect of neatly terminating the process if we didn't get
exactly one value back as a result. And that allows me to catch the error
and decide what to do about it.

If you want to write defensively, then something that discriminates between
the results may be appropriate.

case mnesia:transaction(...) of
    {aborted, Reason} -> ...;
    {atomic, []} -> ...;
    {atomic, [Value]} -> ...;
    {atomic, Values} -> ...
end

Cheers,
Robby


On 15 Nov 2017 21:04, <lloyd@REDACTED> wrote:

Hello,

The result of a mnesia read transaction looks like this:

   {atomic, Results} where result is either a populated list [value1, ...
valueN] or, an empty list [].

If the table is initialized as set, then the returned list will contain at
most one value, e.g:

  [value].

If I want to use of the value in a function or to populate another record,
I need to extract the value from the list.

I've tried various ways to do this, but they all seem clumsy:

E.g.

[MyValue]

hd[vlaue]

confirm([]) ->
    not_found;
confirm([Value]) ->
    Value;
confirm(Value) ->
    Value.

Does there happen to be a preferred/conventional/best-practices way to do
this?

Many thanks,

LRP






_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://erlang.org/mailman/listinfo/erlang-questions





More information about the erlang-questions mailing list