src/evsql_test.c
branchnew-evsql
changeset 45 424ce5ab82fd
parent 44 9e76ee9729b6
child 46 75cecfc4603b
--- a/src/evsql_test.c	Thu Nov 20 01:16:24 2008 +0200
+++ b/src/evsql_test.c	Fri Nov 28 23:46:11 2008 +0200
@@ -1,14 +1,15 @@
-
-#include <event2/event.h>
 
 #include "evsql.h"
 #include "lib/log.h"
 #include "lib/signals.h"
 #include "lib/misc.h"
 
+#include <event2/event.h>
+#include <assert.h>
+
 #define CONNINFO_DEFAULT "dbname=dbfs port=5433"
 
-void db_results (struct evsql_result *result, void *arg) {
+void query_results (struct evsql_result *result, void *arg) {
     uint32_t val;
 
     static struct evsql_result_info result_info = {
@@ -18,11 +19,24 @@
         }
     };
 
+    // begin
+    assert(evsql_result_begin(&result_info, result) == 0);
 
+    // one row
+    assert(evsql_result_next(result, 
+        &val
+    ) > 0);
+
+    // print
+    INFO("[evsql_test.results] got result: %p: val=%lu", result, (unsigned long) val);
+
+    // done
+    evsql_result_end(result);
 }
 
-void do_query (struct evsql *db) {
+void query_send (struct evsql *db, struct evsql_trans *trans) {
     struct evsql_query *query = NULL;
+    static int query_id = 0;
 
     static struct evsql_query_info query_info = {
         .sql    = "SELECT $1::int4 + 5",
@@ -34,10 +48,77 @@
     };
 
     // query
-    assert((query = evsql_query_exec(db, NULL, &query_info, (uint32_t) 4, db_results, NULL)) != NULL);
+    assert((query = evsql_query_exec(db, trans, &query_info, query_results, NULL,
+        (uint32_t) ++query_id
+    )) != NULL);
+
+    INFO("[evsql_test.query_send] enqueued query, trans=%p: %p: %d", trans, query, query_id);
 }
 
-int main (char argc, char **argv) {
+void trans_create_result (struct evsql_result *res, void *arg) {
+    // check
+    if (evsql_result_check(res))
+        FATAL("query failed: %s", evsql_result_error(res));
+    
+    INFO("[evsql_test.create_result] table created succesfully: %p", res);
+
+    // free
+    evsql_result_free(res);
+}
+
+void trans_create_query (struct evsql *db, struct evsql_trans *trans) {
+    struct evsql_query *query = NULL;
+
+    // the query info
+    static struct evsql_query_info query_info = {
+        .sql    = "CREATE TEMPORARY TABLE evsql_test ( id serial4, str varchar(32) DEFAULT $1::varchar ) ON COMMIT DROP",
+
+        .params = {
+            {   EVSQL_FMT_BINARY,   EVSQL_TYPE_STRING   },
+            {   0,                  0,                  }
+        }
+    };
+
+    // run the query
+    assert((query = evsql_query_exec(db, trans, &query_info, trans_create_result, NULL,
+        (const char *) "foobar"
+    )) != NULL);
+
+    INFO("[evsql_test.trans_create_query] enqueued query: %p", query);
+}
+
+void trans_error (struct evsql_trans *trans, void *arg) {
+    struct evsql *db = arg;
+
+    FATAL("[evsql_test.trans_error] failure: %s", evsql_trans_error(trans));
+}
+
+void trans_ready (struct evsql_trans *trans, void *arg) {
+    struct evsql *db = arg;
+
+    INFO("[evsql_test.trans_ready] ready");
+    
+    trans_create_query(db, trans);
+}
+
+void trans_done (struct evsql_trans *trans, void *arg) {
+    struct evsql *db = arg;
+
+    INFO("[evsql_test.trans_done] done");
+}
+
+void begin_transaction (struct evsql *db) {
+    struct evsql_trans *trans;
+
+    assert((trans = evsql_trans(db, EVSQL_TRANS_DEFAULT, 
+        &trans_error, &trans_ready, &trans_done,
+        db
+    )) != NULL);
+
+    INFO("[evsql_test.begin_trans] created transaction");
+ }
+
+int main (int argc, char **argv) {
     struct event_base *ev_base = NULL;
     struct signals *signals = NULL;
     struct evsql *db = NULL;
@@ -59,6 +140,15 @@
     if ((db = evsql_new_pq(ev_base, db_conninfo, NULL, NULL)) == NULL)
         ERROR("evsql_new_pq");
 
+    // send query
+    query_send(db, NULL);
+
+    // being transaction
+    begin_transaction(db);
+
+    // send query
+    query_send(db, NULL);
+
     // run libevent
     INFO("running libevent loop");