~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle-2.0/result.cc

  • Committer: Mark Atwood
  • Date: 2011-12-11 22:56:39 UTC
  • mfrom: (2465.1.1 drizzle)
  • Revision ID: me@mark.atwood.name-20111211225639-3d8ype7g2y82dci5
mergeĀ lp:~brianaker/drizzle/libdrizzle-breakout

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 * @brief Result definitions
40
40
 */
41
41
 
42
 
#include "common.h"
 
42
#include <libdrizzle-2.0/common.h>
43
43
#include <memory>
44
44
 
45
45
/*
46
46
 * Common definitions
47
47
 */
48
48
 
49
 
drizzle_result_st *drizzle_result_create(drizzle_con_st *con,
50
 
                                         drizzle_result_st *result)
 
49
drizzle_result_st *drizzle_result_create(drizzle_con_st *con)
 
50
{
 
51
  return drizzle_result_create_with(con, NULL);
 
52
}
 
53
 
 
54
drizzle_result_st *drizzle_result_create_with(drizzle_con_st *con,
 
55
                                              drizzle_result_st *result)
51
56
{
52
57
  if (result == NULL)
53
58
  {
54
 
    result= new drizzle_result_st;
55
 
    result->options|= DRIZZLE_RESULT_ALLOCATED;
 
59
    result= new (std::nothrow) drizzle_result_st;
 
60
 
 
61
    if (result == NULL)
 
62
    {
 
63
      return NULL;
 
64
    }
 
65
 
 
66
    result->_options.is_allocated= true;
56
67
  }
57
68
  else
58
69
  {
87
98
    result->row_list= NULL;
88
99
    result->field_sizes= NULL;
89
100
    result->field_sizes_list= NULL;
 
101
 
 
102
    result->_options.is_allocated= false;
90
103
  }
91
104
 
92
105
  result->con= con;
102
115
}
103
116
 
104
117
drizzle_result_st *drizzle_result_clone(drizzle_con_st *con,
105
 
                                        drizzle_result_st *result,
106
 
                                        drizzle_result_st *from)
 
118
                                        drizzle_result_st *source)
107
119
{
108
 
  result= drizzle_result_create(con, result);
 
120
  drizzle_result_st *result= drizzle_result_create(con);
109
121
  if (result == NULL)
 
122
  {
110
123
    return NULL;
111
 
 
112
 
  result->options|= from->options & ~DRIZZLE_RESULT_ALLOCATED;
113
 
 
114
 
  drizzle_result_set_info(result, from->info);
115
 
  result->error_code= from->error_code;
116
 
  drizzle_result_set_sqlstate(result, from->sqlstate);
117
 
  result->warning_count= from->warning_count;
118
 
  result->insert_id= from->insert_id;
119
 
  result->affected_rows= from->affected_rows;
120
 
  result->column_count= from->column_count;
121
 
  result->row_count= from->row_count;
 
124
  }
 
125
 
 
126
  result->options= source->options;
 
127
 
 
128
  drizzle_result_set_info(result, source->info);
 
129
  result->error_code= source->error_code;
 
130
  drizzle_result_set_sqlstate(result, source->sqlstate);
 
131
  result->warning_count= source->warning_count;
 
132
  result->insert_id= source->insert_id;
 
133
  result->affected_rows= source->affected_rows;
 
134
  result->column_count= source->column_count;
 
135
  result->row_count= source->row_count;
122
136
 
123
137
  return result;
124
138
}
126
140
void drizzle_result_free(drizzle_result_st *result)
127
141
{
128
142
  drizzle_column_st *column;
129
 
  uint64_t x;
 
143
 
 
144
  if (result == NULL)
 
145
  {
 
146
    return;
 
147
  }
130
148
 
131
149
  for (column= result->column_list; column != NULL; column= result->column_list)
 
150
  {
132
151
    drizzle_column_free(column);
 
152
  }
133
153
 
134
154
  delete[] result->column_buffer;
135
155
 
136
156
  if (result->options & DRIZZLE_RESULT_BUFFER_ROW)
137
157
  {
138
 
    for (x= 0; x < result->row_count; x++)
139
 
      drizzle_row_free(result, result->row_list->at(static_cast<size_t>(x)));
 
158
    for (size_t x= 0; x < result->row_count; x++)
 
159
    {
 
160
      drizzle_row_free(result, result->row_list->at(x));
 
161
    }
140
162
 
141
163
    delete result->row_list;
142
164
    delete result->field_sizes_list;
148
170
    if (result->con->result_list == result)
149
171
      result->con->result_list= result->next;
150
172
  }
 
173
 
151
174
  if (result->prev)
152
175
    result->prev->next= result->next;
 
176
 
153
177
  if (result->next)
154
178
    result->next->prev= result->prev;
155
179
 
156
 
  if (result->options & DRIZZLE_RESULT_ALLOCATED)
 
180
  if (result->_options.is_allocated)
 
181
  {
157
182
    delete result;
 
183
  }
158
184
}
159
185
 
160
186
void drizzle_result_free_all(drizzle_con_st *con)
161
187
{
162
188
  while (con->result_list != NULL)
 
189
  {
163
190
    drizzle_result_free(con->result_list);
 
191
  }
164
192
}
165
193
 
166
194
drizzle_con_st *drizzle_result_drizzle_con(drizzle_result_st *result)
167
195
{
 
196
  if (result == NULL)
 
197
  {
 
198
    return NULL;
 
199
  }
 
200
 
168
201
  return result->con;
169
202
}
170
203
 
171
204
bool drizzle_result_eof(drizzle_result_st *result)
172
205
{
 
206
  if (result == NULL)
 
207
  {
 
208
    return false;
 
209
  }
 
210
 
173
211
  return (result->options & DRIZZLE_RESULT_EOF_PACKET) ? true : false;
174
212
}
175
213
 
176
214
const char *drizzle_result_info(drizzle_result_st *result)
177
215
{
 
216
  if (result == NULL)
 
217
  {
 
218
    return NULL;
 
219
  }
 
220
 
178
221
  return result->info;
179
222
}
180
223
 
181
224
const char *drizzle_result_error(drizzle_result_st *result)
182
225
{
 
226
  if (result == NULL)
 
227
  {
 
228
    return NULL;
 
229
  }
 
230
 
183
231
  return result->info;
184
232
}
185
233
 
186
234
uint16_t drizzle_result_error_code(drizzle_result_st *result)
187
235
{
 
236
  if (result == NULL)
 
237
  {
 
238
    return 0;
 
239
  }
 
240
 
188
241
  return result->error_code;
189
242
}
190
243
 
191
244
const char *drizzle_result_sqlstate(drizzle_result_st *result)
192
245
{
 
246
  if (result == NULL)
 
247
  {
 
248
    return NULL;
 
249
  }
 
250
 
193
251
  return result->sqlstate;
194
252
}
195
253
 
196
254
uint16_t drizzle_result_warning_count(drizzle_result_st *result)
197
255
{
 
256
  if (result == NULL)
 
257
  {
 
258
    return 0;
 
259
  }
 
260
 
198
261
  return result->warning_count;
199
262
}
200
263
 
201
264
uint64_t drizzle_result_insert_id(drizzle_result_st *result)
202
265
{
 
266
  if (result == NULL)
 
267
  {
 
268
    return 0;
 
269
  }
 
270
 
203
271
  return result->insert_id;
204
272
}
205
273
 
206
274
uint64_t drizzle_result_affected_rows(drizzle_result_st *result)
207
275
{
 
276
  if (result == NULL)
 
277
  {
 
278
    return 0;
 
279
  }
 
280
 
208
281
  return result->affected_rows;
209
282
}
210
283
 
211
284
uint16_t drizzle_result_column_count(drizzle_result_st *result)
212
285
{
 
286
  if (result == NULL)
 
287
  {
 
288
    return 0;
 
289
  }
 
290
 
213
291
  return result->column_count;
214
292
}
215
293
 
216
294
uint64_t drizzle_result_row_count(drizzle_result_st *result)
217
295
{
 
296
  if (result == NULL)
 
297
  {
 
298
    return 0;
 
299
  }
 
300
 
218
301
  return result->row_count;
219
302
}
220
303
 
226
309
                                       drizzle_result_st *result,
227
310
                                       drizzle_return_t *ret_ptr)
228
311
{
 
312
  drizzle_return_t unused;
 
313
  if (ret_ptr == NULL)
 
314
  {
 
315
    ret_ptr= &unused;
 
316
  }
 
317
 
 
318
  if (con == NULL)
 
319
  {
 
320
    *ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
 
321
    return NULL;
 
322
  }
 
323
 
229
324
  if (drizzle_state_none(con))
230
325
  {
231
 
    con->result= drizzle_result_create(con, result);
 
326
    con->result= drizzle_result_create_with(con, result);
232
327
    if (con->result == NULL)
233
328
    {
234
329
      *ret_ptr= DRIZZLE_RETURN_MEMORY;
245
340
 
246
341
drizzle_return_t drizzle_result_buffer(drizzle_result_st *result)
247
342
{
248
 
  drizzle_return_t ret;
249
 
  drizzle_row_t row;
 
343
  if (result == NULL)
 
344
  {
 
345
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
346
  }
250
347
 
251
348
  if (!(result->options & DRIZZLE_RESULT_BUFFER_COLUMN))
252
349
  {
253
 
    ret= drizzle_column_buffer(result);
 
350
    drizzle_return_t ret= drizzle_column_buffer(result);
254
351
    if (ret != DRIZZLE_RETURN_OK)
 
352
    {
255
353
      return ret;
 
354
    }
256
355
  }
257
356
 
258
357
  if (result->column_count == 0)
263
362
 
264
363
  while (1)
265
364
  {
266
 
    row= drizzle_row_buffer(result, &ret);
 
365
    drizzle_return_t ret;
 
366
    drizzle_row_t row= drizzle_row_buffer(result, &ret);
267
367
    if (ret != DRIZZLE_RETURN_OK)
 
368
    {
268
369
      return ret;
 
370
    }
269
371
 
270
372
    if (row == NULL)
 
373
    {
271
374
      break;
 
375
    }
272
376
 
273
377
    if (result->row_list == NULL)
274
378
    {
275
 
      result->row_list= new drizzle_row_list_t;
 
379
      result->row_list= new (std::nothrow) drizzle_row_list_t;
 
380
 
 
381
      if (result->row_list == NULL)
 
382
      {
 
383
        return DRIZZLE_RETURN_MEMORY;
 
384
      }
276
385
    }
277
386
 
278
387
 
279
388
    if (result->field_sizes_list == NULL)
280
389
    {
281
 
      result->field_sizes_list= new drizzle_field_sizes_list_t;
 
390
      result->field_sizes_list= new (std::nothrow) drizzle_field_sizes_list_t;
 
391
 
 
392
      if (result->field_sizes_list == NULL)
 
393
      {
 
394
      }
282
395
    }
283
396
 
284
397
    result->row_list->push_back(row);
286
399
  }
287
400
 
288
401
  result->options|= DRIZZLE_RESULT_BUFFER_ROW;
 
402
 
289
403
  return DRIZZLE_RETURN_OK;
290
404
}
291
405
 
292
406
size_t drizzle_result_row_size(drizzle_result_st *result)
293
407
{
 
408
  if (result == NULL)
 
409
  {
 
410
    return 0;
 
411
  }
 
412
 
294
413
  return result->con->packet_size;
295
414
}
296
415
 
301
420
drizzle_return_t drizzle_result_write(drizzle_con_st *con,
302
421
                                      drizzle_result_st *result, bool flush)
303
422
{
 
423
  if (con == NULL)
 
424
  {
 
425
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
426
  }
 
427
 
304
428
  if (drizzle_state_none(con))
305
429
  {
306
430
    con->result= result;
316
440
 
317
441
void drizzle_result_set_row_size(drizzle_result_st *result, size_t size)
318
442
{
 
443
  if (result == NULL)
 
444
  {
 
445
    return;
 
446
  }
 
447
 
319
448
  result->con->packet_size= size;
320
449
}
321
450
 
323
452
                                  const drizzle_field_t *field,
324
453
                                  const size_t *size)
325
454
{
326
 
  uint16_t x;
 
455
  if (result == NULL)
 
456
  {
 
457
    return;
 
458
  }
327
459
 
328
460
  result->con->packet_size= 0;
329
461
 
330
 
  for (x= 0; x < result->column_count; x++)
 
462
  for (uint16_t x= 0; x < result->column_count; x++)
331
463
  {
332
464
    if (field[x] == NULL)
 
465
    {
333
466
      result->con->packet_size++;
 
467
    }
334
468
    else if (size[x] < 251)
 
469
    {
335
470
      result->con->packet_size+= (1 + size[x]);
 
471
    }
336
472
    else if (size[x] < 65536)
 
473
    {
337
474
      result->con->packet_size+= (3 + size[x]);
 
475
    }
338
476
    else if (size[x] < 16777216)
 
477
    {
339
478
      result->con->packet_size+= (4 + size[x]);
 
479
    }
340
480
    else
 
481
    {
341
482
      result->con->packet_size+= (9 + size[x]);
 
483
    }
342
484
  }
343
485
}
344
486
 
345
487
void drizzle_result_set_eof(drizzle_result_st *result, bool is_eof)
346
488
{
 
489
  if (result == NULL)
 
490
  {
 
491
    return;
 
492
  }
 
493
 
347
494
  if (is_eof)
 
495
  {
348
496
    result->options|= DRIZZLE_RESULT_EOF_PACKET;
 
497
  }
349
498
  else
 
499
  {
350
500
    result->options&= ~DRIZZLE_RESULT_EOF_PACKET;
 
501
  }
351
502
}
352
503
 
353
504
void drizzle_result_set_info(drizzle_result_st *result, const char *info)
354
505
{
 
506
  if (result == NULL)
 
507
  {
 
508
    return;
 
509
  }
 
510
 
355
511
  if (info == NULL)
 
512
  {
356
513
    result->info[0]= 0;
 
514
  }
357
515
  else
358
516
  {
359
517
    strncpy(result->info, info, DRIZZLE_MAX_INFO_SIZE);
363
521
 
364
522
void drizzle_result_set_error(drizzle_result_st *result, const char *error)
365
523
{
 
524
  if (result == NULL)
 
525
  {
 
526
    return;
 
527
  }
 
528
 
366
529
  drizzle_result_set_info(result, error);
367
530
}
368
531
 
369
532
void drizzle_result_set_error_code(drizzle_result_st *result,
370
533
                                   uint16_t error_code)
371
534
{
 
535
  if (result == NULL)
 
536
  {
 
537
    return;
 
538
  }
 
539
 
372
540
  result->error_code= error_code;
373
541
}
374
542
 
375
543
void drizzle_result_set_sqlstate(drizzle_result_st *result,
376
544
                                 const char *sqlstate)
377
545
{
 
546
  if (result == NULL)
 
547
  {
 
548
    return;
 
549
  }
 
550
 
378
551
  if (sqlstate == NULL)
 
552
  {
379
553
    result->sqlstate[0]= 0;
 
554
  }
380
555
  else
381
556
  {
382
557
    strncpy(result->sqlstate, sqlstate, DRIZZLE_MAX_SQLSTATE_SIZE + 1);
387
562
void drizzle_result_set_warning_count(drizzle_result_st *result,
388
563
                                      uint16_t warning_count)
389
564
{
 
565
  if (result == NULL)
 
566
  {
 
567
    return;
 
568
  }
 
569
 
390
570
  result->warning_count= warning_count;
391
571
}
392
572
 
393
573
void drizzle_result_set_insert_id(drizzle_result_st *result,
394
574
                                  uint64_t insert_id)
395
575
{
 
576
  if (result == NULL)
 
577
  {
 
578
    return;
 
579
  }
 
580
 
396
581
  result->insert_id= insert_id;
397
582
}
398
583
 
399
584
void drizzle_result_set_affected_rows(drizzle_result_st *result,
400
585
                                      uint64_t affected_rows)
401
586
{
 
587
  if (result == NULL)
 
588
  {
 
589
    return;
 
590
  }
 
591
 
402
592
  result->affected_rows= affected_rows;
403
593
}
404
594
 
405
595
void drizzle_result_set_column_count(drizzle_result_st *result,
406
596
                                     uint16_t column_count)
407
597
{
 
598
  if (result == NULL)
 
599
  {
 
600
    return;
 
601
  }
 
602
 
408
603
  result->column_count= column_count;
409
604
}
410
605
 
416
611
{
417
612
  drizzle_return_t ret;
418
613
 
 
614
  if (con == NULL)
 
615
  {
 
616
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
617
  }
 
618
 
 
619
 
419
620
  drizzle_log_debug(con->drizzle, "drizzle_state_result_read");
420
621
 
421
622
  /* Assume the entire result packet will fit in the buffer. */
422
623
  if (con->buffer_size < con->packet_size)
423
624
  {
424
625
    drizzle_state_push(con, drizzle_state_read);
 
626
 
425
627
    return DRIZZLE_RETURN_OK;
426
628
  }
427
629
 
491
693
  }
492
694
 
493
695
  drizzle_state_pop(con);
 
696
 
494
697
  return ret;
495
698
}
496
699
 
497
700
drizzle_return_t drizzle_state_result_write(drizzle_con_st *con)
498
701
{
 
702
  if (con == NULL)
 
703
  {
 
704
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
705
  }
 
706
 
499
707
  uint8_t *start= con->buffer_ptr + con->buffer_size;
500
708
  uint8_t *ptr;
501
709
  drizzle_result_st *result= con->result;
513
721
  /* Assume the entire result packet will fit in the buffer. */
514
722
  if ((con->packet_size + 4) > DRIZZLE_MAX_BUFFER_SIZE)
515
723
  {
516
 
    drizzle_set_error(con->drizzle, "drizzle_state_result_write",
517
 
                      "buffer too small:%zu", con->packet_size + 4);
 
724
    drizzle_set_error(con->drizzle, "drizzle_state_result_write", "buffer too small:%zu", con->packet_size + 4);
 
725
 
518
726
    return DRIZZLE_RETURN_INTERNAL_ERROR;
519
727
  }
520
728
 
523
731
      con->packet_size)
524
732
  {
525
733
    drizzle_state_push(con, drizzle_state_write);
 
734
 
526
735
    return DRIZZLE_RETURN_OK;
527
736
  }
528
737
 
587
796
  drizzle_set_byte3(start, con->packet_size);
588
797
 
589
798
  drizzle_state_pop(con);
 
799
 
590
800
  return DRIZZLE_RETURN_OK;
591
801
}