I assume that username is a primary key column, hence you can just use:
mnesia:transaction(fun() ->
case mnesia:read(users, UsernameOrNickname) of
[] -> {error, "Username not found"};
[User] -> {ok, User}
end)
But nickname column is not a key, hence you have to add an index for it first:
mnesia:add_table_index(users, nickname)
and later you can use the following code to fetch user by username or nickname:
mnesia:transaction(fun() ->
case mnesia:read(users, UsernameOrNickname) of
[] ->
case mnesia:index_read(users, UsernameOrNickname, #users.nickname) of
[] -> {error, "User not found"};
[User1] -> {ok, User1}
end;
[User2] -> {ok, User2}
end)
Code written from top of my head without testing, take with a grain of salt.
Cheers,
Gleb
> _______________________________________________
> erlang-questions mailing list
>
erlang-q...@erlang.org
>
http://erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions