select_purse_requests_above_serial_id.c (5610B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022 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 select_purse_requests_above_serial_id.c 18 * @brief Implementation of the select_purse_requests_above_serial_id function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "exchange-database/select_purse_requests_above_serial_id.h" 23 #include "helper.h" 24 25 26 /** 27 * Closure for #purse_deposit_serial_helper_cb(). 28 */ 29 struct PurseRequestsSerialContext 30 { 31 32 /** 33 * Callback to call. 34 */ 35 TALER_EXCHANGEDB_PurseRequestCallback cb; 36 37 /** 38 * Closure for @e cb. 39 */ 40 void *cb_cls; 41 42 /** 43 * Plugin context. 44 */ 45 struct TALER_EXCHANGEDB_PostgresContext *pg; 46 47 /** 48 * Status code, set to #GNUNET_SYSERR on hard errors. 49 */ 50 enum GNUNET_GenericReturnValue status; 51 }; 52 53 54 /** 55 * Helper function to be called with the results of a SELECT statement 56 * that has returned @a num_results results. 57 * 58 * @param cls closure of type `struct PurseRequestsSerialContext` 59 * @param result the postgres result 60 * @param num_results the number of results in @a result 61 */ 62 static void 63 purse_requests_serial_helper_cb (void *cls, 64 PGresult *result, 65 unsigned int num_results) 66 { 67 struct PurseRequestsSerialContext *dsc = cls; 68 struct TALER_EXCHANGEDB_PostgresContext *pg = dsc->pg; 69 70 for (unsigned int i = 0; i<num_results; i++) 71 { 72 uint64_t rowid; 73 struct TALER_Amount target_amount; 74 uint32_t age_limit; 75 struct TALER_PurseMergePublicKeyP merge_pub; 76 struct TALER_PurseContractPublicKeyP purse_pub; 77 struct TALER_PrivateContractHashP h_contract_terms; 78 struct TALER_PurseContractSignatureP purse_sig; 79 struct GNUNET_TIME_Timestamp purse_creation; 80 struct GNUNET_TIME_Timestamp purse_expiration; 81 struct GNUNET_PQ_ResultSpec rs[] = { 82 TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee", 83 &target_amount), 84 GNUNET_PQ_result_spec_uint32 ("age_limit", 85 &age_limit), 86 GNUNET_PQ_result_spec_timestamp ("purse_creation", 87 &purse_creation), 88 GNUNET_PQ_result_spec_timestamp ("purse_expiration", 89 &purse_expiration), 90 GNUNET_PQ_result_spec_auto_from_type ("purse_pub", 91 &purse_pub), 92 GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms", 93 &h_contract_terms), 94 GNUNET_PQ_result_spec_auto_from_type ("purse_sig", 95 &purse_sig), 96 GNUNET_PQ_result_spec_auto_from_type ("merge_pub", 97 &merge_pub), 98 GNUNET_PQ_result_spec_uint64 ("purse_requests_serial_id", 99 &rowid), 100 GNUNET_PQ_result_spec_end 101 }; 102 enum GNUNET_GenericReturnValue ret; 103 104 if (GNUNET_OK != 105 GNUNET_PQ_extract_result (result, 106 rs, 107 i)) 108 { 109 GNUNET_break (0); 110 dsc->status = GNUNET_SYSERR; 111 return; 112 } 113 ret = dsc->cb (dsc->cb_cls, 114 rowid, 115 &purse_pub, 116 &merge_pub, 117 purse_creation, 118 purse_expiration, 119 &h_contract_terms, 120 age_limit, 121 &target_amount, 122 &purse_sig); 123 GNUNET_PQ_cleanup_result (rs); 124 if (GNUNET_OK != ret) 125 break; 126 } 127 } 128 129 130 enum GNUNET_DB_QueryStatus 131 TALER_TALER_EXCHANGEDB_select_purse_requests_above_serial_id ( 132 struct TALER_EXCHANGEDB_PostgresContext *pg, 133 uint64_t serial_id, 134 TALER_EXCHANGEDB_PurseRequestCallback cb, 135 void *cb_cls) 136 { 137 struct GNUNET_PQ_QueryParam params[] = { 138 GNUNET_PQ_query_param_uint64 (&serial_id), 139 GNUNET_PQ_query_param_end 140 }; 141 struct PurseRequestsSerialContext dsc = { 142 .cb = cb, 143 .cb_cls = cb_cls, 144 .pg = pg, 145 .status = GNUNET_OK 146 }; 147 enum GNUNET_DB_QueryStatus qs; 148 149 PREPARE (pg, 150 "audit_get_purse_requests_incr", 151 "SELECT" 152 " purse_requests_serial_id" 153 ",purse_pub" 154 ",amount_with_fee" 155 ",age_limit" 156 ",h_contract_terms" 157 ",purse_creation" 158 ",purse_expiration" 159 ",merge_pub" 160 ",purse_sig" 161 " FROM purse_requests" 162 " WHERE (" 163 " (purse_requests_serial_id>=$1)" 164 " )" 165 " ORDER BY purse_requests_serial_id ASC;"); 166 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 167 "audit_get_purse_requests_incr", 168 params, 169 &purse_requests_serial_helper_cb, 170 &dsc); 171 if (GNUNET_OK != dsc.status) 172 return GNUNET_DB_STATUS_HARD_ERROR; 173 return qs; 174 }