merchant

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

select_money_pots.c (5082B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2025 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/select_money_pots.c
     18  * @brief Implementation of the select_money_pots function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/select_money_pots.h"
     24 #include "helper.h"
     25 
     26 
     27 /**
     28  * Context used for TALER_MERCHANTDB_lookup_money_pots().
     29  */
     30 struct LookupMoneyPotsContext
     31 {
     32   /**
     33    * DB context.
     34    */
     35   struct TALER_MERCHANTDB_PostgresContext *pg;
     36 
     37   /**
     38    * Function to call with the results.
     39    */
     40   TALER_MERCHANTDB_MoneyPotsCallback cb;
     41 
     42   /**
     43    * Closure for @a cb.
     44    */
     45   void *cb_cls;
     46 
     47   /**
     48    * Did database result extraction fail?
     49    */
     50   bool extract_failed;
     51 };
     52 
     53 
     54 /**
     55  * Function to be called with the results of a SELECT statement
     56  * that has returned @a num_results results about money pots.
     57  *
     58  * @param[in,out] cls of type `struct LookupMoneyPotsContext *`
     59  * @param result the postgres result
     60  * @param num_results the number of results in @a result
     61  */
     62 static void
     63 lookup_money_pots_cb (void *cls,
     64                       PGresult *result,
     65                       unsigned int num_results)
     66 {
     67   struct LookupMoneyPotsContext *plc = cls;
     68 
     69   for (unsigned int i = 0; i < num_results; i++)
     70   {
     71     uint64_t money_pot_serial;
     72     char *money_pot_name;
     73     size_t pot_total_len;
     74     struct TALER_Amount *pot_totals;
     75     struct GNUNET_PQ_ResultSpec rs[] = {
     76       GNUNET_PQ_result_spec_uint64 ("money_pot_serial",
     77                                     &money_pot_serial),
     78       GNUNET_PQ_result_spec_string ("money_pot_name",
     79                                     &money_pot_name),
     80       TALER_PQ_result_spec_array_amount_with_currency (plc->pg->conn,
     81                                                        "merchant",
     82                                                        "pot_totals",
     83                                                        &pot_total_len,
     84                                                        &pot_totals),
     85       GNUNET_PQ_result_spec_end
     86     };
     87 
     88     if (GNUNET_OK !=
     89         GNUNET_PQ_extract_result (result,
     90                                   rs,
     91                                   i))
     92     {
     93       GNUNET_break (0);
     94       plc->extract_failed = true;
     95       return;
     96     }
     97     plc->cb (plc->cb_cls,
     98              money_pot_serial,
     99              money_pot_name,
    100              pot_total_len,
    101              pot_totals);
    102     GNUNET_PQ_cleanup_result (rs);
    103   }
    104 }
    105 
    106 
    107 enum GNUNET_DB_QueryStatus
    108 TALER_MERCHANTDB_select_money_pots (
    109   struct TALER_MERCHANTDB_PostgresContext *pg,
    110   const char *instance_id,
    111   int64_t limit,
    112   uint64_t offset,
    113   TALER_MERCHANTDB_MoneyPotsCallback cb,
    114   void *cb_cls)
    115 {
    116   uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
    117   struct LookupMoneyPotsContext plc = {
    118     .pg = pg,
    119     .cb = cb,
    120     .cb_cls = cb_cls,
    121     /* Can be overwritten by the lookup_money_pots_cb */
    122     .extract_failed = false,
    123   };
    124   struct GNUNET_PQ_QueryParam params[] = {
    125     GNUNET_PQ_query_param_uint64 (&offset),
    126     GNUNET_PQ_query_param_uint64 (&plimit),
    127     GNUNET_PQ_query_param_end
    128   };
    129   enum GNUNET_DB_QueryStatus qs;
    130 
    131   GNUNET_assert (NULL != pg->current_merchant_id);
    132   GNUNET_assert (0 == strcmp (instance_id,
    133                               pg->current_merchant_id));
    134   check_connection (pg);
    135   if (limit > 0)
    136   {
    137     TMH_PQ_prepare_anon (pg,
    138                          "SELECT"
    139                          "  money_pot_serial"
    140                          " ,money_pot_name"
    141                          " ,pot_totals"
    142                          " FROM merchant_money_pots"
    143                          " WHERE money_pot_serial > $1"
    144                          " ORDER BY money_pot_serial ASC"
    145                          " LIMIT $2");
    146   }
    147   else
    148   {
    149     TMH_PQ_prepare_anon (pg,
    150                          "SELECT"
    151                          "  money_pot_serial"
    152                          " ,money_pot_name"
    153                          " ,pot_totals"
    154                          " FROM merchant_money_pots"
    155                          " WHERE money_pot_serial < $1"
    156                          " ORDER BY money_pot_serial DESC"
    157                          " LIMIT $2");
    158   }
    159   qs = GNUNET_PQ_eval_prepared_multi_select (
    160     pg->conn,
    161     "",
    162     params,
    163     &lookup_money_pots_cb,
    164     &plc);
    165   /* If there was an error inside lookup_money_pots_cb, return a hard error. */
    166   if (plc.extract_failed)
    167   {
    168     GNUNET_break (0);
    169     return GNUNET_DB_STATUS_HARD_ERROR;
    170   }
    171   return qs;
    172 }