LCOV - code coverage report
Current view: top level - backenddb - pg_lookup_refunds.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 16 27 59.3 %
Date: 2025-06-23 16:22:09 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 backenddb/pg_lookup_refunds.c
      18             :  * @brief Implementation of the lookup_refunds function for Postgres
      19             :  * @author Iván Ávalos
      20             :  */
      21             : #include "platform.h"
      22             : #include <taler/taler_error_codes.h>
      23             : #include <taler/taler_dbevents.h>
      24             : #include <taler/taler_pq_lib.h>
      25             : #include "pg_lookup_refunds.h"
      26             : #include "pg_helper.h"
      27             : 
      28             : /**
      29             :  * Closure for #lookup_refunds_cb().
      30             :  */
      31             : struct LookupRefundsContext
      32             : {
      33             :   /**
      34             :    * Function to call for each refund.
      35             :    */
      36             :   TALER_MERCHANTDB_RefundCallback rc;
      37             : 
      38             :   /**
      39             :    * Closure for @e rc.
      40             :    */
      41             :   void *rc_cls;
      42             : 
      43             :   /**
      44             :    * Plugin context.
      45             :    */
      46             :   struct PostgresClosure *pg;
      47             : 
      48             :   /**
      49             :    * Transaction result.
      50             :    */
      51             :   enum GNUNET_DB_QueryStatus qs;
      52             : };
      53             : 
      54             : 
      55             : /**
      56             :  * Function to be called with the results of a SELECT statement
      57             :  * that has returned @a num_results results.
      58             :  *
      59             :  * @param cls of type `struct LookupRefundsContext *`
      60             :  * @param result the postgres result
      61             :  * @param num_results the number of results in @a result
      62             :  */
      63             : static void
      64          68 : lookup_refunds_cb (void *cls,
      65             :                    PGresult *result,
      66             :                    unsigned int num_results)
      67             : {
      68          68 :   struct LookupRefundsContext *lrc = cls;
      69             : 
      70          68 :   for (unsigned int i = 0; i<num_results; i++)
      71             :   {
      72             :     struct TALER_CoinSpendPublicKeyP coin_pub;
      73             :     struct TALER_Amount refund_amount;
      74           0 :     struct GNUNET_PQ_ResultSpec rs[] = {
      75           0 :       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
      76             :                                             &coin_pub),
      77           0 :       TALER_PQ_result_spec_amount_with_currency ("refund_amount",
      78             :                                                  &refund_amount),
      79             :       GNUNET_PQ_result_spec_end
      80             :     };
      81             : 
      82           0 :     if (GNUNET_OK !=
      83           0 :         GNUNET_PQ_extract_result (result,
      84             :                                   rs,
      85             :                                   i))
      86             :     {
      87           0 :       GNUNET_break (0);
      88           0 :       lrc->qs = GNUNET_DB_STATUS_HARD_ERROR;
      89           0 :       return;
      90             :     }
      91           0 :     lrc->rc (lrc->rc_cls,
      92             :              &coin_pub,
      93             :              &refund_amount);
      94           0 :     GNUNET_PQ_cleanup_result (rs); /* technically useless here */
      95             :   }
      96          68 :   lrc->qs = num_results;
      97             : }
      98             : 
      99             : 
     100             : enum GNUNET_DB_QueryStatus
     101          68 : TMH_PG_lookup_refunds (
     102             :   void *cls,
     103             :   const char *instance_id,
     104             :   const struct TALER_PrivateContractHashP *h_contract_terms,
     105             :   TALER_MERCHANTDB_RefundCallback rc,
     106             :   void *rc_cls)
     107             : {
     108          68 :   struct PostgresClosure *pg = cls;
     109          68 :   struct GNUNET_PQ_QueryParam params[] = {
     110          68 :     GNUNET_PQ_query_param_string (instance_id),
     111          68 :     GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
     112             :     GNUNET_PQ_query_param_end
     113             :   };
     114          68 :   struct LookupRefundsContext lrc = {
     115             :     .rc = rc,
     116             :     .rc_cls = rc_cls,
     117             :     .pg = pg
     118             :   };
     119             :   enum GNUNET_DB_QueryStatus qs;
     120             : 
     121             :   /* no preflight check here, run in transaction by caller! */
     122          68 :   TALER_LOG_DEBUG ("Looking for refund of h_contract_terms %s at `%s'\n",
     123             :                    GNUNET_h2s (&h_contract_terms->hash),
     124             :                    instance_id);
     125          68 :   check_connection (pg);
     126          68 :   PREPARE (pg,
     127             :            "lookup_refunds",
     128             :            "SELECT"
     129             :            " coin_pub"
     130             :            ",refund_amount"
     131             :            " FROM merchant_refunds"
     132             :            " WHERE order_serial="
     133             :            "  (SELECT order_serial"
     134             :            "     FROM merchant_contract_terms"
     135             :            "    WHERE h_contract_terms=$2"
     136             :            "      AND merchant_serial="
     137             :            "        (SELECT merchant_serial"
     138             :            "           FROM merchant_instances"
     139             :            "          WHERE merchant_id=$1))");
     140          68 :   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
     141             :                                              "lookup_refunds",
     142             :                                              params,
     143             :                                              &lookup_refunds_cb,
     144             :                                              &lrc);
     145          68 :   if (0 >= qs)
     146          68 :     return qs;
     147           0 :   return lrc.qs;
     148             : }

Generated by: LCOV version 1.16