From 8545a74c7e2929e3485d4ed166a5b24f073ba43e Mon Sep 17 00:00:00 2001 From: Alexey Romanov Date: Wed, 20 Oct 2010 15:01:30 +0400 Subject: [PATCH] Added read_all functions --- src/sqlite3.erl | 33 +++++++++++++++++++++++++++------ test/sqlite3_test.erl | 6 ++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/sqlite3.erl b/src/sqlite3.erl index 5b7c569..71d752d 100644 --- a/src/sqlite3.erl +++ b/src/sqlite3.erl @@ -21,8 +21,7 @@ -export([list_tables/0, list_tables/1, table_info/1, table_info/2]). -export([write/2, write/3]). -export([update/4, update/5]). --export([read/4]). --export([read/2, read/3]). +-export([read_all/2, read_all/3, read/2, read/3, read/4]). -export([delete/2, delete/3]). -export([drop_table/1, drop_table/2]). @@ -292,6 +291,26 @@ update(Tbl, Key, Value, Data) -> update(Db, Tbl, Key, Value, Data) -> gen_server:call(Db, {update, Tbl, Key, Value, Data}). +%%-------------------------------------------------------------------- +%% @spec read_all(Db :: atom(), Table :: atom()) -> any() +%% @doc +%% Reads all rows from Table in Db. +%% @end +%%-------------------------------------------------------------------- +-spec read_all(atom(), atom()) -> any(). +read_all(Db, Tbl) -> + gen_server:call(Db, {read, Tbl}). + +%%-------------------------------------------------------------------- +%% @spec read(Db :: atom(), Table :: atom(), Columns :: [atom()]) -> any() +%% @doc +%% Reads Columns in all rows from Table in Db. +%% @end +%%-------------------------------------------------------------------- +-spec read_all(atom(), atom(), [atom()]) -> any(). +read_all(Db, Tbl, Columns) -> + gen_server:call(Db, {read, Tbl, Columns}). + %%-------------------------------------------------------------------- %% @spec read(Tbl :: atom(), Key) -> [any()] %% Key = {Column :: atom(), Value :: sql_value()} @@ -315,8 +334,8 @@ read(Tbl, Key) -> %% @end %%-------------------------------------------------------------------- -spec read(atom(), atom(), {atom(), any()}) -> any(). -read(Db, Tbl, {Key, Value}) -> - gen_server:call(Db, {read, Tbl, Key, Value}). +read(Db, Tbl, {Column, Value}) -> + gen_server:call(Db, {read, Tbl, Column, Value}). %%-------------------------------------------------------------------- %% @spec read(Db, Tbl, Key, Columns) -> [any()] @@ -608,11 +627,13 @@ create_port_cmd(Dbase) -> wait_result(Port) -> receive + %% Messages given at http://www.erlang.org/doc/reference_manual/ports.html {Port, Reply} -> % io:format("Reply: ~p~n", [Reply]), Reply; - {error, Reason} -> - io:format("Error: ~p~n", [Reason]), + {'EXIT', Port, Reason} -> + error_logger:error_msg("sqlite3 driver port closed with reason~p~n", [Reason]), + % io:format("Error: ~p~n", [Reason]), {error, Reason} %% ; %% _Else -> diff --git a/test/sqlite3_test.erl b/test/sqlite3_test.erl index 3697274..b51ad52 100644 --- a/test/sqlite3_test.erl +++ b/test/sqlite3_test.erl @@ -57,6 +57,12 @@ basic_functionality_test() -> ?assertEqual( [{columns, Columns}, {rows, AllRows}], sqlite3:sql_exec(ct, "select * from user;")), + ?assertEqual( + [{columns, Columns}, {rows, AllRows}], + sqlite3:read_all(ct, user)), + ?assertEqual( + [{columns, ["name"]}, {rows, [{<<"abby">>}, {<<"marge">>}]}], + sqlite3:read_all(ct, user, [name])), ?assertEqual( [{columns, Columns}, {rows, AbbyOnly}], sqlite3:read(ct, user, {name, "abby"})),