select_purse_expired.c (4144B)
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/select_purse_expired.c 18 * @brief Implementation of the select_purse_expired function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "auditor-database/select_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_select_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_select_purse_expired (struct TALER_AUDITORDB_PostgresContext *pg 108 , 109 TALER_AUDITORDB_ExpiredPurseCallback cb, 110 void *cb_cls) 111 { 112 struct GNUNET_TIME_Timestamp now 113 = GNUNET_TIME_timestamp_get (); 114 struct GNUNET_PQ_QueryParam params[] = { 115 GNUNET_PQ_query_param_timestamp (&now), 116 GNUNET_PQ_query_param_end 117 }; 118 struct PurseExpiredContext eic = { 119 .cb = cb, 120 .cb_cls = cb_cls, 121 .pg = pg 122 }; 123 enum GNUNET_DB_QueryStatus qs; 124 125 PREPARE (pg, 126 "auditor_select_expired_purses", 127 "SELECT" 128 " purse_pub" 129 ",expiration_date" 130 ",balance" 131 " FROM auditor_purses" 132 " WHERE expiration_date<$1;"); 133 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 134 "auditor_select_expired_purses", 135 params, 136 &purse_expired_cb, 137 &eic); 138 if (qs > 0) 139 return eic.qs; 140 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 141 return qs; 142 }