donau

Donation authority for GNU Taler (experimental)
Log | Files | Refs | Submodules | README | LICENSE

pg_iterate_donation_units.c (3987B)


      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 donaudb/pg_iterate_donation_units.c
     18  * @brief Implementation of the iterate_donation_units function for Postgres
     19  * @author Johannes Casaburi
     20  */
     21 #include <donau_config.h>
     22 #include <taler/taler_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "pg_iterate_donation_units.h"
     26 #include "pg_helper.h"
     27 #include "donau_pq_lib.h"
     28 
     29 /**
     30  * Closure for #get_donation_units_cb().
     31  */
     32 struct IterateDonationUnitsContext
     33 {
     34   /**
     35    * Function to call per result.
     36    */
     37   DONAUDB_IterateDonationUnitsCallback 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 
     51 /**
     52  * Invoke the callback for each result.
     53  *
     54  * @param cls a `struct MissingWireContext *`
     55  * @param result SQL result
     56  * @param num_results number of rows in @a result
     57  */
     58 static void
     59 iterate_donation_units_cb (void *cls,
     60                            PGresult *result,
     61                            unsigned int num_results)
     62 {
     63   struct IterateDonationUnitsContext *ctx = cls;
     64   struct PostgresClosure *pg = ctx->pg;
     65 
     66   for (unsigned int i = 0; i < num_results; i++)
     67   {
     68     struct DONAU_DonationUnitHashP h_donation_unit_pub;
     69     struct DONAU_DonationUnitPublicKey donation_unit_pub;
     70     uint64_t validity_year;
     71     struct TALER_Amount value;
     72     enum GNUNET_GenericReturnValue iret;
     73 
     74     struct GNUNET_PQ_ResultSpec rs[] = {
     75       GNUNET_PQ_result_spec_auto_from_type ("h_donation_unit_pub",
     76                                             &h_donation_unit_pub),
     77       DONAU_PQ_result_spec_donation_unit_pub ("donation_unit_pub",
     78                                               &donation_unit_pub),
     79       GNUNET_PQ_result_spec_uint64 ("validity_year",
     80                                     &validity_year),
     81       TALER_PQ_RESULT_SPEC_AMOUNT ("value",
     82                                    &value),
     83       GNUNET_PQ_result_spec_end
     84     };
     85 
     86     if (GNUNET_OK !=
     87         GNUNET_PQ_extract_result (result,
     88                                   rs,
     89                                   i))
     90     {
     91       GNUNET_break (0);
     92       return;
     93     }
     94 
     95     iret = ctx->cb (ctx->cb_cls,
     96                     &h_donation_unit_pub,
     97                     &donation_unit_pub,
     98                     validity_year,
     99                     &value);
    100     GNUNET_PQ_cleanup_result (rs);
    101     if (GNUNET_OK != iret)
    102       break;
    103   }
    104 }
    105 
    106 
    107 enum GNUNET_DB_QueryStatus
    108 DH_PG_iterate_donation_units (void *cls,
    109                               DONAUDB_IterateDonationUnitsCallback cb,
    110                               void *cb_cls)
    111 {
    112   struct PostgresClosure *pg = cls;
    113   struct GNUNET_PQ_QueryParam params[] = {
    114     GNUNET_PQ_query_param_end
    115   };
    116   struct IterateDonationUnitsContext ctx = {
    117     .cb = cb,
    118     .cb_cls = cb_cls,
    119     .pg = pg
    120   };
    121 
    122   PREPARE (pg,
    123            "iterate_donation_units",
    124            "SELECT"
    125            " h_donation_unit_pub"
    126            ",donation_unit_pub"
    127            ",validity_year"
    128            ",value"
    129            " FROM donation_units");
    130   return GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    131                                                "iterate_donation_units",
    132                                                params,
    133                                                &iterate_donation_units_cb,
    134                                                &ctx);
    135 }