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