LCOV - code coverage report
Current view: top level - backenddb - pg_select_reports.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 30 0
Test Date: 2026-01-01 16:44:56 Functions: 0.0 % 2 0

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

Generated by: LCOV version 2.0-1