merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

lookup_products.c (6731B)


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