LCOV - code coverage report
Current view: top level - pq - pq_query_helper.c (source / functions) Hit Total Coverage
Test: rcoverage.info Lines: 41 42 97.6 %
Date: 2017-11-25 11:31:41 Functions: 6 6 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         343 : 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         343 :   const struct TALER_AmountNBO *amount = data;
      55         343 :   unsigned int off = 0;
      56             : 
      57         343 :   GNUNET_assert (3 == param_length);
      58         343 :   param_values[off] = (void *) &amount->value;
      59         343 :   param_lengths[off] = sizeof (amount->value);
      60         343 :   param_formats[off] = 1;
      61         343 :   off++;
      62         343 :   param_values[off] = (void *) &amount->fraction;
      63         343 :   param_lengths[off] = sizeof (amount->fraction);
      64         343 :   param_formats[off] = 1;
      65         343 :   off++;
      66         343 :   param_values[off] = (void *) amount->currency;
      67         343 :   param_lengths[off] = strlen (amount->currency);
      68         343 :   param_formats[off] = 1;
      69         343 :   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         191 : TALER_PQ_query_param_amount_nbo (const struct TALER_AmountNBO *x)
      84             : {
      85         191 :   struct GNUNET_PQ_QueryParam res =
      86             :     { &qconv_amount_nbo, NULL, x, sizeof (*x), 3 };
      87         191 :   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         152 : 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         152 :   const struct TALER_Amount *amount_hbo = data;
     117             :   struct TALER_AmountNBO *amount;
     118             : 
     119         152 :   amount = GNUNET_new (struct TALER_AmountNBO);
     120         152 :   scratch[0] = amount;
     121         152 :   TALER_amount_hton (amount,
     122             :                      amount_hbo);
     123         152 :   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         152 :   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         182 : TALER_PQ_query_param_amount (const struct TALER_Amount *x)
     147             : {
     148         182 :   struct GNUNET_PQ_QueryParam res =
     149             :     { &qconv_amount, NULL, x, sizeof (*x), 3 };
     150         182 :   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          79 : 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          79 :   const json_t *json = data;
     180             :   char *str;
     181             : 
     182          79 :   str = json_dumps (json, JSON_COMPACT);
     183          79 :   if (NULL == str)
     184           0 :     return -1;
     185          79 :   scratch[0] = str;
     186          79 :   param_values[0] = (void *) str;
     187          79 :   param_lengths[0] = strlen (str);
     188          79 :   param_formats[0] = 1;
     189          79 :   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          79 : TALER_PQ_query_param_json (const json_t *x)
     201             : {
     202          79 :   struct GNUNET_PQ_QueryParam res =
     203             :     { &qconv_json, NULL, x, 0, 1 };
     204          79 :   return res;
     205             : }
     206             : 
     207             : 
     208             : /* end of pq/pq_query_helper.c */

Generated by: LCOV version 1.13