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 exchangedb/pg_select_aml_attributes.c
18 : * @brief Implementation of the select_aml_attributes function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include "taler_error_codes.h"
23 : #include "taler_dbevents.h"
24 : #include "taler_pq_lib.h"
25 : #include "pg_select_aml_attributes.h"
26 : #include "pg_helper.h"
27 :
28 :
29 : /**
30 : * Closure for #handle_aml_result.
31 : */
32 : struct AmlAttributeResultContext
33 : {
34 : /**
35 : * Function to call on each result.
36 : */
37 : TALER_EXCHANGEDB_AmlAttributeCallback cb;
38 :
39 : /**
40 : * Closure for @e cb.
41 : */
42 : void *cb_cls;
43 :
44 : /**
45 : * Plugin context.
46 : */
47 : struct PostgresClosure *pg;
48 :
49 : /**
50 : * Set to #GNUNET_SYSERR on serious errors.
51 : */
52 : enum GNUNET_GenericReturnValue status;
53 : };
54 :
55 :
56 : /**
57 : * Function to be called with the results of a SELECT statement
58 : * that has returned @a num_results results. Helper function
59 : * for #TEH_PG_select_aml_attributes().
60 : *
61 : * @param cls closure of type `struct AmlAttributeResultContext *`
62 : * @param result the postgres result
63 : * @param num_results the number of results in @a result
64 : */
65 : static void
66 10 : handle_aml_attributes (void *cls,
67 : PGresult *result,
68 : unsigned int num_results)
69 : {
70 10 : struct AmlAttributeResultContext *ctx = cls;
71 :
72 20 : for (unsigned int i = 0; i<num_results; i++)
73 : {
74 : uint64_t rowid;
75 : struct GNUNET_TIME_Timestamp collection_time;
76 : size_t enc_attributes_size;
77 : void *enc_attributes;
78 10 : struct GNUNET_PQ_ResultSpec rs[] = {
79 10 : GNUNET_PQ_result_spec_uint64 ("kyc_attributes_serial_id",
80 : &rowid),
81 10 : GNUNET_PQ_result_spec_timestamp ("collection_time",
82 : &collection_time),
83 10 : GNUNET_PQ_result_spec_variable_size ("encrypted_attributes",
84 : &enc_attributes,
85 : &enc_attributes_size),
86 : GNUNET_PQ_result_spec_end
87 : };
88 :
89 10 : if (GNUNET_OK !=
90 10 : GNUNET_PQ_extract_result (result,
91 : rs,
92 : i))
93 : {
94 0 : GNUNET_break (0);
95 0 : ctx->status = GNUNET_SYSERR;
96 0 : return;
97 : }
98 :
99 10 : ctx->cb (ctx->cb_cls,
100 : rowid,
101 : collection_time,
102 : enc_attributes_size,
103 : enc_attributes);
104 10 : GNUNET_PQ_cleanup_result (rs);
105 : }
106 : }
107 :
108 :
109 : enum GNUNET_DB_QueryStatus
110 10 : TEH_PG_select_aml_attributes (
111 : void *cls,
112 : const struct TALER_NormalizedPaytoHashP *h_payto,
113 : uint64_t offset,
114 : int64_t limit,
115 : TALER_EXCHANGEDB_AmlAttributeCallback cb,
116 : void *cb_cls)
117 : {
118 10 : struct PostgresClosure *pg = cls;
119 10 : uint64_t ulimit = (limit > 0) ? limit : -limit;
120 10 : struct GNUNET_PQ_QueryParam params[] = {
121 10 : GNUNET_PQ_query_param_auto_from_type (h_payto),
122 10 : GNUNET_PQ_query_param_uint64 (&offset),
123 10 : GNUNET_PQ_query_param_uint64 (&ulimit),
124 : GNUNET_PQ_query_param_end
125 : };
126 10 : struct AmlAttributeResultContext ctx = {
127 : .cb = cb,
128 : .cb_cls = cb_cls,
129 : .pg = pg,
130 : .status = GNUNET_OK
131 : };
132 : enum GNUNET_DB_QueryStatus qs;
133 10 : const char *stmt = (limit > 0)
134 : ? "select_aml_attributes_inc"
135 : : "select_aml_attributes_dec";
136 :
137 10 : PREPARE (pg,
138 : "select_aml_attributes_inc",
139 : "SELECT"
140 : " kyc_attributes_serial_id"
141 : ",collection_time"
142 : ",encrypted_attributes"
143 : " FROM kyc_attributes"
144 : " WHERE h_payto=$1"
145 : " AND kyc_attributes_serial_id > $2"
146 : " ORDER BY kyc_attributes_serial_id ASC"
147 : " LIMIT $3");
148 10 : PREPARE (pg,
149 : "select_aml_attributes_dec",
150 : "SELECT"
151 : " kyc_attributes_serial_id"
152 : ",collection_time"
153 : ",encrypted_attributes"
154 : " FROM kyc_attributes"
155 : " WHERE h_payto=$1"
156 : " AND kyc_attributes_serial_id < $2"
157 : " ORDER BY kyc_attributes_serial_id DESC"
158 : " LIMIT $3");
159 10 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
160 : stmt,
161 : params,
162 : &handle_aml_attributes,
163 : &ctx);
164 10 : if (GNUNET_OK != ctx.status)
165 0 : return GNUNET_DB_STATUS_HARD_ERROR;
166 10 : return qs;
167 : }
|