merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

iterate_reports.c (4956B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2025 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/backenddb/iterate_reports.c
     18  * @brief Implementation of the iterate_reports function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/iterate_reports.h"
     24 #include "helper.h"
     25 
     26 
     27 /**
     28  * Hard upper bound on the number of records returned by a single
     29  * call, regardless of the limit requested by the client.
     30  */
     31 #define MAX_RECORDS 50000
     32 
     33 
     34 /**
     35  * Context used for TALER_MERCHANTDB_iterate_reports().
     36  */
     37 struct SelectReportsContext
     38 {
     39   /**
     40    * Function to call with the results.
     41    */
     42   TALER_MERCHANTDB_ReportsCallback cb;
     43 
     44   /**
     45    * Closure for @a cb.
     46    */
     47   void *cb_cls;
     48 
     49   /**
     50    * Did database result extraction fail?
     51    */
     52   bool extract_failed;
     53 };
     54 
     55 
     56 /**
     57  * Function to be called with the results of a SELECT statement
     58  * that has returned @a num_results results about reports.
     59  *
     60  * @param[in,out] cls of type `struct SelectReportsContext *`
     61  * @param result the postgres result
     62  * @param num_results the number of results in @a result
     63  */
     64 static void
     65 select_reports_cb (void *cls,
     66                    PGresult *result,
     67                    unsigned int num_results)
     68 {
     69   struct SelectReportsContext *plc = cls;
     70 
     71   for (unsigned int i = 0; i < num_results; i++)
     72   {
     73     uint64_t report_serial;
     74     char *report_description;
     75     struct GNUNET_TIME_Relative frequency;
     76     struct GNUNET_PQ_ResultSpec rs[] = {
     77       GNUNET_PQ_result_spec_uint64 ("report_serial",
     78                                     &report_serial),
     79       GNUNET_PQ_result_spec_string ("report_description",
     80                                     &report_description),
     81       GNUNET_PQ_result_spec_relative_time ("frequency",
     82                                            &frequency),
     83       GNUNET_PQ_result_spec_end
     84     };
     85 
     86     if (GNUNET_OK !=
     87         GNUNET_PQ_extract_result (result,
     88                                   rs,
     89                                   i))
     90     {
     91       GNUNET_break (0);
     92       plc->extract_failed = true;
     93       return;
     94     }
     95     plc->cb (plc->cb_cls,
     96              report_serial,
     97              report_description,
     98              frequency);
     99     GNUNET_PQ_cleanup_result (rs);
    100   }
    101 }
    102 
    103 
    104 enum GNUNET_DB_QueryStatus
    105 TALER_MERCHANTDB_iterate_reports (
    106   struct TALER_MERCHANTDB_PostgresContext *pg,
    107   const char *instance_id,
    108   int64_t limit,
    109   uint64_t offset,
    110   TALER_MERCHANTDB_ReportsCallback cb,
    111   void *cb_cls)
    112 {
    113   uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
    114                                 TALER_MERCHANTDB_abs_limit (limit));
    115   struct SelectReportsContext plc = {
    116     .cb = cb,
    117     .cb_cls = cb_cls,
    118     /* Can be overwritten by the select_reports_cb */
    119     .extract_failed = false,
    120   };
    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   enum GNUNET_DB_QueryStatus qs;
    127 
    128   GNUNET_assert (NULL != pg->current_merchant_id);
    129   GNUNET_assert (0 == strcmp (instance_id,
    130                               pg->current_merchant_id));
    131   if (limit > 0)
    132   {
    133     TMH_PQ_prepare_anon (pg,
    134                          "SELECT"
    135                          "  report_serial"
    136                          " ,report_description"
    137                          " ,frequency"
    138                          " FROM merchant_reports"
    139                          " WHERE report_serial > $1"
    140                          "   AND NOT one_shot_hidden"
    141                          " ORDER BY report_serial ASC"
    142                          " LIMIT $2");
    143   }
    144   else
    145   {
    146     TMH_PQ_prepare_anon (pg,
    147                          "SELECT"
    148                          "  report_serial"
    149                          " ,report_description"
    150                          " ,frequency"
    151                          " FROM merchant_reports"
    152                          " WHERE report_serial < $1"
    153                          "   AND NOT one_shot_hidden"
    154                          " ORDER BY report_serial DESC"
    155                          " LIMIT $2");
    156   }
    157   qs = GNUNET_PQ_eval_prepared_multi_select (
    158     pg->conn,
    159     "",
    160     params,
    161     &select_reports_cb,
    162     &plc);
    163   /* If there was an error inside select_reports_cb, return a hard error. */
    164   if (plc.extract_failed)
    165   {
    166     GNUNET_break (0);
    167     return GNUNET_DB_STATUS_HARD_ERROR;
    168   }
    169   return qs;
    170 }