1
/* Copyright (C) 2000-2004 MySQL 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 <my_global.h>
23
#include <mysys_err.h>
27
#include "drizzle_version.h"
28
#include "mysqld_error.h"
38
#include <sys/socket.h>
39
#include <netinet/in.h>
40
#include <arpa/inet.h>
45
#ifdef HAVE_SYS_SELECT_H
46
#include <sys/select.h>
55
#include <my_pthread.h> /* because of signal() */
57
#define INADDR_NONE -1
60
#include <sql_common.h>
61
#include "client_settings.h"
63
#undef net_buffer_length
64
#undef max_allowed_packet
66
ulong net_buffer_length=8192;
67
ulong max_allowed_packet= 1024L*1024L*1024L;
70
#define SOCKET_ERROR -1
73
If allowed through some configuration, then this needs to
76
#define MAX_LONG_DATA_LENGTH 8192
77
#define unsigned_field(A) ((A)->flags & UNSIGNED_FLAG)
79
static void append_wild(char *to,char *end,const char *wild);
81
static my_bool mysql_client_init= 0;
82
static my_bool org_my_init_done= 0;
86
Initialize the MySQL client library
92
Should be called before doing any other calls to the MySQL
93
client library to initialize thread specific variables etc.
94
It's called by mysql_init() to ensure that things will work for
95
old not threaded applications that doesn't call mysql_server_init()
100
1 could not initialize environment (out of memory or thread keys)
103
int STDCALL mysql_server_init(int argc __attribute__((unused)),
104
char **argv __attribute__((unused)),
105
char **groups __attribute__((unused)))
108
if (!mysql_client_init)
111
org_my_init_done=my_init_done;
112
if (my_init()) /* Will init threads */
117
mysql_port = MYSQL_PORT;
119
struct servent *serv_ptr;
123
if builder specifically requested a default port, use that
124
(even if it coincides with our factory default).
125
only if they didn't do we check /etc/services (and, failing
126
on that, fall back to the factory default of 3306).
127
either default can be overridden by the environment variable
128
MYSQL_TCP_PORT, which in turn can be overridden with command
132
#if MYSQL_PORT_DEFAULT == 0
133
if ((serv_ptr = getservbyname("mysql", "tcp")))
134
mysql_port = (uint) ntohs((ushort) serv_ptr->s_port);
136
if ((env = getenv("MYSQL_TCP_PORT")))
137
mysql_port =(uint) atoi(env);
140
if (!mysql_unix_port)
143
mysql_unix_port = (char*) MYSQL_UNIX_ADDR;
144
if ((env = getenv("MYSQL_UNIX_PORT")))
145
mysql_unix_port = env;
149
(void) signal(SIGPIPE, SIG_IGN);
153
result= (int)my_thread_init(); /* Init if new thread */
159
Free all memory and resources used by the client library
162
When calling this there should not be any other threads using
165
To make things simpler when used with windows dll's (which calls this
166
function automaticly), it's safe to call this function multiple times.
170
void STDCALL mysql_server_end()
172
if (!mysql_client_init)
175
finish_client_errs();
178
/* If library called my_init(), free memory allocated by it */
179
if (!org_my_init_done)
181
my_end(MY_DONT_FREE_DBUG);
182
/* Remove TRACING, if enabled by mysql_debug() */
191
mysql_client_init= org_my_init_done= 0;
192
#ifdef EMBEDDED_SERVER
195
fclose(stderror_file);
201
static MYSQL_PARAMETERS mysql_internal_parameters=
202
{&max_allowed_packet, &net_buffer_length, 0};
204
MYSQL_PARAMETERS *STDCALL mysql_get_parameters(void)
206
return &mysql_internal_parameters;
209
my_bool STDCALL mysql_thread_init()
211
return my_thread_init();
214
void STDCALL mysql_thread_end()
221
Expand wildcard to a sql string
225
append_wild(char *to, char *end, const char *wild)
227
end-=5; /* Some extra */
230
to=strmov(to," like '");
231
while (*wild && to < end)
233
if (*wild == '\\' || *wild == '\'')
237
if (*wild) /* Too small buffer */
238
*to++='%'; /* Nicer this way */
245
/**************************************************************************
246
Init debugging if MYSQL_DEBUG environment variable is found
247
**************************************************************************/
250
mysql_debug(const char *debug __attribute__((unused)))
258
else if ((env = getenv("MYSQL_DEBUG")))
261
#if !defined(_WINVER) && !defined(WINVER)
262
puts("\n-------------------------------------------------------");
263
puts("MYSQL_DEBUG found. libmysql started with the following:");
265
puts("-------------------------------------------------------\n");
269
buff[sizeof(buff)-1]= 0;
270
strxnmov(buff,sizeof(buff)-1,"libmysql: ", env, NullS);
271
MessageBox((HWND) 0,"Debugging variable MYSQL_DEBUG used",buff,MB_OK);
279
/**************************************************************************
280
Ignore SIGPIPE handler
282
**************************************************************************/
285
my_pipe_sig_handler(int sig __attribute__((unused)))
287
DBUG_PRINT("info",("Hit by signal %d",sig));
288
#ifdef DONT_REMEMBER_SIGNAL
289
(void) signal(SIGPIPE, my_pipe_sig_handler);
294
/**************************************************************************
295
Connect to sql server
296
If host == 0 then use localhost
297
**************************************************************************/
299
#ifdef USE_OLD_FUNCTIONS
301
mysql_connect(MYSQL *mysql,const char *host,
302
const char *user, const char *passwd)
305
mysql=mysql_init(mysql); /* Make it thread safe */
307
DBUG_ENTER("mysql_connect");
308
if (!(res=mysql_real_connect(mysql,host,user,passwd,NullS,0,NullS,0)))
311
my_free((uchar*) mysql,MYF(0));
320
/**************************************************************************
321
Change user and database
322
**************************************************************************/
324
int cli_read_change_user_result(MYSQL *mysql, char *buff, const char *passwd)
326
NET *net= &mysql->net;
329
pkt_length= cli_safe_read(mysql);
331
if (pkt_length == packet_error)
334
if (pkt_length == 1 && net->read_pos[0] == 254 &&
335
mysql->server_capabilities & CLIENT_SECURE_CONNECTION)
338
By sending this very specific reply server asks us to send scrambled
339
password in old format. The reply contains scramble_323.
341
scramble_323(buff, mysql->scramble, passwd);
342
if (my_net_write(net, (uchar*) buff, SCRAMBLE_LENGTH_323 + 1) ||
345
set_mysql_error(mysql, CR_SERVER_LOST, unknown_sqlstate);
348
/* Read what server thinks about out new auth message report */
349
if (cli_safe_read(mysql) == packet_error)
355
my_bool STDCALL mysql_change_user(MYSQL *mysql, const char *user,
356
const char *passwd, const char *db)
358
char buff[USERNAME_LENGTH+SCRAMBLED_PASSWORD_CHAR_LENGTH+NAME_LEN+2];
361
CHARSET_INFO *saved_cs= mysql->charset;
363
DBUG_ENTER("mysql_change_user");
365
/* Get the connection-default character set. */
367
if (mysql_init_character_set(mysql))
369
mysql->charset= saved_cs;
373
/* Use an empty string instead of NULL. */
380
/* Store user into the buffer */
381
end= strmake(end, user, USERNAME_LENGTH) + 1;
383
/* write scrambled password according to server capabilities */
386
if (mysql->server_capabilities & CLIENT_SECURE_CONNECTION)
388
*end++= SCRAMBLE_LENGTH;
389
scramble(end, mysql->scramble, passwd);
390
end+= SCRAMBLE_LENGTH;
394
scramble_323(end, mysql->scramble, passwd);
395
end+= SCRAMBLE_LENGTH_323 + 1;
399
*end++= '\0'; /* empty password */
400
/* Add database if needed */
401
end= strmake(end, db ? db : "", NAME_LEN) + 1;
403
/* Add character set number. */
405
if (mysql->server_capabilities & CLIENT_SECURE_CONNECTION)
407
int2store(end, (ushort) mysql->charset->number);
411
/* Write authentication package */
412
simple_command(mysql,COM_CHANGE_USER, (uchar*) buff, (ulong) (end-buff), 1);
414
rc= (*mysql->methods->read_change_user_result)(mysql, buff, passwd);
418
/* Free old connect information */
419
my_free(mysql->user,MYF(MY_ALLOW_ZERO_PTR));
420
my_free(mysql->passwd,MYF(MY_ALLOW_ZERO_PTR));
421
my_free(mysql->db,MYF(MY_ALLOW_ZERO_PTR));
423
/* alloc new connect information */
424
mysql->user= my_strdup(user,MYF(MY_WME));
425
mysql->passwd=my_strdup(passwd,MYF(MY_WME));
426
mysql->db= db ? my_strdup(db,MYF(MY_WME)) : 0;
430
mysql->charset= saved_cs;
436
#if defined(HAVE_GETPWUID) && defined(NO_GETPWUID_DECL)
437
struct passwd *getpwuid(uid_t);
438
char* getlogin(void);
441
void read_user_name(char *name)
443
DBUG_ENTER("read_user_name");
445
(void) strmov(name,"root"); /* allow use of surun */
451
if ((str=getlogin()) == NULL)
453
if ((skr=getpwuid(geteuid())) != NULL)
455
else if (!(str=getenv("USER")) && !(str=getenv("LOGNAME")) &&
456
!(str=getenv("LOGIN")))
459
(void) strmake(name,str,USERNAME_LENGTH);
461
(void) cuserid(name);
463
strmov(name,"UNKNOWN_USER");
469
my_bool handle_local_infile(MYSQL *mysql, const char *net_filename)
472
uint packet_length=MY_ALIGN(mysql->net.max_packet-16,IO_SIZE);
473
NET *net= &mysql->net;
475
void *li_ptr; /* pass state to local_infile functions */
476
char *buf; /* buffer to be filled by local_infile_read */
477
struct st_mysql_options *options= &mysql->options;
478
DBUG_ENTER("handle_local_infile");
480
/* check that we've got valid callback functions */
481
if (!(options->local_infile_init &&
482
options->local_infile_read &&
483
options->local_infile_end &&
484
options->local_infile_error))
486
/* if any of the functions is invalid, set the default */
487
mysql_set_local_infile_default(mysql);
490
/* copy filename into local memory and allocate read buffer */
491
if (!(buf=my_malloc(packet_length, MYF(0))))
493
set_mysql_error(mysql, CR_OUT_OF_MEMORY, unknown_sqlstate);
497
/* initialize local infile (open file, usually) */
498
if ((*options->local_infile_init)(&li_ptr, net_filename,
499
options->local_infile_userdata))
501
VOID(my_net_write(net,(const uchar*) "",0)); /* Server needs one packet */
503
strmov(net->sqlstate, unknown_sqlstate);
505
(*options->local_infile_error)(li_ptr,
507
sizeof(net->last_error)-1);
511
/* read blocks of data from local infile callback */
513
(*options->local_infile_read)(li_ptr, buf,
516
if (my_net_write(net, (uchar*) buf, readcount))
519
("Lost connection to MySQL server during LOAD DATA of local file"));
520
set_mysql_error(mysql, CR_SERVER_LOST, unknown_sqlstate);
525
/* Send empty packet to mark end of file */
526
if (my_net_write(net, (const uchar*) "", 0) || net_flush(net))
528
set_mysql_error(mysql, CR_SERVER_LOST, unknown_sqlstate);
535
(*options->local_infile_error)(li_ptr,
537
sizeof(net->last_error)-1);
544
/* free up memory allocated with _init, usually */
545
(*options->local_infile_end)(li_ptr);
546
my_free(buf, MYF(0));
551
/****************************************************************************
552
Default handlers for LOAD LOCAL INFILE
553
****************************************************************************/
555
typedef struct st_default_local_infile
559
const char *filename;
560
char error_msg[LOCAL_INFILE_ERROR_LEN];
561
} default_local_infile_data;
565
Open file for LOAD LOCAL INFILE
568
default_local_infile_init()
569
ptr Store pointer to internal data here
570
filename File name to open. This may be in unix format !
574
Even if this function returns an error, the load data interface
575
guarantees that default_local_infile_end() is called.
582
static int default_local_infile_init(void **ptr, const char *filename,
583
void *userdata __attribute__ ((unused)))
585
default_local_infile_data *data;
586
char tmp_name[FN_REFLEN];
588
if (!(*ptr= data= ((default_local_infile_data *)
589
my_malloc(sizeof(default_local_infile_data), MYF(0)))))
590
return 1; /* out of memory */
592
data->error_msg[0]= 0;
594
data->filename= filename;
596
fn_format(tmp_name, filename, "", "", MY_UNPACK_FILENAME);
597
if ((data->fd = my_open(tmp_name, O_RDONLY, MYF(0))) < 0)
599
data->error_num= my_errno;
600
snprintf(data->error_msg, sizeof(data->error_msg)-1,
601
EE(EE_FILENOTFOUND), tmp_name, data->error_num);
609
Read data for LOAD LOCAL INFILE
612
default_local_infile_read()
613
ptr Points to handle allocated by _init
615
buf_len Ammount of data to read
618
> 0 number of bytes read
623
static int default_local_infile_read(void *ptr, char *buf, uint buf_len)
626
default_local_infile_data*data = (default_local_infile_data *) ptr;
628
if ((count= (int) my_read(data->fd, (uchar *) buf, buf_len, MYF(0))) < 0)
630
data->error_num= EE_READ; /* the errmsg for not entire file read */
631
snprintf(data->error_msg, sizeof(data->error_msg)-1,
633
data->filename, my_errno);
640
Read data for LOAD LOCAL INFILE
643
default_local_infile_end()
644
ptr Points to handle allocated by _init
645
May be NULL if _init failed!
650
static void default_local_infile_end(void *ptr)
652
default_local_infile_data *data= (default_local_infile_data *) ptr;
653
if (data) /* If not error on open */
656
my_close(data->fd, MYF(MY_WME));
657
my_free(ptr, MYF(MY_WME));
663
Return error from LOAD LOCAL INFILE
666
default_local_infile_end()
667
ptr Points to handle allocated by _init
668
May be NULL if _init failed!
669
error_msg Store error text here
670
error_msg_len Max lenght of error_msg
677
default_local_infile_error(void *ptr, char *error_msg, uint error_msg_len)
679
default_local_infile_data *data = (default_local_infile_data *) ptr;
680
if (data) /* If not error on open */
682
strmake(error_msg, data->error_msg, error_msg_len);
683
return data->error_num;
685
/* This can only happen if we got error on malloc of handle */
686
strmov(error_msg, ER(CR_OUT_OF_MEMORY));
687
return CR_OUT_OF_MEMORY;
692
mysql_set_local_infile_handler(MYSQL *mysql,
693
int (*local_infile_init)(void **, const char *,
695
int (*local_infile_read)(void *, char *, uint),
696
void (*local_infile_end)(void *),
697
int (*local_infile_error)(void *, char *, uint),
700
mysql->options.local_infile_init= local_infile_init;
701
mysql->options.local_infile_read= local_infile_read;
702
mysql->options.local_infile_end= local_infile_end;
703
mysql->options.local_infile_error= local_infile_error;
704
mysql->options.local_infile_userdata = userdata;
708
void mysql_set_local_infile_default(MYSQL *mysql)
710
mysql->options.local_infile_init= default_local_infile_init;
711
mysql->options.local_infile_read= default_local_infile_read;
712
mysql->options.local_infile_end= default_local_infile_end;
713
mysql->options.local_infile_error= default_local_infile_error;
717
/**************************************************************************
718
Do a query. If query returned rows, free old rows.
719
Read data by mysql_store_result or by repeat call of mysql_fetch_row
720
**************************************************************************/
723
mysql_query(MYSQL *mysql, const char *query)
725
return mysql_real_query(mysql,query, (uint) strlen(query));
729
/**************************************************************************
730
Return next field of the query results
731
**************************************************************************/
733
MYSQL_FIELD * STDCALL
734
mysql_fetch_field(MYSQL_RES *result)
736
if (result->current_field >= result->field_count)
738
return &result->fields[result->current_field++];
742
/**************************************************************************
743
Move to a specific row and column
744
**************************************************************************/
747
mysql_data_seek(MYSQL_RES *result, my_ulonglong row)
750
DBUG_PRINT("info",("mysql_data_seek(%ld)",(long) row));
752
for (tmp=result->data->data; row-- && tmp ; tmp = tmp->next) ;
753
result->current_row=0;
754
result->data_cursor = tmp;
758
/*************************************************************************
759
put the row or field cursor one a position one got from mysql_row_tell()
760
This doesn't restore any data. The next mysql_fetch_row or
761
mysql_fetch_field will return the next row or field after the last used
762
*************************************************************************/
764
MYSQL_ROW_OFFSET STDCALL
765
mysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET row)
767
MYSQL_ROW_OFFSET return_value=result->data_cursor;
768
result->current_row= 0;
769
result->data_cursor= row;
774
MYSQL_FIELD_OFFSET STDCALL
775
mysql_field_seek(MYSQL_RES *result, MYSQL_FIELD_OFFSET field_offset)
777
MYSQL_FIELD_OFFSET return_value=result->current_field;
778
result->current_field=field_offset;
783
/*****************************************************************************
785
*****************************************************************************/
788
mysql_list_dbs(MYSQL *mysql, const char *wild)
791
DBUG_ENTER("mysql_list_dbs");
793
append_wild(strmov(buff,"show databases"),buff+sizeof(buff),wild);
794
if (mysql_query(mysql,buff))
796
DBUG_RETURN (mysql_store_result(mysql));
800
/*****************************************************************************
801
List all tables in a database
802
If wild is given then only the tables matching wild is returned
803
*****************************************************************************/
806
mysql_list_tables(MYSQL *mysql, const char *wild)
809
DBUG_ENTER("mysql_list_tables");
811
append_wild(strmov(buff,"show tables"),buff+sizeof(buff),wild);
812
if (mysql_query(mysql,buff))
814
DBUG_RETURN (mysql_store_result(mysql));
818
MYSQL_FIELD *cli_list_fields(MYSQL *mysql)
821
if (!(query= cli_read_rows(mysql,(MYSQL_FIELD*) 0,
822
protocol_41(mysql) ? 8 : 6)))
825
mysql->field_count= (uint) query->rows;
826
return unpack_fields(query,&mysql->field_alloc,
827
mysql->field_count, 1, mysql->server_capabilities);
831
/**************************************************************************
832
List all fields in a table
833
If wild is given then only the fields matching wild is returned
834
Instead of this use query:
835
show fields in 'table' like "wild"
836
**************************************************************************/
839
mysql_list_fields(MYSQL *mysql, const char *table, const char *wild)
844
DBUG_ENTER("mysql_list_fields");
845
DBUG_PRINT("enter",("table: '%s' wild: '%s'",table,wild ? wild : ""));
847
end=strmake(strmake(buff, table,128)+1,wild ? wild : "",128);
848
free_old_query(mysql);
849
if (simple_command(mysql, COM_FIELD_LIST, (uchar*) buff,
850
(ulong) (end-buff), 1) ||
851
!(fields= (*mysql->methods->list_fields)(mysql)))
854
if (!(result = (MYSQL_RES *) my_malloc(sizeof(MYSQL_RES),
855
MYF(MY_WME | MY_ZEROFILL))))
858
result->methods= mysql->methods;
859
result->field_alloc=mysql->field_alloc;
861
result->field_count = mysql->field_count;
862
result->fields= fields;
867
/* List all running processes (threads) in server */
870
mysql_list_processes(MYSQL *mysql)
875
DBUG_ENTER("mysql_list_processes");
877
if (simple_command(mysql,COM_PROCESS_INFO,0,0,0))
879
free_old_query(mysql);
880
pos=(uchar*) mysql->net.read_pos;
881
field_count=(uint) net_field_length(&pos);
882
if (!(fields = (*mysql->methods->read_rows)(mysql,(MYSQL_FIELD*) 0,
883
protocol_41(mysql) ? 7 : 5)))
885
if (!(mysql->fields=unpack_fields(fields,&mysql->field_alloc,field_count,0,
886
mysql->server_capabilities)))
888
mysql->status=MYSQL_STATUS_GET_RESULT;
889
mysql->field_count=field_count;
890
DBUG_RETURN(mysql_store_result(mysql));
894
#ifdef USE_OLD_FUNCTIONS
896
mysql_create_db(MYSQL *mysql, const char *db)
898
DBUG_ENTER("mysql_createdb");
899
DBUG_PRINT("enter",("db: %s",db));
900
DBUG_RETURN(simple_command(mysql,COM_CREATE_DB,db, (ulong) strlen(db),0));
905
mysql_drop_db(MYSQL *mysql, const char *db)
907
DBUG_ENTER("mysql_drop_db");
908
DBUG_PRINT("enter",("db: %s",db));
909
DBUG_RETURN(simple_command(mysql,COM_DROP_DB,db,(ulong) strlen(db),0));
915
mysql_shutdown(MYSQL *mysql, enum mysql_enum_shutdown_level shutdown_level)
918
DBUG_ENTER("mysql_shutdown");
919
level[0]= (uchar) shutdown_level;
920
DBUG_RETURN(simple_command(mysql, COM_SHUTDOWN, level, 1, 0));
925
mysql_refresh(MYSQL *mysql,uint options)
928
DBUG_ENTER("mysql_refresh");
929
bits[0]= (uchar) options;
930
DBUG_RETURN(simple_command(mysql, COM_REFRESH, bits, 1, 0));
935
mysql_kill(MYSQL *mysql,ulong pid)
938
DBUG_ENTER("mysql_kill");
940
DBUG_RETURN(simple_command(mysql,COM_PROCESS_KILL,buff,sizeof(buff),0));
945
mysql_set_server_option(MYSQL *mysql, enum enum_mysql_set_option option)
948
DBUG_ENTER("mysql_set_server_option");
949
int2store(buff, (uint) option);
950
DBUG_RETURN(simple_command(mysql, COM_SET_OPTION, buff, sizeof(buff), 0));
955
mysql_dump_debug_info(MYSQL *mysql)
957
DBUG_ENTER("mysql_dump_debug_info");
958
DBUG_RETURN(simple_command(mysql,COM_DEBUG,0,0,0));
962
const char *cli_read_statistics(MYSQL *mysql)
964
mysql->net.read_pos[mysql->packet_length]=0; /* End of stat string */
965
if (!mysql->net.read_pos[0])
967
set_mysql_error(mysql, CR_WRONG_HOST_INFO, unknown_sqlstate);
968
return mysql->net.last_error;
970
return (char*) mysql->net.read_pos;
975
mysql_stat(MYSQL *mysql)
977
DBUG_ENTER("mysql_stat");
978
if (simple_command(mysql,COM_STATISTICS,0,0,0))
979
DBUG_RETURN(mysql->net.last_error);
980
DBUG_RETURN((*mysql->methods->read_statistics)(mysql));
985
mysql_ping(MYSQL *mysql)
988
DBUG_ENTER("mysql_ping");
989
res= simple_command(mysql,COM_PING,0,0,0);
990
if (res == CR_SERVER_LOST && mysql->reconnect)
991
res= simple_command(mysql,COM_PING,0,0,0);
997
mysql_get_server_info(MYSQL *mysql)
999
return((char*) mysql->server_version);
1003
const char * STDCALL
1004
mysql_get_host_info(MYSQL *mysql)
1006
return(mysql->host_info);
1011
mysql_get_proto_info(MYSQL *mysql)
1013
return (mysql->protocol_version);
1016
const char * STDCALL
1017
mysql_get_client_info(void)
1019
return (char*) MYSQL_SERVER_VERSION;
1022
ulong STDCALL mysql_get_client_version(void)
1024
return MYSQL_VERSION_ID;
1027
my_bool STDCALL mysql_eof(MYSQL_RES *res)
1032
MYSQL_FIELD * STDCALL mysql_fetch_field_direct(MYSQL_RES *res,uint fieldnr)
1034
return &(res)->fields[fieldnr];
1037
MYSQL_FIELD * STDCALL mysql_fetch_fields(MYSQL_RES *res)
1039
return (res)->fields;
1042
MYSQL_ROW_OFFSET STDCALL mysql_row_tell(MYSQL_RES *res)
1044
return res->data_cursor;
1047
MYSQL_FIELD_OFFSET STDCALL mysql_field_tell(MYSQL_RES *res)
1049
return (res)->current_field;
1054
unsigned int STDCALL mysql_field_count(MYSQL *mysql)
1056
return mysql->field_count;
1059
my_ulonglong STDCALL mysql_affected_rows(MYSQL *mysql)
1061
return mysql->affected_rows;
1064
my_ulonglong STDCALL mysql_insert_id(MYSQL *mysql)
1066
return mysql->insert_id;
1069
const char *STDCALL mysql_sqlstate(MYSQL *mysql)
1071
return mysql ? mysql->net.sqlstate : cant_connect_sqlstate;
1074
uint STDCALL mysql_warning_count(MYSQL *mysql)
1076
return mysql->warning_count;
1079
const char *STDCALL mysql_info(MYSQL *mysql)
1084
ulong STDCALL mysql_thread_id(MYSQL *mysql)
1086
return (mysql)->thread_id;
1089
const char * STDCALL mysql_character_set_name(MYSQL *mysql)
1091
return mysql->charset->csname;
1094
void STDCALL mysql_get_character_set_info(MYSQL *mysql, MY_CHARSET_INFO *csinfo)
1096
csinfo->number = mysql->charset->number;
1097
csinfo->state = mysql->charset->state;
1098
csinfo->csname = mysql->charset->csname;
1099
csinfo->name = mysql->charset->name;
1100
csinfo->comment = mysql->charset->comment;
1101
csinfo->mbminlen = mysql->charset->mbminlen;
1102
csinfo->mbmaxlen = mysql->charset->mbmaxlen;
1104
if (mysql->options.charset_dir)
1105
csinfo->dir = mysql->options.charset_dir;
1107
csinfo->dir = charsets_dir;
1110
uint STDCALL mysql_thread_safe(void)
1116
my_bool STDCALL mysql_embedded(void)
1118
#ifdef EMBEDDED_LIBRARY
1125
/****************************************************************************
1126
Some support functions
1127
****************************************************************************/
1130
Functions called my my_net_init() to set some application specific variables
1133
void my_net_local_init(NET *net)
1135
net->max_packet= (uint) net_buffer_length;
1136
my_net_set_read_timeout(net, CLIENT_NET_READ_TIMEOUT);
1137
my_net_set_write_timeout(net, CLIENT_NET_WRITE_TIMEOUT);
1138
net->retry_count= 1;
1139
net->max_packet_size= max(net_buffer_length, max_allowed_packet);
1143
This function is used to create HEX string that you
1144
can use in a SQL statement in of the either ways:
1145
INSERT INTO blob_column VALUES (0xAABBCC); (any MySQL version)
1146
INSERT INTO blob_column VALUES (X'AABBCC'); (4.1 and higher)
1148
The string in "from" is encoded to a HEX string.
1149
The result is placed in "to" and a terminating null byte is appended.
1151
The string pointed to by "from" must be "length" bytes long.
1152
You must allocate the "to" buffer to be at least length*2+1 bytes long.
1153
Each character needs two bytes, and you need room for the terminating
1154
null byte. When mysql_hex_string() returns, the contents of "to" will
1155
be a null-terminated string. The return value is the length of the
1156
encoded string, not including the terminating null character.
1158
The return value does not contain any leading 0x or a leading X' and
1159
trailing '. The caller must supply whichever of those is desired.
1163
mysql_hex_string(char *to, const char *from, ulong length)
1168
for (end= from + length; from < end; from++)
1170
*to++= _dig_vec_upper[((unsigned char) *from) >> 4];
1171
*to++= _dig_vec_upper[((unsigned char) *from) & 0x0F];
1174
return (ulong) (to-to0);
1178
Add escape characters to a string (blob?) to make it suitable for a insert
1179
to should at least have place for length*2+1 chars
1180
Returns the length of the to string
1184
mysql_escape_string(char *to,const char *from,ulong length)
1186
return escape_string_for_mysql(default_charset_info, to, 0, from, length);
1190
mysql_real_escape_string(MYSQL *mysql, char *to,const char *from,
1193
if (mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES)
1194
return escape_quotes_for_mysql(mysql->charset, to, 0, from, length);
1195
return escape_string_for_mysql(mysql->charset, to, 0, from, length);
1199
myodbc_remove_escape(MYSQL *mysql,char *name)
1203
my_bool use_mb_flag=use_mb(mysql->charset);
1206
for (end=name; *end ; end++) ;
1209
for (to=name ; *name ; name++)
1213
if (use_mb_flag && (l = my_ismbchar( mysql->charset, name , end ) ) )
1221
if (*name == '\\' && name[1])
1228
int cli_unbuffered_fetch(MYSQL *mysql, char **row)
1230
if (packet_error == cli_safe_read(mysql))
1233
*row= ((mysql->net.read_pos[0] == 254) ? NULL :
1234
(char*) (mysql->net.read_pos+1));
1238
/********************************************************************
1240
*********************************************************************/
1243
Commit the current transaction
1246
my_bool STDCALL mysql_commit(MYSQL * mysql)
1248
DBUG_ENTER("mysql_commit");
1249
DBUG_RETURN((my_bool) mysql_real_query(mysql, "commit", 6));
1253
Rollback the current transaction
1256
my_bool STDCALL mysql_rollback(MYSQL * mysql)
1258
DBUG_ENTER("mysql_rollback");
1259
DBUG_RETURN((my_bool) mysql_real_query(mysql, "rollback", 8));
1264
Set autocommit to either true or false
1267
my_bool STDCALL mysql_autocommit(MYSQL * mysql, my_bool auto_mode)
1269
DBUG_ENTER("mysql_autocommit");
1270
DBUG_PRINT("enter", ("mode : %d", auto_mode));
1272
DBUG_RETURN((my_bool) mysql_real_query(mysql, auto_mode ?
1273
"set autocommit=1":"set autocommit=0",
1278
/********************************************************************
1279
Multi query execution + SPs APIs
1280
*********************************************************************/
1283
Returns true/false to indicate whether any more query results exist
1284
to be read using mysql_next_result()
1287
my_bool STDCALL mysql_more_results(MYSQL *mysql)
1290
DBUG_ENTER("mysql_more_results");
1292
res= ((mysql->server_status & SERVER_MORE_RESULTS_EXISTS) ? 1: 0);
1293
DBUG_PRINT("exit",("More results exists ? %d", res));
1299
Reads and returns the next query results
1301
int STDCALL mysql_next_result(MYSQL *mysql)
1303
DBUG_ENTER("mysql_next_result");
1305
if (mysql->status != MYSQL_STATUS_READY)
1307
set_mysql_error(mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate);
1311
net_clear_error(&mysql->net);
1312
mysql->affected_rows= ~(my_ulonglong) 0;
1314
if (mysql->server_status & SERVER_MORE_RESULTS_EXISTS)
1315
DBUG_RETURN((*mysql->methods->next_result)(mysql));
1317
DBUG_RETURN(-1); /* No more results */
1321
MYSQL_RES * STDCALL mysql_use_result(MYSQL *mysql)
1323
return (*mysql->methods->use_result)(mysql);
1326
my_bool STDCALL mysql_read_query_result(MYSQL *mysql)
1328
return (*mysql->methods->read_query_result)(mysql);