1
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
* Drizzle Client & Protocol Library
5
* Copyright (C) 2008 Eric Day (eday@oddments.org)
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions are
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* * Redistributions in binary form must reproduce the above
16
* copyright notice, this list of conditions and the following disclaimer
17
* in the documentation and/or other materials provided with the
20
* * The names of its contributors may not be used to endorse or
21
* promote products derived from this software without specific prior
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
* @brief Row definitions
43
#include <libdrizzle/common.h>
49
uint64_t drizzle_row_read(drizzle_result_st *result, drizzle_return_t *ret_ptr)
51
drizzle_return_t unused_ret;
59
*ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
63
if ((result->column_current != result->column_count) && (!(result->options & DRIZZLE_RESULT_BUFFER_COLUMN)))
65
drizzle_set_error(result->con->drizzle, "drizzle_row_read", "cannot retrieve rows until all columns are retrieved");
66
*ret_ptr= DRIZZLE_RETURN_NOT_READY;
70
if (drizzle_state_none(result->con))
72
drizzle_state_push(result->con, drizzle_state_row_read);
73
drizzle_state_push(result->con, drizzle_state_packet_read);
76
*ret_ptr= drizzle_state_loop(result->con);
78
return result->row_current;
81
drizzle_row_t drizzle_row_buffer(drizzle_result_st *result,
82
drizzle_return_t *ret_ptr)
84
drizzle_return_t unused_ret;
92
*ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
97
drizzle_field_t field;
100
if (result->row == NULL)
102
if (drizzle_row_read(result, ret_ptr) == 0 || *ret_ptr != DRIZZLE_RETURN_OK)
107
result->row= (drizzle_row_t)realloc(NULL, (sizeof(drizzle_field_t) + sizeof(size_t)) * result->column_count);
108
if (result->row == NULL)
110
drizzle_set_error(result->con->drizzle, __func__, "Failed to allocate.");
111
*ret_ptr= DRIZZLE_RETURN_MEMORY;
115
result->field_sizes= (size_t *)(result->row + result->column_count);
120
field= drizzle_field_buffer(result, &total, ret_ptr);
121
if (*ret_ptr == DRIZZLE_RETURN_ROW_END)
124
if (*ret_ptr != DRIZZLE_RETURN_OK)
126
if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
130
result->field_sizes= NULL;
136
result->row[result->field_current - 1]= field;
137
result->field_sizes[result->field_current - 1]= total;
140
*ret_ptr= DRIZZLE_RETURN_OK;
147
void drizzle_row_free(drizzle_result_st *result, drizzle_row_t row)
156
for (x= 0; x < result->column_count; x++)
158
drizzle_field_free(row[x]);
164
size_t *drizzle_row_field_sizes(drizzle_result_st *result)
171
return result->field_sizes;
174
drizzle_row_t drizzle_row_next(drizzle_result_st *result)
181
if (result->row_current == result->row_count)
186
result->field_sizes= result->field_sizes_list[result->row_current];
187
result->row_current++;
188
return result->row_list[result->row_current - 1];
191
drizzle_row_t drizzle_row_prev(drizzle_result_st *result)
198
if (result->row_current == 0)
201
result->row_current--;
202
result->field_sizes= result->field_sizes_list[result->row_current];
203
return result->row_list[result->row_current];
206
void drizzle_row_seek(drizzle_result_st *result, uint64_t row)
213
if (row <= result->row_count)
214
result->row_current= row;
217
drizzle_row_t drizzle_row_index(drizzle_result_st *result, uint64_t row)
224
if (row >= result->row_count)
227
return result->row_list[row];
230
uint64_t drizzle_row_current(drizzle_result_st *result)
237
return result->row_current;
244
drizzle_return_t drizzle_row_write(drizzle_result_st *result)
248
return DRIZZLE_RETURN_INVALID_ARGUMENT;
251
if (drizzle_state_none(result->con))
253
drizzle_state_push(result->con, drizzle_state_row_write);
256
return drizzle_state_loop(result->con);
260
* Internal state functions.
263
drizzle_return_t drizzle_state_row_read(drizzle_con_st *con)
267
return DRIZZLE_RETURN_INVALID_ARGUMENT;
270
drizzle_log_debug(con->drizzle, "drizzle_state_row_read");
272
if (con->packet_size != 0 && con->buffer_size < con->packet_size &&
273
con->buffer_size < 5)
275
drizzle_state_push(con, drizzle_state_read);
276
return DRIZZLE_RETURN_OK;
279
if (con->packet_size == 5 && con->buffer_ptr[0] == 254)
281
/* Got EOF packet, no more rows. */
282
con->result->row_current= 0;
283
con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 1);
284
con->status= drizzle_get_byte2(con->buffer_ptr + 3);
286
con->buffer_size-= 5;
288
else if (con->buffer_ptr[0] == 255)
290
drizzle_state_pop(con);
291
drizzle_state_push(con, drizzle_state_result_read);
292
return DRIZZLE_RETURN_OK;
294
else if (con->result->options & DRIZZLE_RESULT_ROW_BREAK)
296
con->result->options&= ~DRIZZLE_RESULT_ROW_BREAK;
300
con->result->row_count++;
301
con->result->row_current++;
302
con->result->field_current= 0;
305
drizzle_state_pop(con);
306
return DRIZZLE_RETURN_OK;
309
drizzle_return_t drizzle_state_row_write(drizzle_con_st *con)
313
return DRIZZLE_RETURN_INVALID_ARGUMENT;
316
uint8_t *start= con->buffer_ptr + con->buffer_size;
318
drizzle_log_debug(con->drizzle, "drizzle_state_row_write");
320
/* Flush buffer if there is not enough room. */
321
if (((size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer)) < 4)
323
drizzle_state_push(con, drizzle_state_write);
324
return DRIZZLE_RETURN_OK;
327
drizzle_set_byte3(start, con->packet_size);
328
start[3]= con->packet_number;
329
con->packet_number++;
331
con->buffer_size+= 4;
333
drizzle_state_pop(con);
334
return DRIZZLE_RETURN_OK;