Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2024 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 insert_auditor_progress.c
18 : * @brief Low-level (statement-level) Postgres database access for the exchange
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "auditor-database/insert_auditor_progress.h"
23 : #include "pg_helper.h"
24 :
25 :
26 : enum GNUNET_DB_QueryStatus
27 25 : TALER_AUDITORDB_insert_auditor_progress (
28 : struct TALER_AUDITORDB_PostgresContext *pg,
29 : const char *progress_key,
30 : uint64_t progress_offset,
31 : ...)
32 : {
33 25 : unsigned int cnt = 1;
34 : va_list ap;
35 :
36 25 : va_start (ap,
37 : progress_offset);
38 107 : while (NULL != va_arg (ap,
39 : const char *))
40 : {
41 82 : cnt++;
42 82 : (void) va_arg (ap,
43 : uint64_t);
44 : }
45 25 : va_end (ap);
46 25 : {
47 25 : const char *keys[cnt];
48 25 : uint64_t offsets[cnt];
49 25 : unsigned int off = 1;
50 25 : struct GNUNET_PQ_QueryParam params[] = {
51 25 : GNUNET_PQ_query_param_array_ptrs_string (cnt,
52 : keys,
53 : pg->conn),
54 25 : GNUNET_PQ_query_param_array_uint64 (cnt,
55 : offsets,
56 : pg->conn),
57 : GNUNET_PQ_query_param_end
58 : };
59 : enum GNUNET_DB_QueryStatus qs;
60 :
61 25 : keys[0] = progress_key;
62 25 : offsets[0] = progress_offset;
63 :
64 25 : va_start (ap,
65 : progress_offset);
66 107 : while (off < cnt)
67 : {
68 82 : keys[off] = va_arg (ap,
69 : const char *);
70 82 : offsets[off] = va_arg (ap,
71 : uint64_t);
72 82 : off++;
73 : }
74 25 : GNUNET_assert (NULL == va_arg (ap,
75 : const char *));
76 25 : va_end (ap);
77 :
78 : /* Note: this is an upsert, not a plain INSERT: the auditor helpers
79 : cannot tell from the query status of
80 : #TALER_AUDITORDB_get_auditor_progress() whether a progress row
81 : already exists (auditor_do_get_auditor_progress() returns one
82 : row per key, present or not), so this function stores the
83 : offset regardless of whether the row already exists.
84 : DISTINCT ON is required because a caller may pass the same key
85 : twice; ON CONFLICT DO UPDATE cannot touch a row twice in one
86 : statement. */
87 25 : PREPARE (pg,
88 : "insert_auditor_progress",
89 : "INSERT INTO auditor_progress "
90 : "(progress_key"
91 : ",progress_offset"
92 : ") SELECT DISTINCT ON (key) key,off"
93 : " FROM UNNEST (CAST($1 AS TEXT[]),"
94 : " CAST($2 AS INT8[]))"
95 : " AS t(key,off)"
96 : " ON CONFLICT (progress_key) DO UPDATE"
97 : " SET progress_offset=excluded.progress_offset;");
98 25 : qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
99 : "insert_auditor_progress",
100 : params);
101 25 : GNUNET_PQ_cleanup_query_params_closures (params);
102 25 : return qs;
103 : }
104 : }
|