get_purses.c (4518B)
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 #include "taler/taler_pq_lib.h" 17 #include "pg_helper.h" 18 #include "auditor-database/get_purses.h" 19 20 21 struct PursesContext 22 { 23 24 /** 25 * Function to call for each bad sig loss. 26 */ 27 TALER_AUDITORDB_PursesCallback cb; 28 29 /** 30 * Closure for @e cb 31 */ 32 void *cb_cls; 33 34 /** 35 * Plugin context. 36 */ 37 struct TALER_AUDITORDB_PostgresContext *pg; 38 39 /** 40 * Query status to return. 41 */ 42 enum GNUNET_DB_QueryStatus qs; 43 }; 44 45 46 /** 47 * Helper function for #TALER_AUDITORDB_get_purses(). 48 * To be called with the results of a SELECT statement 49 * that has returned @a num_results results. 50 * 51 * @param cls closure of type `struct PursesContext *` 52 * @param result the postgres result 53 * @param num_results the number of results in @a result 54 */ 55 static void 56 purses_cb (void *cls, 57 PGresult *result, 58 unsigned int num_results) 59 { 60 struct PursesContext *dcc = cls; 61 struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg; 62 63 for (unsigned int i = 0; i < num_results; i++) 64 { 65 struct TALER_AUDITORDB_Purses dc; 66 struct GNUNET_PQ_ResultSpec rs[] = { 67 GNUNET_PQ_result_spec_uint64 ("auditor_purses_rowid", 68 &dc.auditor_purses_rowid), 69 GNUNET_PQ_result_spec_auto_from_type ("purse_pub", 70 &dc.purse_pub), 71 TALER_PQ_RESULT_SPEC_AMOUNT ("balance", 72 &dc.balance), 73 TALER_PQ_RESULT_SPEC_AMOUNT ("target", 74 &dc.target), 75 GNUNET_PQ_result_spec_absolute_time ("expiration_date", 76 &dc.expiration_date), 77 GNUNET_PQ_result_spec_end 78 }; 79 enum GNUNET_GenericReturnValue rval; 80 81 if (GNUNET_OK != 82 GNUNET_PQ_extract_result (result, 83 rs, 84 i)) 85 { 86 GNUNET_break (0); 87 dcc->qs = GNUNET_DB_STATUS_HARD_ERROR; 88 return; 89 } 90 dcc->qs = i + 1; 91 rval = dcc->cb (dcc->cb_cls, 92 dc.auditor_purses_rowid, 93 &dc); 94 GNUNET_PQ_cleanup_result (rs); 95 if (GNUNET_OK != rval) 96 break; 97 } 98 } 99 100 101 enum GNUNET_DB_QueryStatus 102 TALER_AUDITORDB_get_purses (struct TALER_AUDITORDB_PostgresContext *pg, 103 int64_t limit, 104 uint64_t offset, 105 TALER_AUDITORDB_PursesCallback cb, 106 void *cb_cls) 107 { 108 uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit); 109 struct GNUNET_PQ_QueryParam params[] = { 110 GNUNET_PQ_query_param_uint64 (&offset), 111 GNUNET_PQ_query_param_uint64 (&plimit), 112 GNUNET_PQ_query_param_end 113 }; 114 struct PursesContext dcc = { 115 .cb = cb, 116 .cb_cls = cb_cls, 117 .pg = pg 118 }; 119 enum GNUNET_DB_QueryStatus qs; 120 121 PREPARE (pg, 122 "auditor_purses_get_desc", 123 "SELECT" 124 " auditor_purses_rowid," 125 " purse_pub," 126 " balance," 127 " target," 128 " expiration_date" 129 " FROM auditor_purses" 130 " WHERE (auditor_purses_rowid < $1)" 131 " ORDER BY auditor_purses_rowid DESC" 132 " LIMIT $2" 133 ); 134 PREPARE (pg, 135 "auditor_purses_get_asc", 136 "SELECT" 137 " auditor_purses_rowid," 138 " purse_pub," 139 " balance," 140 " target," 141 " expiration_date" 142 " FROM auditor_purses" 143 " WHERE (auditor_purses_rowid > $1)" 144 " ORDER BY auditor_purses_rowid ASC" 145 " LIMIT $2" 146 ); 147 qs = GNUNET_PQ_eval_prepared_multi_select ( 148 pg->conn, 149 (limit > 0) 150 ? "auditor_purses_get_asc" 151 : "auditor_purses_get_desc", 152 params, 153 &purses_cb, 154 &dcc); 155 156 if (qs > 0) 157 return dcc.qs; 158 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 159 return qs; 160 }