exchange

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

pg_select_reserves_in_above_serial_id.c (4995B)


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