exchange

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

pg_get_unfinished_close_requests.c (4870B)


      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 pg_get_unfinished_close_requests.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 "taler/exchange-database/get_unfinished_close_requests.h"
     23 #include "helper.h"
     24 
     25 
     26 /**
     27  * Closure for #reserve_close_cb().
     28  */
     29 struct CloseReserveContext
     30 {
     31   /**
     32    * Function to call for each to be closed 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_cb (void *cls,
     63             PGresult *result,
     64             unsigned int num_results)
     65 {
     66   struct CloseReserveContext *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     uint64_t close_request_row;
     77     struct GNUNET_PQ_ResultSpec rs[] = {
     78       GNUNET_PQ_result_spec_timestamp ("expiration_date",
     79                                        &exp_date),
     80       GNUNET_PQ_result_spec_string ("account_details",
     81                                     &account_details.full_payto),
     82       GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
     83                                             &reserve_pub),
     84       TALER_PQ_RESULT_SPEC_AMOUNT ("close",
     85                                    &remaining_balance),
     86       GNUNET_PQ_result_spec_uint64 ("close_request_serial_id",
     87                                     &close_request_row),
     88       GNUNET_PQ_result_spec_end
     89     };
     90 
     91     if (GNUNET_OK !=
     92         GNUNET_PQ_extract_result (result,
     93                                   rs,
     94                                   i))
     95     {
     96       GNUNET_break (0);
     97       ret = GNUNET_SYSERR;
     98       break;
     99     }
    100     ret = erc->rec (erc->rec_cls,
    101                     &reserve_pub,
    102                     &remaining_balance,
    103                     account_details,
    104                     exp_date,
    105                     close_request_row);
    106     GNUNET_PQ_cleanup_result (rs);
    107     if (GNUNET_OK != ret)
    108       break;
    109   }
    110   erc->status = ret;
    111 }
    112 
    113 
    114 enum GNUNET_DB_QueryStatus
    115 TALER_EXCHANGEDB_get_unfinished_close_requests (
    116   struct TALER_EXCHANGEDB_PostgresContext *pg,
    117   TALER_EXCHANGEDB_ReserveExpiredCallback rec,
    118   void *rec_cls)
    119 {
    120   struct GNUNET_PQ_QueryParam params[] = {
    121     GNUNET_PQ_query_param_end
    122   };
    123   struct CloseReserveContext 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_unfinished_close_requests",
    133            "UPDATE close_requests AS rc"
    134            " SET done=TRUE"
    135            " WHERE NOT done"
    136            " RETURNING"
    137            "    reserve_pub"
    138            "   ,close_request_serial_id"
    139            "   ,close_timestamp AS expiration_date"
    140            "   ,close"
    141            "   ,(SELECT payto_uri"
    142            "       FROM reserves_in ri"
    143            "       JOIN wire_targets wt"
    144            "         ON (ri.wire_source_h_payto = wt.wire_target_h_payto)"
    145            "      WHERE ri.reserve_pub=rc.reserve_pub)"
    146            "    AS account_details;");
    147   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    148                                              "get_unfinished_close_requests",
    149                                              params,
    150                                              &reserve_cb,
    151                                              &ectx);
    152   switch (ectx.status)
    153   {
    154   case GNUNET_SYSERR:
    155     return GNUNET_DB_STATUS_HARD_ERROR;
    156   case GNUNET_NO:
    157     return GNUNET_DB_STATUS_SOFT_ERROR;
    158   case GNUNET_OK:
    159     break;
    160   }
    161   return qs;
    162 }