LCOV - code coverage report
Current view: top level - backenddb - pg_lookup_all_products.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 38 0.0 %
Date: 2025-06-23 16:22:09 Functions: 0 2 0.0 %

          Line data    Source code
       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             : /**
      17             :  * @file backenddb/pg_lookup_all_products.c
      18             :  * @brief Implementation of the lookup_all_products 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_lookup_all_products.h"
      26             : #include "pg_helper.h"
      27             : 
      28             : /**
      29             :  * Context used for TMH_PG_lookup_all_products().
      30             :  */
      31             : struct LookupProductsContext
      32             : {
      33             :   /**
      34             :    * Function to call with the results.
      35             :    */
      36             :   TALER_MERCHANTDB_ProductCallback cb;
      37             : 
      38             :   /**
      39             :    * Closure for @a cb.
      40             :    */
      41             :   void *cb_cls;
      42             : 
      43             :   /**
      44             :    * Postgres context.
      45             :    */
      46             :   struct PostgresClosure *pg;
      47             : 
      48             :   /**
      49             :    * Did database result extraction fail?
      50             :    */
      51             :   bool extract_failed;
      52             : };
      53             : 
      54             : 
      55             : /**
      56             :  * Function to be called with the results of a SELECT statement
      57             :  * that has returned @a num_results results about products.
      58             :  *
      59             :  * @param[in,out] cls of type `struct LookupProductsContext *`
      60             :  * @param result the postgres result
      61             :  * @param num_results the number of results in @a result
      62             :  */
      63             : static void
      64           0 : lookup_products_cb (void *cls,
      65             :                     PGresult *result,
      66             :                     unsigned int num_results)
      67             : {
      68           0 :   struct LookupProductsContext *plc = cls;
      69           0 :   struct PostgresClosure *pg = plc->pg;
      70             : 
      71           0 :   for (unsigned int i = 0; i < num_results; i++)
      72             :   {
      73             :     char *product_id;
      74             :     uint64_t product_serial;
      75             :     struct TALER_MERCHANTDB_ProductDetails pd;
      76             :     size_t num_categories;
      77             :     uint64_t *categories;
      78           0 :     struct GNUNET_PQ_ResultSpec rs[] = {
      79           0 :       GNUNET_PQ_result_spec_string ("product_id",
      80             :                                     &product_id),
      81           0 :       GNUNET_PQ_result_spec_uint64 ("product_serial",
      82             :                                     &product_serial),
      83           0 :       GNUNET_PQ_result_spec_string ("description",
      84             :                                     &pd.description),
      85           0 :       TALER_PQ_result_spec_json ("description_i18n",
      86             :                                  &pd.description_i18n),
      87           0 :       GNUNET_PQ_result_spec_string ("unit",
      88             :                                     &pd.unit),
      89           0 :       TALER_PQ_result_spec_amount_with_currency ("price",
      90             :                                                  &pd.price),
      91           0 :       TALER_PQ_result_spec_json ("taxes",
      92             :                                  &pd.taxes),
      93           0 :       GNUNET_PQ_result_spec_uint64 ("total_stock",
      94             :                                     &pd.total_stock),
      95           0 :       GNUNET_PQ_result_spec_uint64 ("total_sold",
      96             :                                     &pd.total_sold),
      97           0 :       GNUNET_PQ_result_spec_uint64 ("total_lost",
      98             :                                     &pd.total_lost),
      99           0 :       GNUNET_PQ_result_spec_string ("image",
     100             :                                     &pd.image),
     101           0 :       TALER_PQ_result_spec_json ("address",
     102             :                                  &pd.address),
     103           0 :       GNUNET_PQ_result_spec_timestamp ("next_restock",
     104             :                                        &pd.next_restock),
     105           0 :       GNUNET_PQ_result_spec_uint32 ("minimum_age",
     106             :                                     &pd.minimum_age),
     107           0 :       GNUNET_PQ_result_spec_array_uint64 (pg->conn,
     108             :                                           "categories",
     109             :                                           &num_categories,
     110             :                                           &categories),
     111             :       GNUNET_PQ_result_spec_end
     112             :     };
     113             : 
     114           0 :     if (GNUNET_OK !=
     115           0 :         GNUNET_PQ_extract_result (result,
     116             :                                   rs,
     117             :                                   i))
     118             :     {
     119           0 :       GNUNET_break (0);
     120           0 :       plc->extract_failed = true;
     121           0 :       return;
     122             :     }
     123           0 :     plc->cb (plc->cb_cls,
     124             :              product_serial,
     125             :              product_id,
     126             :              &pd,
     127             :              num_categories,
     128             :              categories);
     129           0 :     GNUNET_PQ_cleanup_result (rs);
     130             :   }
     131             : }
     132             : 
     133             : 
     134             : enum GNUNET_DB_QueryStatus
     135           0 : TMH_PG_lookup_all_products (void *cls,
     136             :                             const char *instance_id,
     137             :                             TALER_MERCHANTDB_ProductCallback cb,
     138             :                             void *cb_cls)
     139             : {
     140           0 :   struct PostgresClosure *pg = cls;
     141           0 :   struct LookupProductsContext plc = {
     142             :     .cb = cb,
     143             :     .cb_cls = cb_cls,
     144             :     .pg = pg,
     145             :     /* Can be overwritten by the lookup_products_cb */
     146             :     .extract_failed = false,
     147             :   };
     148           0 :   struct GNUNET_PQ_QueryParam params[] = {
     149           0 :     GNUNET_PQ_query_param_string (instance_id),
     150             :     GNUNET_PQ_query_param_end
     151             :   };
     152             :   enum GNUNET_DB_QueryStatus qs;
     153             : 
     154           0 :   check_connection (pg);
     155           0 :   PREPARE (pg,
     156             :            "lookup_all_products",
     157             :            "SELECT"
     158             :            " description"
     159             :            ",description_i18n"
     160             :            ",unit"
     161             :            ",price"
     162             :            ",taxes"
     163             :            ",total_stock"
     164             :            ",total_sold"
     165             :            ",total_lost"
     166             :            ",image"
     167             :            ",minv.address"
     168             :            ",next_restock"
     169             :            ",minimum_age"
     170             :            ",product_id"
     171             :            ",product_serial"
     172             :            ",t.category_array AS categories"
     173             :            " FROM merchant_inventory minv"
     174             :            " JOIN merchant_instances inst"
     175             :            "   USING (merchant_serial)"
     176             :            ",LATERAL ("
     177             :            "   SELECT ARRAY ("
     178             :            "     SELECT mpc.category_serial"
     179             :            "       FROM merchant_product_categories mpc"
     180             :            "      WHERE mpc.product_serial = minv.product_serial"
     181             :            "   ) AS category_array"
     182             :            " ) t"
     183             :            " WHERE inst.merchant_id=$1");
     184           0 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     185             :     pg->conn,
     186             :     "lookup_all_products",
     187             :     params,
     188             :     &lookup_products_cb,
     189             :     &plc);
     190             :   /* If there was an error inside lookup_products_cb, return a hard error. */
     191           0 :   if (plc.extract_failed)
     192           0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     193           0 :   return qs;
     194             : }

Generated by: LCOV version 1.16