LCOV - code coverage report
Current view: top level - backenddb - iterate_transfer_details.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 85.7 % 28 24
Test Date: 2026-09-04 23:42:01 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /*
       2              :    This file is part of TALER
       3              :    Copyright (C) 2023 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 src/backenddb/iterate_transfer_details.c
      18              :  * @brief Implementation of the iterate_transfer_details function for Postgres
      19              :  * @author Iván Ávalos
      20              :  */
      21              : #include "platform.h"
      22              : #include <taler/taler_pq_lib.h>
      23              : #include "merchant-database/iterate_transfer_details.h"
      24              : #include "helper.h"
      25              : 
      26              : /**
      27              :  * Closure for #lookup_transfer_details_cb().
      28              :  */
      29              : struct LookupTransferDetailsContext
      30              : {
      31              :   /**
      32              :    * Function to call for each order that was aggregated.
      33              :    */
      34              :   TALER_MERCHANTDB_TransferDetailsCallback cb;
      35              : 
      36              :   /**
      37              :    * Closure for @e cb.
      38              :    */
      39              :   void *cb_cls;
      40              : 
      41              :   /**
      42              :    * Plugin context.
      43              :    */
      44              :   struct TALER_MERCHANTDB_PostgresContext *pg;
      45              : 
      46              :   /**
      47              :    * Transaction result.
      48              :    */
      49              :   enum GNUNET_DB_QueryStatus qs;
      50              : };
      51              : 
      52              : 
      53              : /**
      54              :  * Function to be called with the results of a SELECT statement
      55              :  * that has returned @a num_results results.
      56              :  *
      57              :  * @param cls of type `struct LookupTransferDetailsContext *`
      58              :  * @param result the postgres result
      59              :  * @param num_results the number of results in @a result
      60              :  */
      61              : static void
      62            1 : lookup_transfer_details_cb (void *cls,
      63              :                             PGresult *result,
      64              :                             unsigned int num_results)
      65              : {
      66            1 :   struct LookupTransferDetailsContext *ltdc = cls;
      67              : 
      68            2 :   for (unsigned int i = 0; i<num_results; i++)
      69              :   {
      70              :     uint64_t current_offset;
      71              :     struct TALER_TrackTransferDetails ttd;
      72            1 :     struct GNUNET_PQ_ResultSpec rs[] = {
      73            1 :       GNUNET_PQ_result_spec_uint64 ("offset_in_exchange_list",
      74              :                                     &current_offset),
      75            1 :       GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms",
      76              :                                             &ttd.h_contract_terms),
      77            1 :       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
      78              :                                             &ttd.coin_pub),
      79            1 :       TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_value",
      80              :                                                  &ttd.coin_value),
      81            1 :       TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_fee",
      82              :                                                  &ttd.coin_fee),
      83              :       GNUNET_PQ_result_spec_end
      84              :     };
      85              : 
      86            1 :     if (GNUNET_OK !=
      87            1 :         GNUNET_PQ_extract_result (result,
      88              :                                   rs,
      89              :                                   i))
      90              :     {
      91            0 :       GNUNET_break (0);
      92            0 :       ltdc->qs = GNUNET_DB_STATUS_HARD_ERROR;
      93            0 :       return;
      94              :     }
      95            1 :     ltdc->cb (ltdc->cb_cls,
      96              :               (unsigned int) current_offset,
      97              :               &ttd);
      98            1 :     GNUNET_PQ_cleanup_result (rs);
      99              :   }
     100            1 :   ltdc->qs = num_results;
     101              : }
     102              : 
     103              : 
     104              : enum GNUNET_DB_QueryStatus
     105            1 : TALER_MERCHANTDB_iterate_transfer_details (
     106              :   struct TALER_MERCHANTDB_PostgresContext *pg,
     107              :   const char *exchange_url,
     108              :   const struct TALER_WireTransferIdentifierRawP *wtid,
     109              :   TALER_MERCHANTDB_TransferDetailsCallback cb,
     110              :   void *cb_cls)
     111              : {
     112            1 :   struct GNUNET_PQ_QueryParam params[] = {
     113            1 :     GNUNET_PQ_query_param_string (exchange_url),
     114            1 :     GNUNET_PQ_query_param_auto_from_type (wtid),
     115              :     GNUNET_PQ_query_param_end
     116              :   };
     117            1 :   struct LookupTransferDetailsContext ltdc = {
     118              :     .cb  = cb,
     119              :     .cb_cls = cb_cls,
     120              :     .pg = pg
     121              :   };
     122              :   enum GNUNET_DB_QueryStatus qs;
     123              : 
     124            1 :   GNUNET_assert (NULL != pg->current_merchant_id);
     125            1 :   TMH_PQ_prepare_anon (pg,
     126              :                        "SELECT"
     127              :                        " mterm.h_contract_terms"
     128              :                        ",mtcoin.offset_in_exchange_list"
     129              :                        ",dep.coin_pub"
     130              :                        ",mtcoin.exchange_deposit_value"
     131              :                        ",mtcoin.exchange_deposit_fee"
     132              :                        " FROM merchant_expected_transfer_to_coin mtcoin"
     133              :                        " JOIN merchant_deposits dep"
     134              :                        "   USING (deposit_serial)"
     135              :                        " JOIN merchant_deposit_confirmations mcon"
     136              :                        "   USING (deposit_confirmation_serial)"
     137              :                        " JOIN merchant_contract_terms mterm"
     138              :                        "   USING (order_serial)"
     139              :                        " JOIN merchant_expected_transfers met"
     140              :                        "   USING (expected_credit_serial)"
     141              :                        " WHERE met.wtid=$2"
     142              :                        "   AND met.exchange_url=$1");
     143              : 
     144            1 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     145              :     pg->conn,
     146              :     "",
     147              :     params,
     148              :     &lookup_transfer_details_cb,
     149              :     &ltdc);
     150            1 :   if (0 >= qs)
     151            0 :     return qs;
     152            1 :   return ltdc.qs;
     153              : }
        

Generated by: LCOV version 2.0-1