From 2d95d287ea1d1596d8e09d69d6065ff896819035 Mon Sep 17 00:00:00 2001 From: Brujo Benavides Date: Mon, 29 Dec 2014 16:25:38 -0300 Subject: [PATCH 1/6] ignoring [aw](http://github.com/inaka/assisted_workflow) files --- .gitignore | 3 ++- src/sumo_store.erl | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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/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). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% From 37a27dbfd454ba0ea0b0fdc33353bd853f9e3d12 Mon Sep 17 00:00:00 2001 From: Brujo Benavides Date: Mon, 29 Dec 2014 16:30:54 -0300 Subject: [PATCH 2/6] Use inaka's mixer --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 36522c34b4ae73a7639e64842cc545d81cfe6199 Mon Sep 17 00:00:00 2001 From: Brujo Benavides Date: Mon, 29 Dec 2014 16:39:25 -0300 Subject: [PATCH 3/6] TODO: elasticsearch:find_by/6 --- src/sumo_store_elasticsearch.erl | 11 +++++++++++ 1 file changed, 11 insertions(+) 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) -> From 50fb556b97d1022e76138a4e030f40a06bb50a51 Mon Sep 17 00:00:00 2001 From: Brujo Benavides Date: Mon, 29 Dec 2014 19:54:50 -0300 Subject: [PATCH 4/6] New functions to avoid dealing with emysql records --- src/sumo_store_mysql.erl | 81 ++++++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 19 deletions(-) diff --git a/src/sumo_store_mysql.erl b/src/sumo_store_mysql.erl index 8220266..be0b2df 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,63 @@ 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}) -> + 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 + ), + lists:reverse(Docs). + %% @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) -> From 7d50ddc08dfc2b16601758aa49d3f42591466023 Mon Sep 17 00:00:00 2001 From: Brujo Benavides Date: Mon, 29 Dec 2014 19:55:42 -0300 Subject: [PATCH 5/6] Missing space --- src/sumo_store_mysql.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sumo_store_mysql.erl b/src/sumo_store_mysql.erl index be0b2df..b217266 100644 --- a/src/sumo_store_mysql.erl +++ b/src/sumo_store_mysql.erl @@ -441,7 +441,7 @@ build_docs(DocName, #result_packet{rows = Rows, field_list = Fields}) -> Docs = lists:foldl( fun(Row, DocList) -> NewDoc = lists:foldl( - fun(Field, [Doc,N]) -> + 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] From 7be80e96e54ecf8b40b1a4799f13669c7a036f06 Mon Sep 17 00:00:00 2001 From: Brujo Benavides Date: Tue, 30 Dec 2014 08:48:35 -0300 Subject: [PATCH 6/6] Improved thanks to @HernanRivasAcosta --- src/sumo_store_mysql.erl | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/sumo_store_mysql.erl b/src/sumo_store_mysql.erl index b217266..27b44e2 100644 --- a/src/sumo_store_mysql.erl +++ b/src/sumo_store_mysql.erl @@ -438,23 +438,12 @@ get_docs(DocName, Name, Args, State) -> end. build_docs(DocName, #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 - ), - lists:reverse(Docs). + 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().