LCOV - code coverage report
Current view: top level - backenddb - iterate_products.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 83.3 % 36 30
Test Date: 2026-09-04 23:42:01 Functions: 100.0 % 2 2

            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 src/backenddb/iterate_products.c
      18              :  * @brief Implementation of the iterate_products function for Postgres
      19              :  * @author Iván Ávalos
      20              :  */
      21              : #include "platform.h"
      22              : #include <taler/taler_pq_lib.h>
      23              : #include "merchant-database/iterate_products.h"
      24              : #include "helper.h"
      25              : 
      26              : 
      27              : /**
      28              :  * Hard upper bound on the number of records returned by a single
      29              :  * call, regardless of the limit requested by the client.
      30              :  */
      31              : #define MAX_RECORDS 50000
      32              : 
      33              : 
      34              : /**
      35              :  * Context used for TALER_MERCHANTDB_iterate_products().
      36              :  */
      37              : struct LookupProductsContext
      38              : {
      39              :   /**
      40              :    * Function to call with the results.
      41              :    */
      42              :   TALER_MERCHANTDB_ProductsCallback cb;
      43              : 
      44              :   /**
      45              :    * Closure for @a cb.
      46              :    */
      47              :   void *cb_cls;
      48              : 
      49              :   /**
      50              :    * Did database result extraction fail?
      51              :    */
      52              :   bool extract_failed;
      53              : };
      54              : 
      55              : 
      56              : /**
      57              :  * Function to be called with the results of a SELECT statement
      58              :  * that has returned @a num_results results about products.
      59              :  *
      60              :  * @param[in,out] cls of type `struct LookupProductsContext *`
      61              :  * @param result the postgres result
      62              :  * @param num_results the number of results in @a result
      63              :  */
      64              : static void
      65           17 : lookup_products_cb (void *cls,
      66              :                     PGresult *result,
      67              :                     unsigned int num_results)
      68              : {
      69           17 :   struct LookupProductsContext *plc = cls;
      70              : 
      71           24 :   for (unsigned int i = 0; i < num_results; i++)
      72              :   {
      73              :     char *product_id;
      74              :     uint64_t product_serial;
      75            7 :     struct GNUNET_PQ_ResultSpec rs[] = {
      76            7 :       GNUNET_PQ_result_spec_string ("product_id",
      77              :                                     &product_id),
      78            7 :       GNUNET_PQ_result_spec_uint64 ("product_serial",
      79              :                                     &product_serial),
      80              :       GNUNET_PQ_result_spec_end
      81              :     };
      82              : 
      83            7 :     if (GNUNET_OK !=
      84            7 :         GNUNET_PQ_extract_result (result,
      85              :                                   rs,
      86              :                                   i))
      87              :     {
      88            0 :       GNUNET_break (0);
      89            0 :       plc->extract_failed = true;
      90            0 :       return;
      91              :     }
      92            7 :     plc->cb (plc->cb_cls,
      93              :              product_serial,
      94              :              product_id);
      95            7 :     GNUNET_PQ_cleanup_result (rs);
      96              :   }
      97              : }
      98              : 
      99              : 
     100              : enum GNUNET_DB_QueryStatus
     101           17 : TALER_MERCHANTDB_iterate_products (
     102              :   struct TALER_MERCHANTDB_PostgresContext *pg,
     103              :   const char *instance_id,
     104              :   uint64_t offset,
     105              :   int64_t limit,
     106              :   const char *category_filter,
     107              :   const char *name_filter,
     108              :   const char *description_filter,
     109              :   uint64_t product_group_id_filter,
     110              :   TALER_MERCHANTDB_ProductsCallback cb,
     111              :   void *cb_cls)
     112              : {
     113           17 :   uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
     114              :                                 TALER_MERCHANTDB_abs_limit (limit));
     115           17 :   struct LookupProductsContext plc = {
     116              :     .cb = cb,
     117              :     .cb_cls = cb_cls,
     118              :     /* Can be overwritten by the lookup_products_cb */
     119              :     .extract_failed = false,
     120              :   };
     121           17 :   struct GNUNET_PQ_QueryParam params[] = {
     122           17 :     GNUNET_PQ_query_param_uint64 (&offset),
     123              :     NULL == category_filter
     124           17 :     ? GNUNET_PQ_query_param_null ()
     125           17 :     : GNUNET_PQ_query_param_string (category_filter),
     126              :     NULL == name_filter
     127           16 :     ? GNUNET_PQ_query_param_null ()
     128           17 :     : GNUNET_PQ_query_param_string (name_filter),
     129              :     NULL == description_filter
     130           17 :     ? GNUNET_PQ_query_param_null ()
     131           17 :     : GNUNET_PQ_query_param_string (description_filter),
     132           17 :     GNUNET_PQ_query_param_uint64 (&plimit),
     133           17 :     GNUNET_PQ_query_param_uint64 (&product_group_id_filter),
     134              :     GNUNET_PQ_query_param_end
     135              :   };
     136              :   enum GNUNET_DB_QueryStatus qs;
     137              : 
     138           17 :   GNUNET_assert (NULL != pg->current_merchant_id);
     139           17 :   GNUNET_assert (0 == strcmp (instance_id,
     140              :                               pg->current_merchant_id));
     141           17 :   if (limit > 0)
     142              :   {
     143           17 :     TMH_PQ_prepare_anon (pg,
     144              :                          "SELECT"
     145              :                          "  product_id"
     146              :                          " ,product_serial"
     147              :                          " FROM merchant_inventory"
     148              :                          " WHERE product_serial > $1"
     149              :                          "   AND ( ($2::TEXT IS NULL) OR"
     150              :                          "         (product_serial IN"
     151              :                          "          (SELECT product_serial"
     152              :                          "             FROM merchant_product_categories"
     153              :                          "            WHERE category_serial IN"
     154              :                          "               (SELECT category_serial"
     155              :                          "                  FROM merchant_categories"
     156              :                          "                 WHERE LOWER(category_name) LIKE LOWER($2)))) )"
     157              :                          "   AND ( (0 = $6::INT8) OR"
     158              :                          "         (product_group_serial = $6) )"
     159              :                          "   AND ( ($3::TEXT IS NULL) OR"
     160              :                          "         (LOWER(product_name) LIKE LOWER($3)) )"
     161              :                          "   AND ( ($4::TEXT IS NULL) OR"
     162              :                          "         (description LIKE LOWER($4)) )"
     163              :                          " ORDER BY product_serial ASC"
     164              :                          " LIMIT $5");
     165              :   }
     166              :   else
     167              :   {
     168            0 :     TMH_PQ_prepare_anon (pg,
     169              :                          "SELECT"
     170              :                          "  product_id"
     171              :                          " ,product_serial"
     172              :                          " FROM merchant_inventory"
     173              :                          " WHERE product_serial < $1"
     174              :                          "   AND ( ($2::TEXT IS NULL) OR"
     175              :                          "         (product_serial IN"
     176              :                          "          (SELECT product_serial"
     177              :                          "             FROM merchant_product_categories"
     178              :                          "            WHERE category_serial IN"
     179              :                          "               (SELECT category_serial"
     180              :                          "                  FROM merchant_categories"
     181              :                          "                 WHERE LOWER(category_name) LIKE LOWER($2)))) )"
     182              :                          "   AND ( (0 = $6::INT8) OR"
     183              :                          "         (product_group_serial = $6) )"
     184              :                          "   AND ( ($3::TEXT IS NULL) OR"
     185              :                          "         (LOWER(product_name) LIKE LOWER($3)) )"
     186              :                          "   AND ( ($4::TEXT IS NULL) OR"
     187              :                          "         (description LIKE LOWER($4)) )"
     188              :                          " ORDER BY product_serial DESC"
     189              :                          " LIMIT $5");
     190              :   }
     191           17 :   qs = GNUNET_PQ_eval_prepared_multi_select (
     192              :     pg->conn,
     193              :     "",
     194              :     params,
     195              :     &lookup_products_cb,
     196              :     &plc);
     197              :   /* If there was an error inside lookup_products_cb, return a hard error. */
     198           17 :   if (plc.extract_failed)
     199              :   {
     200            0 :     GNUNET_break (0);
     201            0 :     return GNUNET_DB_STATUS_HARD_ERROR;
     202              :   }
     203           17 :   return qs;
     204              : }
        

Generated by: LCOV version 2.0-1