LCOV - code coverage report
Current view: top level - exchangedb - pg_get_wire_fees.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 23 27 85.2 %
Date: 2025-06-05 21:03:14 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :    This file is part of TALER
       3             :    Copyright (C) 2022 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 exchangedb/pg_get_wire_fees.c
      18             :  * @brief Implementation of the get_wire_fees function for Postgres
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include "taler_error_codes.h"
      23             : #include "taler_dbevents.h"
      24             : #include "taler_pq_lib.h"
      25             : #include "pg_get_wire_fees.h"
      26             : #include "pg_helper.h"
      27             : 
      28             : /**
      29             :  * Closure for #get_wire_fees_cb().
      30             :  */
      31             : struct GetWireFeesContext
      32             : {
      33             :   /**
      34             :    * Function to call per result.
      35             :    */
      36             :   TALER_EXCHANGEDB_WireFeeCallback cb;
      37             : 
      38             :   /**
      39             :    * Closure for @e cb.
      40             :    */
      41             :   void *cb_cls;
      42             : 
      43             :   /**
      44             :    * Plugin context.
      45             :    */
      46             :   struct PostgresClosure *pg;
      47             : 
      48             :   /**
      49             :    * Flag set to #GNUNET_OK as long as everything is fine.
      50             :    */
      51             :   enum GNUNET_GenericReturnValue status;
      52             : 
      53             : };
      54             : 
      55             : 
      56             : /**
      57             :  * Invoke the callback for each result.
      58             :  *
      59             :  * @param cls a `struct GetWireFeesContext *`
      60             :  * @param result SQL result
      61             :  * @param num_results number of rows in @a result
      62             :  */
      63             : static void
      64          23 : get_wire_fees_cb (void *cls,
      65             :                   PGresult *result,
      66             :                   unsigned int num_results)
      67             : {
      68          23 :   struct GetWireFeesContext *ctx = cls;
      69          23 :   struct PostgresClosure *pg = ctx->pg;
      70             : 
      71          46 :   for (unsigned int i = 0; i < num_results; i++)
      72             :   {
      73             :     struct TALER_MasterSignatureP master_sig;
      74             :     struct TALER_WireFeeSet fees;
      75             :     struct GNUNET_TIME_Timestamp start_date;
      76             :     struct GNUNET_TIME_Timestamp end_date;
      77          23 :     struct GNUNET_PQ_ResultSpec rs[] = {
      78          23 :       TALER_PQ_RESULT_SPEC_AMOUNT ("wire_fee",
      79             :                                    &fees.wire),
      80          23 :       TALER_PQ_RESULT_SPEC_AMOUNT ("closing_fee",
      81             :                                    &fees.closing),
      82          23 :       GNUNET_PQ_result_spec_timestamp ("start_date",
      83             :                                        &start_date),
      84          23 :       GNUNET_PQ_result_spec_timestamp ("end_date",
      85             :                                        &end_date),
      86          23 :       GNUNET_PQ_result_spec_auto_from_type ("master_sig",
      87             :                                             &master_sig),
      88             :       GNUNET_PQ_result_spec_end
      89             :     };
      90             : 
      91          23 :     if (GNUNET_OK !=
      92          23 :         GNUNET_PQ_extract_result (result,
      93             :                                   rs,
      94             :                                   i))
      95             :     {
      96           0 :       GNUNET_break (0);
      97           0 :       ctx->status = GNUNET_SYSERR;
      98           0 :       return;
      99             :     }
     100          23 :     ctx->cb (ctx->cb_cls,
     101             :              &fees,
     102             :              start_date,
     103             :              end_date,
     104             :              &master_sig);
     105          23 :     GNUNET_PQ_cleanup_result (rs);
     106             :   }
     107             : }
     108             : 
     109             : 
     110             : enum GNUNET_DB_QueryStatus
     111          23 : TEH_PG_get_wire_fees (void *cls,
     112             :                       const char *wire_method,
     113             :                       TALER_EXCHANGEDB_WireFeeCallback cb,
     114             :                       void *cb_cls)
     115             : {
     116          23 :   struct PostgresClosure *pg = cls;
     117          23 :   struct GNUNET_PQ_QueryParam params[] = {
     118          23 :     GNUNET_PQ_query_param_string (wire_method),
     119             :     GNUNET_PQ_query_param_end
     120             :   };
     121          23 :   struct GetWireFeesContext ctx = {
     122             :     .cb = cb,
     123             :     .cb_cls = cb_cls,
     124             :     .pg = pg,
     125             :     .status = GNUNET_OK
     126             :   };
     127             :   enum GNUNET_DB_QueryStatus qs;
     128             : 
     129          23 :   PREPARE (pg,
     130             :            "get_wire_fees",
     131             :            "SELECT"
     132             :            " wire_fee"
     133             :            ",closing_fee"
     134             :            ",start_date"
     135             :            ",end_date"
     136             :            ",master_sig"
     137             :            " FROM wire_fee"
     138             :            " WHERE wire_method=$1");
     139          23 :   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
     140             :                                              "get_wire_fees",
     141             :                                              params,
     142             :                                              &get_wire_fees_cb,
     143             :                                              &ctx);
     144          23 :   if (GNUNET_OK != ctx.status)
     145           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     146          23 :   return qs;
     147             : }

Generated by: LCOV version 1.16