pg_select_historic_denom_revenue.c (5033B)
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 pg_select_historic_denom_revenue.c 18 * @brief Low-level (statement-level) Postgres database access for the exchange 19 * @author Christian Grothoff 20 */ 21 #include "taler/platform.h" 22 #include "taler/taler_error_codes.h" 23 #include "taler/taler_dbevents.h" 24 #include "taler/taler_pq_lib.h" 25 #include "pg_select_historic_denom_revenue.h" 26 #include "pg_helper.h" 27 28 29 /** 30 * Closure for #historic_denom_revenue_cb(). 31 */ 32 struct HistoricDenomRevenueContext 33 { 34 /** 35 * Function to call for each result. 36 */ 37 TALER_AUDITORDB_HistoricDenominationRevenueDataCallback cb; 38 39 /** 40 * Closure for @e cb. 41 */ 42 void *cb_cls; 43 44 /** 45 * Plugin context. 46 */ 47 struct PostgresClosure *pg; 48 49 /** 50 * Number of results processed. 51 */ 52 enum GNUNET_DB_QueryStatus qs; 53 }; 54 55 56 /** 57 * Helper function for #TAH_PG_select_historic_denom_revenue(). 58 * To be called with the results of a SELECT statement 59 * that has returned @a num_results results. 60 * 61 * @param cls closure of type `struct HistoricRevenueContext *` 62 * @param result the postgres result 63 * @param num_results the number of results in @a result 64 */ 65 static void 66 historic_denom_revenue_cb (void *cls, 67 PGresult *result, 68 unsigned int num_results) 69 { 70 struct HistoricDenomRevenueContext *hrc = cls; 71 struct PostgresClosure *pg = hrc->pg; 72 73 for (unsigned int i = 0; i < num_results; i++) 74 { 75 uint64_t rowid; 76 struct TALER_DenominationHashP denom_pub_hash; 77 struct GNUNET_TIME_Timestamp revenue_timestamp; 78 struct TALER_Amount revenue_balance; 79 struct TALER_Amount loss; 80 struct GNUNET_PQ_ResultSpec rs[] = { 81 GNUNET_PQ_result_spec_uint64 ("row_id", 82 &rowid), 83 GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash", 84 &denom_pub_hash), 85 GNUNET_PQ_result_spec_timestamp ("revenue_timestamp", 86 &revenue_timestamp), 87 TALER_PQ_RESULT_SPEC_AMOUNT ("revenue_balance", 88 &revenue_balance), 89 TALER_PQ_RESULT_SPEC_AMOUNT ("loss_balance", 90 &loss), 91 GNUNET_PQ_result_spec_end 92 }; 93 94 if (GNUNET_OK != 95 GNUNET_PQ_extract_result (result, 96 rs, 97 i)) 98 { 99 GNUNET_break (0); 100 hrc->qs = GNUNET_DB_STATUS_HARD_ERROR; 101 return; 102 } 103 104 hrc->qs = i + 1; 105 if (GNUNET_OK != 106 hrc->cb (hrc->cb_cls, 107 rowid, 108 &denom_pub_hash, 109 revenue_timestamp, 110 &revenue_balance, 111 &loss)) 112 break; 113 } 114 } 115 116 117 enum GNUNET_DB_QueryStatus 118 TAH_PG_select_historic_denom_revenue ( 119 void *cls, 120 int64_t limit, 121 uint64_t offset, 122 TALER_AUDITORDB_HistoricDenominationRevenueDataCallback cb, 123 void *cb_cls) 124 { 125 struct PostgresClosure *pg = cls; 126 uint64_t ulimit = (limit > 0) ? limit : -limit; 127 struct GNUNET_PQ_QueryParam params[] = { 128 GNUNET_PQ_query_param_uint64 (&offset), 129 GNUNET_PQ_query_param_uint64 (&ulimit), 130 GNUNET_PQ_query_param_end 131 }; 132 struct HistoricDenomRevenueContext hrc = { 133 .cb = cb, 134 .cb_cls = cb_cls, 135 .pg = pg 136 }; 137 enum GNUNET_DB_QueryStatus qs; 138 139 PREPARE (pg, 140 "auditor_historic_denomination_revenue_select_inc", 141 "SELECT" 142 " row_id" 143 ",denom_pub_hash" 144 ",revenue_timestamp" 145 ",revenue_balance" 146 ",loss_balance" 147 " FROM auditor_historic_denomination_revenue" 148 " WHERE row_id > $1" 149 " ORDER BY row_id ASC" 150 " LIMIT $2;"); 151 PREPARE (pg, 152 "auditor_historic_denomination_revenue_select_dec", 153 "SELECT" 154 " row_id" 155 ",denom_pub_hash" 156 ",revenue_timestamp" 157 ",revenue_balance" 158 ",loss_balance" 159 " FROM auditor_historic_denomination_revenue" 160 " WHERE row_id < $1" 161 " ORDER BY row_id DESC" 162 " LIMIT $2;"); 163 qs = GNUNET_PQ_eval_prepared_multi_select ( 164 pg->conn, 165 limit > 0 166 ? "auditor_historic_denomination_revenue_select_inc" 167 : "auditor_historic_denomination_revenue_select_dec", 168 params, 169 &historic_denom_revenue_cb, 170 &hrc); 171 if (qs <= 0) 172 return qs; 173 return hrc.qs; 174 }