16 #include <my_global.h>
19 #include <mysys_err.h>
23 #include "mysql_version.h"
24 #include "mysqld_error.h"
37 #ifdef HAVE_SYS_SELECT_H
38 #include <sys/select.h>
48 #include <my_pthread.h>
51 #define INADDR_NONE -1
54 #include <sql_common.h>
55 #include "client_settings.h"
57 #undef net_buffer_length
58 #undef max_allowed_packet
60 ulong net_buffer_length=8192;
61 ulong max_allowed_packet= 1024L*1024L*1024L;
64 #ifdef EMBEDDED_LIBRARY
74 #define SOCKET_ERROR -1
81 #define MAX_LONG_DATA_LENGTH 8192
82 #define unsigned_field(A) ((A)->flags & UNSIGNED_FLAG)
84 static void append_wild(
char *
to,
char *end,
const char *wild);
85 sig_handler my_pipe_sig_handler(
int sig);
87 static my_bool mysql_client_init= 0;
88 static my_bool org_my_init_done= 0;
114 int STDCALL mysql_server_init(
int argc __attribute__((unused)),
115 char **argv __attribute__((unused)),
116 char **groups __attribute__((unused)))
119 if (!mysql_client_init)
122 org_my_init_done=my_init_done;
131 struct servent *serv_ptr __attribute__((unused));
133 mysql_port = MYSQL_PORT;
145 #if MYSQL_PORT_DEFAULT == 0
146 if ((serv_ptr= getservbyname(
"mysql",
"tcp")))
147 mysql_port= (uint) ntohs((ushort) serv_ptr->s_port);
149 if ((env= getenv(
"MYSQL_TCP_PORT")))
150 mysql_port=(uint) atoi(env);
153 if (!mysql_unix_port)
157 mysql_unix_port = (
char*) MYSQL_NAMEDPIPE;
159 mysql_unix_port = (
char*) MYSQL_UNIX_ADDR;
161 if ((env = getenv(
"MYSQL_UNIX_PORT")))
162 mysql_unix_port = env;
165 #if defined(SIGPIPE) && !defined(__WIN__)
166 (void) signal(SIGPIPE, SIG_IGN);
168 #ifdef EMBEDDED_LIBRARY
170 result= init_embedded_server(argc, argv, groups);
174 result= (int)my_thread_init();
191 void STDCALL mysql_server_end()
193 if (!mysql_client_init)
198 #ifdef EMBEDDED_LIBRARY
199 end_embedded_server();
201 finish_client_errs();
205 if (!org_my_init_done)
215 mysql_client_init= org_my_init_done= 0;
219 {&max_allowed_packet, &net_buffer_length, 0};
223 return &mysql_internal_parameters;
226 my_bool STDCALL mysql_thread_init()
228 return my_thread_init();
231 void STDCALL mysql_thread_end()
242 append_wild(
char *
to,
char *end,
const char *wild)
247 to=strmov(to,
" like '");
248 while (*wild && to < end)
250 if (*wild ==
'\\' || *wild ==
'\'')
267 mysql_debug(
const char *debug __attribute__((unused)))
275 else if ((env = getenv(
"MYSQL_DEBUG")))
278 #if !defined(_WINVER) && !defined(WINVER)
279 puts(
"\n-------------------------------------------------------");
280 puts(
"MYSQL_DEBUG found. libmysql started with the following:");
282 puts(
"-------------------------------------------------------\n");
286 buff[
sizeof(buff)-1]= 0;
287 strxnmov(buff,
sizeof(buff)-1,
"libmysql: ", env, NullS);
288 MessageBox((HWND) 0,
"Debugging variable MYSQL_DEBUG used",buff,MB_OK);
302 my_pipe_sig_handler(
int sig __attribute__((unused)))
304 DBUG_PRINT(
"info",(
"Hit by signal %d",sig));
305 #ifdef SIGNAL_HANDLER_RESET_ON_DELIVERY
306 (void) signal(SIGPIPE, my_pipe_sig_handler);
316 #ifdef USE_OLD_FUNCTIONS
318 mysql_connect(
MYSQL *mysql,
const char *host,
319 const char *user,
const char *passwd)
322 mysql=mysql_init(mysql);
324 DBUG_ENTER(
"mysql_connect");
325 if (!(res=mysql_real_connect(mysql,host,user,passwd,NullS,0,NullS,0)))
341 my_bool STDCALL mysql_change_user(
MYSQL *mysql,
const char *user,
342 const char *passwd,
const char *db)
346 char *saved_user= mysql->user;
347 char *saved_passwd= mysql->passwd;
348 char *saved_db= mysql->db;
350 DBUG_ENTER(
"mysql_change_user");
354 if (mysql_init_character_set(mysql))
356 mysql->charset= saved_cs;
362 mysql->user= (
char*)(user ? user :
"");
363 mysql->passwd= (
char*)(passwd ? passwd :
"");
366 rc= run_plugin_auth(mysql, 0, 0, 0, db);
372 mysql_detach_stmt_list(&mysql->stmts,
"mysql_change_user");
377 my_free(saved_passwd);
381 mysql->user= my_strdup(mysql->user, MYF(MY_WME));
382 mysql->passwd= my_strdup(mysql->passwd, MYF(MY_WME));
383 mysql->db= db ? my_strdup(db, MYF(MY_WME)) : 0;
387 mysql->charset= saved_cs;
388 mysql->user= saved_user;
389 mysql->passwd= saved_passwd;
396 #if defined(HAVE_GETPWUID) && defined(NO_GETPWUID_DECL)
397 struct passwd *getpwuid(uid_t);
398 char* getlogin(
void);
401 #if !defined(__WIN__)
403 void read_user_name(
char *
name)
405 DBUG_ENTER(
"read_user_name");
407 (void) strmov(name,
"root");
413 if ((str=getlogin()) == NULL)
415 if ((skr=getpwuid(geteuid())) != NULL)
417 else if (!(str=getenv(
"USER")) && !(str=getenv(
"LOGNAME")) &&
418 !(str=getenv(
"LOGIN")))
421 (void) strmake(name,str,USERNAME_LENGTH);
423 (void) cuserid(name);
425 strmov(name,
"UNKNOWN_USER");
433 void read_user_name(
char *name)
435 char *str=getenv(
"USER");
436 strmake(name,str ? str :
"ODBC", USERNAME_LENGTH);
441 my_bool handle_local_infile(
MYSQL *mysql,
const char *net_filename)
444 uint packet_length=MY_ALIGN(mysql->net.max_packet-16,IO_SIZE);
445 NET *net= &mysql->net;
450 DBUG_ENTER(
"handle_local_infile");
453 if (!(options->local_infile_init &&
454 options->local_infile_read &&
455 options->local_infile_end &&
456 options->local_infile_error))
459 mysql_set_local_infile_default(mysql);
463 if (!(buf=my_malloc(packet_length, MYF(0))))
465 set_mysql_error(mysql, CR_OUT_OF_MEMORY, unknown_sqlstate);
470 if ((*options->local_infile_init)(&li_ptr, net_filename,
471 options->local_infile_userdata))
475 strmov(net->
sqlstate, unknown_sqlstate);
477 (*options->local_infile_error)(li_ptr,
485 (*options->local_infile_read)(li_ptr, buf,
491 (
"Lost connection to MySQL server during LOAD DATA of local file"));
492 set_mysql_error(mysql, CR_SERVER_LOST, unknown_sqlstate);
500 set_mysql_error(mysql, CR_SERVER_LOST, unknown_sqlstate);
507 (*options->local_infile_error)(li_ptr,
517 (*options->local_infile_end)(li_ptr);
531 const char *filename;
532 char error_msg[LOCAL_INFILE_ERROR_LEN];
554 static int default_local_infile_init(
void **ptr,
const char *filename,
555 void *userdata __attribute__ ((unused)))
558 char tmp_name[FN_REFLEN];
564 data->error_msg[0]= 0;
566 data->filename= filename;
568 fn_format(tmp_name, filename,
"",
"", MY_UNPACK_FILENAME);
569 if ((data->fd = my_open(tmp_name, O_RDONLY, MYF(0))) < 0)
571 char errbuf[MYSYS_STRERROR_SIZE];
572 data->error_num= my_errno;
573 my_snprintf(data->error_msg,
sizeof(data->error_msg)-1,
574 EE(EE_FILENOTFOUND), tmp_name, data->error_num,
575 my_strerror(errbuf,
sizeof(errbuf), data->error_num));
597 static int default_local_infile_read(
void *ptr,
char *buf, uint buf_len)
602 if ((count= (
int) my_read(data->fd, (uchar *) buf, buf_len, MYF(0))) < 0)
604 char errbuf[MYSYS_STRERROR_SIZE];
605 data->error_num= EE_READ;
606 my_snprintf(data->error_msg,
sizeof(data->error_msg)-1,
607 EE(EE_READ), data->filename,
608 my_errno, my_strerror(errbuf,
sizeof(errbuf), my_errno));
625 static void default_local_infile_end(
void *ptr)
631 my_close(data->fd, MYF(MY_WME));
652 default_local_infile_error(
void *ptr,
char *error_msg, uint error_msg_len)
657 strmake(error_msg, data->error_msg, error_msg_len);
658 return data->error_num;
661 strmov(error_msg, ER(CR_OUT_OF_MEMORY));
662 return CR_OUT_OF_MEMORY;
667 mysql_set_local_infile_handler(
MYSQL *mysql,
668 int (*local_infile_init)(
void **,
const char *,
670 int (*local_infile_read)(
void *,
char *, uint),
671 void (*local_infile_end)(
void *),
672 int (*local_infile_error)(
void *,
char *, uint),
675 mysql->options.local_infile_init= local_infile_init;
676 mysql->options.local_infile_read= local_infile_read;
677 mysql->options.local_infile_end= local_infile_end;
678 mysql->options.local_infile_error= local_infile_error;
679 mysql->options.local_infile_userdata = userdata;
683 void mysql_set_local_infile_default(
MYSQL *mysql)
685 mysql->options.local_infile_init= default_local_infile_init;
686 mysql->options.local_infile_read= default_local_infile_read;
687 mysql->options.local_infile_end= default_local_infile_end;
688 mysql->options.local_infile_error= default_local_infile_error;
700 return mysql_real_query(mysql,query, (uint) strlen(query));
711 if (result->current_field >= result->field_count)
713 return &result->fields[result->current_field++];
722 mysql_data_seek(
MYSQL_RES *result, my_ulonglong row)
725 DBUG_PRINT(
"info",(
"mysql_data_seek(%ld)",(
long) row));
727 for (tmp=result->data->data; row-- && tmp ; tmp = tmp->next) ;
728 result->current_row=0;
729 result->data_cursor = tmp;
743 result->current_row= 0;
744 result->data_cursor= row;
749 MYSQL_FIELD_OFFSET STDCALL
750 mysql_field_seek(
MYSQL_RES *result, MYSQL_FIELD_OFFSET field_offset)
752 MYSQL_FIELD_OFFSET return_value=result->current_field;
753 result->current_field=field_offset;
763 mysql_list_dbs(
MYSQL *mysql,
const char *wild)
766 DBUG_ENTER(
"mysql_list_dbs");
768 append_wild(strmov(buff,
"show databases"),buff+
sizeof(buff),wild);
769 if (mysql_query(mysql,buff))
771 DBUG_RETURN (mysql_store_result(mysql));
781 mysql_list_tables(
MYSQL *mysql,
const char *wild)
784 DBUG_ENTER(
"mysql_list_tables");
786 append_wild(strmov(buff,
"show tables"),buff+
sizeof(buff),wild);
787 if (mysql_query(mysql,buff))
789 DBUG_RETURN (mysql_store_result(mysql));
797 protocol_41(mysql) ? 8 : 6)))
800 mysql->field_count= (uint) query->rows;
801 return unpack_fields(mysql, query,&mysql->field_alloc,
802 mysql->field_count, 1, mysql->server_capabilities);
814 mysql_list_fields(
MYSQL *mysql,
const char *
table,
const char *wild)
819 DBUG_ENTER(
"mysql_list_fields");
820 DBUG_PRINT(
"enter",(
"table: '%s' wild: '%s'",table,wild ? wild :
""));
822 end=strmake(strmake(buff, table,128)+1,wild ? wild :
"",128);
823 free_old_query(mysql);
824 if (simple_command(mysql, COM_FIELD_LIST, (uchar*) buff,
825 (ulong) (end-buff), 1) ||
826 !(fields= (*mysql->methods->list_fields)(mysql)))
830 MYF(MY_WME | MY_ZEROFILL))))
833 result->methods= mysql->methods;
834 result->field_alloc=mysql->field_alloc;
836 result->field_count = mysql->field_count;
837 result->fields= fields;
845 mysql_list_processes(
MYSQL *mysql)
850 DBUG_ENTER(
"mysql_list_processes");
853 if (simple_command(mysql,COM_PROCESS_INFO,0,0,0))
855 free_old_query(mysql);
856 pos=(uchar*) mysql->net.read_pos;
857 field_count=(uint) net_field_length(&pos);
858 if (!(fields = (*mysql->methods->read_rows)(mysql,(
MYSQL_FIELD*) 0,
859 protocol_41(mysql) ? 7 : 5)))
861 if (!(mysql->fields=unpack_fields(mysql, fields,&mysql->field_alloc,field_count,0,
862 mysql->server_capabilities)))
864 mysql->status=MYSQL_STATUS_GET_RESULT;
865 mysql->field_count=field_count;
866 DBUG_RETURN(mysql_store_result(mysql));
870 #ifdef USE_OLD_FUNCTIONS
872 mysql_create_db(
MYSQL *mysql,
const char *db)
874 DBUG_ENTER(
"mysql_createdb");
875 DBUG_PRINT(
"enter",(
"db: %s",db));
876 DBUG_RETURN(simple_command(mysql,COM_CREATE_DB,db, (ulong) strlen(db),0));
881 mysql_drop_db(
MYSQL *mysql,
const char *db)
883 DBUG_ENTER(
"mysql_drop_db");
884 DBUG_PRINT(
"enter",(
"db: %s",db));
885 DBUG_RETURN(simple_command(mysql,COM_DROP_DB,db,(ulong) strlen(db),0));
891 mysql_shutdown(
MYSQL *mysql,
enum mysql_enum_shutdown_level shutdown_level)
894 DBUG_ENTER(
"mysql_shutdown");
895 level[0]= (uchar) shutdown_level;
896 DBUG_RETURN(simple_command(mysql, COM_SHUTDOWN, level, 1, 0));
901 mysql_refresh(
MYSQL *mysql,uint options)
904 DBUG_ENTER(
"mysql_refresh");
905 bits[0]= (uchar) options;
906 DBUG_RETURN(simple_command(mysql, COM_REFRESH, bits, 1, 0));
911 mysql_kill(
MYSQL *mysql,ulong pid)
914 DBUG_ENTER(
"mysql_kill");
923 if (pid & (~0xfffffffful))
924 DBUG_RETURN(CR_INVALID_CONN_HANDLE);
926 DBUG_RETURN(simple_command(mysql,COM_PROCESS_KILL,buff,
sizeof(buff),0));
931 mysql_set_server_option(
MYSQL *mysql,
enum enum_mysql_set_option
option)
934 DBUG_ENTER(
"mysql_set_server_option");
935 int2store(buff, (uint) option);
936 DBUG_RETURN(simple_command(mysql, COM_SET_OPTION, buff,
sizeof(buff), 0));
941 mysql_dump_debug_info(
MYSQL *mysql)
943 DBUG_ENTER(
"mysql_dump_debug_info");
944 DBUG_RETURN(simple_command(mysql,COM_DEBUG,0,0,0));
948 const char *cli_read_statistics(
MYSQL *mysql)
950 mysql->net.read_pos[mysql->packet_length]=0;
951 if (!mysql->net.read_pos[0])
953 set_mysql_error(mysql, CR_WRONG_HOST_INFO, unknown_sqlstate);
956 return (
char*) mysql->net.read_pos;
961 mysql_stat(
MYSQL *mysql)
963 DBUG_ENTER(
"mysql_stat");
964 if (simple_command(mysql,COM_STATISTICS,0,0,0))
966 DBUG_RETURN((*mysql->methods->read_statistics)(mysql));
971 mysql_ping(
MYSQL *mysql)
974 DBUG_ENTER(
"mysql_ping");
975 res= simple_command(mysql,COM_PING,0,0,0);
976 if (res == CR_SERVER_LOST && mysql->reconnect)
977 res= simple_command(mysql,COM_PING,0,0,0);
983 mysql_get_server_info(
MYSQL *mysql)
985 return((
char*) mysql->server_version);
990 mysql_get_host_info(
MYSQL *mysql)
992 return(mysql->host_info);
997 mysql_get_proto_info(
MYSQL *mysql)
999 return (mysql->protocol_version);
1002 const char * STDCALL
1003 mysql_get_client_info(
void)
1005 return (
char*) MYSQL_SERVER_VERSION;
1008 ulong STDCALL mysql_get_client_version(
void)
1010 return MYSQL_VERSION_ID;
1013 my_bool STDCALL mysql_eof(
MYSQL_RES *res)
1020 return &(res)->fields[fieldnr];
1025 return (res)->fields;
1030 return res->data_cursor;
1033 MYSQL_FIELD_OFFSET STDCALL mysql_field_tell(
MYSQL_RES *res)
1035 return (res)->current_field;
1040 unsigned int STDCALL mysql_field_count(
MYSQL *mysql)
1042 return mysql->field_count;
1045 my_ulonglong STDCALL mysql_affected_rows(
MYSQL *mysql)
1047 return mysql->affected_rows;
1050 my_ulonglong STDCALL mysql_insert_id(
MYSQL *mysql)
1052 return mysql->insert_id;
1055 const char *STDCALL mysql_sqlstate(
MYSQL *mysql)
1057 return mysql ? mysql->net.
sqlstate : cant_connect_sqlstate;
1060 uint STDCALL mysql_warning_count(
MYSQL *mysql)
1062 return mysql->warning_count;
1065 const char *STDCALL mysql_info(
MYSQL *mysql)
1070 ulong STDCALL mysql_thread_id(
MYSQL *mysql)
1078 return (mysql)->thread_id;
1081 const char * STDCALL mysql_character_set_name(
MYSQL *mysql)
1083 return mysql->charset->csname;
1088 csinfo->number = mysql->charset->number;
1089 csinfo->state = mysql->charset->state;
1090 csinfo->csname = mysql->charset->csname;
1091 csinfo->name = mysql->charset->name;
1092 csinfo->comment = mysql->charset->comment;
1093 csinfo->mbminlen = mysql->charset->mbminlen;
1094 csinfo->mbmaxlen = mysql->charset->mbmaxlen;
1096 if (mysql->options.charset_dir)
1097 csinfo->dir = mysql->options.charset_dir;
1099 csinfo->dir = charsets_dir;
1102 uint STDCALL mysql_thread_safe(
void)
1108 my_bool STDCALL mysql_embedded(
void)
1110 #ifdef EMBEDDED_LIBRARY
1125 void my_net_local_init(
NET *net)
1127 net->max_packet= (uint) net_buffer_length;
1128 my_net_set_read_timeout(net, CLIENT_NET_READ_TIMEOUT);
1129 my_net_set_write_timeout(net, CLIENT_NET_WRITE_TIMEOUT);
1130 net->retry_count= 1;
1131 net->max_packet_size= MY_MAX(net_buffer_length, max_allowed_packet);
1155 mysql_hex_string(
char *to,
const char *from, ulong length)
1160 for (end= from + length; from < end; from++)
1162 *to++= _dig_vec_upper[((
unsigned char) *from) >> 4];
1163 *to++= _dig_vec_upper[((
unsigned char) *from) & 0x0F];
1166 return (ulong) (to-to0);
1176 mysql_escape_string(
char *to,
const char *from,ulong length)
1178 return (uint) escape_string_for_mysql(default_charset_info, to, 0, from, length);
1182 mysql_real_escape_string(
MYSQL *mysql,
char *to,
const char *from,
1185 if (mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES)
1186 return (uint) escape_quotes_for_mysql(mysql->charset, to, 0, from, length);
1187 return (uint) escape_string_for_mysql(mysql->charset, to, 0, from, length);
1191 myodbc_remove_escape(
MYSQL *mysql,
char *name)
1195 my_bool use_mb_flag=use_mb(mysql->charset);
1196 char *UNINIT_VAR(end);
1198 for (end=name; *end ; end++) ;
1201 for (to=name ; *
name ; name++)
1205 if (use_mb_flag && (l = my_ismbchar( mysql->charset, name , end ) ) )
1213 if (*name ==
'\\' && name[1])
1236 #define DEFAULT_PREFETCH_ROWS (ulong) 1
1246 static int stmt_read_row_unbuffered(
MYSQL_STMT *stmt,
unsigned char **row);
1247 static int stmt_read_row_buffered(
MYSQL_STMT *stmt,
unsigned char **row);
1248 static int stmt_read_row_from_cursor(
MYSQL_STMT *stmt,
unsigned char **row);
1249 static int stmt_read_row_no_data(
MYSQL_STMT *stmt,
unsigned char **row);
1250 static int stmt_read_row_no_result_set(
MYSQL_STMT *stmt,
unsigned char **row);
1261 #define RESET_SERVER_SIDE 1
1262 #define RESET_LONG_DATA 2
1263 #define RESET_STORE_RESULT 4
1264 #define RESET_CLEAR_ERROR 8
1274 #define MAX_DATE_REP_LENGTH 5
1280 #define MAX_TIME_REP_LENGTH 13
1286 #define MAX_DATETIME_REP_LENGTH 12
1288 #define MAX_DOUBLE_STRING_REP_LENGTH 331
1292 #define IS_TRUNCATED(value, is_unsigned, min, max, umax) \
1293 ((is_unsigned) ? (((value) > (umax) || (value) < 0) ? 1 : 0) : \
1294 (((value) > (max) || (value) < (min)) ? 1 : 0))
1296 #define BIND_RESULT_DONE 1
1302 #define REPORT_DATA_TRUNCATION 2
1321 static my_bool my_realloc_str(
NET *net, ulong length)
1323 ulong buf_length= (ulong) (net->write_pos - net->buff);
1325 DBUG_ENTER(
"my_realloc_str");
1326 if (buf_length + length > net->max_packet)
1331 strmov(net->
sqlstate, unknown_sqlstate);
1332 strmov(net->
last_error, ER(net->last_errno));
1334 net->write_pos= net->buff+ buf_length;
1340 static void stmt_clear_error(
MYSQL_STMT *stmt)
1342 if (stmt->last_errno)
1344 stmt->last_errno= 0;
1345 stmt->last_error[0]=
'\0';
1346 strmov(stmt->sqlstate, not_error_sqlstate);
1355 void set_stmt_error(
MYSQL_STMT * stmt,
int errcode,
1356 const char *sqlstate,
const char *err)
1358 DBUG_ENTER(
"set_stmt_error");
1359 DBUG_PRINT(
"enter", (
"error: %d '%s'", errcode, ER(errcode)));
1360 DBUG_ASSERT(stmt != 0);
1365 stmt->last_errno= errcode;
1366 strmov(stmt->last_error, ER(errcode));
1367 strmov(stmt->sqlstate, sqlstate);
1382 DBUG_ENTER(
"set_stmt_errmsg");
1383 DBUG_PRINT(
"enter", (
"error: %d/%s '%s'",
1387 DBUG_ASSERT(stmt != 0);
1389 stmt->last_errno= net->last_errno;
1392 strmov(stmt->sqlstate, net->
sqlstate);
1414 uint field_count, param_count;
1415 ulong packet_length;
1417 DBUG_ENTER(
"cli_read_prepare_result");
1419 if ((packet_length= cli_safe_read(mysql)) == packet_error)
1421 mysql->warning_count= 0;
1423 pos= (uchar*) mysql->net.read_pos;
1424 stmt->stmt_id= uint4korr(pos+1); pos+= 5;
1426 field_count= uint2korr(pos); pos+= 2;
1428 param_count= uint2korr(pos); pos+= 2;
1429 if (packet_length >= 12)
1430 mysql->warning_count= uint2korr(pos+1);
1432 if (param_count != 0)
1437 if (!(param_data= (*mysql->methods->read_rows)(mysql, (
MYSQL_FIELD*)0, 7)))
1439 free_rows(param_data);
1442 if (field_count != 0)
1444 if (!(mysql->server_status & SERVER_STATUS_AUTOCOMMIT))
1445 mysql->server_status|= SERVER_STATUS_IN_TRANS;
1447 if (!(fields_data= (*mysql->methods->read_rows)(mysql,(
MYSQL_FIELD*)0,7)))
1449 if (!(stmt->fields= unpack_fields(mysql, fields_data,&stmt->mem_root,
1451 mysql->server_capabilities)))
1454 stmt->field_count= field_count;
1455 stmt->param_count= (ulong) param_count;
1456 DBUG_PRINT(
"exit",(
"field_count: %u param_count: %u warning_count: %u",
1457 field_count, param_count, (uint) mysql->warning_count));
1495 mysql_stmt_init(
MYSQL *mysql)
1498 DBUG_ENTER(
"mysql_stmt_init");
1502 MYF(MY_WME | MY_ZEROFILL))) ||
1505 MYF(MY_WME | MY_ZEROFILL))))
1507 set_mysql_error(mysql, CR_OUT_OF_MEMORY, unknown_sqlstate);
1512 init_alloc_root(&stmt->mem_root, 2048, 2048);
1513 init_alloc_root(&stmt->result.alloc, 4096, 4096);
1514 stmt->result.alloc.min_malloc=
sizeof(
MYSQL_ROWS);
1515 mysql->stmts= list_add(mysql->stmts, &stmt->list);
1516 stmt->list.data= stmt;
1517 stmt->state= MYSQL_STMT_INIT_DONE;
1519 stmt->read_row_func= stmt_read_row_no_result_set;
1520 stmt->prefetch_rows= DEFAULT_PREFETCH_ROWS;
1521 strmov(stmt->sqlstate, not_error_sqlstate);
1524 init_alloc_root(&stmt->extension->fields_mem_root, 2048, 0);
1565 mysql_stmt_prepare(
MYSQL_STMT *stmt,
const char *query, ulong length)
1567 MYSQL *mysql= stmt->mysql;
1568 DBUG_ENTER(
"mysql_stmt_prepare");
1573 set_stmt_error(stmt, CR_SERVER_LOST, unknown_sqlstate, NULL);
1581 stmt->last_errno= 0;
1582 stmt->last_error[0]=
'\0';
1584 if ((
int) stmt->state > (
int) MYSQL_STMT_INIT_DONE)
1587 uchar buff[MYSQL_STMT_HEADER];
1589 if (reset_stmt_handle(stmt, RESET_LONG_DATA | RESET_STORE_RESULT))
1595 stmt->bind_param_done= stmt->bind_result_done= FALSE;
1596 stmt->param_count= stmt->field_count= 0;
1597 free_root(&stmt->mem_root, MYF(MY_KEEP_PREALLOC));
1598 free_root(&stmt->extension->fields_mem_root, MYF(0));
1600 int4store(buff, stmt->stmt_id);
1609 stmt->state= MYSQL_STMT_INIT_DONE;
1610 if (stmt_command(mysql, COM_STMT_CLOSE, buff, 4, stmt))
1612 set_stmt_errmsg(stmt, &mysql->net);
1617 if (stmt_command(mysql, COM_STMT_PREPARE, (
const uchar*) query, length, stmt))
1619 set_stmt_errmsg(stmt, &mysql->net);
1623 if ((*mysql->methods->read_prepare_result)(mysql, stmt))
1625 set_stmt_errmsg(stmt, &mysql->net);
1635 if (!(stmt->params= (
MYSQL_BIND *) alloc_root(&stmt->mem_root,
1637 (stmt->param_count +
1638 stmt->field_count))))
1640 set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate, NULL);
1643 stmt->bind= stmt->params + stmt->param_count;
1644 stmt->state= MYSQL_STMT_PREPARE_DONE;
1645 DBUG_PRINT(
"info", (
"Parameter count: %u", stmt->param_count));
1656 static void alloc_stmt_fields(
MYSQL_STMT *stmt)
1659 MEM_ROOT *fields_mem_root= &stmt->extension->fields_mem_root;
1660 MYSQL *mysql= stmt->mysql;
1662 DBUG_ASSERT(stmt->field_count);
1664 free_root(fields_mem_root, MYF(0));
1670 if (!(stmt->fields= (
MYSQL_FIELD *) alloc_root(fields_mem_root,
1672 stmt->field_count)) ||
1673 !(stmt->bind= (
MYSQL_BIND *) alloc_root(fields_mem_root,
1675 stmt->field_count)))
1677 set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate, NULL);
1681 for (fields= mysql->fields, end= fields+stmt->field_count,
1682 field= stmt->fields;
1683 field && fields < end; fields++, field++)
1686 field->catalog= strmake_root(fields_mem_root,
1688 fields->catalog_length);
1689 field->db= strmake_root(fields_mem_root,
1692 field->table= strmake_root(fields_mem_root,
1694 fields->table_length);
1695 field->org_table= strmake_root(fields_mem_root,
1697 fields->org_table_length);
1698 field->name= strmake_root(fields_mem_root,
1700 fields->name_length);
1701 field->org_name= strmake_root(fields_mem_root,
1703 fields->org_name_length);
1706 field->def= strmake_root(fields_mem_root,
1708 fields->def_length);
1709 field->def_length= fields->def_length;
1714 field->def_length= 0;
1716 field->extension= 0;
1717 field->max_length= 0;
1730 static void update_stmt_fields(
MYSQL_STMT *stmt)
1733 MYSQL_FIELD *field_end= field + stmt->field_count;
1735 MYSQL_BIND *my_bind= stmt->bind_result_done ? stmt->bind : 0;
1737 if (stmt->field_count != stmt->mysql->field_count)
1750 set_stmt_error(stmt, CR_NEW_STMT_METADATA, unknown_sqlstate, NULL);
1754 for (; field < field_end; ++field, ++stmt_field)
1756 stmt_field->charsetnr= field->charsetnr;
1757 stmt_field->length = field->length;
1758 stmt_field->type = field->type;
1759 stmt_field->flags = field->flags;
1760 stmt_field->decimals = field->decimals;
1764 (void) setup_one_fetch_function(my_bind++, stmt_field);
1801 DBUG_ENTER(
"mysql_stmt_result_metadata");
1807 if (!stmt->field_count)
1810 if (!(result=(
MYSQL_RES*) my_malloc(
sizeof(*result),
1811 MYF(MY_WME | MY_ZEROFILL))))
1813 set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate, NULL);
1817 result->methods= stmt->mysql->methods;
1819 result->fields= stmt->fields;
1820 result->field_count= stmt->field_count;
1822 DBUG_RETURN(result);
1847 DBUG_ENTER(
"mysql_stmt_param_metadata");
1849 if (!stmt->param_count)
1862 static void store_param_type(
unsigned char **pos,
MYSQL_BIND *param)
1864 uint typecode= param->buffer_type | (param->is_unsigned ? 32768 : 0);
1865 int2store(*pos, typecode);
1886 static void store_param_tinyint(
NET *net,
MYSQL_BIND *param)
1888 *(net->write_pos++)= *(uchar *) param->buffer;
1893 short value= *(
short*) param->buffer;
1894 int2store(net->write_pos,value);
1900 int32 value= *(int32*) param->buffer;
1901 int4store(net->write_pos,value);
1907 longlong value= *(longlong*) param->buffer;
1908 int8store(net->write_pos,value);
1914 float value= *(
float*) param->buffer;
1915 float4store(net->write_pos, value);
1921 double value= *(
double*) param->buffer;
1922 float8store(net->write_pos, value);
1929 char buff[MAX_TIME_REP_LENGTH], *pos;
1933 pos[0]= tm->neg ? 1: 0;
1934 int4store(pos+1, tm->day);
1935 pos[5]= (uchar) tm->hour;
1936 pos[6]= (uchar) tm->minute;
1937 pos[7]= (uchar) tm->second;
1941 else if (tm->hour || tm->minute || tm->second || tm->day)
1945 buff[0]= (char) length++;
1946 memcpy((
char *)net->write_pos, buff, length);
1947 net->write_pos+= length;
1952 char buff[MAX_DATETIME_REP_LENGTH], *pos;
1957 int2store(pos, tm->year);
1958 pos[2]= (uchar) tm->month;
1959 pos[3]= (uchar) tm->day;
1960 pos[4]= (uchar) tm->hour;
1961 pos[5]= (uchar) tm->minute;
1962 pos[6]= (uchar) tm->second;
1966 else if (tm->hour || tm->minute || tm->second)
1968 else if (tm->year || tm->month || tm->day)
1972 buff[0]= (char) length++;
1973 memcpy((
char *)net->write_pos, buff, length);
1974 net->write_pos+= length;
1981 net_store_datetime(net, &tm);
1984 static void store_param_datetime(
NET *net,
MYSQL_BIND *param)
1987 net_store_datetime(net, tm);
1993 ulong length= *param->length;
1994 uchar *to= net_store_length(net->write_pos, length);
1995 memcpy(to, param->buffer, length);
1996 net->write_pos= to+length;
2016 uint pos= param->param_number;
2017 net->buff[pos/8]|= (uchar) (1 << (pos & 7));
2029 NET *net= &stmt->mysql->net;
2030 DBUG_ENTER(
"store_param");
2031 DBUG_PRINT(
"enter",(
"type: %d buffer: 0x%lx length: %lu is_null: %d",
2033 (
long) (param->buffer ? param->buffer : NullS),
2034 *param->length, *param->is_null));
2036 if (*param->is_null)
2037 store_param_null(net, param);
2044 if ((my_realloc_str(net, *param->length)))
2046 set_stmt_errmsg(stmt, net);
2049 (*param->store_param_func)(net, param);
2060 static my_bool execute(
MYSQL_STMT *stmt,
char *packet, ulong length)
2062 MYSQL *mysql= stmt->mysql;
2063 NET *net= &mysql->net;
2067 DBUG_ENTER(
"execute");
2068 DBUG_DUMP(
"packet", (uchar *) packet, length);
2070 int4store(buff, stmt->stmt_id);
2071 buff[4]= (char) stmt->flags;
2072 int4store(buff+5, 1);
2074 res=
test(cli_advanced_command(mysql, COM_STMT_EXECUTE, buff,
sizeof(buff),
2075 (uchar*) packet, length, 1, stmt) ||
2076 (*mysql->methods->read_query_result)(mysql));
2077 stmt->affected_rows= mysql->affected_rows;
2078 stmt->server_status= mysql->server_status;
2079 stmt->insert_id= mysql->insert_id;
2087 set_stmt_errmsg(stmt, net);
2090 else if (mysql->status == MYSQL_STATUS_GET_RESULT)
2091 stmt->mysql->status= MYSQL_STATUS_STATEMENT_GET_RESULT;
2098 DBUG_ENTER(
"cli_stmt_execute");
2100 if (stmt->param_count)
2102 MYSQL *mysql= stmt->mysql;
2103 NET *net= &mysql->net;
2110 if (!stmt->bind_param_done)
2112 set_stmt_error(stmt, CR_PARAMS_NOT_BOUND, unknown_sqlstate, NULL);
2115 if (mysql->status != MYSQL_STATUS_READY ||
2116 mysql->server_status & SERVER_MORE_RESULTS_EXISTS)
2118 set_stmt_error(stmt, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate, NULL);
2126 set_stmt_errmsg(stmt, net);
2131 null_count= (stmt->param_count+7) /8;
2132 if (my_realloc_str(net, null_count + 1))
2134 set_stmt_errmsg(stmt, net);
2137 memset(net->write_pos, 0, null_count);
2138 net->write_pos+= null_count;
2139 param_end= stmt->params + stmt->param_count;
2142 *(net->write_pos)++= (uchar) stmt->send_types_to_server;
2143 if (stmt->send_types_to_server)
2145 if (my_realloc_str(net, 2 * stmt->param_count))
2147 set_stmt_errmsg(stmt, net);
2154 for (param= stmt->params; param < param_end ; param++)
2155 store_param_type(&net->write_pos, param);
2158 for (param= stmt->params; param < param_end; param++)
2161 if (param->long_data_used)
2162 param->long_data_used= 0;
2163 else if (store_param(stmt, param))
2166 length= (ulong) (net->write_pos - net->buff);
2168 if (!(param_data= my_memdup(net->buff, length, MYF(0))))
2170 set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate, NULL);
2173 result= execute(stmt, param_data, length);
2174 stmt->send_types_to_server=0;
2175 my_free(param_data);
2176 DBUG_RETURN(result);
2178 DBUG_RETURN((
int) execute(stmt,0,0));
2193 static int stmt_read_row_buffered(
MYSQL_STMT *stmt,
unsigned char **row)
2195 if (stmt->data_cursor)
2197 *row= (uchar *) stmt->data_cursor->data;
2198 stmt->data_cursor= stmt->data_cursor->next;
2202 return MYSQL_NO_DATA;
2224 static int stmt_read_row_unbuffered(
MYSQL_STMT *stmt,
unsigned char **row)
2227 MYSQL *mysql= stmt->mysql;
2234 set_stmt_error(stmt, CR_SERVER_LOST, unknown_sqlstate, NULL);
2237 if (mysql->status != MYSQL_STATUS_STATEMENT_GET_RESULT)
2239 set_stmt_error(stmt, stmt->unbuffered_fetch_cancelled ?
2240 CR_FETCH_CANCELED : CR_COMMANDS_OUT_OF_SYNC,
2241 unknown_sqlstate, NULL);
2244 if ((*mysql->methods->unbuffered_fetch)(mysql, (
char**) row))
2246 set_stmt_errmsg(stmt, &mysql->net);
2253 mysql->status= MYSQL_STATUS_READY;
2258 mysql->status= MYSQL_STATUS_READY;
2264 if (mysql->unbuffered_fetch_owner == &stmt->unbuffered_fetch_cancelled)
2265 mysql->unbuffered_fetch_owner= 0;
2283 stmt_read_row_from_cursor(
MYSQL_STMT *stmt,
unsigned char **row)
2285 if (stmt->data_cursor)
2286 return stmt_read_row_buffered(stmt, row);
2287 if (stmt->server_status & SERVER_STATUS_LAST_ROW_SENT)
2288 stmt->server_status &= ~SERVER_STATUS_LAST_ROW_SENT;
2291 MYSQL *mysql= stmt->mysql;
2292 NET *net= &mysql->net;
2297 free_root(&result->alloc, MYF(MY_KEEP_PREALLOC));
2301 int4store(buff, stmt->stmt_id);
2302 int4store(buff + 4, stmt->prefetch_rows);
2303 if ((*mysql->methods->advanced_command)(mysql, COM_STMT_FETCH,
2304 buff,
sizeof(buff), (uchar*) 0, 0,
2312 set_stmt_errmsg(stmt, net);
2315 if ((*mysql->methods->read_rows_from_cursor)(stmt))
2317 stmt->server_status= mysql->server_status;
2319 stmt->data_cursor= result->data;
2320 return stmt_read_row_buffered(stmt, row);
2323 return MYSQL_NO_DATA;
2333 stmt_read_row_no_data(
MYSQL_STMT *stmt __attribute__((unused)),
2334 unsigned char **row __attribute__((unused)))
2336 return MYSQL_NO_DATA;
2340 stmt_read_row_no_result_set(
MYSQL_STMT *stmt __attribute__((unused)),
2341 unsigned char **row __attribute__((unused)))
2343 set_stmt_error(stmt, CR_NO_RESULT_SET, unknown_sqlstate, NULL);
2363 my_bool STDCALL mysql_stmt_attr_set(
MYSQL_STMT *stmt,
2364 enum enum_stmt_attr_type attr_type,
2367 switch (attr_type) {
2368 case STMT_ATTR_UPDATE_MAX_LENGTH:
2369 stmt->update_max_length= value ? *(
const my_bool*) value : 0;
2371 case STMT_ATTR_CURSOR_TYPE:
2374 cursor_type= value ? *(ulong*) value : 0UL;
2375 if (cursor_type > (ulong) CURSOR_TYPE_READ_ONLY)
2376 goto err_not_implemented;
2377 stmt->flags= cursor_type;
2380 case STMT_ATTR_PREFETCH_ROWS:
2382 ulong prefetch_rows= value ? *(ulong*) value : DEFAULT_PREFETCH_ROWS;
2385 stmt->prefetch_rows= prefetch_rows;
2389 goto err_not_implemented;
2392 err_not_implemented:
2393 set_stmt_error(stmt, CR_NOT_IMPLEMENTED, unknown_sqlstate, NULL);
2398 my_bool STDCALL mysql_stmt_attr_get(
MYSQL_STMT *stmt,
2399 enum enum_stmt_attr_type attr_type,
2402 switch (attr_type) {
2403 case STMT_ATTR_UPDATE_MAX_LENGTH:
2404 *(my_bool*) value= stmt->update_max_length;
2406 case STMT_ATTR_CURSOR_TYPE:
2407 *(ulong*) value= stmt->flags;
2409 case STMT_ATTR_PREFETCH_ROWS:
2410 *(ulong*) value= stmt->prefetch_rows;
2430 static void reinit_result_set_metadata(
MYSQL_STMT *stmt)
2433 if (stmt->field_count == 0)
2441 stmt->field_count= stmt->mysql->field_count;
2443 alloc_stmt_fields(stmt);
2461 update_stmt_fields(stmt);
2466 static void prepare_to_fetch_result(
MYSQL_STMT *stmt)
2468 if (stmt->server_status & SERVER_STATUS_CURSOR_EXISTS)
2470 stmt->mysql->status= MYSQL_STATUS_READY;
2471 stmt->read_row_func= stmt_read_row_from_cursor;
2473 else if (stmt->flags & CURSOR_TYPE_READ_ONLY)
2482 mysql_stmt_store_result(stmt);
2486 stmt->mysql->unbuffered_fetch_owner= &stmt->unbuffered_fetch_cancelled;
2487 stmt->unbuffered_fetch_cancelled= FALSE;
2488 stmt->read_row_func= stmt_read_row_unbuffered;
2537 int STDCALL mysql_stmt_execute(
MYSQL_STMT *stmt)
2539 MYSQL *mysql= stmt->mysql;
2540 DBUG_ENTER(
"mysql_stmt_execute");
2548 if (reset_stmt_handle(stmt, RESET_STORE_RESULT | RESET_CLEAR_ERROR))
2554 if (mysql->methods->stmt_execute(stmt))
2556 stmt->state= MYSQL_STMT_EXECUTE_DONE;
2557 if (mysql->field_count)
2559 reinit_result_set_metadata(stmt);
2560 prepare_to_fetch_result(stmt);
2562 DBUG_RETURN(
test(stmt->last_errno));
2570 ulong STDCALL mysql_stmt_param_count(
MYSQL_STMT * stmt)
2572 DBUG_ENTER(
"mysql_stmt_param_count");
2573 DBUG_RETURN(stmt->param_count);
2580 my_ulonglong STDCALL mysql_stmt_affected_rows(
MYSQL_STMT *stmt)
2582 return stmt->affected_rows;
2591 unsigned int STDCALL mysql_stmt_field_count(
MYSQL_STMT *stmt)
2593 return stmt->field_count;
2612 my_ulonglong STDCALL mysql_stmt_insert_id(
MYSQL_STMT *stmt)
2614 return stmt->insert_id;
2618 static my_bool int_is_null_true= 1;
2619 static my_bool int_is_null_false= 0;
2795 DBUG_ENTER(
"mysql_stmt_bind_param");
2797 if (!stmt->param_count)
2799 if ((
int) stmt->state < (
int) MYSQL_STMT_PREPARE_DONE)
2801 set_stmt_error(stmt, CR_NO_PREPARE_STMT, unknown_sqlstate, NULL);
2808 memcpy((
char*) stmt->params, (
char*) my_bind,
2811 for (param= stmt->params, end= param+stmt->param_count;
2815 param->param_number= count++;
2816 param->long_data_used= 0;
2819 if (!param->is_null)
2820 param->is_null= &int_is_null_false;
2823 switch (param->buffer_type) {
2824 case MYSQL_TYPE_NULL:
2825 param->is_null= &int_is_null_true;
2827 case MYSQL_TYPE_TINY:
2829 param->length= ¶m->buffer_length;
2830 param->buffer_length= 1;
2831 param->store_param_func= store_param_tinyint;
2833 case MYSQL_TYPE_SHORT:
2834 param->length= ¶m->buffer_length;
2835 param->buffer_length= 2;
2836 param->store_param_func= store_param_short;
2838 case MYSQL_TYPE_LONG:
2839 param->length= ¶m->buffer_length;
2840 param->buffer_length= 4;
2841 param->store_param_func= store_param_int32;
2843 case MYSQL_TYPE_LONGLONG:
2844 param->length= ¶m->buffer_length;
2845 param->buffer_length= 8;
2846 param->store_param_func= store_param_int64;
2848 case MYSQL_TYPE_FLOAT:
2849 param->length= ¶m->buffer_length;
2850 param->buffer_length= 4;
2851 param->store_param_func= store_param_float;
2853 case MYSQL_TYPE_DOUBLE:
2854 param->length= ¶m->buffer_length;
2855 param->buffer_length= 8;
2856 param->store_param_func= store_param_double;
2858 case MYSQL_TYPE_TIME:
2859 param->store_param_func= store_param_time;
2860 param->buffer_length= MAX_TIME_REP_LENGTH;
2862 case MYSQL_TYPE_DATE:
2863 param->store_param_func= store_param_date;
2864 param->buffer_length= MAX_DATE_REP_LENGTH;
2866 case MYSQL_TYPE_DATETIME:
2867 case MYSQL_TYPE_TIMESTAMP:
2868 param->store_param_func= store_param_datetime;
2869 param->buffer_length= MAX_DATETIME_REP_LENGTH;
2871 case MYSQL_TYPE_TINY_BLOB:
2872 case MYSQL_TYPE_MEDIUM_BLOB:
2873 case MYSQL_TYPE_LONG_BLOB:
2874 case MYSQL_TYPE_BLOB:
2875 case MYSQL_TYPE_VARCHAR:
2876 case MYSQL_TYPE_VAR_STRING:
2877 case MYSQL_TYPE_STRING:
2878 case MYSQL_TYPE_DECIMAL:
2879 case MYSQL_TYPE_NEWDECIMAL:
2880 param->store_param_func= store_param_str;
2887 strmov(stmt->sqlstate, unknown_sqlstate);
2888 sprintf(stmt->last_error,
2889 ER(stmt->last_errno= CR_UNSUPPORTED_PARAM_TYPE),
2890 param->buffer_type, count);
2898 param->length= ¶m->buffer_length;
2901 stmt->send_types_to_server= TRUE;
2902 stmt->bind_param_done= TRUE;
2951 mysql_stmt_send_long_data(
MYSQL_STMT *stmt, uint param_number,
2952 const char *data, ulong length)
2955 DBUG_ENTER(
"mysql_stmt_send_long_data");
2956 DBUG_ASSERT(stmt != 0);
2957 DBUG_PRINT(
"enter",(
"param no: %d data: 0x%lx, length : %ld",
2958 param_number, (
long) data, length));
2964 if (param_number >= stmt->param_count)
2966 set_stmt_error(stmt, CR_INVALID_PARAMETER_NO, unknown_sqlstate, NULL);
2970 param= stmt->params+param_number;
2971 if (!IS_LONGDATA(param->buffer_type))
2974 strmov(stmt->sqlstate, unknown_sqlstate);
2975 sprintf(stmt->last_error, ER(stmt->last_errno= CR_INVALID_BUFFER_USE),
2976 param->param_number);
2984 if (length || param->long_data_used == 0)
2986 MYSQL *mysql= stmt->mysql;
2988 uchar buff[MYSQL_LONG_DATA_HEADER];
2990 int4store(buff, stmt->stmt_id);
2991 int2store(buff + 4, param_number);
2992 param->long_data_used= 1;
2998 if ((*mysql->methods->advanced_command)(mysql, COM_STMT_SEND_LONG_DATA,
2999 buff,
sizeof(buff), (uchar*) data,
3007 set_stmt_errmsg(stmt, &mysql->net);
3035 static void read_binary_time(
MYSQL_TIME *tm, uchar **pos)
3038 uint length= net_field_length(pos);
3045 tm->day= (ulong) sint4korr(to+1);
3046 tm->hour= (uint) to[5];
3047 tm->minute= (uint) to[6];
3048 tm->second= (uint) to[7];
3049 tm->
second_part= (length > 8) ? (ulong) sint4korr(to+8) : 0;
3050 tm->year= tm->month= 0;
3054 tm->hour+= tm->day*24;
3057 tm->time_type= MYSQL_TIMESTAMP_TIME;
3062 set_zero_time(tm, MYSQL_TIMESTAMP_TIME);
3065 static void read_binary_datetime(
MYSQL_TIME *tm, uchar **pos)
3067 uint length= net_field_length(pos);
3074 tm->year= (uint) sint2korr(to);
3075 tm->month= (uint) to[2];
3076 tm->day= (uint) to[3];
3080 tm->hour= (uint) to[4];
3081 tm->minute= (uint) to[5];
3082 tm->second= (uint) to[6];
3085 tm->hour= tm->minute= tm->second= 0;
3086 tm->
second_part= (length > 7) ? (ulong) sint4korr(to+7) : 0;
3087 tm->time_type= MYSQL_TIMESTAMP_DATETIME;
3092 set_zero_time(tm, MYSQL_TIMESTAMP_DATETIME);
3095 static void read_binary_date(
MYSQL_TIME *tm, uchar **pos)
3097 uint length= net_field_length(pos);
3102 tm->year = (uint) sint2korr(to);
3103 tm->month= (uint) to[2];
3104 tm->day= (uint) to[3];
3106 tm->hour= tm->minute= tm->second= 0;
3109 tm->time_type= MYSQL_TIMESTAMP_DATE;
3114 set_zero_time(tm, MYSQL_TIMESTAMP_DATE);
3128 static void fetch_string_with_conversion(
MYSQL_BIND *param,
char *value,
3131 char *buffer= (
char *)param->buffer;
3132 char *endptr= value + length;
3138 switch (param->buffer_type) {
3139 case MYSQL_TYPE_NULL:
3141 case MYSQL_TYPE_TINY:
3144 longlong data= my_strtoll10(value, &endptr, &err);
3145 *param->error= (IS_TRUNCATED(data, param->is_unsigned,
3146 INT_MIN8, INT_MAX8, UINT_MAX8) || err > 0);
3147 *buffer= (uchar) data;
3150 case MYSQL_TYPE_SHORT:
3153 longlong data= my_strtoll10(value, &endptr, &err);
3154 *param->error= (IS_TRUNCATED(data, param->is_unsigned,
3155 INT_MIN16, INT_MAX16, UINT_MAX16) || err > 0);
3156 shortstore(buffer, (
short) data);
3159 case MYSQL_TYPE_LONG:
3162 longlong data= my_strtoll10(value, &endptr, &err);
3163 *param->error= (IS_TRUNCATED(data, param->is_unsigned,
3164 INT_MIN32, INT_MAX32, UINT_MAX32) || err > 0);
3165 longstore(buffer, (int32) data);
3168 case MYSQL_TYPE_LONGLONG:
3171 longlong data= my_strtoll10(value, &endptr, &err);
3172 *param->error= param->is_unsigned ? err != 0 :
3173 (err > 0 || (err == 0 && data < 0));
3174 longlongstore(buffer, data);
3177 case MYSQL_TYPE_FLOAT:
3180 double data= my_strntod(&my_charset_latin1, value, length, &endptr, &err);
3181 float fdata= (float) data;
3182 *param->error= (fdata != data) |
test(err);
3183 floatstore(buffer, fdata);
3186 case MYSQL_TYPE_DOUBLE:
3189 double data= my_strntod(&my_charset_latin1, value, length, &endptr, &err);
3190 *param->error=
test(err);
3191 doublestore(buffer, data);
3194 case MYSQL_TYPE_TIME:
3198 str_to_time(value, length, tm, &status);
3199 *param->error=
test(status.warnings);
3202 case MYSQL_TYPE_DATE:
3203 case MYSQL_TYPE_DATETIME:
3204 case MYSQL_TYPE_TIMESTAMP:
3208 (void) str_to_datetime(value, length, tm, TIME_FUZZY_DATE, &status);
3209 *param->error=
test(status.warnings) &&
3210 (param->buffer_type == MYSQL_TYPE_DATE &&
3211 tm->time_type != MYSQL_TIMESTAMP_DATE);
3214 case MYSQL_TYPE_TINY_BLOB:
3215 case MYSQL_TYPE_MEDIUM_BLOB:
3216 case MYSQL_TYPE_LONG_BLOB:
3217 case MYSQL_TYPE_BLOB:
3218 case MYSQL_TYPE_DECIMAL:
3219 case MYSQL_TYPE_NEWDECIMAL:
3226 char *start= value + param->offset;
3227 char *end= value + length;
3231 copy_length= end - start;
3233 if (param->buffer_length)
3234 memcpy(buffer, start, MY_MIN(copy_length, param->buffer_length));
3238 if (copy_length < param->buffer_length)
3239 buffer[copy_length]=
'\0';
3240 *param->error= copy_length > param->buffer_length;
3245 *param->length= length;
3263 longlong value, my_bool is_unsigned)
3265 char *buffer= (
char *)param->buffer;
3267 switch (param->buffer_type) {
3268 case MYSQL_TYPE_NULL:
3270 case MYSQL_TYPE_TINY:
3271 *param->error= IS_TRUNCATED(value, param->is_unsigned,
3272 INT_MIN8, INT_MAX8, UINT_MAX8);
3273 *(uchar *)param->buffer= (uchar) value;
3275 case MYSQL_TYPE_SHORT:
3276 *param->error= IS_TRUNCATED(value, param->is_unsigned,
3277 INT_MIN16, INT_MAX16, UINT_MAX16);
3278 shortstore(buffer, (
short) value);
3280 case MYSQL_TYPE_LONG:
3281 *param->error= IS_TRUNCATED(value, param->is_unsigned,
3282 INT_MIN32, INT_MAX32, UINT_MAX32);
3283 longstore(buffer, (int32) value);
3285 case MYSQL_TYPE_LONGLONG:
3286 longlongstore(buffer, value);
3287 *param->error= param->is_unsigned != is_unsigned && value < 0;
3289 case MYSQL_TYPE_FLOAT:
3296 volatile float data;
3299 data= (float) ulonglong2double(value);
3300 *param->error= ((ulonglong) value) != ((ulonglong) data);
3305 *param->error= value != ((longlong) data);
3307 floatstore(buffer, data);
3310 case MYSQL_TYPE_DOUBLE:
3312 volatile double data;
3315 data= ulonglong2double(value);
3316 *param->error= ((ulonglong) value) != ((ulonglong) data);
3320 data= (double)value;
3321 *param->error= value != ((longlong) data);
3323 doublestore(buffer, data);
3326 case MYSQL_TYPE_TIME:
3327 case MYSQL_TYPE_DATE:
3328 case MYSQL_TYPE_TIMESTAMP:
3329 case MYSQL_TYPE_DATETIME:
3332 value= number_to_datetime(value, (
MYSQL_TIME *) buffer, TIME_FUZZY_DATE,
3334 *param->error=
test(error);
3340 uchar *end= (uchar*) longlong10_to_str(value, (
char*) buff,
3341 is_unsigned ? 10: -10);
3343 uint length= (uint) (end-buff);
3345 if (field->flags & ZEROFILL_FLAG && length < field->length &&
3348 bmove_upp(buff+field->length,buff+length, length);
3349 memset(buff,
'0', field->length - length);
3350 length= field->length;
3352 fetch_string_with_conversion(param, (
char*) buff, length);
3372 double value, my_gcvt_arg_type
type)
3374 char *buffer= (
char *)param->buffer;
3375 double val64 = (value < 0 ? -floor(-value) : floor(value));
3377 switch (param->buffer_type) {
3378 case MYSQL_TYPE_NULL:
3380 case MYSQL_TYPE_TINY:
3387 if (param->is_unsigned)
3388 *buffer= (uint8) value;
3390 *buffer= (int8) value;
3391 *param->error= val64 != (param->is_unsigned ? (double)((uint8) *buffer) :
3392 (
double)((int8) *buffer));
3394 case MYSQL_TYPE_SHORT:
3395 if (param->is_unsigned)
3397 ushort data= (ushort) value;
3398 shortstore(buffer, data);
3402 short data= (short) value;
3403 shortstore(buffer, data);
3405 *param->error= val64 != (param->is_unsigned ? (double) (*(ushort*) buffer):
3406 (
double) (*(
short*) buffer));
3408 case MYSQL_TYPE_LONG:
3409 if (param->is_unsigned)
3411 uint32 data= (uint32) value;
3412 longstore(buffer, data);
3416 int32 data= (int32) value;
3417 longstore(buffer, data);
3419 *param->error= val64 != (param->is_unsigned ? (double) (*(uint32*) buffer):
3420 (
double) (*(int32*) buffer));
3422 case MYSQL_TYPE_LONGLONG:
3423 if (param->is_unsigned)
3425 ulonglong data= (ulonglong) value;
3426 longlongstore(buffer, data);
3430 longlong data= (longlong) value;
3431 longlongstore(buffer, data);
3433 *param->error= val64 != (param->is_unsigned ?
3434 ulonglong2double(*(ulonglong*) buffer) :
3435 (double) (*(longlong*) buffer));
3437 case MYSQL_TYPE_FLOAT:
3439 float data= (float) value;
3440 floatstore(buffer, data);
3441 *param->error= (*(
float*) buffer) != value;
3444 case MYSQL_TYPE_DOUBLE:
3446 doublestore(buffer, value);
3456 char buff[FLOATING_POINT_BUFFER];
3458 if (field->decimals >= NOT_FIXED_DEC)
3459 len= my_gcvt(value, type,
3460 (
int) MY_MIN(
sizeof(buff)-1, param->buffer_length),
3463 len= my_fcvt(value, (
int) field->decimals, buff, NULL);
3465 if (field->flags & ZEROFILL_FLAG && len < field->length &&
3466 field->length < MAX_DOUBLE_STRING_REP_LENGTH - 1)
3468 bmove_upp((uchar*) buff + field->length, (uchar*) buff + len,
3470 memset(buff,
'0', field->length - len);
3473 fetch_string_with_conversion(param, buff, len);
3489 static void fetch_datetime_with_conversion(
MYSQL_BIND *param,
3493 switch (param->buffer_type) {
3494 case MYSQL_TYPE_NULL:
3496 case MYSQL_TYPE_DATE:
3498 *param->error= my_time->time_type != MYSQL_TIMESTAMP_DATE;
3500 case MYSQL_TYPE_TIME:
3502 *param->error= my_time->time_type != MYSQL_TIMESTAMP_TIME;
3504 case MYSQL_TYPE_DATETIME:
3505 case MYSQL_TYPE_TIMESTAMP:
3509 case MYSQL_TYPE_YEAR:
3510 shortstore(param->buffer, my_time->year);
3513 case MYSQL_TYPE_FLOAT:
3514 case MYSQL_TYPE_DOUBLE:
3516 ulonglong value= TIME_to_ulonglong(my_time);
3517 fetch_float_with_conversion(param, field,
3518 ulonglong2double(value), MY_GCVT_ARG_DOUBLE);
3521 case MYSQL_TYPE_TINY:
3522 case MYSQL_TYPE_SHORT:
3523 case MYSQL_TYPE_INT24:
3524 case MYSQL_TYPE_LONG:
3525 case MYSQL_TYPE_LONGLONG:
3527 longlong value= (longlong) TIME_to_ulonglong(my_time);
3528 fetch_long_with_conversion(param, field, value, TRUE);
3537 char buff[MAX_DATE_STRING_REP_LENGTH];
3538 uint length= my_TIME_to_str(my_time, buff, field->decimals);
3540 fetch_string_with_conversion(param, (
char *)buff, length);
3566 enum enum_field_types field_type= field->type;
3567 uint field_is_unsigned= field->flags & UNSIGNED_FLAG;
3569 switch (field_type) {
3570 case MYSQL_TYPE_TINY:
3574 longlong data= field_is_unsigned ? (longlong) value :
3575 (longlong) (
signed char) value;
3576 fetch_long_with_conversion(param, field, data, 0);
3580 case MYSQL_TYPE_SHORT:
3581 case MYSQL_TYPE_YEAR:
3583 short value= sint2korr(*row);
3584 longlong data= field_is_unsigned ? (longlong) (
unsigned short) value :
3586 fetch_long_with_conversion(param, field, data, 0);
3590 case MYSQL_TYPE_INT24:
3591 case MYSQL_TYPE_LONG:
3593 int32 value= sint4korr(*row);
3594 longlong data= field_is_unsigned ? (longlong) (uint32) value :
3596 fetch_long_with_conversion(param, field, data, 0);
3600 case MYSQL_TYPE_LONGLONG:
3602 longlong value= (longlong)sint8korr(*row);
3603 fetch_long_with_conversion(param, field, value,
3604 field->flags & UNSIGNED_FLAG);
3608 case MYSQL_TYPE_FLOAT:
3611 float4get(value,*row);
3612 fetch_float_with_conversion(param, field, value, MY_GCVT_ARG_FLOAT);
3616 case MYSQL_TYPE_DOUBLE:
3619 float8get(value,*row);
3620 fetch_float_with_conversion(param, field, value, MY_GCVT_ARG_DOUBLE);
3624 case MYSQL_TYPE_DATE:
3628 read_binary_date(&tm, row);
3629 fetch_datetime_with_conversion(param, field, &tm);
3632 case MYSQL_TYPE_TIME:
3636 read_binary_time(&tm, row);
3637 fetch_datetime_with_conversion(param, field, &tm);
3640 case MYSQL_TYPE_DATETIME:
3641 case MYSQL_TYPE_TIMESTAMP:
3645 read_binary_datetime(&tm, row);
3646 fetch_datetime_with_conversion(param, field, &tm);
3651 ulong length= net_field_length(row);
3652 fetch_string_with_conversion(param, (
char*) *row, length);
3682 my_bool field_is_unsigned=
test(field->flags & UNSIGNED_FLAG);
3684 *(uchar *)param->buffer= data;
3685 *param->error= param->is_unsigned != field_is_unsigned && data > INT_MAX8;
3692 my_bool field_is_unsigned=
test(field->flags & UNSIGNED_FLAG);
3693 ushort data= (ushort) sint2korr(*row);
3694 shortstore(param->buffer, data);
3695 *param->error= param->is_unsigned != field_is_unsigned && data > INT_MAX16;
3699 static void fetch_result_int32(
MYSQL_BIND *param,
3703 my_bool field_is_unsigned=
test(field->flags & UNSIGNED_FLAG);
3704 uint32 data= (uint32) sint4korr(*row);
3705 longstore(param->buffer, data);
3706 *param->error= param->is_unsigned != field_is_unsigned && data > INT_MAX32;
3710 static void fetch_result_int64(
MYSQL_BIND *param,
3714 my_bool field_is_unsigned=
test(field->flags & UNSIGNED_FLAG);
3715 ulonglong data= (ulonglong) sint8korr(*row);
3716 *param->error= param->is_unsigned != field_is_unsigned && data > LONGLONG_MAX;
3717 longlongstore(param->buffer, data);
3721 static void fetch_result_float(
MYSQL_BIND *param,
3726 float4get(value,*row);
3727 floatstore(param->buffer, value);
3731 static void fetch_result_double(
MYSQL_BIND *param,
3736 float8get(value,*row);
3737 doublestore(param->buffer, value);
3741 static void fetch_result_time(
MYSQL_BIND *param,
3746 read_binary_time(tm, row);
3749 static void fetch_result_date(
MYSQL_BIND *param,
3754 read_binary_date(tm, row);
3757 static void fetch_result_datetime(
MYSQL_BIND *param,
3762 read_binary_datetime(tm, row);
3765 static void fetch_result_bin(
MYSQL_BIND *param,
3769 ulong length= net_field_length(row);
3770 ulong copy_length= MY_MIN(length, param->buffer_length);
3771 memcpy(param->buffer, (
char *)*row, copy_length);
3772 *param->length= length;
3773 *param->error= copy_length < length;
3777 static void fetch_result_str(
MYSQL_BIND *param,
3781 ulong length= net_field_length(row);
3782 ulong copy_length= MY_MIN(length, param->buffer_length);
3783 memcpy(param->buffer, (
char *)*row, copy_length);
3785 if (copy_length != param->buffer_length)
3786 ((uchar *)param->buffer)[copy_length]=
'\0';
3787 *param->length= length;
3788 *param->error= copy_length < length;
3798 static void skip_result_fixed(
MYSQL_BIND *param,
3803 (*row)+= param->pack_length;
3807 static void skip_result_with_length(
MYSQL_BIND *param __attribute__((unused)),
3812 ulong length= net_field_length(row);
3817 static void skip_result_string(
MYSQL_BIND *param __attribute__((unused)),
3822 ulong length= net_field_length(row);
3824 if (field->max_length < length)
3825 field->max_length= length;
3843 static my_bool is_binary_compatible(
enum enum_field_types type1,
3844 enum enum_field_types type2)
3846 static const enum enum_field_types
3847 range1[]= { MYSQL_TYPE_SHORT, MYSQL_TYPE_YEAR, MYSQL_TYPE_NULL },
3848 range2[]= { MYSQL_TYPE_INT24, MYSQL_TYPE_LONG, MYSQL_TYPE_NULL },
3849 range3[]= { MYSQL_TYPE_DATETIME, MYSQL_TYPE_TIMESTAMP, MYSQL_TYPE_NULL },
3850 range4[]= { MYSQL_TYPE_ENUM, MYSQL_TYPE_SET, MYSQL_TYPE_TINY_BLOB,
3851 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_BLOB,
3852 MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_STRING, MYSQL_TYPE_GEOMETRY,
3853 MYSQL_TYPE_DECIMAL, MYSQL_TYPE_NULL };
3854 static const enum enum_field_types
3855 *range_list[]= { range1, range2, range3, range4 },
3856 **range_list_end= range_list +
sizeof(range_list)/
sizeof(*range_list);
3857 const enum enum_field_types **range, *
type;
3861 for (range= range_list; range != range_list_end; ++range)
3864 my_bool type1_found= FALSE, type2_found= FALSE;
3865 for (type= *range; *type != MYSQL_TYPE_NULL; type++)
3867 type1_found|= type1 == *
type;
3868 type2_found|= type2 == *
type;
3870 if (type1_found || type2_found)
3871 return type1_found && type2_found;
3904 DBUG_ENTER(
"setup_one_fetch_function");
3907 switch (param->buffer_type) {
3908 case MYSQL_TYPE_NULL:
3915 case MYSQL_TYPE_TINY:
3916 param->fetch_result= fetch_result_tinyint;
3919 case MYSQL_TYPE_SHORT:
3920 case MYSQL_TYPE_YEAR:
3921 param->fetch_result= fetch_result_short;
3924 case MYSQL_TYPE_INT24:
3925 case MYSQL_TYPE_LONG:
3926 param->fetch_result= fetch_result_int32;
3929 case MYSQL_TYPE_LONGLONG:
3930 param->fetch_result= fetch_result_int64;
3933 case MYSQL_TYPE_FLOAT:
3934 param->fetch_result= fetch_result_float;
3937 case MYSQL_TYPE_DOUBLE:
3938 param->fetch_result= fetch_result_double;
3941 case MYSQL_TYPE_TIME:
3942 param->fetch_result= fetch_result_time;
3945 case MYSQL_TYPE_DATE:
3946 param->fetch_result= fetch_result_date;
3949 case MYSQL_TYPE_DATETIME:
3950 case MYSQL_TYPE_TIMESTAMP:
3951 param->fetch_result= fetch_result_datetime;
3954 case MYSQL_TYPE_TINY_BLOB:
3955 case MYSQL_TYPE_MEDIUM_BLOB:
3956 case MYSQL_TYPE_LONG_BLOB:
3957 case MYSQL_TYPE_BLOB:
3958 case MYSQL_TYPE_BIT:
3959 DBUG_ASSERT(param->buffer_length != 0);
3960 param->fetch_result= fetch_result_bin;
3962 case MYSQL_TYPE_VAR_STRING:
3963 case MYSQL_TYPE_STRING:
3964 case MYSQL_TYPE_DECIMAL:
3965 case MYSQL_TYPE_NEWDECIMAL:
3966 case MYSQL_TYPE_NEWDATE:
3967 DBUG_ASSERT(param->buffer_length != 0);
3968 param->fetch_result= fetch_result_str;
3971 DBUG_PRINT(
"error", (
"Unknown param->buffer_type: %u",
3972 (uint) param->buffer_type));
3975 if (! is_binary_compatible(param->buffer_type, field->type))
3976 param->fetch_result= fetch_result_with_conversion;
3979 param->skip_result= skip_result_fixed;
3980 switch (field->type) {
3981 case MYSQL_TYPE_NULL:
3982 param->pack_length= 0;
3983 field->max_length= 0;
3985 case MYSQL_TYPE_TINY:
3986 param->pack_length= 1;
3987 field->max_length= 4;
3989 case MYSQL_TYPE_YEAR:
3990 case MYSQL_TYPE_SHORT:
3991 param->pack_length= 2;
3992 field->max_length= 6;
3994 case MYSQL_TYPE_INT24:
3995 field->max_length= 9;
3996 param->pack_length= 4;
3998 case MYSQL_TYPE_LONG:
3999 field->max_length= 11;
4000 param->pack_length= 4;
4002 case MYSQL_TYPE_LONGLONG:
4003 field->max_length= 21;
4004 param->pack_length= 8;
4006 case MYSQL_TYPE_FLOAT:
4007 param->pack_length= 4;
4008 field->max_length= MAX_DOUBLE_STRING_REP_LENGTH;
4010 case MYSQL_TYPE_DOUBLE:
4011 param->pack_length= 8;
4012 field->max_length= MAX_DOUBLE_STRING_REP_LENGTH;
4014 case MYSQL_TYPE_TIME:
4015 field->max_length= 17;
4016 param->skip_result= skip_result_with_length;
4018 case MYSQL_TYPE_DATE:
4019 field->max_length= 10;
4020 param->skip_result= skip_result_with_length;
4022 case MYSQL_TYPE_DATETIME:
4023 case MYSQL_TYPE_TIMESTAMP:
4024 param->skip_result= skip_result_with_length;
4025 field->max_length= MAX_DATE_STRING_REP_LENGTH;
4027 case MYSQL_TYPE_DECIMAL:
4028 case MYSQL_TYPE_NEWDECIMAL:
4029 case MYSQL_TYPE_ENUM:
4030 case MYSQL_TYPE_SET:
4031 case MYSQL_TYPE_GEOMETRY:
4032 case MYSQL_TYPE_TINY_BLOB:
4033 case MYSQL_TYPE_MEDIUM_BLOB:
4034 case MYSQL_TYPE_LONG_BLOB:
4035 case MYSQL_TYPE_BLOB:
4036 case MYSQL_TYPE_VAR_STRING:
4037 case MYSQL_TYPE_STRING:
4038 case MYSQL_TYPE_BIT:
4039 case MYSQL_TYPE_NEWDATE:
4040 param->skip_result= skip_result_string;
4043 DBUG_PRINT(
"error", (
"Unknown field->type: %u", (uint) field->type));
4058 ulong bind_count= stmt->field_count;
4059 uint param_count= 0;
4060 DBUG_ENTER(
"mysql_stmt_bind_result");
4061 DBUG_PRINT(
"enter",(
"field_count: %lu", bind_count));
4065 int errorcode= (int) stmt->state < (
int) MYSQL_STMT_PREPARE_DONE ?
4066 CR_NO_PREPARE_STMT : CR_NO_STMT_METADATA;
4067 set_stmt_error(stmt, errorcode, unknown_sqlstate, NULL);
4079 if (stmt->bind != my_bind)
4080 memcpy((
char*) stmt->bind, (
char*) my_bind,
4083 for (param= stmt->bind, end= param + bind_count, field= stmt->fields ;
4087 DBUG_PRINT(
"info",(
"buffer_type: %u field_type: %u",
4088 (uint) param->buffer_type, (uint) field->type));
4093 if (!param->is_null)
4094 param->is_null= ¶m->is_null_value;
4097 param->length= ¶m->length_value;
4100 param->error= ¶m->error_value;
4102 param->param_number= param_count++;
4105 if (setup_one_fetch_function(param, field))
4107 strmov(stmt->sqlstate, unknown_sqlstate);
4108 sprintf(stmt->last_error,
4109 ER(stmt->last_errno= CR_UNSUPPORTED_PARAM_TYPE),
4110 field->type, param_count);
4114 stmt->bind_result_done= BIND_RESULT_DONE;
4115 if (stmt->mysql->options.report_data_truncation)
4116 stmt->bind_result_done|= REPORT_DATA_TRUNCATION;
4126 static int stmt_fetch_row(
MYSQL_STMT *stmt, uchar *row)
4130 uchar *null_ptr, bit;
4131 int truncation_count= 0;
4136 DBUG_ASSERT(stmt->field_count);
4139 if (!stmt->bind_result_done)
4146 row+= (stmt->field_count+9)/8;
4150 for (my_bind= stmt->bind, end= my_bind + stmt->field_count,
4151 field= stmt->fields ;
4156 if (*null_ptr & bit)
4165 my_bind->row_ptr= NULL;
4166 *my_bind->is_null= 1;
4170 *my_bind->is_null= 0;
4171 my_bind->row_ptr= row;
4172 (*my_bind->fetch_result)(my_bind, field, &row);
4173 truncation_count+= *my_bind->error;
4175 if (!((bit<<=1) & 255))
4181 if (truncation_count && (stmt->bind_result_done & REPORT_DATA_TRUNCATION))
4182 return MYSQL_DATA_TRUNCATED;
4187 int cli_unbuffered_fetch(
MYSQL *mysql,
char **row)
4189 if (packet_error == cli_safe_read(mysql))
4192 *row= ((mysql->net.read_pos[0] == 254) ? NULL :
4193 (
char*) (mysql->net.read_pos+1));
4202 int STDCALL mysql_stmt_fetch(
MYSQL_STMT *stmt)
4206 DBUG_ENTER(
"mysql_stmt_fetch");
4208 if ((rc= (*stmt->read_row_func)(stmt, &row)) ||
4209 ((rc= stmt_fetch_row(stmt, row)) && rc != MYSQL_DATA_TRUNCATED))
4211 stmt->state= MYSQL_STMT_PREPARE_DONE;
4212 stmt->read_row_func= (rc == MYSQL_NO_DATA) ?
4213 stmt_read_row_no_data : stmt_read_row_no_result_set;
4218 stmt->state= MYSQL_STMT_FETCH_DONE;
4241 uint column, ulong
offset)
4244 DBUG_ENTER(
"mysql_stmt_fetch_column");
4246 if ((
int) stmt->state < (
int) MYSQL_STMT_FETCH_DONE)
4248 set_stmt_error(stmt, CR_NO_DATA, unknown_sqlstate, NULL);
4251 if (column >= stmt->field_count)
4253 set_stmt_error(stmt, CR_INVALID_PARAMETER_NO, unknown_sqlstate, NULL);
4257 if (!my_bind->error)
4258 my_bind->error= &my_bind->error_value;
4263 uchar *row= param->row_ptr;
4265 if (my_bind->is_null)
4266 *my_bind->is_null= 0;
4267 if (my_bind->length)
4268 *my_bind->length= *param->length;
4270 my_bind->length= ¶m->length_value;
4271 fetch_result_with_conversion(my_bind, field, &row);
4275 if (my_bind->is_null)
4276 *my_bind->is_null= 1;
4290 MYSQL *mysql= stmt->mysql;
4295 DBUG_ENTER(
"cli_read_binary_rows");
4299 set_stmt_error(stmt, CR_SERVER_LOST, unknown_sqlstate, NULL);
4305 while ((pkt_len= cli_safe_read(mysql)) != packet_error)
4308 if (cp[0] != 254 || pkt_len >= 8)
4310 if (!(cur= (
MYSQL_ROWS*) alloc_root(&result->alloc,
4313 set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate, NULL);
4316 cur->data= (MYSQL_ROW) (cur+1);
4318 prev_ptr= &cur->next;
4319 memcpy((
char *) cur->data, (
char *) cp+1, pkt_len-1);
4320 cur->length= pkt_len;
4327 mysql->warning_count= uint2korr(cp+1);
4328 mysql->server_status= uint2korr(cp+3);
4329 DBUG_PRINT(
"info",(
"status: %u warning_count: %u",
4330 mysql->server_status, mysql->warning_count));
4334 set_stmt_errmsg(stmt, net);
4357 uchar *null_ptr, bit;
4358 uchar *row= (uchar*) data->data;
4360 uchar *row_end= row + data->length;
4364 row+= (stmt->field_count+9)/8;
4368 for (my_bind= stmt->bind, end= my_bind + stmt->field_count, field= stmt->fields ;
4372 if (!(*null_ptr & bit))
4373 (*my_bind->skip_result)(my_bind, field, &row);
4374 DBUG_ASSERT(row <= row_end);
4375 if (!((bit<<=1) & 255))
4388 int STDCALL mysql_stmt_store_result(
MYSQL_STMT *stmt)
4390 MYSQL *mysql= stmt->mysql;
4392 DBUG_ENTER(
"mysql_stmt_store_result");
4397 set_stmt_error(stmt, CR_SERVER_LOST, unknown_sqlstate, NULL);
4401 if (!stmt->field_count)
4404 if ((
int) stmt->state < (
int) MYSQL_STMT_EXECUTE_DONE)
4406 set_stmt_error(stmt, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate, NULL);
4410 if (stmt->last_errno)
4416 if (mysql->status == MYSQL_STATUS_READY &&
4417 stmt->server_status & SERVER_STATUS_CURSOR_EXISTS)
4422 NET *net= &mysql->net;
4427 int4store(buff, stmt->stmt_id);
4428 int4store(buff + 4, (
int)~0);
4429 if (cli_advanced_command(mysql, COM_STMT_FETCH, buff,
sizeof(buff),
4430 (uchar*) 0, 0, 1, stmt))
4437 set_stmt_errmsg(stmt, net);
4441 else if (mysql->status != MYSQL_STATUS_STATEMENT_GET_RESULT)
4443 set_stmt_error(stmt, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate, NULL);
4447 if (stmt->update_max_length && !stmt->bind_result_done)
4455 memset(stmt->bind, 0,
sizeof(*stmt->bind) * stmt->field_count);
4457 for (my_bind= stmt->bind, end= my_bind + stmt->field_count,
4458 field= stmt->fields;
4462 my_bind->buffer_type= MYSQL_TYPE_NULL;
4463 my_bind->buffer_length=1;
4466 if (mysql_stmt_bind_result(stmt, stmt->bind))
4468 stmt->bind_result_done= 0;
4471 if ((*mysql->methods->read_binary_rows)(stmt))
4473 free_root(&result->alloc, MYF(MY_KEEP_PREALLOC));
4476 mysql->status= MYSQL_STATUS_READY;
4481 DBUG_ASSERT(mysql->status != MYSQL_STATUS_READY ||
4482 (mysql->server_status & SERVER_STATUS_LAST_ROW_SENT));
4484 if (stmt->update_max_length)
4487 for(; cur; cur=cur->next)
4488 stmt_update_metadata(stmt, cur);
4491 stmt->data_cursor= result->data;
4492 mysql->affected_rows= stmt->affected_rows= result->rows;
4493 stmt->read_row_func= stmt_read_row_buffered;
4494 mysql->unbuffered_fetch_owner= 0;
4495 mysql->status= MYSQL_STATUS_READY;
4508 DBUG_ENTER(
"mysql_stmt_row_seek");
4510 stmt->data_cursor= row;
4511 DBUG_RETURN(offset);
4522 DBUG_ENTER(
"mysql_stmt_row_tell");
4524 DBUG_RETURN(stmt->data_cursor);
4533 mysql_stmt_data_seek(
MYSQL_STMT *stmt, my_ulonglong row)
4536 DBUG_ENTER(
"mysql_stmt_data_seek");
4537 DBUG_PRINT(
"enter",(
"row id to seek: %ld",(
long) row));
4539 for (; tmp && row; --row, tmp= tmp->next)
4541 stmt->data_cursor= tmp;
4545 stmt->read_row_func= stmt_read_row_buffered;
4546 stmt->state= MYSQL_STMT_EXECUTE_DONE;
4556 my_ulonglong STDCALL mysql_stmt_num_rows(
MYSQL_STMT *stmt)
4558 DBUG_ENTER(
"mysql_stmt_num_rows");
4560 DBUG_RETURN(stmt->result.rows);
4573 if ((
int) stmt->state > (
int) MYSQL_STMT_INIT_DONE)
4575 MYSQL *mysql= stmt->mysql;
4582 if (flags & RESET_STORE_RESULT)
4585 free_root(&result->alloc, MYF(MY_KEEP_PREALLOC));
4588 stmt->data_cursor= NULL;
4590 if (flags & RESET_LONG_DATA)
4592 MYSQL_BIND *param= stmt->params, *param_end= param + stmt->param_count;
4594 for (; param < param_end; param++)
4595 param->long_data_used= 0;
4597 stmt->read_row_func= stmt_read_row_no_result_set;
4600 if ((
int) stmt->state > (
int) MYSQL_STMT_PREPARE_DONE)
4602 if (mysql->unbuffered_fetch_owner == &stmt->unbuffered_fetch_cancelled)
4603 mysql->unbuffered_fetch_owner= 0;
4604 if (stmt->field_count && mysql->status != MYSQL_STATUS_READY)
4607 (*mysql->methods->flush_use_result)(mysql, FALSE);
4608 if (mysql->unbuffered_fetch_owner)
4609 *mysql->unbuffered_fetch_owner= TRUE;
4610 mysql->status= MYSQL_STATUS_READY;
4613 if (flags & RESET_SERVER_SIDE)
4619 uchar buff[MYSQL_STMT_HEADER];
4620 int4store(buff, stmt->stmt_id);
4621 if ((*mysql->methods->advanced_command)(mysql, COM_STMT_RESET, buff,
4622 sizeof(buff), 0, 0, 0, stmt))
4624 set_stmt_errmsg(stmt, &mysql->net);
4625 stmt->state= MYSQL_STMT_INIT_DONE;
4630 if (flags & RESET_CLEAR_ERROR)
4631 stmt_clear_error(stmt);
4632 stmt->state= MYSQL_STMT_PREPARE_DONE;
4637 my_bool STDCALL mysql_stmt_free_result(
MYSQL_STMT *stmt)
4639 DBUG_ENTER(
"mysql_stmt_free_result");
4642 DBUG_RETURN(reset_stmt_handle(stmt, RESET_LONG_DATA | RESET_STORE_RESULT |
4643 RESET_CLEAR_ERROR));
4662 my_bool STDCALL mysql_stmt_close(
MYSQL_STMT *stmt)
4664 MYSQL *mysql= stmt->mysql;
4666 DBUG_ENTER(
"mysql_stmt_close");
4668 free_root(&stmt->result.alloc, MYF(0));
4669 free_root(&stmt->mem_root, MYF(0));
4670 free_root(&stmt->extension->fields_mem_root, MYF(0));
4674 mysql->stmts= list_delete(mysql->stmts, &stmt->list);
4679 net_clear_error(&mysql->net);
4680 if ((
int) stmt->state > (
int) MYSQL_STMT_INIT_DONE)
4682 uchar buff[MYSQL_STMT_HEADER];
4684 if (mysql->unbuffered_fetch_owner == &stmt->unbuffered_fetch_cancelled)
4685 mysql->unbuffered_fetch_owner= 0;
4686 if (mysql->status != MYSQL_STATUS_READY)
4692 (*mysql->methods->flush_use_result)(mysql, TRUE);
4693 if (mysql->unbuffered_fetch_owner)
4694 *mysql->unbuffered_fetch_owner= TRUE;
4695 mysql->status= MYSQL_STATUS_READY;
4697 int4store(buff, stmt->stmt_id);
4698 if ((rc= stmt_command(mysql, COM_STMT_CLOSE, buff, 4, stmt)))
4700 set_stmt_errmsg(stmt, &mysql->net);
4705 my_free(stmt->extension);
4708 DBUG_RETURN(
test(rc));
4715 my_bool STDCALL mysql_stmt_reset(
MYSQL_STMT *stmt)
4717 DBUG_ENTER(
"mysql_stmt_reset");
4718 DBUG_ASSERT(stmt != 0);
4722 set_stmt_error(stmt, CR_SERVER_LOST, unknown_sqlstate, NULL);
4726 DBUG_RETURN(reset_stmt_handle(stmt,
4727 RESET_SERVER_SIDE | RESET_LONG_DATA |
4728 RESET_CLEAR_ERROR));
4735 uint STDCALL mysql_stmt_errno(
MYSQL_STMT * stmt)
4737 DBUG_ENTER(
"mysql_stmt_errno");
4738 DBUG_RETURN(stmt->last_errno);
4741 const char *STDCALL mysql_stmt_sqlstate(
MYSQL_STMT * stmt)
4743 DBUG_ENTER(
"mysql_stmt_sqlstate");
4744 DBUG_RETURN(stmt->sqlstate);
4751 const char *STDCALL mysql_stmt_error(
MYSQL_STMT * stmt)
4753 DBUG_ENTER(
"mysql_stmt_error");
4754 DBUG_RETURN(stmt->last_error);
4766 my_bool STDCALL mysql_commit(
MYSQL * mysql)
4768 DBUG_ENTER(
"mysql_commit");
4769 DBUG_RETURN((my_bool) mysql_real_query(mysql,
"commit", 6));
4776 my_bool STDCALL mysql_rollback(
MYSQL * mysql)
4778 DBUG_ENTER(
"mysql_rollback");
4779 DBUG_RETURN((my_bool) mysql_real_query(mysql,
"rollback", 8));
4787 my_bool STDCALL mysql_autocommit(
MYSQL * mysql, my_bool auto_mode)
4789 DBUG_ENTER(
"mysql_autocommit");
4790 DBUG_PRINT(
"enter", (
"mode : %d", auto_mode));
4792 DBUG_RETURN((my_bool) mysql_real_query(mysql, auto_mode ?
4793 "set autocommit=1":
"set autocommit=0",
4807 my_bool STDCALL mysql_more_results(
MYSQL *mysql)
4810 DBUG_ENTER(
"mysql_more_results");
4812 res= ((mysql->server_status & SERVER_MORE_RESULTS_EXISTS) ? 1: 0);
4813 DBUG_PRINT(
"exit",(
"More results exists ? %d", res));
4821 int STDCALL mysql_next_result(
MYSQL *mysql)
4823 DBUG_ENTER(
"mysql_next_result");
4825 if (mysql->status != MYSQL_STATUS_READY)
4827 set_mysql_error(mysql, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate);
4831 net_clear_error(&mysql->net);
4832 mysql->affected_rows= ~(my_ulonglong) 0;
4834 if (mysql->server_status & SERVER_MORE_RESULTS_EXISTS)
4835 DBUG_RETURN((*mysql->methods->next_result)(mysql));
4841 int STDCALL mysql_stmt_next_result(
MYSQL_STMT *stmt)
4843 MYSQL *mysql= stmt->mysql;
4845 DBUG_ENTER(
"mysql_stmt_next_result");
4850 if (stmt->last_errno)
4851 DBUG_RETURN(stmt->last_errno);
4853 if (mysql->server_status & SERVER_MORE_RESULTS_EXISTS)
4855 if (reset_stmt_handle(stmt, RESET_STORE_RESULT))
4859 rc= mysql_next_result(mysql);
4863 set_stmt_errmsg(stmt, &mysql->net);
4867 if (mysql->status == MYSQL_STATUS_GET_RESULT)
4868 mysql->status= MYSQL_STATUS_STATEMENT_GET_RESULT;
4870 stmt->state= MYSQL_STMT_EXECUTE_DONE;
4871 stmt->bind_result_done= FALSE;
4872 stmt->field_count= mysql->field_count;
4874 if (mysql->field_count)
4876 alloc_stmt_fields(stmt);
4877 prepare_to_fetch_result(stmt);
4886 return (*mysql->methods->use_result)(mysql);
4889 my_bool STDCALL mysql_read_query_result(
MYSQL *mysql)
4891 return (*mysql->methods->read_query_result)(mysql);