exchange

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

get_reserves.c (5750B)


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