2
* Drizzle Client & Protocol Library
4
* Copyright (C) 2008 Eric Day (eday@oddments.org)
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are
11
* * Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
14
* * Redistributions in binary form must reproduce the above
15
* copyright notice, this list of conditions and the following disclaimer
16
* in the documentation and/or other materials provided with the
19
* * The names of its contributors may not be used to endorse or
20
* promote products derived from this software without specific prior
23
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
* @brief Query definitions
44
drizzle_result_st *drizzle_query(drizzle_con_st *con, drizzle_result_st *result,
45
const char *query, size_t size,
46
drizzle_return_t *ret_ptr)
48
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY,
49
(uint8_t *)query, size, size, ret_ptr);
52
drizzle_result_st *drizzle_query_str(drizzle_con_st *con,
53
drizzle_result_st *result,
55
drizzle_return_t *ret_ptr)
61
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY,
62
(uint8_t *)query, size, size, ret_ptr);
65
drizzle_result_st *drizzle_query_inc(drizzle_con_st *con,
66
drizzle_result_st *result,
67
const char *query, size_t size,
68
size_t total, drizzle_return_t *ret_ptr)
70
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY,
71
(uint8_t *)query, size, total, ret_ptr);
74
drizzle_query_st *drizzle_query_add(drizzle_st *drizzle,
75
drizzle_query_st *query,
77
drizzle_result_st *result,
78
const char *query_string, size_t size,
79
drizzle_query_options_t options,
82
query= drizzle_query_create(drizzle, query);
86
drizzle_query_set_con(query, con);
87
drizzle_query_set_result(query, result);
88
drizzle_query_set_string(query, query_string, size);
89
drizzle_query_add_options(query, options);
90
drizzle_query_set_context(query, context);
95
drizzle_query_st *drizzle_query_create(drizzle_st *drizzle,
96
drizzle_query_st *query)
100
query= malloc(sizeof(drizzle_query_st));
103
drizzle_set_error(drizzle, "drizzle_query_create", "malloc");
107
memset(query, 0, sizeof(drizzle_query_st));
108
query->options|= DRIZZLE_CON_ALLOCATED;
111
memset(query, 0, sizeof(drizzle_query_st));
113
query->drizzle= drizzle;
115
if (drizzle->query_list)
116
drizzle->query_list->prev= query;
117
query->next= drizzle->query_list;
118
drizzle->query_list= query;
119
drizzle->query_count++;
120
drizzle->query_new++;
125
void drizzle_query_free(drizzle_query_st *query)
127
if (query->context != NULL && query->context_free_fn != NULL)
128
query->context_free_fn(query, query->context);
130
if (query->drizzle->query_list == query)
131
query->drizzle->query_list= query->next;
133
query->prev->next= query->next;
135
query->next->prev= query->prev;
136
query->drizzle->query_count--;
138
if (query->options & DRIZZLE_QUERY_ALLOCATED)
142
void drizzle_query_free_all(drizzle_st *drizzle)
144
while (drizzle->query_list != NULL)
145
drizzle_query_free(drizzle->query_list);
148
drizzle_con_st *drizzle_query_con(drizzle_query_st *query)
153
void drizzle_query_set_con(drizzle_query_st *query, drizzle_con_st *con)
158
drizzle_result_st *drizzle_query_result(drizzle_query_st *query)
160
return query->result;
163
void drizzle_query_set_result(drizzle_query_st *query,
164
drizzle_result_st *result)
166
query->result= result;
169
char *drizzle_query_string(drizzle_query_st *query, size_t *size)
172
return (char *)(query->string);
175
void drizzle_query_set_string(drizzle_query_st *query, const char *string,
178
query->string= string;
182
drizzle_query_options_t drizzle_query_options(drizzle_query_st *query)
184
return query->options;
187
void drizzle_query_set_options(drizzle_query_st *query,
188
drizzle_query_options_t options)
190
query->options= options;
193
void drizzle_query_add_options(drizzle_query_st *query,
194
drizzle_query_options_t options)
196
query->options|= options;
199
void drizzle_query_remove_options(drizzle_query_st *query,
200
drizzle_query_options_t options)
202
query->options&= ~options;
205
void *drizzle_query_context(drizzle_query_st *query)
207
return query->context;
210
void drizzle_query_set_context(drizzle_query_st *query, void *context)
212
query->context= context;
215
void drizzle_query_set_context_free_fn(drizzle_query_st *query,
216
drizzle_query_context_free_fn *function)
218
query->context_free_fn= function;
221
static void drizzle_query_run_state(drizzle_query_st* query,
222
drizzle_return_t* ret_ptr)
224
switch (query->state)
226
case DRIZZLE_QUERY_STATE_INIT:
227
query->state= DRIZZLE_QUERY_STATE_QUERY;
228
case DRIZZLE_QUERY_STATE_QUERY:
229
query->result= drizzle_query(query->con, query->result, query->string,
230
query->size, ret_ptr);
231
if (*ret_ptr == DRIZZLE_RETURN_IO_WAIT)
235
else if (*ret_ptr != DRIZZLE_RETURN_OK)
237
query->state= DRIZZLE_QUERY_STATE_DONE;
241
query->state= DRIZZLE_QUERY_STATE_RESULT;
243
case DRIZZLE_QUERY_STATE_RESULT:
244
*ret_ptr= drizzle_result_buffer(query->result);
245
if (*ret_ptr == DRIZZLE_RETURN_IO_WAIT)
250
query->state= DRIZZLE_QUERY_STATE_DONE;
254
case DRIZZLE_QUERY_STATE_DONE:
259
drizzle_query_st *drizzle_query_run(drizzle_st *drizzle,
260
drizzle_return_t *ret_ptr)
262
drizzle_options_t options;
263
drizzle_query_st *query;
266
if (drizzle->query_new == 0 && drizzle->query_running == 0)
268
*ret_ptr= DRIZZLE_RETURN_OK;
272
options= drizzle->options;
273
drizzle->options|= DRIZZLE_NON_BLOCKING;
275
/* Check to see if any queries need to be started. */
276
if (drizzle->query_new > 0)
278
for (query= drizzle->query_list; query != NULL; query= query->next)
280
if (query->state != DRIZZLE_QUERY_STATE_INIT)
283
drizzle->query_new--;
284
drizzle->query_running++;
285
assert(query->con->query == NULL);
286
query->con->query= query;
288
drizzle_query_run_state(query, ret_ptr);
289
if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
291
assert(query->state == DRIZZLE_QUERY_STATE_DONE);
292
drizzle->query_running--;
293
drizzle->options= options;
294
query->con->query= NULL;
295
if (*ret_ptr == DRIZZLE_RETURN_ERROR_CODE || *ret_ptr == DRIZZLE_RETURN_OK)
302
assert(drizzle->query_new == 0);
307
/* Loop through each active connection. */
308
while ((con= drizzle_con_ready(drizzle)) != NULL)
311
drizzle_query_run_state(query, ret_ptr);
312
if (query->state == DRIZZLE_QUERY_STATE_DONE)
314
drizzle->query_running--;
315
drizzle->options= options;
319
assert(*ret_ptr == DRIZZLE_RETURN_IO_WAIT);
322
if (options & DRIZZLE_NON_BLOCKING)
324
*ret_ptr= DRIZZLE_RETURN_IO_WAIT;
328
*ret_ptr= drizzle_con_wait(drizzle);
329
if (*ret_ptr != DRIZZLE_RETURN_OK)
331
drizzle->options= options;
337
drizzle_return_t drizzle_query_run_all(drizzle_st *drizzle)
339
drizzle_return_t ret;
341
while (drizzle->query_new > 0 || drizzle->query_running > 0)
343
(void)drizzle_query_run(drizzle, &ret);
344
if (ret != DRIZZLE_RETURN_OK && ret != DRIZZLE_RETURN_ERROR_CODE)
348
return DRIZZLE_RETURN_OK;
351
size_t drizzle_escape_string(char *to, const char *from, size_t from_size)
356
while (from_size > 0)
359
/* All multi-byte UTF8 characters have the high bit set for all bytes. */
402
size_t drizzle_hex_string(char *to, const char *from, size_t from_size)
404
static const char hex_map[]= "0123456789ABCDEF";
405
const char *from_end;
407
for (from_end= from + from_size; from != from_end; from++)
409
*to++= hex_map[((unsigned char) *from) >> 4];
410
*to++= hex_map[((unsigned char) *from) & 0xF];
415
return from_size * 2;
418
void drizzle_mysql_password_hash(char *to, const char *from, size_t from_size)
421
uint8_t hash_tmp1[SHA1_DIGEST_LENGTH];
422
uint8_t hash_tmp2[SHA1_DIGEST_LENGTH];
425
SHA1Update(&ctx, (const uint8_t*)from, from_size);
426
SHA1Final(hash_tmp1, &ctx);
429
SHA1Update(&ctx, hash_tmp1, SHA1_DIGEST_LENGTH);
430
SHA1Final(hash_tmp2, &ctx);
432
(void)drizzle_hex_string(to, (char*)hash_tmp2, SHA1_DIGEST_LENGTH);