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 Row definitions
48
uint64_t drizzle_row_read(drizzle_result_st *result, drizzle_return_t *ret_ptr)
50
if (drizzle_state_none(result->con))
52
drizzle_state_push(result->con, drizzle_state_row_read);
53
drizzle_state_push(result->con, drizzle_state_packet_read);
56
*ret_ptr= drizzle_state_loop(result->con);
58
return result->row_current;
61
drizzle_row_t drizzle_row_buffer(drizzle_result_st *result,
62
drizzle_return_t *ret_ptr)
65
drizzle_field_t field;
68
if (result->row == NULL)
70
if (drizzle_row_read(result, ret_ptr) == 0 || *ret_ptr != DRIZZLE_RETURN_OK)
73
result->row= new drizzle_row_t_type[result->column_count *2];
74
if (result->row == NULL)
76
drizzle_set_error(result->con->drizzle, "drizzle_row_buffer", "malloc");
77
*ret_ptr= DRIZZLE_RETURN_MEMORY;
81
result->field_sizes= reinterpret_cast<size_t *>(result->row + result->column_count);
86
field= drizzle_field_buffer(result, &total, ret_ptr);
87
if (*ret_ptr == DRIZZLE_RETURN_ROW_END)
89
if (*ret_ptr != DRIZZLE_RETURN_OK)
91
if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
94
result->field_sizes= NULL;
100
result->row[result->field_current - 1]= field;
101
result->field_sizes[result->field_current - 1]= total;
104
*ret_ptr= DRIZZLE_RETURN_OK;
111
void drizzle_row_free(drizzle_result_st *result, drizzle_row_t row)
115
for (x= 0; x < result->column_count; x++)
116
drizzle_field_free(row[x]);
121
size_t *drizzle_row_field_sizes(drizzle_result_st *result)
123
return result->field_sizes;
126
drizzle_row_t drizzle_row_next(drizzle_result_st *result)
128
if (result->row_current == result->row_count)
131
result->field_sizes= result->field_sizes_list->at(result->row_current);
132
result->row_current++;
133
return result->row_list->at(result->row_current - 1);
136
drizzle_row_t drizzle_row_prev(drizzle_result_st *result)
138
if (result->row_current == 0)
141
result->row_current--;
142
result->field_sizes= result->field_sizes_list->at(result->row_current);
143
return result->row_list->at(result->row_current);
146
void drizzle_row_seek(drizzle_result_st *result, uint64_t row)
148
if (row <= result->row_count)
149
result->row_current= row;
152
drizzle_row_t drizzle_row_index(drizzle_result_st *result, uint64_t row)
154
if (row >= result->row_count)
157
return (*result->row_list)[row];
160
uint64_t drizzle_row_current(drizzle_result_st *result)
162
return result->row_current;
169
drizzle_return_t drizzle_row_write(drizzle_result_st *result)
171
if (drizzle_state_none(result->con))
172
drizzle_state_push(result->con, drizzle_state_row_write);
174
return drizzle_state_loop(result->con);
178
* Internal state functions.
181
drizzle_return_t drizzle_state_row_read(drizzle_con_st *con)
183
drizzle_log_debug(con->drizzle, "drizzle_state_row_read");
185
if (con->packet_size != 0 && con->buffer_size < con->packet_size &&
186
con->buffer_size < 5)
188
drizzle_state_push(con, drizzle_state_read);
189
return DRIZZLE_RETURN_OK;
192
if (con->packet_size == 5 && con->buffer_ptr[0] == 254)
194
/* Got EOF packet, no more rows. */
195
con->result->row_current= 0;
196
con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 1);
197
con->status= (drizzle_con_status_t)drizzle_get_byte2(con->buffer_ptr + 3);
199
con->buffer_size-= 5;
201
else if (con->buffer_ptr[0] == 255)
203
drizzle_state_pop(con);
204
drizzle_state_push(con, drizzle_state_result_read);
205
return DRIZZLE_RETURN_OK;
207
else if (con->result->options & DRIZZLE_RESULT_ROW_BREAK)
208
con->result->options&= ~DRIZZLE_RESULT_ROW_BREAK;
211
con->result->row_count++;
212
con->result->row_current++;
213
con->result->field_current= 0;
216
drizzle_state_pop(con);
217
return DRIZZLE_RETURN_OK;
220
drizzle_return_t drizzle_state_row_write(drizzle_con_st *con)
222
uint8_t *start= con->buffer_ptr + con->buffer_size;
224
drizzle_log_debug(con->drizzle, "drizzle_state_row_write");
226
/* Flush buffer if there is not enough room. */
227
if (((size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer)) < 4)
229
drizzle_state_push(con, drizzle_state_write);
230
return DRIZZLE_RETURN_OK;
233
drizzle_set_byte3(start, con->packet_size);
234
start[3]= con->packet_number;
235
con->packet_number++;
237
con->buffer_size+= 4;
239
drizzle_state_pop(con);
240
return DRIZZLE_RETURN_OK;