merchant

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

select_unit.c (4716B)


      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_unit.c
     18  * @brief Implementation of the select_unit function for Postgres
     19  * @author Bohdan Potuzhnyi
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/select_unit.h"
     24 #include "helper.h"
     25 
     26 
     27 enum GNUNET_DB_QueryStatus
     28 TALER_MERCHANTDB_select_unit (
     29   struct TALER_MERCHANTDB_PostgresContext *pg,
     30   const char *instance_id,
     31   const char *unit_id,
     32   struct TALER_MERCHANTDB_UnitDetails *ud)
     33 {
     34   enum GNUNET_DB_QueryStatus qs;
     35   struct GNUNET_PQ_QueryParam params[] = {
     36     GNUNET_PQ_query_param_string (unit_id),
     37     GNUNET_PQ_query_param_end
     38   };
     39   struct GNUNET_PQ_ResultSpec rs[] = {
     40     GNUNET_PQ_result_spec_uint64 ("unit_serial",
     41                                   &ud->unit_serial),
     42     GNUNET_PQ_result_spec_string ("unit",
     43                                   &ud->unit),
     44     GNUNET_PQ_result_spec_string ("unit_name_long",
     45                                   &ud->unit_name_long),
     46     GNUNET_PQ_result_spec_string ("unit_name_short",
     47                                   &ud->unit_name_short),
     48     TALER_PQ_result_spec_json ("unit_name_long_i18n",
     49                                &ud->unit_name_long_i18n),
     50     TALER_PQ_result_spec_json ("unit_name_short_i18n",
     51                                &ud->unit_name_short_i18n),
     52     GNUNET_PQ_result_spec_bool ("unit_allow_fraction",
     53                                 &ud->unit_allow_fraction),
     54     GNUNET_PQ_result_spec_uint32 ("unit_precision_level",
     55                                   &ud->unit_precision_level),
     56     GNUNET_PQ_result_spec_bool ("unit_active",
     57                                 &ud->unit_active),
     58     GNUNET_PQ_result_spec_bool ("unit_builtin",
     59                                 &ud->unit_builtin),
     60     GNUNET_PQ_result_spec_end
     61   };
     62 
     63   GNUNET_assert (NULL != pg->current_merchant_id);
     64   GNUNET_assert (0 == strcmp (instance_id,
     65                               pg->current_merchant_id));
     66   check_connection (pg);
     67   // FIXME: combine both SELECT statements into a single statement!
     68   TMH_PQ_prepare_anon (pg,
     69                        "SELECT"
     70                        "  cu.unit_serial"
     71                        " ,cu.unit"
     72                        " ,cu.unit_name_long"
     73                        " ,cu.unit_name_short"
     74                        " ,cu.unit_name_long_i18n"
     75                        " ,cu.unit_name_short_i18n"
     76                        " ,cu.unit_allow_fraction"
     77                        " ,cu.unit_precision_level"
     78                        " ,cu.unit_active"
     79                        " ,FALSE AS unit_builtin"
     80                        " FROM merchant_custom_units cu"
     81                        " WHERE cu.unit=$1");
     82   qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     83                                                  "",
     84                                                  params,
     85                                                  rs);
     86   if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs)
     87     return qs;
     88   TMH_PQ_prepare_anon (pg,
     89                        "SELECT"
     90                        "  bu.unit_serial"
     91                        " ,bu.unit"
     92                        " ,bu.unit_name_long"
     93                        " ,bu.unit_name_short"
     94                        " ,bu.unit_name_long_i18n"
     95                        " ,bu.unit_name_short_i18n"
     96                        " ,COALESCE(bo.override_allow_fraction, bu.unit_allow_fraction)"
     97                        " ,COALESCE(bo.override_precision_level, bu.unit_precision_level)"
     98                        " ,COALESCE(bo.override_active, bu.unit_active)"
     99                        " ,TRUE AS unit_builtin"
    100                        " FROM merchant.merchant_builtin_units bu"
    101                        " LEFT JOIN merchant_builtin_unit_overrides bo"
    102                        "   ON bo.builtin_unit_serial = bu.unit_serial"
    103                        " WHERE bu.unit=$1");
    104   return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
    105                                                    "",
    106                                                    params,
    107                                                    rs);
    108 }