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 Result definitions
48
drizzle_result_st *drizzle_result_create(drizzle_con_st *con,
49
drizzle_result_st *result)
53
result= malloc(sizeof(drizzle_result_st));
56
drizzle_set_error(con->drizzle, "drizzle_result_create", "malloc");
60
memset(result, 0, sizeof(drizzle_result_st));
61
result->options|= DRIZZLE_RESULT_ALLOCATED;
64
memset(result, 0, sizeof(drizzle_result_st));
70
con->result_list->prev= result;
71
result->next= con->result_list;
72
con->result_list= result;
78
drizzle_result_st *drizzle_result_clone(drizzle_con_st *con,
79
drizzle_result_st *result,
80
drizzle_result_st *from)
82
result= drizzle_result_create(con, result);
86
result->options|= (from->options &
87
(drizzle_result_options_t)~DRIZZLE_RESULT_ALLOCATED);
89
drizzle_result_set_info(result, from->info);
90
result->error_code= from->error_code;
91
drizzle_result_set_sqlstate(result, from->sqlstate);
92
result->warning_count= from->warning_count;
93
result->insert_id= from->insert_id;
94
result->affected_rows= from->affected_rows;
95
result->column_count= from->column_count;
96
result->row_count= from->row_count;
101
void drizzle_result_free(drizzle_result_st *result)
103
drizzle_column_st *column;
106
for (column= result->column_list; column != NULL; column= result->column_list)
107
drizzle_column_free(column);
109
if (result->column_buffer != NULL)
110
free(result->column_buffer);
112
if (result->options & DRIZZLE_RESULT_BUFFER_ROW)
114
for (x= 0; x < result->row_count; x++)
115
drizzle_row_free(result, result->row_list[x]);
117
free(result->row_list);
118
free(result->field_sizes_list);
123
result->con->result_count--;
124
if (result->con->result_list == result)
125
result->con->result_list= result->next;
128
result->prev->next= result->next;
130
result->next->prev= result->prev;
132
if (result->options & DRIZZLE_RESULT_ALLOCATED)
136
void drizzle_result_free_all(drizzle_con_st *con)
138
while (con->result_list != NULL)
139
drizzle_result_free(con->result_list);
142
drizzle_con_st *drizzle_result_drizzle_con(drizzle_result_st *result)
147
bool drizzle_result_eof(drizzle_result_st *result)
149
return result->options & DRIZZLE_RESULT_EOF_PACKET;
152
const char *drizzle_result_info(drizzle_result_st *result)
157
const char *drizzle_result_error(drizzle_result_st *result)
162
uint16_t drizzle_result_error_code(drizzle_result_st *result)
164
return result->error_code;
167
const char *drizzle_result_sqlstate(drizzle_result_st *result)
169
return result->sqlstate;
172
uint16_t drizzle_result_warning_count(drizzle_result_st *result)
174
return result->warning_count;
177
uint64_t drizzle_result_insert_id(drizzle_result_st *result)
179
return result->insert_id;
182
uint64_t drizzle_result_affected_rows(drizzle_result_st *result)
184
return result->affected_rows;
187
uint16_t drizzle_result_column_count(drizzle_result_st *result)
189
return result->column_count;
192
uint64_t drizzle_result_row_count(drizzle_result_st *result)
194
return result->row_count;
201
drizzle_result_st *drizzle_result_read(drizzle_con_st *con,
202
drizzle_result_st *result,
203
drizzle_return_t *ret_ptr)
205
if (drizzle_state_none(con))
207
con->result= drizzle_result_create(con, result);
208
if (con->result == NULL)
210
*ret_ptr= DRIZZLE_RETURN_MEMORY;
214
drizzle_state_push(con, drizzle_state_result_read);
215
drizzle_state_push(con, drizzle_state_packet_read);
218
*ret_ptr= drizzle_state_loop(con);
222
drizzle_return_t drizzle_result_buffer(drizzle_result_st *result)
224
drizzle_return_t ret;
226
drizzle_row_t *row_list;
227
size_t **field_sizes_list;
229
if (!(result->options & DRIZZLE_RESULT_BUFFER_COLUMN))
231
ret= drizzle_column_buffer(result);
232
if (ret != DRIZZLE_RETURN_OK)
236
if (result->column_count == 0)
238
result->options|= DRIZZLE_RESULT_BUFFER_ROW;
239
return DRIZZLE_RETURN_OK;
244
row= drizzle_row_buffer(result, &ret);
245
if (ret != DRIZZLE_RETURN_OK)
251
if (result->row_list_size < result->row_count)
253
row_list= realloc(result->row_list, sizeof(drizzle_row_t) *
254
((size_t)(result->row_list_size) +
255
DRIZZLE_ROW_GROW_SIZE));
256
if (row_list == NULL)
258
drizzle_row_free(result, row);
259
drizzle_set_error(result->con->drizzle, "drizzle_result_buffer",
261
return DRIZZLE_RETURN_MEMORY;
264
result->row_list= row_list;
266
field_sizes_list= realloc(result->field_sizes_list, sizeof(size_t *) *
267
((size_t)(result->row_list_size) +
268
DRIZZLE_ROW_GROW_SIZE));
269
if (field_sizes_list == NULL)
271
drizzle_row_free(result, row);
272
drizzle_set_error(result->con->drizzle, "drizzle_result_buffer",
274
return DRIZZLE_RETURN_MEMORY;
277
result->field_sizes_list= field_sizes_list;
279
result->row_list_size+= DRIZZLE_ROW_GROW_SIZE;
282
result->row_list[result->row_current - 1]= row;
283
result->field_sizes_list[result->row_current - 1]= result->field_sizes;
286
result->options|= DRIZZLE_RESULT_BUFFER_ROW;
287
return DRIZZLE_RETURN_OK;
290
size_t drizzle_result_row_size(drizzle_result_st *result)
292
return result->con->packet_size;
299
drizzle_return_t drizzle_result_write(drizzle_con_st *con,
300
drizzle_result_st *result, bool flush)
302
if (drizzle_state_none(con))
307
drizzle_state_push(con, drizzle_state_write);
309
drizzle_state_push(con, drizzle_state_result_write);
312
return drizzle_state_loop(con);
315
void drizzle_result_set_row_size(drizzle_result_st *result, size_t size)
317
result->con->packet_size= size;
320
void drizzle_result_calc_row_size(drizzle_result_st *result,
321
const drizzle_field_t *field,
326
result->con->packet_size= 0;
328
for (x= 0; x < result->column_count; x++)
330
if (field[x] == NULL)
331
result->con->packet_size++;
332
else if (size[x] < 251)
333
result->con->packet_size+= (1 + size[x]);
334
else if (size[x] < 65536)
335
result->con->packet_size+= (3 + size[x]);
336
else if (size[x] < 16777216)
337
result->con->packet_size+= (4 + size[x]);
339
result->con->packet_size+= (9 + size[x]);
343
void drizzle_result_set_eof(drizzle_result_st *result, bool is_eof)
346
result->options|= DRIZZLE_RESULT_EOF_PACKET;
348
result->options&= (drizzle_result_options_t)~DRIZZLE_RESULT_EOF_PACKET;
351
void drizzle_result_set_info(drizzle_result_st *result, const char *info)
357
strncpy(result->info, info, DRIZZLE_MAX_INFO_SIZE);
358
result->info[DRIZZLE_MAX_INFO_SIZE - 1]= 0;
362
void drizzle_result_set_error(drizzle_result_st *result, const char *error)
364
drizzle_result_set_info(result, error);
367
void drizzle_result_set_error_code(drizzle_result_st *result,
370
result->error_code= error_code;
373
void drizzle_result_set_sqlstate(drizzle_result_st *result,
374
const char *sqlstate)
376
if (sqlstate == NULL)
377
result->sqlstate[0]= 0;
380
strncpy(result->sqlstate, sqlstate, DRIZZLE_MAX_SQLSTATE_SIZE + 1);
381
result->sqlstate[DRIZZLE_MAX_SQLSTATE_SIZE]= 0;
385
void drizzle_result_set_warning_count(drizzle_result_st *result,
386
uint16_t warning_count)
388
result->warning_count= warning_count;
391
void drizzle_result_set_insert_id(drizzle_result_st *result,
394
result->insert_id= insert_id;
397
void drizzle_result_set_affected_rows(drizzle_result_st *result,
398
uint64_t affected_rows)
400
result->affected_rows= affected_rows;
403
void drizzle_result_set_column_count(drizzle_result_st *result,
404
uint16_t column_count)
406
result->column_count= column_count;
410
* Internal state functions.
413
drizzle_return_t drizzle_state_result_read(drizzle_con_st *con)
415
drizzle_return_t ret;
417
drizzle_log_debug(con->drizzle, "drizzle_state_result_read");
419
/* Assume the entire result packet will fit in the buffer. */
420
if (con->buffer_size < con->packet_size)
422
drizzle_state_push(con, drizzle_state_read);
423
return DRIZZLE_RETURN_OK;
426
if (con->buffer_ptr[0] == 0)
429
/* We can ignore the returns since we've buffered the entire packet. */
430
con->result->affected_rows= drizzle_unpack_length(con, &ret);
431
con->result->insert_id= drizzle_unpack_length(con, &ret);
432
con->status= drizzle_get_byte2(con->buffer_ptr);
433
con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 2);
435
con->buffer_size-= 5;
436
con->packet_size-= 5;
437
if (con->packet_size > 0)
439
/* Skip one byte for message size. */
441
con->buffer_size-= 1;
442
con->packet_size-= 1;
444
ret= DRIZZLE_RETURN_OK;
446
else if (con->buffer_ptr[0] == 254)
448
con->result->options= DRIZZLE_RESULT_EOF_PACKET;
449
con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 1);
450
con->status= drizzle_get_byte2(con->buffer_ptr + 3);
452
con->buffer_size-= 5;
453
con->packet_size-= 5;
454
ret= DRIZZLE_RETURN_OK;
456
else if (con->buffer_ptr[0] == 255)
458
con->result->error_code= drizzle_get_byte2(con->buffer_ptr + 1);
459
con->drizzle->error_code= con->result->error_code;
460
/* Byte 3 is always a '#' character, skip it. */
461
memcpy(con->result->sqlstate, con->buffer_ptr + 4,
462
DRIZZLE_MAX_SQLSTATE_SIZE);
463
con->result->sqlstate[DRIZZLE_MAX_SQLSTATE_SIZE]= 0;
464
memcpy(con->drizzle->sqlstate, con->result->sqlstate,
465
DRIZZLE_MAX_SQLSTATE_SIZE + 1);
467
con->buffer_size-= 9;
468
con->packet_size-= 9;
469
ret= DRIZZLE_RETURN_ERROR_CODE;
473
/* We can ignore the return since we've buffered the entire packet. */
474
con->result->column_count= (uint16_t)drizzle_unpack_length(con, &ret);
475
ret= DRIZZLE_RETURN_OK;
478
if (con->packet_size > 0)
480
snprintf(con->drizzle->last_error, DRIZZLE_MAX_ERROR_SIZE, "%.*s",
481
(int32_t)con->packet_size, con->buffer_ptr);
482
snprintf(con->result->info, DRIZZLE_MAX_INFO_SIZE, "%.*s",
483
(int32_t)con->packet_size, con->buffer_ptr);
484
con->buffer_ptr+= con->packet_size;
485
con->buffer_size-= con->packet_size;
489
drizzle_state_pop(con);
493
drizzle_return_t drizzle_state_result_write(drizzle_con_st *con)
495
uint8_t *start= con->buffer_ptr + con->buffer_size;
497
drizzle_result_st *result= con->result;
499
drizzle_log_debug(con->drizzle, "drizzle_state_result_write");
501
/* Calculate max packet size. */
502
con->packet_size= 1 /* OK/Field Count/EOF/Error */
503
+ 9 /* Affected rows */
506
+ 2 /* Warning count */
507
+ strlen(result->info); /* Info/error message */
509
/* Assume the entire result packet will fit in the buffer. */
510
if ((con->packet_size + 4) > DRIZZLE_MAX_BUFFER_SIZE)
512
drizzle_set_error(con->drizzle, "drizzle_state_result_write",
513
"buffer too small:%zu", con->packet_size + 4);
514
return DRIZZLE_RETURN_INTERNAL_ERROR;
517
/* Flush buffer if there is not enough room. */
518
if (((size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer)) <
521
drizzle_state_push(con, drizzle_state_write);
522
return DRIZZLE_RETURN_OK;
525
/* Store packet size at the end since it may change. */
527
ptr[3]= con->packet_number;
528
con->packet_number++;
531
if (result->options & DRIZZLE_RESULT_EOF_PACKET)
536
drizzle_set_byte2(ptr, result->warning_count);
539
drizzle_set_byte2(ptr, con->status);
542
else if (result->error_code != 0)
547
drizzle_set_byte2(ptr, result->error_code);
553
memcpy(ptr, result->sqlstate, DRIZZLE_MAX_SQLSTATE_SIZE);
554
ptr+= DRIZZLE_MAX_SQLSTATE_SIZE;
556
memcpy(ptr, result->info, strlen(result->info));
557
ptr+= strlen(result->info);
559
else if (result->column_count == 0)
564
ptr= drizzle_pack_length(result->affected_rows, ptr);
565
ptr= drizzle_pack_length(result->insert_id, ptr);
567
drizzle_set_byte2(ptr, con->status);
570
drizzle_set_byte2(ptr, result->warning_count);
573
memcpy(ptr, result->info, strlen(result->info));
574
ptr+= strlen(result->info);
577
ptr= drizzle_pack_length(result->column_count, ptr);
579
con->packet_size= ((size_t)(ptr - start) - 4);
580
con->buffer_size+= (4 + con->packet_size);
582
/* Store packet size now. */
583
drizzle_set_byte3(start, con->packet_size);
585
drizzle_state_pop(con);
586
return DRIZZLE_RETURN_OK;