exchange

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

select_pending_deposits.c (5542B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2023 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 src/auditordb/select_pending_deposits.c
     18  * @brief Implementation of the select_pending_deposits function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "auditor-database/select_pending_deposits.h"
     23 #include "pg_helper.h"
     24 
     25 
     26 /**
     27  * Closure for #wire_missing_cb().
     28  */
     29 struct WireMissingContext
     30 {
     31 
     32   /**
     33    * Function to call for each pending deposit.
     34    */
     35   TALER_AUDITORDB_WireMissingCallback cb;
     36 
     37   /**
     38    * Closure for @e cb
     39    */
     40   void *cb_cls;
     41 
     42   /**
     43    * Plugin context.
     44    */
     45   struct TALER_AUDITORDB_PostgresContext *pg;
     46 
     47   /**
     48    * Query status to return.
     49    */
     50   enum GNUNET_DB_QueryStatus qs;
     51 };
     52 
     53 
     54 /**
     55  * Helper function for #TALER_AUDITORDB_select_purse_expired().
     56  * To be called with the results of a SELECT statement
     57  * that has returned @a num_results results.
     58  *
     59  * @param cls closure of type `struct WireMissingContext *`
     60  * @param result the postgres result
     61  * @param num_results the number of results in @a result
     62  */
     63 static void
     64 wire_missing_cb (void *cls,
     65                  PGresult *result,
     66                  unsigned int num_results)
     67 {
     68   struct WireMissingContext *eic = cls;
     69   struct TALER_AUDITORDB_PostgresContext *pg = eic->pg;
     70 
     71   for (unsigned int i = 0; i < num_results; i++)
     72   {
     73     uint64_t row_id;
     74     uint64_t batch_deposit_serial_id;
     75     struct TALER_Amount total_amount;
     76     struct TALER_FullPaytoHashP wire_target_h_payto;
     77     struct GNUNET_TIME_Timestamp deadline;
     78     bool suppressed;
     79     struct GNUNET_PQ_ResultSpec rs[] = {
     80       GNUNET_PQ_result_spec_uint64 ("row_id",
     81                                     &row_id),
     82       GNUNET_PQ_result_spec_uint64 ("batch_deposit_serial_id",
     83                                     &batch_deposit_serial_id),
     84       TALER_PQ_RESULT_SPEC_AMOUNT ("total_amount",
     85                                    &total_amount),
     86       GNUNET_PQ_result_spec_auto_from_type ("wire_target_h_payto",
     87                                             &wire_target_h_payto),
     88       GNUNET_PQ_result_spec_timestamp ("deadline",
     89                                        &deadline),
     90       GNUNET_PQ_result_spec_bool ("suppressed",
     91                                   &suppressed),
     92       GNUNET_PQ_result_spec_end
     93     };
     94 
     95     if (GNUNET_OK !=
     96         GNUNET_PQ_extract_result (result,
     97                                   rs,
     98                                   i))
     99     {
    100       GNUNET_break (0);
    101       eic->qs = GNUNET_DB_STATUS_HARD_ERROR;
    102       return;
    103     }
    104     eic->cb (eic->cb_cls,
    105              row_id,
    106              batch_deposit_serial_id,
    107              &total_amount,
    108              &wire_target_h_payto,
    109              deadline,
    110              suppressed);
    111   }
    112   eic->qs = num_results;
    113 }
    114 
    115 
    116 enum GNUNET_DB_QueryStatus
    117 TALER_AUDITORDB_select_pending_deposits (struct
    118                                          TALER_AUDITORDB_PostgresContext *pg,
    119                                          struct GNUNET_TIME_Absolute deadline,
    120                                          int64_t limit,
    121                                          uint64_t offset,
    122                                          bool return_suppressed,
    123                                          TALER_AUDITORDB_WireMissingCallback cb,
    124                                          void *cb_cls)
    125 {
    126   uint64_t ulimit = (limit < 0) ? -limit : limit;
    127   struct GNUNET_PQ_QueryParam params[] = {
    128     GNUNET_PQ_query_param_absolute_time (&deadline),
    129     GNUNET_PQ_query_param_uint64 (&offset),
    130     GNUNET_PQ_query_param_uint64 (&ulimit),
    131     GNUNET_PQ_query_param_bool (return_suppressed),
    132     GNUNET_PQ_query_param_end
    133   };
    134   struct WireMissingContext eic = {
    135     .cb = cb,
    136     .cb_cls = cb_cls,
    137     .pg = pg
    138   };
    139   enum GNUNET_DB_QueryStatus qs;
    140 
    141   PREPARE (pg,
    142            "auditor_select_pending_deposits_asc",
    143            "SELECT"
    144            " row_id"
    145            ",total_amount"
    146            ",wire_target_h_payto"
    147            ",batch_deposit_serial_id"
    148            ",deadline"
    149            ",suppressed"
    150            " FROM auditor_pending_deposits"
    151            " WHERE deadline<$1"
    152            "  AND (row_id > $2)"
    153            "  AND ($4 OR NOT suppressed)"
    154            " ORDER BY row_id ASC"
    155            " LIMIT $3;");
    156   PREPARE (pg,
    157            "auditor_select_pending_deposits_desc",
    158            "SELECT"
    159            " row_id"
    160            ",total_amount"
    161            ",wire_target_h_payto"
    162            ",batch_deposit_serial_id"
    163            ",deadline"
    164            ",suppressed"
    165            " FROM auditor_pending_deposits"
    166            " WHERE deadline<$1"
    167            "  AND (row_id < $2)"
    168            "  AND ($4 OR NOT suppressed)"
    169            " ORDER BY row_id DESC"
    170            " LIMIT $3;");
    171   qs = GNUNET_PQ_eval_prepared_multi_select (
    172     pg->conn,
    173     "auditor_select_pending_deposits",
    174     params,
    175     &wire_missing_cb,
    176     &eic);
    177   if (0 > qs)
    178     return qs;
    179   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != eic.qs);
    180   return eic.qs;
    181 }