merchant

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

pg_select_donau_instances_filtered.c (3763B)


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