merchant

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

pg_lookup_categories.c (4573B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2024 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_categories.c
     18  * @brief Implementation of the lookup_categories function for Postgres
     19  * @author Christian Grothoff
     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_categories.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Context used for TMH_PG_lookup_categories().
     31  */
     32 struct LookupCategoryContext
     33 {
     34   /**
     35    * Function to call with the results.
     36    */
     37   TALER_MERCHANTDB_CategoriesCallback cb;
     38 
     39   /**
     40    * Closure for @a cb.
     41    */
     42   void *cb_cls;
     43 
     44   /**
     45    * Did database result extraction fail?
     46    */
     47   bool extract_failed;
     48 };
     49 
     50 
     51 /**
     52  * Function to be called with the results of a SELECT statement
     53  * that has returned @a num_results results about otp_device.
     54  *
     55  * @param[in,out] cls of type `struct LookupCategoryContext *`
     56  * @param result the postgres result
     57  * @param num_results the number of results in @a result
     58  */
     59 static void
     60 lookup_categories_cb (void *cls,
     61                       PGresult *result,
     62                       unsigned int num_results)
     63 {
     64   struct LookupCategoryContext *tlc = cls;
     65 
     66   for (unsigned int i = 0; i < num_results; i++)
     67   {
     68     uint64_t category_id;
     69     char *category_name;
     70     json_t *category_name_i18n;
     71     uint64_t product_count;
     72     struct GNUNET_PQ_ResultSpec rs[] = {
     73       GNUNET_PQ_result_spec_uint64 ("category_serial",
     74                                     &category_id),
     75       GNUNET_PQ_result_spec_string ("category_name",
     76                                     &category_name),
     77       TALER_PQ_result_spec_json ("category_name_i18n",
     78                                  &category_name_i18n),
     79       GNUNET_PQ_result_spec_uint64 ("product_count",
     80                                     &product_count),
     81       GNUNET_PQ_result_spec_end
     82     };
     83 
     84     if (GNUNET_OK !=
     85         GNUNET_PQ_extract_result (result,
     86                                   rs,
     87                                   i))
     88     {
     89       GNUNET_break (0);
     90       tlc->extract_failed = true;
     91       return;
     92     }
     93     tlc->cb (tlc->cb_cls,
     94              category_id,
     95              category_name,
     96              category_name_i18n,
     97              product_count);
     98     GNUNET_PQ_cleanup_result (rs);
     99   }
    100 }
    101 
    102 
    103 enum GNUNET_DB_QueryStatus
    104 TMH_PG_lookup_categories (void *cls,
    105                           const char *instance_id,
    106                           TALER_MERCHANTDB_CategoriesCallback cb,
    107                           void *cb_cls)
    108 {
    109 
    110   struct PostgresClosure *pg = cls;
    111   struct LookupCategoryContext tlc = {
    112     .cb = cb,
    113     .cb_cls = cb_cls,
    114     /* Can be overwritten by the lookup_categories_cb */
    115     .extract_failed = false,
    116   };
    117   struct GNUNET_PQ_QueryParam params[] = {
    118     GNUNET_PQ_query_param_string (instance_id),
    119     GNUNET_PQ_query_param_end
    120   };
    121   enum GNUNET_DB_QueryStatus qs;
    122 
    123   check_connection (pg);
    124   PREPARE (pg,
    125            "lookup_categories",
    126            "SELECT"
    127            " mc.category_serial"
    128            ",mc.category_name"
    129            ",mc.category_name_i18n::TEXT"
    130            ",COALESCE(COUNT(mpc.product_serial),0)"
    131            "   AS product_count"
    132            " FROM merchant_categories mc"
    133            " LEFT JOIN merchant_product_categories mpc"
    134            "   USING (category_serial)"
    135            " JOIN merchant_instances mi"
    136            "   USING (merchant_serial)"
    137            " WHERE mi.merchant_id=$1"
    138            " GROUP BY mc.category_serial"
    139            " ORDER BY mc.category_serial;");
    140   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    141                                              "lookup_categories",
    142                                              params,
    143                                              &lookup_categories_cb,
    144                                              &tlc);
    145   /* If there was an error inside lookup_categories_cb, return a hard error. */
    146   if (tlc.extract_failed)
    147     return GNUNET_DB_STATUS_HARD_ERROR;
    148   return qs;
    149 }