~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/drizzle.c

  • Committer: Joe Daly
  • Date: 2010-03-08 04:23:54 UTC
  • mto: This revision was merged to the branch mainline in revision 1380.
  • Revision ID: skinny.moey@gmail.com-20100308042354-7k0jibdqaxkhac7o
scoreboardĀ implementationĀ forĀ statistics

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Drizzle Client & Protocol Library
3
 
 *
4
 
 * Copyright (C) 2008 Eric Day (eday@oddments.org)
5
 
 * All rights reserved.
6
 
 *
7
 
 * Use and distribution licensed under the BSD license.  See
8
 
 * the COPYING.BSD file in the root source directory for full text.
9
 
 */
10
 
 
11
 
/**
12
 
 * @file
13
 
 * @brief Drizzle Definitions
14
 
 */
15
 
 
16
 
#include "libdrizzle/common.h"
17
 
 
18
 
 
19
 
/**
20
 
 * @addtogroup drizzle_static Static Drizzle Declarations
21
 
 * @ingroup drizzle
22
 
 * @{
23
 
 */
24
 
 
25
 
/**
26
 
 * Names of the verbose levels provided.
27
 
 */
28
 
static const char *_verbose_name[DRIZZLE_VERBOSE_MAX]=
29
 
{
30
 
  "NEVER",
31
 
  "FATAL",
32
 
  "ERROR",
33
 
  "INFO",
34
 
  "DEBUG",
35
 
  "CRAZY"
36
 
};
37
 
 
38
 
/** @} */
39
 
 
40
 
/*
41
 
 * Common Definitions
42
 
 */
43
 
 
44
 
const char *drizzle_version(void)
45
 
{
46
 
  return PACKAGE_VERSION;
47
 
}
48
 
 
49
 
const char *drizzle_bugreport(void)
50
 
{
51
 
  return PACKAGE_BUGREPORT;
52
 
}
53
 
 
54
 
const char *drizzle_verbose_name(drizzle_verbose_t verbose)
55
 
{
56
 
  if (verbose >= DRIZZLE_VERBOSE_MAX)
57
 
    return "UNKNOWN";
58
 
 
59
 
  return _verbose_name[verbose];
60
 
}
61
 
 
62
 
drizzle_st *drizzle_create(drizzle_st *drizzle)
63
 
{
64
 
#if defined(_WIN32)
65
 
  /* if it is MS windows, invoke WSAStartup */
66
 
  WSADATA wsaData;
67
 
  if ( WSAStartup( MAKEWORD(2,2), &wsaData ) != 0 )
68
 
    printf("Error at WSAStartup()\n");
69
 
#endif
70
 
  struct sigaction act;
71
 
  memset(&act, 0, sizeof(act));
72
 
 
73
 
  act.sa_handler = SIG_IGN;
74
 
  sigaction(SIGPIPE, &act, NULL);
75
 
 
76
 
  if (drizzle == NULL)
77
 
  {
78
 
    drizzle= malloc(sizeof(drizzle_st));
79
 
    if (drizzle == NULL)
80
 
      return NULL;
81
 
 
82
 
    drizzle->options= DRIZZLE_ALLOCATED;
83
 
  }
84
 
  else
85
 
    drizzle->options= DRIZZLE_NONE;
86
 
 
87
 
  /* @todo remove this default free flag with new API. */
88
 
  drizzle->options|= DRIZZLE_FREE_OBJECTS;
89
 
  drizzle->error_code= 0;
90
 
  /* drizzle->options set above */
91
 
  drizzle->verbose= DRIZZLE_VERBOSE_NEVER;
92
 
  drizzle->con_count= 0;
93
 
  drizzle->pfds_size= 0;
94
 
  drizzle->query_count= 0;
95
 
  drizzle->query_new= 0;
96
 
  drizzle->query_running= 0;
97
 
  drizzle->last_errno= 0;
98
 
  drizzle->timeout= -1;
99
 
  drizzle->con_list= NULL;
100
 
  drizzle->context= NULL;
101
 
  drizzle->context_free_fn= NULL;
102
 
  drizzle->event_watch_fn= NULL;
103
 
  drizzle->event_watch_context= NULL;
104
 
  drizzle->log_fn= NULL;
105
 
  drizzle->log_context= NULL;
106
 
  drizzle->pfds= NULL;
107
 
  drizzle->query_list= NULL;
108
 
  drizzle->sqlstate[0]= 0;
109
 
  drizzle->last_error[0]= 0;
110
 
 
111
 
  return drizzle;
112
 
}
113
 
 
114
 
drizzle_st *drizzle_clone(drizzle_st *drizzle, const drizzle_st *from)
115
 
{
116
 
  drizzle_con_st *con;
117
 
 
118
 
  drizzle= drizzle_create(drizzle);
119
 
  if (drizzle == NULL)
120
 
    return NULL;
121
 
 
122
 
  drizzle->options|= (from->options & (drizzle_options_t)~DRIZZLE_ALLOCATED);
123
 
 
124
 
  for (con= from->con_list; con != NULL; con= con->next)
125
 
  {
126
 
    if (drizzle_con_clone(drizzle, NULL, con) == NULL)
127
 
    {
128
 
      drizzle_free(drizzle);
129
 
      return NULL;
130
 
    }
131
 
  }
132
 
 
133
 
  return drizzle;
134
 
}
135
 
 
136
 
void drizzle_free(drizzle_st *drizzle)
137
 
{
138
 
  if (drizzle->context != NULL && drizzle->context_free_fn != NULL)
139
 
    drizzle->context_free_fn(drizzle, drizzle->context);
140
 
 
141
 
  if (drizzle->options & DRIZZLE_FREE_OBJECTS)
142
 
  {
143
 
    drizzle_con_free_all(drizzle);
144
 
    drizzle_query_free_all(drizzle);
145
 
  }
146
 
  else if (drizzle->options & DRIZZLE_ASSERT_DANGLING)
147
 
  {
148
 
    assert(drizzle->con_list == NULL);
149
 
    assert(drizzle->con_list == NULL);
150
 
  }
151
 
 
152
 
  if (drizzle->pfds != NULL)
153
 
    free(drizzle->pfds);
154
 
 
155
 
  if (drizzle->options & DRIZZLE_ALLOCATED)
156
 
    free(drizzle);
157
 
#if defined(_WIN32)
158
 
  /* if it is MS windows, invoke WSACleanup() at the end*/
159
 
  WSACleanup();
160
 
#endif
161
 
}
162
 
 
163
 
const char *drizzle_error(const drizzle_st *drizzle)
164
 
{
165
 
  return (const char *)drizzle->last_error;
166
 
}
167
 
 
168
 
int drizzle_errno(const drizzle_st *drizzle)
169
 
{
170
 
  return drizzle->last_errno;
171
 
}
172
 
 
173
 
uint16_t drizzle_error_code(const drizzle_st *drizzle)
174
 
{
175
 
  return drizzle->error_code;
176
 
}
177
 
 
178
 
const char *drizzle_sqlstate(const drizzle_st *drizzle)
179
 
{
180
 
  return drizzle->sqlstate;
181
 
}
182
 
 
183
 
drizzle_options_t drizzle_options(const drizzle_st *drizzle)
184
 
{
185
 
  return drizzle->options;
186
 
}
187
 
 
188
 
void drizzle_set_options(drizzle_st *drizzle, drizzle_options_t options)
189
 
{
190
 
  drizzle->options= options;
191
 
}
192
 
 
193
 
void drizzle_add_options(drizzle_st *drizzle, drizzle_options_t options)
194
 
{
195
 
  drizzle->options|= options;
196
 
}
197
 
 
198
 
void drizzle_remove_options(drizzle_st *drizzle, drizzle_options_t options)
199
 
{
200
 
  drizzle->options&= ~options;
201
 
}
202
 
 
203
 
void *drizzle_context(const drizzle_st *drizzle)
204
 
{
205
 
  return drizzle->context;
206
 
}
207
 
 
208
 
void drizzle_set_context(drizzle_st *drizzle, void *context)
209
 
{
210
 
  drizzle->context= context;
211
 
}
212
 
 
213
 
void drizzle_set_context_free_fn(drizzle_st *drizzle,
214
 
                                 drizzle_context_free_fn *function)
215
 
{
216
 
  drizzle->context_free_fn= function;
217
 
}
218
 
 
219
 
int drizzle_timeout(const drizzle_st *drizzle)
220
 
{
221
 
  return drizzle->timeout;
222
 
}
223
 
 
224
 
void drizzle_set_timeout(drizzle_st *drizzle, int timeout)
225
 
{
226
 
  drizzle->timeout= timeout;
227
 
}
228
 
 
229
 
drizzle_verbose_t drizzle_verbose(const drizzle_st *drizzle)
230
 
{
231
 
  return drizzle->verbose;
232
 
}
233
 
 
234
 
void drizzle_set_verbose(drizzle_st *drizzle, drizzle_verbose_t verbose)
235
 
{
236
 
  drizzle->verbose= verbose;
237
 
}
238
 
 
239
 
void drizzle_set_log_fn(drizzle_st *drizzle, drizzle_log_fn *function,
240
 
                        void *context)
241
 
{
242
 
  drizzle->log_fn= function;
243
 
  drizzle->log_context= context;
244
 
}
245
 
 
246
 
void drizzle_set_event_watch_fn(drizzle_st *drizzle,
247
 
                                drizzle_event_watch_fn *function,
248
 
                                void *context)
249
 
{
250
 
  drizzle->event_watch_fn= function;
251
 
  drizzle->event_watch_context= context;
252
 
}
253
 
 
254
 
drizzle_con_st *drizzle_con_create(drizzle_st *drizzle, drizzle_con_st *con)
255
 
{
256
 
  if (con == NULL)
257
 
  {
258
 
    con= malloc(sizeof(drizzle_con_st));
259
 
    if (con == NULL)
260
 
    {
261
 
      if (drizzle != NULL)
262
 
        drizzle_set_error(drizzle, "drizzle_con_create", "malloc");
263
 
      return NULL;
264
 
    }
265
 
 
266
 
    con->options= DRIZZLE_CON_ALLOCATED;
267
 
  }
268
 
  else
269
 
    con->options= 0;
270
 
 
271
 
  if (drizzle->con_list != NULL)
272
 
    drizzle->con_list->prev= con;
273
 
  con->next= drizzle->con_list;
274
 
  con->prev= NULL;
275
 
  drizzle->con_list= con;
276
 
  drizzle->con_count++;
277
 
 
278
 
  con->packet_number= 0;
279
 
  con->protocol_version= 0;
280
 
  con->state_current= 0;
281
 
  con->events= 0;
282
 
  con->revents= 0;
283
 
  con->capabilities= DRIZZLE_CAPABILITIES_NONE;
284
 
  con->charset= 0;
285
 
  con->command= 0;
286
 
  con->options|= DRIZZLE_CON_MYSQL;
287
 
  con->socket_type= DRIZZLE_CON_SOCKET_TCP;
288
 
  con->status= DRIZZLE_CON_STATUS_NONE;
289
 
  con->max_packet_size= DRIZZLE_MAX_PACKET_SIZE;
290
 
  con->result_count= 0;
291
 
  con->thread_id= 0;
292
 
  con->backlog= DRIZZLE_DEFAULT_BACKLOG;
293
 
  con->fd= -1;
294
 
  con->buffer_size= 0;
295
 
  con->command_offset= 0;
296
 
  con->command_size= 0;
297
 
  con->command_total= 0;
298
 
  con->packet_size= 0;
299
 
  con->addrinfo_next= NULL;
300
 
  con->buffer_ptr= con->buffer;
301
 
  con->command_buffer= NULL;
302
 
  con->command_data= NULL;
303
 
  con->context= NULL;
304
 
  con->context_free_fn= NULL;
305
 
  con->drizzle= drizzle;
306
 
  /* con->next set above */
307
 
  /* con->prev set above */
308
 
  con->query= NULL;
309
 
  /* con->result doesn't need to be set */
310
 
  con->result_list= NULL;
311
 
  con->scramble= NULL;
312
 
  con->socket.tcp.addrinfo= NULL;
313
 
  con->socket.tcp.host= NULL;
314
 
  con->socket.tcp.port= 0;
315
 
  /* con->buffer doesn't need to be set */
316
 
  con->db[0]= 0;
317
 
  con->password[0]= 0;
318
 
  /* con->scramble_buffer doesn't need to be set */
319
 
  con->server_version[0]= 0;
320
 
  /* con->state_stack doesn't need to be set */
321
 
  con->user[0]= 0;
322
 
 
323
 
  return con;
324
 
}
325
 
 
326
 
drizzle_con_st *drizzle_con_clone(drizzle_st *drizzle, drizzle_con_st *con,
327
 
                                  const drizzle_con_st *from)
328
 
{
329
 
  con= drizzle_con_create(drizzle, con);
330
 
  if (con == NULL)
331
 
    return NULL;
332
 
 
333
 
  /* Clear "operational" options such as IO status. */
334
 
  con->options|= (from->options & (drizzle_con_options_t)~(
335
 
                  DRIZZLE_CON_ALLOCATED|DRIZZLE_CON_READY|
336
 
                  DRIZZLE_CON_NO_RESULT_READ|DRIZZLE_CON_IO_READY|
337
 
                  DRIZZLE_CON_LISTEN));
338
 
  con->backlog= from->backlog;
339
 
  strcpy(con->db, from->db);
340
 
  strcpy(con->password, from->password);
341
 
  strcpy(con->user, from->user);
342
 
 
343
 
  switch (from->socket_type)
344
 
  {
345
 
  case DRIZZLE_CON_SOCKET_TCP:
346
 
    drizzle_con_set_tcp(con, from->socket.tcp.host, from->socket.tcp.port);
347
 
    break;
348
 
 
349
 
  case DRIZZLE_CON_SOCKET_UDS:
350
 
    drizzle_con_set_uds(con, from->socket.uds.sockaddr.sun_path);
351
 
    break;
352
 
 
353
 
  default:
354
 
    break;
355
 
  }
356
 
 
357
 
  return con;
358
 
}
359
 
 
360
 
void drizzle_con_free(drizzle_con_st *con)
361
 
{
362
 
  if (con->context != NULL && con->context_free_fn != NULL)
363
 
    con->context_free_fn(con, con->context);
364
 
 
365
 
  if (con->drizzle->options & DRIZZLE_FREE_OBJECTS)
366
 
    drizzle_result_free_all(con);
367
 
  else if (con->drizzle->options & DRIZZLE_ASSERT_DANGLING)
368
 
    assert(con->result_list == NULL);
369
 
 
370
 
  if (con->fd != -1)
371
 
    drizzle_con_close(con);
372
 
 
373
 
  drizzle_con_reset_addrinfo(con);
374
 
 
375
 
  if (con->drizzle->con_list == con)
376
 
    con->drizzle->con_list= con->next;
377
 
  if (con->prev != NULL)
378
 
    con->prev->next= con->next;
379
 
  if (con->next != NULL)
380
 
    con->next->prev= con->prev;
381
 
  con->drizzle->con_count--;
382
 
 
383
 
  if (con->options & DRIZZLE_CON_ALLOCATED)
384
 
    free(con);
385
 
}
386
 
 
387
 
void drizzle_con_free_all(drizzle_st *drizzle)
388
 
{
389
 
  while (drizzle->con_list != NULL)
390
 
    drizzle_con_free(drizzle->con_list);
391
 
}
392
 
 
393
 
drizzle_return_t drizzle_con_wait(drizzle_st *drizzle)
394
 
{
395
 
  drizzle_con_st *con;
396
 
  struct pollfd *pfds;
397
 
  uint32_t x;
398
 
  int ret;
399
 
  drizzle_return_t dret;
400
 
 
401
 
  if (drizzle->pfds_size < drizzle->con_count)
402
 
  {
403
 
    pfds= realloc(drizzle->pfds, drizzle->con_count * sizeof(struct pollfd));
404
 
    if (pfds == NULL)
405
 
    {
406
 
      drizzle_set_error(drizzle, "drizzle_con_wait", "realloc");
407
 
      return DRIZZLE_RETURN_MEMORY;
408
 
    }
409
 
 
410
 
    drizzle->pfds= pfds;
411
 
    drizzle->pfds_size= drizzle->con_count;
412
 
  }
413
 
  else
414
 
    pfds= drizzle->pfds;
415
 
 
416
 
  x= 0;
417
 
  for (con= drizzle->con_list; con != NULL; con= con->next)
418
 
  {
419
 
    if (con->events == 0)
420
 
      continue;
421
 
 
422
 
    pfds[x].fd= con->fd;
423
 
    pfds[x].events= con->events;
424
 
    pfds[x].revents= 0;
425
 
    x++;
426
 
  }
427
 
 
428
 
  if (x == 0)
429
 
  {
430
 
    drizzle_set_error(drizzle, "drizzle_con_wait",
431
 
                      "no active file descriptors");
432
 
    return DRIZZLE_RETURN_NO_ACTIVE_CONNECTIONS;
433
 
  }
434
 
 
435
 
  while (1)
436
 
  {
437
 
    drizzle_log_crazy(drizzle, "poll count=%d timeout=%d", x,
438
 
                      drizzle->timeout);
439
 
 
440
 
    ret= poll(pfds, x, drizzle->timeout);
441
 
 
442
 
    drizzle_log_crazy(drizzle, "poll return=%d errno=%d", ret, errno);
443
 
 
444
 
    if (ret == -1)
445
 
    {
446
 
      if (errno == EINTR)
447
 
        continue;
448
 
 
449
 
      drizzle_set_error(drizzle, "drizzle_con_wait", "poll:%d", errno);
450
 
      drizzle->last_errno= errno;
451
 
      return DRIZZLE_RETURN_ERRNO;
452
 
    }
453
 
 
454
 
    break;
455
 
  }
456
 
 
457
 
  if (ret == 0)
458
 
  {
459
 
    drizzle_set_error(drizzle, "drizzle_con_wait", "timeout reached");
460
 
    return DRIZZLE_RETURN_TIMEOUT;
461
 
  }
462
 
 
463
 
  x= 0;
464
 
  for (con= drizzle->con_list; con != NULL; con= con->next)
465
 
  {
466
 
    if (con->events == 0)
467
 
      continue;
468
 
 
469
 
    dret= drizzle_con_set_revents(con, pfds[x].revents);
470
 
    if (dret != DRIZZLE_RETURN_OK)
471
 
      return dret;
472
 
 
473
 
    x++;
474
 
  }
475
 
 
476
 
  return DRIZZLE_RETURN_OK;
477
 
}
478
 
 
479
 
drizzle_con_st *drizzle_con_ready(drizzle_st *drizzle)
480
 
{
481
 
  drizzle_con_st *con;
482
 
 
483
 
  /* We can't keep state between calls since connections may be removed during
484
 
     processing. If this list ever gets big, we may want something faster. */
485
 
 
486
 
  for (con= drizzle->con_list; con != NULL; con= con->next)
487
 
  {
488
 
    if (con->options & DRIZZLE_CON_IO_READY)
489
 
    {
490
 
      con->options&= (drizzle_con_options_t)~DRIZZLE_CON_IO_READY;
491
 
      return con;
492
 
    }
493
 
  }
494
 
 
495
 
  return NULL;
496
 
}
497
 
 
498
 
drizzle_con_st *drizzle_con_ready_listen(drizzle_st *drizzle)
499
 
{
500
 
  drizzle_con_st *con;
501
 
 
502
 
  /* We can't keep state between calls since connections may be removed during
503
 
     processing. If this list ever gets big, we may want something faster. */
504
 
 
505
 
  for (con= drizzle->con_list; con != NULL; con= con->next)
506
 
  {
507
 
    if ((con->options & (DRIZZLE_CON_IO_READY | DRIZZLE_CON_LISTEN)) ==
508
 
        (DRIZZLE_CON_IO_READY | DRIZZLE_CON_LISTEN))
509
 
    {
510
 
      con->options&= (drizzle_con_options_t)~DRIZZLE_CON_IO_READY;
511
 
      return con;
512
 
    }
513
 
  }
514
 
 
515
 
  return NULL;
516
 
}
517
 
 
518
 
/*
519
 
 * Client Definitions
520
 
 */
521
 
 
522
 
drizzle_con_st *drizzle_con_add_tcp(drizzle_st *drizzle, drizzle_con_st *con,
523
 
                                    const char *host, in_port_t port,
524
 
                                    const char *user, const char *password,
525
 
                                    const char *db,
526
 
                                    drizzle_con_options_t options)
527
 
{
528
 
  con= drizzle_con_create(drizzle, con);
529
 
  if (con == NULL)
530
 
    return NULL;
531
 
 
532
 
  drizzle_con_set_tcp(con, host, port);
533
 
  drizzle_con_set_auth(con, user, password);
534
 
  drizzle_con_set_db(con, db);
535
 
  drizzle_con_add_options(con, options);
536
 
 
537
 
  return con;
538
 
}
539
 
 
540
 
drizzle_con_st *drizzle_con_add_uds(drizzle_st *drizzle, drizzle_con_st *con,
541
 
                                    const char *uds, const char *user,
542
 
                                    const char *password, const char *db,
543
 
                                    drizzle_con_options_t options)
544
 
{
545
 
  con= drizzle_con_create(drizzle, con);
546
 
  if (con == NULL)
547
 
    return NULL;
548
 
 
549
 
  drizzle_con_set_uds(con, uds);
550
 
  drizzle_con_set_auth(con, user, password);
551
 
  drizzle_con_set_db(con, db);
552
 
  drizzle_con_add_options(con, options);
553
 
 
554
 
  return con;
555
 
}
556
 
 
557
 
/*
558
 
 * Server Definitions
559
 
 */
560
 
 
561
 
drizzle_con_st *drizzle_con_add_tcp_listen(drizzle_st *drizzle,
562
 
                                           drizzle_con_st *con,
563
 
                                           const char *host, in_port_t port,
564
 
                                           int backlog,
565
 
                                           drizzle_con_options_t options)
566
 
{
567
 
  con= drizzle_con_create(drizzle, con);
568
 
  if (con == NULL)
569
 
    return NULL;
570
 
 
571
 
  drizzle_con_set_tcp(con, host, port);
572
 
  drizzle_con_set_backlog(con, backlog);
573
 
  drizzle_con_add_options(con, DRIZZLE_CON_LISTEN | options);
574
 
 
575
 
  return con;
576
 
}
577
 
 
578
 
drizzle_con_st *drizzle_con_add_uds_listen(drizzle_st *drizzle,
579
 
                                           drizzle_con_st *con,
580
 
                                           const char *uds, int backlog,
581
 
                                           drizzle_con_options_t options)
582
 
{
583
 
  con= drizzle_con_create(drizzle, con);
584
 
  if (con == NULL)
585
 
    return NULL;
586
 
 
587
 
  drizzle_con_set_uds(con, uds);
588
 
  drizzle_con_set_backlog(con, backlog);
589
 
  drizzle_con_add_options(con, DRIZZLE_CON_LISTEN | options);
590
 
 
591
 
  return con;
592
 
}
593
 
 
594
 
drizzle_con_st *drizzle_con_accept(drizzle_st *drizzle, drizzle_con_st *con,
595
 
                                   drizzle_return_t *ret_ptr)
596
 
{
597
 
  drizzle_con_st *ready;
598
 
  int fd;
599
 
 
600
 
  while (1)
601
 
  {
602
 
    if ((ready= drizzle_con_ready_listen(drizzle)) != NULL)
603
 
    {
604
 
      fd= accept(ready->fd, NULL, NULL);
605
 
 
606
 
      con= drizzle_con_create(drizzle, con);
607
 
      if (con == NULL)
608
 
      {
609
 
        (void)close(fd);
610
 
        *ret_ptr= DRIZZLE_RETURN_MEMORY;
611
 
        return NULL;
612
 
      }
613
 
 
614
 
      *ret_ptr= drizzle_con_set_fd(con, fd);
615
 
      if (*ret_ptr != DRIZZLE_RETURN_OK)
616
 
      {
617
 
        (void)close(fd);
618
 
        return NULL;
619
 
      }
620
 
 
621
 
      if (ready->options & DRIZZLE_CON_MYSQL)
622
 
        drizzle_con_add_options(con, DRIZZLE_CON_MYSQL);
623
 
 
624
 
      *ret_ptr= DRIZZLE_RETURN_OK;
625
 
      return con;
626
 
    }
627
 
 
628
 
    if (drizzle->options & DRIZZLE_NON_BLOCKING)
629
 
    {
630
 
      *ret_ptr= DRIZZLE_RETURN_IO_WAIT;
631
 
      return NULL;
632
 
    }
633
 
 
634
 
    for (ready= drizzle->con_list; ready != NULL; ready= ready->next)
635
 
    {
636
 
      if (ready->options & DRIZZLE_CON_LISTEN)
637
 
        drizzle_con_set_events(ready, POLLIN);
638
 
    }
639
 
 
640
 
    *ret_ptr= drizzle_con_wait(drizzle);
641
 
    if (*ret_ptr != DRIZZLE_RETURN_OK)
642
 
      return NULL;
643
 
  }
644
 
}
645
 
 
646
 
/*
647
 
 * Local Definitions
648
 
 */
649
 
 
650
 
void drizzle_set_error(drizzle_st *drizzle, const char *function,
651
 
                       const char *format, ...)
652
 
{
653
 
  size_t size;
654
 
  char *ptr;
655
 
  char log_buffer[DRIZZLE_MAX_ERROR_SIZE];
656
 
  va_list args;
657
 
 
658
 
  size= strlen(function);
659
 
  ptr= memcpy(log_buffer, function, size);
660
 
  ptr+= size;
661
 
  ptr[0]= ':';
662
 
  size++;
663
 
  ptr++;
664
 
 
665
 
  va_start(args, format);
666
 
  size+= (size_t)vsnprintf(ptr, DRIZZLE_MAX_ERROR_SIZE - size, format, args);
667
 
  va_end(args);
668
 
 
669
 
  if (drizzle->log_fn == NULL)
670
 
  {
671
 
    if (size >= DRIZZLE_MAX_ERROR_SIZE)
672
 
      size= DRIZZLE_MAX_ERROR_SIZE - 1;
673
 
 
674
 
    memcpy(drizzle->last_error, log_buffer, size + 1);
675
 
  }
676
 
  else
677
 
    drizzle->log_fn(log_buffer, DRIZZLE_VERBOSE_ERROR, drizzle->log_context);
678
 
}
679
 
 
680
 
void drizzle_log(drizzle_st *drizzle, drizzle_verbose_t verbose,
681
 
                 const char *format, va_list args)
682
 
{
683
 
  char log_buffer[DRIZZLE_MAX_ERROR_SIZE];
684
 
 
685
 
  if (drizzle->log_fn == NULL)
686
 
  {
687
 
    printf("%5s: ", drizzle_verbose_name(verbose));
688
 
    vprintf(format, args);
689
 
    printf("\n");
690
 
  }
691
 
  else
692
 
  {
693
 
    vsnprintf(log_buffer, DRIZZLE_MAX_ERROR_SIZE, format, args);
694
 
    drizzle->log_fn(log_buffer, verbose, drizzle->log_context);
695
 
  }
696
 
}