Line data Source code
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/get_unit.c
18 : * @brief Implementation of the get_unit function for Postgres
19 : * @author Bohdan Potuzhnyi
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_pq_lib.h>
23 : #include "merchant-database/get_unit.h"
24 : #include "helper.h"
25 :
26 :
27 : enum GNUNET_DB_QueryStatus
28 76 : TALER_MERCHANTDB_get_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 76 : struct GNUNET_PQ_QueryParam params[] = {
36 76 : GNUNET_PQ_query_param_string (unit_id),
37 : GNUNET_PQ_query_param_end
38 : };
39 76 : struct GNUNET_PQ_ResultSpec rs[] = {
40 76 : GNUNET_PQ_result_spec_uint64 ("unit_serial",
41 : &ud->unit_serial),
42 76 : GNUNET_PQ_result_spec_string ("unit",
43 : &ud->unit),
44 76 : GNUNET_PQ_result_spec_string ("unit_name_long",
45 : &ud->unit_name_long),
46 76 : GNUNET_PQ_result_spec_string ("unit_name_short",
47 : &ud->unit_name_short),
48 76 : TALER_PQ_result_spec_json ("unit_name_long_i18n",
49 : &ud->unit_name_long_i18n),
50 76 : TALER_PQ_result_spec_json ("unit_name_short_i18n",
51 : &ud->unit_name_short_i18n),
52 76 : GNUNET_PQ_result_spec_bool ("unit_allow_fraction",
53 : &ud->unit_allow_fraction),
54 76 : GNUNET_PQ_result_spec_uint32 ("unit_precision_level",
55 : &ud->unit_precision_level),
56 76 : GNUNET_PQ_result_spec_bool ("unit_active",
57 : &ud->unit_active),
58 76 : GNUNET_PQ_result_spec_bool ("unit_builtin",
59 : &ud->unit_builtin),
60 : GNUNET_PQ_result_spec_end
61 : };
62 :
63 76 : GNUNET_assert (NULL != pg->current_merchant_id);
64 76 : GNUNET_assert (0 == strcmp (instance_id,
65 : pg->current_merchant_id));
66 : // FIXME: combine both SELECT statements into a single statement!
67 76 : 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 76 : qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
82 : "",
83 : params,
84 : rs);
85 76 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs)
86 8 : return qs;
87 68 : 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 68 : return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
107 : "",
108 : params,
109 : rs);
110 : }
|