~drizzle-trunk/drizzle/development

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