exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

get_balances.c (3435B)


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