select_unit.c (5147B)
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_error_codes.h> 23 #include <taler/taler_dbevents.h> 24 #include <taler/taler_pq_lib.h> 25 #include "merchant-database/select_unit.h" 26 #include "helper.h" 27 28 29 enum GNUNET_DB_QueryStatus 30 TALER_MERCHANTDB_select_unit (struct TALER_MERCHANTDB_PostgresContext *pg, 31 const char *instance_id, 32 const char *unit_id, 33 struct TALER_MERCHANTDB_UnitDetails *ud) 34 { 35 enum GNUNET_DB_QueryStatus qs; 36 struct GNUNET_PQ_QueryParam params[] = { 37 GNUNET_PQ_query_param_string (instance_id), 38 GNUNET_PQ_query_param_string (unit_id), 39 GNUNET_PQ_query_param_end 40 }; 41 42 if (NULL == ud) 43 { 44 struct GNUNET_PQ_ResultSpec rs_null[] = { 45 GNUNET_PQ_result_spec_end 46 }; 47 48 check_connection (pg); 49 return GNUNET_PQ_eval_prepared_singleton_select ( 50 pg->conn, 51 "select_unit", 52 params, 53 rs_null); 54 } 55 else 56 { 57 struct GNUNET_PQ_ResultSpec rs[] = { 58 GNUNET_PQ_result_spec_uint64 ("unit_serial", 59 &ud->unit_serial), 60 GNUNET_PQ_result_spec_string ("unit", 61 &ud->unit), 62 GNUNET_PQ_result_spec_string ("unit_name_long", 63 &ud->unit_name_long), 64 GNUNET_PQ_result_spec_string ("unit_name_short", 65 &ud->unit_name_short), 66 TALER_PQ_result_spec_json ("unit_name_long_i18n", 67 &ud->unit_name_long_i18n), 68 TALER_PQ_result_spec_json ("unit_name_short_i18n", 69 &ud->unit_name_short_i18n), 70 GNUNET_PQ_result_spec_bool ("unit_allow_fraction", 71 &ud->unit_allow_fraction), 72 GNUNET_PQ_result_spec_uint32 ("unit_precision_level", 73 &ud->unit_precision_level), 74 GNUNET_PQ_result_spec_bool ("unit_active", 75 &ud->unit_active), 76 GNUNET_PQ_result_spec_bool ("unit_builtin", 77 &ud->unit_builtin), 78 GNUNET_PQ_result_spec_end 79 }; 80 81 check_connection (pg); 82 83 PREPARE (pg, 84 "select_unit_custom", 85 "SELECT" 86 " cu.unit_serial" 87 " ,cu.unit" 88 " ,cu.unit_name_long" 89 " ,cu.unit_name_short" 90 " ,cu.unit_name_long_i18n" 91 " ,cu.unit_name_short_i18n" 92 " ,cu.unit_allow_fraction" 93 " ,cu.unit_precision_level" 94 " ,cu.unit_active" 95 " ,FALSE AS unit_builtin" 96 " FROM merchant_custom_units cu" 97 " JOIN merchant_instances inst" 98 " USING (merchant_serial)" 99 " WHERE inst.merchant_id=$1" 100 " AND cu.unit=$2"); 101 qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 102 "select_unit_custom", 103 params, 104 rs); 105 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs) 106 return qs; 107 108 PREPARE (pg, 109 "select_unit_builtin", 110 "SELECT" 111 " bu.unit_serial" 112 " ,bu.unit" 113 " ,bu.unit_name_long" 114 " ,bu.unit_name_short" 115 " ,bu.unit_name_long_i18n" 116 " ,bu.unit_name_short_i18n" 117 " ,COALESCE(bo.override_allow_fraction, bu.unit_allow_fraction)" 118 " ,COALESCE(bo.override_precision_level, bu.unit_precision_level)" 119 " ,COALESCE(bo.override_active, bu.unit_active)" 120 " ,TRUE AS unit_builtin" 121 " FROM merchant_builtin_units bu" 122 " JOIN merchant_instances inst" 123 " ON TRUE" 124 " LEFT JOIN merchant_builtin_unit_overrides bo" 125 " ON bo.builtin_unit_serial = bu.unit_serial" 126 " AND bo.merchant_serial = inst.merchant_serial" 127 " WHERE inst.merchant_id=$1" 128 " AND bu.unit=$2"); 129 return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 130 "select_unit_builtin", 131 params, 132 rs); 133 } 134 }