get_reserve_balance_insufficient_inconsistency.c (5903B)
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_pq_lib.h" 17 #include "pg_helper.h" 18 #include \ 19 "auditor-database/get_reserve_balance_insufficient_inconsistency.h" 20 21 22 /** 23 * Closure for #reserve_balance_insufficient_inconsistency_cb(). 24 */ 25 struct ReserveBalanceInsufficientInconsistencyContext 26 { 27 28 /** 29 * Function to call for each ReserveBalanceInsufficientInconsistency. 30 */ 31 TALER_AUDITORDB_ReserveBalanceInsufficientInconsistencyCallback cb; 32 33 /** 34 * Closure for @e cb 35 */ 36 void *cb_cls; 37 38 /** 39 * Plugin context. 40 */ 41 struct TALER_AUDITORDB_PostgresContext *pg; 42 43 /** 44 * Query status to return. 45 */ 46 enum GNUNET_DB_QueryStatus qs; 47 }; 48 49 50 /** 51 * Helper function for #TALER_AUDITORDB_get_reserve_balance_insufficient_inconsistency(). 52 * To be called with the results of a SELECT statement 53 * that has returned @a num_results results. 54 * 55 * @param cls closure of type `struct ReserveBalanceInsufficientInconsistency *` 56 * @param result the postgres result 57 * @param num_results the number of results in @a result 58 */ 59 static void 60 reserve_balance_insufficient_inconsistency_cb (void *cls, 61 PGresult *result, 62 unsigned int num_results) 63 { 64 struct ReserveBalanceInsufficientInconsistencyContext *dcc = cls; 65 struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg; 66 67 for (unsigned int i = 0; i < num_results; i++) 68 { 69 struct TALER_AUDITORDB_ReserveBalanceInsufficientInconsistency dc; 70 struct GNUNET_PQ_ResultSpec rs[] = { 71 GNUNET_PQ_result_spec_uint64 ("row_id", 72 &dc.row_id), 73 GNUNET_PQ_result_spec_auto_from_type ("reserve_pub", 74 &dc.reserve_pub), 75 GNUNET_PQ_result_spec_bool ("inconsistency_gain", 76 &dc.inconsistency_gain), 77 TALER_PQ_RESULT_SPEC_AMOUNT ("inconsistency_amount", 78 &dc.inconsistency_amount), 79 GNUNET_PQ_result_spec_bool ("suppressed", 80 &dc.suppressed), 81 GNUNET_PQ_result_spec_end 82 }; 83 enum GNUNET_GenericReturnValue rval; 84 85 if (GNUNET_OK != 86 GNUNET_PQ_extract_result (result, 87 rs, 88 i)) 89 { 90 GNUNET_break (0); 91 dcc->qs = GNUNET_DB_STATUS_HARD_ERROR; 92 return; 93 } 94 dcc->qs = i + 1; 95 rval = dcc->cb (dcc->cb_cls, 96 &dc); 97 GNUNET_PQ_cleanup_result (rs); 98 if (GNUNET_OK != rval) 99 break; 100 } 101 } 102 103 104 enum GNUNET_DB_QueryStatus 105 TALER_AUDITORDB_get_reserve_balance_insufficient_inconsistency (struct 106 TALER_AUDITORDB_PostgresContext 107 *pg, 108 int64_t limit, 109 uint64_t offset, 110 bool 111 return_suppressed 112 , 113 TALER_AUDITORDB_ReserveBalanceInsufficientInconsistencyCallback 114 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 ReserveBalanceInsufficientInconsistencyContext 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_reserve_balance_insufficient_inconsistency_get_desc", 133 "SELECT" 134 " row_id" 135 ",reserve_pub" 136 ",inconsistency_gain" 137 ",inconsistency_amount" 138 ",suppressed" 139 " FROM auditor_reserve_balance_insufficient_inconsistency" 140 " WHERE (row_id < $1)" 141 " AND ($2 OR NOT suppressed)" 142 " ORDER BY row_id DESC" 143 " LIMIT $3" 144 ); 145 PREPARE (pg, 146 "auditor_reserve_balance_insufficient_inconsistency_get_asc", 147 "SELECT" 148 " row_id" 149 ",reserve_pub" 150 ",inconsistency_gain" 151 ",inconsistency_amount" 152 ",suppressed" 153 " FROM auditor_reserve_balance_insufficient_inconsistency" 154 " WHERE (row_id > $1)" 155 " AND ($2 OR NOT suppressed)" 156 " ORDER BY row_id ASC" 157 " LIMIT $3" 158 ); 159 qs = GNUNET_PQ_eval_prepared_multi_select ( 160 pg->conn, 161 (limit > 0) 162 ? "auditor_reserve_balance_insufficient_inconsistency_get_asc" 163 : "auditor_reserve_balance_insufficient_inconsistency_get_desc", 164 params, 165 &reserve_balance_insufficient_inconsistency_cb, 166 &dcc); 167 if (qs > 0) 168 return dcc.qs; 169 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 170 return qs; 171 }