merchant

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

select_donau_instances_filtered.c (3638B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2025 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/select_donau_instances_filtered.c
     18  * @brief Implementation of the select_donau_instances_filtered function for Postgres
     19  * @author Bohdan Potuzhnyi
     20  * @author Vlada Svirsh
     21  */
     22 #include "platform.h"
     23 #include <taler/taler_pq_lib.h>
     24 #include "merchant-database/select_donau_instances_filtered.h"
     25 #include "helper.h"
     26 
     27 
     28 /**
     29  * Context for select_donau_instances_filtered().
     30  */
     31 struct SelectDonauInstanceContext
     32 {
     33   /**
     34    * Function to call with the results.
     35    */
     36   TALER_MERCHANTDB_DonauInstanceFilteredCallback cb;
     37 
     38   /**
     39    * Closure for @e cb.
     40    */
     41   void *cb_cls;
     42 
     43   /**
     44    * Did database result extraction fail?
     45    */
     46   bool extract_failed;
     47 };
     48 
     49 
     50 /**
     51  * Function to be called with the results of a SELECT statement
     52  * that has returned @a num_results results about donau instances.
     53  *
     54  * @param[in, out] cls of type `struct SelectDonauInstanceContext *`
     55  * @param result the postgres result
     56  * @param num_results the number of results in @a result
     57  */
     58 static void
     59 select_donau_instance_cb (void *cls,
     60                           PGresult *result,
     61                           unsigned int num_results)
     62 {
     63   struct SelectDonauInstanceContext *sdc = cls;
     64 
     65   for (unsigned int i = 0; i < num_results; i++)
     66   {
     67     char *donau_url;
     68     struct GNUNET_PQ_ResultSpec rs[] = {
     69       GNUNET_PQ_result_spec_string ("donau_url",
     70                                     &donau_url),
     71       GNUNET_PQ_result_spec_end
     72     };
     73 
     74     if (GNUNET_OK !=
     75         GNUNET_PQ_extract_result (result,
     76                                   rs,
     77                                   i))
     78     {
     79       GNUNET_break (0);
     80       sdc->extract_failed = true;
     81       return;
     82     }
     83     sdc->cb (sdc->cb_cls,
     84              donau_url);
     85     GNUNET_PQ_cleanup_result (rs);
     86   }
     87 }
     88 
     89 
     90 enum GNUNET_DB_QueryStatus
     91 TALER_MERCHANTDB_select_donau_instances_filtered (
     92   struct TALER_MERCHANTDB_PostgresContext *pg,
     93   const char *currency,
     94   TALER_MERCHANTDB_DonauInstanceFilteredCallback cb,
     95   void *cb_cls)
     96 {
     97   struct SelectDonauInstanceContext sdc = {
     98     .cb = cb,
     99     .cb_cls = cb_cls,
    100     /* Can be overwritten by the select_donau_instance_cb */
    101     .extract_failed = false,
    102   };
    103   struct GNUNET_PQ_QueryParam params[] = {
    104     GNUNET_PQ_query_param_string (currency),
    105     GNUNET_PQ_query_param_end
    106   };
    107   enum GNUNET_DB_QueryStatus qs;
    108 
    109   TMH_PQ_prepare_anon (pg,
    110                        "SELECT donau_url"
    111                        "  FROM merchant_donau_instances"
    112                        " WHERE (charity_max_per_year).curr = $1");
    113   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    114                                              "",
    115                                              params,
    116                                              &select_donau_instance_cb,
    117                                              &sdc);
    118 
    119   /* If there was an error inside select_donau_instance_cb, return a hard error. */
    120   if (sdc.extract_failed)
    121     return GNUNET_DB_STATUS_HARD_ERROR;
    122   return qs;
    123 }