LCOV - code coverage report
Current view: top level - backenddb - iterate_pending_reports.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 83.9 % 31 26
Test Date: 2026-09-04 23:42:01 Functions: 100.0 % 2 2

            Line data    Source code
       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_pending_reports.c
      18              :  * @brief Implementation of the iterate_pending_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_pending_reports.h"
      24              : #include "helper.h"
      25              : 
      26              : 
      27              : /**
      28              :  * Context used for TALER_MERCHANTDB_iterate_pending_reports().
      29              :  */
      30              : struct SelectReportsContext
      31              : {
      32              :   /**
      33              :    * Function to call with the results.
      34              :    */
      35              :   TALER_MERCHANTDB_ReportsPendingCallback cb;
      36              : 
      37              :   /**
      38              :    * Closure for @a cb.
      39              :    */
      40              :   void *cb_cls;
      41              : 
      42              :   /**
      43              :    * Did database result extraction fail?
      44              :    */
      45              :   bool extract_failed;
      46              : };
      47              : 
      48              : 
      49              : /**
      50              :  * Function to be called with the results of a SELECT statement
      51              :  * that has returned @a num_results results about reports.
      52              :  *
      53              :  * @param[in,out] cls of type `struct SelectReportsContext *`
      54              :  * @param result the postgres result
      55              :  * @param num_results the number of results in @a result
      56              :  */
      57              : static void
      58            1 : select_pending_reports_cb (void *cls,
      59              :                            PGresult *result,
      60              :                            unsigned int num_results)
      61              : {
      62            1 :   struct SelectReportsContext *plc = cls;
      63              : 
      64            2 :   for (unsigned int i = 0; i < num_results; i++)
      65              :   {
      66              :     char *instance_id;
      67              :     uint64_t report_serial;
      68              :     char *report_program_section;
      69              :     char *report_description;
      70              :     char *mime_type;
      71              :     struct TALER_MERCHANT_ReportToken report_token;
      72              :     char *target_address;
      73              :     struct GNUNET_TIME_Relative frequency;
      74              :     struct GNUNET_TIME_Relative frequency_shift;
      75              :     struct GNUNET_TIME_Absolute next_transmission;
      76              :     bool one_shot;
      77            1 :     struct GNUNET_PQ_ResultSpec rs[] = {
      78            1 :       GNUNET_PQ_result_spec_string ("out_merchant_id",
      79              :                                     &instance_id),
      80            1 :       GNUNET_PQ_result_spec_uint64 ("out_report_serial",
      81              :                                     &report_serial),
      82            1 :       GNUNET_PQ_result_spec_string ("out_report_program_section",
      83              :                                     &report_program_section),
      84            1 :       GNUNET_PQ_result_spec_string ("out_report_description",
      85              :                                     &report_description),
      86            1 :       GNUNET_PQ_result_spec_string ("out_mime_type",
      87              :                                     &mime_type),
      88            1 :       GNUNET_PQ_result_spec_auto_from_type ("out_report_token",
      89              :                                             &report_token),
      90            1 :       GNUNET_PQ_result_spec_string ("out_target_address",
      91              :                                     &target_address),
      92            1 :       GNUNET_PQ_result_spec_relative_time ("out_frequency",
      93              :                                            &frequency),
      94            1 :       GNUNET_PQ_result_spec_relative_time ("out_frequency_shift",
      95              :                                            &frequency_shift),
      96            1 :       GNUNET_PQ_result_spec_absolute_time ("out_next_transmission",
      97              :                                            &next_transmission),
      98            1 :       GNUNET_PQ_result_spec_bool ("out_one_shot_hidden",
      99              :                                   &one_shot),
     100              :       GNUNET_PQ_result_spec_end
     101              :     };
     102              : 
     103            1 :     if (GNUNET_OK !=
     104            1 :         GNUNET_PQ_extract_result (result,
     105              :                                   rs,
     106              :                                   i))
     107              :     {
     108            0 :       GNUNET_break (0);
     109            0 :       plc->extract_failed = true;
     110            0 :       return;
     111              :     }
     112            1 :     plc->cb (plc->cb_cls,
     113              :              instance_id,
     114              :              report_serial,
     115              :              report_program_section,
     116              :              report_description,
     117              :              mime_type,
     118              :              &report_token,
     119              :              target_address,
     120              :              frequency,
     121              :              frequency_shift,
     122              :              next_transmission,
     123              :              one_shot);
     124            1 :     GNUNET_PQ_cleanup_result (rs);
     125              :   }
     126              : }
     127              : 
     128              : 
     129              : enum GNUNET_DB_QueryStatus
     130            1 : TALER_MERCHANTDB_iterate_pending_reports (
     131              :   struct TALER_MERCHANTDB_PostgresContext *pg,
     132              :   TALER_MERCHANTDB_ReportsPendingCallback cb,
     133              :   void *cb_cls)
     134              : {
     135            1 :   struct SelectReportsContext plc = {
     136              :     .cb = cb,
     137              :     .cb_cls = cb_cls,
     138              :     /* Can be overwritten by the lookup_reports_cb */
     139              :     .extract_failed = false,
     140              :   };
     141            1 :   struct GNUNET_PQ_QueryParam params[] = {
     142              :     GNUNET_PQ_query_param_end
     143              :   };
     144              :   enum GNUNET_DB_QueryStatus qs;
     145              : 
     146            1 :   PREPARE (pg,
     147              :            "iterate_pending_reports",
     148              :            "SELECT"
     149              :            "  out_merchant_id"
     150              :            " ,out_report_serial"
     151              :            " ,out_report_program_section"
     152              :            " ,out_report_description"
     153              :            " ,out_mime_type"
     154              :            " ,out_report_token"
     155              :            " ,out_target_address"
     156              :            " ,out_frequency"
     157              :            " ,out_frequency_shift"
     158              :            " ,out_next_transmission"
     159              :            " ,out_one_shot_hidden"
     160              :            " FROM merchant.lookup_reports_pending()");
     161            1 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     162              :     pg->conn,
     163              :     "iterate_pending_reports",
     164              :     params,
     165              :     &select_pending_reports_cb,
     166              :     &plc);
     167              :   /* If there was an error inside select_pending_reports_cb, return a hard error. */
     168            1 :   if (plc.extract_failed)
     169              :   {
     170            0 :     GNUNET_break (0);
     171            0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     172              :   }
     173            1 :   return qs;
     174              : }
        

Generated by: LCOV version 2.0-1