39
39
* @brief Query definitions
42
#include <libdrizzle-2.0/common.h>
44
44
drizzle_result_st *drizzle_query(drizzle_con_st *con, drizzle_result_st *result,
45
45
const char *query, size_t size,
55
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);
57
size_t size= strlen(query);
59
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY, (uint8_t *)query, size, size, ret_ptr);
65
62
drizzle_result_st *drizzle_query_inc(drizzle_con_st *con,
67
64
const char *query, size_t size,
68
65
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);
67
return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY, (uint8_t *)query, size, total, ret_ptr);
74
70
drizzle_query_st *drizzle_query_add(drizzle_st *drizzle,
76
72
drizzle_con_st *con,
77
73
drizzle_result_st *result,
78
74
const char *query_string, size_t size,
79
drizzle_query_options_t options,
75
drizzle_query_options_t,
78
// @note drizzle_query_st handle the null drizzle case
82
79
query= drizzle_query_create(drizzle, query);
86
85
drizzle_query_set_con(query, con);
87
86
drizzle_query_set_result(query, result);
88
87
drizzle_query_set_string(query, query_string, size);
89
drizzle_query_add_options(query, options);
90
88
drizzle_query_set_context(query, context);
95
drizzle_query_st *drizzle_query_create(drizzle_st *drizzle,
96
drizzle_query_st *query)
93
drizzle_query_st *drizzle_query_create(drizzle_st *drizzle, drizzle_query_st *query)
98
100
if (query == NULL)
100
query= new drizzle_query_st;
101
query->options|= DRIZZLE_CON_ALLOCATED;
102
query= new (std::nothrow) drizzle_query_st;
108
query->options.is_allocated= true;
105
112
query->prev= NULL;
107
113
query->state= DRIZZLE_QUERY_STATE_INIT;
108
114
query->con= NULL;
109
115
query->result= NULL;
112
118
query->context= NULL;
113
119
query->context_free_fn= NULL;
120
query->options.is_allocated= false;
116
123
query->drizzle= drizzle;
118
125
if (drizzle->query_list)
119
127
drizzle->query_list->prev= query;
120
129
query->next= drizzle->query_list;
121
130
drizzle->query_list= query;
122
131
drizzle->query_count++;
128
137
void drizzle_query_free(drizzle_query_st *query)
130
144
if (query->context != NULL && query->context_free_fn != NULL)
131
146
query->context_free_fn(query, query->context);
133
149
if (query->drizzle->query_list == query)
134
151
query->drizzle->query_list= query->next;
136
156
query->prev->next= query->next;
138
161
query->next->prev= query->prev;
139
164
query->drizzle->query_count--;
141
if (query->options & DRIZZLE_QUERY_ALLOCATED)
166
if (query->options.is_allocated)
145
172
void drizzle_query_free_all(drizzle_st *drizzle)
147
174
while (drizzle->query_list != NULL)
148
176
drizzle_query_free(drizzle->query_list);
151
180
drizzle_con_st *drizzle_query_con(drizzle_query_st *query)
153
187
return query->con;
156
190
void drizzle_query_set_con(drizzle_query_st *query, drizzle_con_st *con)
161
200
drizzle_result_st *drizzle_query_result(drizzle_query_st *query)
163
207
return query->result;
166
210
void drizzle_query_set_result(drizzle_query_st *query,
167
211
drizzle_result_st *result)
169
218
query->result= result;
172
221
char *drizzle_query_string(drizzle_query_st *query, size_t *size)
174
228
*size= query->size;
175
229
return (char *)(query->string);
178
232
void drizzle_query_set_string(drizzle_query_st *query, const char *string,
181
240
query->string= string;
182
241
query->size= size;
185
int drizzle_query_options(drizzle_query_st *query)
187
return query->options;
190
void drizzle_query_set_options(drizzle_query_st *query,
193
query->options= options;
196
void drizzle_query_add_options(drizzle_query_st *query,
199
query->options|= options;
202
void drizzle_query_remove_options(drizzle_query_st *query,
205
query->options&= ~options;
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)
208
261
void *drizzle_query_context(drizzle_query_st *query)
210
268
return query->context;
213
271
void drizzle_query_set_context(drizzle_query_st *query, void *context)
215
278
query->context= context;
218
281
void drizzle_query_set_context_free_fn(drizzle_query_st *query,
219
282
drizzle_query_context_free_fn *function)
221
289
query->context_free_fn= function;
224
292
static void drizzle_query_run_state(drizzle_query_st* query,
225
293
drizzle_return_t* ret_ptr)
227
300
switch (query->state)
229
302
case DRIZZLE_QUERY_STATE_INIT:
230
303
query->state= DRIZZLE_QUERY_STATE_QUERY;
231
305
case DRIZZLE_QUERY_STATE_QUERY:
232
306
query->result= drizzle_query(query->con, query->result, query->string,
233
307
query->size, ret_ptr);
262
336
drizzle_query_st *drizzle_query_run(drizzle_st *drizzle,
263
337
drizzle_return_t *ret_ptr)
266
drizzle_query_st *query;
339
drizzle_return_t unused;
347
*ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
269
351
if (drizzle->query_new == 0 && drizzle->query_running == 0)
275
options= drizzle->options;
276
drizzle->options|= DRIZZLE_NON_BLOCKING;
357
drizzle_st::options_t options= drizzle->options;
358
drizzle->options.is_non_blocking= false;
278
360
/* Check to see if any queries need to be started. */
279
361
if (drizzle->query_new > 0)
281
for (query= drizzle->query_list; query != NULL; query= query->next)
363
for (drizzle_query_st *query= drizzle->query_list; query != NULL; query= query->next)
283
365
if (query->state != DRIZZLE_QUERY_STATE_INIT)
286
370
drizzle->query_new--;
287
371
drizzle->query_running++;
310
396
/* Loop through each active connection. */
311
397
while ((con= drizzle_con_ready(drizzle)) != NULL)
399
drizzle_query_st *query= con->query;
314
400
drizzle_query_run_state(query, ret_ptr);
315
401
if (query->state == DRIZZLE_QUERY_STATE_DONE)
340
426
drizzle_return_t drizzle_query_run_all(drizzle_st *drizzle)
342
drizzle_return_t ret;
430
return DRIZZLE_RETURN_INVALID_ARGUMENT;
344
433
while (drizzle->query_new > 0 || drizzle->query_running > 0)
435
drizzle_return_t ret;
346
437
(void)drizzle_query_run(drizzle, &ret);
347
438
if (ret != DRIZZLE_RETURN_OK && ret != DRIZZLE_RETURN_ERROR_CODE)
351
444
return DRIZZLE_RETURN_OK;
354
ssize_t drizzle_safe_escape_string(char *to, size_t max_to_size, const char *from, size_t from_size)
447
ssize_t drizzle_escape_string(char *to, size_t max_to_size, const char *from, size_t from_size)
356
449
ssize_t to_size= 0;
417
size_t drizzle_escape_string(char *to, const char *from, size_t from_size)
419
return (size_t) drizzle_safe_escape_string(to, (from_size * 2), from, from_size);
422
514
size_t drizzle_hex_string(char *to, const char *from, size_t from_size)
424
516
static const char hex_map[]= "0123456789ABCDEF";