donau

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

iterate_donation_units.c (4325B)


      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/donaudb/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 "iterate_donation_units.h"
     26 #include "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 DONAUDB_PostgresContext *ctx;
     48 
     49   /**
     50    * Set to #GNUNET_SYSERR if extracting a row failed.
     51    */
     52   enum GNUNET_GenericReturnValue status;
     53 
     54 };
     55 
     56 /**
     57  * Invoke the callback for each result.
     58  *
     59  * @param cls a `struct IterateDonationUnitsContext *`
     60  * @param result SQL result
     61  * @param num_results number of rows in @a result
     62  */
     63 static void
     64 iterate_donation_units_cb (void *cls,
     65                            PGresult *result,
     66                            unsigned int num_results)
     67 {
     68   struct IterateDonationUnitsContext *ictx = cls;
     69   struct DONAUDB_PostgresContext *ctx = ictx->ctx;
     70 
     71   for (unsigned int i = 0; i < num_results; i++)
     72   {
     73     struct DONAU_DonationUnitHashP h_donation_unit_pub;
     74     struct DONAU_DonationUnitPublicKey donation_unit_pub;
     75     uint64_t validity_year;
     76     struct TALER_Amount value;
     77     enum GNUNET_GenericReturnValue iret;
     78 
     79     struct GNUNET_PQ_ResultSpec rs[] = {
     80       GNUNET_PQ_result_spec_auto_from_type ("h_donation_unit_pub",
     81                                             &h_donation_unit_pub),
     82       DONAU_PQ_result_spec_donation_unit_pub ("donation_unit_pub",
     83                                               &donation_unit_pub),
     84       GNUNET_PQ_result_spec_uint64 ("validity_year",
     85                                     &validity_year),
     86       TALER_PQ_RESULT_SPEC_AMOUNT ("value",
     87                                    &value),
     88       GNUNET_PQ_result_spec_end
     89     };
     90 
     91     if (GNUNET_OK !=
     92         GNUNET_PQ_extract_result (result,
     93                                   rs,
     94                                   i))
     95     {
     96       GNUNET_break (0);
     97       ictx->status = GNUNET_SYSERR;
     98       return;
     99     }
    100 
    101     iret = ictx->cb (ictx->cb_cls,
    102                      &h_donation_unit_pub,
    103                      &donation_unit_pub,
    104                      validity_year,
    105                      &value);
    106     GNUNET_PQ_cleanup_result (rs);
    107     if (GNUNET_OK != iret)
    108       break;
    109   }
    110 }
    111 
    112 
    113 enum GNUNET_DB_QueryStatus
    114 DONAUDB_iterate_donation_units (struct DONAUDB_PostgresContext *ctx,
    115                                 DONAUDB_IterateDonationUnitsCallback cb,
    116                                 void *cb_cls)
    117 {
    118   struct GNUNET_PQ_QueryParam params[] = {
    119     GNUNET_PQ_query_param_end
    120   };
    121   struct IterateDonationUnitsContext ictx = {
    122     .cb = cb,
    123     .cb_cls = cb_cls,
    124     .ctx = ctx,
    125     .status = GNUNET_OK
    126   };
    127 
    128   PREPARE (ctx,
    129            "iterate_donation_units",
    130            "SELECT"
    131            " h_donation_unit_pub"
    132            ",donation_unit_pub"
    133            ",validity_year"
    134            ",value"
    135            " FROM donation_units");
    136   {
    137     enum GNUNET_DB_QueryStatus qs;
    138 
    139     qs = GNUNET_PQ_eval_prepared_multi_select (ctx->conn,
    140                                                "iterate_donation_units",
    141                                                params,
    142                                                &iterate_donation_units_cb,
    143                                                &ictx);
    144     if (GNUNET_OK != ictx.status)
    145       return GNUNET_DB_STATUS_HARD_ERROR;
    146     return qs;
    147   }
    148 }