exchange

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

iterate_reserve_close_info.c (3579B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022 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 iterate_reserve_close_info.c
     18  * @brief Low-level (statement-level) Postgres database access for the exchange
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "exchange-database/iterate_reserve_close_info.h"
     23 #include "helper.h"
     24 
     25 /**
     26  * Closure for #iterate_reserve_close_info_cb()
     27  */
     28 struct IteratorContext
     29 {
     30   /**
     31    * Function to call with the results.
     32    */
     33   TALER_KYCLOGIC_KycAmountCallback cb;
     34 
     35   /**
     36    * Closure to pass to @e cb
     37    */
     38   void *cb_cls;
     39 
     40   /**
     41    * Plugin context.
     42    */
     43   struct TALER_EXCHANGEDB_PostgresContext *pg;
     44 };
     45 
     46 
     47 /**
     48  * Helper function for #TALER_EXCHANGEDB_iterate_reserve_close_info().
     49  * Calls the callback with each denomination key.
     50  *
     51  * @param cls a `struct IteratorContext`
     52  * @param result db results
     53  * @param num_results number of results in @a result
     54  */
     55 static void
     56 iterate_reserve_close_info_cb (void *cls,
     57                                PGresult *result,
     58                                unsigned int num_results)
     59 {
     60   struct IteratorContext *ic = cls;
     61   struct TALER_EXCHANGEDB_PostgresContext *pg = ic->pg;
     62 
     63   for (unsigned int i = 0; i<num_results; i++)
     64   {
     65     struct TALER_Amount amount;
     66     struct GNUNET_TIME_Absolute ts;
     67     struct GNUNET_PQ_ResultSpec rs[] = {
     68       GNUNET_PQ_result_spec_absolute_time ("execution_date",
     69                                            &ts),
     70       TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
     71                                    &amount),
     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       return;
     82     }
     83     ic->cb (ic->cb_cls,
     84             &amount,
     85             ts);
     86   }
     87 }
     88 
     89 
     90 enum GNUNET_DB_QueryStatus
     91 TALER_EXCHANGEDB_iterate_reserve_close_info (
     92   struct TALER_EXCHANGEDB_PostgresContext *pg,
     93   const struct TALER_NormalizedPaytoHashP *h_payto,
     94   struct GNUNET_TIME_Absolute time_limit,
     95   TALER_KYCLOGIC_KycAmountCallback kac,
     96   void *kac_cls)
     97 {
     98   struct GNUNET_PQ_QueryParam params[] = {
     99     GNUNET_PQ_query_param_auto_from_type (h_payto),
    100     GNUNET_PQ_query_param_absolute_time (&time_limit),
    101     GNUNET_PQ_query_param_end
    102   };
    103   struct IteratorContext ic = {
    104     .cb = kac,
    105     .cb_cls = kac_cls,
    106     .pg = pg
    107   };
    108 
    109   PREPARE (pg,
    110            "iterate_reserve_close_info",
    111            "SELECT amount"
    112            "      ,execution_date"
    113            "  FROM reserves_close"
    114            " WHERE wire_target_h_payto IN ("
    115            "   SELECT wire_target_h_payto"
    116            "     FROM wire_targets"
    117            "    WHERE h_normalized_payto=$1"
    118            "   )"
    119            "   AND execution_date >= $2"
    120            " ORDER BY execution_date DESC");
    121   return GNUNET_PQ_eval_prepared_multi_select (
    122     pg->conn,
    123     "iterate_reserve_close_info",
    124     params,
    125     &iterate_reserve_close_info_cb,
    126     &ic);
    127 }