exchange

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

get_emergency_by_count.c (5552B)


      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_pq_lib.h"
     18 #include "pg_helper.h"
     19 #include "auditor-database/get_emergency_by_count.h"
     20 
     21 
     22 /**
     23  * Closure for #emergency_cb().
     24  */
     25 struct EmergencyByCountContext
     26 {
     27 
     28   /**
     29    * Function to call for each deposit confirmation.
     30    */
     31   TALER_AUDITORDB_EmergenciesByCountCallback cb;
     32 
     33   /**
     34    * Closure for @e cb
     35    */
     36   void *cb_cls;
     37 
     38   /**
     39    * Plugin context.
     40    */
     41   struct TALER_AUDITORDB_PostgresContext *pg;
     42 
     43   /**
     44    * Query status to return.
     45    */
     46   enum GNUNET_DB_QueryStatus qs;
     47 };
     48 
     49 
     50 /**
     51  * Helper function for #TALER_AUDITORDB_get_emergency_by_count().
     52  * To be called with the results of a SELECT statement
     53  * that has returned @a num_results results.
     54  *
     55  * @param cls closure of type `struct Emergency *`
     56  * @param result the postgres result
     57  * @param num_results the number of results in @a result
     58  */
     59 static void
     60 emergency_by_count_cb (void *cls,
     61                        PGresult *result,
     62                        unsigned int num_results)
     63 {
     64   struct EmergencyByCountContext *dcc = cls;
     65   struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg;
     66 
     67   for (unsigned int i = 0; i < num_results; i++)
     68   {
     69     struct TALER_AUDITORDB_EmergenciesByCount dc;
     70     struct GNUNET_PQ_ResultSpec rs[] = {
     71       GNUNET_PQ_result_spec_uint64 ("row_id",
     72                                     &dc.row_id),
     73       GNUNET_PQ_result_spec_auto_from_type ("denompub_h",
     74                                             &dc.denompub_h),
     75       GNUNET_PQ_result_spec_uint64 ("num_issued",
     76                                     &dc.num_issued),
     77       GNUNET_PQ_result_spec_uint64 ("num_known",
     78                                     &dc.num_known),
     79       TALER_PQ_RESULT_SPEC_AMOUNT ("risk",
     80                                    &dc.risk),
     81       GNUNET_PQ_result_spec_absolute_time ("start",
     82                                            &dc.start),
     83       GNUNET_PQ_result_spec_absolute_time ("deposit_end",
     84                                            &dc.deposit_end),
     85       TALER_PQ_RESULT_SPEC_AMOUNT ("value",
     86                                    &dc.value),
     87       GNUNET_PQ_result_spec_bool ("suppressed",
     88                                   &dc.suppressed),
     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);
    105     GNUNET_PQ_cleanup_result (rs);
    106     if (GNUNET_OK != rval)
    107       break;
    108   }
    109 }
    110 
    111 
    112 enum GNUNET_DB_QueryStatus
    113 TALER_AUDITORDB_get_emergency_by_count (struct TALER_AUDITORDB_PostgresContext *
    114                                         pg,
    115                                         int64_t limit,
    116                                         uint64_t offset,
    117                                         bool return_suppressed,
    118                                         TALER_AUDITORDB_EmergenciesByCountCallback
    119                                         cb,
    120                                         void *cb_cls)
    121 {
    122   uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
    123   struct GNUNET_PQ_QueryParam params[] = {
    124     GNUNET_PQ_query_param_uint64 (&offset),
    125     GNUNET_PQ_query_param_bool (return_suppressed),
    126     GNUNET_PQ_query_param_uint64 (&plimit),
    127     GNUNET_PQ_query_param_end
    128   };
    129   struct EmergencyByCountContext dcc = {
    130     .cb = cb,
    131     .cb_cls = cb_cls,
    132     .pg = pg
    133   };
    134   enum GNUNET_DB_QueryStatus qs;
    135 
    136   PREPARE (pg,
    137            "auditor_emergency_by_count_get_desc",
    138            "SELECT"
    139            " row_id"
    140            ",denompub_h"
    141            ",num_issued"
    142            ",num_known"
    143            ",risk"
    144            ",start"
    145            ",deposit_end"
    146            ",value"
    147            ",suppressed"
    148            " FROM auditor_emergency_by_count"
    149            " WHERE (row_id < $1)"
    150            " AND ($2 OR NOT suppressed)"
    151            " ORDER BY row_id DESC"
    152            " LIMIT $3"
    153            );
    154   PREPARE (pg,
    155            "auditor_emergency_by_count_get_asc",
    156            "SELECT"
    157            " row_id"
    158            ",denompub_h"
    159            ",num_issued"
    160            ",num_known"
    161            ",risk"
    162            ",start"
    163            ",deposit_end"
    164            ",value"
    165            ",suppressed"
    166            " FROM auditor_emergency_by_count"
    167            " WHERE (row_id > $1)"
    168            " AND ($2 OR NOT suppressed)"
    169            " ORDER BY row_id ASC"
    170            " LIMIT $3"
    171            );
    172   qs = GNUNET_PQ_eval_prepared_multi_select (
    173     pg->conn,
    174     (limit > 0)
    175     ? "auditor_emergency_by_count_get_asc"
    176     : "auditor_emergency_by_count_get_desc",
    177     params,
    178     &emergency_by_count_cb,
    179     &dcc);
    180   if (qs > 0)
    181     return dcc.qs;
    182   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
    183   return qs;
    184 }