merchant

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

pg_select_money_pots.c (5173B)


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