~drizzle-trunk/libdrizzle/jenkins-Libdrizzle-78

« back to all changes in this revision

Viewing changes to libdrizzle/result.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 Result definitions
 
41
 */
 
42
 
 
43
#include <libdrizzle/common.h>
 
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 (con == NULL)
 
53
  {
 
54
    return NULL;
 
55
  }
 
56
 
 
57
  if (result == NULL)
 
58
  {
 
59
    result= malloc(sizeof(drizzle_result_st));
 
60
    if (result == NULL)
 
61
    {
 
62
      drizzle_set_error(con->drizzle, __func__, "Failed to allocate.");
 
63
      return NULL;
 
64
    }
 
65
 
 
66
    result->options|= DRIZZLE_RESULT_ALLOCATED;
 
67
  }
 
68
  else
 
69
  {
 
70
    memset(result, 0, sizeof(drizzle_result_st));
 
71
  }
 
72
 
 
73
  result->con= con;
 
74
  con->result= result;
 
75
 
 
76
  if (con->result_list)
 
77
    con->result_list->prev= result;
 
78
  result->next= con->result_list;
 
79
  con->result_list= result;
 
80
  con->result_count++;
 
81
 
 
82
  return result;
 
83
}
 
84
 
 
85
drizzle_result_st *drizzle_result_clone(drizzle_con_st *con,
 
86
                                        drizzle_result_st *result,
 
87
                                        drizzle_result_st *from)
 
88
{
 
89
  // A NULL con will return a NULL result
 
90
  result= drizzle_result_create(con, result);
 
91
  if (result == NULL)
 
92
  {
 
93
    return NULL;
 
94
  }
 
95
 
 
96
  result->options|= from->options & ~DRIZZLE_RESULT_ALLOCATED;
 
97
 
 
98
  drizzle_result_set_info(result, from->info);
 
99
  result->error_code= from->error_code;
 
100
  drizzle_result_set_sqlstate(result, from->sqlstate);
 
101
  result->warning_count= from->warning_count;
 
102
  result->insert_id= from->insert_id;
 
103
  result->affected_rows= from->affected_rows;
 
104
  result->column_count= from->column_count;
 
105
  result->row_count= from->row_count;
 
106
 
 
107
  return result;
 
108
}
 
109
 
 
110
void drizzle_result_free(drizzle_result_st *result)
 
111
{
 
112
  drizzle_column_st* column;
 
113
 
 
114
  if (result == NULL)
 
115
  {
 
116
    return;
 
117
  }
 
118
 
 
119
  for (column= result->column_list; column != NULL; column= result->column_list)
 
120
  {
 
121
    drizzle_column_free(column);
 
122
  }
 
123
 
 
124
  free(result->column_buffer);
 
125
 
 
126
  if (result->options & DRIZZLE_RESULT_BUFFER_ROW)
 
127
  {
 
128
    uint64_t x;
 
129
    for (x= 0; x < result->row_count; x++)
 
130
    {
 
131
      drizzle_row_free(result, result->row_list[x]);
 
132
    }
 
133
 
 
134
    free(result->row_list);
 
135
    free(result->field_sizes_list);
 
136
  }
 
137
 
 
138
  if (result->con)
 
139
  {
 
140
    result->con->result_count--;
 
141
    if (result->con->result_list == result)
 
142
      result->con->result_list= result->next;
 
143
  }
 
144
 
 
145
  if (result->prev)
 
146
  {
 
147
    result->prev->next= result->next;
 
148
  }
 
149
  if (result->next)
 
150
  {
 
151
    result->next->prev= result->prev;
 
152
  }
 
153
 
 
154
  if (result->options & DRIZZLE_RESULT_ALLOCATED)
 
155
  {
 
156
    free(result);
 
157
  }
 
158
}
 
159
 
 
160
void drizzle_result_free_all(drizzle_con_st *con)
 
161
{
 
162
  if (con == NULL)
 
163
  {
 
164
    return;
 
165
  }
 
166
 
 
167
  while (con->result_list != NULL)
 
168
  {
 
169
    drizzle_result_free(con->result_list);
 
170
  }
 
171
}
 
172
 
 
173
drizzle_con_st *drizzle_result_drizzle_con(drizzle_result_st *result)
 
174
{
 
175
  if (result == NULL)
 
176
  {
 
177
    return NULL;
 
178
  }
 
179
 
 
180
  return result->con;
 
181
}
 
182
 
 
183
bool drizzle_result_eof(drizzle_result_st *result)
 
184
{
 
185
  if (result == NULL)
 
186
  {
 
187
    return false;
 
188
  }
 
189
 
 
190
  return result->options & DRIZZLE_RESULT_EOF_PACKET;
 
191
}
 
192
 
 
193
const char *drizzle_result_info(drizzle_result_st *result)
 
194
{
 
195
  if (result == NULL)
 
196
  {
 
197
    return NULL;
 
198
  }
 
199
 
 
200
  return result->info;
 
201
}
 
202
 
 
203
const char *drizzle_result_error(drizzle_result_st *result)
 
204
{
 
205
  if (result == NULL)
 
206
  {
 
207
    return NULL;
 
208
  }
 
209
 
 
210
  return result->info;
 
211
}
 
212
 
 
213
uint16_t drizzle_result_error_code(drizzle_result_st *result)
 
214
{
 
215
  if (result == NULL)
 
216
  {
 
217
    return 0;
 
218
  }
 
219
 
 
220
  return result->error_code;
 
221
}
 
222
 
 
223
const char *drizzle_result_sqlstate(drizzle_result_st *result)
 
224
{
 
225
  if (result == NULL)
 
226
  {
 
227
    return NULL;
 
228
  }
 
229
 
 
230
  return result->sqlstate;
 
231
}
 
232
 
 
233
uint16_t drizzle_result_warning_count(drizzle_result_st *result)
 
234
{
 
235
  if (result == NULL)
 
236
  {
 
237
    return 0;
 
238
  }
 
239
 
 
240
  return result->warning_count;
 
241
}
 
242
 
 
243
uint64_t drizzle_result_insert_id(drizzle_result_st *result)
 
244
{
 
245
  if (result == NULL)
 
246
  {
 
247
    return 0;
 
248
  }
 
249
 
 
250
  return result->insert_id;
 
251
}
 
252
 
 
253
uint64_t drizzle_result_affected_rows(drizzle_result_st *result)
 
254
{
 
255
  if (result == NULL)
 
256
  {
 
257
    return 0;
 
258
  }
 
259
 
 
260
  return result->affected_rows;
 
261
}
 
262
 
 
263
uint16_t drizzle_result_column_count(drizzle_result_st *result)
 
264
{
 
265
  if (result == NULL)
 
266
  {
 
267
    return 0;
 
268
  }
 
269
 
 
270
  return result->column_count;
 
271
}
 
272
 
 
273
uint64_t drizzle_result_row_count(drizzle_result_st *result)
 
274
{
 
275
  if (result == NULL)
 
276
  {
 
277
    return 0;
 
278
  }
 
279
 
 
280
  return result->row_count;
 
281
}
 
282
 
 
283
/*
 
284
 * Client definitions
 
285
 */
 
286
 
 
287
drizzle_result_st *drizzle_result_read(drizzle_con_st *con,
 
288
                                       drizzle_result_st *result,
 
289
                                       drizzle_return_t *ret_ptr)
 
290
{
 
291
  drizzle_return_t unused;
 
292
  if (ret_ptr == NULL)
 
293
  {
 
294
    ret_ptr= &unused;
 
295
  }
 
296
 
 
297
  if (con == NULL)
 
298
  {
 
299
    *ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
 
300
    return NULL;
 
301
  }
 
302
 
 
303
  if (drizzle_state_none(con))
 
304
  {
 
305
    con->result= drizzle_result_create(con, result);
 
306
    if (con->result == NULL)
 
307
    {
 
308
      *ret_ptr= DRIZZLE_RETURN_MEMORY;
 
309
      return NULL;
 
310
    }
 
311
 
 
312
    drizzle_state_push(con, drizzle_state_result_read);
 
313
    drizzle_state_push(con, drizzle_state_packet_read);
 
314
  }
 
315
 
 
316
  *ret_ptr= drizzle_state_loop(con);
 
317
  return con->result;
 
318
}
 
319
 
 
320
drizzle_return_t drizzle_result_buffer(drizzle_result_st *result)
 
321
{
 
322
  if (result == NULL)
 
323
  {
 
324
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
325
  }
 
326
 
 
327
  drizzle_return_t ret;
 
328
  drizzle_row_t row;
 
329
  drizzle_row_t *row_list;
 
330
  size_t **field_sizes_list;
 
331
 
 
332
  if (!(result->options & DRIZZLE_RESULT_BUFFER_COLUMN))
 
333
  {
 
334
    ret= drizzle_column_buffer(result);
 
335
    if (ret != DRIZZLE_RETURN_OK)
 
336
      return ret;
 
337
  }
 
338
 
 
339
  if (result->column_count == 0)
 
340
  {
 
341
    result->options|= DRIZZLE_RESULT_BUFFER_ROW;
 
342
    return DRIZZLE_RETURN_OK;
 
343
  }
 
344
 
 
345
  while (1)
 
346
  {
 
347
    row= drizzle_row_buffer(result, &ret);
 
348
    if (ret != DRIZZLE_RETURN_OK)
 
349
      return ret;
 
350
 
 
351
    if (row == NULL)
 
352
      break;
 
353
 
 
354
    if (result->row_list_size < result->row_count)
 
355
    {
 
356
      row_list= (drizzle_row_t *)realloc(result->row_list, sizeof(drizzle_row_t) * ((size_t)(result->row_list_size) + DRIZZLE_ROW_GROW_SIZE));
 
357
      if (row_list == NULL)
 
358
      {
 
359
        drizzle_row_free(result, row);
 
360
        drizzle_set_error(result->con->drizzle, __func__, "Failed to realloc row_list.");
 
361
        return DRIZZLE_RETURN_MEMORY;
 
362
      }
 
363
 
 
364
      result->row_list= row_list;
 
365
 
 
366
      field_sizes_list= (size_t **)realloc(result->field_sizes_list, sizeof(size_t *) * ((size_t)(result->row_list_size) + DRIZZLE_ROW_GROW_SIZE));
 
367
      if (field_sizes_list == NULL)
 
368
      {
 
369
        drizzle_row_free(result, row);
 
370
        drizzle_set_error(result->con->drizzle, "drizzle_result_buffer", "Failed to realloc field list.");
 
371
        return DRIZZLE_RETURN_MEMORY;
 
372
      }
 
373
 
 
374
      result->field_sizes_list= field_sizes_list;
 
375
 
 
376
      result->row_list_size+= DRIZZLE_ROW_GROW_SIZE;
 
377
    }
 
378
 
 
379
    result->row_list[result->row_current - 1]= row;
 
380
    result->field_sizes_list[result->row_current - 1]= result->field_sizes;
 
381
  }
 
382
 
 
383
  result->options|= DRIZZLE_RESULT_BUFFER_ROW;
 
384
  return DRIZZLE_RETURN_OK;
 
385
}
 
386
 
 
387
size_t drizzle_result_row_size(drizzle_result_st *result)
 
388
{
 
389
  if (result == NULL)
 
390
  {
 
391
    return 0;
 
392
  }
 
393
 
 
394
  return result->con->packet_size;
 
395
}
 
396
 
 
397
/*
 
398
 * Server definitions
 
399
 */
 
400
 
 
401
drizzle_return_t drizzle_result_write(drizzle_con_st *con,
 
402
                                      drizzle_result_st *result, bool flush)
 
403
{
 
404
  if (con == NULL)
 
405
  {
 
406
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
407
  }
 
408
 
 
409
  if (drizzle_state_none(con))
 
410
  {
 
411
    con->result= result;
 
412
 
 
413
    if (flush)
 
414
      drizzle_state_push(con, drizzle_state_write);
 
415
 
 
416
    drizzle_state_push(con, drizzle_state_result_write);
 
417
  }
 
418
 
 
419
  return drizzle_state_loop(con);
 
420
}
 
421
 
 
422
void drizzle_result_set_row_size(drizzle_result_st *result, size_t size)
 
423
{
 
424
  if (result == NULL)
 
425
  {
 
426
    return;
 
427
  }
 
428
 
 
429
  result->con->packet_size= size;
 
430
}
 
431
 
 
432
void drizzle_result_calc_row_size(drizzle_result_st *result,
 
433
                                  const drizzle_field_t *field,
 
434
                                  const size_t *size)
 
435
{
 
436
  uint16_t x;
 
437
  if (result == NULL)
 
438
  {
 
439
    return;
 
440
  }
 
441
 
 
442
  result->con->packet_size= 0;
 
443
 
 
444
  for (x= 0; x < result->column_count; x++)
 
445
  {
 
446
    if (field[x] == NULL)
 
447
    {
 
448
      result->con->packet_size++;
 
449
    }
 
450
    else if (size[x] < 251)
 
451
    {
 
452
      result->con->packet_size+= (1 + size[x]);
 
453
    }
 
454
    else if (size[x] < 65536)
 
455
    {
 
456
      result->con->packet_size+= (3 + size[x]);
 
457
    }
 
458
    else if (size[x] < 16777216)
 
459
    {
 
460
      result->con->packet_size+= (4 + size[x]);
 
461
    }
 
462
    else
 
463
    {
 
464
      result->con->packet_size+= (9 + size[x]);
 
465
    }
 
466
  }
 
467
}
 
468
 
 
469
void drizzle_result_set_eof(drizzle_result_st *result, bool is_eof)
 
470
{
 
471
  if (result == NULL)
 
472
  {
 
473
    return;
 
474
  }
 
475
 
 
476
  if (is_eof)
 
477
    result->options|= DRIZZLE_RESULT_EOF_PACKET;
 
478
  else
 
479
    result->options&= ~DRIZZLE_RESULT_EOF_PACKET;
 
480
}
 
481
 
 
482
void drizzle_result_set_info(drizzle_result_st *result, const char *info)
 
483
{
 
484
  if (result == NULL)
 
485
  {
 
486
    return;
 
487
  }
 
488
 
 
489
  if (info == NULL)
 
490
  {
 
491
    result->info[0]= 0;
 
492
  }
 
493
  else
 
494
  {
 
495
    strncpy(result->info, info, DRIZZLE_MAX_INFO_SIZE);
 
496
    result->info[DRIZZLE_MAX_INFO_SIZE - 1]= 0;
 
497
  }
 
498
}
 
499
 
 
500
void drizzle_result_set_error(drizzle_result_st *result, const char *error)
 
501
{
 
502
  if (result == NULL)
 
503
  {
 
504
    return;
 
505
  }
 
506
 
 
507
  drizzle_result_set_info(result, error);
 
508
}
 
509
 
 
510
void drizzle_result_set_error_code(drizzle_result_st *result,
 
511
                                   uint16_t error_code)
 
512
{
 
513
  if (result == NULL)
 
514
  {
 
515
    return;
 
516
  }
 
517
 
 
518
  result->error_code= error_code;
 
519
}
 
520
 
 
521
void drizzle_result_set_sqlstate(drizzle_result_st *result,
 
522
                                 const char *sqlstate)
 
523
{
 
524
  if (result == NULL)
 
525
  {
 
526
    return;
 
527
  }
 
528
 
 
529
  if (sqlstate == NULL)
 
530
  {
 
531
    result->sqlstate[0]= 0;
 
532
  }
 
533
  else
 
534
  {
 
535
    strncpy(result->sqlstate, sqlstate, DRIZZLE_MAX_SQLSTATE_SIZE + 1);
 
536
    result->sqlstate[DRIZZLE_MAX_SQLSTATE_SIZE]= 0;
 
537
  }
 
538
}
 
539
 
 
540
void drizzle_result_set_warning_count(drizzle_result_st *result,
 
541
                                      uint16_t warning_count)
 
542
{
 
543
  if (result == NULL)
 
544
  {
 
545
    return;
 
546
  }
 
547
 
 
548
  result->warning_count= warning_count;
 
549
}
 
550
 
 
551
void drizzle_result_set_insert_id(drizzle_result_st *result,
 
552
                                  uint64_t insert_id)
 
553
{
 
554
  if (result == NULL)
 
555
  {
 
556
    return;
 
557
  }
 
558
 
 
559
  result->insert_id= insert_id;
 
560
}
 
561
 
 
562
void drizzle_result_set_affected_rows(drizzle_result_st *result,
 
563
                                      uint64_t affected_rows)
 
564
{
 
565
  if (result == NULL)
 
566
  {
 
567
    return;
 
568
  }
 
569
 
 
570
  result->affected_rows= affected_rows;
 
571
}
 
572
 
 
573
void drizzle_result_set_column_count(drizzle_result_st *result,
 
574
                                     uint16_t column_count)
 
575
{
 
576
  if (result == NULL)
 
577
  {
 
578
    return;
 
579
  }
 
580
 
 
581
  result->column_count= column_count;
 
582
}
 
583
 
 
584
/*
 
585
 * Internal state functions.
 
586
 */
 
587
 
 
588
drizzle_return_t drizzle_state_result_read(drizzle_con_st *con)
 
589
{
 
590
  drizzle_return_t ret;
 
591
 
 
592
  if (con == NULL)
 
593
  {
 
594
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
595
  }
 
596
 
 
597
  drizzle_log_debug(con->drizzle, "drizzle_state_result_read");
 
598
 
 
599
  /* Assume the entire result packet will fit in the buffer. */
 
600
  if (con->buffer_size < con->packet_size)
 
601
  {
 
602
    drizzle_state_push(con, drizzle_state_read);
 
603
    return DRIZZLE_RETURN_OK;
 
604
  }
 
605
 
 
606
  if (con->buffer_ptr[0] == 0)
 
607
  {
 
608
    con->buffer_ptr++;
 
609
    /* We can ignore the returns since we've buffered the entire packet. */
 
610
    con->result->affected_rows= drizzle_unpack_length(con, &ret);
 
611
    con->result->insert_id= drizzle_unpack_length(con, &ret);
 
612
    con->status= drizzle_get_byte2(con->buffer_ptr);
 
613
    con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 2);
 
614
    con->buffer_ptr+= 4;
 
615
    con->buffer_size-= 5;
 
616
    con->packet_size-= 5;
 
617
    if (con->packet_size > 0)
 
618
    {
 
619
      /* Skip one byte for message size. */
 
620
      con->buffer_ptr+= 1;
 
621
      con->buffer_size-= 1;
 
622
      con->packet_size-= 1;
 
623
    }
 
624
    ret= DRIZZLE_RETURN_OK;
 
625
  }
 
626
  else if (con->buffer_ptr[0] == 254)
 
627
  {
 
628
    con->result->options= DRIZZLE_RESULT_EOF_PACKET;
 
629
    con->result->warning_count= drizzle_get_byte2(con->buffer_ptr + 1);
 
630
    con->status= drizzle_get_byte2(con->buffer_ptr + 3);
 
631
    con->buffer_ptr+= 5;
 
632
    con->buffer_size-= 5;
 
633
    con->packet_size-= 5;
 
634
    ret= DRIZZLE_RETURN_OK;
 
635
  }
 
636
  else if (con->buffer_ptr[0] == 255)
 
637
  {
 
638
    con->result->error_code= drizzle_get_byte2(con->buffer_ptr + 1);
 
639
    con->drizzle->error_code= con->result->error_code;
 
640
    /* Byte 3 is always a '#' character, skip it. */
 
641
    memcpy(con->result->sqlstate, con->buffer_ptr + 4,
 
642
           DRIZZLE_MAX_SQLSTATE_SIZE);
 
643
    con->result->sqlstate[DRIZZLE_MAX_SQLSTATE_SIZE]= 0;
 
644
    memcpy(con->drizzle->sqlstate, con->result->sqlstate,
 
645
           DRIZZLE_MAX_SQLSTATE_SIZE + 1);
 
646
    con->buffer_ptr+= 9;
 
647
    con->buffer_size-= 9;
 
648
    con->packet_size-= 9;
 
649
    ret= DRIZZLE_RETURN_ERROR_CODE;
 
650
  }
 
651
  else
 
652
  {
 
653
    /* We can ignore the return since we've buffered the entire packet. */
 
654
    con->result->column_count= (uint16_t)drizzle_unpack_length(con, &ret);
 
655
    ret= DRIZZLE_RETURN_OK;
 
656
  }
 
657
 
 
658
  if (con->packet_size > 0)
 
659
  {
 
660
    snprintf(con->drizzle->last_error, DRIZZLE_MAX_ERROR_SIZE, "%.*s",
 
661
             (int32_t)con->packet_size, con->buffer_ptr);
 
662
    con->drizzle->last_error[DRIZZLE_MAX_ERROR_SIZE-1]= 0;
 
663
    snprintf(con->result->info, DRIZZLE_MAX_INFO_SIZE, "%.*s",
 
664
             (int32_t)con->packet_size, con->buffer_ptr);
 
665
    con->result->info[DRIZZLE_MAX_INFO_SIZE-1]= 0;
 
666
    con->buffer_ptr+= con->packet_size;
 
667
    con->buffer_size-= con->packet_size;
 
668
    con->packet_size= 0;
 
669
  }
 
670
 
 
671
  drizzle_state_pop(con);
 
672
  return ret;
 
673
}
 
674
 
 
675
drizzle_return_t drizzle_state_result_write(drizzle_con_st *con)
 
676
{
 
677
  if (con == NULL)
 
678
  {
 
679
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
680
  }
 
681
 
 
682
  uint8_t *start= con->buffer_ptr + con->buffer_size;
 
683
  uint8_t *ptr;
 
684
  drizzle_result_st *result= con->result;
 
685
 
 
686
  drizzle_log_debug(con->drizzle, "drizzle_state_result_write");
 
687
 
 
688
  /* Calculate max packet size. */
 
689
  con->packet_size= 1 /* OK/Field Count/EOF/Error */
 
690
                  + 9 /* Affected rows */
 
691
                  + 9 /* Insert ID */
 
692
                  + 2 /* Status */
 
693
                  + 2 /* Warning count */
 
694
                  + strlen(result->info); /* Info/error message */
 
695
 
 
696
  /* Assume the entire result packet will fit in the buffer. */
 
697
  if ((con->packet_size + 4) > DRIZZLE_MAX_BUFFER_SIZE)
 
698
  {
 
699
    drizzle_set_error(con->drizzle, "drizzle_state_result_write",
 
700
                      "buffer too small:%zu", con->packet_size + 4);
 
701
    return DRIZZLE_RETURN_INTERNAL_ERROR;
 
702
  }
 
703
 
 
704
  /* Flush buffer if there is not enough room. */
 
705
  if (((size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer)) <
 
706
      con->packet_size)
 
707
  {
 
708
    drizzle_state_push(con, drizzle_state_write);
 
709
    return DRIZZLE_RETURN_OK;
 
710
  }
 
711
 
 
712
  /* Store packet size at the end since it may change. */
 
713
  ptr= start;
 
714
  ptr[3]= con->packet_number;
 
715
  con->packet_number++;
 
716
  ptr+= 4;
 
717
 
 
718
  if (result->options & DRIZZLE_RESULT_EOF_PACKET)
 
719
  {
 
720
    ptr[0]= 254;
 
721
    ptr++;
 
722
 
 
723
    drizzle_set_byte2(ptr, result->warning_count);
 
724
    ptr+= 2;
 
725
 
 
726
    drizzle_set_byte2(ptr, con->status);
 
727
    ptr+= 2;
 
728
  }
 
729
  else if (result->error_code != 0)
 
730
  {
 
731
    ptr[0]= 255;
 
732
    ptr++;
 
733
 
 
734
    drizzle_set_byte2(ptr, result->error_code);
 
735
    ptr+= 2;
 
736
 
 
737
    ptr[0]= '#';
 
738
    ptr++;
 
739
 
 
740
    memcpy(ptr, result->sqlstate, DRIZZLE_MAX_SQLSTATE_SIZE);
 
741
    ptr+= DRIZZLE_MAX_SQLSTATE_SIZE;
 
742
 
 
743
    memcpy(ptr, result->info, strlen(result->info));
 
744
    ptr+= strlen(result->info);
 
745
  }
 
746
  else if (result->column_count == 0)
 
747
  {
 
748
    ptr[0]= 0;
 
749
    ptr++;
 
750
 
 
751
    ptr= drizzle_pack_length(result->affected_rows, ptr);
 
752
    ptr= drizzle_pack_length(result->insert_id, ptr);
 
753
 
 
754
    drizzle_set_byte2(ptr, con->status);
 
755
    ptr+= 2;
 
756
 
 
757
    drizzle_set_byte2(ptr, result->warning_count);
 
758
    ptr+= 2;
 
759
 
 
760
    memcpy(ptr, result->info, strlen(result->info));
 
761
    ptr+= strlen(result->info);
 
762
  }
 
763
  else
 
764
    ptr= drizzle_pack_length(result->column_count, ptr);
 
765
 
 
766
  con->packet_size= ((size_t)(ptr - start) - 4);
 
767
  con->buffer_size+= (4 + con->packet_size);
 
768
 
 
769
  /* Store packet size now. */
 
770
  drizzle_set_byte3(start, con->packet_size);
 
771
 
 
772
  drizzle_state_pop(con);
 
773
  return DRIZZLE_RETURN_OK;
 
774
}