lookup_transfers.c (8601B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022-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 src/backenddb/lookup_transfers.c 18 * @brief Implementation of the lookup_transfers function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include <taler/taler_pq_lib.h> 23 #include "merchant-database/lookup_transfers.h" 24 #include "helper.h" 25 26 27 /** 28 * Closure for #lookup_transfers_cb(). 29 */ 30 struct LookupTransfersContext 31 { 32 /** 33 * Function to call on results. 34 */ 35 TALER_MERCHANTDB_TransferCallback cb; 36 37 /** 38 * Closure for @e cb. 39 */ 40 void *cb_cls; 41 42 /** 43 * Postgres context. 44 */ 45 struct TALER_MERCHANTDB_PostgresContext *pg; 46 47 /** 48 * Transaction status (set). 49 */ 50 enum GNUNET_DB_QueryStatus qs; 51 52 }; 53 54 55 /** 56 * Function to be called with the results of a SELECT statement 57 * that has returned @a num_results results. 58 * 59 * @param cls of type `struct LookupTransfersContext *` 60 * @param result the postgres result 61 * @param num_results the number of results in @a result 62 */ 63 static void 64 lookup_transfers_cb (void *cls, 65 PGresult *result, 66 unsigned int num_results) 67 { 68 struct LookupTransfersContext *ltc = cls; 69 70 for (unsigned int i = 0; i<num_results; i++) 71 { 72 struct TALER_Amount credit_amount; 73 struct TALER_WireTransferIdentifierRawP wtid; 74 struct TALER_FullPayto payto_uri; 75 char *exchange_url; 76 uint64_t transfer_serial_id; 77 uint64_t expected_transfer_serial_id = 0; 78 struct GNUNET_TIME_Absolute execution_time; 79 bool expected; 80 struct GNUNET_PQ_ResultSpec rs[] = { 81 TALER_PQ_result_spec_amount_with_currency ("credit_amount", 82 &credit_amount), 83 GNUNET_PQ_result_spec_auto_from_type ("wtid", 84 &wtid), 85 GNUNET_PQ_result_spec_string ("payto_uri", 86 &payto_uri.full_payto), 87 GNUNET_PQ_result_spec_string ("exchange_url", 88 &exchange_url), 89 GNUNET_PQ_result_spec_uint64 ("credit_serial", 90 &transfer_serial_id), 91 GNUNET_PQ_result_spec_allow_null ( 92 GNUNET_PQ_result_spec_uint64 ("expected_credit_serial", 93 &expected_transfer_serial_id), 94 NULL), 95 GNUNET_PQ_result_spec_absolute_time ("execution_time", 96 &execution_time), 97 GNUNET_PQ_result_spec_bool ("expected", 98 &expected), 99 GNUNET_PQ_result_spec_end 100 }; 101 102 if (GNUNET_OK != 103 GNUNET_PQ_extract_result (result, 104 rs, 105 i)) 106 { 107 GNUNET_break (0); 108 ltc->qs = GNUNET_DB_STATUS_HARD_ERROR; 109 return; 110 } 111 ltc->cb (ltc->cb_cls, 112 &credit_amount, 113 &wtid, 114 payto_uri, 115 exchange_url, 116 transfer_serial_id, 117 expected_transfer_serial_id, 118 execution_time, 119 expected); 120 GNUNET_PQ_cleanup_result (rs); 121 } 122 ltc->qs = num_results; 123 } 124 125 126 enum GNUNET_DB_QueryStatus 127 TALER_MERCHANTDB_lookup_transfers ( 128 struct TALER_MERCHANTDB_PostgresContext *pg, 129 const char *instance_id, 130 struct TALER_FullPayto payto_uri, 131 struct GNUNET_TIME_Timestamp before, 132 struct GNUNET_TIME_Timestamp after, 133 int64_t limit, 134 uint64_t offset, 135 enum TALER_EXCHANGE_YesNoAll expected, 136 TALER_MERCHANTDB_TransferCallback cb, 137 void *cb_cls) 138 { 139 uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit); 140 bool by_time = ( (! GNUNET_TIME_absolute_is_never (before.abs_time)) || 141 (! GNUNET_TIME_absolute_is_zero (after.abs_time)) ); 142 struct LookupTransfersContext ltc = { 143 .cb = cb, 144 .cb_cls = cb_cls, 145 .pg = pg 146 }; 147 struct GNUNET_PQ_QueryParam params[] = { 148 GNUNET_PQ_query_param_timestamp (&before), 149 GNUNET_PQ_query_param_timestamp (&after), 150 GNUNET_PQ_query_param_uint64 (&offset), 151 GNUNET_PQ_query_param_uint64 (&plimit), 152 NULL == payto_uri.full_payto 153 ? GNUNET_PQ_query_param_null () /* NULL: do not filter by payto URI */ 154 : GNUNET_PQ_query_param_string (payto_uri.full_payto), 155 GNUNET_PQ_query_param_bool (! by_time), /* $6: filter by time? */ 156 GNUNET_PQ_query_param_bool (TALER_EXCHANGE_YNA_ALL == expected), /* filter by expected? */ 157 GNUNET_PQ_query_param_bool (TALER_EXCHANGE_YNA_YES == expected), 158 159 GNUNET_PQ_query_param_end 160 }; 161 enum GNUNET_DB_QueryStatus qs; 162 163 GNUNET_assert (NULL != pg->current_merchant_id); 164 GNUNET_assert (0 == strcmp (instance_id, 165 pg->current_merchant_id)); 166 check_connection (pg); 167 if (limit > 0) 168 { 169 TMH_PQ_prepare_anon (pg, 170 "SELECT" 171 " mt.credit_amount" 172 ",mt.wtid" 173 ",mac.payto_uri" 174 ",mt.exchange_url" 175 ",mt.credit_serial" 176 ",mt.execution_time" 177 ",mt.expected" 178 ",met.expected_credit_serial" 179 " FROM merchant_transfers mt" 180 " JOIN merchant_accounts mac" 181 " USING (account_serial)" 182 " LEFT JOIN merchant_expected_transfers met" 183 " ON mt.wtid = met.wtid" 184 " AND mt.account_serial = met.account_serial" 185 " AND mt.exchange_url = met.exchange_url" 186 " AND mt.expected" 187 " WHERE ( $6 OR " 188 " (mt.execution_time < $1 AND" 189 " mt.execution_time >= $2) )" 190 " AND ( (CAST($5 AS TEXT) IS NULL) OR " 191 " (REGEXP_REPLACE(mac.payto_uri,'\\?.*','')" 192 " =REGEXP_REPLACE($5,'\\?.*','')) )" 193 " AND ( $7 OR " 194 " (mt.expected = $8) )" 195 " AND (mt.credit_serial > $3)" 196 " ORDER BY mt.credit_serial ASC" 197 " LIMIT $4"); 198 } 199 else 200 { 201 TMH_PQ_prepare_anon (pg, 202 "SELECT" 203 " mt.credit_amount" 204 ",mt.wtid" 205 ",mac.payto_uri" 206 ",mt.exchange_url" 207 ",mt.credit_serial" 208 ",mt.execution_time" 209 ",mt.expected" 210 ",met.expected_credit_serial" 211 " FROM merchant_transfers mt" 212 " JOIN merchant_accounts mac" 213 " USING (account_serial)" 214 " LEFT JOIN merchant_expected_transfers met" 215 " ON mt.wtid = met.wtid" 216 " AND mt.account_serial = met.account_serial" 217 " AND mt.exchange_url = met.exchange_url" 218 " AND mt.expected" 219 " WHERE ( $6 OR " 220 " (mt.execution_time < $1 AND" 221 " mt.execution_time >= $2) )" 222 " AND ( (CAST($5 AS TEXT) IS NULL) OR " 223 " (REGEXP_REPLACE(mac.payto_uri,'\\?.*','')" 224 " =REGEXP_REPLACE($5,'\\?.*','')) )" 225 " AND ( $7 OR " 226 " (mt.expected = $8) )" 227 " AND (mt.credit_serial < $3)" 228 " ORDER BY mt.credit_serial DESC" 229 " LIMIT $4"); 230 } 231 qs = GNUNET_PQ_eval_prepared_multi_select ( 232 pg->conn, 233 "", 234 params, 235 &lookup_transfers_cb, 236 <c); 237 if (0 >= qs) 238 return qs; 239 return ltc.qs; 240 }