summaryrefslogtreecommitdiff
path: root/src/mango/src/mango_crud.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/mango/src/mango_crud.erl')
-rw-r--r--src/mango/src/mango_crud.erl58
1 files changed, 26 insertions, 32 deletions
diff --git a/src/mango/src/mango_crud.erl b/src/mango/src/mango_crud.erl
index 41a4d143d..c13dbdcb9 100644
--- a/src/mango/src/mango_crud.erl
+++ b/src/mango/src/mango_crud.erl
@@ -24,14 +24,12 @@
collect_cb/2
]).
-
-include_lib("couch/include/couch_db.hrl").
-include("mango.hrl").
-
-insert(Db, #doc{}=Doc, Opts) ->
+insert(Db, #doc{} = Doc, Opts) ->
insert(Db, [Doc], Opts);
-insert(Db, {_}=Doc, Opts) ->
+insert(Db, {_} = Doc, Opts) ->
insert(Db, [Doc], Opts);
insert(Db, Docs, Opts0) when is_list(Docs) ->
Opts1 = maybe_add_user_ctx(Db, Opts0),
@@ -45,14 +43,12 @@ insert(Db, Docs, Opts0) when is_list(Docs) ->
{error, lists:map(fun result_to_json/1, Errors)}
end.
-
find(Db, Selector, Callback, UserAcc, Opts0) ->
Opts1 = maybe_add_user_ctx(Db, Opts0),
Opts2 = maybe_int_to_str(r, Opts1),
{ok, Cursor} = mango_cursor:create(Db, Selector, Opts2),
mango_cursor:execute(Cursor, Callback, UserAcc).
-
update(Db, Selector, Update, Options) ->
Upsert = proplists:get_value(upsert, Options),
case collect_docs(Db, Selector, Options) of
@@ -65,47 +61,51 @@ update(Db, Selector, Update, Options) ->
% Probably need to catch and rethrow errors from
% this function.
Doc = couch_doc:from_json_obj(InitDoc),
- NewDoc = case Doc#doc.id of
- <<"">> ->
- Doc#doc{id=couch_uuids:new(), revs={0, []}};
- _ ->
- Doc
- end,
+ NewDoc =
+ case Doc#doc.id of
+ <<"">> ->
+ Doc#doc{id = couch_uuids:new(), revs = {0, []}};
+ _ ->
+ Doc
+ end,
insert(Db, NewDoc, Options)
end;
{ok, Docs} ->
- NewDocs = lists:map(fun(Doc) ->
- mango_doc:apply_update(Doc, Update)
- end, Docs),
+ NewDocs = lists:map(
+ fun(Doc) ->
+ mango_doc:apply_update(Doc, Update)
+ end,
+ Docs
+ ),
insert(Db, NewDocs, Options);
Else ->
Else
end.
-
delete(Db, Selector, Options) ->
case collect_docs(Db, Selector, Options) of
{ok, Docs} ->
- NewDocs = lists:map(fun({Props}) ->
- {[
- {<<"_id">>, proplists:get_value(<<"_id">>, Props)},
- {<<"_rev">>, proplists:get_value(<<"_rev">>, Props)},
- {<<"_deleted">>, true}
- ]}
- end, Docs),
+ NewDocs = lists:map(
+ fun({Props}) ->
+ {[
+ {<<"_id">>, proplists:get_value(<<"_id">>, Props)},
+ {<<"_rev">>, proplists:get_value(<<"_rev">>, Props)},
+ {<<"_deleted">>, true}
+ ]}
+ end,
+ Docs
+ ),
insert(Db, NewDocs, Options);
Else ->
Else
end.
-
explain(Db, Selector, Opts0) ->
Opts1 = maybe_add_user_ctx(Db, Opts0),
Opts2 = maybe_int_to_str(r, Opts1),
{ok, Cursor} = mango_cursor:create(Db, Selector, Opts2),
mango_cursor:explain(Cursor).
-
maybe_add_user_ctx(Db, Opts) ->
case lists:keyfind(user_ctx, 1, Opts) of
{user_ctx, _} ->
@@ -114,7 +114,6 @@ maybe_add_user_ctx(Db, Opts) ->
[{user_ctx, couch_db:get_user_ctx(Db)} | Opts]
end.
-
maybe_int_to_str(_Key, []) ->
[];
maybe_int_to_str(Key, [{Key, Val} | Rest]) when is_integer(Val) ->
@@ -122,8 +121,7 @@ maybe_int_to_str(Key, [{Key, Val} | Rest]) when is_integer(Val) ->
maybe_int_to_str(Key, [KV | Rest]) ->
[KV | maybe_int_to_str(Key, Rest)].
-
-result_to_json(#doc{id=Id}, Result) ->
+result_to_json(#doc{id = Id}, Result) ->
result_to_json(Id, Result);
result_to_json({Props}, Result) ->
Id = couch_util:get_value(<<"_id">>, Props),
@@ -149,7 +147,6 @@ result_to_json(DocId, Error) ->
{reason, Reason}
]}.
-
% This is for errors because for some reason we
% need a different return value for errors? Blargh.
result_to_json({{Id, Rev}, Error}) ->
@@ -161,7 +158,6 @@ result_to_json({{Id, Rev}, Error}) ->
{reason, Reason}
]}.
-
collect_docs(Db, Selector, Options) ->
Cb = fun ?MODULE:collect_cb/2,
case find(Db, Selector, Cb, [], Options) of
@@ -171,7 +167,5 @@ collect_docs(Db, Selector, Options) ->
Else
end.
-
collect_cb({row, Doc}, Acc) ->
{ok, [Doc | Acc]}.
-