exchange

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

get_progress_points.c (3600B)


      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  * @file src/auditordb/get_progress_points.c
     18  * @brief Implementation of the get_progress_points function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "auditor-database/get_progress_points.h"
     23 #include "pg_helper.h"
     24 
     25 
     26 struct ProgressContext
     27 {
     28 
     29   /**
     30    * Function to call for each progress point.
     31    */
     32   TALER_AUDITORDB_ProgressPointsCallback cb;
     33 
     34   /**
     35    * Closure for @e cb
     36    */
     37   void *cb_cls;
     38 
     39   /**
     40    * Plugin context.
     41    */
     42   struct TALER_AUDITORDB_PostgresContext *pg;
     43 
     44   /**
     45    * Query status to return.
     46    */
     47   enum GNUNET_DB_QueryStatus qs;
     48 };
     49 
     50 
     51 /**
     52  * Helper function for #TALER_AUDITORDB_get_progress_points().
     53  * To be called with the results of a SELECT statement
     54  * that has returned @a num_results results.
     55  *
     56  * @param cls closure of type `struct ProgressContext *`
     57  * @param result the postgres result
     58  * @param num_results the number of results in @a result
     59  */
     60 static void
     61 progress_cb (void *cls,
     62              PGresult *result,
     63              unsigned int num_results)
     64 {
     65   struct ProgressContext *dcc = cls;
     66 
     67   for (unsigned int i = 0; i < num_results; i++)
     68   {
     69     struct TALER_AUDITORDB_Progress dc;
     70     struct GNUNET_PQ_ResultSpec rs[] = {
     71       GNUNET_PQ_result_spec_string ("progress_key",
     72                                     &dc.progress_key),
     73       GNUNET_PQ_result_spec_uint64 ("progress_offset",
     74                                     &dc.progress_offset),
     75       GNUNET_PQ_result_spec_end
     76     };
     77     enum GNUNET_GenericReturnValue rval;
     78 
     79     if (GNUNET_OK !=
     80         GNUNET_PQ_extract_result (result,
     81                                   rs,
     82                                   i))
     83     {
     84       GNUNET_break (0);
     85       dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
     86       return;
     87     }
     88     dcc->qs = i + 1;
     89     rval = dcc->cb (dcc->cb_cls,
     90                     &dc);
     91     GNUNET_PQ_cleanup_result (rs);
     92     if (GNUNET_OK != rval)
     93       break;
     94   }
     95 }
     96 
     97 
     98 enum GNUNET_DB_QueryStatus
     99 TALER_AUDITORDB_get_progress_points (
    100   struct TALER_AUDITORDB_PostgresContext *pg,
    101   const char *progress_key,
    102   TALER_AUDITORDB_ProgressPointsCallback cb,
    103   void *cb_cls)
    104 {
    105   struct GNUNET_PQ_QueryParam params[] = {
    106     NULL == progress_key
    107     ? GNUNET_PQ_query_param_null ()
    108     : GNUNET_PQ_query_param_string (progress_key),
    109     GNUNET_PQ_query_param_end
    110   };
    111   struct ProgressContext dcc = {
    112     .cb = cb,
    113     .cb_cls = cb_cls,
    114     .pg = pg
    115   };
    116   enum GNUNET_DB_QueryStatus qs;
    117 
    118   PREPARE (pg,
    119            "auditor_progress_points_get",
    120            "SELECT"
    121            " progress_key"
    122            ",progress_offset"
    123            " FROM auditor_progress"
    124            " WHERE ($1::TEXT IS NULL OR progress_key = $1)"
    125            );
    126   qs = GNUNET_PQ_eval_prepared_multi_select (
    127     pg->conn,
    128     "auditor_progress_points_get",
    129     params,
    130     &progress_cb,
    131     &dcc);
    132   if (qs > 0)
    133     return dcc.qs;
    134   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
    135   return qs;
    136 }