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

Don't require the import of emysql records on stores based on sumo_store_msyql #106

Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.awconfig
ebin
deps
doc
Expand All @@ -9,4 +10,4 @@ examples/blog/log
.rebar
log*/
erl_crash.dump
.erlang.mk.*
.erlang.mk.*
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions src/sumo_store.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down
11 changes: 11 additions & 0 deletions src/sumo_store_elasticsearch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
persist/2,
find_by/3,
find_by/5,
find_by/6,
find_all/2,
find_all/5,
delete/3,
Expand Down Expand Up @@ -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) ->
Expand Down
70 changes: 51 additions & 19 deletions src/sumo_store_mysql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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) ->
Expand Down