From ec04d67632a0d002c4b4849275129dbcef249b86 Mon Sep 17 00:00:00 2001 From: Maas-Maarten Zeeman Date: Tue, 1 Jan 2013 14:42:43 +0100 Subject: [PATCH] Check for issue #2. No problem... Fixes #2 --- c_src/esqlite3_nif.c | 6 ++--- src/esqlite3.erl | 2 +- src/esqlite3_nif.erl | 4 ++-- test/esqlite_test.erl | 52 +++++++++++++++++++++++++++++++++++-------- 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/c_src/esqlite3_nif.c b/c_src/esqlite3_nif.c index 151d1b7..5e2e001 100644 --- a/c_src/esqlite3_nif.c +++ b/c_src/esqlite3_nif.c @@ -1,5 +1,5 @@ /* - * Copyright 2011, 2012 Maas-Maarten Zeeman + * Copyright 2011, 2012, 2013 Maas-Maarten Zeeman * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -326,8 +326,8 @@ 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)) { + /* Bind lists which have the same length as the binary as text. */ + if(enif_is_list(env, cell) && (strnlen((char *) the_blob.data, the_blob.size) == the_blob.size)) { return sqlite3_bind_text(stmt, i, (char *) the_blob.data, the_blob.size, SQLITE_TRANSIENT); } diff --git a/src/esqlite3.erl b/src/esqlite3.erl index 93f03cc..5493040 100644 --- a/src/esqlite3.erl +++ b/src/esqlite3.erl @@ -1,5 +1,5 @@ %% @author Maas-Maarten Zeeman -%% @copyright 2011, 2012 Maas-Maarten Zeeman +%% @copyright 2011, 2012, 2013 Maas-Maarten Zeeman %% @doc Erlang API for sqlite3 databases diff --git a/src/esqlite3_nif.erl b/src/esqlite3_nif.erl index 794a326..e1de979 100644 --- a/src/esqlite3_nif.erl +++ b/src/esqlite3_nif.erl @@ -1,9 +1,9 @@ %% @author Maas-Maarten Zeeman -%% @copyright 2011, 2012 Maas-Maarten Zeeman +%% @copyright 2011, 2012, 2013 Maas-Maarten Zeeman %% @doc Low level erlang API for sqlite3 databases -%% Copyright 2011, 2012 Maas-Maarten Zeeman +%% Copyright 2011, 2012, 2013 Maas-Maarten Zeeman %% %% Licensed under the Apache License, Version 2.0 (the "License"); %% you may not use this file except in compliance with the License. diff --git a/test/esqlite_test.erl b/test/esqlite_test.erl index 452a5d2..bd21975 100644 --- a/test/esqlite_test.erl +++ b/test/esqlite_test.erl @@ -51,26 +51,60 @@ prepare_test() -> bind_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\"", ",", "11" ");"], Db), - ok = esqlite3:exec(["insert into test_table values(", "\"hello3\"", ",", "12" ");"], Db), - ok = esqlite3:exec(["insert into test_table values(", "\"hello4\"", ",", "13" ");"], Db), ok = esqlite3:exec("commit;", Db), %% Create a prepared statement {ok, Statement} = esqlite3:prepare("insert into test_table values(?1, ?2)", Db), esqlite3:bind(Statement, [one, 2]), esqlite3:step(Statement), - esqlite3:bind(Statement, ["three", 4]), + esqlite3:bind(Statement, ["three", 4]), esqlite3:step(Statement), - esqlite3:bind(Statement, [<<"five">>, 6]), + esqlite3:bind(Statement, [<<"five">>, 6]), + esqlite3:step(Statement), + esqlite3:bind(Statement, [[<<"se">>, $v, "en"], 8]), % iolist bound as text + esqlite3:step(Statement), + esqlite3:bind(Statement, [[<<"nine">>], 10]), % iolist bound as text + esqlite3:step(Statement), + esqlite3:bind(Statement, [[<<"eleven">>, 0], 12]), % iolist bound as blob with trailing eos. esqlite3:step(Statement), - [{"one", 2}] = esqlite3:q("select * from test_table where two = '2'", Db), - [{"three", 4}] = esqlite3:q("select * from test_table where two = 4", Db), - [{<<"five">>, 6}] = esqlite3:q("select * from test_table where two = 6", Db), + ?assertEqual([{"one", 2}], + esqlite3:q("select one, two from test_table where two = '2'", Db)), + ?assertEqual([{"three", 4}], + esqlite3:q("select one, two from test_table where two = 4", Db)), + ?assertEqual([{<<"five">>, 6}], + esqlite3:q("select one, two from test_table where two = 6", Db)), + ?assertEqual([{"seven", 8}], + esqlite3:q("select one, two from test_table where two = 8", Db)), + ?assertEqual([{"nine", 10}], + esqlite3:q("select one, two from test_table where two = 10", Db)), + ?assertEqual([{<<$e,$l,$e,$v,$e,$n,0>>, 12}], + esqlite3:q("select one, two from test_table where two = 12", Db)), + + ok. + +bind_for_queries_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("commit;", 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)), + + %% Bound as blob... sqlite can't find the table then. + ?assertEqual([{0}], esqlite3:q(<<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>, + [<<"test_table">>], Db)), + + %% As list it is matched as text. + ?assertEqual([{1}], esqlite3:q(<<"SELECT count(type) FROM sqlite_master WHERE type='table' AND name=?;">>, + [[<<"test_table">>]], Db)), ok.