pg_select_reports.c (4874B)
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 backenddb/pg_select_reports.c 18 * @brief Implementation of the select_reports 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_reports.h" 26 #include "pg_helper.h" 27 28 29 /** 30 * Context used for TMH_PG_select_reports(). 31 */ 32 struct SelectReportsContext 33 { 34 /** 35 * Function to call with the results. 36 */ 37 TALER_MERCHANTDB_ReportsCallback cb; 38 39 /** 40 * Closure for @a cb. 41 */ 42 void *cb_cls; 43 44 /** 45 * Did database result extraction fail? 46 */ 47 bool extract_failed; 48 }; 49 50 51 /** 52 * Function to be called with the results of a SELECT statement 53 * that has returned @a num_results results about reports. 54 * 55 * @param[in,out] cls of type `struct SelectReportsContext *` 56 * @param result the postgres result 57 * @param num_results the number of results in @a result 58 */ 59 static void 60 select_reports_cb (void *cls, 61 PGresult *result, 62 unsigned int num_results) 63 { 64 struct SelectReportsContext *plc = cls; 65 66 for (unsigned int i = 0; i < num_results; i++) 67 { 68 uint64_t report_serial; 69 char *report_description; 70 struct GNUNET_TIME_Relative frequency; 71 struct GNUNET_PQ_ResultSpec rs[] = { 72 GNUNET_PQ_result_spec_uint64 ("report_serial", 73 &report_serial), 74 GNUNET_PQ_result_spec_string ("report_description", 75 &report_description), 76 GNUNET_PQ_result_spec_relative_time ("frequency", 77 &frequency), 78 GNUNET_PQ_result_spec_end 79 }; 80 81 if (GNUNET_OK != 82 GNUNET_PQ_extract_result (result, 83 rs, 84 i)) 85 { 86 GNUNET_break (0); 87 plc->extract_failed = true; 88 return; 89 } 90 plc->cb (plc->cb_cls, 91 report_serial, 92 report_description, 93 frequency); 94 GNUNET_PQ_cleanup_result (rs); 95 } 96 } 97 98 99 enum GNUNET_DB_QueryStatus 100 TMH_PG_select_reports (void *cls, 101 const char *instance_id, 102 int64_t limit, 103 uint64_t offset, 104 TALER_MERCHANTDB_ReportsCallback cb, 105 void *cb_cls) 106 { 107 struct PostgresClosure *pg = cls; 108 uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit); 109 struct SelectReportsContext plc = { 110 .cb = cb, 111 .cb_cls = cb_cls, 112 /* Can be overwritten by the select_reports_cb */ 113 .extract_failed = false, 114 }; 115 struct GNUNET_PQ_QueryParam params[] = { 116 GNUNET_PQ_query_param_string (instance_id), 117 GNUNET_PQ_query_param_uint64 (&offset), 118 GNUNET_PQ_query_param_uint64 (&plimit), 119 GNUNET_PQ_query_param_end 120 }; 121 enum GNUNET_DB_QueryStatus qs; 122 123 check_connection (pg); 124 PREPARE (pg, 125 "select_reports_asc", 126 "SELECT" 127 " report_serial" 128 " ,report_description" 129 " ,frequency" 130 " FROM merchant_reports" 131 " JOIN merchant_instances" 132 " USING (merchant_serial)" 133 " WHERE merchant_instances.merchant_id=$1" 134 " AND report_serial > $2" 135 " AND NOT one_shot_hidden" 136 " ORDER BY report_serial ASC" 137 " LIMIT $3"); 138 PREPARE (pg, 139 "select_reports_desc", 140 "SELECT" 141 " report_serial" 142 " ,report_description" 143 " ,frequency" 144 " FROM merchant_reports" 145 " JOIN merchant_instances" 146 " USING (merchant_serial)" 147 " WHERE merchant_instances.merchant_id=$1" 148 " AND report_serial < $2" 149 " AND NOT one_shot_hidden" 150 " ORDER BY report_serial DESC" 151 " LIMIT $3"); 152 qs = GNUNET_PQ_eval_prepared_multi_select ( 153 pg->conn, 154 (limit > 0) 155 ? "select_reports_asc" 156 : "select_reports_desc", 157 params, 158 &select_reports_cb, 159 &plc); 160 /* If there was an error inside select_reports_cb, return a hard error. */ 161 if (plc.extract_failed) 162 { 163 GNUNET_break (0); 164 return GNUNET_DB_STATUS_HARD_ERROR; 165 } 166 return qs; 167 }