Prepare is now possible

This commit is contained in:
Maas-Maarten Zeeman
2011-10-24 23:32:25 +02:00
parent 5139fe05ed
commit f21f022eb7
2 changed files with 8 additions and 8 deletions

View File

@@ -221,6 +221,7 @@ do_prepare(ErlNifEnv *env, esqlite_connection *conn, const ERL_NIF_TERM arg)
ErlNifBinary bin; ErlNifBinary bin;
esqlite_statement *stmt; esqlite_statement *stmt;
ERL_NIF_TERM esqlite_stmt; ERL_NIF_TERM esqlite_stmt;
const char *tail;
int rc; int rc;
if(!conn->db) if(!conn->db)
@@ -234,11 +235,14 @@ do_prepare(ErlNifEnv *env, esqlite_connection *conn, const ERL_NIF_TERM arg)
if(!stmt) if(!stmt)
return make_error_tuple(env, "no_memory"); return make_error_tuple(env, "no_memory");
rc = sqlite3_prepare_v2(conn->db, (char *) bin.data, bin.size, &(stmt->statement), &tail);
if(rc != SQLITE_OK)
return make_error_tuple(env, sqlite3_errmsg(conn->db));
/* Keep a reference to the connection */ /* Keep a reference to the connection */
enif_keep_resource(conn); enif_keep_resource(conn);
stmt->connection = conn; stmt->connection = conn;
stmt->statement = NULL; stmt->statement = NULL;
// sqlite3_prepare_v2(conn->db, (char *) bin.data, &(stmt->statement), NULL);
esqlite_stmt = enif_make_resource(env, stmt); esqlite_stmt = enif_make_resource(env, stmt);
enif_release_resource(stmt); enif_release_resource(stmt);
@@ -428,7 +432,7 @@ esqlite_exec_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
} }
/* /*
* Execute the sql statement * Prepare the sql statement
*/ */
static ERL_NIF_TERM static ERL_NIF_TERM
esqlite_prepare_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) esqlite_prepare_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
@@ -437,8 +441,6 @@ esqlite_prepare_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
esqlite_command *cmd = NULL; esqlite_command *cmd = NULL;
ErlNifPid pid; ErlNifPid pid;
fprintf(stderr, "prepare\n");
if(argc != 4) if(argc != 4)
return enif_make_badarg(env); return enif_make_badarg(env);
if(!enif_get_resource(env, argv[0], esqlite_connection_type, (void **) &conn)) if(!enif_get_resource(env, argv[0], esqlite_connection_type, (void **) &conn))
@@ -460,8 +462,6 @@ esqlite_prepare_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
if(!queue_push(conn->commands, cmd)) if(!queue_push(conn->commands, cmd))
return make_error_tuple(env, "command_push_failed"); return make_error_tuple(env, "command_push_failed");
fprintf(stderr, "command pushed");
return _atom_ok; return _atom_ok;
} }

View File

@@ -36,7 +36,7 @@ prepare_test() ->
{ok, Db} = esqlite:open(":memory:"), {ok, Db} = esqlite:open(":memory:"),
esqlite:exec(Db, "begin;"), esqlite:exec(Db, "begin;"),
esqlite:exec(Db, "create table test_table(one varchar(10), two int);"), esqlite:exec(Db, "create table test_table(one varchar(10), two int);"),
{ok, Statement} = esqlite:prepare(Db, "insert into_test_table(:one, :two)"). {ok, Statement} = esqlite:prepare(Db, "insert into test_table values(:one, :two)").
%% esqlite:bind(Statement, ":one", "hello"), %% esqlite:bind(Statement, ":one", "hello"),
%% esqlite:bind(Statement, ":two", 11), %% esqlite:bind(Statement, ":two", 11),