Skip to content

Commit

Permalink
Fixes #111: Dialyzed
Browse files Browse the repository at this point in the history
  • Loading branch information
elbrujohalcon committed Jan 5, 2015
1 parent 3f36a50 commit d5199f7
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 43 deletions.
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()) -> ignore | {error, term()} | {ok, pid()}.
-spec start(term(), term()) -> {error, term()} | {ok, pid()}.
start(_StartType, _StartArgs) ->
sumo_sup:start_link().

Expand Down
2 changes: 1 addition & 1 deletion src/sumo_backend_elasticsearch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
start_link(Name, Options) ->
gen_server:start_link({local, Name}, ?MODULE, Options, []).

-spec get_index(atom() | pid()) -> atom().
-spec get_index(atom() | pid()) -> atom() | string() | binary().
get_index(Name) ->
gen_server:call(Name, get_index).

Expand Down
10 changes: 6 additions & 4 deletions src/sumo_sql_builder.erl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ s(TableName, SelectFields, Conditions, ExtraWhere, Page, PageSize, OrderBy) ->
}.

%% @doc INSERT.
-spec i(atom() | string(), sumo:doc()) -> {iolist(), [term()]}.
-spec i(atom() | string(), proplists:proplist()) -> {iodata(), [term()]}.
i(TableName, Proplist) ->
{Fields, Values, Args} = lists:foldr(
fun({K, V}, {Fs, Vs, Args}) ->
Expand All @@ -100,8 +100,8 @@ i(TableName, Proplist) ->

%% @doc UPDATE.
-spec u(
atom() | string(), sumo:doc(), condition()
) -> {iolist(), [term()], [term()]}.
atom() | string(), proplists:proplist(), condition()
) -> {iodata(), [term()], [term()]}.
u(TableName, UpdateFields, Conditions) ->
{_Select, Where, WValues} = form_select_query([], Conditions, ""),
{UFields, UValues} = lists:foldr(
Expand Down Expand Up @@ -204,6 +204,8 @@ where_clause(Exprs, EscapeFun) ->
where_clause(Exprs, EscapeFun, fun slot_question/1).

-spec where_clause(sumo_internal:expression(), fun(), fun()) -> iodata().
where_clause([], _EscapeFun, _SlotFun) ->
[];
where_clause(Exprs, EscapeFun, SlotFun) when is_list(Exprs) ->
Clauses = [where_clause(Expr, EscapeFun, SlotFun) || Expr <- Exprs],
["(", interpose(" AND ", Clauses), ")"];
Expand All @@ -228,7 +230,7 @@ where_clause({Name, 'not_null'}, EscapeFun, _SlotFun) ->
-spec slot_question({'?', integer()}) -> string().
slot_question(_) -> " ? ".

-spec slot_numbered({'?', integer()}) -> string().
-spec slot_numbered({'?', integer()}) -> iodata().
slot_numbered({_, N}) -> [" $", integer_to_list(N), " "].

-spec interpose(term(), list()) -> list().
Expand Down
3 changes: 2 additions & 1 deletion src/sumo_store_elasticsearch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ init(Options) ->

{ok, #{index => Index, pool_name => PoolName}}.

-spec persist(sumo:doc(), state()) -> sumo:user_doc().
-spec persist(sumo_internal:doc(), state()) ->
sumo_store:result(sumo_internal:doc(), state()).
persist(Doc, #{index := Index, pool_name := PoolName} = State) ->
DocName = sumo_internal:doc_name(Doc),
Type = atom_to_binary(DocName, utf8),
Expand Down
4 changes: 2 additions & 2 deletions src/sumo_store_mongo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,16 @@ create_schema(Schema, #state{pool=Pool} = State) ->
lists:foreach(
fun(Field) ->
create_field(Field),
Name = sumo_internal:field_name(Field),
lager:debug("creating index: ~p for ~p", [Name, SchemaName]),
ok = emongo:ensure_index(
Pool, atom_to_list(SchemaName), [{atom_to_list(Name), 1}]
Pool, atom_to_list(SchemaName), [{atom_to_list(Name), 1}])
end,
Fields
),
{ok, State}.

create_field(Field) ->
Name = sumo_internal:field_name(Field),
Attrs = sumo_internal:field_attrs(Field),
lists:foldl(
fun(Attr, Acc) ->
Expand Down
34 changes: 17 additions & 17 deletions src/sumo_store_mysql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ persist(Doc, State) ->
[ColumnDqls, ColumnSqls] =
lists:foldl(
fun(Name, [Dqls, Sqls]) ->
Dql = [escape(atom_to_list(Name))],
Dql = [escape(Name)],
Sql = "?",
[[Dql | Dqls], [Sql | Sqls]]
end,
Expand All @@ -99,14 +99,14 @@ persist(Doc, State) ->
NPColumnDqls =
lists:foldl(
fun(Name, Dqls) ->
Dql = [escape(atom_to_list(Name))],
Dql = [escape(Name)],
[Dql | Dqls]
end,
[],
NPFieldNames
),

TableName = escape(atom_to_list(DocName)),
TableName = escape(DocName),
ColumnsText = string:join(ColumnDqls, ","),
InsertValueSlots = string:join(ColumnSqls, ","),
OnDuplicateColumns = [[ColumnName, "=?"] || ColumnName <- NPColumnDqls],
Expand Down Expand Up @@ -149,8 +149,8 @@ persist(Doc, State) ->
sumo_store:result(sumo_store:affected_rows(), state()).
delete(DocName, Id, State) ->
StatementName = prepare(DocName, delete, fun() -> [
"DELETE FROM ", escape(atom_to_list(DocName)),
" WHERE ", escape(atom_to_list(sumo_internal:id_field_name(DocName))),
"DELETE FROM ", escape(DocName),
" WHERE ", escape(sumo_internal:id_field_name(DocName)),
"=? LIMIT 1"
] end),
case execute(StatementName, [Id], State) of
Expand All @@ -169,7 +169,7 @@ delete_by(DocName, Conditions, State) ->
StatementFun =
fun() ->
[ "DELETE FROM ",
escape(atom_to_list(DocName)),
escape(DocName),
" WHERE ",
lists:flatten(Clauses)
]
Expand All @@ -185,7 +185,7 @@ delete_by(DocName, Conditions, State) ->
sumo_store:result(sumo_store:affected_rows(), state()).
delete_all(DocName, State) ->
StatementName = prepare(DocName, delete_all, fun() ->
["DELETE FROM ", escape(atom_to_list(DocName))]
["DELETE FROM ", escape(DocName)]
end),
case execute(StatementName, State) of
#ok_packet{affected_rows = NumRows} -> {ok, NumRows, State};
Expand Down Expand Up @@ -263,7 +263,7 @@ find_by(DocName, Conditions, SortFields, Limit, Offset, State) ->
Fun = fun() ->
% Select * is not good..
Sql1 = [ "SELECT * FROM ",
escape(atom_to_list(DocName)),
escape(DocName),
WhereClause,
OrderByClause
],
Expand Down Expand Up @@ -306,7 +306,7 @@ create_schema(Schema, State) ->
lists:map(fun create_index/1, Fields)
),
Dql = [
"CREATE TABLE IF NOT EXISTS ", escape(atom_to_list(Name)), " (",
"CREATE TABLE IF NOT EXISTS ", escape(Name), " (",
string:join(FieldsDql, ", "), ", ", string:join(Indexes, ", "),
") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8"
],
Expand All @@ -322,25 +322,25 @@ create_column(Field) ->
sumo_internal:field_attrs(Field)).

create_column(Name, integer, Attrs) ->
[escape(atom_to_list(Name)), " INT(11) ", create_column_options(Attrs)];
[escape(Name), " INT(11) ", create_column_options(Attrs)];

create_column(Name, float, Attrs) ->
[escape(atom_to_list(Name)), " FLOAT ", create_column_options(Attrs)];
[escape(Name), " FLOAT ", create_column_options(Attrs)];

create_column(Name, text, Attrs) ->
[escape(atom_to_list(Name)), " TEXT ", create_column_options(Attrs)];
[escape(Name), " TEXT ", create_column_options(Attrs)];

create_column(Name, binary, Attrs) ->
[escape(atom_to_list(Name)), " BLOB ", create_column_options(Attrs)];
[escape(Name), " BLOB ", create_column_options(Attrs)];

create_column(Name, string, Attrs) ->
[escape(atom_to_list(Name)), " VARCHAR ", create_column_options(Attrs)];
[escape(Name), " VARCHAR ", create_column_options(Attrs)];

create_column(Name, date, Attrs) ->
[escape(atom_to_list(Name)), " DATE ", create_column_options(Attrs)];
[escape(Name), " DATE ", create_column_options(Attrs)];

create_column(Name, datetime, Attrs) ->
[escape(atom_to_list(Name)), " DATETIME ", create_column_options(Attrs)].
[escape(Name), " DATETIME ", create_column_options(Attrs)].

create_column_options(Attrs) ->
lists:filter(fun(T) -> is_list(T) end, lists:map(
Expand Down Expand Up @@ -373,7 +373,7 @@ create_index(Field) ->
)).

create_index(Name, id) ->
["PRIMARY KEY(", escape(atom_to_list(Name)), ")"];
["PRIMARY KEY(", escape(Name), ")"];

create_index(Name, unique) ->
List = atom_to_list(Name),
Expand Down
14 changes: 9 additions & 5 deletions src/sumo_store_pgsql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ persist(Doc, #{conn := Conn} = State) ->
end,

ProcessedValues = lists:map(ToNullFun, Values),
case pgsql:equery(Conn, Sql, ProcessedValues) of
case pgsql:equery(Conn, stringify(Sql), ProcessedValues) of
{ok, _Count, _Columns, Rows} ->
{LastId} = hd(Rows),
NewDoc = sumo_internal:set_field(IdField, LastId, Doc),
Expand All @@ -135,7 +135,7 @@ delete(DocName, Id, #{conn := Conn} = State) ->
" WHERE ", escape(atom_to_list(sumo_internal:id_field_name(DocName))),
"= $1 LIMIT 1"] ,

case pgsql:equery(Conn, Sql, [Id]) of
case pgsql:equery(Conn, stringify(Sql), [Id]) of
{ok, Count} ->
{ok, Count > 0, State};
{error, Error} ->
Expand All @@ -157,7 +157,7 @@ delete_by(DocName, Conditions, #{conn := Conn} = State) ->
lists:flatten(Clauses)
],

case pgsql:equery(Conn, Sql, Values) of
case pgsql:equery(Conn, stringify(Sql), Values) of
{ok, Count} ->
{ok, Count, State};
{error, Error} ->
Expand All @@ -169,7 +169,7 @@ delete_by(DocName, Conditions, #{conn := Conn} = State) ->
delete_all(DocName, #{conn := Conn} = State) ->
Sql = ["DELETE FROM ", escape(DocName)],

case pgsql:equery(Conn, Sql, []) of
case pgsql:equery(Conn, stringify(Sql), []) of
{ok, Count} ->
{ok, Count, State};
{error, Error} ->
Expand Down Expand Up @@ -257,7 +257,7 @@ find_by(DocName, Conditions, SortFields, Limit, Offset, State) ->
Limit -> Values ++ [Limit, Offset]
end,

case pgsql:equery(Conn, Sql2, AllValues) of
case pgsql:equery(Conn, stringify(Sql2), AllValues) of
{ok, Columns, Rows} ->
ColFun = fun(Col) -> binary_to_atom(Col#column.name, utf8) end,
ColumnNames = lists:map(ColFun, Columns),
Expand Down Expand Up @@ -381,3 +381,7 @@ create_index(Name, index) ->
["KEY ", escape(List), " (", escape(List), ")"];
create_index(_, _) ->
none.

%% @todo remove this once pgsql specs are fixed to support iodata and make
%% dialyzer happy
stringify(Sql) -> binary_to_list(iolist_to_binary(Sql)).
20 changes: 8 additions & 12 deletions src/sumo_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
{{supervisor:strategy(), non_neg_integer(), non_neg_integer()},
[supervisor:child_spec()]
}
}
| ignore.
}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Exports.
Expand All @@ -41,23 +40,20 @@
%% Supervisor callbacks
-export([init/1]).

%% Helper macro for declaring children of supervisor
-define(CLD(I), {I, {I, start_link, []}, permanent, 5000, worker, [I]}).
-define(SUP(I), {I, {I, start_link, []}, permanent, infinity, supervisor, [I]}).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Code starts here.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-spec start_link() -> supervisor:startlink_ret().
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
-spec start_link() -> {ok, pid()} | {error, term()}.
start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []).

-spec init(term()) -> init_result().
-spec init([]) -> init_result().
init([]) ->
{ok, {
{one_for_one, 5, 10},
[ ?SUP(sumo_backend_sup)
, ?SUP(sumo_store_sup)
[ sup(sumo_backend_sup)
, sup(sumo_store_sup)
]
}}.

sup(I) -> {I, {I, start_link, []}, permanent, infinity, supervisor, [I]}.

0 comments on commit d5199f7

Please sign in to comment.