LCOV - code coverage report
Current view: top level - backenddb - pg_lookup_product.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 65 65
Test Date: 2025-12-02 20:17:27 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           64 : 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           64 :   struct PostgresClosure *pg = cls;
      38           64 :   struct GNUNET_PQ_QueryParam params[] = {
      39           64 :     GNUNET_PQ_query_param_string (instance_id),
      40           64 :     GNUNET_PQ_query_param_string (product_id),
      41              :     GNUNET_PQ_query_param_end
      42              :   };
      43              : 
      44           64 :   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.price_array"
      53              :            ",mi.taxes::TEXT"
      54              :             ",mi.total_stock"
      55              :             ",mi.total_stock_frac"
      56              :             ",mi.allow_fractional_quantity"
      57              :            ",mi.fractional_precision_level"
      58              :            ",mi.total_sold"
      59              :            ",mi.total_sold_frac"
      60              :            ",mi.total_lost"
      61              :            ",mi.total_lost_frac"
      62              :            ",mi.image"
      63              :            ",mi.address::TEXT"
      64              :            ",mi.next_restock"
      65              :            ",mi.minimum_age"
      66              :            ",t.category_array AS categories"
      67              :            " FROM merchant_inventory mi"
      68              :            " JOIN merchant_instances inst"
      69              :            "   USING (merchant_serial)"
      70              :            ",LATERAL ("
      71              :            "   SELECT ARRAY ("
      72              :            "     SELECT mpc.category_serial"
      73              :            "       FROM merchant_product_categories mpc"
      74              :            "      WHERE mpc.product_serial = mi.product_serial"
      75              :            "   ) AS category_array"
      76              :            " ) t"
      77              :            " WHERE inst.merchant_id=$1"
      78              :            "   AND mi.product_id=$2"
      79              :            );
      80           64 :   if (NULL == pd)
      81              :   {
      82            5 :     struct GNUNET_PQ_ResultSpec rs_null[] = {
      83              :       GNUNET_PQ_result_spec_end
      84              :     };
      85              : 
      86            5 :     check_connection (pg);
      87            5 :     return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
      88              :                                                      "lookup_product",
      89              :                                                      params,
      90              :                                                      rs_null);
      91              :   }
      92              :   else
      93              :   {
      94           59 :     char *my_name = NULL;
      95           59 :     char *my_description = NULL;
      96           59 :     json_t *my_description_i18n = NULL;
      97           59 :     char *my_unit = NULL;
      98           59 :     char *my_image = NULL;
      99           59 :     json_t *my_address = NULL;
     100           59 :     json_t *my_taxes = NULL;
     101           59 :     uint64_t *my_categories = NULL;
     102           59 :     struct TALER_Amount *my_price_array = NULL;
     103           59 :     size_t my_price_array_length = 0;
     104           59 :     struct GNUNET_PQ_ResultSpec rs[] = {
     105           59 :       GNUNET_PQ_result_spec_string ("description",
     106              :                                     &my_description),
     107           59 :       TALER_PQ_result_spec_json ("description_i18n",
     108              :                                  &my_description_i18n),
     109           59 :       GNUNET_PQ_result_spec_string ("product_name",
     110              :                                     &my_name),
     111           59 :       GNUNET_PQ_result_spec_string ("unit",
     112              :                                     &my_unit),
     113           59 :       TALER_PQ_result_spec_amount_with_currency ("price",
     114              :                                                  &pd->price),
     115           59 :       TALER_PQ_result_spec_array_amount_with_currency (pg->conn,
     116              :                                                        "price_array",
     117              :                                                        &my_price_array_length,
     118              :                                                        &my_price_array),
     119           59 :       TALER_PQ_result_spec_json ("taxes",
     120              :                                  &my_taxes),
     121           59 :       GNUNET_PQ_result_spec_uint64 ("total_stock",
     122              :                                     &pd->total_stock),
     123           59 :       GNUNET_PQ_result_spec_uint32 ("total_stock_frac",
     124              :                                     &pd->total_stock_frac),
     125           59 :       GNUNET_PQ_result_spec_bool ("allow_fractional_quantity",
     126              :                                   &pd->allow_fractional_quantity),
     127           59 :       GNUNET_PQ_result_spec_uint32 ("fractional_precision_level",
     128              :                                     &pd->fractional_precision_level),
     129           59 :       GNUNET_PQ_result_spec_uint64 ("total_sold",
     130              :                                     &pd->total_sold),
     131           59 :       GNUNET_PQ_result_spec_uint32 ("total_sold_frac",
     132              :                                     &pd->total_sold_frac),
     133           59 :       GNUNET_PQ_result_spec_uint64 ("total_lost",
     134              :                                     &pd->total_lost),
     135           59 :       GNUNET_PQ_result_spec_uint32 ("total_lost_frac",
     136              :                                     &pd->total_lost_frac),
     137           59 :       GNUNET_PQ_result_spec_string ("image",
     138              :                                     &my_image),
     139           59 :       TALER_PQ_result_spec_json ("address",
     140              :                                  &my_address),
     141           59 :       GNUNET_PQ_result_spec_timestamp ("next_restock",
     142              :                                        &pd->next_restock),
     143           59 :       GNUNET_PQ_result_spec_uint32 ("minimum_age",
     144              :                                     &pd->minimum_age),
     145           59 :       GNUNET_PQ_result_spec_array_uint64 (pg->conn,
     146              :                                           "categories",
     147              :                                           num_categories,
     148              :                                           &my_categories),
     149              :       GNUNET_PQ_result_spec_end
     150              :     };
     151              :     enum GNUNET_DB_QueryStatus qs;
     152              : 
     153           59 :     check_connection (pg);
     154           59 :     qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     155              :                                                    "lookup_product",
     156              :                                                    params,
     157              :                                                    rs);
     158           59 :     pd->product_name = my_name;
     159           59 :     pd->description = my_description;
     160           59 :     pd->description_i18n = my_description_i18n;
     161           59 :     pd->unit = my_unit;
     162           59 :     pd->taxes = my_taxes;
     163           59 :     pd->image = my_image;
     164           59 :     pd->address = my_address;
     165           59 :     pd->price_array = my_price_array;
     166           59 :     pd->price_array_length = my_price_array_length;
     167           59 :     *categories = my_categories;
     168              :     /* Clear original pointers to that cleanup_result doesn't squash them */
     169           59 :     my_name = NULL;
     170           59 :     my_description = NULL;
     171           59 :     my_description_i18n = NULL;
     172           59 :     my_unit = NULL;
     173           59 :     my_taxes = NULL;
     174           59 :     my_image = NULL;
     175           59 :     my_address = NULL;
     176           59 :     my_price_array = NULL;
     177           59 :     my_price_array_length = 0;
     178           59 :     my_categories = NULL;
     179           59 :     GNUNET_PQ_cleanup_result (rs);
     180           59 :     return qs;
     181              :   }
     182              : }
        

Generated by: LCOV version 2.0-1