select_early_aggregations.c (5203B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2025 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 src/auditordb/select_early_aggregations.c 18 * @brief Implementation of the select_early_aggregations function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_error_codes.h" 22 #include "taler/taler_pq_lib.h" 23 #include "auditor-database/select_early_aggregations.h" 24 #include "pg_helper.h" 25 26 27 /** 28 * Closure for #early_aggregation_cb(). 29 */ 30 struct EarlyAggregationContext 31 { 32 33 /** 34 * Function to call for each early aggregation. 35 */ 36 TALER_AUDITORDB_EarlyAggregationsCallback cb; 37 38 /** 39 * Closure for @e cb 40 */ 41 void *cb_cls; 42 43 /** 44 * Plugin context. 45 */ 46 struct TALER_AUDITORDB_PostgresContext *pg; 47 48 /** 49 * Query status to return. 50 */ 51 enum GNUNET_DB_QueryStatus qs; 52 }; 53 54 55 /** 56 * Helper function for #TALER_AUDITORDB_select_purse_expired(). 57 * To be called with the results of a SELECT statement 58 * that has returned @a num_results results. 59 * 60 * @param cls closure of type `struct EarlyAggregationContext *` 61 * @param result the postgres result 62 * @param num_results the number of results in @a result 63 */ 64 static void 65 early_aggregation_cb (void *cls, 66 PGresult *result, 67 unsigned int num_results) 68 { 69 struct EarlyAggregationContext *eic = cls; 70 struct TALER_AUDITORDB_PostgresContext *pg = eic->pg; 71 72 for (unsigned int i = 0; i < num_results; i++) 73 { 74 struct TALER_AUDITORDB_EarlyAggregation ea; 75 struct GNUNET_PQ_ResultSpec rs[] = { 76 GNUNET_PQ_result_spec_uint64 ("row_id", 77 &ea.row_id), 78 GNUNET_PQ_result_spec_uint64 ("batch_deposit_serial_id", 79 &ea.batch_deposit_serial_id), 80 GNUNET_PQ_result_spec_uint64 ("tracking_serial_id", 81 &ea.tracking_serial_id), 82 TALER_PQ_RESULT_SPEC_AMOUNT ("amount", 83 &ea.total), 84 GNUNET_PQ_result_spec_bool ("suppressed", 85 &ea.suppressed), 86 GNUNET_PQ_result_spec_end 87 }; 88 89 /* just to be safe in case the structure changes */ 90 memset (&ea, 91 0, 92 sizeof (ea)); 93 if (GNUNET_OK != 94 GNUNET_PQ_extract_result (result, 95 rs, 96 i)) 97 { 98 GNUNET_break (0); 99 eic->qs = GNUNET_DB_STATUS_HARD_ERROR; 100 return; 101 } 102 eic->cb (eic->cb_cls, 103 &ea); 104 } 105 eic->qs = num_results; 106 } 107 108 109 enum GNUNET_DB_QueryStatus 110 TALER_AUDITORDB_select_early_aggregations (struct 111 TALER_AUDITORDB_PostgresContext *pg, 112 int64_t limit, 113 uint64_t offset, 114 bool return_suppressed, 115 TALER_AUDITORDB_EarlyAggregationsCallback 116 cb, 117 void *cb_cls) 118 { 119 uint64_t ulimit = (limit < 0) ? -limit : limit; 120 struct GNUNET_PQ_QueryParam params[] = { 121 GNUNET_PQ_query_param_uint64 (&offset), 122 GNUNET_PQ_query_param_uint64 (&ulimit), 123 GNUNET_PQ_query_param_bool (return_suppressed), 124 GNUNET_PQ_query_param_end 125 }; 126 struct EarlyAggregationContext eic = { 127 .cb = cb, 128 .cb_cls = cb_cls, 129 .pg = pg 130 }; 131 enum GNUNET_DB_QueryStatus qs; 132 133 PREPARE (pg, 134 "auditor_select_early_aggregations_asc", 135 "SELECT" 136 " row_id" 137 ",batch_deposit_serial_id" 138 ",tracking_serial_id" 139 ",amount" 140 ",suppressed" 141 " FROM auditor_early_aggregations" 142 " WHERE row_id > $1" 143 " AND ($3 OR NOT suppressed)" 144 " ORDER BY row_id ASC" 145 " LIMIT $2;"); 146 PREPARE (pg, 147 "auditor_select_early_aggregations_desc", 148 "SELECT" 149 " row_id" 150 ",batch_deposit_serial_id" 151 ",tracking_serial_id" 152 ",amount" 153 ",suppressed" 154 " FROM auditor_early_aggregations" 155 " WHERE row_id < $1" 156 " AND ($3 OR NOT suppressed)" 157 " ORDER BY row_id DESC" 158 " LIMIT $2;"); 159 qs = GNUNET_PQ_eval_prepared_multi_select ( 160 pg->conn, 161 (limit < 0) 162 ? "auditor_select_early_aggregations_desc" 163 : "auditor_select_early_aggregations_asc ", 164 params, 165 &early_aggregation_cb, 166 &eic); 167 if (0 > qs) 168 return qs; 169 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != eic.qs); 170 return eic.qs; 171 }