Construct SQL queries as iolists, not as strings

This commit is contained in:
Alexey Romanov
2010-10-05 14:27:28 +04:00
parent 709f3e8c5a
commit 842df283d0
2 changed files with 41 additions and 43 deletions

View File

@@ -255,7 +255,7 @@ write(Db, Tbl, Data) ->
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
update (Tbl, Key, Value, Data) -> update (Tbl, Key, Value, Data) ->
?MODULE:update (?MODULE, Tbl, Key, Value, Data). ?MODULE:update(?MODULE, Tbl, Key, Value, Data).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec update (Db::atom (), Tbl::atom (), Key::atom (), Value, Data) -> Result %% @spec update (Db::atom (), Tbl::atom (), Key::atom (), Value, Data) -> Result
@@ -270,7 +270,7 @@ update (Tbl, Key, Value, Data) ->
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
update (Db, Tbl, Key, Value, Data) -> update (Db, Tbl, Key, Value, Data) ->
gen_server:call (Db, {update, Tbl, Key, Value, Data}). gen_server:call(Db, {update, Tbl, Key, Value, Data}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read(Tbl::atom(), Key) -> [term()] %% @spec read(Tbl::atom(), Key) -> [term()]
@@ -312,7 +312,7 @@ read(Db, Tbl, Key) ->
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
read (Db, Tbl, Key, Columns) -> read (Db, Tbl, Key, Columns) ->
gen_server:call (Db, {read, Tbl, Key, Columns}). gen_server:call(Db, {read, Tbl, Key, Columns}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec delete(Tbl::atom(), Key) -> term() %% @spec delete(Tbl::atom(), Key) -> term()
@@ -553,7 +553,7 @@ exec(_Port, {create_function, _FunctionName, _Function}) ->
exec(Port, {sql_exec, Cmd}) -> exec(Port, {sql_exec, Cmd}) ->
port_control(Port, ?SQL_EXEC_COMMAND, list_to_binary(Cmd)), port_control(Port, ?SQL_EXEC_COMMAND, Cmd),
wait_result(Port). wait_result(Port).

View File

@@ -56,7 +56,7 @@ write_value_sql(Values) ->
(?NULL_ATOM) -> "null"; (?NULL_ATOM) -> "null";
(X) -> io_lib:format("'~s'", [X]) (X) -> io_lib:format("'~s'", [X])
end, Values), end, Values),
string:join(StrValues, ","). intersperse(StrValues, ",").
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec write_col_sql([atom()]) -> string() %% @spec write_col_sql([atom()]) -> string()
@@ -68,7 +68,7 @@ write_col_sql(Cols) ->
StrCols = lists:map(fun(X) -> StrCols = lists:map(fun(X) ->
atom_to_list(X) atom_to_list(X)
end, Cols), end, Cols),
string:join(StrCols, ","). intersperse(StrCols, ",").
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec replace (String) -> string () %% @spec replace (String) -> string ()
@@ -102,18 +102,15 @@ replace ([H | T], L) ->
update_set_sql (Data) -> update_set_sql (Data) ->
Set = lists:map (fun Set = lists:map (fun
({Col, Value}) when is_integer (Value) -> ({Col, Value}) when is_integer (Value) ->
string:join ( [atom_to_list (Col), " = ", integer_to_list (Value)];
[atom_to_list (Col), integer_to_list (Value)], " = ");
({Col, Value}) when is_float (Value) -> ({Col, Value}) when is_float (Value) ->
string:join ( [atom_to_list (Col), " = ", float_to_list (Value)];
[atom_to_list (Col), integer_to_list (Value)], " = ");
({Col, Value}) -> ({Col, Value}) ->
string:join ( [atom_to_list (Col), " = ",
[atom_to_list (Col), io_lib:format ("\"~s\"", [replace (Value)])]
io_lib:format ("\"~s\"", [replace (Value)])], " = ")
end, end,
Data), Data),
string:join (Set, ", "). intersperse(Set, ", ").
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_cols_sql (Columns::[atom ()]) -> string () %% @spec read_cols_sql (Columns::[atom ()]) -> string ()
@@ -123,8 +120,8 @@ update_set_sql (Data) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec (read_cols_sql/1::([atom ()]) -> string ()). -spec (read_cols_sql/1::([atom ()]) -> string ()).
read_cols_sql (Columns) -> read_cols_sql (Columns) ->
string:join ( intersperse(
lists:map (fun (A) -> atom_to_list (A) end, Columns), ", "). lists:map(fun atom_to_list/1, Columns), ", ").
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec create_table_sql(Tbl, [{ColName, Type}]) -> string() %% @spec create_table_sql(Tbl, [{ColName, Type}]) -> string()
@@ -138,11 +135,11 @@ read_cols_sql (Columns) ->
create_table_sql(Tbl, [{ColName, Type} | Tl]) -> create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
CT = io_lib:format("CREATE TABLE ~p ", [Tbl]), CT = io_lib:format("CREATE TABLE ~p ", [Tbl]),
Start = io_lib:format("(~p ~s PRIMARY KEY, ", [ColName, sqlite3_lib:col_type(Type)]), Start = io_lib:format("(~p ~s PRIMARY KEY, ", [ColName, sqlite3_lib:col_type(Type)]),
End = string:join( End = [intersperse(
lists:map(fun({Name0, Type0}) -> lists:map(fun({Name0, Type0}) ->
io_lib:format("~p ~s", [Name0, sqlite3_lib:col_type(Type0)]) io_lib:format("~p ~s", [Name0, sqlite3_lib:col_type(Type0)])
end, Tl), ", ") ++ ");", end, Tl), ", "), ");"],
lists:flatten(CT ++ Start ++ End). [CT, Start, End].
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec update_sql (Tbl, Key, Value, Data) -> string () %% @spec update_sql (Tbl, Key, Value, Data) -> string ()
@@ -159,13 +156,12 @@ create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
-type(sql_value() :: string() | integer() | float()). -type(sql_value() :: string() | integer() | float()).
-spec (update_sql/4::(atom (), atom (), atom (), [{atom (), sql_value ()}]) -> string ()). -spec (update_sql/4::(atom (), atom (), atom (), [{atom (), sql_value ()}]) -> string ()).
update_sql (Tbl, Key, Value, Data) -> update_sql (Tbl, Key, Value, Data) ->
lists:flatten ( io_lib:format("UPDATE ~p SET ~s WHERE ~p = ~p;",
io_lib:format ("UPDATE ~p SET ~s WHERE ~p = ~p;", [Tbl,
[Tbl, sqlite3_lib:update_set_sql (Data),
sqlite3_lib:update_set_sql (Data), Key,
Key, Value
Value ]).
])).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec write_sql(Tbl, Data) -> string() %% @spec write_sql(Tbl, Data) -> string()
@@ -178,11 +174,11 @@ update_sql (Tbl, Key, Value, Data) ->
-spec(write_sql/2::(atom(), [{atom(), sql_value()}]) -> string()). -spec(write_sql/2::(atom(), [{atom(), sql_value()}]) -> string()).
write_sql(Tbl, Data) -> write_sql(Tbl, Data) ->
{Cols, Values} = lists:unzip(Data), {Cols, Values} = lists:unzip(Data),
lists:flatten( io_lib:format("INSERT INTO ~p (~s) values (~s);",
io_lib:format("INSERT INTO ~p (~s) values (~s);", [Tbl,
[Tbl, sqlite3_lib:write_col_sql(Cols),
sqlite3_lib:write_col_sql(Cols), sqlite3_lib:write_value_sql(Values)
sqlite3_lib:write_value_sql(Values)])). ]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_sql(Tbl, Key, Value) -> string() %% @spec read_sql(Tbl, Key, Value) -> string()
@@ -195,8 +191,7 @@ write_sql(Tbl, Data) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec(read_sql/3::(atom(), atom(), sql_value()) -> string()). -spec(read_sql/3::(atom(), atom(), sql_value()) -> string()).
read_sql(Tbl, Key, Value) -> read_sql(Tbl, Key, Value) ->
lists:flatten( io_lib:format("SELECT * FROM ~p WHERE ~p = ~p;", [Tbl, Key, Value]).
io_lib:format("SELECT * FROM ~p WHERE ~p = ~p;", [Tbl, Key, Value])).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec read_sql (Tbl, Key, Value, Columns) -> string () %% @spec read_sql (Tbl, Key, Value, Columns) -> string ()
@@ -211,13 +206,12 @@ read_sql(Tbl, Key, Value) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec (read_sql/4::(atom (), atom (), sql_value (), [atom ()]) -> string ()). -spec (read_sql/4::(atom (), atom (), sql_value (), [atom ()]) -> string ()).
read_sql (Tbl, Key, Value, Columns) -> read_sql (Tbl, Key, Value, Columns) ->
lists:flatten ( io_lib:format("SELECT ~s FROM ~p WHERE ~p = ~p;",
io_lib:format ("SELECT ~s FROM ~p WHERE ~p = ~p;",
[sqlite3_lib:read_cols_sql (Columns), [sqlite3_lib:read_cols_sql (Columns),
Tbl, Tbl,
Key, Key,
Value Value
])). ]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec delete_sql(Tbl, Key, Value) -> string() %% @spec delete_sql(Tbl, Key, Value) -> string()
@@ -230,8 +224,7 @@ read_sql (Tbl, Key, Value, Columns) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec(delete_sql/3::(atom(), atom(), sql_value()) -> string()). -spec(delete_sql/3::(atom(), atom(), sql_value()) -> string()).
delete_sql(Tbl, Key, Value) -> delete_sql(Tbl, Key, Value) ->
lists:flatten( io_lib:format("DELETE FROM ~p WHERE ~p = ~p;", [Tbl, Key, Value]).
io_lib:format("DELETE FROM ~p WHERE ~p = ~p;", [Tbl, Key, Value])).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @spec drop_table(Tbl) -> string() %% @spec drop_table(Tbl) -> string()
@@ -241,9 +234,14 @@ delete_sql(Tbl, Key, Value) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec(drop_table/1::(atom()) -> string()). -spec(drop_table/1::(atom()) -> string()).
drop_table(Tbl) -> drop_table(Tbl) ->
lists:flatten( io_lib:format("DROP TABLE ~p;", [Tbl]).
io_lib:format("DROP TABLE ~p;", [Tbl])).
%%==================================================================== %%====================================================================
%% Internal functions %% Internal functions
%%==================================================================== %%====================================================================
%% @doc Works like string:join for iolists
intersperse([], _Sep) -> [];
intersperse([Elem], _Sep) -> [Elem];
intersperse([Head | Tail], Sep) -> [Head, Sep | intersperse(Tail, Sep)].