~linuxjedi/libdrizzle/5.1-optimize

« back to all changes in this revision

Viewing changes to libdrizzle/row.c

  • Committer: Andrew Hutchings
  • Date: 2012-09-03 18:38:34 UTC
  • Revision ID: git-v1:e936672f2f2e4af2b54de08ebf06d53ca1dc6872
The "it compiles!" version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: 
 
2
 *
 
3
 *  Drizzle Client & Protocol Library
 
4
 *
 
5
 * Copyright (C) 2008 Eric Day (eday@oddments.org)
 
6
 * All rights reserved.
 
7
 *
 
8
 * Redistribution and use in source and binary forms, with or without
 
9
 * modification, are permitted provided that the following conditions are
 
10
 * met:
 
11
 *
 
12
 *     * Redistributions of source code must retain the above copyright
 
13
 * notice, this list of conditions and the following disclaimer.
 
14
 *
 
15
 *     * Redistributions in binary form must reproduce the above
 
16
 * copyright notice, this list of conditions and the following disclaimer
 
17
 * in the documentation and/or other materials provided with the
 
18
 * distribution.
 
19
 *
 
20
 *     * The names of its contributors may not be used to endorse or
 
21
 * promote products derived from this software without specific prior
 
22
 * written permission.
 
23
 *
 
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
25
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
26
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
27
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
28
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
29
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
30
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
31
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
32
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
33
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
34
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
35
 *
 
36
 */
 
37
 
 
38
/**
 
39
 * @file
 
40
 * @brief Row definitions
 
41
 */
 
42
 
 
43
#include <libdrizzle/common.h>
 
44
 
 
45
/*
 
46
 * Client definitions
 
47
 */
 
48
 
 
49
uint64_t drizzle_row_read(drizzle_result_st *result, drizzle_return_t *ret_ptr)
 
50
{
 
51
  drizzle_return_t unused_ret;
 
52
  if (ret_ptr == NULL)
 
53
  {
 
54
    ret_ptr= &unused_ret;
 
55
  }
 
56
 
 
57
  if (result == NULL)
 
58
  {
 
59
    *ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
 
60
    return 0;
 
61
  }
 
62
 
 
63
  if ((result->column_current != result->column_count) && (!(result->options & DRIZZLE_RESULT_BUFFER_COLUMN)))
 
64
  {
 
65
    drizzle_set_error(result->con->drizzle, "drizzle_row_read", "cannot retrieve rows until all columns are retrieved");
 
66
    *ret_ptr= DRIZZLE_RETURN_NOT_READY;
 
67
    return 0;
 
68
  }
 
69
 
 
70
  if (drizzle_state_none(result->con))
 
71
  {
 
72
    drizzle_state_push(result->con, drizzle_state_row_read);
 
73
    drizzle_state_push(result->con, drizzle_state_packet_read);
 
74
  }
 
75
 
 
76
  *ret_ptr= drizzle_state_loop(result->con);
 
77
 
 
78
  return result->row_current;
 
79
}
 
80
 
 
81
drizzle_row_t drizzle_row_buffer(drizzle_result_st *result,
 
82
                                 drizzle_return_t *ret_ptr)
 
83
{
 
84
  drizzle_return_t unused_ret;
 
85
  if (ret_ptr == NULL)
 
86
  {
 
87
    ret_ptr= &unused_ret;
 
88
  }
 
89
 
 
90
  if (result == NULL)
 
91
  {
 
92
    *ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
 
93
    return 0;
 
94
  }
 
95
 
 
96
  size_t total;
 
97
  drizzle_field_t field;
 
98
  drizzle_row_t row;
 
99
 
 
100
  if (result->row == NULL)
 
101
  {
 
102
    if (drizzle_row_read(result, ret_ptr) == 0 || *ret_ptr != DRIZZLE_RETURN_OK)
 
103
    {
 
104
      return NULL;
 
105
    }
 
106
 
 
107
    result->row= (drizzle_row_t)realloc(NULL, (sizeof(drizzle_field_t) + sizeof(size_t)) * result->column_count);
 
108
    if (result->row == NULL)
 
109
    {
 
110
      drizzle_set_error(result->con->drizzle, __func__, "Failed to allocate.");
 
111
      *ret_ptr= DRIZZLE_RETURN_MEMORY;
 
112
      return NULL;
 
113
    }
 
114
 
 
115
    result->field_sizes= (size_t *)(result->row + result->column_count);
 
116
  }
 
117
 
 
118
  while (1)
 
119
  {
 
120
    field= drizzle_field_buffer(result, &total, ret_ptr);
 
121
    if (*ret_ptr == DRIZZLE_RETURN_ROW_END)
 
122
      break;
 
123
 
 
124
    if (*ret_ptr != DRIZZLE_RETURN_OK)
 
125
    {
 
126
      if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
 
127
      {
 
128
        free(result->row);
 
129
        result->row= NULL;
 
130
        result->field_sizes= NULL;
 
131
      }
 
132
 
 
133
      return NULL;
 
134
    }
 
135
 
 
136
    result->row[result->field_current - 1]= field;
 
137
    result->field_sizes[result->field_current - 1]= total;
 
138
  }
 
139
 
 
140
  *ret_ptr= DRIZZLE_RETURN_OK;
 
141
  row= result->row;
 
142
  result->row= NULL;
 
143
 
 
144
  return row;
 
145
}
 
146
 
 
147
void drizzle_row_free(drizzle_result_st *result, drizzle_row_t row)
 
148
{
 
149
  uint16_t x;
 
150
 
 
151
  if (result == NULL)
 
152
  {
 
153
    return;
 
154
  }
 
155
 
 
156
  for (x= 0; x < result->column_count; x++)
 
157
  {
 
158
    drizzle_field_free(row[x]);
 
159
  }
 
160
 
 
161
  free(row);
 
162
}
 
163
 
 
164
size_t *drizzle_row_field_sizes(drizzle_result_st *result)
 
165
{
 
166
  if (result == NULL)
 
167
  {
 
168
    return NULL;
 
169
  }
 
170
 
 
171
  return result->field_sizes;
 
172
}
 
173
 
 
174
drizzle_row_t drizzle_row_next(drizzle_result_st *result)
 
175
{
 
176
  if (result == NULL)
 
177
  {
 
178
    return NULL;
 
179
  }
 
180
 
 
181
  if (result->row_current == result->row_count)
 
182
  {
 
183
    return NULL;
 
184
  }
 
185
 
 
186
  result->field_sizes= result->field_sizes_list[result->row_current];
 
187
  result->row_current++;
 
188
  return result->row_list[result->row_current - 1];
 
189
}
 
190
 
 
191
drizzle_row_t drizzle_row_prev(drizzle_result_st *result)
 
192
{
 
193
  if (result == NULL)
 
194
  {
 
195
    return NULL;
 
196
  }
 
197
 
 
198
  if (result->row_current == 0)
 
199
    return NULL;
 
200
 
 
201
  result->row_current--;
 
202
  result->field_sizes= result->field_sizes_list[result->row_current];
 
203
  return result->row_list[result->row_current];
 
204
}
 
205
 
 
206
void drizzle_row_seek(drizzle_result_st *result, uint64_t row)
 
207
{
 
208
  if (result == NULL)
 
209
  {
 
210
    return;
 
211
  }
 
212
 
 
213
  if (row <= result->row_count)
 
214
    result->row_current= row;
 
215
}
 
216
 
 
217
drizzle_row_t drizzle_row_index(drizzle_result_st *result, uint64_t row)
 
218
{
 
219
  if (result == NULL)
 
220
  {
 
221
    return NULL;
 
222
  }
 
223
 
 
224
  if (row >= result->row_count)
 
225
    return NULL;
 
226
 
 
227
  return result->row_list[row];
 
228
}
 
229
 
 
230
uint64_t drizzle_row_current(drizzle_result_st *result)
 
231
{
 
232
  if (result == NULL)
 
233
  {
 
234
    return 0;
 
235
  }
 
236
 
 
237
  return result->row_current;
 
238
}
 
239
 
 
240
/*
 
241
 * Server definitions
 
242
 */
 
243
 
 
244
drizzle_return_t drizzle_row_write(drizzle_result_st *result)
 
245
{
 
246
  if (result == NULL)
 
247
  {
 
248
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
249
  }
 
250
 
 
251
  if (drizzle_state_none(result->con))
 
252
  {
 
253
    drizzle_state_push(result->con, drizzle_state_row_write);
 
254
  }
 
255
 
 
256
  return drizzle_state_loop(result->con);
 
257
}
 
258
 
 
259
/*
 
260
 * Internal state functions.
 
261
 */
 
262
 
 
263
drizzle_return_t drizzle_state_row_read(drizzle_con_st *con)
 
264
{
 
265
  if (con == NULL)
 
266
  {
 
267
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
268
  }
 
269
 
 
270
  drizzle_log_debug(con->drizzle, "drizzle_state_row_read");
 
271
 
 
272
  if (con->packet_size != 0 && con->buffer_size < con->packet_size && 
 
273
    con->buffer_size < 5)
 
274
  {
 
275
    drizzle_state_push(con, drizzle_state_read);
 
276
    return DRIZZLE_RETURN_OK;
 
277
  }
 
278
 
 
279
  if (con->packet_size == 5 && con->buffer_ptr[0] == 254)
 
280
  {
 
281
    /* Got EOF packet, no more rows. */
 
282
    con->result->row_current= 0;
 
283
    con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 1);
 
284
    con->status= drizzle_get_byte2(con->buffer_ptr + 3);
 
285
    con->buffer_ptr+= 5;
 
286
    con->buffer_size-= 5;
 
287
  }
 
288
  else if (con->buffer_ptr[0] == 255)
 
289
  {
 
290
    drizzle_state_pop(con);
 
291
    drizzle_state_push(con, drizzle_state_result_read);
 
292
    return DRIZZLE_RETURN_OK;
 
293
  }
 
294
  else if (con->result->options & DRIZZLE_RESULT_ROW_BREAK)
 
295
  {
 
296
    con->result->options&= ~DRIZZLE_RESULT_ROW_BREAK;
 
297
  }
 
298
  else
 
299
  {
 
300
    con->result->row_count++;
 
301
    con->result->row_current++;
 
302
    con->result->field_current= 0;
 
303
  }
 
304
 
 
305
  drizzle_state_pop(con);
 
306
  return DRIZZLE_RETURN_OK;
 
307
}
 
308
 
 
309
drizzle_return_t drizzle_state_row_write(drizzle_con_st *con)
 
310
{
 
311
  if (con == NULL)
 
312
  {
 
313
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
314
  }
 
315
 
 
316
  uint8_t *start= con->buffer_ptr + con->buffer_size;
 
317
 
 
318
  drizzle_log_debug(con->drizzle, "drizzle_state_row_write");
 
319
 
 
320
  /* Flush buffer if there is not enough room. */
 
321
  if (((size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer)) < 4)
 
322
  {
 
323
    drizzle_state_push(con, drizzle_state_write);
 
324
    return DRIZZLE_RETURN_OK;
 
325
  }
 
326
 
 
327
  drizzle_set_byte3(start, con->packet_size);
 
328
  start[3]= con->packet_number;
 
329
  con->packet_number++;
 
330
 
 
331
  con->buffer_size+= 4;
 
332
 
 
333
  drizzle_state_pop(con);
 
334
  return DRIZZLE_RETURN_OK;
 
335
}