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