1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
1 |
/*
|
2 |
* Drizzle Client & Protocol Library
|
|
3 |
*
|
|
4 |
* Copyright (C) 2008 Eric Day (eday@oddments.org)
|
|
5 |
* All rights reserved.
|
|
6 |
*
|
|
7 |
* Use and distribution licensed under the BSD license. See
|
|
8 |
* the COPYING file in this directory for full text.
|
|
9 |
*/
|
|
10 |
||
11 |
/**
|
|
12 |
* @file
|
|
13 |
* @brief Row definitions
|
|
14 |
*/
|
|
15 |
||
16 |
#include "common.h" |
|
17 |
||
18 |
/*
|
|
19 |
* Client definitions
|
|
20 |
*/
|
|
21 |
||
22 |
uint64_t drizzle_row_read(drizzle_result_st *result, drizzle_return_t *ret_ptr) |
|
23 |
{
|
|
24 |
if (drizzle_state_none(result->con)) |
|
25 |
{
|
|
26 |
drizzle_state_push(result->con, drizzle_state_row_read); |
|
27 |
drizzle_state_push(result->con, drizzle_state_packet_read); |
|
28 |
}
|
|
29 |
||
30 |
*ret_ptr= drizzle_state_loop(result->con); |
|
31 |
||
32 |
return result->row_current; |
|
33 |
}
|
|
34 |
||
35 |
drizzle_row_t drizzle_row_buffer(drizzle_result_st *result, |
|
36 |
drizzle_return_t *ret_ptr) |
|
37 |
{
|
|
38 |
size_t total; |
|
39 |
drizzle_field_t field; |
|
40 |
drizzle_row_t row; |
|
41 |
||
42 |
if (result->row == NULL) |
|
43 |
{
|
|
44 |
if (drizzle_row_read(result, ret_ptr) == 0 || *ret_ptr != DRIZZLE_RETURN_OK) |
|
45 |
return NULL; |
|
46 |
||
47 |
result->row= malloc((sizeof(drizzle_field_t) + sizeof(size_t)) * |
|
48 |
result->column_count); |
|
49 |
if (result->row == NULL) |
|
50 |
{
|
|
51 |
drizzle_set_error(result->con->drizzle, "drizzle_row_buffer", "malloc"); |
|
52 |
*ret_ptr= DRIZZLE_RETURN_MEMORY; |
|
53 |
return NULL; |
|
54 |
}
|
|
55 |
||
56 |
result->field_sizes= (size_t *)(result->row + result->column_count); |
|
57 |
}
|
|
58 |
||
59 |
while (1) |
|
60 |
{
|
|
61 |
field= drizzle_field_buffer(result, &total, ret_ptr); |
|
62 |
if (*ret_ptr == DRIZZLE_RETURN_ROW_END) |
|
63 |
break; |
|
64 |
if (*ret_ptr != DRIZZLE_RETURN_OK) |
|
65 |
{
|
|
66 |
if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT) |
|
67 |
{
|
|
68 |
free(result->row); |
|
69 |
result->row= NULL; |
|
70 |
result->field_sizes= NULL; |
|
71 |
}
|
|
72 |
||
73 |
return NULL; |
|
74 |
}
|
|
75 |
||
76 |
result->row[result->field_current - 1]= field; |
|
77 |
result->field_sizes[result->field_current - 1]= total; |
|
78 |
}
|
|
79 |
||
80 |
*ret_ptr= DRIZZLE_RETURN_OK; |
|
81 |
row= result->row; |
|
82 |
result->row= NULL; |
|
83 |
||
84 |
return row; |
|
85 |
}
|
|
86 |
||
87 |
void drizzle_row_free(drizzle_result_st *result, drizzle_row_t row) |
|
88 |
{
|
|
89 |
uint16_t x; |
|
90 |
||
91 |
for (x= 0; x < result->column_count; x++) |
|
92 |
drizzle_field_free(row[x]); |
|
93 |
||
94 |
free(row); |
|
95 |
}
|
|
96 |
||
97 |
size_t *drizzle_row_field_sizes(drizzle_result_st *result) |
|
98 |
{
|
|
99 |
return result->field_sizes; |
|
100 |
}
|
|
101 |
||
102 |
drizzle_row_t drizzle_row_next(drizzle_result_st *result) |
|
103 |
{
|
|
104 |
if (result->row_current == result->row_count) |
|
105 |
return NULL; |
|
106 |
||
107 |
result->field_sizes= result->field_sizes_list[result->row_current]; |
|
108 |
result->row_current++; |
|
109 |
return result->row_list[result->row_current - 1]; |
|
110 |
}
|
|
111 |
||
112 |
drizzle_row_t drizzle_row_prev(drizzle_result_st *result) |
|
113 |
{
|
|
114 |
if (result->row_current == 0) |
|
115 |
return NULL; |
|
116 |
||
117 |
result->row_current--; |
|
118 |
result->field_sizes= result->field_sizes_list[result->row_current]; |
|
119 |
return result->row_list[result->row_current]; |
|
120 |
}
|
|
121 |
||
122 |
void drizzle_row_seek(drizzle_result_st *result, uint64_t row) |
|
123 |
{
|
|
124 |
if (row <= result->row_count) |
|
125 |
result->row_current= row; |
|
126 |
}
|
|
127 |
||
128 |
drizzle_row_t drizzle_row_index(drizzle_result_st *result, uint64_t row) |
|
129 |
{
|
|
130 |
if (row >= result->row_count) |
|
131 |
return NULL; |
|
132 |
||
133 |
return result->row_list[row]; |
|
134 |
}
|
|
135 |
||
136 |
uint64_t drizzle_row_current(drizzle_result_st *result) |
|
137 |
{
|
|
138 |
return result->row_current; |
|
139 |
}
|
|
140 |
||
141 |
/*
|
|
142 |
* Server definitions
|
|
143 |
*/
|
|
144 |
||
145 |
drizzle_return_t drizzle_row_write(drizzle_result_st *result) |
|
146 |
{
|
|
147 |
if (drizzle_state_none(result->con)) |
|
148 |
drizzle_state_push(result->con, drizzle_state_row_write); |
|
149 |
||
150 |
return drizzle_state_loop(result->con); |
|
151 |
}
|
|
152 |
||
153 |
/*
|
|
154 |
* Internal state functions.
|
|
155 |
*/
|
|
156 |
||
157 |
drizzle_return_t drizzle_state_row_read(drizzle_con_st *con) |
|
158 |
{
|
|
159 |
drizzle_log_debug(con->drizzle, "drizzle_state_row_read"); |
|
160 |
||
161 |
if (con->packet_size == 5 && con->buffer_ptr[0] == 254) |
|
162 |
{
|
|
1712.1.5
by Monty Taylor
Reverted fix for #571579. |
163 |
if (con->buffer_size < 5) |
164 |
{
|
|
165 |
drizzle_state_push(con, drizzle_state_read); |
|
166 |
return DRIZZLE_RETURN_OK; |
|
167 |
}
|
|
168 |
||
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
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); |
|
173 |
con->buffer_ptr+= 5; |
|
174 |
con->buffer_size-= 5; |
|
175 |
}
|
|
176 |
else if (con->buffer_ptr[0] == 255) |
|
177 |
{
|
|
178 |
drizzle_state_pop(con); |
|
179 |
drizzle_state_push(con, drizzle_state_result_read); |
|
180 |
return DRIZZLE_RETURN_OK; |
|
181 |
}
|
|
182 |
else if (con->result->options & DRIZZLE_RESULT_ROW_BREAK) |
|
183 |
con->result->options&= (drizzle_result_options_t)~DRIZZLE_RESULT_ROW_BREAK; |
|
184 |
else
|
|
185 |
{
|
|
186 |
con->result->row_count++; |
|
187 |
con->result->row_current++; |
|
188 |
con->result->field_current= 0; |
|
189 |
}
|
|
190 |
||
191 |
drizzle_state_pop(con); |
|
192 |
return DRIZZLE_RETURN_OK; |
|
193 |
}
|
|
194 |
||
195 |
drizzle_return_t drizzle_state_row_write(drizzle_con_st *con) |
|
196 |
{
|
|
197 |
uint8_t *start= con->buffer_ptr + con->buffer_size; |
|
198 |
||
199 |
drizzle_log_debug(con->drizzle, "drizzle_state_row_write"); |
|
200 |
||
201 |
/* Flush buffer if there is not enough room. */
|
|
202 |
if (((size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer)) < 4) |
|
203 |
{
|
|
204 |
drizzle_state_push(con, drizzle_state_write); |
|
205 |
return DRIZZLE_RETURN_OK; |
|
206 |
}
|
|
207 |
||
208 |
drizzle_set_byte3(start, con->packet_size); |
|
209 |
start[3]= con->packet_number; |
|
210 |
con->packet_number++; |
|
211 |
||
212 |
con->buffer_size+= 4; |
|
213 |
||
214 |
drizzle_state_pop(con); |
|
215 |
return DRIZZLE_RETURN_OK; |
|
216 |
}
|