diff --git a/.gitignore b/.gitignore index 7dcc781..5088c9e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.awconfig ebin deps doc @@ -9,4 +10,4 @@ examples/blog/log .rebar log*/ erl_crash.dump -.erlang.mk.* \ No newline at end of file +.erlang.mk.* diff --git a/Makefile b/Makefile index e3c4190..d5315c8 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ dep_pgsql = git https://github.com/epgsql/epgsql 2.0.0 dep_worker_pool = git https://github.com/inaka/worker_pool.git 1.0 TEST_DEPS = mixer -dep_mixer = git https://github.com/opscode/mixer.git 0.1.1 +dep_mixer = git git://github.com/inaka/mixer.git 0.1.2 include erlang.mk diff --git a/src/sumo_store.erl b/src/sumo_store.erl index 671bdb2..b03d029 100644 --- a/src/sumo_store.erl +++ b/src/sumo_store.erl @@ -77,6 +77,14 @@ -callback find_by(sumo:schema_name(), sumo:conditions(), non_neg_integer(), non_neg_integer(), State) -> result([sumo_internal:doc()], State). +-callback find_by(sumo:schema_name(), sumo:conditions(), sumo:sort(), + non_neg_integer(), non_neg_integer(), State) -> + result([sumo_internal:doc()], State). +-callback find_all(sumo:schema_name(), State) -> + result([sumo_internal:doc()], State). +-callback find_all(sumo:schema_name(), sumo:sort(), non_neg_integer(), + non_neg_integer(), State) -> + result([sumo_internal:doc()], State). -callback create_schema(sumo:schema(), State) -> result(State). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/src/sumo_store_elasticsearch.erl b/src/sumo_store_elasticsearch.erl index 20a43a8..bf361bc 100644 --- a/src/sumo_store_elasticsearch.erl +++ b/src/sumo_store_elasticsearch.erl @@ -33,6 +33,7 @@ persist/2, find_by/3, find_by/5, + find_by/6, find_all/2, find_all/5, delete/3, @@ -154,6 +155,16 @@ find_by(DocName, Conditions, Limit, Offset, {ok, Docs, State}. +-spec find_by(sumo:schema_name(), + sumo:conditions(), + sumo:sort(), + non_neg_integer(), + non_neg_integer(), + state()) -> + sumo_store:result([sumo_internal:doc()], state()). +find_by(_DocName, _Conditions, _SortFields, _Limit, _Offset, State) -> + {error, not_supported, State}. + -spec find_by(sumo:schema_name(), sumo:conditions(), state()) -> sumo_store:result([sumo_internal:doc()], state()). find_by(DocName, Conditions, State) -> diff --git a/src/sumo_store_mysql.erl b/src/sumo_store_mysql.erl index 8220266..27b44e2 100644 --- a/src/sumo_store_mysql.erl +++ b/src/sumo_store_mysql.erl @@ -36,6 +36,7 @@ -export([persist/2]). -export([delete/3, delete_by/3, delete_all/2]). -export([prepare/3, execute/2, execute/3]). +-export([just_execute/2, just_execute/3, get_docs/3, get_docs/4]). -export([find_all/2, find_all/5, find_by/3, find_by/5, find_by/6]). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -282,25 +283,10 @@ find_by(DocName, Conditions, SortFields, Limit, Offset, State) -> end, case execute(StatementName, ExecArgs, State) of - #result_packet{rows = Rows, field_list = Fields} -> - Docs = lists:foldl( - fun(Row, DocList) -> - NewDoc = lists:foldl( - fun(Field, [Doc,N]) -> - FieldRecord = lists:nth(N, Fields), - FieldName = list_to_atom(binary_to_list(FieldRecord#field.name)), - [sumo_internal:set_field(FieldName, Field, Doc), N+1] - end, - [sumo_internal:new_doc(DocName), 1], - Row - ), - [hd(NewDoc)|DocList] - end, - [], - Rows - ), - {ok, lists:reverse(Docs), State}; - Error -> evaluate_execute_result(Error, State) + #result_packet{} = Result -> + {ok, build_docs(DocName, Result), State}; + Error -> + evaluate_execute_result(Error, State) end. %% XXX: Refactor: @@ -413,6 +399,52 @@ prepare(DocName, PreName, Fun) when is_atom(PreName), is_function(Fun) -> end, Name. +%% @doc Call prepare/3 first, to get a well formed statement name. +-spec just_execute(atom() | list(), state()) -> + {ok, {raw, ok}, state()} | {error, binary(), state()}. +just_execute(Query, State) -> + case execute(Query, State) of + #ok_packet{} -> {ok, {raw, ok}, State}; + Error -> evaluate_execute_result(Error, State) + end. + +-spec just_execute(atom(), list(), state()) -> + {ok, {raw, ok}, state()} | {error, binary(), state()}. +just_execute(Name, Args, State) -> + case execute(Name, Args, State) of + #ok_packet{} -> {ok, {raw, ok}, State}; + Error -> evaluate_execute_result(Error, State) + end. + +%% @doc Call prepare/3 first, to get a well formed statement name. +-spec get_docs(atom(), atom() | list(), state()) -> + {ok, {docs, [sumo_internal:doc()]}, state()} | {error, binary(), state()}. +get_docs(DocName, Query, State) -> + case execute(Query, State) of + #result_packet{} = Result -> + {ok, {docs, build_docs(DocName, Result)}, State}; + Error -> + evaluate_execute_result(Error, State) + end. + +-spec get_docs(atom(), atom(), list(), state()) -> + {ok, {docs, [sumo_internal:doc()]}, state()} | {error, binary(), state()}. +get_docs(DocName, Name, Args, State) -> + case execute(Name, Args, State) of + #result_packet{} = Result -> + {ok, {docs, build_docs(DocName, Result)}, State}; + Error -> + evaluate_execute_result(Error, State) + end. + +build_docs(DocName, #result_packet{rows = Rows, field_list = Fields}) -> + FieldNames = [binary_to_atom(Field#field.name, utf8) || Field <- Fields], + [build_doc(sumo_internal:new_doc(DocName), FieldNames, Row) || Row <- Rows]. + +build_doc(Doc, [], []) -> Doc; +build_doc(Doc, [FieldName|FieldNames], [Value|Values]) -> + build_doc(sumo_internal:set_field(FieldName, Value, Doc), FieldNames, Values). + %% @doc Call prepare/3 first, to get a well formed statement name. -spec execute(atom(), list(), state()) -> term(). execute(Name, Args, #state{pool=Pool}) when is_atom(Name), is_list(Args) ->