LCOV - code coverage report
Current view: top level - auditordb - get_balance.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 90.2 % 51 46
Test Date: 2026-04-14 15:39:31 Functions: 100.0 % 2 2

            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              : /**
      17              :  * @file src/auditordb/get_balance.c
      18              :  * @brief Implementation of the get_balance function for Postgres
      19              :  * @author Christian Grothoff
      20              :  */
      21              : #include "taler/taler_error_codes.h"
      22              : #include "taler/taler_pq_lib.h"
      23              : #include "auditor-database/get_balance.h"
      24              : #include "pg_helper.h"
      25              : 
      26              : 
      27              : /**
      28              :  * Closure for #balance_cb().
      29              :  */
      30              : struct BalanceContext
      31              : {
      32              : 
      33              :   /**
      34              :    * Where to store results.
      35              :    */
      36              :   struct TALER_Amount **dst;
      37              : 
      38              :   /**
      39              :    * Offset in @e dst.
      40              :    */
      41              :   unsigned int off;
      42              : 
      43              :   /**
      44              :    * Length of array at @e dst.
      45              :    */
      46              :   unsigned int len;
      47              : 
      48              :   /**
      49              :    * Plugin context.
      50              :    */
      51              :   struct TALER_AUDITORDB_PostgresContext *pg;
      52              : 
      53              :   /**
      54              :    * Set to true on failure.
      55              :    */
      56              :   bool failure;
      57              : 
      58              : };
      59              : 
      60              : 
      61              : /**
      62              :  * Helper function for #TALER_AUDITORDB_get_balance().
      63              :  * To be called with the results of a SELECT statement
      64              :  * that has returned @a num_results results.
      65              :  *
      66              :  * @param cls closure of type `struct BalanceContext *`
      67              :  * @param result the postgres result
      68              :  * @param num_results the number of results in @a result
      69              :  */
      70              : static void
      71           24 : balance_cb (void *cls,
      72              :             PGresult *result,
      73              :             unsigned int num_results)
      74              : {
      75           24 :   struct BalanceContext *ctx = cls;
      76           24 :   struct TALER_AUDITORDB_PostgresContext *pg = ctx->pg;
      77              : 
      78           24 :   GNUNET_assert (num_results <= ctx->len);
      79          200 :   for (unsigned int i = 0; i < num_results; i++)
      80              :   {
      81              :     bool is_missing;
      82          176 :     struct GNUNET_PQ_ResultSpec rs[] = {
      83          176 :       GNUNET_PQ_result_spec_allow_null (
      84              :         TALER_PQ_result_spec_amount ("balance",
      85          176 :                                      pg->currency,
      86          176 :                                      ctx->dst[i]),
      87              :         &is_missing),
      88              :       GNUNET_PQ_result_spec_end
      89              :     };
      90              : 
      91          176 :     if (GNUNET_OK !=
      92          176 :         GNUNET_PQ_extract_result (result,
      93              :                                   rs,
      94              :                                   i))
      95              :     {
      96            0 :       GNUNET_break (0);
      97            0 :       ctx->failure = true;
      98            0 :       return;
      99              :     }
     100          176 :     if (is_missing)
     101              :     {
     102          173 :       GNUNET_assert (GNUNET_OK ==
     103              :                      TALER_amount_set_zero (pg->currency,
     104              :                                             ctx->dst[i]));
     105              :     }
     106          176 :     ctx->off++;
     107              :   }
     108              : }
     109              : 
     110              : 
     111              : enum GNUNET_DB_QueryStatus
     112           24 : TALER_AUDITORDB_get_balance (
     113              :   struct TALER_AUDITORDB_PostgresContext *pg,
     114              :   const char *balance_key,
     115              :   struct TALER_Amount *balance_value,
     116              :   ...)
     117              : {
     118           24 :   unsigned int cnt = 1;
     119              :   va_list ap;
     120              : 
     121           24 :   GNUNET_assert (GNUNET_OK ==
     122              :                  TALER_amount_set_zero (pg->currency,
     123              :                                         balance_value));
     124           24 :   va_start (ap,
     125              :             balance_value);
     126          176 :   while (NULL != va_arg (ap,
     127              :                          const char *))
     128              :   {
     129              :     struct TALER_Amount *dst;
     130              : 
     131          152 :     cnt++;
     132          152 :     dst = va_arg (ap,
     133              :                   struct TALER_Amount *);
     134          152 :     GNUNET_assert (GNUNET_OK ==
     135              :                    TALER_amount_set_zero (pg->currency,
     136              :                                           dst));
     137              :   }
     138           24 :   va_end (ap);
     139           24 :   {
     140           24 :     const char *keys[cnt];
     141           24 :     struct TALER_Amount *dsts[cnt];
     142           24 :     unsigned int off = 1;
     143           24 :     struct GNUNET_PQ_QueryParam params[] = {
     144           24 :       GNUNET_PQ_query_param_array_ptrs_string (cnt,
     145              :                                                keys,
     146              :                                                pg->conn),
     147              :       GNUNET_PQ_query_param_end
     148              :     };
     149           24 :     struct BalanceContext ctx = {
     150              :       .dst = dsts,
     151              :       .len = cnt,
     152              :       .pg = pg
     153              :     };
     154              :     enum GNUNET_DB_QueryStatus qs;
     155              : 
     156           24 :     keys[0] = balance_key;
     157           24 :     dsts[0] = balance_value;
     158           24 :     va_start (ap,
     159              :               balance_value);
     160          176 :     while (off < cnt)
     161              :     {
     162          152 :       keys[off] = va_arg (ap,
     163              :                           const char *);
     164          152 :       dsts[off] = va_arg (ap,
     165              :                           struct TALER_Amount *);
     166          152 :       off++;
     167              :     }
     168           24 :     GNUNET_assert (NULL == va_arg (ap,
     169              :                                    const char *));
     170           24 :     va_end (ap);
     171              : 
     172           24 :     PREPARE (pg,
     173              :              "get_balance",
     174              :              "SELECT "
     175              :              " auditor_do_get_balance AS balance"
     176              :              " FROM auditor_do_get_balance "
     177              :              "($1);");
     178           24 :     qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
     179              :                                                "get_balance",
     180              :                                                params,
     181              :                                                &balance_cb,
     182              :                                                &ctx);
     183           24 :     GNUNET_PQ_cleanup_query_params_closures (params);
     184           24 :     if (ctx.failure)
     185            0 :       return GNUNET_DB_STATUS_HARD_ERROR;
     186           24 :     if (qs < 0)
     187            0 :       return qs;
     188           24 :     GNUNET_assert (qs == ctx.off);
     189           24 :     return qs;
     190              :   }
     191              : }
        

Generated by: LCOV version 2.0-1