Skip to content

Commit

Permalink
rocked
Browse files Browse the repository at this point in the history
  • Loading branch information
elbrujohalcon committed Jan 5, 2015
1 parent d4ddf37 commit f096860
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 79 deletions.
47 changes: 24 additions & 23 deletions elvis.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,47 @@
{config,
[#{dirs => ["src", "test"],
filter => "*.erl",
rules => [{elvis_style, line_length, [80]},
{elvis_style, no_tabs, []},
{elvis_style, macro_names, []},
{elvis_style, macro_module_names, []},
{elvis_style, operator_spaces, [{right, ","},
{right, "++"},
{left, "++"}]},
{elvis_style, nesting_level, [3]},
{elvis_style, god_modules, [25]},
{elvis_style, no_if_expression, []},
rules => [{elvis_style, line_length, #{limit => 80}},
{elvis_style, no_tabs},
{elvis_style, macro_names},
{elvis_style, macro_module_names},
{elvis_style, operator_spaces, #{rules => [{right, ","},
{right, "++"},
{left, "++"}]}},
{elvis_style, nesting_level, #{level => 3}},
{elvis_style, god_modules, #{limit => 25}},
{elvis_style, no_if_expression},
{elvis_style,
invalid_dynamic_call,
[conditional_logic_SUITE,
sumo_find_SUITE,
sumo_basic_SUITE,
sumo,
sumo_internal]
#{ignore =>
[conditional_logic_SUITE,
sumo_find_SUITE,
sumo_basic_SUITE,
sumo,
sumo_internal]}
},
{elvis_style, used_ignored_variable, []},
{elvis_style, no_behavior_info, []},
{elvis_style, used_ignored_variable},
{elvis_style, no_behavior_info},
{
elvis_style,
module_naming_convention,
["^([a-z][a-z0-9]*_?)*(_SUITE)?$", []]
#{regex => "^([a-z][a-z0-9]*_?)*(_SUITE)?$", ignore => []}
},
{elvis_style, state_record_and_type, []},
{elvis_style, no_spec_with_records, []}
{elvis_style, state_record_and_type},
{elvis_style, no_spec_with_records}
]
},
#{dirs => ["."],
filter => "Makefile",
rules => [{elvis_project, no_deps_master_erlang_mk, []}]
rules => [{elvis_project, no_deps_master_erlang_mk, #{ignore => []}}]
},
#{dirs => ["."],
filter => "rebar.config",
rules => [{elvis_project, no_deps_master_rebar, []}]
rules => [{elvis_project, no_deps_master_rebar, #{ignore => []}}]
},
#{dirs => ["."],
filter => "elvis.config",
rules => [{elvis_project, old_configuration_format, []}]
rules => [{elvis_project, old_configuration_format}]
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion src/sumo_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Code starts here.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec start(term(), term()) -> ok.
-spec start(term(), term()) -> ignore | {error, term()} | {ok, pid()}.
start(_StartType, _StartArgs) ->
sumo_sup:start_link().

Expand Down
12 changes: 6 additions & 6 deletions src/sumo_backend_mongo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ get_pool(Name) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% gen_server stuff.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec init([term()]) -> {ok, #state{}}.
-spec init([term()]) -> {ok, state()}.
init(Options) ->
PoolSize = proplists:get_value(poolsize, Options),
Pool = erlang:ref_to_list(make_ref()),
Expand All @@ -87,22 +87,22 @@ init(Options) ->
),
{ok, #state{pool=Pool}}.

-spec handle_call(term(), term(), state()) -> {reply, term(), #state{}}.
-spec handle_call(term(), term(), state()) -> {reply, term(), state()}.
handle_call(get_pool, _From, State = #state{pool=Pool}) ->
{reply, Pool, State}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Unused Callbacks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-spec handle_cast(term(), #state{}) -> {noreply, #state{}}.
-spec handle_cast(term(), state()) -> {noreply, state()}.
handle_cast(_Msg, State) -> {noreply, State}.

-spec handle_info(term(), #state{}) -> {noreply, #state{}}.
-spec handle_info(term(), state()) -> {noreply, state()}.
handle_info(_Msg, State) -> {noreply, State}.

-spec terminate(term(), #state{}) -> ok.
-spec terminate(term(), state()) -> ok.
terminate(_Reason, _State) -> ok.

-spec code_change(term(), #state{}, term()) -> {ok, #state{}}.
-spec code_change(term(), state(), term()) -> {ok, state()}.
code_change(_OldVsn, State, _Extra) -> {ok, State}.
12 changes: 6 additions & 6 deletions src/sumo_backend_mysql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ get_pool(Name) ->
%% gen_server stuff.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-spec init([term()]) -> {ok, #state{}}.
-spec init([term()]) -> {ok, state()}.
init(Options) ->
PoolSize = proplists:get_value(poolsize, Options),
Pool = list_to_atom(erlang:ref_to_list(make_ref())),
Expand All @@ -88,22 +88,22 @@ init(Options) ->
),
{ok, #state{pool=Pool}}.

-spec handle_call(term(), term(), state()) -> {reply, term(), #state{}}.
-spec handle_call(term(), term(), state()) -> {reply, term(), state()}.
handle_call(get_pool, _From, State = #state{pool=Pool}) ->
{reply, Pool, State}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Unused Callbacks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-spec handle_cast(term(), #state{}) -> {noreply, #state{}}.
-spec handle_cast(term(), state()) -> {noreply, state()}.
handle_cast(_Msg, State) -> {noreply, State}.

-spec handle_info(term(), #state{}) -> {noreply, #state{}}.
-spec handle_info(term(), state()) -> {noreply, state()}.
handle_info(_Msg, State) -> {noreply, State}.

-spec terminate(term(), #state{}) -> ok.
-spec terminate(term(), state()) -> ok.
terminate(_Reason, _State) -> ok.

-spec code_change(term(), #state{}, term()) -> {ok, #state{}}.
-spec code_change(term(), state(), term()) -> {ok, state()}.
code_change(_OldVsn, State, _Extra) -> {ok, State}.
3 changes: 2 additions & 1 deletion src/sumo_internal.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
%%% @doc Main **internal** module for sumo. Use this one from your own applications.
%%% @doc Main **internal** module for sumo.
%%% Use this one from your own applications.
%%%
%%% Copyright 2012 Inaka <[email protected]>
%%%
Expand Down
51 changes: 34 additions & 17 deletions src/sumo_sql_builder.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,37 @@
%%% Public API.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% @doc Returns number of results, useful for pagination.
-spec s_count(string(), [field()], condition(), string()) -> {iolist(), [term()]}.
-spec s_count(
string(), [field()], condition(), string()
) -> {iolist(), [term()]}.
s_count(TableName, SelectFields, Conditions, ExtraWhere) ->
{_Select, Where, WValues} = form_select_query(SelectFields, Conditions, ExtraWhere),
{_Select, Where, WValues} =
form_select_query(SelectFields, Conditions, ExtraWhere),
{
["SELECT COUNT(1) AS `count` FROM ", escape(TableName), " WHERE ", Where],
WValues
}.

%% @doc Generic select function.
-spec s(
string(), [field()], condition(), string(), non_neg_integer(), non_neg_integer(), string()
string(), [field()], condition(), string(), non_neg_integer(),
non_neg_integer(), string()
) -> {iolist(), [term()]}.
s(TableName, SelectFields, Conditions, ExtraWhere, Page, PageSize, OrderBy) ->
Paging = [" LIMIT ", integer_to_list((Page-1) * PageSize), ",", integer_to_list(PageSize)],
{Select, Where, WValues} = form_select_query(SelectFields, Conditions, ExtraWhere),
Paging =
[ " LIMIT ", integer_to_list((Page-1) * PageSize), ", ",
integer_to_list(PageSize)],
{Select, Where, WValues} =
form_select_query(SelectFields, Conditions, ExtraWhere),
{
["SELECT ", Select, " FROM ", escape(TableName), " WHERE ", Where, " ", OrderBy, " ", Paging],
["SELECT ", Select,
" FROM ", escape(TableName),
" WHERE ", Where, " ", OrderBy, " ", Paging],
WValues
}.

%% @doc INSERT.
-spec i(string(), sumo:doc()) -> {iolist(), [term()]}.
-spec i(atom() | string(), sumo:doc()) -> {iolist(), [term()]}.
i(TableName, Proplist) ->
{Fields, Values, Args} = lists:foldr(
fun({K, V}, {Fs, Vs, Args}) ->
Expand All @@ -83,15 +92,15 @@ i(TableName, Proplist) ->
),
{
[
"INSERT INTO ", escape(TableName), " (", string:join(Fields, ","), ") ",
"VALUES (", string:join(Args, ","),")"
"INSERT INTO ", escape(TableName), " (", string:join(Fields, ", "), ") ",
"VALUES (", string:join(Args, ", "), ")"
],
Values
}.

%% @doc UPDATE.
-spec u(
string(), sumo:doc(), condition()
atom() | string(), sumo:doc(), condition()
) -> {iolist(), [term()], [term()]}.
u(TableName, UpdateFields, Conditions) ->
{_Select, Where, WValues} = form_select_query([], Conditions, ""),
Expand All @@ -103,7 +112,10 @@ u(TableName, UpdateFields, Conditions) ->
UpdateFields
),
Update = string:join(UFields, ","),
{["UPDATE ", escape(TableName), " SET ", Update, " WHERE ", Where], UValues, WValues}.
{["UPDATE ", escape(TableName), " SET ", Update, " WHERE ", Where],
UValues,
WValues
}.

%% @doc DELETE.
-spec d(string(), condition()) -> {iolist(), [term()]}.
Expand All @@ -125,14 +137,16 @@ escape(Field) when is_atom(Field) ->
escape(Field) when is_list(Field) ->
lists:flatten(["`", Field, "`"]).

-spec form_select_query([field()], condition(), string()) -> {string(), string(), [string()]}.
-spec form_select_query([field()], condition(), string()) ->
{string(), string(), [string()]}.
form_select_query(SelectFields, Conditions, ExtraWhere) ->
{Values, CleanConditions} = values_conditions(Conditions),
WhereTmp = where_clause(CleanConditions),
SFields = [escape(F) || F <- SelectFields],
Where = case ExtraWhere of
[] -> WhereTmp;
ExtraWhere -> [WhereTmp, case WhereTmp of [] -> " "; _ -> " AND " end, ExtraWhere]
ExtraWhere ->
[WhereTmp, case WhereTmp of [] -> " "; _ -> " AND " end, ExtraWhere]
end,
Select = string:join(SFields, ","),
% SelectedFields, Where clause, and Where values
Expand All @@ -150,8 +164,11 @@ values_conditions({LogicalOp, Exprs}, {Values, CleanExprs, Count})
when (LogicalOp == 'and')
or (LogicalOp == 'or')
or (LogicalOp == 'not') ->
{NewValues, NewCleanExprs, NewCount} = values_conditions(Exprs, {Values, [], Count}),
{NewValues, [{LogicalOp, lists:reverse(NewCleanExprs)} | CleanExprs], NewCount};
{NewValues, NewCleanExprs, NewCount} =
values_conditions(Exprs, {Values, [], Count}),
{NewValues,
[{LogicalOp, lists:reverse(NewCleanExprs)} | CleanExprs],
NewCount};
values_conditions({Name, Op, Value}, {Values, CleanExprs, Count})
when not is_atom(Value) ->
sumo_internal:check_operator(Op),
Expand Down Expand Up @@ -188,12 +205,12 @@ where_clause(Exprs, EscapeFun) ->

-spec where_clause(sumo_internal:expression(), fun(), fun()) -> iodata().
where_clause(Exprs, EscapeFun, SlotFun) when is_list(Exprs) ->
Clauses = lists:map(fun(Expr) -> where_clause(Expr, EscapeFun, SlotFun) end, Exprs),
Clauses = [where_clause(Expr, EscapeFun, SlotFun) || Expr <- Exprs],
["(", interpose(" AND ", Clauses), ")"];
where_clause({'and', Exprs}, EscapeFun, SlotFun) ->
where_clause(Exprs, EscapeFun, SlotFun);
where_clause({'or', Exprs}, EscapeFun, SlotFun) ->
Clauses = lists:map(fun(Expr) -> where_clause(Expr, EscapeFun, SlotFun) end, Exprs),
Clauses = [where_clause(Expr, EscapeFun, SlotFun) || Expr <- Exprs],
["(", interpose(" OR ", Clauses), ")"];
where_clause({'not', Expr}, EscapeFun, SlotFun) ->
[" NOT ", "(", where_clause(Expr, EscapeFun, SlotFun), ")"];
Expand Down
18 changes: 9 additions & 9 deletions src/sumo_store.erl
Original file line number Diff line number Diff line change
Expand Up @@ -207,42 +207,42 @@ init([Module, Options]) ->
-spec handle_call(term(), _, state()) -> {reply, tuple(), state()}.
handle_call(
{persist, Doc}, _From,
#state{handler=Handler, handler_state=HState}=State
#state{handler = Handler, handler_state = HState} = State
) ->
{OkOrError, Reply, NewState} = Handler:persist(Doc, HState),
{reply, {OkOrError, Reply}, State#state{handler_state=NewState}};

handle_call(
{delete, DocName, Id}, _From,
#state{handler=Handler,handler_state=HState}=State
#state{handler = Handler, handler_state = HState} = State
) ->
{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
#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
#state{handler = Handler, handler_state = HState} = State
) ->
{OkOrError, Reply, NewState} = Handler:delete_all(DocName, HState),
{reply, {OkOrError, Reply}, State#state{handler_state=NewState}};

handle_call(
{find_all, DocName}, _From,
#state{handler=Handler, handler_state=HState}=State
#state{handler = Handler, handler_state = HState} = State
) ->
{OkOrError, Reply, NewState} = Handler:find_all(DocName, HState),
{reply, {OkOrError, Reply}, State#state{handler_state = NewState}};

handle_call(
{find_all, DocName, SortFields, Limit, Offset}, _From,
#state{handler=Handler, handler_state=HState}=State
#state{handler = Handler, handler_state = HState} = State
) ->
{OkOrError, Reply, NewState} = Handler:find_all(
DocName, SortFields, Limit, Offset, HState
Expand All @@ -251,14 +251,14 @@ handle_call(

handle_call(
{find_by, DocName, Conditions}, _From,
#state{handler=Handler,handler_state=HState}=State
#state{handler = Handler, handler_state = HState} = State
) ->
{OkOrError, Reply, NewState} = Handler:find_by(DocName, Conditions, HState),
{reply, {OkOrError, Reply}, State#state{handler_state=NewState}};

handle_call(
{find_by, DocName, Conditions, Limit, Offset}, _From,
#state{handler=Handler,handler_state=HState}=State
#state{handler = Handler, handler_state = HState} = State
) ->
{OkOrError, Reply, NewState} = Handler:find_by(
DocName, Conditions, Limit, Offset, HState
Expand All @@ -284,7 +284,7 @@ handle_call(

handle_call(
{create_schema, Schema}, _From,
#state{handler=Handler,handler_state=HState}=State
#state{handler = Handler, handler_state = HState} = State
) ->
{Result, NewState} = case Handler:create_schema(Schema, HState) of
{ok, NewState_} -> {ok, NewState_};
Expand Down
Loading

0 comments on commit f096860

Please sign in to comment.