LCOV - code coverage report
Current view: top level - auditordb - pg_get_misattribution_in_inconsistency.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 29 35 82.9 %
Date: 2025-06-05 21:03:14 Functions: 2 2 100.0 %

          Line data    Source code
       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             : 
      17             : 
      18             : #include "platform.h"
      19             : #include "taler_error_codes.h"
      20             : #include "taler_dbevents.h"
      21             : #include "taler_pq_lib.h"
      22             : #include "pg_helper.h"
      23             : 
      24             : #include "pg_get_misattribution_in_inconsistency.h"
      25             : 
      26             : 
      27             : struct MisattributionInInconsistencyContext
      28             : {
      29             : 
      30             :   /**
      31             :    * Function to call for each bad sig loss.
      32             :    */
      33             :   TALER_AUDITORDB_MisattributionInInconsistencyCallback 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_misattribution_in_inconsistency().
      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 MisattributionInInconsistencyContext *`
      58             :  * @param result the postgres result
      59             :  * @param num_results the number of results in @a result
      60             :  */
      61             : static void
      62           1 : misattribution_in_inconsistency_cb (void *cls,
      63             :                                     PGresult *result,
      64             :                                     unsigned int num_results)
      65             : {
      66           1 :   struct MisattributionInInconsistencyContext *dcc = cls;
      67           1 :   struct PostgresClosure *pg = dcc->pg;
      68             : 
      69           2 :   for (unsigned int i = 0; i < num_results; i++)
      70             :   {
      71             :     struct TALER_AUDITORDB_MisattributionInInconsistency dc;
      72           1 :     struct GNUNET_PQ_ResultSpec rs[] = {
      73           1 :       GNUNET_PQ_result_spec_uint64 ("row_id",
      74             :                                     &dc.row_id),
      75           1 :       TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
      76             :                                    &dc.amount),
      77           1 :       GNUNET_PQ_result_spec_uint64 ("bank_row",
      78             :                                     &dc.bank_row),
      79           1 :       GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
      80             :                                             &dc.reserve_pub),
      81           1 :       GNUNET_PQ_result_spec_bool ("suppressed",
      82             :                                   &dc.suppressed),
      83             :       GNUNET_PQ_result_spec_end
      84             :     };
      85             :     enum GNUNET_GenericReturnValue rval;
      86             : 
      87           1 :     if (GNUNET_OK !=
      88           1 :         GNUNET_PQ_extract_result (result,
      89             :                                   rs,
      90             :                                   i))
      91             :     {
      92           0 :       GNUNET_break (0);
      93           0 :       dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
      94           0 :       return;
      95             :     }
      96           1 :     dcc->qs = i + 1;
      97           1 :     rval = dcc->cb (dcc->cb_cls,
      98             :                     &dc);
      99           1 :     GNUNET_PQ_cleanup_result (rs);
     100           1 :     if (GNUNET_OK != rval)
     101           0 :       break;
     102             :   }
     103             : }
     104             : 
     105             : 
     106             : enum GNUNET_DB_QueryStatus
     107           1 : TAH_PG_get_misattribution_in_inconsistency (
     108             :   void *cls,
     109             :   int64_t limit,
     110             :   uint64_t offset,
     111             :   bool return_suppressed,
     112             :   TALER_AUDITORDB_MisattributionInInconsistencyCallback cb,
     113             :   void *cb_cls)
     114             : {
     115           1 :   struct PostgresClosure *pg = cls;
     116           1 :   uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
     117           1 :   struct GNUNET_PQ_QueryParam params[] = {
     118           1 :     GNUNET_PQ_query_param_uint64 (&offset),
     119           1 :     GNUNET_PQ_query_param_bool (return_suppressed),
     120           1 :     GNUNET_PQ_query_param_uint64 (&plimit),
     121             :     GNUNET_PQ_query_param_end
     122             :   };
     123           1 :   struct MisattributionInInconsistencyContext dcc = {
     124             :     .cb = cb,
     125             :     .cb_cls = cb_cls,
     126             :     .pg = pg
     127             :   };
     128             :   enum GNUNET_DB_QueryStatus qs;
     129             : 
     130           1 :   PREPARE (pg,
     131             :            "auditor_misattribution_in_inconsistency_get_desc",
     132             :            "SELECT"
     133             :            " row_id"
     134             :            ",amount"
     135             :            ",bank_row"
     136             :            ",reserve_pub"
     137             :            ",suppressed"
     138             :            " FROM auditor_misattribution_in_inconsistency"
     139             :            " WHERE (row_id < $1)"
     140             :            " AND ($2 OR NOT suppressed)"
     141             :            " ORDER BY row_id DESC"
     142             :            " LIMIT $3"
     143             :            );
     144           1 :   PREPARE (pg,
     145             :            "auditor_misattribution_in_inconsistency_get_asc",
     146             :            "SELECT"
     147             :            " row_id"
     148             :            ",amount"
     149             :            ",bank_row"
     150             :            ",reserve_pub"
     151             :            ",suppressed"
     152             :            " FROM auditor_misattribution_in_inconsistency"
     153             :            " WHERE (row_id > $1)"
     154             :            " AND ($2 OR NOT suppressed)"
     155             :            " ORDER BY row_id ASC"
     156             :            " LIMIT $3"
     157             :            );
     158           1 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     159             :     pg->conn,
     160             :     (limit > 0)
     161             :     ? "auditor_misattribution_in_inconsistency_get_asc"
     162             :     : "auditor_misattribution_in_inconsistency_get_desc",
     163             :     params,
     164             :     &misattribution_in_inconsistency_cb,
     165             :     &dcc);
     166           1 :   if (qs > 0)
     167           1 :     return dcc.qs;
     168           0 :   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
     169           0 :   return qs;
     170             : }

Generated by: LCOV version 1.16