exchange

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

pg_get_global_fees.c (5087B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022 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 exchangedb/pg_get_global_fees.c
     18  * @brief Implementation of the get_global_fees function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/platform.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_global_fees.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Closure for #global_fees_cb().
     31  */
     32 struct GlobalFeeContext
     33 {
     34   /**
     35    * Function to call for each global fee block.
     36    */
     37   TALER_EXCHANGEDB_GlobalFeeCallback cb;
     38 
     39   /**
     40    * Closure to give to @e rec.
     41    */
     42   void *cb_cls;
     43 
     44   /**
     45    * Plugin context.
     46    */
     47   struct PostgresClosure *pg;
     48 
     49   /**
     50    * Set to #GNUNET_SYSERR on error.
     51    */
     52   enum GNUNET_GenericReturnValue status;
     53 };
     54 
     55 
     56 /**
     57  * Function to be called with the results of a SELECT statement
     58  * that has returned @a num_results results.
     59  *
     60  * @param cls closure
     61  * @param result the postgres result
     62  * @param num_results the number of results in @a result
     63  */
     64 static void
     65 global_fees_cb (void *cls,
     66                 PGresult *result,
     67                 unsigned int num_results)
     68 {
     69   struct GlobalFeeContext *gctx = cls;
     70   struct PostgresClosure *pg = gctx->pg;
     71 
     72   for (unsigned int i = 0; i<num_results; i++)
     73   {
     74     struct TALER_GlobalFeeSet fees;
     75     struct GNUNET_TIME_Relative purse_timeout;
     76     struct GNUNET_TIME_Relative history_expiration;
     77     uint32_t purse_account_limit;
     78     struct GNUNET_TIME_Timestamp start_date;
     79     struct GNUNET_TIME_Timestamp end_date;
     80     struct TALER_MasterSignatureP master_sig;
     81     struct GNUNET_PQ_ResultSpec rs[] = {
     82       GNUNET_PQ_result_spec_timestamp ("start_date",
     83                                        &start_date),
     84       GNUNET_PQ_result_spec_timestamp ("end_date",
     85                                        &end_date),
     86       TALER_PQ_RESULT_SPEC_AMOUNT ("history_fee",
     87                                    &fees.history),
     88       TALER_PQ_RESULT_SPEC_AMOUNT ("account_fee",
     89                                    &fees.account),
     90       TALER_PQ_RESULT_SPEC_AMOUNT ("purse_fee",
     91                                    &fees.purse),
     92       GNUNET_PQ_result_spec_relative_time ("purse_timeout",
     93                                            &purse_timeout),
     94       GNUNET_PQ_result_spec_relative_time ("history_expiration",
     95                                            &history_expiration),
     96       GNUNET_PQ_result_spec_uint32 ("purse_account_limit",
     97                                     &purse_account_limit),
     98       GNUNET_PQ_result_spec_auto_from_type ("master_sig",
     99                                             &master_sig),
    100       GNUNET_PQ_result_spec_end
    101     };
    102     if (GNUNET_OK !=
    103         GNUNET_PQ_extract_result (result,
    104                                   rs,
    105                                   i))
    106     {
    107       GNUNET_break (0);
    108       gctx->status = GNUNET_SYSERR;
    109       break;
    110     }
    111     gctx->cb (gctx->cb_cls,
    112               &fees,
    113               purse_timeout,
    114               history_expiration,
    115               purse_account_limit,
    116               start_date,
    117               end_date,
    118               &master_sig);
    119     GNUNET_PQ_cleanup_result (rs);
    120   }
    121 }
    122 
    123 
    124 enum GNUNET_DB_QueryStatus
    125 TEH_PG_get_global_fees (void *cls,
    126                         TALER_EXCHANGEDB_GlobalFeeCallback cb,
    127                         void *cb_cls)
    128 {
    129   struct PostgresClosure *pg = cls;
    130   struct GNUNET_TIME_Timestamp date
    131     = GNUNET_TIME_absolute_to_timestamp (
    132         GNUNET_TIME_absolute_subtract (
    133           GNUNET_TIME_absolute_get (),
    134           GNUNET_TIME_UNIT_YEARS));
    135   struct GNUNET_PQ_QueryParam params[] = {
    136     GNUNET_PQ_query_param_timestamp (&date),
    137     GNUNET_PQ_query_param_end
    138   };
    139   struct GlobalFeeContext gctx = {
    140     .cb = cb,
    141     .cb_cls = cb_cls,
    142     .pg = pg,
    143     .status = GNUNET_OK
    144   };
    145 
    146   PREPARE (pg,
    147            "get_global_fees",
    148            "SELECT "
    149            " start_date"
    150            ",end_date"
    151            ",history_fee"
    152            ",account_fee"
    153            ",purse_fee"
    154            ",purse_timeout"
    155            ",history_expiration"
    156            ",purse_account_limit"
    157            ",master_sig"
    158            " FROM global_fee"
    159            " WHERE start_date >= $1");
    160   return GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    161                                                "get_global_fees",
    162                                                params,
    163                                                &global_fees_cb,
    164                                                &gctx);
    165 }