lookup_reports_pending.c (5529B)
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/backenddb/lookup_reports_pending.c 18 * @brief Implementation of the lookup_reports_pending function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include <taler/taler_pq_lib.h> 23 #include "merchant-database/lookup_reports_pending.h" 24 #include "helper.h" 25 26 27 /** 28 * Context used for TALER_MERCHANTDB_lookup_reports_pending(). 29 */ 30 struct SelectReportsContext 31 { 32 /** 33 * Function to call with the results. 34 */ 35 TALER_MERCHANTDB_ReportsPendingCallback cb; 36 37 /** 38 * Closure for @a cb. 39 */ 40 void *cb_cls; 41 42 /** 43 * Did database result extraction fail? 44 */ 45 bool extract_failed; 46 }; 47 48 49 /** 50 * Function to be called with the results of a SELECT statement 51 * that has returned @a num_results results about reports. 52 * 53 * @param[in,out] cls of type `struct SelectReportsContext *` 54 * @param result the postgres result 55 * @param num_results the number of results in @a result 56 */ 57 static void 58 select_pending_reports_cb (void *cls, 59 PGresult *result, 60 unsigned int num_results) 61 { 62 struct SelectReportsContext *plc = cls; 63 64 for (unsigned int i = 0; i < num_results; i++) 65 { 66 char *instance_id; 67 uint64_t report_serial; 68 char *report_program_section; 69 char *report_description; 70 char *mime_type; 71 struct TALER_MERCHANT_ReportToken report_token; 72 char *target_address; 73 struct GNUNET_TIME_Relative frequency; 74 struct GNUNET_TIME_Relative frequency_shift; 75 struct GNUNET_TIME_Absolute next_transmission; 76 bool one_shot; 77 struct GNUNET_PQ_ResultSpec rs[] = { 78 GNUNET_PQ_result_spec_string ("out_merchant_id", 79 &instance_id), 80 GNUNET_PQ_result_spec_uint64 ("out_report_serial", 81 &report_serial), 82 GNUNET_PQ_result_spec_string ("out_report_program_section", 83 &report_program_section), 84 GNUNET_PQ_result_spec_string ("out_report_description", 85 &report_description), 86 GNUNET_PQ_result_spec_string ("out_mime_type", 87 &mime_type), 88 GNUNET_PQ_result_spec_auto_from_type ("out_report_token", 89 &report_token), 90 GNUNET_PQ_result_spec_string ("out_target_address", 91 &target_address), 92 GNUNET_PQ_result_spec_relative_time ("out_frequency", 93 &frequency), 94 GNUNET_PQ_result_spec_relative_time ("out_frequency_shift", 95 &frequency_shift), 96 GNUNET_PQ_result_spec_absolute_time ("out_next_transmission", 97 &next_transmission), 98 GNUNET_PQ_result_spec_bool ("out_one_shot_hidden", 99 &one_shot), 100 GNUNET_PQ_result_spec_end 101 }; 102 103 if (GNUNET_OK != 104 GNUNET_PQ_extract_result (result, 105 rs, 106 i)) 107 { 108 GNUNET_break (0); 109 plc->extract_failed = true; 110 return; 111 } 112 plc->cb (plc->cb_cls, 113 instance_id, 114 report_serial, 115 report_program_section, 116 report_description, 117 mime_type, 118 &report_token, 119 target_address, 120 frequency, 121 frequency_shift, 122 next_transmission, 123 one_shot); 124 GNUNET_PQ_cleanup_result (rs); 125 } 126 } 127 128 129 enum GNUNET_DB_QueryStatus 130 TALER_MERCHANTDB_lookup_reports_pending ( 131 struct TALER_MERCHANTDB_PostgresContext *pg, 132 TALER_MERCHANTDB_ReportsPendingCallback cb, 133 void *cb_cls) 134 { 135 struct SelectReportsContext plc = { 136 .cb = cb, 137 .cb_cls = cb_cls, 138 /* Can be overwritten by the lookup_reports_cb */ 139 .extract_failed = false, 140 }; 141 struct GNUNET_PQ_QueryParam params[] = { 142 GNUNET_PQ_query_param_end 143 }; 144 enum GNUNET_DB_QueryStatus qs; 145 146 check_connection (pg); 147 PREPARE (pg, 148 "lookup_reports_pending", 149 "SELECT" 150 " out_merchant_id" 151 " ,out_report_serial" 152 " ,out_report_program_section" 153 " ,out_report_description" 154 " ,out_mime_type" 155 " ,out_report_token" 156 " ,out_target_address" 157 " ,out_frequency" 158 " ,out_frequency_shift" 159 " ,out_next_transmission" 160 " ,out_one_shot_hidden" 161 " FROM merchant.lookup_reports_pending()"); 162 qs = GNUNET_PQ_eval_prepared_multi_select ( 163 pg->conn, 164 "lookup_reports_pending", 165 params, 166 &select_pending_reports_cb, 167 &plc); 168 /* If there was an error inside select_pending_reports_cb, return a hard error. */ 169 if (plc.extract_failed) 170 { 171 GNUNET_break (0); 172 return GNUNET_DB_STATUS_HARD_ERROR; 173 } 174 return qs; 175 }