Line data Source code
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 12 : TALER_AUDITORDB_update_balance (struct TALER_AUDITORDB_PostgresContext *pg,
28 : const char *balance_key,
29 : const struct TALER_Amount *balance_amount,
30 : ...)
31 : {
32 12 : unsigned int cnt = 1;
33 : va_list ap;
34 :
35 12 : va_start (ap,
36 : balance_amount);
37 88 : while (NULL != va_arg (ap,
38 : const char *))
39 : {
40 76 : cnt++;
41 76 : (void) va_arg (ap,
42 : const struct TALER_Amount *);
43 : }
44 12 : va_end (ap);
45 12 : {
46 12 : const char *keys[cnt];
47 12 : struct TALER_Amount amounts[cnt];
48 12 : unsigned int off = 1;
49 12 : struct GNUNET_PQ_QueryParam params[] = {
50 12 : GNUNET_PQ_query_param_array_ptrs_string (cnt,
51 : keys,
52 : pg->conn),
53 12 : 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 12 : keys[0] = balance_key;
61 12 : amounts[0] = *balance_amount;
62 :
63 12 : va_start (ap,
64 : balance_amount);
65 88 : while (off < cnt)
66 : {
67 76 : keys[off] = va_arg (ap,
68 : const char *);
69 76 : amounts[off] = *va_arg (ap,
70 : const struct TALER_Amount *);
71 76 : off++;
72 : }
73 12 : GNUNET_assert (NULL == va_arg (ap,
74 : const char *));
75 12 : va_end (ap);
76 :
77 12 : 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 12 : qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
90 : "auditor_balance_update",
91 : params);
92 12 : GNUNET_PQ_cleanup_query_params_closures (params);
93 12 : return qs;
94 : }
95 : }
|