select_unit.c (4837B)
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 // FIXME: combine both SELECT statements into a single statement! 67 TMH_PQ_prepare_anon (pg, 68 "SELECT" 69 " cu.unit_serial" 70 " ,cu.unit" 71 " ,cu.unit_name_long" 72 " ,cu.unit_name_short" 73 " ,cu.unit_name_long_i18n" 74 " ,cu.unit_name_short_i18n" 75 " ,cu.unit_allow_fraction" 76 " ,cu.unit_precision_level" 77 " ,cu.unit_active" 78 " ,FALSE AS unit_builtin" 79 " FROM merchant_custom_units cu" 80 " WHERE cu.unit=$1"); 81 qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 82 "", 83 params, 84 rs); 85 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs) 86 return qs; 87 TMH_PQ_prepare_anon (pg, 88 "SELECT" 89 " bu.unit_serial" 90 " ,bu.unit" 91 " ,bu.unit_name_long" 92 " ,bu.unit_name_short" 93 " ,bu.unit_name_long_i18n" 94 " ,bu.unit_name_short_i18n" 95 " ,COALESCE(bo.override_allow_fraction, bu.unit_allow_fraction)" 96 " AS unit_allow_fraction" 97 " ,COALESCE(bo.override_precision_level, bu.unit_precision_level)" 98 " AS unit_precision_level" 99 " ,COALESCE(bo.override_active, bu.unit_active)" 100 " AS unit_active" 101 " ,TRUE AS unit_builtin" 102 " FROM merchant.merchant_builtin_units bu" 103 " LEFT JOIN merchant_builtin_unit_overrides bo" 104 " ON bo.builtin_unit_serial = bu.unit_serial" 105 " WHERE bu.unit=$1"); 106 return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 107 "", 108 params, 109 rs); 110 }