2
* Drizzle Client & Protocol Library
4
* Copyright (C) 2008 Eric Day (eday@oddments.org)
7
* Use and distribution licensed under the BSD license. See
8
* the COPYING file in this directory for full text.
13
* @brief Row definitions
22
uint64_t drizzle_row_read(drizzle_result_st *result, drizzle_return_t *ret_ptr)
24
if (drizzle_state_none(result->con))
26
drizzle_state_push(result->con, drizzle_state_row_read);
27
drizzle_state_push(result->con, drizzle_state_packet_read);
30
*ret_ptr= drizzle_state_loop(result->con);
32
return result->row_current;
35
drizzle_row_t drizzle_row_buffer(drizzle_result_st *result,
36
drizzle_return_t *ret_ptr)
39
drizzle_field_t field;
42
if (result->row == NULL)
44
if (drizzle_row_read(result, ret_ptr) == 0 || *ret_ptr != DRIZZLE_RETURN_OK)
47
result->row= malloc((sizeof(drizzle_field_t) + sizeof(size_t)) *
48
result->column_count);
49
if (result->row == NULL)
51
drizzle_set_error(result->con->drizzle, "drizzle_row_buffer", "malloc");
52
*ret_ptr= DRIZZLE_RETURN_MEMORY;
56
result->field_sizes= (size_t *)(result->row + result->column_count);
61
field= drizzle_field_buffer(result, &total, ret_ptr);
62
if (*ret_ptr == DRIZZLE_RETURN_ROW_END)
64
if (*ret_ptr != DRIZZLE_RETURN_OK)
66
if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
70
result->field_sizes= NULL;
76
result->row[result->field_current - 1]= field;
77
result->field_sizes[result->field_current - 1]= total;
80
*ret_ptr= DRIZZLE_RETURN_OK;
87
void drizzle_row_free(drizzle_result_st *result, drizzle_row_t row)
91
for (x= 0; x < result->column_count; x++)
92
drizzle_field_free(row[x]);
97
size_t *drizzle_row_field_sizes(drizzle_result_st *result)
99
return result->field_sizes;
102
drizzle_row_t drizzle_row_next(drizzle_result_st *result)
104
if (result->row_current == result->row_count)
107
result->field_sizes= result->field_sizes_list[result->row_current];
108
result->row_current++;
109
return result->row_list[result->row_current - 1];
112
drizzle_row_t drizzle_row_prev(drizzle_result_st *result)
114
if (result->row_current == 0)
117
result->row_current--;
118
result->field_sizes= result->field_sizes_list[result->row_current];
119
return result->row_list[result->row_current];
122
void drizzle_row_seek(drizzle_result_st *result, uint64_t row)
124
if (row <= result->row_count)
125
result->row_current= row;
128
drizzle_row_t drizzle_row_index(drizzle_result_st *result, uint64_t row)
130
if (row >= result->row_count)
133
return result->row_list[row];
136
uint64_t drizzle_row_current(drizzle_result_st *result)
138
return result->row_current;
145
drizzle_return_t drizzle_row_write(drizzle_result_st *result)
147
if (drizzle_state_none(result->con))
148
drizzle_state_push(result->con, drizzle_state_row_write);
150
return drizzle_state_loop(result->con);
154
* Internal state functions.
157
drizzle_return_t drizzle_state_row_read(drizzle_con_st *con)
159
drizzle_log_debug(con->drizzle, "drizzle_state_row_read");
161
if (con->packet_size != 0 && con->buffer_size < con->packet_size)
163
drizzle_state_push(con, drizzle_state_read);
164
return DRIZZLE_RETURN_OK;
167
if (con->packet_size == 5 && con->buffer_ptr[0] == 254)
169
/* Got EOF packet, no more rows. */
170
con->result->row_current= 0;
171
con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 1);
172
con->status= drizzle_get_byte2(con->buffer_ptr + 3);
174
con->buffer_size-= 5;
176
else if (con->buffer_ptr[0] == 255)
178
drizzle_state_pop(con);
179
drizzle_state_push(con, drizzle_state_result_read);
180
return DRIZZLE_RETURN_OK;
182
else if (con->result->options & DRIZZLE_RESULT_ROW_BREAK)
183
con->result->options&= (drizzle_result_options_t)~DRIZZLE_RESULT_ROW_BREAK;
186
con->result->row_count++;
187
con->result->row_current++;
188
con->result->field_current= 0;
191
drizzle_state_pop(con);
192
return DRIZZLE_RETURN_OK;
195
drizzle_return_t drizzle_state_row_write(drizzle_con_st *con)
197
uint8_t *start= con->buffer_ptr + con->buffer_size;
199
drizzle_log_debug(con->drizzle, "drizzle_state_row_write");
201
/* Flush buffer if there is not enough room. */
202
if (((size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer)) < 4)
204
drizzle_state_push(con, drizzle_state_write);
205
return DRIZZLE_RETURN_OK;
208
drizzle_set_byte3(start, con->packet_size);
209
start[3]= con->packet_number;
210
con->packet_number++;
212
con->buffer_size+= 4;
214
drizzle_state_pop(con);
215
return DRIZZLE_RETURN_OK;