~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
 *
7
 * Use and distribution licensed under the BSD license.  See
1799.2.3 by Monty Taylor
Reference root BSD copying file.
8
 * the COPYING.BSD file in the root source directory for full text.
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
9
 */
10
11
/**
12
 * @file
13
 * @brief Row definitions
14
 */
15
16
#include "common.h"
17
18
/*
19
 * Client definitions
20
 */
21
22
uint64_t drizzle_row_read(drizzle_result_st *result, drizzle_return_t *ret_ptr)
23
{
24
  if (drizzle_state_none(result->con))
25
  {
26
    drizzle_state_push(result->con, drizzle_state_row_read);
27
    drizzle_state_push(result->con, drizzle_state_packet_read);
28
  }
29
30
  *ret_ptr= drizzle_state_loop(result->con);
31
32
  return result->row_current;
33
}
34
35
drizzle_row_t drizzle_row_buffer(drizzle_result_st *result,
36
                                 drizzle_return_t *ret_ptr)
37
{
38
  size_t total;
39
  drizzle_field_t field;
40
  drizzle_row_t row;
41
42
  if (result->row == NULL)
43
  {
44
    if (drizzle_row_read(result, ret_ptr) == 0 || *ret_ptr != DRIZZLE_RETURN_OK)
45
      return NULL;
46
47
    result->row= malloc((sizeof(drizzle_field_t) + sizeof(size_t)) *
48
                        result->column_count);
49
    if (result->row == NULL)
50
    {
51
      drizzle_set_error(result->con->drizzle, "drizzle_row_buffer", "malloc");
52
      *ret_ptr= DRIZZLE_RETURN_MEMORY;
53
      return NULL;
54
    }
55
56
    result->field_sizes= (size_t *)(result->row + result->column_count);
57
  }
58
59
  while (1)
60
  {
61
    field= drizzle_field_buffer(result, &total, ret_ptr);
62
    if (*ret_ptr == DRIZZLE_RETURN_ROW_END)
63
      break;
64
    if (*ret_ptr != DRIZZLE_RETURN_OK)
65
    {
66
      if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
67
      {
68
        free(result->row);
69
        result->row= NULL;
70
        result->field_sizes= NULL;
71
      }
72
73
      return NULL;
74
    }
75
76
    result->row[result->field_current - 1]= field;
77
    result->field_sizes[result->field_current - 1]= total;
78
  }
79
80
  *ret_ptr= DRIZZLE_RETURN_OK;
81
  row= result->row;
82
  result->row= NULL;
83
84
  return row;
85
}
86
87
void drizzle_row_free(drizzle_result_st *result, drizzle_row_t row)
88
{
89
  uint16_t x;
90
91
  for (x= 0; x < result->column_count; x++)
92
      drizzle_field_free(row[x]);
93
94
  free(row);
95
}
96
97
size_t *drizzle_row_field_sizes(drizzle_result_st *result)
98
{
99
  return result->field_sizes;
100
}
101
102
drizzle_row_t drizzle_row_next(drizzle_result_st *result)
103
{
104
  if (result->row_current == result->row_count)
105
    return NULL;
106
107
  result->field_sizes= result->field_sizes_list[result->row_current];
108
  result->row_current++;
109
  return result->row_list[result->row_current - 1];
110
}
111
112
drizzle_row_t drizzle_row_prev(drizzle_result_st *result)
113
{
114
  if (result->row_current == 0)
115
    return NULL;
116
117
  result->row_current--;
118
  result->field_sizes= result->field_sizes_list[result->row_current];
119
  return result->row_list[result->row_current];
120
}
121
122
void drizzle_row_seek(drizzle_result_st *result, uint64_t row)
123
{
124
  if (row <= result->row_count)
125
    result->row_current= row;
126
}
127
128
drizzle_row_t drizzle_row_index(drizzle_result_st *result, uint64_t row)
129
{
130
  if (row >= result->row_count)
131
    return NULL;
132
133
  return result->row_list[row];
134
}
135
136
uint64_t drizzle_row_current(drizzle_result_st *result)
137
{
138
  return result->row_current;
139
}
140
141
/*
142
 * Server definitions
143
 */
144
145
drizzle_return_t drizzle_row_write(drizzle_result_st *result)
146
{
147
  if (drizzle_state_none(result->con))
148
    drizzle_state_push(result->con, drizzle_state_row_write);
149
150
  return drizzle_state_loop(result->con);
151
}
152
153
/*
154
 * Internal state functions.
155
 */
156
157
drizzle_return_t drizzle_state_row_read(drizzle_con_st *con)
158
{
159
  drizzle_log_debug(con->drizzle, "drizzle_state_row_read");
160
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
161
  if (con->packet_size != 0 && con->buffer_size < con->packet_size && 
162
    con->buffer_size < 5)
1843.3.1 by Andrew Hutchings
Port libdrizzle rev.147:
163
  {
164
    drizzle_state_push(con, drizzle_state_read);
165
    return DRIZZLE_RETURN_OK;
166
  }
167
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
168
  if (con->packet_size == 5 && con->buffer_ptr[0] == 254)
169
  {
170
    /* Got EOF packet, no more rows. */
171
    con->result->row_current= 0;
172
    con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 1);
173
    con->status= drizzle_get_byte2(con->buffer_ptr + 3);
174
    con->buffer_ptr+= 5;
175
    con->buffer_size-= 5;
176
  }
177
  else if (con->buffer_ptr[0] == 255)
178
  {
179
    drizzle_state_pop(con);
180
    drizzle_state_push(con, drizzle_state_result_read);
181
    return DRIZZLE_RETURN_OK;
182
  }
183
  else if (con->result->options & DRIZZLE_RESULT_ROW_BREAK)
184
    con->result->options&= (drizzle_result_options_t)~DRIZZLE_RESULT_ROW_BREAK;
185
  else
186
  {
187
    con->result->row_count++;
188
    con->result->row_current++;
189
    con->result->field_current= 0;
190
  }
191
192
  drizzle_state_pop(con);
193
  return DRIZZLE_RETURN_OK;
194
}
195
196
drizzle_return_t drizzle_state_row_write(drizzle_con_st *con)
197
{
198
  uint8_t *start= con->buffer_ptr + con->buffer_size;
199
200
  drizzle_log_debug(con->drizzle, "drizzle_state_row_write");
201
202
  /* Flush buffer if there is not enough room. */
203
  if (((size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer)) < 4)
204
  {
205
    drizzle_state_push(con, drizzle_state_write);
206
    return DRIZZLE_RETURN_OK;
207
  }
208
209
  drizzle_set_byte3(start, con->packet_size);
210
  start[3]= con->packet_number;
211
  con->packet_number++;
212
213
  con->buffer_size+= 4;
214
215
  drizzle_state_pop(con);
216
  return DRIZZLE_RETURN_OK;
217
}