exchange

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

pg_select_purse_deposits_by_purse.c (4558B)


      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_purse_deposits_by_purse.c
     18  * @brief Implementation of the select_purse_deposits_by_purse function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "taler/exchange-database/select_purse_deposits_by_purse.h"
     23 #include "helper.h"
     24 
     25 /**
     26  * Closure for #purse_refund_coin_helper_cb().
     27  */
     28 struct PurseRefundCoinContext
     29 {
     30 
     31   /**
     32    * Callback to call.
     33    */
     34   TALER_EXCHANGEDB_PurseRefundCoinCallback cb;
     35 
     36   /**
     37    * Closure for @e cb.
     38    */
     39   void *cb_cls;
     40 
     41   /**
     42    * Plugin 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 PurseRefundCoinContext`
     58  * @param result the postgres result
     59  * @param num_results the number of results in @a result
     60  */
     61 static void
     62 purse_refund_coin_helper_cb (void *cls,
     63                              PGresult *result,
     64                              unsigned int num_results)
     65 {
     66   struct PurseRefundCoinContext *dsc = cls;
     67   struct TALER_EXCHANGEDB_PostgresContext *pg = dsc->pg;
     68 
     69   for (unsigned int i = 0; i<num_results; i++)
     70   {
     71     struct TALER_Amount amount_with_fee;
     72     struct TALER_CoinSpendPublicKeyP coin_pub;
     73     struct TALER_DenominationPublicKey denom_pub;
     74     uint64_t rowid;
     75     struct GNUNET_PQ_ResultSpec rs[] = {
     76       TALER_PQ_result_spec_denom_pub ("denom_pub",
     77                                       &denom_pub),
     78       TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
     79                                    &amount_with_fee),
     80       GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
     81                                             &coin_pub),
     82       GNUNET_PQ_result_spec_uint64 ("purse_deposit_serial_id",
     83                                     &rowid),
     84       GNUNET_PQ_result_spec_end
     85     };
     86     enum GNUNET_GenericReturnValue ret;
     87 
     88     if (GNUNET_OK !=
     89         GNUNET_PQ_extract_result (result,
     90                                   rs,
     91                                   i))
     92     {
     93       GNUNET_break (0);
     94       dsc->status = GNUNET_SYSERR;
     95       return;
     96     }
     97     ret = dsc->cb (dsc->cb_cls,
     98                    rowid,
     99                    &amount_with_fee,
    100                    &coin_pub,
    101                    &denom_pub);
    102     GNUNET_PQ_cleanup_result (rs);
    103     if (GNUNET_OK != ret)
    104       break;
    105   }
    106 }
    107 
    108 
    109 enum GNUNET_DB_QueryStatus
    110 TALER_TALER_EXCHANGEDB_select_purse_deposits_by_purse (
    111   struct TALER_EXCHANGEDB_PostgresContext *pg,
    112   const struct TALER_PurseContractPublicKeyP *purse_pub,
    113   TALER_EXCHANGEDB_PurseRefundCoinCallback cb,
    114   void *cb_cls)
    115 {
    116   struct GNUNET_PQ_QueryParam params[] = {
    117     GNUNET_PQ_query_param_auto_from_type (purse_pub),
    118     GNUNET_PQ_query_param_end
    119   };
    120   struct PurseRefundCoinContext dsc = {
    121     .cb = cb,
    122     .cb_cls = cb_cls,
    123     .pg = pg,
    124     .status = GNUNET_OK
    125   };
    126   enum GNUNET_DB_QueryStatus qs;
    127 
    128   PREPARE (pg,
    129            "audit_get_purse_deposits_by_purse",
    130            "SELECT"
    131            " pd.purse_deposit_serial_id"
    132            ",pd.amount_with_fee"
    133            ",pd.coin_pub"
    134            ",denom.denom_pub"
    135            " FROM purse_deposits pd"
    136            " JOIN known_coins kc"
    137            "   USING (coin_pub)"
    138            " JOIN denominations denom"
    139            "   USING (denominations_serial)"
    140            " WHERE purse_pub=$1;");
    141   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    142                                              "audit_get_purse_deposits_by_purse",
    143                                              params,
    144                                              &purse_refund_coin_helper_cb,
    145                                              &dsc);
    146   if (GNUNET_OK != dsc.status)
    147     return GNUNET_DB_STATUS_HARD_ERROR;
    148   return qs;
    149 }