~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

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