diff --git a/c_src/esqlite3_nif.c b/c_src/esqlite3_nif.c index 42d4440..616242f 100644 --- a/c_src/esqlite3_nif.c +++ b/c_src/esqlite3_nif.c @@ -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)); } +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 * get_sqlite3_return_code_msg(int r) { @@ -419,7 +425,7 @@ make_row(ErlNifEnv *env, sqlite3_stmt *statement) for(i = 0; i < size; 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); return row; } diff --git a/src/esqlite.app.src b/src/esqlite.app.src index 48c7bfb..8218d2e 100644 --- a/src/esqlite.app.src +++ b/src/esqlite.app.src @@ -2,7 +2,7 @@ [ {description, "sqlite nif interface"}, {vsn, "1"}, - {modules, [esqlite, esqlite_nif]}, + {modules, [esqlite3, esqlite3_nif]}, {registered, []}, {applications, [ kernel, diff --git a/src/esqlite3.erl b/src/esqlite3.erl index ec33fb1..83d793d 100644 --- a/src/esqlite3.erl +++ b/src/esqlite3.erl @@ -62,28 +62,44 @@ q(Sql, Connection) -> %% @doc Execute statement, bind args and return a list with tuples as result. q(Sql, [], Connection) -> - {ok, Statement} = prepare(Sql, Connection), - fetchall(Statement); + case prepare(Sql, Connection) of + {ok, Statement} -> + fetchall(Statement); + {error, _Msg}=E -> + throw(E) + end; q(Sql, Args, Connection) -> - {ok, Statement} = prepare(Sql, Connection), - ok = bind(Statement, Args), - fetchall(Statement). + case prepare(Sql, Connection) of + {ok, Statement} -> + ok = bind(Statement, Args), + fetchall(Statement); + {error, _Msg}=E -> + throw(E) + end. %% map(F, Sql, Connection) -> - {ok, Statement} = prepare(Sql, Connection), - map_s(F, Statement). + case prepare(Sql, Connection) of + {ok, Statement} -> + map_s(F, Statement); + {error, _Msg}=E -> + throw(E) + end. %% foreach(F, Sql, Connection) -> - {ok, Statement} = prepare(Sql, Connection), - foreach_s(F, Statement). + case prepare(Sql, Connection) of + {ok, Statement} -> + foreach_s(F, Statement); + {error, _Msg}=E -> + throw(E) + end. %% foreach_s(F, Statement) when is_function(F, 1) -> case try_step(Statement, 0) of '$done' -> ok; - Row when is_tuple(Row) -> + {row, Row} -> F(Row), foreach_s(F, Statement) end; @@ -91,7 +107,7 @@ foreach_s(F, Statement) when is_function(F, 2) -> ColumnNames = column_names(Statement), case try_step(Statement, 0) of '$done' -> ok; - Row when is_tuple(Row) -> + {row, Row} -> F(ColumnNames, Row), foreach_s(F, Statement) end. @@ -100,14 +116,14 @@ foreach_s(F, Statement) when is_function(F, 2) -> map_s(F, Statement) when is_function(F, 1) -> case try_step(Statement, 0) of '$done' -> []; - Row when is_tuple(Row) -> + {row, Row} -> [F(Row) | map_s(F, Statement)] end; map_s(F, Statement) when is_function(F, 2) -> ColumnNames = column_names(Statement), case try_step(Statement, 0) of '$done' -> []; - Row when is_tuple(Row) -> + {row, Row} -> [F(ColumnNames, Row) | map_s(F, Statement)] end. @@ -115,8 +131,7 @@ map_s(F, Statement) when is_function(F, 2) -> fetchone(Statement) -> case try_step(Statement, 0) of '$done' -> ok; - Row when is_tuple(Row) -> - Row + {row, Row} -> Row end. %% @@ -124,7 +139,7 @@ fetchall(Statement) -> case try_step(Statement, 0) of '$done' -> []; - Row when is_tuple(Row) -> + {row, Row} -> [Row | fetchall(Statement)] end. @@ -156,7 +171,7 @@ exec(Sql, Connection, Timeout) -> %% @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, ?DEFAULT_TIMEOUT). @@ -226,12 +241,10 @@ add_eos(IoList) -> receive_answer(Ref, Timeout) -> receive - {Ref, Resp} -> - Resp; - Other -> - throw(Other) + {Ref, Resp} -> Resp; + Other -> throw(Other) after Timeout -> - throw({error, timeout, Ref}) + throw({error, timeout, Ref}) end.