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_by_account.c (5142B)


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