pg_select_historic_reserve_revenue.c (4721B)
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_reserve_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_reserve_revenue.h" 26 #include "pg_helper.h" 27 28 29 /** 30 * Closure for #historic_reserve_revenue_cb(). 31 */ 32 struct HistoricReserveRevenueContext 33 { 34 /** 35 * Function to call for each result. 36 */ 37 TALER_AUDITORDB_HistoricReserveRevenueDataCallback 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_reserve_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_reserve_revenue_cb (void *cls, 67 PGresult *result, 68 unsigned int num_results) 69 { 70 struct HistoricReserveRevenueContext *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 GNUNET_TIME_Timestamp start_date; 77 struct GNUNET_TIME_Timestamp end_date; 78 struct TALER_Amount reserve_profits; 79 struct GNUNET_PQ_ResultSpec rs[] = { 80 GNUNET_PQ_result_spec_uint64 ("row_id", 81 &rowid), 82 GNUNET_PQ_result_spec_timestamp ("start_date", 83 &start_date), 84 GNUNET_PQ_result_spec_timestamp ("end_date", 85 &end_date), 86 TALER_PQ_RESULT_SPEC_AMOUNT ("reserve_profits", 87 &reserve_profits), 88 GNUNET_PQ_result_spec_end 89 }; 90 91 if (GNUNET_OK != 92 GNUNET_PQ_extract_result (result, 93 rs, 94 i)) 95 { 96 GNUNET_break (0); 97 hrc->qs = GNUNET_DB_STATUS_HARD_ERROR; 98 return; 99 } 100 hrc->qs = i + 1; 101 if (GNUNET_OK != 102 hrc->cb (hrc->cb_cls, 103 rowid, 104 start_date, 105 end_date, 106 &reserve_profits)) 107 break; 108 } 109 } 110 111 112 enum GNUNET_DB_QueryStatus 113 TAH_PG_select_historic_reserve_revenue ( 114 void *cls, 115 int64_t limit, 116 uint64_t offset, 117 TALER_AUDITORDB_HistoricReserveRevenueDataCallback cb, 118 void *cb_cls) 119 { 120 struct PostgresClosure *pg = cls; 121 uint64_t ulimit = (limit > 0) ? limit : -limit; 122 struct GNUNET_PQ_QueryParam params[] = { 123 GNUNET_PQ_query_param_uint64 (&offset), 124 GNUNET_PQ_query_param_uint64 (&ulimit), 125 GNUNET_PQ_query_param_end 126 }; 127 enum GNUNET_DB_QueryStatus qs; 128 struct HistoricReserveRevenueContext hrc = { 129 .cb = cb, 130 .cb_cls = cb_cls, 131 .pg = pg 132 }; 133 134 PREPARE (pg, 135 "auditor_historic_reserve_summary_select_inc", 136 "SELECT" 137 " row_id" 138 ",start_date" 139 ",end_date" 140 ",reserve_profits" 141 " FROM auditor_historic_reserve_summary" 142 " WHERE row_id > $1" 143 " ORDER BY row_id ASC" 144 " LIMIT $2"); 145 PREPARE (pg, 146 "auditor_historic_reserve_summary_select_dec", 147 "SELECT" 148 " row_id" 149 ",start_date" 150 ",end_date" 151 ",reserve_profits" 152 " FROM auditor_historic_reserve_summary" 153 " WHERE row_id < $1" 154 " ORDER BY row_id DESC" 155 " LIMIT $2"); 156 qs = GNUNET_PQ_eval_prepared_multi_select ( 157 pg->conn, 158 limit > 0 159 ? "auditor_historic_reserve_summary_select_inc" 160 : "auditor_historic_reserve_summary_select_dec", 161 params, 162 &historic_reserve_revenue_cb, 163 &hrc); 164 if (0 >= qs) 165 return qs; 166 return hrc.qs; 167 }