merchant

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

pg_lookup_template.c (3893B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022, 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_template.c
     18  * @brief Implementation of the lookup_template function for Postgres
     19  * @author Christian Grothoff
     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_template.h"
     26 #include "pg_helper.h"
     27 
     28 
     29 /**
     30  * Lookup details about a particular template.
     31  *
     32  * @param cls closure
     33  * @param instance_id instance to lookup template for
     34  * @param template_id template to lookup
     35  * @param[out] td set to the template details on success, can be NULL
     36  *             (in that case we only want to check if the template exists)
     37  * @return database result code
     38  */
     39 enum GNUNET_DB_QueryStatus
     40 TMH_PG_lookup_template (void *cls,
     41                         const char *instance_id,
     42                         const char *template_id,
     43                         struct TALER_MERCHANTDB_TemplateDetails *td)
     44 {
     45   struct PostgresClosure *pg = cls;
     46   struct GNUNET_PQ_QueryParam params[] = {
     47     GNUNET_PQ_query_param_string (instance_id),
     48     GNUNET_PQ_query_param_string (template_id),
     49     GNUNET_PQ_query_param_end
     50   };
     51   enum GNUNET_DB_QueryStatus qs;
     52 
     53   check_connection (pg);
     54   PREPARE (pg,
     55            "lookup_template",
     56            "SELECT"
     57            " mt.template_description"
     58            ",mod.otp_id"
     59            ",mt.template_contract::TEXT"
     60            ",mt.editable_defaults::TEXT"
     61            " FROM merchant_template mt"
     62            " JOIN merchant_instances mi"
     63            "   ON (mi.merchant_serial = mt.merchant_serial)"
     64            " LEFT JOIN merchant_otp_devices mod"
     65            "   ON (mod.otp_serial = mt.otp_device_id)"
     66            " WHERE mi.merchant_id=$1"
     67            "   AND mt.template_id=$2");
     68   if (NULL == td)
     69   {
     70     struct GNUNET_PQ_ResultSpec rs_null[] = {
     71       GNUNET_PQ_result_spec_end
     72     };
     73 
     74     qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     75                                                    "lookup_template",
     76                                                    params,
     77                                                    rs_null);
     78     GNUNET_PQ_cleanup_query_params_closures (params);
     79     return qs;
     80   }
     81   else
     82   {
     83     struct GNUNET_PQ_ResultSpec rs[] = {
     84       GNUNET_PQ_result_spec_string ("template_description",
     85                                     &td->template_description),
     86       GNUNET_PQ_result_spec_allow_null (
     87         GNUNET_PQ_result_spec_string ("otp_id",
     88                                       &td->otp_id),
     89         NULL),
     90       TALER_PQ_result_spec_json ("template_contract",
     91                                  &td->template_contract),
     92       GNUNET_PQ_result_spec_allow_null (
     93         TALER_PQ_result_spec_json ("editable_defaults",
     94                                    &td->editable_defaults),
     95         NULL),
     96       GNUNET_PQ_result_spec_end
     97     };
     98 
     99     memset (td,
    100             0,
    101             sizeof (*td));
    102     qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
    103                                                    "lookup_template",
    104                                                    params,
    105                                                    rs);
    106     GNUNET_PQ_cleanup_query_params_closures (params);
    107     return qs;
    108   }
    109 }