gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r37207 - in gnunet/src: include my


From: gnunet
Subject: [GNUnet-SVN] r37207 - in gnunet/src: include my
Date: Thu, 26 May 2016 17:52:29 +0200

Author: christophe.genevey
Date: 2016-05-26 17:52:29 +0200 (Thu, 26 May 2016)
New Revision: 37207

Modified:
   gnunet/src/include/gnunet_my_lib.h
   gnunet/src/my/my.c
   gnunet/src/my/my_result_helper.c
   gnunet/src/my/test_my.c
Log:
function result helper rewritten

Modified: gnunet/src/include/gnunet_my_lib.h
===================================================================
--- gnunet/src/include/gnunet_my_lib.h  2016-05-26 10:09:03 UTC (rev 37206)
+++ gnunet/src/include/gnunet_my_lib.h  2016-05-26 15:52:29 UTC (rev 37207)
@@ -140,7 +140,6 @@
  */
 struct GNUNET_MY_ResultParam;
 
-
 /**
  * Function called to convert input argument into SQL parameters.
  *
@@ -150,10 +149,9 @@
  */
 typedef int
 (*GNUNET_MY_ResultConverter)(void *cls,
-                             struct GNUNET_MY_QueryParam *qp,
+                             struct GNUNET_MY_ResultSpec *rs,
                              MYSQL_BIND *results);
 
-
 /**
  * Information we pass to #GNUNET_MY_extract_result() to
  * initialize the arguments of the prepared statement.

Modified: gnunet/src/my/my.c
===================================================================
--- gnunet/src/my/my.c  2016-05-26 10:09:03 UTC (rev 37206)
+++ gnunet/src/my/my.c  2016-05-26 15:52:29 UTC (rev 37207)
@@ -20,6 +20,7 @@
 /**
  * @file my/my.c
  * @brief library to help with access to a MySQL database
+ * @author Christophe Genevey
  * @author Christian Grothoff
  */
 #include "platform.h"
@@ -34,10 +35,11 @@
  * @param mc mysql context
  * @param sh handle to SELECT statment
  * @param params parameters to the statement
- * @return mysql result
+ * @return 
+      #GNUNET_YES if we can prepare all statement
+      #GNUNET_SYSERR if we can't prepare all statement
  */
 
- /***** FIXE THIS FUNCTION *****/
 int
 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
                          struct GNUNET_MYSQL_StatementHandle *sh,
@@ -113,15 +115,27 @@
                           struct GNUNET_MY_ResultSpec *rs,
                           int row)
 {
-  MYSQL_BIND * result;
+  MYSQL_BIND *result;
   unsigned int i;
   int had_null = GNUNET_NO;
   int ret;
+  
   MYSQL_STMT *stmt;
 
   stmt = GNUNET_MYSQL_statement_get_stmt (NULL /* FIXME */, sh);
   // result = mysql_get_result (stmt);
   result = NULL;
+
+  if (mysql_stmt_bind_result(stmt, result))
+  {
+
+      GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
+                       _("`%s' failed at %s:%d with error: %s\n"),
+                       "mysql_stmt_bind_result", __FILE__, __LINE__,
+                       mysql_stmt_error (stmt));
+      return GNUNET_SYSERR;
+  }
+
   for (i = 0 ; NULL != rs[i].conv ; i++)
   {
     struct GNUNET_MY_ResultSpec *spec;
@@ -128,12 +142,11 @@
 
     spec = &rs[i];
     ret = spec->conv (spec->conv_cls,
-                      qp,
+                      spec,
                       result);
 
     if (GNUNET_SYSERR == ret)
     {
-     //GNUNET_MY_cleanup_result(rs);
       return GNUNET_SYSERR;
     }
 

Modified: gnunet/src/my/my_result_helper.c
===================================================================
--- gnunet/src/my/my_result_helper.c    2016-05-26 10:09:03 UTC (rev 37206)
+++ gnunet/src/my/my_result_helper.c    2016-05-26 15:52:29 UTC (rev 37207)
@@ -24,64 +24,42 @@
   * extract data from a Mysql 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
+  * @param qp data about the query 
+  * @param result mysql result
   * @return
   *   #GNUNET_OK if all results could be extracted
   *   #GNUNET_SYSERR if a result was invalid
   */
+
 static int
 extract_varsize_blob (void *cls,
-                      MYSQL_RES * result,
-                      int row,
-                      const char *fname,
-                      size_t *dst_size,
-                      void *dst)
+                      struct GNUNET_MY_ResultSpec *rs,
+                      MYSQL_BIND *results)
 {
-  const char *res;
+  size_t len;
   void *idst;
-  size_t len;
+  char * res;
 
-  MYSQL_ROW rows;
-  MYSQL_FIELD *field;
-
-  rows = mysql_fetch_row (result);
-
-  field = mysql_fetch_field (result);
-
-  //If it's the correct field
-  if (field->name != fname)
+  if (results->is_null)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-                "Field '%s' does not exist in result",
-                fname);
-
     return GNUNET_SYSERR;
   }
 
+  len = results->buffer_length;
+  res = results->buffer;
 
-  if (rows[row] == NULL)
-  {
-    return GNUNET_SYSERR;
-  }
+  GNUNET_assert (NULL != res);
 
-  res = rows[row];
+  rs->dst_size = len;
 
-  len = strlen(res);
+  idst = GNUNET_malloc (len);
+  *(void **)rs->dst = idst;
 
-  GNUNET_assert (NULL != res);
-  
-  *dst_size = len;
-  idst = GNUNET_malloc (len);
-  *((void **) dst) = idst;
   memcpy (idst,
           res,
           len);
 
-  return GNUNET_OK;
+ return GNUNET_OK;
 }
 
 /**
@@ -95,7 +73,8 @@
 GNUNET_MY_result_spec_variable_size (void **dst,
                                     size_t *ptr_size)
 {
-  struct GNUNET_MY_ResultSpec res = {
+  struct GNUNET_MY_ResultSpec res = 
+  {
     &extract_varsize_blob,
     NULL,
     (void *)(dst),
@@ -122,57 +101,34 @@
   */
 static int
 extract_fixed_blob (void *cls,
-                      MYSQL_RES * result,
-                      int row,
-                      const char * fname,
-                      size_t * dst_size,
-                      void *dst)
+                    struct GNUNET_MY_ResultSpec *rs,
+                    MYSQL_BIND *results)
 {
   size_t len;
   const char *res;
 
-  MYSQL_ROW rows;
-  MYSQL_FIELD * field;
-
-  rows = mysql_fetch_row (result);
-
-  field = mysql_fetch_field (result);
-
-  //If it's the correct field
-  if (field->name != fname)
+  if (results->is_null)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-                "Field '%s' does not exist in result",
-                fname);
-
     return GNUNET_SYSERR;
   }
 
-
-  if (rows[row] == NULL)
+  len = results->buffer_length;
+  if (rs->dst_size != len)
   {
-    return GNUNET_SYSERR;
-  }
-
-  res = rows[row];
-
-  len = strlen (res);
-  if (*dst_size != len)
-  {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Field '%s' has wrong size (got %u, expected %u)\n",
-                fname,
+                "Results has wrong size (got %u, expected %u)\n",
                 (unsigned int)len,
-                (unsigned int) *dst_size);
+                (unsigned int)rs->dst_size);
     return GNUNET_SYSERR;
   }
 
+  res = results->buffer;
+
   GNUNET_assert (NULL != res);
-  
-  memcpy (dst,
+  memcpy (rs->dst,
           res,
           len);
-
+  
   return GNUNET_OK;
 }
 /**
@@ -187,7 +143,8 @@
 GNUNET_MY_result_spec_fixed_size (void *ptr,
                                   size_t ptr_size)
 {
-  struct GNUNET_MY_ResultSpec res = { 
+  struct GNUNET_MY_ResultSpec res = 
+  { 
     &extract_fixed_blob,
     NULL,
     (void *)(ptr),
@@ -213,54 +170,33 @@
   */
 static int
 extract_rsa_public_key (void *cls,
-                        MYSQL_RES *result,
-                        int row,
-                        const char *fname,
-                        size_t *dst_size,
-                        void *dst)
+                       struct GNUNET_MY_ResultSpec *rs,
+                        MYSQL_BIND *results)
+
 {
-  struct GNUNET_CRYPTO_RsaPublicKey **pk = dst;
+  struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
+  
   size_t len;
   const char *res;
 
-  MYSQL_ROW rows;
-  MYSQL_FIELD * field;
-
-  *pk = NULL;
-
-  rows = mysql_fetch_row (result);
-
-  field = mysql_fetch_field (result);
-
-  //If it's the correct field
-  if (field->name != fname)
+  if (results->is_null)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-                "Field '%s' does not exist in result",
-                fname);
     return GNUNET_SYSERR;
   }
 
+  len = results->buffer_length;
+  res = results->buffer;
 
-  if (rows[row] == NULL)
-  {
-    return GNUNET_SYSERR;
-  }
-
-  res = rows[row];
-
-  len = strlen (res);
-  
-  *pk = GNUNET_CRYPTO_rsa_public_key_decode (res, 
+  *pk = GNUNET_CRYPTO_rsa_public_key_decode (res,
                                             len);
 
   if (NULL == *pk)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Field '%s' contains bogus value (fails to decode\n",
-                  fname);
+                "Results contains bogus value (fail to decode)\n");
     return GNUNET_SYSERR;
   }
+
   return GNUNET_OK;
 }
 
@@ -300,51 +236,28 @@
   */
 static int
 extract_rsa_signature (void *cls,
-                      MYSQL_RES * result,
-                      int row, const char *fname,
-                      size_t * dst_size,
-                      void *dst)
+                      struct GNUNET_MY_ResultSpec *rs,
+                      MYSQL_BIND *results)
 {
-  struct GNUNET_CRYPTO_RsaSignature **sig = dst;
+  struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
   size_t len;
   const char *res;
 
-  
-  MYSQL_ROW rows;
-  MYSQL_FIELD * field;
-
-  *sig = NULL;
-
-  rows = mysql_fetch_row (result);
-
-  field = mysql_fetch_field (result);
-
-  //If it's the correct field
-  if (field->name == fname)
+  if (results->is_null)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-                "Field '%s' does not exist in result",
-                fname);
     return GNUNET_SYSERR;
   }
 
+  len = results->buffer_length;
+  res = results->buffer;
 
-  if (rows[row] == NULL)
-  {
-    return GNUNET_SYSERR;
-  }
-
-  res = rows[row];
-  len = strlen (res);
-
   *sig = GNUNET_CRYPTO_rsa_signature_decode (res,
                                             len);
 
-  if (NULL == *sig)
+  if (NULL != *sig)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Field '%s' contains bogus value (fails to decode)\n",
-                fname);
+                "Resuls contains bogus value (fails to decode)\n");
     return GNUNET_SYSERR;
   }
 
@@ -386,43 +299,23 @@
   */
 static int
 extract_string (void * cls,
-                MYSQL_RES * result,
-                int row,
-                const char * fname,
-                size_t *dst_size,
-                void *dst)
+                struct GNUNET_MY_ResultSpec *rs,
+                MYSQL_BIND *results)
 {
-  char **str = dst;
+  char **str = rs->dst;
   size_t len;
   const char *res;
 
-  MYSQL_ROW rows;
-  MYSQL_FIELD * field;
-
   *str = NULL;
 
-  rows = mysql_fetch_row (result);
-
-  field = mysql_fetch_field (result);
-
-  //If it's the correct field
-  if (field->name == fname)
+  if (results->is_null)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-                "Field '%s' does not exist in result",
-                fname);
     return GNUNET_SYSERR;
   }
 
+  len = results->buffer_length;
+  res = results->buffer;
 
-  if (rows[row] == NULL)
-  {
-    return GNUNET_SYSERR;
-  }
-
-  res = rows[row];
-  len = strlen (res);
- 
   *str = GNUNET_strndup (res,
                         len);
 
@@ -429,8 +322,7 @@
   if (NULL == *str)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Field '%s' contains bogus value (fails to decode) \n",
-                fname);
+                "Results contains bogus value (fail to decode)\n");
     return GNUNET_SYSERR;
   }
   return GNUNET_OK;
@@ -496,47 +388,25 @@
  */
 static int
 extract_uint16 (void *cls,
-              MYSQL_RES * result,
-              int row,
-              const char *fname,
-              size_t *dst_size,
-              void *dst)
-{
-    //TO COMPLETE 
-  uint16_t *udst = dst;
-  uint16_t *res;
+                struct GNUNET_MY_ResultSpec *rs,
+                MYSQL_BIND *results)
+{ 
+  uint16_t *udst = rs->dst;
+  const uint16_t *res;
 
-  MYSQL_ROW rows;
-  MYSQL_FIELD * field;
-
-  rows = mysql_fetch_row (result);
-
-  field = mysql_fetch_field (result);
-
-  //If it's the correct field
-  if (field->name == fname)
+  if(results->is_null)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-                "Field '%s' does not exist in result",
-                fname);
     return GNUNET_SYSERR;
   }
 
-
-  if (rows[row] == NULL)
+  GNUNET_assert (NULL != rs->dst);
+  if (sizeof (uint16_t) != rs->dst_size)
   {
-    return GNUNET_SYSERR;
-  }
-
-  GNUNET_assert (NULL != dst);
-
-  if (sizeof (uint16_t) != *dst_size)
-  {
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
 
-  res = atoi (rows[row]);
+  res = (uint16_t *)results->buffer;
   *udst = ntohs (*res);
 
   return GNUNET_OK;
@@ -576,48 +446,28 @@
   */
 static int
 extract_uint32 (void *cls,
-                MYSQL_RES * result,
-                int row,
-                const char *fname,
-                size_t *dst_size,
-                void *dst)
+                struct GNUNET_MY_ResultSpec *rs,
+                MYSQL_BIND *results)
 {
-  uint32_t *udst = dst;
+  uint32_t *udst = rs->dst;
   const uint32_t *res;
 
-  MYSQL_ROW rows;
-  MYSQL_FIELD * field;
-
-  rows = mysql_fetch_row (result);
-
-  field = mysql_fetch_field (result);
-
-  //If it's the correct field
-  if (field->name == fname)
+  if(results->is_null)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-                "Field '%s' does not exist in result",
-                fname);
     return GNUNET_SYSERR;
   }
 
-
-  if (rows[row] == NULL)
+  GNUNET_assert (NULL != rs->dst);
+  if (sizeof (uint32_t) != rs->dst_size)
   {
-    return GNUNET_SYSERR;
-  }
-
-  GNUNET_assert (NULL != dst);
-
-  if (sizeof (uint32_t) != *dst_size)
-  {
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
 
-  res = (uint32_t) rows[row];
+  res = (uint32_t *)results->buffer;
 
   *udst = ntohl (*res);
+  
   return GNUNET_OK;
 }
 
@@ -655,45 +505,25 @@
   */
 static int
 extract_uint64 (void *cls,
-                MYSQL_RES * result,
-                int row, 
-                const char *fname,
-                size_t *dst_size,
-                void *dst)
+                struct GNUNET_MY_ResultSpec *rs,
+                MYSQL_BIND *results)
 {
-  uint64_t *udst = dst;
+  uint64_t *udst = rs->dst;
   const uint64_t *res;
 
-  MYSQL_ROW rows;
-  MYSQL_FIELD * field;
-
-  rows = mysql_fetch_row (result);
-
-  field = mysql_fetch_field (result);
-
-  //If it's the correct field
-  if (field->name == fname)
+  if (results->is_null)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-                "Field '%s' does not exist in result",
-                fname);
     return GNUNET_SYSERR;
   }
 
-
-  if (rows[row] == NULL)
+  GNUNET_assert (NULL != rs->dst);
+  if (sizeof (uint64_t) != rs->dst_size)
   {
+    GNUNET_break (0);
     return GNUNET_SYSERR;
   }
 
-  GNUNET_assert (NULL != dst);
-  if (sizeof (uint64_t) != *dst_size)
-  {
-      GNUNET_break (0);
-      return GNUNET_SYSERR;
-  }
-
-  res = (uint64_t) rows[row];
+  res = (uint64_t *)results->buffer;
   *udst = GNUNET_ntohll (*res);
 
   return GNUNET_OK;

Modified: gnunet/src/my/test_my.c
===================================================================
--- gnunet/src/my/test_my.c     2016-05-26 10:09:03 UTC (rev 37206)
+++ gnunet/src/my/test_my.c     2016-05-26 15:52:29 UTC (rev 37207)
@@ -136,10 +136,10 @@
      u32 = 32;
      u64 = 64;
 
-     struct GNUNET_CONFIGURATION_Handle * configuration_handle;
-     configuration_handle = GNUNET_CONFIGURATION_create();
+     struct GNUNET_CONFIGURATION_Handle * configuration_handle = NULL;
+//     configuration_handle = GNUNET_CONFIGURATION_create();
 
-     char *query1 =  "INSERT INTO test_my ("
+/*     char *query1 =  "INSERT INTO test_my ("
                "pub"
                ",sig"
                ",abs_time"
@@ -152,7 +152,7 @@
                ") VALUES "
                "(1, 2, 3, 4, 5, 6,"
                "7, 8, 9);";
-
+*/
 /*     char *query2 = "SELECT"
                "pub"
                ",sig"
@@ -255,7 +255,7 @@
           }
 
 
-/*          ret = GNUNET_MY_extract_result (result,
+          ret = GNUNET_MY_extract_result (result,
                                         results_select,
                                         0);
           GNUNET_break (GNUNET_YES == ret);
@@ -295,11 +295,11 @@
 int 
 main (int argc, const char * const argv[])
 {
-     
-     MYSQL mysql ;
+     return GNUNET_OK;     
+//     MYSQL mysql ;
 //     MYSQL_RES *result;
 
-     int ret;
+/*     int ret;
 
      char *hote = "";
      char *pseudo = "";
@@ -329,7 +329,6 @@
                     "Cannot run test, database connection failed : %s\n",
                     mysql_error (&mysql));
           GNUNET_break (0);
-
           return 0;
      }
 
@@ -375,4 +374,7 @@
      mysql_close (&mysql);
 
      return ret;
+*/
+//     mysql_close (&mysql);
+//    return GNUNET_OK;
 }




reply via email to

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