select_aggregations_above_serial.c (4155B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2023, 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 /** 17 * @file exchangedb/select_aggregations_above_serial.c 18 * @brief Implementation of the select_aggregations_above_serial function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "exchange-database/select_aggregations_above_serial.h" 23 #include "helper.h" 24 25 /** 26 * Closure for #aggregation_serial_helper_cb(). 27 */ 28 struct AggregationSerialContext 29 { 30 31 /** 32 * Callback to call. 33 */ 34 TALER_EXCHANGEDB_AggregationCallback cb; 35 36 /** 37 * Closure for @e cb. 38 */ 39 void *cb_cls; 40 41 /** 42 * Plugin context. 43 */ 44 struct TALER_EXCHANGEDB_PostgresContext *pg; 45 46 /** 47 * Status code, set to #GNUNET_SYSERR on hard errors. 48 */ 49 enum GNUNET_GenericReturnValue status; 50 }; 51 52 53 /** 54 * Helper function to be called with the results of a SELECT statement 55 * that has returned @a num_results results. 56 * 57 * @param cls closure of type `struct AggregationSerialContext` 58 * @param result the postgres result 59 * @param num_results the number of results in @a result 60 */ 61 static void 62 aggregation_serial_helper_cb (void *cls, 63 PGresult *result, 64 unsigned int num_results) 65 { 66 struct AggregationSerialContext *dsc = cls; 67 struct TALER_EXCHANGEDB_PostgresContext *pg = dsc->pg; 68 69 for (unsigned int i = 0; i<num_results; i++) 70 { 71 uint64_t tracking_rowid; 72 uint64_t batch_deposit_serial_id; 73 struct TALER_Amount amount; 74 struct GNUNET_PQ_ResultSpec rs[] = { 75 TALER_PQ_RESULT_SPEC_AMOUNT ("total_amount", 76 &amount), 77 GNUNET_PQ_result_spec_uint64 ("aggregation_serial_id", 78 &tracking_rowid), 79 GNUNET_PQ_result_spec_uint64 ("batch_deposit_serial_id", 80 &batch_deposit_serial_id), 81 GNUNET_PQ_result_spec_end 82 }; 83 84 if (GNUNET_OK != 85 GNUNET_PQ_extract_result (result, 86 rs, 87 i)) 88 { 89 GNUNET_break (0); 90 dsc->status = GNUNET_SYSERR; 91 return; 92 } 93 dsc->cb (dsc->cb_cls, 94 &amount, 95 tracking_rowid, 96 batch_deposit_serial_id); 97 GNUNET_PQ_cleanup_result (rs); 98 } 99 } 100 101 102 enum GNUNET_DB_QueryStatus 103 TALER_EXCHANGEDB_select_aggregations_above_serial ( 104 struct TALER_EXCHANGEDB_PostgresContext *pg, 105 uint64_t min_tracking_serial_id, 106 TALER_EXCHANGEDB_AggregationCallback cb, 107 void *cb_cls) 108 { 109 struct GNUNET_PQ_QueryParam params[] = { 110 GNUNET_PQ_query_param_uint64 (&min_tracking_serial_id), 111 GNUNET_PQ_query_param_end 112 }; 113 struct AggregationSerialContext asc = { 114 .cb = cb, 115 .cb_cls = cb_cls, 116 .pg = pg, 117 .status = GNUNET_OK 118 }; 119 enum GNUNET_DB_QueryStatus qs; 120 121 /* Fetch aggregations with rowid '\geq' the given parameter */ 122 PREPARE (pg, 123 "select_aggregations_above_serial", 124 "SELECT" 125 " aggregation_serial_id" 126 ",batch_deposit_serial_id" 127 ",total_amount" 128 " FROM exchange_do_select_aggregations_above_serial($1);"); 129 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 130 "select_aggregations_above_serial", 131 params, 132 &aggregation_serial_helper_cb, 133 &asc); 134 if (GNUNET_OK != asc.status) 135 return GNUNET_DB_STATUS_HARD_ERROR; 136 return qs; 137 }