Line data Source code
1 : /* 2 : This file is part of TALER 3 : Copyright (C) 2022, 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_merge_amounts_for_kyc_check.c 18 : * @brief Implementation of the select_merge_amounts_for_kyc_check function for Postgres 19 : * @author Christian Grothoff 20 : */ 21 : #include "platform.h" 22 : #include "taler_error_codes.h" 23 : #include "taler_dbevents.h" 24 : #include "taler_pq_lib.h" 25 : #include "pg_select_merge_amounts_for_kyc_check.h" 26 : #include "pg_helper.h" 27 : 28 : 29 : /** 30 : * Closure for #get_kyc_amounts_cb(). 31 : */ 32 : struct KycAmountCheckContext 33 : { 34 : /** 35 : * Function to call per result. 36 : */ 37 : TALER_EXCHANGEDB_KycAmountCallback 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 : * Flag set to #GNUNET_OK as long as everything is fine. 51 : */ 52 : enum GNUNET_GenericReturnValue status; 53 : 54 : }; 55 : 56 : /** 57 : * Invoke the callback for each result. 58 : * 59 : * @param cls a `struct KycAmountCheckContext *` 60 : * @param result SQL result 61 : * @param num_results number of rows in @a result 62 : */ 63 : static void 64 4 : get_kyc_amounts_cb (void *cls, 65 : PGresult *result, 66 : unsigned int num_results) 67 : { 68 4 : struct KycAmountCheckContext *ctx = cls; 69 4 : struct PostgresClosure *pg = ctx->pg; 70 : 71 4 : for (unsigned int i = 0; i < num_results; i++) 72 : { 73 : struct GNUNET_TIME_Absolute date; 74 : struct TALER_Amount amount; 75 0 : struct GNUNET_PQ_ResultSpec rs[] = { 76 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount", 77 : &amount), 78 0 : GNUNET_PQ_result_spec_absolute_time ("date", 79 : &date), 80 : GNUNET_PQ_result_spec_end 81 : }; 82 : enum GNUNET_GenericReturnValue ret; 83 : 84 0 : if (GNUNET_OK != 85 0 : GNUNET_PQ_extract_result (result, 86 : rs, 87 : i)) 88 : { 89 0 : GNUNET_break (0); 90 0 : ctx->status = GNUNET_SYSERR; 91 0 : return; 92 : } 93 0 : ret = ctx->cb (ctx->cb_cls, 94 : &amount, 95 : date); 96 0 : GNUNET_PQ_cleanup_result (rs); 97 0 : switch (ret) 98 : { 99 0 : case GNUNET_OK: 100 0 : continue; 101 0 : case GNUNET_NO: 102 0 : break; 103 0 : case GNUNET_SYSERR: 104 0 : ctx->status = GNUNET_SYSERR; 105 0 : break; 106 : } 107 0 : break; 108 : } 109 : } 110 : 111 : 112 : enum GNUNET_DB_QueryStatus 113 4 : TEH_PG_select_merge_amounts_for_kyc_check ( 114 : void *cls, 115 : const struct TALER_NormalizedPaytoHashP *h_payto, 116 : struct GNUNET_TIME_Absolute time_limit, 117 : TALER_EXCHANGEDB_KycAmountCallback kac, 118 : void *kac_cls) 119 : { 120 4 : struct PostgresClosure *pg = cls; 121 4 : struct GNUNET_PQ_QueryParam params[] = { 122 4 : GNUNET_PQ_query_param_auto_from_type (h_payto), 123 4 : GNUNET_PQ_query_param_absolute_time (&time_limit), 124 : GNUNET_PQ_query_param_end 125 : }; 126 4 : struct KycAmountCheckContext ctx = { 127 : .cb = kac, 128 : .cb_cls = kac_cls, 129 : .pg = pg, 130 : .status = GNUNET_OK 131 : }; 132 : enum GNUNET_DB_QueryStatus qs; 133 : 134 4 : PREPARE (pg, 135 : "select_kyc_relevant_merge_events", 136 : "SELECT" 137 : " amount_with_fee AS amount" 138 : ",merge_timestamp AS date" 139 : " FROM account_merges" 140 : " JOIN purse_merges USING (purse_pub)" 141 : " JOIN purse_requests USING (purse_pub)" 142 : " JOIN purse_decision USING (purse_pub)" 143 : " WHERE wallet_h_payto=$1" 144 : " AND merge_timestamp >= $2" 145 : " AND NOT refunded" 146 : " ORDER BY merge_timestamp DESC"); 147 4 : qs = GNUNET_PQ_eval_prepared_multi_select ( 148 : pg->conn, 149 : "select_kyc_relevant_merge_events", 150 : params, 151 : &get_kyc_amounts_cb, 152 : &ctx); 153 4 : if (GNUNET_OK != ctx.status) 154 0 : return GNUNET_DB_STATUS_HARD_ERROR; 155 4 : return qs; 156 : }