merchant

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

pg_lookup_categories_by_ids.c (4620B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 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_lookup_categories_by_ids.c
     18  * @brief Lookup product categories by ID
     19  * @author Bohdan Potuzhnyi
     20  */
     21 #include "taler/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_by_ids.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Context used for TMH_PG_lookup_categories_by_ids().
     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 static void
     52 lookup_categories_cb (void *cls,
     53                       PGresult *result,
     54                       unsigned int num_results)
     55 {
     56   struct LookupCategoryContext *tlc = cls;
     57 
     58   for (unsigned int i = 0; i < num_results; i++)
     59   {
     60     uint64_t category_id;
     61     char *category_name;
     62     json_t *category_name_i18n;
     63     uint64_t product_count;
     64     struct GNUNET_PQ_ResultSpec rs[] = {
     65       GNUNET_PQ_result_spec_uint64 ("category_serial",
     66                                     &category_id),
     67       GNUNET_PQ_result_spec_string ("category_name",
     68                                     &category_name),
     69       TALER_PQ_result_spec_json ("category_name_i18n",
     70                                  &category_name_i18n),
     71       GNUNET_PQ_result_spec_uint64 ("product_count",
     72                                     &product_count),
     73       GNUNET_PQ_result_spec_end
     74     };
     75 
     76     if (GNUNET_OK !=
     77         GNUNET_PQ_extract_result (result,
     78                                   rs,
     79                                   i))
     80     {
     81       GNUNET_break (0);
     82       tlc->extract_failed = true;
     83       return;
     84     }
     85     tlc->cb (tlc->cb_cls,
     86              category_id,
     87              category_name,
     88              category_name_i18n,
     89              product_count);
     90     GNUNET_PQ_cleanup_result (rs);
     91   }
     92 }
     93 
     94 
     95 enum GNUNET_DB_QueryStatus
     96 TMH_PG_lookup_categories_by_ids (void *cls,
     97                                  const char *instance_id,
     98                                  const uint64_t *category_ids,
     99                                  size_t num_category_ids,
    100                                  TALER_MERCHANTDB_CategoriesCallback cb,
    101                                  void *cb_cls)
    102 {
    103   struct PostgresClosure *pg = cls;
    104   struct LookupCategoryContext tlc = {
    105     .cb = cb,
    106     .cb_cls = cb_cls,
    107     /* Can be overwritten by the lookup_categories_cb */
    108     .extract_failed = false,
    109   };
    110   struct GNUNET_PQ_QueryParam params[] = {
    111     GNUNET_PQ_query_param_string (instance_id),
    112     GNUNET_PQ_query_param_array_uint64 (num_category_ids,
    113                                         category_ids,
    114                                         pg->conn),
    115     GNUNET_PQ_query_param_end
    116   };
    117   enum GNUNET_DB_QueryStatus qs;
    118 
    119   check_connection (pg);
    120   PREPARE (pg,
    121            "lookup_categories_by_ids",
    122            "SELECT"
    123            " mc.category_serial"
    124            ",mc.category_name"
    125            ",mc.category_name_i18n::TEXT"
    126            ",COALESCE(COUNT(mpc.product_serial),0)"
    127            "   AS product_count"
    128            " FROM merchant_categories mc"
    129            " LEFT JOIN merchant_product_categories mpc"
    130            "   USING (category_serial)"
    131            " JOIN merchant_instances mi"
    132            "   USING (merchant_serial)"
    133            " WHERE mi.merchant_id=$1"
    134            "   AND mc.category_serial = ANY ($2)"
    135            " GROUP BY mc.category_serial"
    136            " ORDER BY mc.category_serial;");
    137   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    138                                              "lookup_categories_by_ids",
    139                                              params,
    140                                              &lookup_categories_cb,
    141                                              &tlc);
    142   GNUNET_PQ_cleanup_query_params_closures (params);
    143   if (tlc.extract_failed)
    144     return GNUNET_DB_STATUS_HARD_ERROR;
    145   return qs;
    146 }