diff --git a/src/sqlite3.erl b/src/sqlite3.erl index c68d8d5..635edee 100644 --- a/src/sqlite3.erl +++ b/src/sqlite3.erl @@ -189,7 +189,7 @@ sql_exec(Db, SQL, Params) -> gen_server:call(Db, {sql_bind_and_exec, SQL, Params}). %%-------------------------------------------------------------------- -%% @spec sql_exec_timeout(Db :: atom(), Sql :: iodata()) -> sql_result() +%% @spec sql_exec_timeout(Db :: atom(), Sql :: iodata(), Timeout :: timeout()) -> sql_result() %% @doc %% Executes the Sql statement directly on the Db database. Returns the %% result of the Sql call. @@ -200,7 +200,7 @@ sql_exec_timeout(Db, SQL, Timeout) -> gen_server:call(Db, {sql_exec, SQL}, Timeout). %%-------------------------------------------------------------------- -%% @spec sql_exec_timeout(Db :: atom(), Sql :: iodata(), Params) -> sql_result() +%% @spec sql_exec_timeout(Db :: atom(), Sql :: iodata(), Params, Timeout :: timeout()) -> sql_result() %% Params = [sql_value() | {atom() | string() | integer(), sql_value()}] %% @doc %% Executes the Sql statement with parameters Params directly on the Db @@ -427,7 +427,7 @@ table_info(Db, Tbl) -> gen_server:call(Db, {table_info, Tbl}). %%-------------------------------------------------------------------- -%% @spec table_info_timeout(Db :: atom(), Tbl :: atom()) -> table_info() +%% @spec table_info_timeout(Db :: atom(), Tbl :: atom(), Timeout :: timeout()) -> table_info() %% @doc %% Returns table schema for Tbl in Db. %% @end @@ -461,7 +461,7 @@ write(Db, Tbl, Data) -> gen_server:call(Db, {write, Tbl, Data}). %%-------------------------------------------------------------------- -%% @spec write_timeout(Db :: atom(), Tbl :: atom(), Data) -> sql_non_query_result() +%% @spec write_timeout(Db :: atom(), Tbl :: atom(), Data, Timeout :: timeout()) -> sql_non_query_result() %% Data = [{Column :: atom(), Value :: sql_value()}] %% @doc %% Write Data into Tbl table in Db database. Value must be of the @@ -497,7 +497,7 @@ write_many(Db, Tbl, Data) -> gen_server:call(Db, {write_many, Tbl, Data}). %%-------------------------------------------------------------------- -%% @spec write_many_timeout(Db :: atom(), Tbl :: atom(), Data) -> sql_non_query_result() +%% @spec write_many_timeout(Db :: atom(), Tbl :: atom(), Data, Timeout :: timeout()) -> sql_non_query_result() %% Data = [[{Column :: atom(), Value :: sql_value()}]] %% @doc %% Write all records in Data into table Tbl in database Db. Value @@ -530,12 +530,13 @@ update(Tbl, {Key, Value}, Data) -> %% matches the value in Key with Data. %% @end %%-------------------------------------------------------------------- --spec update(atom(), atom(), {atom(), sql_value()}, [{atom(), sql_value()}]) -> sql_non_query_result(). +-spec update(atom(), atom(), {atom(), sql_value()}, [{atom(), sql_value()}]) -> + sql_non_query_result(). update(Db, Tbl, {Key, Value}, Data) -> gen_server:call(Db, {update, Tbl, Key, Value, Data}). %%-------------------------------------------------------------------- -%% @spec update_timeout(Db :: atom(), Tbl :: atom(), {Key :: atom(), Value}, Data) -> sql_non_query_result() +%% @spec update_timeout(Db :: atom(), Tbl :: atom(), {Key :: atom(), Value}, Data, Timeout :: timeout()) -> sql_non_query_result() %% Value = sql_value() %% Data = [{Column :: atom(), Value :: sql_value()}] %% @doc @@ -543,7 +544,8 @@ update(Db, Tbl, {Key, Value}, Data) -> %% matches the value in Key with Data. %% @end %%-------------------------------------------------------------------- --spec update_timeout(atom(), atom(), {atom(), sql_value()}, [{atom(), sql_value()}], timeout()) -> sql_non_query_result(). +-spec update_timeout(atom(), atom(), {atom(), sql_value()}, [{atom(), sql_value()}], timeout()) -> + sql_non_query_result(). update_timeout(Db, Tbl, {Key, Value}, Data, Timeout) -> gen_server:call(Db, {update, Tbl, Key, Value, Data}, Timeout). @@ -558,7 +560,7 @@ read_all(Db, Tbl) -> gen_server:call(Db, {read, Tbl}). %%-------------------------------------------------------------------- -%% @spec read_all_timeout(Db :: atom(), Table :: atom()) -> sql_result() +%% @spec read_all_timeout(Db :: atom(), Table :: atom(), Timeout :: timeout()) -> sql_result() %% @doc %% Reads all rows from Table in Db. %% @end @@ -578,7 +580,7 @@ read_all(Db, Tbl, Columns) -> gen_server:call(Db, {read, Tbl, Columns}). %%-------------------------------------------------------------------- -%% @spec read_all_timeout(Db :: atom(), Table :: atom(), Columns :: [atom()]) -> sql_result() +%% @spec read_all_timeout(Db :: atom(), Table :: atom(), Columns :: [atom()], Timeout :: timeout()) -> sql_result() %% @doc %% Reads Columns in all rows from Table in Db. %% @end @@ -630,7 +632,7 @@ read(Db, Tbl, {Key, Value}, Columns) -> gen_server:call(Db, {read, Tbl, Key, Value, Columns}). %%-------------------------------------------------------------------- -%% @spec read_timeout(Db :: atom(), Tbl :: atom(), Key) -> sql_result() +%% @spec read_timeout(Db :: atom(), Tbl :: atom(), Key, Timeout :: timeout()) -> sql_result() %% Key = {Column :: atom(), Value :: sql_value()} %% @doc %% Reads a row from Tbl table in Db database such that the Value @@ -643,7 +645,7 @@ read_timeout(Db, Tbl, {Column, Value}, Timeout) -> gen_server:call(Db, {read, Tbl, Column, Value}, Timeout). %%-------------------------------------------------------------------- -%% @spec read_timeout(Db, Tbl, Key, Columns) -> [any()] +%% @spec read_timeout(Db, Tbl, Key, Columns, Timeout :: timeout()) -> [any()] %% Db = atom() %% Tbl = atom() %% Key = {Column :: atom(), Value :: sql_value()} @@ -672,7 +674,7 @@ delete(Tbl, Key) -> delete(?MODULE, Tbl, Key). %%-------------------------------------------------------------------- -%% @spec delete_timeout(Db :: atom(), Tbl :: atom(), Key) -> sql_non_query_result() +%% @spec delete_timeout(Db :: atom(), Tbl :: atom(), Key, Timeout :: timeout()) -> sql_non_query_result() %% Key = {Column :: atom(), Value :: sql_value()} %% @doc %% Delete a row from Tbl table in Db database such that the Value @@ -718,7 +720,7 @@ drop_table(Db, Tbl) -> gen_server:call(Db, {drop_table, Tbl}). %%-------------------------------------------------------------------- -%% @spec drop_table_timeout(Db :: atom(), Tbl :: atom()) -> sql_non_query_result() +%% @spec drop_table_timeout(Db :: atom(), Tbl :: atom(), Timeout :: timeout()) -> sql_non_query_result() %% @doc %% Drop the table Tbl from Db database. %% @end @@ -748,7 +750,7 @@ vacuum(Db) -> gen_server:call(Db, vacuum). %%-------------------------------------------------------------------- -%% @spec vacuum_timeout(Db :: atom()) -> sql_non_query_result() +%% @spec vacuum_timeout(Db :: atom(), Timeout :: timeout()) -> sql_non_query_result() %% @doc %% Vacuum the Db database. %% @end @@ -1183,15 +1185,40 @@ build_primary_key_constraint(Tail, Acc) -> %% {no_on_conflict, NoOnConflictClause}. %%-------------------------------------------------------------------- -%% @type sql_value() = number() | 'null' | iodata(). +%% @type sql_value() = null | number() | iodata() | {blob, binary()}. %% -%% Values accepted in SQL statements include numbers, atom 'null', -%% and io:iolist(). +%% Values accepted in SQL statements are atom 'null', numbers, +%% strings (represented as iodata()) and blobs. +%% @end +%% @type sql_type() = integer | text | double | blob | atom() | string(). +%% +%% Types of SQLite columns are represented by atoms 'integer', 'text', 'double', +%% 'blob'. Other atoms and strings may also be used (e.g. "VARCHAR(20)", 'smallint', etc.) +%% See [http://www.sqlite.org/datatype3.html]. +%% @end +%% @type pk_constraint() = autoincrement | desc | asc. +%% See {@link pk_constraints()}. +%% @type pk_constraints() = pk_constraint() | [pk_constraint()]. +%% See {@link column_constraint()}. +%% @type column_constraint() = non_null | primary_key | {primary_key, pk_constraints()} +%% | unique | {default, sql_value()}. +%% See {@link column_constraints()}. +%% @type column_constraints() = column_constraint() | [column_constraint()]. +%% See {@link table_info()}. +%% @type table_info() = [{atom(), sql_type()} | {atom(), sql_type(), column_constraints()}]. +%% +%% Describes the columns of an SQLite table: each tuple contains name, type and constraints (if any) +%% of one column. +%% @end +%% @type table_constraint() = {primary_key, [atom()]} | {unique, [atom()]}. +%% @type table_constraints() = table_constraint() | [table_constraint()]. +%% +%% Currently supported constraints for {@link table_info()} and {@link sqlite3:create_table/4}. %% @end %% @type sqlite_error() = {'error', integer(), string()}. %% %% Errors are reported by their SQLite result code -%% (http://www.sqlite.org/c3ref/c_busy_recovery.html) and a string containing +%% ([http://www.sqlite.org/c3ref/c_busy_recovery.html]) and a string containing %% English-language text that describes the error. %% @end %% @type sql_non_query_result() = ok | sqlite_error() | {rowid, integer()}. diff --git a/src/sqlite3_lib.erl b/src/sqlite3_lib.erl index ded64d7..f3e2979 100644 --- a/src/sqlite3_lib.erl +++ b/src/sqlite3_lib.erl @@ -54,7 +54,10 @@ col_type_to_atom("TEXT") -> col_type_to_atom("REAL") -> double; col_type_to_atom("BLOB") -> - blob. + blob; +col_type_to_atom(String) -> + list_to_atom(string:to_lower(String)). + %%-------------------------------------------------------------------- %% @spec value_to_sql_unsafe(Value :: sql_value()) -> iolist()