~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle-2.0/libdrizzle/row.cc

  • Committer: Mark Atwood
  • Date: 2011-08-11 03:05:03 UTC
  • mfrom: (2385.1.12 refactor4)
  • Revision ID: me@mark.atwood.name-20110811030503-rp9xjihc5x3y0x4q
mergeĀ lp:~olafvdspek/drizzle/refactor4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
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= new drizzle_row_t_type[result->column_count *2];
 
74
    result->field_sizes= reinterpret_cast<size_t *>(result->row + result->column_count);
 
75
  }
 
76
 
 
77
  while (1)
 
78
  {
 
79
    field= drizzle_field_buffer(result, &total, ret_ptr);
 
80
    if (*ret_ptr == DRIZZLE_RETURN_ROW_END)
 
81
      break;
 
82
    if (*ret_ptr != DRIZZLE_RETURN_OK)
 
83
    {
 
84
      if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
 
85
      {
 
86
        delete[] result->row;
 
87
        result->field_sizes= NULL;
 
88
      }
 
89
 
 
90
      return NULL;
 
91
    }
 
92
 
 
93
    result->row[result->field_current - 1]= field;
 
94
    result->field_sizes[result->field_current - 1]= total;
 
95
  }
 
96
 
 
97
  *ret_ptr= DRIZZLE_RETURN_OK;
 
98
  row= result->row;
 
99
  result->row= NULL;
 
100
 
 
101
  return row;
 
102
}
 
103
 
 
104
void drizzle_row_free(drizzle_result_st *result, drizzle_row_t row)
 
105
{
 
106
  uint16_t x;
 
107
 
 
108
  for (x= 0; x < result->column_count; x++)
 
109
      drizzle_field_free(row[x]);
 
110
 
 
111
  delete[] row;
 
112
}
 
113
 
 
114
size_t *drizzle_row_field_sizes(drizzle_result_st *result)
 
115
{
 
116
  return result->field_sizes;
 
117
}
 
118
 
 
119
drizzle_row_t drizzle_row_next(drizzle_result_st *result)
 
120
{
 
121
  if (result->row_current == result->row_count)
 
122
    return NULL;
 
123
 
 
124
  result->field_sizes= result->field_sizes_list->at(static_cast<size_t>(result->row_current));
 
125
  result->row_current++;
 
126
  return result->row_list->at(static_cast<size_t>(result->row_current) - 1);
 
127
}
 
128
 
 
129
drizzle_row_t drizzle_row_prev(drizzle_result_st *result)
 
130
{
 
131
  if (result->row_current == 0)
 
132
    return NULL;
 
133
 
 
134
  result->row_current--;
 
135
  result->field_sizes= result->field_sizes_list->at(static_cast<size_t>(result->row_current));
 
136
  return result->row_list->at(static_cast<size_t>(result->row_current));
 
137
}
 
138
 
 
139
void drizzle_row_seek(drizzle_result_st *result, uint64_t row)
 
140
{
 
141
  if (row <= result->row_count)
 
142
    result->row_current= row;
 
143
}
 
144
 
 
145
drizzle_row_t drizzle_row_index(drizzle_result_st *result, uint64_t row)
 
146
{
 
147
  if (row >= result->row_count)
 
148
    return NULL;
 
149
 
 
150
  return (*result->row_list)[static_cast<size_t>(row)];
 
151
}
 
152
 
 
153
uint64_t drizzle_row_current(drizzle_result_st *result)
 
154
{
 
155
  return result->row_current;
 
156
}
 
157
 
 
158
/*
 
159
 * Server definitions
 
160
 */
 
161
 
 
162
drizzle_return_t drizzle_row_write(drizzle_result_st *result)
 
163
{
 
164
  if (drizzle_state_none(result->con))
 
165
    drizzle_state_push(result->con, drizzle_state_row_write);
 
166
 
 
167
  return drizzle_state_loop(result->con);
 
168
}
 
169
 
 
170
/*
 
171
 * Internal state functions.
 
172
 */
 
173
 
 
174
drizzle_return_t drizzle_state_row_read(drizzle_con_st *con)
 
175
{
 
176
  drizzle_log_debug(con->drizzle, "drizzle_state_row_read");
 
177
 
 
178
  if (con->packet_size != 0 && con->buffer_size < con->packet_size && 
 
179
    con->buffer_size < 5)
 
180
  {
 
181
    drizzle_state_push(con, drizzle_state_read);
 
182
    return DRIZZLE_RETURN_OK;
 
183
  }
 
184
 
 
185
  if (con->packet_size == 5 && con->buffer_ptr[0] == 254)
 
186
  {
 
187
    /* Got EOF packet, no more rows. */
 
188
    con->result->row_current= 0;
 
189
    con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 1);
 
190
    con->status= (drizzle_con_status_t)drizzle_get_byte2(con->buffer_ptr + 3);
 
191
    con->buffer_ptr+= 5;
 
192
    con->buffer_size-= 5;
 
193
  }
 
194
  else if (con->buffer_ptr[0] == 255)
 
195
  {
 
196
    drizzle_state_pop(con);
 
197
    drizzle_state_push(con, drizzle_state_result_read);
 
198
    return DRIZZLE_RETURN_OK;
 
199
  }
 
200
  else if (con->result->options & DRIZZLE_RESULT_ROW_BREAK)
 
201
    con->result->options&= ~DRIZZLE_RESULT_ROW_BREAK;
 
202
  else
 
203
  {
 
204
    con->result->row_count++;
 
205
    con->result->row_current++;
 
206
    con->result->field_current= 0;
 
207
  }
 
208
 
 
209
  drizzle_state_pop(con);
 
210
  return DRIZZLE_RETURN_OK;
 
211
}
 
212
 
 
213
drizzle_return_t drizzle_state_row_write(drizzle_con_st *con)
 
214
{
 
215
  uint8_t *start= con->buffer_ptr + con->buffer_size;
 
216
 
 
217
  drizzle_log_debug(con->drizzle, "drizzle_state_row_write");
 
218
 
 
219
  /* Flush buffer if there is not enough room. */
 
220
  if (((size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer)) < 4)
 
221
  {
 
222
    drizzle_state_push(con, drizzle_state_write);
 
223
    return DRIZZLE_RETURN_OK;
 
224
  }
 
225
 
 
226
  drizzle_set_byte3(start, con->packet_size);
 
227
  start[3]= con->packet_number;
 
228
  con->packet_number++;
 
229
 
 
230
  con->buffer_size+= 4;
 
231
 
 
232
  drizzle_state_pop(con);
 
233
  return DRIZZLE_RETURN_OK;
 
234
}