Now also binds text

This commit is contained in:
Maas-Maarten Zeeman
2011-11-04 21:17:34 +01:00
parent ecace408fe
commit 69bf43054d
2 changed files with 20 additions and 21 deletions

View File

@@ -236,18 +236,8 @@ bind_cell(ErlNifEnv *env, const ERL_NIF_TERM cell, sqlite3_stmt *stmt, unsigned
char the_atom[MAX_ATOM_LENGTH+1]; char the_atom[MAX_ATOM_LENGTH+1];
ErlNifBinary the_blob; 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 the error codes! */
/* TODO check for atom undefined */
if(enif_get_int(env, cell, &the_int)) { if(enif_get_int(env, cell, &the_int)) {
sqlite3_bind_int(stmt, i, the_int); sqlite3_bind_int(stmt, i, the_int);
return; 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)) { 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); sqlite3_bind_blob(stmt, i, the_blob.data, the_blob.size, SQLITE_TRANSIENT);
return; return;
} }
/* */
sqlite3_bind_int(stmt, i, i);
} }
static ERL_NIF_TERM static ERL_NIF_TERM

View File

@@ -62,24 +62,31 @@ bind_test() ->
{ok, Statement} = esqlite:prepare(Db, "insert into test_table values(?1, ?2)"), {ok, Statement} = esqlite:prepare(Db, "insert into test_table values(?1, ?2)"),
esqlite:bind(Statement, [one, 2]), esqlite:bind(Statement, [one, 2]),
esqlite:step(Statement), esqlite:step(Statement),
esqlite:bind(Statement, [three, 4]), esqlite:bind(Statement, ["three", 4]),
esqlite:step(Statement),
esqlite:bind(Statement, [<<"five">>, 6]),
esqlite:step(Statement), 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"), [{"three", 4}] = q(Db, "select * from test_table where two = 4"),
[{<<"five">>, 6}] = q(Db, "select * from test_table where two = 6"),
ok. ok.
%% Handy functions... %% Handy functions...
%% %%
q(Connection, Sql) ->
q(Db, Sql) when is_list(Sql) -> {ok, Statement} = esqlite:prepare(Connection, Sql),
{ok, Statement} = esqlite:prepare(Db, Sql),
exec(Statement). 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), esqlite:bind(Statement, Args),
exec(Statement). exec(Statement).
exec(Statement) -> exec(Statement) ->
exec(Statement, [], 0). exec(Statement, [], 0).