misc minor fixes
[oweals/gnunet.git] / src / namecache / plugin_namecache_postgres.c
index 72cfcf243dc75d9563709c41b0b9f4703c18e657..bec8bffd27b6b3e2fd2d86cb4dfb79bffe1186c9 100644 (file)
@@ -1,6 +1,6 @@
  /*
   * This file is part of GNUnet
-  * (C) 2009-2013 Christian Grothoff (and other contributing authors)
+  * Copyright (C) 2009-2013, 2016 GNUnet e.V.
   *
   * GNUnet is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
   *
   * 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.
+  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+  * Boston, MA 02110-1301, USA.
   */
 
 /**
@@ -28,6 +28,7 @@
 #include "gnunet_namecache_service.h"
 #include "gnunet_gnsrecord_lib.h"
 #include "gnunet_postgres_lib.h"
+#include "gnunet_pq_lib.h"
 #include "namecache.h"
 
 
@@ -184,19 +185,15 @@ static void
 namecache_postgres_expire_blocks (struct Plugin *plugin)
 {
   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
-  struct GNUNET_TIME_AbsoluteNBO now_be = GNUNET_TIME_absolute_hton (now);
-  const char *paramValues[] = {
-    (const char *) &now_be
+  struct GNUNET_PQ_QueryParam params[] = { 
+    GNUNET_PQ_query_param_absolute_time (&now),
+    GNUNET_PQ_query_param_end
   };
-  int paramLengths[] = {
-    sizeof (now_be)
-  };
-  const int paramFormats[] = { 1 };
   PGresult *res;
 
-  res =
-    PQexecPrepared (plugin->dbh, "expire_blocks", 1,
-                   paramValues, paramLengths, paramFormats, 1);
+  res = GNUNET_PQ_exec_prepared (plugin->dbh,
+                                "expire_blocks",
+                                params);
   if (GNUNET_OK !=
       GNUNET_POSTGRES_check_result (plugin->dbh,
                                     res,
@@ -211,30 +208,25 @@ namecache_postgres_expire_blocks (struct Plugin *plugin)
 /**
  * Delete older block in the datastore.
  *
- * @param the plugin
+ * @param plugin the plugin
  * @param query query for the block
- * @param expiration time how old does the block have to be for deletion
- * @return #GNUNET_OK on success, else #GNUNET_SYSERR
+ * @param expiration_time how old does the block have to be for deletion
  */
 static void
 delete_old_block (struct Plugin *plugin,
                   const struct GNUNET_HashCode *query,
                   struct GNUNET_TIME_AbsoluteNBO expiration_time)
 {
-  const char *paramValues[] = {
-    (const char *) query,
-    (const char *) &expiration_time
+  struct GNUNET_PQ_QueryParam params[] = { 
+    GNUNET_PQ_query_param_auto_from_type (query),
+    GNUNET_PQ_query_param_absolute_time_nbo (&expiration_time),
+    GNUNET_PQ_query_param_end
   };
-  int paramLengths[] = {
-    sizeof (*query),
-    sizeof (expiration_time)
-  };
-  const int paramFormats[] = { 1, 1 };
   PGresult *res;
 
-  res =
-    PQexecPrepared (plugin->dbh, "delete_block", 2,
-                   paramValues, paramLengths, paramFormats, 1);
+  res = GNUNET_PQ_exec_prepared (plugin->dbh,
+                                "delete_block",
+                                params);
   if (GNUNET_OK !=
       GNUNET_POSTGRES_check_result (plugin->dbh,
                                     res,
@@ -255,24 +247,19 @@ delete_old_block (struct Plugin *plugin,
  */
 static int
 namecache_postgres_cache_block (void *cls,
-                                const struct GNUNET_NAMESTORE_Block *block)
+                                const struct GNUNET_GNSRECORD_Block *block)
 {
   struct Plugin *plugin = cls;
   struct GNUNET_HashCode query;
   size_t block_size = ntohl (block->purpose.size) +
     sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
     sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
-  const char *paramValues[] = {
-    (const char *) &query,
-    (const char *) block,
-    (const char *) &block->expiration_time
-  };
-  int paramLengths[] = {
-    sizeof (query),
-    (int) block_size,
-    sizeof (block->expiration_time)
+  struct GNUNET_PQ_QueryParam params[] = { 
+    GNUNET_PQ_query_param_auto_from_type (&query),
+    GNUNET_PQ_query_param_fixed_size (block, block_size),
+    GNUNET_PQ_query_param_absolute_time_nbo (&block->expiration_time),
+    GNUNET_PQ_query_param_end
   };
-  const int paramFormats[] = { 1, 1, 1 };
   PGresult *res;
 
   namecache_postgres_expire_blocks (plugin);
@@ -286,9 +273,9 @@ namecache_postgres_cache_block (void *cls,
   }
   delete_old_block (plugin, &query, block->expiration_time);
 
-  res =
-    PQexecPrepared (plugin->dbh, "cache_block", 3,
-                   paramValues, paramLengths, paramFormats, 1);
+  res = GNUNET_PQ_exec_prepared (plugin->dbh,
+                                "cache_block",
+                                params);
   if (GNUNET_OK !=
       GNUNET_POSTGRES_check_result (plugin->dbh,
                                     res,
@@ -317,22 +304,18 @@ namecache_postgres_lookup_block (void *cls,
                                  GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
 {
   struct Plugin *plugin = cls;
-  const char *paramValues[] = {
-    (const char *) query
-  };
-  int paramLengths[] = {
-    sizeof (*query)
+  struct GNUNET_PQ_QueryParam params[] = { 
+    GNUNET_PQ_query_param_auto_from_type (query),
+    GNUNET_PQ_query_param_end
   };
-  const int paramFormats[] = { 1 };
   PGresult *res;
   unsigned int cnt;
   size_t bsize;
-  const struct GNUNET_NAMESTORE_Block *block;
+  const struct GNUNET_GNSRECORD_Block *block;
 
-  res = PQexecPrepared (plugin->dbh,
-                        "lookup_block", 1,
-                        paramValues, paramLengths, paramFormats,
-                        1);
+  res = GNUNET_PQ_exec_prepared (plugin->dbh,
+                                "lookup_block",
+                                params);
   if (GNUNET_OK !=
       GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK,
                                     "PQexecPrepared",
@@ -353,7 +336,7 @@ namecache_postgres_lookup_block (void *cls,
   GNUNET_assert (1 == cnt);
   GNUNET_assert (1 != PQnfields (res));
   bsize = PQgetlength (res, 0, 0);
-  block = (const struct GNUNET_NAMESTORE_Block *) PQgetvalue (res, 0, 0);
+  block = (const struct GNUNET_GNSRECORD_Block *) PQgetvalue (res, 0, 0);
   if ( (bsize < sizeof (*block)) ||
        (bsize != ntohl (block->purpose.size) +
         sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
@@ -388,8 +371,8 @@ database_shutdown (struct Plugin *plugin)
 /**
  * Entry point for the plugin.
  *
- * @param cls the "struct GNUNET_NAMECACHE_PluginEnvironment*"
- * @return NULL on error, othrewise the plugin context
+ * @param cls the `struct GNUNET_NAMECACHE_PluginEnvironment *`
+ * @return NULL on error, otherwise the plugin context
  */
 void *
 libgnunet_plugin_namecache_postgres_init (void *cls)