~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/client.c

  • Committer: Monty Taylor
  • Date: 2008-09-14 19:39:48 UTC
  • mfrom: (383.1.43 client-split)
  • Revision ID: monty@inaugust.com-20080914193948-ns2p0lts4qtsqx7n
Merged from client-split.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 MySQL
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
40
40
  server.
41
41
*/
42
42
 
 
43
#include <stdarg.h>
 
44
 
43
45
#include <drizzled/global.h>
44
46
 
45
47
#include "drizzle.h"
55
57
 
56
58
#define CLI_DRIZZLE_CONNECT drizzle_connect
57
59
 
58
 
#include <mysys/my_sys.h>
59
 
#include <mystrings/m_string.h>
60
 
#include <mystrings/m_ctype.h>
61
 
#include <drizzled/error.h>
62
 
#include "errmsg.h"
 
60
#include <libdrizzle/errmsg.h>
63
61
#include <vio/violite.h>
64
62
 
65
63
#include <sys/stat.h>
107
105
static int wait_for_data(int fd, int32_t timeout);
108
106
int connect_with_timeout(int fd, const struct sockaddr *name, uint namelen, int32_t timeout);
109
107
 
110
 
CHARSET_INFO *default_client_charset_info = &my_charset_utf8_general_ci;
111
 
 
112
108
/* Server error code and message */
113
109
unsigned int drizzle_server_last_errno;
114
110
char drizzle_server_last_error[DRIZZLE_ERRMSG_SIZE];
309
305
      return (packet_error);
310
306
#endif /*DRIZZLE_SERVER*/
311
307
    end_server(drizzle);
312
 
    set_drizzle_error(drizzle, net->last_errno == ER_NET_PACKET_TOO_LARGE ?
313
 
                    CR_NET_PACKET_TOO_LARGE: CR_SERVER_LOST, unknown_sqlstate);
 
308
    set_drizzle_error(drizzle, net->last_errno == CR_NET_PACKET_TOO_LARGE ?
 
309
                      CR_NET_PACKET_TOO_LARGE : CR_SERVER_LOST,
 
310
                      unknown_sqlstate);
314
311
    return (packet_error);
315
312
  }
316
313
  if (net->read_pos[0] == 255)
361
358
{
362
359
  if (cur)
363
360
  {
364
 
    free_root(&cur->alloc,MYF(0));
365
 
    my_free((uchar*) cur,MYF(0));
 
361
    if (cur->data != NULL)
 
362
    {
 
363
      struct st_drizzle_rows * row= cur->data;
 
364
      uint64_t x;
 
365
      for (x= 0; x< cur->rows; x++)
 
366
      {
 
367
        struct st_drizzle_rows * next_row= row->next;
 
368
        free(row);
 
369
        row= next_row;
 
370
      }
 
371
    }
 
372
    free((uchar*) cur);
366
373
  }
367
374
}
368
375
 
404
411
  if (net_write_command(net,(uchar) command, header, header_length,
405
412
      arg, arg_length))
406
413
  {
407
 
    if (net->last_errno == ER_NET_PACKET_TOO_LARGE)
 
414
    if (net->last_errno == CR_NET_PACKET_TOO_LARGE)
408
415
    {
409
416
      set_drizzle_error(drizzle, CR_NET_PACKET_TOO_LARGE, unknown_sqlstate);
410
417
      goto end;
431
438
void free_old_query(DRIZZLE *drizzle)
432
439
{
433
440
  if (drizzle->fields)
434
 
    free_root(&drizzle->field_alloc,MYF(0));
435
 
  init_alloc_root(&drizzle->field_alloc,8192,0); /* Assume rowlength < 8192 */
 
441
  {
 
442
    /* TODO - we need to de-alloc field storage */
 
443
    free(drizzle->fields->catalog);
 
444
    free(drizzle->fields->db);
 
445
    free(drizzle->fields->table);
 
446
    free(drizzle->fields->org_table);
 
447
    free(drizzle->fields->name);
 
448
    free(drizzle->fields->org_name);
 
449
    free(drizzle->fields->def);
 
450
    free(drizzle->fields);
 
451
 
 
452
  }
 
453
  /* init_alloc_root(&drizzle->field_alloc,8192,0); */ /* Assume rowlength < 8192 */
436
454
  drizzle->fields= 0;
437
455
  drizzle->field_count= 0;      /* For API */
438
456
  drizzle->warning_count= 0;
505
523
      }
506
524
    }
507
525
    free_rows(result->data);
508
 
    if (result->fields)
509
 
      free_root(&result->field_alloc,MYF(0));
 
526
    /* TODO: free result->fields */
510
527
    if (result->row)
511
 
      my_free((uchar*) result->row,MYF(0));
512
 
    my_free((uchar*) result,MYF(0));
513
 
  }
514
 
}
515
 
 
516
 
/****************************************************************************
517
 
  Get options from my.cnf
518
 
****************************************************************************/
519
 
 
520
 
static const char *default_options[]=
521
 
{
522
 
  "port","socket","compress","password","pipe", "timeout", "user",
523
 
  "init-command", "host", "database", "return-found-rows",
524
 
  "ssl-key" ,"ssl-cert" ,"ssl-ca" ,"ssl-capath",
525
 
  "character-sets-dir", "default-character-set", "interactive-timeout",
526
 
  "connect-timeout", "local-infile", "disable-local-infile",
527
 
  "ssl-cipher", "max-allowed-packet", "protocol", "shared-memory-base-name",
528
 
  "multi-results", "multi-statements", "multi-queries", "secure-auth",
529
 
  "report-data-truncation",
530
 
  NullS
531
 
};
532
 
 
533
 
static TYPELIB option_types={array_elements(default_options)-1,
534
 
           "options",default_options, NULL};
535
 
 
536
 
const char *sql_protocol_names_lib[] =
537
 
{ "TCP", "SOCKET", "PIPE", "MEMORY", NullS };
538
 
TYPELIB sql_protocol_typelib = {array_elements(sql_protocol_names_lib)-1,"",
539
 
        sql_protocol_names_lib, NULL};
540
 
 
541
 
static int add_init_command(struct st_drizzle_options *options, const char *cmd)
542
 
{
543
 
  char *tmp;
544
 
 
545
 
  if (!options->init_commands)
546
 
  {
547
 
    options->init_commands= (DYNAMIC_ARRAY*)my_malloc(sizeof(DYNAMIC_ARRAY),
548
 
                  MYF(MY_WME));
549
 
    init_dynamic_array(options->init_commands,sizeof(char*),0,5 CALLER_INFO);
550
 
  }
551
 
 
552
 
  if (!(tmp= my_strdup(cmd,MYF(MY_WME))) ||
553
 
      insert_dynamic(options->init_commands, (uchar*)&tmp))
554
 
  {
555
 
    my_free(tmp, MYF(MY_ALLOW_ZERO_PTR));
556
 
    return 1;
557
 
  }
558
 
 
559
 
  return 0;
560
 
}
561
 
 
562
 
void drizzle_read_default_options(struct st_drizzle_options *options,
563
 
        const char *filename,const char *group)
564
 
{
565
 
  int argc;
566
 
  char *argv_buff[1],**argv;
567
 
  const char *groups[3];
568
 
 
569
 
  argc=1; argv=argv_buff; argv_buff[0]= (char*) "client";
570
 
  groups[0]= (char*) "client"; groups[1]= (char*) group; groups[2]=0;
571
 
 
572
 
  load_defaults(filename, groups, &argc, &argv);
573
 
  if (argc != 1)        /* If some default option */
574
 
  {
575
 
    char **option=argv;
576
 
    while (*++option)
577
 
    {
578
 
      if (option[0][0] == '-' && option[0][1] == '-')
579
 
      {
580
 
        char *end=strrchr(*option,'=');
581
 
        char *opt_arg=0;
582
 
        if (end != NULL)
583
 
        {
584
 
          opt_arg=end+1;
585
 
          *end=0;        /* Remove '=' */
586
 
        }
587
 
        /* Change all '_' in variable name to '-' */
588
 
        for (end= *option ; *(end= strrchr(end,'_')) ; )
589
 
          *end= '-';
590
 
        switch (find_type(*option+2,&option_types,2)) {
591
 
        case 1:        /* port */
592
 
          if (opt_arg)
593
 
            options->port=atoi(opt_arg);
594
 
          break;
595
 
        case 2:        /* socket */
596
 
          if (opt_arg)
597
 
          {
598
 
            my_free(options->unix_socket,MYF(MY_ALLOW_ZERO_PTR));
599
 
            options->unix_socket=my_strdup(opt_arg,MYF(MY_WME));
600
 
          }
601
 
          break;
602
 
        case 3:        /* compress */
603
 
          options->compress=1;
604
 
          options->client_flag|= CLIENT_COMPRESS;
605
 
          break;
606
 
        case 4:        /* password */
607
 
          if (opt_arg)
608
 
          {
609
 
            my_free(options->password,MYF(MY_ALLOW_ZERO_PTR));
610
 
            options->password=my_strdup(opt_arg,MYF(MY_WME));
611
 
          }
612
 
          break;
613
 
        case 20:      /* connect_timeout */
614
 
        case 6:        /* timeout */
615
 
          if (opt_arg)
616
 
            options->connect_timeout=atoi(opt_arg);
617
 
          break;
618
 
        case 7:        /* user */
619
 
          if (opt_arg)
620
 
          {
621
 
            my_free(options->user,MYF(MY_ALLOW_ZERO_PTR));
622
 
            options->user=my_strdup(opt_arg,MYF(MY_WME));
623
 
          }
624
 
          break;
625
 
        case 8:        /* init-command */
626
 
          add_init_command(options,opt_arg);
627
 
          break;
628
 
        case 9:        /* host */
629
 
          if (opt_arg)
630
 
          {
631
 
            my_free(options->host,MYF(MY_ALLOW_ZERO_PTR));
632
 
            options->host=my_strdup(opt_arg,MYF(MY_WME));
633
 
          }
634
 
          break;
635
 
        case 10:      /* database */
636
 
          if (opt_arg)
637
 
          {
638
 
            my_free(options->db,MYF(MY_ALLOW_ZERO_PTR));
639
 
            options->db=my_strdup(opt_arg,MYF(MY_WME));
640
 
          }
641
 
          break;
642
 
        case 12:      /* return-found-rows */
643
 
          options->client_flag|=CLIENT_FOUND_ROWS;
644
 
          break;
645
 
        case 13:        /* Ignore SSL options */
646
 
        case 14:
647
 
        case 15:
648
 
        case 16:
649
 
        case 23:
650
 
          break;
651
 
        case 17:      /* charset-lib */
652
 
          my_free(options->charset_dir,MYF(MY_ALLOW_ZERO_PTR));
653
 
          options->charset_dir = my_strdup(opt_arg, MYF(MY_WME));
654
 
          break;
655
 
        case 18:
656
 
          my_free(options->charset_name,MYF(MY_ALLOW_ZERO_PTR));
657
 
          options->charset_name = my_strdup(opt_arg, MYF(MY_WME));
658
 
          break;
659
 
        case 19:        /* Interactive-timeout */
660
 
          options->client_flag|= CLIENT_INTERACTIVE;
661
 
          break;
662
 
        case 21:
663
 
          if (!opt_arg || atoi(opt_arg) != 0)
664
 
            options->client_flag|= CLIENT_LOCAL_FILES;
665
 
          else
666
 
            options->client_flag&= ~CLIENT_LOCAL_FILES;
667
 
          break;
668
 
        case 22:
669
 
          options->client_flag&= ~CLIENT_LOCAL_FILES;
670
 
          break;
671
 
        case 24: /* max-allowed-packet */
672
 
          if (opt_arg)
673
 
            options->max_allowed_packet= atoi(opt_arg);
674
 
          break;
675
 
        case 25: /* protocol */
676
 
          if ((options->protocol= find_type(opt_arg,
677
 
                                            &sql_protocol_typelib,0)) <= 0)
678
 
          {
679
 
            fprintf(stderr, _("Unknown option to protocol: %s\n"), opt_arg);
680
 
            exit(1);
681
 
          }
682
 
          break;
683
 
        case 27: /* multi-results */
684
 
          options->client_flag|= CLIENT_MULTI_RESULTS;
685
 
          break;
686
 
        case 28: /* multi-statements */
687
 
        case 29: /* multi-queries */
688
 
          options->client_flag|= CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS;
689
 
          break;
690
 
        case 30: /* secure-auth */
691
 
          options->secure_auth= true;
692
 
          break;
693
 
        case 31: /* report-data-truncation */
694
 
          options->report_data_truncation= opt_arg ? test(atoi(opt_arg)) : 1;
695
 
          break;
696
 
        default:
697
 
          break;
698
 
        }
699
 
      }
700
 
    }
701
 
  }
702
 
  free_defaults(argv);
703
 
  return;
 
528
      free((uchar*) result->row);
 
529
    free((uchar*) result);
 
530
  }
704
531
}
705
532
 
706
533
 
736
563
***************************************************************************/
737
564
 
738
565
DRIZZLE_FIELD *
739
 
unpack_fields(DRIZZLE_DATA *data, MEM_ROOT *alloc,uint fields,
 
566
unpack_fields(DRIZZLE_DATA *data, uint fields,
740
567
              bool default_value)
741
568
{
742
569
  DRIZZLE_ROWS  *row;
743
570
  DRIZZLE_FIELD  *field,*result;
744
571
  uint32_t lengths[9];        /* Max of fields */
745
572
 
746
 
  field= result= (DRIZZLE_FIELD*) alloc_root(alloc,
747
 
             (uint) sizeof(*field)*fields);
 
573
  field= result= (DRIZZLE_FIELD*) malloc((uint) sizeof(*field)*fields);
748
574
  if (!result)
749
575
  {
750
576
    free_rows(data);        /* Free old data */
758
584
    /* fields count may be wrong */
759
585
    assert((uint) (field - result) < fields);
760
586
    cli_fetch_lengths(&lengths[0], row->data, default_value ? 8 : 7);
761
 
    field->catalog=   strmake_root(alloc,(char*) row->data[0], lengths[0]);
762
 
    field->db=        strmake_root(alloc,(char*) row->data[1], lengths[1]);
763
 
    field->table=     strmake_root(alloc,(char*) row->data[2], lengths[2]);
764
 
    field->org_table= strmake_root(alloc,(char*) row->data[3], lengths[3]);
765
 
    field->name=      strmake_root(alloc,(char*) row->data[4], lengths[4]);
766
 
    field->org_name=  strmake_root(alloc,(char*) row->data[5], lengths[5]);
 
587
    field->catalog=   strndup((char*) row->data[0], lengths[0]);
 
588
    field->db=        strndup((char*) row->data[1], lengths[1]);
 
589
    field->table=     strndup((char*) row->data[2], lengths[2]);
 
590
    field->org_table= strndup((char*) row->data[3], lengths[3]);
 
591
    field->name=      strndup((char*) row->data[4], lengths[4]);
 
592
    field->org_name=  strndup((char*) row->data[5], lengths[5]);
767
593
 
768
594
    field->catalog_length=  lengths[0];
769
595
    field->db_length=    lengths[1];
784
610
      field->flags|= NUM_FLAG;
785
611
    if (default_value && row->data[7])
786
612
    {
787
 
      field->def=strmake_root(alloc,(char*) row->data[7], lengths[7]);
 
613
      field->def=strndup((char*) row->data[7], lengths[7]);
788
614
      field->def_length= lengths[7];
789
615
    }
790
616
    else
811
637
 
812
638
  if ((pkt_len= cli_safe_read(drizzle)) == packet_error)
813
639
    return(0);
814
 
  if (!(result=(DRIZZLE_DATA*) my_malloc(sizeof(DRIZZLE_DATA),
815
 
               MYF(MY_WME | MY_ZEROFILL))))
 
640
  if (!(result=(DRIZZLE_DATA*) malloc(sizeof(DRIZZLE_DATA))))
816
641
  {
817
642
    set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
818
643
    return(0);
819
644
  }
820
 
  init_alloc_root(&result->alloc,8192,0);  /* Assume rowlength < 8192 */
821
 
  result->alloc.min_malloc=sizeof(DRIZZLE_ROWS);
 
645
  memset(result, 0, sizeof(DRIZZLE_DATA));
822
646
  prev_ptr= &result->data;
823
647
  result->rows=0;
824
648
  result->fields=fields;
833
657
  while (*(cp=net->read_pos) != DRIZZLE_PROTOCOL_NO_MORE_DATA || pkt_len >= 8)
834
658
  {
835
659
    result->rows++;
836
 
    if (!(cur= (DRIZZLE_ROWS*) alloc_root(&result->alloc,
837
 
          sizeof(DRIZZLE_ROWS))) ||
838
 
  !(cur->data= ((DRIZZLE_ROW)
839
 
          alloc_root(&result->alloc,
840
 
         (fields+1)*sizeof(char *)+pkt_len))))
 
660
    if (!(cur= (DRIZZLE_ROWS*) malloc(sizeof(DRIZZLE_ROWS))) ||
 
661
        !(cur->data= ((DRIZZLE_ROW) malloc((fields+1)*sizeof(char *)+pkt_len))))
841
662
    {
842
663
      free_rows(result);
843
664
      set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
956
777
  if (!drizzle_client_init)
957
778
  {
958
779
    drizzle_client_init=true;
959
 
    org_my_init_done=my_init_done;
960
 
 
961
 
    /* Will init threads */
962
 
    if (my_init())
963
 
      return NULL;
964
780
 
965
781
    if (!drizzle_port)
966
782
    {
991
807
    (void) signal(SIGPIPE, SIG_IGN);
992
808
#endif
993
809
  }
994
 
  else
995
 
    /* Init if new thread */
996
 
    if (my_thread_init())
997
 
      return NULL;
998
810
 
999
811
  if (ptr == NULL)
1000
812
  {
1014
826
  }
1015
827
 
1016
828
  ptr->options.connect_timeout= CONNECT_TIMEOUT;
1017
 
  ptr->charset=default_client_charset_info;
1018
829
  strcpy(ptr->net.sqlstate, not_error_sqlstate);
1019
830
 
1020
831
  /*
1069
880
 
1070
881
  vio_end();
1071
882
 
1072
 
  /* If library called my_init(), free memory allocated by it */
1073
 
  if (!org_my_init_done)
1074
 
  {
1075
 
    my_end(0);
1076
 
  }
1077
 
  else
1078
 
  {
1079
 
    free_charsets();
1080
 
    drizzle_thread_end();
1081
 
  }
1082
 
 
1083
883
  drizzle_client_init= org_my_init_done= 0;
1084
 
#ifdef EMBEDDED_SERVER
1085
 
  if (stderror_file)
1086
 
  {
1087
 
    fclose(stderror_file);
1088
 
    stderror_file= 0;
1089
 
  }
1090
 
#endif
1091
884
}
1092
885
 
1093
886
 
1094
887
/*
1095
 
  Fill in SSL part of DRIZZLE structure and set 'use_ssl' flag.
1096
 
  NB! Errors are not reported until you do drizzle_connect.
1097
 
*/
1098
 
 
1099
 
#define strdup_if_not_null(A) (A) == 0 ? 0 : my_strdup((A),MYF(MY_WME))
1100
 
 
1101
 
/*
1102
888
  Note that the drizzle argument must be initialized with drizzle_init()
1103
889
  before calling drizzle_connect !
1104
890
*/
1125
911
#endif
1126
912
};
1127
913
 
1128
 
C_MODE_START
1129
 
int drizzle_init_character_set(DRIZZLE *drizzle)
1130
 
{
1131
 
  const char *default_collation_name;
1132
 
 
1133
 
  /* Set character set */
1134
 
  if (!drizzle->options.charset_name)
1135
 
  {
1136
 
    default_collation_name= DRIZZLE_DEFAULT_COLLATION_NAME;
1137
 
    if (!(drizzle->options.charset_name=
1138
 
       my_strdup(DRIZZLE_DEFAULT_CHARSET_NAME,MYF(MY_WME))))
1139
 
    return 1;
1140
 
  }
1141
 
  else
1142
 
    default_collation_name= NULL;
1143
 
 
1144
 
  {
1145
 
    const char *save= charsets_dir;
1146
 
    if (drizzle->options.charset_dir)
1147
 
      charsets_dir=drizzle->options.charset_dir;
1148
 
    drizzle->charset=get_charset_by_csname(drizzle->options.charset_name,
1149
 
                                         MY_CS_PRIMARY, MYF(MY_WME));
1150
 
    if (drizzle->charset && default_collation_name)
1151
 
    {
1152
 
      const CHARSET_INFO *collation;
1153
 
      if ((collation=
1154
 
           get_charset_by_name(default_collation_name, MYF(MY_WME))))
1155
 
      {
1156
 
        if (!my_charset_same(drizzle->charset, collation))
1157
 
        {
1158
 
          my_printf_error(ER_UNKNOWN_ERROR,
1159
 
                         _("COLLATION %s is not valid for CHARACTER SET %s"),
1160
 
                         MYF(0),
1161
 
                         default_collation_name, drizzle->options.charset_name);
1162
 
          drizzle->charset= NULL;
1163
 
        }
1164
 
        else
1165
 
        {
1166
 
          drizzle->charset= collation;
1167
 
        }
1168
 
      }
1169
 
      else
1170
 
        drizzle->charset= NULL;
1171
 
    }
1172
 
    charsets_dir= save;
1173
 
  }
1174
 
 
1175
 
  if (!drizzle->charset)
1176
 
  {
1177
 
    if (drizzle->options.charset_dir)
1178
 
      set_drizzle_extended_error(drizzle, CR_CANT_READ_CHARSET, unknown_sqlstate,
1179
 
                               ER(CR_CANT_READ_CHARSET),
1180
 
                               drizzle->options.charset_name,
1181
 
                               drizzle->options.charset_dir);
1182
 
    else
1183
 
    {
1184
 
      char cs_dir_name[FN_REFLEN];
1185
 
      get_charsets_dir(cs_dir_name);
1186
 
      set_drizzle_extended_error(drizzle, CR_CANT_READ_CHARSET, unknown_sqlstate,
1187
 
                               ER(CR_CANT_READ_CHARSET),
1188
 
                               drizzle->options.charset_name,
1189
 
                               cs_dir_name);
1190
 
    }
1191
 
    return 1;
1192
 
  }
1193
 
  return 0;
1194
 
}
1195
 
C_MODE_END
1196
 
 
1197
914
 
1198
915
DRIZZLE *
1199
916
CLI_DRIZZLE_CONNECT(DRIZZLE *drizzle,const char *host, const char *user,
1200
 
                       const char *passwd, const char *db,
1201
 
                       uint32_t port, const char *unix_socket, uint32_t client_flag)
 
917
                    const char *passwd, const char *db,
 
918
                    uint32_t port,
 
919
                    const char * unix_port __attribute__((__unused__)),
 
920
                    uint32_t client_flag)
1202
921
{
1203
922
  char          buff[NAME_LEN+USERNAME_LENGTH+100];
1204
923
  char          *end,*host_info=NULL;
1212
931
  net->vio = 0;        /* If something goes wrong */
1213
932
  drizzle->client_flag=0;      /* For handshake */
1214
933
 
1215
 
  /* use default options */
1216
 
  if (drizzle->options.my_cnf_file || drizzle->options.my_cnf_group)
1217
 
  {
1218
 
    drizzle_read_default_options(&drizzle->options,
1219
 
             (drizzle->options.my_cnf_file ?
1220
 
        drizzle->options.my_cnf_file : "my"),
1221
 
             drizzle->options.my_cnf_group);
1222
 
    my_free(drizzle->options.my_cnf_file,MYF(MY_ALLOW_ZERO_PTR));
1223
 
    my_free(drizzle->options.my_cnf_group,MYF(MY_ALLOW_ZERO_PTR));
1224
 
    drizzle->options.my_cnf_file=drizzle->options.my_cnf_group=0;
1225
 
  }
1226
 
 
1227
934
  /* Some empty-string-tests are done because of ODBC */
1228
935
  if (!host || !host[0])
1229
936
    host=drizzle->options.host;
1243
950
    db=drizzle->options.db;
1244
951
  if (!port)
1245
952
    port=drizzle->options.port;
1246
 
  if (!unix_socket)
1247
 
    unix_socket=drizzle->options.unix_socket;
1248
953
 
1249
954
  drizzle->server_status=SERVER_STATUS_AUTOCOMMIT;
1250
955
 
1259
964
    int gai_errno;
1260
965
    char port_buf[NI_MAXSERV];
1261
966
 
1262
 
    unix_socket=0;        /* This is not used */
1263
 
 
1264
967
    if (!port)
1265
968
      port= drizzle_port;
1266
969
 
1402
1105
    goto error;
1403
1106
  }
1404
1107
 
1405
 
  if (drizzle_init_character_set(drizzle))
1406
 
    goto error;
1407
 
 
1408
1108
  /* Save connection information */
1409
 
  if (!my_multi_malloc(MYF(0),
1410
 
           &drizzle->host_info, (uint) strlen(host_info)+1,
1411
 
           &drizzle->host,      (uint) strlen(host)+1,
1412
 
           &drizzle->unix_socket,unix_socket ?
1413
 
           (uint) strlen(unix_socket)+1 : (uint) 1,
1414
 
           &drizzle->server_version,
1415
 
           (uint) (end - (char*) net->read_pos),
1416
 
           NullS) ||
1417
 
      !(drizzle->user=my_strdup(user,MYF(0))) ||
1418
 
      !(drizzle->passwd=my_strdup(passwd,MYF(0))))
 
1109
  if (!(drizzle->host_info= (char *)malloc(strlen(host_info)+1+strlen(host)+1
 
1110
                                           +(end - (char*) net->read_pos))) ||
 
1111
      !(drizzle->user=strdup(user)) ||
 
1112
      !(drizzle->passwd=strdup(passwd)))
1419
1113
  {
1420
1114
    set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
1421
1115
    goto error;
1422
1116
  }
 
1117
  drizzle->host= drizzle->host_info+strlen(host_info)+1;
 
1118
  drizzle->server_version= drizzle->host+strlen(host)+1;
1423
1119
  strcpy(drizzle->host_info,host_info);
1424
1120
  strcpy(drizzle->host,host);
1425
 
  if (unix_socket)
1426
 
    strcpy(drizzle->unix_socket,unix_socket);
1427
 
  else
1428
 
    drizzle->unix_socket=0;
1429
1121
  strcpy(drizzle->server_version,(char*) net->read_pos+1);
1430
1122
  drizzle->port=port;
1431
1123
 
1432
1124
  /*
1433
1125
    Part 2: format and send client info to the server for access check
1434
1126
  */
1435
 
 
 
1127
 
1436
1128
  client_flag|=drizzle->options.client_flag;
1437
1129
  client_flag|=CLIENT_CAPABILITIES;
1438
1130
  if (client_flag & CLIENT_MULTI_STATEMENTS)
1449
1141
 
1450
1142
  int4store(buff, client_flag);
1451
1143
  int4store(buff+4, net->max_packet_size);
1452
 
  buff[8]= (char) drizzle->charset->number;
 
1144
  buff[8]= (char) 45; // utf8 charset number
1453
1145
  memset(buff+9, 0, 32-9);
1454
1146
  end= buff+32;
1455
1147
 
1479
1171
  if (db && (drizzle->server_capabilities & CLIENT_CONNECT_WITH_DB))
1480
1172
  {
1481
1173
    end= strncpy(end, db, NAME_LEN) + NAME_LEN + 1;
1482
 
    drizzle->db= my_strdup(db,MYF(MY_WME));
 
1174
    drizzle->db= strdup(db);
1483
1175
    db= 0;
1484
1176
  }
1485
1177
  /* Write authentication package */
1518
1210
    goto error;
1519
1211
  }
1520
1212
 
1521
 
  if (drizzle->options.init_commands)
1522
 
  {
1523
 
    DYNAMIC_ARRAY *init_commands= drizzle->options.init_commands;
1524
 
    char **ptr= (char**)init_commands->buffer;
1525
 
    char **end_command= ptr + init_commands->elements;
1526
 
 
1527
 
    bool reconnect=drizzle->reconnect;
1528
 
    drizzle->reconnect=0;
1529
 
 
1530
 
    for (; ptr < end_command; ptr++)
1531
 
    {
1532
 
      DRIZZLE_RES *res;
1533
 
      if (drizzle_real_query(drizzle,*ptr, (uint32_t) strlen(*ptr)))
1534
 
  goto error;
1535
 
      if (drizzle->fields)
1536
 
      {
1537
 
  if (!(res= cli_use_result(drizzle)))
1538
 
    goto error;
1539
 
  drizzle_free_result(res);
1540
 
      }
1541
 
    }
1542
 
    drizzle->reconnect=reconnect;
1543
 
  }
1544
1213
 
1545
1214
  reset_sigpipe(drizzle);
1546
1215
  return(drizzle);
1576
1245
  tmp_drizzle.options.my_cnf_file= tmp_drizzle.options.my_cnf_group= 0;
1577
1246
 
1578
1247
  if (!drizzle_connect(&tmp_drizzle,drizzle->host,drizzle->user,drizzle->passwd,
1579
 
        drizzle->db, drizzle->port, drizzle->unix_socket,
 
1248
        drizzle->db, drizzle->port, 0,
1580
1249
        drizzle->client_flag | CLIENT_REMEMBER_OPTIONS))
1581
1250
  {
1582
1251
    drizzle->net.last_errno= tmp_drizzle.net.last_errno;
1584
1253
    strcpy(drizzle->net.sqlstate, tmp_drizzle.net.sqlstate);
1585
1254
    return(1);
1586
1255
  }
1587
 
  if (drizzle_set_character_set(&tmp_drizzle, drizzle->charset->csname))
1588
 
  {
1589
 
    memset(&tmp_drizzle.options, 0, sizeof(tmp_drizzle.options));
1590
 
    drizzle_close(&tmp_drizzle);
1591
 
    drizzle->net.last_errno= tmp_drizzle.net.last_errno;
1592
 
    strcpy(drizzle->net.last_error, tmp_drizzle.net.last_error);
1593
 
    strcpy(drizzle->net.sqlstate, tmp_drizzle.net.sqlstate);
1594
 
    return(1);
1595
 
  }
1596
1256
 
1597
1257
  tmp_drizzle.reconnect= 1;
1598
1258
  tmp_drizzle.free_me= drizzle->free_me;
1620
1280
  if ((error=simple_command(drizzle,COM_INIT_DB, (const uchar*) db,
1621
1281
                            (uint32_t) strlen(db),0)))
1622
1282
    return(error);
1623
 
  my_free(drizzle->db,MYF(MY_ALLOW_ZERO_PTR));
1624
 
  drizzle->db=my_strdup(db,MYF(MY_WME));
 
1283
  if (drizzle->db != NULL)
 
1284
    free(drizzle->db);
 
1285
  drizzle->db=strdup(db);
1625
1286
  return(0);
1626
1287
}
1627
1288
 
1633
1294
 
1634
1295
static void drizzle_close_free_options(DRIZZLE *drizzle)
1635
1296
{
1636
 
  my_free(drizzle->options.user,MYF(MY_ALLOW_ZERO_PTR));
1637
 
  my_free(drizzle->options.host,MYF(MY_ALLOW_ZERO_PTR));
1638
 
  my_free(drizzle->options.password,MYF(MY_ALLOW_ZERO_PTR));
1639
 
  my_free(drizzle->options.unix_socket,MYF(MY_ALLOW_ZERO_PTR));
1640
 
  my_free(drizzle->options.db,MYF(MY_ALLOW_ZERO_PTR));
1641
 
  my_free(drizzle->options.my_cnf_file,MYF(MY_ALLOW_ZERO_PTR));
1642
 
  my_free(drizzle->options.my_cnf_group,MYF(MY_ALLOW_ZERO_PTR));
1643
 
  my_free(drizzle->options.charset_dir,MYF(MY_ALLOW_ZERO_PTR));
1644
 
  my_free(drizzle->options.charset_name,MYF(MY_ALLOW_ZERO_PTR));
1645
 
  my_free(drizzle->options.client_ip,MYF(MY_ALLOW_ZERO_PTR));
1646
 
  if (drizzle->options.init_commands)
1647
 
  {
1648
 
    DYNAMIC_ARRAY *init_commands= drizzle->options.init_commands;
1649
 
    char **ptr= (char**)init_commands->buffer;
1650
 
    char **end= ptr + init_commands->elements;
1651
 
    for (; ptr<end; ptr++)
1652
 
      my_free(*ptr,MYF(MY_WME));
1653
 
    delete_dynamic(init_commands);
1654
 
    my_free((char*)init_commands,MYF(MY_WME));
1655
 
  }
 
1297
  if (drizzle->options.user != NULL)
 
1298
    free(drizzle->options.user);
 
1299
  if (drizzle->options.host != NULL)
 
1300
    free(drizzle->options.host);
 
1301
  if (drizzle->options.password != NULL)
 
1302
    free(drizzle->options.password);
 
1303
  if (drizzle->options.db != NULL)
 
1304
    free(drizzle->options.db);
 
1305
  if (drizzle->options.my_cnf_file != NULL)
 
1306
    free(drizzle->options.my_cnf_file);
 
1307
  if (drizzle->options.my_cnf_group != NULL)
 
1308
    free(drizzle->options.my_cnf_group);
 
1309
  if (drizzle->options.client_ip != NULL)
 
1310
    free(drizzle->options.client_ip);
1656
1311
  memset(&drizzle->options, 0, sizeof(drizzle->options));
1657
1312
  return;
1658
1313
}
1660
1315
 
1661
1316
static void drizzle_close_free(DRIZZLE *drizzle)
1662
1317
{
1663
 
  my_free((uchar*) drizzle->host_info,MYF(MY_ALLOW_ZERO_PTR));
1664
 
  my_free(drizzle->user,MYF(MY_ALLOW_ZERO_PTR));
1665
 
  my_free(drizzle->passwd,MYF(MY_ALLOW_ZERO_PTR));
1666
 
  my_free(drizzle->db,MYF(MY_ALLOW_ZERO_PTR));
1667
 
  my_free(drizzle->info_buffer,MYF(MY_ALLOW_ZERO_PTR));
 
1318
  if (drizzle->host_info != NULL)
 
1319
    free((uchar*) drizzle->host_info);
 
1320
  if (drizzle->user != NULL)
 
1321
    free(drizzle->user);
 
1322
  if (drizzle->passwd != NULL)
 
1323
    free(drizzle->passwd);
 
1324
  if (drizzle->db != NULL)
 
1325
    free(drizzle->db);
 
1326
  if (drizzle->info_buffer != NULL)
 
1327
    free(drizzle->info_buffer);
1668
1328
  drizzle->info_buffer= 0;
1669
1329
 
1670
1330
  /* Clear pointers for better safety */
1688
1348
    drizzle_close_free_options(drizzle);
1689
1349
    drizzle_close_free(drizzle);
1690
1350
    if (drizzle->free_me)
1691
 
      my_free((uchar*) drizzle,MYF(0));
 
1351
      free((uchar*) drizzle);
1692
1352
  }
1693
1353
  return;
1694
1354
}
1742
1402
 
1743
1403
  if (!(fields=cli_read_rows(drizzle,(DRIZZLE_FIELD*)0, 7)))
1744
1404
    return(1);
1745
 
  if (!(drizzle->fields= unpack_fields(fields,&drizzle->field_alloc,
1746
 
            (uint) field_count, 0)))
 
1405
  if (!(drizzle->fields= unpack_fields(fields, (uint) field_count, 0)))
1747
1406
    return(1);
1748
1407
  drizzle->status= DRIZZLE_STATUS_GET_RESULT;
1749
1408
  drizzle->field_count= (uint) field_count;
1790
1449
    return(0);
1791
1450
  }
1792
1451
  drizzle->status=DRIZZLE_STATUS_READY;    /* server is ready */
1793
 
  if (!(result=(DRIZZLE_RES*) my_malloc((uint) (sizeof(DRIZZLE_RES)+
 
1452
  if (!(result=(DRIZZLE_RES*) malloc((uint) (sizeof(DRIZZLE_RES)+
1794
1453
                sizeof(uint32_t) *
1795
 
                drizzle->field_count),
1796
 
              MYF(MY_WME | MY_ZEROFILL))))
 
1454
                drizzle->field_count))))
1797
1455
  {
1798
1456
    set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
1799
1457
    return(0);
1800
1458
  }
 
1459
  memset(result, 0,(sizeof(DRIZZLE_RES)+ sizeof(uint32_t) *
 
1460
                    drizzle->field_count));
1801
1461
  result->methods= drizzle->methods;
1802
1462
  result->eof= 1;        /* Marker for buffered */
1803
1463
  result->lengths= (uint32_t*) (result+1);
1804
1464
  if (!(result->data=
1805
1465
  (*drizzle->methods->read_rows)(drizzle,drizzle->fields,drizzle->field_count)))
1806
1466
  {
1807
 
    my_free((uchar*) result,MYF(0));
 
1467
    free((uchar*) result);
1808
1468
    return(0);
1809
1469
  }
1810
1470
  drizzle->affected_rows= result->row_count= result->data->rows;
1811
1471
  result->data_cursor=  result->data->data;
1812
1472
  result->fields=  drizzle->fields;
1813
 
  result->field_alloc=  drizzle->field_alloc;
1814
1473
  result->field_count=  drizzle->field_count;
1815
1474
  /* The rest of result members is zeroed in malloc */
1816
1475
  drizzle->fields=0;        /* fields is now in result */
1817
 
  clear_alloc_root(&drizzle->field_alloc);
1818
1476
  /* just in case this was mistakenly called after drizzle_stmt_execute() */
1819
1477
  drizzle->unbuffered_fetch_owner= 0;
1820
1478
  return(result);        /* Data fetched */
1842
1500
    set_drizzle_error(drizzle, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate);
1843
1501
    return(0);
1844
1502
  }
1845
 
  if (!(result=(DRIZZLE_RES*) my_malloc(sizeof(*result)+
1846
 
              sizeof(uint32_t)*drizzle->field_count,
1847
 
              MYF(MY_WME | MY_ZEROFILL))))
 
1503
  if (!(result=(DRIZZLE_RES*) malloc(sizeof(*result)+
 
1504
                                     sizeof(uint32_t)*drizzle->field_count)))
1848
1505
    return(0);
 
1506
  memset(result, 0, sizeof(*result)+ sizeof(uint32_t)*drizzle->field_count);
1849
1507
  result->lengths=(uint32_t*) (result+1);
1850
1508
  result->methods= drizzle->methods;
1851
1509
  if (!(result->row=(DRIZZLE_ROW)
1852
 
  my_malloc(sizeof(result->row[0])*(drizzle->field_count+1), MYF(MY_WME))))
 
1510
        malloc(sizeof(result->row[0])*(drizzle->field_count+1))))
1853
1511
  {          /* Ptrs: to one row */
1854
 
    my_free((uchar*) result,MYF(0));
 
1512
    free((uchar*) result);
1855
1513
    return(0);
1856
1514
  }
1857
1515
  result->fields=  drizzle->fields;
1858
 
  result->field_alloc=  drizzle->field_alloc;
1859
1516
  result->field_count=  drizzle->field_count;
1860
1517
  result->current_field=0;
1861
1518
  result->handle=  drizzle;
1862
1519
  result->current_row=  0;
1863
1520
  drizzle->fields=0;      /* fields is now in result */
1864
 
  clear_alloc_root(&drizzle->field_alloc);
1865
1521
  drizzle->status=DRIZZLE_STATUS_USE_RESULT;
1866
1522
  drizzle->unbuffered_fetch_owner= &result->unbuffered_fetch_cancelled;
1867
1523
  return(result);      /* Data is read to be fetched */
1960
1616
    else
1961
1617
      drizzle->options.client_flag&= ~CLIENT_LOCAL_FILES;
1962
1618
    break;
1963
 
  case DRIZZLE_INIT_COMMAND:
1964
 
    add_init_command(&drizzle->options,arg);
1965
 
    break;
1966
1619
  case DRIZZLE_READ_DEFAULT_FILE:
1967
 
    my_free(drizzle->options.my_cnf_file,MYF(MY_ALLOW_ZERO_PTR));
1968
 
    drizzle->options.my_cnf_file=my_strdup(arg,MYF(MY_WME));
 
1620
    if (drizzle->options.my_cnf_file != NULL)
 
1621
      free(drizzle->options.my_cnf_file);
 
1622
    drizzle->options.my_cnf_file=strdup(arg);
1969
1623
    break;
1970
1624
  case DRIZZLE_READ_DEFAULT_GROUP:
1971
 
    my_free(drizzle->options.my_cnf_group,MYF(MY_ALLOW_ZERO_PTR));
1972
 
    drizzle->options.my_cnf_group=my_strdup(arg,MYF(MY_WME));
1973
 
    break;
1974
 
  case DRIZZLE_SET_CHARSET_DIR:
1975
 
    my_free(drizzle->options.charset_dir,MYF(MY_ALLOW_ZERO_PTR));
1976
 
    drizzle->options.charset_dir=my_strdup(arg,MYF(MY_WME));
1977
 
    break;
1978
 
  case DRIZZLE_SET_CHARSET_NAME:
1979
 
    my_free(drizzle->options.charset_name,MYF(MY_ALLOW_ZERO_PTR));
1980
 
    drizzle->options.charset_name=my_strdup(arg,MYF(MY_WME));
 
1625
    if (drizzle->options.my_cnf_group != NULL)
 
1626
      free(drizzle->options.my_cnf_group);
 
1627
    drizzle->options.my_cnf_group=strdup(arg);
1981
1628
    break;
1982
1629
  case DRIZZLE_OPT_PROTOCOL:
1983
1630
    drizzle->options.protocol= *(const uint*) arg;
1987
1634
    drizzle->options.methods_to_use= option;
1988
1635
    break;
1989
1636
  case DRIZZLE_SET_CLIENT_IP:
1990
 
    drizzle->options.client_ip= my_strdup(arg, MYF(MY_WME));
 
1637
    drizzle->options.client_ip= strdup(arg);
1991
1638
    break;
1992
1639
  case DRIZZLE_SECURE_AUTH:
1993
1640
    drizzle->options.secure_auth= *(const bool *) arg;
2067
1714
  return (uint32_t) major*10000L+(uint32_t) (minor*100+version);
2068
1715
}
2069
1716
 
2070
 
 
2071
 
/*
2072
 
   drizzle_set_character_set function sends SET NAMES cs_name to
2073
 
   the server (which changes character_set_client, character_set_result
2074
 
   and character_set_connection) and updates drizzle->charset so other
2075
 
   functions like drizzle_real_escape will work correctly.
2076
 
*/
2077
 
int drizzle_set_character_set(DRIZZLE *drizzle, const char *cs_name)
2078
 
{
2079
 
  const CHARSET_INFO *cs;
2080
 
  const char *save_csdir= charsets_dir;
2081
 
 
2082
 
  if (drizzle->options.charset_dir)
2083
 
    charsets_dir= drizzle->options.charset_dir;
2084
 
 
2085
 
  if (strlen(cs_name) < MY_CS_NAME_SIZE &&
2086
 
      (cs= get_charset_by_csname(cs_name, MY_CS_PRIMARY, MYF(0))))
2087
 
  {
2088
 
    char buff[MY_CS_NAME_SIZE + 10];
2089
 
    charsets_dir= save_csdir;
2090
 
    sprintf(buff, "SET NAMES %s", cs_name);
2091
 
    if (!drizzle_real_query(drizzle, buff, strlen(buff)))
2092
 
    {
2093
 
      drizzle->charset= cs;
2094
 
    }
2095
 
  }
2096
 
  else
2097
 
  {
2098
 
    char cs_dir_name[FN_REFLEN];
2099
 
    get_charsets_dir(cs_dir_name);
2100
 
    set_drizzle_extended_error(drizzle, CR_CANT_READ_CHARSET, unknown_sqlstate,
2101
 
                             ER(CR_CANT_READ_CHARSET), cs_name, cs_dir_name);
2102
 
  }
2103
 
  charsets_dir= save_csdir;
2104
 
  return drizzle->net.last_errno;
2105
 
}
2106
 
 
2107