commented out wrong message type
[oweals/gnunet.git] / src / psycstore / plugin_psycstore_postgres.c
index cd933667ff37f3f52f7df4c7c8deeb3be0d64eb1..046daf6eadcfb8271265d0cee3c4dcdea089a146 100644 (file)
@@ -25,6 +25,7 @@
  * @author Gabor X Toth
  * @author Christian Grothoff
  * @author Christophe Genevey
+ * @author Jeffrey Burdges
  */
 
 #include "platform.h"
@@ -34,7 +35,6 @@
 #include "gnunet_crypto_lib.h"
 #include "gnunet_psyc_util_lib.h"
 #include "psycstore.h"
-#include "gnunet_postgres_lib.h"
 #include "gnunet_pq_lib.h"
 
 /**
@@ -391,7 +391,7 @@ exec_channel (struct Plugin *plugin, const char *stmt,
     GNUNET_PQ_query_param_end
   };
 
-  if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS !=
+  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
       GNUNET_PQ_eval_prepared_non_select (plugin->dbh, stmt, params))
     return GNUNET_SYSERR;
 
@@ -409,7 +409,7 @@ transaction_begin (struct Plugin *plugin, enum Transactions transaction)
     GNUNET_PQ_query_param_end
   };
 
-  if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS !=
+  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
       GNUNET_PQ_eval_prepared_non_select (plugin->dbh, "transaction_begin", params))
     return GNUNET_SYSERR;
 
@@ -428,7 +428,7 @@ transaction_commit (struct Plugin *plugin)
     GNUNET_PQ_query_param_end
   };
 
-  if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS !=
+  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
       GNUNET_PQ_eval_prepared_non_select (plugin->dbh, "transaction_commit", params))
     return GNUNET_SYSERR;
 
@@ -447,7 +447,7 @@ transaction_rollback (struct Plugin *plugin)
     GNUNET_PQ_query_param_end
   };
 
-  if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS !=
+  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
       GNUNET_PQ_eval_prepared_non_select (plugin->dbh, "transaction_rollback", params))
     return GNUNET_SYSERR;
 
@@ -465,8 +465,10 @@ channel_key_store (struct Plugin *plugin,
     GNUNET_PQ_query_param_end
   };
 
-  if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS !=
-      GNUNET_PQ_eval_prepared_non_select (plugin->dbh, "insert_channel_key", params))
+  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
+      GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
+                                         "insert_channel_key",
+                                         params))
     return GNUNET_SYSERR;
 
   return GNUNET_OK;
@@ -482,7 +484,7 @@ slave_key_store (struct Plugin *plugin,
     GNUNET_PQ_query_param_end
   };
 
-  if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS !=
+  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
       GNUNET_PQ_eval_prepared_non_select (plugin->dbh, "insert_slave_key", params))
     return GNUNET_SYSERR;
 
@@ -508,21 +510,22 @@ postgres_membership_store (void *cls,
                            uint64_t group_generation)
 {
   struct Plugin *plugin = cls;
-
-  uint32_t idid_join = (uint32_t)did_join;
+  uint32_t idid_join = (uint32_t) did_join;
 
   GNUNET_assert (TRANSACTION_NONE == plugin->transaction);
 
-  if (announced_at > INT64_MAX ||
-      effective_since > INT64_MAX ||
-      group_generation > INT64_MAX)
+  if ( (announced_at > INT64_MAX) ||
+       (effective_since > INT64_MAX) ||
+       (group_generation > INT64_MAX) )
   {
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
 
-  if (GNUNET_OK != channel_key_store (plugin, channel_key)
-      || GNUNET_OK != slave_key_store (plugin, slave_key))
+  if ( (GNUNET_OK !=
+       channel_key_store (plugin, channel_key)) ||
+       (GNUNET_OK !=
+       slave_key_store (plugin, slave_key)) )
     return GNUNET_SYSERR;
 
   struct GNUNET_PQ_QueryParam params[] = {
@@ -535,8 +538,10 @@ postgres_membership_store (void *cls,
     GNUNET_PQ_query_param_end
   };
 
-  if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS !=
-      GNUNET_PQ_eval_prepared_non_select (plugin->dbh, "insert_membership", params))
+  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
+      GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
+                                         "insert_membership",
+                                         params))
     return GNUNET_SYSERR;
 
   return GNUNET_OK;
@@ -556,13 +561,10 @@ membership_test (void *cls,
                  const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
                  uint64_t message_id)
 {
-  PGresult *res;
   struct Plugin *plugin = cls;
 
   uint32_t did_join = 0;
 
-  int ret = GNUNET_SYSERR;
-
   struct GNUNET_PQ_QueryParam params_select[] = {
     GNUNET_PQ_query_param_auto_from_type (channel_key),
     GNUNET_PQ_query_param_auto_from_type (slave_key),
@@ -570,35 +572,17 @@ membership_test (void *cls,
     GNUNET_PQ_query_param_end
   };
 
-  res = GNUNET_PQ_exec_prepared (plugin->dbh, "select_membership", params_select);
-  if (GNUNET_OK !=
-      GNUNET_POSTGRES_check_result (plugin->dbh,
-                                    res,
-                                    PGRES_TUPLES_OK,
-                                    "PQexecPrepared", "select_membership"))
-  {
-    return GNUNET_SYSERR;
-  }
-
   struct GNUNET_PQ_ResultSpec results_select[] = {
     GNUNET_PQ_result_spec_uint32 ("did_join", &did_join),
     GNUNET_PQ_result_spec_end
   };
 
-  switch (GNUNET_PQ_extract_result (res, results_select, 0))
-  {
-    case GNUNET_OK:
-      ret = GNUNET_YES;
-      break;
-
-    default:
-      ret = GNUNET_NO;
-      break;
-  }
+  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
+      GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh, "select_membership", 
+                                                params_select, results_select))
+     return GNUNET_SYSERR;
 
-  PQclear (res);
-
-  return ret;
+  return GNUNET_OK;
 }
 
 /**
@@ -656,7 +640,7 @@ fragment_store (void *cls,
     GNUNET_PQ_query_param_end
   };
 
-  if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS !=
+  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
       GNUNET_PQ_eval_prepared_non_select (plugin->dbh, "insert_fragment", params_insert))
     return GNUNET_SYSERR;
 
@@ -685,7 +669,7 @@ message_add_flags (void *cls,
     GNUNET_PQ_query_param_end
   };
 
-  if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS !=
+  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
       GNUNET_PQ_eval_prepared_non_select (plugin->dbh, "update_message_flags", params_update))
     return GNUNET_SYSERR;
 
@@ -693,59 +677,72 @@ message_add_flags (void *cls,
 }
 
 
-static int
-fragment_row (struct Plugin *plugin,
-              const char *stmt,
-              PGresult *res,
-              GNUNET_PSYCSTORE_FragmentCallback cb,
-              void *cb_cls,
-              uint64_t *returned_fragments)
+/**
+ * Closure for #fragment_rows.
+ */
+struct FragmentRowsContext {
+  GNUNET_PSYCSTORE_FragmentCallback cb;
+  void *cb_cls;
+
+  uint64_t *returned_fragments;
+
+  /* I preserved this but I do not see the point since
+   * it cannot stop the loop early and gets overwritten ?? */
+  int ret;
+};
+
+
+/**
+ * Callback that retrieves the results of a SELECT statement
+ * reading form the messages table.
+ *
+ * Only passed to GNUNET_PQ_eval_prepared_multi_select and
+ * has type GNUNET_PQ_PostgresResultHandler.
+ *
+ * @param cls closure
+ * @param result the postgres result
+ * @param num_result the number of results in @a result
+ */
+void fragment_rows (void *cls,
+                    PGresult *res,
+                    unsigned int num_results)
 {
-  uint32_t hop_counter;
-  void *signature = NULL;
-  void *purpose = NULL;
-  size_t signature_size;
-  size_t purpose_size;
-  uint64_t fragment_id;
-  uint64_t fragment_offset;
-  uint64_t message_id;
-  uint64_t group_generation;
-  uint32_t flags;
-  void *buf;
-  size_t buf_size;
-  int ret = GNUNET_SYSERR;
-  struct GNUNET_MULTICAST_MessageHeader *mp;
-  uint32_t msg_flags;
-  struct GNUNET_PQ_ResultSpec results[] = {
-    GNUNET_PQ_result_spec_uint32 ("hop_counter", &hop_counter),
-    GNUNET_PQ_result_spec_variable_size ("signature", &signature, &signature_size),
-    GNUNET_PQ_result_spec_variable_size ("purpose", &purpose, &purpose_size),
-    GNUNET_PQ_result_spec_uint64 ("fragment_id", &fragment_id),
-    GNUNET_PQ_result_spec_uint64 ("fragment_offset", &fragment_offset),
-    GNUNET_PQ_result_spec_uint64 ("message_id", &message_id),
-    GNUNET_PQ_result_spec_uint64 ("group_generation", &group_generation),
-    GNUNET_PQ_result_spec_uint32 ("multicast_flags", &msg_flags),
-    GNUNET_PQ_result_spec_uint32 ("psycstore_flags", &flags),
-    GNUNET_PQ_result_spec_variable_size ("data", &buf, &buf_size),
-    GNUNET_PQ_result_spec_end
-  };
+  struct FragmentRowsContext *c = cls;
 
-  if (GNUNET_OK !=
-      GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK,
-                                    "PQexecPrepared",
-                                    stmt))
+  for (unsigned int i=0;i<num_results;i++)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Failing fragment lookup (postgres error)\n");
-    return GNUNET_SYSERR;
-  }
+    uint32_t hop_counter;
+    void *signature = NULL;
+    void *purpose = NULL;
+    size_t signature_size;
+    size_t purpose_size;
+    uint64_t fragment_id;
+    uint64_t fragment_offset;
+    uint64_t message_id;
+    uint64_t group_generation;
+    uint32_t flags;
+    void *buf;
+    size_t buf_size;
+    uint32_t msg_flags;
+    struct GNUNET_PQ_ResultSpec results[] = {
+      GNUNET_PQ_result_spec_uint32 ("hop_counter", &hop_counter),
+      GNUNET_PQ_result_spec_variable_size ("signature", &signature, &signature_size),
+      GNUNET_PQ_result_spec_variable_size ("purpose", &purpose, &purpose_size),
+      GNUNET_PQ_result_spec_uint64 ("fragment_id", &fragment_id),
+      GNUNET_PQ_result_spec_uint64 ("fragment_offset", &fragment_offset),
+      GNUNET_PQ_result_spec_uint64 ("message_id", &message_id),
+      GNUNET_PQ_result_spec_uint64 ("group_generation", &group_generation),
+      GNUNET_PQ_result_spec_uint32 ("multicast_flags", &msg_flags),
+      GNUNET_PQ_result_spec_uint32 ("psycstore_flags", &flags),
+      GNUNET_PQ_result_spec_variable_size ("data", &buf, &buf_size),
+      GNUNET_PQ_result_spec_end
+    };
+    struct GNUNET_MULTICAST_MessageHeader *mp;
 
-  int nrows = PQntuples (res);
-  for (int row = 0; row < nrows; row++)
-  {
-    if (GNUNET_OK != GNUNET_PQ_extract_result (res, results, row))
+    if (GNUNET_YES != GNUNET_PQ_extract_result (res, results, i))
     {
-      break;
+      GNUNET_PQ_cleanup_result(results);  /* missing previously, a memory leak?? */
+      break;  /* nothing more?? */
     }
 
     mp = GNUNET_malloc (sizeof (*mp) + buf_size);
@@ -754,11 +751,9 @@ fragment_row (struct Plugin *plugin,
     mp->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
     mp->hop_counter = htonl (hop_counter);
     GNUNET_memcpy (&mp->signature,
-                   signature,
-                   signature_size);
+                   signature, signature_size);
     GNUNET_memcpy (&mp->purpose,
-                   purpose,
-                   purpose_size);
+                   purpose, purpose_size);
     mp->fragment_id = GNUNET_htonll (fragment_id);
     mp->fragment_offset = GNUNET_htonll (fragment_offset);
     mp->message_id = GNUNET_htonll (message_id);
@@ -766,15 +761,12 @@ fragment_row (struct Plugin *plugin,
     mp->flags = htonl(msg_flags);
 
     GNUNET_memcpy (&mp[1],
-                   buf,
-                   buf_size);
+                   buf, buf_size);
     GNUNET_PQ_cleanup_result(results);
-    ret = cb (cb_cls, mp, (enum GNUNET_PSYCSTORE_MessageFlags) flags);
-    if (NULL != returned_fragments)
-      (*returned_fragments)++;
+    c->ret = c->cb (c->cb_cls, mp, (enum GNUNET_PSYCSTORE_MessageFlags) flags);
+    if (NULL != c->returned_fragments)
+      (*c->returned_fragments)++;
   }
-
-  return ret;
 }
 
 
@@ -786,26 +778,19 @@ fragment_select (struct Plugin *plugin,
                  GNUNET_PSYCSTORE_FragmentCallback cb,
                  void *cb_cls)
 {
-  PGresult *res;
-  int ret = GNUNET_SYSERR;
-
-  res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params);
-  if (GNUNET_YES ==
-      GNUNET_POSTGRES_check_result (plugin->dbh,
-                                    res,
-                                    PGRES_TUPLES_OK,
-                                    "PQexecPrepared", stmt))
-  {
-    if (PQntuples (res) == 0)
-      ret = GNUNET_NO;
-    else
-    {
-      ret = fragment_row (plugin, stmt, res, cb, cb_cls, returned_fragments);
-    }
-    PQclear (res);
-  }
+  /* Stack based closure */
+  struct FragmentRowsContext frc = {
+    .cb = cb,
+    .cb_cls = cb_cls,
+    .returned_fragments = returned_fragments,
+    .ret = GNUNET_SYSERR
+  };
 
-  return ret;
+  if (0 > GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
+                                                stmt, params,
+                                                &fragment_rows, &frc))
+    return GNUNET_SYSERR;
+  return frc.ret;  /* GNUNET_OK ?? */
 }
 
 /**
@@ -959,9 +944,7 @@ message_get_fragment (void *cls,
                       GNUNET_PSYCSTORE_FragmentCallback cb,
                       void *cb_cls)
 {
-  PGresult *res;
   struct Plugin *plugin = cls;
-  int ret = GNUNET_SYSERR;
   const char *stmt = "select_message_fragment";
 
   struct GNUNET_PQ_QueryParam params_select[] = {
@@ -971,21 +954,19 @@ message_get_fragment (void *cls,
     GNUNET_PQ_query_param_end
   };
 
-  res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
-  if (GNUNET_OK == GNUNET_POSTGRES_check_result (plugin->dbh,
-                                                 res,
-                                                 PGRES_TUPLES_OK,
-                                                 "PQexecPrepared", stmt))
-  {
-    if (PQntuples (res) == 0)
-      ret = GNUNET_NO;
-    else
-      ret = fragment_row (plugin, stmt, res, cb, cb_cls, NULL);
-
-    PQclear (res);
-  }
+  /* Stack based closure */
+  struct FragmentRowsContext frc = {
+    .cb = cb,
+    .cb_cls = cb_cls,
+    .returned_fragments = NULL,
+    .ret = GNUNET_SYSERR
+  };
 
-  return ret;
+  if (0 > GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
+                                                stmt, params_select,
+                                                &fragment_rows, &frc))
+    return GNUNET_SYSERR;
+  return frc.ret;  /* GNUNET_OK ?? */
 }
 
 /**
@@ -1002,7 +983,6 @@ counters_message_get (void *cls,
                       uint64_t *max_message_id,
                       uint64_t *max_group_generation)
 {
-  PGresult *res;
   struct Plugin *plugin = cls;
 
   const char *stmt = "select_counters_message";
@@ -1012,15 +992,6 @@ counters_message_get (void *cls,
     GNUNET_PQ_query_param_end
   };
 
-  res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
-  if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
-                                                 res,
-                                                 PGRES_TUPLES_OK,
-                                                 "PQexecPrepared", stmt))
-  {
-    return GNUNET_SYSERR;
-  }
-
   struct GNUNET_PQ_ResultSpec results_select[] = {
     GNUNET_PQ_result_spec_uint64 ("fragment_id", max_fragment_id),
     GNUNET_PQ_result_spec_uint64 ("message_id", max_message_id),
@@ -1028,14 +999,10 @@ counters_message_get (void *cls,
     GNUNET_PQ_result_spec_end
   };
 
-  if (GNUNET_OK != GNUNET_PQ_extract_result (res, results_select, 0))
-  {
-    PQclear (res);
-    return GNUNET_SYSERR;
-  }
-
-  GNUNET_PQ_cleanup_result(results_select);
-  PQclear (res);
+  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
+      GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh, stmt, 
+                                                params_select, results_select))
+     return GNUNET_SYSERR;
 
   return GNUNET_OK;
 }
@@ -1052,44 +1019,26 @@ counters_state_get (void *cls,
                     const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
                     uint64_t *max_state_message_id)
 {
-  PGresult *res;
   struct Plugin *plugin = cls;
 
   const char *stmt = "select_counters_state";
 
-  int ret = GNUNET_SYSERR;
-
   struct GNUNET_PQ_QueryParam params_select[] = {
     GNUNET_PQ_query_param_auto_from_type (channel_key),
     GNUNET_PQ_query_param_end
   };
 
-  res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
-  if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
-                                                 res,
-                                                 PGRES_TUPLES_OK,
-                                                 "PQexecPrepared", stmt))
-  {
-    return GNUNET_SYSERR;
-  }
-
   struct GNUNET_PQ_ResultSpec results_select[] = {
     GNUNET_PQ_result_spec_uint64 ("max_state_message_id", max_state_message_id),
     GNUNET_PQ_result_spec_end
   };
 
-  ret = GNUNET_PQ_extract_result (res, results_select, 0);
-
-  if (GNUNET_OK != ret)
-  {
-    PQclear (res);
-    return GNUNET_SYSERR;
-  }
-
-  GNUNET_PQ_cleanup_result(results_select);
-  PQclear (res);
+  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
+      GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh, stmt, 
+                                                params_select, results_select))
+     return GNUNET_SYSERR;
 
-  return ret;
+  return GNUNET_OK;
 }
 
 
@@ -1110,7 +1059,7 @@ state_assign (struct Plugin *plugin, const char *stmt,
     GNUNET_PQ_query_param_end
   };
 
-  if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS !=
+  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
       GNUNET_PQ_eval_prepared_non_select (plugin->dbh, stmt, params))
     return GNUNET_SYSERR;
 
@@ -1130,7 +1079,7 @@ update_message_id (struct Plugin *plugin,
     GNUNET_PQ_query_param_end
   };
 
-  if (GNUNET_PQ_STATUS_SUCCESS_NO_RESULTS !=
+  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
       GNUNET_PQ_eval_prepared_non_select (plugin->dbh, stmt, params))
     return GNUNET_SYSERR;
 
@@ -1343,10 +1292,7 @@ static int
 state_get (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
            const char *name, GNUNET_PSYCSTORE_StateCallback cb, void *cb_cls)
 {
-  PGresult *res;
-
   struct Plugin *plugin = cls;
-  int ret = GNUNET_SYSERR;
 
   const char *stmt = "select_state_one";
 
@@ -1359,43 +1305,79 @@ state_get (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
   void *value_current = NULL;
   size_t value_size = 0;
 
-  struct GNUNET_PQ_ResultSpec results[] = {
+  struct GNUNET_PQ_ResultSpec results_select[] = {
     GNUNET_PQ_result_spec_variable_size ("value_current", &value_current, &value_size),
     GNUNET_PQ_result_spec_end
   };
 
-  res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
-  if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
-                                                 res,
-                                                 PGRES_TUPLES_OK,
-                                                 "PQexecPrepared", stmt))
-  {
-    return GNUNET_SYSERR;
-  }
+  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT !=
+      GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh, stmt, 
+                                                params_select, results_select))
+     return GNUNET_SYSERR;
 
-  if (PQntuples (res) == 0)
-  {
-    PQclear (res);
-    ret = GNUNET_NO;
-  }
+  return cb (cb_cls, name, value_current,
+            value_size);
+}
 
-  ret = GNUNET_PQ_extract_result (res, results, 0);
 
-  if (GNUNET_OK != ret)
-  {
-    PQclear (res);
-    return GNUNET_SYSERR;
-  }
 
-  ret = cb (cb_cls, name, value_current,
-            value_size);
+/**
+ * Closure for #get_state_cb.
+ */
+struct GetStateContext {
+  const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key;
+  // const char *name,
+  GNUNET_PSYCSTORE_StateCallback cb;
+  void *cb_cls;
 
-  GNUNET_PQ_cleanup_result(results);
-  PQclear (res);
+  const char *value_id;
 
-  return ret;
-}
+  /* I preserved this but I do not see the point since
+   * it cannot stop the loop early and gets overwritten ?? */
+  int ret;
+};
+
+
+/**
+ * Callback that retrieves the results of a SELECT statement
+ * reading form the state table.
+ *
+ * Only passed to GNUNET_PQ_eval_prepared_multi_select and
+ * has type GNUNET_PQ_PostgresResultHandler.
+ *
+ * @param cls closure
+ * @param result the postgres result
+ * @param num_result the number of results in @a result
+ */
+static void 
+get_state_cb (void *cls,
+                PGresult *res,
+                unsigned int num_results)
+{
+  struct GetStateContext *c = cls;
+
+  for (unsigned int i=0;i<num_results;i++)
+  {
+    char *name = "";
+    void *value = NULL;
+    size_t value_size = 0;
+
+    struct GNUNET_PQ_ResultSpec results[] = {
+      GNUNET_PQ_result_spec_string ("name", &name),
+      GNUNET_PQ_result_spec_variable_size (c->value_id, &value, &value_size),
+      GNUNET_PQ_result_spec_end
+    };
+
+    if (GNUNET_YES != GNUNET_PQ_extract_result (res, results, i))
+    {
+      GNUNET_PQ_cleanup_result(results);  /* previously invoked via PQclear?? */
+      break;  /* nothing more?? */
+    }
 
+    c->ret = c->cb (c->cb_cls, (const char *) name, value, value_size);
+    GNUNET_PQ_cleanup_result(results);
+  }
+}
 
 /**
  * Retrieve all state variables for a channel with the given prefix.
@@ -1409,9 +1391,7 @@ state_get_prefix (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_
                   const char *name, GNUNET_PSYCSTORE_StateCallback cb,
                   void *cb_cls)
 {
-  PGresult *res;
   struct Plugin *plugin = cls;
-  int ret = GNUNET_NO;
 
   const char *stmt = "select_state_prefix";
 
@@ -1425,42 +1405,18 @@ state_get_prefix (void *cls, const struct GNUNET_CRYPTO_EddsaPublicKey *channel_
     GNUNET_PQ_query_param_end
   };
 
-  char *name2 = "";
-  void *value_current = NULL;
-  size_t value_size = 0;
-
-  struct GNUNET_PQ_ResultSpec results[] = {
-    GNUNET_PQ_result_spec_string ("name", &name2),
-    GNUNET_PQ_result_spec_variable_size ("value_current", &value_current, &value_size),
-    GNUNET_PQ_result_spec_end
+  struct GetStateContext gsc = {
+    .cb = cb,
+    .cb_cls = cb_cls,
+    .value_id = "value_current",
+    .ret = GNUNET_NO
   };
 
-  res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
-  if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
-                                                 res,
-                                                 PGRES_TUPLES_OK,
-                                                 "PQexecPrepared", stmt))
-  {
+  if (0 > GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
+                                                stmt, params_select,
+                                                &get_state_cb, &gsc))
     return GNUNET_SYSERR;
-  }
-
-  int nrows = PQntuples (res);
-  for (int row = 0; row < nrows; row++)
-  {
-    if (GNUNET_OK != GNUNET_PQ_extract_result (res, results, row))
-    {
-      break;
-    }
-
-    ret = cb (cb_cls, (const char *) name2,
-              value_current,
-              value_size);
-    GNUNET_PQ_cleanup_result(results);
-  }
-
-  PQclear (res);
-
-  return ret;
+  return gsc.ret;  /* GNUNET_OK ?? */
 }
 
 
@@ -1476,9 +1432,7 @@ state_get_signed (void *cls,
                   const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
                   GNUNET_PSYCSTORE_StateCallback cb, void *cb_cls)
 {
-  PGresult *res;
   struct Plugin *plugin = cls;
-  int ret = GNUNET_NO;
 
   const char *stmt = "select_state_signed";
 
@@ -1487,43 +1441,18 @@ state_get_signed (void *cls,
     GNUNET_PQ_query_param_end
   };
 
-  char *name = "";
-  void *value_signed = NULL;
-  size_t value_size = 0;
-
-  struct GNUNET_PQ_ResultSpec results[] = {
-    GNUNET_PQ_result_spec_string ("name", &name),
-    GNUNET_PQ_result_spec_variable_size ("value_signed", &value_signed, &value_size),
-    GNUNET_PQ_result_spec_end
+  struct GetStateContext gsc = {
+    .cb = cb,
+    .cb_cls = cb_cls,
+    .value_id = "value_signed",
+    .ret = GNUNET_NO
   };
 
-  res = GNUNET_PQ_exec_prepared (plugin->dbh, stmt, params_select);
-  if (GNUNET_OK != GNUNET_POSTGRES_check_result (plugin->dbh,
-                                                 res,
-                                                 PGRES_TUPLES_OK,
-                                                 "PQexecPrepared", stmt))
-  {
+  if (0 > GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
+                                                stmt, params_select,
+                                                &get_state_cb, &gsc))
     return GNUNET_SYSERR;
-  }
-
-  int nrows = PQntuples (res);
-  for (int row = 0; row < nrows; row++)
-  {
-    if (GNUNET_OK != GNUNET_PQ_extract_result (res, results, row))
-    {
-      break;
-    }
-
-    ret = cb (cb_cls, (const char *) name,
-              value_signed,
-              value_size);
-
-    GNUNET_PQ_cleanup_result (results);
-  }
-
-  PQclear (res);
-
-  return ret;
+  return gsc.ret;  /* GNUNET_OK ?? */
 }