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_debit_transfers.c (5741B)


      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_debit_transfers.c
     18  * @brief Implementation of the select_exchange_debit_transfers 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_exchange_debit_transfers.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Closure for #handle_aml_result.
     31  */
     32 struct SelectTransferContext
     33 {
     34   /**
     35    * Function to call on each result.
     36    */
     37   TALER_EXCHANGEDB_AmlTransferCallback 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    * Set to #GNUNET_SYSERR on serious errors.
     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.  Helper function
     59  * for #TEH_PG_select_exchange_debit_transfers().
     60  *
     61  * @param cls closure of type `struct SelectTransferContext *`
     62  * @param result the postgres result
     63  * @param num_results the number of results in @a result
     64  */
     65 static void
     66 handle_transfer_result (void *cls,
     67                         PGresult *result,
     68                         unsigned int num_results)
     69 {
     70   struct SelectTransferContext *stc = cls;
     71   struct PostgresClosure *pg = stc->pg;
     72 
     73   for (unsigned int i = 0; i<num_results; i++)
     74   {
     75     char *payto_uri;
     76     uint64_t rowid;
     77     struct GNUNET_TIME_Absolute execution_time;
     78     struct TALER_Amount amount;
     79     struct GNUNET_PQ_ResultSpec rs[] = {
     80       GNUNET_PQ_result_spec_uint64 ("serial_id",
     81                                     &rowid),
     82       GNUNET_PQ_result_spec_string ("payto_uri",
     83                                     &payto_uri),
     84       GNUNET_PQ_result_spec_absolute_time ("execution_time",
     85                                            &execution_time),
     86       TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
     87                                    &amount),
     88       GNUNET_PQ_result_spec_end
     89     };
     90 
     91     if (GNUNET_OK !=
     92         GNUNET_PQ_extract_result (result,
     93                                   rs,
     94                                   i))
     95     {
     96       GNUNET_break (0);
     97       stc->status = GNUNET_SYSERR;
     98       return;
     99     }
    100     stc->cb (stc->cb_cls,
    101              rowid,
    102              payto_uri,
    103              execution_time,
    104              &amount);
    105     GNUNET_PQ_cleanup_result (rs);
    106   }
    107 }
    108 
    109 
    110 enum GNUNET_DB_QueryStatus
    111 TEH_PG_select_exchange_debit_transfers (
    112   void *cls,
    113   const struct TALER_Amount *threshold,
    114   uint64_t offset,
    115   int64_t limit,
    116   const struct TALER_NormalizedPaytoHashP *h_payto,
    117   TALER_EXCHANGEDB_AmlTransferCallback cb,
    118   void *cb_cls)
    119 {
    120   struct PostgresClosure *pg = cls;
    121   struct SelectTransferContext stc = {
    122     .pg = pg,
    123     .cb = cb,
    124     .cb_cls = cb_cls,
    125     .status = GNUNET_OK
    126   };
    127   uint64_t ulimit = (limit > 0) ? limit : -limit;
    128   struct GNUNET_PQ_QueryParam params[] = {
    129     GNUNET_PQ_query_param_uint64 (&offset),
    130     GNUNET_PQ_query_param_uint64 (&ulimit),
    131     TALER_PQ_query_param_amount (pg->conn,
    132                                  threshold),
    133     NULL != h_payto
    134     ? GNUNET_PQ_query_param_auto_from_type (h_payto)
    135     : GNUNET_PQ_query_param_null (),
    136     GNUNET_PQ_query_param_end
    137   };
    138   enum GNUNET_DB_QueryStatus qs;
    139 
    140   PREPARE (pg,
    141            "select_exchange_debit_transfers_inc",
    142            "SELECT"
    143            " wo.wireout_uuid AS serial_id"
    144            ",wt.payto_uri"
    145            ",wo.execution_date AS execution_time"
    146            ",wo.amount"
    147            " FROM wire_out wo"
    148            " LEFT JOIN wire_targets wt"
    149            "   USING (wire_target_h_payto)"
    150            " WHERE (wo.wireout_uuid > $1)"
    151            "   AND ( ($4::BYTEA IS NULL) OR (wt.h_normalized_payto=$4) )"
    152            "   AND ( ( (wo.amount).val > ($3::taler_amount).val)"
    153            "      OR ( ( (wo.amount).val >= ($3::taler_amount).val)"
    154            "       AND ( (wo.amount).frac >= ($3::taler_amount).frac) ) )"
    155            " ORDER BY wo.wireout_uuid ASC"
    156            " LIMIT $2");
    157   PREPARE (pg,
    158            "select_exchange_debit_transfers_dec",
    159            "SELECT"
    160            " wo.wireout_uuid AS serial_id"
    161            ",wt.payto_uri"
    162            ",wo.execution_date AS execution_time"
    163            ",wo.amount"
    164            " FROM wire_out wo"
    165            " LEFT JOIN wire_targets wt"
    166            "   USING (wire_target_h_payto)"
    167            " WHERE (wo.wireout_uuid < $1)"
    168            "   AND ( ($4::BYTEA IS NULL) OR (wt.h_normalized_payto=$4) )"
    169            "   AND ( ( (wo.amount).val > ($3::taler_amount).val)"
    170            "      OR ( ( (wo.amount).val >= ($3::taler_amount).val)"
    171            "       AND ( (wo.amount).frac >= ($3::taler_amount).frac) ) )"
    172            " ORDER BY wo.wireout_uuid DESC"
    173            " LIMIT $2");
    174   qs = GNUNET_PQ_eval_prepared_multi_select (
    175     pg->conn,
    176     (limit > 0)
    177     ? "select_exchange_debit_transfers_inc"
    178     : "select_exchange_debit_transfers_dec",
    179     params,
    180     &handle_transfer_result,
    181     &stc);
    182   if (GNUNET_OK != stc.status)
    183     return GNUNET_DB_STATUS_HARD_ERROR;
    184   return qs;
    185 }