exchange

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

select_reserve_closed_above_serial_id.c (5138B)


      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 select_reserve_closed_above_serial_id.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/select_reserve_closed_above_serial_id.h"
     23 #include "helper.h"
     24 
     25 /**
     26  * Closure for #reserve_closed_serial_helper_cb().
     27  */
     28 struct ReserveClosedSerialContext
     29 {
     30 
     31   /**
     32    * Callback to call.
     33    */
     34   TALER_EXCHANGEDB_ReserveClosedCallback cb;
     35 
     36   /**
     37    * Closure for @e cb.
     38    */
     39   void *cb_cls;
     40 
     41   /**
     42    * Plugin's context.
     43    */
     44   struct TALER_EXCHANGEDB_PostgresContext *pg;
     45 
     46   /**
     47    * Status code, set to #GNUNET_SYSERR on hard errors.
     48    */
     49   enum GNUNET_GenericReturnValue status;
     50 };
     51 
     52 
     53 /**
     54  * Helper function to be called with the results of a SELECT statement
     55  * that has returned @a num_results results.
     56  *
     57  * @param cls closure of type `struct ReserveClosedSerialContext`
     58  * @param result the postgres result
     59  * @param num_results the number of results in @a result
     60  */
     61 static void
     62 reserve_closed_serial_helper_cb (void *cls,
     63                                  PGresult *result,
     64                                  unsigned int num_results)
     65 {
     66   struct ReserveClosedSerialContext *rcsc = cls;
     67   struct TALER_EXCHANGEDB_PostgresContext *pg = rcsc->pg;
     68 
     69   for (unsigned int i = 0; i<num_results; i++)
     70   {
     71     uint64_t rowid;
     72     struct TALER_ReservePublicKeyP reserve_pub;
     73     struct TALER_FullPayto receiver_account;
     74     struct TALER_WireTransferIdentifierRawP wtid;
     75     struct TALER_Amount amount_with_fee;
     76     struct TALER_Amount closing_fee;
     77     struct GNUNET_TIME_Timestamp execution_date;
     78     uint64_t close_request_row;
     79     struct GNUNET_PQ_ResultSpec rs[] = {
     80       GNUNET_PQ_result_spec_uint64 ("close_uuid",
     81                                     &rowid),
     82       GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
     83                                             &reserve_pub),
     84       GNUNET_PQ_result_spec_timestamp ("execution_date",
     85                                        &execution_date),
     86       GNUNET_PQ_result_spec_auto_from_type ("wtid",
     87                                             &wtid),
     88       GNUNET_PQ_result_spec_string ("receiver_account",
     89                                     &receiver_account.full_payto),
     90       TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
     91                                    &amount_with_fee),
     92       TALER_PQ_RESULT_SPEC_AMOUNT ("closing_fee",
     93                                    &closing_fee),
     94       GNUNET_PQ_result_spec_uint64 ("close_request_row",
     95                                     &close_request_row),
     96       GNUNET_PQ_result_spec_end
     97     };
     98     enum GNUNET_GenericReturnValue ret;
     99 
    100     if (GNUNET_OK !=
    101         GNUNET_PQ_extract_result (result,
    102                                   rs,
    103                                   i))
    104     {
    105       GNUNET_break (0);
    106       rcsc->status = GNUNET_SYSERR;
    107       return;
    108     }
    109     ret = rcsc->cb (rcsc->cb_cls,
    110                     rowid,
    111                     execution_date,
    112                     &amount_with_fee,
    113                     &closing_fee,
    114                     &reserve_pub,
    115                     receiver_account,
    116                     &wtid,
    117                     close_request_row);
    118     GNUNET_PQ_cleanup_result (rs);
    119     if (GNUNET_OK != ret)
    120       break;
    121   }
    122 }
    123 
    124 
    125 enum GNUNET_DB_QueryStatus
    126 TALER_EXCHANGEDB_select_reserve_closed_above_serial_id (
    127   struct TALER_EXCHANGEDB_PostgresContext *pg,
    128   uint64_t serial_id,
    129   TALER_EXCHANGEDB_ReserveClosedCallback cb,
    130   void *cb_cls)
    131 {
    132   struct GNUNET_PQ_QueryParam params[] = {
    133     GNUNET_PQ_query_param_uint64 (&serial_id),
    134     GNUNET_PQ_query_param_end
    135   };
    136   struct ReserveClosedSerialContext rcsc = {
    137     .cb = cb,
    138     .cb_cls = cb_cls,
    139     .pg = pg,
    140     .status = GNUNET_OK
    141   };
    142   enum GNUNET_DB_QueryStatus qs;
    143 
    144   PREPARE (
    145     pg,
    146     "reserves_close_get_incr",
    147     "SELECT"
    148     " close_uuid"
    149     ",reserves.reserve_pub"
    150     ",execution_date"
    151     ",wtid"
    152     ",wt.payto_uri AS receiver_account"
    153     ",amount"
    154     ",closing_fee"
    155     ",close_request_row"
    156     " FROM reserves_close"
    157     " JOIN wire_targets wt"
    158     "   USING (wire_target_h_payto)"
    159     " JOIN reserves"
    160     "   USING (reserve_pub)"
    161     " WHERE close_uuid>=$1"
    162     " ORDER BY close_uuid ASC;");
    163   qs = GNUNET_PQ_eval_prepared_multi_select (
    164     pg->conn,
    165     "reserves_close_get_incr",
    166     params,
    167     &reserve_closed_serial_helper_cb,
    168     &rcsc);
    169   if (GNUNET_OK != rcsc.status)
    170     return GNUNET_DB_STATUS_HARD_ERROR;
    171   return qs;
    172 }