Formatting fixed
This commit is contained in:
123
src/sqlite3.erl
123
src/sqlite3.erl
@@ -53,8 +53,9 @@
|
||||
%% To open multiple dbases on the same node use open/1 or open/2.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-type(result() :: {'ok', pid()} | 'ignore' | {'error', any()}).
|
||||
-spec(start_link/1::(atom()) -> result()).
|
||||
-type result() :: {'ok', pid()} | 'ignore' | {'error', any()}.
|
||||
|
||||
-spec start_link(atom()) -> result().
|
||||
|
||||
start_link(Db) ->
|
||||
?MODULE:open(Db, [{db, "./" ++ atom_to_list(Db) ++ ".db"}]).
|
||||
@@ -74,7 +75,7 @@ start_link(Db) ->
|
||||
%% To open multiple dbases on the same node use open/1 or open/2.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(start_link/2::(atom(), [{atom(), any()}]) -> result()).
|
||||
-spec start_link(atom(), [{atom(), any()}]) -> result().
|
||||
start_link(Db, Options) ->
|
||||
Opts = case proplists:get_value(db, Options) of
|
||||
undefined -> [{db, "./" ++ atom_to_list(Db) ++ ".db"} | Options];
|
||||
@@ -92,7 +93,7 @@ start_link(Db, Options) ->
|
||||
%% and drop_table/2.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(open/1::(atom()) -> result()).
|
||||
-spec open(atom()) -> result().
|
||||
open(Db) ->
|
||||
?MODULE:open(Db, [{db, "./" ++ atom_to_list(Db) ++ ".db"}]).
|
||||
|
||||
@@ -108,7 +109,7 @@ open(Db) ->
|
||||
%% and drop_table/2.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(open/2::(atom(), [{atom(), any()}]) -> result()).
|
||||
-spec open(atom(), [{atom(), any()}]) -> result().
|
||||
open(Db, Options) ->
|
||||
gen_server:start_link({local, Db}, ?MODULE, Options, []).
|
||||
|
||||
@@ -118,7 +119,7 @@ open(Db, Options) ->
|
||||
%% Closes the Db sqlite3 dbase.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(close/1::(atom()) -> 'ok').
|
||||
-spec close(atom()) -> 'ok'.
|
||||
close(Db) ->
|
||||
gen_server:call(Db, close).
|
||||
|
||||
@@ -128,7 +129,7 @@ close(Db) ->
|
||||
%% Closes the sqlite3 dbase.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(stop/0::() -> 'ok').
|
||||
-spec stop() -> 'ok'.
|
||||
stop() ->
|
||||
?MODULE:close(?MODULE).
|
||||
|
||||
@@ -138,7 +139,7 @@ stop() ->
|
||||
%% Executes the Sql statement directly.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(sql_exec/1::(string()) -> any()).
|
||||
-spec sql_exec(string()) -> any().
|
||||
sql_exec(SQL) ->
|
||||
?MODULE:sql_exec(?MODULE, SQL).
|
||||
|
||||
@@ -149,7 +150,7 @@ sql_exec(SQL) ->
|
||||
%% result of the Sql call.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(sql_exec/2::(atom(), string()) -> any()).
|
||||
-spec sql_exec(atom(), string()) -> any().
|
||||
sql_exec(Db, SQL) ->
|
||||
gen_server:call(Db, {sql_exec, SQL}).
|
||||
|
||||
@@ -163,7 +164,7 @@ sql_exec(Db, SQL) ->
|
||||
%% Returns the result of the create table call.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(create_table/2::(atom(), [tuple()]) -> any()).
|
||||
-spec create_table(atom(), [tuple()]) -> any().
|
||||
create_table(Tbl, Options) ->
|
||||
?MODULE:create_table(?MODULE, Tbl, Options).
|
||||
|
||||
@@ -177,7 +178,7 @@ create_table(Tbl, Options) ->
|
||||
%% Returns the result of the create table call.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(create_table/3::(atom(), atom(), [{atom(), any()}]) -> any()).
|
||||
-spec create_table(atom(), atom(), [{atom(), any()}]) -> any().
|
||||
create_table(Db, Tbl, Options) ->
|
||||
gen_server:call(Db, {create_table, Tbl, Options}).
|
||||
|
||||
@@ -187,7 +188,7 @@ create_table(Db, Tbl, Options) ->
|
||||
%% Returns a list of tables.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(list_tables/0::() -> [atom()]).
|
||||
-spec list_tables() -> [atom()].
|
||||
list_tables() ->
|
||||
?MODULE:list_tables(?MODULE).
|
||||
|
||||
@@ -197,7 +198,7 @@ list_tables() ->
|
||||
%% Returns a list of tables for Db.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(list_tables/1::(atom()) -> [atom()]).
|
||||
-spec list_tables(atom()) -> [atom()].
|
||||
list_tables(Db) ->
|
||||
gen_server:call(Db, list_tables).
|
||||
|
||||
@@ -207,7 +208,7 @@ list_tables(Db) ->
|
||||
%% Returns table schema for Tbl.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(table_info/1::(atom()) -> [any()]).
|
||||
-spec table_info(atom()) -> [any()].
|
||||
table_info(Tbl) ->
|
||||
?MODULE:table_info(?MODULE, Tbl).
|
||||
|
||||
@@ -217,7 +218,7 @@ table_info(Tbl) ->
|
||||
%% Returns table schema for Tbl in Db.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(table_info/2::(atom(), atom()) -> [any()]).
|
||||
-spec table_info(atom(), atom()) -> [any()].
|
||||
table_info(Db, Tbl) ->
|
||||
gen_server:call(Db, {table_info, Tbl}).
|
||||
|
||||
@@ -229,7 +230,7 @@ table_info(Db, Tbl) ->
|
||||
%% determined from table_info/2.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(write/2::(atom(), [{atom(), any()}]) -> any()).
|
||||
-spec write(atom(), [{atom(), any()}]) -> any().
|
||||
write(Tbl, Data) ->
|
||||
?MODULE:write(?MODULE, Tbl, Data).
|
||||
|
||||
@@ -241,38 +242,38 @@ write(Tbl, Data) ->
|
||||
%% same type as determined from table_info/3.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(write/3::(atom(), atom(), [{atom(), any()}]) -> any()).
|
||||
-spec write(atom(), atom(), [{atom(), any()}]) -> any().
|
||||
write(Db, Tbl, Data) ->
|
||||
gen_server:call(Db, {write, Tbl, Data}).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec update (Tbl::atom (), Key::atom (), Value, Data) -> Result
|
||||
%% Value = any ()
|
||||
%% Data = [{Column::atom (), Value::string () | integer () | float ()}]
|
||||
%% @spec update(Tbl :: atom(), Key :: atom(), Value, Data) -> Result
|
||||
%% Value = any()
|
||||
%% Data = [{Column :: atom(), Value :: string() | integer() | float()}]
|
||||
%% Result = {ok, ID} | Unknown
|
||||
%% Unknown = term ()
|
||||
%% Unknown = term()
|
||||
%% @doc
|
||||
%% Updates rows into Tbl table such that the Value matches the
|
||||
%% value in Key with Data. Returns ID of the first updated
|
||||
%% record.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
update (Tbl, Key, Value, Data) ->
|
||||
update(Tbl, Key, Value, Data) ->
|
||||
?MODULE:update(?MODULE, Tbl, Key, Value, Data).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec update (Db::atom (), Tbl::atom (), Key::atom (), Value, Data) -> Result
|
||||
%% Value = any ()
|
||||
%% Data = [{Column::atom (), Value::string () | integer () | float ()}]
|
||||
%% @spec update(Db::atom(), Tbl::atom(), Key::atom(), Value, Data) -> Result
|
||||
%% Value = any()
|
||||
%% Data = [{Column::atom(), Value::string() | integer() | float()}]
|
||||
%% Result = {ok, ID} | Unknown
|
||||
%% Unknown = term ()
|
||||
%% Unknown = term()
|
||||
%% @doc
|
||||
%% Updates rows into Tbl table in Db dbase such that the Value
|
||||
%% matches the value in Key with Data. Returns ID of the first
|
||||
%% updated record.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
update (Db, Tbl, Key, Value, Data) ->
|
||||
update(Db, Tbl, Key, Value, Data) ->
|
||||
gen_server:call(Db, {update, Tbl, Key, Value, Data}).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
@@ -284,7 +285,7 @@ update (Db, Tbl, Key, Value, Data) ->
|
||||
%% have the same type as determined from table_info/2.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(read/2::(atom(), {atom(), any()}) -> any()).
|
||||
-spec read(atom(), {atom(), any()}) -> any().
|
||||
read(Tbl, Key) ->
|
||||
?MODULE:read(?MODULE, Tbl, Key).
|
||||
|
||||
@@ -297,16 +298,16 @@ read(Tbl, Key) ->
|
||||
%% ColValue must have the same type as determined from table_info/3.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(read/3::(atom(), atom(), {atom(), any()}) -> any()).
|
||||
-spec read(atom(), atom(), {atom(), any()}) -> any().
|
||||
read(Db, 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 ()]
|
||||
%% @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
|
||||
@@ -314,7 +315,7 @@ read(Db, Tbl, Key) ->
|
||||
%% from table_info/3.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
read (Db, Tbl, Key, Columns) ->
|
||||
read(Db, Tbl, Key, Columns) ->
|
||||
gen_server:call(Db, {read, Tbl, Key, Columns}).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
@@ -326,7 +327,7 @@ read (Db, Tbl, Key, Columns) ->
|
||||
%% ColValue must have the same type as determined from table_info/3.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(delete/2::(atom(), {atom(), any()}) -> any()).
|
||||
-spec delete(atom(), {atom(), any()}) -> any().
|
||||
delete(Tbl, Key) ->
|
||||
?MODULE:delete(?MODULE, Tbl, Key).
|
||||
|
||||
@@ -339,7 +340,7 @@ delete(Tbl, Key) ->
|
||||
%% ColValue must have the same type as determined from table_info/3.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(delete/3::(atom(), atom(), {atom(), any()}) -> any()).
|
||||
-spec delete(atom(), atom(), {atom(), any()}) -> any().
|
||||
delete(Db, Tbl, Key) ->
|
||||
gen_server:call(Db, {delete, Tbl, Key}).
|
||||
|
||||
@@ -349,7 +350,7 @@ delete(Db, Tbl, Key) ->
|
||||
%% Drop the table Tbl.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(drop_table/1::(atom()) -> any()).
|
||||
-spec drop_table(atom()) -> any().
|
||||
drop_table(Tbl) ->
|
||||
?MODULE:drop_table(?MODULE, Tbl).
|
||||
|
||||
@@ -359,7 +360,7 @@ drop_table(Tbl) ->
|
||||
%% Drop the table Tbl from Db dbase.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(drop_table/2::(atom(), atom()) -> any()).
|
||||
-spec drop_table(atom(), atom()) -> any().
|
||||
drop_table(Db, Tbl) ->
|
||||
gen_server:call(Db, {drop_table, Tbl}).
|
||||
|
||||
@@ -371,7 +372,7 @@ drop_table(Db, Tbl) ->
|
||||
%%
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(create_function/3::(atom(), atom(), function()) -> any()).
|
||||
-spec create_function(atom(), atom(), function()) -> any().
|
||||
create_function(Db, FunctionName, Function) ->
|
||||
gen_server:call(Db, {create_function, FunctionName, Function}).
|
||||
|
||||
@@ -390,7 +391,7 @@ create_function(Db, FunctionName, Function) ->
|
||||
%% Reexported from sqlite3_lib:value_to_sql/1 for user convenience.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(value_to_sql_unsafe/1::(sql_value()) -> iodata()).
|
||||
-spec value_to_sql_unsafe(sql_value()) -> iodata().
|
||||
value_to_sql_unsafe(X) -> sqlite3_lib:value_to_sql_unsafe(X).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
@@ -405,7 +406,7 @@ value_to_sql_unsafe(X) -> sqlite3_lib:value_to_sql_unsafe(X).
|
||||
%% Reexported from sqlite3_lib:value_to_sql/1 for user convenience.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(value_to_sql/1::(sql_value()) -> iolist()).
|
||||
-spec value_to_sql(sql_value()) -> iolist().
|
||||
value_to_sql(X) -> sqlite3_lib:value_to_sql(X).
|
||||
|
||||
%%====================================================================
|
||||
@@ -421,19 +422,19 @@ value_to_sql(X) -> sqlite3_lib:value_to_sql(X).
|
||||
%% @end
|
||||
%% @hidden
|
||||
%%--------------------------------------------------------------------
|
||||
-type(init_return() :: {'ok', tuple()} | {'ok', tuple(), integer()} | 'ignore' | {'stop', any()}).
|
||||
-spec(init/1::([any()]) -> init_return()).
|
||||
-type init_return() :: {'ok', tuple()} | {'ok', tuple(), integer()} | 'ignore' | {'stop', any()}.
|
||||
-spec init([any()]) -> init_return().
|
||||
init(Options) ->
|
||||
Dbase = proplists:get_value(db, Options),
|
||||
{?MODULE, _, FileName} = code:get_object_code(?MODULE),
|
||||
SearchDir = filename:dirname(FileName),
|
||||
case erl_ddll:load(SearchDir, atom_to_list(?DRIVER_NAME)) of
|
||||
ok ->
|
||||
Port = open_port({spawn, string:join ([atom_to_list (?DRIVER_NAME), Dbase], " ")}, [binary]),
|
||||
Port = open_port({spawn, string:join([atom_to_list(?DRIVER_NAME), Dbase], " ")}, [binary]),
|
||||
{ok, #state{port = Port, ops = Options}};
|
||||
{error, Error} ->
|
||||
Msg = io_lib:format("Error loading ~p: ~p", [?DRIVER_NAME, erl_ddll:format_error(Error)]),
|
||||
{stop, lists:flatten (Msg)}
|
||||
{stop, lists:flatten(Msg)}
|
||||
end.
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
@@ -447,10 +448,10 @@ init(Options) ->
|
||||
%% @end
|
||||
%% @hidden
|
||||
%%--------------------------------------------------------------------
|
||||
-type(handle_call_return() :: {reply, any(), tuple()} | {reply, any(), tuple(), integer()} |
|
||||
-type handle_call_return() :: {reply, any(), tuple()} | {reply, any(), tuple(), integer()} |
|
||||
{noreply, tuple()} | {noreply, tuple(), integer()} |
|
||||
{stop, any(), any(), tuple()} | {stop, any(), tuple()}).
|
||||
-spec(handle_call/3::(any(), pid(), tuple()) -> handle_call_return()).
|
||||
{stop, any(), any(), tuple()} | {stop, any(), tuple()}.
|
||||
-spec handle_call(any(), pid(), tuple()) -> handle_call_return().
|
||||
handle_call(close, _From, State) ->
|
||||
Reply = ok,
|
||||
{stop, normal, Reply, State};
|
||||
@@ -460,7 +461,7 @@ handle_call({sql_exec, SQL}, _From, #state{port = Port} = State) ->
|
||||
handle_call(list_tables, _From, #state{port = Port} = State) ->
|
||||
Reply = exec(Port, {sql_exec, "select name from sqlite_master where type='table';"}),
|
||||
TableList = proplists:get_value(rows, Reply),
|
||||
TableNames = [erlang:list_to_atom (erlang:binary_to_list (Name)) || {Name} <- TableList],
|
||||
TableNames = [erlang:list_to_atom(erlang:binary_to_list(Name)) || {Name} <- TableList],
|
||||
{reply, TableNames, State};
|
||||
handle_call({table_info, Tbl}, _From, #state{port = Port} = State) ->
|
||||
% make sure we only get table info.
|
||||
@@ -481,8 +482,8 @@ handle_call({create_table, Tbl, Options}, _From, #state{port = Port} = State) ->
|
||||
Cmd = {sql_exec, SQL},
|
||||
Reply = exec(Port, Cmd),
|
||||
{reply, Reply, State};
|
||||
handle_call ({update, Tbl, Key, Value, Data}, _From, #state{port = Port} = State)->
|
||||
Reply = exec (Port, {sql_exec, sqlite3_lib:update_sql (Tbl, Key, Value, Data)}),
|
||||
handle_call({update, Tbl, Key, Value, Data}, _From, #state{port = Port} = State)->
|
||||
Reply = exec(Port, {sql_exec, sqlite3_lib:update_sql(Tbl, Key, Value, Data)}),
|
||||
{reply, Reply, State};
|
||||
handle_call({write, Tbl, Data}, _From, #state{port = Port} = State) ->
|
||||
% insert into t1 (data,num) values ('This is sample data',3);
|
||||
@@ -492,8 +493,8 @@ handle_call({read, Tbl, {Key, Value}}, _From, #state{port = Port} = State) ->
|
||||
% select * from Tbl where Key = Value;
|
||||
Reply = exec(Port, {sql_exec, sqlite3_lib:read_sql(Tbl, Key, Value)}),
|
||||
{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)}),
|
||||
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) ->
|
||||
% delete from Tbl where Key = Value;
|
||||
@@ -514,9 +515,9 @@ handle_call(_Request, _From, State) ->
|
||||
%% @end
|
||||
%% @hidden
|
||||
%%--------------------------------------------------------------------
|
||||
-type(handle_cast_return() :: {noreply, tuple()} | {noreply, tuple(), integer()} |
|
||||
{stop, any(), tuple()}).
|
||||
-spec(handle_cast/2::(any(), tuple()) -> handle_cast_return()).
|
||||
-type handle_cast_return() :: {noreply, tuple()} | {noreply, tuple(), integer()} |
|
||||
{stop, any(), tuple()}.
|
||||
-spec handle_cast(any(), tuple()) -> handle_cast_return().
|
||||
handle_cast(_Msg, State) ->
|
||||
{noreply, State}.
|
||||
|
||||
@@ -528,7 +529,7 @@ handle_cast(_Msg, State) ->
|
||||
%% @end
|
||||
%% @hidden
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(handle_info/2::(any(), tuple()) -> handle_cast_return()).
|
||||
-spec handle_info(any(), tuple()) -> handle_cast_return().
|
||||
handle_info(_Info, State) ->
|
||||
{noreply, State}.
|
||||
|
||||
@@ -541,7 +542,7 @@ handle_info(_Info, State) ->
|
||||
%% @end
|
||||
%% @hidden
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(terminate/2::(atom(), tuple()) -> atom()).
|
||||
-spec terminate(atom(), tuple()) -> atom().
|
||||
terminate(normal, #state{port = Port}) ->
|
||||
port_command(Port, term_to_binary({close, nop})),
|
||||
port_close(Port),
|
||||
@@ -582,7 +583,7 @@ wait_result(Port) ->
|
||||
end.
|
||||
|
||||
exec(_Port, {create_function, _FunctionName, _Function}) ->
|
||||
error_logger:error_report ([{application, sqlite3}, "NOT IMPL YET"]);
|
||||
error_logger:error_report([{application, sqlite3}, "NOT IMPL YET"]);
|
||||
%port_control(Port, ?SQL_CREATE_FUNCTION, list_to_binary(Cmd)),
|
||||
%wait_result(Port);
|
||||
|
||||
@@ -604,5 +605,5 @@ build_table_info([], Acc) ->
|
||||
build_table_info([[ColName, ColType] | Tl], Acc) ->
|
||||
build_table_info(Tl, [{list_to_atom(ColName), sqlite3_lib:col_type_to_atom(ColType)}| Acc]);
|
||||
build_table_info([[ColName, ColType, "PRIMARY", "KEY"] | Tl], Acc) ->
|
||||
build_table_info(Tl, [{list_to_atom(ColName), primary_key}| Acc]).
|
||||
build_table_info(Tl, [{list_to_atom(ColName), primary_key} | Acc]).
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
%% @doc Maps sqlite3 column type.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(col_type_to_string/1::(atom()) -> string()).
|
||||
-spec col_type_to_string(atom()) -> string().
|
||||
col_type_to_string(integer) ->
|
||||
"INTEGER";
|
||||
col_type_to_string(text) ->
|
||||
@@ -40,7 +40,7 @@ col_type_to_string(real) ->
|
||||
%% @doc Maps sqlite3 column type.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(col_type_to_atom/1::(string()) -> atom()).
|
||||
-spec col_type_to_atom(string()) -> atom().
|
||||
col_type_to_atom("INTEGER") ->
|
||||
integer;
|
||||
col_type_to_atom("TEXT") ->
|
||||
@@ -61,7 +61,7 @@ col_type_to_atom("REAL") ->
|
||||
%% single quotes (e.g. can be entered by users).
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(value_to_sql_unsafe/1::(sql_value()) -> iodata()).
|
||||
-spec value_to_sql_unsafe(sql_value()) -> iodata().
|
||||
value_to_sql_unsafe(X) ->
|
||||
if
|
||||
is_integer(X) -> integer_to_list(X);
|
||||
@@ -80,7 +80,7 @@ value_to_sql_unsafe(X) ->
|
||||
%% All single quotes (') will be replaced with ''.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(value_to_sql/1::(sql_value()) -> iolist()).
|
||||
-spec value_to_sql(sql_value()) -> iolist().
|
||||
value_to_sql(X) ->
|
||||
if
|
||||
is_integer(X) -> integer_to_list(X);
|
||||
@@ -95,7 +95,7 @@ value_to_sql(X) ->
|
||||
%% Creates the values portion of the sql stmt.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(write_value_sql/1::(any()) -> string()).
|
||||
-spec write_value_sql(any()) -> string().
|
||||
write_value_sql(Values) ->
|
||||
map_intersperse(fun value_to_sql/1, Values, ",").
|
||||
|
||||
@@ -104,17 +104,17 @@ write_value_sql(Values) ->
|
||||
%% @doc Creates the column/data stmt for SQL.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(write_col_sql/1::([atom()]) -> string()).
|
||||
-spec write_col_sql([atom()]) -> string().
|
||||
write_col_sql(Cols) ->
|
||||
map_intersperse(fun atom_to_list/1, Cols, ",").
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec replace (String) -> string ()
|
||||
%% String = string ()
|
||||
%% @spec replace(String) -> string()
|
||||
%% String = string()
|
||||
%% @doc Returns copy of String for which \" replaced with '
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec (replace/1::(string ()) -> string ()).
|
||||
-spec replace(string()) -> string().
|
||||
replace([]) -> [];
|
||||
replace([$" | T]) ->
|
||||
[$' | replace(T)];
|
||||
@@ -127,35 +127,35 @@ replace([H | T]) ->
|
||||
%% @doc Returns copy of IoData with all ' replaced by ''
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(escape/1::(iodata()) -> iodata()).
|
||||
-spec escape(iodata()) -> iodata().
|
||||
escape(IoData) -> re:replace(IoData, "'", "''", [global]).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec update_set_sql ([{Col, Value}]) -> string ()
|
||||
%% Col = atom ()
|
||||
%% Value = number () | atom () | string ()
|
||||
%% @spec update_set_sql([{Col, Value}]) -> string()
|
||||
%% Col = atom()
|
||||
%% Value = number() | atom() | string()
|
||||
%% @doc
|
||||
%% Creates update set stmt.
|
||||
%% Currently supports integer, double/float and strings.
|
||||
%% For strings \" replaced with '.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec (update_set_sql/1::(any ()) -> string ()).
|
||||
update_set_sql (Data) ->
|
||||
-spec update_set_sql(any()) -> string().
|
||||
update_set_sql(Data) ->
|
||||
ColValueToSqlFun =
|
||||
fun({Col, Value}) ->
|
||||
[atom_to_list (Col), " = ", value_to_sql(Value)]
|
||||
[atom_to_list(Col), " = ", value_to_sql(Value)]
|
||||
end,
|
||||
map_intersperse(ColValueToSqlFun, Data, ", ").
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec read_cols_sql (Columns::[atom ()]) -> string ()
|
||||
%% @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) ->
|
||||
-spec read_cols_sql([atom()]) -> string().
|
||||
read_cols_sql(Columns) ->
|
||||
map_intersperse(fun atom_to_list/1, Columns, ", ").
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
@@ -167,7 +167,7 @@ read_cols_sql (Columns) ->
|
||||
%% First column listed is considered the primary key.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(create_table_sql/2::(atom(), [{atom(), string()}]) -> string()).
|
||||
-spec create_table_sql(atom(), [{atom(), string()}]) -> string().
|
||||
create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
|
||||
CT = ["CREATE TABLE ", atom_to_list(Tbl), " "],
|
||||
Start = ["(", atom_to_list(ColName), " ", col_type_to_string(Type), " PRIMARY KEY, "],
|
||||
@@ -178,19 +178,19 @@ create_table_sql(Tbl, [{ColName, Type} | Tl]) ->
|
||||
[CT, Start, End].
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec update_sql (Tbl, Key, Value, Data) -> string ()
|
||||
%% Tbl = atom ()
|
||||
%% Key = atom ()
|
||||
%% Value = atom ()
|
||||
%% Data = [{ColName :: atom (), Value :: string () | integer () | float ()}]
|
||||
%% @spec update_sql(Tbl, Key, Value, Data) -> string()
|
||||
%% Tbl = atom()
|
||||
%% Key = atom()
|
||||
%% Value = atom()
|
||||
%% Data = [{ColName :: atom(), Value :: string() | integer() | float()}]
|
||||
%% @doc
|
||||
%% Using Key as the column name and Data as list of column names
|
||||
%% and values pairs it creates the proper update SQL stmt for the
|
||||
%% record with matching Value.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec (update_sql/4::(atom (), atom (), atom (), [{atom (), sql_value ()}]) -> string ()).
|
||||
update_sql (Tbl, Key, Value, Data) ->
|
||||
-spec update_sql(atom(), atom(), atom(), [{atom(), sql_value()}]) -> string().
|
||||
update_sql(Tbl, Key, Value, Data) ->
|
||||
["UPDATE ", atom_to_list(Tbl), " SET ", update_set_sql(Data),
|
||||
" WHERE ", atom_to_list(Key), " = ", value_to_sql(Value), ";"].
|
||||
|
||||
@@ -202,7 +202,7 @@ update_sql (Tbl, Key, Value, Data) ->
|
||||
%% proper insertion SQL stmt.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(write_sql/2::(atom(), [{atom(), sql_value()}]) -> string()).
|
||||
-spec write_sql(atom(), [{atom(), sql_value()}]) -> string().
|
||||
write_sql(Tbl, Data) ->
|
||||
{Cols, Values} = lists:unzip(Data),
|
||||
["INSERT INTO ", atom_to_list(Tbl), " (", sqlite3_lib:write_col_sql(Cols),
|
||||
@@ -217,24 +217,24 @@ write_sql(Tbl, Data) ->
|
||||
%% matching Value.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(read_sql/3::(atom(), atom(), sql_value()) -> string()).
|
||||
-spec read_sql(atom(), atom(), sql_value()) -> string().
|
||||
read_sql(Tbl, Key, Value) ->
|
||||
["SELECT * FROM ", atom_to_list(Tbl), " WHERE ", atom_to_list(Key),
|
||||
" = ", value_to_sql(Value), ";"].
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec read_sql (Tbl, Key, Value, Columns) -> string ()
|
||||
%% Tbl = atom ()
|
||||
%% Key = atom ()
|
||||
%% Value = string () | integer () | float ()
|
||||
%% Columns = [atom ()]
|
||||
%% @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) ->
|
||||
-spec read_sql(atom(), atom(), sql_value(), [atom()]) -> string().
|
||||
read_sql(Tbl, Key, Value, Columns) ->
|
||||
["SELECT ", sqlite3_lib:read_cols_sql(Columns), " FROM ",
|
||||
atom_to_list(Tbl), " WHERE ", atom_to_list(Key), " = ",
|
||||
value_to_sql(Value), ";"].
|
||||
@@ -248,7 +248,7 @@ read_sql (Tbl, Key, Value, Columns) ->
|
||||
%% matching Value then deletes that record.
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(delete_sql/3::(atom(), atom(), sql_value()) -> string()).
|
||||
-spec delete_sql(atom(), atom(), sql_value()) -> string().
|
||||
delete_sql(Tbl, Key, Value) ->
|
||||
["DELETE FROM ", atom_to_list(Tbl), " WHERE ", atom_to_list(Key),
|
||||
" = ", value_to_sql(Value), ";"].
|
||||
@@ -259,7 +259,7 @@ delete_sql(Tbl, Key, Value) ->
|
||||
%% @doc Drop the table Tbl from the database
|
||||
%% @end
|
||||
%%--------------------------------------------------------------------
|
||||
-spec(drop_table/1::(atom()) -> string()).
|
||||
-spec drop_table(atom()) -> string().
|
||||
drop_table(Tbl) ->
|
||||
["DROP TABLE ", atom_to_list(Tbl), ";"].
|
||||
|
||||
|
||||
Reference in New Issue
Block a user