add insert function to get last insert rowid
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
%% higher-level export
|
||||
-export([open/1, open/2,
|
||||
exec/2, exec/3,
|
||||
insert/2,
|
||||
prepare/2, prepare/3,
|
||||
step/1, step/2,
|
||||
bind/2, bind/3,
|
||||
@@ -199,6 +200,20 @@ exec(Sql, {connection, _Ref, Connection}, Timeout) ->
|
||||
ok = esqlite3_nif:exec(Connection, Ref, self(), Sql),
|
||||
receive_answer(Ref, Timeout).
|
||||
|
||||
%% @doc Insert records, returns the last rowid.
|
||||
%%
|
||||
%% @spec insert(iolist(), connection()) -> integer() | {error, error_message()}
|
||||
insert(Sql, Connection) ->
|
||||
insert(Sql, Connection, ?DEFAULT_TIMEOUT).
|
||||
|
||||
%% @doc Insert
|
||||
%%
|
||||
%% @spec insert(iolist(), connection(), timeout()) -> integer() | {error, error_message()}
|
||||
insert(Sql, {connection, _Ref, Connection}, Timeout) ->
|
||||
Ref = make_ref(),
|
||||
ok = esqlite3_nif:insert(Connection, Ref, self(), Sql),
|
||||
receive_answer(Ref, Timeout).
|
||||
|
||||
%% @doc Prepare a statement
|
||||
%%
|
||||
%% @spec prepare(iolist(), connection()) -> {ok, prepared_statement()} | {error, error_message()}
|
||||
|
||||
@@ -21,15 +21,16 @@
|
||||
-author("Maas-Maarten Zeeman <mmzeeman@xs4all.nl>").
|
||||
|
||||
%% low-level exports
|
||||
-export([start/0,
|
||||
open/4,
|
||||
exec/4,
|
||||
prepare/4,
|
||||
step/3,
|
||||
finalize/3,
|
||||
bind/4,
|
||||
column_names/3,
|
||||
close/3
|
||||
-export([start/0,
|
||||
open/4,
|
||||
exec/4,
|
||||
insert/4,
|
||||
prepare/4,
|
||||
step/3,
|
||||
finalize/3,
|
||||
bind/4,
|
||||
column_names/3,
|
||||
close/3
|
||||
]).
|
||||
|
||||
-on_load(init/0).
|
||||
@@ -37,9 +38,9 @@
|
||||
init() ->
|
||||
NifName = "esqlite3_nif",
|
||||
NifFileName = case code:priv_dir(esqlite) of
|
||||
{error, bad_name} -> filename:join("priv", NifName);
|
||||
Dir -> filename:join(Dir, NifName)
|
||||
end,
|
||||
{error, bad_name} -> filename:join("priv", NifName);
|
||||
Dir -> filename:join(Dir, NifName)
|
||||
end,
|
||||
ok = erlang:load_nif(NifFileName, 0).
|
||||
|
||||
%% @doc Start a low level thread which will can handle sqlite3 calls.
|
||||
@@ -105,7 +106,12 @@ column_names(_Stmt, _Ref, _Dest) ->
|
||||
close(_Db, _Ref, _Dest) ->
|
||||
exit(nif_library_not_loaded).
|
||||
|
||||
|
||||
|
||||
%% @doc Insert record
|
||||
%%
|
||||
%% @spec insert(connection(), Ref::reference(), Dest::pid(), string()) -> ok | {error, message()}
|
||||
insert(_Db, _Ref, _Dest, _Sql) ->
|
||||
exit(nif_library_not_loaded).
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user