6931ddcbd891d33496f54296b11f940394bed4da
[oweals/gnunet.git] / src / include / gnunet_my_lib.h
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2012 GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @author Christian Grothoff
22  *
23  * @file
24  * Helper library to access a MySQL database
25  *
26  * @defgroup mysql  MySQL library
27  * Helper library to access a MySQL database.
28  * @{
29  */
30 #ifndef GNUNET_MY_LIB_H
31 #define GNUNET_MY_LIB_H
32
33 #include "gnunet_util_lib.h"
34 #include "gnunet_mysql_lib.h"
35 #include <mysql/mysql.h>
36
37 #ifdef __cplusplus
38 extern "C"
39 {
40 #if 0                           /* keep Emacsens' auto-indent happy */
41 }
42 #endif
43 #endif
44
45
46
47 /**
48  * Information we pass to #GNUNET_MY_exec_prepared() to
49  * initialize the arguments of the prepared statement.
50  */
51 struct GNUNET_MY_QueryParam;
52
53
54 /**
55  * Function called to convert input argument into SQL parameters.
56  *
57  * @param cls closure
58  * @param pq data about the query
59  * @param qbind array of parameters to initialize
60  * @return -1 on error
61  */
62 typedef int
63 (*GNUNET_MY_QueryConverter)(void *cls,
64                             const struct GNUNET_MY_QueryParam *qp,
65                             MYSQL_BIND *qbind);
66
67
68 /**
69  * Information we pass to #GNUNET_MY_exec_prepared() to
70  * initialize the arguments of the prepared statement.
71  */
72 struct GNUNET_MY_QueryParam
73 {
74
75   /**
76    * Function to call for the type conversion.
77    */
78   GNUNET_MY_QueryConverter conv;
79
80   /**
81    * Closure for @e conv.
82    */
83   void *conv_cls;
84
85   /**
86    * Number of arguments the @a conv converter expects to initialize.
87    */
88   unsigned int num_params;
89
90   /**
91    * Information to pass to @e conv.
92    */
93   const void *data;
94
95   /**
96    * Information to pass to @e conv.  Size of @a data.
97    */
98   unsigned long data_len ;
99
100 };
101
102
103 /**
104  * End of query parameter specification.
105  *
106  * @return array last entry for the result specification to use
107  */
108 #define GNUNET_MY_query_param_end { NULL, NULL, 0, NULL, 0 }
109
110
111
112 /**
113  * Generate query parameter for a buffer @a ptr of
114  * @a ptr_size bytes.
115  *
116  * @param ptr pointer to the query parameter to pass
117  * @oaran ptr_size number of bytes in @a ptr
118  */
119 struct GNUNET_MY_QueryParam
120 GNUNET_MY_query_param_fixed_size (const void *ptr,
121                                   size_t ptr_size);
122
123 /**
124  * Run a prepared SELECT statement.
125  *
126  * @param mc mysql context
127  * @param sh handle to SELECT statment
128  * @param params parameters to the statement
129  * @return TBD
130  */
131 int
132 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
133                          struct GNUNET_MYSQL_StatementHandle *sh,
134                          const struct GNUNET_MY_QueryParam *params);
135
136
137 /**
138  * Information we pass to #GNUNET_MY_extract_result() to
139  * initialize the arguments of the prepared statement.
140  */
141 struct GNUNET_MY_ResultParam;
142
143
144 /**
145  * Function called to convert input argument into SQL parameters.
146  *
147  * @param cls closure
148  * @param pq data about the query
149  * @return -1 on error
150  */
151 typedef int
152 (*GNUNET_MY_ResultConverter)(void *cls,
153                              struct GNUNET_MY_QueryParam *qp,
154                              MYSQL_BIND *results);
155
156
157 /**
158  * Information we pass to #GNUNET_MY_extract_result() to
159  * initialize the arguments of the prepared statement.
160  */
161 struct GNUNET_MY_ResultSpec
162 {
163
164   /**
165    * Function to call for the type conversion.
166    */
167   GNUNET_MY_ResultConverter conv;
168
169   /**
170    * Closure for @e conv.
171    */
172   void *conv_cls;
173
174   /**
175    * Destination for the data.
176    */
177   void *dst;
178
179   /**
180    * Allowed size for the data, 0 for variable-size
181    * (in this case, the type of @e dst is a `void **`
182    * and we need to allocate a buffer of the right size).
183    */
184   size_t dst_size;
185
186   /**
187    * Where to store actual size of the result.
188    */
189   size_t *result_size;
190
191 };
192
193
194 /**
195  * End of result speceter specification.
196  *
197  * @return array last entry for the result specification to use
198  */
199 #define GNUNET_MY_result_spec_end { NULL, NULL, NULL, 0, NULL }
200
201
202
203 /**
204  * Obtain fixed size result of @a ptr_size bytes from
205  * MySQL, store in already allocated buffer at @a ptr.
206  *
207  * @spec ptr where to write the result
208  * @oaran ptr_size number of bytes available at @a ptr
209  */
210 struct GNUNET_MY_ResultSpec
211 GNUNET_MY_result_spec_fixed_size (void *ptr,
212                                   size_t ptr_size);
213
214 /**
215   * Generate query parameter for a string
216   *
217   *@param ptr pointer to the string query parameter to pass
218   */
219 struct GNUNET_MY_QueryParam
220 GNUNET_MY_query_param_string (const char *ptr);
221
222 /**
223   * Generate fixed-size query parameter with size determined
224   * by variable type.
225   *
226   * @param x pointer to the query parameter to pass
227   */
228 #define GNUNET_MY_query_param_auto_from_type(x) GNUNET_MY_query_param_fixed_size ((x), sizeof (*(x)))
229
230 /**
231   * Generate query parameter for an RSA public key. The
232   * database must contain a BLOB type in the respective position.
233   *
234   * @param x the query parameter to pass
235   * @return array entry for the query parameters to use
236   */
237 struct GNUNET_MY_QueryParam
238 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x);
239
240 /**
241   * Generate query parameter for an RSA signature. The
242   * database must contain a BLOB type in the respective position
243   *
244   *@param x the query parameter to pass
245   *@return array entry for the query parameters to use
246   */
247 struct GNUNET_MY_QueryParam
248 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x);
249
250 /**
251   * Generate query parameter for an absolute time value.
252   * The database must store a 64-bit integer.
253   *
254   *@param x pointer to the query parameter to pass
255   *@return array entry for the query parameters to use
256   */
257 struct GNUNET_MY_QueryParam
258 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
259
260 /**
261   * Generate query parameter for an absolute time value.
262   * The database must store a 64-bit integer.
263   *
264   *@param x pointer to the query parameter to pass
265   */
266 struct GNUNET_MY_QueryParam
267 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x);
268
269 /**
270   * Generate query parameter for an uint16_t in host byte order.
271   *
272   * @param x pointer to the query parameter to pass
273   */
274 struct GNUNET_MY_QueryParam
275 GNUNET_MY_query_param_uint16 (const uint16_t *x);
276
277 /**
278   * Generate query parameter for an uint32_t in host byte order
279   *
280   *@param x pointer to the query parameter to pass
281   */
282 struct GNUNET_MY_QueryParam
283 GNUNET_MY_query_param_uint32 (const uint32_t *x);
284
285 /**
286   * Generate query parameter for an uint64_t in host byte order
287   *
288   *@param x pointer to the query parameter to pass
289   */
290 struct GNUNET_MY_QueryParam
291 GNUNET_MY_query_param_uint64 (const uint64_t *x);
292
293 /**
294  * We expect a fixed-size result, with size determined by the type of `* dst`
295  *
296  * @spec name name of the field in the table
297  * @spec dst point to where to store the result, type fits expected result size
298  * @return array entry for the result specification to use
299  */
300 #define GNUNET_MY_result_spec_auto_from_type(dst) GNUNET_MY_result_spec_fixed_size ((dst), sizeof (*(dst)))
301
302
303 /**
304  * FIXME.
305  *
306  */
307  
308  /**
309   * Variable-size result expected
310   *
311   * @param[out] dst where to store the result, allocated
312   * @param[out] sptr where to store the size of @a dst
313   * @return array entru for the result specification to use
314   */
315 struct GNUNET_MY_ResultSpec
316 GNUNET_MY_result_spec_variable_size (void **dst,
317                                     size_t *ptr_size);
318 /**
319   * RSA public key expected
320   *
321   * @param name name of the field in the table
322   * @param[out] rsa where to store the result
323   * @return array entry for the result specification to use
324   */
325 struct GNUNET_MY_ResultSpec
326 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa);
327
328 /**
329   * RSA signature expected.
330   *
331   * @param[out] sig where to store the result;
332   * @return array entry for the result specification to use
333   */
334 struct GNUNET_MY_ResultSpec
335 GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig);
336
337 /**
338   * 0- terminated string exprected.
339   *
340   * @param[out] dst where to store the result, allocated
341   * @return array entry for the result specification to use
342   */
343 struct GNUNET_MY_ResultSpec
344 GNUNET_MY_result_spec_string (char **dst);
345
346 /**
347   * Absolute time expected
348   *
349   * @param name name of the field in the table
350   * @param[out] at where to store the result
351   * @return array entry for the result specification to use
352   */
353 struct GNUNET_MY_ResultSpec
354 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at);
355
356 /**
357   * Absolute time in network byte order expected
358   *
359   * @param[out] at where to store the result
360   * @return array entry for the result specification to use
361   */
362 struct GNUNET_MY_ResultSpec
363 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at);
364
365 /**
366   * uint16_t expected
367   *
368   * @param[out] u16 where to store the result
369   * @return array entry for the result specification to use
370   */
371 struct GNUNET_MY_ResultSpec
372 GNUNET_MY_result_spec_uint16 (uint16_t *u16);
373
374 /**
375   * uint32_t expected
376   *
377   * @param[out] u32 where to store the result
378   * @return array entry for the result specification to use
379   */
380 struct GNUNET_MY_ResultSpec
381 GNUNET_MY_result_spec_uint32 (uint32_t *u32);
382
383 /**
384   * uint64_t expected.
385   *
386   * @param[out] u64 where to store the result
387   * @return array entry for the result specification to use
388   */
389 struct GNUNET_MY_ResultSpec
390 GNUNET_MY_result_spec_uint64 (uint64_t *u64);
391
392 int
393 GNUNET_MY_extract_result (MYSQL_BIND * result,
394                           struct GNUNET_MY_QueryParam *qp,
395                           struct GNUNET_MY_ResultSpec *specs,
396                           int row);
397
398 #if 0                           /* keep Emacsens' auto-indent happy */
399 {
400 #endif
401 #ifdef __cplusplus
402 }
403 #endif
404
405 #endif
406
407 /** @} */  /* end of group */