exchange

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

get_expired_reserves.c (5055B)


      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 get_expired_reserves.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/get_expired_reserves.h"
     23 #include "helper.h"
     24 
     25 
     26 /**
     27  * Closure for #reserve_expired_cb().
     28  */
     29 struct ExpiredReserveContext
     30 {
     31   /**
     32    * Function to call for each expired reserve.
     33    */
     34   TALER_EXCHANGEDB_ReserveExpiredCallback rec;
     35 
     36   /**
     37    * Closure to give to @e rec.
     38    */
     39   void *rec_cls;
     40 
     41   /**
     42    * Plugin context.
     43    */
     44   struct TALER_EXCHANGEDB_PostgresContext *pg;
     45 
     46   /**
     47    * Set to #GNUNET_SYSERR on error.
     48    */
     49   enum GNUNET_GenericReturnValue status;
     50 };
     51 
     52 
     53 /**
     54  * Function to be called with the results of a SELECT statement
     55  * that has returned @a num_results results.
     56  *
     57  * @param cls closure
     58  * @param result the postgres result
     59  * @param num_results the number of results in @a result
     60  */
     61 static void
     62 reserve_expired_cb (void *cls,
     63                     PGresult *result,
     64                     unsigned int num_results)
     65 {
     66   struct ExpiredReserveContext *erc = cls;
     67   struct TALER_EXCHANGEDB_PostgresContext *pg = erc->pg;
     68   enum GNUNET_GenericReturnValue ret = GNUNET_OK;
     69 
     70   for (unsigned int i = 0; i<num_results; i++)
     71   {
     72     struct GNUNET_TIME_Timestamp exp_date;
     73     struct TALER_FullPayto account_details;
     74     struct TALER_ReservePublicKeyP reserve_pub;
     75     struct TALER_Amount remaining_balance;
     76     struct GNUNET_PQ_ResultSpec rs[] = {
     77       GNUNET_PQ_result_spec_timestamp ("expiration_date",
     78                                        &exp_date),
     79       GNUNET_PQ_result_spec_string ("account_details",
     80                                     &account_details.full_payto),
     81       GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
     82                                             &reserve_pub),
     83       TALER_PQ_result_spec_amount ("current_balance",
     84                                    pg->currency,
     85                                    &remaining_balance),
     86       GNUNET_PQ_result_spec_end
     87     };
     88 
     89     if (GNUNET_OK !=
     90         GNUNET_PQ_extract_result (result,
     91                                   rs,
     92                                   i))
     93     {
     94       GNUNET_break (0);
     95       ret = GNUNET_SYSERR;
     96       break;
     97     }
     98     ret = erc->rec (erc->rec_cls,
     99                     &reserve_pub,
    100                     &remaining_balance,
    101                     account_details,
    102                     exp_date,
    103                     0);
    104     GNUNET_PQ_cleanup_result (rs);
    105     if (GNUNET_OK != ret)
    106       break;
    107   }
    108   erc->status = ret;
    109 }
    110 
    111 
    112 enum GNUNET_DB_QueryStatus
    113 TALER_EXCHANGEDB_get_expired_reserves (
    114   struct TALER_EXCHANGEDB_PostgresContext *pg,
    115   struct GNUNET_TIME_Timestamp now,
    116   TALER_EXCHANGEDB_ReserveExpiredCallback rec,
    117   void *rec_cls)
    118 {
    119   struct GNUNET_PQ_QueryParam params[] = {
    120     GNUNET_PQ_query_param_timestamp (&now),
    121     GNUNET_PQ_query_param_end
    122   };
    123   struct ExpiredReserveContext ectx = {
    124     .rec = rec,
    125     .rec_cls = rec_cls,
    126     .pg = pg,
    127     .status = GNUNET_OK
    128   };
    129   enum GNUNET_DB_QueryStatus qs;
    130 
    131   PREPARE (pg,
    132            "get_expired_reserves",
    133            "WITH ed AS MATERIALIZED ( "
    134            " SELECT expiration_date"
    135            "       ,wire_source_h_payto"
    136            "       ,current_balance"
    137            "       ,r.reserve_pub"
    138            " FROM reserves r"
    139            " JOIN reserves_in"
    140            "   USING (reserve_pub)"
    141            " WHERE expiration_date <= $1 "
    142            "   AND ((current_balance).val != 0 OR (current_balance).frac != 0) "
    143            " ORDER BY expiration_date ASC "
    144            " LIMIT 1 "
    145            ") "
    146            "SELECT"
    147            "  wt.payto_uri AS account_details"
    148            " ,ed.expiration_date"
    149            " ,ed.reserve_pub"
    150            " ,ed.current_balance"
    151            " FROM wire_targets wt"
    152            " JOIN ed"
    153            "   ON (ed.wire_source_h_payto=wt.wire_target_h_payto);");
    154   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    155                                              "get_expired_reserves",
    156                                              params,
    157                                              &reserve_expired_cb,
    158                                              &ectx);
    159   switch (ectx.status)
    160   {
    161   case GNUNET_SYSERR:
    162     return GNUNET_DB_STATUS_HARD_ERROR;
    163   case GNUNET_NO:
    164     return GNUNET_DB_STATUS_SOFT_ERROR;
    165   case GNUNET_OK:
    166     break;
    167   }
    168   return qs;
    169 }