Hi,
Is there a way to specify a ‘begins with’ style query in Mnesia? My keys are strings, and I’d like to get all of them sharing a given prefix.
Thanks,
Janos
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions
Thanks for the answer. As far as I understand it works great if the prefix is known at compile time. I wasn’t clear on this, but in my case the prefix is given in runtime.
So far I come up with this solution. It understands a ‘*’ character at a pattern’s end. I’d be happy to get suggestions to improve it.
%% finds '*' in pattern and return a minimum and maximum pattern
%% e.g. <<"hello*">> -> {<<"hello">>, <<"hellp">>}
%% if binary string X starts with 'hello' then <<"hello">> <= X < <<"hellp">>
wildcard(Pattern) when is_binary(Pattern) ->
case binary:match(Pattern, <<"*">>) of
nomatch ->
{Pattern, Pattern};
{0, _} ->
{error, wrong_pattern};
{S, _} ->
PatPref = binary:part(Pattern, 0, S-1),
io:format("~s", [PatPref]),
<<PatLast:8>> = binary:part(Pattern, S-1, 1),
{<<PatPref/binary, PatLast:8>>, <<PatPref/binary, (PatLast+1):8>>}
end.
find(Pat) ->
{PatMin, PatMax} = wildcard(Pat),
F = fun() ->
qlc:eval(qlc:q(
[Patient || #patient{search_name = N}=Patient <- mnesia:table(pat),
N >= PatMin, N < PatMax]
))
end,
mnesia:activity(transaction, F, [], mnesia_frag).
Regards,
Janos
Thanks for the answer. As far as I understand it works great if the prefix is known at compile time. I wasn’t clear on this, but in my case the prefix is given in runtime.
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions