adding libgnunetpostgres for shared postgres functionality between postgres datastore...
authorChristian Grothoff <christian@grothoff.org>
Thu, 22 Mar 2012 18:47:07 +0000 (18:47 +0000)
committerChristian Grothoff <christian@grothoff.org>
Thu, 22 Mar 2012 18:47:07 +0000 (18:47 +0000)
configure.ac
src/Makefile.am
src/datacache/Makefile.am
src/datacache/plugin_datacache_postgres.c
src/datastore/plugin_datastore_postgres.c
src/include/Makefile.am
src/include/gnunet_mysql_lib.h
src/include/gnunet_postgres_lib.h [new file with mode: 0644]
src/postgres/Makefile.am [new file with mode: 0644]
src/postgres/postgres.c [new file with mode: 0644]

index c9f3d1177f8ab56b6dbeece5afd6b3908b7830d1..fe5de4c126467e548992327657f3c6b281228e74 100644 (file)
@@ -896,6 +896,7 @@ src/nse/nse.conf
 src/peerinfo/Makefile
 src/peerinfo/peerinfo.conf
 src/peerinfo-tool/Makefile
+src/postgres/Makefile
 src/pt/Makefile
 src/statistics/Makefile
 src/statistics/statistics.conf
index 06b2aedc47c2cb9b3c85acf90b38b879c0d51bb4..fc9518558607e52f64c9e9cc1799ffe4cd0bc397 100644 (file)
@@ -15,6 +15,10 @@ if HAVE_MYSQL
  MYSQL_DIR = mysql
 endif
 
+if HAVE_POSTGRES
+ POSTGRES_DIR = postgres
+endif
+
 
 SUBDIRS = \
   include $(INTLEMU_SUBDIRS) \
@@ -26,6 +30,7 @@ SUBDIRS = \
   arm \
   peerinfo \
   $(MYSQL_DIR) \
+  $(POSTGRES_DIR) \
   datacache \
   datastore \
   namestore \
index 88545dcdc2309680ae3435b0646cfec991049976..d34e6e8c7ebbc6cab7cd8b1b16f6d6bfabccd4d6 100644 (file)
@@ -70,6 +70,7 @@ libgnunet_plugin_datacache_mysql_la_LDFLAGS = \
 libgnunet_plugin_datacache_postgres_la_SOURCES = \
   plugin_datacache_postgres.c
 libgnunet_plugin_datacache_postgres_la_LIBADD = \
+  $(top_builddir)/src/postgres/libgnunetpostgres.la \
   $(top_builddir)/src/statistics/libgnunetstatistics.la \
   $(top_builddir)/src/util/libgnunetutil.la \
   $(GN_PLUGIN_LDFLAGS) $(POSTGRES_LDFLAGS) -lpq
index 0fabdb4c2cf99daa9d0f9004fb312e427173e6d0..cd4286b924fbed0a284dd76779575003085991ca 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet
-     (C) 2006, 2009, 2010 Christian Grothoff (and other contributing authors)
+     (C) 2006, 2009, 2010, 2012 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -25,6 +25,7 @@
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
+#include "gnunet_postgres_lib.h"
 #include "gnunet_datacache_plugin.h"
 #include <postgresql/libpq-fe.h>
 
@@ -53,83 +54,6 @@ struct Plugin
 };
 
 
-/**
- * Check if the result obtained from Postgres has
- * the desired status code.  If not, log an error, clear the
- * result and return GNUNET_SYSERR.
- *
- * @return GNUNET_OK if the result is acceptable
- */
-static int
-check_result (struct Plugin *plugin, PGresult * ret, int expected_status,
-              const char *command, const char *args, int line)
-{
-  if (ret == NULL)
-  {
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
-                     "datastore-postgres",
-                     "Postgres failed to allocate result for `%s:%s' at %d\n",
-                     command, args, line);
-    return GNUNET_SYSERR;
-  }
-  if (PQresultStatus (ret) != expected_status)
-  {
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
-                     "datastore-postgres",
-                     _("`%s:%s' failed at %s:%d with error: %s"), command, args,
-                     __FILE__, line, PQerrorMessage (plugin->dbh));
-    PQclear (ret);
-    return GNUNET_SYSERR;
-  }
-  return GNUNET_OK;
-}
-
-
-/**
- * Run simple SQL statement (without results).
- *
- * @param plugin global context
- * @param sql statement to run
- * @param line code line for error reporting */
-static int
-pq_exec (struct Plugin *plugin, const char *sql, int line)
-{
-  PGresult *ret;
-
-  ret = PQexec (plugin->dbh, sql);
-  if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "PQexec", sql, line))
-    return GNUNET_SYSERR;
-  PQclear (ret);
-  return GNUNET_OK;
-}
-
-
-/**
- * Prepare SQL statement.
- *
- * @param plugin global context
- * @param name name for the prepared SQL statement
- * @param sql SQL code to prepare
- * @param nparams number of parameters in sql
- * @param line code line for error reporting
- * @return GNUNET_OK on success
- */
-static int
-pq_prepare (struct Plugin *plugin, const char *name, const char *sql,
-            int nparms, int line)
-{
-  PGresult *ret;
-
-  ret = PQprepare (plugin->dbh, name, sql, nparms, NULL);
-  if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "PQprepare", sql, line))
-    return GNUNET_SYSERR;
-  PQclear (ret);
-  return GNUNET_OK;
-}
-
-
 /**
  * @brief Get a database handle
  *
@@ -139,31 +63,12 @@ pq_prepare (struct Plugin *plugin, const char *name, const char *sql,
 static int
 init_connection (struct Plugin *plugin)
 {
-  char *conninfo;
   PGresult *ret;
 
-  /* Open database and precompile statements */
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
-                                             "datacache-postgres", "CONFIG",
-                                             &conninfo))
-    conninfo = NULL;
-  plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo);
-  GNUNET_free_non_null (conninfo);
+  plugin->dbh = GNUNET_POSTGRES_connect (plugin->env->cfg,
+                                        "datacache-postgres");
   if (NULL == plugin->dbh)
-  {
-    /* FIXME: warn about out-of-memory? */
     return GNUNET_SYSERR;
-  }
-  if (PQstatus (plugin->dbh) != CONNECTION_OK)
-  {
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "datacache-postgres",
-                     _("Unable to initialize Postgres: %s"),
-                     PQerrorMessage (plugin->dbh));
-    PQfinish (plugin->dbh);
-    plugin->dbh = NULL;
-    return GNUNET_SYSERR;
-  }
   ret =
       PQexec (plugin->dbh,
               "CREATE TEMPORARY TABLE gn090dc ("
@@ -176,8 +81,8 @@ init_connection (struct Plugin *plugin)
                                                                                     (ret,
                                                                                      PG_DIAG_SQLSTATE)))))
   {
-    (void) check_result (plugin, ret, PGRES_COMMAND_OK, "CREATE TABLE",
-                         "gn090dc", __LINE__);
+    (void) GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "CREATE TABLE",
+                                        "gn090dc");
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
     return GNUNET_SYSERR;
@@ -185,10 +90,9 @@ init_connection (struct Plugin *plugin)
   if (PQresultStatus (ret) == PGRES_COMMAND_OK)
   {
     if ((GNUNET_OK !=
-         pq_exec (plugin, "CREATE INDEX idx_key ON gn090dc (key)", __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_key ON gn090dc (key)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin, "CREATE INDEX idx_dt ON gn090dc (discard_time)",
-                  __LINE__)))
+         GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_dt ON gn090dc (discard_time)")))
     {
       PQclear (ret);
       PQfinish (plugin->dbh);
@@ -201,8 +105,7 @@ init_connection (struct Plugin *plugin)
       PQexec (plugin->dbh,
               "ALTER TABLE gn090dc ALTER value SET STORAGE EXTERNAL");
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090dc",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090dc"))
   {
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
@@ -211,8 +114,7 @@ init_connection (struct Plugin *plugin)
   PQclear (ret);
   ret = PQexec (plugin->dbh, "ALTER TABLE gn090dc ALTER key SET STORAGE PLAIN");
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090dc",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090dc"))
   {
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
@@ -220,24 +122,23 @@ init_connection (struct Plugin *plugin)
   }
   PQclear (ret);
   if ((GNUNET_OK !=
-       pq_prepare (plugin, "getkt",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "getkt",
                    "SELECT discard_time,type,value FROM gn090dc "
-                   "WHERE key=$1 AND type=$2 ", 2, __LINE__)) ||
+                   "WHERE key=$1 AND type=$2 ", 2)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "getk",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "getk",
                    "SELECT discard_time,type,value FROM gn090dc "
-                   "WHERE key=$1", 1, __LINE__)) ||
+                   "WHERE key=$1", 1)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "getm",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "getm",
                    "SELECT length(value),oid,key FROM gn090dc "
-                   "ORDER BY discard_time ASC LIMIT 1", 0, __LINE__)) ||
+                   "ORDER BY discard_time ASC LIMIT 1", 0)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "delrow", "DELETE FROM gn090dc WHERE oid=$1", 1,
-                   __LINE__)) ||
+       GNUNET_POSTGRES_prepare (plugin->dbh, "delrow", "DELETE FROM gn090dc WHERE oid=$1", 1)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "put",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "put",
                    "INSERT INTO gn090dc (type, discard_time, key, value) "
-                   "VALUES ($1, $2, $3, $4)", 4, __LINE__)))
+                   "VALUES ($1, $2, $3, $4)", 4)))
   {
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
@@ -247,37 +148,6 @@ init_connection (struct Plugin *plugin)
 }
 
 
-/**
- * Delete the row identified by the given rowid (qid
- * in postgres).
- *
- * @param plugin global context
- * @param rowid which row to delete
- * @return GNUNET_OK on success
- */
-static int
-delete_by_rowid (struct Plugin *plugin, uint32_t rowid)
-{
-  uint32_t brow = htonl (rowid);
-  const char *paramValues[] = { (const char *) &brow };
-  int paramLengths[] = { sizeof (brow) };
-  const int paramFormats[] = { 1 };
-  PGresult *ret;
-
-  ret =
-      PQexecPrepared (plugin->dbh, "delrow", 1, paramValues, paramLengths,
-                      paramFormats, 1);
-  if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow",
-                    __LINE__))
-  {
-    return GNUNET_SYSERR;
-  }
-  PQclear (ret);
-  return GNUNET_OK;
-}
-
-
 /**
  * Store an item in the datastore.
  *
@@ -317,8 +187,7 @@ postgres_plugin_put (void *cls, const GNUNET_HashCode * key, size_t size,
       PQexecPrepared (plugin->dbh, "put", 4, paramValues, paramLengths,
                       paramFormats, 1);
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put"))
     return GNUNET_SYSERR;
   PQclear (ret);
   return size + OVERHEAD;
@@ -364,8 +233,8 @@ postgres_plugin_get (void *cls, const GNUNET_HashCode * key,
                       (type == 0) ? 1 : 2, paramValues, paramLengths,
                       paramFormats, 1);
   if (GNUNET_OK !=
-      check_result (plugin, res, PGRES_TUPLES_OK, "PQexecPrepared",
-                    (type == 0) ? "getk" : "getkt", __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK, "PQexecPrepared",
+                                   (type == 0) ? "getk" : "getkt"))
   {
 #if DEBUG_POSTGRES
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
@@ -442,8 +311,7 @@ postgres_plugin_del (void *cls)
 
   res = PQexecPrepared (plugin->dbh, "getm", 0, NULL, NULL, NULL, 1);
   if (GNUNET_OK !=
-      check_result (plugin, res, PGRES_TUPLES_OK, "PQexecPrepared", "getm",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK, "PQexecPrepared", "getm"))
   {
 #if DEBUG_POSTGRES
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
@@ -473,7 +341,7 @@ postgres_plugin_del (void *cls)
   oid = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1));
   memcpy (&key, PQgetvalue (res, 0, 2), sizeof (GNUNET_HashCode));
   PQclear (res);
-  if (GNUNET_OK != delete_by_rowid (plugin, oid))
+  if (GNUNET_OK != GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", oid))
     return GNUNET_SYSERR;
   plugin->env->delete_notify (plugin->env->cls, &key, size + OVERHEAD);
   return GNUNET_OK;
index 31ba09a8711676987f8a04cfba7fa818bdcae0d2..b9c27c7b1a5bc861bbf19eca68a6a078e553c65d 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet
-     (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+     (C) 2009, 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -26,6 +26,7 @@
 
 #include "platform.h"
 #include "gnunet_datastore_plugin.h"
+#include "gnunet_postgres_lib.h"
 #include <postgresql/libpq-fe.h>
 
 #define DEBUG_POSTGRES GNUNET_EXTRA_LOGGING
@@ -61,87 +62,6 @@ struct Plugin
 };
 
 
-/**
- * Check if the result obtained from Postgres has
- * the desired status code.  If not, log an error, clear the
- * result and return GNUNET_SYSERR.
- *
- * @param plugin global context
- * @param ret result to check
- * @param expected_status expected return value
- * @param command name of SQL command that was run
- * @param args arguments to SQL command
- * @param line line number for error reporting
- * @return GNUNET_OK if the result is acceptable
- */
-static int
-check_result (struct Plugin *plugin, PGresult * ret, int expected_status,
-              const char *command, const char *args, int line)
-{
-  if (ret == NULL)
-  {
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
-                     "datastore-postgres",
-                     "Postgres failed to allocate result for `%s:%s' at %d\n",
-                     command, args, line);
-    return GNUNET_SYSERR;
-  }
-  if (PQresultStatus (ret) != expected_status)
-  {
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
-                     "datastore-postgres",
-                     _("`%s:%s' failed at %s:%d with error: %s"), command, args,
-                     __FILE__, line, PQerrorMessage (plugin->dbh));
-    PQclear (ret);
-    return GNUNET_SYSERR;
-  }
-  return GNUNET_OK;
-}
-
-/**
- * Run simple SQL statement (without results).
- *
- * @param plugin global context
- * @param sql statement to run
- * @param line code line for error reporting
- */
-static int
-pq_exec (struct Plugin *plugin, const char *sql, int line)
-{
-  PGresult *ret;
-
-  ret = PQexec (plugin->dbh, sql);
-  if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "PQexec", sql, line))
-    return GNUNET_SYSERR;
-  PQclear (ret);
-  return GNUNET_OK;
-}
-
-/**
- * Prepare SQL statement.
- *
- * @param plugin global context
- * @param name name for the prepared SQL statement
- * @param sql SQL code to prepare
- * @param nparams number of parameters in sql
- * @param line code line for error reporting
- * @return GNUNET_OK on success
- */
-static int
-pq_prepare (struct Plugin *plugin, const char *name, const char *sql,
-            int nparams, int line)
-{
-  PGresult *ret;
-
-  ret = PQprepare (plugin->dbh, name, sql, nparams, NULL);
-  if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "PQprepare", sql, line))
-    return GNUNET_SYSERR;
-  PQclear (ret);
-  return GNUNET_OK;
-}
-
 /**
  * @brief Get a database handle
  *
@@ -151,33 +71,11 @@ pq_prepare (struct Plugin *plugin, const char *name, const char *sql,
 static int
 init_connection (struct Plugin *plugin)
 {
-  char *conninfo;
   PGresult *ret;
 
-  /* Open database and precompile statements */
-  conninfo = NULL;
-  (void) GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
-                                                "datastore-postgres", "CONFIG",
-                                                &conninfo);
-  plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo);
+  plugin->dbh = GNUNET_POSTGRES_connect (plugin->env->cfg, "datastore-postgres");
   if (NULL == plugin->dbh)
-  {
-    /* FIXME: warn about out-of-memory? */
-    GNUNET_free_non_null (conninfo);
     return GNUNET_SYSERR;
-  }
-  if (PQstatus (plugin->dbh) != CONNECTION_OK)
-  {
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "datastore-postgres",
-                     _
-                     ("Unable to initialize Postgres with configuration `%s': %s"),
-                     conninfo, PQerrorMessage (plugin->dbh));
-    PQfinish (plugin->dbh);
-    plugin->dbh = NULL;
-    GNUNET_free_non_null (conninfo);
-    return GNUNET_SYSERR;
-  }
-  GNUNET_free_non_null (conninfo);
   ret =
       PQexec (plugin->dbh,
               "CREATE TABLE gn090 (" "  repl INTEGER NOT NULL DEFAULT 0,"
@@ -194,8 +92,7 @@ init_connection (struct Plugin *plugin)
                                                                                     (ret,
                                                                                      PG_DIAG_SQLSTATE)))))
   {
-    (void) check_result (plugin, ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090",
-                         __LINE__);
+    (void) GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090");
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
     return GNUNET_SYSERR;
@@ -203,29 +100,23 @@ init_connection (struct Plugin *plugin)
   if (PQresultStatus (ret) == PGRES_COMMAND_OK)
   {
     if ((GNUNET_OK !=
-         pq_exec (plugin, "CREATE INDEX idx_hash ON gn090 (hash)", __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_hash ON gn090 (hash)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)",
-                  __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin, "CREATE INDEX idx_prio ON gn090 (prio)", __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_prio ON gn090 (prio)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin, "CREATE INDEX idx_expire ON gn090 (expire)",
-                  __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_expire ON gn090 (expire)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin,
-                  "CREATE INDEX idx_prio_anon ON gn090 (prio,anonLevel)",
-                  __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh,
+                  "CREATE INDEX idx_prio_anon ON gn090 (prio,anonLevel)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin,
-                  "CREATE INDEX idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)",
-                  __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh,
+                  "CREATE INDEX idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin, "CREATE INDEX idx_repl_rvalue ON gn090 (repl,rvalue)",
-                  __LINE__)) ||
+         GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_repl_rvalue ON gn090 (repl,rvalue)")) ||
         (GNUNET_OK !=
-         pq_exec (plugin, "CREATE INDEX idx_expire_hash ON gn090 (expire,hash)",
-                  __LINE__)))
+         GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_expire_hash ON gn090 (expire,hash)")))
     {
       PQclear (ret);
       PQfinish (plugin->dbh);
@@ -238,8 +129,7 @@ init_connection (struct Plugin *plugin)
       PQexec (plugin->dbh,
               "ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL");
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
   {
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
@@ -248,8 +138,7 @@ init_connection (struct Plugin *plugin)
   PQclear (ret);
   ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN");
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
   {
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
@@ -258,8 +147,7 @@ init_connection (struct Plugin *plugin)
   PQclear (ret);
   ret = PQexec (plugin->dbh, "ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN");
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090"))
   {
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
@@ -267,60 +155,56 @@ init_connection (struct Plugin *plugin)
   }
   PQclear (ret);
   if ((GNUNET_OK !=
-       pq_prepare (plugin, "getvt",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "getvt",
                    "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
                    "WHERE hash=$1 AND vhash=$2 AND type=$3 "
-                   "ORDER BY oid ASC LIMIT 1 OFFSET $4", 4, __LINE__)) ||
+                   "ORDER BY oid ASC LIMIT 1 OFFSET $4", 4)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "gett",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "gett",
                    "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
                    "WHERE hash=$1 AND type=$2 "
-                   "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3, __LINE__)) ||
+                   "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "getv",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "getv",
                    "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
                    "WHERE hash=$1 AND vhash=$2 "
-                   "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3, __LINE__)) ||
+                   "ORDER BY oid ASC LIMIT 1 OFFSET $3", 3)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "get",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "get",
                    "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
-                   "WHERE hash=$1 " "ORDER BY oid ASC LIMIT 1 OFFSET $2", 2,
-                   __LINE__)) ||
+                   "WHERE hash=$1 " "ORDER BY oid ASC LIMIT 1 OFFSET $2", 2)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "put",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "put",
                    "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
-                   "VALUES ($1, $2, $3, $4, $5, RANDOM(), $6, $7, $8)", 9,
-                   __LINE__)) ||
+                   "VALUES ($1, $2, $3, $4, $5, RANDOM(), $6, $7, $8)", 9)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "update",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "update",
                    "UPDATE gn090 SET prio = prio + $1, expire = CASE WHEN expire < $2 THEN $2 ELSE expire END "
-                   "WHERE oid = $3", 3, __LINE__)) ||
+                   "WHERE oid = $3", 3)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "decrepl",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "decrepl",
                    "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) "
-                   "WHERE oid = $1", 1, __LINE__)) ||
+                   "WHERE oid = $1", 1)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "select_non_anonymous",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "select_non_anonymous",
                    "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
                    "WHERE anonLevel = 0 AND type = $1 ORDER BY oid DESC LIMIT 1 OFFSET $2",
-                   1, __LINE__)) ||
+                   1)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "select_expiration_order",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "select_expiration_order",
                    "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
                    "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) " "UNION "
                    "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
                    "ORDER BY prio ASC LIMIT 1) " "ORDER BY expire ASC LIMIT 1",
-                   1, __LINE__)) ||
+                   1)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "select_replication_order",
+       GNUNET_POSTGRES_prepare (plugin->dbh, "select_replication_order",
                    "SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "
-                   "ORDER BY repl DESC,RANDOM() LIMIT 1", 0, __LINE__)) ||
+                   "ORDER BY repl DESC,RANDOM() LIMIT 1", 0)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1,
-                   __LINE__)) ||
+       GNUNET_POSTGRES_prepare (plugin->dbh, "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1)) ||
       (GNUNET_OK !=
-       pq_prepare (plugin, "get_keys", "SELECT hash FROM gn090", 0,
-                   __LINE__)))
+       GNUNET_POSTGRES_prepare (plugin->dbh, "get_keys", "SELECT hash FROM gn090", 0)))
   {
     PQfinish (plugin->dbh);
     plugin->dbh = NULL;
@@ -330,38 +214,6 @@ init_connection (struct Plugin *plugin)
 }
 
 
-/**
- * Delete the row identified by the given rowid (qid
- * in postgres).
- *
- * @param plugin global context
- * @param rowid which row to delete
- * @return GNUNET_OK on success
- */
-static int
-delete_by_rowid (struct Plugin *plugin, uint32_t rowid)
-{
-  uint32_t browid;
-  const char *paramValues[] = { (const char *) &browid };
-  int paramLengths[] = { sizeof (browid) };
-  const int paramFormats[] = { 1 };
-  PGresult *ret;
-
-  browid = htonl (rowid);
-  ret =
-      PQexecPrepared (plugin->dbh, "delrow", 1, paramValues, paramLengths,
-                      paramFormats, 1);
-  if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow",
-                    __LINE__))
-  {
-    return GNUNET_SYSERR;
-  }
-  PQclear (ret);
-  return GNUNET_OK;
-}
-
-
 /**
  * Get an estimate of how much space the database is
  * currently using.
@@ -381,8 +233,7 @@ postgres_plugin_estimate_size (void *cls)
                     "SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090", 0,
                     NULL, NULL, NULL, NULL, 1);
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_TUPLES_OK, "PQexecParams", "get_size",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_TUPLES_OK, "PQexecParams", "get_size"))
   {
     return 0;
   }
@@ -457,8 +308,7 @@ postgres_plugin_put (void *cls, const GNUNET_HashCode * key, uint32_t size,
       PQexecPrepared (plugin->dbh, "put", 8, paramValues, paramLengths,
                       paramFormats, 1);
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put"))
     return GNUNET_SYSERR;
   PQclear (ret);
   plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
@@ -478,11 +328,13 @@ postgres_plugin_put (void *cls, const GNUNET_HashCode * key, uint32_t size,
  * @param proc function to call the value (once only).
  * @param proc_cls closure for proc
  * @param res result from exec
+ * @param filename filename for error messages
  * @param line line number for error messages
  */
 static void
 process_result (struct Plugin *plugin, PluginDatumProcessor proc,
-                void *proc_cls, PGresult * res, int line)
+                void *proc_cls, PGresult * res, 
+               const char *filename, int line)
 {
   int iret;
   enum GNUNET_BLOCK_Type type;
@@ -494,8 +346,8 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
   GNUNET_HashCode key;
 
   if (GNUNET_OK !=
-      check_result (plugin, res, PGRES_TUPLES_OK, "PQexecPrepared", "select",
-                    line))
+      GNUNET_POSTGRES_check_result_ (plugin->dbh, res, PGRES_TUPLES_OK, "PQexecPrepared", "select",
+                                    filename, line))
   {
 #if DEBUG_POSTGRES
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
@@ -534,7 +386,7 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
   {
     GNUNET_break (0);
     PQclear (res);
-    delete_by_rowid (plugin, rowid);
+    GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", rowid);
     proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
     return;
   }
@@ -562,7 +414,7 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Processor asked for item %u to be removed.\n", rowid);
 #endif
-    if (GNUNET_OK == delete_by_rowid (plugin, rowid))
+    if (GNUNET_OK == GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", rowid))
     {
 #if DEBUG_POSTGRES
       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
@@ -679,8 +531,7 @@ postgres_plugin_get_key (void *cls, uint64_t offset,
     }
   }
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_TUPLES_OK, "PQexecParams", pname,
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_TUPLES_OK, "PQexecParams", pname))
   {
     proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
     return;
@@ -704,7 +555,7 @@ postgres_plugin_get_key (void *cls, uint64_t offset,
   ret =
       PQexecPrepared (plugin->dbh, pname, nparams, paramValues, paramLengths,
                       paramFormats, 1);
-  process_result (plugin, proc, proc_cls, ret, __LINE__);
+  process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__);
 }
 
 
@@ -739,7 +590,7 @@ postgres_plugin_get_zero_anonymity (void *cls, uint64_t offset,
   ret =
       PQexecPrepared (plugin->dbh, "select_non_anonymous", 2, paramValues,
                       paramLengths, paramFormats, 1);
-  process_result (plugin, proc, proc_cls, ret, __LINE__);
+  process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__);
 }
 
 
@@ -815,8 +666,8 @@ repl_proc (void *cls, const GNUNET_HashCode * key, uint32_t size,
         PQexecPrepared (plugin->dbh, "decrepl", 1, paramValues, paramLengths,
                         paramFormats, 1);
     if (GNUNET_OK !=
-        check_result (plugin, qret, PGRES_COMMAND_OK, "PQexecPrepared",
-                      "decrepl", __LINE__))
+        GNUNET_POSTGRES_check_result (plugin->dbh, qret, PGRES_COMMAND_OK, "PQexecPrepared",
+                      "decrepl"))
       return GNUNET_SYSERR;
     PQclear (qret);
   }
@@ -848,7 +699,7 @@ postgres_plugin_get_replication (void *cls, PluginDatumProcessor proc,
   ret =
       PQexecPrepared (plugin->dbh, "select_replication_order", 0, NULL, NULL,
                       NULL, 1);
-  process_result (plugin, &repl_proc, &rc, ret, __LINE__);
+  process_result (plugin, &repl_proc, &rc, ret, __FILE__, __LINE__);
 }
 
 
@@ -875,7 +726,7 @@ postgres_plugin_get_expiration (void *cls, PluginDatumProcessor proc,
   ret =
       PQexecPrepared (plugin->dbh, "select_expiration_order", 1, paramValues,
                       paramLengths, paramFormats, 1);
-  process_result (plugin, proc, proc_cls, ret, __LINE__);
+  process_result (plugin, proc, proc_cls, ret, __FILE__, __LINE__);
 }
 
 
@@ -928,8 +779,7 @@ postgres_plugin_update (void *cls, uint64_t uid, int delta,
       PQexecPrepared (plugin->dbh, "update", 3, paramValues, paramLengths,
                       paramFormats, 1);
   if (GNUNET_OK !=
-      check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "update",
-                    __LINE__))
+      GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "update"))
     return GNUNET_SYSERR;
   PQclear (ret);
   return GNUNET_OK;
@@ -978,7 +828,7 @@ postgres_plugin_drop (void *cls)
 {
   struct Plugin *plugin = cls;
 
-  pq_exec (plugin, "DROP TABLE gn090", __LINE__);
+  GNUNET_POSTGRES_exec (plugin->dbh, "DROP TABLE gn090");
 }
 
 
index 4c7ea470350b504d97ac0a225bfe3a5bde3a4c1d..626aae0ec95dc4932024f6b3c73e0b24bce5381f 100644 (file)
@@ -62,6 +62,7 @@ gnunetinclude_HEADERS = \
   gnunet_peer_lib.h \
   gnunet_peerinfo_service.h \
   gnunet_plugin_lib.h \
+  gnunet_postgres_lib.h \
   gnunet_program_lib.h \
   gnunet_protocols.h \
   gnunet_pseudonym_lib.h \
index 7311c2c52b4850cd1a3eef3044911103350c5233..c61bdcab184b1c33100543f02cf609e03f7c32a7 100644 (file)
@@ -26,8 +26,6 @@
 #define GNUNET_MYSQL_LIB_H
 
 #include "gnunet_util_lib.h"
-#include "gnunet_bandwidth_lib.h"
-#include "gnunet_statistics_service.h"
 #include <mysql/mysql.h>
 
 #ifdef __cplusplus
diff --git a/src/include/gnunet_postgres_lib.h b/src/include/gnunet_postgres_lib.h
new file mode 100644 (file)
index 0000000..12c0110
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+     This file is part of GNUnet
+     (C) 2012 Christian Grothoff (and other contributing authors)
+
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 3, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+/**
+ * @file include/gnunet_postgres_lib.h
+ * @brief library to help with access to a Postgres database
+ * @author Christian Grothoff
+ */
+#ifndef GNUNET_POSTGRES_LIB_H
+#define GNUNET_POSTGRES_LIB_H
+
+#include "gnunet_util_lib.h"
+#include <postgresql/libpq-fe.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#if 0                           /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
+
+/**
+ * Check if the result obtained from Postgres has
+ * the desired status code.  If not, log an error, clear the
+ * result and return GNUNET_SYSERR.
+ *
+ * @param dbh database handle
+ * @param ret return value from database operation to check
+ * @param expected_status desired status
+ * @param command description of the command that was run
+ * @param args arguments given to the command 
+ * @param filename name of the source file where the command was run
+ * @param line line number in the source file
+ * @return GNUNET_OK if the result is acceptable
+ */
+int
+GNUNET_POSTGRES_check_result_ (PGconn *dbh, PGresult * ret, int expected_status,
+                              const char *command, const char *args,
+                              const char *filename, int line);
+
+
+/**
+ * Check if the result obtained from Postgres has
+ * the desired status code.  If not, log an error, clear the
+ * result and return GNUNET_SYSERR.
+ *
+ * @param dbh database handle
+ * @param ret return value from database operation to check
+ * @param expected_status desired status
+ * @param command description of the command that was run
+ * @param args arguments given to the command 
+ * @return GNUNET_OK if the result is acceptable
+ */
+#define GNUNET_POSTGRES_check_result(dbh,ret,expected_status,command,args) GNUNET_POSTGRES_check_result_(dbh,ret,expected_status,command,args,__FILE__,__LINE__)
+
+
+/**
+ * Run simple SQL statement (without results).
+ *
+ * @param dbh database handle
+ * @param sql statement to run
+ * @param filename filename for error reporting
+ * @param line code line for error reporting 
+ * @return GNUNET_OK on success
+ */
+int
+GNUNET_POSTGRES_exec_ (PGconn *dbh, const char *sql, const char *filename, int line);
+
+
+/**
+ * Run simple SQL statement (without results).
+ *
+ * @param dbh database handle
+ * @param sql statement to run
+ * @return GNUNET_OK on success
+ */
+#define GNUNET_POSTGRES_exec(dbh,sql) GNUNET_POSTGRES_exec_(dbh,sql,__FILE__,__LINE__)
+
+
+/**
+ * Prepare SQL statement.
+ *
+ * @param dbh database handle
+ * @param name name for the prepared SQL statement
+ * @param sql SQL code to prepare
+ * @param nparams number of parameters in sql
+ * @param filename filename for error reporting
+ * @param line code line for error reporting
+ * @return GNUNET_OK on success
+ */
+int
+GNUNET_POSTGRES_prepare_ (PGconn *dbh, const char *name, const char *sql,
+                         int nparms,
+                         const char *filename, int line);
+
+
+/**
+ * Prepare SQL statement.
+ *
+ * @param dbh database handle
+ * @param name name for the prepared SQL statement
+ * @param sql SQL code to prepare
+ * @param nparams number of parameters in sql
+ * @return GNUNET_OK on success
+ */
+#define GNUNET_POSTGRES_prepare(dbh,name,sql,nparams) GNUNET_POSTGRES_prepare_(dbh,name,sql,nparams,__FILE__,__LINE__)
+
+
+/**
+ * Connect to a postgres database
+ *
+ * @param cfg configuration
+ * @param section configuration section to use to get Postgres configuration options
+ * @return the postgres handle
+ */
+PGconn *
+GNUNET_POSTGRES_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                        const char *section);
+
+
+/**
+ * Delete the row identified by the given rowid (qid
+ * in postgres).
+ *
+ * @param dbh database handle
+ * @param stmt name of the prepared statement
+ * @param rowid which row to delete
+ * @return GNUNET_OK on success
+ */
+int
+GNUNET_POSTGRES_delete_by_rowid (PGconn *dbh,
+                                const char *stmt,
+                                uint32_t rowid);
+
+
+
+#if 0                           /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
+#endif
+
+/* end of gnunet_postgres_lib.h */
+#endif
diff --git a/src/postgres/Makefile.am b/src/postgres/Makefile.am
new file mode 100644 (file)
index 0000000..0b3be5e
--- /dev/null
@@ -0,0 +1,20 @@
+INCLUDES = -I$(top_srcdir)/src/include
+
+if MINGW
+  WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols
+endif
+
+if USE_COVERAGE
+  AM_CFLAGS = --coverage
+endif
+
+lib_LTLIBRARIES = libgnunetpostgres.la
+
+libgnunetpostgres_la_SOURCES = \
+  postgres.c
+libgnunetpostgres_la_LIBADD = -lpq \
+ $(top_builddir)/src/util/libgnunetutil.la  
+libgnunetpostgres_la_LDFLAGS = \
+ $(GN_LIB_LDFLAGS) \
+  -version-info 0:0:0
+
diff --git a/src/postgres/postgres.c b/src/postgres/postgres.c
new file mode 100644 (file)
index 0000000..c6b9a5a
--- /dev/null
@@ -0,0 +1,194 @@
+/*
+     This file is part of GNUnet
+     (C) 2009, 2010, 2012 Christian Grothoff (and other contributing authors)
+
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 3, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+/**
+ * @file postgres/postgres.c
+ * @brief library to help with access to a Postgres database
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "gnunet_postgres_lib.h"
+
+
+/**
+ * Check if the result obtained from Postgres has
+ * the desired status code.  If not, log an error, clear the
+ * result and return GNUNET_SYSERR.
+ *
+ * @param dbh database handle
+ * @param ret return value from database operation to check
+ * @param expected_status desired status
+ * @param command description of the command that was run
+ * @param args arguments given to the command 
+ * @param filename name of the source file where the command was run
+ * @param line line number in the source file
+ * @return GNUNET_OK if the result is acceptable
+ */
+int
+GNUNET_POSTGRES_check_result_ (PGconn *dbh, PGresult * ret, int expected_status,
+                             const char *command, const char *args, const char *filename,
+                             int line)
+{
+  if (ret == NULL)
+  {
+    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
+                     "postgres",
+                     "Postgres failed to allocate result for `%s:%s' at %s:%d\n",
+                     command, args, filename, line);
+    return GNUNET_SYSERR;
+  }
+  if (PQresultStatus (ret) != expected_status)
+  {
+    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
+                     "postgres",
+                     _("`%s:%s' failed at %s:%d with error: %s"), command, args,
+                     filename, line, PQerrorMessage (dbh));
+    PQclear (ret);
+    return GNUNET_SYSERR;
+  }
+  return GNUNET_OK;
+}
+
+
+/**
+ * Run simple SQL statement (without results).
+ *
+ * @param dbh database handle
+ * @param sql statement to run
+ * @param filename filename for error reporting
+ * @param line code line for error reporting 
+ * @return GNUNET_OK on success
+ */
+int
+GNUNET_POSTGRES_exec_ (PGconn *dbh, const char *sql,
+                     const char *filename,
+                     int line)
+{
+  PGresult *ret;
+
+  ret = PQexec (dbh, sql);
+  if (GNUNET_OK !=
+      GNUNET_POSTGRES_check_result_ (dbh, ret, PGRES_COMMAND_OK, "PQexec", sql, filename, line))
+    return GNUNET_SYSERR;
+  PQclear (ret);
+  return GNUNET_OK;
+}
+
+
+/**
+ * Prepare SQL statement.
+ *
+ * @param dbh database handle
+ * @param name name for the prepared SQL statement
+ * @param sql SQL code to prepare
+ * @param nparams number of parameters in sql
+ * @param filename filename for error reporting
+ * @param line code line for error reporting
+ * @return GNUNET_OK on success
+ */
+int
+GNUNET_POSTGRES_prepare_ (PGconn *dbh, const char *name, const char *sql,
+                        int nparms, const char *filename, int line)
+{
+  PGresult *ret;
+
+  ret = PQprepare (dbh, name, sql, nparms, NULL);
+  if (GNUNET_OK !=
+      GNUNET_POSTGRES_check_result_ (dbh, ret, PGRES_COMMAND_OK, "PQprepare", sql, filename, line))
+    return GNUNET_SYSERR;
+  PQclear (ret);
+  return GNUNET_OK;
+}
+
+
+/**
+ * Connect to a postgres database
+ *
+ * @param cfg configuration
+ * @param section configuration section to use to get Postgres configuration options
+ * @return the postgres handle
+ */
+PGconn *
+GNUNET_POSTGRES_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                        const char *section)
+{
+  PGconn *dbh;
+  char *conninfo;
+
+  /* Open database and precompile statements */
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_string (cfg,
+                                             section, "CONFIG",
+                                             &conninfo))
+    conninfo = NULL;
+  dbh = PQconnectdb (conninfo == NULL ? "" : conninfo);
+  GNUNET_free_non_null (conninfo);
+  if (NULL == dbh)
+  {
+    /* FIXME: warn about out-of-memory? */
+    return NULL;
+  }
+  if (PQstatus (dbh) != CONNECTION_OK)
+  {
+    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "postgres",
+                     _("Unable to initialize Postgres: %s"),
+                     PQerrorMessage (dbh));
+    PQfinish (dbh);
+    return NULL;
+  }
+  return dbh;
+}
+
+
+/**
+ * Delete the row identified by the given rowid (qid
+ * in postgres).
+ *
+ * @param database handle
+ * @param stmt name of the prepared statement
+ * @param rowid which row to delete
+ * @return GNUNET_OK on success
+ */
+int
+GNUNET_POSTGRES_delete_by_rowid (PGconn *dbh,
+                                const char *stmt,
+                                uint32_t rowid)
+{
+  uint32_t brow = htonl (rowid);
+  const char *paramValues[] = { (const char *) &brow };
+  int paramLengths[] = { sizeof (brow) };
+  const int paramFormats[] = { 1 };
+  PGresult *ret;
+
+  ret =
+    PQexecPrepared (dbh, stmt, 1, paramValues, paramLengths,
+                      paramFormats, 1);
+  if (GNUNET_OK !=
+      GNUNET_POSTGRES_check_result_ (dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow",
+                                   __FILE__,
+                                   __LINE__))
+  {
+    return GNUNET_SYSERR;
+  }
+  PQclear (ret);
+  return GNUNET_OK;
+}
+
+
+/* end of postgres.c */