donau

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

get_history.c (3613B)


      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/get_history.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 "get_history.h"
     26 #include "helper.h"
     27 
     28 
     29 /**
     30  * Closure for #get_history_cb().
     31  */
     32 struct GetHistoryContext
     33 {
     34   /**
     35    * Function to call per result.
     36    */
     37   DONAUDB_GetHistoryCallback 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    * 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 GetHistoryContext *`
     61  * @param result SQL result
     62  * @param num_results number of rows in @a result
     63  */
     64 static void
     65 get_history_cb (void *cls,
     66                 PGresult *result,
     67                 unsigned int num_results)
     68 {
     69   struct GetHistoryContext *gctx = cls;
     70   struct DONAUDB_PostgresContext *ctx = gctx->ctx;
     71 
     72   for (unsigned int i = 0; i < num_results; i++)
     73   {
     74     uint64_t charity_id;
     75     struct TALER_Amount final_amount;
     76     uint64_t donation_year;
     77 
     78     struct GNUNET_PQ_ResultSpec rs[] = {
     79       GNUNET_PQ_result_spec_uint64 ("charity_id",
     80                                     &charity_id),
     81       TALER_PQ_RESULT_SPEC_AMOUNT ("final_amount",
     82                                    &final_amount),
     83       GNUNET_PQ_result_spec_uint64 ("donation_year",
     84                                     &donation_year),
     85       GNUNET_PQ_result_spec_end
     86     };
     87 
     88     if (GNUNET_OK !=
     89         GNUNET_PQ_extract_result (result,
     90                                   rs,
     91                                   i))
     92     {
     93       GNUNET_break (0);
     94       gctx->qs = GNUNET_DB_STATUS_HARD_ERROR;
     95       return;
     96     }
     97 
     98     gctx->qs = i + 1;
     99     if (GNUNET_OK !=
    100         gctx->cb (gctx->cb_cls,
    101                   charity_id,
    102                   final_amount,
    103                   donation_year))
    104       break;
    105   }
    106 }
    107 
    108 
    109 enum GNUNET_DB_QueryStatus
    110 DONAUDB_get_history (struct DONAUDB_PostgresContext *ctx,
    111                      DONAUDB_GetHistoryCallback cb,
    112                      void *cb_cls)
    113 {
    114   struct GNUNET_PQ_QueryParam params[] = {
    115     GNUNET_PQ_query_param_end
    116   };
    117   struct GetHistoryContext gctx = {
    118     .cb = cb,
    119     .cb_cls = cb_cls,
    120     .ctx = ctx
    121   };
    122   enum GNUNET_DB_QueryStatus qs;
    123 
    124   PREPARE (ctx,
    125            "get_history",
    126            "SELECT"
    127            " charity_id"
    128            ",final_amount"
    129            ",donation_year"
    130            " FROM history");
    131   qs = GNUNET_PQ_eval_prepared_multi_select (ctx->conn,
    132                                              "get_history",
    133                                              params,
    134                                              &get_history_cb,
    135                                              &gctx);
    136   if (qs <= 0)
    137     return qs;
    138   return gctx.qs;
    139 }