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 (4909B)


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