~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
 *
1971.2.1 by kalebral at gmail
update files that did not have license or had incorrect license structure
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
 *
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
35
 */
36
37
/**
38
 * @file
39
 * @brief Result definitions
40
 */
41
42
#include "common.h"
43
44
/*
45
 * Common definitions
46
 */
47
48
drizzle_result_st *drizzle_result_create(drizzle_con_st *con,
49
                                         drizzle_result_st *result)
50
{
51
  if (result == NULL)
52
  {
53
    result= malloc(sizeof(drizzle_result_st));
54
    if (result == NULL)
55
    {
56
      drizzle_set_error(con->drizzle, "drizzle_result_create", "malloc");
57
      return NULL;
58
    }
59
60
    memset(result, 0, sizeof(drizzle_result_st));
61
    result->options|= DRIZZLE_RESULT_ALLOCATED;
62
  }
63
  else
64
    memset(result, 0, sizeof(drizzle_result_st));
65
66
  result->con= con;
67
  con->result= result;
68
69
  if (con->result_list)
70
    con->result_list->prev= result;
71
  result->next= con->result_list;
72
  con->result_list= result;
73
  con->result_count++;
74
75
  return result;
76
}
77
78
drizzle_result_st *drizzle_result_clone(drizzle_con_st *con,
79
                                        drizzle_result_st *result,
80
                                        drizzle_result_st *from)
81
{
82
  result= drizzle_result_create(con, result);
83
  if (result == NULL)
84
    return NULL;
85
86
  result->options|= (from->options &
87
                     (drizzle_result_options_t)~DRIZZLE_RESULT_ALLOCATED);
88
89
  drizzle_result_set_info(result, from->info);
90
  result->error_code= from->error_code;
91
  drizzle_result_set_sqlstate(result, from->sqlstate);
92
  result->warning_count= from->warning_count;
93
  result->insert_id= from->insert_id;
94
  result->affected_rows= from->affected_rows;
95
  result->column_count= from->column_count;
96
  result->row_count= from->row_count;
97
98
  return result;
99
}
100
101
void drizzle_result_free(drizzle_result_st *result)
102
{
103
  drizzle_column_st *column;
104
  uint64_t x;
105
106
  for (column= result->column_list; column != NULL; column= result->column_list)
107
    drizzle_column_free(column);
108
109
  if (result->column_buffer != NULL)
110
    free(result->column_buffer);
111
112
  if (result->options & DRIZZLE_RESULT_BUFFER_ROW)
113
  {
114
    for (x= 0; x < result->row_count; x++)
115
      drizzle_row_free(result, result->row_list[x]);
116
117
    free(result->row_list);
118
    free(result->field_sizes_list);
119
  }
120
121
  if (result->con->result_list == result)
122
    result->con->result_list= result->next;
123
  if (result->prev)
124
    result->prev->next= result->next;
125
  if (result->next)
126
    result->next->prev= result->prev;
127
  result->con->result_count--;
128
129
  if (result->options & DRIZZLE_RESULT_ALLOCATED)
130
    free(result);
131
}
132
133
void drizzle_result_free_all(drizzle_con_st *con)
134
{
135
  while (con->result_list != NULL)
136
    drizzle_result_free(con->result_list);
137
}
138
139
drizzle_con_st *drizzle_result_drizzle_con(drizzle_result_st *result)
140
{
141
  return result->con;
142
}
143
144
bool drizzle_result_eof(drizzle_result_st *result)
145
{
146
  return result->options & DRIZZLE_RESULT_EOF_PACKET;
147
}
148
149
const char *drizzle_result_info(drizzle_result_st *result)
150
{
151
  return result->info;
152
}
153
154
const char *drizzle_result_error(drizzle_result_st *result)
155
{
156
  return result->info;
157
}
158
159
uint16_t drizzle_result_error_code(drizzle_result_st *result)
160
{
161
  return result->error_code;
162
}
163
164
const char *drizzle_result_sqlstate(drizzle_result_st *result)
165
{
166
  return result->sqlstate;
167
}
168
169
uint16_t drizzle_result_warning_count(drizzle_result_st *result)
170
{
171
  return result->warning_count;
172
}
173
174
uint64_t drizzle_result_insert_id(drizzle_result_st *result)
175
{
176
  return result->insert_id;
177
}
178
179
uint64_t drizzle_result_affected_rows(drizzle_result_st *result)
180
{
181
  return result->affected_rows;
182
}
183
184
uint16_t drizzle_result_column_count(drizzle_result_st *result)
185
{
186
  return result->column_count;
187
}
188
189
uint64_t drizzle_result_row_count(drizzle_result_st *result)
190
{
191
  return result->row_count;
192
}
193
194
/*
195
 * Client definitions
196
 */
197
198
drizzle_result_st *drizzle_result_read(drizzle_con_st *con,
199
                                       drizzle_result_st *result,
200
                                       drizzle_return_t *ret_ptr)
201
{
202
  if (drizzle_state_none(con))
203
  {
204
    con->result= drizzle_result_create(con, result);
205
    if (con->result == NULL)
206
    {
207
      *ret_ptr= DRIZZLE_RETURN_MEMORY;
208
      return NULL;
209
    }
210
211
    drizzle_state_push(con, drizzle_state_result_read);
212
    drizzle_state_push(con, drizzle_state_packet_read);
213
  }
214
215
  *ret_ptr= drizzle_state_loop(con);
216
  return con->result;
217
}
218
219
drizzle_return_t drizzle_result_buffer(drizzle_result_st *result)
220
{
221
  drizzle_return_t ret;
222
  drizzle_row_t row;
223
  drizzle_row_t *row_list;
224
  size_t **field_sizes_list;
225
226
  if (!(result->options & DRIZZLE_RESULT_BUFFER_COLUMN))
227
  {
228
    ret= drizzle_column_buffer(result);
229
    if (ret != DRIZZLE_RETURN_OK)
230
      return ret;
231
  }
232
233
  if (result->column_count == 0)
234
  {
235
    result->options|= DRIZZLE_RESULT_BUFFER_ROW;
236
    return DRIZZLE_RETURN_OK;
237
  }
238
239
  while (1)
240
  {
241
    row= drizzle_row_buffer(result, &ret);
242
    if (ret != DRIZZLE_RETURN_OK)
243
      return ret;
244
245
    if (row == NULL)
246
      break;
247
248
    if (result->row_list_size < result->row_count)
249
    {
250
      row_list= realloc(result->row_list, sizeof(drizzle_row_t) *
251
                        ((size_t)(result->row_list_size) +
252
                         DRIZZLE_ROW_GROW_SIZE));
253
      if (row_list == NULL)
254
      {
255
        drizzle_row_free(result, row);
256
        drizzle_set_error(result->con->drizzle, "drizzle_result_buffer",
257
                          "realloc");
258
        return DRIZZLE_RETURN_MEMORY;
259
      }
260
261
      result->row_list= row_list;
262
263
      field_sizes_list= realloc(result->field_sizes_list, sizeof(size_t *) *
264
                                ((size_t)(result->row_list_size) +
265
                                 DRIZZLE_ROW_GROW_SIZE));
266
      if (field_sizes_list == NULL)
267
      {
268
        drizzle_row_free(result, row);
269
        drizzle_set_error(result->con->drizzle, "drizzle_result_buffer",
270
                          "realloc");
271
        return DRIZZLE_RETURN_MEMORY;
272
      }
273
274
      result->field_sizes_list= field_sizes_list;
275
276
      result->row_list_size+= DRIZZLE_ROW_GROW_SIZE;
277
    }
278
279
    result->row_list[result->row_current - 1]= row;
280
    result->field_sizes_list[result->row_current - 1]= result->field_sizes;
281
  }
282
283
  result->options|= DRIZZLE_RESULT_BUFFER_ROW;
284
  return DRIZZLE_RETURN_OK;
285
}
286
287
size_t drizzle_result_row_size(drizzle_result_st *result)
288
{
289
  return result->con->packet_size;
290
}
291
292
/*
293
 * Server definitions
294
 */
295
296
drizzle_return_t drizzle_result_write(drizzle_con_st *con,
297
                                      drizzle_result_st *result, bool flush)
298
{
299
  if (drizzle_state_none(con))
300
  {
301
    con->result= result;
302
303
    if (flush)
304
      drizzle_state_push(con, drizzle_state_write);
305
306
    drizzle_state_push(con, drizzle_state_result_write);
307
  }
308
309
  return drizzle_state_loop(con);
310
}
311
312
void drizzle_result_set_row_size(drizzle_result_st *result, size_t size)
313
{
314
  result->con->packet_size= size;
315
}
316
317
void drizzle_result_calc_row_size(drizzle_result_st *result,
318
                                  const drizzle_field_t *field,
319
                                  const size_t *size)
320
{
321
  uint16_t x;
322
323
  result->con->packet_size= 0;
324
325
  for (x= 0; x < result->column_count; x++)
326
  {
327
    if (field[x] == NULL)
328
      result->con->packet_size++;
329
    else if (size[x] < 251)
330
      result->con->packet_size+= (1 + size[x]);
331
    else if (size[x] < 65536)
332
      result->con->packet_size+= (3 + size[x]);
333
    else if (size[x] < 16777216)
334
      result->con->packet_size+= (4 + size[x]);
335
    else
336
      result->con->packet_size+= (9 + size[x]);
337
  }
338
}
339
340
void drizzle_result_set_eof(drizzle_result_st *result, bool is_eof)
341
{
342
  if (is_eof)
343
    result->options|= DRIZZLE_RESULT_EOF_PACKET;
344
  else
345
    result->options&= (drizzle_result_options_t)~DRIZZLE_RESULT_EOF_PACKET;
346
}
347
348
void drizzle_result_set_info(drizzle_result_st *result, const char *info)
349
{
350
  if (info == NULL)
351
    result->info[0]= 0;
352
  else
353
  {
354
    strncpy(result->info, info, DRIZZLE_MAX_INFO_SIZE);
355
    result->info[DRIZZLE_MAX_INFO_SIZE - 1]= 0;
356
  }
357
}
358
359
void drizzle_result_set_error(drizzle_result_st *result, const char *error)
360
{
361
  drizzle_result_set_info(result, error);
362
}
363
364
void drizzle_result_set_error_code(drizzle_result_st *result,
365
                                   uint16_t error_code)
366
{
367
  result->error_code= error_code;
368
}
369
370
void drizzle_result_set_sqlstate(drizzle_result_st *result,
371
                                 const char *sqlstate)
372
{
373
  if (sqlstate == NULL)
374
    result->sqlstate[0]= 0;
375
  else
376
  {
377
    strncpy(result->sqlstate, sqlstate, DRIZZLE_MAX_SQLSTATE_SIZE + 1);
378
    result->sqlstate[DRIZZLE_MAX_SQLSTATE_SIZE]= 0;
379
  }
380
}
381
382
void drizzle_result_set_warning_count(drizzle_result_st *result,
383
                                      uint16_t warning_count)
384
{
385
  result->warning_count= warning_count;
386
}
387
388
void drizzle_result_set_insert_id(drizzle_result_st *result,
389
                                  uint64_t insert_id)
390
{
391
  result->insert_id= insert_id;
392
}
393
394
void drizzle_result_set_affected_rows(drizzle_result_st *result,
395
                                      uint64_t affected_rows)
396
{
397
  result->affected_rows= affected_rows;
398
}
399
400
void drizzle_result_set_column_count(drizzle_result_st *result,
401
                                     uint16_t column_count)
402
{
403
  result->column_count= column_count;
404
}
405
406
/*
407
 * Internal state functions.
408
 */
409
410
drizzle_return_t drizzle_state_result_read(drizzle_con_st *con)
411
{
412
  drizzle_return_t ret;
413
414
  drizzle_log_debug(con->drizzle, "drizzle_state_result_read");
415
416
  /* Assume the entire result packet will fit in the buffer. */
417
  if (con->buffer_size < con->packet_size)
418
  {
419
    drizzle_state_push(con, drizzle_state_read);
420
    return DRIZZLE_RETURN_OK;
421
  }
422
423
  if (con->buffer_ptr[0] == 0)
424
  {
425
    con->buffer_ptr++;
426
    /* We can ignore the returns since we've buffered the entire packet. */
427
    con->result->affected_rows= drizzle_unpack_length(con, &ret);
428
    con->result->insert_id= drizzle_unpack_length(con, &ret);
429
    con->status= drizzle_get_byte2(con->buffer_ptr);
430
    con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 2);
431
    con->buffer_ptr+= 4;
432
    con->buffer_size-= 5;
433
    con->packet_size-= 5;
434
    if (con->packet_size > 0)
435
    {
436
      /* Skip one byte for message size. */
437
      con->buffer_ptr+= 1;
438
      con->buffer_size-= 1;
439
      con->packet_size-= 1;
440
    }
441
    ret= DRIZZLE_RETURN_OK;
442
  }
443
  else if (con->buffer_ptr[0] == 254)
444
  {
445
    con->result->options= DRIZZLE_RESULT_EOF_PACKET;
446
    con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 1);
447
    con->status= drizzle_get_byte2(con->buffer_ptr + 3);
448
    con->buffer_ptr+= 5;
449
    con->buffer_size-= 5;
450
    con->packet_size-= 5;
451
    ret= DRIZZLE_RETURN_OK;
452
  }
453
  else if (con->buffer_ptr[0] == 255)
454
  {
455
    con->result->error_code= drizzle_get_byte2(con->buffer_ptr + 1);
456
    con->drizzle->error_code= con->result->error_code;
457
    /* Byte 3 is always a '#' character, skip it. */
458
    memcpy(con->result->sqlstate, con->buffer_ptr + 4,
459
           DRIZZLE_MAX_SQLSTATE_SIZE);
460
    con->result->sqlstate[DRIZZLE_MAX_SQLSTATE_SIZE]= 0;
461
    memcpy(con->drizzle->sqlstate, con->result->sqlstate,
462
           DRIZZLE_MAX_SQLSTATE_SIZE + 1);
463
    con->buffer_ptr+= 9;
464
    con->buffer_size-= 9;
465
    con->packet_size-= 9;
466
    ret= DRIZZLE_RETURN_ERROR_CODE;
467
  }
468
  else
469
  {
470
    /* We can ignore the return since we've buffered the entire packet. */
471
    con->result->column_count= (uint16_t)drizzle_unpack_length(con, &ret);
472
    ret= DRIZZLE_RETURN_OK;
473
  }
474
475
  if (con->packet_size > 0)
476
  {
477
    snprintf(con->drizzle->last_error, DRIZZLE_MAX_ERROR_SIZE, "%.*s",
478
             (int32_t)con->packet_size, con->buffer_ptr);
479
    snprintf(con->result->info, DRIZZLE_MAX_INFO_SIZE, "%.*s",
480
             (int32_t)con->packet_size, con->buffer_ptr);
481
    con->buffer_ptr+= con->packet_size;
482
    con->buffer_size-= con->packet_size;
483
    con->packet_size= 0;
484
  }
485
486
  drizzle_state_pop(con);
487
  return ret;
488
}
489
490
drizzle_return_t drizzle_state_result_write(drizzle_con_st *con)
491
{
492
  uint8_t *start= con->buffer_ptr + con->buffer_size;
493
  uint8_t *ptr;
494
  drizzle_result_st *result= con->result;
495
496
  drizzle_log_debug(con->drizzle, "drizzle_state_result_write");
497
498
  /* Calculate max packet size. */
499
  con->packet_size= 1 /* OK/Field Count/EOF/Error */
500
                  + 9 /* Affected rows */
501
                  + 9 /* Insert ID */
502
                  + 2 /* Status */
503
                  + 2 /* Warning count */
504
                  + strlen(result->info); /* Info/error message */
505
506
  /* Assume the entire result packet will fit in the buffer. */
507
  if ((con->packet_size + 4) > DRIZZLE_MAX_BUFFER_SIZE)
508
  {
509
    drizzle_set_error(con->drizzle, "drizzle_state_result_write",
510
                      "buffer too small:%zu", con->packet_size + 4);
511
    return DRIZZLE_RETURN_INTERNAL_ERROR;
512
  }
513
514
  /* Flush buffer if there is not enough room. */
515
  if (((size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer)) <
516
      con->packet_size)
517
  {
518
    drizzle_state_push(con, drizzle_state_write);
519
    return DRIZZLE_RETURN_OK;
520
  }
521
522
  /* Store packet size at the end since it may change. */
523
  ptr= start;
524
  ptr[3]= con->packet_number;
525
  con->packet_number++;
526
  ptr+= 4;
527
528
  if (result->options & DRIZZLE_RESULT_EOF_PACKET)
529
  {
530
    ptr[0]= 254;
531
    ptr++;
532
533
    drizzle_set_byte2(ptr, result->warning_count);
534
    ptr+= 2;
535
536
    drizzle_set_byte2(ptr, con->status);
537
    ptr+= 2;
538
  }
539
  else if (result->error_code != 0)
540
  {
541
    ptr[0]= 255;
542
    ptr++;
543
544
    drizzle_set_byte2(ptr, result->error_code);
545
    ptr+= 2;
546
547
    ptr[0]= '#';
548
    ptr++;
549
550
    memcpy(ptr, result->sqlstate, DRIZZLE_MAX_SQLSTATE_SIZE);
551
    ptr+= DRIZZLE_MAX_SQLSTATE_SIZE;
552
553
    memcpy(ptr, result->info, strlen(result->info));
554
    ptr+= strlen(result->info);
555
  }
556
  else if (result->column_count == 0)
557
  {
558
    ptr[0]= 0;
559
    ptr++;
560
561
    ptr= drizzle_pack_length(result->affected_rows, ptr);
562
    ptr= drizzle_pack_length(result->insert_id, ptr);
563
564
    drizzle_set_byte2(ptr, con->status);
565
    ptr+= 2;
566
567
    drizzle_set_byte2(ptr, result->warning_count);
568
    ptr+= 2;
569
570
    memcpy(ptr, result->info, strlen(result->info));
571
    ptr+= strlen(result->info);
572
  }
573
  else
574
    ptr= drizzle_pack_length(result->column_count, ptr);
575
576
  con->packet_size= ((size_t)(ptr - start) - 4);
577
  con->buffer_size+= (4 + con->packet_size);
578
579
  /* Store packet size now. */
580
  drizzle_set_byte3(start, con->packet_size);
581
582
  drizzle_state_pop(con);
583
  return DRIZZLE_RETURN_OK;
584
}