LCOV - code coverage report
Current view: top level - exchangedb - pg_select_purse_deposits_above_serial_id.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 13 40 32.5 %
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 pg_select_purse_deposits_above_serial_id.c
      18             :  * @brief Implementation of the select_purse_deposits_above_serial_id 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_select_purse_deposits_above_serial_id.h"
      26             : #include "pg_helper.h"
      27             : 
      28             : /**
      29             :  * Closure for #purse_deposit_serial_helper_cb().
      30             :  */
      31             : struct PurseDepositSerialContext
      32             : {
      33             : 
      34             :   /**
      35             :    * Callback to call.
      36             :    */
      37             :   TALER_EXCHANGEDB_PurseDepositCallback cb;
      38             : 
      39             :   /**
      40             :    * Closure for @e cb.
      41             :    */
      42             :   void *cb_cls;
      43             : 
      44             :   /**
      45             :    * Plugin context.
      46             :    */
      47             :   struct PostgresClosure *pg;
      48             : 
      49             :   /**
      50             :    * Status code, set to #GNUNET_SYSERR on hard errors.
      51             :    */
      52             :   enum GNUNET_GenericReturnValue status;
      53             : };
      54             : 
      55             : 
      56             : /**
      57             :  * Helper function to be called with the results of a SELECT statement
      58             :  * that has returned @a num_results results.
      59             :  *
      60             :  * @param cls closure of type `struct DepositSerialContext`
      61             :  * @param result the postgres result
      62             :  * @param num_results the number of results in @a result
      63             :  */
      64             : static void
      65         148 : purse_deposit_serial_helper_cb (void *cls,
      66             :                                 PGresult *result,
      67             :                                 unsigned int num_results)
      68             : {
      69         148 :   struct PurseDepositSerialContext *dsc = cls;
      70         148 :   struct PostgresClosure *pg = dsc->pg;
      71             : 
      72         148 :   for (unsigned int i = 0; i<num_results; i++)
      73             :   {
      74           0 :     struct TALER_EXCHANGEDB_PurseDeposit deposit = {
      75             :       .exchange_base_url = NULL
      76             :     };
      77             :     struct TALER_DenominationPublicKey denom_pub;
      78             :     uint64_t rowid;
      79             :     uint32_t flags32;
      80             :     struct TALER_ReservePublicKeyP reserve_pub;
      81           0 :     bool not_merged = false;
      82             :     struct TALER_Amount purse_balance;
      83             :     struct TALER_Amount purse_total;
      84           0 :     struct GNUNET_PQ_ResultSpec rs[] = {
      85           0 :       TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
      86             :                                    &deposit.amount),
      87           0 :       TALER_PQ_RESULT_SPEC_AMOUNT ("balance",
      88             :                                    &purse_balance),
      89           0 :       TALER_PQ_RESULT_SPEC_AMOUNT ("total",
      90             :                                    &purse_total),
      91           0 :       TALER_PQ_RESULT_SPEC_AMOUNT ("fee_deposit",
      92             :                                    &deposit.deposit_fee),
      93           0 :       GNUNET_PQ_result_spec_allow_null (
      94             :         GNUNET_PQ_result_spec_string ("partner_base_url",
      95             :                                       &deposit.exchange_base_url),
      96             :         NULL),
      97           0 :       GNUNET_PQ_result_spec_allow_null (
      98             :         GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
      99             :                                               &reserve_pub),
     100             :         &not_merged),
     101           0 :       TALER_PQ_result_spec_denom_pub ("denom_pub",
     102             :                                       &denom_pub),
     103           0 :       GNUNET_PQ_result_spec_auto_from_type ("purse_pub",
     104             :                                             &deposit.purse_pub),
     105           0 :       GNUNET_PQ_result_spec_auto_from_type ("coin_sig",
     106             :                                             &deposit.coin_sig),
     107           0 :       GNUNET_PQ_result_spec_uint32 ("flags",
     108             :                                     &flags32),
     109           0 :       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
     110             :                                             &deposit.coin_pub),
     111           0 :       GNUNET_PQ_result_spec_allow_null (
     112             :         GNUNET_PQ_result_spec_auto_from_type ("age_commitment_hash",
     113             :                                               &deposit.h_age_commitment),
     114             :         &deposit.no_age_commitment),
     115           0 :       GNUNET_PQ_result_spec_uint64 ("purse_deposit_serial_id",
     116             :                                     &rowid),
     117             :       GNUNET_PQ_result_spec_end
     118             :     };
     119             :     enum GNUNET_GenericReturnValue ret;
     120             : 
     121           0 :     memset (&deposit,
     122             :             0,
     123             :             sizeof (deposit));
     124           0 :     if (GNUNET_OK !=
     125           0 :         GNUNET_PQ_extract_result (result,
     126             :                                   rs,
     127             :                                   i))
     128             :     {
     129           0 :       GNUNET_break (0);
     130           0 :       dsc->status = GNUNET_SYSERR;
     131           0 :       return;
     132             :     }
     133           0 :     ret = dsc->cb (dsc->cb_cls,
     134             :                    rowid,
     135             :                    &deposit,
     136             :                    not_merged ? NULL : &reserve_pub,
     137             :                    (enum TALER_WalletAccountMergeFlags) flags32,
     138             :                    &purse_balance,
     139             :                    &purse_total,
     140             :                    &denom_pub);
     141           0 :     GNUNET_PQ_cleanup_result (rs);
     142           0 :     if (GNUNET_OK != ret)
     143           0 :       break;
     144             :   }
     145             : }
     146             : 
     147             : 
     148             : enum GNUNET_DB_QueryStatus
     149         148 : TEH_PG_select_purse_deposits_above_serial_id (
     150             :   void *cls,
     151             :   uint64_t serial_id,
     152             :   TALER_EXCHANGEDB_PurseDepositCallback cb,
     153             :   void *cb_cls)
     154             : {
     155         148 :   struct PostgresClosure *pg = cls;
     156         148 :   struct GNUNET_PQ_QueryParam params[] = {
     157         148 :     GNUNET_PQ_query_param_uint64 (&serial_id),
     158             :     GNUNET_PQ_query_param_end
     159             :   };
     160         148 :   struct PurseDepositSerialContext dsc = {
     161             :     .cb = cb,
     162             :     .cb_cls = cb_cls,
     163             :     .pg = pg,
     164             :     .status = GNUNET_OK
     165             :   };
     166             :   enum GNUNET_DB_QueryStatus qs;
     167             : 
     168         148 :   PREPARE (pg,
     169             :            "audit_get_purse_deposits_incr",
     170             :            "SELECT"
     171             :            " pd.amount_with_fee"
     172             :            ",pr.amount_with_fee AS total"
     173             :            ",pr.balance"
     174             :            ",pr.flags"
     175             :            ",pd.purse_pub"
     176             :            ",pd.coin_sig"
     177             :            ",partner_base_url"
     178             :            ",denom.denom_pub"
     179             :            ",denom.fee_deposit"
     180             :            ",pm.reserve_pub"
     181             :            ",kc.coin_pub"
     182             :            ",kc.age_commitment_hash"
     183             :            ",pd.purse_deposit_serial_id"
     184             :            " FROM purse_deposits pd"
     185             :            " LEFT JOIN partners USING (partner_serial_id)"
     186             :            " LEFT JOIN purse_merges pm USING (purse_pub)"
     187             :            " JOIN purse_requests pr USING (purse_pub)"
     188             :            " JOIN known_coins kc USING (coin_pub)"
     189             :            " JOIN denominations denom USING (denominations_serial)"
     190             :            " WHERE ("
     191             :            "  (purse_deposit_serial_id>=$1)"
     192             :            " )"
     193             :            " ORDER BY purse_deposit_serial_id ASC;");
     194         148 :   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
     195             :                                              "audit_get_purse_deposits_incr",
     196             :                                              params,
     197             :                                              &purse_deposit_serial_helper_cb,
     198             :                                              &dsc);
     199         148 :   if (GNUNET_OK != dsc.status)
     200           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     201         148 :   return qs;
     202             : }

Generated by: LCOV version 1.16