merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

lookup_otp_devices.c (3832B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022 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/backenddb/lookup_otp_devices.c
     18  * @brief Implementation of the lookup_otp_devices function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/lookup_otp_devices.h"
     24 #include "helper.h"
     25 
     26 
     27 /**
     28  * Context used for TALER_MERCHANTDB_lookup_otp_devices().
     29  */
     30 struct LookupOtpDeviceContext
     31 {
     32   /**
     33    * Function to call with the results.
     34    */
     35   TALER_MERCHANTDB_OtpDeviceCallback cb;
     36 
     37   /**
     38    * Closure for @a cb.
     39    */
     40   void *cb_cls;
     41 
     42   /**
     43    * Did database result extraction fail?
     44    */
     45   bool extract_failed;
     46 };
     47 
     48 
     49 /**
     50  * Function to be called with the results of a SELECT statement
     51  * that has returned @a num_results results about otp_device.
     52  *
     53  * @param[in,out] cls of type `struct LookupOtpDeviceContext *`
     54  * @param result the postgres result
     55  * @param num_results the number of results in @a result
     56  */
     57 static void
     58 lookup_otp_devices_cb (void *cls,
     59                        PGresult *result,
     60                        unsigned int num_results)
     61 {
     62   struct LookupOtpDeviceContext *tlc = cls;
     63 
     64   for (unsigned int i = 0; i < num_results; i++)
     65   {
     66     char *otp_device_id;
     67     char *otp_device_description;
     68     struct GNUNET_PQ_ResultSpec rs[] = {
     69       GNUNET_PQ_result_spec_string ("otp_id",
     70                                     &otp_device_id),
     71       GNUNET_PQ_result_spec_string ("otp_description",
     72                                     &otp_device_description),
     73       GNUNET_PQ_result_spec_end
     74     };
     75 
     76     if (GNUNET_OK !=
     77         GNUNET_PQ_extract_result (result,
     78                                   rs,
     79                                   i))
     80     {
     81       GNUNET_break (0);
     82       tlc->extract_failed = true;
     83       return;
     84     }
     85     tlc->cb (tlc->cb_cls,
     86              otp_device_id,
     87              otp_device_description);
     88     GNUNET_PQ_cleanup_result (rs);
     89   }
     90 }
     91 
     92 
     93 enum GNUNET_DB_QueryStatus
     94 TALER_MERCHANTDB_lookup_otp_devices (
     95   struct TALER_MERCHANTDB_PostgresContext *pg,
     96   const char *instance_id,
     97   TALER_MERCHANTDB_OtpDeviceCallback cb,
     98   void *cb_cls)
     99 {
    100   struct LookupOtpDeviceContext tlc = {
    101     .cb = cb,
    102     .cb_cls = cb_cls,
    103     /* Can be overwritten by the lookup_otp_device_cb */
    104     .extract_failed = false,
    105   };
    106   struct GNUNET_PQ_QueryParam params[] = {
    107     GNUNET_PQ_query_param_end
    108   };
    109   enum GNUNET_DB_QueryStatus qs;
    110 
    111   GNUNET_assert (NULL != pg->current_merchant_id);
    112   GNUNET_assert (0 == strcmp (instance_id,
    113                               pg->current_merchant_id));
    114   check_connection (pg);
    115   TMH_PQ_prepare_anon (pg,
    116                        "SELECT"
    117                        " otp_id"
    118                        ",otp_description"
    119                        " FROM merchant_otp_devices");
    120   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    121                                              "",
    122                                              params,
    123                                              &lookup_otp_devices_cb,
    124                                              &tlc);
    125   /* If there was an error inside lookup_otp_device_cb, return a hard error. */
    126   if (tlc.extract_failed)
    127     return GNUNET_DB_STATUS_HARD_ERROR;
    128   return qs;
    129 }