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= new drizzle_query_st;
103
drizzle_set_error(drizzle, "drizzle_query_create", "malloc");
107
query->options|= DRIZZLE_CON_ALLOCATED;
113
query->state= DRIZZLE_QUERY_STATE_INIT;
118
query->context= NULL;
119
query->context_free_fn= NULL;
122
query->drizzle= drizzle;
124
if (drizzle->query_list)
125
drizzle->query_list->prev= query;
126
query->next= drizzle->query_list;
127
drizzle->query_list= query;
128
drizzle->query_count++;
129
drizzle->query_new++;
134
void drizzle_query_free(drizzle_query_st *query)
136
if (query->context != NULL && query->context_free_fn != NULL)
137
query->context_free_fn(query, query->context);
139
if (query->drizzle->query_list == query)
140
query->drizzle->query_list= query->next;
142
query->prev->next= query->next;
144
query->next->prev= query->prev;
145
query->drizzle->query_count--;
147
if (query->options & DRIZZLE_QUERY_ALLOCATED)
151
void drizzle_query_free_all(drizzle_st *drizzle)
153
while (drizzle->query_list != NULL)
154
drizzle_query_free(drizzle->query_list);
157
drizzle_con_st *drizzle_query_con(drizzle_query_st *query)
162
void drizzle_query_set_con(drizzle_query_st *query, drizzle_con_st *con)
167
drizzle_result_st *drizzle_query_result(drizzle_query_st *query)
169
return query->result;
172
void drizzle_query_set_result(drizzle_query_st *query,
173
drizzle_result_st *result)
175
query->result= result;
178
char *drizzle_query_string(drizzle_query_st *query, size_t *size)
181
return (char *)(query->string);
184
void drizzle_query_set_string(drizzle_query_st *query, const char *string,
187
query->string= string;
191
int drizzle_query_options(drizzle_query_st *query)
193
return query->options;
196
void drizzle_query_set_options(drizzle_query_st *query,
199
query->options= options;
202
void drizzle_query_add_options(drizzle_query_st *query,
205
query->options|= options;
208
void drizzle_query_remove_options(drizzle_query_st *query,
211
query->options&= ~options;
214
void *drizzle_query_context(drizzle_query_st *query)
216
return query->context;
219
void drizzle_query_set_context(drizzle_query_st *query, void *context)
221
query->context= context;
224
void drizzle_query_set_context_free_fn(drizzle_query_st *query,
225
drizzle_query_context_free_fn *function)
227
query->context_free_fn= function;
230
static void drizzle_query_run_state(drizzle_query_st* query,
231
drizzle_return_t* ret_ptr)
233
switch (query->state)
235
case DRIZZLE_QUERY_STATE_INIT:
236
query->state= DRIZZLE_QUERY_STATE_QUERY;
237
case DRIZZLE_QUERY_STATE_QUERY:
238
query->result= drizzle_query(query->con, query->result, query->string,
239
query->size, ret_ptr);
240
if (*ret_ptr == DRIZZLE_RETURN_IO_WAIT)
244
else if (*ret_ptr != DRIZZLE_RETURN_OK)
246
query->state= DRIZZLE_QUERY_STATE_DONE;
250
query->state= DRIZZLE_QUERY_STATE_RESULT;
252
case DRIZZLE_QUERY_STATE_RESULT:
253
*ret_ptr= drizzle_result_buffer(query->result);
254
if (*ret_ptr == DRIZZLE_RETURN_IO_WAIT)
259
query->state= DRIZZLE_QUERY_STATE_DONE;
263
case DRIZZLE_QUERY_STATE_DONE:
268
drizzle_query_st *drizzle_query_run(drizzle_st *drizzle,
269
drizzle_return_t *ret_ptr)
272
drizzle_query_st *query;
275
if (drizzle->query_new == 0 && drizzle->query_running == 0)
277
*ret_ptr= DRIZZLE_RETURN_OK;
281
options= drizzle->options;
282
drizzle->options|= DRIZZLE_NON_BLOCKING;
284
/* Check to see if any queries need to be started. */
285
if (drizzle->query_new > 0)
287
for (query= drizzle->query_list; query != NULL; query= query->next)
289
if (query->state != DRIZZLE_QUERY_STATE_INIT)
292
drizzle->query_new--;
293
drizzle->query_running++;
294
assert(query->con->query == NULL);
295
query->con->query= query;
297
drizzle_query_run_state(query, ret_ptr);
298
if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
300
assert(query->state == DRIZZLE_QUERY_STATE_DONE);
301
drizzle->query_running--;
302
drizzle->options= options;
303
query->con->query= NULL;
304
if (*ret_ptr == DRIZZLE_RETURN_ERROR_CODE || *ret_ptr == DRIZZLE_RETURN_OK)
311
assert(drizzle->query_new == 0);
316
/* Loop through each active connection. */
317
while ((con= drizzle_con_ready(drizzle)) != NULL)
320
drizzle_query_run_state(query, ret_ptr);
321
if (query->state == DRIZZLE_QUERY_STATE_DONE)
323
drizzle->query_running--;
324
drizzle->options= options;
328
assert(*ret_ptr == DRIZZLE_RETURN_IO_WAIT);
331
if (options & DRIZZLE_NON_BLOCKING)
333
*ret_ptr= DRIZZLE_RETURN_IO_WAIT;
337
*ret_ptr= drizzle_con_wait(drizzle);
338
if (*ret_ptr != DRIZZLE_RETURN_OK)
340
drizzle->options= options;
346
drizzle_return_t drizzle_query_run_all(drizzle_st *drizzle)
348
drizzle_return_t ret;
350
while (drizzle->query_new > 0 || drizzle->query_running > 0)
352
(void)drizzle_query_run(drizzle, &ret);
353
if (ret != DRIZZLE_RETURN_OK && ret != DRIZZLE_RETURN_ERROR_CODE)
357
return DRIZZLE_RETURN_OK;
360
ssize_t drizzle_safe_escape_string(char *to, size_t max_to_size, const char *from, size_t from_size)
366
for (end= from + from_size; from < end; from++)
369
/* All multi-byte UTF8 characters have the high bit set for all bytes. */
401
if ((size_t)to_size + 2 > max_to_size)
410
if ((size_t)to_size + 1 > max_to_size)
423
size_t drizzle_escape_string(char *to, const char *from, size_t from_size)
425
return (size_t) drizzle_safe_escape_string(to, (from_size * 2), from, from_size);
428
size_t drizzle_hex_string(char *to, const char *from, size_t from_size)
430
static const char hex_map[]= "0123456789ABCDEF";
431
const char *from_end;
433
for (from_end= from + from_size; from != from_end; from++)
435
*to++= hex_map[((unsigned char) *from) >> 4];
436
*to++= hex_map[((unsigned char) *from) & 0xF];
441
return from_size * 2;
444
void drizzle_mysql_password_hash(char *to, const char *from, size_t from_size)
447
uint8_t hash_tmp1[SHA1_DIGEST_LENGTH];
448
uint8_t hash_tmp2[SHA1_DIGEST_LENGTH];
451
SHA1Update(&ctx, (const uint8_t*)from, from_size);
452
SHA1Final(hash_tmp1, &ctx);
455
SHA1Update(&ctx, hash_tmp1, SHA1_DIGEST_LENGTH);
456
SHA1Final(hash_tmp2, &ctx);
458
(void)drizzle_hex_string(to, (char*)hash_tmp2, SHA1_DIGEST_LENGTH);