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

            Line data    Source code
       1              : /*
       2              :    This file is part of TALER
       3              :    Copyright (C) 2022, 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 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::TEXT"
      49              :            ",mi.product_name"
      50              :            ",mi.unit"
      51              :            ",mi.price"
      52              :            ",mi.taxes::TEXT"
      53              :            ",mi.total_stock"
      54              :            ",mi.total_sold"
      55              :            ",mi.total_lost"
      56              :            ",mi.image"
      57              :            ",mi.address::TEXT"
      58              :            ",mi.next_restock"
      59              :            ",mi.minimum_age"
      60              :            ",t.category_array AS categories"
      61              :            " FROM merchant_inventory mi"
      62              :            " JOIN merchant_instances inst"
      63              :            "   USING (merchant_serial)"
      64              :            ",LATERAL ("
      65              :            "   SELECT ARRAY ("
      66              :            "     SELECT mpc.category_serial"
      67              :            "       FROM merchant_product_categories mpc"
      68              :            "      WHERE mpc.product_serial = mi.product_serial"
      69              :            "   ) AS category_array"
      70              :            " ) t"
      71              :            " WHERE inst.merchant_id=$1"
      72              :            "   AND mi.product_id=$2"
      73              :            );
      74           40 :   if (NULL == pd)
      75              :   {
      76            9 :     struct GNUNET_PQ_ResultSpec rs_null[] = {
      77              :       GNUNET_PQ_result_spec_end
      78              :     };
      79              : 
      80            9 :     check_connection (pg);
      81            9 :     return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
      82              :                                                      "lookup_product",
      83              :                                                      params,
      84              :                                                      rs_null);
      85              :   }
      86              :   else
      87              :   {
      88           31 :     char *my_name = NULL;
      89           31 :     char *my_description = NULL;
      90           31 :     json_t *my_description_i18n = NULL;
      91           31 :     char *my_unit = NULL;
      92           31 :     char *my_image = NULL;
      93           31 :     json_t *my_address = NULL;
      94           31 :     json_t *my_taxes = NULL;
      95           31 :     uint64_t *my_categories = NULL;
      96           31 :     struct GNUNET_PQ_ResultSpec rs[] = {
      97           31 :       GNUNET_PQ_result_spec_string ("description",
      98              :                                     &my_description),
      99           31 :       TALER_PQ_result_spec_json ("description_i18n",
     100              :                                  &my_description_i18n),
     101           31 :       GNUNET_PQ_result_spec_string ("product_name",
     102              :                                     &my_name),
     103           31 :       GNUNET_PQ_result_spec_string ("unit",
     104              :                                     &my_unit),
     105           31 :       TALER_PQ_result_spec_amount_with_currency ("price",
     106              :                                                  &pd->price),
     107           31 :       TALER_PQ_result_spec_json ("taxes",
     108              :                                  &my_taxes),
     109           31 :       GNUNET_PQ_result_spec_uint64 ("total_stock",
     110              :                                     &pd->total_stock),
     111           31 :       GNUNET_PQ_result_spec_uint64 ("total_sold",
     112              :                                     &pd->total_sold),
     113           31 :       GNUNET_PQ_result_spec_uint64 ("total_lost",
     114              :                                     &pd->total_lost),
     115           31 :       GNUNET_PQ_result_spec_string ("image",
     116              :                                     &my_image),
     117           31 :       TALER_PQ_result_spec_json ("address",
     118              :                                  &my_address),
     119           31 :       GNUNET_PQ_result_spec_timestamp ("next_restock",
     120              :                                        &pd->next_restock),
     121           31 :       GNUNET_PQ_result_spec_uint32 ("minimum_age",
     122              :                                     &pd->minimum_age),
     123           31 :       GNUNET_PQ_result_spec_array_uint64 (pg->conn,
     124              :                                           "categories",
     125              :                                           num_categories,
     126              :                                           &my_categories),
     127              :       GNUNET_PQ_result_spec_end
     128              :     };
     129              :     enum GNUNET_DB_QueryStatus qs;
     130              : 
     131           31 :     check_connection (pg);
     132           31 :     qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     133              :                                                    "lookup_product",
     134              :                                                    params,
     135              :                                                    rs);
     136           31 :     pd->product_name = my_name;
     137           31 :     pd->description = my_description;
     138           31 :     pd->description_i18n = my_description_i18n;
     139           31 :     pd->unit = my_unit;
     140           31 :     pd->taxes = my_taxes;
     141           31 :     pd->image = my_image;
     142           31 :     pd->address = my_address;
     143           31 :     *categories = my_categories;
     144              :     /* Clear original pointers to that cleanup_result doesn't squash them */
     145           31 :     my_name = NULL;
     146           31 :     my_description = NULL;
     147           31 :     my_description_i18n = NULL;
     148           31 :     my_unit = NULL;
     149           31 :     my_taxes = NULL;
     150           31 :     my_image = NULL;
     151           31 :     my_address = NULL;
     152           31 :     my_categories = NULL;
     153           31 :     GNUNET_PQ_cleanup_result (rs);
     154           31 :     return qs;
     155              :   }
     156              : }
        

Generated by: LCOV version 2.0-1