1
/* Copyright (C) 2000-2004 DRIZZLE AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation.
7
There are special exceptions to the terms and conditions of the GPL as it
8
is applied to this software. View the full text of the exception in file
9
EXCEPTIONS-CLIENT in the directory of this software distribution.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
20
#include <drizzled/global.h>
21
#include <mysys/my_sys.h>
23
#include <mysys/mysys_err.h>
24
#include <mystrings/m_string.h>
25
#include <mystrings/m_ctype.h>
28
#include <vio/violite.h>
36
#include <sys/socket.h>
37
#include <netinet/in.h>
38
#include <arpa/inet.h>
43
#ifdef HAVE_SYS_SELECT_H
44
#include <sys/select.h>
53
#include <mysys/my_pthread.h> /* because of signal() */
55
#define INADDR_NONE -1
58
#include <sql_common.h>
59
#include "client_settings.h"
61
#undef net_buffer_length
62
#undef max_allowed_packet
64
uint32_t net_buffer_length= 8192;
65
uint32_t max_allowed_packet= 1024L*1024L*1024L;
68
#define SOCKET_ERROR -1
71
If allowed through some configuration, then this needs to
74
#define MAX_LONG_DATA_LENGTH 8192
75
#define unsigned_field(A) ((A)->flags & UNSIGNED_FLAG)
77
static void append_wild(char *to,char *end,const char *wild);
80
static DRIZZLE_PARAMETERS drizzle_internal_parameters=
81
{&max_allowed_packet, &net_buffer_length, 0};
83
const DRIZZLE_PARAMETERS *STDCALL drizzle_get_parameters(void)
85
return &drizzle_internal_parameters;
88
bool STDCALL drizzle_thread_init()
90
return my_thread_init();
93
void STDCALL drizzle_thread_end()
100
Expand wildcard to a sql string
104
append_wild(char *to, char *end, const char *wild)
106
end-=5; /* Some extra */
109
to=strmov(to," like '");
110
while (*wild && to < end)
112
if (*wild == '\\' || *wild == '\'')
116
if (*wild) /* Too small buffer */
117
*to++='%'; /* Nicer this way */
123
/**************************************************************************
124
Ignore SIGPIPE handler
126
**************************************************************************/
129
my_pipe_sig_handler(int sig __attribute__((unused)))
131
#ifdef DONT_REMEMBER_SIGNAL
132
(void) signal(SIGPIPE, my_pipe_sig_handler);
137
/**************************************************************************
138
Change user and database
139
**************************************************************************/
141
int cli_read_change_user_result(DRIZZLE *drizzle)
145
pkt_length= cli_safe_read(drizzle);
147
if (pkt_length == packet_error)
153
bool STDCALL drizzle_change_user(DRIZZLE *drizzle, const char *user,
154
const char *passwd, const char *db)
156
char buff[USERNAME_LENGTH+SCRAMBLED_PASSWORD_CHAR_LENGTH+NAME_LEN+2];
159
const CHARSET_INFO *saved_cs= drizzle->charset;
161
/* Get the connection-default character set. */
163
if (drizzle_init_character_set(drizzle))
165
drizzle->charset= saved_cs;
169
/* Use an empty string instead of NULL. */
176
/* Store user into the buffer */
177
end= strmake(end, user, USERNAME_LENGTH) + 1;
179
/* write scrambled password according to server capabilities */
183
*end++= SCRAMBLE_LENGTH;
184
scramble(end, drizzle->scramble, passwd);
185
end+= SCRAMBLE_LENGTH;
189
*end++= '\0'; /* empty password */
190
/* Add database if needed */
191
end= strmake(end, db ? db : "", NAME_LEN) + 1;
193
/* Add character set number. */
195
if (drizzle->server_capabilities & CLIENT_SECURE_CONNECTION)
197
int2store(end, (ushort) drizzle->charset->number);
201
/* Write authentication package */
202
(void)simple_command(drizzle,COM_CHANGE_USER, (uchar*) buff, (ulong) (end-buff), 1);
204
rc= (*drizzle->methods->read_change_user_result)(drizzle);
208
/* Free old connect information */
212
free(drizzle->passwd);
216
/* alloc new connect information */
217
drizzle->user= my_strdup(user,MYF(MY_WME));
218
drizzle->passwd=my_strdup(passwd,MYF(MY_WME));
219
drizzle->db= db ? my_strdup(db,MYF(MY_WME)) : 0;
223
drizzle->charset= saved_cs;
230
/**************************************************************************
231
Do a query. If query returned rows, free old rows.
232
Read data by drizzle_store_result or by repeat call of drizzle_fetch_row
233
**************************************************************************/
236
drizzle_query(DRIZZLE *drizzle, const char *query)
238
return drizzle_real_query(drizzle,query, (uint) strlen(query));
242
/**************************************************************************
243
Return next field of the query results
244
**************************************************************************/
246
DRIZZLE_FIELD * STDCALL
247
drizzle_fetch_field(DRIZZLE_RES *result)
249
if (result->current_field >= result->field_count)
251
return &result->fields[result->current_field++];
255
/**************************************************************************
256
Move to a specific row and column
257
**************************************************************************/
260
drizzle_data_seek(DRIZZLE_RES *result, uint64_t row)
264
for (tmp=result->data->data; row-- && tmp ; tmp = tmp->next) ;
265
result->current_row=0;
266
result->data_cursor = tmp;
270
/*************************************************************************
271
put the row or field cursor one a position one got from DRIZZLE_ROW_tell()
272
This doesn't restore any data. The next drizzle_fetch_row or
273
drizzle_fetch_field will return the next row or field after the last used
274
*************************************************************************/
276
DRIZZLE_ROW_OFFSET STDCALL
277
drizzle_row_seek(DRIZZLE_RES *result, DRIZZLE_ROW_OFFSET row)
279
DRIZZLE_ROW_OFFSET return_value=result->data_cursor;
280
result->current_row= 0;
281
result->data_cursor= row;
286
DRIZZLE_FIELD_OFFSET STDCALL
287
drizzle_field_seek(DRIZZLE_RES *result, DRIZZLE_FIELD_OFFSET field_offset)
289
DRIZZLE_FIELD_OFFSET return_value=result->current_field;
290
result->current_field=field_offset;
295
/*****************************************************************************
297
*****************************************************************************/
299
DRIZZLE_RES * STDCALL
300
drizzle_list_dbs(DRIZZLE *drizzle, const char *wild)
304
append_wild(strmov(buff,"show databases"),buff+sizeof(buff),wild);
305
if (drizzle_query(drizzle,buff))
307
return (drizzle_store_result(drizzle));
311
/*****************************************************************************
312
List all tables in a database
313
If wild is given then only the tables matching wild is returned
314
*****************************************************************************/
316
DRIZZLE_RES * STDCALL
317
drizzle_list_tables(DRIZZLE *drizzle, const char *wild)
321
append_wild(strmov(buff,"show tables"),buff+sizeof(buff),wild);
322
if (drizzle_query(drizzle,buff))
324
return (drizzle_store_result(drizzle));
328
DRIZZLE_FIELD *cli_list_fields(DRIZZLE *drizzle)
331
if (!(query= cli_read_rows(drizzle,(DRIZZLE_FIELD*) 0,
332
protocol_41(drizzle) ? 8 : 6)))
335
drizzle->field_count= (uint) query->rows;
336
return unpack_fields(query,&drizzle->field_alloc,
337
drizzle->field_count, 1, drizzle->server_capabilities);
341
/**************************************************************************
342
List all fields in a table
343
If wild is given then only the fields matching wild is returned
344
Instead of this use query:
345
show fields in 'table' like "wild"
346
**************************************************************************/
348
DRIZZLE_RES * STDCALL
349
drizzle_list_fields(DRIZZLE *drizzle, const char *table, const char *wild)
352
DRIZZLE_FIELD *fields;
355
end=strmake(strmake(buff, table,128)+1,wild ? wild : "",128);
356
free_old_query(drizzle);
357
if (simple_command(drizzle, COM_FIELD_LIST, (uchar*) buff,
358
(ulong) (end-buff), 1) ||
359
!(fields= (*drizzle->methods->list_fields)(drizzle)))
362
if (!(result = (DRIZZLE_RES *) malloc(sizeof(DRIZZLE_RES))))
365
result->methods= drizzle->methods;
366
result->field_alloc=drizzle->field_alloc;
368
result->field_count = drizzle->field_count;
369
result->fields= fields;
374
/* List all running processes (threads) in server */
376
DRIZZLE_RES * STDCALL
377
drizzle_list_processes(DRIZZLE *drizzle)
379
DRIZZLE_DATA *fields;
383
if (simple_command(drizzle,COM_PROCESS_INFO,0,0,0))
385
free_old_query(drizzle);
386
pos=(uchar*) drizzle->net.read_pos;
387
field_count=(uint) net_field_length(&pos);
388
if (!(fields = (*drizzle->methods->read_rows)(drizzle,(DRIZZLE_FIELD*) 0,
389
protocol_41(drizzle) ? 7 : 5)))
391
if (!(drizzle->fields=unpack_fields(fields,&drizzle->field_alloc,field_count,0,
392
drizzle->server_capabilities)))
394
drizzle->status=DRIZZLE_STATUS_GET_RESULT;
395
drizzle->field_count=field_count;
396
return(drizzle_store_result(drizzle));
401
drizzle_shutdown(DRIZZLE *drizzle, enum drizzle_enum_shutdown_level shutdown_level)
404
level[0]= (uchar) shutdown_level;
405
return(simple_command(drizzle, COM_SHUTDOWN, level, 1, 0));
410
drizzle_refresh(DRIZZLE *drizzle,uint options)
413
bits[0]= (uchar) options;
414
return(simple_command(drizzle, COM_REFRESH, bits, 1, 0));
419
drizzle_kill(DRIZZLE *drizzle, uint32_t pid)
423
return(simple_command(drizzle,COM_PROCESS_KILL,buff,sizeof(buff),0));
428
drizzle_set_server_option(DRIZZLE *drizzle, enum enum_drizzle_set_option option)
431
int2store(buff, (uint) option);
432
return(simple_command(drizzle, COM_SET_OPTION, buff, sizeof(buff), 0));
436
const char *cli_read_statistics(DRIZZLE *drizzle)
438
drizzle->net.read_pos[drizzle->packet_length]=0; /* End of stat string */
439
if (!drizzle->net.read_pos[0])
441
set_drizzle_error(drizzle, CR_WRONG_HOST_INFO, unknown_sqlstate);
442
return drizzle->net.last_error;
444
return (char*) drizzle->net.read_pos;
449
drizzle_stat(DRIZZLE *drizzle)
451
if (simple_command(drizzle,COM_STATISTICS,0,0,0))
452
return(drizzle->net.last_error);
453
return((*drizzle->methods->read_statistics)(drizzle));
458
drizzle_ping(DRIZZLE *drizzle)
461
res= simple_command(drizzle,COM_PING,0,0,0);
462
if (res == CR_SERVER_LOST && drizzle->reconnect)
463
res= simple_command(drizzle,COM_PING,0,0,0);
469
drizzle_get_server_info(const DRIZZLE *drizzle)
471
return((char*) drizzle->server_version);
476
drizzle_get_host_info(const DRIZZLE *drizzle)
478
return(drizzle->host_info);
483
drizzle_get_proto_info(const DRIZZLE *drizzle)
485
return (drizzle->protocol_version);
489
drizzle_get_client_info(void)
491
return (char*) MYSQL_SERVER_VERSION;
494
uint32_t STDCALL drizzle_get_client_version(void)
496
return MYSQL_VERSION_ID;
499
bool STDCALL drizzle_eof(const DRIZZLE_RES *res)
504
const DRIZZLE_FIELD * STDCALL drizzle_fetch_field_direct(const DRIZZLE_RES *res, unsigned int fieldnr)
506
return &(res)->fields[fieldnr];
509
const DRIZZLE_FIELD * STDCALL drizzle_fetch_fields(const DRIZZLE_RES *res)
514
DRIZZLE_ROW_OFFSET STDCALL drizzle_row_tell(const DRIZZLE_RES *res)
516
return res->data_cursor;
519
DRIZZLE_FIELD_OFFSET STDCALL drizzle_field_tell(const DRIZZLE_RES *res)
521
return res->current_field;
526
unsigned int STDCALL drizzle_field_count(const DRIZZLE *drizzle)
528
return drizzle->field_count;
531
uint64_t STDCALL drizzle_affected_rows(const DRIZZLE *drizzle)
533
return drizzle->affected_rows;
536
uint64_t STDCALL drizzle_insert_id(const DRIZZLE *drizzle)
538
return drizzle->insert_id;
541
const char *STDCALL drizzle_sqlstate(const DRIZZLE *drizzle)
543
return drizzle ? drizzle->net.sqlstate : cant_connect_sqlstate;
546
uint32_t STDCALL drizzle_warning_count(const DRIZZLE *drizzle)
548
return drizzle->warning_count;
551
const char *STDCALL drizzle_info(const DRIZZLE *drizzle)
553
return drizzle->info;
556
uint32_t STDCALL drizzle_thread_id(const DRIZZLE *drizzle)
558
return drizzle->thread_id;
561
const char * STDCALL drizzle_character_set_name(const DRIZZLE *drizzle)
563
return drizzle->charset->csname;
566
void STDCALL drizzle_get_character_set_info(const DRIZZLE *drizzle, MY_CHARSET_INFO *csinfo)
568
csinfo->number = drizzle->charset->number;
569
csinfo->state = drizzle->charset->state;
570
csinfo->csname = drizzle->charset->csname;
571
csinfo->name = drizzle->charset->name;
572
csinfo->comment = drizzle->charset->comment;
573
csinfo->mbminlen = drizzle->charset->mbminlen;
574
csinfo->mbmaxlen = drizzle->charset->mbmaxlen;
576
if (drizzle->options.charset_dir)
577
csinfo->dir = drizzle->options.charset_dir;
579
csinfo->dir = charsets_dir;
582
uint STDCALL drizzle_thread_safe(void)
588
bool STDCALL drizzle_embedded(void)
590
#ifdef EMBEDDED_LIBRARY
597
/****************************************************************************
598
Some support functions
599
****************************************************************************/
602
Functions called my my_net_init() to set some application specific variables
605
void my_net_local_init(NET *net)
607
net->max_packet= (uint) net_buffer_length;
608
my_net_set_read_timeout(net, CLIENT_NET_READ_TIMEOUT);
609
my_net_set_write_timeout(net, CLIENT_NET_WRITE_TIMEOUT);
611
net->max_packet_size= max(net_buffer_length, max_allowed_packet);
615
This function is used to create HEX string that you
616
can use in a SQL statement in of the either ways:
617
INSERT INTO blob_column VALUES (0xAABBCC); (any DRIZZLE version)
618
INSERT INTO blob_column VALUES (X'AABBCC'); (4.1 and higher)
620
The string in "from" is encoded to a HEX string.
621
The result is placed in "to" and a terminating null byte is appended.
623
The string pointed to by "from" must be "length" bytes long.
624
You must allocate the "to" buffer to be at least length*2+1 bytes long.
625
Each character needs two bytes, and you need room for the terminating
626
null byte. When drizzle_hex_string() returns, the contents of "to" will
627
be a null-terminated string. The return value is the length of the
628
encoded string, not including the terminating null character.
630
The return value does not contain any leading 0x or a leading X' and
631
trailing '. The caller must supply whichever of those is desired.
635
drizzle_hex_string(char *to, const char *from, uint32_t length)
640
for (end= from + length; from < end; from++)
642
*to++= _dig_vec_upper[((unsigned char) *from) >> 4];
643
*to++= _dig_vec_upper[((unsigned char) *from) & 0x0F];
646
return (uint32_t) (to-to0);
650
Add escape characters to a string (blob?) to make it suitable for a insert
651
to should at least have place for length*2+1 chars
652
Returns the length of the to string
656
drizzle_escape_string(char *to,const char *from, uint32_t length)
658
return escape_string_for_drizzle(default_charset_info, to, 0, from, length);
662
drizzle_real_escape_string(DRIZZLE *drizzle, char *to,const char *from,
665
if (drizzle->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES)
666
return escape_quotes_for_drizzle(drizzle->charset, to, 0, from, length);
667
return escape_string_for_drizzle(drizzle->charset, to, 0, from, length);
671
myodbc_remove_escape(const DRIZZLE *drizzle, char *name)
675
bool use_mb_flag= use_mb(drizzle->charset);
678
for (end=name; *end ; end++) ;
681
for (to=name ; *name ; name++)
685
if (use_mb_flag && (l = my_ismbchar( drizzle->charset, name , end ) ) )
693
if (*name == '\\' && name[1])
700
int cli_unbuffered_fetch(DRIZZLE *drizzle, char **row)
702
if (packet_error == cli_safe_read(drizzle))
705
*row= ((drizzle->net.read_pos[0] == DRIZZLE_PROTOCOL_NO_MORE_DATA) ? NULL :
706
(char*) (drizzle->net.read_pos+1));
710
/********************************************************************
712
*********************************************************************/
715
Commit the current transaction
718
bool STDCALL drizzle_commit(DRIZZLE *drizzle)
720
return((bool) drizzle_real_query(drizzle, "commit", 6));
724
Rollback the current transaction
727
bool STDCALL drizzle_rollback(DRIZZLE *drizzle)
729
return((bool) drizzle_real_query(drizzle, "rollback", 8));
734
Set autocommit to either true or false
737
bool STDCALL drizzle_autocommit(DRIZZLE *drizzle, bool auto_mode)
739
return((bool) drizzle_real_query(drizzle, auto_mode ?
740
"set autocommit=1":"set autocommit=0",
745
/********************************************************************
746
Multi query execution + SPs APIs
747
*********************************************************************/
750
Returns true/false to indicate whether any more query results exist
751
to be read using drizzle_next_result()
754
bool STDCALL drizzle_more_results(const DRIZZLE *drizzle)
756
return (drizzle->server_status & SERVER_MORE_RESULTS_EXISTS) ? true:false;
761
Reads and returns the next query results
763
int STDCALL drizzle_next_result(DRIZZLE *drizzle)
765
if (drizzle->status != DRIZZLE_STATUS_READY)
767
set_drizzle_error(drizzle, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate);
771
net_clear_error(&drizzle->net);
772
drizzle->affected_rows= ~(uint64_t) 0;
774
if (drizzle->server_status & SERVER_MORE_RESULTS_EXISTS)
775
return((*drizzle->methods->next_result)(drizzle));
777
return(-1); /* No more results */
781
DRIZZLE_RES * STDCALL drizzle_use_result(DRIZZLE *drizzle)
783
return (*drizzle->methods->use_result)(drizzle);
786
bool STDCALL drizzle_read_query_result(DRIZZLE *drizzle)
788
return (*drizzle->methods->read_query_result)(drizzle);