LCOV - code coverage report
Current view: top level - sq - sq_query_helper.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 30 34 88.2 %
Date: 2022-08-25 06:15:09 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2020 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 sq/sq_query_helper.c
      18             :  * @brief helper functions for Taler-specific SQLite3 interactions
      19             :  * @author Jonathan Buchanan
      20             :  */
      21             : #include "platform.h"
      22             : #include <sqlite3.h>
      23             : #include <gnunet/gnunet_util_lib.h>
      24             : #include <gnunet/gnunet_sq_lib.h>
      25             : #include "taler_sq_lib.h"
      26             : 
      27             : 
      28             : /**
      29             :  * Function called to convert input argument into SQL parameters.
      30             :  *
      31             :  * @param cls closure
      32             :  * @param data pointer to input argument, here a `struct TALER_Amount`
      33             :  * @param data_len number of bytes in @a data (if applicable)
      34             :  * @param stmt sqlite statement to parameters for
      35             :  * @param off offset of the argument to bind in @a stmt, numbered from 1,
      36             :  *            so immediately suitable for passing to `sqlite3_bind`-functions.
      37             :  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
      38             :  */
      39             : static enum GNUNET_GenericReturnValue
      40           2 : qconv_amount (void *cls,
      41             :               const void *data,
      42             :               size_t data_len,
      43             :               sqlite3_stmt *stmt,
      44             :               unsigned int off)
      45             : {
      46           2 :   const struct TALER_Amount *amount = data;
      47             : 
      48             :   (void) cls;
      49           2 :   GNUNET_assert (sizeof (struct TALER_Amount) == data_len);
      50           2 :   if (SQLITE_OK != sqlite3_bind_int64 (stmt,
      51             :                                        (int) off,
      52           2 :                                        (sqlite3_int64) amount->value))
      53           0 :     return GNUNET_SYSERR;
      54           2 :   if (SQLITE_OK != sqlite3_bind_int64 (stmt,
      55           2 :                                        (int) off + 1,
      56           2 :                                        (sqlite3_int64) amount->fraction))
      57           0 :     return GNUNET_SYSERR;
      58           2 :   return GNUNET_OK;
      59             : }
      60             : 
      61             : 
      62             : struct GNUNET_SQ_QueryParam
      63           1 : TALER_SQ_query_param_amount (const struct TALER_Amount *x)
      64             : {
      65           1 :   struct GNUNET_SQ_QueryParam res = {
      66             :     .conv = &qconv_amount,
      67             :     .data = x,
      68             :     .size = sizeof (*x),
      69             :     .num_params = 2
      70             :   };
      71             : 
      72           1 :   return res;
      73             : }
      74             : 
      75             : 
      76             : /**
      77             :  * Function called to convert input argument into SQL parameters.
      78             :  *
      79             :  * @param cls closure
      80             :  * @param data pointer to input argument, here a `struct TALER_AmountNBO`
      81             :  * @param data_len number of bytes in @a data (if applicable)
      82             :  * @param stmt sqlite statement to parameters for
      83             :  * @param off offset of the argument to bind in @a stmt, numbered from 1,
      84             :  *            so immediately suitable for passing to `sqlite3_bind`-functions.
      85             :  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
      86             :  */
      87             : static enum GNUNET_GenericReturnValue
      88           1 : qconv_amount_nbo (void *cls,
      89             :                   const void *data,
      90             :                   size_t data_len,
      91             :                   sqlite3_stmt *stmt,
      92             :                   unsigned int off)
      93             : {
      94           1 :   const struct TALER_AmountNBO *amount = data;
      95             :   struct TALER_Amount amount_hbo;
      96             : 
      97             :   (void) cls;
      98             :   (void) data_len;
      99           1 :   TALER_amount_ntoh (&amount_hbo,
     100             :                      amount);
     101           1 :   return qconv_amount (cls,
     102             :                        &amount_hbo,
     103             :                        sizeof (struct TALER_Amount),
     104             :                        stmt,
     105             :                        off);
     106             : }
     107             : 
     108             : 
     109             : struct GNUNET_SQ_QueryParam
     110           1 : TALER_SQ_query_param_amount_nbo (const struct TALER_AmountNBO *x)
     111             : {
     112           1 :   struct GNUNET_SQ_QueryParam res = {
     113             :     .conv = &qconv_amount_nbo,
     114             :     .data = x,
     115             :     .size = sizeof (*x),
     116             :     .num_params = 2
     117             :   };
     118             : 
     119           1 :   return res;
     120             : }
     121             : 
     122             : 
     123             : /**
     124             :  * Function called to convert input argument into SQL parameters.
     125             :  *
     126             :  * @param cls closure
     127             :  * @param data pointer to input argument, here a `struct TALER_Amount`
     128             :  * @param data_len number of bytes in @a data (if applicable)
     129             :  * @param stmt sqlite statement to parameters for
     130             :  * @param off offset of the argument to bind in @a stmt, numbered from 1,
     131             :  *            so immediately suitable for passing to `sqlite3_bind`-functions.
     132             :  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
     133             :  */
     134             : static enum GNUNET_GenericReturnValue
     135           1 : qconv_json (void *cls,
     136             :             const void *data,
     137             :             size_t data_len,
     138             :             sqlite3_stmt *stmt,
     139             :             unsigned int off)
     140             : {
     141           1 :   const json_t *json = data;
     142             :   char *str;
     143             : 
     144             :   (void) cls;
     145             :   (void) data_len;
     146           1 :   str = json_dumps (json, JSON_COMPACT);
     147           1 :   if (NULL == str)
     148           0 :     return GNUNET_SYSERR;
     149             : 
     150           1 :   if (SQLITE_OK != sqlite3_bind_text (stmt,
     151             :                                       (int) off,
     152             :                                       str,
     153           1 :                                       strlen (str) + 1,
     154             :                                       SQLITE_TRANSIENT))
     155           0 :     return GNUNET_SYSERR;
     156           1 :   GNUNET_free (str);
     157           1 :   return GNUNET_OK;
     158             : }
     159             : 
     160             : 
     161             : struct GNUNET_SQ_QueryParam
     162           1 : TALER_SQ_query_param_json (const json_t *x)
     163             : {
     164           1 :   struct GNUNET_SQ_QueryParam res = {
     165             :     .conv = &qconv_json,
     166             :     .data = x,
     167             :     .size = sizeof (*x),
     168             :     .num_params = 1
     169             :   };
     170             : 
     171           1 :   return res;
     172             : }
     173             : 
     174             : 
     175             : /* end of sq/sq_query_helper.c */

Generated by: LCOV version 1.14