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