LCOV - code coverage report
Current view: top level - exchangedb - pg_select_kyc_attributes.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 84.0 % 25 21
Test Date: 2025-12-28 14:06:02 Functions: 100.0 % 2 2

            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_kyc_attributes.c
      18              :  * @brief Implementation of the select_kyc_attributes 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_kyc_attributes.h"
      26              : #include "pg_helper.h"
      27              : 
      28              : 
      29              : /**
      30              :  * Closure for #get_attributes_cb().
      31              :  */
      32              : struct GetAttributesContext
      33              : {
      34              :   /**
      35              :    * Function to call per result.
      36              :    */
      37              :   TALER_EXCHANGEDB_AttributeCallback 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              :    * Key of our query.
      51              :    */
      52              :   const struct TALER_NormalizedPaytoHashP *h_payto;
      53              : 
      54              :   /**
      55              :    * Flag set to #GNUNET_OK as long as everything is fine.
      56              :    */
      57              :   enum GNUNET_GenericReturnValue status;
      58              : 
      59              : };
      60              : 
      61              : 
      62              : /**
      63              :  * Invoke the callback for each result.
      64              :  *
      65              :  * @param cls a `struct GetAttributesContext *`
      66              :  * @param result SQL result
      67              :  * @param num_results number of rows in @a result
      68              :  */
      69              : static void
      70            7 : get_attributes_cb (void *cls,
      71              :                    PGresult *result,
      72              :                    unsigned int num_results)
      73              : {
      74            7 :   struct GetAttributesContext *ctx = cls;
      75              : 
      76            9 :   for (unsigned int i = 0; i < num_results; i++)
      77              :   {
      78              :     struct GNUNET_TIME_Timestamp collection_time;
      79              :     struct GNUNET_TIME_Timestamp expiration_time;
      80              :     size_t enc_attributes_size;
      81              :     void *enc_attributes;
      82              :     char *provider;
      83            2 :     struct GNUNET_PQ_ResultSpec rs[] = {
      84            2 :       GNUNET_PQ_result_spec_string ("provider_name",
      85              :                                     &provider),
      86            2 :       GNUNET_PQ_result_spec_timestamp ("collection_time",
      87              :                                        &collection_time),
      88            2 :       GNUNET_PQ_result_spec_timestamp ("expiration_time",
      89              :                                        &expiration_time),
      90            2 :       GNUNET_PQ_result_spec_variable_size ("encrypted_attributes",
      91              :                                            &enc_attributes,
      92              :                                            &enc_attributes_size),
      93              :       GNUNET_PQ_result_spec_end
      94              :     };
      95              : 
      96            2 :     if (GNUNET_OK !=
      97            2 :         GNUNET_PQ_extract_result (result,
      98              :                                   rs,
      99              :                                   i))
     100              :     {
     101            0 :       GNUNET_break (0);
     102            0 :       ctx->status = GNUNET_SYSERR;
     103            0 :       return;
     104              :     }
     105            2 :     ctx->cb (ctx->cb_cls,
     106              :              ctx->h_payto,
     107              :              provider,
     108              :              collection_time,
     109              :              expiration_time,
     110              :              enc_attributes_size,
     111              :              enc_attributes);
     112            2 :     GNUNET_PQ_cleanup_result (rs);
     113              :   }
     114              : }
     115              : 
     116              : 
     117              : enum GNUNET_DB_QueryStatus
     118            7 : TEH_PG_select_kyc_attributes (
     119              :   void *cls,
     120              :   const struct TALER_NormalizedPaytoHashP *h_payto,
     121              :   TALER_EXCHANGEDB_AttributeCallback cb,
     122              :   void *cb_cls)
     123              : {
     124            7 :   struct PostgresClosure *pg = cls;
     125            7 :   struct GNUNET_PQ_QueryParam params[] = {
     126            7 :     GNUNET_PQ_query_param_auto_from_type (h_payto),
     127              :     GNUNET_PQ_query_param_end
     128              :   };
     129            7 :   struct GetAttributesContext ctx = {
     130              :     .cb = cb,
     131              :     .cb_cls = cb_cls,
     132              :     .pg = pg,
     133              :     .h_payto = h_payto,
     134              :     .status = GNUNET_OK
     135              :   };
     136              :   enum GNUNET_DB_QueryStatus qs;
     137              : 
     138            7 :   PREPARE (pg,
     139              :            "select_kyc_attributes",
     140              :            "SELECT "
     141              :            " lp.provider_name"
     142              :            ",ka.collection_time"
     143              :            ",ka.expiration_time"
     144              :            ",ka.encrypted_attributes"
     145              :            " FROM kyc_attributes ka"
     146              :            " JOIN legitimization_processes lp"
     147              :            "   ON (ka.legitimization_serial = lp.legitimization_process_serial_id)"
     148              :            " WHERE ka.h_payto=$1");
     149            7 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     150              :     pg->conn,
     151              :     "select_kyc_attributes",
     152              :     params,
     153              :     &get_attributes_cb,
     154              :     &ctx);
     155            7 :   if (GNUNET_OK != ctx.status)
     156            0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     157            7 :   return qs;
     158              : }
        

Generated by: LCOV version 2.0-1