gnunet-svn
[Top][All Lists]
Advanced

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

[gnunet] branch master updated: PQ: add int64 data type support


From: gnunet
Subject: [gnunet] branch master updated: PQ: add int64 data type support
Date: Fri, 08 Mar 2024 22:19:21 +0100

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository gnunet.

The following commit(s) were added to refs/heads/master by this push:
     new 6c81dc288 PQ: add int64 data type support
6c81dc288 is described below

commit 6c81dc288274027abcd8903764be401e3e43339c
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Fri Mar 8 22:17:45 2024 +0100

    PQ: add int64 data type support
---
 contrib/handbook                  |   2 +-
 src/include/gnunet_pq_lib.h       |  25 +++++++++-
 src/lib/pq/pq_query_helper.c      |  57 ++++++++++++++++++++++
 src/lib/pq/pq_result_helper.c     | 100 +++++++++++++++++++++++++++++++++++---
 src/lib/util/gnunet_error_codes.c |   1 -
 5 files changed, 174 insertions(+), 11 deletions(-)

diff --git a/contrib/handbook b/contrib/handbook
index f9368db2b..f88a38942 160000
--- a/contrib/handbook
+++ b/contrib/handbook
@@ -1 +1 @@
-Subproject commit f9368db2bf8ce9eb6a9e951891142309c9c898d4
+Subproject commit f88a389426a665df222486b0563e5a91e77a7045
diff --git a/src/include/gnunet_pq_lib.h b/src/include/gnunet_pq_lib.h
index 3d9c12cb2..b5d39ed41 100644
--- a/src/include/gnunet_pq_lib.h
+++ b/src/include/gnunet_pq_lib.h
@@ -577,7 +577,7 @@ GNUNET_PQ_query_param_uint32 (const uint32_t *x);
 
 
 /**
- * Generate query parameter for an uint16_t in host byte order.
+ * Generate query parameter for an uint64_t in host byte order.
  *
  * @param x pointer to the query parameter to pass
  * @return query parameter to use
@@ -586,6 +586,16 @@ struct GNUNET_PQ_QueryParam
 GNUNET_PQ_query_param_uint64 (const uint64_t *x);
 
 
+/**
+ * Generate query parameter for an int64_t in host byte order.
+ *
+ * @param x pointer to the query parameter to pass
+ * @return query parameter to use
+ */
+struct GNUNET_PQ_QueryParam
+GNUNET_PQ_query_param_int64 (const int64_t *x);
+
+
 /* ************************* pq_result_helper.c functions 
************************ */
 
 
@@ -895,6 +905,19 @@ struct GNUNET_PQ_ResultSpec
 GNUNET_PQ_result_spec_uint64 (const char *name,
                               uint64_t *u64);
 
+
+/**
+ * int64_t expected.
+ *
+ * @param name name of the field in the table
+ * @param[out] i64 where to store the result
+ * @return array entry for the result specification to use
+ */
+struct GNUNET_PQ_ResultSpec
+GNUNET_PQ_result_spec_int64 (const char *name,
+                             int64_t *i64);
+
+
 /**
  * array of bool expected.
  *
diff --git a/src/lib/pq/pq_query_helper.c b/src/lib/pq/pq_query_helper.c
index 1e2f7a965..211c35076 100644
--- a/src/lib/pq/pq_query_helper.c
+++ b/src/lib/pq/pq_query_helper.c
@@ -323,6 +323,63 @@ GNUNET_PQ_query_param_uint64 (const uint64_t *x)
 }
 
 
+/**
+ * Function called to convert input argument into SQL parameters.
+ *
+ * @param cls closure
+ * @param data pointer to input argument
+ * @param data_len number of bytes in @a data (if applicable)
+ * @param[out] param_values SQL data to set
+ * @param[out] param_lengths SQL length data to set
+ * @param[out] param_formats SQL format data to set
+ * @param param_length number of entries available in the @a param_values, @a 
param_lengths and @a param_formats arrays
+ * @param[out] scratch buffer for dynamic allocations (to be done via 
#GNUNET_malloc()
+ * @param scratch_length number of entries left in @a scratch
+ * @return -1 on error, number of offsets used in @a scratch otherwise
+ */
+static int
+qconv_int64 (void *cls,
+             const void *data,
+             size_t data_len,
+             void *param_values[],
+             int param_lengths[],
+             int param_formats[],
+             unsigned int param_length,
+             void *scratch[],
+             unsigned int scratch_length)
+{
+  const int64_t *u_hbo = data;
+  int64_t *u_nbo;
+
+  (void) scratch;
+  (void) scratch_length;
+  GNUNET_break (NULL == cls);
+  if (1 != param_length)
+    return -1;
+  u_nbo = GNUNET_new (int64_t);
+  scratch[0] = u_nbo;
+  *u_nbo = GNUNET_htonll (*u_hbo);
+  param_values[0] = (void *) u_nbo;
+  param_lengths[0] = sizeof(int64_t);
+  param_formats[0] = 1;
+  return 1;
+}
+
+
+struct GNUNET_PQ_QueryParam
+GNUNET_PQ_query_param_int64 (const int64_t *x)
+{
+  struct GNUNET_PQ_QueryParam res = {
+    .conv = &qconv_int64,
+    .data = x,
+    .size = sizeof(*x),
+    .num_params = 1
+  };
+
+  return res;
+}
+
+
 /**
  * Function called to convert input argument into SQL parameters.
  *
diff --git a/src/lib/pq/pq_result_helper.c b/src/lib/pq/pq_result_helper.c
index f230826cb..6210746bb 100644
--- a/src/lib/pq/pq_result_helper.c
+++ b/src/lib/pq/pq_result_helper.c
@@ -1,6 +1,6 @@
 /*
    This file is part of GNUnet
-   Copyright (C) 2014, 2015, 2016, 2020 GNUnet e.V.
+   Copyright (C) 2014-2024 GNUnet e.V.
 
    GNUnet is free software: you can redistribute it and/or modify it
    under the terms of the GNU Affero General Public License as published
@@ -1121,6 +1121,90 @@ GNUNET_PQ_result_spec_uint64 (const char *name,
 }
 
 
+/**
+ * Extract data from a Postgres database @a result at row @a row.
+ *
+ * @param cls closure
+ * @param result where to extract data from
+ * @param row row to extract data from
+ * @param fname name (or prefix) of the fields to extract from
+ * @param[in,out] dst_size where to store size of result, may be NULL
+ * @param[out] dst where to store the result
+ * @return
+ *   #GNUNET_YES if all results could be extracted
+ *   #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
+ */
+static enum GNUNET_GenericReturnValue
+extract_int64 (void *cls,
+               PGresult *result,
+               int row,
+               const char *fname,
+               size_t *dst_size,
+               void *dst)
+{
+  int64_t *udst = dst;
+  const int64_t *res;
+  int fnum;
+
+  (void) cls;
+  fnum = PQfnumber (result,
+                    fname);
+  if (fnum < 0)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Field %s missing in result\n",
+                fname);
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  if (PQgetisnull (result,
+                   row,
+                   fnum))
+    return GNUNET_NO;
+
+  GNUNET_assert (NULL != dst);
+  if (sizeof(int64_t) != *dst_size)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  if (sizeof(int64_t) !=
+      PQgetlength (result,
+                   row,
+                   fnum))
+  {
+    GNUNET_break (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Got length %u for field `%s'\n",
+                PQgetlength (result,
+                             row,
+                             fnum),
+                fname);
+    return GNUNET_SYSERR;
+  }
+  res = (int64_t *) PQgetvalue (result,
+                                row,
+                                fnum);
+  *udst = GNUNET_ntohll (*res);
+  return GNUNET_OK;
+}
+
+
+struct GNUNET_PQ_ResultSpec
+GNUNET_PQ_result_spec_int64 (const char *name,
+                             int64_t *i64)
+{
+  struct GNUNET_PQ_ResultSpec res = {
+    .conv = &extract_int64,
+    .dst = (void *) i64,
+    .dst_size = sizeof(*i64),
+    .fname = name
+  };
+
+  return res;
+}
+
+
 /**
  * Closure for the array result specifications.  Contains type information
  * for the generic parser extract_array_generic and out-pointers for the 
results.
@@ -1180,13 +1264,13 @@ extract_array_generic (
   *((void **) dst) = NULL;
 
   #define FAIL_IF(cond) \
-          do { \
-            if ((cond)) \
-            { \
-              GNUNET_break (! (cond)); \
-              goto FAIL; \
-            } \
-          } while (0)
+  do { \
+    if ((cond)) \
+    { \
+      GNUNET_break (! (cond)); \
+      goto FAIL; \
+    } \
+  } while (0)
 
   col_num = PQfnumber (result, fname);
   FAIL_IF (0 > col_num);
diff --git a/src/lib/util/gnunet_error_codes.c 
b/src/lib/util/gnunet_error_codes.c
index c286f2e52..11ce2d0c8 100644
--- a/src/lib/util/gnunet_error_codes.c
+++ b/src/lib/util/gnunet_error_codes.c
@@ -17,7 +17,6 @@
 
      SPDX-License-Identifier: AGPL3.0-or-later
  */
-#include "platform.h"
 #include "gnunet_error_codes.h"
 #include <stddef.h>
 #include <microhttpd.h>

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

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