Skip to content

Commit

Permalink
Merge pull request #22 from inaka/marcelog_delete_by_8
Browse files Browse the repository at this point in the history
Marcelog delete by 8
  • Loading branch information
igaray committed Sep 18, 2013
2 parents a19cd69 + f7ce7be commit e804458
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 24 deletions.
4 changes: 3 additions & 1 deletion examples/blog/run
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ main(_) ->
io:format("Another Reader: ~p~n", [blog:find_reader(blog_reader:id(Reader))]),

% Create a vote
_Vote = blog:new_vote(blog_reader:id(Reader2), blog_post:id(Post2)).
_Vote = blog:new_vote(blog_reader:id(Reader2), blog_post:id(Post2)),

io:format("Deleted ~p authors~n", [blog:del_author_by_name("Pepe Gorostiga")]).

setup_codepath() ->
Dir = filename:dirname(escript:script_name()),
Expand Down
9 changes: 7 additions & 2 deletions examples/blog/src/blog.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@

%%% Author API.
-export([
new_author/2, save_author/1, del_author/0, del_author/1, find_author/1,
find_all_authors/2, find_authors_by_name/3
new_author/2, save_author/1, del_author/0, del_author/1, del_author_by_name/1,
find_author/1, find_all_authors/2, find_authors_by_name/3
]).

%%% Reader API.
Expand Down Expand Up @@ -91,6 +91,11 @@ del_post() ->
del_reader() ->
sumo:delete_all(blog_reader).

%% @doc Deletes the given author.
-spec del_author_by_name(string()) -> pos_integer().
del_author_by_name(Name) ->
sumo:delete_by(blog_author, [{name, Name}]).

%% @doc Deletes the given author.
-spec del_author(blog_author:author()) -> ok.
del_author(Author) ->
Expand Down
21 changes: 13 additions & 8 deletions src/sumo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
-export([create_schema/0, create_schema/1, create_schema/2]).

%%% API for standard CRUD functions.
-export([persist/2, delete/2, delete_all/1]).
-export([persist/2, delete/2, delete_by/2, delete_all/1]).
-export([find/2, find_all/1, find_all/4, find_by/2, find_by/4, find_one/2]).

%%% API for repo handling.
Expand Down Expand Up @@ -179,13 +179,18 @@ delete_all(DocName) ->
%% @doc Deletes the doc identified by Id.
-spec delete(sumo_schema_name(), term()) -> ok.
delete(DocName, Id) ->
case sumo_repo:delete(get_repo(DocName), DocName, Id) of
{ok, Result} ->
case Result of
true -> sumo_event:dispatch(DocName, deleted, [Id]);
false -> ok
end,
Result;
IdField = sumo:field_name(sumo:get_id_field(DocName)),
case delete_by(DocName, [{IdField, Id}]) of
1 -> sumo_event:dispatch(DocName, deleted, [Id]), true;
0 -> false
end.

%% @doc Deletes the doc identified by Conditions.
-spec delete_by(sumo_schema_name(), term()) -> ok.
delete_by(DocName, Conditions) ->
case sumo_repo:delete_by(get_repo(DocName), DocName, Conditions) of
{ok, 0} -> 0;
{ok, NumRows} -> sumo_event:dispatch(DocName, deleted, [NumRows]), NumRows;
Error -> throw(Error)
end.

Expand Down
25 changes: 19 additions & 6 deletions src/sumo_repo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
%% Exports.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Public API.
-export([
create_schema/2, persist/2, find_all/2, find_all/5, find_by/3, find_by/5,
delete/3, delete_all/2, call/4
]).
-export([create_schema/2]).
-export([persist/2]).
-export([find_all/2, find_all/5, find_by/3, find_by/5]).
-export([delete/3, delete_by/3, delete_all/2]).
-export([call/4]).
-export([start_link/3]).

%%% Exports for gen_server
Expand All @@ -60,8 +61,8 @@
-spec behaviour_info(callbacks) -> proplists:proplist()|undefined.
behaviour_info(callbacks) ->
[
{init,1}, {persist,2}, {delete,3}, {find_by,3}, {find_by,5},
{create_schema,2}
{init,1}, {persist,2}, {delete,3}, {delete_by, 3}, {delete_all, 2},
{find_by,3}, {find_by,5}, {create_schema,2}
];

behaviour_info(_Other) ->
Expand All @@ -80,6 +81,11 @@ persist(Name, #sumo_doc{}=Doc) ->
delete(Name, DocName, Id) ->
gen_server:call(Name, {delete, DocName, Id}).

%% @doc Deletes the docs identified by the given conditions.
-spec delete_by(atom(), sumo_schema_name(), proplists:proplist()) -> ok.
delete_by(Name, DocName, Conditions) ->
gen_server:call(Name, {delete_by, DocName, Conditions}).

%% @doc Deletes all docs in the given repository name.
-spec delete_all(atom(), sumo_schema_name()) -> ok.
delete_all(Name, DocName) ->
Expand Down Expand Up @@ -151,6 +157,13 @@ handle_call(
{OkOrError, Reply, NewState} = Handler:delete(DocName, Id, HState),
{reply, {OkOrError, Reply}, State#state{handler_state=NewState}};

handle_call(
{delete_by, DocName, Conditions}, _From,
#state{handler=Handler,handler_state=HState}=State
) ->
{OkOrError, Reply, NewState} = Handler:delete_by(DocName, Conditions, HState),
{reply, {OkOrError, Reply}, State#state{handler_state=NewState}};

handle_call(
{delete_all, DocName}, _From,
#state{handler=Handler,handler_state=HState}=State
Expand Down
6 changes: 5 additions & 1 deletion src/sumo_repo_mongo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
%%% Public API.
-export([
init/1, create_schema/2, persist/2, find_by/3, find_by/5,
delete/3, delete_all/2
delete/3, delete_by/3, delete_all/2
]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -67,6 +67,10 @@ delete(DocName, Id, #state{pool=Pool}=State) ->
),
{ok, 1, State}.

delete_by(DocName, Conditions, #state{pool=Pool}=State) ->
throw(not_implemented),
{ok, 0, State}.

delete_all(DocName, #state{pool=Pool}=State) ->
lager:debug("Dropping collection: ~p", [DocName]),
ok = emongo:delete(Pool, atom_to_list(DocName)),
Expand Down
18 changes: 17 additions & 1 deletion src/sumo_repo_mysql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
-export([init/1]).
-export([create_schema/2]).
-export([persist/2]).
-export([delete/3, delete_all/2]).
-export([delete/3, delete_by/3, delete_all/2]).
-export([prepare/3, execute/2, execute/3]).
-export([find_all/2, find_all/5, find_by/3, find_by/5]).

Expand Down Expand Up @@ -109,6 +109,22 @@ delete(DocName, Id, State) ->
Error -> evaluate_execute_result(Error, State)
end.

delete_by(DocName, Conditions, State) ->
PreStatementName = list_to_atom("delete_by_" ++ string:join(
[atom_to_list(K) || {K, _V} <- Conditions],
"_"
)),
StatementName = prepare(DocName, PreStatementName, fun() -> [
"DELETE FROM ", escape(atom_to_list(DocName)),
" WHERE ",
[[escape(atom_to_list(K)), "=?"] || {K, _V} <- Conditions]
] end),
Values = [V || {_K, V} <- Conditions],
case execute(StatementName, Values, State) of
#ok_packet{affected_rows = NumRows} -> {ok, NumRows, State};
Error -> evaluate_execute_result(Error, State)
end.

delete_all(DocName, State) ->
StatementName = prepare(DocName, delete_all, fun() ->
["DELETE FROM ", escape(atom_to_list(DocName))]
Expand Down
11 changes: 7 additions & 4 deletions src/sumo_repo_redis.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
%% Public API.
-export([
init/1, create_schema/2, persist/2, find_by/3, find_by/5,
delete/3, delete_all/2, execute/2, execute/3
delete/3, delete_by/3, delete_all/2, execute/2, execute/3
]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -67,13 +67,16 @@ persist(#sumo_doc{name=DocName, fields=Fields}=Doc, State) ->
),
{ok, NewDoc, State}.

delete(DocName, Id, State) ->
delete_by(_DocName, _Conditions, _State) ->
ok.

delete_all(DocName, State) ->
delete(_DocName, _Id, _State) ->
ok.

find_by(DocName, Conditions, Limit, Offset, State) ->
delete_all(_DocName, _State) ->
ok.

find_by(_DocName, _Conditions, _Limit, _Offset, _State) ->
ok.

find_by(DocName, Conditions, State) ->
Expand Down
5 changes: 4 additions & 1 deletion src/sumo_repo_sqlite3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
%% Public API.
-export([
init/1, create_schema/2, persist/2, find_by/3, find_by/5,
delete/3, delete_all/2, execute/2, execute/3
delete/3, delete_by/3, delete_all/2, execute/2, execute/3
]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand All @@ -53,6 +53,9 @@ persist(#sumo_doc{name=DocName}=Doc, State) ->
delete(DocName, Id, State) ->
ok.

delete_by(DocName, Conditions, State) ->
ok.

delete_all(DocName, State) ->
ok.

Expand Down

0 comments on commit e804458

Please sign in to comment.