~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/oldlibdrizzle/drizzle.cc

  • Committer: Brian Aker
  • Date: 2009-09-26 04:00:11 UTC
  • mfrom: (1126.12.1 trunk-nodebug)
  • Revision ID: brian@gaz-20090926040011-2qzxdcbpm1ibpkhl
Merge Lee

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
21
 
 
22
 
#include <libdrizzle/drizzle_com.h>
23
 
#include <libdrizzle/libdrizzle.h>
24
 
#include <libdrizzle/errmsg.h>
25
 
#include <libdrizzle/drizzle.h>
26
 
#include <libdrizzle/gettext.h>
27
 
#include <libdrizzle/net_serv.h>
28
 
#include <libdrizzle/drizzle_data.h>
29
 
#include <libdrizzle/local_infile.h>
 
20
#include <drizzled/global.h>
 
21
#include <drizzled/common.h>
30
22
 
31
23
#include "libdrizzle_priv.h"
32
24
 
33
 
#include <vio/violite.h>
 
25
#include "libdrizzle.h"
 
26
#include "pack.h"
 
27
#include "errmsg.h"
 
28
#include "drizzle.h"
 
29
#include "net_serv.h"
 
30
#include "drizzle_data.h"
34
31
 
35
 
#include <drizzled/version.h>
 
32
#include <drizzled/gettext.h>
36
33
 
37
34
#include <stdio.h>
38
35
#include <stdlib.h>
42
39
#include <assert.h>
43
40
#include <pwd.h>
44
41
#include <sys/socket.h>
 
42
#include <signal.h>
 
43
#include <errno.h>
 
44
#include <arpa/inet.h>
 
45
#include <netinet/in.h>
45
46
 
46
47
 
47
48
#define CONNECT_TIMEOUT 0
 
49
#define PROTOCOL_VERSION 10
 
50
 
48
51
 
49
52
static bool drizzle_client_init= false;
50
53
unsigned int drizzle_server_last_errno;
51
54
 
52
55
/* Server error code and message */
53
 
char drizzle_server_last_error[DRIZZLE_ERRMSG_SIZE];
 
56
char drizzle_server_last_error[LIBDRIZZLE_ERRMSG_SIZE];
54
57
 
55
58
/*
56
59
  Note that the drizzle argument must be initialized with drizzle_init()
57
 
  before calling drizzle_connect !
 
60
  before calling drizzleclient_connect !
58
61
*/
59
62
 
60
63
 
61
64
 
62
65
static DRIZZLE_METHODS client_methods=
63
66
{
64
 
  cli_read_query_result,                       /* read_query_result */
65
 
  cli_advanced_command,                        /* advanced_command */
66
 
  cli_read_rows,                               /* read_rows */
67
 
  cli_use_result,                              /* use_result */
68
 
  cli_fetch_lengths,                           /* fetch_lengths */
69
 
  cli_flush_use_result,                        /* flush_use_result */
70
 
  cli_list_fields,                             /* list_fields */
71
 
  cli_unbuffered_fetch,                        /* unbuffered_fetch */
72
 
  cli_read_statistics,                         /* read_statistics */
73
 
  cli_read_query_result,                       /* next_result */
74
 
  cli_read_change_user_result,                 /* read_change_user_result */
 
67
  drizzleclient_cli_read_query_result,                       /* read_query_result */
 
68
  drizzleclient_cli_advanced_command,                        /* advanced_command */
 
69
  drizzleclient_cli_read_rows,                               /* read_rows */
 
70
  drizzleclient_cli_use_result,                              /* use_result */
 
71
  drizzleclient_cli_fetch_lengths,                           /* fetch_lengths */
 
72
  drizzleclient_cli_flush_use_result,                        /* flush_use_result */
 
73
  drizzleclient_cli_list_fields,                             /* list_fields */
 
74
  drizzleclient_cli_unbuffered_fetch,                        /* unbuffered_fetch */
 
75
  drizzleclient_cli_read_statistics,                         /* read_statistics */
 
76
  drizzleclient_cli_read_query_result,                       /* next_result */
75
77
};
76
78
 
77
79
 
81
83
****************************************************************************/
82
84
 
83
85
DRIZZLE *
84
 
drizzle_create(DRIZZLE *ptr)
 
86
drizzleclient_create(DRIZZLE *ptr)
85
87
{
86
88
 
87
89
  if (!drizzle_client_init)
88
90
  {
89
91
    drizzle_client_init=true;
90
92
 
91
 
    if (!drizzle_get_default_port())
 
93
    if (!drizzleclient_get_default_port())
92
94
    {
93
 
      drizzle_set_default_port(DRIZZLE_PORT);
 
95
      drizzleclient_set_default_port(DRIZZLE_TCP_PORT);
94
96
      {
95
97
        struct servent *serv_ptr;
96
98
        char *env;
105
107
          line options.
106
108
        */
107
109
 
108
 
#if DRIZZLE_PORT_DEFAULT == 0
 
110
#if DRIZZLE_TCP_PORT_DEFAULT == 0
109
111
        if ((serv_ptr = getservbyname("drizzle", "tcp")))
110
 
          drizzle_set_default_port((uint32_t) ntohs((uint16_t) serv_ptr->s_port));
 
112
          drizzleclient_set_default_port((uint32_t) ntohs((uint16_t) serv_ptr->s_port));
111
113
#endif
112
114
        if ((env = getenv("DRIZZLE_TCP_PORT")))
113
 
          drizzle_set_default_port((uint32_t) atoi(env));
 
115
          drizzleclient_set_default_port((uint32_t) atoi(env));
114
116
      }
115
117
    }
116
118
#if defined(SIGPIPE)
124
126
 
125
127
    if (ptr == NULL)
126
128
    {
127
 
      drizzle_set_error(NULL, CR_OUT_OF_MEMORY, sqlstate_get_unknown());
 
129
      drizzleclient_set_error(NULL, CR_OUT_OF_MEMORY, drizzleclient_sqlstate_get_unknown());
128
130
      return 0;
129
131
    }
130
132
    memset(ptr, 0, sizeof(DRIZZLE));
136
138
  }
137
139
 
138
140
  ptr->options.connect_timeout= CONNECT_TIMEOUT;
139
 
  strcpy(ptr->net.sqlstate, sqlstate_get_not_error());
 
141
  strcpy(ptr->net.sqlstate, drizzleclient_sqlstate_get_not_error());
140
142
 
141
143
  /*
142
144
    Only enable LOAD DATA INFILE by default if configured with
154
156
    By default we don't reconnect because it could silently corrupt data (after
155
157
    reconnection you potentially lose table locks, user variables, session
156
158
    variables (transactions but they are specifically dealt with in
157
 
    drizzle_reconnect()).
 
159
    drizzleclient_reconnect()).
158
160
    This is a change: < 5.0.3 drizzle->reconnect was set to 1 by default.
159
161
    How this change impacts existing apps:
160
162
    - existing apps which relyed on the default will see a behaviour change;
161
 
    they will have to set reconnect=1 after drizzle_connect().
 
163
    they will have to set reconnect=1 after drizzleclient_connect().
162
164
    - existing apps which explicitely asked for reconnection (the only way they
163
 
    could do it was by setting drizzle.reconnect to 1 after drizzle_connect())
 
165
    could do it was by setting drizzle.reconnect to 1 after drizzleclient_connect())
164
166
    will not see a behaviour change.
165
167
    - existing apps which explicitely asked for no reconnection
166
168
    (drizzle.reconnect=0) will not see a behaviour change.
199
201
}
200
202
 
201
203
DRIZZLE *
202
 
drizzle_connect(DRIZZLE *drizzle,const char *host, const char *user,
 
204
drizzleclient_connect(DRIZZLE *drizzle,const char *host, const char *user,
203
205
                const char *passwd, const char *db,
204
206
                uint32_t port,
205
 
                const char * unix_port __attribute__((__unused__)),
 
207
                const char * unix_port,
206
208
                uint32_t client_flag)
207
209
{
 
210
  (void)unix_port;
208
211
  char          buff[NAME_LEN+USERNAME_LENGTH+100];
209
212
  char          *end,*host_info=NULL;
210
213
  uint32_t         pkt_length;
246
249
    char port_buf[NI_MAXSERV];
247
250
 
248
251
    if (!port)
249
 
      port= drizzle_port;
 
252
      port= drizzleclient_get_default_port();
250
253
 
251
254
    if (!host)
252
255
      host= LOCAL_HOST;
253
256
 
254
 
    snprintf(host_info=buff, sizeof(buff)-1, ER(CR_TCP_CONNECTION), host);
 
257
    snprintf(host_info=buff, sizeof(buff)-1, _("%-.100s via TCP/IP"), host);
255
258
 
256
259
    memset(&hints, 0, sizeof(hints));
257
260
    hints.ai_socktype= SOCK_STREAM;
261
264
 
262
265
    if (gai_errno != 0)
263
266
    {
264
 
      drizzle_set_extended_error(drizzle, CR_UNKNOWN_HOST, sqlstate_get_unknown(),
 
267
      drizzleclient_set_extended_error(drizzle, CR_UNKNOWN_HOST, drizzleclient_sqlstate_get_unknown(),
265
268
                                 ER(CR_UNKNOWN_HOST), host, errno);
266
269
 
267
270
      goto error;
274
277
      if (sock < 0)
275
278
        continue;
276
279
 
277
 
      net->vio= vio_new(sock, VIO_TYPE_TCPIP, VIO_BUFFERED_READ);
 
280
      net->vio= drizzleclient_vio_new(sock, VIO_TYPE_TCPIP, VIO_BUFFERED_READ);
278
281
      if (! net->vio )
279
282
      {
280
283
        close(sock);
281
284
        continue;
282
285
      }
283
286
 
284
 
      if (connect_with_timeout(sock, t_res->ai_addr, t_res->ai_addrlen, drizzle->options.connect_timeout))
 
287
      if (drizzleclient_connect_with_timeout(sock, t_res->ai_addr, t_res->ai_addrlen, drizzle->options.connect_timeout))
285
288
      {
286
 
        vio_delete(net->vio);
 
289
        drizzleclient_vio_delete(net->vio);
287
290
        net->vio= 0;
288
291
        continue;
289
292
      }
295
298
 
296
299
  if (!net->vio)
297
300
  {
298
 
    drizzle_set_extended_error(drizzle, CR_CONN_HOST_ERROR, sqlstate_get_unknown(),
299
 
                               ER(CR_CONN_HOST_ERROR), host, errno);
 
301
    drizzleclient_set_extended_error(drizzle, CR_CONN_HOST_ERROR,
 
302
                                     drizzleclient_sqlstate_get_unknown(),
 
303
                                     ER(CR_CONN_HOST_ERROR),
 
304
                                     host, port, errno);
300
305
    goto error;
301
306
  }
302
307
 
303
 
  if (my_net_init(net, net->vio))
 
308
  if (drizzleclient_net_init(net, net->vio))
304
309
  {
305
 
    vio_delete(net->vio);
 
310
    drizzleclient_vio_delete(net->vio);
306
311
    net->vio = 0;
307
 
    drizzle_set_error(drizzle, CR_OUT_OF_MEMORY, sqlstate_get_unknown());
 
312
    drizzleclient_set_error(drizzle, CR_OUT_OF_MEMORY, drizzleclient_sqlstate_get_unknown());
308
313
    goto error;
309
314
  }
310
 
  vio_keepalive(net->vio,true);
 
315
  drizzleclient_vio_keepalive(net->vio,true);
311
316
 
312
317
  /* If user set read_timeout, let it override the default */
313
318
  if (drizzle->options.read_timeout)
314
 
    my_net_set_read_timeout(net, drizzle->options.read_timeout);
 
319
    drizzleclient_net_set_read_timeout(net, drizzle->options.read_timeout);
315
320
 
316
321
  /* If user set write_timeout, let it override the default */
317
322
  if (drizzle->options.write_timeout)
318
 
    my_net_set_write_timeout(net, drizzle->options.write_timeout);
 
323
    drizzleclient_net_set_write_timeout(net, drizzle->options.write_timeout);
319
324
 
320
325
  if (drizzle->options.max_allowed_packet)
321
326
    net->max_packet_size= drizzle->options.max_allowed_packet;
323
328
  /* Get version info */
324
329
  drizzle->protocol_version= PROTOCOL_VERSION;  /* Assume this */
325
330
  if (drizzle->options.connect_timeout &&
326
 
      vio_poll_read(net->vio, drizzle->options.connect_timeout))
 
331
      drizzleclient_vio_poll_read(net->vio, drizzle->options.connect_timeout))
327
332
  {
328
 
    drizzle_set_extended_error(drizzle, CR_SERVER_LOST, sqlstate_get_unknown(),
 
333
    drizzleclient_set_extended_error(drizzle, CR_SERVER_LOST, drizzleclient_sqlstate_get_unknown(),
329
334
                               ER(CR_SERVER_LOST_INITIAL_COMM_WAIT),
330
335
                               errno);
331
336
    goto error;
335
340
    Part 1: Connection established, read and parse first packet
336
341
  */
337
342
 
338
 
  if ((pkt_length=cli_safe_read(drizzle)) == packet_error)
 
343
  if ((pkt_length=drizzleclient_cli_safe_read(drizzle)) == packet_error)
339
344
  {
340
345
    if (drizzle->net.last_errno == CR_SERVER_LOST)
341
 
      drizzle_set_extended_error(drizzle, CR_SERVER_LOST, sqlstate_get_unknown(),
 
346
      drizzleclient_set_extended_error(drizzle, CR_SERVER_LOST, drizzleclient_sqlstate_get_unknown(),
342
347
                                 ER(CR_SERVER_LOST_INITIAL_COMM_READ),
343
348
                                 errno);
344
349
    goto error;
348
353
  drizzle->protocol_version= net->read_pos[0];
349
354
  if (drizzle->protocol_version != PROTOCOL_VERSION)
350
355
  {
351
 
    drizzle_set_extended_error(drizzle, CR_VERSION_ERROR, sqlstate_get_unknown(),
 
356
    drizzleclient_set_extended_error(drizzle, CR_VERSION_ERROR, drizzleclient_sqlstate_get_unknown(),
352
357
                               ER(CR_VERSION_ERROR), drizzle->protocol_version,
353
358
                               PROTOCOL_VERSION);
354
359
    goto error;
382
387
  if (drizzle->options.secure_auth && passwd[0] &&
383
388
      !(drizzle->server_capabilities & CLIENT_SECURE_CONNECTION))
384
389
  {
385
 
    drizzle_set_error(drizzle, CR_SECURE_AUTH, sqlstate_get_unknown());
 
390
    drizzleclient_set_error(drizzle, CR_SECURE_AUTH, drizzleclient_sqlstate_get_unknown());
386
391
    goto error;
387
392
  }
388
393
 
392
397
      !(drizzle->user=strdup(user)) ||
393
398
      !(drizzle->passwd=strdup(passwd)))
394
399
  {
395
 
    drizzle_set_error(drizzle, CR_OUT_OF_MEMORY, sqlstate_get_unknown());
 
400
    drizzleclient_set_error(drizzle, CR_OUT_OF_MEMORY, drizzleclient_sqlstate_get_unknown());
396
401
    goto error;
397
402
  }
398
403
  drizzle->host= drizzle->host_info+strlen(host_info)+1;
451
456
  /* Add database if needed */
452
457
  if (db && (drizzle->server_capabilities & CLIENT_CONNECT_WITH_DB))
453
458
  {
454
 
    end= strncpy(end, db, NAME_LEN) + NAME_LEN + 1;
 
459
    size_t db_len= strlen(db);
 
460
 
 
461
    if (db_len >= NAME_LEN)
 
462
      db_len= NAME_LEN - 1;
 
463
    end= (char *)memcpy(end, db, db_len);
 
464
    end[db_len]= 0;
 
465
    end+= (db_len + 1);
 
466
 
455
467
    drizzle->db= strdup(db);
456
468
    db= 0;
457
469
  }
458
470
  /* Write authentication package */
459
 
  if (my_net_write(net, (unsigned char*) buff, (size_t) (end-buff)) || net_flush(net))
 
471
  if (drizzleclient_net_write(net, (unsigned char*) buff, (size_t) (end-buff)) || drizzleclient_net_flush(net))
460
472
  {
461
 
    drizzle_set_extended_error(drizzle, CR_SERVER_LOST, sqlstate_get_unknown(),
 
473
    drizzleclient_set_extended_error(drizzle, CR_SERVER_LOST, drizzleclient_sqlstate_get_unknown(),
462
474
                               ER(CR_SERVER_LOST_SEND_AUTH),
463
475
                               errno);
464
476
    goto error;
469
481
    OK-packet, or re-request scrambled password.
470
482
  */
471
483
 
472
 
  if ((pkt_length=cli_safe_read(drizzle)) == packet_error)
 
484
  if ((pkt_length=drizzleclient_cli_safe_read(drizzle)) == packet_error)
473
485
  {
474
486
    if (drizzle->net.last_errno == CR_SERVER_LOST)
475
 
      drizzle_set_extended_error(drizzle, CR_SERVER_LOST, sqlstate_get_unknown(),
 
487
      drizzleclient_set_extended_error(drizzle, CR_SERVER_LOST, drizzleclient_sqlstate_get_unknown(),
476
488
                                 ER(CR_SERVER_LOST_READ_AUTH),
477
489
                                 errno);
478
490
    goto error;
482
494
    net->compress=1;
483
495
 
484
496
 
485
 
  if (db && drizzle_select_db(drizzle, db))
 
497
  if (db && drizzleclient_select_db(drizzle, db))
486
498
  {
487
499
    if (drizzle->net.last_errno == CR_SERVER_LOST)
488
 
      drizzle_set_extended_error(drizzle, CR_SERVER_LOST, sqlstate_get_unknown(),
 
500
      drizzleclient_set_extended_error(drizzle, CR_SERVER_LOST, drizzleclient_sqlstate_get_unknown(),
489
501
                                 ER(CR_SERVER_LOST_SETTING_DB),
490
502
                                 errno);
491
503
    goto error;
497
509
error:
498
510
  {
499
511
    /* Free alloced memory */
500
 
    drizzle_disconnect(drizzle);
501
 
    drizzle_close_free(drizzle);
 
512
    drizzleclient_disconnect(drizzle);
 
513
    drizzleclient_close_free(drizzle);
502
514
    if (!(((uint32_t) client_flag) & CLIENT_REMEMBER_OPTIONS))
503
 
      drizzle_close_free_options(drizzle);
 
515
      drizzleclient_close_free_options(drizzle);
504
516
  }
505
517
  return(0);
506
518
}
513
525
**************************************************************************/
514
526
 
515
527
int
516
 
drizzle_select_db(DRIZZLE *drizzle, const char *db)
 
528
drizzleclient_select_db(DRIZZLE *drizzle, const char *db)
517
529
{
518
530
  int error;
519
531
 
526
538
  return(0);
527
539
}
528
540
 
529
 
bool drizzle_reconnect(DRIZZLE *drizzle)
 
541
bool drizzleclient_reconnect(DRIZZLE *drizzle)
530
542
{
531
543
  DRIZZLE tmp_drizzle;
532
544
  assert(drizzle);
536
548
  {
537
549
    /* Allow reconnect next time */
538
550
    drizzle->server_status&= ~SERVER_STATUS_IN_TRANS;
539
 
    drizzle_set_error(drizzle, CR_SERVER_GONE_ERROR, sqlstate_get_unknown());
 
551
    drizzleclient_set_error(drizzle, CR_SERVER_GONE_ERROR, drizzleclient_sqlstate_get_unknown());
540
552
    return(1);
541
553
  }
542
 
  drizzle_create(&tmp_drizzle);
 
554
  drizzleclient_create(&tmp_drizzle);
543
555
  tmp_drizzle.options= drizzle->options;
544
556
  tmp_drizzle.options.my_cnf_file= tmp_drizzle.options.my_cnf_group= 0;
545
557
 
546
 
  if (!drizzle_connect(&tmp_drizzle,drizzle->host,drizzle->user,drizzle->passwd,
 
558
  if (!drizzleclient_connect(&tmp_drizzle,drizzle->host,drizzle->user,drizzle->passwd,
547
559
                       drizzle->db, drizzle->port, 0,
548
560
                       drizzle->client_flag | CLIENT_REMEMBER_OPTIONS))
549
561
  {
559
571
  /* Don't free options as these are now used in tmp_drizzle */
560
572
  memset(&drizzle->options, 0, sizeof(drizzle->options));
561
573
  drizzle->free_me=0;
562
 
  drizzle_close(drizzle);
 
574
  drizzleclient_close(drizzle);
563
575
  *drizzle=tmp_drizzle;
564
 
  net_clear(&drizzle->net, 1);
 
576
  drizzleclient_net_clear(&drizzle->net, 1);
565
577
  drizzle->affected_rows= ~(uint64_t) 0;
566
578
  return(0);
567
579
}
570
582
  Shut down connection
571
583
**************************************************************************/
572
584
 
573
 
void drizzle_disconnect(DRIZZLE *drizzle)
 
585
void drizzleclient_disconnect(DRIZZLE *drizzle)
574
586
{
575
587
  int save_errno= errno;
576
588
  if (drizzle->net.vio != 0)
577
589
  {
578
 
    vio_delete(drizzle->net.vio);
 
590
    drizzleclient_vio_delete(drizzle->net.vio);
579
591
    drizzle->net.vio= 0;          /* Marker */
580
592
  }
581
 
  net_end(&drizzle->net);
582
 
  free_old_query(drizzle);
 
593
  drizzleclient_net_end(&drizzle->net);
 
594
  drizzleclient_free_old_query(drizzle);
583
595
  errno= save_errno;
584
596
}
585
597
 
589
601
  If handle is alloced by DRIZZLE connect free it.
590
602
*************************************************************************/
591
603
 
592
 
void drizzle_close_free_options(DRIZZLE *drizzle)
 
604
void drizzleclient_close_free_options(DRIZZLE *drizzle)
593
605
{
594
606
  if (drizzle->options.user != NULL)
595
607
    free(drizzle->options.user);
610
622
}
611
623
 
612
624
 
613
 
void drizzle_close_free(DRIZZLE *drizzle)
 
625
void drizzleclient_close_free(DRIZZLE *drizzle)
614
626
{
615
627
  if (drizzle->host_info != NULL)
616
628
    free((unsigned char*) drizzle->host_info);
629
641
}
630
642
 
631
643
 
632
 
void drizzle_close(DRIZZLE *drizzle)
 
644
void drizzleclient_close(DRIZZLE *drizzle)
633
645
{
634
646
  if (drizzle)          /* Some simple safety */
635
647
  {
636
648
    /* If connection is still up, send a QUIT message */
637
649
    if (drizzle->net.vio != 0)
638
650
    {
639
 
      free_old_query(drizzle);
 
651
      drizzleclient_free_old_query(drizzle);
640
652
      drizzle->status=DRIZZLE_STATUS_READY; /* Force command */
641
653
      drizzle->reconnect=0;
642
654
      simple_command(drizzle,COM_QUIT,(unsigned char*) 0,0,1);
643
 
      drizzle_disconnect(drizzle);      /* Sets drizzle->net.vio= 0 */
 
655
      drizzleclient_disconnect(drizzle);      /* Sets drizzle->net.vio= 0 */
644
656
    }
645
 
    drizzle_close_free_options(drizzle);
646
 
    drizzle_close_free(drizzle);
 
657
    drizzleclient_close_free_options(drizzle);
 
658
    drizzleclient_close_free(drizzle);
647
659
    if (drizzle->free_me)
648
660
      free((unsigned char*) drizzle);
649
661
  }
651
663
}
652
664
 
653
665
 
654
 
bool cli_read_query_result(DRIZZLE *drizzle)
 
666
bool drizzleclient_cli_read_query_result(DRIZZLE *drizzle)
655
667
{
656
668
  unsigned char *pos;
657
669
  uint32_t field_count;
658
670
  DRIZZLE_DATA *fields;
659
671
  uint32_t length;
660
672
 
661
 
  if ((length = cli_safe_read(drizzle)) == packet_error)
 
673
  if ((length = drizzleclient_cli_safe_read(drizzle)) == packet_error)
662
674
    return(1);
663
 
  free_old_query(drizzle);    /* Free old result */
664
 
get_info:
 
675
  drizzleclient_free_old_query(drizzle);    /* Free old result */
 
676
 
665
677
  pos=(unsigned char*) drizzle->net.read_pos;
666
 
  if ((field_count= net_field_length(&pos)) == 0)
 
678
  if ((field_count= drizzleclient_net_field_length(&pos)) == 0)
667
679
  {
668
 
    drizzle->affected_rows= net_field_length_ll(&pos);
669
 
    drizzle->insert_id=    net_field_length_ll(&pos);
 
680
    drizzle->affected_rows= drizzleclient_drizzleclient_net_field_length_ll(&pos);
 
681
    drizzle->insert_id=    drizzleclient_drizzleclient_net_field_length_ll(&pos);
670
682
 
671
683
    drizzle->server_status= uint2korr(pos); pos+=2;
672
684
    drizzle->warning_count= uint2korr(pos); pos+=2;
673
685
 
674
 
    if (pos < drizzle->net.read_pos+length && net_field_length(&pos))
 
686
    if (pos < drizzle->net.read_pos+length && drizzleclient_net_field_length(&pos))
675
687
      drizzle->info=(char*) pos;
676
 
    return(0);
 
688
    return 0;
677
689
  }
678
690
  if (field_count == NULL_LENGTH)    /* LOAD DATA LOCAL INFILE */
679
691
  {
680
 
    int error;
681
 
 
682
 
    if (!(drizzle->options.client_flag & CLIENT_LOCAL_FILES))
683
 
    {
684
 
      drizzle_set_error(drizzle, CR_MALFORMED_PACKET, sqlstate_get_unknown());
685
 
      return(1);
686
 
    }
687
 
 
688
 
    error= handle_local_infile(drizzle,(char*) pos);
689
 
    if ((length= cli_safe_read(drizzle)) == packet_error || error)
690
 
      return(1);
691
 
    goto get_info;        /* Get info packet */
 
692
    drizzleclient_set_error(drizzle, CR_MALFORMED_PACKET, drizzleclient_sqlstate_get_unknown());
 
693
 
 
694
    return 1;
692
695
  }
693
696
  if (!(drizzle->server_status & SERVER_STATUS_AUTOCOMMIT))
694
697
    drizzle->server_status|= SERVER_STATUS_IN_TRANS;
695
698
 
696
 
  if (!(fields=cli_read_rows(drizzle,(DRIZZLE_FIELD*)0, 7)))
 
699
  if (!(fields=drizzleclient_cli_read_rows(drizzle,(DRIZZLE_FIELD*)0, 7)))
697
700
    return(1);
698
 
  if (!(drizzle->fields= unpack_fields(fields, (uint32_t) field_count, 0)))
 
701
  if (!(drizzle->fields= drizzleclient_unpack_fields(fields, (uint32_t) field_count, 0)))
699
702
    return(1);
700
703
  drizzle->status= DRIZZLE_STATUS_GET_RESULT;
701
704
  drizzle->field_count= (uint32_t) field_count;
705
708
 
706
709
/*
707
710
  Send the query and return so we can do something else.
708
 
  Needs to be followed by drizzle_read_query_result() when we want to
 
711
  Needs to be followed by drizzleclient_read_query_result() when we want to
709
712
  finish processing it.
710
713
*/
711
714
 
712
715
int32_t
713
 
drizzle_send_query(DRIZZLE *drizzle, const char* query, uint32_t length)
 
716
drizzleclient_send_query(DRIZZLE *drizzle, const char* query, uint32_t length)
714
717
{
715
718
  return(simple_command(drizzle, COM_QUERY, (unsigned char*) query, length, 1));
716
719
}
717
720
 
718
721
 
719
722
int32_t
720
 
drizzle_real_query(DRIZZLE *drizzle, const char *query, uint32_t length)
 
723
drizzleclient_real_query(DRIZZLE *drizzle, const char *query, uint32_t length)
721
724
{
722
 
  if (drizzle_send_query(drizzle,query,length))
 
725
  if (drizzleclient_send_query(drizzle,query,length))
723
726
    return(1);
724
727
  return((int) (*drizzle->methods->read_query_result)(drizzle));
725
728
}
727
730
 
728
731
/**************************************************************************
729
732
  Alloc result struct for buffered results. All rows are read to buffer.
730
 
  drizzle_data_seek may be used.
 
733
  drizzleclient_data_seek may be used.
731
734
**************************************************************************/
732
735
 
733
 
DRIZZLE_RES * drizzle_store_result(DRIZZLE *drizzle)
 
736
DRIZZLE_RES * drizzleclient_store_result(DRIZZLE *drizzle)
734
737
{
735
738
  DRIZZLE_RES *result;
736
739
 
738
741
    return(0);
739
742
  if (drizzle->status != DRIZZLE_STATUS_GET_RESULT)
740
743
  {
741
 
    drizzle_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC, sqlstate_get_unknown());
 
744
    drizzleclient_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC, drizzleclient_sqlstate_get_unknown());
742
745
    return(0);
743
746
  }
744
747
  drizzle->status=DRIZZLE_STATUS_READY;    /* server is ready */
746
749
                sizeof(uint32_t) *
747
750
                drizzle->field_count))))
748
751
  {
749
 
    drizzle_set_error(drizzle, CR_OUT_OF_MEMORY, sqlstate_get_unknown());
 
752
    drizzleclient_set_error(drizzle, CR_OUT_OF_MEMORY, drizzleclient_sqlstate_get_unknown());
750
753
    return(0);
751
754
  }
752
755
  memset(result, 0,(sizeof(DRIZZLE_RES)+ sizeof(uint32_t) *
774
777
 
775
778
/**************************************************************************
776
779
  Alloc struct for use with unbuffered reads. Data is fetched by domand
777
 
  when calling to drizzle_fetch_row.
 
780
  when calling to drizzleclient_fetch_row.
778
781
  DRIZZLE_DATA_seek is a noop.
779
782
 
780
783
  No other queries may be specified with the same DRIZZLE handle.
782
785
  have to wait for the client (and will not wait more than 30 sec/packet).
783
786
**************************************************************************/
784
787
 
785
 
DRIZZLE_RES * cli_use_result(DRIZZLE *drizzle)
 
788
DRIZZLE_RES * drizzleclient_cli_use_result(DRIZZLE *drizzle)
786
789
{
787
790
  DRIZZLE_RES *result;
788
791
 
790
793
    return(0);
791
794
  if (drizzle->status != DRIZZLE_STATUS_GET_RESULT)
792
795
  {
793
 
    drizzle_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC, sqlstate_get_unknown());
 
796
    drizzleclient_set_error(drizzle, CR_COMMANDS_OUT_OF_SYNC, drizzleclient_sqlstate_get_unknown());
794
797
    return(0);
795
798
  }
796
799
  if (!(result=(DRIZZLE_RES*) malloc(sizeof(*result)+
827
830
   @parma sqlstate SQL standard sqlstate
828
831
*/
829
832
 
830
 
void drizzle_set_error(DRIZZLE *drizzle, int errcode, const char *sqlstate)
 
833
void drizzleclient_set_error(DRIZZLE *drizzle, int errcode, const char *sqlstate)
831
834
{
832
835
  NET *net;
833
836
  assert(drizzle != 0);
848
851
}
849
852
 
850
853
 
851
 
unsigned int drizzle_errno(const DRIZZLE *drizzle)
 
854
unsigned int drizzleclient_errno(const DRIZZLE *drizzle)
852
855
{
853
856
  return drizzle ? drizzle->net.last_errno : drizzle_server_last_errno;
854
857
}
855
858
 
856
859
 
857
 
const char * drizzle_error(const DRIZZLE *drizzle)
 
860
const char * drizzleclient_error(const DRIZZLE *drizzle)
858
861
{
859
862
  return drizzle ? _(drizzle->net.last_error) : _(drizzle_server_last_error);
860
863
}
864
867
 
865
868
   @param drizzle connection handle
866
869
   @param errcode   CR_* errcode, for client errors
867
 
   @param sqlstate  SQL standard sql state, sqlstate_get_unknown() for the
 
870
   @param sqlstate  SQL standard sql state, drizzleclient_sqlstate_get_unknown() for the
868
871
   majority of client errors.
869
872
   @param format    error message template, in sprintf format
870
873
   @param ...       variable number of arguments
871
874
*/
872
875
 
873
 
void drizzle_set_extended_error(DRIZZLE *drizzle, int errcode,
 
876
void drizzleclient_set_extended_error(DRIZZLE *drizzle, int errcode,
874
877
                                const char *sqlstate,
875
878
                                const char *format, ...)
876
879
{
895
898
  Flush result set sent from server
896
899
*/
897
900
 
898
 
void cli_flush_use_result(DRIZZLE *drizzle)
 
901
void drizzleclient_cli_flush_use_result(DRIZZLE *drizzle)
899
902
{
900
903
  /* Clear the current execution status */
901
904
  for (;;)
902
905
  {
903
906
    uint32_t pkt_len;
904
 
    if ((pkt_len=cli_safe_read(drizzle)) == packet_error)
 
907
    if ((pkt_len=drizzleclient_cli_safe_read(drizzle)) == packet_error)
905
908
      break;
906
909
    if (pkt_len <= 8 && drizzle->net.read_pos[0] == DRIZZLE_PROTOCOL_NO_MORE_DATA)
907
910
    {
917
920
 
918
921
/**************************************************************************
919
922
  Get column lengths of the current row
920
 
  If one uses drizzle_use_result, res->lengths contains the length information,
 
923
  If one uses drizzleclient_use_result, res->lengths contains the length information,
921
924
  else the lengths are calculated from the offset between pointers.
922
925
**************************************************************************/
923
926
 
924
 
void cli_fetch_lengths(uint32_t *to, DRIZZLE_ROW column, uint32_t field_count)
 
927
void drizzleclient_cli_fetch_lengths(uint32_t *to, DRIZZLE_ROW column, uint32_t field_count)
925
928
{
926
929
  uint32_t *prev_length;
927
930
  char *start=0;