exchange

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

update_balance.c (3183B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2023 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/update_balance.c
     18  * @brief Implementation of the update_balance function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "auditor-database/update_balance.h"
     23 #include "pg_helper.h"
     24 
     25 
     26 enum GNUNET_DB_QueryStatus
     27 TALER_AUDITORDB_update_balance (struct TALER_AUDITORDB_PostgresContext *pg,
     28                                 const char *balance_key,
     29                                 const struct TALER_Amount *balance_amount,
     30                                 ...)
     31 {
     32   unsigned int cnt = 1;
     33   va_list ap;
     34 
     35   va_start (ap,
     36             balance_amount);
     37   while (NULL != va_arg (ap,
     38                          const char *))
     39   {
     40     cnt++;
     41     (void) va_arg (ap,
     42                    const struct TALER_Amount *);
     43   }
     44   va_end (ap);
     45   {
     46     const char *keys[cnt];
     47     struct TALER_Amount amounts[cnt];
     48     unsigned int off = 1;
     49     struct GNUNET_PQ_QueryParam params[] = {
     50       GNUNET_PQ_query_param_array_ptrs_string (cnt,
     51                                                keys,
     52                                                pg->conn),
     53       TALER_PQ_query_param_array_amount (cnt,
     54                                          amounts,
     55                                          pg->conn),
     56       GNUNET_PQ_query_param_end
     57     };
     58     enum GNUNET_DB_QueryStatus qs;
     59 
     60     keys[0] = balance_key;
     61     amounts[0] = *balance_amount;
     62 
     63     va_start (ap,
     64               balance_amount);
     65     while (off < cnt)
     66     {
     67       keys[off] = va_arg (ap,
     68                           const char *);
     69       amounts[off] = *va_arg (ap,
     70                               const struct TALER_Amount *);
     71       off++;
     72     }
     73     GNUNET_assert (NULL == va_arg (ap,
     74                                    const char *));
     75     va_end (ap);
     76 
     77     PREPARE (pg,
     78              "auditor_balance_update",
     79              "UPDATE auditor_balances"
     80              "  SET balance_value.val=data.val"
     81              "     ,balance_value.frac=data.frac"
     82              "  FROM ("
     83              "    SELECT *"
     84              "      FROM UNNEST (CAST($1 AS TEXT[]),"
     85              "                   CAST($2 AS taler_amount[]))"
     86              "      AS t(key,val,frac)"
     87              "  ) AS data"
     88              " WHERE auditor_balances.balance_key=data.key;");
     89     qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
     90                                              "auditor_balance_update",
     91                                              params);
     92     GNUNET_PQ_cleanup_query_params_closures (params);
     93     return qs;
     94   }
     95 }