diff options
author | Christophe Genevey Metat <genevey.christophe@gmail.com> | 2016-05-20 15:29:50 +0000 |
---|---|---|
committer | Christophe Genevey Metat <genevey.christophe@gmail.com> | 2016-05-20 15:29:50 +0000 |
commit | bbd7f90332fa9df514cb14bb24f2187c78db432a (patch) | |
tree | 56b40d3e810c61e7aecb325109bf80ed64e8a808 /src/my/my_query_helper.c | |
parent | 81224501b6a9483907571508460e41617643050f (diff) | |
download | gnunet-bbd7f90332fa9df514cb14bb24f2187c78db432a.tar.gz gnunet-bbd7f90332fa9df514cb14bb24f2187c78db432a.zip |
libgnunetmy query helper
Diffstat (limited to 'src/my/my_query_helper.c')
-rw-r--r-- | src/my/my_query_helper.c | 283 |
1 files changed, 279 insertions, 4 deletions
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 | */ |
38 | static int | 38 | static int |
39 | pq_conv_fixed_size (void *cls, | 39 | my_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 | */ |
58 | struct GNUNET_MY_QueryParam | 58 | struct GNUNET_MY_QueryParam |
59 | GNUNET_MY_query_param_fixed_size (const void *ptr, | 59 | GNUNET_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 | */ | ||
78 | struct GNUNET_MY_QueryParam | ||
79 | GNUNET_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 | */ | ||
93 | static int | ||
94 | my_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 | */ | ||
117 | struct GNUNET_MY_QueryParam | ||
118 | GNUNET_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 | */ | ||
139 | static int | ||
140 | my_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 | */ | ||
164 | struct GNUNET_MY_QueryParam | ||
165 | GNUNET_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 | */ | ||
186 | static int | ||
187 | my_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 | */ | ||
211 | struct GNUNET_MY_QueryParam | ||
212 | GNUNET_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 | */ | ||
233 | static int | ||
234 | my_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 | */ | ||
260 | struct GNUNET_MY_QueryParam | ||
261 | GNUNET_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 | */ | ||
282 | static int | ||
283 | my_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 | */ | ||
310 | struct GNUNET_MY_QueryParam | ||
311 | GNUNET_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 | */ | ||
330 | struct GNUNET_MY_QueryParam | ||
331 | GNUNET_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 | */ | ||
342 | struct GNUNET_MY_QueryParam | ||
343 | GNUNET_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 | ||