Replace return type string() with iolist() for SQL-generating functions
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
-export([value_to_sql/1, value_to_sql_unsafe/1, escape/1]).
|
||||
-export([write_value_sql/1, write_col_sql/1]).
|
||||
-export([create_table_sql/2, write_sql/2, read_sql/3, delete_sql/3, drop_table/1]).
|
||||
-export([update_sql/4, update_set_sql/1, replace/1]).
|
||||
-export([update_sql/4, update_set_sql/1]).
|
||||
-export([read_sql/4, read_cols_sql/1]).
|
||||
|
||||
%%====================================================================
|
||||
@@ -61,7 +61,7 @@ col_type_to_atom("REAL") ->
|
||||
%% single quotes (e.g. can be entered by users).
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec value_to_sql_unsafe(sql_value()) -> iodata().
|
||||
-spec value_to_sql_unsafe(sql_value()) -> iolist().
|
||||
value_to_sql_unsafe(X) ->
|
||||
if
|
||||
is_integer(X) -> integer_to_list(X);
|
||||
@@ -90,37 +90,24 @@ value_to_sql(X) ->
|
||||
end.
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec write_value_sql(Value :: [term()]) -> string()
|
||||
%% @spec write_value_sql(Value :: [term()]) -> iolist()
|
||||
%% @doc
|
||||
%% Creates the values portion of the sql stmt.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec write_value_sql(any()) -> string().
|
||||
-spec write_value_sql(any()) -> iolist().
|
||||
write_value_sql(Values) ->
|
||||
map_intersperse(fun value_to_sql/1, Values, ",").
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec write_col_sql([atom()]) -> string()
|
||||
%% @spec write_col_sql([atom()]) -> iolist()
|
||||
%% @doc Creates the column/data stmt for SQL.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec write_col_sql([atom()]) -> string().
|
||||
-spec write_col_sql([atom()]) -> iolist().
|
||||
write_col_sql(Cols) ->
|
||||
map_intersperse(fun atom_to_list/1, Cols, ",").
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec replace(String) -> string()
|
||||
%% String = string()
|
||||
%% @doc Returns copy of String for which \" replaced with '
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec replace(string()) -> string().
|
||||
replace([]) -> [];
|
||||
replace([$" | T]) ->
|
||||
[$' | replace(T)];
|
||||
replace([H | T]) ->
|
||||
[H | replace(T)].
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec escape(IoData :: iodata()) -> iodata()
|
||||
%%
|
||||
@@ -131,7 +118,7 @@ replace([H | T]) ->
|
||||
escape(IoData) -> re:replace(IoData, "'", "''", [global]).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec update_set_sql([{Col, Value}]) -> string()
|
||||
%% @spec update_set_sql([{Col, Value}]) -> iolist()
|
||||
%% Col = atom()
|
||||
%% Value = number() | atom() | string()
|
||||
%% @doc
|
||||
@@ -140,7 +127,7 @@ escape(IoData) -> re:replace(IoData, "'", "''", [global]).
|
||||
%% For strings \" replaced with '.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec update_set_sql(any()) -> string().
|
||||
-spec update_set_sql(any()) -> iolist().
|
||||
update_set_sql(Data) ->
|
||||
ColValueToSqlFun =
|
||||
fun({Col, Value}) ->
|
||||
@@ -149,25 +136,25 @@ update_set_sql(Data) ->
|
||||
map_intersperse(ColValueToSqlFun, Data, ", ").
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec read_cols_sql(Columns::[atom()]) -> string()
|
||||
%% @spec read_cols_sql(Columns::[atom()]) -> iolist()
|
||||
%% @doc
|
||||
%% Creates list of columns for select stmt.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec read_cols_sql([atom()]) -> string().
|
||||
-spec read_cols_sql([atom()]) -> iolist().
|
||||
read_cols_sql(Columns) ->
|
||||
map_intersperse(fun atom_to_list/1, Columns, ", ").
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec create_table_sql(Tbl, [{ColName, Type}]) -> string()
|
||||
%% @spec create_table_sql(Tbl, [{ColName, Type}]) -> iolist()
|
||||
%% Tbl = atom()
|
||||
%% ColName = atom()
|
||||
%% Type = string()
|
||||
%% Type = atom()
|
||||
%% @doc Generates a table create stmt in SQL.
|
||||
%% First column listed is considered the primary key.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec create_table_sql(atom(), [{atom(), string()}]) -> string().
|
||||
-spec create_table_sql(atom(), [{atom(), atom()}]) -> iolist().
|
||||
create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
|
||||
CT = ["CREATE TABLE ", atom_to_list(Tbl), " "],
|
||||
Start = ["(", atom_to_list(ColName), " ", col_type_to_string(Type), " PRIMARY KEY, "],
|
||||
@@ -178,7 +165,7 @@ create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
|
||||
[CT, Start, End].
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec update_sql(Tbl, Key, Value, Data) -> string()
|
||||
%% @spec update_sql(Tbl, Key, Value, Data) -> iolist()
|
||||
%% Tbl = atom()
|
||||
%% Key = atom()
|
||||
%% Value = atom()
|
||||
@@ -189,27 +176,27 @@ create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
|
||||
%% record with matching Value.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec update_sql(atom(), atom(), atom(), [{atom(), sql_value()}]) -> string().
|
||||
-spec update_sql(atom(), atom(), atom(), [{atom(), sql_value()}]) -> iolist().
|
||||
update_sql(Tbl, Key, Value, Data) ->
|
||||
["UPDATE ", atom_to_list(Tbl), " SET ", update_set_sql(Data),
|
||||
" WHERE ", atom_to_list(Key), " = ", value_to_sql(Value), ";"].
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec write_sql(Tbl, Data) -> string()
|
||||
%% @spec write_sql(Tbl, Data) -> iolist()
|
||||
%% Tbl = atom()
|
||||
%% Data = [{ColName :: atom(), Values :: string() | integer() | float()}]
|
||||
%% Data = [{ColName :: atom(), Value :: string() | integer() | float()}]
|
||||
%% @doc Taking Data as list of column names and values pairs it creates the
|
||||
%% proper insertion SQL stmt.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec write_sql(atom(), [{atom(), sql_value()}]) -> string().
|
||||
-spec write_sql(atom(), [{atom(), sql_value()}]) -> iolist().
|
||||
write_sql(Tbl, Data) ->
|
||||
{Cols, Values} = lists:unzip(Data),
|
||||
["INSERT INTO ", atom_to_list(Tbl), " (", sqlite3_lib:write_col_sql(Cols),
|
||||
") values (", sqlite3_lib:write_value_sql(Values), ");"].
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec read_sql(Tbl, Key, Value) -> string()
|
||||
%% @spec read_sql(Tbl, Key, Value) -> iolist()
|
||||
%% Tbl = atom()
|
||||
%% Key = atom()
|
||||
%% Value = string() | integer() | float()
|
||||
@@ -217,13 +204,13 @@ write_sql(Tbl, Data) ->
|
||||
%% matching Value.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec read_sql(atom(), atom(), sql_value()) -> string().
|
||||
-spec read_sql(atom(), atom(), sql_value()) -> iolist().
|
||||
read_sql(Tbl, Key, Value) ->
|
||||
["SELECT * FROM ", atom_to_list(Tbl), " WHERE ", atom_to_list(Key),
|
||||
" = ", value_to_sql(Value), ";"].
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec read_sql(Tbl, Key, Value, Columns) -> string()
|
||||
%% @spec read_sql(Tbl, Key, Value, Columns) -> iolist()
|
||||
%% Tbl = atom()
|
||||
%% Key = atom()
|
||||
%% Value = string() | integer() | float()
|
||||
@@ -233,14 +220,14 @@ read_sql(Tbl, Key, Value) ->
|
||||
%% matching Value and returns only specified columns Columns.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec read_sql(atom(), atom(), sql_value(), [atom()]) -> string().
|
||||
-spec read_sql(atom(), atom(), sql_value(), [atom()]) -> iolist().
|
||||
read_sql(Tbl, Key, Value, Columns) ->
|
||||
["SELECT ", sqlite3_lib:read_cols_sql(Columns), " FROM ",
|
||||
atom_to_list(Tbl), " WHERE ", atom_to_list(Key), " = ",
|
||||
value_to_sql(Value), ";"].
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec delete_sql(Tbl, Key, Value) -> string()
|
||||
%% @spec delete_sql(Tbl, Key, Value) -> iolist()
|
||||
%% Tbl = atom()
|
||||
%% Key = atom()
|
||||
%% Value = string() | integer() | float()
|
||||
@@ -248,18 +235,18 @@ read_sql(Tbl, Key, Value, Columns) ->
|
||||
%% matching Value then deletes that record.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec delete_sql(atom(), atom(), sql_value()) -> string().
|
||||
-spec delete_sql(atom(), atom(), sql_value()) -> iolist().
|
||||
delete_sql(Tbl, Key, Value) ->
|
||||
["DELETE FROM ", atom_to_list(Tbl), " WHERE ", atom_to_list(Key),
|
||||
" = ", value_to_sql(Value), ";"].
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec drop_table(Tbl) -> string()
|
||||
%% @spec drop_table(Tbl) -> iolist()
|
||||
%% Tbl = atom()
|
||||
%% @doc Drop the table Tbl from the database
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec drop_table(atom()) -> string().
|
||||
-spec drop_table(atom()) -> iolist().
|
||||
drop_table(Tbl) ->
|
||||
["DROP TABLE ", atom_to_list(Tbl), ";"].
|
||||
|
||||
|
||||
Reference in New Issue
Block a user