LCOV - code coverage report
Current view: top level - backenddb - pg_lookup_transfer_details_by_order.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 86.7 % 30 26
Test Date: 2025-11-06 19:31:41 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /*
       2              :    This file is part of TALER
       3              :    Copyright (C) 2023, 2025 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_transfer_details_by_order.c
      18              :  * @brief Implementation of the lookup_transfer_details_by_order 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_transfer_details_by_order.h"
      26              : #include "pg_helper.h"
      27              : 
      28              : /**
      29              :  * Closure for lookup_transfer_details_by_order_cb().
      30              :  */
      31              : struct LookupTransferDetailsByOrderContext
      32              : {
      33              : 
      34              :   /**
      35              :    * Plugin context.
      36              :    */
      37              :   struct PostgresClosure *pg;
      38              : 
      39              :   /**
      40              :    * Function to call with all results.
      41              :    */
      42              :   TALER_MERCHANTDB_OrderTransferDetailsCallback cb;
      43              : 
      44              :   /**
      45              :    * Closure for @e cb.
      46              :    */
      47              :   void *cb_cls;
      48              : 
      49              :   /**
      50              :    * Set to the query result.
      51              :    */
      52              :   enum GNUNET_DB_QueryStatus qs;
      53              : };
      54              : 
      55              : 
      56              : /**
      57              :  * Function to be called with the results of a SELECT statement
      58              :  * that has returned @a num_results results.
      59              :  *
      60              :  * @param cls of type `struct LookupTransferDetailsByOrderContext *`
      61              :  * @param result the postgres result
      62              :  * @param num_results the number of results in @a result
      63              :  */
      64              : static void
      65           36 : lookup_transfer_details_by_order_cb (void *cls,
      66              :                                      PGresult *result,
      67              :                                      unsigned int num_results)
      68              : {
      69           36 :   struct LookupTransferDetailsByOrderContext *ltdo = cls;
      70              : 
      71           50 :   for (unsigned int i = 0; i<num_results; i++)
      72              :   {
      73              :     struct TALER_WireTransferIdentifierRawP wtid;
      74              :     char *exchange_url;
      75              :     uint64_t deposit_serial;
      76              :     struct GNUNET_TIME_Timestamp execution_time;
      77              :     struct TALER_Amount deposit_value;
      78              :     struct TALER_Amount deposit_fee;
      79              :     bool confirmed;
      80           14 :     struct GNUNET_PQ_ResultSpec rs[] = {
      81           14 :       GNUNET_PQ_result_spec_uint64 ("deposit_serial",
      82              :                                     &deposit_serial),
      83           14 :       GNUNET_PQ_result_spec_timestamp ("deposit_timestamp",
      84              :                                        &execution_time),
      85           14 :       GNUNET_PQ_result_spec_string ("exchange_url",
      86              :                                     &exchange_url),
      87           14 :       GNUNET_PQ_result_spec_bool ("confirmed",
      88              :                                   &confirmed),
      89           14 :       GNUNET_PQ_result_spec_auto_from_type ("wtid",
      90              :                                             &wtid),
      91           14 :       TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_value",
      92              :                                                  &deposit_value),
      93           14 :       TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_fee",
      94              :                                                  &deposit_fee),
      95              :       GNUNET_PQ_result_spec_end
      96              :     };
      97              : 
      98           14 :     if (GNUNET_OK !=
      99           14 :         GNUNET_PQ_extract_result (result,
     100              :                                   rs,
     101              :                                   i))
     102              :     {
     103            0 :       GNUNET_break (0);
     104            0 :       ltdo->qs = GNUNET_DB_STATUS_HARD_ERROR;
     105            0 :       return;
     106              :     }
     107           14 :     ltdo->cb (ltdo->cb_cls,
     108              :               &wtid,
     109              :               exchange_url,
     110              :               execution_time,
     111              :               &deposit_value,
     112              :               &deposit_fee,
     113              :               confirmed);
     114           14 :     GNUNET_PQ_cleanup_result (rs); /* technically useless here */
     115              :   }
     116           36 :   ltdo->qs = num_results;
     117              : }
     118              : 
     119              : 
     120              : enum GNUNET_DB_QueryStatus
     121           36 : TMH_PG_lookup_transfer_details_by_order (
     122              :   void *cls,
     123              :   uint64_t order_serial,
     124              :   TALER_MERCHANTDB_OrderTransferDetailsCallback cb,
     125              :   void *cb_cls)
     126              : {
     127           36 :   struct PostgresClosure *pg = cls;
     128           36 :   struct LookupTransferDetailsByOrderContext ltdo = {
     129              :     .pg = pg,
     130              :     .cb = cb,
     131              :     .cb_cls = cb_cls
     132              :   };
     133           36 :   struct GNUNET_PQ_QueryParam params[] = {
     134           36 :     GNUNET_PQ_query_param_uint64 (&order_serial),
     135              :     GNUNET_PQ_query_param_end
     136              :   };
     137              :   enum GNUNET_DB_QueryStatus qs;
     138              : 
     139           36 :   check_connection (pg);
     140           36 :   PREPARE (pg,
     141              :            "lookup_transfer_details_by_order",
     142              :            "SELECT"
     143              :            " md.deposit_serial"
     144              :            ",mcon.exchange_url"
     145              :            ",met.wtid"
     146              :            ",mtc.exchange_deposit_value"
     147              :            ",mtc.exchange_deposit_fee"
     148              :            ",mcon.deposit_timestamp"
     149              :            ",met.confirmed"
     150              :            " FROM merchant_expected_transfer_to_coin mtc"
     151              :            " JOIN merchant_deposits md"
     152              :            "   USING (deposit_serial)"
     153              :            " JOIN merchant_deposit_confirmations mcon"
     154              :            "   USING (deposit_confirmation_serial)"
     155              :            " JOIN merchant_expected_transfers met"
     156              :            "   USING (expected_credit_serial)"
     157              :            " JOIN merchant_accounts acc"
     158              :            "   ON (acc.account_serial = met.account_serial)"
     159              :            /* Check that all this is for the same instance */
     160              :            " JOIN merchant_contract_terms contracts"
     161              :            "   USING (merchant_serial, order_serial)"
     162              :            " WHERE mcon.order_serial=$1");
     163              : 
     164           36 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     165              :     pg->conn,
     166              :     "lookup_transfer_details_by_order",
     167              :     params,
     168              :     &lookup_transfer_details_by_order_cb,
     169              :     &ltdo);
     170           36 :   if (qs < 0)
     171            0 :     return qs;
     172           36 :   return ltdo.qs;
     173              : }
        

Generated by: LCOV version 2.0-1