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
49
drizzle_result_st *drizzle_result_create(drizzle_con_st *con,
50
drizzle_result_st *result)
54
result= new drizzle_result_st;
57
drizzle_set_error(con->drizzle, "drizzle_result_create", "malloc");
61
result->options|= DRIZZLE_RESULT_ALLOCATED;
68
result->info[0]= '\0';
69
result->error_code= 0;
70
result->sqlstate[0]= '\0';
72
result->warning_count= 0;
73
result->affected_rows= 0;
75
result->column_count= 0;
76
result->column_current= 0;
77
result->column_list= NULL;
79
result->column_buffer= NULL;
82
result->row_current= 0;
84
result->field_current= 0;
85
result->field_total= 0;
86
result->field_offset= 0;
87
result->field_size= 0;
89
result->field_buffer= NULL;
91
result->row_list_size= 0;
93
result->row_list= NULL;
94
result->field_sizes= NULL;
95
result->field_sizes_list= NULL;
101
if (con->result_list)
102
con->result_list->prev= result;
103
result->next= con->result_list;
104
con->result_list= result;
110
drizzle_result_st *drizzle_result_clone(drizzle_con_st *con,
111
drizzle_result_st *result,
112
drizzle_result_st *from)
114
result= drizzle_result_create(con, result);
118
result->options|= from->options & ~DRIZZLE_RESULT_ALLOCATED;
120
drizzle_result_set_info(result, from->info);
121
result->error_code= from->error_code;
122
drizzle_result_set_sqlstate(result, from->sqlstate);
123
result->warning_count= from->warning_count;
124
result->insert_id= from->insert_id;
125
result->affected_rows= from->affected_rows;
126
result->column_count= from->column_count;
127
result->row_count= from->row_count;
132
void drizzle_result_free(drizzle_result_st *result)
134
drizzle_column_st *column;
137
for (column= result->column_list; column != NULL; column= result->column_list)
138
drizzle_column_free(column);
140
if (result->column_buffer != NULL)
142
delete[] result->column_buffer;
145
if (result->options & DRIZZLE_RESULT_BUFFER_ROW)
147
for (x= 0; x < result->row_count; x++)
148
drizzle_row_free(result, result->row_list->at(x));
150
delete result->row_list;
151
delete result->field_sizes_list;
156
result->con->result_count--;
157
if (result->con->result_list == result)
158
result->con->result_list= result->next;
161
result->prev->next= result->next;
163
result->next->prev= result->prev;
165
if (result->options & DRIZZLE_RESULT_ALLOCATED)
169
void drizzle_result_free_all(drizzle_con_st *con)
171
while (con->result_list != NULL)
172
drizzle_result_free(con->result_list);
175
drizzle_con_st *drizzle_result_drizzle_con(drizzle_result_st *result)
180
bool drizzle_result_eof(drizzle_result_st *result)
182
return result->options & DRIZZLE_RESULT_EOF_PACKET;
185
const char *drizzle_result_info(drizzle_result_st *result)
190
const char *drizzle_result_error(drizzle_result_st *result)
195
uint16_t drizzle_result_error_code(drizzle_result_st *result)
197
return result->error_code;
200
const char *drizzle_result_sqlstate(drizzle_result_st *result)
202
return result->sqlstate;
205
uint16_t drizzle_result_warning_count(drizzle_result_st *result)
207
return result->warning_count;
210
uint64_t drizzle_result_insert_id(drizzle_result_st *result)
212
return result->insert_id;
215
uint64_t drizzle_result_affected_rows(drizzle_result_st *result)
217
return result->affected_rows;
220
uint16_t drizzle_result_column_count(drizzle_result_st *result)
222
return result->column_count;
225
uint64_t drizzle_result_row_count(drizzle_result_st *result)
227
return result->row_count;
234
drizzle_result_st *drizzle_result_read(drizzle_con_st *con,
235
drizzle_result_st *result,
236
drizzle_return_t *ret_ptr)
238
if (drizzle_state_none(con))
240
con->result= drizzle_result_create(con, result);
241
if (con->result == NULL)
243
*ret_ptr= DRIZZLE_RETURN_MEMORY;
247
drizzle_state_push(con, drizzle_state_result_read);
248
drizzle_state_push(con, drizzle_state_packet_read);
251
*ret_ptr= drizzle_state_loop(con);
255
drizzle_return_t drizzle_result_buffer(drizzle_result_st *result)
257
drizzle_return_t ret;
260
if (!(result->options & DRIZZLE_RESULT_BUFFER_COLUMN))
262
ret= drizzle_column_buffer(result);
263
if (ret != DRIZZLE_RETURN_OK)
267
if (result->column_count == 0)
269
result->options|= DRIZZLE_RESULT_BUFFER_ROW;
270
return DRIZZLE_RETURN_OK;
275
row= drizzle_row_buffer(result, &ret);
276
if (ret != DRIZZLE_RETURN_OK)
282
if (result->row_list == NULL)
284
result->row_list= new (std::nothrow) drizzle_row_list_t;
285
if (result->row_list == NULL)
287
drizzle_row_free(result, row);
288
drizzle_set_error(result->con->drizzle, "drizzle_result_buffer",
290
return DRIZZLE_RETURN_MEMORY;
295
if (result->field_sizes_list == NULL)
297
result->field_sizes_list= new (std::nothrow) drizzle_field_sizes_list_t;
298
if (result->field_sizes_list == NULL)
300
drizzle_row_free(result, row);
301
drizzle_set_error(result->con->drizzle, "drizzle_result_buffer",
303
return DRIZZLE_RETURN_MEMORY;
307
result->row_list->push_back(row);
308
result->field_sizes_list->push_back(result->field_sizes);
311
result->options|= DRIZZLE_RESULT_BUFFER_ROW;
312
return DRIZZLE_RETURN_OK;
315
size_t drizzle_result_row_size(drizzle_result_st *result)
317
return result->con->packet_size;
324
drizzle_return_t drizzle_result_write(drizzle_con_st *con,
325
drizzle_result_st *result, bool flush)
327
if (drizzle_state_none(con))
332
drizzle_state_push(con, drizzle_state_write);
334
drizzle_state_push(con, drizzle_state_result_write);
337
return drizzle_state_loop(con);
340
void drizzle_result_set_row_size(drizzle_result_st *result, size_t size)
342
result->con->packet_size= size;
345
void drizzle_result_calc_row_size(drizzle_result_st *result,
346
const drizzle_field_t *field,
351
result->con->packet_size= 0;
353
for (x= 0; x < result->column_count; x++)
355
if (field[x] == NULL)
356
result->con->packet_size++;
357
else if (size[x] < 251)
358
result->con->packet_size+= (1 + size[x]);
359
else if (size[x] < 65536)
360
result->con->packet_size+= (3 + size[x]);
361
else if (size[x] < 16777216)
362
result->con->packet_size+= (4 + size[x]);
364
result->con->packet_size+= (9 + size[x]);
368
void drizzle_result_set_eof(drizzle_result_st *result, bool is_eof)
371
result->options|= DRIZZLE_RESULT_EOF_PACKET;
373
result->options&= ~DRIZZLE_RESULT_EOF_PACKET;
376
void drizzle_result_set_info(drizzle_result_st *result, const char *info)
382
strncpy(result->info, info, DRIZZLE_MAX_INFO_SIZE);
383
result->info[DRIZZLE_MAX_INFO_SIZE - 1]= 0;
387
void drizzle_result_set_error(drizzle_result_st *result, const char *error)
389
drizzle_result_set_info(result, error);
392
void drizzle_result_set_error_code(drizzle_result_st *result,
395
result->error_code= error_code;
398
void drizzle_result_set_sqlstate(drizzle_result_st *result,
399
const char *sqlstate)
401
if (sqlstate == NULL)
402
result->sqlstate[0]= 0;
405
strncpy(result->sqlstate, sqlstate, DRIZZLE_MAX_SQLSTATE_SIZE + 1);
406
result->sqlstate[DRIZZLE_MAX_SQLSTATE_SIZE]= 0;
410
void drizzle_result_set_warning_count(drizzle_result_st *result,
411
uint16_t warning_count)
413
result->warning_count= warning_count;
416
void drizzle_result_set_insert_id(drizzle_result_st *result,
419
result->insert_id= insert_id;
422
void drizzle_result_set_affected_rows(drizzle_result_st *result,
423
uint64_t affected_rows)
425
result->affected_rows= affected_rows;
428
void drizzle_result_set_column_count(drizzle_result_st *result,
429
uint16_t column_count)
431
result->column_count= column_count;
435
* Internal state functions.
438
drizzle_return_t drizzle_state_result_read(drizzle_con_st *con)
440
drizzle_return_t ret;
442
drizzle_log_debug(con->drizzle, "drizzle_state_result_read");
444
/* Assume the entire result packet will fit in the buffer. */
445
if (con->buffer_size < con->packet_size)
447
drizzle_state_push(con, drizzle_state_read);
448
return DRIZZLE_RETURN_OK;
451
if (con->buffer_ptr[0] == 0)
454
/* We can ignore the returns since we've buffered the entire packet. */
455
con->result->affected_rows= drizzle_unpack_length(con, &ret);
456
con->result->insert_id= drizzle_unpack_length(con, &ret);
457
con->status= (drizzle_con_status_t)drizzle_get_byte2(con->buffer_ptr);
458
con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 2);
460
con->buffer_size-= 5;
461
con->packet_size-= 5;
462
if (con->packet_size > 0)
464
/* Skip one byte for message size. */
466
con->buffer_size-= 1;
467
con->packet_size-= 1;
469
ret= DRIZZLE_RETURN_OK;
471
else if (con->buffer_ptr[0] == 254)
473
con->result->options= DRIZZLE_RESULT_EOF_PACKET;
474
con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 1);
475
con->status= (drizzle_con_status_t)drizzle_get_byte2(con->buffer_ptr + 3);
477
con->buffer_size-= 5;
478
con->packet_size-= 5;
479
ret= DRIZZLE_RETURN_OK;
481
else if (con->buffer_ptr[0] == 255)
483
con->result->error_code= drizzle_get_byte2(con->buffer_ptr + 1);
484
con->drizzle->error_code= con->result->error_code;
485
/* Byte 3 is always a '#' character, skip it. */
486
memcpy(con->result->sqlstate, con->buffer_ptr + 4,
487
DRIZZLE_MAX_SQLSTATE_SIZE);
488
con->result->sqlstate[DRIZZLE_MAX_SQLSTATE_SIZE]= 0;
489
memcpy(con->drizzle->sqlstate, con->result->sqlstate,
490
DRIZZLE_MAX_SQLSTATE_SIZE + 1);
492
con->buffer_size-= 9;
493
con->packet_size-= 9;
494
ret= DRIZZLE_RETURN_ERROR_CODE;
498
/* We can ignore the return since we've buffered the entire packet. */
499
con->result->column_count= (uint16_t)drizzle_unpack_length(con, &ret);
500
ret= DRIZZLE_RETURN_OK;
503
if (con->packet_size > 0)
505
snprintf(con->drizzle->last_error, DRIZZLE_MAX_ERROR_SIZE, "%.*s",
506
(int32_t)con->packet_size, con->buffer_ptr);
507
snprintf(con->result->info, DRIZZLE_MAX_INFO_SIZE, "%.*s",
508
(int32_t)con->packet_size, con->buffer_ptr);
509
con->buffer_ptr+= con->packet_size;
510
con->buffer_size-= con->packet_size;
514
drizzle_state_pop(con);
518
drizzle_return_t drizzle_state_result_write(drizzle_con_st *con)
520
uint8_t *start= con->buffer_ptr + con->buffer_size;
522
drizzle_result_st *result= con->result;
524
drizzle_log_debug(con->drizzle, "drizzle_state_result_write");
526
/* Calculate max packet size. */
527
con->packet_size= 1 /* OK/Field Count/EOF/Error */
528
+ 9 /* Affected rows */
531
+ 2 /* Warning count */
532
+ strlen(result->info); /* Info/error message */
534
/* Assume the entire result packet will fit in the buffer. */
535
if ((con->packet_size + 4) > DRIZZLE_MAX_BUFFER_SIZE)
537
drizzle_set_error(con->drizzle, "drizzle_state_result_write",
538
"buffer too small:%zu", con->packet_size + 4);
539
return DRIZZLE_RETURN_INTERNAL_ERROR;
542
/* Flush buffer if there is not enough room. */
543
if (((size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer)) <
546
drizzle_state_push(con, drizzle_state_write);
547
return DRIZZLE_RETURN_OK;
550
/* Store packet size at the end since it may change. */
552
ptr[3]= con->packet_number;
553
con->packet_number++;
556
if (result->options & DRIZZLE_RESULT_EOF_PACKET)
561
drizzle_set_byte2(ptr, result->warning_count);
564
drizzle_set_byte2(ptr, con->status);
567
else if (result->error_code != 0)
572
drizzle_set_byte2(ptr, result->error_code);
578
memcpy(ptr, result->sqlstate, DRIZZLE_MAX_SQLSTATE_SIZE);
579
ptr+= DRIZZLE_MAX_SQLSTATE_SIZE;
581
memcpy(ptr, result->info, strlen(result->info));
582
ptr+= strlen(result->info);
584
else if (result->column_count == 0)
589
ptr= drizzle_pack_length(result->affected_rows, ptr);
590
ptr= drizzle_pack_length(result->insert_id, ptr);
592
drizzle_set_byte2(ptr, con->status);
595
drizzle_set_byte2(ptr, result->warning_count);
598
memcpy(ptr, result->info, strlen(result->info));
599
ptr+= strlen(result->info);
602
ptr= drizzle_pack_length(result->column_count, ptr);
604
con->packet_size= ((size_t)(ptr - start) - 4);
605
con->buffer_size+= (4 + con->packet_size);
607
/* Store packet size now. */
608
drizzle_set_byte3(start, con->packet_size);
610
drizzle_state_pop(con);
611
return DRIZZLE_RETURN_OK;