merchant

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

select_product_groups.c (5196B)


      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 src/backenddb/select_product_groups.c
     18  * @brief Implementation of the select_product_groups 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 "merchant-database/select_product_groups.h"
     26 #include "helper.h"
     27 
     28 
     29 /**
     30  * Context used for TALER_MERCHANTDB_lookup_product_groups().
     31  */
     32 struct LookupProductGroupsContext
     33 {
     34   /**
     35    * Function to call with the results.
     36    */
     37   TALER_MERCHANTDB_ProductGroupsCallback 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 product groups.
     54  *
     55  * @param[in,out] cls of type `struct LookupProductGroupsContext *`
     56  * @param result the postgres result
     57  * @param num_results the number of results in @a result
     58  */
     59 static void
     60 lookup_product_groups_cb (void *cls,
     61                           PGresult *result,
     62                           unsigned int num_results)
     63 {
     64   struct LookupProductGroupsContext *plc = cls;
     65 
     66   for (unsigned int i = 0; i < num_results; i++)
     67   {
     68     uint64_t product_group_serial;
     69     char *product_group_name;
     70     char *product_group_description;
     71     struct GNUNET_PQ_ResultSpec rs[] = {
     72       GNUNET_PQ_result_spec_uint64 ("product_group_serial",
     73                                     &product_group_serial),
     74       GNUNET_PQ_result_spec_string ("product_group_name",
     75                                     &product_group_name),
     76       GNUNET_PQ_result_spec_string ("product_group_description",
     77                                     &product_group_description),
     78       GNUNET_PQ_result_spec_end
     79     };
     80 
     81     if (GNUNET_OK !=
     82         GNUNET_PQ_extract_result (result,
     83                                   rs,
     84                                   i))
     85     {
     86       GNUNET_break (0);
     87       plc->extract_failed = true;
     88       return;
     89     }
     90     plc->cb (plc->cb_cls,
     91              product_group_serial,
     92              product_group_name,
     93              product_group_description);
     94     GNUNET_PQ_cleanup_result (rs);
     95   }
     96 }
     97 
     98 
     99 enum GNUNET_DB_QueryStatus
    100 TALER_MERCHANTDB_select_product_groups (struct TALER_MERCHANTDB_PostgresContext *pg,
    101                                         const char *instance_id,
    102                                         int64_t limit,
    103                                         uint64_t offset,
    104                                         TALER_MERCHANTDB_ProductGroupsCallback cb,
    105                                         void *cb_cls)
    106 {
    107   uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
    108   struct LookupProductGroupsContext plc = {
    109     .cb = cb,
    110     .cb_cls = cb_cls,
    111     /* Can be overwritten by the lookup_product_groups_cb */
    112     .extract_failed = false,
    113   };
    114   struct GNUNET_PQ_QueryParam params[] = {
    115     GNUNET_PQ_query_param_string (instance_id),
    116     GNUNET_PQ_query_param_uint64 (&offset),
    117     GNUNET_PQ_query_param_uint64 (&plimit),
    118     GNUNET_PQ_query_param_end
    119   };
    120   enum GNUNET_DB_QueryStatus qs;
    121 
    122   check_connection (pg);
    123   PREPARE (pg,
    124            "lookup_product_groups_asc",
    125            "SELECT"
    126            "  product_group_serial"
    127            " ,product_group_name"
    128            " ,product_group_description"
    129            " FROM merchant_product_groups"
    130            " JOIN merchant_instances"
    131            "   USING (merchant_serial)"
    132            " WHERE merchant_instances.merchant_id=$1"
    133            "   AND product_group_serial > $2"
    134            " ORDER BY product_group_serial ASC"
    135            " LIMIT $3");
    136   PREPARE (pg,
    137            "lookup_product_groups_desc",
    138            "SELECT"
    139            "  product_group_serial"
    140            " ,product_group_name"
    141            " ,product_group_description"
    142            " FROM merchant_product_groups"
    143            " JOIN merchant_instances"
    144            "   USING (merchant_serial)"
    145            " WHERE merchant_instances.merchant_id=$1"
    146            "   AND product_group_serial < $2"
    147            " ORDER BY product_group_serial DESC"
    148            " LIMIT $3");
    149   qs = GNUNET_PQ_eval_prepared_multi_select (
    150     pg->conn,
    151     (limit > 0)
    152     ? "lookup_product_groups_asc"
    153     : "lookup_product_groups_desc",
    154     params,
    155     &lookup_product_groups_cb,
    156     &plc);
    157   /* If there was an error inside lookup_product_groups_cb, return a hard error. */
    158   if (plc.extract_failed)
    159   {
    160     GNUNET_break (0);
    161     return GNUNET_DB_STATUS_HARD_ERROR;
    162   }
    163   return qs;
    164 }