pg_select_aggregations_above_serial.c (4203B)
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/pg_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/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_aggregations_above_serial.h" 26 #include "pg_helper.h" 27 28 /** 29 * Closure for #aggregation_serial_helper_cb(). 30 */ 31 struct AggregationSerialContext 32 { 33 34 /** 35 * Callback to call. 36 */ 37 TALER_EXCHANGEDB_AggregationCallback 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 * Status code, set to #GNUNET_SYSERR on hard errors. 51 */ 52 enum GNUNET_GenericReturnValue status; 53 }; 54 55 56 /** 57 * Helper function 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 AggregationSerialContext` 61 * @param result the postgres result 62 * @param num_results the number of results in @a result 63 */ 64 static void 65 aggregation_serial_helper_cb (void *cls, 66 PGresult *result, 67 unsigned int num_results) 68 { 69 struct AggregationSerialContext *dsc = cls; 70 struct PostgresClosure *pg = dsc->pg; 71 72 for (unsigned int i = 0; i<num_results; i++) 73 { 74 uint64_t tracking_rowid; 75 uint64_t batch_deposit_serial_id; 76 struct TALER_Amount amount; 77 struct GNUNET_PQ_ResultSpec rs[] = { 78 TALER_PQ_RESULT_SPEC_AMOUNT ("total_amount", 79 &amount), 80 GNUNET_PQ_result_spec_uint64 ("aggregation_serial_id", 81 &tracking_rowid), 82 GNUNET_PQ_result_spec_uint64 ("batch_deposit_serial_id", 83 &batch_deposit_serial_id), 84 GNUNET_PQ_result_spec_end 85 }; 86 87 if (GNUNET_OK != 88 GNUNET_PQ_extract_result (result, 89 rs, 90 i)) 91 { 92 GNUNET_break (0); 93 dsc->status = GNUNET_SYSERR; 94 return; 95 } 96 dsc->cb (dsc->cb_cls, 97 &amount, 98 tracking_rowid, 99 batch_deposit_serial_id); 100 GNUNET_PQ_cleanup_result (rs); 101 } 102 } 103 104 105 enum GNUNET_DB_QueryStatus 106 TEH_PG_select_aggregations_above_serial ( 107 void *cls, 108 uint64_t min_tracking_serial_id, 109 TALER_EXCHANGEDB_AggregationCallback cb, 110 void *cb_cls) 111 { 112 struct PostgresClosure *pg = cls; 113 struct GNUNET_PQ_QueryParam params[] = { 114 GNUNET_PQ_query_param_uint64 (&min_tracking_serial_id), 115 GNUNET_PQ_query_param_end 116 }; 117 struct AggregationSerialContext asc = { 118 .cb = cb, 119 .cb_cls = cb_cls, 120 .pg = pg, 121 .status = GNUNET_OK 122 }; 123 enum GNUNET_DB_QueryStatus qs; 124 125 /* Fetch aggregations with rowid '\geq' the given parameter */ 126 PREPARE (pg, 127 "select_aggregations_above_serial", 128 "SELECT" 129 " aggregation_serial_id" 130 ",batch_deposit_serial_id" 131 ",total_amount" 132 " FROM exchange_do_select_aggregations_above_serial($1);"); 133 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 134 "select_aggregations_above_serial", 135 params, 136 &aggregation_serial_helper_cb, 137 &asc); 138 if (GNUNET_OK != asc.status) 139 return GNUNET_DB_STATUS_HARD_ERROR; 140 return qs; 141 }