LCOV - code coverage report
Current view: top level - auditordb - pg_get_balances.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 25 29 86.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) 2024 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             : #include "platform.h"
      17             : #include "taler_error_codes.h"
      18             : #include "taler_dbevents.h"
      19             : #include "taler_pq_lib.h"
      20             : #include "pg_helper.h"
      21             : #include "pg_get_balances.h"
      22             : 
      23             : 
      24             : struct BalancesContext
      25             : {
      26             : 
      27             :   /**
      28             :    * Function to call for each bad sig loss.
      29             :    */
      30             :   TALER_AUDITORDB_BalancesCallback cb;
      31             : 
      32             :   /**
      33             :    * Closure for @e cb
      34             :    */
      35             :   void *cb_cls;
      36             : 
      37             :   /**
      38             :    * Plugin context.
      39             :    */
      40             :   struct PostgresClosure *pg;
      41             : 
      42             :   /**
      43             :    * Query status to return.
      44             :    */
      45             :   enum GNUNET_DB_QueryStatus qs;
      46             : };
      47             : 
      48             : 
      49             : /**
      50             :  * Helper function for #TAH_PG_get_balances().
      51             :  * To be called with the results of a SELECT statement
      52             :  * that has returned @a num_results results.
      53             :  *
      54             :  * @param cls closure of type `struct BalancesContext *`
      55             :  * @param result the postgres result
      56             :  * @param num_results the number of results in @a result
      57             :  */
      58             : static void
      59          85 : balances_cb (void *cls,
      60             :              PGresult *result,
      61             :              unsigned int num_results)
      62             : {
      63          85 :   struct BalancesContext *dcc = cls;
      64          85 :   struct PostgresClosure *pg = dcc->pg;
      65             : 
      66         467 :   for (unsigned int i = 0; i < num_results; i++)
      67             :   {
      68             :     struct TALER_AUDITORDB_Balances dc;
      69         382 :     struct GNUNET_PQ_ResultSpec rs[] = {
      70         382 :       GNUNET_PQ_result_spec_string ("balance_key",
      71             :                                     &dc.balance_key),
      72         382 :       TALER_PQ_RESULT_SPEC_AMOUNT ("balance_value",
      73             :                                    &dc.balance_value),
      74             :       GNUNET_PQ_result_spec_end
      75             :     };
      76             :     enum GNUNET_GenericReturnValue rval;
      77             : 
      78         382 :     if (GNUNET_OK !=
      79         382 :         GNUNET_PQ_extract_result (result,
      80             :                                   rs,
      81             :                                   i))
      82             :     {
      83           0 :       GNUNET_break (0);
      84           0 :       dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
      85           0 :       return;
      86             :     }
      87         382 :     dcc->qs = i + 1;
      88         382 :     rval = dcc->cb (dcc->cb_cls,
      89             :                     &dc);
      90         382 :     GNUNET_PQ_cleanup_result (rs);
      91         382 :     if (GNUNET_OK != rval)
      92           0 :       break;
      93             :   }
      94             : }
      95             : 
      96             : 
      97             : enum GNUNET_DB_QueryStatus
      98          85 : TAH_PG_get_balances (
      99             :   void *cls,
     100             :   const char *balance_key,
     101             :   TALER_AUDITORDB_BalancesCallback cb,
     102             :   void *cb_cls)
     103             : {
     104          85 :   struct PostgresClosure *pg = cls;
     105          85 :   struct GNUNET_PQ_QueryParam params[] = {
     106             :     NULL == balance_key
     107           6 :     ? GNUNET_PQ_query_param_null ()
     108          85 :     : GNUNET_PQ_query_param_string (balance_key),
     109             :     GNUNET_PQ_query_param_end
     110             :   };
     111          85 :   struct BalancesContext dcc = {
     112             :     .cb = cb,
     113             :     .cb_cls = cb_cls,
     114             :     .pg = pg
     115             :   };
     116             :   enum GNUNET_DB_QueryStatus qs;
     117             : 
     118          85 :   PREPARE (pg,
     119             :            "auditor_balances_get",
     120             :            "SELECT"
     121             :            " balance_key"
     122             :            ",balance_value"
     123             :            " FROM auditor_balances"
     124             :            " WHERE ($1::TEXT IS NULL OR balance_key = $1)"
     125             :            );
     126          85 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     127             :     pg->conn,
     128             :     "auditor_balances_get",
     129             :     params,
     130             :     &balances_cb,
     131             :     &dcc);
     132          85 :   if (qs > 0)
     133          84 :     return dcc.qs;
     134           1 :   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
     135           1 :   return qs;
     136             : }

Generated by: LCOV version 1.16