aboutsummaryrefslogtreecommitdiff
path: root/src/my
diff options
context:
space:
mode:
authorChristophe Genevey Metat <genevey.christophe@gmail.com>2016-05-20 15:29:50 +0000
committerChristophe Genevey Metat <genevey.christophe@gmail.com>2016-05-20 15:29:50 +0000
commitbbd7f90332fa9df514cb14bb24f2187c78db432a (patch)
tree56b40d3e810c61e7aecb325109bf80ed64e8a808 /src/my
parent81224501b6a9483907571508460e41617643050f (diff)
downloadgnunet-bbd7f90332fa9df514cb14bb24f2187c78db432a.tar.gz
gnunet-bbd7f90332fa9df514cb14bb24f2187c78db432a.zip
libgnunetmy query helper
Diffstat (limited to 'src/my')
-rw-r--r--src/my/my.c18
-rw-r--r--src/my/my_query_helper.c283
2 files changed, 288 insertions, 13 deletions
diff --git a/src/my/my.c b/src/my/my.c
index 71d30dd16..89d8c3370 100644
--- a/src/my/my.c
+++ b/src/my/my.c
@@ -109,30 +109,30 @@ GNUNET_MY_extract_result (MYSQL_BIND * result,
109 int row, 109 int row,
110 struct GNUNET_MY_ResultSpec *specs) 110 struct GNUNET_MY_ResultSpec *specs)
111{ 111{
112 unsigned int i ; 112 unsigned int i;
113 int had_null = GNUNET_NO ; 113 int had_null = GNUNET_NO;
114 int ret ; 114 int ret;
115 115
116 for(i = 0 ; NULL != specs[i].conv ; i++) 116 for(i = 0 ; NULL != specs[i].conv ; i++)
117 { 117 {
118 struct GNUNET_MY_ResultSpec * spec ; 118 struct GNUNET_MY_ResultSpec *spec;
119 119
120 spec = &specs[i] ; 120 spec = &specs[i];
121 ret = spec->conv(spec->conv_cls, 121 ret = spec->conv(spec->conv_cls,
122 NULL, //wait GNUNET_MY_QueryParam 122 NULL, //wait GNUNET_MY_QueryParam
123 result); 123 result);
124 124
125 if(ret == GNUNET_SYSERR) 125 if(ret == GNUNET_SYSERR)
126 return GNUNET_SYSERR ; 126 return GNUNET_SYSERR;
127 127
128 if(spec->result_size != NULL) 128 if(spec->result_size != NULL)
129 *spec->result_size = spec->dst_size; 129 *spec->result_size = spec->dst_size;
130 } 130 }
131 131
132 if(GNUNET_YES == had_null) 132 if(GNUNET_YES == had_null)
133 return GNUNET_NO ; 133 return GNUNET_NO;
134 134
135 return GNUNET_OK ; 135 return GNUNET_OK;
136} 136}
137 137
138/* end of my.c */ 138/* end of my.c */ \ No newline at end of file
diff --git a/src/my/my_query_helper.c b/src/my/my_query_helper.c
index 057c32d9f..3b5d90179 100644
--- a/src/my/my_query_helper.c
+++ b/src/my/my_query_helper.c
@@ -36,7 +36,7 @@
36 * @return -1 on error 36 * @return -1 on error
37 */ 37 */
38static int 38static int
39pq_conv_fixed_size (void *cls, 39my_conv_fixed_size (void *cls,
40 const struct GNUNET_MY_QueryParam *qp, 40 const struct GNUNET_MY_QueryParam *qp,
41 MYSQL_BIND *qbind) 41 MYSQL_BIND *qbind)
42{ 42{
@@ -53,14 +53,14 @@ pq_conv_fixed_size (void *cls,
53 * @a ptr_size bytes. 53 * @a ptr_size bytes.
54 * 54 *
55 * @param ptr pointer to the query parameter to pass 55 * @param ptr pointer to the query parameter to pass
56 * @oaran ptr_size number of bytes in @a ptr 56 * @param ptr_size number of bytes in @a ptr
57 */ 57 */
58struct GNUNET_MY_QueryParam 58struct GNUNET_MY_QueryParam
59GNUNET_MY_query_param_fixed_size (const void *ptr, 59GNUNET_MY_query_param_fixed_size (const void *ptr,
60 size_t ptr_size) 60 size_t ptr_size)
61{ 61{
62 struct GNUNET_MY_QueryParam qp = { 62 struct GNUNET_MY_QueryParam qp = {
63 &pq_conv_fixed_size, 63 &my_conv_fixed_size,
64 NULL, 64 NULL,
65 1, 65 1,
66 ptr, 66 ptr,
@@ -70,4 +70,279 @@ GNUNET_MY_query_param_fixed_size (const void *ptr,
70} 70}
71 71
72 72
73/* end of my_query_helper.c */ 73/**
74 * Generate query parameter for a string
75 *
76 *@param ptr pointer to the string query parameter to pass
77 */
78struct GNUNET_MY_QueryParam
79GNUNET_MY_query_param_string (const char *ptr)
80{
81 return GNUNET_MY_query_param_fixed_size(ptr,
82 strlen(ptr));
83}
84
85/**
86 * Function called to convert input argument into SQL parameters
87 *
88 *@param cls closure
89 *@param pq data about the query
90 * @param qbind array of parameters to initialize
91 *@return -1 on error
92 */
93static int
94my_conv_uint16 (void *cls,
95 const struct GNUNET_MY_QueryParam * qp,
96 MYSQL_BIND *qbind)
97{
98 const uint16_t *u_hbo = qp->data;
99 uint16_t *u_nbo;
100
101 GNUNET_assert (1 == qp->num_params);
102
103 u_nbo = GNUNET_new (uint16_t);
104 *u_nbo = htons (*u_hbo);
105 qbind->buffer = (void *) u_nbo;
106 qbind->buffer_length = sizeof(uint16_t);
107 qbind->buffer_type = 1;
108
109 return 1;
110}
111
112/**
113 * Generate query parameter for an uint16_t in host byte order.
114 *
115 * @param x pointer to the query parameter to pass
116 */
117struct GNUNET_MY_QueryParam
118GNUNET_MY_query_param_uint16 (const uint16_t *x)
119{
120 struct GNUNET_MY_QueryParam res = {
121 &my_conv_uint16,
122 NULL,
123 1,
124 x,
125 sizeof (*x)
126 };
127
128 return res;
129}
130
131/**
132 * Function called to convert input argument into SQL parameters
133 *
134 *@param cls closure
135 *@param pq data about the query
136 * @param qbind array of parameters to initialize
137 *@return -1 on error
138 */
139static int
140my_conv_uint32 (void *cls,
141 const struct GNUNET_MY_QueryParam *qp,
142 MYSQL_BIND *qbind)
143{
144 const uint32_t *u_hbo = qp->data;
145 uint32_t * u_nbo;
146
147 GNUNET_assert (1 == qp->num_params);
148
149 u_nbo = GNUNET_new (uint32_t);
150 *u_nbo = htonl (*u_hbo);
151
152 qbind->buffer = (void *) u_nbo;
153 qbind->buffer_length = sizeof(uint32_t);
154 qbind->buffer_type = 1;
155
156 return 1;
157}
158
159/**
160 * Generate query parameter for an uint32_t in host byte order
161 *
162 *@param x pointer to the query parameter to pass
163 */
164struct GNUNET_MY_QueryParam
165GNUNET_MY_query_param_uint32 (const uint32_t *x)
166{
167 struct GNUNET_MY_QueryParam res = {
168 &my_conv_uint32,
169 NULL,
170 1,
171 x,
172 sizeof (*x)
173 };
174
175 return res;
176}
177
178/**
179 * Function called to convert input argument into SQL parameters
180 *
181 *@param cls closure
182 *@param pq data about the query
183 * @param qbind array of parameters to initialize
184 *@return -1 on error
185 */
186static int
187my_conv_uint64 (void *cls,
188 const struct GNUNET_MY_QueryParam *qp,
189 MYSQL_BIND * qbind)
190{
191 const uint64_t * u_hbo = qp->data;
192 uint64_t *u_nbo;
193
194 GNUNET_assert (1 == qp->num_params);
195
196 u_nbo = GNUNET_new(uint64_t);
197 *u_nbo = GNUNET_htonll (*u_hbo);
198
199 qbind->buffer = (void *) u_nbo;
200 qbind->buffer_length = sizeof (uint64_t);
201 qbind->buffer_type = 1;
202
203 return 1;
204}
205
206/**
207 * Generate query parameter for an uint64_t in host byte order
208 *
209 *@param x pointer to the query parameter to pass
210 */
211struct GNUNET_MY_QueryParam
212GNUNET_MY_query_param_uint64 (const uint64_t *x)
213{
214 struct GNUNET_MY_QueryParam res = {
215 &my_conv_uint64,
216 NULL,
217 1,
218 x,
219 sizeof(*x)
220 };
221
222 return res;
223}
224
225/**
226 * Function called to convert input argument into SQL parameters
227 *
228 *@param cls closure
229 *@param pq data about the query
230 * @param qbind array of parameters to initialize
231 *@return -1 on error
232 */
233static int
234my_conv_rsa_public_key (void *cls,
235 const struct GNUNET_MY_QueryParam *qp,
236 MYSQL_BIND * qbind)
237 {
238 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
239 char *buf;
240 size_t buf_size;
241
242 GNUNET_assert(1 == qp->num_params);
243
244 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, &buf);
245
246 qbind->buffer = (void *)buf;
247 qbind->buffer_length = buf_size - 1;
248 qbind->buffer_type = 1;
249
250 return 1;
251 }
252
253 /**
254 * Generate query parameter for an RSA public key. The
255 * database must contain a BLOB type in the respective position.
256 *
257 * @param x the query parameter to pass
258 * @return array entry for the query parameters to use
259 */
260struct GNUNET_MY_QueryParam
261GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
262{
263 struct GNUNET_MY_QueryParam res = {
264 &my_conv_rsa_public_key,
265 NULL,
266 1,
267 x,
268 0
269 };
270
271 return res;
272}
273
274/**
275 * Function called to convert input argument into SQL parameters
276 *
277 *@param cls closure
278 *@param pq data about the query
279 * @param qbind array of parameters to initialize
280 *@return -1 on error
281 */
282static int
283my_conv_rsa_signature (void *cls,
284 const struct GNUNET_MY_QueryParam *qp,
285 MYSQL_BIND * qbind)
286{
287 const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
288 char *buf;
289 size_t buf_size;
290
291 GNUNET_assert(1 == qp->num_params);
292
293 buf_size = GNUNET_CRYPTO_rsa_signature_encode(sig,
294 &buf);
295
296 qbind->buffer = (void *)buf;
297 qbind->buffer_length = buf_size - 1;
298 qbind->buffer_type = 1;
299
300 return 1;
301}
302
303/**
304 * Generate query parameter for an RSA signature. The
305 * database must contain a BLOB type in the respective position
306 *
307 *@param x the query parameter to pass
308 *@return array entry for the query parameters to use
309 */
310struct GNUNET_MY_QueryParam
311GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
312{
313 struct GNUNET_MY_QueryParam res = {
314 &my_conv_rsa_signature,
315 NULL,
316 1,
317 (x),
318 0
319 };
320 return res;
321}
322
323/**
324 * Generate query parameter for an absolute time value.
325 * The database must store a 64-bit integer.
326 *
327 *@param x pointer to the query parameter to pass
328 *@return array entry for the query parameters to use
329 */
330struct GNUNET_MY_QueryParam
331GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
332{
333 return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
334}
335
336/**
337 * Generate query parameter for an absolute time value.
338 * The database must store a 64-bit integer.
339 *
340 *@param x pointer to the query parameter to pass
341 */
342struct GNUNET_MY_QueryParam
343GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
344{
345 return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
346}
347
348/* end of my_query_helper.c */ \ No newline at end of file