507
525
free_rows(result->data);
509
free_root(&result->field_alloc,MYF(0));
526
/* TODO: free result->fields */
511
my_free((uchar*) result->row,MYF(0));
512
my_free((uchar*) result,MYF(0));
516
/****************************************************************************
517
Get options from my.cnf
518
****************************************************************************/
520
static const char *default_options[]=
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",
533
static TYPELIB option_types={array_elements(default_options)-1,
534
"options",default_options, NULL};
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};
541
static int add_init_command(struct st_drizzle_options *options, const char *cmd)
545
if (!options->init_commands)
547
options->init_commands= (DYNAMIC_ARRAY*)my_malloc(sizeof(DYNAMIC_ARRAY),
549
init_dynamic_array(options->init_commands,sizeof(char*),0,5 CALLER_INFO);
552
if (!(tmp= my_strdup(cmd,MYF(MY_WME))) ||
553
insert_dynamic(options->init_commands, (uchar*)&tmp))
555
my_free(tmp, MYF(MY_ALLOW_ZERO_PTR));
562
void drizzle_read_default_options(struct st_drizzle_options *options,
563
const char *filename,const char *group)
566
char *argv_buff[1],**argv;
567
const char *groups[3];
569
argc=1; argv=argv_buff; argv_buff[0]= (char*) "client";
570
groups[0]= (char*) "client"; groups[1]= (char*) group; groups[2]=0;
572
load_defaults(filename, groups, &argc, &argv);
573
if (argc != 1) /* If some default option */
578
if (option[0][0] == '-' && option[0][1] == '-')
580
char *end=strrchr(*option,'=');
585
*end=0; /* Remove '=' */
587
/* Change all '_' in variable name to '-' */
588
for (end= *option ; *(end= strrchr(end,'_')) ; )
590
switch (find_type(*option+2,&option_types,2)) {
593
options->port=atoi(opt_arg);
598
my_free(options->unix_socket,MYF(MY_ALLOW_ZERO_PTR));
599
options->unix_socket=my_strdup(opt_arg,MYF(MY_WME));
602
case 3: /* compress */
604
options->client_flag|= CLIENT_COMPRESS;
606
case 4: /* password */
609
my_free(options->password,MYF(MY_ALLOW_ZERO_PTR));
610
options->password=my_strdup(opt_arg,MYF(MY_WME));
613
case 20: /* connect_timeout */
614
case 6: /* timeout */
616
options->connect_timeout=atoi(opt_arg);
621
my_free(options->user,MYF(MY_ALLOW_ZERO_PTR));
622
options->user=my_strdup(opt_arg,MYF(MY_WME));
625
case 8: /* init-command */
626
add_init_command(options,opt_arg);
631
my_free(options->host,MYF(MY_ALLOW_ZERO_PTR));
632
options->host=my_strdup(opt_arg,MYF(MY_WME));
635
case 10: /* database */
638
my_free(options->db,MYF(MY_ALLOW_ZERO_PTR));
639
options->db=my_strdup(opt_arg,MYF(MY_WME));
642
case 12: /* return-found-rows */
643
options->client_flag|=CLIENT_FOUND_ROWS;
645
case 13: /* Ignore SSL options */
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));
656
my_free(options->charset_name,MYF(MY_ALLOW_ZERO_PTR));
657
options->charset_name = my_strdup(opt_arg, MYF(MY_WME));
659
case 19: /* Interactive-timeout */
660
options->client_flag|= CLIENT_INTERACTIVE;
663
if (!opt_arg || atoi(opt_arg) != 0)
664
options->client_flag|= CLIENT_LOCAL_FILES;
666
options->client_flag&= ~CLIENT_LOCAL_FILES;
669
options->client_flag&= ~CLIENT_LOCAL_FILES;
671
case 24: /* max-allowed-packet */
673
options->max_allowed_packet= atoi(opt_arg);
675
case 25: /* protocol */
676
if ((options->protocol= find_type(opt_arg,
677
&sql_protocol_typelib,0)) <= 0)
679
fprintf(stderr, _("Unknown option to protocol: %s\n"), opt_arg);
683
case 27: /* multi-results */
684
options->client_flag|= CLIENT_MULTI_RESULTS;
686
case 28: /* multi-statements */
687
case 29: /* multi-queries */
688
options->client_flag|= CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS;
690
case 30: /* secure-auth */
691
options->secure_auth= true;
693
case 31: /* report-data-truncation */
694
options->report_data_truncation= opt_arg ? test(atoi(opt_arg)) : 1;
528
free((uchar*) result->row);
529
free((uchar*) result);
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]);
768
594
field->catalog_length= lengths[0];
769
595
field->db_length= lengths[1];
1129
int drizzle_init_character_set(DRIZZLE *drizzle)
1131
const char *default_collation_name;
1133
/* Set character set */
1134
if (!drizzle->options.charset_name)
1136
default_collation_name= DRIZZLE_DEFAULT_COLLATION_NAME;
1137
if (!(drizzle->options.charset_name=
1138
my_strdup(DRIZZLE_DEFAULT_CHARSET_NAME,MYF(MY_WME))))
1142
default_collation_name= NULL;
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)
1152
const CHARSET_INFO *collation;
1154
get_charset_by_name(default_collation_name, MYF(MY_WME))))
1156
if (!my_charset_same(drizzle->charset, collation))
1158
my_printf_error(ER_UNKNOWN_ERROR,
1159
_("COLLATION %s is not valid for CHARACTER SET %s"),
1161
default_collation_name, drizzle->options.charset_name);
1162
drizzle->charset= NULL;
1166
drizzle->charset= collation;
1170
drizzle->charset= NULL;
1175
if (!drizzle->charset)
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);
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,
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,
919
const char * unix_port __attribute__((__unused__)),
920
uint32_t client_flag)
1203
922
char buff[NAME_LEN+USERNAME_LENGTH+100];
1204
923
char *end,*host_info=NULL;
1405
if (drizzle_init_character_set(drizzle))
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),
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)))
1420
1114
set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
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);
1426
strcpy(drizzle->unix_socket,unix_socket);
1428
drizzle->unix_socket=0;
1429
1121
strcpy(drizzle->server_version,(char*) net->read_pos+1);
1430
1122
drizzle->port=port;
1433
1125
Part 2: format and send client info to the server for access check
1436
1128
client_flag|=drizzle->options.client_flag;
1437
1129
client_flag|=CLIENT_CAPABILITIES;
1438
1130
if (client_flag & CLIENT_MULTI_STATEMENTS)
1634
1295
static void drizzle_close_free_options(DRIZZLE *drizzle)
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)
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));
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));
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))))
1798
1456
set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
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)))
1807
my_free((uchar*) result,MYF(0));
1467
free((uchar*) result);
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);
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)))
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);
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 */
1961
1617
drizzle->options.client_flag&= ~CLIENT_LOCAL_FILES;
1963
case DRIZZLE_INIT_COMMAND:
1964
add_init_command(&drizzle->options,arg);
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);
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));
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));
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);
1982
1629
case DRIZZLE_OPT_PROTOCOL:
1983
1630
drizzle->options.protocol= *(const uint*) arg;
2067
1714
return (uint32_t) major*10000L+(uint32_t) (minor*100+version);
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.
2077
int drizzle_set_character_set(DRIZZLE *drizzle, const char *cs_name)
2079
const CHARSET_INFO *cs;
2080
const char *save_csdir= charsets_dir;
2082
if (drizzle->options.charset_dir)
2083
charsets_dir= drizzle->options.charset_dir;
2085
if (strlen(cs_name) < MY_CS_NAME_SIZE &&
2086
(cs= get_charset_by_csname(cs_name, MY_CS_PRIMARY, MYF(0))))
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)))
2093
drizzle->charset= cs;
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);
2103
charsets_dir= save_csdir;
2104
return drizzle->net.last_errno;