LCOV - code coverage report
Current view: top level - auditordb - pg_get_row_minor_inconsistencies.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 28 34 82.4 %
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             : #include "platform.h"
      17             : #include "taler_error_codes.h"
      18             : #include "taler_dbevents.h"
      19             : #include "taler_pq_lib.h"
      20             : #include "pg_helper.h"
      21             : #include "pg_get_row_minor_inconsistencies.h"
      22             : 
      23             : 
      24             : struct RowMinorInconsistenciesContext
      25             : {
      26             : 
      27             :   /**
      28             :    * Function to call for each bad sig loss.
      29             :    */
      30             :   TALER_AUDITORDB_RowMinorInconsistenciesCallback cb;
      31             : 
      32             :   /**
      33             :    * Closure for @e cb
      34             :    */
      35             :   void *cb_cls;
      36             : 
      37             :   /**
      38             :    * Plugin context.
      39             :    */
      40             :   struct PostgresClosure *pg;
      41             : 
      42             :   /**
      43             :    * Query status to return.
      44             :    */
      45             :   enum GNUNET_DB_QueryStatus qs;
      46             : };
      47             : 
      48             : 
      49             : /**
      50             :  * Helper function for #TAH_PG_get_row_minor_inconsistencies().
      51             :  * To be called with the results of a SELECT statement
      52             :  * that has returned @a num_results results.
      53             :  *
      54             :  * @param cls closure of type `struct RowMinorInconsistenciesContext *`
      55             :  * @param result the postgres result
      56             :  * @param num_results the number of results in @a result
      57             :  */
      58             : static void
      59           5 : row_minor_inconsistencies_cb (void *cls,
      60             :                               PGresult *result,
      61             :                               unsigned int num_results)
      62             : {
      63           5 :   struct RowMinorInconsistenciesContext *dcc = cls;
      64             : 
      65          10 :   for (unsigned int i = 0; i < num_results; i++)
      66             :   {
      67             :     struct TALER_AUDITORDB_RowMinorInconsistencies dc;
      68           5 :     struct GNUNET_PQ_ResultSpec rs[] = {
      69           5 :       GNUNET_PQ_result_spec_uint64 ("row_id",
      70             :                                     &dc.row_id),
      71           5 :       GNUNET_PQ_result_spec_string ("row_table",
      72             :                                     &dc.row_table),
      73           5 :       GNUNET_PQ_result_spec_uint64 ("problem_row",
      74             :                                     &dc.problem_row),
      75           5 :       GNUNET_PQ_result_spec_string ("diagnostic",
      76             :                                     &dc.diagnostic),
      77           5 :       GNUNET_PQ_result_spec_bool ("suppressed",
      78             :                                   &dc.suppressed),
      79             :       GNUNET_PQ_result_spec_end
      80             :     };
      81             :     enum GNUNET_GenericReturnValue rval;
      82             : 
      83           5 :     if (GNUNET_OK !=
      84           5 :         GNUNET_PQ_extract_result (result,
      85             :                                   rs,
      86             :                                   i))
      87             :     {
      88           0 :       GNUNET_break (0);
      89           0 :       dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
      90           0 :       return;
      91             :     }
      92           5 :     dcc->qs = i + 1;
      93           5 :     rval = dcc->cb (dcc->cb_cls,
      94             :                     &dc);
      95           5 :     GNUNET_PQ_cleanup_result (rs);
      96           5 :     if (GNUNET_OK != rval)
      97           0 :       break;
      98             :   }
      99             : }
     100             : 
     101             : 
     102             : enum GNUNET_DB_QueryStatus
     103           5 : TAH_PG_get_row_minor_inconsistencies (
     104             :   void *cls,
     105             :   int64_t limit,
     106             :   uint64_t offset,
     107             :   bool return_suppressed,
     108             :   TALER_AUDITORDB_RowMinorInconsistenciesCallback cb,
     109             :   void *cb_cls)
     110             : {
     111           5 :   struct PostgresClosure *pg = cls;
     112           5 :   uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
     113           5 :   struct GNUNET_PQ_QueryParam params[] = {
     114           5 :     GNUNET_PQ_query_param_uint64 (&offset),
     115           5 :     GNUNET_PQ_query_param_bool (return_suppressed),
     116           5 :     GNUNET_PQ_query_param_uint64 (&plimit),
     117             :     GNUNET_PQ_query_param_end
     118             :   };
     119           5 :   struct RowMinorInconsistenciesContext dcc = {
     120             :     .cb = cb,
     121             :     .cb_cls = cb_cls,
     122             :     .pg = pg
     123             :   };
     124             :   enum GNUNET_DB_QueryStatus qs;
     125             : 
     126           5 :   PREPARE (pg,
     127             :            "auditor_row_minor_inconsistencies_get_desc",
     128             :            "SELECT"
     129             :            " row_id"
     130             :            ",problem_row"
     131             :            ",row_table"
     132             :            ",diagnostic"
     133             :            ",suppressed"
     134             :            " FROM auditor_row_minor_inconsistencies"
     135             :            " WHERE (row_id < $1)"
     136             :            " AND ($2 OR NOT suppressed)"
     137             :            " ORDER BY row_id DESC"
     138             :            " LIMIT $3"
     139             :            );
     140           5 :   PREPARE (pg,
     141             :            "auditor_row_minor_inconsistencies_get_asc",
     142             :            "SELECT"
     143             :            " row_id"
     144             :            ",problem_row"
     145             :            ",row_table"
     146             :            ",diagnostic"
     147             :            ",suppressed"
     148             :            " FROM auditor_row_minor_inconsistencies"
     149             :            " WHERE (row_id > $1)"
     150             :            " AND ($2 OR NOT suppressed)"
     151             :            " ORDER BY row_id ASC"
     152             :            " LIMIT $3"
     153             :            );
     154           5 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     155             :     pg->conn,
     156             :     (limit > 0)
     157             :     ? "auditor_row_minor_inconsistencies_get_asc"
     158             :     : "auditor_row_minor_inconsistencies_get_desc",
     159             :     params,
     160             :     &row_minor_inconsistencies_cb,
     161             :     &dcc);
     162           5 :   if (qs > 0)
     163           5 :     return dcc.qs;
     164           0 :   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
     165           0 :   return qs;
     166             : }

Generated by: LCOV version 1.16