~drizzle-trunk/drizzle/development

2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
1
/*
2
 * Drizzle Client & Protocol Library
3
 *
4
 * Copyright (C) 2008 Eric Day (eday@oddments.org)
5
 * All rights reserved.
6
 *
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
 *
35
 */
36
37
/**
38
 * @file
39
 * @brief Row definitions
40
 */
41
2449.1.2 by Brian Aker
Additional fixes for libdrizzle.
42
#include <libdrizzle-2.0/common.h>
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
43
44
/*
45
 * Client definitions
46
 */
47
48
uint64_t drizzle_row_read(drizzle_result_st *result, drizzle_return_t *ret_ptr)
49
{
2411.1.1 by Andrew Hutchings
Add function drizzle_column_skip_all
50
  if ((result->column_current != result->column_count) && (!(result->options & DRIZZLE_RESULT_BUFFER_COLUMN)))
51
  {
52
    drizzle_set_error(result->con->drizzle, "drizzle_row_read", "cannot retrieve rows until all columns are retrieved");
53
    *ret_ptr= DRIZZLE_RETURN_NOT_READY;
54
    return 0;
55
  }
56
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
57
  if (drizzle_state_none(result->con))
58
  {
59
    drizzle_state_push(result->con, drizzle_state_row_read);
60
    drizzle_state_push(result->con, drizzle_state_packet_read);
61
  }
62
63
  *ret_ptr= drizzle_state_loop(result->con);
64
65
  return result->row_current;
66
}
67
68
drizzle_row_t drizzle_row_buffer(drizzle_result_st *result,
69
                                 drizzle_return_t *ret_ptr)
70
{
71
  size_t total;
72
  drizzle_row_t row;
73
2461.1.2 by Brian Aker
Pass through refactoring.
74
  if (result == NULL)
75
  {
76
    return drizzle_row_t();
77
  }
78
79
  drizzle_return_t unused;
80
  if (ret_ptr == NULL)
81
  {
82
    ret_ptr= &unused;
83
  }
84
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
85
  if (result->row == NULL)
86
  {
87
    if (drizzle_row_read(result, ret_ptr) == 0 || *ret_ptr != DRIZZLE_RETURN_OK)
2461.1.2 by Brian Aker
Pass through refactoring.
88
    {
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
89
      return NULL;
2461.1.2 by Brian Aker
Pass through refactoring.
90
    }
91
92
    result->row= new (std::nothrow) drizzle_row_t_type[result->column_count *2];
93
94
    if (result->row == NULL)
95
    {
96
      *ret_ptr= DRIZZLE_RETURN_MEMORY;
97
      return drizzle_row_t();
98
    }
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
99
    result->field_sizes= reinterpret_cast<size_t *>(result->row + result->column_count);
100
  }
101
102
  while (1)
103
  {
2461.1.2 by Brian Aker
Pass through refactoring.
104
    drizzle_field_t field= drizzle_field_buffer(result, &total, ret_ptr);
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
105
    if (*ret_ptr == DRIZZLE_RETURN_ROW_END)
2461.1.2 by Brian Aker
Pass through refactoring.
106
    {
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
107
      break;
2461.1.2 by Brian Aker
Pass through refactoring.
108
    }
109
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
110
    if (*ret_ptr != DRIZZLE_RETURN_OK)
111
    {
112
      if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
113
      {
114
        delete[] result->row;
115
        result->field_sizes= NULL;
116
      }
117
118
      return NULL;
119
    }
120
121
    result->row[result->field_current - 1]= field;
122
    result->field_sizes[result->field_current - 1]= total;
123
  }
124
125
  *ret_ptr= DRIZZLE_RETURN_OK;
126
  row= result->row;
127
  result->row= NULL;
128
129
  return row;
130
}
131
132
void drizzle_row_free(drizzle_result_st *result, drizzle_row_t row)
133
{
134
  uint16_t x;
135
136
  for (x= 0; x < result->column_count; x++)
137
      drizzle_field_free(row[x]);
138
139
  delete[] row;
140
}
141
142
size_t *drizzle_row_field_sizes(drizzle_result_st *result)
143
{
144
  return result->field_sizes;
145
}
146
147
drizzle_row_t drizzle_row_next(drizzle_result_st *result)
148
{
149
  if (result->row_current == result->row_count)
150
    return NULL;
151
2265.1.1 by Monty Taylor
Fixed the windows build related to the new libdrizzle-2.0 stuff.
152
  result->field_sizes= result->field_sizes_list->at(static_cast<size_t>(result->row_current));
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
153
  result->row_current++;
2265.1.1 by Monty Taylor
Fixed the windows build related to the new libdrizzle-2.0 stuff.
154
  return result->row_list->at(static_cast<size_t>(result->row_current) - 1);
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
155
}
156
157
drizzle_row_t drizzle_row_prev(drizzle_result_st *result)
158
{
159
  if (result->row_current == 0)
160
    return NULL;
161
162
  result->row_current--;
2265.1.1 by Monty Taylor
Fixed the windows build related to the new libdrizzle-2.0 stuff.
163
  result->field_sizes= result->field_sizes_list->at(static_cast<size_t>(result->row_current));
164
  return result->row_list->at(static_cast<size_t>(result->row_current));
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
165
}
166
167
void drizzle_row_seek(drizzle_result_st *result, uint64_t row)
168
{
169
  if (row <= result->row_count)
170
    result->row_current= row;
171
}
172
173
drizzle_row_t drizzle_row_index(drizzle_result_st *result, uint64_t row)
174
{
175
  if (row >= result->row_count)
176
    return NULL;
177
2265.1.1 by Monty Taylor
Fixed the windows build related to the new libdrizzle-2.0 stuff.
178
  return (*result->row_list)[static_cast<size_t>(row)];
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
179
}
180
181
uint64_t drizzle_row_current(drizzle_result_st *result)
182
{
183
  return result->row_current;
184
}
185
186
/*
187
 * Server definitions
188
 */
189
190
drizzle_return_t drizzle_row_write(drizzle_result_st *result)
191
{
192
  if (drizzle_state_none(result->con))
193
    drizzle_state_push(result->con, drizzle_state_row_write);
194
195
  return drizzle_state_loop(result->con);
196
}
197
198
/*
199
 * Internal state functions.
200
 */
201
202
drizzle_return_t drizzle_state_row_read(drizzle_con_st *con)
203
{
204
  drizzle_log_debug(con->drizzle, "drizzle_state_row_read");
205
206
  if (con->packet_size != 0 && con->buffer_size < con->packet_size && 
207
    con->buffer_size < 5)
208
  {
209
    drizzle_state_push(con, drizzle_state_read);
210
    return DRIZZLE_RETURN_OK;
211
  }
212
213
  if (con->packet_size == 5 && con->buffer_ptr[0] == 254)
214
  {
215
    /* Got EOF packet, no more rows. */
216
    con->result->row_current= 0;
217
    con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 1);
218
    con->status= (drizzle_con_status_t)drizzle_get_byte2(con->buffer_ptr + 3);
219
    con->buffer_ptr+= 5;
220
    con->buffer_size-= 5;
221
  }
222
  else if (con->buffer_ptr[0] == 255)
223
  {
224
    drizzle_state_pop(con);
225
    drizzle_state_push(con, drizzle_state_result_read);
226
    return DRIZZLE_RETURN_OK;
227
  }
228
  else if (con->result->options & DRIZZLE_RESULT_ROW_BREAK)
2461.1.3 by Brian Aker
Small correction to style.
229
  {
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
230
    con->result->options&= ~DRIZZLE_RESULT_ROW_BREAK;
2461.1.3 by Brian Aker
Small correction to style.
231
  }
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
232
  else
233
  {
234
    con->result->row_count++;
235
    con->result->row_current++;
236
    con->result->field_current= 0;
237
  }
238
239
  drizzle_state_pop(con);
2461.1.3 by Brian Aker
Small correction to style.
240
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
241
  return DRIZZLE_RETURN_OK;
242
}
243
244
drizzle_return_t drizzle_state_row_write(drizzle_con_st *con)
245
{
246
  uint8_t *start= con->buffer_ptr + con->buffer_size;
247
248
  drizzle_log_debug(con->drizzle, "drizzle_state_row_write");
249
250
  /* Flush buffer if there is not enough room. */
251
  if (((size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer)) < 4)
252
  {
253
    drizzle_state_push(con, drizzle_state_write);
254
    return DRIZZLE_RETURN_OK;
255
  }
256
257
  drizzle_set_byte3(start, con->packet_size);
258
  start[3]= con->packet_number;
259
  con->packet_number++;
260
261
  con->buffer_size+= 4;
262
263
  drizzle_state_pop(con);
2461.1.3 by Brian Aker
Small correction to style.
264
2244.1.1 by Monty Taylor
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.
265
  return DRIZZLE_RETURN_OK;
266
}