LCOV - code coverage report
Current view: top level - pq - pq_query_helper.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 70 73 95.9 %
Date: 2019-07-18 06:05:54 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014, 2015, 2016 GNUnet e.V.
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file pq/pq_query_helper.c
      18             :  * @brief helper functions for Taler-specific libpq (PostGres) interactions
      19             :  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
      20             :  * @author Florian Dold
      21             :  * @author Christian Grothoff
      22             :  */
      23             : #include "platform.h"
      24             : #include <gnunet/gnunet_util_lib.h>
      25             : #include <gnunet/gnunet_pq_lib.h>
      26             : #include "taler_pq_lib.h"
      27             : 
      28             : 
      29             : /**
      30             :  * Function called to convert input argument into SQL parameters.
      31             :  *
      32             :  * @param cls closure
      33             :  * @param data pointer to input argument, here a `struct TALER_AmountNBO`
      34             :  * @param data_len number of bytes in @a data (if applicable)
      35             :  * @param[out] param_values SQL data to set
      36             :  * @param[out] param_lengths SQL length data to set
      37             :  * @param[out] param_formats SQL format data to set
      38             :  * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
      39             :  * @param[out] scratch buffer for dynamic allocations (to be done via GNUNET_malloc()
      40             :  * @param scratch_length number of entries left in @a scratch
      41             :  * @return -1 on error, number of offsets used in @a scratch otherwise
      42             :  */
      43             : static int
      44         928 : qconv_amount_nbo (void *cls,
      45             :                   const void *data,
      46             :                   size_t data_len,
      47             :                   void *param_values[],
      48             :                   int param_lengths[],
      49             :                   int param_formats[],
      50             :                   unsigned int param_length,
      51             :                   void *scratch[],
      52             :                   unsigned int scratch_length)
      53             : {
      54         928 :   const struct TALER_AmountNBO *amount = data;
      55         928 :   unsigned int off = 0;
      56             : 
      57         928 :   GNUNET_assert (3 == param_length);
      58         928 :   param_values[off] = (void *) &amount->value;
      59         928 :   param_lengths[off] = sizeof (amount->value);
      60         928 :   param_formats[off] = 1;
      61         928 :   off++;
      62         928 :   param_values[off] = (void *) &amount->fraction;
      63         928 :   param_lengths[off] = sizeof (amount->fraction);
      64         928 :   param_formats[off] = 1;
      65         928 :   off++;
      66         928 :   param_values[off] = (void *) amount->currency;
      67         928 :   param_lengths[off] = strlen (amount->currency);
      68         928 :   param_formats[off] = 1;
      69         928 :   return 0;
      70             : }
      71             : 
      72             : 
      73             : /**
      74             :  * Generate query parameter for a currency, consisting of the three
      75             :  * components "value", "fraction" and "currency" in this order. The
      76             :  * types must be a 64-bit integer, 32-bit integer and a
      77             :  * #TALER_CURRENCY_LEN-sized BLOB/VARCHAR respectively.
      78             :  *
      79             :  * @param x pointer to the query parameter to pass
      80             :  * @return array entry for the query parameters to use
      81             :  */
      82             : struct GNUNET_PQ_QueryParam
      83         581 : TALER_PQ_query_param_amount_nbo (const struct TALER_AmountNBO *x)
      84             : {
      85         581 :   struct GNUNET_PQ_QueryParam res =
      86             :     { &qconv_amount_nbo, NULL, x, sizeof (*x), 3 };
      87         581 :   return res;
      88             : }
      89             : 
      90             : 
      91             : /**
      92             :  * Function called to convert input argument into SQL parameters.
      93             :  *
      94             :  * @param cls closure
      95             :  * @param data pointer to input argument, here a `struct TALER_Amount`
      96             :  * @param data_len number of bytes in @a data (if applicable)
      97             :  * @param[out] param_values SQL data to set
      98             :  * @param[out] param_lengths SQL length data to set
      99             :  * @param[out] param_formats SQL format data to set
     100             :  * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
     101             :  * @param[out] scratch buffer for dynamic allocations (to be done via GNUNET_malloc()
     102             :  * @param scratch_length number of entries left in @a scratch
     103             :  * @return -1 on error, number of offsets used in @a scratch otherwise
     104             :  */
     105             : static int
     106         347 : qconv_amount (void *cls,
     107             :               const void *data,
     108             :               size_t data_len,
     109             :               void *param_values[],
     110             :               int param_lengths[],
     111             :               int param_formats[],
     112             :               unsigned int param_length,
     113             :               void *scratch[],
     114             :               unsigned int scratch_length)
     115             : {
     116         347 :   const struct TALER_Amount *amount_hbo = data;
     117             :   struct TALER_AmountNBO *amount;
     118             : 
     119         347 :   amount = GNUNET_new (struct TALER_AmountNBO);
     120         347 :   scratch[0] = amount;
     121         347 :   TALER_amount_hton (amount,
     122             :                      amount_hbo);
     123         347 :   qconv_amount_nbo (cls,
     124             :                     amount,
     125             :                     sizeof (struct TALER_AmountNBO),
     126             :                     param_values,
     127             :                     param_lengths,
     128             :                     param_formats,
     129             :                     param_length,
     130             :                     &scratch[1],
     131             :                     scratch_length - 1);
     132         347 :   return 1;
     133             : }
     134             : 
     135             : 
     136             : /**
     137             :  * Generate query parameter for a currency, consisting of the three
     138             :  * components "value", "fraction" and "currency" in this order. The
     139             :  * types must be a 64-bit integer, 32-bit integer and a
     140             :  * #TALER_CURRENCY_LEN-sized BLOB/VARCHAR respectively.
     141             :  *
     142             :  * @param x pointer to the query parameter to pass
     143             :  * @return array entry for the query parameters to use
     144             :  */
     145             : struct GNUNET_PQ_QueryParam
     146         449 : TALER_PQ_query_param_amount (const struct TALER_Amount *x)
     147             : {
     148         449 :   struct GNUNET_PQ_QueryParam res =
     149             :     { &qconv_amount, NULL, x, sizeof (*x), 3 };
     150         449 :   return res;
     151             : }
     152             : 
     153             : 
     154             : /**
     155             :  * Function called to convert input argument into SQL parameters.
     156             :  *
     157             :  * @param cls closure
     158             :  * @param data pointer to input argument, here a `json_t *`
     159             :  * @param data_len number of bytes in @a data (if applicable)
     160             :  * @param[out] param_values SQL data to set
     161             :  * @param[out] param_lengths SQL length data to set
     162             :  * @param[out] param_formats SQL format data to set
     163             :  * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
     164             :  * @param[out] scratch buffer for dynamic allocations (to be done via GNUNET_malloc()
     165             :  * @param scratch_length number of entries left in @a scratch
     166             :  * @return -1 on error, number of offsets used in @a scratch otherwise
     167             :  */
     168             : static int
     169          89 : qconv_json (void *cls,
     170             :             const void *data,
     171             :             size_t data_len,
     172             :             void *param_values[],
     173             :             int param_lengths[],
     174             :             int param_formats[],
     175             :             unsigned int param_length,
     176             :             void *scratch[],
     177             :             unsigned int scratch_length)
     178             : {
     179          89 :   const json_t *json = data;
     180             :   char *str;
     181             : 
     182          89 :   str = json_dumps (json, JSON_COMPACT);
     183          89 :   if (NULL == str)
     184           0 :     return -1;
     185          89 :   scratch[0] = str;
     186          89 :   param_values[0] = (void *) str;
     187          89 :   param_lengths[0] = strlen (str);
     188          89 :   param_formats[0] = 1;
     189          89 :   return 1;
     190             : }
     191             : 
     192             : 
     193             : /**
     194             :  * Generate query parameter for a JSON object (stored as a string
     195             :  * in the DB).
     196             :  *
     197             :  * @param x pointer to the json object to pass
     198             :  */
     199             : struct GNUNET_PQ_QueryParam
     200          89 : TALER_PQ_query_param_json (const json_t *x)
     201             : {
     202          89 :   struct GNUNET_PQ_QueryParam res =
     203             :     { &qconv_json, NULL, x, 0, 1 };
     204          89 :   return res;
     205             : }
     206             : 
     207             : 
     208             : /**
     209             :  * Function called to convert input argument into SQL parameters.
     210             :  *
     211             :  * @param cls closure
     212             :  * @param data pointer to input argument
     213             :  * @param data_len number of bytes in @a data (if applicable)
     214             :  * @param[out] param_values SQL data to set
     215             :  * @param[out] param_lengths SQL length data to set
     216             :  * @param[out] param_formats SQL format data to set
     217             :  * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
     218             :  * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
     219             :  * @param scratch_length number of entries left in @a scratch
     220             :  * @return -1 on error, number of offsets used in @a scratch otherwise
     221             :  */
     222             : static int
     223         623 : qconv_round_time (void *cls,
     224             :                   const void *data,
     225             :                   size_t data_len,
     226             :                   void *param_values[],
     227             :                   int param_lengths[],
     228             :                   int param_formats[],
     229             :                   unsigned int param_length,
     230             :                   void *scratch[],
     231             :                   unsigned int scratch_length)
     232             : {
     233         623 :   const struct GNUNET_TIME_Absolute *at = data;
     234             :   struct GNUNET_TIME_Absolute tmp;
     235             :   struct GNUNET_TIME_AbsoluteNBO *buf;
     236             : 
     237         623 :   GNUNET_break (NULL == cls);
     238         623 :   if (1 != param_length)
     239           0 :     return -1;
     240         623 :   tmp = *at;
     241         623 :   GNUNET_assert (GNUNET_OK ==
     242             :                  GNUNET_TIME_round_abs (&tmp));
     243         623 :   buf = GNUNET_new (struct GNUNET_TIME_AbsoluteNBO);
     244         623 :   *buf = GNUNET_TIME_absolute_hton (tmp);
     245         623 :   scratch[0] = buf;
     246         623 :   param_values[0] = (void *) buf;
     247         623 :   param_lengths[0] = sizeof (struct GNUNET_TIME_AbsoluteNBO);
     248         623 :   param_formats[0] = 1;
     249         623 :   return 1;
     250             : }
     251             : 
     252             : 
     253             : /**
     254             :  * Generate query parameter for an absolute time value.
     255             :  * In contrast to
     256             :  * #GNUNET_PQ_query_param_absolute_time(), this function
     257             :  * will abort (!) if the time given is not rounded!
     258             :  * The database must store a 64-bit integer.
     259             :  *
     260             :  * @param x pointer to the query parameter to pass
     261             :  * @return array entry for the query parameters to use
     262             :  */
     263             : struct GNUNET_PQ_QueryParam
     264         724 : TALER_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
     265             : {
     266         724 :   struct GNUNET_PQ_QueryParam res =
     267             :     { &qconv_round_time, NULL, x, sizeof (*x), 1 };
     268         724 :   return res;
     269             : }
     270             : 
     271             : 
     272             : /**
     273             :  * Function called to convert input argument into SQL parameters.
     274             :  *
     275             :  * @param cls closure
     276             :  * @param data pointer to input argument
     277             :  * @param data_len number of bytes in @a data (if applicable)
     278             :  * @param[out] param_values SQL data to set
     279             :  * @param[out] param_lengths SQL length data to set
     280             :  * @param[out] param_formats SQL format data to set
     281             :  * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
     282             :  * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
     283             :  * @param scratch_length number of entries left in @a scratch
     284             :  * @return -1 on error, number of offsets used in @a scratch otherwise
     285             :  */
     286             : static int
     287         464 : qconv_round_time_abs (void *cls,
     288             :                       const void *data,
     289             :                       size_t data_len,
     290             :                       void *param_values[],
     291             :                       int param_lengths[],
     292             :                       int param_formats[],
     293             :                       unsigned int param_length,
     294             :                       void *scratch[],
     295             :                       unsigned int scratch_length)
     296             : {
     297         464 :   const struct GNUNET_TIME_AbsoluteNBO *at = data;
     298             :   struct GNUNET_TIME_Absolute tmp;
     299             : 
     300         464 :   GNUNET_break (NULL == cls);
     301         464 :   if (1 != param_length)
     302           0 :     return -1;
     303         464 :   tmp = GNUNET_TIME_absolute_ntoh (*at);
     304         464 :   GNUNET_assert (GNUNET_OK ==
     305             :                  GNUNET_TIME_round_abs (&tmp));
     306         464 :   param_values[0] = (void *) at;
     307         464 :   param_lengths[0] = sizeof (struct GNUNET_TIME_AbsoluteNBO);
     308         464 :   param_formats[0] = 1;
     309         464 :   return 0;
     310             : }
     311             : 
     312             : 
     313             : /**
     314             :  * Generate query parameter for an absolute time value.
     315             :  * In contrast to
     316             :  * #GNUNET_PQ_query_param_absolute_time(), this function
     317             :  * will abort (!) if the time given is not rounded!
     318             :  * The database must store a 64-bit integer.
     319             :  *
     320             :  * @param x pointer to the query parameter to pass
     321             :  */
     322             : struct GNUNET_PQ_QueryParam
     323         464 : TALER_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
     324             : {
     325         464 :   struct GNUNET_PQ_QueryParam res =
     326             :     { &qconv_round_time_abs, NULL, x, sizeof (*x), 1 };
     327         464 :   return res;
     328             : }
     329             : 
     330             : 
     331             : /* end of pq/pq_query_helper.c */

Generated by: LCOV version 1.13