~drizzle-trunk/drizzle/development

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
 *
1971.2.1 by kalebral at gmail
update files that did not have license or had incorrect license structure
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions are
9
 * met:
10
 *
11
 *     * Redistributions of source code must retain the above copyright
12
 * notice, this list of conditions and the following disclaimer.
13
 *
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
17
 * distribution.
18
 *
19
 *     * The names of its contributors may not be used to endorse or
20
 * promote products derived from this software without specific prior
21
 * written permission.
22
 *
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.
34
 *
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
35
 */
36
37
/**
38
 * @file
39
 * @brief Row definitions
40
 */
41
42
#include "common.h"
43
44
/*
45
 * Client definitions
46
 */
47
48
uint64_t drizzle_row_read(drizzle_result_st *result, drizzle_return_t *ret_ptr)
49
{
50
  if (drizzle_state_none(result->con))
51
  {
52
    drizzle_state_push(result->con, drizzle_state_row_read);
53
    drizzle_state_push(result->con, drizzle_state_packet_read);
54
  }
55
56
  *ret_ptr= drizzle_state_loop(result->con);
57
58
  return result->row_current;
59
}
60
61
drizzle_row_t drizzle_row_buffer(drizzle_result_st *result,
62
                                 drizzle_return_t *ret_ptr)
63
{
64
  size_t total;
65
  drizzle_field_t field;
66
  drizzle_row_t row;
67
68
  if (result->row == NULL)
69
  {
70
    if (drizzle_row_read(result, ret_ptr) == 0 || *ret_ptr != DRIZZLE_RETURN_OK)
71
      return NULL;
72
73
    result->row= malloc((sizeof(drizzle_field_t) + sizeof(size_t)) *
74
                        result->column_count);
75
    if (result->row == NULL)
76
    {
77
      drizzle_set_error(result->con->drizzle, "drizzle_row_buffer", "malloc");
78
      *ret_ptr= DRIZZLE_RETURN_MEMORY;
79
      return NULL;
80
    }
81
82
    result->field_sizes= (size_t *)(result->row + result->column_count);
83
  }
84
85
  while (1)
86
  {
87
    field= drizzle_field_buffer(result, &total, ret_ptr);
88
    if (*ret_ptr == DRIZZLE_RETURN_ROW_END)
89
      break;
90
    if (*ret_ptr != DRIZZLE_RETURN_OK)
91
    {
92
      if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
93
      {
94
        free(result->row);
95
        result->row= NULL;
96
        result->field_sizes= NULL;
97
      }
98
99
      return NULL;
100
    }
101
102
    result->row[result->field_current - 1]= field;
103
    result->field_sizes[result->field_current - 1]= total;
104
  }
105
106
  *ret_ptr= DRIZZLE_RETURN_OK;
107
  row= result->row;
108
  result->row= NULL;
109
110
  return row;
111
}
112
113
void drizzle_row_free(drizzle_result_st *result, drizzle_row_t row)
114
{
115
  uint16_t x;
116
117
  for (x= 0; x < result->column_count; x++)
118
      drizzle_field_free(row[x]);
119
120
  free(row);
121
}
122
123
size_t *drizzle_row_field_sizes(drizzle_result_st *result)
124
{
125
  return result->field_sizes;
126
}
127
128
drizzle_row_t drizzle_row_next(drizzle_result_st *result)
129
{
130
  if (result->row_current == result->row_count)
131
    return NULL;
132
133
  result->field_sizes= result->field_sizes_list[result->row_current];
134
  result->row_current++;
135
  return result->row_list[result->row_current - 1];
136
}
137
138
drizzle_row_t drizzle_row_prev(drizzle_result_st *result)
139
{
140
  if (result->row_current == 0)
141
    return NULL;
142
143
  result->row_current--;
144
  result->field_sizes= result->field_sizes_list[result->row_current];
145
  return result->row_list[result->row_current];
146
}
147
148
void drizzle_row_seek(drizzle_result_st *result, uint64_t row)
149
{
150
  if (row <= result->row_count)
151
    result->row_current= row;
152
}
153
154
drizzle_row_t drizzle_row_index(drizzle_result_st *result, uint64_t row)
155
{
156
  if (row >= result->row_count)
157
    return NULL;
158
159
  return result->row_list[row];
160
}
161
162
uint64_t drizzle_row_current(drizzle_result_st *result)
163
{
164
  return result->row_current;
165
}
166
167
/*
168
 * Server definitions
169
 */
170
171
drizzle_return_t drizzle_row_write(drizzle_result_st *result)
172
{
173
  if (drizzle_state_none(result->con))
174
    drizzle_state_push(result->con, drizzle_state_row_write);
175
176
  return drizzle_state_loop(result->con);
177
}
178
179
/*
180
 * Internal state functions.
181
 */
182
183
drizzle_return_t drizzle_state_row_read(drizzle_con_st *con)
184
{
185
  drizzle_log_debug(con->drizzle, "drizzle_state_row_read");
186
1843.3.2 by Andrew Hutchings
Add David Mikulec's fix in bug#643772 which fixes the problem which caused the revert of libdrizzle rev.147
187
  if (con->packet_size != 0 && con->buffer_size < con->packet_size && 
188
    con->buffer_size < 5)
1843.3.1 by Andrew Hutchings
Port libdrizzle rev.147:
189
  {
190
    drizzle_state_push(con, drizzle_state_read);
191
    return DRIZZLE_RETURN_OK;
192
  }
193
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
194
  if (con->packet_size == 5 && con->buffer_ptr[0] == 254)
195
  {
196
    /* Got EOF packet, no more rows. */
197
    con->result->row_current= 0;
198
    con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 1);
199
    con->status= drizzle_get_byte2(con->buffer_ptr + 3);
200
    con->buffer_ptr+= 5;
201
    con->buffer_size-= 5;
202
  }
203
  else if (con->buffer_ptr[0] == 255)
204
  {
205
    drizzle_state_pop(con);
206
    drizzle_state_push(con, drizzle_state_result_read);
207
    return DRIZZLE_RETURN_OK;
208
  }
209
  else if (con->result->options & DRIZZLE_RESULT_ROW_BREAK)
210
    con->result->options&= (drizzle_result_options_t)~DRIZZLE_RESULT_ROW_BREAK;
211
  else
212
  {
213
    con->result->row_count++;
214
    con->result->row_current++;
215
    con->result->field_current= 0;
216
  }
217
218
  drizzle_state_pop(con);
219
  return DRIZZLE_RETURN_OK;
220
}
221
222
drizzle_return_t drizzle_state_row_write(drizzle_con_st *con)
223
{
224
  uint8_t *start= con->buffer_ptr + con->buffer_size;
225
226
  drizzle_log_debug(con->drizzle, "drizzle_state_row_write");
227
228
  /* Flush buffer if there is not enough room. */
229
  if (((size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer)) < 4)
230
  {
231
    drizzle_state_push(con, drizzle_state_write);
232
    return DRIZZLE_RETURN_OK;
233
  }
234
235
  drizzle_set_byte3(start, con->packet_size);
236
  start[3]= con->packet_number;
237
  con->packet_number++;
238
239
  con->buffer_size+= 4;
240
241
  drizzle_state_pop(con);
242
  return DRIZZLE_RETURN_OK;
243
}