Merge pull request #72 from mmzeeman/cleanup-connection-and-statement-types
Cleanup connection and statement types
This commit is contained in:
@@ -310,14 +310,17 @@ update_callback(void *arg, int sqlite_operation_type, char const *sqlite_databas
|
||||
}
|
||||
|
||||
static ERL_NIF_TERM
|
||||
do_set_update_hook(ErlNifEnv *env, esqlite_connection *db, const ERL_NIF_TERM arg)
|
||||
do_set_update_hook(ErlNifEnv *env, esqlite_connection *conn, const ERL_NIF_TERM arg)
|
||||
{
|
||||
if(!enif_get_local_pid(env, arg, &db->notification_pid))
|
||||
if(!enif_get_local_pid(env, arg, &conn->notification_pid)) {
|
||||
return make_error_tuple(env, "invalid_pid");
|
||||
}
|
||||
|
||||
sqlite3_update_hook(db->db, NULL, NULL);
|
||||
if(sqlite3_update_hook(db->db, update_callback, db) != SQLITE_OK)
|
||||
return make_error_tuple(env, "sqlite3_update_hook_fail");
|
||||
if(!conn->db) {
|
||||
return make_error_tuple(env, "closed");
|
||||
}
|
||||
|
||||
sqlite3_update_hook(conn->db, update_callback, conn);
|
||||
|
||||
return make_atom(env, "ok");
|
||||
}
|
||||
@@ -331,8 +334,7 @@ do_exec(ErlNifEnv *env, esqlite_connection *conn, const ERL_NIF_TERM arg)
|
||||
int rc;
|
||||
ERL_NIF_TERM eos = enif_make_int(env, 0);
|
||||
|
||||
enif_inspect_iolist_as_binary(env,
|
||||
enif_make_list2(env, arg, eos), &bin);
|
||||
enif_inspect_iolist_as_binary(env, enif_make_list2(env, arg, eos), &bin);
|
||||
|
||||
rc = sqlite3_exec(conn->db, (char *) bin.data, NULL, NULL, NULL);
|
||||
if(rc != SQLITE_OK)
|
||||
@@ -347,6 +349,10 @@ do_exec(ErlNifEnv *env, esqlite_connection *conn, const ERL_NIF_TERM arg)
|
||||
static ERL_NIF_TERM
|
||||
do_changes(ErlNifEnv *env, esqlite_connection *conn, const ERL_NIF_TERM arg)
|
||||
{
|
||||
if(!conn->db) {
|
||||
return make_error_tuple(env, "closed");
|
||||
}
|
||||
|
||||
int changes = sqlite3_changes(conn->db);
|
||||
|
||||
ERL_NIF_TERM changes_term = enif_make_int64(env, changes);
|
||||
@@ -374,15 +380,24 @@ do_insert(ErlNifEnv *env, esqlite_connection *conn, const ERL_NIF_TERM arg)
|
||||
return make_ok_tuple(env, last_rowid_term);
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the last inserted rowid
|
||||
*/
|
||||
static ERL_NIF_TERM
|
||||
do_last_insert_rowid(ErlNifEnv *env, esqlite_connection *conn)
|
||||
{
|
||||
if(!conn->db) {
|
||||
return make_error_tuple(env, "closed");
|
||||
}
|
||||
|
||||
sqlite3_int64 last_rowid = sqlite3_last_insert_rowid(conn->db);
|
||||
ERL_NIF_TERM last_rowid_term = enif_make_int64(env, last_rowid);
|
||||
|
||||
return make_ok_tuple(env, last_rowid_term);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compile a sql statement
|
||||
*/
|
||||
static ERL_NIF_TERM
|
||||
do_prepare(ErlNifEnv *env, esqlite_connection *conn, const ERL_NIF_TERM arg)
|
||||
@@ -503,11 +518,15 @@ do_bind(ErlNifEnv *env, sqlite3 *db, sqlite3_stmt *stmt, const ERL_NIF_TERM arg)
|
||||
static ERL_NIF_TERM
|
||||
do_get_autocommit(ErlNifEnv *env, esqlite_connection *conn)
|
||||
{
|
||||
if(!conn->db) {
|
||||
return make_error_tuple(env, "closed");
|
||||
}
|
||||
|
||||
if(sqlite3_get_autocommit(conn->db) != 0) {
|
||||
return make_atom(env, "true");
|
||||
} else {
|
||||
return make_atom(env, "false");
|
||||
}
|
||||
|
||||
return make_atom(env, "false");
|
||||
}
|
||||
|
||||
static ERL_NIF_TERM
|
||||
@@ -738,10 +757,10 @@ evaluate_command(esqlite_command *cmd, esqlite_connection *conn)
|
||||
return do_column_types(cmd->env, stmt->statement);
|
||||
case cmd_close:
|
||||
return do_close(cmd->env, conn, cmd->arg);
|
||||
case cmd_insert:
|
||||
return do_insert(cmd->env, conn, cmd->arg);
|
||||
case cmd_last_insert_rowid:
|
||||
return do_last_insert_rowid(cmd->env, conn);
|
||||
case cmd_insert:
|
||||
return do_insert(cmd->env, conn, cmd->arg);
|
||||
case cmd_get_autocommit:
|
||||
return do_get_autocommit(cmd->env, conn);
|
||||
default:
|
||||
|
||||
@@ -24,6 +24,9 @@ CFlags =
|
||||
|
||||
[
|
||||
{require_min_otp_vsn, "21"},
|
||||
|
||||
{xref_checks, [undefined_function_calls]},
|
||||
|
||||
{port_env, [
|
||||
%% Default darwin ldflags causes loading of system sqlite. Removed -bundle flag.
|
||||
{"darwin", "DRV_LDFLAGS",
|
||||
@@ -46,5 +49,14 @@ CFlags =
|
||||
{provider_hooks,
|
||||
[{post,
|
||||
[{compile, {pc, compile}},
|
||||
{clean, {pc, clean}}]}]}
|
||||
{clean, {pc, clean}}]}]},
|
||||
|
||||
{dialyzer, [
|
||||
{warnings, [
|
||||
unmatched_returns,
|
||||
error_handling,
|
||||
race_conditions
|
||||
% underspecs
|
||||
]}
|
||||
]}
|
||||
].
|
||||
|
||||
135
src/esqlite3.erl
135
src/esqlite3.erl
@@ -50,8 +50,17 @@
|
||||
|
||||
%%
|
||||
|
||||
-type connection() :: {connection, reference(), term()}.
|
||||
-type statement() :: {statement, term(), connection()}.
|
||||
-record(connection, {
|
||||
raw_connection :: esqlite_nif:raw_connection()
|
||||
}).
|
||||
|
||||
-record(statement, {
|
||||
raw_statement :: esqlite_nif:raw_statement(),
|
||||
raw_connection :: esqlite_nif:raw_connection()
|
||||
}).
|
||||
|
||||
-type connection() :: #connection{}.
|
||||
-type statement() :: #statement{}.
|
||||
-type sql() :: iodata().
|
||||
|
||||
%% erlang -> sqlite type conversions
|
||||
@@ -93,13 +102,13 @@ open(Filename) ->
|
||||
%%
|
||||
-spec open(string(), timeout()) -> {ok, connection()} | {error, _}.
|
||||
open(Filename, Timeout) ->
|
||||
{ok, Connection} = esqlite3_nif:start(),
|
||||
{ok, RawConnection} = esqlite3_nif:start(),
|
||||
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:open(Connection, Ref, self(), Filename),
|
||||
case receive_answer(Connection, Ref, Timeout) of
|
||||
ok = esqlite3_nif:open(RawConnection, Ref, self(), Filename),
|
||||
case receive_answer(RawConnection, Ref, Timeout) of
|
||||
ok ->
|
||||
{ok, {connection, make_ref(), Connection}};
|
||||
{ok, #connection{raw_connection=RawConnection}};
|
||||
{error, _Msg}=Error ->
|
||||
Error
|
||||
end.
|
||||
@@ -119,10 +128,10 @@ set_update_hook(Pid, Connection) ->
|
||||
|
||||
%% @doc Same as set_update_hook, but with an additional timeout parameter.
|
||||
-spec set_update_hook(pid(), connection(), timeout()) -> ok | {error, term()}.
|
||||
set_update_hook(Pid, {connection, _Ref, Connection}, Timeout) ->
|
||||
set_update_hook(Pid, #connection{raw_connection=RawConnection}, Timeout) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:set_update_hook(Connection, Ref, self(), Pid),
|
||||
receive_answer(Connection, Ref, Timeout).
|
||||
ok = esqlite3_nif:set_update_hook(RawConnection, Ref, self(), Pid),
|
||||
receive_answer(RawConnection, Ref, Timeout).
|
||||
|
||||
%% @doc Execute a sql statement, returns a list with tuples.
|
||||
-spec q(sql(), connection()) -> list(tuple()) | {error, term()}.
|
||||
@@ -341,20 +350,24 @@ exec(Sql, Connection) ->
|
||||
exec(Sql, [], Connection, ?DEFAULT_TIMEOUT).
|
||||
|
||||
-spec exec(sql(), list(cell_type()) | connection(), connection() | timeout()) -> ok | {error, _}.
|
||||
exec(Sql, {connection, _,_}=Connection, Timeout) ->
|
||||
exec(Sql, #connection{}=Connection, Timeout) ->
|
||||
exec(Sql, [], Connection, Timeout);
|
||||
exec(Sql, Params, Connection) ->
|
||||
exec(Sql, Params, #connection{}=Connection) ->
|
||||
exec(Sql, Params, Connection, ?DEFAULT_TIMEOUT).
|
||||
|
||||
-spec exec(sql(), list(cell_type()), connection(), timeout()) -> ok | {error, _}.
|
||||
exec(Sql, [], {connection, _Ref, Connection}, Timeout) ->
|
||||
exec(Sql, [], #connection{raw_connection=RawConnection}, Timeout) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:exec(Connection, Ref, self(), Sql),
|
||||
receive_answer(Connection, Ref, Timeout);
|
||||
ok = esqlite3_nif:exec(RawConnection, Ref, self(), Sql),
|
||||
receive_answer(RawConnection, Ref, Timeout);
|
||||
exec(Sql, Params, Connection, Timeout) ->
|
||||
{ok, Statement} = prepare(Sql, Connection, Timeout),
|
||||
bind(Statement, Params),
|
||||
step(Statement, Timeout).
|
||||
case bind(Statement, Params) of
|
||||
ok ->
|
||||
step(Statement, Timeout);
|
||||
{error, _}=Error ->
|
||||
Error
|
||||
end.
|
||||
|
||||
|
||||
%% @doc Return the number of affected rows of last statement.
|
||||
@@ -363,10 +376,10 @@ changes(Connection) ->
|
||||
changes(Connection, ?DEFAULT_TIMEOUT).
|
||||
|
||||
-spec changes(connection(), timeout()) -> non_neg_integer().
|
||||
changes({connection, _Ref, Connection}, Timeout) ->
|
||||
changes(#connection{raw_connection=RawConnection}, Timeout) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:changes(Connection, Ref, self()),
|
||||
receive_answer(Connection, Ref, Timeout).
|
||||
ok = esqlite3_nif:changes(RawConnection, Ref, self()),
|
||||
receive_answer(RawConnection, Ref, Timeout).
|
||||
|
||||
%% @doc Insert records, returns the last rowid.
|
||||
%%
|
||||
@@ -376,10 +389,10 @@ insert(Sql, Connection) ->
|
||||
|
||||
%% @doc Like insert/2, but with extra timeout parameter.
|
||||
-spec insert(sql(), connection(), timeout()) -> {ok, rowid()} | {error, _}.
|
||||
insert(Sql, {connection, _Ref, Connection}, Timeout) ->
|
||||
insert(Sql, #connection{raw_connection=RawConnection}, Timeout) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:insert(Connection, Ref, self(), Sql),
|
||||
receive_answer(Connection, Ref, Timeout).
|
||||
ok = esqlite3_nif:insert(RawConnection, Ref, self(), Sql),
|
||||
receive_answer(RawConnection, Ref, Timeout).
|
||||
|
||||
%% @doc Get the last insert rowid, using the default timeout.
|
||||
%%
|
||||
@@ -389,11 +402,11 @@ last_insert_rowid(Connection) ->
|
||||
|
||||
%% @doc Get the last insert rowid.
|
||||
%%
|
||||
-spec last_insert_rowid(connection(), timeout()) -> {ok, integer()} | {error, _}.
|
||||
last_insert_rowid({connection, _Ref, Connection}, Timeout) ->
|
||||
-spec last_insert_rowid(connection(), timeout()) -> {ok, rowid()} | {error, _}.
|
||||
last_insert_rowid(#connection{raw_connection=RawConnection}, Timeout) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:last_insert_rowid(Connection, Ref, self()),
|
||||
receive_answer(Connection, Ref, Timeout).
|
||||
ok = esqlite3_nif:last_insert_rowid(RawConnection, Ref, self()),
|
||||
receive_answer(RawConnection, Ref, Timeout).
|
||||
|
||||
%% @doc Get autocommit
|
||||
%% @doc Check if the connection is in auto-commit mode.
|
||||
@@ -405,10 +418,10 @@ get_autocommit(Connection) ->
|
||||
|
||||
%% @doc Like autocommit/1, but with an extra timeout attribute.
|
||||
-spec get_autocommit(connection(), timeout()) -> true | false.
|
||||
get_autocommit({connection, _Ref, Connection}, Timeout) ->
|
||||
get_autocommit(#connection{raw_connection=RawConnection}, Timeout) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:get_autocommit(Connection, Ref, self()),
|
||||
receive_answer(Connection, Ref, Timeout).
|
||||
ok = esqlite3_nif:get_autocommit(RawConnection, Ref, self()),
|
||||
receive_answer(RawConnection, Ref, Timeout).
|
||||
|
||||
%% @doc Compile a SQL statement. Returns a cached compiled statement which can be used in
|
||||
%% queries.
|
||||
@@ -419,12 +432,14 @@ prepare(Sql, Connection) ->
|
||||
|
||||
%% @doc Like prepare/2, but with an extra timeout value.
|
||||
-spec prepare(sql(), connection(), timeout()) -> {ok, statement()} | {error, _}.
|
||||
prepare(Sql, {connection, _Ref, Connection}=C, Timeout) ->
|
||||
prepare(Sql, #connection{raw_connection=RawConnection}, Timeout) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:prepare(Connection, Ref, self(), Sql),
|
||||
case receive_answer(Connection, Ref, Timeout) of
|
||||
{ok, Stmt} -> {ok, {statement, Stmt, C}};
|
||||
Else -> Else
|
||||
ok = esqlite3_nif:prepare(RawConnection, Ref, self(), Sql),
|
||||
case receive_answer(RawConnection, Ref, Timeout) of
|
||||
{ok, Stmt} ->
|
||||
{ok, #statement{raw_statement=Stmt, raw_connection=RawConnection}};
|
||||
Else ->
|
||||
Else
|
||||
end.
|
||||
|
||||
%% @doc Step
|
||||
@@ -436,10 +451,10 @@ step(Stmt) ->
|
||||
%% @doc
|
||||
%%
|
||||
-spec step(statement(), timeout()) -> tuple() | '$busy' | '$done'.
|
||||
step({statement, Stmt, {connection, _, Conn}}, Timeout) ->
|
||||
step(#statement{raw_statement=RawStatement, raw_connection=RawConnection}, Timeout) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:multi_step(Conn, Stmt, 1, Ref, self()),
|
||||
case receive_answer(Conn, Ref, Timeout) of
|
||||
ok = esqlite3_nif:multi_step(RawConnection, RawStatement, 1, Ref, self()),
|
||||
case receive_answer(RawConnection, Ref, Timeout) of
|
||||
{rows, [Row | []]} -> {row, Row};
|
||||
{'$done', []} -> '$done';
|
||||
{'$busy', []} -> '$busy';
|
||||
@@ -452,19 +467,19 @@ step({statement, Stmt, {connection, _, Conn}}, Timeout) ->
|
||||
{rows, list(tuple())} |
|
||||
{'$busy', list(tuple())} |
|
||||
{'$done', list(tuple())} |
|
||||
{error, term()}.
|
||||
multi_step({statement, Stmt, {connection, _, Conn}}, ChunkSize, Timeout) ->
|
||||
{error, _}.
|
||||
multi_step(#statement{raw_statement=RawStatement, raw_connection=RawConnection}, ChunkSize, Timeout) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:multi_step(Conn, Stmt, ChunkSize, Ref, self()),
|
||||
receive_answer(Conn, Ref, Timeout).
|
||||
ok = esqlite3_nif:multi_step(RawConnection, RawStatement, ChunkSize, Ref, self()),
|
||||
receive_answer(RawConnection, Ref, Timeout).
|
||||
|
||||
%% @doc Reset the prepared statement back to its initial state.
|
||||
%%
|
||||
-spec reset(statement()) -> ok | {error, _}.
|
||||
reset({statement, Stmt, {connection, _, Conn}}) ->
|
||||
reset(#statement{raw_statement=RawStatement, raw_connection=RawConnection}) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:reset(Conn, Stmt, Ref, self()),
|
||||
receive_answer(Conn, Ref, ?DEFAULT_TIMEOUT).
|
||||
ok = esqlite3_nif:reset(RawConnection, RawStatement, Ref, self()),
|
||||
receive_answer(RawConnection, Ref, ?DEFAULT_TIMEOUT).
|
||||
|
||||
%% @doc Bind values to prepared statements
|
||||
%%
|
||||
@@ -474,10 +489,10 @@ bind(Stmt, Args) ->
|
||||
|
||||
%% @doc Bind values to prepared statements
|
||||
-spec bind(statement(), list(cell_type()), timeout()) -> ok | {error, _}.
|
||||
bind({statement, Stmt, {connection, _, Conn}}, Args, Timeout) ->
|
||||
bind(#statement{raw_statement=RawStatement, raw_connection=RawConnection}, Args, Timeout) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:bind(Conn, Stmt, Ref, self(), Args),
|
||||
receive_answer(Conn, Ref, Timeout).
|
||||
ok = esqlite3_nif:bind(RawConnection, RawStatement, Ref, self(), Args),
|
||||
receive_answer(RawConnection, Ref, Timeout).
|
||||
|
||||
%% @doc Return the column names of the prepared statement.
|
||||
%%
|
||||
@@ -486,22 +501,22 @@ column_names(Stmt) ->
|
||||
column_names(Stmt, ?DEFAULT_TIMEOUT).
|
||||
|
||||
-spec column_names(statement(), timeout()) -> {atom()}.
|
||||
column_names({statement, Stmt, {connection, _, Conn}}, Timeout) ->
|
||||
column_names(#statement{raw_statement=RawStatement, raw_connection=RawConnection}, Timeout) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:column_names(Conn, Stmt, Ref, self()),
|
||||
receive_answer(Conn, Ref, Timeout).
|
||||
ok = esqlite3_nif:column_names(RawConnection, RawStatement, Ref, self()),
|
||||
receive_answer(RawConnection, Ref, Timeout).
|
||||
|
||||
%% @doc Return the column types of the prepared statement.
|
||||
%%
|
||||
-spec column_types(statement()) -> {atom()}.
|
||||
column_types(Stmt) ->
|
||||
column_types(Stmt, ?DEFAULT_TIMEOUT).
|
||||
column_types(Statement) ->
|
||||
column_types(Statement, ?DEFAULT_TIMEOUT).
|
||||
|
||||
-spec column_types(statement(), timeout()) -> {atom()}.
|
||||
column_types({statement, Stmt, {connection, _, Conn}}, Timeout) ->
|
||||
column_types(#statement{raw_statement=RawStatement, raw_connection=RawConnection}, Timeout) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:column_types(Conn, Stmt, Ref, self()),
|
||||
receive_answer(Conn, Ref, Timeout).
|
||||
ok = esqlite3_nif:column_types(RawConnection, RawStatement, Ref, self()),
|
||||
receive_answer(RawConnection, Ref, Timeout).
|
||||
|
||||
%% @doc Close the database
|
||||
-spec close(connection()) -> ok | {error, _}.
|
||||
@@ -510,10 +525,10 @@ close(Connection) ->
|
||||
|
||||
%% @doc Close the database
|
||||
-spec close(connection(), timeout()) -> ok | {error, _}.
|
||||
close({connection, _Ref, Connection}, Timeout) ->
|
||||
close(#connection{raw_connection=RawConnection}, Timeout) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:close(Connection, Ref, self()),
|
||||
receive_answer(Connection, Ref, Timeout).
|
||||
ok = esqlite3_nif:close(RawConnection, Ref, self()),
|
||||
receive_answer(RawConnection, Ref, Timeout).
|
||||
|
||||
|
||||
%% @doc Flush any stale answers left in the mailbox of the current process.
|
||||
@@ -527,12 +542,12 @@ flush() ->
|
||||
|
||||
%% Internal functions
|
||||
|
||||
receive_answer(Connection, Ref, Timeout) ->
|
||||
receive_answer(RawConnection, Ref, Timeout) ->
|
||||
receive
|
||||
{esqlite3, Ref, Resp} -> Resp
|
||||
after
|
||||
Timeout ->
|
||||
ok = esqlite3_nif:interrupt(Connection),
|
||||
ok = esqlite3_nif:interrupt(RawConnection),
|
||||
throw({error, timeout, Ref})
|
||||
end.
|
||||
|
||||
|
||||
@@ -40,6 +40,11 @@
|
||||
close/3
|
||||
]).
|
||||
|
||||
-type raw_connection() :: reference().
|
||||
-type raw_statement() :: reference().
|
||||
|
||||
-export_type([raw_connection/0, raw_statement/0]).
|
||||
|
||||
-on_load(init/0).
|
||||
|
||||
init() ->
|
||||
@@ -52,7 +57,7 @@ init() ->
|
||||
|
||||
%% @doc Start a low level thread which will can handle sqlite3 calls.
|
||||
%%
|
||||
-spec start() -> {ok, esqlite:connection()} | {error, any()}.
|
||||
-spec start() -> {ok, raw_connection()} | {error, _}.
|
||||
start() ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
@@ -61,10 +66,11 @@ start() ->
|
||||
%% Sends an asynchronous open command over the connection and returns
|
||||
%% ok immediately. When the database is opened
|
||||
%%
|
||||
-spec open(esqlite:connection(), reference(), pid(), string()) -> ok | {error, any()}.
|
||||
-spec open(raw_connection(), reference(), pid(), string()) -> ok | {error, _}.
|
||||
open(_Db, _Ref, _Dest, _Filename) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
-spec set_update_hook(raw_connection(), reference(), pid(), pid()) -> ok | {error, _}.
|
||||
set_update_hook(_Db, _Ref, _Dest, _Pid) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
@@ -76,7 +82,7 @@ set_update_hook(_Db, _Ref, _Dest, _Pid) ->
|
||||
%% When the statement is executed Dest will receive message {Ref, answer()}
|
||||
%% with answer() integer | {error, reason()}
|
||||
%%
|
||||
-spec exec(esqlite:connection(), reference(), pid(), string()) -> ok | {error, any()}.
|
||||
-spec exec(raw_connection(), reference(), pid(), string()) -> ok | {error, _}.
|
||||
exec(_Db, _Ref, _Dest, _Sql) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
@@ -84,76 +90,78 @@ exec(_Db, _Ref, _Dest, _Sql) ->
|
||||
%%
|
||||
%% When the statement is executed Dest will receive message {Ref, answer()}
|
||||
%% with answer() integer | {error, reason()}
|
||||
%%
|
||||
-spec changes(raw_connection(), reference(), pid()) -> ok | {error, _}.
|
||||
changes(_Db, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc
|
||||
%%
|
||||
-spec prepare(esqlite:connection(), reference(), pid(), string()) -> ok | {error, any()}.
|
||||
-spec prepare(raw_connection(), reference(), pid(), string()) -> ok | {error, _}.
|
||||
prepare(_Db, _Ref, _Dest, _Sql) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc
|
||||
%%
|
||||
-spec multi_step(esqlite:connection(), esqlite:statement(), pos_integer(), reference(), pid()) -> ok | {error, any()}.
|
||||
-spec multi_step(raw_connection(), raw_statement(), pos_integer(), reference(), pid()) -> ok | {error, _}.
|
||||
multi_step(_Db, _Stmt, _Chunk_Size, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc
|
||||
%%
|
||||
-spec reset(esqlite:connection(), esqlite:statement(), reference(), pid()) -> ok | {error, any()}.
|
||||
-spec reset(raw_connection(), raw_statement(), reference(), pid()) -> ok | {error, _}.
|
||||
reset(_Db, _Stmt, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc
|
||||
%%
|
||||
-spec finalize(esqlite:connection(), esqlite:statement(), reference(), pid()) -> ok | {error, any()}.
|
||||
-spec finalize(raw_connection(), raw_statement(), reference(), pid()) -> ok | {error, _}.
|
||||
finalize(_Db, _Stmt, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Bind parameters to a prepared statement.
|
||||
%%
|
||||
-spec bind(esqlite:connection(), esqlite:statement(), reference(), pid(), list(any())) -> ok | {error, any()}.
|
||||
-spec bind(raw_connection(), raw_statement(), reference(), pid(), list(any())) -> ok | {error, _}.
|
||||
bind(_Db, _Stmt, _Ref, _Dest, _Args) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Retrieve the column names of the prepared statement
|
||||
%%
|
||||
-spec column_names(esqlite:connection(), esqlite:statement(), reference(), pid()) -> ok | {error, any()}.
|
||||
-spec column_names(raw_connection(), raw_statement(), reference(), pid()) -> ok | {error, _}.
|
||||
column_names(_Db, _Stmt, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Retrieve the column types of the prepared statement
|
||||
%%
|
||||
-spec column_types(esqlite:connection(), esqlite:statement(), reference(), pid()) -> ok | {error, any()}.
|
||||
-spec column_types(raw_connection(), raw_statement(), reference(), pid()) -> ok | {error, _}.
|
||||
column_types(_Db, _Stmt, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Interrupt all active queries.
|
||||
-spec interrupt(raw_connection()) -> ok.
|
||||
interrupt(_Db) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Close the connection.
|
||||
%%
|
||||
-spec close(esqlite:connection(), reference(), pid()) -> ok | {error, any()}.
|
||||
-spec close(raw_connection(), reference(), pid()) -> ok | {error, _}.
|
||||
close(_Db, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Insert record
|
||||
%%
|
||||
-spec insert(esqlite:connection(), reference(), pid(), esqlite:sql()) -> ok | {error, any()}.
|
||||
-spec insert(raw_connection(), reference(), pid(), esqlite:sql()) -> ok | {error, _}.
|
||||
insert(_Db, _Ref, _Dest, _Sql) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Get the last insert rowid.
|
||||
%%
|
||||
%% @spec insert(connection(), Ref::reference(), Dest::pid()) -> {ok, integer()} | {error, message()}
|
||||
-spec last_insert_rowid(raw_connection(), reference(), pid()) -> ok | {error, _}.
|
||||
last_insert_rowid(_Db, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
%% @doc Get automcommit
|
||||
%%
|
||||
-spec get_autocommit(esqlite:connection(), reference(), pid()) -> ok | {error, any()}.
|
||||
-spec get_autocommit(raw_connection(), reference(), pid()) -> ok | {error, _}.
|
||||
get_autocommit(_Db, _Ref, _Dest) ->
|
||||
erlang:nif_error(nif_library_not_loaded).
|
||||
|
||||
|
||||
@@ -10,6 +10,21 @@ open_single_database_test() ->
|
||||
{ok, _C1} = esqlite3:open("test.db"),
|
||||
ok.
|
||||
|
||||
close_test() ->
|
||||
%% Open and close the database immediately
|
||||
{ok, C} = esqlite3:open(":memory:"),
|
||||
ok = esqlite3:close(C),
|
||||
|
||||
%% Check if functions still return sensible values.
|
||||
{error, closed} = esqlite3:set_update_hook(self(), C),
|
||||
{error, closed} = esqlite3:changes(C),
|
||||
{error, closed} = esqlite3:get_autocommit(C),
|
||||
{error, closed} = esqlite3:last_insert_rowid(C),
|
||||
|
||||
{error, _} = esqlite3:exec("create table test(one, two, three)", C),
|
||||
|
||||
ok.
|
||||
|
||||
open_multiple_same_databases_test() ->
|
||||
{ok, _C1} = esqlite3:open("test.db"),
|
||||
{ok, _C2} = esqlite3:open("test.db"),
|
||||
|
||||
Reference in New Issue
Block a user