LCOV - code coverage report
Current view: top level - backenddb - update_product.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 95.9 % 49 47
Test Date: 2026-09-04 23:42:01 Functions: 100.0 % 1 1

            Line data    Source code
       1              : /*
       2              :    This file is part of TALER
       3              :    Copyright (C) 2022, 2024, 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 src/backenddb/update_product.c
      18              :  * @brief Implementation of the update_product function for Postgres
      19              :  * @author Christian Grothoff
      20              :  * @author Iván Ávalos
      21              :  */
      22              : #include "platform.h"
      23              : #include <taler/taler_pq_lib.h>
      24              : #include "merchant-database/update_product.h"
      25              : #include "helper.h"
      26              : 
      27              : 
      28              : enum GNUNET_DB_QueryStatus
      29           29 : TALER_MERCHANTDB_update_product (
      30              :   struct TALER_MERCHANTDB_PostgresContext *pg,
      31              :   const char *instance_id,
      32              :   const char *product_id,
      33              :   const struct TALER_MERCHANTDB_ProductDetails *pd,
      34              :   size_t num_cats,
      35              :   const uint64_t *cats,
      36              :   ssize_t *no_cat,
      37              :   bool *no_product,
      38              :   bool *lost_reduced,
      39              :   bool *sold_reduced,
      40              :   bool *stocked_reduced,
      41              :   bool *no_group,
      42              :   bool *no_pot)
      43              : {
      44           29 :   struct GNUNET_PQ_QueryParam params[] = {
      45           29 :     GNUNET_PQ_query_param_string (product_id), /* $1 */
      46           29 :     GNUNET_PQ_query_param_string (pd->description),
      47           29 :     TALER_PQ_query_param_json (pd->description_i18n), /* $3 */
      48           29 :     GNUNET_PQ_query_param_string (pd->unit),
      49           29 :     GNUNET_PQ_query_param_string (pd->image), /* $5 */
      50           29 :     TALER_PQ_query_param_json (pd->taxes), /* $6 */
      51           29 :     TALER_PQ_query_param_array_amount_with_currency (
      52           29 :       pd->price_array_length,
      53           29 :       pd->price_array,
      54              :       "merchant",
      55              :       pg->conn), /* $7 */
      56           29 :     GNUNET_PQ_query_param_uint64 (&pd->total_stock),  /* $8 */
      57           29 :     GNUNET_PQ_query_param_uint32 (&pd->total_stock_frac), /* $9 */
      58           29 :     GNUNET_PQ_query_param_bool (pd->allow_fractional_quantity), /* $10 */
      59           29 :     GNUNET_PQ_query_param_uint32 (&pd->fractional_precision_level),
      60           29 :     GNUNET_PQ_query_param_uint64 (&pd->total_lost),
      61           29 :     TALER_PQ_query_param_json (pd->address), /* $13 */
      62           29 :     GNUNET_PQ_query_param_timestamp (&pd->next_restock),
      63           29 :     GNUNET_PQ_query_param_uint32 (&pd->minimum_age),
      64           29 :     GNUNET_PQ_query_param_array_uint64 (num_cats,
      65              :                                         cats,
      66              :                                         pg->conn), /* $16 */
      67           29 :     GNUNET_PQ_query_param_string (pd->product_name), /* $17 */
      68           29 :     (0 == pd->product_group_id)
      69           29 :     ? GNUNET_PQ_query_param_null ()
      70           29 :     : GNUNET_PQ_query_param_uint64 (&pd->product_group_id),
      71           29 :     (0 == pd->money_pot_id)
      72           29 :     ? GNUNET_PQ_query_param_null ()
      73           29 :     : GNUNET_PQ_query_param_uint64 (&pd->money_pot_id), /* $19 */
      74           29 :     GNUNET_PQ_query_param_bool (pd->price_is_net),
      75              :     GNUNET_PQ_query_param_end
      76              :   };
      77              :   uint64_t ncat;
      78           29 :   bool cats_found = true;
      79           29 :   struct GNUNET_PQ_ResultSpec rs[] = {
      80           29 :     GNUNET_PQ_result_spec_bool ("no_product",
      81              :                                 no_product),
      82           29 :     GNUNET_PQ_result_spec_bool ("lost_reduced",
      83              :                                 lost_reduced),
      84           29 :     GNUNET_PQ_result_spec_bool ("sold_reduced",
      85              :                                 sold_reduced),
      86           29 :     GNUNET_PQ_result_spec_bool ("stocked_reduced",
      87              :                                 stocked_reduced),
      88           29 :     GNUNET_PQ_result_spec_allow_null (
      89              :       GNUNET_PQ_result_spec_uint64 ("no_cat",
      90              :                                     &ncat),
      91              :       &cats_found),
      92           29 :     GNUNET_PQ_result_spec_bool ("no_group",
      93              :                                 no_group),
      94           29 :     GNUNET_PQ_result_spec_bool ("no_pot",
      95              :                                 no_pot),
      96              :     GNUNET_PQ_result_spec_end
      97              :   };
      98              :   enum GNUNET_DB_QueryStatus qs;
      99              : 
     100           29 :   if ( (pd->total_stock < pd->total_lost + pd->total_sold) ||
     101           29 :        (pd->total_lost > pd->total_lost
     102           29 :         + pd->total_sold) /* integer overflow */)
     103              :   {
     104            0 :     GNUNET_break (0);
     105            0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     106              :   }
     107           29 :   GNUNET_assert (NULL != pg->current_merchant_id);
     108           29 :   GNUNET_assert (0 == strcmp (instance_id,
     109              :                               pg->current_merchant_id));
     110           29 :   TMH_PQ_prepare_anon (pg,
     111              :                        "SELECT"
     112              :                        " out_lost_reduced AS lost_reduced"
     113              :                        ",out_sold_reduced AS sold_reduced"
     114              :                        ",out_stocked_reduced AS stocked_reduced"
     115              :                        ",out_no_product AS no_product"
     116              :                        ",out_no_cat AS no_cat"
     117              :                        ",out_no_group AS no_group"
     118              :                        ",out_no_pot AS no_pot"
     119              :                        " FROM merchant_do_update_product"
     120              :                        "($1,$2,$3::TEXT::JSONB,$4,$5,$6::TEXT::JSONB,$7,$8"
     121              :                        ",$9,$10,$11,$12,$13::TEXT::JSONB,$14,$15,$16,$17"
     122              :                        ",$18,$19,$20);");
     123           29 :   qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     124              :                                                  "",
     125              :                                                  params,
     126              :                                                  rs);
     127           29 :   GNUNET_PQ_cleanup_query_params_closures (params);
     128           29 :   *no_cat = (cats_found) ? -1 : (ssize_t) ncat;
     129           29 :   return qs;
     130              : }
        

Generated by: LCOV version 2.0-1