~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle-2.0/drizzle.cc

  • Committer: Mark Atwood
  • Date: 2011-11-14 07:30:41 UTC
  • Revision ID: me@mark.atwood.name-20111114073041-mo2hgg8ouseo2kpu
releaseĀ 2011.11.29

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
 
  {
83
82
    return "UNKNOWN";
84
 
  }
85
83
 
86
84
  return _verbose_name[verbose];
87
85
}
88
86
 
89
 
drizzle_st *drizzle_create()
 
87
drizzle_st *drizzle_create(drizzle_st *drizzle)
90
88
{
91
89
#if defined(_WIN32)
92
90
  /* if it is MS windows, invoke WSAStartup */
101
99
  sigaction(SIGPIPE, &act, NULL);
102
100
#endif
103
101
 
104
 
  drizzle_st *drizzle= new (std::nothrow) drizzle_st;
105
 
 
106
102
  if (drizzle == NULL)
107
103
  {
108
 
    return NULL;
 
104
    drizzle= new drizzle_st;
 
105
    drizzle->options= DRIZZLE_ALLOCATED;
109
106
  }
 
107
  else
 
108
    drizzle->options= DRIZZLE_NONE;
110
109
 
111
110
  /* @todo remove this default free flag with new API. */
112
 
  drizzle->options.is_free_objects= true;
 
111
  drizzle->options|= DRIZZLE_FREE_OBJECTS;
113
112
  drizzle->error_code= 0;
114
 
 
115
113
  /* drizzle->options set above */
116
114
  drizzle->verbose= DRIZZLE_VERBOSE_NEVER;
117
115
  drizzle->con_count= 0;
136
134
  return drizzle;
137
135
}
138
136
 
139
 
drizzle_st *drizzle_clone(const drizzle_st *source)
 
137
drizzle_st *drizzle_clone(drizzle_st *drizzle, const drizzle_st *from)
140
138
{
141
 
  drizzle_st *drizzle= drizzle_create();
 
139
  drizzle= drizzle_create(drizzle);
142
140
  if (drizzle == NULL)
143
 
  {
144
141
    return NULL;
145
 
  }
146
 
 
147
 
  for (drizzle_con_st* con= source->con_list; con != NULL; con= con->next)
 
142
 
 
143
  drizzle->options|= (from->options & ~DRIZZLE_ALLOCATED);
 
144
 
 
145
  for (drizzle_con_st* con= from->con_list; con != NULL; con= con->next)
148
146
  {
149
 
    if (drizzle_con_clone(drizzle, con) == NULL)
 
147
    if (drizzle_con_clone(drizzle, NULL, con) == NULL)
150
148
    {
151
149
      drizzle_free(drizzle);
152
150
      return NULL;
159
157
void drizzle_free(drizzle_st *drizzle)
160
158
{
161
159
  if (drizzle->context != NULL && drizzle->context_free_fn != NULL)
162
 
  {
163
160
    drizzle->context_free_fn(drizzle, drizzle->context);
164
 
  }
165
161
 
166
 
  if (drizzle->options.is_free_objects)
 
162
  if (drizzle->options & DRIZZLE_FREE_OBJECTS)
167
163
  {
168
164
    drizzle_con_free_all(drizzle);
169
165
    drizzle_query_free_all(drizzle);
170
166
  }
171
 
  else if (drizzle->options.is_assert_dangling)
 
167
  else if (drizzle->options & DRIZZLE_ASSERT_DANGLING)
172
168
  {
173
169
    assert(drizzle->con_list == NULL);
174
170
    assert(drizzle->query_list == NULL);
176
172
 
177
173
  free(drizzle->pfds);
178
174
 
179
 
  delete drizzle;
 
175
  if (drizzle->options & DRIZZLE_ALLOCATED)
 
176
    delete drizzle;
180
177
#if defined(_WIN32)
181
178
  /* if it is MS windows, invoke WSACleanup() at the end*/
182
179
  WSACleanup();
188
185
  return drizzle->last_error;
189
186
}
190
187
 
191
 
drizzle_return_t drizzle_set_option(drizzle_st *drizzle, drizzle_options_t arg, bool set)
192
 
{
193
 
  switch (arg)
194
 
  {
195
 
  case DRIZZLE_NON_BLOCKING:
196
 
    drizzle->options.is_non_blocking= set;
197
 
    return DRIZZLE_RETURN_OK;
198
 
 
199
 
  case DRIZZLE_FREE_OBJECTS:
200
 
    return DRIZZLE_RETURN_OK;
201
 
 
202
 
  case DRIZZLE_ASSERT_DANGLING:
203
 
    return DRIZZLE_RETURN_OK;
204
 
 
205
 
  default:
206
 
    break;
207
 
  }
208
 
 
209
 
  return DRIZZLE_RETURN_INVALID_ARGUMENT;
210
 
}
211
 
 
212
188
int drizzle_errno(const drizzle_st *drizzle)
213
189
{
214
190
  return drizzle->last_errno;
224
200
  return drizzle->sqlstate;
225
201
}
226
202
 
 
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;
 
221
}
 
222
 
227
223
void *drizzle_context(const drizzle_st *drizzle)
228
224
{
229
225
  return drizzle->context;
275
271
  drizzle->event_watch_context= context;
276
272
}
277
273
 
278
 
drizzle_con_st *drizzle_con_create(drizzle_st *drizzle)
 
274
drizzle_con_st *drizzle_con_create(drizzle_st *drizzle, drizzle_con_st *con)
279
275
{
280
 
  if (drizzle == NULL)
281
 
  {
282
 
    return NULL;
283
 
  }
284
 
 
285
 
  drizzle_con_st *con= new (std::nothrow) drizzle_con_st;
286
 
 
287
276
  if (con == NULL)
288
277
  {
289
 
    return NULL;
 
278
    con= new drizzle_con_st;
 
279
    con->options= DRIZZLE_CON_ALLOCATED;
290
280
  }
 
281
  else
 
282
    con->options= 0;
291
283
 
292
284
  if (drizzle->con_list != NULL)
293
 
  {
294
285
    drizzle->con_list->prev= con;
295
 
  }
296
 
 
297
286
  con->next= drizzle->con_list;
298
287
  con->prev= NULL;
299
288
  drizzle->con_list= con;
337
326
  con->socket.tcp.host= NULL;
338
327
  con->socket.tcp.port= 0;
339
328
  /* con->buffer doesn't need to be set */
340
 
  con->schema[0]= 0;
 
329
  con->db[0]= 0;
341
330
  con->password[0]= 0;
342
331
  /* con->scramble_buffer doesn't need to be set */
343
332
  con->server_version[0]= 0;
347
336
  return con;
348
337
}
349
338
 
350
 
drizzle_con_st *drizzle_con_clone(drizzle_st *drizzle, drizzle_con_st *source)
 
339
drizzle_con_st *drizzle_con_clone(drizzle_st *drizzle, drizzle_con_st *con,
 
340
                                  const drizzle_con_st *from)
351
341
{
352
 
  drizzle_con_st *con= drizzle_con_create(drizzle);
 
342
  con= drizzle_con_create(drizzle, con);
353
343
  if (con == NULL)
354
 
  {
355
344
    return NULL;
356
 
  }
357
345
 
358
346
  /* Clear "operational" options such as IO status. */
359
 
  con->options|= (source->options & ~(DRIZZLE_CON_ALLOCATED|DRIZZLE_CON_READY|
 
347
  con->options|= (from->options & ~(DRIZZLE_CON_ALLOCATED|DRIZZLE_CON_READY|
360
348
                  DRIZZLE_CON_NO_RESULT_READ|DRIZZLE_CON_IO_READY|
361
349
                  DRIZZLE_CON_LISTEN));
362
 
  con->backlog= source->backlog;
363
 
  strcpy(con->schema, source->schema);
364
 
  strcpy(con->password, source->password);
365
 
  strcpy(con->user, source->user);
 
350
  con->backlog= from->backlog;
 
351
  strcpy(con->db, from->db);
 
352
  strcpy(con->password, from->password);
 
353
  strcpy(con->user, from->user);
366
354
 
367
 
  switch (source->socket_type)
 
355
  switch (from->socket_type)
368
356
  {
369
357
  case DRIZZLE_CON_SOCKET_TCP:
370
 
    drizzle_con_set_tcp(con, source->socket.tcp.host, source->socket.tcp.port);
 
358
    drizzle_con_set_tcp(con, from->socket.tcp.host, from->socket.tcp.port);
371
359
    break;
372
360
 
373
361
  case DRIZZLE_CON_SOCKET_UDS:
374
 
    drizzle_con_set_uds(con, source->socket.uds.sockaddr.sun_path);
 
362
    drizzle_con_set_uds(con, from->socket.uds.sockaddr.sun_path);
375
363
    break;
376
364
  }
377
365
 
381
369
void drizzle_con_free(drizzle_con_st *con)
382
370
{
383
371
  if (con->context != NULL && con->context_free_fn != NULL)
384
 
  {
385
372
    con->context_free_fn(con, con->context);
386
 
  }
387
373
 
388
 
  if (con->drizzle->options.is_free_objects)
389
 
  {
 
374
  if (con->drizzle->options & DRIZZLE_FREE_OBJECTS)
390
375
    drizzle_result_free_all(con);
391
 
  }
392
 
  else if (con->drizzle->options.is_assert_dangling)
393
 
  {
 
376
  else if (con->drizzle->options & DRIZZLE_ASSERT_DANGLING)
394
377
    assert(con->result_list == NULL);
395
 
  }
396
378
 
397
379
  if (con->fd != -1)
398
 
  {
399
380
    drizzle_con_close(con);
400
 
  }
401
381
 
402
382
  drizzle_con_reset_addrinfo(con);
403
383
 
404
384
  if (con->drizzle->con_list == con)
405
 
  {
406
385
    con->drizzle->con_list= con->next;
407
 
  }
408
386
  if (con->prev != NULL)
409
 
  {
410
387
    con->prev->next= con->next;
411
 
  }
412
388
  if (con->next != NULL)
413
 
  {
414
389
    con->next->prev= con->prev;
415
 
  }
416
390
  con->drizzle->con_count--;
417
391
 
418
 
  delete con;
 
392
  if (con->options & DRIZZLE_CON_ALLOCATED)
 
393
    delete con;
419
394
}
420
395
 
421
396
void drizzle_con_free_all(drizzle_st *drizzle)
422
397
{
423
398
  while (drizzle->con_list != NULL)
424
 
  {
425
399
    drizzle_con_free(drizzle->con_list);
426
 
  }
427
400
}
428
401
 
429
402
drizzle_return_t drizzle_con_wait(drizzle_st *drizzle)
445
418
    drizzle->pfds_size= drizzle->con_count;
446
419
  }
447
420
  else
448
 
  {
449
421
    pfds= drizzle->pfds;
450
 
  }
451
422
 
452
423
  uint32_t x= 0;
453
424
  for (drizzle_con_st* con= drizzle->con_list; con != NULL; con= con->next)
549
520
 * Client Definitions
550
521
 */
551
522
 
552
 
drizzle_con_st *drizzle_con_add_tcp(drizzle_st *drizzle,
 
523
drizzle_con_st *drizzle_con_add_tcp(drizzle_st *drizzle, drizzle_con_st *con,
553
524
                                    const char *host, in_port_t port,
554
525
                                    const char *user, const char *password,
555
526
                                    const char *db,
556
527
                                    drizzle_con_options_t options)
557
528
{
558
 
  drizzle_con_st *con= drizzle_con_create(drizzle);
 
529
  con= drizzle_con_create(drizzle, con);
559
530
  if (con == NULL)
560
 
  {
561
531
    return NULL;
562
 
  }
563
532
 
564
533
  drizzle_con_set_tcp(con, host, port);
565
534
  drizzle_con_set_auth(con, user, password);
569
538
  return con;
570
539
}
571
540
 
572
 
drizzle_con_st *drizzle_con_add_uds(drizzle_st *drizzle,
 
541
drizzle_con_st *drizzle_con_add_uds(drizzle_st *drizzle, drizzle_con_st *con,
573
542
                                    const char *uds, const char *user,
574
543
                                    const char *password, const char *db,
575
544
                                    drizzle_con_options_t options)
576
545
{
577
 
  drizzle_con_st *con= drizzle_con_create(drizzle);
 
546
  con= drizzle_con_create(drizzle, con);
578
547
  if (con == NULL)
579
 
  {
580
548
    return NULL;
581
 
  }
582
549
 
583
550
  drizzle_con_set_uds(con, uds);
584
551
  drizzle_con_set_auth(con, user, password);
593
560
 */
594
561
 
595
562
drizzle_con_st *drizzle_con_add_tcp_listen(drizzle_st *drizzle,
 
563
                                           drizzle_con_st *con,
596
564
                                           const char *host, in_port_t port,
597
565
                                           int backlog,
598
566
                                           drizzle_con_options_t options)
599
567
{
600
 
  drizzle_con_st *con= drizzle_con_create(drizzle);
 
568
  con= drizzle_con_create(drizzle, con);
601
569
  if (con == NULL)
602
 
  {
603
570
    return NULL;
604
 
  }
605
571
 
606
572
  drizzle_con_set_tcp(con, host, port);
607
573
  drizzle_con_set_backlog(con, backlog);
611
577
}
612
578
 
613
579
drizzle_con_st *drizzle_con_add_uds_listen(drizzle_st *drizzle,
 
580
                                           drizzle_con_st *con,
614
581
                                           const char *uds, int backlog,
615
582
                                           drizzle_con_options_t options)
616
583
{
617
 
  drizzle_con_st *con= drizzle_con_create(drizzle);
 
584
  con= drizzle_con_create(drizzle, con);
618
585
  if (con == NULL)
619
 
  {
620
586
    return NULL;
621
 
  }
622
587
 
623
588
  drizzle_con_set_uds(con, uds);
624
589
  drizzle_con_set_backlog(con, backlog);
627
592
  return con;
628
593
}
629
594
 
630
 
drizzle_con_st *drizzle_con_accept(drizzle_st *drizzle,
 
595
drizzle_con_st *drizzle_con_accept(drizzle_st *drizzle, drizzle_con_st *con,
631
596
                                   drizzle_return_t *ret_ptr)
632
597
{
633
 
  drizzle_return_t unused;
634
 
  if (ret_ptr == NULL)
635
 
  {
636
 
    ret_ptr= &unused;
637
 
  }
638
 
 
639
598
  while (1)
640
599
  {
641
600
    if (drizzle_con_st* ready= drizzle_con_ready_listen(drizzle))
642
601
    {
643
602
      int fd= accept(ready->fd, NULL, NULL);
644
603
 
645
 
      drizzle_con_st *con= drizzle_con_create(drizzle);
 
604
      con= drizzle_con_create(drizzle, con);
646
605
      if (con == NULL)
647
606
      {
648
607
        (void)closesocket(fd);
664
623
      return con;
665
624
    }
666
625
 
667
 
    if (drizzle->options.is_non_blocking)
 
626
    if (drizzle->options & DRIZZLE_NON_BLOCKING)
668
627
    {
669
628
      *ret_ptr= DRIZZLE_RETURN_IO_WAIT;
670
629
      return NULL;
673
632
    for (drizzle_con_st* ready= drizzle->con_list; ready != NULL; ready= ready->next)
674
633
    {
675
634
      if (ready->options & DRIZZLE_CON_LISTEN)
676
 
      {
677
635
        drizzle_con_set_events(ready, POLLIN);
678
 
      }
679
636
    }
680
637
 
681
638
    *ret_ptr= drizzle_con_wait(drizzle);
682
639
    if (*ret_ptr != DRIZZLE_RETURN_OK)
683
 
    {
684
640
      return NULL;
685
 
    }
686
641
  }
687
642
}
688
643
 
693
648
void drizzle_set_error(drizzle_st *drizzle, const char *function,
694
649
                       const char *format, ...)
695
650
{
696
 
  if (drizzle == NULL)
697
 
  {
698
 
    return;
699
 
  }
700
651
  char log_buffer[DRIZZLE_MAX_ERROR_SIZE];
701
652
 
702
653
  size_t size= strlen(function);
711
662
  int written= vsnprintf(ptr, DRIZZLE_MAX_ERROR_SIZE - size, format, args);
712
663
  va_end(args);
713
664
 
714
 
  if (written < 0) 
715
 
  {
716
 
    size= DRIZZLE_MAX_ERROR_SIZE;
717
 
  }
718
 
  else 
719
 
  {
720
 
    size+= written;
721
 
  }
722
 
 
 
665
  if (written < 0) size= DRIZZLE_MAX_ERROR_SIZE;
 
666
  else size+= written;
723
667
  if (size >= DRIZZLE_MAX_ERROR_SIZE)
724
 
  {
725
668
    size= DRIZZLE_MAX_ERROR_SIZE - 1;
726
 
  }
727
669
  log_buffer[size]= 0;
728
670
 
729
671
  if (drizzle->log_fn == NULL)
730
 
  {
731
672
    memcpy(drizzle->last_error, log_buffer, size + 1);
732
 
  }
733
673
  else
734
 
  {
735
674
    drizzle->log_fn(log_buffer, DRIZZLE_VERBOSE_ERROR, drizzle->log_context);
736
 
  }
737
675
}
738
676
 
739
677
void drizzle_log(drizzle_st *drizzle, drizzle_verbose_t verbose,
741
679
{
742
680
  char log_buffer[DRIZZLE_MAX_ERROR_SIZE];
743
681
 
744
 
  if (drizzle == NULL)
745
 
  {
746
 
    return;
747
 
  }
748
 
 
749
682
  if (drizzle->log_fn == NULL)
750
683
  {
751
684
    printf("%5s: ", drizzle_verbose_name(verbose));