LCOV - code coverage report
Current view: top level - exchangedb - pg_select_exchange_kycauth_transfers.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 32 0.0 %
Date: 2025-06-22 12:09:43 Functions: 0 2 0.0 %

          Line data    Source code
       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 exchangedb/pg_select_exchange_kycauth_transfers.c
      18             :  * @brief Implementation of the select_exchange_kycauth_transfers 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_exchange_kycauth_transfers.h"
      26             : #include "pg_helper.h"
      27             : 
      28             : /**
      29             :  * Closure for #handle_aml_result.
      30             :  */
      31             : struct SelectTransferContext
      32             : {
      33             :   /**
      34             :    * Function to call on each result.
      35             :    */
      36             :   TALER_EXCHANGEDB_AmlTransferCallback cb;
      37             : 
      38             :   /**
      39             :    * Closure for @e cb.
      40             :    */
      41             :   void *cb_cls;
      42             : 
      43             :   /**
      44             :    * Plugin context.
      45             :    */
      46             :   struct PostgresClosure *pg;
      47             : 
      48             :   /**
      49             :    * Set to #GNUNET_SYSERR on serious errors.
      50             :    */
      51             :   enum GNUNET_GenericReturnValue status;
      52             : };
      53             : 
      54             : 
      55             : /**
      56             :  * Function to be called with the results of a SELECT statement
      57             :  * that has returned @a num_results results.  Helper function
      58             :  * for #TEH_PG_select_exchange_debit_transfers().
      59             :  *
      60             :  * @param cls closure of type `struct SelectTransferContext *`
      61             :  * @param result the postgres result
      62             :  * @param num_results the number of results in @a result
      63             :  */
      64             : static void
      65           0 : handle_transfer_result (void *cls,
      66             :                         PGresult *result,
      67             :                         unsigned int num_results)
      68             : {
      69           0 :   struct SelectTransferContext *stc = cls;
      70           0 :   struct PostgresClosure *pg = stc->pg;
      71             : 
      72           0 :   for (unsigned int i = 0; i<num_results; i++)
      73             :   {
      74             :     char *payto_uri;
      75             :     uint64_t rowid;
      76             :     struct GNUNET_TIME_Absolute execution_time;
      77             :     struct TALER_Amount amount;
      78           0 :     struct GNUNET_PQ_ResultSpec rs[] = {
      79           0 :       GNUNET_PQ_result_spec_uint64 ("serial_id",
      80             :                                     &rowid),
      81           0 :       GNUNET_PQ_result_spec_string ("payto_uri",
      82             :                                     &payto_uri),
      83           0 :       GNUNET_PQ_result_spec_absolute_time ("execution_time",
      84             :                                            &execution_time),
      85           0 :       TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
      86             :                                    &amount),
      87             :       GNUNET_PQ_result_spec_end
      88             :     };
      89             : 
      90           0 :     if (GNUNET_OK !=
      91           0 :         GNUNET_PQ_extract_result (result,
      92             :                                   rs,
      93             :                                   i))
      94             :     {
      95           0 :       GNUNET_break (0);
      96           0 :       stc->status = GNUNET_SYSERR;
      97           0 :       return;
      98             :     }
      99           0 :     stc->cb (stc->cb_cls,
     100             :              rowid,
     101             :              payto_uri,
     102             :              execution_time,
     103             :              &amount);
     104           0 :     GNUNET_PQ_cleanup_result (rs);
     105             :   }
     106             : }
     107             : 
     108             : 
     109             : enum GNUNET_DB_QueryStatus
     110           0 : TEH_PG_select_exchange_kycauth_transfers (
     111             :   void *cls,
     112             :   const struct TALER_Amount *threshold,
     113             :   uint64_t offset,
     114             :   int64_t limit,
     115             :   const struct TALER_NormalizedPaytoHashP *h_payto,
     116             :   TALER_EXCHANGEDB_AmlTransferCallback cb,
     117             :   void *cb_cls)
     118             : {
     119           0 :   struct PostgresClosure *pg = cls;
     120           0 :   struct SelectTransferContext stc = {
     121             :     .pg = pg,
     122             :     .cb = cb,
     123             :     .cb_cls = cb_cls,
     124             :     .status = GNUNET_OK
     125             :   };
     126           0 :   uint64_t ulimit = (limit > 0) ? limit : -limit;
     127           0 :   struct GNUNET_PQ_QueryParam params[] = {
     128           0 :     GNUNET_PQ_query_param_uint64 (&offset),
     129           0 :     GNUNET_PQ_query_param_uint64 (&ulimit),
     130           0 :     TALER_PQ_query_param_amount (pg->conn,
     131             :                                  threshold),
     132             :     NULL != h_payto
     133           0 :     ? GNUNET_PQ_query_param_auto_from_type (h_payto)
     134           0 :     : GNUNET_PQ_query_param_null (),
     135             :     GNUNET_PQ_query_param_end
     136             :   };
     137             :   enum GNUNET_DB_QueryStatus qs;
     138             : 
     139           0 :   PREPARE (pg,
     140             :            "select_exchange_kycauth_transfers_inc",
     141             :            "SELECT"
     142             :            " ki.kycauth_in_serial_id AS serial_id"
     143             :            ",wt.payto_uri"
     144             :            ",ki.execution_date AS execution_time"
     145             :            ",ki.credit AS amount"
     146             :            " FROM kycauths_in ki"
     147             :            " LEFT JOIN wire_targets wt"
     148             :            "   ON (ki.wire_source_h_payto = wt.wire_target_h_payto)"
     149             :            " WHERE (ki.kycauth_in_serial_id > $1)"
     150             :            "   AND ( ($4::BYTEA IS NULL) OR (wt.h_normalized_payto=$4) )"
     151             :            "   AND ( ( (ki.credit).val > ($3::taler_amount).val)"
     152             :            "      OR ( ( (ki.credit).val >= ($3::taler_amount).val)"
     153             :            "       AND ( (ki.credit).frac >= ($3::taler_amount).frac) ) )"
     154             :            " ORDER BY ki.kycauth_in_serial_id ASC"
     155             :            " LIMIT $2");
     156           0 :   PREPARE (pg,
     157             :            "select_exchange_kycauth_transfers_dec",
     158             :            "SELECT"
     159             :            " ki.kycauth_in_serial_id AS serial_id"
     160             :            ",wt.payto_uri"
     161             :            ",ki.execution_date AS execution_time"
     162             :            ",ki.credit AS amount"
     163             :            " FROM kycauths_in ki"
     164             :            " LEFT JOIN wire_targets wt"
     165             :            "   ON (ki.wire_source_h_payto = wt.wire_target_h_payto)"
     166             :            " WHERE (ki.kycauth_in_serial_id < $1)"
     167             :            "   AND ( ($4::BYTEA IS NULL) OR (wt.h_normalized_payto=$4) )"
     168             :            "   AND ( ( (ki.credit).val > ($3::taler_amount).val)"
     169             :            "      OR ( ( (ki.credit).val >= ($3::taler_amount).val)"
     170             :            "       AND ( (ki.credit).frac >= ($3::taler_amount).frac) ) )"
     171             :            " ORDER BY ki.kycauth_in_serial_id DESC"
     172             :            " LIMIT $2");
     173           0 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     174             :     pg->conn,
     175             :     (limit > 0)
     176             :     ? "select_exchange_kycauth_transfers_inc"
     177             :     : "select_exchange_kycauth_transfers_dec",
     178             :     params,
     179             :     &handle_transfer_result,
     180             :     &stc);
     181           0 :   if (GNUNET_OK != stc.status)
     182           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     183           0 :   return qs;
     184             : }

Generated by: LCOV version 1.16