More tests
This commit is contained in:
@@ -315,8 +315,8 @@ read(Tbl, Key) ->
|
|||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec read(atom(), atom(), {atom(), any()}) -> any().
|
-spec read(atom(), atom(), {atom(), any()}) -> any().
|
||||||
read(Db, Tbl, Key) ->
|
read(Db, Tbl, {Key, Value}) ->
|
||||||
gen_server:call(Db, {read, Tbl, Key}).
|
gen_server:call(Db, {read, Tbl, Key, Value}).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @spec read(Db, Tbl, Key, Columns) -> [any()]
|
%% @spec read(Db, Tbl, Key, Columns) -> [any()]
|
||||||
@@ -330,8 +330,8 @@ read(Db, Tbl, Key) ->
|
|||||||
%% determined from table_info/3.
|
%% determined from table_info/3.
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
read(Db, Tbl, Key, Columns) ->
|
read(Db, Tbl, {Key, Value}, Columns) ->
|
||||||
gen_server:call(Db, {read, Tbl, Key, Columns}).
|
gen_server:call(Db, {read, Tbl, Key, Value, Columns}).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @spec delete(Tbl :: atom(), Key) -> any()
|
%% @spec delete(Tbl :: atom(), Key) -> any()
|
||||||
@@ -514,11 +514,18 @@ handle_call({write, Tbl, Data}, _From, #state{port = Port} = State) ->
|
|||||||
% insert into t1 (data,num) values ('This is sample data',3);
|
% insert into t1 (data,num) values ('This is sample data',3);
|
||||||
Reply = exec(Port, {sql_exec, sqlite3_lib:write_sql(Tbl, Data)}),
|
Reply = exec(Port, {sql_exec, sqlite3_lib:write_sql(Tbl, Data)}),
|
||||||
{reply, Reply, State};
|
{reply, Reply, State};
|
||||||
handle_call({read, Tbl, {Key, Value}}, _From, #state{port = Port} = State) ->
|
handle_call({read, Tbl}, _From, #state{port = Port} = State) ->
|
||||||
|
% select * from Tbl where Key = Value;
|
||||||
|
Reply = exec(Port, {sql_exec, sqlite3_lib:read_sql(Tbl)}),
|
||||||
|
{reply, Reply, State};
|
||||||
|
handle_call({read, Tbl, Columns}, _From, #state{port = Port} = State) ->
|
||||||
|
Reply = exec(Port, {sql_exec, sqlite3_lib:read_sql(Tbl, Columns)}),
|
||||||
|
{reply, Reply, State};
|
||||||
|
handle_call({read, Tbl, Key, Value}, _From, #state{port = Port} = State) ->
|
||||||
% select * from Tbl where Key = Value;
|
% select * from Tbl where Key = Value;
|
||||||
Reply = exec(Port, {sql_exec, sqlite3_lib:read_sql(Tbl, Key, Value)}),
|
Reply = exec(Port, {sql_exec, sqlite3_lib:read_sql(Tbl, Key, Value)}),
|
||||||
{reply, Reply, State};
|
{reply, Reply, State};
|
||||||
handle_call({read, Tbl, {Key, Value}, Columns}, _From, #state{port = Port} = State) ->
|
handle_call({read, Tbl, Key, Value, Columns}, _From, #state{port = Port} = State) ->
|
||||||
Reply = exec(Port, {sql_exec, sqlite3_lib:read_sql(Tbl, Key, Value, Columns)}),
|
Reply = exec(Port, {sql_exec, sqlite3_lib:read_sql(Tbl, Key, Value, Columns)}),
|
||||||
{reply, Reply, State};
|
{reply, Reply, State};
|
||||||
handle_call({delete, Tbl, {Key, Value}}, _From, #state{port = Port} = State) ->
|
handle_call({delete, Tbl, {Key, Value}}, _From, #state{port = Port} = State) ->
|
||||||
@@ -526,7 +533,7 @@ handle_call({delete, Tbl, {Key, Value}}, _From, #state{port = Port} = State) ->
|
|||||||
Reply = exec(Port, {sql_exec, sqlite3_lib:delete_sql(Tbl, Key, Value)}),
|
Reply = exec(Port, {sql_exec, sqlite3_lib:delete_sql(Tbl, Key, Value)}),
|
||||||
{reply, Reply, State};
|
{reply, Reply, State};
|
||||||
handle_call({drop_table, Tbl}, _From, #state{port = Port} = State) ->
|
handle_call({drop_table, Tbl}, _From, #state{port = Port} = State) ->
|
||||||
Reply = exec(Port, {sql_exec, sqlite3_lib:drop_table(Tbl)}),
|
Reply = exec(Port, {sql_exec, sqlite3_lib:drop_table_sql(Tbl)}),
|
||||||
{reply, Reply, State};
|
{reply, Reply, State};
|
||||||
handle_call(_Request, _From, State) ->
|
handle_call(_Request, _From, State) ->
|
||||||
Reply = ok,
|
Reply = ok,
|
||||||
|
|||||||
@@ -13,9 +13,9 @@
|
|||||||
-export([col_type_to_atom/1]).
|
-export([col_type_to_atom/1]).
|
||||||
-export([value_to_sql/1, value_to_sql_unsafe/1, sql_to_value/1, escape/1]).
|
-export([value_to_sql/1, value_to_sql_unsafe/1, sql_to_value/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, create_table_sql/3, drop_table/1]).
|
-export([create_table_sql/2, create_table_sql/3, drop_table_sql/1]).
|
||||||
-export([write_sql/2, update_sql/4, update_set_sql/1, delete_sql/3]).
|
-export([write_sql/2, update_sql/4, update_set_sql/1, delete_sql/3]).
|
||||||
-export([read_sql/3, read_sql/4, read_cols_sql/1]).
|
-export([read_sql/1, read_sql/2, read_sql/3, read_sql/4, read_cols_sql/1]).
|
||||||
|
|
||||||
%%====================================================================
|
%%====================================================================
|
||||||
%% API
|
%% API
|
||||||
@@ -233,6 +233,29 @@ write_sql(Tbl, 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) -> iolist()
|
||||||
|
%% Tbl = atom()
|
||||||
|
%% @doc Returns all records from table Tbl.
|
||||||
|
%% @end
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
-spec read_sql(atom()) -> iolist().
|
||||||
|
read_sql(Tbl) ->
|
||||||
|
["SELECT * FROM ", atom_to_list(Tbl), ";"].
|
||||||
|
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% @spec read_sql(Tbl, Columns) -> iolist()
|
||||||
|
%% Tbl = atom()
|
||||||
|
%% Columns = [atom()]
|
||||||
|
%% @doc
|
||||||
|
%% Returns only specified Columns of all records from table Tbl.
|
||||||
|
%% @end
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
-spec read_sql(atom(), [atom()]) -> iolist().
|
||||||
|
read_sql(Tbl, Columns) ->
|
||||||
|
["SELECT ", sqlite3_lib:read_cols_sql(Columns), " FROM ",
|
||||||
|
atom_to_list(Tbl), ";"].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @spec read_sql(Tbl, Key, Value) -> iolist()
|
%% @spec read_sql(Tbl, Key, Value) -> iolist()
|
||||||
%% Tbl = atom()
|
%% Tbl = atom()
|
||||||
@@ -254,8 +277,8 @@ read_sql(Tbl, Key, Value) ->
|
|||||||
%% Value = sql_value()
|
%% Value = sql_value()
|
||||||
%% Columns = [atom()]
|
%% Columns = [atom()]
|
||||||
%% @doc
|
%% @doc
|
||||||
%% Using Key as the column name searhces for the record with
|
%% Using Key as the column name searches for the record with
|
||||||
%% matching Value and returns only specified columns Columns.
|
%% matching Value and returns only specified Columns.
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec read_sql(atom(), atom(), sql_value(), [atom()]) -> iolist().
|
-spec read_sql(atom(), atom(), sql_value(), [atom()]) -> iolist().
|
||||||
@@ -279,13 +302,13 @@ delete_sql(Tbl, Key, Value) ->
|
|||||||
" = ", value_to_sql(Value), ";"].
|
" = ", value_to_sql(Value), ";"].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @spec drop_table(Tbl) -> iolist()
|
%% @spec drop_table_sql(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()) -> iolist().
|
-spec drop_table_sql(atom()) -> iolist().
|
||||||
drop_table(Tbl) ->
|
drop_table_sql(Tbl) ->
|
||||||
["DROP TABLE ", atom_to_list(Tbl), ";"].
|
["DROP TABLE ", atom_to_list(Tbl), ";"].
|
||||||
|
|
||||||
%%====================================================================
|
%%====================================================================
|
||||||
@@ -363,10 +386,52 @@ indexed_column_sql(ColumnName) -> atom_to_list(ColumnName).
|
|||||||
-include_lib("eunit/include/eunit.hrl").
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
-define(FLAT(X), iolist_to_binary(X)).
|
-define(FLAT(X), iolist_to_binary(X)).
|
||||||
|
-define(assertFlat(Expected, Value), ?assertEqual(?FLAT(Expected), ?FLAT(Value))).
|
||||||
|
|
||||||
quote_test() ->
|
quote_test() ->
|
||||||
?assertEqual(<<"'abc'">>, ?FLAT(value_to_sql("abc"))),
|
?assertFlat("'abc'", value_to_sql("abc")),
|
||||||
?assertEqual(<<"'a''b''''c'">>, ?FLAT(value_to_sql("a'b''c"))).
|
?assertFlat("'a''b''''c'", value_to_sql("a'b''c")).
|
||||||
|
|
||||||
|
create_table_sql_test() ->
|
||||||
|
?assertFlat(
|
||||||
|
"CREATE TABLE user (id INTEGER PRIMARY KEY, name TEXT);",
|
||||||
|
create_table_sql(user, [{id, integer, [primary_key]}, {name, text}])),
|
||||||
|
?assertFlat(
|
||||||
|
"CREATE TABLE user (id INTEGER, name TEXT, PRIMARY KEY(id));",
|
||||||
|
create_table_sql(user, [{id, integer}, {name, text}], [{primary_key, [id]}])).
|
||||||
|
|
||||||
|
update_sql_test() ->
|
||||||
|
?assertFlat(
|
||||||
|
"UPDATE user SET name = 'a' WHERE id = 1;",
|
||||||
|
update_sql(user, id, 1, [{name, "a"}])).
|
||||||
|
|
||||||
|
write_sql_test() ->
|
||||||
|
?assertFlat(
|
||||||
|
"INSERT INTO user (id, name) values (1, 'a');",
|
||||||
|
write_sql(user, [{id, 1}, {name, "a"}])).
|
||||||
|
|
||||||
|
read_sql_test() ->
|
||||||
|
?assertFlat(
|
||||||
|
"SELECT * FROM user;",
|
||||||
|
read_sql(user)),
|
||||||
|
?assertFlat(
|
||||||
|
"SELECT id, name FROM user;",
|
||||||
|
read_sql(user, [id, name])),
|
||||||
|
?assertFlat(
|
||||||
|
"SELECT * FROM user WHERE id = 1;",
|
||||||
|
read_sql(user, id, 1)),
|
||||||
|
?assertFlat(
|
||||||
|
"SELECT id, name FROM user WHERE id = 1;",
|
||||||
|
read_sql(user, id, 1, [id, name])).
|
||||||
|
|
||||||
|
delete_sql_test() ->
|
||||||
|
?assertFlat(
|
||||||
|
"DELETE FROM user WHERE id = 1;",
|
||||||
|
delete_sql(user, id, 1)).
|
||||||
|
|
||||||
|
drop_table_sql_test() ->
|
||||||
|
?assertFlat(
|
||||||
|
"DROP TABLE user;",
|
||||||
|
drop_table_sql(user)).
|
||||||
|
|
||||||
-endif.
|
-endif.
|
||||||
|
|||||||
Reference in New Issue
Block a user