Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2020 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 sq/sq_query_helper.c
18 : * @brief helper functions for Taler-specific SQLite3 interactions
19 : * @author Jonathan Buchanan
20 : */
21 : #include <sqlite3.h>
22 : #include <gnunet/gnunet_util_lib.h>
23 : #include <gnunet/gnunet_sq_lib.h>
24 : #include "taler/taler_sq_lib.h"
25 :
26 :
27 : /**
28 : * Function called to convert input argument into SQL parameters.
29 : *
30 : * @param cls closure
31 : * @param data pointer to input argument, here a `struct TALER_Amount`
32 : * @param data_len number of bytes in @a data (if applicable)
33 : * @param stmt sqlite statement to parameters for
34 : * @param off offset of the argument to bind in @a stmt, numbered from 1,
35 : * so immediately suitable for passing to `sqlite3_bind`-functions.
36 : * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
37 : */
38 : static enum GNUNET_GenericReturnValue
39 2 : qconv_amount (void *cls,
40 : const void *data,
41 : size_t data_len,
42 : sqlite3_stmt *stmt,
43 : unsigned int off)
44 : {
45 2 : const struct TALER_Amount *amount = data;
46 :
47 : (void) cls;
48 2 : GNUNET_assert (sizeof (struct TALER_Amount) == data_len);
49 2 : if (SQLITE_OK != sqlite3_bind_int64 (stmt,
50 : (int) off,
51 2 : (sqlite3_int64) amount->value))
52 0 : return GNUNET_SYSERR;
53 2 : if (SQLITE_OK != sqlite3_bind_int64 (stmt,
54 2 : (int) off + 1,
55 2 : (sqlite3_int64) amount->fraction))
56 0 : return GNUNET_SYSERR;
57 2 : return GNUNET_OK;
58 : }
59 :
60 :
61 : struct GNUNET_SQ_QueryParam
62 1 : TALER_SQ_query_param_amount (const struct TALER_Amount *x)
63 : {
64 1 : struct GNUNET_SQ_QueryParam res = {
65 : .conv = &qconv_amount,
66 : .data = x,
67 : .size = sizeof (*x),
68 : .num_params = 2
69 : };
70 :
71 1 : return res;
72 : }
73 :
74 :
75 : /**
76 : * Function called to convert input argument into SQL parameters.
77 : *
78 : * @param cls closure
79 : * @param data pointer to input argument, here a `struct TALER_AmountNBO`
80 : * @param data_len number of bytes in @a data (if applicable)
81 : * @param stmt sqlite statement to parameters for
82 : * @param off offset of the argument to bind in @a stmt, numbered from 1,
83 : * so immediately suitable for passing to `sqlite3_bind`-functions.
84 : * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
85 : */
86 : static enum GNUNET_GenericReturnValue
87 1 : qconv_amount_nbo (void *cls,
88 : const void *data,
89 : size_t data_len,
90 : sqlite3_stmt *stmt,
91 : unsigned int off)
92 : {
93 1 : const struct TALER_AmountNBO *amount = data;
94 : struct TALER_Amount amount_hbo;
95 :
96 : (void) cls;
97 : (void) data_len;
98 1 : TALER_amount_ntoh (&amount_hbo,
99 : amount);
100 1 : return qconv_amount (cls,
101 : &amount_hbo,
102 : sizeof (struct TALER_Amount),
103 : stmt,
104 : off);
105 : }
106 :
107 :
108 : struct GNUNET_SQ_QueryParam
109 1 : TALER_SQ_query_param_amount_nbo (const struct TALER_AmountNBO *x)
110 : {
111 1 : struct GNUNET_SQ_QueryParam res = {
112 : .conv = &qconv_amount_nbo,
113 : .data = x,
114 : .size = sizeof (*x),
115 : .num_params = 2
116 : };
117 :
118 1 : return res;
119 : }
120 :
121 :
122 : /**
123 : * Function called to convert input argument into SQL parameters.
124 : *
125 : * @param cls closure
126 : * @param data pointer to input argument, here a `struct TALER_Amount`
127 : * @param data_len number of bytes in @a data (if applicable)
128 : * @param stmt sqlite statement to parameters for
129 : * @param off offset of the argument to bind in @a stmt, numbered from 1,
130 : * so immediately suitable for passing to `sqlite3_bind`-functions.
131 : * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
132 : */
133 : static enum GNUNET_GenericReturnValue
134 1 : qconv_json (void *cls,
135 : const void *data,
136 : size_t data_len,
137 : sqlite3_stmt *stmt,
138 : unsigned int off)
139 : {
140 1 : const json_t *json = data;
141 : char *str;
142 :
143 : (void) cls;
144 : (void) data_len;
145 1 : str = json_dumps (json, JSON_COMPACT);
146 1 : if (NULL == str)
147 0 : return GNUNET_SYSERR;
148 :
149 1 : if (SQLITE_OK != sqlite3_bind_text (stmt,
150 : (int) off,
151 : str,
152 1 : strlen (str),
153 : SQLITE_TRANSIENT))
154 0 : return GNUNET_SYSERR;
155 1 : GNUNET_free (str);
156 1 : return GNUNET_OK;
157 : }
158 :
159 :
160 : struct GNUNET_SQ_QueryParam
161 1 : TALER_SQ_query_param_json (const json_t *x)
162 : {
163 1 : struct GNUNET_SQ_QueryParam res = {
164 : .conv = &qconv_json,
165 : .data = x,
166 : .size = sizeof (*x),
167 : .num_params = 1
168 : };
169 :
170 1 : return res;
171 : }
172 :
173 :
174 : /* end of sq/sq_query_helper.c */
|