~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-22 17:04:41 UTC
  • mfrom: (2462.1.3 drizzle-include)
  • Revision ID: me@mark.atwood.name-20111122170441-5dehm0e0ax20z19b
mergeĀ lp:~brianaker/drizzle/fedora-16-fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
86
86
  return _verbose_name[verbose];
87
87
}
88
88
 
89
 
drizzle_st *drizzle_create()
 
89
drizzle_st *drizzle_create(drizzle_st *drizzle)
90
90
{
91
91
#if defined(_WIN32)
92
92
  /* if it is MS windows, invoke WSAStartup */
101
101
  sigaction(SIGPIPE, &act, NULL);
102
102
#endif
103
103
 
104
 
  drizzle_st *drizzle= new (std::nothrow) drizzle_st;
105
 
 
106
104
  if (drizzle == NULL)
107
105
  {
108
 
    return NULL;
 
106
    drizzle= new (std::nothrow) drizzle_st;
 
107
 
 
108
    if (drizzle == NULL)
 
109
    {
 
110
      return NULL;
 
111
    }
 
112
    drizzle->options.is_allocated= true;
109
113
  }
110
114
 
111
115
  /* @todo remove this default free flag with new API. */
136
140
  return drizzle;
137
141
}
138
142
 
139
 
drizzle_st *drizzle_clone(const drizzle_st *source)
 
143
drizzle_st *drizzle_clone(drizzle_st *drizzle, const drizzle_st *from)
140
144
{
141
 
  drizzle_st *drizzle= drizzle_create();
 
145
  drizzle= drizzle_create(drizzle);
142
146
  if (drizzle == NULL)
143
147
  {
144
148
    return NULL;
145
149
  }
146
150
 
147
 
  for (drizzle_con_st* con= source->con_list; con != NULL; con= con->next)
 
151
  bool cache_state= drizzle->options.is_allocated;
 
152
  drizzle->options= from->options;
 
153
  drizzle->options.is_allocated= cache_state;
 
154
 
 
155
  for (drizzle_con_st* con= from->con_list; con != NULL; con= con->next)
148
156
  {
149
 
    if (drizzle_con_clone(drizzle, con) == NULL)
 
157
    if (drizzle_con_clone(drizzle, NULL, con) == NULL)
150
158
    {
151
159
      drizzle_free(drizzle);
152
160
      return NULL;
176
184
 
177
185
  free(drizzle->pfds);
178
186
 
179
 
  delete drizzle;
 
187
  if (drizzle->options.is_allocated)
 
188
  {
 
189
    delete drizzle;
 
190
  }
180
191
#if defined(_WIN32)
181
192
  /* if it is MS windows, invoke WSACleanup() at the end*/
182
193
  WSACleanup();
188
199
  return drizzle->last_error;
189
200
}
190
201
 
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
202
int drizzle_errno(const drizzle_st *drizzle)
213
203
{
214
204
  return drizzle->last_errno;
224
214
  return drizzle->sqlstate;
225
215
}
226
216
 
 
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
{
 
228
}
 
229
 
227
230
void *drizzle_context(const drizzle_st *drizzle)
228
231
{
229
232
  return drizzle->context;
275
278
  drizzle->event_watch_context= context;
276
279
}
277
280
 
278
 
drizzle_con_st *drizzle_con_create(drizzle_st *drizzle)
 
281
drizzle_con_st *drizzle_con_create(drizzle_st *drizzle, drizzle_con_st *con)
279
282
{
280
 
  if (drizzle == NULL)
281
 
  {
282
 
    return NULL;
283
 
  }
284
 
 
285
 
  drizzle_con_st *con= new (std::nothrow) drizzle_con_st;
286
 
 
287
283
  if (con == NULL)
288
284
  {
289
 
    return NULL;
 
285
    con= new (std::nothrow) drizzle_con_st;
 
286
    con->options= DRIZZLE_CON_ALLOCATED;
 
287
  }
 
288
  else
 
289
  {
 
290
    con->options= 0;
290
291
  }
291
292
 
292
293
  if (drizzle->con_list != NULL)
293
 
  {
294
294
    drizzle->con_list->prev= con;
295
 
  }
296
295
 
297
296
  con->next= drizzle->con_list;
298
297
  con->prev= NULL;
347
346
  return con;
348
347
}
349
348
 
350
 
drizzle_con_st *drizzle_con_clone(drizzle_st *drizzle, drizzle_con_st *source)
 
349
drizzle_con_st *drizzle_con_clone(drizzle_st *drizzle, drizzle_con_st *con,
 
350
                                  const drizzle_con_st *from)
351
351
{
352
 
  drizzle_con_st *con= drizzle_con_create(drizzle);
 
352
  con= drizzle_con_create(drizzle, con);
353
353
  if (con == NULL)
354
354
  {
355
355
    return NULL;
356
356
  }
357
357
 
358
358
  /* Clear "operational" options such as IO status. */
359
 
  con->options|= (source->options & ~(DRIZZLE_CON_ALLOCATED|DRIZZLE_CON_READY|
 
359
  con->options|= (from->options & ~(DRIZZLE_CON_ALLOCATED|DRIZZLE_CON_READY|
360
360
                  DRIZZLE_CON_NO_RESULT_READ|DRIZZLE_CON_IO_READY|
361
361
                  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);
 
362
  con->backlog= from->backlog;
 
363
  strcpy(con->schema, from->schema);
 
364
  strcpy(con->password, from->password);
 
365
  strcpy(con->user, from->user);
366
366
 
367
 
  switch (source->socket_type)
 
367
  switch (from->socket_type)
368
368
  {
369
369
  case DRIZZLE_CON_SOCKET_TCP:
370
 
    drizzle_con_set_tcp(con, source->socket.tcp.host, source->socket.tcp.port);
 
370
    drizzle_con_set_tcp(con, from->socket.tcp.host, from->socket.tcp.port);
371
371
    break;
372
372
 
373
373
  case DRIZZLE_CON_SOCKET_UDS:
374
 
    drizzle_con_set_uds(con, source->socket.uds.sockaddr.sun_path);
 
374
    drizzle_con_set_uds(con, from->socket.uds.sockaddr.sun_path);
375
375
    break;
376
376
  }
377
377
 
415
415
  }
416
416
  con->drizzle->con_count--;
417
417
 
418
 
  delete con;
 
418
  if (con->options & DRIZZLE_CON_ALLOCATED)
 
419
  {
 
420
    delete con;
 
421
  }
419
422
}
420
423
 
421
424
void drizzle_con_free_all(drizzle_st *drizzle)
549
552
 * Client Definitions
550
553
 */
551
554
 
552
 
drizzle_con_st *drizzle_con_add_tcp(drizzle_st *drizzle,
 
555
drizzle_con_st *drizzle_con_add_tcp(drizzle_st *drizzle, drizzle_con_st *con,
553
556
                                    const char *host, in_port_t port,
554
557
                                    const char *user, const char *password,
555
558
                                    const char *db,
556
559
                                    drizzle_con_options_t options)
557
560
{
558
 
  drizzle_con_st *con= drizzle_con_create(drizzle);
 
561
  con= drizzle_con_create(drizzle, con);
559
562
  if (con == NULL)
560
563
  {
561
564
    return NULL;
569
572
  return con;
570
573
}
571
574
 
572
 
drizzle_con_st *drizzle_con_add_uds(drizzle_st *drizzle,
 
575
drizzle_con_st *drizzle_con_add_uds(drizzle_st *drizzle, drizzle_con_st *con,
573
576
                                    const char *uds, const char *user,
574
577
                                    const char *password, const char *db,
575
578
                                    drizzle_con_options_t options)
576
579
{
577
 
  drizzle_con_st *con= drizzle_con_create(drizzle);
 
580
  con= drizzle_con_create(drizzle, con);
578
581
  if (con == NULL)
579
582
  {
580
583
    return NULL;
593
596
 */
594
597
 
595
598
drizzle_con_st *drizzle_con_add_tcp_listen(drizzle_st *drizzle,
 
599
                                           drizzle_con_st *con,
596
600
                                           const char *host, in_port_t port,
597
601
                                           int backlog,
598
602
                                           drizzle_con_options_t options)
599
603
{
600
 
  drizzle_con_st *con= drizzle_con_create(drizzle);
 
604
  con= drizzle_con_create(drizzle, con);
601
605
  if (con == NULL)
602
606
  {
603
607
    return NULL;
611
615
}
612
616
 
613
617
drizzle_con_st *drizzle_con_add_uds_listen(drizzle_st *drizzle,
 
618
                                           drizzle_con_st *con,
614
619
                                           const char *uds, int backlog,
615
620
                                           drizzle_con_options_t options)
616
621
{
617
 
  drizzle_con_st *con= drizzle_con_create(drizzle);
 
622
  con= drizzle_con_create(drizzle, con);
618
623
  if (con == NULL)
619
624
  {
620
625
    return NULL;
627
632
  return con;
628
633
}
629
634
 
630
 
drizzle_con_st *drizzle_con_accept(drizzle_st *drizzle,
 
635
drizzle_con_st *drizzle_con_accept(drizzle_st *drizzle, drizzle_con_st *con,
631
636
                                   drizzle_return_t *ret_ptr)
632
637
{
633
 
  drizzle_return_t unused;
634
 
  if (ret_ptr == NULL)
635
 
  {
636
 
    ret_ptr= &unused;
637
 
  }
638
 
 
639
638
  while (1)
640
639
  {
641
640
    if (drizzle_con_st* ready= drizzle_con_ready_listen(drizzle))
642
641
    {
643
642
      int fd= accept(ready->fd, NULL, NULL);
644
643
 
645
 
      drizzle_con_st *con= drizzle_con_create(drizzle);
 
644
      con= drizzle_con_create(drizzle, con);
646
645
      if (con == NULL)
647
646
      {
648
647
        (void)closesocket(fd);
673
672
    for (drizzle_con_st* ready= drizzle->con_list; ready != NULL; ready= ready->next)
674
673
    {
675
674
      if (ready->options & DRIZZLE_CON_LISTEN)
676
 
      {
677
675
        drizzle_con_set_events(ready, POLLIN);
678
 
      }
679
676
    }
680
677
 
681
678
    *ret_ptr= drizzle_con_wait(drizzle);
682
679
    if (*ret_ptr != DRIZZLE_RETURN_OK)
683
 
    {
684
680
      return NULL;
685
 
    }
686
681
  }
687
682
}
688
683
 
693
688
void drizzle_set_error(drizzle_st *drizzle, const char *function,
694
689
                       const char *format, ...)
695
690
{
696
 
  if (drizzle == NULL)
697
 
  {
698
 
    return;
699
 
  }
700
691
  char log_buffer[DRIZZLE_MAX_ERROR_SIZE];
701
692
 
702
693
  size_t size= strlen(function);
741
732
{
742
733
  char log_buffer[DRIZZLE_MAX_ERROR_SIZE];
743
734
 
744
 
  if (drizzle == NULL)
745
 
  {
746
 
    return;
747
 
  }
748
 
 
749
735
  if (drizzle->log_fn == NULL)
750
736
  {
751
737
    printf("%5s: ", drizzle_verbose_name(verbose));