exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

get_row_inconsistency.c (4682B)


      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 #include "taler/taler_pq_lib.h"
     18 #include "pg_helper.h"
     19 #include "auditor-database/get_row_inconsistency.h"
     20 
     21 /**
     22  * Closure for #deposit_confirmation_cb().
     23  */
     24 struct RowInconsistencyContext
     25 {
     26 
     27   /**
     28    * Function to call for each deposit confirmation.
     29    */
     30   TALER_AUDITORDB_RowInconsistencyCallback cb;
     31 
     32   /**
     33    * Closure for @e cb
     34    */
     35   void *cb_cls;
     36 
     37   /**
     38    * Plugin context.
     39    */
     40   struct TALER_AUDITORDB_PostgresContext *pg;
     41 
     42   /**
     43    * Query status to return.
     44    */
     45   enum GNUNET_DB_QueryStatus qs;
     46 };
     47 
     48 
     49 /**
     50  * Helper function for #TALER_AUDITORDB_get_deposit_confirmations().
     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 DepositConfirmationContext *`
     55  * @param result the postgres result
     56  * @param num_results the number of results in @a result
     57  */
     58 static void
     59 row_inconsistency_cb (void *cls,
     60                       PGresult *result,
     61                       unsigned int num_results)
     62 {
     63   struct RowInconsistencyContext *dcc = cls;
     64 
     65   for (unsigned int i = 0; i < num_results; i++)
     66   {
     67     uint64_t serial_id;
     68 
     69     struct TALER_AUDITORDB_RowInconsistency dc;
     70 
     71     struct GNUNET_PQ_ResultSpec rs[] = {
     72       GNUNET_PQ_result_spec_uint64 ("row_id", &serial_id),
     73 
     74       GNUNET_PQ_result_spec_string ("row_table", &dc.row_table),
     75       GNUNET_PQ_result_spec_string ("diagnostic", &dc.diagnostic),
     76       GNUNET_PQ_result_spec_bool ("suppressed",  &dc.suppressed),
     77 
     78 
     79       GNUNET_PQ_result_spec_end
     80     };
     81     enum GNUNET_GenericReturnValue rval;
     82 
     83     if (GNUNET_OK !=
     84         GNUNET_PQ_extract_result (result,
     85                                   rs,
     86                                   i))
     87     {
     88       GNUNET_break (0);
     89       dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
     90       return;
     91     }
     92 
     93     dcc->qs = i + 1;
     94 
     95 
     96     rval = dcc->cb (dcc->cb_cls,
     97                     serial_id,
     98                     &dc);
     99     GNUNET_PQ_cleanup_result (rs);
    100     if (GNUNET_OK != rval)
    101       break;
    102   }
    103 }
    104 
    105 
    106 enum GNUNET_DB_QueryStatus
    107 TALER_AUDITORDB_get_row_inconsistency (
    108   struct TALER_AUDITORDB_PostgresContext *pg,
    109   int64_t limit,
    110   uint64_t offset,
    111   bool return_suppressed,            // maybe not needed
    112   TALER_AUDITORDB_RowInconsistencyCallback cb,
    113   void *cb_cls)
    114 {
    115 
    116   uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
    117   struct GNUNET_PQ_QueryParam params[] = {
    118     GNUNET_PQ_query_param_uint64 (&offset),
    119     GNUNET_PQ_query_param_bool (return_suppressed),
    120     GNUNET_PQ_query_param_uint64 (&plimit),
    121     GNUNET_PQ_query_param_end
    122   };
    123   struct RowInconsistencyContext dcc = {
    124     .cb = cb,
    125     .cb_cls = cb_cls,
    126     .pg = pg
    127   };
    128   enum GNUNET_DB_QueryStatus qs;
    129 
    130 
    131   PREPARE (pg,
    132            "auditor_row_inconsistency_select_desc",
    133            "SELECT"
    134            " row_id"
    135            ",row_table"
    136            ",diagnostic"
    137            ",suppressed"
    138            " FROM auditor_row_inconsistency"
    139            " WHERE (row_id < $1)"
    140            " AND ($2 OR suppressed is false)"
    141            " ORDER BY row_id DESC"
    142            " LIMIT $3"
    143            );
    144   PREPARE (pg,
    145            "auditor_row_inconsistency_select_asc",
    146            "SELECT"
    147            " row_id"
    148            ",row_table"
    149            ",diagnostic"
    150            ",suppressed"
    151            " FROM auditor_row_inconsistency"
    152            " WHERE (row_id > $1)"
    153            " AND ($2 OR suppressed is false)"
    154            " ORDER BY row_id DESC"
    155            " LIMIT $3"
    156            );
    157   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    158                                              (limit > 0) ?
    159                                              "auditor_row_inconsistency_select_asc"
    160   :
    161                                              "auditor_row_inconsistency_select_desc",
    162                                              params,
    163                                              &row_inconsistency_cb,
    164                                              &dcc);
    165 
    166 
    167   if (qs > 0)
    168     return dcc.qs;
    169   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
    170   return qs;
    171 }