Add read operation (with specified columns to read)
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
-export([list_tables/0, list_tables/1, table_info/1, table_info/2]).
|
-export([list_tables/0, list_tables/1, table_info/1, table_info/2]).
|
||||||
-export([write/2, write/3]).
|
-export([write/2, write/3]).
|
||||||
-export([update/4, update/5]).
|
-export([update/4, update/5]).
|
||||||
|
-export([read/4]).
|
||||||
-export([read/2, read/3]).
|
-export([read/2, read/3]).
|
||||||
-export([delete/2, delete/3]).
|
-export([delete/2, delete/3]).
|
||||||
-export([drop_table/1, drop_table/2]).
|
-export([drop_table/1, drop_table/2]).
|
||||||
@@ -297,6 +298,22 @@ read(Tbl, Key) ->
|
|||||||
read(Db, Tbl, Key) ->
|
read(Db, Tbl, Key) ->
|
||||||
gen_server:call(Db, {read, Tbl, Key}).
|
gen_server:call(Db, {read, Tbl, Key}).
|
||||||
|
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% @spec read (Db, Tbl, Key, Columns) -> [term ()]
|
||||||
|
%% Db = atom ()
|
||||||
|
%% Tbl = atom ()
|
||||||
|
%% Key = {Column::atom (), Value::term ()}
|
||||||
|
%% Columns = [atom ()]
|
||||||
|
%% @doc
|
||||||
|
%% Reads a row from Tbl table in Db dbase such that the Value
|
||||||
|
%% matches the value in Column. Returns columns Columns only for
|
||||||
|
%% the first match. Value must have the same type as determined
|
||||||
|
%% from table_info/3.
|
||||||
|
%% @end
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
read (Db, Tbl, Key, Columns) ->
|
||||||
|
gen_server:call (Db, {read, Tbl, Key, Columns}).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @spec delete(Tbl::atom(), Key) -> term()
|
%% @spec delete(Tbl::atom(), Key) -> term()
|
||||||
%% Key = {ColName::atom(), ColValue::term()}
|
%% Key = {ColName::atom(), ColValue::term()}
|
||||||
@@ -439,6 +456,9 @@ 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) ->
|
||||||
|
Reply = exec (Port, {sql_exec, sqlite3_lib:read_sql (Tbl, Key, Value, Columns)}),
|
||||||
|
{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) ->
|
||||||
% delete from Tbl where Key = Value;
|
% delete from Tbl where Key = Value;
|
||||||
Reply = exec(Port, {sql_exec, sqlite3_lib:delete_sql(Tbl, Key, Value)}),
|
Reply = exec(Port, {sql_exec, sqlite3_lib:delete_sql(Tbl, Key, Value)}),
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
-export([write_value_sql/1, write_col_sql/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([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]).
|
-export ([update_sql/4, update_set_sql/1, replace/1]).
|
||||||
|
-export ([read_sql/4, read_cols_sql/1]).
|
||||||
|
|
||||||
%%====================================================================
|
%%====================================================================
|
||||||
%% API
|
%% API
|
||||||
@@ -115,6 +116,17 @@ update_set_sql (Data) ->
|
|||||||
Data),
|
Data),
|
||||||
string:join (Set, ", ").
|
string:join (Set, ", ").
|
||||||
|
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% @spec read_cols_sql (Columns::[atom ()]) -> string ()
|
||||||
|
%% @doc
|
||||||
|
%% Creates list of columns for select stmt.
|
||||||
|
%% @end
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
-spec (read_cols_sql/1::([atom ()]) -> string ()).
|
||||||
|
read_cols_sql (Columns) ->
|
||||||
|
string:join (
|
||||||
|
lists:map (fun (A) -> atom_to_list (A) end, Columns), ", ").
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @spec create_table_sql(Tbl, [{ColName, Type}]) -> string()
|
%% @spec create_table_sql(Tbl, [{ColName, Type}]) -> string()
|
||||||
%% Tbl = atom()
|
%% Tbl = atom()
|
||||||
@@ -187,6 +199,27 @@ read_sql(Tbl, Key, Value) ->
|
|||||||
lists:flatten(
|
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 ()
|
||||||
|
%% Tbl = atom ()
|
||||||
|
%% Key = atom ()
|
||||||
|
%% Value = string () | integer () | float ()
|
||||||
|
%% Columns = [atom ()]
|
||||||
|
%% @doc
|
||||||
|
%% Using Key as the column name searhces for the record with
|
||||||
|
%% matching Value and returns only specified columns Columns.
|
||||||
|
%% @end
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
-spec (read_sql/4::(atom (), atom (), sql_value (), [atom ()]) -> string ()).
|
||||||
|
read_sql (Tbl, Key, Value, Columns) ->
|
||||||
|
lists:flatten (
|
||||||
|
io_lib:format ("SELECT ~p FROM ~p WHERE ~p = ~p;",
|
||||||
|
[sqlite3_lib:read_cols_sql (Columns),
|
||||||
|
Tbl,
|
||||||
|
Key,
|
||||||
|
Value
|
||||||
|
])).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @spec delete_sql(Tbl, Key, Value) -> string()
|
%% @spec delete_sql(Tbl, Key, Value) -> string()
|
||||||
%% Tbl = atom()
|
%% Tbl = atom()
|
||||||
|
|||||||
Reference in New Issue
Block a user