~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle-2.0/drizzle.cc

  • Committer: Brian Aker
  • Date: 2011-11-18 16:11:02 UTC
  • mto: (2463.1.1 drizzle-include)
  • mto: This revision was merged to the branch mainline in revision 2462.
  • Revision ID: brian@tangent.org-20111118161102-d1mbswn88iwc2iv1
Pass through refactoring.

Never throw exceptions from libraries. You can not have the assumption that in a driver that the above language can handle this.

Show diffs side-by-side

added added

removed removed

Lines of Context:
79
79
const char *drizzle_verbose_name(drizzle_verbose_t verbose)
80
80
{
81
81
  if (verbose >= DRIZZLE_VERBOSE_MAX)
 
82
  {
82
83
    return "UNKNOWN";
 
84
  }
83
85
 
84
86
  return _verbose_name[verbose];
85
87
}
101
103
 
102
104
  if (drizzle == NULL)
103
105
  {
104
 
    drizzle= new drizzle_st;
105
 
    drizzle->options= DRIZZLE_ALLOCATED;
 
106
    drizzle= new (std::nothrow) drizzle_st;
 
107
 
 
108
    if (drizzle == NULL)
 
109
    {
 
110
      return NULL;
 
111
    }
 
112
    drizzle->options.is_allocated= true;
106
113
  }
107
 
  else
108
 
    drizzle->options= DRIZZLE_NONE;
109
114
 
110
115
  /* @todo remove this default free flag with new API. */
111
 
  drizzle->options|= DRIZZLE_FREE_OBJECTS;
 
116
  drizzle->options.is_free_objects= true;
112
117
  drizzle->error_code= 0;
 
118
 
113
119
  /* drizzle->options set above */
114
120
  drizzle->verbose= DRIZZLE_VERBOSE_NEVER;
115
121
  drizzle->con_count= 0;
138
144
{
139
145
  drizzle= drizzle_create(drizzle);
140
146
  if (drizzle == NULL)
 
147
  {
141
148
    return NULL;
 
149
  }
142
150
 
143
 
  drizzle->options|= (from->options & ~DRIZZLE_ALLOCATED);
 
151
  bool cache_state= drizzle->options.is_allocated;
 
152
  drizzle->options= from->options;
 
153
  drizzle->options.is_allocated= cache_state;
144
154
 
145
155
  for (drizzle_con_st* con= from->con_list; con != NULL; con= con->next)
146
156
  {
157
167
void drizzle_free(drizzle_st *drizzle)
158
168
{
159
169
  if (drizzle->context != NULL && drizzle->context_free_fn != NULL)
 
170
  {
160
171
    drizzle->context_free_fn(drizzle, drizzle->context);
 
172
  }
161
173
 
162
 
  if (drizzle->options & DRIZZLE_FREE_OBJECTS)
 
174
  if (drizzle->options.is_free_objects)
163
175
  {
164
176
    drizzle_con_free_all(drizzle);
165
177
    drizzle_query_free_all(drizzle);
166
178
  }
167
 
  else if (drizzle->options & DRIZZLE_ASSERT_DANGLING)
 
179
  else if (drizzle->options.is_assert_dangling)
168
180
  {
169
181
    assert(drizzle->con_list == NULL);
170
182
    assert(drizzle->query_list == NULL);
172
184
 
173
185
  free(drizzle->pfds);
174
186
 
175
 
  if (drizzle->options & DRIZZLE_ALLOCATED)
 
187
  if (drizzle->options.is_allocated)
 
188
  {
176
189
    delete drizzle;
 
190
  }
177
191
#if defined(_WIN32)
178
192
  /* if it is MS windows, invoke WSACleanup() at the end*/
179
193
  WSACleanup();
200
214
  return drizzle->sqlstate;
201
215
}
202
216
 
203
 
int drizzle_options(const drizzle_st *drizzle)
204
 
{
205
 
  return drizzle->options;
206
 
}
207
 
 
208
 
void drizzle_set_options(drizzle_st *drizzle, int options)
209
 
{
210
 
  drizzle->options= options;
211
 
}
212
 
 
213
 
void drizzle_add_options(drizzle_st *drizzle, int options)
214
 
{
215
 
  drizzle->options|= options;
216
 
}
217
 
 
218
 
void drizzle_remove_options(drizzle_st *drizzle, drizzle_options_t options)
219
 
{
220
 
  drizzle->options&= ~options;
 
217
int drizzle_options(const drizzle_st *)
 
218
{
 
219
  return 0;
 
220
}
 
221
 
 
222
void drizzle_set_options(drizzle_st *, int)
 
223
{
 
224
}
 
225
 
 
226
void drizzle_add_options(drizzle_st *, int)
 
227
{
221
228
}
222
229
 
223
230
void *drizzle_context(const drizzle_st *drizzle)
275
282
{
276
283
  if (con == NULL)
277
284
  {
278
 
    con= new drizzle_con_st;
 
285
    con= new (std::nothrow) drizzle_con_st;
279
286
    con->options= DRIZZLE_CON_ALLOCATED;
280
287
  }
281
288
  else
 
289
  {
282
290
    con->options= 0;
 
291
  }
283
292
 
284
293
  if (drizzle->con_list != NULL)
285
294
    drizzle->con_list->prev= con;
 
295
 
286
296
  con->next= drizzle->con_list;
287
297
  con->prev= NULL;
288
298
  drizzle->con_list= con;
341
351
{
342
352
  con= drizzle_con_create(drizzle, con);
343
353
  if (con == NULL)
 
354
  {
344
355
    return NULL;
 
356
  }
345
357
 
346
358
  /* Clear "operational" options such as IO status. */
347
359
  con->options|= (from->options & ~(DRIZZLE_CON_ALLOCATED|DRIZZLE_CON_READY|
369
381
void drizzle_con_free(drizzle_con_st *con)
370
382
{
371
383
  if (con->context != NULL && con->context_free_fn != NULL)
 
384
  {
372
385
    con->context_free_fn(con, con->context);
 
386
  }
373
387
 
374
 
  if (con->drizzle->options & DRIZZLE_FREE_OBJECTS)
 
388
  if (con->drizzle->options.is_free_objects)
 
389
  {
375
390
    drizzle_result_free_all(con);
376
 
  else if (con->drizzle->options & DRIZZLE_ASSERT_DANGLING)
 
391
  }
 
392
  else if (con->drizzle->options.is_assert_dangling)
 
393
  {
377
394
    assert(con->result_list == NULL);
 
395
  }
378
396
 
379
397
  if (con->fd != -1)
 
398
  {
380
399
    drizzle_con_close(con);
 
400
  }
381
401
 
382
402
  drizzle_con_reset_addrinfo(con);
383
403
 
384
404
  if (con->drizzle->con_list == con)
 
405
  {
385
406
    con->drizzle->con_list= con->next;
 
407
  }
386
408
  if (con->prev != NULL)
 
409
  {
387
410
    con->prev->next= con->next;
 
411
  }
388
412
  if (con->next != NULL)
 
413
  {
389
414
    con->next->prev= con->prev;
 
415
  }
390
416
  con->drizzle->con_count--;
391
417
 
392
418
  if (con->options & DRIZZLE_CON_ALLOCATED)
 
419
  {
393
420
    delete con;
 
421
  }
394
422
}
395
423
 
396
424
void drizzle_con_free_all(drizzle_st *drizzle)
397
425
{
398
426
  while (drizzle->con_list != NULL)
 
427
  {
399
428
    drizzle_con_free(drizzle->con_list);
 
429
  }
400
430
}
401
431
 
402
432
drizzle_return_t drizzle_con_wait(drizzle_st *drizzle)
418
448
    drizzle->pfds_size= drizzle->con_count;
419
449
  }
420
450
  else
 
451
  {
421
452
    pfds= drizzle->pfds;
 
453
  }
422
454
 
423
455
  uint32_t x= 0;
424
456
  for (drizzle_con_st* con= drizzle->con_list; con != NULL; con= con->next)
528
560
{
529
561
  con= drizzle_con_create(drizzle, con);
530
562
  if (con == NULL)
 
563
  {
531
564
    return NULL;
 
565
  }
532
566
 
533
567
  drizzle_con_set_tcp(con, host, port);
534
568
  drizzle_con_set_auth(con, user, password);
545
579
{
546
580
  con= drizzle_con_create(drizzle, con);
547
581
  if (con == NULL)
 
582
  {
548
583
    return NULL;
 
584
  }
549
585
 
550
586
  drizzle_con_set_uds(con, uds);
551
587
  drizzle_con_set_auth(con, user, password);
567
603
{
568
604
  con= drizzle_con_create(drizzle, con);
569
605
  if (con == NULL)
 
606
  {
570
607
    return NULL;
 
608
  }
571
609
 
572
610
  drizzle_con_set_tcp(con, host, port);
573
611
  drizzle_con_set_backlog(con, backlog);
583
621
{
584
622
  con= drizzle_con_create(drizzle, con);
585
623
  if (con == NULL)
 
624
  {
586
625
    return NULL;
 
626
  }
587
627
 
588
628
  drizzle_con_set_uds(con, uds);
589
629
  drizzle_con_set_backlog(con, backlog);
623
663
      return con;
624
664
    }
625
665
 
626
 
    if (drizzle->options & DRIZZLE_NON_BLOCKING)
 
666
    if (drizzle->options.is_non_blocking)
627
667
    {
628
668
      *ret_ptr= DRIZZLE_RETURN_IO_WAIT;
629
669
      return NULL;
662
702
  int written= vsnprintf(ptr, DRIZZLE_MAX_ERROR_SIZE - size, format, args);
663
703
  va_end(args);
664
704
 
665
 
  if (written < 0) size= DRIZZLE_MAX_ERROR_SIZE;
666
 
  else size+= written;
 
705
  if (written < 0) 
 
706
  {
 
707
    size= DRIZZLE_MAX_ERROR_SIZE;
 
708
  }
 
709
  else 
 
710
  {
 
711
    size+= written;
 
712
  }
 
713
 
667
714
  if (size >= DRIZZLE_MAX_ERROR_SIZE)
 
715
  {
668
716
    size= DRIZZLE_MAX_ERROR_SIZE - 1;
 
717
  }
669
718
  log_buffer[size]= 0;
670
719
 
671
720
  if (drizzle->log_fn == NULL)
 
721
  {
672
722
    memcpy(drizzle->last_error, log_buffer, size + 1);
 
723
  }
673
724
  else
 
725
  {
674
726
    drizzle->log_fn(log_buffer, DRIZZLE_VERBOSE_ERROR, drizzle->log_context);
 
727
  }
675
728
}
676
729
 
677
730
void drizzle_log(drizzle_st *drizzle, drizzle_verbose_t verbose,