From 69bf43054dd635dbc133eb60b3c574cd574e497e Mon Sep 17 00:00:00 2001 From: Maas-Maarten Zeeman Date: Fri, 4 Nov 2011 21:17:34 +0100 Subject: [PATCH] Now also binds text --- c_src/esqlite_nif.c | 20 ++++++-------------- test/esqlite_test.erl | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/c_src/esqlite_nif.c b/c_src/esqlite_nif.c index 0ac8430..b800744 100644 --- a/c_src/esqlite_nif.c +++ b/c_src/esqlite_nif.c @@ -236,18 +236,8 @@ bind_cell(ErlNifEnv *env, const ERL_NIF_TERM cell, sqlite3_stmt *stmt, unsigned char the_atom[MAX_ATOM_LENGTH+1]; ErlNifBinary the_blob; - /* - erlang atom undefined -> sqlite null - erlang atom (not undefined) -> sqlite text - erlang int -> sqlite int - erlang double -> sqlite double - erlang iolist -> sqlite blob - erlang list -> sqlite text - */ - /* TODO: check the error codes! */ - /* TODO check for atom undefined */ if(enif_get_int(env, cell, &the_int)) { sqlite3_bind_int(stmt, i, the_int); return; @@ -269,13 +259,15 @@ bind_cell(ErlNifEnv *env, const ERL_NIF_TERM cell, sqlite3_stmt *stmt, unsigned } if(enif_inspect_iolist_as_binary(env, cell, &the_blob)) { + /* Bind lists which have the same length as the binary as text */ + if(enif_is_list(env, cell) && (strlen((char *) the_blob.data) == the_blob.size)) { + sqlite3_bind_text(stmt, i, (char *) the_blob.data, the_blob.size, SQLITE_TRANSIENT); + return; + } + sqlite3_bind_blob(stmt, i, the_blob.data, the_blob.size, SQLITE_TRANSIENT); return; } - - /* */ - - sqlite3_bind_int(stmt, i, i); } static ERL_NIF_TERM diff --git a/test/esqlite_test.erl b/test/esqlite_test.erl index 973b611..84270c4 100644 --- a/test/esqlite_test.erl +++ b/test/esqlite_test.erl @@ -62,24 +62,31 @@ bind_test() -> {ok, Statement} = esqlite:prepare(Db, "insert into test_table values(?1, ?2)"), esqlite:bind(Statement, [one, 2]), esqlite:step(Statement), - esqlite:bind(Statement, [three, 4]), + esqlite:bind(Statement, ["three", 4]), + esqlite:step(Statement), + esqlite:bind(Statement, [<<"five">>, 6]), esqlite:step(Statement), - [{"one", 2}] = q(Db, "select * from test_table where two = 2"), + [{"one", 2}] = q(Db, "select * from test_table where two = '2'"), [{"three", 4}] = q(Db, "select * from test_table where two = 4"), + [{<<"five">>, 6}] = q(Db, "select * from test_table where two = 6"), + ok. %% Handy functions... %% - -q(Db, Sql) when is_list(Sql) -> - {ok, Statement} = esqlite:prepare(Db, Sql), +q(Connection, Sql) -> + {ok, Statement} = esqlite:prepare(Connection, Sql), exec(Statement). -q(Db, Sql, Args) when is_list(Sql) -> - {ok, Statement} = esqlite:prepare(Db, Sql), + +q(Connection, Sql, []) -> + q(Connection, Sql); +q(Connection, Sql, Args) -> + {ok, Statement} = esqlite:prepare(Connection, Sql), esqlite:bind(Statement, Args), exec(Statement). + exec(Statement) -> exec(Statement, [], 0).