donau

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

pg_get_charities.c (4571B)


      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_get_charities.c
     18  * @brief Implementation of the lookup_donation_unit_key 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_get_charities.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Closure for #get_charities_cb().
     31  */
     32 struct GetCharitiesContext
     33 {
     34   /**
     35    * Function to call per result.
     36    */
     37   DONAUDB_GetCharitiesCallback 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    * Number of results processed.
     51    */
     52   enum GNUNET_DB_QueryStatus qs;
     53 
     54 };
     55 
     56 
     57 /**
     58  * Invoke the callback for each result.
     59  *
     60  * @param cls a `struct MissingWireContext *`
     61  * @param result SQL result
     62  * @param num_results number of rows in @a result
     63  */
     64 static void
     65 get_charities_cb (void *cls,
     66                   PGresult *result,
     67                   unsigned int num_results)
     68 {
     69   struct GetCharitiesContext *ctx = cls;
     70   struct PostgresClosure *pg = ctx->pg;
     71 
     72   for (unsigned int i = 0; i < num_results; i++)
     73   {
     74     uint64_t charity_id;
     75     struct DONAU_CharityPublicKeyP charity_pub;
     76     char *charity_name;
     77     struct TALER_Amount max_per_year;
     78     struct TALER_Amount receipts_to_date;
     79     uint32_t current_year;
     80     struct GNUNET_PQ_ResultSpec rs[] = {
     81       GNUNET_PQ_result_spec_auto_from_type ("charity_pub",
     82                                             &charity_pub),
     83       GNUNET_PQ_result_spec_uint64 ("charity_id",
     84                                     &charity_id),
     85       GNUNET_PQ_result_spec_string ("charity_name",
     86                                     &charity_name),
     87       TALER_PQ_RESULT_SPEC_AMOUNT ("max_per_year",
     88                                    &max_per_year),
     89       GNUNET_PQ_result_spec_uint32 ("current_year",
     90                                     &current_year),
     91       TALER_PQ_RESULT_SPEC_AMOUNT ("receipts_to_date",
     92                                    &receipts_to_date),
     93       GNUNET_PQ_result_spec_end
     94     };
     95     enum GNUNET_GenericReturnValue ret;
     96 
     97     if (GNUNET_OK !=
     98         GNUNET_PQ_extract_result (result,
     99                                   rs,
    100                                   i))
    101     {
    102       GNUNET_break (0);
    103       ctx->qs = GNUNET_DB_STATUS_HARD_ERROR;
    104       return;
    105     }
    106     if (current_year !=
    107         GNUNET_TIME_get_current_year ())
    108       GNUNET_assert (GNUNET_OK ==
    109                      TALER_amount_set_zero (max_per_year.currency,
    110                                             &receipts_to_date));
    111     ctx->qs = i + 1;
    112     ret = ctx->cb (ctx->cb_cls,
    113                    charity_id,
    114                    &charity_pub,
    115                    charity_name,
    116                    &max_per_year,
    117                    current_year,
    118                    &receipts_to_date);
    119     GNUNET_PQ_cleanup_result (rs);
    120     if (GNUNET_OK != ret)
    121       break;
    122   }
    123 }
    124 
    125 
    126 enum GNUNET_DB_QueryStatus
    127 DH_PG_get_charities (void *cls,
    128                      DONAUDB_GetCharitiesCallback cb,
    129                      void *cb_cls)
    130 {
    131   struct PostgresClosure *pg = cls;
    132   struct GNUNET_PQ_QueryParam params[] = {
    133     GNUNET_PQ_query_param_end
    134   };
    135   struct GetCharitiesContext ctx = {
    136     .cb = cb,
    137     .cb_cls = cb_cls,
    138     .pg = pg
    139   };
    140   enum GNUNET_DB_QueryStatus qs;
    141 
    142   PREPARE (pg,
    143            "get_charities",
    144            "SELECT"
    145            " charity_id"
    146            ",charity_pub"
    147            ",charity_name"
    148            ",max_per_year"
    149            ",receipts_to_date"
    150            ",current_year"
    151            " FROM charities");
    152   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    153                                              "get_charities",
    154                                              params,
    155                                              &get_charities_cb,
    156                                              &ctx);
    157   if (qs <= 0)
    158     return qs;
    159   return ctx.qs;
    160 }