Add update operation
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
-export([col_type/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]).
|
||||
|
||||
%%====================================================================
|
||||
%% API
|
||||
@@ -19,6 +20,7 @@
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec col_type(Type :: term()) -> term()
|
||||
%% @doc Maps sqlite3 column type.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(col_type/1::(atom() | string()) -> atom() | string()).
|
||||
col_type(integer) ->
|
||||
@@ -59,6 +61,7 @@ write_value_sql(Values) ->
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec write_col_sql([atom()]) -> string()
|
||||
%% @doc Creates the column/data stmt for SQL.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(write_col_sql/1::([atom()]) -> string()).
|
||||
write_col_sql(Cols) ->
|
||||
@@ -67,12 +70,58 @@ write_col_sql(Cols) ->
|
||||
end, Cols),
|
||||
string:join(StrCols, ",").
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec replace (String) -> string ()
|
||||
%% String = string ()
|
||||
%% @doc Returns copy of String for which \" replaced with '
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec (replace/1::(string ()) -> string ()).
|
||||
replace ([34 | T]) ->
|
||||
replace (T, ["'"]);
|
||||
replace ([H | T]) ->
|
||||
replace (T, [binary_to_list (<<H>>)]).
|
||||
replace ([], L) ->
|
||||
lists:flatten (lists:reverse (L));
|
||||
replace ([34 | T], L) ->
|
||||
replace (T, ["'" | L]);
|
||||
replace ([H | T], L) ->
|
||||
replace (T, [binary_to_list (<<H>>) | L]).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec update_set_sql ([{Col, Value}]) -> string ()
|
||||
%% Col = atom ()
|
||||
%% Value = integer () | float () | string ()
|
||||
%% @doc
|
||||
%% Creates update set stmt.
|
||||
%% Currently only supports integer, double/float and strings.
|
||||
%% For strings \" replaced with '.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec (update_set_sql/1::(any ()) -> string ()).
|
||||
update_set_sql (Data) ->
|
||||
Set = lists:map (fun
|
||||
({Col, Value}) when is_integer (Value) ->
|
||||
string:join (
|
||||
[atom_to_list (Col), integer_to_list (Value)], " = ");
|
||||
({Col, Value}) when is_float (Value) ->
|
||||
string:join (
|
||||
[atom_to_list (Col), integer_to_list (Value)], " = ");
|
||||
({Col, Value}) ->
|
||||
string:join (
|
||||
[atom_to_list (Col),
|
||||
io_lib:format ("\"~s\"", [replace (Value)])], " = ")
|
||||
end,
|
||||
Data),
|
||||
string:join (Set, ", ").
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec create_table_sql(Tbl, [{ColName, Type}]) -> string()
|
||||
%% Tbl = atom()
|
||||
%% ColName = atom()
|
||||
%% Type = string()
|
||||
%% @doc Generates a table create stmt in SQL.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(create_table_sql/2::(atom(), [{atom(), string()}]) -> string()).
|
||||
create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
|
||||
@@ -84,6 +133,29 @@ create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
|
||||
end, Tl), ", ") ++ ");",
|
||||
lists:flatten(CT ++ Start ++ End).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec update_sql (Tbl, Key, Value, Data) -> string ()
|
||||
%% Tbl = atom ()
|
||||
%% Key = atom ()
|
||||
%% Value = atom ()
|
||||
%% Data = [{ColName :: atom (), Value :: string () | integer () | float ()}]
|
||||
%% @doc
|
||||
%% Using Key as the column name and Data as list of column names
|
||||
%% and values pairs it creates the proper update SQL stmt for the
|
||||
%% record with matching Value.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-type(sql_value() :: string() | integer() | float()).
|
||||
-spec (update_sql/4::(atom (), atom (), atom (), [{atom (), sql_value ()}]) -> string ()).
|
||||
update_sql (Tbl, Key, Value, Data) ->
|
||||
lists:flatten (
|
||||
io_lib:format ("UPDATE ~p SET ~s WHERE ~p = ~p;",
|
||||
[Tbl,
|
||||
sqlite3_lib:update_set_sql (Data),
|
||||
Key,
|
||||
Value
|
||||
])).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec write_sql(Tbl, Data) -> string()
|
||||
%% Tbl = atom()
|
||||
@@ -92,7 +164,6 @@ create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
|
||||
%% proper insertion SQL stmt.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-type(sql_value() :: string() | integer() | float()).
|
||||
-spec(write_sql/2::(atom(), [{atom(), sql_value()}]) -> string()).
|
||||
write_sql(Tbl, Data) ->
|
||||
{Cols, Values} = lists:unzip(Data),
|
||||
|
||||
Reference in New Issue
Block a user