LCOV - code coverage report
Current view: top level - pq - pq_query_helper.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 74 75 98.7 %
Date: 2021-01-22 06:21:36 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014, 2015, 2016 Taler Systems SA
       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        3298 : 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        3298 :   const struct TALER_AmountNBO *amount = data;
      55        3298 :   unsigned int off = 0;
      56             : 
      57             :   (void) cls;
      58             :   (void) scratch;
      59             :   (void) scratch_length;
      60        3298 :   GNUNET_assert (sizeof (struct TALER_AmountNBO) == data_len);
      61        3298 :   GNUNET_assert (2 == param_length);
      62        3298 :   param_values[off] = (void *) &amount->value;
      63        3298 :   param_lengths[off] = sizeof (amount->value);
      64        3298 :   param_formats[off] = 1;
      65        3298 :   off++;
      66        3298 :   param_values[off] = (void *) &amount->fraction;
      67        3298 :   param_lengths[off] = sizeof (amount->fraction);
      68        3298 :   param_formats[off] = 1;
      69        3298 :   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         161 : TALER_PQ_query_param_amount_nbo (const struct TALER_AmountNBO *x)
      84             : {
      85         161 :   struct GNUNET_PQ_QueryParam res =
      86             :   { &qconv_amount_nbo, NULL, x, sizeof (*x), 2 };
      87         161 :   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        3137 : 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        3137 :   const struct TALER_Amount *amount_hbo = data;
     117             :   struct TALER_AmountNBO *amount;
     118             : 
     119             :   (void) cls;
     120             :   (void) scratch;
     121             :   (void) scratch_length;
     122        3137 :   GNUNET_assert (2 == param_length);
     123        3137 :   GNUNET_assert (sizeof (struct TALER_AmountNBO) == data_len);
     124        3137 :   amount = GNUNET_new (struct TALER_AmountNBO);
     125        3137 :   scratch[0] = amount;
     126        3137 :   TALER_amount_hton (amount,
     127             :                      amount_hbo);
     128        3137 :   qconv_amount_nbo (cls,
     129             :                     amount,
     130             :                     sizeof (struct TALER_AmountNBO),
     131             :                     param_values,
     132             :                     param_lengths,
     133             :                     param_formats,
     134             :                     param_length,
     135             :                     &scratch[1],
     136             :                     scratch_length - 1);
     137        3137 :   return 1;
     138             : }
     139             : 
     140             : 
     141             : /**
     142             :  * Generate query parameter for a currency, consisting of the three
     143             :  * components "value", "fraction" and "currency" in this order. The
     144             :  * types must be a 64-bit integer, 32-bit integer and a
     145             :  * #TALER_CURRENCY_LEN-sized BLOB/VARCHAR respectively.
     146             :  *
     147             :  * @param x pointer to the query parameter to pass
     148             :  * @return array entry for the query parameters to use
     149             :  */
     150             : struct GNUNET_PQ_QueryParam
     151        3139 : TALER_PQ_query_param_amount (const struct TALER_Amount *x)
     152             : {
     153        3139 :   struct GNUNET_PQ_QueryParam res =
     154             :   { &qconv_amount, NULL, x, sizeof (*x), 2 };
     155        3139 :   return res;
     156             : }
     157             : 
     158             : 
     159             : /**
     160             :  * Function called to convert input argument into SQL parameters.
     161             :  *
     162             :  * @param cls closure
     163             :  * @param data pointer to input argument, here a `json_t *`
     164             :  * @param data_len number of bytes in @a data (if applicable)
     165             :  * @param[out] param_values SQL data to set
     166             :  * @param[out] param_lengths SQL length data to set
     167             :  * @param[out] param_formats SQL format data to set
     168             :  * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
     169             :  * @param[out] scratch buffer for dynamic allocations (to be done via GNUNET_malloc()
     170             :  * @param scratch_length number of entries left in @a scratch
     171             :  * @return -1 on error, number of offsets used in @a scratch otherwise
     172             :  */
     173             : static int
     174          94 : qconv_json (void *cls,
     175             :             const void *data,
     176             :             size_t data_len,
     177             :             void *param_values[],
     178             :             int param_lengths[],
     179             :             int param_formats[],
     180             :             unsigned int param_length,
     181             :             void *scratch[],
     182             :             unsigned int scratch_length)
     183             : {
     184          94 :   const json_t *json = data;
     185             :   char *str;
     186             : 
     187             :   (void) cls;
     188             :   (void) data_len;
     189          94 :   GNUNET_assert (1 == param_length);
     190          94 :   GNUNET_assert (scratch_length > 0);
     191          94 :   str = json_dumps (json, JSON_COMPACT);
     192          94 :   if (NULL == str)
     193           0 :     return -1;
     194          94 :   scratch[0] = str;
     195          94 :   param_values[0] = (void *) str;
     196          94 :   param_lengths[0] = strlen (str);
     197          94 :   param_formats[0] = 1;
     198          94 :   return 1;
     199             : }
     200             : 
     201             : 
     202             : /**
     203             :  * Generate query parameter for a JSON object (stored as a string
     204             :  * in the DB).
     205             :  *
     206             :  * @param x pointer to the json object to pass
     207             :  */
     208             : struct GNUNET_PQ_QueryParam
     209          94 : TALER_PQ_query_param_json (const json_t *x)
     210             : {
     211          94 :   struct GNUNET_PQ_QueryParam res =
     212             :   { &qconv_json, NULL, x, 0, 1 };
     213          94 :   return res;
     214             : }
     215             : 
     216             : 
     217             : /**
     218             :  * Function called to convert input argument into SQL parameters.
     219             :  *
     220             :  * @param cls closure
     221             :  * @param data pointer to input argument
     222             :  * @param data_len number of bytes in @a data (if applicable)
     223             :  * @param[out] param_values SQL data to set
     224             :  * @param[out] param_lengths SQL length data to set
     225             :  * @param[out] param_formats SQL format data to set
     226             :  * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
     227             :  * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
     228             :  * @param scratch_length number of entries left in @a scratch
     229             :  * @return -1 on error, number of offsets used in @a scratch otherwise
     230             :  */
     231             : static int
     232        2956 : qconv_round_time (void *cls,
     233             :                   const void *data,
     234             :                   size_t data_len,
     235             :                   void *param_values[],
     236             :                   int param_lengths[],
     237             :                   int param_formats[],
     238             :                   unsigned int param_length,
     239             :                   void *scratch[],
     240             :                   unsigned int scratch_length)
     241             : {
     242        2956 :   const struct GNUNET_TIME_Absolute *at = data;
     243             :   struct GNUNET_TIME_Absolute tmp;
     244             :   struct GNUNET_TIME_AbsoluteNBO *buf;
     245             : 
     246             :   (void) cls;
     247        2956 :   GNUNET_assert (1 == param_length);
     248        2956 :   GNUNET_assert (sizeof (struct GNUNET_TIME_AbsoluteNBO) == data_len);
     249        2956 :   GNUNET_assert (scratch_length > 0);
     250        2956 :   GNUNET_break (NULL == cls);
     251        2956 :   tmp = *at;
     252        2956 :   GNUNET_assert (GNUNET_OK ==
     253             :                  GNUNET_TIME_round_abs (&tmp));
     254        2956 :   buf = GNUNET_new (struct GNUNET_TIME_AbsoluteNBO);
     255        2956 :   *buf = GNUNET_TIME_absolute_hton (tmp);
     256        2956 :   scratch[0] = buf;
     257        2956 :   param_values[0] = (void *) buf;
     258        2956 :   param_lengths[0] = sizeof (struct GNUNET_TIME_AbsoluteNBO);
     259        2956 :   param_formats[0] = 1;
     260        2956 :   return 1;
     261             : }
     262             : 
     263             : 
     264             : /**
     265             :  * Generate query parameter for an absolute time value.
     266             :  * In contrast to
     267             :  * #GNUNET_PQ_query_param_absolute_time(), this function
     268             :  * will abort (!) if the time given is not rounded!
     269             :  * The database must store a 64-bit integer.
     270             :  *
     271             :  * @param x pointer to the query parameter to pass
     272             :  * @return array entry for the query parameters to use
     273             :  */
     274             : struct GNUNET_PQ_QueryParam
     275        2957 : TALER_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
     276             : {
     277        2957 :   struct GNUNET_PQ_QueryParam res =
     278             :   { &qconv_round_time, NULL, x, sizeof (*x), 1 };
     279        2957 :   return res;
     280             : }
     281             : 
     282             : 
     283             : /**
     284             :  * Function called to convert input argument into SQL parameters.
     285             :  *
     286             :  * @param cls closure
     287             :  * @param data pointer to input argument
     288             :  * @param data_len number of bytes in @a data (if applicable)
     289             :  * @param[out] param_values SQL data to set
     290             :  * @param[out] param_lengths SQL length data to set
     291             :  * @param[out] param_formats SQL format data to set
     292             :  * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
     293             :  * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
     294             :  * @param scratch_length number of entries left in @a scratch
     295             :  * @return -1 on error, number of offsets used in @a scratch otherwise
     296             :  */
     297             : static int
     298         128 : qconv_round_time_abs (void *cls,
     299             :                       const void *data,
     300             :                       size_t data_len,
     301             :                       void *param_values[],
     302             :                       int param_lengths[],
     303             :                       int param_formats[],
     304             :                       unsigned int param_length,
     305             :                       void *scratch[],
     306             :                       unsigned int scratch_length)
     307             : {
     308         128 :   const struct GNUNET_TIME_AbsoluteNBO *at = data;
     309             :   struct GNUNET_TIME_Absolute tmp;
     310             : 
     311             :   (void) cls;
     312             :   (void) scratch;
     313             :   (void) scratch_length;
     314         128 :   GNUNET_assert (1 == param_length);
     315         128 :   GNUNET_assert (sizeof (struct GNUNET_TIME_AbsoluteNBO) == data_len);
     316         128 :   GNUNET_break (NULL == cls);
     317         128 :   tmp = GNUNET_TIME_absolute_ntoh (*at);
     318         128 :   GNUNET_assert (GNUNET_OK ==
     319             :                  GNUNET_TIME_round_abs (&tmp));
     320         128 :   param_values[0] = (void *) at;
     321         128 :   param_lengths[0] = sizeof (struct GNUNET_TIME_AbsoluteNBO);
     322         128 :   param_formats[0] = 1;
     323         128 :   return 0;
     324             : }
     325             : 
     326             : 
     327             : /**
     328             :  * Generate query parameter for an absolute time value.
     329             :  * In contrast to
     330             :  * #GNUNET_PQ_query_param_absolute_time(), this function
     331             :  * will abort (!) if the time given is not rounded!
     332             :  * The database must store a 64-bit integer.
     333             :  *
     334             :  * @param x pointer to the query parameter to pass
     335             :  */
     336             : struct GNUNET_PQ_QueryParam
     337         128 : TALER_PQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
     338             : {
     339         128 :   struct GNUNET_PQ_QueryParam res =
     340             :   { &qconv_round_time_abs, NULL, x, sizeof (*x), 1 };
     341         128 :   return res;
     342             : }
     343             : 
     344             : 
     345             : /* end of pq/pq_query_helper.c */

Generated by: LCOV version 1.14