iterate_purse_expired.c (4176B)
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 src/auditordb/iterate_purse_expired.c 18 * @brief Implementation of the iterate_purse_expired function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "auditor-database/iterate_purse_expired.h" 23 #include "pg_helper.h" 24 25 26 /** 27 * Closure for #purse_expired_cb(). 28 */ 29 struct PurseExpiredContext 30 { 31 32 /** 33 * Function to call for each expired purse. 34 */ 35 TALER_AUDITORDB_ExpiredPurseCallback cb; 36 37 /** 38 * Closure for @e cb 39 */ 40 void *cb_cls; 41 42 /** 43 * Plugin context. 44 */ 45 struct TALER_AUDITORDB_PostgresContext *pg; 46 47 /** 48 * Query status to return. 49 */ 50 enum GNUNET_DB_QueryStatus qs; 51 }; 52 53 54 /** 55 * Helper function for #TALER_AUDITORDB_iterate_purse_expired(). 56 * To be called with the results of a SELECT statement 57 * that has returned @a num_results results. 58 * 59 * @param cls closure of type `struct PurseExpiredContext *` 60 * @param result the postgres result 61 * @param num_results the number of results in @a result 62 */ 63 static void 64 purse_expired_cb (void *cls, 65 PGresult *result, 66 unsigned int num_results) 67 { 68 struct PurseExpiredContext *eic = cls; 69 struct TALER_AUDITORDB_PostgresContext *pg = eic->pg; 70 71 for (unsigned int i = 0; i < num_results; i++) 72 { 73 struct TALER_PurseContractPublicKeyP purse_pub; 74 struct GNUNET_TIME_Timestamp expiration_date; 75 struct TALER_Amount balance; 76 struct GNUNET_PQ_ResultSpec rs[] = { 77 GNUNET_PQ_result_spec_auto_from_type ("purse_pub", 78 &purse_pub), 79 TALER_PQ_RESULT_SPEC_AMOUNT ("balance", 80 &balance), 81 GNUNET_PQ_result_spec_timestamp ("expiration_date", 82 &expiration_date), 83 GNUNET_PQ_result_spec_end 84 }; 85 86 if (GNUNET_OK != 87 GNUNET_PQ_extract_result (result, 88 rs, 89 i)) 90 { 91 GNUNET_break (0); 92 eic->qs = GNUNET_DB_STATUS_HARD_ERROR; 93 return; 94 } 95 eic->qs = i + 1; 96 if (GNUNET_OK != 97 eic->cb (eic->cb_cls, 98 &purse_pub, 99 &balance, 100 expiration_date)) 101 break; 102 } 103 } 104 105 106 enum GNUNET_DB_QueryStatus 107 TALER_AUDITORDB_iterate_purse_expired (struct TALER_AUDITORDB_PostgresContext * 108 pg 109 , 110 TALER_AUDITORDB_ExpiredPurseCallback cb, 111 void *cb_cls) 112 { 113 struct GNUNET_TIME_Timestamp now 114 = GNUNET_TIME_timestamp_get (); 115 struct GNUNET_PQ_QueryParam params[] = { 116 GNUNET_PQ_query_param_timestamp (&now), 117 GNUNET_PQ_query_param_end 118 }; 119 struct PurseExpiredContext eic = { 120 .cb = cb, 121 .cb_cls = cb_cls, 122 .pg = pg 123 }; 124 enum GNUNET_DB_QueryStatus qs; 125 126 PREPARE (pg, 127 "iterate_purse_expired", 128 "SELECT" 129 " purse_pub" 130 ",expiration_date" 131 ",balance" 132 " FROM auditor_purses" 133 " WHERE expiration_date<$1;"); 134 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 135 "iterate_purse_expired", 136 params, 137 &purse_expired_cb, 138 &eic); 139 if (qs > 0) 140 return eic.qs; 141 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 142 return qs; 143 }