Re-added higher level api

This commit is contained in:
Maas-Maarten Zeeman
2022-05-23 22:39:19 +02:00
parent 2f3eecdfe5
commit 20ad5138a1
4 changed files with 177 additions and 196 deletions

View File

@@ -815,7 +815,7 @@ esqlite_bind_text(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
return enif_make_badarg(env); return enif_make_badarg(env);
} }
if(!enif_inspect_iolist_as_binary(env, argv[1], &text)) { if(!enif_inspect_iolist_as_binary(env, argv[2], &text)) {
return enif_make_badarg(env); return enif_make_badarg(env);
} }

View File

@@ -48,11 +48,12 @@
bind_null/2, bind_null/2,
step/1, step/1,
reset/1 reset/1,
q/2, q/3,
% fetchone/1, fetchone/1,
% fetchall/1, fetchall/2, fetchall/3, fetchall/1
% %
% backup_init/4, backup_init/5, % backup_init/4, backup_init/5,
% backup_finish/1, backup_finish/2, % backup_finish/1, backup_finish/2,
@@ -160,44 +161,38 @@ error_info(#esqlite3{db=Connection}) ->
set_update_hook(#esqlite3{db=Connection}, MaybePid) when is_pid(MaybePid) orelse MaybePid =:= undefined -> set_update_hook(#esqlite3{db=Connection}, MaybePid) when is_pid(MaybePid) orelse MaybePid =:= undefined ->
esqlite3_nif:set_update_hook(Connection, MaybePid). esqlite3_nif:set_update_hook(Connection, MaybePid).
%%% %%%
%%% q %%% q
%%% %%%
%
%%% @doc Execute a sql statement, returns a list with tuples. %%% @doc Execute a sql statement, returns a list with tuples.
%-spec q(sql(), connection()) -> list(row()) | {error, _}. %-spec q(sql(), connection()) -> list(row()) | {error, _}.
%q(Sql, Connection) -> q(Connection, Sql) ->
% q(Sql, [], Connection, ?DEFAULT_TIMEOUT). q(Connection, Sql, []).
%
%%% @doc Execute statement, bind args and return a list with tuples as result.
%-spec q(sql(), list(), connection()) -> list(row()) | {error, _}.
%%q(Sql, Args, Connection) ->
% q(Sql, Args, Connection, ?DEFAULT_TIMEOUT).
%
%% @doc Execute statement, bind args and return a list with tuples as result restricted by timeout. %% @doc Execute statement, bind args and return a list with tuples as result restricted by timeout.
%-spec q(sql(), list(), connection(), timeout()) -> list(row()) | {error, _}. %-spec q(sql(), list(), connection(), timeout()) -> list(row()) | {error, _}.
%q(Sql, [], Connection, Timeout) -> q(Connection, Sql, []) ->
% case prepare(Sql, Connection, Timeout) of case prepare(Connection, Sql) of
% {ok, Statement} -> {ok, Statement} ->
% fetchall(Statement, ?DEFAULT_CHUNK_SIZE, Timeout); fetchall(Statement);
% {error, _Msg}=Error -> {error, _Msg}=Error ->
% Error Error
% end; end;
%q(Sql, Args, Connection, Timeout) ->
% case prepare(Sql, Connection, Timeout) of q(Connection, Sql, Args) ->
% {ok, Statement} -> case prepare(Connection, Sql) of
% case bind(Statement, Args, Timeout) of {ok, Statement} ->
% ok -> case bind(Statement, Args) of
% fetchall(Statement, ?DEFAULT_CHUNK_SIZE, Timeout); ok ->
% {error, _}=Error -> fetchall(Statement);
% Error {error, _}=Error ->
% end; Error
% {error, _Msg}=Error -> end;
% Error {error, _Msg}=Error ->
% end. Error
% end.
%%% %%%
%% map %% map
%%% %%%
@@ -280,45 +275,63 @@ set_update_hook(#esqlite3{db=Connection}, MaybePid) when is_pid(MaybePid) orelse
%% %%
%-spec fetchone(statement()) -> tuple(). %-spec fetchone(statement()) -> tuple().
%fetchone(Statement) -> fetchone(Statement) ->
% case try_multi_step(Statement, 1, [], 0) of case step(Statement) of
% {'$done', []} -> ok; Row when is_list(Row) ->
% {error, _} = E -> E; Row;
% {rows, [Row | []]} -> Row '$done' ->
% end. ok;
{error, _} = E ->
E
end.
% %
%%% @doc Fetch all records %%% @doc Fetch all records
%%% @param Statement is prepared sql statement %%% @param Statement is prepared sql statement
%-spec fetchall(statement()) -> list(row()) | {error, _}. %-spec fetchall(statement()) -> list(row()) | {error, _}.
%fetchall(Statement) -> fetchall(Statement) ->
% fetchall(Statement, ?DEFAULT_CHUNK_SIZE, ?DEFAULT_TIMEOUT). fetchall1(Statement, []).
%
%% @doc Fetch all records fetchall1(Statement, Acc) ->
%% @param Statement is prepared sql statement case step(Statement) of
%% @param ChunkSize is a count of rows to read from sqlite and send to erlang process in one bulk. Row when is_list(Row) ->
%% Decrease this value if rows are heavy. Default value is 5000 (DEFAULT_CHUNK_SIZE). fetchall1(Statement, [Row|Acc]);
%-spec fetchall(statement(), pos_integer()) -> list(row()) | {error, _}. '$done' ->
%fetchall(Statement, ChunkSize) -> lists:reverse(Acc);
% fetchall(Statement, ChunkSize, ?DEFAULT_TIMEOUT). {error, _} = E ->
% E
%%% @doc Fetch all records end.
%%% @param Statement is prepared sql statement
%% @param ChunkSize is a count of rows to read from sqlite and send to erlang process in one bulk. bind(Statement, Args) ->
%% Decrease this value if rows are heavy. Default value is 5000 (DEFAULT_CHUNK_SIZE). bind1(Statement, 1, Args).
%% @param Timeout is timeout per each request of the one bulk
%-spec fetchall(statement(), pos_integer(), timeout()) -> list(row()) | {error, _}. bind1(_Statement, _Column, []) ->
%fetchall(Statement, ChunkSize, Timeout) -> ok;
% case fetchall_internal(Statement, ChunkSize, [], Timeout) of bind1(Statement, Column, [Arg | Args]) ->
% {'$done', Rows} -> lists:reverse(Rows); bind_arg(Statement, Column, Arg),
% {error, _} = E -> E bind1(Statement, Column + 1, Args).
% end.
% Do automatic conversion
bind_arg(Statement, Column, undefined) ->
bind_null(Statement, Column);
bind_arg(Statement, Column, null) ->
bind_null(Statement, Column);
bind_arg(Statement, Column, Atom) when is_atom(Atom) ->
bind_text(Statement, Column, atom_to_binary(Atom, utf8));
bind_arg(Statement, Column, Int) when is_integer(Int) ->
bind_int64(Statement, Column, Int);
bind_arg(Statement, Column, Float) when is_float(Float) ->
bind_double(Statement, Column, Float);
bind_arg(Statement, Column, Bin) when is_binary(Bin) ->
bind_text(Statement, Column, Bin);
bind_arg(Statement, Column, String) when is_list(String) ->
bind_text(Statement, Column, String).
%% @doc Get the last insert rowid. %% @doc Get the last insert rowid.
%% %%
-spec last_insert_rowid(Connection) -> RowidResult -spec last_insert_rowid(Connection) -> RowidResult when
when Connection :: esqlite3(), Connection :: esqlite3(),
RowidResult :: integer() | {error, closed}. RowidResult :: integer() | {error, closed}.
last_insert_rowid(#esqlite3{db=Connection}) -> last_insert_rowid(#esqlite3{db=Connection}) ->
esqlite3_nif:last_insert_rowid(Connection). esqlite3_nif:last_insert_rowid(Connection).
@@ -400,6 +413,14 @@ bind_int64(#esqlite3_stmt{stmt=Stmt}, Index, Value) ->
bind_double(#esqlite3_stmt{stmt=Stmt}, Index, Value) -> bind_double(#esqlite3_stmt{stmt=Stmt}, Index, Value) ->
esqlite3_nif:bind_double(Stmt, Index, Value). esqlite3_nif:bind_double(Stmt, Index, Value).
-spec bind_text(Statement, Index, Value) -> BindResult
when Statement :: esqlite3_stmt(),
Index :: integer(),
Value :: iodata(),
BindResult :: ok | {error, _}.
bind_text(#esqlite3_stmt{stmt=Stmt}, Index, Value) ->
esqlite3_nif:bind_text(Stmt, Index, Value).
-spec bind_blob(Statement, Index, Value) -> BindResult -spec bind_blob(Statement, Index, Value) -> BindResult
when Statement :: esqlite3_stmt(), when Statement :: esqlite3_stmt(),
Index :: integer(), Index :: integer(),
@@ -631,8 +652,3 @@ props_to_prepare_flag(Props) ->
end. end.

View File

@@ -141,10 +141,10 @@ bind_int64(_Statement, _Index, _Value) ->
bind_double(_Statement, _Index, _Value) -> bind_double(_Statement, _Index, _Value) ->
erlang:nif_error(nif_library_not_loaded). erlang:nif_error(nif_library_not_loaded).
bind_blob(_Statement, _Index, _Value) -> bind_text(_Statement, _Index, _Value) ->
erlang:nif_error(nif_library_not_loaded). erlang:nif_error(nif_library_not_loaded).
bind_text(_Statement, _Index, _Value) -> bind_blob(_Statement, _Index, _Value) ->
erlang:nif_error(nif_library_not_loaded). erlang:nif_error(nif_library_not_loaded).
bind_null(_Statement, _Index) -> bind_null(_Statement, _Index) ->
@@ -156,27 +156,6 @@ step(_Statement) ->
reset(_Statement) -> reset(_Statement) ->
erlang:nif_error(nif_library_not_loaded). erlang:nif_error(nif_library_not_loaded).
%% @doc Exec the query.
%%
%% Sends an asynchronous exec command over the connection and returns
%% ok immediately.
%%
%% When the statement is executed Dest will receive message {Ref, answer()}
%% with answer() integer | {error, reason()}
%%
%-spec exec(esqlite3(), reference(), pid(), sql()) -> ok | {error, _}.
%%exec(_Db, _Ref, _Dest, _Sql) ->
% erlang:nif_error(nif_library_not_loaded).
%% @doc Get the number of affected rows of last statement
%%
%% When the statement is executed Dest will receive message {Ref, answer()}
%% with answer() integer | {error, reason()}
%-spec changes(esqlite3(), reference(), pid()) -> ok | {error, _}.
%changes(_Db, _Ref, _Dest) ->
% erlang:nif_error(nif_library_not_loaded).
%% @doc %% @doc
%% %%
%-spec multi_step(esqlite3(), esqlite3_stmt(), pos_integer(), reference(), pid()) -> ok | {error, _}. %-spec multi_step(esqlite3(), esqlite3_stmt(), pos_integer(), reference(), pid()) -> ok | {error, _}.
@@ -195,12 +174,6 @@ reset(_Statement) ->
%finalize(_Db, _Stmt, _Ref, _Dest) -> %finalize(_Db, _Stmt, _Ref, _Dest) ->
% erlang:nif_error(nif_library_not_loaded). % erlang:nif_error(nif_library_not_loaded).
%% @doc Bind parameters to a prepared statement.
%%
%-spec bind(esqlite3(), esqlite3_stmt(), 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 %% @doc Retrieve the column names of the prepared statement
%% %%
-spec column_names(esqlite3_stmt()) -> list(binary()) | {error, _}. -spec column_names(esqlite3_stmt()) -> list(binary()) | {error, _}.

View File

@@ -64,13 +64,9 @@ column_names_test() ->
column_decltypes_test() -> column_decltypes_test() ->
{ok, C} = esqlite3:open(":memory:"), {ok, C} = esqlite3:open(":memory:"),
{ok, Stmt} = esqlite3:prepare(C, "select 1, 2, 3"), {ok, Stmt} = esqlite3:prepare(C, "select 1, 2, 3"),
?assertEqual([undefined, undefined, undefined], esqlite3:column_decltypes(Stmt)), ?assertEqual([undefined, undefined, undefined], esqlite3:column_decltypes(Stmt)),
%% Need to be able to define tables.
ok. ok.
step_test() -> step_test() ->
@@ -197,7 +193,7 @@ prepare2_test() ->
ok = esqlite3:exec(Db, "insert into test_table values('hello4', 13);"), ok = esqlite3:exec(Db, "insert into test_table values('hello4', 13);"),
%% Check if the values are there. %% Check if the values are there.
%% [{<<"one">>, 2}, {<<"hello4">>, 13}] = esqlite3:q(Db, "select * from test_table order by two"), [[<<"one">>, 2], [<<"hello4">>, 13]] = esqlite3:q(Db, "select * from test_table order by two"),
esqlite3:exec(Db, "commit;"), esqlite3:exec(Db, "commit;"),
esqlite3:close(Db), esqlite3:close(Db),
@@ -266,94 +262,91 @@ prepare2_test() ->
% %
% ok. % ok.
%bind_for_queries_test() -> bind_for_queries_test() ->
% {ok, Db} = esqlite3:open(":memory:"), {ok, Db} = esqlite3:open(":memory:"),
%
% ok = esqlite3:exec("begin;", Db),
% ok = esqlite3:exec("create table test_table(one varchar(10), two int);", Db),
% ok = esqlite3:exec("commit;", Db),
% ?assertEqual([{1}], esqlite3:q(<<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>, ok = esqlite3:exec(Db, "begin;"),
% [test_table], Db)), ok = esqlite3:exec(Db, "create table test_table(one varchar(10), two int);"),
% ?assertEqual([{1}], esqlite3:q(<<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>, ok = esqlite3:exec(Db, "commit;"),
% ["test_table"], Db)),
% ?assertEqual([{1}], esqlite3:q(<<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>,
% [<<"test_table">>], Db)),
% ?assertEqual([{1}], esqlite3:q(<<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>,
% [[<<"test_table">>]], Db)),
%
% ok.
%column_names_test() -> ?assertEqual([[1]], esqlite3:q(Db, <<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>,
% {ok, Db} = esqlite3:open(":memory:"), [test_table])),
% ok = esqlite3:exec("begin;", Db), ?assertEqual([[1]], esqlite3:q(Db, <<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>,
% ok = esqlite3:exec("create table test_table(one varchar(10), two int);", Db), ["test_table"])),
% ok = esqlite3:exec("insert into test_table values('hello1', 10);", Db), ?assertEqual([[1]], esqlite3:q(Db, <<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>,
% ok = esqlite3:exec("insert into test_table values('hello2', 20);", Db), [<<"test_table">>])),
% ok = esqlite3:exec("commit;", Db), ?assertEqual([[1]], esqlite3:q(Db, <<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>,
[[<<"test_table">>]])),
ok.
column_names2_test() ->
{ok, Db} = esqlite3:open(":memory:"),
ok = esqlite3:exec(Db, "begin;"),
ok = esqlite3:exec(Db, "create table test_table(one varchar(10), two int);"),
ok = esqlite3:exec(Db, "insert into test_table values('hello1', 10);"),
ok = esqlite3:exec(Db, "insert into test_table values('hello2', 20);"),
ok = esqlite3:exec(Db, "commit;"),
%% All columns %% All columns
% {ok, Stmt} = esqlite3:prepare("select * from test_table", Db), {ok, Stmt} = esqlite3:prepare(Db, "select * from test_table"),
% {one, two} = esqlite3:column_names(Stmt), [<<"one">>, <<"two">>] = esqlite3:column_names(Stmt),
% {row, {<<"hello1">>, 10}} = esqlite3:step(Stmt), [<<"hello1">>, 10] = esqlite3:step(Stmt),
% {one, two} = esqlite3:column_names(Stmt), [<<"one">>, <<"two">>] = esqlite3:column_names(Stmt),
% {row, {<<"hello2">>, 20}} = esqlite3:step(Stmt), [<<"hello2">>, 20] = esqlite3:step(Stmt),
% {one, two} = esqlite3:column_names(Stmt), [<<"one">>, <<"two">>] = esqlite3:column_names(Stmt),
% '$done' = esqlite3:step(Stmt), '$done' = esqlite3:step(Stmt),
% {one, two} = esqlite3:column_names(Stmt), [<<"one">>, <<"two">>] = esqlite3:column_names(Stmt),
%% One column %% One column
% {ok, Stmt2} = esqlite3:prepare("select two from test_table", Db), {ok, Stmt2} = esqlite3:prepare(Db, "select two from test_table"),
% {two} = esqlite3:column_names(Stmt2), [<<"two">>] = esqlite3:column_names(Stmt2),
% {row, {10}} = esqlite3:step(Stmt2), [10] = esqlite3:step(Stmt2),
% {two} = esqlite3:column_names(Stmt2), [<<"two">>] = esqlite3:column_names(Stmt2),
% {row, {20}} = esqlite3:step(Stmt2), [20] = esqlite3:step(Stmt2),
% {two} = esqlite3:column_names(Stmt2), [<<"two">>] = esqlite3:column_names(Stmt2),
% '$done' = esqlite3:step(Stmt2), '$done' = esqlite3:step(Stmt2),
% {two} = esqlite3:column_names(Stmt2), [<<"two">>] = esqlite3:column_names(Stmt2),
%% No columns %% No columns
% {ok, Stmt3} = esqlite3:prepare("values(1);", Db), {ok, Stmt3} = esqlite3:prepare(Db, "values(1);"),
% {column1} = esqlite3:column_names(Stmt3), [<<"column1">>] = esqlite3:column_names(Stmt3),
% {row, {1}} = esqlite3:step(Stmt3), [1] = esqlite3:step(Stmt3),
% {column1} = esqlite3:column_names(Stmt3), [<<"column1">>] = esqlite3:column_names(Stmt3),
%% Things get a bit weird when you retrieve the column name %% Things get a bit weird when you retrieve the column name
%% when calling an aggragage function. %% when calling an aggragage function.
%% {ok, Stmt4} = esqlite3:prepare("select date('now');", Db), {ok, Stmt4} = esqlite3:prepare(Db, "select date('now');"),
% {'date(\'now\')'} = esqlite3:column_names(Stmt4), [<<"date(\'now\')">>] = esqlite3:column_names(Stmt4),
% {row, {Date}} = esqlite3:step(Stmt4), [Date] = esqlite3:step(Stmt4),
% true = is_binary(Date), true = is_binary(Date),
%% Some statements have no column names %% Some statements have no column names
% {ok, Stmt5} = esqlite3:prepare("create table dummy(a, b, c);", Db), {ok, Stmt5} = esqlite3:prepare(Db, "create table dummy(a, b, c);"),
% {} = esqlite3:column_names(Stmt5), [] = esqlite3:column_names(Stmt5),
%
% ok. ok.
column_types_test() ->
{ok, Db} = esqlite3:open(":memory:"),
ok = esqlite3:exec(Db, "begin;"),
ok = esqlite3:exec(Db, "create table test_table(one varchar(10), two int);"),
ok = esqlite3:exec(Db, "insert into test_table values('hello1', 10);"),
ok = esqlite3:exec(Db, "insert into test_table values('hello2', 20);"),
ok = esqlite3:exec(Db, "commit;"),
%column_types_test() ->
% {ok, Db} = esqlite3:open(":memory:"),
% ok = esqlite3:exec("begin;", Db),
% ok = esqlite3:exec("create table test_table(one varchar(10), two int);", Db),
% ok = esqlite3:exec("insert into test_table values('hello1', 10);", Db),
% ok = esqlite3:exec("insert into test_table values('hello2', 20);", Db),
% ok = esqlite3:exec("commit;", Db),
%
%% All columns %% All columns
% {ok, Stmt} = esqlite3:prepare("select * from test_table", Db), {ok, Stmt} = esqlite3:prepare(Db, "select * from test_table"),
% ?assertEqual({'varchar(10)', 'INT'}, esqlite3:column_types(Stmt)), ?assertEqual([<<"varchar(10)">>, <<"INT">>], esqlite3:column_decltypes(Stmt)),
% {row, {<<"hello1">>, 10}} = esqlite3:step(Stmt),
% {'varchar(10)', 'INT'} = esqlite3:column_types(Stmt), %% Some statements have no column types
% {row, {<<"hello2">>, 20}} = esqlite3:step(Stmt), {ok, Stmt2} = esqlite3:prepare(Db, "create table dummy(a, b, c);"),
% {'varchar(10)', 'INT'} = esqlite3:column_types(Stmt), [] = esqlite3:column_decltypes(Stmt2),
% '$done' = esqlite3:step(Stmt),
% {'varchar(10)', 'INT'} = esqlite3:column_types(Stmt), {ok, Stmt3} = esqlite3:prepare(Db, "select 1, 2, 3;"),
% [undefined, undefined, undefined] = esqlite3:column_decltypes(Stmt3),
% %% Some statements have no column types
% {ok, Stmt2} = esqlite3:prepare("create table dummy(a, b, c);", Db), ok.
% {} = esqlite3:column_types(Stmt2),
%
% ok.
nil_column_decltypes_test() -> nil_column_decltypes_test() ->
{ok, Db} = esqlite3:open(":memory:"), {ok, Db} = esqlite3:open(":memory:"),
@@ -636,27 +629,26 @@ sqlite_source_id_test() ->
% end % end
% end. % end.
%garbage_collect_test() -> garbage_collect_test() ->
% F = fun() -> F = fun() ->
% {ok, Db} = esqlite3:open(":memory:"), {ok, Db} = esqlite3:open(":memory:"),
% [] = esqlite3:q("create table test(one, two, three)", Db), [] = esqlite3:q(Db, "create table test(one, two, three)"),
% [] = esqlite3:q("insert into test values(1, '2', 3.0)", Db), [] = esqlite3:q(Db, "insert into test values(1, '2', 3.0)"),
% {ok, Stmt} = esqlite3:prepare("select * from test", Db), {ok, Stmt} = esqlite3:prepare(Db, "select * from test"),
% {row, {1, <<"2">>, 3.0}} = esqlite3:step(Stmt), [1, <<"2">>, 3.0] = esqlite3:step(Stmt),
% '$done' = esqlite3:step(Stmt), '$done' = esqlite3:step(Stmt),
% ok = esqlite3:close(Db) ok = esqlite3:close(Db)
% end, end,
%
% [spawn(F) || _X <- lists:seq(0,30)],
% receive after 500 -> ok end,
% erlang:garbage_collect(),
%
% [spawn(F) || _X <- lists:seq(0,30)],
% receive after 500 -> ok end,
% erlang:garbage_collect(),
[spawn(F) || _X <- lists:seq(0,30)],
receive after 500 -> ok end,
erlang:garbage_collect(),
% ok. [spawn(F) || _X <- lists:seq(0,30)],
receive after 500 -> ok end,
erlang:garbage_collect(),
ok.
%% %%
%% Helpers %% Helpers