sync

Backup service to store encrypted wallet databases (experimental)
Log | Files | Refs | Submodules | README | LICENSE

syncdb_lookup_pending_payments_by_account_TR.c (4002B)


      1 /*
      2   This file is part of TALER
      3   (C) 2014--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 Lesser 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 syncdb/syncdb_lookup_pending_payments_by_account_TR.c
     18  * @brief lookup pending payments by account
     19  * @author Christian Grothoff
     20  */
     21 #include "syncdb_pg.h"
     22 
     23 
     24 /**
     25  * Closure for #payment_by_account_cb.
     26  */
     27 struct PaymentIteratorContext
     28 {
     29   /**
     30    * Function to call on each result
     31    */
     32   SYNC_DB_PaymentPendingIterator it;
     33 
     34   /**
     35    * Closure for @e it.
     36    */
     37   void *it_cls;
     38 
     39   /**
     40    * Query status to return.
     41    */
     42   enum GNUNET_DB_QueryStatus qs;
     43 
     44 };
     45 
     46 
     47 /**
     48  * Helper function for #SYNCDB_lookup_pending_payments_by_account_TR().
     49  * To be called with the results of a SELECT statement
     50  * that has returned @a num_results results.
     51  *
     52  * @param cls closure of type `struct PaymentIteratorContext *`
     53  * @param result the postgres result
     54  * @param num_results the number of results in @a result
     55  */
     56 static void
     57 payment_by_account_cb (void *cls,
     58                        PGresult *result,
     59                        unsigned int num_results)
     60 {
     61   struct PaymentIteratorContext *pic = cls;
     62 
     63   for (unsigned int i = 0; i < num_results; i++)
     64   {
     65     struct GNUNET_TIME_Timestamp timestamp;
     66     char *order_id;
     67     struct TALER_Amount amount;
     68     struct TALER_ClaimTokenP token;
     69     struct GNUNET_PQ_ResultSpec rs[] = {
     70       GNUNET_PQ_result_spec_timestamp ("timestamp",
     71                                        &timestamp),
     72       GNUNET_PQ_result_spec_string ("order_id",
     73                                     &order_id),
     74       GNUNET_PQ_result_spec_auto_from_type ("token",
     75                                             &token),
     76       TALER_PQ_result_spec_amount ("amount",
     77                                    pg->currency,
     78                                    &amount),
     79       GNUNET_PQ_result_spec_end
     80     };
     81 
     82     if (GNUNET_OK !=
     83         GNUNET_PQ_extract_result (result,
     84                                   rs,
     85                                   i))
     86     {
     87       GNUNET_break (0);
     88       pic->qs = GNUNET_DB_STATUS_HARD_ERROR;
     89       return;
     90     }
     91     pic->qs = i + 1;
     92     pic->it (pic->it_cls,
     93              timestamp,
     94              order_id,
     95              &token,
     96              &amount);
     97     GNUNET_PQ_cleanup_result (rs);
     98   }
     99 }
    100 
    101 
    102 enum GNUNET_DB_QueryStatus
    103 SYNCDB_lookup_pending_payments_by_account_TR (
    104   const struct SYNC_AccountPublicKeyP *account_pub,
    105   SYNC_DB_PaymentPendingIterator it,
    106   void *it_cls)
    107 {
    108   struct GNUNET_PQ_QueryParam params[] = {
    109     GNUNET_PQ_query_param_auto_from_type (account_pub),
    110     GNUNET_PQ_query_param_end
    111   };
    112   struct PaymentIteratorContext pic = {
    113     .it = it,
    114     .it_cls = it_cls
    115   };
    116   enum GNUNET_DB_QueryStatus qs;
    117 
    118   SYNCDB_preflight ();
    119   PREPARE ("payments_select_by_account",
    120            "SELECT"
    121            " timestamp"
    122            ",order_id"
    123            ",token"
    124            ",amount"
    125            " FROM payments"
    126            " WHERE"
    127            "  paid=FALSE"
    128            " AND"
    129            "  account_pub=$1;");
    130   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    131                                              "payments_select_by_account",
    132                                              params,
    133                                              &payment_by_account_cb,
    134                                              &pic);
    135   if (qs > 0)
    136     return pic.qs;
    137   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
    138   return qs;
    139 }
    140 
    141 
    142 /* end of syncdb_lookup_pending_payments_by_account_TR.c */