exchange

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

get_auditor_closure_lags.c (4902B)


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