1
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
* Drizzle Client & Protocol Library
5
* Copyright (C) 2008 Eric Day (eday@oddments.org)
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions are
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* * Redistributions in binary form must reproduce the above
16
* copyright notice, this list of conditions and the following disclaimer
17
* in the documentation and/or other materials provided with the
20
* * The names of its contributors may not be used to endorse or
21
* promote products derived from this software without specific prior
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
* @brief Query definitions
43
#include <libdrizzle/common.h>
45
drizzle_result_st *drizzle_query(drizzle_con_st *con, drizzle_result_st *result,
46
const char *query, size_t size,
47
drizzle_return_t *ret_ptr)
49
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY,
50
(uint8_t *)query, size, size, ret_ptr);
53
drizzle_result_st *drizzle_query_str(drizzle_con_st *con,
54
drizzle_result_st *result,
56
drizzle_return_t *ret_ptr)
63
size_t size= strlen(query);
65
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY,
66
(uint8_t *)query, size, size, ret_ptr);
69
drizzle_result_st *drizzle_query_inc(drizzle_con_st *con,
70
drizzle_result_st *result,
71
const char *query, size_t size,
72
size_t total, drizzle_return_t *ret_ptr)
74
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY,
75
(uint8_t *)query, size, total, ret_ptr);
78
drizzle_query_st *drizzle_query_add(drizzle_st *drizzle,
79
drizzle_query_st *query,
81
drizzle_result_st *result,
82
const char *query_string, size_t size,
83
drizzle_query_options_t options,
91
query= drizzle_query_create(drizzle, query);
97
drizzle_query_set_con(query, con);
98
drizzle_query_set_result(query, result);
99
drizzle_query_set_string(query, query_string, size);
100
drizzle_query_add_options(query, options);
101
drizzle_query_set_context(query, context);
106
drizzle_query_st *drizzle_query_create(drizzle_st *drizzle,
107
drizzle_query_st *query)
116
query= malloc(sizeof(drizzle_query_st));
119
drizzle_set_error(drizzle, __func__, "Failed to allocate.");
123
query->options|= DRIZZLE_CON_ALLOCATED;
127
memset(query, 0, sizeof(drizzle_query_st));
130
query->drizzle= drizzle;
132
if (drizzle->query_list)
133
drizzle->query_list->prev= query;
134
query->next= drizzle->query_list;
135
drizzle->query_list= query;
136
drizzle->query_count++;
137
drizzle->query_new++;
142
void drizzle_query_free(drizzle_query_st *query)
149
if (query->context != NULL && query->context_free_fn != NULL)
150
query->context_free_fn(query, query->context);
152
if (query->drizzle->query_list == query)
153
query->drizzle->query_list= query->next;
156
query->prev->next= query->next;
159
query->next->prev= query->prev;
160
query->drizzle->query_count--;
162
if (query->options & DRIZZLE_QUERY_ALLOCATED)
168
void drizzle_query_free_all(drizzle_st *drizzle)
175
while (drizzle->query_list != NULL)
177
drizzle_query_free(drizzle->query_list);
181
drizzle_con_st *drizzle_query_con(drizzle_query_st *query)
191
void drizzle_query_set_con(drizzle_query_st *query, drizzle_con_st *con)
201
drizzle_result_st *drizzle_query_result(drizzle_query_st *query)
208
return query->result;
211
void drizzle_query_set_result(drizzle_query_st *query,
212
drizzle_result_st *result)
219
query->result= result;
222
char *drizzle_query_string(drizzle_query_st *query, size_t *size)
234
return (char *)(query->string);
237
void drizzle_query_set_string(drizzle_query_st *query, const char *string,
245
query->string= string;
249
drizzle_query_options_t drizzle_query_options(drizzle_query_st *query)
256
return query->options;
259
void drizzle_query_set_options(drizzle_query_st *query,
260
drizzle_query_options_t options)
267
query->options= options;
270
void drizzle_query_add_options(drizzle_query_st *query,
271
drizzle_query_options_t options)
278
query->options|= options;
281
void drizzle_query_remove_options(drizzle_query_st *query,
282
drizzle_query_options_t options)
289
query->options&= ~options;
292
void *drizzle_query_context(drizzle_query_st *query)
299
return query->context;
302
void drizzle_query_set_context(drizzle_query_st *query, void *context)
309
query->context= context;
312
void drizzle_query_set_context_free_fn(drizzle_query_st *query,
313
drizzle_query_context_free_fn *function)
320
query->context_free_fn= function;
323
static void drizzle_query_run_state(drizzle_query_st* query,
324
drizzle_return_t* ret_ptr)
326
drizzle_return_t unused_ret;
329
ret_ptr= &unused_ret;
334
*ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
338
switch (query->state)
340
case DRIZZLE_QUERY_STATE_INIT:
341
query->state= DRIZZLE_QUERY_STATE_QUERY;
342
case DRIZZLE_QUERY_STATE_QUERY:
343
query->result= drizzle_query(query->con, query->result, query->string,
344
query->size, ret_ptr);
345
if (*ret_ptr == DRIZZLE_RETURN_IO_WAIT)
349
else if (*ret_ptr != DRIZZLE_RETURN_OK)
351
query->state= DRIZZLE_QUERY_STATE_DONE;
355
query->state= DRIZZLE_QUERY_STATE_RESULT;
357
case DRIZZLE_QUERY_STATE_RESULT:
358
*ret_ptr= drizzle_result_buffer(query->result);
359
if (*ret_ptr == DRIZZLE_RETURN_IO_WAIT)
364
query->state= DRIZZLE_QUERY_STATE_DONE;
368
case DRIZZLE_QUERY_STATE_DONE:
373
drizzle_query_st *drizzle_query_run(drizzle_st *drizzle,
374
drizzle_return_t *ret_ptr)
376
drizzle_return_t unused_ret;
379
ret_ptr= &unused_ret;
384
*ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
389
drizzle_query_st *query;
392
if (drizzle->query_new == 0 && drizzle->query_running == 0)
394
*ret_ptr= DRIZZLE_RETURN_OK;
398
options= drizzle->options;
399
drizzle->options|= DRIZZLE_NON_BLOCKING;
401
/* Check to see if any queries need to be started. */
402
if (drizzle->query_new > 0)
404
for (query= drizzle->query_list; query != NULL; query= query->next)
406
if (query->state != DRIZZLE_QUERY_STATE_INIT)
409
drizzle->query_new--;
410
drizzle->query_running++;
411
assert(query->con->query == NULL);
412
query->con->query= query;
414
drizzle_query_run_state(query, ret_ptr);
415
if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
417
assert(query->state == DRIZZLE_QUERY_STATE_DONE);
418
drizzle->query_running--;
419
drizzle->options= options;
420
query->con->query= NULL;
421
if (*ret_ptr == DRIZZLE_RETURN_ERROR_CODE || *ret_ptr == DRIZZLE_RETURN_OK)
428
assert(drizzle->query_new == 0);
433
/* Loop through each active connection. */
434
while ((con= drizzle_con_ready(drizzle)) != NULL)
437
drizzle_query_run_state(query, ret_ptr);
438
if (query->state == DRIZZLE_QUERY_STATE_DONE)
440
drizzle->query_running--;
441
drizzle->options= options;
445
assert(*ret_ptr == DRIZZLE_RETURN_IO_WAIT);
448
if (options & DRIZZLE_NON_BLOCKING)
450
*ret_ptr= DRIZZLE_RETURN_IO_WAIT;
454
*ret_ptr= drizzle_con_wait(drizzle);
455
if (*ret_ptr != DRIZZLE_RETURN_OK)
457
drizzle->options= options;
463
drizzle_return_t drizzle_query_run_all(drizzle_st *drizzle)
467
return DRIZZLE_RETURN_INVALID_ARGUMENT;
470
while (drizzle->query_new > 0 || drizzle->query_running > 0)
472
drizzle_return_t ret;
474
(void)drizzle_query_run(drizzle, &ret);
475
if (ret != DRIZZLE_RETURN_OK && ret != DRIZZLE_RETURN_ERROR_CODE)
479
return DRIZZLE_RETURN_OK;
482
ssize_t drizzle_safe_escape_string(char *to, const size_t max_to_size, const char *from, const size_t from_size)
486
if (to == NULL || max_to_size == 0 || from == NULL || from_size == 0)
494
for (end= from +from_size; from < end; from++)
497
/* All multi-byte UTF8 characters have the high bit set for all bytes. */
529
if ((size_t)to_size + 2 > max_to_size)
538
if ((size_t)to_size + 1 > max_to_size)
551
size_t drizzle_escape_string(char *to, const char *from, const size_t from_size)
553
return (size_t) drizzle_safe_escape_string(to, (from_size * 2), from, from_size);
556
size_t drizzle_hex_string(char *to, const char *from, const size_t from_size)
558
if (to == NULL || from == NULL || from_size == 0)
563
static const char hex_map[]= "0123456789ABCDEF";
564
const char *from_end;
566
for (from_end= from + from_size; from != from_end; from++)
568
*to++= hex_map[((unsigned char) *from) >> 4];
569
*to++= hex_map[((unsigned char) *from) & 0xF];
574
return from_size * 2;
577
void drizzle_mysql_password_hash(char *to, const char *from, const size_t from_size)
580
uint8_t hash_tmp1[SHA1_DIGEST_LENGTH];
581
uint8_t hash_tmp2[SHA1_DIGEST_LENGTH];
584
SHA1Update(&ctx, (const uint8_t*)from, from_size);
585
SHA1Final(hash_tmp1, &ctx);
588
SHA1Update(&ctx, hash_tmp1, SHA1_DIGEST_LENGTH);
589
SHA1Final(hash_tmp2, &ctx);
591
(void)drizzle_hex_string(to, (char*)hash_tmp2, SHA1_DIGEST_LENGTH);