~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-30 07:06:47 UTC
  • mfrom: (2463.1.6 drizzle-include)
  • Revision ID: me@mark.atwood.name-20111130070647-ixp7oalp70hkbt6l
mergeĀ lp:~brianaker/drizzle/libdrizzle-2.0-not-install

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