select_purse_merges_above_serial_id.c (5996B)
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_merges_above_serial_id.c 18 * @brief Implementation of the select_purse_merges_above_serial_id function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "exchange-database/select_purse_merges_above_serial_id.h" 23 #include "helper.h" 24 25 26 /** 27 * Closure for #purse_deposit_serial_helper_cb(). 28 */ 29 struct PurseMergeSerialContext 30 { 31 32 /** 33 * Callback to call. 34 */ 35 TALER_EXCHANGEDB_PurseMergeCallback 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 PurseMergeSerialContext` 59 * @param result the postgres result 60 * @param num_results the number of results in @a result 61 */ 62 static void 63 purse_merges_serial_helper_cb (void *cls, 64 PGresult *result, 65 unsigned int num_results) 66 { 67 struct PurseMergeSerialContext *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 char *partner_base_url = NULL; 74 struct TALER_Amount amount; 75 struct TALER_Amount balance; 76 uint32_t flags32; 77 enum TALER_WalletAccountMergeFlags flags; 78 struct TALER_PurseMergePublicKeyP merge_pub; 79 struct TALER_ReservePublicKeyP reserve_pub; 80 struct TALER_PurseMergeSignatureP merge_sig; 81 struct TALER_PurseContractPublicKeyP purse_pub; 82 struct GNUNET_TIME_Timestamp merge_timestamp; 83 struct GNUNET_PQ_ResultSpec rs[] = { 84 TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee", 85 &amount), 86 TALER_PQ_RESULT_SPEC_AMOUNT ("balance", 87 &balance), 88 GNUNET_PQ_result_spec_allow_null ( 89 GNUNET_PQ_result_spec_string ("partner_base_url", 90 &partner_base_url), 91 NULL), 92 GNUNET_PQ_result_spec_uint32 ("flags", 93 &flags32), 94 GNUNET_PQ_result_spec_timestamp ("merge_timestamp", 95 &merge_timestamp), 96 GNUNET_PQ_result_spec_auto_from_type ("purse_pub", 97 &purse_pub), 98 GNUNET_PQ_result_spec_auto_from_type ("reserve_pub", 99 &reserve_pub), 100 GNUNET_PQ_result_spec_auto_from_type ("merge_sig", 101 &merge_sig), 102 GNUNET_PQ_result_spec_auto_from_type ("merge_pub", 103 &merge_pub), 104 GNUNET_PQ_result_spec_uint64 ("purse_merge_request_serial_id", 105 &rowid), 106 GNUNET_PQ_result_spec_end 107 }; 108 enum GNUNET_GenericReturnValue ret; 109 110 if (GNUNET_OK != 111 GNUNET_PQ_extract_result (result, 112 rs, 113 i)) 114 { 115 GNUNET_break (0); 116 dsc->status = GNUNET_SYSERR; 117 return; 118 } 119 flags = (enum TALER_WalletAccountMergeFlags) flags32; 120 ret = dsc->cb (dsc->cb_cls, 121 rowid, 122 partner_base_url, 123 &amount, 124 &balance, 125 flags, 126 &merge_pub, 127 &reserve_pub, 128 &merge_sig, 129 &purse_pub, 130 merge_timestamp); 131 GNUNET_PQ_cleanup_result (rs); 132 if (GNUNET_OK != ret) 133 break; 134 } 135 } 136 137 138 enum GNUNET_DB_QueryStatus 139 TALER_TALER_TALER_EXCHANGEDB_select_purse_merges_above_serial_id ( 140 struct TALER_EXCHANGEDB_PostgresContext *pg, 141 uint64_t serial_id, 142 TALER_EXCHANGEDB_PurseMergeCallback cb, 143 void *cb_cls) 144 { 145 struct GNUNET_PQ_QueryParam params[] = { 146 GNUNET_PQ_query_param_uint64 (&serial_id), 147 GNUNET_PQ_query_param_end 148 }; 149 struct PurseMergeSerialContext dsc = { 150 .cb = cb, 151 .cb_cls = cb_cls, 152 .pg = pg, 153 .status = GNUNET_OK 154 }; 155 enum GNUNET_DB_QueryStatus qs; 156 157 PREPARE (pg, 158 "audit_get_purse_merge_incr", 159 "SELECT" 160 " pm.purse_merge_request_serial_id" 161 ",partner_base_url" 162 ",pr.amount_with_fee" 163 ",pr.balance" 164 ",pr.flags" 165 ",pr.merge_pub" 166 ",pm.reserve_pub" 167 ",pm.merge_sig" 168 ",pm.purse_pub" 169 ",pm.merge_timestamp" 170 " FROM purse_merges pm" 171 " JOIN purse_requests pr USING (purse_pub)" 172 " LEFT JOIN partners USING (partner_serial_id)" 173 " WHERE (" 174 " (purse_merge_request_serial_id>=$1)" 175 " )" 176 " ORDER BY purse_merge_request_serial_id ASC;"); 177 178 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 179 "audit_get_purse_merge_incr", 180 params, 181 &purse_merges_serial_helper_cb, 182 &dsc); 183 if (GNUNET_OK != dsc.status) 184 return GNUNET_DB_STATUS_HARD_ERROR; 185 return qs; 186 }