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
42
#include <libdrizzle-2.0/common.h>
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)
57
size_t size= strlen(query);
59
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY, (uint8_t *)query, size, size, ret_ptr);
62
drizzle_result_st *drizzle_query_inc(drizzle_con_st *con,
63
drizzle_result_st *result,
64
const char *query, size_t size,
65
size_t total, drizzle_return_t *ret_ptr)
67
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY, (uint8_t *)query, size, total, ret_ptr);
70
drizzle_query_st *drizzle_query_add(drizzle_st *drizzle,
71
drizzle_query_st *query,
73
drizzle_result_st *result,
74
const char *query_string, size_t size,
75
drizzle_query_options_t,
78
// @note drizzle_query_st handle the null drizzle case
79
query= drizzle_query_create(drizzle, query);
85
drizzle_query_set_con(query, con);
86
drizzle_query_set_result(query, result);
87
drizzle_query_set_string(query, query_string, size);
88
drizzle_query_set_context(query, context);
93
drizzle_query_st *drizzle_query_create(drizzle_st *drizzle, drizzle_query_st *query)
102
query= new (std::nothrow) drizzle_query_st;
108
query->options.is_allocated= true;
113
query->state= DRIZZLE_QUERY_STATE_INIT;
118
query->context= NULL;
119
query->context_free_fn= NULL;
120
query->options.is_allocated= false;
123
query->drizzle= drizzle;
125
if (drizzle->query_list)
127
drizzle->query_list->prev= query;
129
query->next= drizzle->query_list;
130
drizzle->query_list= query;
131
drizzle->query_count++;
132
drizzle->query_new++;
137
void drizzle_query_free(drizzle_query_st *query)
144
if (query->context != NULL && query->context_free_fn != NULL)
146
query->context_free_fn(query, query->context);
149
if (query->drizzle->query_list == query)
151
query->drizzle->query_list= query->next;
156
query->prev->next= query->next;
161
query->next->prev= query->prev;
164
query->drizzle->query_count--;
166
if (query->options.is_allocated)
172
void drizzle_query_free_all(drizzle_st *drizzle)
174
while (drizzle->query_list != NULL)
176
drizzle_query_free(drizzle->query_list);
180
drizzle_con_st *drizzle_query_con(drizzle_query_st *query)
190
void drizzle_query_set_con(drizzle_query_st *query, drizzle_con_st *con)
200
drizzle_result_st *drizzle_query_result(drizzle_query_st *query)
207
return query->result;
210
void drizzle_query_set_result(drizzle_query_st *query,
211
drizzle_result_st *result)
218
query->result= result;
221
char *drizzle_query_string(drizzle_query_st *query, size_t *size)
229
return (char *)(query->string);
232
void drizzle_query_set_string(drizzle_query_st *query, const char *string,
240
query->string= string;
244
int drizzle_query_options(drizzle_query_st *)
249
void drizzle_query_set_options(drizzle_query_st *, int)
253
void drizzle_query_add_options(drizzle_query_st *, int)
257
void drizzle_query_remove_options(drizzle_query_st *, int)
261
void *drizzle_query_context(drizzle_query_st *query)
268
return query->context;
271
void drizzle_query_set_context(drizzle_query_st *query, void *context)
278
query->context= context;
281
void drizzle_query_set_context_free_fn(drizzle_query_st *query,
282
drizzle_query_context_free_fn *function)
289
query->context_free_fn= function;
292
static void drizzle_query_run_state(drizzle_query_st* query,
293
drizzle_return_t* ret_ptr)
300
switch (query->state)
302
case DRIZZLE_QUERY_STATE_INIT:
303
query->state= DRIZZLE_QUERY_STATE_QUERY;
305
case DRIZZLE_QUERY_STATE_QUERY:
306
query->result= drizzle_query(query->con, query->result, query->string,
307
query->size, ret_ptr);
308
if (*ret_ptr == DRIZZLE_RETURN_IO_WAIT)
312
else if (*ret_ptr != DRIZZLE_RETURN_OK)
314
query->state= DRIZZLE_QUERY_STATE_DONE;
318
query->state= DRIZZLE_QUERY_STATE_RESULT;
320
case DRIZZLE_QUERY_STATE_RESULT:
321
*ret_ptr= drizzle_result_buffer(query->result);
322
if (*ret_ptr == DRIZZLE_RETURN_IO_WAIT)
327
query->state= DRIZZLE_QUERY_STATE_DONE;
331
case DRIZZLE_QUERY_STATE_DONE:
336
drizzle_query_st *drizzle_query_run(drizzle_st *drizzle,
337
drizzle_return_t *ret_ptr)
339
drizzle_return_t unused;
347
*ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
351
if (drizzle->query_new == 0 && drizzle->query_running == 0)
353
*ret_ptr= DRIZZLE_RETURN_OK;
357
drizzle_st::options_t options= drizzle->options;
358
drizzle->options.is_non_blocking= false;
360
/* Check to see if any queries need to be started. */
361
if (drizzle->query_new > 0)
363
for (drizzle_query_st *query= drizzle->query_list; query != NULL; query= query->next)
365
if (query->state != DRIZZLE_QUERY_STATE_INIT)
370
drizzle->query_new--;
371
drizzle->query_running++;
372
assert(query->con->query == NULL);
373
query->con->query= query;
375
drizzle_query_run_state(query, ret_ptr);
376
if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
378
assert(query->state == DRIZZLE_QUERY_STATE_DONE);
379
drizzle->query_running--;
380
drizzle->options= options;
381
query->con->query= NULL;
382
if (*ret_ptr == DRIZZLE_RETURN_ERROR_CODE || *ret_ptr == DRIZZLE_RETURN_OK)
389
assert(drizzle->query_new == 0);
396
/* Loop through each active connection. */
397
while ((con= drizzle_con_ready(drizzle)) != NULL)
399
drizzle_query_st *query= con->query;
400
drizzle_query_run_state(query, ret_ptr);
401
if (query->state == DRIZZLE_QUERY_STATE_DONE)
403
drizzle->query_running--;
404
drizzle->options= options;
408
assert(*ret_ptr == DRIZZLE_RETURN_IO_WAIT);
411
if (options.is_non_blocking)
413
*ret_ptr= DRIZZLE_RETURN_IO_WAIT;
417
*ret_ptr= drizzle_con_wait(drizzle);
418
if (*ret_ptr != DRIZZLE_RETURN_OK)
420
drizzle->options= options;
426
drizzle_return_t drizzle_query_run_all(drizzle_st *drizzle)
430
return DRIZZLE_RETURN_INVALID_ARGUMENT;
433
while (drizzle->query_new > 0 || drizzle->query_running > 0)
435
drizzle_return_t ret;
437
(void)drizzle_query_run(drizzle, &ret);
438
if (ret != DRIZZLE_RETURN_OK && ret != DRIZZLE_RETURN_ERROR_CODE)
444
return DRIZZLE_RETURN_OK;
447
ssize_t drizzle_escape_string(char *to, size_t max_to_size, const char *from, size_t from_size)
453
for (end= from + from_size; from < end; from++)
456
/* All multi-byte UTF8 characters have the high bit set for all bytes. */
488
if ((size_t)to_size + 2 > max_to_size)
499
if ((size_t)to_size + 1 > max_to_size)
514
size_t drizzle_hex_string(char *to, const char *from, size_t from_size)
516
static const char hex_map[]= "0123456789ABCDEF";
517
const char *from_end;
519
for (from_end= from + from_size; from != from_end; from++)
521
*to++= hex_map[((unsigned char) *from) >> 4];
522
*to++= hex_map[((unsigned char) *from) & 0xF];
527
return from_size * 2;
530
void drizzle_mysql_password_hash(char *to, const char *from, size_t from_size)
533
uint8_t hash_tmp1[SHA1_DIGEST_LENGTH];
534
uint8_t hash_tmp2[SHA1_DIGEST_LENGTH];
537
SHA1Update(&ctx, (const uint8_t*)from, from_size);
538
SHA1Final(hash_tmp1, &ctx);
541
SHA1Update(&ctx, hash_tmp1, SHA1_DIGEST_LENGTH);
542
SHA1Final(hash_tmp2, &ctx);
544
(void)drizzle_hex_string(to, (char*)hash_tmp2, SHA1_DIGEST_LENGTH);