From 0331764f99b629da98e32cfe6d6fe1c22ff00ee5 Mon Sep 17 00:00:00 2001 From: Maas-Maarten Zeeman Date: Wed, 26 Oct 2011 23:38:14 +0200 Subject: [PATCH] Foundations for step. Exploring api --- c_src/esqlite_nif.c | 8 +++++++- test/esqlite_test.erl | 24 +++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/c_src/esqlite_nif.c b/c_src/esqlite_nif.c index 37a2f9c..70069c0 100644 --- a/c_src/esqlite_nif.c +++ b/c_src/esqlite_nif.c @@ -252,8 +252,14 @@ do_step(ErlNifEnv *env, esqlite_connection *conn, sqlite3_stmt *stmt) rc = sqlite3_step(stmt); + if(rc == SQLITE_DONE) + return make_atom(env, "$done"); + if(rc == SQLITE_BUSY) + return make_atom(env, "$busy"); + + /* Now prepare the output... */ fprintf(stderr, "step returned: %d\n", rc); - fprintf(stderr, "nu columns: %d\n", sqlite3_column_count(stmt)); + fprintf(stderr, "# columns: %d\n", sqlite3_column_count(stmt)); return _atom_ok; } diff --git a/test/esqlite_test.erl b/test/esqlite_test.erl index f1620f4..fb25e83 100644 --- a/test/esqlite_test.erl +++ b/test/esqlite_test.erl @@ -38,10 +38,28 @@ prepare_test() -> esqlite:exec(Db, "create table test_table(one varchar(10), two int);"), {ok, Statement} = esqlite:prepare(Db, "insert into test_table values(\"one\", 2)"), - esqlite:step(Statement), - + '$done' = esqlite:step(Statement), + {ok, St2} = esqlite:prepare(Db, "select * from test_table"), - esqlite:step(St2). + + [ok] = exec(St2), + ok. + +exec(Statement) -> + exec(Statement, [], 0). + +exec(_Statement, _Acc, Tries) when Tries > 5 -> + throw(too_many_tries); +exec(Statement, Acc, Tries) -> + case esqlite:step(Statement) of + '$done' -> + lists:reverse(Acc); + '$busy' -> + timer:sleep(100), + exec(Statement, Acc, Tries + 1); + V -> + exec(Statement, [V | Acc], 0) + end. %% esqlite:bind(Statement, ":one", "hello"), %% esqlite:bind(Statement, ":two", 11),