pg_select_purse_decisions_above_serial_id.c (4901B)
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 exchangedb/pg_select_purse_decisions_above_serial_id.c 18 * @brief Implementation of the select_purse_decisions_above_serial_id function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/platform.h" 22 #include "taler/taler_error_codes.h" 23 #include "taler/taler_dbevents.h" 24 #include "taler/taler_pq_lib.h" 25 #include "pg_select_purse_decisions_above_serial_id.h" 26 #include "pg_helper.h" 27 28 /** 29 * Closure for #purse_decision_serial_helper_cb(). 30 */ 31 struct PurseDecisionSerialContext 32 { 33 34 /** 35 * Callback to call. 36 */ 37 TALER_EXCHANGEDB_PurseDecisionCallback 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 * Status code, set to #GNUNET_SYSERR on hard errors. 51 */ 52 enum GNUNET_GenericReturnValue status; 53 }; 54 55 56 /** 57 * Helper function to be called with the results of a SELECT statement 58 * that has returned @a num_results results. 59 * 60 * @param cls closure of type `struct PurseRefundSerialContext` 61 * @param result the postgres result 62 * @param num_results the number of results in @a result 63 */ 64 static void 65 purse_decision_serial_helper_cb (void *cls, 66 PGresult *result, 67 unsigned int num_results) 68 { 69 struct PurseDecisionSerialContext *dsc = cls; 70 struct PostgresClosure *pg = dsc->pg; 71 72 for (unsigned int i = 0; i<num_results; i++) 73 { 74 struct TALER_PurseContractPublicKeyP purse_pub; 75 struct TALER_ReservePublicKeyP reserve_pub; 76 bool no_reserve = true; 77 uint64_t rowid; 78 struct TALER_Amount val; 79 struct GNUNET_PQ_ResultSpec rs[] = { 80 GNUNET_PQ_result_spec_auto_from_type ("purse_pub", 81 &purse_pub), 82 GNUNET_PQ_result_spec_allow_null ( 83 GNUNET_PQ_result_spec_auto_from_type ("reserve_pub", 84 &reserve_pub), 85 &no_reserve), 86 TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee", 87 &val), 88 GNUNET_PQ_result_spec_uint64 ("purse_decision_serial_id", 89 &rowid), 90 GNUNET_PQ_result_spec_end 91 }; 92 enum GNUNET_GenericReturnValue ret; 93 94 if (GNUNET_OK != 95 GNUNET_PQ_extract_result (result, 96 rs, 97 i)) 98 { 99 GNUNET_break (0); 100 dsc->status = GNUNET_SYSERR; 101 return; 102 } 103 ret = dsc->cb (dsc->cb_cls, 104 rowid, 105 &purse_pub, 106 no_reserve ? NULL : &reserve_pub, 107 &val); 108 GNUNET_PQ_cleanup_result (rs); 109 if (GNUNET_OK != ret) 110 break; 111 } 112 } 113 114 115 enum GNUNET_DB_QueryStatus 116 TEH_PG_select_purse_decisions_above_serial_id ( 117 void *cls, 118 uint64_t serial_id, 119 bool refunded, 120 TALER_EXCHANGEDB_PurseDecisionCallback cb, 121 void *cb_cls) 122 { 123 struct PostgresClosure *pg = cls; 124 struct GNUNET_PQ_QueryParam params[] = { 125 GNUNET_PQ_query_param_uint64 (&serial_id), 126 GNUNET_PQ_query_param_bool (refunded), 127 GNUNET_PQ_query_param_end 128 }; 129 struct PurseDecisionSerialContext dsc = { 130 .cb = cb, 131 .cb_cls = cb_cls, 132 .pg = pg, 133 .status = GNUNET_OK 134 }; 135 enum GNUNET_DB_QueryStatus qs; 136 137 PREPARE (pg, 138 "audit_get_purse_decisions_incr", 139 "SELECT" 140 " pd.purse_pub" 141 ",pm.reserve_pub" 142 ",pd.purse_decision_serial_id" 143 ",pr.amount_with_fee" 144 " FROM purse_decision pd" 145 " JOIN purse_requests pr ON (pd.purse_pub = pr.purse_pub)" 146 " LEFT JOIN purse_merges pm ON (pm.purse_pub = pd.purse_pub)" 147 " WHERE (" 148 " (purse_decision_serial_id>=$1) AND " 149 " (refunded=$2)" 150 " )" 151 " ORDER BY purse_decision_serial_id ASC;"); 152 153 154 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 155 "audit_get_purse_decisions_incr", 156 params, 157 &purse_decision_serial_helper_cb, 158 &dsc); 159 if (GNUNET_OK != dsc.status) 160 return GNUNET_DB_STATUS_HARD_ERROR; 161 return qs; 162 }