LCOV - code coverage report
Current view: top level - backenddb - pg_update_product.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 94.6 % 37 35
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, 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 backenddb/pg_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_error_codes.h>
      24              : #include <taler/taler_dbevents.h>
      25              : #include <taler/taler_pq_lib.h>
      26              : #include "pg_update_product.h"
      27              : #include "pg_helper.h"
      28              : 
      29              : 
      30              : enum GNUNET_DB_QueryStatus
      31            8 : TMH_PG_update_product (void *cls,
      32              :                        const char *instance_id,
      33              :                        const char *product_id,
      34              :                        const struct TALER_MERCHANTDB_ProductDetails *pd,
      35              :                        size_t num_cats,
      36              :                        const uint64_t *cats,
      37              :                        bool *no_instance,
      38              :                        ssize_t *no_cat,
      39              :                        bool *no_product,
      40              :                        bool *lost_reduced,
      41              :                        bool *sold_reduced,
      42              :                        bool *stocked_reduced)
      43              : {
      44            8 :   struct PostgresClosure *pg = cls;
      45            8 :   struct GNUNET_PQ_QueryParam params[] = {
      46            8 :     GNUNET_PQ_query_param_string (instance_id), /* $1 */
      47            8 :     GNUNET_PQ_query_param_string (product_id),
      48            8 :     GNUNET_PQ_query_param_string (pd->description),
      49            8 :     TALER_PQ_query_param_json (pd->description_i18n),
      50            8 :     GNUNET_PQ_query_param_string (pd->unit),
      51            8 :     GNUNET_PQ_query_param_string (pd->image), /* $6 */
      52            8 :     TALER_PQ_query_param_json (pd->taxes),
      53            8 :     TALER_PQ_query_param_amount_with_currency (pg->conn,
      54              :                                                &pd->price), /* $8 */
      55            8 :     GNUNET_PQ_query_param_uint64 (&pd->total_stock),  /* $9 */
      56            8 :     GNUNET_PQ_query_param_uint64 (&pd->total_lost),
      57            8 :     TALER_PQ_query_param_json (pd->address),
      58            8 :     GNUNET_PQ_query_param_timestamp (&pd->next_restock),
      59            8 :     GNUNET_PQ_query_param_uint32 (&pd->minimum_age),
      60            8 :     GNUNET_PQ_query_param_array_uint64 (num_cats,
      61              :                                         cats,
      62              :                                         pg->conn),
      63            8 :     GNUNET_PQ_query_param_string (pd->product_name),
      64              :     GNUNET_PQ_query_param_end
      65              :   };
      66              :   uint64_t ncat;
      67            8 :   bool cats_found = true;
      68            8 :   struct GNUNET_PQ_ResultSpec rs[] = {
      69            8 :     GNUNET_PQ_result_spec_bool ("no_instance",
      70              :                                 no_instance),
      71            8 :     GNUNET_PQ_result_spec_bool ("no_product",
      72              :                                 no_product),
      73            8 :     GNUNET_PQ_result_spec_bool ("lost_reduced",
      74              :                                 lost_reduced),
      75            8 :     GNUNET_PQ_result_spec_bool ("sold_reduced",
      76              :                                 sold_reduced),
      77            8 :     GNUNET_PQ_result_spec_bool ("stocked_reduced",
      78              :                                 stocked_reduced),
      79            8 :     GNUNET_PQ_result_spec_allow_null (
      80              :       GNUNET_PQ_result_spec_uint64 ("no_cat",
      81              :                                     &ncat),
      82              :       &cats_found),
      83              :     GNUNET_PQ_result_spec_end
      84              :   };
      85              :   enum GNUNET_DB_QueryStatus qs;
      86              : 
      87            8 :   if ( (pd->total_stock < pd->total_lost + pd->total_sold) ||
      88            8 :        (pd->total_lost < pd->total_lost
      89            8 :         + pd->total_sold) /* integer overflow */)
      90              :   {
      91            0 :     GNUNET_break (0);
      92            0 :     return GNUNET_DB_STATUS_HARD_ERROR;
      93              :   }
      94            8 :   check_connection (pg);
      95            8 :   PREPARE (pg,
      96              :            "update_product",
      97              :            "SELECT"
      98              :            " out_lost_reduced AS lost_reduced"
      99              :            ",out_sold_reduced AS sold_reduced"
     100              :            ",out_stocked_reduced AS stocked_reduced"
     101              :            ",out_no_product AS no_product"
     102              :            ",out_no_cat AS no_cat"
     103              :            ",out_no_instance AS no_instance"
     104              :            " FROM merchant_do_update_product"
     105              :            "($1,$2,$3,$4::TEXT::JSONB,$5,$6,$7::TEXT::JSONB,$8,$9"
     106              :            ",$10,$11::TEXT::JSONB,$12,$13,$14,$15);");
     107            8 :   qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     108              :                                                  "update_product",
     109              :                                                  params,
     110              :                                                  rs);
     111            8 :   GNUNET_PQ_cleanup_query_params_closures (params);
     112            8 :   *no_cat = (cats_found) ? -1 : (ssize_t) ncat;
     113            8 :   return qs;
     114              : }
        

Generated by: LCOV version 2.0-1