LCOV - code coverage report
Current view: top level - backenddb - pg_lookup_product.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 49 49 100.0 %
Date: 2025-06-23 16:22:09 Functions: 1 1 100.0 %

          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_lookup_product.c
      18             :  * @brief Implementation of the lookup_product function for Postgres
      19             :  * @author Iván Ávalos
      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_product.h"
      26             : #include "pg_helper.h"
      27             : 
      28             : 
      29             : enum GNUNET_DB_QueryStatus
      30          40 : TMH_PG_lookup_product (void *cls,
      31             :                        const char *instance_id,
      32             :                        const char *product_id,
      33             :                        struct TALER_MERCHANTDB_ProductDetails *pd,
      34             :                        size_t *num_categories,
      35             :                        uint64_t **categories)
      36             : {
      37          40 :   struct PostgresClosure *pg = cls;
      38          40 :   struct GNUNET_PQ_QueryParam params[] = {
      39          40 :     GNUNET_PQ_query_param_string (instance_id),
      40          40 :     GNUNET_PQ_query_param_string (product_id),
      41             :     GNUNET_PQ_query_param_end
      42             :   };
      43             : 
      44          40 :   PREPARE (pg,
      45             :            "lookup_product",
      46             :            "SELECT"
      47             :            " mi.description"
      48             :            ",mi.description_i18n"
      49             :            ",mi.unit"
      50             :            ",mi.price"
      51             :            ",mi.taxes"
      52             :            ",mi.total_stock"
      53             :            ",mi.total_sold"
      54             :            ",mi.total_lost"
      55             :            ",mi.image"
      56             :            ",mi.address"
      57             :            ",mi.next_restock"
      58             :            ",mi.minimum_age"
      59             :            ",t.category_array AS categories"
      60             :            " FROM merchant_inventory mi"
      61             :            " JOIN merchant_instances inst"
      62             :            "   USING (merchant_serial)"
      63             :            ",LATERAL ("
      64             :            "   SELECT ARRAY ("
      65             :            "     SELECT mpc.category_serial"
      66             :            "       FROM merchant_product_categories mpc"
      67             :            "      WHERE mpc.product_serial = mi.product_serial"
      68             :            "   ) AS category_array"
      69             :            " ) t"
      70             :            " WHERE inst.merchant_id=$1"
      71             :            "   AND mi.product_id=$2"
      72             :            );
      73          40 :   if (NULL == pd)
      74             :   {
      75           9 :     struct GNUNET_PQ_ResultSpec rs_null[] = {
      76             :       GNUNET_PQ_result_spec_end
      77             :     };
      78             : 
      79           9 :     check_connection (pg);
      80           9 :     return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
      81             :                                                      "lookup_product",
      82             :                                                      params,
      83             :                                                      rs_null);
      84             :   }
      85             :   else
      86             :   {
      87          31 :     char *my_description = NULL;
      88          31 :     json_t *my_description_i18n = NULL;
      89          31 :     char *my_unit = NULL;
      90          31 :     char *my_image = NULL;
      91          31 :     json_t *my_address = NULL;
      92          31 :     json_t *my_taxes = NULL;
      93          31 :     uint64_t *my_categories = NULL;
      94          31 :     struct GNUNET_PQ_ResultSpec rs[] = {
      95          31 :       GNUNET_PQ_result_spec_string ("description",
      96             :                                     &my_description),
      97          31 :       TALER_PQ_result_spec_json ("description_i18n",
      98             :                                  &my_description_i18n),
      99          31 :       GNUNET_PQ_result_spec_string ("unit",
     100             :                                     &my_unit),
     101          31 :       TALER_PQ_result_spec_amount_with_currency ("price",
     102             :                                                  &pd->price),
     103          31 :       TALER_PQ_result_spec_json ("taxes",
     104             :                                  &my_taxes),
     105          31 :       GNUNET_PQ_result_spec_uint64 ("total_stock",
     106             :                                     &pd->total_stock),
     107          31 :       GNUNET_PQ_result_spec_uint64 ("total_sold",
     108             :                                     &pd->total_sold),
     109          31 :       GNUNET_PQ_result_spec_uint64 ("total_lost",
     110             :                                     &pd->total_lost),
     111          31 :       GNUNET_PQ_result_spec_string ("image",
     112             :                                     &my_image),
     113          31 :       TALER_PQ_result_spec_json ("address",
     114             :                                  &my_address),
     115          31 :       GNUNET_PQ_result_spec_timestamp ("next_restock",
     116             :                                        &pd->next_restock),
     117          31 :       GNUNET_PQ_result_spec_uint32 ("minimum_age",
     118             :                                     &pd->minimum_age),
     119          31 :       GNUNET_PQ_result_spec_array_uint64 (pg->conn,
     120             :                                           "categories",
     121             :                                           num_categories,
     122             :                                           &my_categories),
     123             :       GNUNET_PQ_result_spec_end
     124             :     };
     125             :     enum GNUNET_DB_QueryStatus qs;
     126             : 
     127          31 :     check_connection (pg);
     128          31 :     qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     129             :                                                    "lookup_product",
     130             :                                                    params,
     131             :                                                    rs);
     132          31 :     pd->description = my_description;
     133          31 :     pd->description_i18n = my_description_i18n;
     134          31 :     pd->unit = my_unit;
     135          31 :     pd->taxes = my_taxes;
     136          31 :     pd->image = my_image;
     137          31 :     pd->address = my_address;
     138          31 :     *categories = my_categories;
     139             :     /* Clear original pointers to that cleanup_result doesn't squash them */
     140          31 :     my_description = NULL;
     141          31 :     my_description_i18n = NULL;
     142          31 :     my_unit = NULL;
     143          31 :     my_taxes = NULL;
     144          31 :     my_image = NULL;
     145          31 :     my_address = NULL;
     146          31 :     my_categories = NULL;
     147          31 :     GNUNET_PQ_cleanup_result (rs);
     148          31 :     return qs;
     149             :   }
     150             : }

Generated by: LCOV version 1.16