protocol.cc 28.8 KB
Newer Older
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1
/* Copyright (C) 2000-2003 MySQL AB
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/*
  Low level functions for storing data to be send to the MySQL client
  The actual communction is handled by the net_xxx functions in net_serv.cc
*/

#ifdef __GNUC__
#pragma implementation				// gcc: Class implementation
#endif

#include "mysql_priv.h"
#include <stdarg.h>

hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
29 30
#ifndef EMBEDDED_LIBRARY
bool Protocol::net_store_data(const char *from, uint length)
hf@deer.(none)'s avatar
SCRUM  
hf@deer.(none) committed
31 32 33
#else
bool Protocol_prep::net_store_data(const char *from, uint length)
#endif
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
34 35
{
  ulong packet_length=packet->length();
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
36 37 38 39 40 41
  /* 
     The +9 comes from that strings of length longer than 16M require
     9 bytes to be stored (see net_store_length).
  */
  if (packet_length+9+length > packet->alloced_length() &&
      packet->realloc(packet_length+9+length))
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
42 43 44 45 46 47 48 49 50
    return 1;
  char *to=(char*) net_store_length((char*) packet->ptr()+packet_length,
				    (ulonglong) length);
  memcpy(to,from,length);
  packet->length((uint) (to+length-packet->ptr()));
  return 0;
}


51 52 53 54
	/* Send a error string to client */

void send_error(THD *thd, uint sql_errno, const char *err)
{
55
#ifndef EMBEDDED_LIBRARY 
56
  uint length;
57
  char buff[MYSQL_ERRMSG_SIZE+2], *pos;
58
#endif
59 60 61 62 63 64
  NET *net= &thd->net;
  DBUG_ENTER("send_error");
  DBUG_PRINT("enter",("sql_errno: %d  err: %s", sql_errno,
		      err ? err : net->last_error[0] ?
		      net->last_error : "NULL"));

hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
65
#ifndef EMBEDDED_LIBRARY  /* TODO query cache in embedded library*/
66
  query_cache_abort(net);
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
67
#endif
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  thd->query_error=  1; // needed to catch query errors during replication
  if (!err)
  {
    if (sql_errno)
      err=ER(sql_errno);
    else
    {
      if ((err=net->last_error)[0])
	sql_errno=net->last_errno;
      else
      {
	sql_errno=ER_UNKNOWN_ERROR;
	err=ER(sql_errno);	 /* purecov: inspected */
      }
    }
  }
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
84 85 86 87

#ifdef EMBEDDED_LIBRARY
  net->last_errno= sql_errno;
  strmake(net->last_error, err, sizeof(net->last_error)-1);
88
  strmov(net->sqlstate, mysql_errno_to_sqlstate(sql_errno));
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
89 90
#else

91 92 93 94 95 96 97 98 99 100 101 102 103
  if (net->vio == 0)
  {
    if (thd->bootstrap)
    {
      /* In bootstrap it's ok to print on stderr */
      fprintf(stderr,"ERROR: %d  %s\n",sql_errno,err);
    }
    DBUG_VOID_RETURN;
  }

  if (net->return_errno)
  {				// new client code; Add errno before message
    int2store(buff,sql_errno);
104 105 106 107
    pos= buff+2;
    if (thd->client_capabilities & CLIENT_PROTOCOL_41)
    {
      /* The first # is to make the protocol backward compatible */
108
      buff[2]= '#';
109
      pos= strmov(buff+3, mysql_errno_to_sqlstate(sql_errno));
110 111
    }
    length= (uint) (strmake(pos, err, MYSQL_ERRMSG_SIZE-1) - buff);
112 113 114 115 116 117 118 119
    err=buff;
  }
  else
  {
    length=(uint) strlen(err);
    set_if_smaller(length,MYSQL_ERRMSG_SIZE-1);
  }
  VOID(net_write_command(net,(uchar) 255, "", 0, (char*) err,length));
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
120
#endif  /* EMBEDDED_LIBRARY*/
121
  thd->is_fatal_error=0;			// Error message is given
122 123 124 125
  thd->net.report_error= 0;
  DBUG_VOID_RETURN;
}

126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
/*
  Send a warning to the end user

  SYNOPSIS
    send_warning()
    thd			Thread handler
    sql_errno		Warning number (error message)
    err			Error string.  If not set, use ER(sql_errno)

  DESCRIPTION
    Register the warning so that the user can get it with mysql_warnings()
    Send an ok (+ warning count) to the end user.
*/

void send_warning(THD *thd, uint sql_errno, const char *err)
{
  DBUG_ENTER("send_warning");  
  push_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN, sql_errno,
	       err ? err : ER(sql_errno));
  send_ok(thd);
  DBUG_VOID_RETURN;
}


/*
   Write error package and flush to client
   It's a little too low level, but I don't want to use another buffer for
   this
*/

void
net_printf(THD *thd, uint errcode, ...)
{
  va_list args;
  uint length,offset;
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
162 163 164 165
  const char *format;
#ifndef EMBEDDED_LIBRARY
  const char *text_pos;
#else
166
  char text_pos[1024];
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
167
#endif
168 169
  int head_length= NET_HEADER_SIZE;
  NET *net= &thd->net;
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
170

171 172 173 174
  DBUG_ENTER("net_printf");
  DBUG_PRINT("enter",("message: %u",errcode));

  thd->query_error=  1; // needed to catch query errors during replication
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
175
#ifndef EMBEDDED_LIBRARY
176
  query_cache_abort(net);	// Safety
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
177
#endif
178 179 180 181 182 183 184 185 186 187 188 189 190 191
  va_start(args,errcode);
  /*
    The following is needed to make net_printf() work with 0 argument for
    errorcode and use the argument after that as the format string. This
    is useful for rare errors that are not worth the hassle to put in
    errmsg.sys, but at the same time, the message is not fixed text
  */
  if (errcode)
    format= ER(errcode);
  else
  {
    format=va_arg(args,char*);
    errcode= ER_UNKNOWN_ERROR;
  }
192 193 194
  offset= (net->return_errno ?
	   ((thd->client_capabilities & CLIENT_PROTOCOL_41) ?
	    2+SQLSTATE_LENGTH+1 : 2) : 0);
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
195
#ifndef EMBEDDED_LIBRARY
196
  text_pos=(char*) net->buff + head_length + offset + 1;
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
197
#endif
198 199 200 201 202 203
  (void) vsprintf(my_const_cast(char*) (text_pos),format,args);
  length=(uint) strlen((char*) text_pos);
  if (length >= sizeof(net->last_error))
    length=sizeof(net->last_error)-1;		/* purecov: inspected */
  va_end(args);

hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
204
#ifndef EMBEDDED_LIBRARY
205 206 207 208
  if (net->vio == 0)
  {
    if (thd->bootstrap)
    {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
209 210 211 212
      /*
	In bootstrap it's ok to print on stderr
	This may also happen when we get an error from a slave thread
      */
213
      fprintf(stderr,"ERROR: %d  %s\n",errcode,text_pos);
214
      thd->fatal_error();
215 216 217 218 219 220 221 222
    }
    DBUG_VOID_RETURN;
  }

  int3store(net->buff,length+1+offset);
  net->buff[3]= (net->compress) ? 0 : (uchar) (net->pkt_nr++);
  net->buff[head_length]=(uchar) 255;		// Error package
  if (offset)
223 224 225 226 227
  {
    uchar *pos= net->buff+head_length+1;
    int2store(pos, errcode);
    if (thd->client_capabilities & CLIENT_PROTOCOL_41)
    {
228 229
      pos[2]= '#';      /* To make the protocol backward compatible */
      memcpy(pos+3, mysql_errno_to_sqlstate(errcode), SQLSTATE_LENGTH);
230 231
    }
  }
232
  VOID(net_real_write(net,(char*) net->buff,length+head_length+1+offset));
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
233 234 235
#else
  net->last_errno= errcode;
  strmake(net->last_error, text_pos, length);
236
  strmake(net->sqlstate, mysql_errno_to_sqlstate(errcode), SQLSTATE_LENGTH);
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
237
#endif
238
  thd->is_fatal_error=0;			// Error message is given
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
  DBUG_VOID_RETURN;
}

/*
  Return ok to the client.

  SYNOPSIS
    send_ok()
    thd			Thread handler
    affected_rows	Number of rows changed by statement
    id			Auto_increment id for first row (if used)
    message		Message to send to the client (Used by mysql_status)

  DESCRIPTION
    The ok packet has the following structure

    0			Marker (1 byte)
    affected_rows	Stored in 1-9 bytes
    id			Stored in 1-9 bytes
    server_status	Copy of thd->server_status;  Can be used by client
			to check if we are inside an transaction
			New in 4.0 protocol
    warning_count	Stored in 2 bytes; New in 4.1 protocol
    message		Stored as packed length (1-9 bytes) + message
			Is not stored if no message

   If net->no_send_ok return without sending packet
*/    

268
#ifndef EMBEDDED_LIBRARY
269 270 271 272 273 274
void
send_ok(THD *thd, ha_rows affected_rows, ulonglong id, const char *message)
{
  NET *net= &thd->net;
  char buff[MYSQL_ERRMSG_SIZE+10],*pos;
  DBUG_ENTER("send_ok");
275 276 277 278

  if (net->no_send_ok || !net->vio)	// hack for re-parsing queries
    DBUG_VOID_RETURN;

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  buff[0]=0;					// No fields
  pos=net_store_length(buff+1,(ulonglong) affected_rows);
  pos=net_store_length(pos, (ulonglong) id);
  if (thd->client_capabilities & CLIENT_PROTOCOL_41)
  {
    int2store(pos,thd->server_status);
    pos+=2;

    /* We can only return up to 65535 warnings in two bytes */
    uint tmp= min(thd->total_warn_count, 65535);
    int2store(pos, tmp);
    pos+= 2;
  }
  else if (net->return_status)			// For 4.0 protocol
  {
    int2store(pos,thd->server_status);
    pos+=2;
  }
  if (message)
    pos=net_store_data((char*) pos, message, strlen(message));
  VOID(my_net_write(net,buff,(uint) (pos-buff)));
  VOID(net_flush(net));
  DBUG_VOID_RETURN;
}


/*
  Send eof (= end of result set) to the client

  SYNOPSIS
    send_eof()
    thd			Thread handler
    no_flush		Set to 1 if there will be more data to the client,
			like in send_fields().

  DESCRIPTION
    The eof packet has the following structure

    254			Marker (1 byte)
    warning_count	Stored in 2 bytes; New in 4.1 protocol
    status_flag		Stored in 2 bytes;
			For flags like SERVER_STATUS_MORE_RESULTS

    Note that the warning count will not be sent if 'no_flush' is set as
    we don't want to report the warning count until all data is sent to the
    client.
*/    

void
send_eof(THD *thd, bool no_flush)
{
  static char eof_buff[1]= { (char) 254 };	/* Marker for end of fields */
  NET *net= &thd->net;
  DBUG_ENTER("send_eof");
  if (net->vio != 0)
  {
    if (!no_flush && (thd->client_capabilities & CLIENT_PROTOCOL_41))
    {
      uchar buff[5];
      uint tmp= min(thd->total_warn_count, 65535);
      buff[0]=254;
      int2store(buff+1, tmp);
      int2store(buff+3, 0);			// No flags yet
      VOID(my_net_write(net,(char*) buff,5));
      VOID(net_flush(net));
    }
    else
    {
      VOID(my_net_write(net,eof_buff,1));
      if (!no_flush)
	VOID(net_flush(net));
    }
  }
  DBUG_VOID_RETURN;
}
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

/*
    Please client to send scrambled_password in old format.
  SYNOPSYS
    send_old_password_request()
    thd thread handle
     
  RETURN VALUE
    0  ok
   !0  error
*/

bool send_old_password_request(THD *thd)
{
  static char buff[1]= { (char) 254 };
  NET *net= &thd->net;
  return my_net_write(net, buff, 1) || net_flush(net);
}

hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
373
#endif /* EMBEDDED_LIBRARY */
374 375

/*
376 377 378 379 380 381 382
  Faster net_store_length when we know that length is less than 65536.
  We keep a separate version for that range because it's widely used in
  libmysql.
  uint is used as agrument type because of MySQL type conventions:
  uint for 0..65536
  ulong for 0..4294967296
  ulonglong for bigger numbers.
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
*/

char *net_store_length(char *pkg, uint length)
{
  uchar *packet=(uchar*) pkg;
  if (length < 251)
  {
    *packet=(uchar) length;
    return (char*) packet+1;
  }
  *packet++=252;
  int2store(packet,(uint) length);
  return (char*) packet+2;
}


/****************************************************************************
  Functions used by the protocol functions (like send_ok) to store strings
  and numbers in the header result packet.
****************************************************************************/

/* The following will only be used for short strings < 65K */

char *net_store_data(char *to,const char *from, uint length)
{
  to=net_store_length(to,length);
  memcpy(to,from,length);
  return to+length;
}

char *net_store_data(char *to,int32 from)
{
  char buff[20];
  uint length=(uint) (int10_to_str(from,buff,10)-buff);
  to=net_store_length(to,length);
  memcpy(to,buff,length);
  return to+length;
}

char *net_store_data(char *to,longlong from)
{
  char buff[22];
  uint length=(uint) (longlong10_to_str(from,buff,10)-buff);
  to=net_store_length(to,length);
  memcpy(to,buff,length);
  return to+length;
}


/*****************************************************************************
  Default Protocol functions
*****************************************************************************/

void Protocol::init(THD *thd_arg)
{
  thd=thd_arg;
  packet= &thd->packet;
#ifndef DEBUG_OFF
  field_types= 0;
#endif
}

445

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
/*
  Send name and type of result to client.

  SYNOPSIS
    send_fields()
    THD		Thread data object
    list	List of items to send to client
    flag	Bit mask with the following functions:
		1 send number of rows
		2 send default values

  DESCRIPTION
    Sum fields has table name empty and field_name.

  RETURN VALUES
    0	ok
    1	Error  (Note that in this case the error is not sent to the client)
*/

hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
465
#ifndef EMBEDDED_LIBRARY
466 467 468 469 470
bool Protocol::send_fields(List<Item> *list, uint flag)
{
  List_iterator_fast<Item> it(*list);
  Item *item;
  char buff[80];
471
  String tmp((char*) buff,sizeof(buff),&my_charset_bin);
472 473
  Protocol_simple prot(thd);
  String *packet= prot.storage_packet();
474
  CHARSET_INFO *thd_charset= thd->variables.character_set_results;
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
  DBUG_ENTER("send_fields");

  if (flag & 1)
  {				// Packet with number of elements
    char *pos=net_store_length(buff, (uint) list->elements);
    (void) my_net_write(&thd->net, buff,(uint) (pos-buff));
  }

#ifndef DEBUG_OFF
  field_types= (enum_field_types*) thd->alloc(sizeof(field_types) *
					      list->elements);
  uint count= 0;
#endif

  while ((item=it++))
  {
    char *pos;
492
    CHARSET_INFO *cs= system_charset_info;
493 494 495 496 497 498
    Send_field field;
    item->make_field(&field);
    prot.prepare_for_resend();

    if (thd->client_capabilities & CLIENT_PROTOCOL_41)
    {
499
      if (prot.store("def", 3, cs, thd_charset) ||
500 501
	  prot.store(field.db_name, (uint) strlen(field.db_name),
		     cs, thd_charset) ||
502
	  prot.store(field.table_name, (uint) strlen(field.table_name),
503
		     cs, thd_charset) ||
504
	  prot.store(field.org_table_name, (uint) strlen(field.org_table_name),
505
		     cs, thd_charset) ||
506
	  prot.store(field.col_name, (uint) strlen(field.col_name),
507
		     cs, thd_charset) ||
508
	  prot.store(field.org_col_name, (uint) strlen(field.org_col_name),
509
		     cs, thd_charset) ||
510
	  packet->realloc(packet->length()+12))
511
	goto err;
512 513
      /* Store fixed length fields */
      pos= (char*) packet->ptr()+packet->length();
514
      *pos++= 12;				// Length of packed fields
515
      int2store(pos, field.charsetnr);
516 517 518 519
      int4store(pos+2, field.length);
      pos[6]= field.type;
      int2store(pos+7,field.flags);
      pos[9]= (char) field.decimals;
520
      pos[10]= 0;				// For the future
521 522
      pos[11]= 0;				// For the future
      pos+= 12;
523 524 525
    }
    else
    {
526
      if (prot.store(field.table_name, (uint) strlen(field.table_name),
527
		     cs, thd_charset) ||
528
	  prot.store(field.col_name, (uint) strlen(field.col_name),
529
		     cs, thd_charset) ||
530
	  packet->realloc(packet->length()+10))
531
	goto err;
532
      pos= (char*) packet->ptr()+packet->length();
533 534

#ifdef TO_BE_DELETED_IN_6
535 536 537 538 539 540 541 542 543 544 545 546
      if (!(thd->client_capabilities & CLIENT_LONG_FLAG))
      {
	pos[0]=3;
	int3store(pos+1,field.length);
	pos[4]=1;
	pos[5]=field.type;
	pos[6]=2;
	pos[7]= (char) field.flags;
	pos[8]= (char) field.decimals;
	pos+= 9;
      }
      else
547
#endif
548 549 550 551 552 553 554 555 556 557
      {
	pos[0]=3;
	int3store(pos+1,field.length);
	pos[4]=1;
	pos[5]=field.type;
	pos[6]=3;
	int2store(pos+7,field.flags);
	pos[9]= (char) field.decimals;
	pos+= 10;
      }
558
    }
559
    packet->length((uint) (pos - packet->ptr()));
560 561 562 563 564 565 566 567 568
    if (flag & 2)
      item->send(&prot, &tmp);			// Send default value
    if (prot.write())
      break;					/* purecov: inspected */
#ifndef DEBUG_OFF
    field_types[count++]= field.type;
#endif
  }

569
  send_eof(thd, 1);
570 571 572 573 574 575 576
  DBUG_RETURN(prepare_for_send(list));

err:
  send_error(thd,ER_OUT_OF_RESOURCES);		/* purecov: inspected */
  DBUG_RETURN(1);				/* purecov: inspected */
}

577

hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
578 579 580 581 582 583 584 585 586
bool Protocol::send_records_num(List<Item> *list, ulonglong records)
{
  char *pos;
  char buff[20];
  pos=net_store_length(buff, (uint) list->elements);
  pos=net_store_length(pos, records);
  return my_net_write(&thd->net, buff,(uint) (pos-buff));
}

587

588 589 590
bool Protocol::write()
{
  DBUG_ENTER("Protocol::write");
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
591
  DBUG_RETURN(my_net_write(&thd->net, packet->ptr(), packet->length()));
592
}
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
593 594 595
#endif /* EMBEDDED_LIBRARY */


596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
/*
  Send \0 end terminated string

  SYNOPSIS
    store()
    from	NullS or \0 terminated string

  NOTES
    In most cases one should use store(from, length) instead of this function

  RETURN VALUES
    0		ok
    1		error
*/

611
bool Protocol::store(const char *from, CHARSET_INFO *cs)
612 613 614 615
{
  if (!from)
    return store_null();
  uint length= strlen(from);
616
  return store(from, length, cs);
617 618 619 620 621 622 623 624 625 626
}


/*
  Send a set of strings as one long string with ',' in between
*/

bool Protocol::store(I_List<i_string>* str_list)
{
  char buf[256];
627
  String tmp(buf, sizeof(buf), &my_charset_bin);
628
  uint32 len;
629 630 631
  I_List_iterator<i_string> it(*str_list);
  i_string* s;

632
  tmp.length(0);
633 634 635
  while ((s=it++))
  {
    tmp.append(s->ptr);
636
    tmp.append(',');
637
  }
638 639
  if ((len= tmp.length()))
    len--;					// Remove last ','
640
  return store((char*) tmp.ptr(), len,  tmp.charset());
641 642 643 644 645 646 647 648 649 650 651
}


/****************************************************************************
  Functions to handle the simple (default) protocol where everything is
  This protocol is the one that is used by default between the MySQL server
  and client when you are not using prepared statements.

  All data are sent as 'packed-string-length' followed by 'string-data'
****************************************************************************/

hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
652
#ifndef EMBEDDED_LIBRARY
653 654 655 656 657 658 659 660 661 662 663 664 665 666
void Protocol_simple::prepare_for_resend()
{
  packet->length(0);
#ifndef DEBUG_OFF
  field_pos= 0;
#endif
}

bool Protocol_simple::store_null()
{
#ifndef DEBUG_OFF
  field_pos++;
#endif
  char buff[1];
667
  buff[0]= (char)251;
668 669
  return packet->append(buff, sizeof(buff), PACKET_BUFFET_EXTRA_ALLOC);
}
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
670
#endif
671

672

673 674
bool Protocol_simple::store(const char *from, uint length,
			    CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
675 676 677
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
678 679 680
	      field_types[field_pos] == MYSQL_TYPE_DECIMAL ||
	      (field_types[field_pos] >= MYSQL_TYPE_ENUM &&
	       field_types[field_pos] <= MYSQL_TYPE_GEOMETRY));
681 682
  field_pos++;
#endif
683
  if (tocs && !my_charset_same(fromcs, tocs) &&
684 685
      (fromcs != &my_charset_bin) &&
      (tocs   != &my_charset_bin))
686
  {
687 688 689 690 691 692 693 694 695 696 697
    convert.copy(from, length, fromcs, tocs);
    return net_store_data(convert.ptr(), convert.length());
  }
  else
    return net_store_data(from, length);
}


bool Protocol_simple::store(const char *from, uint length,
			    CHARSET_INFO *fromcs)
{
698
  CHARSET_INFO *tocs= this->thd->variables.character_set_results;
699 700 701 702 703 704 705
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
	      field_types[field_pos] == MYSQL_TYPE_DECIMAL ||
	      (field_types[field_pos] >= MYSQL_TYPE_ENUM &&
	       field_types[field_pos] <= MYSQL_TYPE_GEOMETRY));
  field_pos++;
#endif
706
  if (tocs && !my_charset_same(fromcs, tocs) &&
707 708 709 710
      (fromcs != &my_charset_bin) &&
      (tocs   != &my_charset_bin))
  {
    convert.copy(from, length, fromcs, tocs);
711
    return net_store_data(convert.ptr(), convert.length());
712 713 714
  }
  else
    return net_store_data(from, length);
715 716 717 718 719 720
}


bool Protocol_simple::store_tiny(longlong from)
{
#ifndef DEBUG_OFF
721 722
  DBUG_ASSERT(field_types == 0 || field_types[field_pos] == MYSQL_TYPE_TINY);
  field_pos++;
723 724
#endif
  char buff[20];
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
725
  return net_store_data((char*) buff,
726 727 728
			(uint) (int10_to_str((int) from,buff, -10)-buff));
}

729

730 731 732 733
bool Protocol_simple::store_short(longlong from)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
734 735
	      field_types[field_pos] == MYSQL_TYPE_SHORT);
  field_pos++;
736 737
#endif
  char buff[20];
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
738
  return net_store_data((char*) buff,
739 740 741
			(uint) (int10_to_str((int) from,buff, -10)-buff));
}

742

743 744 745
bool Protocol_simple::store_long(longlong from)
{
#ifndef DEBUG_OFF
746 747 748 749
  DBUG_ASSERT(field_types == 0 ||
              field_types[field_pos] == MYSQL_TYPE_INT24 ||
              field_types[field_pos] == MYSQL_TYPE_LONG);
  field_pos++;
750 751
#endif
  char buff[20];
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
752
  return net_store_data((char*) buff,
753 754 755 756 757 758 759 760
			(uint) (int10_to_str((int) from,buff, -10)-buff));
}


bool Protocol_simple::store_longlong(longlong from, bool unsigned_flag)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
761 762
	      field_types[field_pos] == MYSQL_TYPE_LONGLONG);
  field_pos++;
763 764
#endif
  char buff[22];
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
765
  return net_store_data((char*) buff,
766 767 768 769 770 771 772 773 774 775
			(uint) (longlong10_to_str(from,buff,
						  unsigned_flag ? 10 : -10)-
				buff));
}


bool Protocol_simple::store(float from, uint32 decimals, String *buffer)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
776 777
	      field_types[field_pos] == MYSQL_TYPE_FLOAT);
  field_pos++;
778
#endif
779
  buffer->set((double) from, decimals, thd->charset());
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
780
  return net_store_data((char*) buffer->ptr(), buffer->length());
781 782
}

783

784 785 786 787
bool Protocol_simple::store(double from, uint32 decimals, String *buffer)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
788 789
	      field_types[field_pos] == MYSQL_TYPE_DOUBLE);
  field_pos++;
790
#endif
791
  buffer->set(from, decimals, thd->charset());
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
792
  return net_store_data((char*) buffer->ptr(), buffer->length());
793 794 795 796 797
}


bool Protocol_simple::store(Field *field)
{
798 799
  if (field->is_null())
    return store_null();
800 801 802 803
#ifndef DEBUG_OFF
  field_pos++;
#endif
  char buff[MAX_FIELD_WIDTH];
804
  String str(buff,sizeof(buff), &my_charset_bin);
805 806
  CHARSET_INFO *tocs= this->thd->variables.character_set_results;

807
  field->val_str(&str,&str);
808
  if (tocs && !my_charset_same(field->charset(), tocs) &&
809
      (field->charset() != &my_charset_bin) &&
810
      (tocs != &my_charset_bin))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
811
  {
812
    convert.copy(str.ptr(), str.length(), str.charset(), tocs);
813
    return net_store_data(convert.ptr(), convert.length());
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
814 815
  }
  else
816
    return net_store_data(str.ptr(), str.length());
817 818 819
}


820 821 822 823 824 825 826
/*
   TODO:
        Second_part format ("%06") needs to change when 
        we support 0-6 decimals for time.
*/


827 828 829 830
bool Protocol_simple::store(TIME *tm)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
831 832 833
	      field_types[field_pos] == MYSQL_TYPE_DATETIME ||
	      field_types[field_pos] == MYSQL_TYPE_TIMESTAMP);
  field_pos++;
834 835
#endif
  char buff[40];
836 837 838 839 840 841 842 843 844 845 846
  uint length;
  length= my_sprintf(buff,(buff, "%04d-%02d-%02d %02d:%02d:%02d",
			   (int) tm->year,
			   (int) tm->month,
			   (int) tm->day,
			   (int) tm->hour,
			   (int) tm->minute,
			   (int) tm->second));
  if (tm->second_part)
    length+= my_sprintf(buff+length,(buff+length, ".%06d", (int)tm->second_part));
  return net_store_data((char*) buff, length);
847 848 849 850 851 852 853
}


bool Protocol_simple::store_date(TIME *tm)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
854 855
	      field_types[field_pos] == MYSQL_TYPE_DATE);
  field_pos++;
856 857
#endif
  char buff[40];
858
  String tmp((char*) buff,sizeof(buff),&my_charset_bin);
859
  make_date((DATE_TIME_FORMAT *) 0, tm, &tmp);
860
  return net_store_data((char*) tmp.ptr(), tmp.length());
861 862 863
}


864 865 866 867 868 869
/*
   TODO:
        Second_part format ("%06") needs to change when 
        we support 0-6 decimals for time.
*/

870 871 872 873
bool Protocol_simple::store_time(TIME *tm)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
874 875
	      field_types[field_pos] == MYSQL_TYPE_TIME);
  field_pos++;
876 877
#endif
  char buff[40];
878
  uint length;
879
  uint day= (tm->year || tm->month) ? 0 : tm->day;
880 881 882 883 884 885 886 887
  length= my_sprintf(buff,(buff, "%s%02ld:%02d:%02d",
			   tm->neg ? "-" : "",
			   (long) day*24L+(long) tm->hour,
			   (int) tm->minute,
			   (int) tm->second));
  if (tm->second_part)
    length+= my_sprintf(buff+length,(buff+length, ".%06d", (int)tm->second_part));
  return net_store_data((char*) buff, length);
888 889 890 891 892
}


/****************************************************************************
  Functions to handle the binary protocol used with prepared statements
893 894 895

  Data format:

896 897 898 899 900 901 902 903 904 905 906 907
   [ok:1]                            reserved ok packet
   [null_field:(field_count+7+2)/8]  reserved to send null data. The size is
                                     calculated using:
                                     bit_fields= (field_count+7+2)/8; 
                                     2 bits are reserved for identifying type
				     of package.
   [[length]data]                    data field (the length applies only for 
                                     string/binary/time/timestamp fields and 
                                     rest of them are not sent as they have 
                                     the default length that client understands
                                     based on the field type
   [..]..[[length]data]              data
908 909 910 911
****************************************************************************/

bool Protocol_prep::prepare_for_send(List<Item> *item_list)
{
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
912
  Protocol::prepare_for_send(item_list);
913 914
  bit_fields= (field_count+9)/8;
  if (packet->alloc(bit_fields+1))
915 916 917 918 919 920 921 922
    return 1;
  /* prepare_for_resend will be called after this one */
  return 0;
}


void Protocol_prep::prepare_for_resend()
{
923 924
  packet->length(bit_fields+1);
  bzero((char*) packet->ptr(), 1+bit_fields);
925 926 927 928
  field_pos=0;
}


929
bool Protocol_prep::store(const char *from,uint length, CHARSET_INFO *cs)
930 931 932
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
933 934 935
	      field_types[field_pos] == MYSQL_TYPE_DECIMAL ||
	      (field_types[field_pos] >= MYSQL_TYPE_ENUM &&
	       field_types[field_pos] <= MYSQL_TYPE_GEOMETRY));
936 937
#endif
  field_pos++;
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
938
  return net_store_data(from, length);
939 940
}

941 942 943 944 945 946 947 948 949 950 951 952 953
bool Protocol_prep::store(const char *from,uint length,
			  CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
	      field_types[field_pos] == MYSQL_TYPE_DECIMAL ||
	      (field_types[field_pos] >= MYSQL_TYPE_ENUM &&
	       field_types[field_pos] <= MYSQL_TYPE_GEOMETRY));
#endif
  field_pos++;
  return net_store_data(from, length);
}

954 955
bool Protocol_prep::store_null()
{
956
  uint offset= (field_pos+2)/8+1, bit= (1 << ((field_pos+2) & 7));
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
  /* Room for this as it's allocated in prepare_for_send */
  char *to= (char*) packet->ptr()+offset;
  *to= (char) ((uchar) *to | (uchar) bit);
  field_pos++;
  return 0;
}


bool Protocol_prep::store_tiny(longlong from)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
	      field_types[field_pos] == MYSQL_TYPE_TINY);
#endif
  char buff[1];
  field_pos++;
  buff[0]= (uchar) from;
  return packet->append(buff, sizeof(buff), PACKET_BUFFET_EXTRA_ALLOC);
}


bool Protocol_prep::store_short(longlong from)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
982 983
        field_types[field_pos] == MYSQL_TYPE_SHORT ||
        field_types[field_pos] == MYSQL_TYPE_YEAR);
984 985 986 987 988 989 990 991 992 993 994 995 996 997
#endif
  field_pos++;
  char *to= packet->prep_append(2, PACKET_BUFFET_EXTRA_ALLOC);
  if (!to)
    return 1;
  int2store(to, (int) from);
  return 0;
}


bool Protocol_prep::store_long(longlong from)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
998
	      field_types[field_pos] == MYSQL_TYPE_INT24 ||
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
	      field_types[field_pos] == MYSQL_TYPE_LONG);
#endif
  field_pos++;
  char *to= packet->prep_append(4, PACKET_BUFFET_EXTRA_ALLOC);
  if (!to)
    return 1;
  int4store(to, from);
  return 0;
}


bool Protocol_prep::store_longlong(longlong from, bool unsigned_flag)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
	      field_types[field_pos] == MYSQL_TYPE_LONGLONG);
#endif
  field_pos++;
  char *to= packet->prep_append(8, PACKET_BUFFET_EXTRA_ALLOC);
  if (!to)
    return 1;
  int8store(to, from);
  return 0;
}


bool Protocol_prep::store(float from, uint32 decimals, String *buffer)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
	      field_types[field_pos] == MYSQL_TYPE_FLOAT);
#endif
  field_pos++;
  char *to= packet->prep_append(4, PACKET_BUFFET_EXTRA_ALLOC);
  if (!to)
    return 1;
  float4store(to, from);
  return 0;
}


bool Protocol_prep::store(double from, uint32 decimals, String *buffer)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
	      field_types[field_pos] == MYSQL_TYPE_DOUBLE);
#endif
  field_pos++;
  char *to= packet->prep_append(8, PACKET_BUFFET_EXTRA_ALLOC);
  if (!to)
    return 1;
  float8store(to, from);
  return 0;
}


bool Protocol_prep::store(Field *field)
{
  /*
1058
    We should not increment field_pos here as send_binary() will call another
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
    protocol function to do this for us
  */
  if (field->is_null())
    return store_null();
  return field->send_binary(this);
}


bool Protocol_prep::store(TIME *tm)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
	      field_types[field_pos] == MYSQL_TYPE_DATETIME ||
1072 1073
	      field_types[field_pos] == MYSQL_TYPE_DATE ||
	      field_types[field_pos] == MYSQL_TYPE_TIMESTAMP);
1074 1075 1076 1077 1078
#endif
  char buff[12],*pos;
  uint length;
  field_pos++;
  pos= buff+1;
1079

1080
  int2store(pos, tm->year);
1081 1082 1083 1084 1085
  pos[2]= (uchar) tm->month;
  pos[3]= (uchar) tm->day;
  pos[4]= (uchar) tm->hour;
  pos[5]= (uchar) tm->minute;
  pos[6]= (uchar) tm->second;
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
  int4store(pos+7, tm->second_part);
  if (tm->second_part)
    length=11;
  else if (tm->hour || tm->minute || tm->second)
    length=7;
  else if (tm->year || tm->month || tm->day)
    length=4;
  else
    length=0;
  buff[0]=(char) length;			// Length is stored first
  return packet->append(buff, length+1, PACKET_BUFFET_EXTRA_ALLOC);
}

bool Protocol_prep::store_date(TIME *tm)
{
  tm->hour= tm->minute= tm->second=0;
  tm->second_part= 0;
  return Protocol_prep::store(tm);
}


bool Protocol_prep::store_time(TIME *tm)
{
#ifndef DEBUG_OFF
  DBUG_ASSERT(field_types == 0 ||
	      field_types[field_pos] == MYSQL_TYPE_TIME);
#endif
  char buff[15],*pos;
  uint length;
  field_pos++;
  pos= buff+1;
  pos[0]= tm->neg ? 1 : 0;
1118
  int4store(pos+1, tm->day);
1119 1120 1121 1122
  pos[5]= (uchar) tm->hour;
  pos[6]= (uchar) tm->minute;
  pos[7]= (uchar) tm->second;
  int4store(pos+8, tm->second_part);
1123
  if (tm->second_part)
1124
    length=11;
1125
  else if (tm->hour || tm->minute || tm->second || tm->day)
1126
    length=8;
1127 1128 1129 1130 1131
  else
    length=0;
  buff[0]=(char) length;			// Length is stored first
  return packet->append(buff, length+1, PACKET_BUFFET_EXTRA_ALLOC);
}
hf@deer.(none)'s avatar
hf@deer.(none) committed
1132 1133 1134 1135 1136 1137 1138 1139 1140

#ifdef EMBEDDED_LIBRARY
/* Should be removed when we define the Protocol_cursor's future */
bool Protocol_cursor::write()
{
  return Protocol_simple::write();
}
#endif