Skip to content

Commit

Permalink
[#192] Make Riak store take care of every condition when building the…
Browse files Browse the repository at this point in the history
… query
  • Loading branch information
harenson committed Feb 25, 2016
1 parent 0c51a27 commit 29bbad8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/sumo_store_riak.erl
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,12 @@ find_by(DocName, Conditions, State) ->
) -> sumo_store:result([sumo_internal:doc()], state()).
find_by(DocName, Conditions, Limit, Offset, State) when is_list(Conditions) ->
IdField = sumo_internal:id_field_name(DocName),
%% If the key field is present in the conditions, we are looking for a
%% particular document. If not, it is a general query.
case lists:keyfind(IdField, 1, Conditions) of
{_K, Key} ->
%% If **ONLY** the key field is present in the conditions, we are looking
%% for a particular document. Otherwise, it is a general query.
IdFieldFound = lists:keymember(IdField, 1, Conditions),
case {IdFieldFound, length(Conditions)} of
{true, 1} ->
{_K, Key} = lists:keyfind(IdField, 1, Conditions),
find_by_id_field(DocName, Key, State);
_ ->
find_by_query(DocName, Conditions, Limit, Offset, State)
Expand Down
6 changes: 5 additions & 1 deletion test/sumo_basic_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ find_by_module(Module) ->
{Today, _} = Module:created_at(LastPerson),

%% Check find_by ID
[First1] = sumo:find_by(Module, [{id, Module:id(First)}]),
FirstId = Module:id(First),
[First1] = sumo:find_by(Module, [{id, FirstId}]),
[First1] = sumo:find_by(Module, [{last_name, <<"D">>},
{id, FirstId}]),
[] = sumo:find_by(Module, [{name, <<"NotB">>}, {id, FirstId}]),
First1 = First,
%% Check pagination
Results1 = sumo:find_by(Module, [], 3, 1),
Expand Down

0 comments on commit 29bbad8

Please sign in to comment.