Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marcelog delete by 8 #22

Merged
merged 7 commits into from
Sep 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion examples/blog/run
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ main(_) ->

% Let's create them again
Author2 = blog:new_author("Pepe Gorostiga", <<"some binary jpg">>),
Author3 = blog:new_author("Some other guy", <<"some other photo">>),
Post2 = blog:new_post("How awesome sumo_db is", "It just make things easy", Author2),

% Update author and post.
blog:save_author(blog_author:update_photo(<<"new photo">>, Author2)),
blog:save_post(blog_post:update_title("My new shiny title", Post2)),

io:format("Authors by name: ~p~n", [blog:find_authors_by_name("Pepe Gorostiga", 10, 0)]),
io:format("Authors: ~p~n", [blog:find_all_authors(10, 0)]),
io:format("A blog: ~p~n", [blog:find_post(blog_post:id(Post2))]),
io:format("An author: ~p~n", [blog:find_author(blog_author:id(Author2))]),

Expand All @@ -54,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
16 changes: 15 additions & 1 deletion examples/blog/src/blog.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@

%%% Author API.
-export([
new_author/2, save_author/1, del_author/0, del_author/1, find_author/1
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 All @@ -46,6 +47,14 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Code starts here.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% @doc Paginates all authors, sorts by name.
-spec find_all_authors(pos_integer(), pos_integer()) -> [blog_author:author()].
find_all_authors(Limit, Offset) ->
sumo:find_all(blog_author, name, Limit, Offset).

-spec find_authors_by_name(string(), pos_integer(), pos_integer()) -> [blog_author:author()].
find_authors_by_name(Name, Limit, Offset) ->
sumo:find_by(blog_author, [{name, list_to_atom(Name)}], Limit, Offset).

%% @doc Finds a post given the id.
-spec find_post(blog_post:id()) -> blog_post:post()|notfound.
Expand Down Expand Up @@ -82,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
Loading