iterate_emergencies.c (5484B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024 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 #include "taler/taler_error_codes.h" 17 #include "taler/taler_pq_lib.h" 18 #include "pg_helper.h" 19 #include "auditor-database/iterate_emergencies.h" 20 21 22 /** 23 * Hard upper bound on the number of records returned by a single 24 * call, regardless of the limit requested by the client. 25 */ 26 #define MAX_RECORDS 50000 27 28 29 /** 30 * Closure for #emergency_cb(). 31 */ 32 struct EmergencyContext 33 { 34 35 /** 36 * Function to call for each deposit confirmation. 37 */ 38 TALER_AUDITORDB_EmergencyCallback cb; 39 40 /** 41 * Closure for @e cb 42 */ 43 void *cb_cls; 44 45 /** 46 * Plugin context. 47 */ 48 struct TALER_AUDITORDB_PostgresContext *pg; 49 50 /** 51 * Query status to return. 52 */ 53 enum GNUNET_DB_QueryStatus qs; 54 }; 55 56 57 /** 58 * Helper function for #TALER_AUDITORDB_iterate_emergencies(). 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 Emergency *` 63 * @param result the postgres result 64 * @param num_results the number of results in @a result 65 */ 66 static void 67 emergency_cb (void *cls, 68 PGresult *result, 69 unsigned int num_results) 70 { 71 struct EmergencyContext *dcc = cls; 72 struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg; 73 74 for (unsigned int i = 0; i < num_results; i++) 75 { 76 struct TALER_AUDITORDB_Emergency dc; 77 struct GNUNET_PQ_ResultSpec rs[] = { 78 GNUNET_PQ_result_spec_uint64 ("row_id", 79 &dc.row_id), 80 GNUNET_PQ_result_spec_auto_from_type ("denompub_h", 81 &dc.denompub_h), 82 TALER_PQ_RESULT_SPEC_AMOUNT ("denom_risk", 83 &dc.denom_risk), 84 TALER_PQ_RESULT_SPEC_AMOUNT ("denom_loss", 85 &dc.denom_loss), 86 GNUNET_PQ_result_spec_absolute_time ("deposit_start", 87 &dc.deposit_start), 88 GNUNET_PQ_result_spec_absolute_time ("deposit_end", 89 &dc.deposit_end), 90 TALER_PQ_RESULT_SPEC_AMOUNT ("value", 91 &dc.value), 92 GNUNET_PQ_result_spec_bool ("suppressed", 93 &dc.suppressed), 94 GNUNET_PQ_result_spec_end 95 }; 96 enum GNUNET_GenericReturnValue rval; 97 98 if (GNUNET_OK != 99 GNUNET_PQ_extract_result (result, 100 rs, 101 i)) 102 { 103 GNUNET_break (0); 104 dcc->qs = GNUNET_DB_STATUS_HARD_ERROR; 105 return; 106 } 107 dcc->qs = i + 1; 108 rval = dcc->cb (dcc->cb_cls, 109 &dc); 110 GNUNET_PQ_cleanup_result (rs); 111 if (GNUNET_OK != rval) 112 break; 113 } 114 } 115 116 117 enum GNUNET_DB_QueryStatus 118 TALER_AUDITORDB_iterate_emergencies (struct TALER_AUDITORDB_PostgresContext *pg, 119 int64_t limit, 120 uint64_t offset, 121 bool return_suppressed, 122 TALER_AUDITORDB_EmergencyCallback cb, 123 void *cb_cls) 124 { 125 uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS, 126 TALER_AUDITORDB_abs_limit (limit)); 127 struct GNUNET_PQ_QueryParam params[] = { 128 GNUNET_PQ_query_param_uint64 (&offset), 129 GNUNET_PQ_query_param_bool (return_suppressed), 130 GNUNET_PQ_query_param_uint64 (&plimit), 131 GNUNET_PQ_query_param_end 132 }; 133 struct EmergencyContext dcc = { 134 .cb = cb, 135 .cb_cls = cb_cls, 136 .pg = pg 137 }; 138 enum GNUNET_DB_QueryStatus qs; 139 140 PREPARE (pg, 141 "iterate_emergencies_emergency_get_desc", 142 "SELECT" 143 " row_id" 144 ",denompub_h" 145 ",denom_risk" 146 ",denom_loss" 147 ",deposit_start" 148 ",deposit_end" 149 ",value" 150 ",suppressed" 151 " FROM auditor_emergency" 152 " WHERE (row_id < $1)" 153 " AND ($2 OR NOT suppressed)" 154 " ORDER BY row_id DESC" 155 " LIMIT $3" 156 ); 157 PREPARE (pg, 158 "iterate_emergencies_emergency_get_asc", 159 "SELECT" 160 " row_id" 161 ",denompub_h" 162 ",denom_risk" 163 ",denom_loss" 164 ",deposit_start" 165 ",deposit_end" 166 ",value" 167 ",suppressed" 168 " FROM auditor_emergency" 169 " WHERE (row_id > $1)" 170 " AND ($2 OR NOT suppressed)" 171 " ORDER BY row_id ASC" 172 " LIMIT $3" 173 ); 174 qs = GNUNET_PQ_eval_prepared_multi_select ( 175 pg->conn, 176 (limit > 0) 177 ? "iterate_emergencies_emergency_get_asc" 178 : "iterate_emergencies_emergency_get_desc", 179 params, 180 &emergency_cb, 181 &dcc); 182 183 if (qs > 0) 184 return dcc.qs; 185 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 186 return qs; 187 }