Types improved

This commit is contained in:
Alexey Romanov
2011-04-28 13:20:44 +04:00
parent 766ba3f682
commit 13cb7f8148
2 changed files with 50 additions and 20 deletions

View File

@@ -189,7 +189,7 @@ sql_exec(Db, SQL, Params) ->
gen_server:call(Db, {sql_bind_and_exec, 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 %% @doc
%% Executes the Sql statement directly on the Db database. Returns the %% Executes the Sql statement directly on the Db database. Returns the
%% result of the Sql call. %% result of the Sql call.
@@ -200,7 +200,7 @@ sql_exec_timeout(Db, SQL, Timeout) ->
gen_server:call(Db, {sql_exec, 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()}] %% Params = [sql_value() | {atom() | string() | integer(), sql_value()}]
%% @doc %% @doc
%% Executes the Sql statement with parameters Params directly on the Db %% 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}). 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 %% @doc
%% Returns table schema for Tbl in Db. %% Returns table schema for Tbl in Db.
%% @end %% @end
@@ -461,7 +461,7 @@ write(Db, Tbl, Data) ->
gen_server:call(Db, {write, 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()}] %% Data = [{Column :: atom(), Value :: sql_value()}]
%% @doc %% @doc
%% Write Data into Tbl table in Db database. Value must be of the %% 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}). 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()}]] %% Data = [[{Column :: atom(), Value :: sql_value()}]]
%% @doc %% @doc
%% Write all records in Data into table Tbl in database Db. Value %% 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. %% matches the value in Key with Data.
%% @end %% @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) -> update(Db, Tbl, {Key, Value}, Data) ->
gen_server:call(Db, {update, 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() %% Value = sql_value()
%% Data = [{Column :: atom(), Value :: sql_value()}] %% Data = [{Column :: atom(), Value :: sql_value()}]
%% @doc %% @doc
@@ -543,7 +544,8 @@ update(Db, Tbl, {Key, Value}, Data) ->
%% matches the value in Key with Data. %% matches the value in Key with Data.
%% @end %% @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) -> update_timeout(Db, Tbl, {Key, Value}, Data, Timeout) ->
gen_server:call(Db, {update, 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}). 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 %% @doc
%% Reads all rows from Table in Db. %% Reads all rows from Table in Db.
%% @end %% @end
@@ -578,7 +580,7 @@ read_all(Db, Tbl, Columns) ->
gen_server:call(Db, {read, 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 %% @doc
%% Reads Columns in all rows from Table in Db. %% Reads Columns in all rows from Table in Db.
%% @end %% @end
@@ -630,7 +632,7 @@ read(Db, Tbl, {Key, Value}, Columns) ->
gen_server:call(Db, {read, 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()} %% Key = {Column :: atom(), Value :: sql_value()}
%% @doc %% @doc
%% Reads a row from Tbl table in Db database such that the Value %% 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). 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() %% Db = atom()
%% Tbl = atom() %% Tbl = atom()
%% Key = {Column :: atom(), Value :: sql_value()} %% Key = {Column :: atom(), Value :: sql_value()}
@@ -672,7 +674,7 @@ delete(Tbl, Key) ->
delete(?MODULE, 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()} %% Key = {Column :: atom(), Value :: sql_value()}
%% @doc %% @doc
%% Delete a row from Tbl table in Db database such that the Value %% 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}). 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 %% @doc
%% Drop the table Tbl from Db database. %% Drop the table Tbl from Db database.
%% @end %% @end
@@ -748,7 +750,7 @@ vacuum(Db) ->
gen_server:call(Db, vacuum). 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 %% @doc
%% Vacuum the Db database. %% Vacuum the Db database.
%% @end %% @end
@@ -1183,15 +1185,40 @@ build_primary_key_constraint(Tail, Acc) ->
%% {no_on_conflict, NoOnConflictClause}. %% {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', %% Values accepted in SQL statements are atom 'null', numbers,
%% and io:iolist(). %% 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 %% @end
%% @type sqlite_error() = {'error', integer(), string()}. %% @type sqlite_error() = {'error', integer(), string()}.
%% %%
%% Errors are reported by their SQLite result code %% 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. %% English-language text that describes the error.
%% @end %% @end
%% @type sql_non_query_result() = ok | sqlite_error() | {rowid, integer()}. %% @type sql_non_query_result() = ok | sqlite_error() | {rowid, integer()}.

View File

@@ -54,7 +54,10 @@ col_type_to_atom("TEXT") ->
col_type_to_atom("REAL") -> col_type_to_atom("REAL") ->
double; double;
col_type_to_atom("BLOB") -> 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() %% @spec value_to_sql_unsafe(Value :: sql_value()) -> iolist()