LCOV - code coverage report
Current view: top level - backenddb - pg_select_accounts.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 89.5 % 38 34
Test Date: 2025-11-06 19:31:41 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /*
       2              :    This file is part of TALER
       3              :    Copyright (C) 2022 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 backenddb/pg_select_accounts.c
      18              :  * @brief Implementation of the select_accounts function for Postgres
      19              :  * @author Christian Grothoff
      20              :  */
      21              : #include "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_accounts.h"
      26              : #include "pg_helper.h"
      27              : 
      28              : 
      29              : /**
      30              :  * Context for select_accounts().
      31              :  */
      32              : struct SelectAccountsContext
      33              : {
      34              :   /**
      35              :    * Function to call with the results.
      36              :    */
      37              :   TALER_MERCHANTDB_AccountCallback cb;
      38              : 
      39              :   /**
      40              :    * Closure for @e cb.
      41              :    */
      42              :   void *cb_cls;
      43              : 
      44              :   /**
      45              :    * Database context.
      46              :    */
      47              :   struct PostgresClosure *pg;
      48              : 
      49              :   /**
      50              :    * Set to the return value on errors.
      51              :    */
      52              :   enum GNUNET_DB_QueryStatus qs;
      53              : 
      54              : };
      55              : 
      56              : 
      57              : /**
      58              :  * Function to be called with the results of a SELECT statement
      59              :  * that has returned @a num_results results about accounts.
      60              :  *
      61              :  * @param cls of type `struct SelectAccountsContext *`
      62              :  * @param result the postgres result
      63              :  * @param num_results the number of results in @a result
      64              :  */
      65              : static void
      66          146 : select_account_cb (void *cls,
      67              :                    PGresult *result,
      68              :                    unsigned int num_results)
      69              : {
      70          146 :   struct SelectAccountsContext *lic = cls;
      71              : 
      72          224 :   for (unsigned int i = 0; i < num_results; i++)
      73              :   {
      74              :     struct TALER_FullPayto payto;
      75              :     char *instance_id;
      76           78 :     char *facade_url = NULL;
      77           78 :     json_t *credential = NULL;
      78              :     struct TALER_MERCHANTDB_AccountDetails acc;
      79              :     struct TALER_MerchantPrivateKeyP merchant_priv;
      80              :     bool no_priv;
      81           78 :     struct GNUNET_PQ_ResultSpec rs[] = {
      82           78 :       GNUNET_PQ_result_spec_auto_from_type ("h_wire",
      83              :                                             &acc.h_wire),
      84           78 :       GNUNET_PQ_result_spec_auto_from_type ("salt",
      85              :                                             &acc.salt),
      86           78 :       GNUNET_PQ_result_spec_string ("payto_uri",
      87              :                                     &payto.full_payto),
      88           78 :       GNUNET_PQ_result_spec_string ("merchant_id",
      89              :                                     &instance_id),
      90           78 :       GNUNET_PQ_result_spec_allow_null (
      91              :         GNUNET_PQ_result_spec_string ("credit_facade_url",
      92              :                                       &facade_url),
      93              :         NULL),
      94           78 :       GNUNET_PQ_result_spec_allow_null (
      95              :         TALER_PQ_result_spec_json ("credit_facade_credentials",
      96              :                                    &credential),
      97              :         NULL),
      98           78 :       GNUNET_PQ_result_spec_bool ("active",
      99              :                                   &acc.active),
     100           78 :       GNUNET_PQ_result_spec_allow_null (
     101              :         GNUNET_PQ_result_spec_auto_from_type ("merchant_priv",
     102              :                                               &merchant_priv),
     103              :         &no_priv),
     104              :       GNUNET_PQ_result_spec_end
     105              :     };
     106              : 
     107           78 :     if (GNUNET_OK !=
     108           78 :         GNUNET_PQ_extract_result (result,
     109              :                                   rs,
     110              :                                   i))
     111              :     {
     112            0 :       GNUNET_break (0);
     113            0 :       lic->qs = GNUNET_DB_STATUS_HARD_ERROR;
     114            0 :       return;
     115              :     }
     116           78 :     acc.instance_id = instance_id;
     117           78 :     acc.payto_uri = payto;
     118           78 :     acc.credit_facade_url = facade_url;
     119           78 :     acc.credit_facade_credentials = credential;
     120           78 :     if (NULL != lic->cb)
     121           75 :       lic->cb (lic->cb_cls,
     122              :                no_priv ? NULL : &merchant_priv,
     123              :                &acc);
     124           78 :     GNUNET_PQ_cleanup_result (rs);
     125              :   }
     126              : }
     127              : 
     128              : 
     129              : enum GNUNET_DB_QueryStatus
     130          146 : TMH_PG_select_accounts (void *cls,
     131              :                         const char *id,
     132              :                         TALER_MERCHANTDB_AccountCallback cb,
     133              :                         void *cb_cls)
     134              : {
     135          146 :   struct PostgresClosure *pg = cls;
     136          146 :   struct SelectAccountsContext lic = {
     137              :     .cb = cb,
     138              :     .cb_cls = cb_cls,
     139              :     .pg = pg
     140              :   };
     141          146 :   struct GNUNET_PQ_QueryParam params[] = {
     142              :     NULL == id
     143           53 :     ? GNUNET_PQ_query_param_null ()
     144          146 :     : GNUNET_PQ_query_param_string (id),
     145              :     GNUNET_PQ_query_param_end
     146              :   };
     147              :   enum GNUNET_DB_QueryStatus qs;
     148              : 
     149          146 :   check_connection (pg);
     150          146 :   PREPARE (pg,
     151              :            "select_accounts",
     152              :            "SELECT"
     153              :            " ma.h_wire"
     154              :            ",ma.salt"
     155              :            ",ma.payto_uri"
     156              :            ",ma.credit_facade_url"
     157              :            ",ma.credit_facade_credentials::TEXT"
     158              :            ",ma.active"
     159              :            ",mk.merchant_priv"
     160              :            ",mi.merchant_id"
     161              :            " FROM merchant_accounts ma"
     162              :            " JOIN merchant_instances mi"
     163              :            "   ON (mi.merchant_serial=ma.merchant_serial)"
     164              :            " LEFT JOIN merchant_keys mk"
     165              :            "   ON (mk.merchant_serial=ma.merchant_serial)"
     166              :            " WHERE"
     167              :            "  ($1::TEXT IS NULL) OR"
     168              :            "  (ma.merchant_serial="
     169              :            "    (SELECT merchant_serial "
     170              :            "       FROM merchant_instances"
     171              :            "      WHERE merchant_id=$1));");
     172          146 :   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
     173              :                                              "select_accounts",
     174              :                                              params,
     175              :                                              &select_account_cb,
     176              :                                              &lic);
     177          146 :   if (0 > lic.qs)
     178            0 :     return lic.qs;
     179          146 :   return qs;
     180              : }
        

Generated by: LCOV version 2.0-1