exchange

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

pg_get_reserves.c (5679B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 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 #include "taler/platform.h"
     17 #include "taler/taler_error_codes.h"
     18 #include "taler/taler_dbevents.h"
     19 #include "taler/taler_pq_lib.h"
     20 #include "pg_helper.h"
     21 #include "pg_get_reserves.h"
     22 
     23 
     24 struct ReservesContext
     25 {
     26 
     27   /**
     28    * Function to call for each bad sig loss.
     29    */
     30   TALER_AUDITORDB_ReservesCallback cb;
     31 
     32   /**
     33    * Closure for @e cb
     34    */
     35   void *cb_cls;
     36 
     37   /**
     38    * Plugin context.
     39    */
     40   struct PostgresClosure *pg;
     41 
     42   /**
     43    * Query status to return.
     44    */
     45   enum GNUNET_DB_QueryStatus qs;
     46 };
     47 
     48 
     49 /**
     50  * Helper function for #TAH_PG_get_reserves().
     51  * To be called with the results of a SELECT statement
     52  * that has returned @a num_results results.
     53  *
     54  * @param cls closure of type `struct ReservesContext *`
     55  * @param result the postgres result
     56  * @param num_results the number of results in @a result
     57  */
     58 static void
     59 reserves_cb (void *cls,
     60              PGresult *result,
     61              unsigned int num_results)
     62 {
     63   struct ReservesContext *dcc = cls;
     64   struct PostgresClosure *pg = dcc->pg;
     65 
     66   for (unsigned int i = 0; i < num_results; i++)
     67   {
     68     struct TALER_AUDITORDB_Reserves dc;
     69     struct GNUNET_PQ_ResultSpec rs[] = {
     70       GNUNET_PQ_result_spec_uint64 ("auditor_reserves_rowid",
     71                                     &dc.auditor_reserves_rowid),
     72       GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
     73                                             &dc.reserve_pub),
     74       TALER_PQ_RESULT_SPEC_AMOUNT ("reserve_balance",
     75                                    &dc.reserve_balance),
     76       TALER_PQ_RESULT_SPEC_AMOUNT ("reserve_loss",
     77                                    &dc.reserve_loss),
     78       TALER_PQ_RESULT_SPEC_AMOUNT ("withdraw_fee_balance",
     79                                    &dc.withdraw_fee_balance),
     80       TALER_PQ_RESULT_SPEC_AMOUNT ("close_fee_balance",
     81                                    &dc.close_fee_balance),
     82       TALER_PQ_RESULT_SPEC_AMOUNT ("purse_fee_balance",
     83                                    &dc.purse_fee_balance),
     84       TALER_PQ_RESULT_SPEC_AMOUNT ("open_fee_balance",
     85                                    &dc.open_fee_balance),
     86       TALER_PQ_RESULT_SPEC_AMOUNT ("history_fee_balance",
     87                                    &dc.history_fee_balance),
     88       GNUNET_PQ_result_spec_absolute_time ("expiration_date",
     89                                            &dc.expiration_date),
     90       GNUNET_PQ_result_spec_string ("origin_account",
     91                                     &dc.origin_account.full_payto),
     92       GNUNET_PQ_result_spec_end
     93     };
     94     enum GNUNET_GenericReturnValue rval;
     95 
     96     if (GNUNET_OK !=
     97         GNUNET_PQ_extract_result (result,
     98                                   rs,
     99                                   i))
    100     {
    101       GNUNET_break (0);
    102       dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
    103       return;
    104     }
    105     dcc->qs = i + 1;
    106     rval = dcc->cb (dcc->cb_cls,
    107                     dc.auditor_reserves_rowid,
    108                     &dc);
    109     GNUNET_PQ_cleanup_result (rs);
    110     if (GNUNET_OK != rval)
    111       break;
    112   }
    113 }
    114 
    115 
    116 enum GNUNET_DB_QueryStatus
    117 TAH_PG_get_reserves (
    118   void *cls,
    119   int64_t limit,
    120   uint64_t offset,
    121   TALER_AUDITORDB_ReservesCallback cb,
    122   void *cb_cls)
    123 {
    124   uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
    125   struct PostgresClosure *pg = cls;
    126   struct GNUNET_PQ_QueryParam params[] = {
    127     GNUNET_PQ_query_param_uint64 (&offset),
    128     GNUNET_PQ_query_param_uint64 (&plimit),
    129     GNUNET_PQ_query_param_end
    130   };
    131   struct ReservesContext dcc = {
    132     .cb = cb,
    133     .cb_cls = cb_cls,
    134     .pg = pg
    135   };
    136   enum GNUNET_DB_QueryStatus qs;
    137 
    138   PREPARE (pg,
    139            "auditor_reserves_get_desc",
    140            "SELECT"
    141            " auditor_reserves_rowid,"
    142            " reserve_pub,"
    143            " reserve_balance,"
    144            " reserve_loss,"
    145            " withdraw_fee_balance,"
    146            " close_fee_balance,"
    147            " purse_fee_balance,"
    148            " open_fee_balance,"
    149            " history_fee_balance,"
    150            " expiration_date,"
    151            " origin_account"
    152            " FROM auditor_reserves"
    153            " WHERE (auditor_reserves_rowid < $1)"
    154            " ORDER BY auditor_reserves_rowid DESC"
    155            " LIMIT $2"
    156            );
    157   PREPARE (pg,
    158            "auditor_reserves_get_asc",
    159            "SELECT"
    160            " auditor_reserves_rowid,"
    161            " reserve_pub,"
    162            " reserve_balance,"
    163            " reserve_loss,"
    164            " withdraw_fee_balance,"
    165            " close_fee_balance,"
    166            " purse_fee_balance,"
    167            " open_fee_balance,"
    168            " history_fee_balance,"
    169            " expiration_date,"
    170            " origin_account"
    171            " FROM auditor_reserves"
    172            " WHERE (auditor_reserves_rowid > $1)"
    173            " ORDER BY auditor_reserves_rowid ASC"
    174            " LIMIT $2"
    175            );
    176   qs = GNUNET_PQ_eval_prepared_multi_select (
    177     pg->conn,
    178     (limit > 0)
    179     ? "auditor_reserves_get_asc"
    180     : "auditor_reserves_get_desc",
    181     params,
    182     &reserves_cb,
    183     &dcc);
    184   if (qs > 0)
    185     return dcc.qs;
    186   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
    187   return qs;
    188 }