avoid failing hard if 'gnunetcheck' db does not exist
[oweals/gnunet.git] / src / pq / pq_result_helper.c
index 3805b8a8d900a5c8753491471dcce9291e4b4280..336ca8c4253556f8de7a2389ab8a72f4eb76724a 100644 (file)
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Affero General Public License for more details.
+
   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 /**
  * @file pq/pq_result_helper.c
@@ -38,6 +40,7 @@ clean_varsize_blob (void *cls,
 {
   void **dst = rd;
 
+  (void) cls;
   if (NULL != *dst)
   {
     GNUNET_free (*dst);
@@ -72,6 +75,7 @@ extract_varsize_blob (void *cls,
   void *idst;
   int fnum;
 
+  (void) cls;
   *dst_size = 0;
   *((void **) dst) = NULL;
 
@@ -154,6 +158,7 @@ extract_fixed_blob (void *cls,
   const char *res;
   int fnum;
 
+  (void) cls;
   fnum = PQfnumber (result,
                    fname);
   if (fnum < 0)
@@ -237,6 +242,7 @@ extract_rsa_public_key (void *cls,
   const char *res;
   int fnum;
 
+  (void) cls;
   *pk = NULL;
   fnum = PQfnumber (result,
                    fname);
@@ -284,6 +290,7 @@ clean_rsa_public_key (void *cls,
 {
   struct GNUNET_CRYPTO_RsaPublicKey **pk = rd;
 
+  (void) cls;
   if (NULL != *pk)
   {
     GNUNET_CRYPTO_rsa_public_key_free (*pk);
@@ -338,6 +345,7 @@ extract_rsa_signature (void *cls,
   const char *res;
   int fnum;
 
+  (void) cls;
   *sig = NULL;
   fnum = PQfnumber (result,
                    fname);
@@ -385,6 +393,7 @@ clean_rsa_signature (void *cls,
 {
   struct GNUNET_CRYPTO_RsaSignature **sig = rd;
 
+  (void) cls;
   if (NULL != *sig)
   {
     GNUNET_CRYPTO_rsa_signature_free (*sig);
@@ -439,6 +448,7 @@ extract_string (void *cls,
   const char *res;
   int fnum;
 
+  (void) cls;
   *str = NULL;
   fnum = PQfnumber (result,
                    fname);
@@ -486,6 +496,7 @@ clean_string (void *cls,
 {
   char **str = rd;
 
+  (void) cls;
   if (NULL != *str)
   {
     GNUNET_free (*str);
@@ -514,6 +525,71 @@ GNUNET_PQ_result_spec_string (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 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 int
+extract_abs_time (void *cls,
+                 PGresult *result,
+                 int row,
+                 const char *fname,
+                 size_t *dst_size,
+                 void *dst)
+{
+  struct GNUNET_TIME_Absolute *udst = dst;
+  const int64_t *res;
+  int fnum;
+
+  (void) cls;
+  fnum = PQfnumber (result,
+                   fname);
+  if (fnum < 0)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  if (PQgetisnull (result,
+                  row,
+                  fnum))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  GNUNET_assert (NULL != dst);
+  if (sizeof (struct GNUNET_TIME_Absolute) != *dst_size)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  if (sizeof (int64_t) !=
+      PQgetlength (result,
+                   row,
+                   fnum))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  res = (int64_t *) PQgetvalue (result,
+                               row,
+                               fnum);
+  if (INT64_MAX == *res)
+    *udst = GNUNET_TIME_UNIT_FOREVER_ABS;
+  else
+    udst->abs_value_us = GNUNET_ntohll ((uint64_t) *res);
+  return GNUNET_OK;
+}
+
+
 /**
  * Absolute time expected.
  *
@@ -525,8 +601,12 @@ struct GNUNET_PQ_ResultSpec
 GNUNET_PQ_result_spec_absolute_time (const char *name,
                                     struct GNUNET_TIME_Absolute *at)
 {
-  return GNUNET_PQ_result_spec_uint64 (name,
-                                      &at->abs_value_us);
+  struct GNUNET_PQ_ResultSpec res =
+    { &extract_abs_time,
+      NULL,
+      NULL,
+      (void *) at, sizeof (*at), (name), NULL };
+  return res;
 }
 
 
@@ -572,6 +652,7 @@ extract_uint16 (void *cls,
   const uint16_t *res;
   int fnum;
 
+  (void) cls;
   fnum = PQfnumber (result,
                    fname);
   if (fnum < 0)
@@ -653,6 +734,7 @@ extract_uint32 (void *cls,
   const uint32_t *res;
   int fnum;
 
+  (void) cls;
   fnum = PQfnumber (result,
                    fname);
   if (fnum < 0)
@@ -734,6 +816,7 @@ extract_uint64 (void *cls,
   const uint64_t *res;
   int fnum;
 
+  (void) cls;
   fnum = PQfnumber (result,
                    fname);
   if (fnum < 0)