Step now returns {row, Row} to more clearly return errors

This commit is contained in:
Maas-Maarten Zeeman
2012-12-26 14:25:53 +01:00
parent 0b7da430bb
commit 2b669a706f
3 changed files with 43 additions and 24 deletions

View File

@@ -95,6 +95,12 @@ make_error_tuple(ErlNifEnv *env, const char *reason)
return enif_make_tuple2(env, make_atom(env, "error"), make_atom(env, reason)); return enif_make_tuple2(env, make_atom(env, "error"), make_atom(env, reason));
} }
static ERL_NIF_TERM
make_row_tuple(ErlNifEnv *env, ERL_NIF_TERM value)
{
return enif_make_tuple2(env, make_atom(env, "row"), value);
}
static const char * static const char *
get_sqlite3_return_code_msg(int r) get_sqlite3_return_code_msg(int r)
{ {
@@ -419,7 +425,7 @@ make_row(ErlNifEnv *env, sqlite3_stmt *statement)
for(i = 0; i < size; i++) for(i = 0; i < size; i++)
array[i] = make_cell(env, statement, i); array[i] = make_cell(env, statement, i);
row = enif_make_tuple_from_array(env, array, size); row = make_row_tuple(env, enif_make_tuple_from_array(env, array, size));
free(array); free(array);
return row; return row;
} }

View File

@@ -2,7 +2,7 @@
[ [
{description, "sqlite nif interface"}, {description, "sqlite nif interface"},
{vsn, "1"}, {vsn, "1"},
{modules, [esqlite, esqlite_nif]}, {modules, [esqlite3, esqlite3_nif]},
{registered, []}, {registered, []},
{applications, [ {applications, [
kernel, kernel,

View File

@@ -62,28 +62,44 @@ q(Sql, Connection) ->
%% @doc Execute statement, bind args and return a list with tuples as result. %% @doc Execute statement, bind args and return a list with tuples as result.
q(Sql, [], Connection) -> q(Sql, [], Connection) ->
{ok, Statement} = prepare(Sql, Connection), case prepare(Sql, Connection) of
fetchall(Statement); {ok, Statement} ->
fetchall(Statement);
{error, _Msg}=E ->
throw(E)
end;
q(Sql, Args, Connection) -> q(Sql, Args, Connection) ->
{ok, Statement} = prepare(Sql, Connection), case prepare(Sql, Connection) of
ok = bind(Statement, Args), {ok, Statement} ->
fetchall(Statement). ok = bind(Statement, Args),
fetchall(Statement);
{error, _Msg}=E ->
throw(E)
end.
%% %%
map(F, Sql, Connection) -> map(F, Sql, Connection) ->
{ok, Statement} = prepare(Sql, Connection), case prepare(Sql, Connection) of
map_s(F, Statement). {ok, Statement} ->
map_s(F, Statement);
{error, _Msg}=E ->
throw(E)
end.
%% %%
foreach(F, Sql, Connection) -> foreach(F, Sql, Connection) ->
{ok, Statement} = prepare(Sql, Connection), case prepare(Sql, Connection) of
foreach_s(F, Statement). {ok, Statement} ->
foreach_s(F, Statement);
{error, _Msg}=E ->
throw(E)
end.
%% %%
foreach_s(F, Statement) when is_function(F, 1) -> foreach_s(F, Statement) when is_function(F, 1) ->
case try_step(Statement, 0) of case try_step(Statement, 0) of
'$done' -> ok; '$done' -> ok;
Row when is_tuple(Row) -> {row, Row} ->
F(Row), F(Row),
foreach_s(F, Statement) foreach_s(F, Statement)
end; end;
@@ -91,7 +107,7 @@ foreach_s(F, Statement) when is_function(F, 2) ->
ColumnNames = column_names(Statement), ColumnNames = column_names(Statement),
case try_step(Statement, 0) of case try_step(Statement, 0) of
'$done' -> ok; '$done' -> ok;
Row when is_tuple(Row) -> {row, Row} ->
F(ColumnNames, Row), F(ColumnNames, Row),
foreach_s(F, Statement) foreach_s(F, Statement)
end. end.
@@ -100,14 +116,14 @@ foreach_s(F, Statement) when is_function(F, 2) ->
map_s(F, Statement) when is_function(F, 1) -> map_s(F, Statement) when is_function(F, 1) ->
case try_step(Statement, 0) of case try_step(Statement, 0) of
'$done' -> []; '$done' -> [];
Row when is_tuple(Row) -> {row, Row} ->
[F(Row) | map_s(F, Statement)] [F(Row) | map_s(F, Statement)]
end; end;
map_s(F, Statement) when is_function(F, 2) -> map_s(F, Statement) when is_function(F, 2) ->
ColumnNames = column_names(Statement), ColumnNames = column_names(Statement),
case try_step(Statement, 0) of case try_step(Statement, 0) of
'$done' -> []; '$done' -> [];
Row when is_tuple(Row) -> {row, Row} ->
[F(ColumnNames, Row) | map_s(F, Statement)] [F(ColumnNames, Row) | map_s(F, Statement)]
end. end.
@@ -115,8 +131,7 @@ map_s(F, Statement) when is_function(F, 2) ->
fetchone(Statement) -> fetchone(Statement) ->
case try_step(Statement, 0) of case try_step(Statement, 0) of
'$done' -> ok; '$done' -> ok;
Row when is_tuple(Row) -> {row, Row} -> Row
Row
end. end.
%% %%
@@ -124,7 +139,7 @@ fetchall(Statement) ->
case try_step(Statement, 0) of case try_step(Statement, 0) of
'$done' -> '$done' ->
[]; [];
Row when is_tuple(Row) -> {row, Row} ->
[Row | fetchall(Statement)] [Row | fetchall(Statement)]
end. end.
@@ -156,7 +171,7 @@ exec(Sql, Connection, Timeout) ->
%% @doc Prepare a statement %% @doc Prepare a statement
%% %%
%% @spec prepare(iolost(), connection()) -> {ok, prepared_statement()} | {error, error_message()} %% @spec prepare(iolist(), connection()) -> {ok, prepared_statement()} | {error, error_message()}
prepare(Sql, Connection) -> prepare(Sql, Connection) ->
prepare(Sql, Connection, ?DEFAULT_TIMEOUT). prepare(Sql, Connection, ?DEFAULT_TIMEOUT).
@@ -226,12 +241,10 @@ add_eos(IoList) ->
receive_answer(Ref, Timeout) -> receive_answer(Ref, Timeout) ->
receive receive
{Ref, Resp} -> {Ref, Resp} -> Resp;
Resp; Other -> throw(Other)
Other ->
throw(Other)
after Timeout -> after Timeout ->
throw({error, timeout, Ref}) throw({error, timeout, Ref})
end. end.