From 344bb59b6d2e88342bfc0c335040456f5f84f9f3 Mon Sep 17 00:00:00 2001 From: Tee Teoh Date: Thu, 17 Jul 2008 13:52:23 -0400 Subject: [PATCH] Moved functions to sqlite_lib and added edocs comments. --- src/sqlite.erl | 56 +++++++++++++++++++------------ src/sqlite_lib.erl | 84 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 112 insertions(+), 28 deletions(-) diff --git a/src/sqlite.erl b/src/sqlite.erl index bb30335..721b894 100644 --- a/src/sqlite.erl +++ b/src/sqlite.erl @@ -18,8 +18,9 @@ -export([create_table/2, create_table/3]). -export([list_tables/0, list_tables/1, table_info/1, table_info/2]). -export([write/2, write/3]). - --export([write_sql/2]). +-export([read/2, read/3]). +-export([delete/2, delete/3]). +-export([drop_table/1, drop_table/2]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, @@ -93,6 +94,25 @@ write(Tbl, Data) -> write(Db, Tbl, Data) -> gen_server:call(Db, {write, Tbl, Data}). +read(Tbl, Key) -> + ?MODULE:read(?MODULE, Tbl, Key). + +read(Db, Tbl, Key) -> + gen_server:call(Db, {read, Tbl, Key}). + +delete(Tbl, Key) -> + ?MODULE:delete(?MODULE, Tbl, Key). + +delete(Db, Tbl, Key) -> + gen_server:call(Db, {delete, Tbl, Key}). + +drop_table(Tbl) -> + ?MODULE:drop_table(?MODULE, Tbl). + +drop_table(Db, Tbl) -> + gen_server:call(Db, {drop_table, Tbl}). + + %%==================================================================== %% gen_server callbacks %%==================================================================== @@ -135,13 +155,24 @@ handle_call({table_info, Tbl}, _From, #state{port = Port} = State) -> Reply = parse_table_info(Info), {reply, Reply, State}; handle_call({create_table, Tbl, Options}, _From, #state{port = Port} = State) -> - SQL = create_table_sql(Tbl, Options), + SQL = sqlite_lib:create_table_sql(Tbl, Options), Cmd = {sql_exec, SQL}, Reply = exec(Port, Cmd), {reply, Reply, State}; handle_call({write, Tbl, Data}, _From, #state{port = Port} = State) -> % insert into t1 (data,num) values ('This is sample data',3); - Reply = exec(Port, {sql_exec, write_sql(Tbl, Data)}), + Reply = exec(Port, {sql_exec, sqlite_lib:write_sql(Tbl, Data)}), + {reply, Reply, State}; +handle_call({read, Tbl, {Key, Value}}, _From, #state{port = Port} = State) -> + % select * from Tbl where Key = Value; + Reply = exec(Port, {sql_exec, sqlite_lib:read_sql(Tbl, Key, Value)}), + {reply, Reply, State}; +handle_call({delete, Tbl, {Key, Value}}, _From, #state{port = Port} = State) -> + % delete from Tbl where Key = Value; + Reply = exec(Port, {sql_exec, sqlite_lib:delete_sql(Tbl, Key, Value)}), + {reply, Reply, State}; +handle_call({drop_table, Tbl}, _From, #state{port = Port} = State) -> + Reply = exec(Port, {sql_exec, sqlite_lib:drop_table(Tbl)}), {reply, Reply, State}; handle_call(_Request, _From, State) -> Reply = ok, @@ -221,20 +252,3 @@ build_table_info([[ColName, ColType] | Tl], Acc) -> build_table_info([[ColName, ColType, "PRIMARY", "KEY"] | Tl], Acc) -> build_table_info(Tl, [{list_to_atom(ColName), sqlite_lib:col_type(ColType)}| Acc]). -create_table_sql(Tbl, [{Name, Type} | Tl]) -> - CT = io_lib:format("CREATE TABLE ~p ", [Tbl]), - Start = io_lib:format("(~p ~s PRIMARY KEY, ", [Name, sqlite_lib:col_type(Type)]), - End = string:join( - lists:map(fun({Name0, Type0}) -> - io_lib:format("~p ~s", [Name0, sqlite_lib:col_type(Type0)]) - end, Tl), ", ") ++ ");", - lists:flatten(CT ++ Start ++ End). - -% insert into t1 (data,num) values ('This is sample data',3); -write_sql(Tbl, Data) -> - {Cols, Values} = lists:unzip(Data), - lists:flatten( - io_lib:format("INSERT INTO ~p (~s) values (~s);", - [Tbl, sqlite_lib:write_col_sql(Cols), sqlite_lib:write_value_sql(Values)])). - - diff --git a/src/sqlite_lib.erl b/src/sqlite_lib.erl index 1b87fe1..bb0cafa 100644 --- a/src/sqlite_lib.erl +++ b/src/sqlite_lib.erl @@ -10,13 +10,14 @@ %% API -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]). %%==================================================================== %% API %%==================================================================== %%-------------------------------------------------------------------- -%% Function: col_type(Type :: term()) -> term() -%% Description: Maps sqlite column type. +%% @spec col_type(Type :: term()) -> term() +%% @doc Maps sqlite column type. %%-------------------------------------------------------------------- col_type(integer) -> "INTEGER"; @@ -36,9 +37,11 @@ col_type("DATE") -> date. %%-------------------------------------------------------------------- -%% Function: write_value_sql(Value :: [term()]) -> string() -%% Description: Creates the values portion of the sql stmt. -%% Currently only support integer, double/float and strings. +%% @spec write_value_sql(Value :: [term()]) -> string() +%% @doc +%% Creates the values portion of the sql stmt. +%% Currently only support integer, double/float and strings. +%% @end %%-------------------------------------------------------------------- write_value_sql(Values) -> StrValues = lists:map(fun(X) when is_integer(X) -> @@ -51,8 +54,8 @@ write_value_sql(Values) -> string:join(StrValues, ","). %%-------------------------------------------------------------------- -%% Function: write_col_sql([atom()]) -> string() -%% Description: Creates the column/data stmt for SQL. +%% @spec write_col_sql([atom()]) -> string() +%% @doc Creates the column/data stmt for SQL. %%-------------------------------------------------------------------- write_col_sql(Cols) -> StrCols = lists:map(fun(X) -> @@ -60,6 +63,73 @@ write_col_sql(Cols) -> end, Cols), string:join(StrCols, ","). +%%-------------------------------------------------------------------- +%% @spec create_table_sql(Tbl, [{ColName, Type}]) -> string() +%% Tbl :: atom() +%% ColName :: atom() +%% Type :: string() +%% @doc Generates a table create stmt in SQL. +%%-------------------------------------------------------------------- +create_table_sql(Tbl, [{ColName, Type} | Tl]) -> + CT = io_lib:format("CREATE TABLE ~p ", [Tbl]), + Start = io_lib:format("(~p ~s PRIMARY KEY, ", [ColName, sqlite_lib:col_type(Type)]), + End = string:join( + lists:map(fun({Name0, Type0}) -> + io_lib:format("~p ~s", [Name0, sqlite_lib:col_type(Type0)]) + end, Tl), ", ") ++ ");", + lists:flatten(CT ++ Start ++ End). + +%%-------------------------------------------------------------------- +%% @spec write_sql(Tbl, Data) -> string() +%% Tbl :: atom() +%% Data :: [{ColName :: atom(), Values :: string() || integer() || float()}] +%% @doc Taking Data as list of column names and values pairs it creates the +%% proper insertion SQL stmt. +%% @end +%%-------------------------------------------------------------------- +write_sql(Tbl, Data) -> + {Cols, Values} = lists:unzip(Data), + lists:flatten( + io_lib:format("INSERT INTO ~p (~s) values (~s);", + [Tbl, + sqlite_lib:write_col_sql(Cols), + sqlite_lib:write_value_sql(Values)])). + +%%-------------------------------------------------------------------- +%% @spec read_sql(Tbl, Key, Value) -> string() +%% Tbl :: atom() +%% Key :: atom() +%% Value :: string() || integer() || float() +%% @doc Using Key as the column name searches for the record with +%% matching Value. +%% @end +%%-------------------------------------------------------------------- +read_sql(Tbl, Key, Value) -> + lists:flatten( + io_lib:format("SELECT * FROM ~p WHERE ~p = ~p;", [Tbl, Key, Value])). + +%%-------------------------------------------------------------------- +%% @spec delete_sql(Tbl, Key, Value) -> string() +%% Tbl :: atom() +%% Key :: atom() +%% Value :: string() || integer() || float() +%% @doc Using Key as the column name searches for the record with +%% matching Value then deletes that record. +%% @end +%%-------------------------------------------------------------------- +delete_sql(Tbl, Key, Value) -> + lists:flatten( + io_lib:format("DELETE FROM ~p WHERE ~p = ~p;", [Tbl, Key, Value])). + +%%-------------------------------------------------------------------- +%% @spec drop_table(Tbl) -> string() +%% Tbl :: atom() +%% @doc Drop the table Tbl from the database +%% @end +%%-------------------------------------------------------------------- +drop_table(Tbl) -> + lists:flatten( + io_lib:format("DROP TABLE ~p;", [Tbl])). %%==================================================================== %% Internal functions