exchange

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

pg_get_emergency_by_count.c (5285B)


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