gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r7822 - in GNUnet/src: applications/dstore_mysql applicatio


From: gnunet
Subject: [GNUnet-SVN] r7822 - in GNUnet/src: applications/dstore_mysql applications/sqstore_mysql include
Date: Sun, 2 Nov 2008 15:56:27 -0700 (MST)

Author: grothoff
Date: 2008-11-02 15:56:27 -0700 (Sun, 02 Nov 2008)
New Revision: 7822

Modified:
   GNUnet/src/applications/dstore_mysql/dstore_mysql.c
   GNUnet/src/applications/sqstore_mysql/Makefile.am
   GNUnet/src/applications/sqstore_mysql/mysql.c
   GNUnet/src/include/gnunet_mysql.h
Log:
towards fixing Mantis 1413/1403

Modified: GNUnet/src/applications/dstore_mysql/dstore_mysql.c
===================================================================
--- GNUnet/src/applications/dstore_mysql/dstore_mysql.c 2008-11-02 22:45:18 UTC 
(rev 7821)
+++ GNUnet/src/applications/dstore_mysql/dstore_mysql.c 2008-11-02 22:56:27 UTC 
(rev 7822)
@@ -30,7 +30,7 @@
 #include "gnunet_util.h"
 #include "gnunet_dstore_service.h"
 #include "gnunet_stats_service.h"
-#include <mysql/mysql.h>
+#include "gnunet_mysql.h"
 
 #define DEBUG_DSTORE GNUNET_NO
 
@@ -72,169 +72,78 @@
 static char *bloom_name;
 
 /**
- * Path to MySQL configuration file.
- */
-static char *cnffile;
-
-/**
  * Handle for the MySQL database.
  */
-static MYSQL *dbf;
+static struct GNUNET_MysqlDatabaseHandle *db;
 
 
 #define SELECT_VALUE_STMT "SELECT size, value FROM gn080dstore FORCE INDEX 
(hashidx) WHERE hash=? AND type=? AND expire >= ? LIMIT 1 OFFSET ?"
-static MYSQL_STMT *select_value;
+static struct GNUNET_MysqlStatementHandle *select_value;
 
 #define COUNT_VALUE_STMT "SELECT count(*) FROM gn080dstore FORCE INDEX 
(hashidx) WHERE hash=? AND type=? AND expire >= ?"
-static MYSQL_STMT *count_value;
+static struct GNUNET_MysqlStatementHandle *count_value;
 
 #define SELECT_OLD_VALUE_STMT "SELECT hash, vhash, type, size, value FROM 
gn080dstore FORCE INDEX (expireidx) ORDER BY puttime ASC LIMIT 1"
-static MYSQL_STMT *select_old_value;
+static struct GNUNET_MysqlStatementHandle *select_old_value;
 
 #define DELETE_VALUE_STMT "DELETE FROM gn080dstore WHERE hash = ? AND vhash = 
? AND type = ? AND "\
                           "size = ? AND value = ?"
-static MYSQL_STMT *delete_value;
+static struct GNUNET_MysqlStatementHandle *delete_value;
 
 #define INSERT_VALUE_STMT "INSERT INTO gn080dstore (size, type, puttime, 
expire, hash, vhash, value) "\
                           "VALUES (?, ?, ?, ?, ?, ?, ?)"
-static MYSQL_STMT *insert_value;
+static struct GNUNET_MysqlStatementHandle *insert_value;
 
 #define UPDATE_VALUE_STMT "UPDATE gn080dstore FORCE INDEX (allidx) SET 
puttime=?, expire=? "\
                           "WHERE hash=? AND vhash=? AND type=? AND size=?"
-static MYSQL_STMT *update_value;
+static struct GNUNET_MysqlStatementHandle *update_value;
 
-/**
- * Die with an error message that indicates
- * a failure of the command 'cmd' with the message given
- * by strerror(errno).
- */
-#define DIE_MYSQL(cmd, dbh) do { GNUNET_GE_LOG(coreAPI->ectx, GNUNET_GE_FATAL 
| GNUNET_GE_ADMIN | GNUNET_GE_IMMEDIATE, _("`%s' failed at %s:%d with error: 
%s\n"), cmd, __FILE__, __LINE__, mysql_error((dbh)); abort(); } while(0);
 
-/**
- * Log an error message at log-level 'level' that indicates
- * a failure of the command 'cmd' on file 'filename'
- * with the message given by strerror(errno).
- */
-#define LOG_MYSQL(level, cmd, dbh) do { GNUNET_GE_LOG(coreAPI->ectx, level, 
_("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, 
mysql_error((dbh))); } while(0);
-
-
-/**
- * Close the database connection.
- */
 static int
-iclose ()
+iopen ()
 {
-#define PEND(h) if (h != NULL) { mysql_stmt_close(h); h = NULL; } else {}
-  if (dbf == NULL)
+  if (db != NULL)
+    return GNUNET_OK;
+  db = GNUNET_MYSQL_database_open(coreAPI->ectx,
+                                 coreAPI->cfg);
+  if (db == NULL)
     return GNUNET_SYSERR;
-  PEND (select_value);
-  PEND (count_value);
-  PEND (select_old_value);
-  PEND (delete_value);
-  PEND (insert_value);
-  PEND (update_value);
-#undef PEND
-  mysql_close (dbf);
-  payload = 0;
-  dbf = NULL;
+#define MRUNS(a) (GNUNET_OK != GNUNET_MYSQL_run_statement (db, a) )
+#define PINIT(a,b) (NULL == (a = GNUNET_MYSQL_prepared_statement_create(db, 
b)))
+  if ( MRUNS("DROP TABLE gn080dstore") ||
+       MRUNS("CREATE TEMPORARY TABLE gn080dstore ("
+            "  size INT(11) UNSIGNED NOT NULL DEFAULT 0,"
+            "  type INT(11) UNSIGNED NOT NULL DEFAULT 0,"
+            "  puttime BIGINT UNSIGNED NOT NULL DEFAULT 0,"
+            "  expire BIGINT UNSIGNED NOT NULL DEFAULT 0,"
+            "  hash BINARY(64) NOT NULL DEFAULT '',"
+            "  vhash BINARY(64) NOT NULL DEFAULT '',"
+            "  value BLOB NOT NULL DEFAULT '',"
+            "  INDEX hashidx (hash(64),type,expire),"
+            "  INDEX allidx (hash(64),vhash(64),type,size),"
+            "  INDEX expireidx (puttime)" ") ENGINE=InnoDB") ||
+       MRUNS("SET AUTOCOMMIT = 1") ||
+       PINIT (select_value, SELECT_VALUE_STMT) ||
+       PINIT (count_value, COUNT_VALUE_STMT) ||
+       PINIT (select_old_value, SELECT_OLD_VALUE_STMT) ||
+       PINIT (delete_value, DELETE_VALUE_STMT) ||
+       PINIT (insert_value, INSERT_VALUE_STMT) ||
+       PINIT (update_value, UPDATE_VALUE_STMT) )
+    {
+      GNUNET_MYSQL_database_close(db);
+      db = NULL;
+      return GNUNET_SYSERR; 
+    }
+#undef PINIT
+#undef MRUNS
   return GNUNET_OK;
 }
 
-/**
- * Initiate the database connection.
- *
- * @return GNUNET_OK on success
- */
 static int
-iopen ()
+return_ok(void * cls,
+         unsigned int num_values,
+         MYSQL_BIND * values)
 {
-  char *dbname;
-  my_bool reconnect = 0;
-  unsigned int timeout = 60;    /* in seconds */
-
-  if (dbf != NULL)
-    return GNUNET_OK;
-  if (cnffile == NULL)
-    {
-      GNUNET_GE_BREAK (NULL, 0);
-      return GNUNET_SYSERR;
-    }
-  dbf = mysql_init (NULL);
-  if (dbf == NULL)
-    {
-      GNUNET_GE_BREAK (NULL, 0);
-      return GNUNET_SYSERR;
-    }
-  mysql_options (dbf, MYSQL_READ_DEFAULT_FILE, cnffile);
-  mysql_options (dbf, MYSQL_READ_DEFAULT_GROUP, "client");
-  mysql_options (dbf, MYSQL_OPT_RECONNECT, &reconnect);
-  mysql_options (dbf, MYSQL_OPT_CONNECT_TIMEOUT, (const void *) &timeout);
-  mysql_options (dbf, MYSQL_OPT_READ_TIMEOUT, (const void *) &timeout);
-  mysql_options (dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *) &timeout);
-
-  dbname = NULL;
-  GNUNET_GC_get_configuration_value_string (coreAPI->cfg,
-                                            "MYSQL", "DATABASE", "gnunet",
-                                            &dbname);
-  mysql_real_connect (dbf, NULL, NULL, NULL, dbname, 0, NULL, 0);
-  GNUNET_free (dbname);
-  if (mysql_error (dbf)[0])
-    {
-      LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                 "mysql_real_connect", dbf);
-      iclose ();
-      return GNUNET_SYSERR;
-    }
-  mysql_query (dbf,
-               "SET SESSION net_read_timeout=60, SESSION 
net_write_timeout=60");
-  if (mysql_error (dbf)[0])
-    {
-      LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                 "mysql_query", dbf);
-      iclose ();
-      return GNUNET_SYSERR;
-    }
-
-  mysql_query (dbf, "DROP TABLE gn080dstore");
-  mysql_query (dbf,
-               "CREATE TEMPORARY TABLE gn080dstore ("
-               "  size INT(11) UNSIGNED NOT NULL DEFAULT 0,"
-               "  type INT(11) UNSIGNED NOT NULL DEFAULT 0,"
-               "  puttime BIGINT UNSIGNED NOT NULL DEFAULT 0,"
-               "  expire BIGINT UNSIGNED NOT NULL DEFAULT 0,"
-               "  hash BINARY(64) NOT NULL DEFAULT '',"
-               "  vhash BINARY(64) NOT NULL DEFAULT '',"
-               "  value BLOB NOT NULL DEFAULT '',"
-               "  INDEX hashidx (hash(64),type,expire),"
-               "  INDEX allidx (hash(64),vhash(64),type,size),"
-               "  INDEX expireidx (puttime)" ") ENGINE=InnoDB");
-  if (mysql_error (dbf)[0])
-    {
-      LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                 "mysql_query", dbf);
-      iclose ();
-      return GNUNET_SYSERR;
-    }
-  mysql_query (dbf, "SET AUTOCOMMIT = 1");
-  if (mysql_error (dbf)[0])
-    {
-      LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                 "mysql_query", dbf);
-      iclose ();
-      return GNUNET_SYSERR;
-    }
-#define PINIT(a,b) a = mysql_stmt_init(dbf); if (a == NULL) { iclose(); return 
GNUNET_SYSERR; } else { \
-    if (mysql_stmt_prepare (a, b, strlen(b))) { \
-      GNUNET_GE_LOG (coreAPI->ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | 
GNUNET_GE_USER, \
-             _("`%s' failed at %s:%d with error: %s"), "mysql_stmt_prepare", 
__FILE__, __LINE__, \
-             mysql_stmt_error (a));  iclose(); return GNUNET_SYSERR; } }
-  PINIT (select_value, SELECT_VALUE_STMT);
-  PINIT (count_value, COUNT_VALUE_STMT);
-  PINIT (select_old_value, SELECT_OLD_VALUE_STMT);
-  PINIT (delete_value, DELETE_VALUE_STMT);
-  PINIT (insert_value, INSERT_VALUE_STMT);
-  PINIT (update_value, UPDATE_VALUE_STMT);
-#undef PINIT
   return GNUNET_OK;
 }
 
@@ -286,79 +195,42 @@
   rbind[4].buffer_length = GNUNET_MAX_BUFFER_SIZE;
   rbind[4].length = &v_length;
   rbind[4].buffer = GNUNET_malloc (GNUNET_MAX_BUFFER_SIZE);
-
-  GNUNET_mutex_lock (lock);
-  mysql_thread_init ();
-  if (mysql_stmt_execute (select_old_value))
+  if ( (GNUNET_OK !=
+       GNUNET_MYSQL_prepared_statement_run_select(select_old_value,
+                                                  5,
+                                                  rbind,
+                                                  return_ok,
+                                                  NULL,
+                                                  -1)) ||
+       (GNUNET_OK !=
+       GNUNET_MYSQL_prepared_statement_run(delete_value,
+                                           NULL,
+                                           MYSQL_TYPE_BLOB,
+                                           &v_key,
+                                           sizeof(GNUNET_HashCode),
+                                           &k_length,
+                                           MYSQL_TYPE_BLOB,
+                                           &vhash,
+                                           sizeof(GNUNET_HashCode),
+                                           &h_length,
+                                           MYSQL_TYPE_LONG,
+                                           &v_type,
+                                           GNUNET_YES,
+                                           MYSQL_TYPE_LONG,
+                                           &v_size,
+                                           GNUNET_YES,
+                                           MYSQL_TYPE_BLOB,
+                                           rbind[4].buffer,
+                                           (unsigned long long) 
GNUNET_MAX_BUFFER_SIZE,
+                                           &v_length,
+                                           -1)) )
     {
-      GNUNET_GE_LOG (coreAPI->ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_execute",
-                     __FILE__, __LINE__, mysql_stmt_error (select_old_value));
       GNUNET_free (rbind[4].buffer);
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
       return GNUNET_SYSERR;
     }
-  GNUNET_GE_ASSERT (coreAPI->ectx,
-                    mysql_stmt_field_count (select_old_value) == 5);
-  if (mysql_stmt_bind_result (select_old_value, rbind))
-    {
-      GNUNET_GE_LOG (coreAPI->ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_result",
-                     __FILE__, __LINE__, mysql_stmt_error (select_old_value));
-      GNUNET_free (rbind[4].buffer);
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  if (mysql_stmt_fetch (select_old_value))
-    {
-      /* odd -- over quota but cannot select old!? */
-      GNUNET_free (rbind[4].buffer);
-      mysql_stmt_reset (select_old_value);
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  mysql_stmt_reset (select_old_value);
-  if (mysql_stmt_bind_param (delete_value, rbind))
-    {
-      GNUNET_GE_LOG (coreAPI->ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_param",
-                     __FILE__, __LINE__, mysql_stmt_error (delete_value));
-      GNUNET_free (rbind[4].buffer);
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  GNUNET_GE_BREAK (NULL, h_length == sizeof (GNUNET_HashCode));
-
-  if (mysql_stmt_execute (delete_value))
-    {
-      GNUNET_GE_LOG (coreAPI->ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_execute",
-                     __FILE__, __LINE__, mysql_stmt_error (delete_value));
-      GNUNET_free (rbind[4].buffer);
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
   GNUNET_free (rbind[4].buffer);
+  GNUNET_mutex_lock (lock);
   payload -= v_length + OVERHEAD;
-  mysql_stmt_reset (delete_value);
-  mysql_thread_end ();
   GNUNET_mutex_unlock (lock);
   if (bloom != NULL)
     GNUNET_bloomfilter_remove (bloom, &v_key);
@@ -377,7 +249,6 @@
        unsigned int type,
        GNUNET_CronTime discard_time, unsigned int size, const char *data)
 {
-  MYSQL_BIND rbind[7];
   GNUNET_CronTime now;
   unsigned long k_length;
   unsigned long h_length;
@@ -387,116 +258,74 @@
   if (size > MAX_CONTENT_SIZE)
     return GNUNET_SYSERR;
   GNUNET_hash (data, size, &vhash);
-  GNUNET_mutex_lock (lock);
-  mysql_thread_init ();
-  iopen ();
   now = GNUNET_get_time ();
 
   /* first try UPDATE */
   h_length = sizeof (GNUNET_HashCode);
   k_length = sizeof (GNUNET_HashCode);
   v_length = size;
-  memset (rbind, 0, sizeof (rbind));
-  rbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
-  rbind[0].is_unsigned = 1;
-  rbind[0].buffer = &now;
-  rbind[1].buffer_type = MYSQL_TYPE_LONGLONG;
-  rbind[1].is_unsigned = 1;
-  rbind[1].buffer = &discard_time;
-  rbind[2].buffer_type = MYSQL_TYPE_BLOB;
-  rbind[2].buffer_length = sizeof (GNUNET_HashCode);
-  rbind[2].length = &k_length;
-  rbind[2].buffer = (void *) key;
-  rbind[3].buffer_type = MYSQL_TYPE_BLOB;
-  rbind[3].buffer_length = sizeof (GNUNET_HashCode);
-  rbind[3].length = &h_length;
-  rbind[3].buffer = &vhash;
-  rbind[4].buffer_type = MYSQL_TYPE_LONG;
-  rbind[4].is_unsigned = 1;
-  rbind[4].buffer = &type;
-  rbind[5].buffer_type = MYSQL_TYPE_LONG;
-  rbind[5].is_unsigned = 1;
-  rbind[5].buffer = &size;
+  if (GNUNET_OK ==
+      GNUNET_MYSQL_prepared_statement_run(update_value,
+                                         NULL,
+                                         MYSQL_TYPE_LONGLONG,
+                                         &now,
+                                         GNUNET_YES,
+                                         MYSQL_TYPE_LONGLONG,
+                                         &discard_time,
+                                         GNUNET_YES,
+                                         MYSQL_TYPE_BLOB,
+                                         key,
+                                         sizeof(GNUNET_HashCode),
+                                         &k_length,
+                                         MYSQL_TYPE_BLOB,
+                                         &vhash,
+                                         sizeof(GNUNET_HashCode),
+                                         &h_length,
+                                         MYSQL_TYPE_LONG,
+                                         &type,
+                                         GNUNET_YES,
+                                         MYSQL_TYPE_LONG,
+                                         &size,
+                                         GNUNET_YES,
+                                         -1))
+    return GNUNET_OK;    
 
-  if (mysql_stmt_bind_param (update_value, rbind))
-    {
-      GNUNET_GE_LOG (coreAPI->ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_param",
-                     __FILE__, __LINE__, mysql_stmt_error (update_value));
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  if (mysql_stmt_execute (update_value))
-    {
-      mysql_stmt_reset (update_value);
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_OK;
-    }
-  mysql_stmt_reset (update_value);
   /* now try INSERT */
-
   h_length = sizeof (GNUNET_HashCode);
   k_length = sizeof (GNUNET_HashCode);
   v_length = size;
-  memset (rbind, 0, sizeof (rbind));
-  rbind[0].buffer_type = MYSQL_TYPE_LONG;
-  rbind[0].is_unsigned = 1;
-  rbind[0].buffer = &size;
-  rbind[1].buffer_type = MYSQL_TYPE_LONG;
-  rbind[1].is_unsigned = 1;
-  rbind[1].buffer = &type;
-  rbind[2].buffer_type = MYSQL_TYPE_LONGLONG;
-  rbind[2].is_unsigned = 1;
-  rbind[2].buffer = &now;
-  rbind[3].buffer_type = MYSQL_TYPE_LONGLONG;
-  rbind[3].is_unsigned = 1;
-  rbind[3].buffer = &discard_time;
-  rbind[4].buffer_type = MYSQL_TYPE_BLOB;
-  rbind[4].buffer_length = sizeof (GNUNET_HashCode);
-  rbind[4].length = &k_length;
-  rbind[4].buffer = (void *) key;
-  rbind[5].buffer_type = MYSQL_TYPE_BLOB;
-  rbind[5].buffer_length = sizeof (GNUNET_HashCode);
-  rbind[5].length = &h_length;
-  rbind[5].buffer = &vhash;
-  rbind[6].buffer_type = MYSQL_TYPE_BLOB;
-  rbind[6].buffer_length = size;
-  rbind[6].length = &v_length;
-  rbind[6].buffer = (void *) data;
-
-  if (mysql_stmt_bind_param (insert_value, rbind))
-    {
-      GNUNET_GE_LOG (coreAPI->ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_param",
-                     __FILE__, __LINE__, mysql_stmt_error (insert_value));
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  if (mysql_stmt_execute (insert_value))
-    {
-      GNUNET_GE_LOG (coreAPI->ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_execute",
-                     __FILE__, __LINE__, mysql_stmt_error (insert_value));
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  mysql_stmt_reset (insert_value);
-  mysql_thread_end ();
+  if (GNUNET_OK !=
+      GNUNET_MYSQL_prepared_statement_run(update_value,
+                                         NULL,
+                                         MYSQL_TYPE_LONG,
+                                         &size,
+                                         GNUNET_YES,
+                                         MYSQL_TYPE_LONG,
+                                         &type,
+                                         GNUNET_YES,
+                                         MYSQL_TYPE_LONGLONG,
+                                         &now,
+                                         GNUNET_YES,
+                                         MYSQL_TYPE_LONGLONG,
+                                         &discard_time,
+                                         GNUNET_YES,
+                                         MYSQL_TYPE_BLOB,
+                                         key,
+                                         sizeof(GNUNET_HashCode),
+                                         &k_length,
+                                         MYSQL_TYPE_BLOB,
+                                         &vhash,
+                                         sizeof(GNUNET_HashCode),
+                                         &h_length,
+                                         MYSQL_TYPE_BLOB,
+                                         data,
+                                         (unsigned long) size,
+                                         &v_length,
+                                         -1))
+    return GNUNET_SYSERR;
   if (bloom != NULL)
     GNUNET_bloomfilter_add (bloom, key);
+  GNUNET_mutex_lock (lock);
   payload += size + OVERHEAD;
   GNUNET_mutex_unlock (lock);
   checkQuota ();
@@ -519,7 +348,6 @@
 d_get (const GNUNET_HashCode * key,
        unsigned int type, GNUNET_ResultProcessor handler, void *closure)
 {
-  MYSQL_BIND qbind[4];
   MYSQL_BIND rbind[2];
   unsigned int v_size;
   unsigned long h_length;
@@ -535,182 +363,79 @@
       GNUNET_mutex_unlock (lock);
       return 0;
     }
-#if DEBUG_DSTORE
-  GNUNET_GE_LOG (coreAPI->ectx,
-                 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_DEVELOPER,
-                 "dstore processes get\n");
-#endif
   now = GNUNET_get_time ();
-
   h_length = sizeof (GNUNET_HashCode);
   v_length = GNUNET_MAX_BUFFER_SIZE;
-  memset (qbind, 0, sizeof (qbind));
-  qbind[0].buffer_type = MYSQL_TYPE_BLOB;
-  qbind[0].buffer_length = sizeof (GNUNET_HashCode);
-  qbind[0].length = &h_length;
-  qbind[0].buffer = (void *) key;
-  qbind[1].buffer_type = MYSQL_TYPE_LONG;
-  qbind[1].is_unsigned = 1;
-  qbind[1].buffer = &type;
-  qbind[2].buffer_type = MYSQL_TYPE_LONGLONG;
-  qbind[2].is_unsigned = 1;
-  qbind[2].buffer = &now;
-
   total = -1;
   memset (rbind, 0, sizeof (rbind));
   rbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
   rbind[0].buffer = &total;
   rbind[0].is_unsigned = GNUNET_YES;
-
-  mysql_thread_init ();
-  iopen ();
-
-  if (mysql_stmt_bind_param (count_value, qbind))
-    {
-      GNUNET_GE_LOG (coreAPI->ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_param",
-                     __FILE__, __LINE__, mysql_stmt_error (count_value));
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  if (mysql_stmt_execute (count_value))
-    {
-      GNUNET_GE_LOG (coreAPI->ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_execute",
-                     __FILE__, __LINE__, mysql_stmt_error (count_value));
-      iclose ();
-      GNUNET_mutex_unlock (lock);
-      mysql_thread_end ();
-      return GNUNET_SYSERR;
-    }
-
-
-  if (mysql_stmt_bind_result (count_value, rbind))
-    {
-      GNUNET_GE_LOG (coreAPI->ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_result",
-                     __FILE__, __LINE__, mysql_stmt_error (count_value));
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  if (0 != mysql_stmt_fetch (count_value))
-    {
-      GNUNET_GE_LOG (coreAPI->ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_fetch",
-                     __FILE__, __LINE__, mysql_stmt_error (count_value));
-      mysql_stmt_reset (count_value);
-      iclose ();
-      GNUNET_mutex_unlock (lock);
-      mysql_thread_end ();
-      return GNUNET_SYSERR;
-    }
-  if (-1 == total)
-    {
-      GNUNET_GE_LOG (coreAPI->ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_fetch",
-                     __FILE__, __LINE__, mysql_stmt_error (count_value));
-      iclose ();
-      GNUNET_mutex_unlock (lock);
-      mysql_thread_end ();
-      return GNUNET_SYSERR;
-    }
-  mysql_stmt_reset (count_value);
+  if ( (GNUNET_OK !=
+       GNUNET_MYSQL_prepared_statement_run_select(count_value,
+                                                1,
+                                                  rbind,
+                                                  return_ok,
+                                                  NULL,                        
                         
+                                                  MYSQL_TYPE_BLOB,
+                                                  key,
+                                                  sizeof(GNUNET_HashCode),
+                                                  &h_length,
+                                                  MYSQL_TYPE_LONG,
+                                                  &type,
+                                                  GNUNET_YES,
+                                                  MYSQL_TYPE_LONGLONG,
+                                                  &now,
+                                                  GNUNET_YES,
+                                                  -1)) ||
+       (-1 == total) )
+    return GNUNET_SYSERR;    
   if ((handler == NULL) || (total == 0))
-    {
-      GNUNET_mutex_unlock (lock);
-      mysql_thread_end ();
-      return (int) total;
-    }
+    return (int) total;    
 
   off = GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, total);
-  qbind[3].buffer_type = MYSQL_TYPE_LONG;
-  qbind[3].is_unsigned = 1;
-  qbind[3].buffer = &off;
-
+  memset (rbind, 0, sizeof (rbind));
+  rbind[0].buffer_type = MYSQL_TYPE_LONG;
+  rbind[0].is_unsigned = 1;
+  rbind[0].buffer = &v_size;
+  rbind[1].buffer_type = MYSQL_TYPE_BLOB;
+  rbind[1].buffer_length = GNUNET_MAX_BUFFER_SIZE;
+  rbind[1].length = &v_length;
+  rbind[1].buffer = GNUNET_malloc (GNUNET_MAX_BUFFER_SIZE);
   cnt = 0;
   while (cnt < total)
     {
       off = (off + 1) % total;
-      if (mysql_stmt_bind_param (select_value, qbind))
-        {
-          GNUNET_GE_LOG (coreAPI->ectx,
-                         GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                         _("`%s' failed at %s:%d with error: %s\n"),
-                         "mysql_stmt_bind_param",
-                         __FILE__, __LINE__, mysql_stmt_error (select_value));
-          iclose ();
-          mysql_thread_end ();
-          GNUNET_mutex_unlock (lock);
-          return GNUNET_SYSERR;
-        }
-      if (mysql_stmt_execute (select_value))
-        {
-          GNUNET_GE_LOG (coreAPI->ectx,
-                         GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                         _("`%s' failed at %s:%d with error: %s\n"),
-                         "mysql_stmt_execute",
-                         __FILE__, __LINE__, mysql_stmt_error (select_value));
-          iclose ();
-          GNUNET_mutex_unlock (lock);
-          mysql_thread_end ();
-          return GNUNET_SYSERR;
-        }
-      memset (rbind, 0, sizeof (rbind));
-      rbind[0].buffer_type = MYSQL_TYPE_LONG;
-      rbind[0].is_unsigned = 1;
-      rbind[0].buffer = &v_size;
-      rbind[1].buffer_type = MYSQL_TYPE_BLOB;
-      rbind[1].buffer_length = GNUNET_MAX_BUFFER_SIZE;
-      rbind[1].length = &v_length;
-      rbind[1].buffer = GNUNET_malloc (GNUNET_MAX_BUFFER_SIZE);
-      if (mysql_stmt_bind_result (select_value, rbind))
-        {
-          GNUNET_GE_LOG (coreAPI->ectx,
-                         GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                         _("`%s' failed at %s:%d with error: %s\n"),
-                         "mysql_stmt_bind_result",
-                         __FILE__, __LINE__, mysql_stmt_error (select_value));
-          iclose ();
-          mysql_thread_end ();
-          GNUNET_mutex_unlock (lock);
+      if ( (GNUNET_OK !=
+           GNUNET_MYSQL_prepared_statement_run_select(select_value,
+                                                      2,
+                                                      rbind,
+                                                      return_ok,
+                                                      NULL,                    
                         
+                                                      MYSQL_TYPE_BLOB,
+                                                      key,
+                                                      sizeof(GNUNET_HashCode),
+                                                      &h_length,
+                                                      MYSQL_TYPE_LONG,
+                                                      &type,
+                                                      GNUNET_YES,
+                                                      MYSQL_TYPE_LONGLONG,
+                                                      &now,
+                                                      GNUNET_YES,
+                                                      MYSQL_TYPE_LONG,
+                                                      &off,
+                                                      GNUNET_YES,
+                                                      -1)) ||
+          (v_length != v_size) )
+       {
+          GNUNET_GE_BREAK (NULL, v_length == v_size);
           GNUNET_free (rbind[1].buffer);
-          return GNUNET_SYSERR;
-        }
-      if (0 != mysql_stmt_fetch (select_value))
-        {
-          GNUNET_GE_BREAK (NULL, 0);
-          break;
-        }
-      if (v_length != v_size)
-        {
-          GNUNET_GE_BREAK (NULL, 0);
-          iclose ();
-          mysql_thread_end ();
-          GNUNET_mutex_unlock (lock);
-          GNUNET_free (rbind[1].buffer);
-          return cnt;
-        }
+         return GNUNET_SYSERR;    
+       }
       cnt++;
       if (GNUNET_OK != handler (key, type, v_size, rbind[1].buffer, closure))
         break;
     }
-  mysql_stmt_reset (select_value);
-  mysql_thread_end ();
-  GNUNET_mutex_unlock (lock);
   GNUNET_free (rbind[1].buffer);
   return cnt;
 }
@@ -720,11 +445,6 @@
 {
   static GNUNET_Dstore_ServiceAPI api;
   int fd;
-#ifndef WINDOWS
-  struct passwd *pw;
-#endif
-  size_t nX;
-  char *home_dir;
 
   coreAPI = capi;
 #if DEBUG_SQLITE
@@ -733,42 +453,12 @@
                  "MySQL Dstore: initializing database\n");
 #endif
 
-  /* verify that .my.cnf can be found */
-#ifndef WINDOWS
-  pw = getpwuid (getuid ());
-  if (!pw)
-    GNUNET_GE_DIE_STRERROR (coreAPI->ectx,
-                            GNUNET_GE_FATAL | GNUNET_GE_ADMIN |
-                            GNUNET_GE_IMMEDIATE, "getpwuid");
-  home_dir = GNUNET_strdup (pw->pw_dir);
-#else
-  home_dir = (char *) GNUNET_malloc (_MAX_PATH + 1);
-  plibc_conv_to_win_path ("~/", home_dir);
-#endif
-  nX = strlen (home_dir) + 10;
-  cnffile = GNUNET_malloc (nX);
-  GNUNET_snprintf (cnffile, nX, "%s/.my.cnf", home_dir);
-  GNUNET_free (home_dir);
-  GNUNET_GC_get_configuration_value_filename (capi->cfg,
-                                              "MYSQL", "CONFIG", cnffile,
-                                              &home_dir);
-  GNUNET_free (cnffile);
-  cnffile = home_dir;
-  GNUNET_GE_ASSERT (NULL, cnffile != NULL);
-  GNUNET_GE_LOG (coreAPI->ectx,
-                 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
-                 _("Trying to use file `%s' for MySQL configuration.\n"),
-                 cnffile);
-
-
   if (iopen () != GNUNET_OK)
     {
       GNUNET_GE_LOG (coreAPI->ectx,
                      GNUNET_GE_ERROR | GNUNET_GE_IMMEDIATE | GNUNET_GE_USER,
                      _
-                     ("Failed to initialize MySQL database connection for 
dstore.\n"),
-                     cnffile);
-      GNUNET_free (cnffile);
+                     ("Failed to initialize MySQL database connection for 
dstore.\n"));
       return NULL;
     }
   lock = GNUNET_mutex_create (GNUNET_NO);
@@ -780,7 +470,6 @@
   if (quota == 0)               /* error */
     quota = 1;
   quota *= 1024 * 1024;
-
   bloom_name = GNUNET_strdup ("/tmp/dbloomXXXXXX");
   fd = mkstemp (bloom_name);
   if (fd != -1)
@@ -824,10 +513,10 @@
                  GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
                  "MySQL Dstore: database shutdown\n");
 #endif
+  GNUNET_MYSQL_database_close(db);
+  db = NULL;   
   GNUNET_mutex_destroy (lock);
   coreAPI = NULL;
-  GNUNET_free (cnffile);
-  cnffile = NULL;
 }
 
 /* end of dstore_mysql.c */

Modified: GNUnet/src/applications/sqstore_mysql/Makefile.am
===================================================================
--- GNUnet/src/applications/sqstore_mysql/Makefile.am   2008-11-02 22:45:18 UTC 
(rev 7821)
+++ GNUnet/src/applications/sqstore_mysql/Makefile.am   2008-11-02 22:56:27 UTC 
(rev 7822)
@@ -28,7 +28,8 @@
   $(MYSQL_LDFLAGS)
 libgnunetmodule_sqstore_mysql_la_LIBADD = \
  $(top_builddir)/src/util/libgnunetutil.la \
- -lmysqlclient $(ZLIB_LNK)
+ $(top_builddir)/src/libs/mysql/libgnunetmysql.la \
+ $(ZLIB_LNK)
 
 EXTRA_DIST = check.conf
 

Modified: GNUnet/src/applications/sqstore_mysql/mysql.c
===================================================================
--- GNUnet/src/applications/sqstore_mysql/mysql.c       2008-11-02 22:45:18 UTC 
(rev 7821)
+++ GNUnet/src/applications/sqstore_mysql/mysql.c       2008-11-02 22:56:27 UTC 
(rev 7822)
@@ -125,32 +125,16 @@
 
 #include "platform.h"
 #include "gnunet_util.h"
+#include "gnunet_mysql.h"
 #include "gnunet_protocols.h"
 #include "gnunet_sqstore_service.h"
 #include "gnunet_stats_service.h"
 #include "gnunet_state_service.h"
-#include <mysql/mysql.h>
 
 #define DEBUG_MYSQL GNUNET_NO
 
-#define DEBUG_TIME_MYSQL GNUNET_NO
-
 #define MAX_DATUM_SIZE 65536
 
-/**
- * Die with an error message that indicates
- * a failure of the command 'cmd' with the message given
- * by strerror(errno).
- */
-#define DIE_MYSQL(cmd, dbh) do { GNUNET_GE_LOG(ectx, GNUNET_GE_FATAL | 
GNUNET_GE_ADMIN | GNUNET_GE_IMMEDIATE, _("`%s' failed at %s:%d with error: 
%s\n"), cmd, __FILE__, __LINE__, mysql_error((dbh)->dbf)); abort(); } while(0);
-
-/**
- * Log an error message at log-level 'level' that indicates
- * a failure of the command 'cmd' on file 'filename'
- * with the message given by strerror(errno).
- */
-#define LOG_MYSQL(level, cmd, dbh) do { GNUNET_GE_LOG(ectx, level, _("`%s' 
failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, 
mysql_error((dbh)->dbf)); } while(0);
-
 static GNUNET_Stats_ServiceAPI *stats;
 
 static GNUNET_CoreAPIForPlugins *coreAPI;
@@ -169,81 +153,52 @@
 
 static struct GNUNET_GE_Context *ectx;
 
-/**
- * @brief mysql wrapper
- */
-typedef struct
-{
-  MYSQL *dbf;
+static struct GNUNET_MysqlDatabaseHandle * db;
 
-  char *cnffile;
-
-  int valid;
-
-  /* stuff dealing with gn072 table */
+/* stuff dealing with gn072 table */
 #define SELECT_VALUE "SELECT value FROM gn072 WHERE vkey=?"
-  MYSQL_STMT *select_value;
+static struct GNUNET_MysqlStatementHandle *select_value;
 
 #define DELETE_VALUE "DELETE FROM gn072 WHERE vkey=?"
-  MYSQL_STMT *delete_value;
+static struct GNUNET_MysqlStatementHandle *delete_value;
 
 #define INSERT_VALUE "INSERT INTO gn072 (value) VALUES (?)"
-  MYSQL_STMT *insert_value;
+static struct GNUNET_MysqlStatementHandle *insert_value;
 
-  /* stuff dealing with gn080 table */
+/* stuff dealing with gn080 table */
 #define INSERT_ENTRY "INSERT INTO gn080 
(size,type,prio,anonLevel,expire,hash,vhash,vkey) VALUES (?,?,?,?,?,?,?,?)"
-  MYSQL_STMT *insert_entry;
+static struct GNUNET_MysqlStatementHandle *insert_entry;
 
 #define DELETE_ENTRY_BY_VKEY "DELETE FROM gn080 WHERE vkey=?"
-  MYSQL_STMT *delete_entry_by_vkey;
+static struct GNUNET_MysqlStatementHandle *delete_entry_by_vkey;
 
 #define SELECT_ENTRY_BY_HASH "SELECT size,type,prio,anonLevel,expire,hash,vkey 
FROM gn080 FORCE INDEX (hash_vkey) WHERE hash=? AND vkey > ? ORDER BY vkey ASC 
LIMIT 1 OFFSET ?"
-  MYSQL_STMT *select_entry_by_hash;
+static struct GNUNET_MysqlStatementHandle *select_entry_by_hash;
 
 #define SELECT_ENTRY_BY_HASH_AND_VHASH "SELECT 
size,type,prio,anonLevel,expire,hash,vkey FROM gn080 FORCE INDEX 
(hash_vhash_vkey) WHERE hash=? AND vhash=? AND vkey > ? ORDER BY vkey ASC LIMIT 
1 OFFSET ?"
-  MYSQL_STMT *select_entry_by_hash_and_vhash;
+static struct GNUNET_MysqlStatementHandle *select_entry_by_hash_and_vhash;
 
 #define SELECT_ENTRY_BY_HASH_AND_TYPE "SELECT 
size,type,prio,anonLevel,expire,hash,vkey FROM gn080 FORCE INDEX (hash_vkey) 
WHERE hash=? AND vkey > ? AND type=? ORDER BY vkey ASC LIMIT 1 OFFSET ?"
-  MYSQL_STMT *select_entry_by_hash_and_type;
+static struct GNUNET_MysqlStatementHandle *select_entry_by_hash_and_type;
 
 #define SELECT_ENTRY_BY_HASH_VHASH_AND_TYPE "SELECT 
size,type,prio,anonLevel,expire,hash,vkey FROM gn080 FORCE INDEX 
(hash_vhash_vkey) WHERE hash=? AND vhash=? AND vkey > ? AND type=? ORDER BY 
vkey ASC LIMIT 1 OFFSET ?"
-  MYSQL_STMT *select_entry_by_hash_vhash_and_type;
+static struct GNUNET_MysqlStatementHandle *select_entry_by_hash_vhash_and_type;
 
 #define COUNT_ENTRY_BY_HASH "SELECT count(*) FROM gn080 FORCE INDEX (hash) 
WHERE hash=?"
-  MYSQL_STMT *count_entry_by_hash;
+static struct GNUNET_MysqlStatementHandle *count_entry_by_hash;
 
 #define COUNT_ENTRY_BY_HASH_AND_VHASH "SELECT count(*) FROM gn080 FORCE INDEX 
(hash_vhash_vkey) WHERE hash=? AND vhash=?"
-  MYSQL_STMT *count_entry_by_hash_and_vhash;
+static struct GNUNET_MysqlStatementHandle *count_entry_by_hash_and_vhash;
 
 #define COUNT_ENTRY_BY_HASH_AND_TYPE "SELECT count(*) FROM gn080 FORCE INDEX 
(hash) WHERE hash=? AND type=?"
-  MYSQL_STMT *count_entry_by_hash_and_type;
+static struct GNUNET_MysqlStatementHandle *count_entry_by_hash_and_type;
 
 #define COUNT_ENTRY_BY_HASH_VHASH_AND_TYPE "SELECT count(*) FROM gn080 FORCE 
INDEX (hash_vhash) WHERE hash=? AND vhash=? AND type=?"
-  MYSQL_STMT *count_entry_by_hash_vhash_and_type;
+static struct GNUNET_MysqlStatementHandle *count_entry_by_hash_vhash_and_type;
 
 #define UPDATE_ENTRY "UPDATE gn080 SET 
prio=prio+?,expire=IF(expire>=?,expire,?) WHERE vkey=?"
-  MYSQL_STMT *update_entry;
+static struct GNUNET_MysqlStatementHandle *update_entry;
 
-
-#if 0
-  /* old, easier to read statments -- do not use,
-     C code no longer works with these! */
-#define SELECT_IT_LOW_PRIORITY "SELECT 
size,type,prio,anonLevel,expire,hash,vkey FROM gn080 WHERE ( (prio = ? AND vkey 
> ?) OR (prio > ? AND vkey != ?) )"\
-                               "ORDER BY prio ASC,vkey ASC LIMIT 1"
-
-#define SELECT_IT_NON_ANONYMOUS "SELECT 
size,type,prio,anonLevel,expire,hash,vkey FROM gn080 WHERE ( (prio = ? AND vkey 
< ?) OR (prio < ? AND vkey != ?) ) "\
-                                "AND anonLevel=0 AND type != 0xFFFFFFFF "\
-                                "ORDER BY prio DESC,vkey DESC LIMIT 1"
-
-#define SELECT_IT_EXPIRATION_TIME "SELECT 
size,type,prio,anonLevel,expire,hash,vkey FROM gn080 WHERE ( (expire = ? AND 
vkey > ?) OR (expire > ? AND vkey != ?) ) "\
-                                  "ORDER BY expire ASC,vkey ASC LIMIT 1"
-
-#define SELECT_IT_MIGRATION_ORDER "SELECT 
size,type,prio,anonLevel,expire,hash,vkey FROM gn080 WHERE ( (expire = ? AND 
vkey < ?) OR (expire < ? AND vkey != ?) ) "\
-                                  "AND expire > ? AND type!=3 "\
-                                  "ORDER BY expire DESC,vkey DESC LIMIT 1"
-
-#endif
-
 /* warning, slighly crazy mysql statements ahead.  Essentially, MySQL does not 
handle
    "OR" very well, so we need to use UNION instead.  And UNION does not
    automatically apply a LIMIT on the outermost clause, so we need to
@@ -279,251 +234,96 @@
                                   " AND expire > ? AND type!=3"\
                                   " ORDER BY expire DESC,vkey DESC LIMIT 1)"\
                                   "ORDER BY expire DESC,vkey DESC LIMIT 1"
-  MYSQL_STMT *iter[4];
+static struct GNUNET_MysqlStatementHandle *iter[4];
 
-} mysqlHandle;
-
-
 #define SELECT_SIZE "SELECT sum(size) FROM gn080"
 
-static mysqlHandle *dbh;
-
 /**
- * Close the database connection.
- */
-static int
-iclose ()
-{
-#define PEND(h) if (h != NULL) { mysql_stmt_close(h); h = NULL; } else {}
-  if (dbh->dbf == NULL)
-    return GNUNET_SYSERR;
-  PEND (dbh->select_value);
-  PEND (dbh->delete_value);
-  PEND (dbh->insert_value);
-  PEND (dbh->insert_entry);
-  PEND (dbh->delete_entry_by_vkey);
-  PEND (dbh->select_entry_by_hash);
-  PEND (dbh->select_entry_by_hash_and_vhash);
-  PEND (dbh->select_entry_by_hash_and_type);
-  PEND (dbh->select_entry_by_hash_vhash_and_type);
-  PEND (dbh->count_entry_by_hash);
-  PEND (dbh->count_entry_by_hash_and_vhash);
-  PEND (dbh->count_entry_by_hash_and_type);
-  PEND (dbh->count_entry_by_hash_vhash_and_type);
-  PEND (dbh->update_entry);
-  PEND (dbh->iter[0]);
-  PEND (dbh->iter[1]);
-  PEND (dbh->iter[2]);
-  PEND (dbh->iter[3]);
-  mysql_close (dbh->dbf);
-  dbh->dbf = NULL;
-  dbh->valid = GNUNET_NO;
-  return GNUNET_OK;
-}
-
-/**
- * Initiate the database connection.  Uses dbh->cnffile for the
- * configuration, so that must be set already.
+ * Initiate the database connection.
  *
  * @return GNUNET_OK on success
  */
 static int
 iopen ()
 {
-  char *dbname;
-  my_bool reconnect = 0;
-  unsigned int timeout = 60;    /* in seconds */
-
-  if (dbh->cnffile == NULL)
+  if (db != NULL)
+    return GNUNET_OK;
+  db = GNUNET_MYSQL_database_open(ectx,
+                                 coreAPI->cfg);
+  if (db == NULL)
     return GNUNET_SYSERR;
-  dbh->dbf = mysql_init (NULL);
-  if (dbh->dbf == NULL)
-    return GNUNET_SYSERR;
-  mysql_options (dbh->dbf, MYSQL_READ_DEFAULT_FILE, dbh->cnffile);
-  mysql_options (dbh->dbf, MYSQL_READ_DEFAULT_GROUP, "client");
-  mysql_options (dbh->dbf, MYSQL_OPT_RECONNECT, &reconnect);
-  mysql_options (dbh->dbf,
-                 MYSQL_OPT_CONNECT_TIMEOUT, (const void *) &timeout);
-  mysql_options (dbh->dbf, MYSQL_OPT_READ_TIMEOUT, (const void *) &timeout);
-  mysql_options (dbh->dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *) &timeout);
-
-  dbname = NULL;
-  GNUNET_GC_get_configuration_value_string (coreAPI->cfg,
-                                            "MYSQL", "DATABASE", "gnunet",
-                                            &dbname);
-  GNUNET_GE_ASSERT (ectx, dbname != NULL);
-  mysql_real_connect (dbh->dbf, NULL, NULL, NULL, dbname, 0, NULL, 0);
-  GNUNET_free (dbname);
-  if (mysql_error (dbh->dbf)[0])
+#define MRUNS(a) (GNUNET_OK != GNUNET_MYSQL_run_statement (db, a) )
+#define PINIT(a,b) (NULL == (a = GNUNET_MYSQL_prepared_statement_create(db, 
b)))
+  if ( MRUNS("CREATE TABLE IF NOT EXISTS gn080 ("
+            " size INT(11) UNSIGNED NOT NULL DEFAULT 0,"
+            " type INT(11) UNSIGNED NOT NULL DEFAULT 0,"
+            " prio INT(11) UNSIGNED NOT NULL DEFAULT 0,"
+            " anonLevel INT(11) UNSIGNED NOT NULL DEFAULT 0,"
+            " expire BIGINT UNSIGNED NOT NULL DEFAULT 0,"
+            " hash BINARY(64) NOT NULL DEFAULT '',"
+            " vhash BINARY(64) NOT NULL DEFAULT '',"
+            " vkey BIGINT UNSIGNED NOT NULL DEFAULT 0,"
+            " INDEX hash (hash(64)),"
+            " INDEX hash_vhash_vkey (hash(64),vhash(64),vkey),"
+            " INDEX hash_vkey (hash(64),vkey),"
+            " INDEX vkey (vkey),"
+            " INDEX prio (prio,vkey),"
+            " INDEX expire (expire,vkey,type),"
+            " INDEX anonLevel (anonLevel,prio,vkey,type)"
+            ") ENGINE=InnoDB") ||
+       MRUNS("CREATE TABLE IF NOT EXISTS gn072 ("
+            " vkey BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
+            " value BLOB NOT NULL DEFAULT '') ENGINE=MyISAM") ||       
+       MRUNS("SET AUTOCOMMIT = 1") ||
+       PINIT (select_value, SELECT_VALUE) ||
+       PINIT (delete_value, DELETE_VALUE) ||
+       PINIT (insert_value, INSERT_VALUE) ||
+       PINIT (insert_entry, INSERT_ENTRY) ||
+       PINIT (delete_entry_by_vkey, DELETE_ENTRY_BY_VKEY) ||
+       PINIT (select_entry_by_hash, SELECT_ENTRY_BY_HASH) ||
+       PINIT (select_entry_by_hash_and_vhash, SELECT_ENTRY_BY_HASH_AND_VHASH) 
||
+       PINIT (select_entry_by_hash_and_type, SELECT_ENTRY_BY_HASH_AND_TYPE) ||
+       PINIT (select_entry_by_hash_vhash_and_type,
+             SELECT_ENTRY_BY_HASH_VHASH_AND_TYPE) ||
+       PINIT (count_entry_by_hash, COUNT_ENTRY_BY_HASH) ||
+       PINIT (count_entry_by_hash_and_vhash, COUNT_ENTRY_BY_HASH_AND_VHASH) ||
+       PINIT (count_entry_by_hash_and_type, COUNT_ENTRY_BY_HASH_AND_TYPE) ||
+       PINIT (count_entry_by_hash_vhash_and_type,
+             COUNT_ENTRY_BY_HASH_VHASH_AND_TYPE) ||
+       PINIT (update_entry, UPDATE_ENTRY) ||
+       PINIT (iter[0], SELECT_IT_LOW_PRIORITY) ||
+       PINIT (iter[1], SELECT_IT_NON_ANONYMOUS) ||
+       PINIT (iter[2], SELECT_IT_EXPIRATION_TIME) ||
+       PINIT (iter[3], SELECT_IT_MIGRATION_ORDER) )     
     {
-      LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                 "mysql_real_connect", dbh);
-      iclose ();
+      GNUNET_MYSQL_database_close(db);
+      db = NULL;
       return GNUNET_SYSERR;
     }
-  mysql_query (dbh->dbf,
-               "SET SESSION net_read_timeout=60, SESSION 
net_write_timeout=60");
-  if (mysql_error (dbh->dbf)[0])
-    {
-      LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                 "mysql_query", dbh);
-      iclose ();
-      return GNUNET_SYSERR;
-    }
-  /* MySQL 5.0.46 fixes a bug in MyISAM (presumably);
-     earlier versions have issues with INDEX over BINARY data,
-     which is why we need to use InnoDB for those
-     (even though MyISAM would be faster) */
-  if (50046 <= mysql_get_server_version (dbh->dbf))
-    {
-      /* MySQL 5.0.46 fixes bug in MyISAM */
-      mysql_query (dbh->dbf,
-                   "CREATE TABLE IF NOT EXISTS gn080 ("
-                   " size INT(11) UNSIGNED NOT NULL DEFAULT 0,"
-                   " type INT(11) UNSIGNED NOT NULL DEFAULT 0,"
-                   " prio INT(11) UNSIGNED NOT NULL DEFAULT 0,"
-                   " anonLevel INT(11) UNSIGNED NOT NULL DEFAULT 0,"
-                   " expire BIGINT UNSIGNED NOT NULL DEFAULT 0,"
-                   " hash BINARY(64) NOT NULL,"
-                   " vhash BINARY(64) NOT NULL PRIMARY KEY,"
-                   " vkey BIGINT UNSIGNED NOT NULL DEFAULT 0,"
-                   " INDEX hash (hash(64)),"
-                   " INDEX hash_vhash_vkey (hash(64),vhash(64),vkey),"
-                   " INDEX hash_vkey (hash(64),vkey),"
-                   " INDEX vkey (vkey),"
-                   " INDEX prio (prio,vkey),"
-                   " INDEX expire (expire,vkey,type),"
-                   " INDEX anonLevel (anonLevel,prio,vkey,type)"
-                   ") ENGINE=MyISAM");
-    }
-  else
-    {
-      mysql_query (dbh->dbf,
-                   "CREATE TABLE IF NOT EXISTS gn080 ("
-                   " size INT(11) UNSIGNED NOT NULL DEFAULT 0,"
-                   " type INT(11) UNSIGNED NOT NULL DEFAULT 0,"
-                   " prio INT(11) UNSIGNED NOT NULL DEFAULT 0,"
-                   " anonLevel INT(11) UNSIGNED NOT NULL DEFAULT 0,"
-                   " expire BIGINT UNSIGNED NOT NULL DEFAULT 0,"
-                   " hash BINARY(64) NOT NULL DEFAULT '',"
-                   " vhash BINARY(64) NOT NULL DEFAULT '',"
-                   " vkey BIGINT UNSIGNED NOT NULL DEFAULT 0,"
-                   " INDEX hash (hash(64)),"
-                   " INDEX hash_vhash_vkey (hash(64),vhash(64),vkey),"
-                   " INDEX hash_vkey (hash(64),vkey),"
-                   " INDEX vkey (vkey),"
-                   " INDEX prio (prio,vkey),"
-                   " INDEX expire (expire,vkey,type),"
-                   " INDEX anonLevel (anonLevel,prio,vkey,type)"
-                   ") ENGINE=InnoDB");
-    }
-  if (mysql_error (dbh->dbf)[0])
-    {
-      LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                 "mysql_query", dbh);
-      iclose ();
-      return GNUNET_SYSERR;
-    }
-  mysql_query (dbh->dbf,
-               "CREATE TABLE IF NOT EXISTS gn072 ("
-               " vkey BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
-               " value BLOB NOT NULL DEFAULT '') ENGINE=MyISAM");
-  if (mysql_error (dbh->dbf)[0])
-    {
-      LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                 "mysql_query", dbh);
-      iclose ();
-      return GNUNET_SYSERR;
-    }
-  mysql_query (dbh->dbf, "SET AUTOCOMMIT = 1");
-  if (mysql_error (dbh->dbf)[0])
-    {
-      LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                 "mysql_query", dbh);
-      iclose ();
-      return GNUNET_SYSERR;
-    }
-#define PINIT(a,b) a = mysql_stmt_init(dbh->dbf); if (a == NULL) { iclose(); 
return GNUNET_SYSERR; } else { \
-    if (mysql_stmt_prepare (a, b, strlen(b))) { \
-      GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, \
-             _("`%s' failed at %s:%d with error: %s"), "mysql_stmt_prepare", 
__FILE__, __LINE__, \
-             mysql_stmt_error (a));  iclose(); return GNUNET_SYSERR; } }
-  PINIT (dbh->select_value, SELECT_VALUE);
-  PINIT (dbh->delete_value, DELETE_VALUE);
-  PINIT (dbh->insert_value, INSERT_VALUE);
-  PINIT (dbh->insert_entry, INSERT_ENTRY);
-  PINIT (dbh->delete_entry_by_vkey, DELETE_ENTRY_BY_VKEY);
-  PINIT (dbh->select_entry_by_hash, SELECT_ENTRY_BY_HASH);
-  PINIT (dbh->select_entry_by_hash_and_vhash, SELECT_ENTRY_BY_HASH_AND_VHASH);
-  PINIT (dbh->select_entry_by_hash_and_type, SELECT_ENTRY_BY_HASH_AND_TYPE);
-  PINIT (dbh->select_entry_by_hash_vhash_and_type,
-         SELECT_ENTRY_BY_HASH_VHASH_AND_TYPE);
-  PINIT (dbh->count_entry_by_hash, COUNT_ENTRY_BY_HASH);
-  PINIT (dbh->count_entry_by_hash_and_vhash, COUNT_ENTRY_BY_HASH_AND_VHASH);
-  PINIT (dbh->count_entry_by_hash_and_type, COUNT_ENTRY_BY_HASH_AND_TYPE);
-  PINIT (dbh->count_entry_by_hash_vhash_and_type,
-         COUNT_ENTRY_BY_HASH_VHASH_AND_TYPE);
-  PINIT (dbh->update_entry, UPDATE_ENTRY);
-  PINIT (dbh->iter[0], SELECT_IT_LOW_PRIORITY);
-  PINIT (dbh->iter[1], SELECT_IT_NON_ANONYMOUS);
-  PINIT (dbh->iter[2], SELECT_IT_EXPIRATION_TIME);
-  PINIT (dbh->iter[3], SELECT_IT_MIGRATION_ORDER);
-  dbh->valid = GNUNET_YES;
+#undef PINIT
+#undef MRUNS
   return GNUNET_OK;
 }
 
 /**
- * Check if DBH handle is valid, return GNUNET_OK if it is.
- * Also tries to re-connect to the DB if the connection
- * is down.
- */
-#define CHECK_DBH ((dbh->valid == GNUNET_NO) ? iopen(dbh, GNUNET_YES) : 
GNUNET_OK)
-
-
-/**
  * Delete an value from the gn072 table.
  *
  * @param vkey vkey identifying the value to delete
  * @return GNUNET_OK on success, GNUNET_NO if no such value exists, 
GNUNET_SYSERR on error
  */
 static int
-delete_value (unsigned long long vkey)
+do_delete_value (unsigned long long vkey)
 {
-  MYSQL_BIND qbind[1];
   int ret;
-
-  memset (qbind, 0, sizeof (qbind));
-  qbind[0].is_unsigned = GNUNET_YES;
-  qbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
-  qbind[0].buffer = &vkey;
-  GNUNET_GE_ASSERT (ectx, mysql_stmt_param_count (dbh->delete_value) == 1);
-  if (mysql_stmt_bind_param (dbh->delete_value, qbind))
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_param",
-                     __FILE__, __LINE__,
-                     mysql_stmt_error (dbh->delete_value));
-      iclose ();
-      return GNUNET_SYSERR;
-    }
-  if (mysql_stmt_execute (dbh->delete_value))
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_execute",
-                     __FILE__, __LINE__,
-                     mysql_stmt_error (dbh->delete_value));
-      iclose ();
-      return GNUNET_SYSERR;
-    }
-  if (mysql_stmt_affected_rows (dbh->delete_value) == 0)
-    ret = GNUNET_NO;
-  else
+  
+  ret = GNUNET_MYSQL_prepared_statement_run(delete_value,
+                                           NULL,
+                                           MYSQL_TYPE_LONGLONG,
+                                           &vkey,
+                                           GNUNET_YES,                         
            
+                                           -1);
+  if (ret > 0)
     ret = GNUNET_OK;
-  mysql_stmt_reset (dbh->delete_value);
   return ret;
 }
 
@@ -536,42 +336,17 @@
  * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 static int
-insert_value (const void *value, unsigned int size, unsigned long long *vkey)
+do_insert_value (const void *value, unsigned int size, unsigned long long 
*vkey)
 {
-  MYSQL_BIND qbind[1];
   unsigned long length = size;
 
-  memset (qbind, 0, sizeof (qbind));
-  qbind[0].buffer_type = MYSQL_TYPE_BLOB;
-  qbind[0].buffer = (void *) value;
-  qbind[0].buffer_length = size;
-  qbind[0].length = &length;
-  GNUNET_GE_ASSERT (ectx, mysql_stmt_param_count (dbh->insert_value) == 1);
-  if (mysql_stmt_bind_param (dbh->insert_value, qbind))
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_param",
-                     __FILE__, __LINE__,
-                     mysql_stmt_error (dbh->insert_value));
-      iclose ();
-      return GNUNET_SYSERR;
-    }
-  if (mysql_stmt_execute (dbh->insert_value))
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_execute",
-                     __FILE__, __LINE__,
-                     mysql_stmt_error (dbh->insert_value));
-      iclose ();
-      return GNUNET_SYSERR;
-    }
-  *vkey = (unsigned long long) mysql_stmt_insert_id (dbh->insert_value);
-  mysql_stmt_reset (dbh->insert_value);
-  return GNUNET_OK;
+  return GNUNET_MYSQL_prepared_statement_run(insert_value,
+                                            vkey,
+                                            MYSQL_TYPE_BLOB,
+                                            value,
+                                            length,
+                                            &length,
+                                            -1);
 }
 
 /**
@@ -581,47 +356,29 @@
  * @return GNUNET_OK on success, GNUNET_NO if no such value exists, 
GNUNET_SYSERR on error
  */
 static int
-delete_entry_by_vkey (unsigned long long vkey)
+do_delete_entry_by_vkey (unsigned long long vkey)
 {
-  MYSQL_BIND qbind[1];
   int ret;
-
-  memset (qbind, 0, sizeof (qbind));
-  qbind[0].is_unsigned = GNUNET_YES;
-  qbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
-  qbind[0].buffer = &vkey;
-  GNUNET_GE_ASSERT (ectx,
-                    mysql_stmt_param_count (dbh->delete_entry_by_vkey) == 1);
-  if (mysql_stmt_bind_param (dbh->delete_entry_by_vkey, qbind))
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_param",
-                     __FILE__, __LINE__,
-                     mysql_stmt_error (dbh->delete_entry_by_vkey));
-      iclose ();
-      return GNUNET_SYSERR;
-    }
-  if (mysql_stmt_execute (dbh->delete_entry_by_vkey))
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_execute",
-                     __FILE__, __LINE__,
-                     mysql_stmt_error (dbh->delete_entry_by_vkey));
-      iclose ();
-      return GNUNET_SYSERR;
-    }
-  if (mysql_stmt_affected_rows (dbh->delete_entry_by_vkey) == 0)
-    ret = GNUNET_NO;
-  else
+  
+  ret = GNUNET_MYSQL_prepared_statement_run(delete_entry_by_vkey,
+                                           NULL,
+                                           MYSQL_TYPE_LONGLONG,
+                                           &vkey,
+                                           GNUNET_YES,
+                                           -1);
+  if (ret > 0)
     ret = GNUNET_OK;
-  mysql_stmt_reset (dbh->delete_entry_by_vkey);
   return ret;
 }
 
+static int
+return_ok(void * cls,
+         unsigned int num_values,
+         MYSQL_BIND * values)
+{
+  return GNUNET_OK;
+}
+
 /**
  * Given a full (SELECT *) result set from gn080 table,
  * assemble it into a GNUNET_DatastoreValue representation.
@@ -643,8 +400,8 @@
   unsigned long long exp;
   unsigned long long vkey;
   unsigned long length;
-  MYSQL_BIND qbind[1];
   MYSQL_BIND rbind[1];
+  int ret;
 
   if ((result[0].buffer_type != MYSQL_TYPE_LONG) ||
       (!result[0].is_unsigned) ||
@@ -684,87 +441,42 @@
 
   /* now do query on gn072 */
   length = contentSize;
-  memset (qbind, 0, sizeof (qbind));
-  qbind[0].is_unsigned = 1;
-  qbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
-  qbind[0].buffer = &vkey;
   memset (rbind, 0, sizeof (rbind));
   rbind[0].buffer_type = MYSQL_TYPE_BLOB;
   rbind[0].buffer_length = contentSize;
   rbind[0].length = &length;
   rbind[0].buffer = &datum[1];
-  GNUNET_mutex_lock (lock);
-  if (GNUNET_OK != CHECK_DBH)
+  ret = GNUNET_MYSQL_prepared_statement_run_select(select_value,
+                                                  1,
+                                                  rbind,
+                                                  &return_ok,
+                                                  NULL,
+                                                  MYSQL_TYPE_LONGLONG,
+                                                  &vkey,
+                                                  GNUNET_YES,
+                                                  -1);
+  GNUNET_GE_BREAK(NULL, ret <= 1); /* should only have one result! */
+  if (ret > 0)
+    ret = GNUNET_OK;
+  if ( (ret != GNUNET_OK) ||
+       (rbind[0].buffer_length != contentSize) || 
+       (length != contentSize) )
     {
-      GNUNET_mutex_unlock (lock);
-      GNUNET_free (datum);
-      return NULL;
-    }
-  GNUNET_GE_ASSERT (ectx, mysql_stmt_param_count (dbh->select_value) == 1);
-  if (mysql_stmt_bind_param (dbh->select_value, qbind))
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_param",
-                     __FILE__, __LINE__,
-                     mysql_stmt_error (dbh->select_value));
-      iclose ();
-      GNUNET_mutex_unlock (lock);
-      GNUNET_free (datum);
-      return NULL;
-    }
-  if (mysql_stmt_execute (dbh->select_value))
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_execute",
-                     __FILE__, __LINE__,
-                     mysql_stmt_error (dbh->select_value));
-      iclose ();
-      GNUNET_mutex_unlock (lock);
-      GNUNET_free (datum);
-      return NULL;
-    }
-  GNUNET_GE_ASSERT (ectx, mysql_stmt_field_count (dbh->select_value) == 1);
-  if (mysql_stmt_bind_result (dbh->select_value, rbind))
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_result",
-                     __FILE__, __LINE__,
-                     mysql_stmt_error (dbh->select_value));
-      iclose ();
-      GNUNET_mutex_unlock (lock);
-      GNUNET_free (datum);
-      return NULL;
-    }
-  if ((0 != mysql_stmt_fetch (dbh->select_value)) ||
-      (rbind[0].buffer_length != contentSize) || (length != contentSize))
-    {
-      mysql_stmt_reset (dbh->select_value);
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_result",
-                     __FILE__, __LINE__,
-                     mysql_stmt_error (dbh->select_value));
-      delete_entry_by_vkey (vkey);
+      GNUNET_GE_BREAK(NULL, 0);
+      do_delete_entry_by_vkey (vkey);
       content_size -= ntohl (datum->size);
       GNUNET_mutex_unlock (lock);
       GNUNET_free (datum);
       return NULL;
     }
-  mysql_stmt_reset (dbh->select_value);
-  GNUNET_mutex_unlock (lock);
   return datum;
 }
 
 /**
  * Store an item in the datastore.
  *
+ * @param key key for the item
+ * @param value information to store
  * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 static int
@@ -780,10 +492,6 @@
   unsigned long long expiration;
   unsigned long long vkey;
   GNUNET_HashCode vhash;
-  MYSQL_BIND qbind[8];
-#if DEBUG_MYSQL
-  GNUNET_EncName enc;
-#endif
 
   if (((ntohl (value->size) < sizeof (GNUNET_DatastoreValue))) ||
       ((ntohl (value->size) - sizeof (GNUNET_DatastoreValue)) >
@@ -801,94 +509,49 @@
   expiration = GNUNET_ntohll (value->expiration_time);
   contentSize = ntohl (value->size) - sizeof (GNUNET_DatastoreValue);
   GNUNET_hash (&value[1], contentSize, &vhash);
-  GNUNET_mutex_lock (lock);
-  mysql_thread_init ();
-  if (GNUNET_OK != CHECK_DBH)
-    {
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  if (GNUNET_OK != insert_value (&value[1], contentSize, &vkey))
-    {
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-#if DEBUG_MYSQL
-  IF_GELOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
-            GNUNET_hash_to_enc (key, &enc));
-  GNUNET_GE_LOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
-                 "Storing in database block with type %u and key %s.\n", type,
-                 &enc);
-#endif
-  GNUNET_GE_ASSERT (ectx, mysql_stmt_param_count (dbh->insert_entry) == 8);
-  memset (qbind, 0, sizeof (qbind));
-  qbind[0].buffer_type = MYSQL_TYPE_LONG;       /* size */
-  qbind[0].buffer = &size;
-  qbind[0].is_unsigned = 1;
-  qbind[1].buffer_type = MYSQL_TYPE_LONG;       /* type */
-  qbind[1].is_unsigned = 1;
-  qbind[1].buffer = &type;
-  qbind[2].buffer_type = MYSQL_TYPE_LONG;       /* priority */
-  qbind[2].is_unsigned = 1;
-  qbind[2].buffer = &prio;
-  qbind[3].buffer_type = MYSQL_TYPE_LONG;       /* anon level */
-  qbind[3].is_unsigned = 1;
-  qbind[3].buffer = &level;
-  qbind[4].buffer_type = MYSQL_TYPE_LONGLONG;   /* expiration */
-  qbind[4].is_unsigned = 1;
-  qbind[4].buffer = &expiration;
-  qbind[5].buffer_type = MYSQL_TYPE_BLOB;       /* GNUNET_hash */
-  qbind[5].buffer = (void *) key;
-  qbind[5].length = &hashSize;
-  qbind[5].buffer_length = hashSize;
-  qbind[6].buffer_type = MYSQL_TYPE_BLOB;       /* vhash */
-  qbind[6].buffer = (void *) &vhash;
-  qbind[6].length = &hashSize2;
-  qbind[6].buffer_length = hashSize2;
-  qbind[7].buffer_type = MYSQL_TYPE_LONGLONG;   /* vkey */
-  qbind[7].is_unsigned = 1;
-  qbind[7].buffer = &vkey;
 
-  if (mysql_stmt_bind_param (dbh->insert_entry, qbind))
+  if (GNUNET_OK != do_insert_value (&value[1], contentSize, &vkey))
+    return GNUNET_SYSERR;    
+  if (GNUNET_OK != 
+      GNUNET_MYSQL_prepared_statement_run(insert_entry,
+                                         NULL,
+                                         MYSQL_TYPE_LONG,
+                                         &size,
+                                         GNUNET_YES,
+                                         MYSQL_TYPE_LONG,
+                                         &type,
+                                         GNUNET_YES,
+                                         MYSQL_TYPE_LONG,
+                                         &prio,
+                                         GNUNET_YES,
+                                         MYSQL_TYPE_LONG,
+                                         &level,
+                                         GNUNET_YES,
+                                         MYSQL_TYPE_LONGLONG,
+                                         &expiration,
+                                         GNUNET_YES,
+                                         MYSQL_TYPE_BLOB,
+                                         key,
+                                         hashSize,
+                                         &hashSize,
+                                         MYSQL_TYPE_BLOB,
+                                         &vhash,
+                                         hashSize2,
+                                         &hashSize2,
+                                         MYSQL_TYPE_LONGLONG,
+                                         &vkey,
+                                         GNUNET_YES,
+                                         -1) )
     {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_param",
-                     __FILE__, __LINE__,
-                     mysql_stmt_error (dbh->insert_entry));
-      delete_value (vkey);
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
+      do_delete_value (vkey);
       return GNUNET_SYSERR;
     }
-
-  if (mysql_stmt_execute (dbh->insert_entry))
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_execute",
-                     __FILE__, __LINE__,
-                     mysql_stmt_error (dbh->insert_entry));
-      delete_value (vkey);
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  mysql_thread_end ();
+  GNUNET_mutex_lock (lock);
   content_size += ntohl (value->size);
   GNUNET_mutex_unlock (lock);
   return GNUNET_OK;
 }
 
-
-
-
 /**
  * Iterate over the items in the datastore
  * using the given query to select and order
@@ -898,17 +561,13 @@
  *        Use 0 for any type.
  * @param iter never NULL
  * @param is_asc are we using ascending order?
- * @param is_prio is the extra ordering by priority (otherwise by expiration)
- * @param is_migr is this IT_MIGRATON_ORDER (with expire)
  * @return the number of results, GNUNET_SYSERR if the
  *   iter is non-NULL and aborted the iteration
  */
 static int
 iterateHelper (unsigned int type,
                int is_asc,
-               int is_prio,
-               int is_migr,
-               unsigned int iter_select, GNUNET_DatastoreValueIterator iter,
+               unsigned int iter_select, GNUNET_DatastoreValueIterator dviter,
                void *closure)
 {
   GNUNET_DatastoreValue *datum;
@@ -926,11 +585,8 @@
   unsigned long hashSize;
   GNUNET_HashCode key;
   GNUNET_CronTime now;
-  MYSQL_BIND qbind[6];
   MYSQL_BIND rbind[7];
-  MYSQL_STMT *stmt;
 
-  GNUNET_GE_ASSERT (NULL, ((is_migr == 1) || (is_migr == 0)));
   if (is_asc)
     {
       last_prio = 0;
@@ -943,41 +599,6 @@
       last_vkey = 0x7FFFFFFFFFFFFFFFLL; /* MySQL only supports 63 bits */
       last_expire = 0x7FFFFFFFFFFFFFFFLL;       /* MySQL only supports 63 bits 
*/
     }
-  memset (qbind, 0, sizeof (qbind));
-  if (is_prio)
-    {
-      qbind[0].buffer_type = MYSQL_TYPE_LONG;
-      qbind[0].buffer = &last_prio;
-      qbind[0].is_unsigned = 1;
-      qbind[2 + is_migr].buffer_type = MYSQL_TYPE_LONG;
-      qbind[2 + is_migr].buffer = &last_prio;
-      qbind[2 + is_migr].is_unsigned = 1;
-    }
-  else
-    {
-      qbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
-      qbind[0].buffer = &last_expire;
-      qbind[0].is_unsigned = 1;
-      qbind[2 + is_migr].buffer_type = MYSQL_TYPE_LONGLONG;
-      qbind[2 + is_migr].buffer = &last_expire;
-      qbind[2 + is_migr].is_unsigned = 1;
-    }
-  qbind[1].buffer_type = MYSQL_TYPE_LONGLONG;
-  qbind[1].buffer = &last_vkey;
-  qbind[1].is_unsigned = 1;
-  qbind[3 + is_migr].buffer_type = MYSQL_TYPE_LONGLONG;
-  qbind[3 + is_migr].buffer = &last_vkey;
-  qbind[3 + is_migr].is_unsigned = 1;
-  if (is_migr)
-    {
-      qbind[2].buffer_type = MYSQL_TYPE_LONGLONG;
-      qbind[2].buffer = &now;
-      qbind[2].is_unsigned = 1;
-      qbind[5].buffer_type = MYSQL_TYPE_LONGLONG;
-      qbind[5].buffer = &now;
-      qbind[5].is_unsigned = 1;
-    }
-
   hashSize = sizeof (GNUNET_HashCode);
   memset (rbind, 0, sizeof (rbind));
   rbind[0].buffer_type = MYSQL_TYPE_LONG;
@@ -1003,76 +624,95 @@
   rbind[6].buffer = &vkey;
   rbind[6].is_unsigned = GNUNET_YES;
 
-  mysql_thread_init ();
+  now = GNUNET_get_time();
   count = 0;
   while (1)
     {
-      GNUNET_mutex_lock (lock);
-      if (GNUNET_OK != CHECK_DBH)
-        {
-          GNUNET_mutex_unlock (lock);
-          mysql_thread_end ();
-          return GNUNET_SYSERR;
-        }
-      stmt = dbh->iter[iter_select];
-      GNUNET_GE_ASSERT (ectx, mysql_stmt_param_count (stmt) <= 6);
-      GNUNET_GE_ASSERT (ectx, mysql_stmt_field_count (stmt) == 7);
-      now = GNUNET_get_time ();
-      if (mysql_stmt_bind_param (stmt, qbind))
-        {
-          GNUNET_GE_LOG (ectx,
-                         GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                         _("`%s' failed at %s:%d with error: %s\n"),
-                         "mysql_stmt_bind_param",
-                         __FILE__, __LINE__, mysql_stmt_error (stmt));
-          iclose ();
-          GNUNET_mutex_unlock (lock);
-          mysql_thread_end ();
-          return GNUNET_SYSERR;
-        }
-      if (mysql_stmt_execute (stmt))
-        {
-          GNUNET_GE_LOG (ectx,
-                         GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                         _("`%s' failed at %s:%d with error: %s\n"),
-                         "mysql_stmt_execute",
-                         __FILE__, __LINE__, mysql_stmt_error (stmt));
-          iclose ();
-          GNUNET_mutex_unlock (lock);
-          mysql_thread_end ();
-          return GNUNET_SYSERR;
-        }
-      if (mysql_stmt_bind_result (stmt, rbind))
-        {
-          GNUNET_GE_LOG (ectx,
-                         GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                         _("`%s' failed at %s:%d with error: %s\n"),
-                         "mysql_stmt_bind_result",
-                         __FILE__, __LINE__, mysql_stmt_error (stmt));
-          iclose ();
-          mysql_thread_end ();
-          GNUNET_mutex_unlock (lock);
-          return GNUNET_SYSERR;
-        }
-      datum = NULL;
-      if (0 != mysql_stmt_fetch (stmt))
-        {
-          mysql_stmt_reset (stmt);
-          GNUNET_mutex_unlock (lock);
-          break;
-        }
-      mysql_stmt_reset (stmt);
-      GNUNET_mutex_unlock (lock);
+      switch (iter_select)
+       {
+       case 0:
+       case 1:
+         ret = GNUNET_MYSQL_prepared_statement_run_select(iter[iter_select],
+                                                          7,
+                                                          rbind,
+                                                          &return_ok,
+                                                          NULL,                
                                           
+                                                          MYSQL_TYPE_LONG,
+                                                          &last_prio,
+                                                          GNUNET_YES,
+                                                          MYSQL_TYPE_LONGLONG,
+                                                          &last_vkey,
+                                                          GNUNET_YES,
+                                                          MYSQL_TYPE_LONG,
+                                                          &last_prio,
+                                                          GNUNET_YES,
+                                                          MYSQL_TYPE_LONGLONG,
+                                                          &last_vkey,
+                                                          GNUNET_YES,
+                                                          -1);
+         break;
+       case 2:
+         ret = GNUNET_MYSQL_prepared_statement_run_select(iter[iter_select],
+                                                          7,
+                                                          rbind,
+                                                          &return_ok,
+                                                          NULL,                
                                           
+                                                          MYSQL_TYPE_LONGLONG,
+                                                          &last_expire,
+                                                          GNUNET_YES,
+                                                          MYSQL_TYPE_LONGLONG,
+                                                          &last_vkey,
+                                                          GNUNET_YES,
+                                                          MYSQL_TYPE_LONGLONG,
+                                                          &last_expire,
+                                                          GNUNET_YES,
+                                                          MYSQL_TYPE_LONGLONG,
+                                                          &last_vkey,
+                                                          GNUNET_YES,
+                                                          -1);
+         break;
+       case 3:
+         ret = GNUNET_MYSQL_prepared_statement_run_select(iter[iter_select],
+                                                          7,
+                                                          rbind,
+                                                          &return_ok,
+                                                          NULL,                
                                           
+                                                          MYSQL_TYPE_LONGLONG,
+                                                          &last_expire,
+                                                          GNUNET_YES,
+                                                          MYSQL_TYPE_LONGLONG,
+                                                          &last_vkey,
+                                                          GNUNET_YES,
+                                                          MYSQL_TYPE_LONGLONG,
+                                                          &now,
+                                                          GNUNET_YES,
+                                                          MYSQL_TYPE_LONGLONG,
+                                                          &last_expire,
+                                                          GNUNET_YES,
+                                                          MYSQL_TYPE_LONGLONG,
+                                                          &last_vkey,
+                                                          GNUNET_YES,
+                                                          MYSQL_TYPE_LONGLONG,
+                                                          &now,
+                                                          GNUNET_YES,
+                                                          -1);
+         break;
+       default:
+         GNUNET_GE_BREAK(NULL, 0);
+         return GNUNET_SYSERR;
+       }
+      if (ret != GNUNET_OK)
+       break;
       last_vkey = vkey;
       last_prio = prio;
       last_expire = expiration;
       count++;
-      if (iter != NULL)
+      if (dviter != NULL)
         {
           datum = assembleDatum (rbind);
           if (datum == NULL)
             continue;
-          ret = iter (&key, datum, closure, vkey);
+          ret = dviter (&key, datum, closure, vkey);
           if (ret == GNUNET_SYSERR)
             {
               GNUNET_free (datum);
@@ -1080,16 +720,15 @@
             }
           if (ret == GNUNET_NO)
             {
+              do_delete_value (vkey);
+              do_delete_entry_by_vkey (vkey);
               GNUNET_mutex_lock (lock);
-              delete_value (vkey);
-              delete_entry_by_vkey (vkey);
               content_size -= ntohl (datum->size);
               GNUNET_mutex_unlock (lock);
             }
           GNUNET_free (datum);
         }
     }
-  mysql_thread_end ();
   return count;
 }
 
@@ -1107,8 +746,7 @@
 iterateLowPriority (unsigned int type, GNUNET_DatastoreValueIterator iter,
                     void *closure)
 {
-  return iterateHelper (type, GNUNET_YES, GNUNET_YES, GNUNET_NO, 0, iter,
-                        closure);
+  return iterateHelper (type, GNUNET_YES, 0, iter, closure);
 }
 
 /**
@@ -1125,8 +763,7 @@
 iterateNonAnonymous (unsigned int type, GNUNET_DatastoreValueIterator iter,
                      void *closure)
 {
-  return iterateHelper (type, GNUNET_NO, GNUNET_YES, GNUNET_NO, 1, iter,
-                        closure);
+  return iterateHelper (type, GNUNET_NO, 1, iter, closure);
 }
 
 /**
@@ -1143,8 +780,7 @@
 iterateExpirationTime (unsigned int type, GNUNET_DatastoreValueIterator iter,
                        void *closure)
 {
-  return iterateHelper (type, GNUNET_YES, GNUNET_NO, GNUNET_NO, 2, iter,
-                        closure);
+  return iterateHelper (type, GNUNET_YES, 2, iter, closure);
 }
 
 /**
@@ -1158,8 +794,7 @@
 static int
 iterateMigrationOrder (GNUNET_DatastoreValueIterator iter, void *closure)
 {
-  return iterateHelper (0, GNUNET_NO, GNUNET_NO, GNUNET_YES, 3, iter,
-                        closure);
+  return iterateHelper (0, GNUNET_NO, 3, iter, closure);
 }
 
 /**
@@ -1173,8 +808,7 @@
 static int
 iterateAllNow (GNUNET_DatastoreValueIterator iter, void *closure)
 {
-  return iterateHelper (0, GNUNET_YES, GNUNET_YES, GNUNET_NO, 0, iter,
-                        closure);
+  return iterateHelper (0, GNUNET_YES, 0, iter, closure);
 }
 
 /**
@@ -1204,7 +838,6 @@
   unsigned long long total;
   int off;
   int ret;
-  MYSQL_STMT *stmt;
   unsigned int size;
   unsigned int rtype;
   unsigned int prio;
@@ -1217,165 +850,101 @@
   GNUNET_HashCode key;
   unsigned long hashSize;
   unsigned long hashSize2;
-  MYSQL_BIND qbind[5];
   MYSQL_BIND rbind[7];
-#if DEBUG_MYSQL
-  GNUNET_EncName enc;
-#endif
-  int sqoff;
 
   if (query == NULL)
     return iterateLowPriority (type, iter, closure);
-
-#if DEBUG_MYSQL
-  IF_GELOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
-            GNUNET_hash_to_enc (query, &enc));
-  GNUNET_GE_LOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
-                 "MySQL looks for `%s' of type %u\n", &enc, type);
-#endif
-
   hashSize = sizeof (GNUNET_HashCode);
   hashSize2 = sizeof (GNUNET_HashCode);
-  memset (qbind, 0, sizeof (qbind));
-  qbind[0].buffer_type = MYSQL_TYPE_BLOB;
-  qbind[0].buffer = (void *) query;
-  qbind[0].length = &hashSize;
-  qbind[0].buffer_length = hashSize;
-  sqoff = 1;
-  if (vhash != NULL)
-    {
-      qbind[sqoff].buffer_type = MYSQL_TYPE_BLOB;
-      qbind[sqoff].buffer = (void *) vhash;
-      qbind[sqoff].length = &hashSize2;
-      qbind[sqoff].buffer_length = hashSize2;
-      sqoff++;
-    }
-  if (type != 0)
-    {
-      qbind[sqoff].buffer_type = MYSQL_TYPE_LONG;
-      qbind[sqoff].is_unsigned = GNUNET_YES;
-      qbind[sqoff].buffer = &type;
-      sqoff++;
-    }
   memset (rbind, 0, sizeof (rbind));
+  total = -1;
   rbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
   rbind[0].buffer = &total;
   rbind[0].is_unsigned = GNUNET_YES;
-  /* first, determine total number of results */
-  mysql_thread_init ();
-  GNUNET_mutex_lock (lock);
-  if (GNUNET_OK != CHECK_DBH)
+  if (type != 0)
     {
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
+      if (vhash != NULL)
+       {
+         ret = 
GNUNET_MYSQL_prepared_statement_run_select(count_entry_by_hash_vhash_and_type,
+                                                          1,
+                                                          rbind,
+                                                          &return_ok,
+                                                          NULL,
+                                                          MYSQL_TYPE_BLOB,
+                                                          query,
+                                                          hashSize2,
+                                                          &hashSize2,
+                                                          MYSQL_TYPE_BLOB,
+                                                          vhash,
+                                                          hashSize2,
+                                                          &hashSize2,
+                                                          MYSQL_TYPE_LONG,
+                                                          &type,
+                                                          GNUNET_YES,
+                                                          -1);
+       }
+      else
+       {
+         ret = 
GNUNET_MYSQL_prepared_statement_run_select(count_entry_by_hash_and_type,
+                                                          1,
+                                                          rbind,
+                                                          &return_ok,
+                                                          NULL,
+                                                          MYSQL_TYPE_BLOB,
+                                                          query,
+                                                          hashSize2,
+                                                          &hashSize2,
+                                                          MYSQL_TYPE_LONG,
+                                                          &type,
+                                                          GNUNET_YES,
+                                                          -1);
+         
+       }
     }
-  if (type != 0)
-    stmt =
-      (vhash !=
-       NULL) ? dbh->count_entry_by_hash_vhash_and_type : dbh->
-      count_entry_by_hash_and_type;
   else
-    stmt =
-      (vhash !=
-       NULL) ? dbh->count_entry_by_hash_and_vhash : dbh->count_entry_by_hash;
-  GNUNET_GE_ASSERT (ectx, mysql_stmt_param_count (stmt) <= 3);
-  GNUNET_GE_ASSERT (ectx, mysql_stmt_field_count (stmt) == 1);
-  if (mysql_stmt_bind_param (stmt, qbind))
     {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_param",
-                     __FILE__, __LINE__, mysql_stmt_error (stmt));
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
+      if (vhash != NULL)
+       {
+         ret = 
GNUNET_MYSQL_prepared_statement_run_select(count_entry_by_hash_and_vhash,
+                                                          1,
+                                                          rbind,
+                                                          &return_ok,
+                                                          NULL,
+                                                          MYSQL_TYPE_BLOB,
+                                                          query,
+                                                          hashSize2,
+                                                          &hashSize2,
+                                                          MYSQL_TYPE_BLOB,
+                                                          vhash,
+                                                          hashSize2,
+                                                          &hashSize2,
+                                                          -1);
+         
+       }
+      else
+       {
+         ret = GNUNET_MYSQL_prepared_statement_run_select(count_entry_by_hash,
+                                                          1,
+                                                          rbind,
+                                                          &return_ok,
+                                                          NULL,
+                                                          MYSQL_TYPE_BLOB,
+                                                          query,
+                                                          hashSize2,
+                                                          &hashSize2,
+                                                          -1);          
+       }
     }
-  if (mysql_stmt_execute (stmt))
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_execute",
-                     __FILE__, __LINE__, mysql_stmt_error (stmt));
-      iclose ();
-      GNUNET_mutex_unlock (lock);
-      mysql_thread_end ();
-      return GNUNET_SYSERR;
-    }
-  if (mysql_stmt_bind_result (stmt, rbind))
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_result",
-                     __FILE__, __LINE__, mysql_stmt_error (stmt));
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  if (0 != mysql_stmt_fetch (stmt))
-    {
-      mysql_stmt_reset (stmt);
-      GNUNET_mutex_unlock (lock);
-      mysql_thread_end ();
-      return GNUNET_SYSERR;
-    }
-  if (-1 == total)
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_num_rows",
-                     __FILE__, __LINE__, mysql_stmt_error (stmt));
-      iclose ();
-      GNUNET_mutex_unlock (lock);
-      mysql_thread_end ();
-      return GNUNET_SYSERR;
-    }
-  mysql_stmt_reset (stmt);
+  if ( (ret != GNUNET_OK) ||
+       (-1 == total) )
+    return GNUNET_SYSERR;
   if ((iter == NULL) || (total == 0))
-    {
-      GNUNET_mutex_unlock (lock);
-      mysql_thread_end ();
-      return (int) total;
-    }
-  GNUNET_mutex_unlock (lock);
+    return (int) total;
+    
   last_vkey = 0;
   count = 0;
   off = GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, total);
-  memset (qbind, 0, sizeof (qbind));
-  qbind[0].buffer_type = MYSQL_TYPE_BLOB;
-  qbind[0].buffer = (void *) query;
-  qbind[0].length = &hashSize;
-  qbind[0].buffer_length = hashSize;
-  sqoff = 1;
-  if (vhash != NULL)
-    {
-      qbind[sqoff].buffer_type = MYSQL_TYPE_BLOB;
-      qbind[sqoff].buffer = (void *) vhash;
-      qbind[sqoff].length = &hashSize2;
-      qbind[sqoff].buffer_length = hashSize2;
-      sqoff++;
-    }
-  qbind[sqoff].buffer_type = MYSQL_TYPE_LONGLONG;
-  qbind[sqoff].is_unsigned = GNUNET_YES;
-  qbind[sqoff].buffer = &last_vkey;
-  sqoff++;
-  if (type != 0)
-    {
-      qbind[sqoff].buffer_type = MYSQL_TYPE_LONG;
-      qbind[sqoff].is_unsigned = GNUNET_YES;
-      qbind[sqoff].buffer = &type;
-      sqoff++;
-    }
-  qbind[sqoff].buffer_type = MYSQL_TYPE_LONG;
-  qbind[sqoff].is_unsigned = GNUNET_YES;
-  qbind[sqoff].buffer = &limit_off;
-  sqoff++;
+  
   memset (rbind, 0, sizeof (rbind));
   rbind[0].buffer_type = MYSQL_TYPE_LONG;
   rbind[0].buffer = &size;
@@ -1401,74 +970,109 @@
   rbind[6].is_unsigned = GNUNET_YES;
   while (1)
     {
-      GNUNET_mutex_lock (lock);
-      if (GNUNET_OK != CHECK_DBH)
-        {
-          GNUNET_mutex_unlock (lock);
-          mysql_thread_end ();
-          return GNUNET_SYSERR;
-        }
-      if (type != 0)
-        stmt =
-          (vhash !=
-           NULL) ? dbh->select_entry_by_hash_vhash_and_type :
-          dbh->select_entry_by_hash_and_type;
-      else
-        stmt =
-          (vhash !=
-           NULL) ? dbh->select_entry_by_hash_and_vhash : dbh->
-          select_entry_by_hash;
       if (count == 0)
         limit_off = off;
       else
         limit_off = 0;
-      GNUNET_GE_ASSERT (ectx, mysql_stmt_param_count (stmt) <= 5);
-      GNUNET_GE_ASSERT (ectx, mysql_stmt_field_count (stmt) == 7);
-      if (mysql_stmt_bind_param (stmt, qbind))
-        {
-          GNUNET_GE_LOG (ectx,
-                         GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                         _("`%s' failed at %s:%d with error: %s\n"),
-                         "mysql_stmt_bind_param",
-                         __FILE__, __LINE__, mysql_stmt_error (stmt));
-          iclose ();
-          mysql_thread_end ();
-          GNUNET_mutex_unlock (lock);
-          return GNUNET_SYSERR;
-        }
-      if (mysql_stmt_execute (stmt))
-        {
-          GNUNET_GE_LOG (ectx,
-                         GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                         _("`%s' failed at %s:%d with error: %s\n"),
-                         "mysql_stmt_execute",
-                         __FILE__, __LINE__, mysql_stmt_error (stmt));
-          iclose ();
-          GNUNET_mutex_unlock (lock);
-          mysql_thread_end ();
-          return GNUNET_SYSERR;
-        }
-      if (mysql_stmt_bind_result (stmt, rbind))
-        {
-          GNUNET_GE_LOG (ectx,
-                         GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                         _("`%s' failed at %s:%d with error: %s\n"),
-                         "mysql_stmt_bind_result",
-                         __FILE__, __LINE__, mysql_stmt_error (stmt));
-          iclose ();
-          mysql_thread_end ();
-          GNUNET_mutex_unlock (lock);
-          return GNUNET_SYSERR;
-        }
-      if (0 != mysql_stmt_fetch (stmt))
-        {
-          mysql_stmt_reset (stmt);
-          GNUNET_mutex_unlock (lock);
-          break;
-        }
+      if (type != 0)
+       {
+         if (vhash != NULL) 
+           {
+             ret = 
GNUNET_MYSQL_prepared_statement_run_select(select_entry_by_hash_vhash_and_type,
+                                                              7,
+                                                              rbind,
+                                                              &return_ok,
+                                                              NULL,
+                                                              MYSQL_TYPE_BLOB,
+                                                              query,
+                                                              hashSize,
+                                                              &hashSize,
+                                                              MYSQL_TYPE_BLOB,
+                                                              vhash,
+                                                              hashSize2,
+                                                              &hashSize2,
+                                                              
MYSQL_TYPE_LONGLONG,
+                                                              &last_vkey,
+                                                              GNUNET_YES,
+                                                              MYSQL_TYPE_LONG,
+                                                              &type,
+                                                              GNUNET_YES,
+                                                              MYSQL_TYPE_LONG,
+                                                              &limit_off,
+                                                              GNUNET_YES,
+                                                              -1);
+           }
+         else
+           {
+             ret = 
GNUNET_MYSQL_prepared_statement_run_select(select_entry_by_hash_and_type,
+                                                              7,
+                                                              rbind,
+                                                              &return_ok,
+                                                              NULL,
+                                                              MYSQL_TYPE_BLOB,
+                                                              query,
+                                                              hashSize,
+                                                              &hashSize,
+                                                              
MYSQL_TYPE_LONGLONG,
+                                                              &last_vkey,
+                                                              GNUNET_YES,
+                                                              MYSQL_TYPE_LONG,
+                                                              &type,
+                                                              GNUNET_YES,
+                                                              MYSQL_TYPE_LONG,
+                                                              &limit_off,
+                                                              GNUNET_YES,
+                                                              -1);
+           }
+       }      
+      else
+       {
+         if (vhash != NULL) 
+           {
+             ret = 
GNUNET_MYSQL_prepared_statement_run_select(select_entry_by_hash_and_vhash,
+                                                              7,
+                                                              rbind,
+                                                              &return_ok,
+                                                              NULL,
+                                                              MYSQL_TYPE_BLOB,
+                                                              query,
+                                                              hashSize,
+                                                              &hashSize,
+                                                              MYSQL_TYPE_BLOB,
+                                                              vhash,
+                                                              hashSize2,
+                                                              &hashSize2,
+                                                              
MYSQL_TYPE_LONGLONG,
+                                                              &last_vkey,
+                                                              GNUNET_YES,
+                                                              MYSQL_TYPE_LONG,
+                                                              &limit_off,
+                                                              GNUNET_YES,
+                                                              -1);
+           }
+         else
+           {
+             ret = 
GNUNET_MYSQL_prepared_statement_run_select(select_entry_by_hash,
+                                                              7,
+                                                              rbind,
+                                                              &return_ok,
+                                                              NULL,
+                                                              MYSQL_TYPE_BLOB,
+                                                              query,
+                                                              hashSize,
+                                                              &hashSize,
+                                                              
MYSQL_TYPE_LONGLONG,
+                                                              &last_vkey,
+                                                              GNUNET_YES,
+                                                              MYSQL_TYPE_LONG,
+                                                              &limit_off,
+                                                              GNUNET_YES,
+                                                              -1);
+           }
+       }
+      if (ret == GNUNET_SYSERR)
+       break;
       last_vkey = vkey;
-      mysql_stmt_reset (stmt);
-      GNUNET_mutex_unlock (lock);
       datum = assembleDatum (rbind);
       if (datum == NULL)
         continue;
@@ -1481,9 +1085,9 @@
         }
       if (ret == GNUNET_NO)
         {
+          do_delete_value (vkey);
+          do_delete_entry_by_vkey (vkey);
           GNUNET_mutex_lock (lock);
-          delete_value (vkey);
-          delete_entry_by_vkey (vkey);
           content_size -= ntohl (datum->size);
           GNUNET_mutex_unlock (lock);
         }
@@ -1493,78 +1097,36 @@
       if (count == total)
         break;
     }
-  mysql_thread_end ();
   return count;
 }
 
 /**
  * Update the priority for a particular key
  * in the datastore.
+ *
+ * @param vkey identifies entry in the datastore
+ * @param delta change in priority
+ * @param expire new expiration value (will be MAX of this value and the old 
value)
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 static int
 update (unsigned long long vkey, int delta, GNUNET_CronTime expire)
 {
-  GNUNET_CronTime start;
-  MYSQL_BIND qbind[4];
-
-  GNUNET_mutex_lock (lock);
-  mysql_thread_init ();
-  if (GNUNET_OK != CHECK_DBH)
-    {
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  memset (qbind, 0, sizeof (qbind));
-  qbind[0].buffer_type = MYSQL_TYPE_LONG;
-  qbind[0].buffer = &delta;
-  qbind[1].buffer_type = MYSQL_TYPE_LONGLONG;
-  qbind[1].buffer = &expire;
-  qbind[1].is_unsigned = GNUNET_YES;
-  qbind[2].buffer_type = MYSQL_TYPE_LONGLONG;
-  qbind[2].is_unsigned = GNUNET_YES;
-  qbind[2].buffer = &expire;
-  qbind[3].buffer_type = MYSQL_TYPE_LONGLONG;
-  qbind[3].is_unsigned = GNUNET_YES;
-  qbind[3].buffer = &vkey;
-  GNUNET_GE_ASSERT (ectx, mysql_stmt_param_count (dbh->update_entry) == 4);
-  if (mysql_stmt_bind_param (dbh->update_entry, qbind))
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error: %s\n"),
-                     "mysql_stmt_bind_param",
-                     __FILE__, __LINE__,
-                     mysql_stmt_error (dbh->update_entry));
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  /* NOTE: as the table entry for 'priority' is defined as unsigned,
-   * mysql will zero the value if its about to go negative. (This
-   * will generate a warning though, but its probably not seen
-   * at all in this context.)
-   */
-  start = GNUNET_get_time ();
-  if (mysql_stmt_execute (dbh->update_entry))
-    {
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
-                     _("`%s' failed at %s:%d with error `%s' after %llums\n"),
-                     "mysql_stmt_execute",
-                     __FILE__, __LINE__,
-                     mysql_stmt_error (dbh->update_entry),
-                     GNUNET_get_time () - start);
-      iclose ();
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  mysql_stmt_reset (dbh->update_entry);
-  mysql_thread_end ();
-  GNUNET_mutex_unlock (lock);
-  return GNUNET_OK;
+  return GNUNET_MYSQL_prepared_statement_run(update_entry,
+                                            NULL,
+                                            MYSQL_TYPE_LONG,
+                                            &delta,
+                                            GNUNET_NO,
+                                            MYSQL_TYPE_LONGLONG,
+                                            &expire,
+                                            GNUNET_YES,
+                                            MYSQL_TYPE_LONGLONG,
+                                            &expire,
+                                            GNUNET_YES,
+                                            MYSQL_TYPE_LONGLONG
+                                            &vkey,
+                                            GNUNET_YES,
+                                            -1);
 }
 
 
@@ -1594,36 +1156,12 @@
 static void
 drop ()
 {
-  int ok;
-
-  ok = GNUNET_YES;
-  GNUNET_mutex_lock (lock);
-  mysql_thread_init ();
-  if (GNUNET_OK != CHECK_DBH)
-    {
-      mysql_thread_end ();
-      GNUNET_mutex_unlock (lock);
-      return;
-    }
-  mysql_query (dbh->dbf, "DROP TABLE gn080");
-  if (mysql_error (dbh->dbf)[0])
-    {
-      LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                 "mysql_query", dbh);
-      ok = GNUNET_NO;
-    }
-  mysql_query (dbh->dbf, "DROP TABLE gn072");
-  if (mysql_error (dbh->dbf)[0])
-    {
-      LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                 "mysql_query", dbh);
-      ok = GNUNET_NO;
-    }
-  if (ok == GNUNET_YES)
-    content_size = 0;
-  iclose ();
-  mysql_thread_end ();
-  GNUNET_mutex_unlock (lock);
+  if ( (GNUNET_OK != GNUNET_MYSQL_run_statement(db,
+                                               "DROP TABLE gn080")) ||
+       (GNUNET_OK != GNUNET_MYSQL_run_statement(db,
+                                               "DROP TABLE gn072")) )
+    return; /* error */
+  content_size = 0;
 }
 
 GNUNET_SQstore_ServiceAPI *
@@ -1631,16 +1169,8 @@
 {
   static GNUNET_SQstore_ServiceAPI api;
   GNUNET_State_ServiceAPI *state;
-  char *cnffile;
-  FILE *fp;
-  struct passwd *pw;
-  size_t nX;
-#ifndef WINDOWS
-  char *home_dir;
-#endif
   unsigned long long *sb;
-  MYSQL_RES *sql_res;
-  MYSQL_ROW sql_row;
+  char * res;
 
   ectx = capi->ectx;
   coreAPI = capi;
@@ -1648,99 +1178,31 @@
   if (stats)
     stat_size = stats->create (gettext_noop ("# bytes in datastore"));
 
-  /* verify that .my.cnf can be found */
-#ifndef WINDOWS
-  pw = getpwuid (getuid ());
-  if (!pw)
-    GNUNET_GE_DIE_STRERROR (ectx,
-                            GNUNET_GE_FATAL | GNUNET_GE_ADMIN |
-                            GNUNET_GE_IMMEDIATE, "getpwuid");
-  home_dir = GNUNET_strdup (pw->pw_dir);
-#else
-  home_dir = (char *) GNUNET_malloc (_MAX_PATH + 1);
-  plibc_conv_to_win_path ("~/", home_dir);
-#endif
-  nX = strlen (home_dir) + 10;
-  cnffile = GNUNET_malloc (nX);
-  GNUNET_snprintf (cnffile, nX, "%s/.my.cnf", home_dir);
-  GNUNET_free (home_dir);
-  GNUNET_GC_get_configuration_value_filename (capi->cfg,
-                                              "MYSQL", "CONFIG", cnffile,
-                                              &home_dir);
-  GNUNET_free (cnffile);
-  cnffile = home_dir;
-  GNUNET_GE_LOG (ectx,
-                 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
-                 _("Trying to use file `%s' for MySQL configuration.\n"),
-                 cnffile);
-  fp = FOPEN (cnffile, "r");
-  if (!fp)
-    {
-      GNUNET_GE_LOG_STRERROR_FILE (ectx,
-                                   GNUNET_GE_ERROR | GNUNET_GE_ADMIN |
-                                   GNUNET_GE_BULK, "fopen", cnffile);
-      if (stats != NULL)
-        coreAPI->service_release (stats);
-      GNUNET_free (cnffile);
-      return NULL;
-    }
-  else
-    {
-      fclose (fp);
-    }
-  dbh = GNUNET_malloc (sizeof (mysqlHandle));
-  memset (dbh, 0, sizeof (mysqlHandle));
-  dbh->cnffile = cnffile;
   if (GNUNET_OK != iopen ())
     {
-      GNUNET_free (cnffile);
-      GNUNET_free (dbh);
       GNUNET_GE_LOG (ectx,
                      GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
                      _
                      ("Failed to load MySQL database module.  Check that MySQL 
is running and configured properly!\n"));
-      dbh = NULL;
       if (stats != NULL)
         coreAPI->service_release (stats);
       return NULL;
     }
-
   lock = GNUNET_mutex_create (GNUNET_NO);
   state = coreAPI->service_request ("state");
   sb = NULL;
   if (sizeof (unsigned long long)
       != state->read (ectx, "mysql-size", (void *) &sb))
     {
-      /* need to recompute! */
-      sql_res = NULL;
-      mysql_query (dbh->dbf, SELECT_SIZE);
-      if ((mysql_error (dbh->dbf)[0]) ||
-          (!(sql_res = mysql_use_result (dbh->dbf))) ||
-          (!(sql_row = mysql_fetch_row (sql_res))))
-        {
-          LOG_MYSQL (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                     "mysql_query", dbh);
-          content_size = 0;
-          iclose (dbh);
-        }
-      else
-        {
-          if ((mysql_num_fields (sql_res) != 1) || (sql_row[0] == NULL))
-            {
-              GNUNET_GE_BREAK (ectx, mysql_num_fields (sql_res) == 1);
-              content_size = 0;
-            }
-          else
-            {
-              if (1 != SSCANF (sql_row[0], "%llu", &content_size))
-                {
-                  GNUNET_GE_BREAK (ectx, 0);
-                  content_size = 0;
-                }
-            }
-        }
-      if (sql_res != NULL)
-        mysql_free_result (sql_res);
+      res = GNUNET_MYSQL_run_statement_select(db,
+                                             SELECT_SIZE);
+      if ( (res == NULL) ||
+          (1 != SSCANF (res, "%llu", &content_size)) )
+       {
+         GNUNET_GE_BREAK (ectx, 0);
+         content_size = 0;
+       }
+      GNUNET_free_non_null(res);
     }
   else
     {
@@ -1772,10 +1234,8 @@
 {
   GNUNET_State_ServiceAPI *state;
 
-  iclose (dbh);
-  GNUNET_free (dbh->cnffile);
-  GNUNET_free (dbh);
-  dbh = NULL;
+  GNUNET_MYSQL_database_close(db);
+  db = NULL;
   if (stats != NULL)
     coreAPI->service_release (stats);
   GNUNET_mutex_destroy (lock);
@@ -1783,7 +1243,6 @@
   state->write (ectx,
                 "mysql-size", sizeof (unsigned long long), &content_size);
   coreAPI->service_release (state);
-  mysql_library_end ();
   ectx = NULL;
   coreAPI = NULL;
 }
@@ -1795,69 +1254,18 @@
 void
 update_module_sqstore_mysql (GNUNET_UpdateAPI * uapi)
 {
-  char *cnffile;
-  FILE *fp;
-  struct passwd *pw;
-  size_t nX;
-  char *home_dir;
-
   ectx = uapi->ectx;
-#ifndef WINDOWS
-  pw = getpwuid (getuid ());
-  if (!pw)
-    GNUNET_GE_DIE_STRERROR (ectx,
-                            GNUNET_GE_FATAL | GNUNET_GE_ADMIN |
-                            GNUNET_GE_IMMEDIATE, "getpwuid");
-  home_dir = GNUNET_strdup (pw->pw_dir);
-#else
-  home_dir = (char *) GNUNET_malloc (_MAX_PATH + 1);
-  plibc_conv_to_win_path ("~/", home_dir);
-#endif
-  nX = strlen (home_dir) + 10;
-  cnffile = GNUNET_malloc (nX);
-  GNUNET_snprintf (cnffile, nX, "%s/.my.cnf", home_dir);
-  GNUNET_free (home_dir);
-  GNUNET_GC_get_configuration_value_filename (uapi->cfg,
-                                              "MYSQL", "CONFIG", cnffile,
-                                              &home_dir);
-  GNUNET_free (cnffile);
-  cnffile = home_dir;
-  GNUNET_GE_LOG (ectx,
-                 GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
-                 _("Trying to use file `%s' for MySQL configuration.\n"),
-                 cnffile);
-  fp = FOPEN (cnffile, "r");
-  if (!fp)
-    {
-      GNUNET_GE_LOG_STRERROR_FILE (ectx,
-                                   GNUNET_GE_ERROR | GNUNET_GE_ADMIN |
-                                   GNUNET_GE_BULK, "fopen", cnffile);
-      GNUNET_free (cnffile);
-      return;
-    }
-  else
-    {
-      fclose (fp);
-    }
-  dbh = GNUNET_malloc (sizeof (mysqlHandle));
-  memset (dbh, 0, sizeof (mysqlHandle));
-  dbh->cnffile = cnffile;
   if (GNUNET_OK != iopen ())
     {
-      GNUNET_free (cnffile);
-      GNUNET_free (dbh);
       GNUNET_GE_LOG (ectx,
                      GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER,
                      _
                      ("Failed to load MySQL database module.  Check that MySQL 
is running and configured properly!\n"));
-      dbh = NULL;
       return;
     }
   /* run update queries here */
-  iclose (dbh);
-  GNUNET_free (dbh->cnffile);
-  GNUNET_free (dbh);
-  dbh = NULL;
+  GNUNET_MYSQL_database_close(db);
+  db = NULL;
   mysql_library_end ();
   ectx = NULL;
 }

Modified: GNUnet/src/include/gnunet_mysql.h
===================================================================
--- GNUnet/src/include/gnunet_mysql.h   2008-11-02 22:45:18 UTC (rev 7821)
+++ GNUnet/src/include/gnunet_mysql.h   2008-11-02 22:56:27 UTC (rev 7822)
@@ -62,7 +62,19 @@
 GNUNET_MYSQL_run_statement(struct GNUNET_MysqlDatabaseHandle * dbh,
                           const char * statement);
 
+
 /**
+ * Run the given MySQL SELECT statement.  The statement
+ * must have only a single result (one column, one row).
+ *
+ * @return result on success, NULL on error
+ */
+char *
+GNUNET_MYSQL_run_statement_select(struct GNUNET_MysqlDatabaseHandle * dbh,
+                                 const char * statement);
+
+
+/**
  * Create a prepared statement.
  *
  * @return NULL on error





reply via email to

[Prev in Thread] Current Thread [Next in Thread]