From cfe06ddfd921e930e22f352cd8c37e361ce84f7b Mon Sep 17 00:00:00 2001 From: Maas-Maarten Zeeman Date: Fri, 22 Feb 2013 08:37:20 +0100 Subject: [PATCH] New trick to get 0 terminated strings from an iolist --- c_src/esqlite3_nif.c | 8 ++++++-- src/esqlite3.erl | 6 ++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/c_src/esqlite3_nif.c b/c_src/esqlite3_nif.c index d4d06d2..5b981b2 100644 --- a/c_src/esqlite3_nif.c +++ b/c_src/esqlite3_nif.c @@ -265,8 +265,10 @@ do_exec(ErlNifEnv *env, esqlite_connection *conn, const ERL_NIF_TERM arg) { ErlNifBinary bin; int rc; + ERL_NIF_TERM eos = enif_make_int(env, 0); - enif_inspect_iolist_as_binary(env, arg, &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) @@ -285,8 +287,10 @@ do_prepare(ErlNifEnv *env, esqlite_connection *conn, const ERL_NIF_TERM arg) ERL_NIF_TERM esqlite_stmt; const char *tail; int rc; + ERL_NIF_TERM eos = enif_make_int(env, 0); - enif_inspect_iolist_as_binary(env, arg, &bin); + enif_inspect_iolist_as_binary(env, + enif_make_list2(env, arg, eos), &bin); stmt = enif_alloc_resource(esqlite_statement_type, sizeof(esqlite_statement)); if(!stmt) diff --git a/src/esqlite3.erl b/src/esqlite3.erl index 5493040..1b40920 100644 --- a/src/esqlite3.erl +++ b/src/esqlite3.erl @@ -196,7 +196,7 @@ exec(Sql, Connection) -> %% @spec exec(iolist(), connection(), timeout()) -> integer() | {error, error_message()} exec(Sql, {connection, _Ref, Connection}, Timeout) -> Ref = make_ref(), - ok = esqlite3_nif:exec(Connection, Ref, self(), add_eos(Sql)), + ok = esqlite3_nif:exec(Connection, Ref, self(), Sql), receive_answer(Ref, Timeout). %% @doc Prepare a statement @@ -210,7 +210,7 @@ prepare(Sql, Connection) -> %% @spec(iolist(), connection(), timeout()) -> {ok, prepared_statement()} | {error, error_message()} prepare(Sql, {connection, _Ref, Connection}, Timeout) -> Ref = make_ref(), - ok = esqlite3_nif:prepare(Connection, Ref, self(), add_eos(Sql)), + ok = esqlite3_nif:prepare(Connection, Ref, self(), Sql), receive_answer(Ref, Timeout). %% @doc Step @@ -270,8 +270,6 @@ close({connection, _Ref, Connection}, Timeout) -> receive_answer(Ref, Timeout). %% Internal functions -add_eos(IoList) -> - [IoList, 0]. receive_answer(Ref, Timeout) -> receive