merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

iterate_fountain_grants.c (4799B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2026 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/backenddb/iterate_fountain_grants.c
     18  * @brief Implementation of the iterate_fountain_grants function for Postgres
     19  * @author Bohdan Potuzhnyi
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/iterate_fountain_grants.h"
     24 #include "helper.h"
     25 
     26 
     27 /**
     28  * Context used for TALER_MERCHANTDB_iterate_fountain_grants().
     29  */
     30 struct LookupFountainGrantsContext
     31 {
     32   /**
     33    * Function to call with the results.
     34    */
     35   TALER_MERCHANTDB_FountainGrantsCallback cb;
     36 
     37   /**
     38    * Closure for @a cb.
     39    */
     40   void *cb_cls;
     41 
     42   /**
     43    * Did database result extraction fail?
     44    */
     45   bool extract_failed;
     46 };
     47 
     48 
     49 /**
     50  * Function to be called with the results of a SELECT statement
     51  * that has returned @a num_results results about fountain grants.
     52  *
     53  * @param[in,out] cls of type `struct LookupFountainGrantsContext *`
     54  * @param result the postgres result
     55  * @param num_results the number of results in @a result
     56  */
     57 static void
     58 lookup_fountain_grants_cb (void *cls,
     59                            PGresult *result,
     60                            unsigned int num_results)
     61 {
     62   struct LookupFountainGrantsContext *lgc = cls;
     63 
     64   for (unsigned int i = 0; i < num_results; i++)
     65   {
     66     char *token_family_slug;
     67     uint64_t token_family_serial;
     68     uint64_t tokens_per_period_limit;
     69     uint64_t tokens_per_period_stash;
     70     uint64_t key_window_size;
     71     struct GNUNET_PQ_ResultSpec rs[] = {
     72       GNUNET_PQ_result_spec_string ("slug",
     73                                     &token_family_slug),
     74       GNUNET_PQ_result_spec_uint64 ("token_family_serial",
     75                                     &token_family_serial),
     76       GNUNET_PQ_result_spec_uint64 ("tokens_per_period_limit",
     77                                     &tokens_per_period_limit),
     78       GNUNET_PQ_result_spec_uint64 ("tokens_per_period_stash",
     79                                     &tokens_per_period_stash),
     80       GNUNET_PQ_result_spec_uint64 ("key_window_size",
     81                                     &key_window_size),
     82       GNUNET_PQ_result_spec_end
     83     };
     84 
     85     if (GNUNET_OK !=
     86         GNUNET_PQ_extract_result (result,
     87                                   rs,
     88                                   i))
     89     {
     90       GNUNET_break (0);
     91       lgc->extract_failed = true;
     92       return;
     93     }
     94     lgc->cb (lgc->cb_cls,
     95              token_family_slug,
     96              token_family_serial,
     97              tokens_per_period_limit,
     98              tokens_per_period_stash,
     99              (uint32_t) key_window_size);
    100     GNUNET_PQ_cleanup_result (rs);
    101   }
    102 }
    103 
    104 
    105 enum GNUNET_DB_QueryStatus
    106 TALER_MERCHANTDB_iterate_fountain_grants (
    107   struct TALER_MERCHANTDB_PostgresContext *pg,
    108   const char *instance_id,
    109   uint64_t fountain_serial,
    110   TALER_MERCHANTDB_FountainGrantsCallback cb,
    111   void *cb_cls)
    112 {
    113   struct LookupFountainGrantsContext lgc = {
    114     .cb = cb,
    115     .cb_cls = cb_cls,
    116     .extract_failed = false,
    117   };
    118   struct GNUNET_PQ_QueryParam params[] = {
    119     GNUNET_PQ_query_param_uint64 (&fountain_serial),
    120     GNUNET_PQ_query_param_end
    121   };
    122   enum GNUNET_DB_QueryStatus qs;
    123 
    124   GNUNET_assert (NULL != pg->current_merchant_id);
    125   GNUNET_assert (0 == strcmp (instance_id,
    126                               pg->current_merchant_id));
    127   TMH_PQ_prepare_anon (pg,
    128                        "SELECT"
    129                        " tf.slug"
    130                        ",fg.token_family_serial"
    131                        ",fg.tokens_per_period_limit"
    132                        ",fg.tokens_per_period_stash"
    133                        ",fg.key_window_size"
    134                        " FROM merchant_fountain_grants fg"
    135                        " JOIN merchant_token_families tf"
    136                        "   USING (token_family_serial)"
    137                        " WHERE fg.fountain_serial=$1"
    138                        " ORDER BY tf.slug ASC");
    139   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    140                                              "",
    141                                              params,
    142                                              &lookup_fountain_grants_cb,
    143                                              &lgc);
    144   if (lgc.extract_failed)
    145     return GNUNET_DB_STATUS_HARD_ERROR;
    146   return qs;
    147 }