Check for issue #2. No problem... Fixes #2

This commit is contained in:
Maas-Maarten Zeeman
2013-01-01 14:42:43 +01:00
parent d5cb6b103d
commit ec04d67632
4 changed files with 49 additions and 15 deletions

View File

@@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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)) { if(enif_inspect_iolist_as_binary(env, cell, &the_blob)) {
/* Bind lists which have the same length as the binary as text */ /* 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)) { 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); return sqlite3_bind_text(stmt, i, (char *) the_blob.data, the_blob.size, SQLITE_TRANSIENT);
} }

View File

@@ -1,5 +1,5 @@
%% @author Maas-Maarten Zeeman <mmzeeman@xs4all.nl> %% @author Maas-Maarten Zeeman <mmzeeman@xs4all.nl>
%% @copyright 2011, 2012 Maas-Maarten Zeeman %% @copyright 2011, 2012, 2013 Maas-Maarten Zeeman
%% @doc Erlang API for sqlite3 databases %% @doc Erlang API for sqlite3 databases

View File

@@ -1,9 +1,9 @@
%% @author Maas-Maarten Zeeman <mmzeeman@xs4all.nl> %% @author Maas-Maarten Zeeman <mmzeeman@xs4all.nl>
%% @copyright 2011, 2012 Maas-Maarten Zeeman %% @copyright 2011, 2012, 2013 Maas-Maarten Zeeman
%% @doc Low level erlang API for sqlite3 databases %% @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"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.

View File

@@ -51,26 +51,60 @@ prepare_test() ->
bind_test() -> bind_test() ->
{ok, Db} = esqlite3:open(":memory:"), {ok, Db} = esqlite3:open(":memory:"),
ok = esqlite3:exec("begin;", Db), ok = esqlite3:exec("begin;", Db),
ok = esqlite3:exec("create table test_table(one varchar(10), two int);", 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), ok = esqlite3:exec("commit;", Db),
%% Create a prepared statement %% Create a prepared statement
{ok, Statement} = esqlite3:prepare("insert into test_table values(?1, ?2)", Db), {ok, Statement} = esqlite3:prepare("insert into test_table values(?1, ?2)", Db),
esqlite3:bind(Statement, [one, 2]), esqlite3:bind(Statement, [one, 2]),
esqlite3:step(Statement), esqlite3:step(Statement),
esqlite3:bind(Statement, ["three", 4]), esqlite3:bind(Statement, ["three", 4]),
esqlite3:step(Statement), 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), esqlite3:step(Statement),
[{"one", 2}] = esqlite3:q("select * from test_table where two = '2'", Db), ?assertEqual([{"one", 2}],
[{"three", 4}] = esqlite3:q("select * from test_table where two = 4", Db), esqlite3:q("select one, two from test_table where two = '2'", Db)),
[{<<"five">>, 6}] = esqlite3:q("select * from test_table where two = 6", 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. ok.