merchant

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

select_all_donau_instances.c (5463B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2024, 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_all_donau_instances.c
     18  * @brief Implementation of the select_donau_instance function for Postgres
     19  * @author Bohdan Potuzhnyi
     20  * @author Vlada Svirsh
     21  * @author Christian Grothoff
     22  */
     23 #include "platform.h"
     24 #include <taler/taler_pq_lib.h>
     25 #include "merchant-database/select_all_donau_instances.h"
     26 #include "helper.h"
     27 
     28 /**
     29  * Context for select_donau_instances().
     30  */
     31 struct SelectDonauInstanceContext
     32 {
     33   /**
     34    * Function to call with the results.
     35    */
     36   TALER_MERCHANTDB_DonauInstanceCallback 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     uint64_t donau_instance_serial;
     68     char *donau_url;
     69     char *charity_name;
     70     struct DONAU_CharityPublicKeyP charity_pub_key;
     71     uint64_t charity_id;
     72     struct TALER_Amount charity_max_per_year;
     73     struct TALER_Amount charity_receipts_to_date;
     74     int64_t current_year;
     75     json_t *donau_keys_json = NULL;
     76     struct GNUNET_PQ_ResultSpec rs[] = {
     77       GNUNET_PQ_result_spec_uint64 ("out_donau_instances_serial",
     78                                     &donau_instance_serial),
     79       GNUNET_PQ_result_spec_string ("out_donau_url",
     80                                     &donau_url),
     81       GNUNET_PQ_result_spec_string ("out_charity_name",
     82                                     &charity_name),
     83       GNUNET_PQ_result_spec_auto_from_type ("out_charity_pub_key",
     84                                             &charity_pub_key),
     85       GNUNET_PQ_result_spec_uint64 ("out_charity_id",
     86                                     &charity_id),
     87       TALER_PQ_result_spec_amount_with_currency ("out_charity_max_per_year",
     88                                                  &charity_max_per_year),
     89       TALER_PQ_result_spec_amount_with_currency ("out_charity_receipts_to_date",
     90                                                  &charity_receipts_to_date),
     91       GNUNET_PQ_result_spec_int64 ("out_current_year",
     92                                    &current_year),
     93       GNUNET_PQ_result_spec_allow_null (
     94         TALER_PQ_result_spec_json ("out_keys_json",
     95                                    &donau_keys_json),
     96         NULL),
     97       GNUNET_PQ_result_spec_end
     98     };
     99 
    100     if (GNUNET_OK !=
    101         GNUNET_PQ_extract_result (result,
    102                                   rs,
    103                                   i))
    104     {
    105       GNUNET_break (0);
    106       sdc->extract_failed = true;
    107       return;
    108     }
    109     sdc->cb (sdc->cb_cls,
    110              donau_instance_serial,
    111              donau_url,
    112              charity_name,
    113              &charity_pub_key,
    114              charity_id,
    115              &charity_max_per_year,
    116              &charity_receipts_to_date,
    117              current_year,
    118              donau_keys_json);
    119     GNUNET_PQ_cleanup_result (rs);
    120   }
    121 }
    122 
    123 
    124 enum GNUNET_DB_QueryStatus
    125 TALER_MERCHANTDB_select_all_donau_instances (
    126   struct TALER_MERCHANTDB_PostgresContext *pg,
    127   TALER_MERCHANTDB_DonauInstanceCallback cb,
    128   void *cb_cls)
    129 {
    130   struct SelectDonauInstanceContext sdc = {
    131     .cb = cb,
    132     .cb_cls = cb_cls,
    133     /* Can be overwritten by the select_donau_instance_cb */
    134     .extract_failed = false,
    135   };
    136   struct GNUNET_PQ_QueryParam params[] = {
    137     GNUNET_PQ_query_param_end
    138   };
    139   enum GNUNET_DB_QueryStatus qs;
    140 
    141   check_connection (pg);
    142   PREPARE (pg,
    143            "select_all_donau_instances",
    144            "SELECT"
    145            "  out_donau_instances_serial"
    146            " ,out_donau_url"
    147            " ,out_charity_name"
    148            " ,out_charity_pub_key"
    149            " ,out_charity_id"
    150            " ,out_charity_max_per_year"
    151            " ,out_charity_receipts_to_date"
    152            " ,out_current_year"
    153            " ,out_keys_json::TEXT"
    154            " FROM merchant.select_all_donau_instances()");
    155   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    156                                              "select_all_donau_instances",
    157                                              params,
    158                                              &select_donau_instance_cb,
    159                                              &sdc);
    160 
    161   /* If there was an error inside select_donau_instance_cb, return a hard error. */
    162   if (sdc.extract_failed)
    163     return GNUNET_DB_STATUS_HARD_ERROR;
    164 
    165   return qs;
    166 }