pg_get_history.c (3587B)
1 /* 2 This file is part of TALER 3 Copyright (C) 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 donaudb/pg_get_history.c 18 * @brief Implementation of the lookup_donation_unit_key function for Postgres 19 * @author Johannes Casaburi 20 */ 21 #include "donau_config.h" 22 #include "taler/taler_error_codes.h" 23 #include "taler/taler_dbevents.h" 24 #include "taler/taler_pq_lib.h" 25 #include "pg_get_history.h" 26 #include "pg_helper.h" 27 28 29 /** 30 * Closure for #get_history_cb(). 31 */ 32 struct GetHistoryContext 33 { 34 /** 35 * Function to call per result. 36 */ 37 DONAUDB_GetHistoryCallback 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 /** 58 * Invoke the callback for each result. 59 * 60 * @param cls a `struct GetHistoryContext *` 61 * @param result SQL result 62 * @param num_results number of rows in @a result 63 */ 64 static void 65 get_history_cb (void *cls, 66 PGresult *result, 67 unsigned int num_results) 68 { 69 struct GetHistoryContext *ctx = cls; 70 struct PostgresClosure *pg = ctx->pg; 71 72 for (unsigned int i = 0; i < num_results; i++) 73 { 74 uint64_t charity_id; 75 struct TALER_Amount final_amount; 76 uint64_t donation_year; 77 78 struct GNUNET_PQ_ResultSpec rs[] = { 79 GNUNET_PQ_result_spec_uint64 ("charity_id", 80 &charity_id), 81 TALER_PQ_RESULT_SPEC_AMOUNT ("final_amount", 82 &final_amount), 83 GNUNET_PQ_result_spec_uint64 ("donation_year", 84 &donation_year), 85 GNUNET_PQ_result_spec_end 86 }; 87 88 if (GNUNET_OK != 89 GNUNET_PQ_extract_result (result, 90 rs, 91 i)) 92 { 93 GNUNET_break (0); 94 ctx->qs = GNUNET_DB_STATUS_HARD_ERROR; 95 return; 96 } 97 98 ctx->qs = i + 1; 99 if (GNUNET_OK != 100 ctx->cb (ctx->cb_cls, 101 charity_id, 102 final_amount, 103 donation_year)) 104 break; 105 } 106 } 107 108 109 enum GNUNET_DB_QueryStatus 110 DH_PG_get_history (void *cls, 111 DONAUDB_GetHistoryCallback cb, 112 void *cb_cls) 113 { 114 struct PostgresClosure *pg = cls; 115 struct GNUNET_PQ_QueryParam params[] = { 116 GNUNET_PQ_query_param_end 117 }; 118 struct GetHistoryContext ctx = { 119 .cb = cb, 120 .cb_cls = cb_cls, 121 .pg = pg 122 }; 123 enum GNUNET_DB_QueryStatus qs; 124 125 PREPARE (pg, 126 "get_history", 127 "SELECT" 128 " charity_id" 129 ",final_amount" 130 ",donation_year" 131 " FROM history"); 132 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 133 "get_history", 134 params, 135 &get_history_cb, 136 &ctx); 137 if (qs <= 0) 138 return qs; 139 return ctx.qs; 140 }