exchange

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

pg_get_wire_accounts.c (5027B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022, 2024 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_wire_accounts.c
     18  * @brief Implementation of the get_wire_accounts 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_wire_accounts.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Closure for #get_wire_accounts_cb().
     31  */
     32 struct GetWireAccountsContext
     33 {
     34   /**
     35    * Function to call per result.
     36    */
     37   TALER_EXCHANGEDB_WireAccountCallback cb;
     38 
     39   /**
     40    * Closure for @e cb.
     41    */
     42   void *cb_cls;
     43 
     44   /**
     45    * Flag set to #GNUNET_OK as long as everything is fine.
     46    */
     47   enum GNUNET_GenericReturnValue status;
     48 
     49 };
     50 
     51 
     52 /**
     53  * Invoke the callback for each result.
     54  *
     55  * @param cls a `struct MissingWireContext *`
     56  * @param result SQL result
     57  * @param num_results number of rows in @a result
     58  */
     59 static void
     60 get_wire_accounts_cb (void *cls,
     61                       PGresult *result,
     62                       unsigned int num_results)
     63 {
     64   struct GetWireAccountsContext *ctx = cls;
     65 
     66   for (unsigned int i = 0; i < num_results; i++)
     67   {
     68     struct TALER_FullPayto payto_uri;
     69     char *conversion_url = NULL;
     70     json_t *debit_restrictions = NULL;
     71     json_t *credit_restrictions = NULL;
     72     struct TALER_MasterSignatureP master_sig;
     73     char *bank_label = NULL;
     74     int64_t priority;
     75     struct GNUNET_PQ_ResultSpec rs[] = {
     76       GNUNET_PQ_result_spec_string ("payto_uri",
     77                                     &payto_uri.full_payto),
     78       GNUNET_PQ_result_spec_allow_null (
     79         GNUNET_PQ_result_spec_string ("conversion_url",
     80                                       &conversion_url),
     81         NULL),
     82       GNUNET_PQ_result_spec_allow_null (
     83         GNUNET_PQ_result_spec_string ("bank_label",
     84                                       &bank_label),
     85         NULL),
     86       GNUNET_PQ_result_spec_int64 ("priority",
     87                                    &priority),
     88       GNUNET_PQ_result_spec_allow_null (
     89         TALER_PQ_result_spec_json ("debit_restrictions",
     90                                    &debit_restrictions),
     91         NULL),
     92       GNUNET_PQ_result_spec_allow_null (
     93         TALER_PQ_result_spec_json ("credit_restrictions",
     94                                    &credit_restrictions),
     95         NULL),
     96       GNUNET_PQ_result_spec_auto_from_type ("master_sig",
     97                                             &master_sig),
     98       GNUNET_PQ_result_spec_end
     99     };
    100 
    101     if (GNUNET_OK !=
    102         GNUNET_PQ_extract_result (result,
    103                                   rs,
    104                                   i))
    105     {
    106       GNUNET_break (0);
    107       ctx->status = GNUNET_SYSERR;
    108       return;
    109     }
    110     if (NULL == debit_restrictions)
    111     {
    112       debit_restrictions = json_array ();
    113       GNUNET_assert (NULL != debit_restrictions);
    114     }
    115     if (NULL == credit_restrictions)
    116     {
    117       credit_restrictions = json_array ();
    118       GNUNET_assert (NULL != credit_restrictions);
    119     }
    120     ctx->cb (ctx->cb_cls,
    121              payto_uri,
    122              conversion_url,
    123              debit_restrictions,
    124              credit_restrictions,
    125              &master_sig,
    126              bank_label,
    127              priority);
    128     GNUNET_PQ_cleanup_result (rs);
    129   }
    130 }
    131 
    132 
    133 enum GNUNET_DB_QueryStatus
    134 TEH_PG_get_wire_accounts (void *cls,
    135                           TALER_EXCHANGEDB_WireAccountCallback cb,
    136                           void *cb_cls)
    137 {
    138   struct PostgresClosure *pg = cls;
    139   struct GetWireAccountsContext ctx = {
    140     .cb = cb,
    141     .cb_cls = cb_cls,
    142     .status = GNUNET_OK
    143   };
    144   struct GNUNET_PQ_QueryParam params[] = {
    145     GNUNET_PQ_query_param_end
    146   };
    147   enum GNUNET_DB_QueryStatus qs;
    148 
    149   PREPARE (pg,
    150            "get_wire_accounts",
    151            "SELECT"
    152            " payto_uri"
    153            ",conversion_url"
    154            ",debit_restrictions::TEXT"
    155            ",credit_restrictions::TEXT"
    156            ",master_sig"
    157            ",bank_label"
    158            ",priority"
    159            " FROM wire_accounts"
    160            " WHERE is_active");
    161   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    162                                              "get_wire_accounts",
    163                                              params,
    164                                              &get_wire_accounts_cb,
    165                                              &ctx);
    166   if (GNUNET_OK != ctx.status)
    167     return GNUNET_DB_STATUS_HARD_ERROR;
    168   return qs;
    169 }