exchange

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

pg_select_exchange_credit_transfers.c (5843B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2025 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_exchange_credit_transfers.c
     18  * @brief Implementation of the select_exchange_credit_transfers function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "taler/exchange-database/select_exchange_credit_transfers.h"
     23 #include "helper.h"
     24 
     25 /**
     26  * Closure for #handle_aml_result.
     27  */
     28 struct SelectTransferContext
     29 {
     30   /**
     31    * Function to call on each result.
     32    */
     33   TALER_EXCHANGEDB_AmlTransferCallback cb;
     34 
     35   /**
     36    * Closure for @e cb.
     37    */
     38   void *cb_cls;
     39 
     40   /**
     41    * Plugin context.
     42    */
     43   struct TALER_EXCHANGEDB_PostgresContext *pg;
     44 
     45   /**
     46    * Set to #GNUNET_SYSERR on serious errors.
     47    */
     48   enum GNUNET_GenericReturnValue status;
     49 };
     50 
     51 
     52 /**
     53  * Function to be called with the results of a SELECT statement
     54  * that has returned @a num_results results.  Helper function
     55  * for #TALER_EXCHANGEDB_select_exchange_debit_transfers().
     56  *
     57  * @param cls closure of type `struct SelectTransferContext *`
     58  * @param result the postgres result
     59  * @param num_results the number of results in @a result
     60  */
     61 static void
     62 handle_transfer_result (void *cls,
     63                         PGresult *result,
     64                         unsigned int num_results)
     65 {
     66   struct SelectTransferContext *stc = cls;
     67   struct TALER_EXCHANGEDB_PostgresContext *pg = stc->pg;
     68 
     69   for (unsigned int i = 0; i<num_results; i++)
     70   {
     71     char *payto_uri;
     72     uint64_t rowid;
     73     struct GNUNET_TIME_Absolute execution_time;
     74     struct TALER_Amount amount;
     75     struct GNUNET_PQ_ResultSpec rs[] = {
     76       GNUNET_PQ_result_spec_uint64 ("serial_id",
     77                                     &rowid),
     78       GNUNET_PQ_result_spec_string ("payto_uri",
     79                                     &payto_uri),
     80       GNUNET_PQ_result_spec_absolute_time ("execution_time",
     81                                            &execution_time),
     82       TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
     83                                    &amount),
     84       GNUNET_PQ_result_spec_end
     85     };
     86 
     87     if (GNUNET_OK !=
     88         GNUNET_PQ_extract_result (result,
     89                                   rs,
     90                                   i))
     91     {
     92       GNUNET_break (0);
     93       stc->status = GNUNET_SYSERR;
     94       return;
     95     }
     96     stc->cb (stc->cb_cls,
     97              rowid,
     98              payto_uri,
     99              execution_time,
    100              &amount);
    101     GNUNET_PQ_cleanup_result (rs);
    102   }
    103 }
    104 
    105 
    106 enum GNUNET_DB_QueryStatus
    107 TALER_EXCHANGEDB_select_exchange_credit_transfers (
    108   struct TALER_EXCHANGEDB_PostgresContext *pg,
    109   const struct TALER_Amount *threshold,
    110   uint64_t offset,
    111   int64_t limit,
    112   const struct TALER_NormalizedPaytoHashP *h_payto,
    113   TALER_EXCHANGEDB_AmlTransferCallback cb,
    114   void *cb_cls)
    115 {
    116   struct SelectTransferContext stc = {
    117     .pg = pg,
    118     .cb = cb,
    119     .cb_cls = cb_cls,
    120     .status = GNUNET_OK
    121   };
    122   uint64_t ulimit = (limit > 0) ? limit : -limit;
    123   struct GNUNET_PQ_QueryParam params[] = {
    124     GNUNET_PQ_query_param_uint64 (&offset),
    125     GNUNET_PQ_query_param_uint64 (&ulimit),
    126     TALER_PQ_query_param_amount (pg->conn,
    127                                  threshold),
    128     NULL != h_payto
    129     ? GNUNET_PQ_query_param_auto_from_type (h_payto)
    130     : GNUNET_PQ_query_param_null (),
    131     GNUNET_PQ_query_param_end
    132   };
    133   enum GNUNET_DB_QueryStatus qs;
    134 
    135   PREPARE (pg,
    136            "select_exchange_credit_transfers_inc",
    137            "SELECT"
    138            " ri.reserve_in_serial_id AS serial_id"
    139            ",wt.payto_uri"
    140            ",ri.execution_date AS execution_time"
    141            ",ri.credit AS amount"
    142            " FROM reserves_in ri"
    143            " LEFT JOIN wire_targets wt"
    144            "   ON (ri.wire_source_h_payto = wt.wire_target_h_payto)"
    145            " WHERE (ri.reserve_in_serial_id > $1)"
    146            "   AND ( ($4::BYTEA IS NULL) OR (wt.h_normalized_payto=$4) )"
    147            "   AND ( ( (ri.credit).val > ($3::taler_amount).val)"
    148            "      OR ( ( (ri.credit).val >= ($3::taler_amount).val)"
    149            "       AND ( (ri.credit).frac >= ($3::taler_amount).frac) ) )"
    150            " ORDER BY ri.reserve_in_serial_id ASC"
    151            " LIMIT $2");
    152   PREPARE (pg,
    153            "select_exchange_credit_transfers_dec",
    154            "SELECT"
    155            " ri.reserve_in_serial_id AS serial_id"
    156            ",wt.payto_uri"
    157            ",ri.execution_date AS execution_time"
    158            ",ri.credit AS amount"
    159            " FROM reserves_in ri"
    160            " LEFT JOIN wire_targets wt"
    161            "   ON (ri.wire_source_h_payto = wt.wire_target_h_payto)"
    162            " WHERE (ri.reserve_in_serial_id < $1)"
    163            "   AND ( ($4::BYTEA IS NULL) OR (wt.h_normalized_payto=$4) )"
    164            "   AND ( ( (ri.credit).val > ($3::taler_amount).val)"
    165            "      OR ( ( (ri.credit).val >= ($3::taler_amount).val)"
    166            "       AND ( (ri.credit).frac >= ($3::taler_amount).frac) ) )"
    167            " ORDER BY ri.reserve_in_serial_id DESC"
    168            " LIMIT $2");
    169   qs = GNUNET_PQ_eval_prepared_multi_select (
    170     pg->conn,
    171     (limit > 0)
    172     ? "select_exchange_credit_transfers_inc"
    173     : "select_exchange_credit_transfers_dec",
    174     params,
    175     &handle_transfer_result,
    176     &stc);
    177   if (GNUNET_OK != stc.status)
    178     return GNUNET_DB_STATUS_HARD_ERROR;
    179   return qs;
    180 }