merchant

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

pg_lookup_units.c (5322B)


      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_units.c
     18  * @brief Implementation of the lookup_units function for Postgres
     19  * @author Bohdan Potuzhnyi
     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_units.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Context used for TMH_PG_lookup_units().
     31  */
     32 struct LookupUnitsContext
     33 {
     34   TALER_MERCHANTDB_UnitsCallback cb;
     35   void *cb_cls;
     36   bool extract_failed;
     37 };
     38 
     39 
     40 static void
     41 lookup_units_cb (void *cls,
     42                  PGresult *result,
     43                  unsigned int num_results)
     44 {
     45   struct LookupUnitsContext *luc = cls;
     46 
     47   for (unsigned int i = 0; i<num_results; i++)
     48   {
     49     struct TALER_MERCHANTDB_UnitDetails ud = { 0 };
     50     struct GNUNET_PQ_ResultSpec rs[] = {
     51       GNUNET_PQ_result_spec_uint64 ("unit_serial",
     52                                     &ud.unit_serial),
     53       GNUNET_PQ_result_spec_string ("unit",
     54                                     &ud.unit),
     55       GNUNET_PQ_result_spec_string ("unit_name_long",
     56                                     &ud.unit_name_long),
     57       GNUNET_PQ_result_spec_string ("unit_name_short",
     58                                     &ud.unit_name_short),
     59       TALER_PQ_result_spec_json ("unit_name_long_i18n",
     60                                  &ud.unit_name_long_i18n),
     61       TALER_PQ_result_spec_json ("unit_name_short_i18n",
     62                                  &ud.unit_name_short_i18n),
     63       GNUNET_PQ_result_spec_bool ("unit_allow_fraction",
     64                                   &ud.unit_allow_fraction),
     65       GNUNET_PQ_result_spec_uint32 ("unit_precision_level",
     66                                     &ud.unit_precision_level),
     67       GNUNET_PQ_result_spec_bool ("unit_active",
     68                                   &ud.unit_active),
     69       GNUNET_PQ_result_spec_bool ("unit_builtin",
     70                                   &ud.unit_builtin),
     71       GNUNET_PQ_result_spec_end
     72     };
     73 
     74     if (GNUNET_OK !=
     75         GNUNET_PQ_extract_result (result,
     76                                   rs,
     77                                   i))
     78     {
     79       GNUNET_break (0);
     80       luc->extract_failed = true;
     81       return;
     82     }
     83     luc->cb (luc->cb_cls,
     84              ud.unit_serial,
     85              &ud);
     86     GNUNET_PQ_cleanup_result (rs);
     87   }
     88 }
     89 
     90 
     91 enum GNUNET_DB_QueryStatus
     92 TMH_PG_lookup_units (void *cls,
     93                      const char *instance_id,
     94                      TALER_MERCHANTDB_UnitsCallback cb,
     95                      void *cb_cls)
     96 {
     97   struct PostgresClosure *pg = cls;
     98   struct LookupUnitsContext luc = {
     99     .cb = cb,
    100     .cb_cls = cb_cls,
    101     .extract_failed = false
    102   };
    103   struct GNUNET_PQ_QueryParam params[] = {
    104     GNUNET_PQ_query_param_string (instance_id),
    105     GNUNET_PQ_query_param_end
    106   };
    107   enum GNUNET_DB_QueryStatus qs;
    108 
    109   check_connection (pg);
    110   PREPARE (pg,
    111            "lookup_units",
    112            "WITH mi AS ("
    113            "  SELECT merchant_serial FROM merchant_instances WHERE merchant_id=$1"
    114            ")"
    115            "SELECT cu.unit_serial"
    116            "      ,cu.unit"
    117            "      ,cu.unit_name_long"
    118            "      ,cu.unit_name_short"
    119            "      ,cu.unit_name_long_i18n"
    120            "      ,cu.unit_name_short_i18n"
    121            "      ,cu.unit_allow_fraction"
    122            "      ,cu.unit_precision_level"
    123            "      ,cu.unit_active"
    124            "      ,FALSE AS unit_builtin"
    125            "  FROM merchant_custom_units cu"
    126            "  JOIN mi ON cu.merchant_serial = mi.merchant_serial"
    127            " UNION ALL "
    128            "SELECT bu.unit_serial"
    129            "      ,bu.unit"
    130            "      ,bu.unit_name_long"
    131            "      ,bu.unit_name_short"
    132            "      ,bu.unit_name_long_i18n"
    133            "      ,bu.unit_name_short_i18n"
    134            "      ,COALESCE(bo.override_allow_fraction, bu.unit_allow_fraction)"
    135            "      ,COALESCE(bo.override_precision_level, bu.unit_precision_level)"
    136            "      ,COALESCE(bo.override_active, bu.unit_active)"
    137            "      ,TRUE AS unit_builtin"
    138            "  FROM merchant_builtin_units bu"
    139            "  JOIN mi ON TRUE"
    140            "  LEFT JOIN merchant_builtin_unit_overrides bo"
    141            "    ON bo.builtin_unit_serial = bu.unit_serial"
    142            "   AND bo.merchant_serial = mi.merchant_serial"
    143            " ORDER BY unit");
    144   qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
    145                                              "lookup_units",
    146                                              params,
    147                                              &lookup_units_cb,
    148                                              &luc);
    149   if (luc.extract_failed)
    150     return GNUNET_DB_STATUS_HARD_ERROR;
    151   return qs;
    152 }