gnunet-svn
[Top][All Lists]
Advanced

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

[gnunet] branch master updated: add bool type to libgnunetpq


From: gnunet
Subject: [gnunet] branch master updated: add bool type to libgnunetpq
Date: Sun, 05 Dec 2021 11:58:50 +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 2266d4614 add bool type to libgnunetpq
2266d4614 is described below

commit 2266d461461384be87dd4385e1fc2043c61bc99e
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Sun Dec 5 11:58:45 2021 +0100

    add bool type to libgnunetpq
---
 src/include/gnunet_pq_lib.h |  22 +++++++
 src/pq/pq_query_helper.c    |  47 ++++++++++-----
 src/pq/pq_result_helper.c   | 139 ++++++++++++++++++++++++++++++++------------
 3 files changed, 156 insertions(+), 52 deletions(-)

diff --git a/src/include/gnunet_pq_lib.h b/src/include/gnunet_pq_lib.h
index 6a374f7e2..05d373f88 100644
--- a/src/include/gnunet_pq_lib.h
+++ b/src/include/gnunet_pq_lib.h
@@ -132,6 +132,16 @@ struct GNUNET_PQ_QueryParam
 GNUNET_PQ_query_param_string (const char *ptr);
 
 
+/**
+ * Pass a boolean into a query.
+ *
+ * @param b boolean bit to pass
+ * @return query parameter to use
+ */
+struct GNUNET_PQ_QueryParam
+GNUNET_PQ_query_param_bool (bool b);
+
+
 /**
  * Generate fixed-size query parameter with size determined
  * by variable type.
@@ -413,6 +423,18 @@ GNUNET_PQ_result_spec_string (const char *name,
                               char **dst);
 
 
+/**
+ * boolean expected.
+ *
+ * @param name name of the field in the table
+ * @param[out] dst where to store the result
+ * @return array entry for the result specification to use
+ */
+struct GNUNET_PQ_ResultSpec
+GNUNET_PQ_result_spec_bool (const char *name,
+                            bool *dst);
+
+
 /**
  * RSA public key expected.
  *
diff --git a/src/pq/pq_query_helper.c b/src/pq/pq_query_helper.c
index cee84d203..78c324512 100644
--- a/src/pq/pq_query_helper.c
+++ b/src/pq/pq_query_helper.c
@@ -61,7 +61,7 @@ qconv_null (void *cls,
     return -1;
   param_values[0] = NULL;
   param_lengths[0] = 0;
-  param_formats[0] = 1; 
+  param_formats[0] = 1;
   return 0;
 }
 
@@ -134,6 +134,17 @@ GNUNET_PQ_query_param_string (const char *ptr)
 }
 
 
+struct GNUNET_PQ_QueryParam
+GNUNET_PQ_query_param_bool (bool b)
+{
+  static uint8_t bt = 1;
+  static uint8_t bf = 0;
+
+  return GNUNET_PQ_query_param_fixed_size (b ? &bt : &bf,
+                                           sizeof (uint8_t));
+}
+
+
 /**
  * Function called to convert input argument into SQL parameters.
  *
@@ -180,8 +191,9 @@ qconv_uint16 (void *cls,
 struct GNUNET_PQ_QueryParam
 GNUNET_PQ_query_param_uint16 (const uint16_t *x)
 {
-  struct GNUNET_PQ_QueryParam res =
-  { &qconv_uint16, NULL, x, sizeof(*x), 1 };
+  struct GNUNET_PQ_QueryParam res = {
+    &qconv_uint16, NULL, x, sizeof(*x), 1
+  };
 
   return res;
 }
@@ -233,8 +245,9 @@ qconv_uint32 (void *cls,
 struct GNUNET_PQ_QueryParam
 GNUNET_PQ_query_param_uint32 (const uint32_t *x)
 {
-  struct GNUNET_PQ_QueryParam res =
-  { &qconv_uint32, NULL, x, sizeof(*x), 1 };
+  struct GNUNET_PQ_QueryParam res = {
+    &qconv_uint32, NULL, x, sizeof(*x), 1
+  };
 
   return res;
 }
@@ -286,8 +299,9 @@ qconv_uint64 (void *cls,
 struct GNUNET_PQ_QueryParam
 GNUNET_PQ_query_param_uint64 (const uint64_t *x)
 {
-  struct GNUNET_PQ_QueryParam res =
-  { &qconv_uint64, NULL, x, sizeof(*x), 1 };
+  struct GNUNET_PQ_QueryParam res = {
+    &qconv_uint64, NULL, x, sizeof(*x), 1
+  };
 
   return res;
 }
@@ -339,8 +353,9 @@ struct GNUNET_PQ_QueryParam
 GNUNET_PQ_query_param_rsa_public_key (const struct
                                       GNUNET_CRYPTO_RsaPublicKey *x)
 {
-  struct GNUNET_PQ_QueryParam res =
-  { &qconv_rsa_public_key, NULL, (x), 0, 1 };
+  struct GNUNET_PQ_QueryParam res = {
+    &qconv_rsa_public_key, NULL, (x), 0, 1
+  };
 
   return res;
 }
@@ -391,8 +406,9 @@ qconv_rsa_signature (void *cls,
 struct GNUNET_PQ_QueryParam
 GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature 
*x)
 {
-  struct GNUNET_PQ_QueryParam res =
-  { &qconv_rsa_signature, NULL, (x), 0, 1 };
+  struct GNUNET_PQ_QueryParam res = {
+    &qconv_rsa_signature, NULL, (x), 0, 1
+  };
 
   return res;
 }
@@ -446,8 +462,9 @@ qconv_rel_time (void *cls,
 struct GNUNET_PQ_QueryParam
 GNUNET_PQ_query_param_relative_time (const struct GNUNET_TIME_Relative *x)
 {
-  struct GNUNET_PQ_QueryParam res =
-  { &qconv_rel_time, NULL, x, sizeof(*x), 1 };
+  struct GNUNET_PQ_QueryParam res = {
+    &qconv_rel_time, NULL, x, sizeof(*x), 1
+  };
 
   return res;
 }
@@ -510,8 +527,8 @@ GNUNET_PQ_query_param_absolute_time (const struct 
GNUNET_TIME_Absolute *x)
 
 
 struct GNUNET_PQ_QueryParam
-GNUNET_PQ_query_param_absolute_time_nbo (const struct
-                                         GNUNET_TIME_AbsoluteNBO *x)
+GNUNET_PQ_query_param_absolute_time_nbo (
+  const struct GNUNET_TIME_AbsoluteNBO *x)
 {
   return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
 }
diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c
index f264603f4..4057772ec 100644
--- a/src/pq/pq_result_helper.c
+++ b/src/pq/pq_result_helper.c
@@ -496,11 +496,82 @@ struct GNUNET_PQ_ResultSpec
 GNUNET_PQ_result_spec_string (const char *name,
                               char **dst)
 {
-  struct GNUNET_PQ_ResultSpec res =
-  { &extract_string,
-    &clean_string,
-    NULL,
-    (void *) dst, 0, (name), NULL };
+  struct GNUNET_PQ_ResultSpec res = {
+    .conv = &extract_string,
+    .cleaner = &clean_string,
+    .dst = (void *) dst,
+    .fname = (name)
+  };
+
+  return res;
+}
+
+
+/**
+ * Extract data from a Postgres database @a result at row @a row.
+ *
+ * @param cls closure
+ * @param result where to extract data from
+ * @param int 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_bool (void *cls,
+              PGresult *result,
+              int row,
+              const char *fname,
+              size_t *dst_size,
+              void *dst)
+{
+  bool *b = dst;
+  const uint8_t *res;
+  int fnum;
+  size_t len;
+
+  (void) cls;
+  fnum = PQfnumber (result,
+                    fname);
+  if (fnum < 0)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  if (PQgetisnull (result,
+                   row,
+                   fnum))
+    return GNUNET_NO;
+  /* if a field is null, continue but
+   * remember that we now return a different result */
+  len = PQgetlength (result,
+                     row,
+                     fnum);
+  if (sizeof (uint8_t) != len)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  res = (const uint8_t *) PQgetvalue (result,
+                                      row,
+                                      fnum);
+  *b = (0 != *res);
+  return GNUNET_OK;
+}
+
+
+struct GNUNET_PQ_ResultSpec
+GNUNET_PQ_result_spec_bool (const char *name,
+                            bool *dst)
+{
+  struct GNUNET_PQ_ResultSpec res = {
+    .conv = &extract_bool,
+    .dst = (void *) dst,
+    .fname = name
+  };
 
   return res;
 }
@@ -573,13 +644,10 @@ GNUNET_PQ_result_spec_relative_time (const char *name,
                                      struct GNUNET_TIME_Relative *rt)
 {
   struct GNUNET_PQ_ResultSpec res = {
-    &extract_rel_time,
-    NULL,
-    NULL,
-    (void *) rt,
-    sizeof(*rt),
-    name,
-    NULL
+    .conv = &extract_rel_time,
+    .dst = (void *) rt,
+    .dst_size = sizeof(*rt),
+    .fname = name
   };
 
   return res;
@@ -652,11 +720,12 @@ struct GNUNET_PQ_ResultSpec
 GNUNET_PQ_result_spec_absolute_time (const char *name,
                                      struct GNUNET_TIME_Absolute *at)
 {
-  struct GNUNET_PQ_ResultSpec res =
-  { &extract_abs_time,
-    NULL,
-    NULL,
-    (void *) at, sizeof(*at), (name), NULL };
+  struct GNUNET_PQ_ResultSpec res = {
+    .conv = &extract_abs_time,
+    .dst = (void *) at,
+    .dst_size = sizeof(*at),
+    .fname = name
+  };
 
   return res;
 }
@@ -667,7 +736,8 @@ GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
                                          struct GNUNET_TIME_AbsoluteNBO *at)
 {
   struct GNUNET_PQ_ResultSpec res =
-    GNUNET_PQ_result_spec_auto_from_type (name, &at->abs_value_us__);
+    GNUNET_PQ_result_spec_auto_from_type (name,
+                                          &at->abs_value_us__);
 
   return res;
 }
@@ -736,11 +806,12 @@ struct GNUNET_PQ_ResultSpec
 GNUNET_PQ_result_spec_uint16 (const char *name,
                               uint16_t *u16)
 {
-  struct GNUNET_PQ_ResultSpec res =
-  { &extract_uint16,
-    NULL,
-    NULL,
-    (void *) u16, sizeof(*u16), (name), NULL };
+  struct GNUNET_PQ_ResultSpec res = {
+    .conv = &extract_uint16,
+    .dst = (void *) u16,
+    .dst_size = sizeof(*u16),
+    .fname = name
+  };
 
   return res;
 }
@@ -810,13 +881,10 @@ GNUNET_PQ_result_spec_uint32 (const char *name,
                               uint32_t *u32)
 {
   struct GNUNET_PQ_ResultSpec res = {
-    &extract_uint32,
-    NULL,
-    NULL,
-    (void *) u32,
-    sizeof(*u32),
-    (name),
-    NULL
+    .conv = &extract_uint32,
+    .dst = (void *) u32,
+    .dst_size = sizeof(*u32),
+    .fname = name
   };
 
   return res;
@@ -891,13 +959,10 @@ GNUNET_PQ_result_spec_uint64 (const char *name,
                               uint64_t *u64)
 {
   struct GNUNET_PQ_ResultSpec res = {
-    &extract_uint64,
-    NULL,
-    NULL,
-    (void *) u64,
-    sizeof(*u64),
-    (name),
-    NULL
+    .conv = &extract_uint64,
+    .dst = (void *) u64,
+    .dst_size = sizeof(*u64),
+    .fname = name
   };
 
   return res;

-- 
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]