field.cc 126 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
3 4 5 6
   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.
unknown's avatar
unknown committed
7

unknown's avatar
unknown committed
8 9 10 11
   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.
unknown's avatar
unknown committed
12

unknown's avatar
unknown committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
   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 */


/*****************************************************************************
** This file implements classes defined in field.h
*****************************************************************************/

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

#include "mysql_priv.h"
#include "sql_select.h"
#include <m_ctype.h>
#include <errno.h>
#ifdef HAVE_FCONVERT
#include <floatingpoint.h>
#endif

unknown's avatar
unknown committed
34 35 36
// Maximum allowed exponent value for converting string to decimal
#define MAX_EXPONENT 1024

unknown's avatar
unknown committed
37
/*****************************************************************************
38
  Instansiate templates and static variables
unknown's avatar
unknown committed
39 40 41 42 43 44 45 46 47 48 49
*****************************************************************************/

#ifdef __GNUC__
template class List<create_field>;
template class List_iterator<create_field>;
#endif

uchar Field_null::null[1]={1};
const char field_separator=',';

/*****************************************************************************
50
  Static help functions
unknown's avatar
unknown committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
*****************************************************************************/

void Field_num::prepend_zeros(String *value)
{
  int diff;
  if ((diff= (int) (field_length - value->length())) > 0)
  {
    bmove_upp((char*) value->ptr()+field_length,value->ptr()+value->length(),
	      value->length());
    bfill((char*) value->ptr(),diff,'0');
    value->length(field_length);
    (void) value->c_ptr_quick();		// Avoid warnings in purify
  }
}

/*
67
  Test if given number is a int (or a fixed format float with .000)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

  SYNOPSIS
    test_if_int()
    str		String to test
    end		Pointer to char after last used digit
    cs		Character set

  NOTES
    This is called after one has called my_strntol() or similar function.
    This is only used to give warnings in ALTER TABLE or LOAD DATA...

  TODO
    Make this multi-byte-character safe

  RETURN
    0	ok
    1	error
unknown's avatar
unknown committed
85 86
*/

87 88
bool test_if_int(const char *str, int length, const char *int_end,
		 CHARSET_INFO *cs)
unknown's avatar
unknown committed
89
{
90 91
  if (str == int_end)
    return 0;					// Empty string
unknown's avatar
unknown committed
92
  const char *end=str+length;
93 94
  if ((str= int_end) == end)
    return 1;					// All digits was used
unknown's avatar
unknown committed
95

96 97
  /* Allow end .0000 */
  if (*str == '.')
unknown's avatar
unknown committed
98
  {
99 100 101 102 103 104 105
    for (str++ ; str != end && *str == '0'; str++) ;
  }
  /* Allow end space */
  for (str++ ; str != end ; str++)
  {
    if (!my_isspace(cs,*str))
      return 0;
unknown's avatar
unknown committed
106 107 108 109 110
  }
  return 1;
}


111
static bool test_if_real(const char *str,int length, CHARSET_INFO *cs)
unknown's avatar
unknown committed
112
{
113
  cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
114 115

  while (length && my_isspace(cs,*str))
unknown's avatar
unknown committed
116 117 118 119 120 121 122 123
  {						// Allow start space
    length--; str++;
  }
  if (!length)
    return 0;
  if (*str == '+' || *str == '-')
  {
    length--; str++;
124
    if (!length || !(my_isdigit(cs,*str) || *str == '.'))
unknown's avatar
unknown committed
125 126
      return 0;
  }
127
  while (length && my_isdigit(cs,*str))
unknown's avatar
unknown committed
128 129 130 131 132 133 134 135
  {
    length--; str++;
  }
  if (!length)
    return 1;
  if (*str == '.')
  {
    length--; str++;
136
    while (length && my_isdigit(cs,*str))
unknown's avatar
unknown committed
137 138 139 140 141 142 143 144
    {
      length--; str++;
    }
  }
  if (!length)
    return 1;
  if (*str == 'E' || *str == 'e')
  {
145
    if (length < 3 || (str[1] != '+' && str[1] != '-') || 
146
        !my_isdigit(cs,str[2]))
unknown's avatar
unknown committed
147 148 149
      return 0;
    length-=3;
    str+=3;
150
    while (length && my_isdigit(cs,*str))
unknown's avatar
unknown committed
151 152 153 154
    {
      length--; str++;
    }
  }
155
  for (; length ; length--, str++)
unknown's avatar
unknown committed
156
  {						// Allow end space
157
    if (!my_isspace(cs,*str))
unknown's avatar
unknown committed
158 159 160 161 162 163
      return 0;
  }
  return 1;
}


164 165 166 167 168 169 170 171
static inline uint field_length_without_space(const char *ptr, uint length)
{
  const char *end= ptr+length;
  while (end > ptr && end[-1] == ' ')
    end--;
  return (uint) (end-ptr);
}

unknown's avatar
unknown committed
172 173
/****************************************************************************
** Functions for the base classes
unknown's avatar
unknown committed
174
** This is an unpacked number.
unknown's avatar
unknown committed
175 176 177
****************************************************************************/

Field::Field(char *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
unknown's avatar
unknown committed
178
	     uchar null_bit_arg,
unknown's avatar
unknown committed
179 180
	     utype unireg_check_arg, const char *field_name_arg,
	     struct st_table *table_arg)
unknown's avatar
unknown committed
181 182 183 184 185 186
  :ptr(ptr_arg),null_ptr(null_ptr_arg),
   table(table_arg),table_name(table_arg ? table_arg->table_name : 0),
   field_name(field_name_arg),
   query_id(0),key_start(0),part_of_key(0),part_of_sortkey(0),
   unireg_check(unireg_check_arg),
   field_length(length_arg),null_bit(null_bit_arg)
unknown's avatar
unknown committed
187 188
{
  flags=null_ptr ? 0: NOT_NULL_FLAG;
unknown's avatar
unknown committed
189 190
  comment.str= (char*) "";
  comment.length=0;
unknown's avatar
unknown committed
191 192 193 194 195 196 197 198 199 200 201 202 203
}

uint Field::offset()
{
  return (uint) (ptr - (char*) table->record[0]);
}


void Field::copy_from_tmp(int row_offset)
{
  memcpy(ptr,ptr+row_offset,pack_length());
  if (null_ptr)
  {
unknown's avatar
unknown committed
204 205
    *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) |
			null_ptr[row_offset] & (uchar) null_bit);
unknown's avatar
unknown committed
206 207 208 209
  }
}


210
bool Field::send_binary(Protocol *protocol)
unknown's avatar
unknown committed
211 212
{
  char buff[MAX_FIELD_WIDTH];
213
  String tmp(buff,sizeof(buff),charset());
unknown's avatar
unknown committed
214
  val_str(&tmp,&tmp);
215
  return protocol->store(tmp.ptr(), tmp.length(), tmp.charset());
unknown's avatar
unknown committed
216 217 218 219 220
}


void Field_num::add_zerofill_and_unsigned(String &res) const
{
221 222 223 224
  if (unsigned_flag)
    res.append(" unsigned");
  if (zerofill)
    res.append(" zerofill");
unknown's avatar
unknown committed
225 226 227 228
}

void Field_num::make_field(Send_field *field)
{
unknown's avatar
unknown committed
229
  /* table_cache_key is not set for temp tables */
unknown's avatar
unknown committed
230 231
  field->db_name=table->table_cache_key ? table->table_cache_key : ""; 
  field->org_table_name=table->real_name;
unknown's avatar
unknown committed
232
  field->table_name=table_name;
unknown's avatar
unknown committed
233
  field->col_name=field->org_col_name=field_name;
234
  field->charsetnr= charset()->number;
unknown's avatar
unknown committed
235 236 237 238 239 240 241 242 243
  field->length=field_length;
  field->type=type();
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
  field->decimals=dec;
}


void Field_str::make_field(Send_field *field)
{
unknown's avatar
unknown committed
244
  /* table_cache_key is not set for temp tables */
unknown's avatar
unknown committed
245 246
  field->db_name=table->table_cache_key ? table->table_cache_key : ""; 
  field->org_table_name=table->real_name;
unknown's avatar
unknown committed
247
  field->table_name=table_name;
unknown's avatar
unknown committed
248
  field->col_name=field->org_col_name=field_name;
249
  field->charsetnr= charset()->number;
unknown's avatar
unknown committed
250 251 252 253 254 255
  field->length=field_length;
  field->type=type();
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
  field->decimals=0;
}

256

unknown's avatar
unknown committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
uint Field::fill_cache_field(CACHE_FIELD *copy)
{
  copy->str=ptr;
  copy->length=pack_length();
  copy->blob_field=0;
  if (flags & BLOB_FLAG)
  {
    copy->blob_field=(Field_blob*) this;
    copy->strip=0;
    copy->length-=table->blob_ptr_size;
    return copy->length;
  }
  else if (!zero_pack() && (type() == FIELD_TYPE_STRING && copy->length > 4 ||
			    type() == FIELD_TYPE_VAR_STRING))
    copy->strip=1;				/* Remove end space */
  else
    copy->strip=0;
  return copy->length+(int) copy->strip;
}

bool Field::get_date(TIME *ltime,bool fuzzydate)
{
  char buff[40];
unknown's avatar
unknown committed
280
  String tmp(buff,sizeof(buff),&my_charset_bin),tmp2,*res;
unknown's avatar
unknown committed
281 282 283 284 285 286 287 288 289
  if (!(res=val_str(&tmp,&tmp2)) ||
      str_to_TIME(res->ptr(),res->length(),ltime,fuzzydate) == TIMESTAMP_NONE)
    return 1;
  return 0;
}

bool Field::get_time(TIME *ltime)
{
  char buff[40];
unknown's avatar
unknown committed
290
  String tmp(buff,sizeof(buff),&my_charset_bin),tmp2,*res;
unknown's avatar
unknown committed
291
  if (!(res=val_str(&tmp,&tmp2)) ||
unknown's avatar
unknown committed
292
      str_to_time(res->ptr(),res->length(),ltime))
unknown's avatar
unknown committed
293 294 295 296 297 298 299 300 301 302 303
    return 1;
  return 0;
}


/* This is called when storing a date in a string */
void Field::store_time(TIME *ltime,timestamp_type type)
{
  char buff[25];
  switch (type)  {
  case TIMESTAMP_NONE:
unknown's avatar
unknown committed
304
    store("",0,&my_charset_bin);	// Probably an error
unknown's avatar
unknown committed
305 306 307
    break;
  case TIMESTAMP_DATE:
    sprintf(buff,"%04d-%02d-%02d", ltime->year,ltime->month,ltime->day);
unknown's avatar
unknown committed
308
    store(buff,10,&my_charset_bin);
unknown's avatar
unknown committed
309 310 311 312 313
    break;
  case TIMESTAMP_FULL:
    sprintf(buff,"%04d-%02d-%02d %02d:%02d:%02d",
	    ltime->year,ltime->month,ltime->day,
	    ltime->hour,ltime->minute,ltime->second);
unknown's avatar
unknown committed
314
    store(buff,19,&my_charset_bin);
unknown's avatar
unknown committed
315 316
    break;
  case TIMESTAMP_TIME:
unknown's avatar
unknown committed
317 318 319
  {
    ulong length= my_sprintf(buff, (buff, "%02d:%02d:%02d",
				    ltime->hour,ltime->minute,ltime->second));
unknown's avatar
unknown committed
320
    store(buff,(uint) length, &my_charset_bin);
unknown's avatar
unknown committed
321 322
    break;
  }
323
  }
unknown's avatar
unknown committed
324 325 326
}


327
bool Field::optimize_range(uint idx)
unknown's avatar
unknown committed
328
{
329
  return test(table->file->index_flags(idx) & HA_READ_NEXT);
unknown's avatar
unknown committed
330 331
}

332
/****************************************************************************
333
  Field_null, a field that always return NULL
334 335 336 337
****************************************************************************/

void Field_null::sql_type(String &res) const
{
338
  res.set_ascii("null", 4);
339 340 341
}


unknown's avatar
unknown committed
342
/****************************************************************************
unknown's avatar
unknown committed
343 344
  Functions for the Field_decimal class
  This is an number stored as a pre-space (or pre-zero) string
unknown's avatar
unknown committed
345 346 347 348 349
****************************************************************************/

void
Field_decimal::reset(void)
{
unknown's avatar
unknown committed
350
  Field_decimal::store("0",1,&my_charset_bin);
unknown's avatar
unknown committed
351 352 353 354 355
}

void Field_decimal::overflow(bool negative)
{
  uint len=field_length;
356
  char *to=ptr, filler= '9';
unknown's avatar
unknown committed
357

358
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
359
  if (negative)
unknown's avatar
unknown committed
360
  {
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    if (!unsigned_flag)
    {
      /* Put - sign as a first digit so we'll have -999..999 or 999..999 */
      *to++ = '-';
      len--;
    }
    else
    {
      filler= '0';				// Fill up with 0
      if (!zerofill)
      {
	/*
	  Handle unsigned integer without zerofill, in which case
	  the number should be of format '   0' or '   0.000'
	*/
	uint whole_part=field_length- (dec ? dec+2 : 1);
	// Fill with spaces up to the first digit
	bfill(to, whole_part, ' ');
	to+=  whole_part;
	len-= whole_part;
	// The main code will also handle the 0 before the decimal point
      }
    }
unknown's avatar
unknown committed
384
  }
385
  bfill(to, len, filler);
unknown's avatar
unknown committed
386 387 388 389 390 391
  if (dec)
    ptr[field_length-dec-1]='.';
  return;
}


unknown's avatar
unknown committed
392
int Field_decimal::store(const char *from, uint len, CHARSET_INFO *cs)
unknown's avatar
unknown committed
393
{
394
  char buff[80];
unknown's avatar
unknown committed
395
  String tmp(buff,sizeof(buff), &my_charset_bin);
396

397 398 399
  /* Convert character set if the old one is multi byte */
  if (cs->mbmaxlen > 1)
  { 
unknown's avatar
unknown committed
400
    tmp.copy(from, len, cs, &my_charset_bin);
401 402 403
    from= tmp.ptr();
    len=  tmp.length();
  }
404

405 406 407 408 409 410 411 412 413
  const char *end= from+len;
  /* The pointer where the field value starts (i.e., "where to write") */
  char *to=ptr;
  uint tmp_dec, tmp_uint;
  /*
    The sign of the number : will be 0 (means positive but sign not
    specified), '+' or '-'
  */
  char sign_char=0;
unknown's avatar
unknown committed
414
  /* The pointers where prezeros start and stop */
415
  const char *pre_zeros_from, *pre_zeros_end;
unknown's avatar
unknown committed
416
  /* The pointers where digits at the left of '.' start and stop */
417
  const char *int_digits_from, *int_digits_end;
unknown's avatar
unknown committed
418
  /* The pointers where digits at the right of '.' start and stop */
419
  const char *frac_digits_from, *frac_digits_end;
unknown's avatar
unknown committed
420
  /* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
421
  char expo_sign_char=0;
unknown's avatar
unknown committed
422
  uint exponent=0;                                // value of the exponent
423 424 425 426 427 428 429
  /*
    Pointers used when digits move from the left of the '.' to the
    right of the '.' (explained below)
  */
  const char *int_digits_tail_from;
  /* Number of 0 that need to be added at the left of the '.' (1E3: 3 zeros) */
  uint int_digits_added_zeros;
unknown's avatar
unknown committed
430 431 432 433
  /*
    Pointer used when digits move from the right of the '.' to the left
    of the '.'
  */
434
  const char *frac_digits_head_end;
unknown's avatar
unknown committed
435
  /* Number of 0 that need to be added at the right of the '.' (for 1E-3) */
436 437 438 439 440
  uint frac_digits_added_zeros;
  char *pos,*tmp_left_pos,*tmp_right_pos;
  /* Pointers that are used as limits (begin and end of the field buffer) */
  char *left_wall,*right_wall;
  char tmp_char;
unknown's avatar
unknown committed
441 442 443 444
  /*
    To remember if current_thd->cuted_fields has already been incremented,
    to do that only once
  */
445 446 447 448 449 450 451 452 453
  bool is_cuted_fields_incr=0;

  LINT_INIT(int_digits_tail_from);
  LINT_INIT(int_digits_added_zeros);
  LINT_INIT(frac_digits_head_end);
  LINT_INIT(frac_digits_added_zeros);

  /*
    There are three steps in this function :
unknown's avatar
unknown committed
454 455
    - parse the input string
    - modify the position of digits around the decimal dot '.' 
456
      according to the exponent value (if specified)
unknown's avatar
unknown committed
457
    - write the formatted number
458 459 460 461 462
  */

  if ((tmp_dec=dec))
    tmp_dec++;

unknown's avatar
unknown committed
463
  /* skip pre-space */
unknown's avatar
unknown committed
464
  while (from != end && my_isspace(&my_charset_bin,*from))
unknown's avatar
unknown committed
465
    from++;
466
  if (from == end)
unknown's avatar
unknown committed
467
  {
468
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
469
    is_cuted_fields_incr=1;
unknown's avatar
unknown committed
470
  }
471
  else if (*from == '+' || *from == '-')	// Found some sign ?
unknown's avatar
unknown committed
472
  {
473
    sign_char= *from++;
unknown's avatar
unknown committed
474
    /*
475 476 477
      We allow "+" for unsigned decimal unless defined different
      Both options allowed as one may wish not to have "+" for unsigned numbers
      because of data processing issues
478 479 480 481
    */ 
    if (unsigned_flag)  
    { 
      if (sign_char=='-')
482
      {
483
        Field_decimal::overflow(1);
unknown's avatar
unknown committed
484
        return 1;
485
      }
486
      /* 
unknown's avatar
unknown committed
487 488
	 Defining this will not store "+" for unsigned decimal type even if
	 it is passed in numeric string. This will make some tests to fail
489 490
      */	 
#ifdef DONT_ALLOW_UNSIGNED_PLUS      
491 492
      else 
        sign_char=0;
493
#endif 	
unknown's avatar
unknown committed
494
    }
495
  }
unknown's avatar
unknown committed
496

497 498 499 500
  pre_zeros_from= from;
  for (; from!=end && *from == '0'; from++) ;	// Read prezeros
  pre_zeros_end=int_digits_from=from;      
  /* Read non zero digits at the left of '.'*/
unknown's avatar
unknown committed
501
  for (; from != end && my_isdigit(&my_charset_bin, *from) ; from++) ;
502 503 504 505 506
  int_digits_end=from;
  if (from!=end && *from == '.')		// Some '.' ?
    from++;
  frac_digits_from= from;
  /* Read digits at the right of '.' */
unknown's avatar
unknown committed
507
  for (;from!=end && my_isdigit(&my_charset_bin, *from); from++) ;
508 509 510 511 512 513 514 515 516 517
  frac_digits_end=from;
  // Some exponentiation symbol ?
  if (from != end && (*from == 'e' || *from == 'E'))
  {   
    from++;
    if (from != end && (*from == '+' || *from == '-'))  // Some exponent sign ?
      expo_sign_char= *from++;
    else
      expo_sign_char= '+';
    /*
518 519 520 521
      Read digits of the exponent and compute its value.  We must care about
      'exponent' overflow, because as unsigned arithmetic is "modulo", big 
      exponents will become small (e.g. 1e4294967296 will become 1e0, and the 
      field will finally contain 1 instead of its max possible value).
522
    */
unknown's avatar
unknown committed
523
    for (;from!=end && my_isdigit(&my_charset_bin, *from); from++)
524
    {
525
      exponent=10*exponent+(*from-'0');
unknown's avatar
unknown committed
526
      if (exponent>MAX_EXPONENT)
527 528
        break;
    }
unknown's avatar
unknown committed
529
  }
530
  
unknown's avatar
unknown committed
531
  /*
532 533 534 535
    We only have to generate warnings if count_cuted_fields is set.
    This is to avoid extra checks of the number when they are not needed.
    Even if this flag is not set, it's ok to increment warnings, if
    it makes the code easer to read.
unknown's avatar
unknown committed
536
  */
537 538

  if (current_thd->count_cuted_fields)
unknown's avatar
unknown committed
539
  {
540
    // Skip end spaces
unknown's avatar
unknown committed
541
    for (;from != end && my_isspace(&my_charset_bin, *from); from++) ;
542
    if (from != end)                     // If still something left, warn
unknown's avatar
unknown committed
543
    {
544
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED); 
545
      is_cuted_fields_incr=1;
unknown's avatar
unknown committed
546
    }
547 548 549 550 551 552
  }
  
  /*
    Now "move" digits around the decimal dot according to the exponent value,
    and add necessary zeros.
    Examples :
unknown's avatar
unknown committed
553 554 555
    - 1E+3 : needs 3 more zeros at the left of '.' (int_digits_added_zeros=3)
    - 1E-3 : '1' moves at the right of '.', and 2 more zeros are needed
    between '.' and '1'
556
    - 1234.5E-3 : '234' moves at the right of '.'
unknown's avatar
unknown committed
557 558
    These moves are implemented with pointers which point at the begin
    and end of each moved segment. Examples :
559
    - 1234.5E-3 : before the code below is executed, the int_digits part is
unknown's avatar
unknown committed
560 561 562
    from '1' to '4' and the frac_digits part from '5' to '5'. After the code
    below, the int_digits part is from '1' to '1', the frac_digits_head
    part is from '2' to '4', and the frac_digits part from '5' to '5'.
563
    - 1234.5E3 : before the code below is executed, the int_digits part is
unknown's avatar
unknown committed
564 565 566 567
    from '1' to '4' and the frac_digits part from '5' to '5'. After the code
    below, the int_digits part is from '1' to '4', the int_digits_tail
    part is from '5' to '5', the frac_digits part is empty, and
    int_digits_added_zeros=2 (to make 1234500).
568 569
  */
  
570
  /* 
unknown's avatar
unknown committed
571
     Below tmp_uint cannot overflow with small enough MAX_EXPONENT setting,
572
     as int_digits_added_zeros<=exponent<4G and 
573 574
     (int_digits_end-int_digits_from)<=max_allowed_packet<=2G and
     (frac_digits_from-int_digits_tail_from)<=max_allowed_packet<=2G
575 576
  */

577 578 579 580 581 582 583 584
  if (!expo_sign_char)
    tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);
  else if (expo_sign_char == '-') 
  {
    tmp_uint=min(exponent,(uint)(int_digits_end-int_digits_from));
    frac_digits_added_zeros=exponent-tmp_uint;
    int_digits_end -= tmp_uint;
    frac_digits_head_end=int_digits_end+tmp_uint;
unknown's avatar
unknown committed
585
    tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);     
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
  }
  else // (expo_sign_char=='+') 
  {
    tmp_uint=min(exponent,(uint)(frac_digits_end-frac_digits_from));
    int_digits_added_zeros=exponent-tmp_uint;
    int_digits_tail_from=frac_digits_from;
    frac_digits_from=frac_digits_from+tmp_uint;
    /*
      We "eat" the heading zeros of the 
      int_digits.int_digits_tail.int_digits_added_zeros concatenation
      (for example 0.003e3 must become 3 and not 0003)
    */
    if (int_digits_from == int_digits_end) 
    {
      /*
	There was nothing in the int_digits part, so continue
	eating int_digits_tail zeros
      */
      for (; int_digits_tail_from != frac_digits_from &&
	     *int_digits_tail_from == '0'; int_digits_tail_from++) ;
      if (int_digits_tail_from == frac_digits_from) 
      {
	// there were only zeros in int_digits_tail too
	int_digits_added_zeros=0;
      }
unknown's avatar
unknown committed
611
    }
612 613 614
    tmp_uint= (tmp_dec+(int_digits_end-int_digits_from)+
               (uint)(frac_digits_from-int_digits_tail_from)+
               int_digits_added_zeros);
615 616 617 618 619 620 621 622 623 624
  }
  
  /*
    Now write the formated number
    
    First the digits of the int_% parts.
    Do we have enough room to write these digits ?
    If the sign is defined and '-', we need one position for it
  */

625
  if (field_length < tmp_uint + (int) (sign_char == '-')) 
626
  {
unknown's avatar
unknown committed
627
    // too big number, change to max or min number
628
    Field_decimal::overflow(sign_char == '-');
629
    return 1;
unknown's avatar
unknown committed
630
  }
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
 
  /*
    Tmp_left_pos is the position where the leftmost digit of
    the int_% parts will be written
  */
  tmp_left_pos=pos=to+(uint)(field_length-tmp_uint);
  
  // Write all digits of the int_% parts
  while (int_digits_from != int_digits_end)
    *pos++ = *int_digits_from++ ;

  if (expo_sign_char == '+')
  {    
    while (int_digits_tail_from != frac_digits_from)
      *pos++= *int_digits_tail_from++;
    while (int_digits_added_zeros-- >0)
      *pos++= '0';  
unknown's avatar
unknown committed
648
  }
649 650 651 652 653 654
  /*
    Note the position where the rightmost digit of the int_% parts has been
    written (this is to later check if the int_% parts contained nothing,
    meaning an extra 0 is needed).
  */
  tmp_right_pos=pos;
unknown's avatar
unknown committed
655 656

  /*
657 658
    Step back to the position of the leftmost digit of the int_% parts,
    to write sign and fill with zeros or blanks or prezeros.
unknown's avatar
unknown committed
659
  */
660 661 662 663 664 665 666 667 668
  pos=tmp_left_pos-1;
  if (zerofill)
  {
    left_wall=to-1;
    while (pos != left_wall)			// Fill with zeros
      *pos--='0';
  }
  else
  {
unknown's avatar
unknown committed
669
    left_wall=to+(sign_char != 0)-1;
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
    if (!expo_sign_char)	// If exponent was specified, ignore prezeros
    {
      for (;pos != left_wall && pre_zeros_from !=pre_zeros_end;
	   pre_zeros_from++)
	*pos--= '0';
    }
    if (pos == tmp_right_pos-1)
      *pos--= '0';		// no 0 has ever been written, so write one
    left_wall= to-1;
    if (sign_char && pos != left_wall)
    {
      /* Write sign if possible (it is if sign is '-') */
      *pos--= sign_char;
    }
    while (pos != left_wall)
      *pos--=' ';  //fill with blanks
  }
  
688 689 690 691 692 693 694 695
  /*
    Write digits of the frac_% parts ;
    Depending on current_thd->count_cutted_fields, we may also want
    to know if some non-zero tail of these parts will
    be truncated (for example, 0.002->0.00 will generate a warning,
    while 0.000->0.00 will not)
    (and 0E1000000000 will not, while 1E-1000000000 will)
  */
696
      
697 698 699
  pos=to+(uint)(field_length-tmp_dec);	// Calculate post to '.'
  right_wall=to+field_length;
  if (pos != right_wall) 
700 701
    *pos++='.';

702 703 704
  if (expo_sign_char == '-')
  {
    while (frac_digits_added_zeros-- > 0)
unknown's avatar
unknown committed
705
    {
706
      if (pos == right_wall) 
707
      {
708 709
        if (current_thd->count_cuted_fields && !is_cuted_fields_incr) 
          break; // Go on below to see if we lose non zero digits
unknown's avatar
unknown committed
710
        return 0;
711
      }
712 713 714 715 716 717
      *pos++='0';
    }
    while (int_digits_end != frac_digits_head_end)
    {
      tmp_char= *int_digits_end++;
      if (pos == right_wall)
unknown's avatar
unknown committed
718
      {
719 720 721
        if (tmp_char != '0')			// Losing a non zero digit ?
        {
          if (!is_cuted_fields_incr)
722
            set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
723
          return 0;
724 725
        }
        continue;
unknown's avatar
unknown committed
726
      }
727
      *pos++= tmp_char;
unknown's avatar
unknown committed
728
    }
729
  }
730

731 732 733 734
  for (;frac_digits_from!=frac_digits_end;) 
  {
    tmp_char= *frac_digits_from++;
    if (pos == right_wall)
735
    {
736
      if (tmp_char != '0')			// Losing a non zero digit ?
737
      {
738
        if (!is_cuted_fields_incr)
739
	  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
740
        return 0;
741
      }
742
      continue;
743
    }
744
    *pos++= tmp_char;
unknown's avatar
unknown committed
745
  }
746 747 748
      
  while (pos != right_wall)
   *pos++='0';			// Fill with zeros at right of '.'
unknown's avatar
unknown committed
749
  return 0;
unknown's avatar
unknown committed
750 751 752
}


unknown's avatar
unknown committed
753
int Field_decimal::store(double nr)
unknown's avatar
unknown committed
754 755 756 757
{
  if (unsigned_flag && nr < 0)
  {
    overflow(1);
758
    return 1;
unknown's avatar
unknown committed
759
  }
760
  
761 762
#ifdef HAVE_FINITE
  if (!finite(nr)) // Handle infinity as special case
763 764
  {
    overflow(nr < 0.0);
unknown's avatar
unknown committed
765
    return 1;
766
  }
767
#endif
unknown's avatar
unknown committed
768

unknown's avatar
unknown committed
769 770 771 772 773
  reg4 uint i,length;
  char fyllchar,*to;
  char buff[320];

  fyllchar = zerofill ? (char) '0' : (char) ' ';
unknown's avatar
unknown committed
774
#ifdef HAVE_SNPRINTF_
unknown's avatar
unknown committed
775 776
  buff[sizeof(buff)-1]=0;			// Safety
  snprintf(buff,sizeof(buff)-1, "%.*f",(int) dec,nr);
777
  length=(uint) strlen(buff);
unknown's avatar
unknown committed
778
#else
779
  length=(uint) my_sprintf(buff,(buff,"%.*f",dec,nr));
unknown's avatar
unknown committed
780
#endif
unknown's avatar
unknown committed
781 782 783 784

  if (length > field_length)
  {
    overflow(nr < 0.0);
785
    return 1;
unknown's avatar
unknown committed
786 787 788 789 790 791 792
  }
  else
  {
    to=ptr;
    for (i=field_length-length ; i-- > 0 ;)
      *to++ = fyllchar;
    memcpy(to,buff,length);
793
    return 0;
unknown's avatar
unknown committed
794 795 796 797
  }
}


unknown's avatar
unknown committed
798
int Field_decimal::store(longlong nr)
unknown's avatar
unknown committed
799 800 801 802
{
  if (unsigned_flag && nr < 0)
  {
    overflow(1);
803
    return 1;
unknown's avatar
unknown committed
804 805 806 807 808 809 810 811
  }
  char buff[22];
  uint length=(uint) (longlong10_to_str(nr,buff,-10)-buff);
  uint int_part=field_length- (dec  ? dec+1 : 0);

  if (length > int_part)
  {
    overflow(test(nr < 0L));			/* purecov: inspected */
812
    return 1;
unknown's avatar
unknown committed
813 814 815 816 817 818 819 820 821 822 823 824 825
  }
  else
  {
    char fyllchar = zerofill ? (char) '0' : (char) ' ';
    char *to=ptr;
    for (uint i=int_part-length ; i-- > 0 ;)
      *to++ = fyllchar;
    memcpy(to,buff,length);
    if (dec)
    {
      to[length]='.';
      bfill(to+length+1,dec,'0');
    }
826
    return 0;
unknown's avatar
unknown committed
827 828 829 830 831 832
  }
}


double Field_decimal::val_real(void)
{
833
  int not_used;
unknown's avatar
unknown committed
834
  return my_strntod(&my_charset_bin, ptr, field_length, NULL, &not_used);
unknown's avatar
unknown committed
835 836 837 838
}

longlong Field_decimal::val_int(void)
{
839
  int not_used;
unknown's avatar
unknown committed
840
  if (unsigned_flag)
unknown's avatar
unknown committed
841
    return my_strntoull(&my_charset_bin, ptr, field_length, 10, NULL,
842
			&not_used);
unknown's avatar
unknown committed
843
  else
unknown's avatar
unknown committed
844
    return my_strntoll(&my_charset_bin, ptr, field_length, 10, NULL,
845
			&not_used);
unknown's avatar
unknown committed
846 847
}

848

unknown's avatar
unknown committed
849 850 851 852 853 854
String *Field_decimal::val_str(String *val_buffer __attribute__((unused)),
			       String *val_ptr)
{
  char *str;
  for (str=ptr ; *str == ' ' ; str++) ;
  uint tmp_length=(uint) (str-ptr);
unknown's avatar
unknown committed
855
  val_ptr->set_charset(&my_charset_bin);
unknown's avatar
unknown committed
856 857 858
  if (field_length < tmp_length)		// Error in data
    val_ptr->length(0);
  else
859
    val_ptr->set_ascii((const char*) str, field_length-tmp_length);
unknown's avatar
unknown committed
860 861 862 863 864 865 866 867 868 869 870
  return val_ptr;
}

/*
** Should be able to handle at least the following fixed decimal formats:
** 5.00 , -1.0,  05,  -05, +5 with optional pre/end space
*/

int Field_decimal::cmp(const char *a_ptr,const char *b_ptr)
{
  const char *end;
871
  int swap=0;
unknown's avatar
unknown committed
872 873 874 875
  /* First remove prefixes '0', ' ', and '-' */
  for (end=a_ptr+field_length;
       a_ptr != end &&
	 (*a_ptr == *b_ptr ||
unknown's avatar
unknown committed
876
	  ((my_isspace(&my_charset_bin,*a_ptr)  || *a_ptr == '+' || 
877
            *a_ptr == '0') &&
unknown's avatar
unknown committed
878
	   (my_isspace(&my_charset_bin,*b_ptr) || *b_ptr == '+' || 
879
            *b_ptr == '0')));
880 881 882 883 884
       a_ptr++,b_ptr++)
  {
    if (*a_ptr == '-')				// If both numbers are negative
      swap= -1 ^ 1;				// Swap result      
  }
unknown's avatar
unknown committed
885 886 887
  if (a_ptr == end)
    return 0;
  if (*a_ptr == '-')
888 889
    return -1;
  else if (*b_ptr == '-')
unknown's avatar
unknown committed
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
    return 1;

  while (a_ptr != end)
  {
    if (*a_ptr++ != *b_ptr++)
      return swap ^ (a_ptr[-1] < b_ptr[-1] ? -1 : 1); // compare digits
  }
  return 0;
}


void Field_decimal::sort_string(char *to,uint length)
{
  char *str,*end;
  for (str=ptr,end=ptr+length;
       str != end &&
unknown's avatar
unknown committed
906
	 ((my_isspace(&my_charset_bin,*str) || *str == '+' ||
unknown's avatar
unknown committed
907
	   *str == '0')) ;
unknown's avatar
unknown committed
908 909 910 911 912 913 914 915 916 917
       str++)
    *to++=' ';
  if (str == end)
    return;					/* purecov: inspected */

  if (*str == '-')
  {
    *to++=1;					// Smaller than any number
    str++;
    while (str != end)
unknown's avatar
unknown committed
918
      if (my_isdigit(&my_charset_bin,*str))
unknown's avatar
unknown committed
919 920 921 922 923 924 925
	*to++= (char) ('9' - *str++);
      else
	*to++= *str++;
  }
  else memcpy(to,str,(uint) (end-str));
}

unknown's avatar
unknown committed
926

unknown's avatar
unknown committed
927 928
void Field_decimal::sql_type(String &res) const
{
unknown's avatar
unknown committed
929
  CHARSET_INFO *cs=res.charset();
unknown's avatar
unknown committed
930 931 932 933 934
  uint tmp=field_length;
  if (!unsigned_flag)
    tmp--;
  if (dec)
    tmp--;
935
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
936
			  "decimal(%d,%d)",tmp,dec));
unknown's avatar
unknown committed
937 938 939 940 941 942 943 944
  add_zerofill_and_unsigned(res);
}


/****************************************************************************
** tiny int
****************************************************************************/

unknown's avatar
unknown committed
945
int Field_tiny::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
946
{
947
  int not_used;				// We can ignore result from str2int
948
  char *end;
949
  long tmp= my_strntol(cs, from, len, 10, &end, &not_used);
unknown's avatar
unknown committed
950
  int error= 0;
unknown's avatar
unknown committed
951 952 953 954 955 956

  if (unsigned_flag)
  {
    if (tmp < 0)
    {
      tmp=0; /* purecov: inspected */
957
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);      
unknown's avatar
unknown committed
958
      error= 1;
unknown's avatar
unknown committed
959 960 961 962
    }
    else if (tmp > 255)
    {
      tmp= 255;
963
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
964
      error= 1;
unknown's avatar
unknown committed
965
    }
966
    else if (current_thd->count_cuted_fields && !test_if_int(from,len,end,cs))
967
    {
968
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
969
      error= 1;
970
    }
unknown's avatar
unknown committed
971 972 973 974 975 976
  }
  else
  {
    if (tmp < -128)
    {
      tmp= -128;
977
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
978
      error= 1;
unknown's avatar
unknown committed
979 980 981 982
    }
    else if (tmp >= 128)
    {
      tmp= 127;
983
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
984
      error= 1;
unknown's avatar
unknown committed
985
    }
986
    else if (current_thd->count_cuted_fields && !test_if_int(from,len,end,cs))
987
    {
988
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
989
      error= 1;
990
    }
unknown's avatar
unknown committed
991 992
  }
  ptr[0]= (char) tmp;
993
  return error;
unknown's avatar
unknown committed
994 995 996
}


unknown's avatar
unknown committed
997
int Field_tiny::store(double nr)
unknown's avatar
unknown committed
998
{
unknown's avatar
unknown committed
999
  int error= 0;
unknown's avatar
unknown committed
1000 1001 1002 1003 1004 1005
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0.0)
    {
      *ptr=0;
1006
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1007
      error= 1;
unknown's avatar
unknown committed
1008 1009 1010 1011
    }
    else if (nr > 255.0)
    {
      *ptr=(char) 255;
1012
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1013
      error= 1;
unknown's avatar
unknown committed
1014 1015 1016 1017 1018 1019 1020 1021 1022
    }
    else
      *ptr=(char) nr;
  }
  else
  {
    if (nr < -128.0)
    {
      *ptr= (char) -128;
1023
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1024
      error= 1;
unknown's avatar
unknown committed
1025 1026 1027 1028
    }
    else if (nr > 127.0)
    {
      *ptr=127;
1029
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1030
      error= 1;
unknown's avatar
unknown committed
1031 1032 1033 1034
    }
    else
      *ptr=(char) nr;
  }
1035
  return error;
unknown's avatar
unknown committed
1036 1037
}

unknown's avatar
unknown committed
1038
int Field_tiny::store(longlong nr)
unknown's avatar
unknown committed
1039
{
unknown's avatar
unknown committed
1040
  int error= 0;
unknown's avatar
unknown committed
1041 1042 1043 1044 1045
  if (unsigned_flag)
  {
    if (nr < 0L)
    {
      *ptr=0;
1046
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1047
      error= 1;
unknown's avatar
unknown committed
1048 1049 1050 1051
    }
    else if (nr > 255L)
    {
      *ptr= (char) 255;
1052
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1053
      error= 1;
unknown's avatar
unknown committed
1054 1055 1056 1057 1058 1059 1060 1061 1062
    }
    else
      *ptr=(char) nr;
  }
  else
  {
    if (nr < -128L)
    {
      *ptr= (char) -128;
1063
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1064
      error= 1;
unknown's avatar
unknown committed
1065 1066 1067 1068
    }
    else if (nr > 127L)
    {
      *ptr=127;
1069
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1070
      error= 1;
unknown's avatar
unknown committed
1071 1072 1073 1074
    }
    else
      *ptr=(char) nr;
  }
1075
  return error;
unknown's avatar
unknown committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
}


double Field_tiny::val_real(void)
{
  int tmp= unsigned_flag ? (int) ((uchar*) ptr)[0] :
    (int) ((signed char*) ptr)[0];
  return (double) tmp;
}

longlong Field_tiny::val_int(void)
{
  int tmp= unsigned_flag ? (int) ((uchar*) ptr)[0] :
    (int) ((signed char*) ptr)[0];
  return (longlong) tmp;
}

String *Field_tiny::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
unknown's avatar
unknown committed
1096
  CHARSET_INFO *cs= &my_charset_bin;
unknown's avatar
unknown committed
1097
  uint length;
1098 1099
  uint mlength=max(field_length+1,5*cs->mbmaxlen);
  val_buffer->alloc(mlength);
unknown's avatar
unknown committed
1100
  char *to=(char*) val_buffer->ptr();
1101

unknown's avatar
unknown committed
1102
  if (unsigned_flag)
1103 1104
    length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
					   (long) *((uchar*) ptr));
unknown's avatar
unknown committed
1105
  else
1106 1107
    length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
					   (long) *((signed char*) ptr));
1108
  
unknown's avatar
unknown committed
1109 1110 1111 1112 1113 1114
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}

1115 1116 1117 1118
bool Field_tiny::send_binary(Protocol *protocol)
{
  return protocol->store_tiny((longlong) (int8) ptr[0]);
}
unknown's avatar
unknown committed
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138

int Field_tiny::cmp(const char *a_ptr, const char *b_ptr)
{
  signed char a,b;
  a=(signed char) a_ptr[0]; b= (signed char) b_ptr[0];
  if (unsigned_flag)
    return ((uchar) a < (uchar) b) ? -1 : ((uchar) a > (uchar) b) ? 1 : 0;
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_tiny::sort_string(char *to,uint length __attribute__((unused)))
{
  if (unsigned_flag)
    *to= *ptr;
  else
    to[0] = (char) ((uchar) ptr[0] ^ (uchar) 128);	/* Revers signbit */
}

void Field_tiny::sql_type(String &res) const
{
unknown's avatar
unknown committed
1139
  CHARSET_INFO *cs=res.charset();
1140
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
1141
			  "tinyint(%d)",(int) field_length));
unknown's avatar
unknown committed
1142 1143 1144 1145
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
1146
 Field type short int (2 byte)
unknown's avatar
unknown committed
1147 1148
****************************************************************************/

unknown's avatar
unknown committed
1149
int Field_short::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
1150
{
1151
  int not_used;				// We can ignore result from str2int
1152
  char *end;
1153
  long tmp= my_strntol(cs, from, len, 10, &end, &not_used);
unknown's avatar
unknown committed
1154
  int error= 0;
1155

unknown's avatar
unknown committed
1156 1157 1158 1159 1160
  if (unsigned_flag)
  {
    if (tmp < 0)
    {
      tmp=0;
1161
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1162
      error= 1;
unknown's avatar
unknown committed
1163 1164 1165 1166
    }
    else if (tmp > (uint16) ~0)
    {
      tmp=(uint16) ~0;
1167
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1168
      error= 1;
unknown's avatar
unknown committed
1169
    }
1170
    else if (current_thd->count_cuted_fields && !test_if_int(from,len,end,cs))
1171
    {
1172
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
1173
      error= 1;
1174
    }
unknown's avatar
unknown committed
1175 1176 1177 1178 1179 1180
  }
  else
  {
    if (tmp < INT_MIN16)
    {
      tmp= INT_MIN16;
1181
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1182
      error= 1;
unknown's avatar
unknown committed
1183 1184 1185 1186
    }
    else if (tmp > INT_MAX16)
    {
      tmp=INT_MAX16;
1187
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1188
      error= 1;
unknown's avatar
unknown committed
1189
    }
1190
    else if (current_thd->count_cuted_fields && !test_if_int(from,len,end,cs))
1191
    {
1192
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
1193
      error= 1;
1194
    }
unknown's avatar
unknown committed
1195 1196 1197 1198 1199 1200 1201 1202 1203
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int2store(ptr,tmp);
  }
  else
#endif
    shortstore(ptr,(short) tmp);
1204
  return error;
unknown's avatar
unknown committed
1205 1206 1207
}


unknown's avatar
unknown committed
1208
int Field_short::store(double nr)
unknown's avatar
unknown committed
1209
{
unknown's avatar
unknown committed
1210
  int error= 0;
unknown's avatar
unknown committed
1211 1212 1213 1214 1215 1216 1217
  int16 res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
1218
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1219
      error= 1;
unknown's avatar
unknown committed
1220 1221 1222 1223
    }
    else if (nr > (double) (uint16) ~0)
    {
      res=(int16) (uint16) ~0;
1224
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1225
      error= 1;
unknown's avatar
unknown committed
1226 1227 1228 1229 1230 1231 1232 1233 1234
    }
    else
      res=(int16) (uint16) nr;
  }
  else
  {
    if (nr < (double) INT_MIN16)
    {
      res=INT_MIN16;
1235
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1236
      error= 1;
unknown's avatar
unknown committed
1237 1238 1239 1240
    }
    else if (nr > (double) INT_MAX16)
    {
      res=INT_MAX16;
1241
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1242
      error= 1;
unknown's avatar
unknown committed
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
    }
    else
      res=(int16) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int2store(ptr,res);
  }
  else
#endif
    shortstore(ptr,res);
1255
  return error;
unknown's avatar
unknown committed
1256 1257
}

unknown's avatar
unknown committed
1258
int Field_short::store(longlong nr)
unknown's avatar
unknown committed
1259
{
unknown's avatar
unknown committed
1260
  int error= 0;
unknown's avatar
unknown committed
1261 1262 1263 1264 1265 1266
  int16 res;
  if (unsigned_flag)
  {
    if (nr < 0L)
    {
      res=0;
1267
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1268
      error= 1;
unknown's avatar
unknown committed
1269 1270 1271 1272
    }
    else if (nr > (longlong) (uint16) ~0)
    {
      res=(int16) (uint16) ~0;
1273
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1274
      error= 1;
unknown's avatar
unknown committed
1275 1276 1277 1278 1279 1280 1281 1282 1283
    }
    else
      res=(int16) (uint16) nr;
  }
  else
  {
    if (nr < INT_MIN16)
    {
      res=INT_MIN16;
1284
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1285
      error= 1;
unknown's avatar
unknown committed
1286 1287 1288 1289
    }
    else if (nr > INT_MAX16)
    {
      res=INT_MAX16;
1290
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1291
      error= 1;
unknown's avatar
unknown committed
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
    }
    else
      res=(int16) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int2store(ptr,res);
  }
  else
#endif
    shortstore(ptr,res);
1304
  return error;
unknown's avatar
unknown committed
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
}


double Field_short::val_real(void)
{
  short j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint2korr(ptr);
  else
#endif
    shortget(j,ptr);
  return unsigned_flag ? (double) (unsigned short) j : (double) j;
}

longlong Field_short::val_int(void)
{
  short j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint2korr(ptr);
  else
#endif
    shortget(j,ptr);
  return unsigned_flag ? (longlong) (unsigned short) j : (longlong) j;
}

1332

unknown's avatar
unknown committed
1333 1334 1335
String *Field_short::val_str(String *val_buffer,
			     String *val_ptr __attribute__((unused)))
{
unknown's avatar
unknown committed
1336
  CHARSET_INFO *cs= &my_charset_bin;
unknown's avatar
unknown committed
1337
  uint length;
1338 1339
  uint mlength=max(field_length+1,7*cs->mbmaxlen);
  val_buffer->alloc(mlength);
unknown's avatar
unknown committed
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
  char *to=(char*) val_buffer->ptr();
  short j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint2korr(ptr);
  else
#endif
    shortget(j,ptr);

  if (unsigned_flag)
1350 1351
    length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10, 
					  (long) (uint16) j);
unknown's avatar
unknown committed
1352
  else
1353
    length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
unknown's avatar
unknown committed
1354 1355 1356 1357 1358 1359 1360
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


1361 1362 1363 1364 1365 1366
bool Field_short::send_binary(Protocol *protocol)
{
  return protocol->store_short(Field_short::val_int());
}


unknown's avatar
unknown committed
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
int Field_short::cmp(const char *a_ptr, const char *b_ptr)
{
  short a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    a=sint2korr(a_ptr);
    b=sint2korr(b_ptr);
  }
  else
#endif
  {
    shortget(a,a_ptr);
    shortget(b,b_ptr);
  }

  if (unsigned_flag)
    return ((unsigned short) a < (unsigned short) b) ? -1 :
    ((unsigned short) a > (unsigned short) b) ? 1 : 0;
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_short::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
  if (!table->db_low_byte_first)
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
unknown's avatar
unknown committed
1397
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
1398 1399 1400 1401 1402 1403 1404 1405
    to[1]   = ptr[1];
  }
  else
#endif
  {
    if (unsigned_flag)
      to[0] = ptr[1];
    else
unknown's avatar
unknown committed
1406
      to[0] = (char) (ptr[1] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
1407 1408 1409 1410 1411 1412
    to[1]   = ptr[0];
  }
}

void Field_short::sql_type(String &res) const
{
unknown's avatar
unknown committed
1413
  CHARSET_INFO *cs=res.charset();
1414
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
1415
			  "smallint(%d)",(int) field_length));
unknown's avatar
unknown committed
1416 1417 1418 1419 1420
  add_zerofill_and_unsigned(res);
}


/****************************************************************************
1421
  Field type medium int (3 byte)
unknown's avatar
unknown committed
1422 1423
****************************************************************************/

unknown's avatar
unknown committed
1424
int Field_medium::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
1425
{
1426
  int not_used;				// We can ignore result from str2int
1427
  char *end;
1428
  long tmp= my_strntol(cs, from, len, 10, &end, &not_used);
unknown's avatar
unknown committed
1429
  int error= 0;
unknown's avatar
unknown committed
1430 1431 1432 1433 1434 1435

  if (unsigned_flag)
  {
    if (tmp < 0)
    {
      tmp=0;
1436
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1437
      error= 1;
unknown's avatar
unknown committed
1438 1439 1440 1441
    }
    else if (tmp >= (long) (1L << 24))
    {
      tmp=(long) (1L << 24)-1L;
1442
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1443
      error= 1;
unknown's avatar
unknown committed
1444
    }
1445
    else if (current_thd->count_cuted_fields && !test_if_int(from,len,end,cs))
1446
    {
1447
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
1448
      error= 1;
1449
    }
unknown's avatar
unknown committed
1450 1451 1452 1453 1454 1455
  }
  else
  {
    if (tmp < INT_MIN24)
    {
      tmp= INT_MIN24;
1456
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1457
      error= 1;
unknown's avatar
unknown committed
1458 1459 1460 1461
    }
    else if (tmp > INT_MAX24)
    {
      tmp=INT_MAX24;
1462
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1463
      error= 1;
unknown's avatar
unknown committed
1464
    }
1465
    else if (current_thd->count_cuted_fields && !test_if_int(from,len,end,cs))
1466
    {
1467
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
1468
      error= 1;
1469
    }
unknown's avatar
unknown committed
1470 1471 1472
  }

  int3store(ptr,tmp);
1473
  return error;
unknown's avatar
unknown committed
1474 1475 1476
}


unknown's avatar
unknown committed
1477
int Field_medium::store(double nr)
unknown's avatar
unknown committed
1478
{
unknown's avatar
unknown committed
1479
  int error= 0;
unknown's avatar
unknown committed
1480 1481 1482 1483 1484 1485
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      int3store(ptr,0);
1486
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1487
      error= 1;
unknown's avatar
unknown committed
1488 1489 1490
    }
    else if (nr >= (double) (long) (1L << 24))
    {
unknown's avatar
unknown committed
1491
      uint32 tmp=(uint32) (1L << 24)-1L;
unknown's avatar
unknown committed
1492
      int3store(ptr,tmp);
1493
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1494
      error= 1;
unknown's avatar
unknown committed
1495 1496
    }
    else
unknown's avatar
unknown committed
1497
      int3store(ptr,(uint32) nr);
unknown's avatar
unknown committed
1498 1499 1500 1501 1502 1503 1504
  }
  else
  {
    if (nr < (double) INT_MIN24)
    {
      long tmp=(long) INT_MIN24;
      int3store(ptr,tmp);
1505
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1506
      error= 1;
unknown's avatar
unknown committed
1507 1508 1509 1510 1511
    }
    else if (nr > (double) INT_MAX24)
    {
      long tmp=(long) INT_MAX24;
      int3store(ptr,tmp);
1512
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1513
      error= 1;
unknown's avatar
unknown committed
1514 1515 1516 1517
    }
    else
      int3store(ptr,(long) nr);
  }
1518
  return error;
unknown's avatar
unknown committed
1519 1520
}

unknown's avatar
unknown committed
1521
int Field_medium::store(longlong nr)
unknown's avatar
unknown committed
1522
{
unknown's avatar
unknown committed
1523
  int error= 0;
unknown's avatar
unknown committed
1524 1525 1526 1527 1528
  if (unsigned_flag)
  {
    if (nr < 0L)
    {
      int3store(ptr,0);
1529
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1530
      error= 1;
unknown's avatar
unknown committed
1531 1532 1533 1534 1535
    }
    else if (nr >= (longlong) (long) (1L << 24))
    {
      long tmp=(long) (1L << 24)-1L;;
      int3store(ptr,tmp);
1536
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1537
      error= 1;
unknown's avatar
unknown committed
1538 1539
    }
    else
unknown's avatar
unknown committed
1540
      int3store(ptr,(uint32) nr);
unknown's avatar
unknown committed
1541 1542 1543 1544 1545 1546 1547
  }
  else
  {
    if (nr < (longlong) INT_MIN24)
    {
      long tmp=(long) INT_MIN24;
      int3store(ptr,tmp);
1548
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1549
      error= 1;
unknown's avatar
unknown committed
1550 1551 1552 1553 1554
    }
    else if (nr > (longlong) INT_MAX24)
    {
      long tmp=(long) INT_MAX24;
      int3store(ptr,tmp);
1555
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1556
      error= 1;
unknown's avatar
unknown committed
1557 1558 1559 1560
    }
    else
      int3store(ptr,(long) nr);
  }
1561
  return error;
unknown's avatar
unknown committed
1562 1563 1564 1565 1566 1567 1568 1569 1570
}


double Field_medium::val_real(void)
{
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
  return (double) j;
}

1571

unknown's avatar
unknown committed
1572 1573 1574 1575 1576 1577
longlong Field_medium::val_int(void)
{
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
  return (longlong) j;
}

1578

unknown's avatar
unknown committed
1579 1580 1581
String *Field_medium::val_str(String *val_buffer,
			      String *val_ptr __attribute__((unused)))
{
unknown's avatar
unknown committed
1582
  CHARSET_INFO *cs= &my_charset_bin;
unknown's avatar
unknown committed
1583
  uint length;
1584 1585
  uint mlength=max(field_length+1,10*cs->mbmaxlen);
  val_buffer->alloc(mlength);
unknown's avatar
unknown committed
1586 1587 1588
  char *to=(char*) val_buffer->ptr();
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);

1589
  length=(uint) cs->cset->long10_to_str(cs,to,mlength,-10,j);
unknown's avatar
unknown committed
1590 1591 1592 1593 1594 1595 1596
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer); /* purecov: inspected */
  return val_buffer;
}


1597 1598 1599 1600 1601 1602
bool Field_medium::send_binary(Protocol *protocol)
{
  return protocol->store_long(Field_medium::val_int());
}


unknown's avatar
unknown committed
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
int Field_medium::cmp(const char *a_ptr, const char *b_ptr)
{
  long a,b;
  if (unsigned_flag)
  {
    a=uint3korr(a_ptr);
    b=uint3korr(b_ptr);
  }
  else
  {
    a=sint3korr(a_ptr);
    b=sint3korr(b_ptr);
  }
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_medium::sort_string(char *to,uint length __attribute__((unused)))
{
  if (unsigned_flag)
    to[0] = ptr[2];
  else
    to[0] = (uchar) (ptr[2] ^ 128);		/* Revers signbit */
  to[1] = ptr[1];
  to[2] = ptr[0];
}


void Field_medium::sql_type(String &res) const
{
unknown's avatar
unknown committed
1632
  CHARSET_INFO *cs=res.charset();
1633
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), 
1634
			  "mediumint(%d)",(int) field_length));
unknown's avatar
unknown committed
1635 1636 1637 1638 1639 1640 1641 1642
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
** long int
****************************************************************************/


unknown's avatar
unknown committed
1643
int Field_long::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
1644
{
1645 1646
  long tmp;
  int error= 0;
1647
  char *end;
unknown's avatar
unknown committed
1648
  
1649
  tmp= cs->cset->scan(cs, from, from+len, MY_SEQ_SPACES);
unknown's avatar
unknown committed
1650 1651
  len-= tmp;
  from+= tmp;
1652
  my_errno=0;
unknown's avatar
unknown committed
1653 1654 1655 1656 1657
  if (unsigned_flag)
  {
    if (!len || *from == '-')
    {
      tmp=0;					// Set negative to 0
1658
      my_errno=ERANGE;
unknown's avatar
unknown committed
1659
      error= 1;
unknown's avatar
unknown committed
1660 1661
    }
    else
1662
      tmp=(long) my_strntoul(cs,from,len,10,&end,&error);
unknown's avatar
unknown committed
1663 1664
  }
  else
1665 1666
    tmp=my_strntol(cs,from,len,10,&end,&error);
  if (error ||
1667
      (from+len != end && current_thd->count_cuted_fields &&
1668
       !test_if_int(from,len,end,cs)))
unknown's avatar
unknown committed
1669
  {
1670
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
1671
    error= 1;
1672
  }
unknown's avatar
unknown committed
1673 1674 1675 1676 1677 1678 1679 1680
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
1681
  return error;
unknown's avatar
unknown committed
1682 1683 1684
}


unknown's avatar
unknown committed
1685
int Field_long::store(double nr)
unknown's avatar
unknown committed
1686
{
unknown's avatar
unknown committed
1687
  int error= 0;
unknown's avatar
unknown committed
1688 1689 1690 1691 1692 1693 1694
  int32 res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
1695
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1696
      error= 1;
unknown's avatar
unknown committed
1697 1698 1699 1700
    }
    else if (nr > (double) (ulong) ~0L)
    {
      res=(int32) (uint32) ~0L;
1701
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1702
      error= 1;
unknown's avatar
unknown committed
1703 1704 1705 1706 1707 1708 1709 1710 1711
    }
    else
      res=(int32) (ulong) nr;
  }
  else
  {
    if (nr < (double) INT_MIN32)
    {
      res=(int32) INT_MIN32;
1712
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1713
      error= 1;
unknown's avatar
unknown committed
1714 1715 1716 1717
    }
    else if (nr > (double) INT_MAX32)
    {
      res=(int32) INT_MAX32;
1718
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1719
      error= 1;
unknown's avatar
unknown committed
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
    }
    else
      res=(int32) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,res);
  }
  else
#endif
    longstore(ptr,res);
1732
  return error;
unknown's avatar
unknown committed
1733 1734 1735
}


unknown's avatar
unknown committed
1736
int Field_long::store(longlong nr)
unknown's avatar
unknown committed
1737
{
unknown's avatar
unknown committed
1738
  int error= 0;
unknown's avatar
unknown committed
1739 1740 1741 1742 1743 1744
  int32 res;
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
1745
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1746
      error= 1;
unknown's avatar
unknown committed
1747 1748 1749 1750
    }
    else if (nr >= (LL(1) << 32))
    {
      res=(int32) (uint32) ~0L;
1751
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1752
      error= 1;
unknown's avatar
unknown committed
1753 1754 1755 1756 1757 1758 1759 1760 1761
    }
    else
      res=(int32) (uint32) nr;
  }
  else
  {
    if (nr < (longlong) INT_MIN32)
    {
      res=(int32) INT_MIN32;
1762
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1763
      error= 1;
unknown's avatar
unknown committed
1764 1765 1766 1767
    }
    else if (nr > (longlong) INT_MAX32)
    {
      res=(int32) INT_MAX32;
1768
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1769
      error= 1;
unknown's avatar
unknown committed
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
    }
    else
      res=(int32) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,res);
  }
  else
#endif
    longstore(ptr,res);
1782
  return error;
unknown's avatar
unknown committed
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
}


double Field_long::val_real(void)
{
  int32 j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);
  return unsigned_flag ? (double) (uint32) j : (double) j;
}

longlong Field_long::val_int(void)
{
  int32 j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);
  return unsigned_flag ? (longlong) (uint32) j : (longlong) j;
}

String *Field_long::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
unknown's avatar
unknown committed
1813
  CHARSET_INFO *cs= &my_charset_bin;
unknown's avatar
unknown committed
1814
  uint length;
1815 1816
  uint mlength=max(field_length+1,12*cs->mbmaxlen);
  val_buffer->alloc(mlength);
unknown's avatar
unknown committed
1817 1818 1819 1820 1821 1822 1823 1824 1825
  char *to=(char*) val_buffer->ptr();
  int32 j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);

1826
  if (unsigned_flag)
1827
    length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32)j);
1828
  else
1829
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
unknown's avatar
unknown committed
1830 1831 1832 1833 1834 1835 1836
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


1837 1838 1839 1840 1841
bool Field_long::send_binary(Protocol *protocol)
{
  return protocol->store_long(Field_long::val_int());
}

unknown's avatar
unknown committed
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
int Field_long::cmp(const char *a_ptr, const char *b_ptr)
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    a=sint4korr(a_ptr);
    b=sint4korr(b_ptr);
  }
  else
#endif
  {
    longget(a,a_ptr);
    longget(b,b_ptr);
  }
  if (unsigned_flag)
unknown's avatar
unknown committed
1858
    return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
unknown's avatar
unknown committed
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_long::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
  if (!table->db_low_byte_first)
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
unknown's avatar
unknown committed
1870
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
    to[1]   = ptr[1];
    to[2]   = ptr[2];
    to[3]   = ptr[3];
  }
  else
#endif
  {
    if (unsigned_flag)
      to[0] = ptr[3];
    else
unknown's avatar
unknown committed
1881
      to[0] = (char) (ptr[3] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
1882 1883 1884 1885 1886 1887 1888 1889 1890
    to[1]   = ptr[2];
    to[2]   = ptr[1];
    to[3]   = ptr[0];
  }
}


void Field_long::sql_type(String &res) const
{
unknown's avatar
unknown committed
1891
  CHARSET_INFO *cs=res.charset();
1892
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
1893
			  "int(%d)",(int) field_length));
unknown's avatar
unknown committed
1894 1895 1896 1897
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
1898
 Field type longlong int (8 bytes)
unknown's avatar
unknown committed
1899 1900
****************************************************************************/

unknown's avatar
unknown committed
1901
int Field_longlong::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
1902
{
1903 1904
  longlong tmp;
  int error= 0;
1905
  char *end;
unknown's avatar
unknown committed
1906
  
1907
  tmp= cs->cset->scan(cs, from, from+len, MY_SEQ_SPACES);
1908
  len-= (uint)tmp;
unknown's avatar
unknown committed
1909
  from+= tmp;
1910
  my_errno=0;
unknown's avatar
unknown committed
1911 1912 1913 1914 1915
  if (unsigned_flag)
  {
    if (!len || *from == '-')
    {
      tmp=0;					// Set negative to 0
1916
      my_errno= ERANGE;
unknown's avatar
unknown committed
1917
      error= 1;
unknown's avatar
unknown committed
1918 1919
    }
    else
1920
      tmp=(longlong) my_strntoull(cs,from,len,10,&end,&error);
unknown's avatar
unknown committed
1921 1922
  }
  else
1923 1924
    tmp=my_strntoll(cs,from,len,10,&end,&error);
  if (error ||
1925
      (from+len != end && current_thd->count_cuted_fields &&
1926
       !test_if_int(from,len,end,cs)))
1927
  {
1928
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
1929 1930
    error= 1;
  }
unknown's avatar
unknown committed
1931 1932 1933 1934 1935 1936 1937 1938
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
1939
  return error;
unknown's avatar
unknown committed
1940 1941 1942
}


unknown's avatar
unknown committed
1943
int Field_longlong::store(double nr)
unknown's avatar
unknown committed
1944
{
unknown's avatar
unknown committed
1945
  int error= 0;
unknown's avatar
unknown committed
1946 1947 1948 1949 1950 1951 1952
  longlong res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
1953
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1954
      error= 1;
unknown's avatar
unknown committed
1955 1956 1957 1958
    }
    else if (nr >= (double) ~ (ulonglong) 0)
    {
      res= ~(longlong) 0;
1959
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1960
      error= 1;
unknown's avatar
unknown committed
1961 1962 1963 1964 1965 1966 1967 1968 1969
    }
    else
      res=(longlong) (ulonglong) nr;
  }
  else
  {
    if (nr <= (double) LONGLONG_MIN)
    {
      res=(longlong) LONGLONG_MIN;
1970
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1971
      error= 1;
unknown's avatar
unknown committed
1972 1973 1974 1975
    }
    else if (nr >= (double) LONGLONG_MAX)
    {
      res=(longlong) LONGLONG_MAX;
1976
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
1977
      error= 1;
unknown's avatar
unknown committed
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
    }
    else
      res=(longlong) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,res);
  }
  else
#endif
    longlongstore(ptr,res);
1990
  return error;
unknown's avatar
unknown committed
1991 1992 1993
}


unknown's avatar
unknown committed
1994
int Field_longlong::store(longlong nr)
unknown's avatar
unknown committed
1995 1996 1997 1998 1999 2000 2001 2002 2003
{
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,nr);
  }
  else
#endif
    longlongstore(ptr,nr);
2004
  return 0;
unknown's avatar
unknown committed
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
}


double Field_longlong::val_real(void)
{
  longlong j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    j=sint8korr(ptr);
  }
  else
#endif
    longlongget(j,ptr);
2019
  return unsigned_flag ? ulonglong2double((ulonglong) j) : (double) j;
unknown's avatar
unknown committed
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
}

longlong Field_longlong::val_int(void)
{
  longlong j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint8korr(ptr);
  else
#endif
    longlongget(j,ptr);
  return j;
}


String *Field_longlong::val_str(String *val_buffer,
				String *val_ptr __attribute__((unused)))
{
unknown's avatar
unknown committed
2038
  CHARSET_INFO *cs= &my_charset_bin;
unknown's avatar
unknown committed
2039
  uint length;
2040 2041
  uint mlength=max(field_length+1,22*cs->mbmaxlen);
  val_buffer->alloc(mlength);
unknown's avatar
unknown committed
2042 2043 2044 2045 2046 2047 2048 2049 2050
  char *to=(char*) val_buffer->ptr();
  longlong j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint8korr(ptr);
  else
#endif
    longlongget(j,ptr);

2051
  length=(uint) (cs->cset->longlong10_to_str)(cs,to,mlength,
unknown's avatar
unknown committed
2052
					unsigned_flag ? 10 : -10, j);
unknown's avatar
unknown committed
2053 2054 2055 2056 2057 2058 2059
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


2060 2061 2062 2063 2064 2065
bool Field_longlong::send_binary(Protocol *protocol)
{
  return protocol->store_longlong(Field_longlong::val_int(), unsigned_flag);
}


unknown's avatar
unknown committed
2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094
int Field_longlong::cmp(const char *a_ptr, const char *b_ptr)
{
  longlong a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    a=sint8korr(a_ptr);
    b=sint8korr(b_ptr);
  }
  else
#endif
  {
    longlongget(a,a_ptr);
    longlongget(b,b_ptr);
  }
  if (unsigned_flag)
    return ((ulonglong) a < (ulonglong) b) ? -1 :
    ((ulonglong) a > (ulonglong) b) ? 1 : 0;
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_longlong::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
  if (!table->db_low_byte_first)
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
unknown's avatar
unknown committed
2095
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
    to[1]   = ptr[1];
    to[2]   = ptr[2];
    to[3]   = ptr[3];
    to[4]   = ptr[4];
    to[5]   = ptr[5];
    to[6]   = ptr[6];
    to[7]   = ptr[7];
  }
  else
#endif
  {
    if (unsigned_flag)
      to[0] = ptr[7];
    else
unknown's avatar
unknown committed
2110
      to[0] = (char) (ptr[7] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
    to[1]   = ptr[6];
    to[2]   = ptr[5];
    to[3]   = ptr[4];
    to[4]   = ptr[3];
    to[5]   = ptr[2];
    to[6]   = ptr[1];
    to[7]   = ptr[0];
  }
}


void Field_longlong::sql_type(String &res) const
{
unknown's avatar
unknown committed
2124
  CHARSET_INFO *cs=res.charset();
2125
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2126
			  "bigint(%d)",(int) field_length));
unknown's avatar
unknown committed
2127 2128 2129 2130 2131 2132 2133
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
** single precision float
****************************************************************************/

unknown's avatar
unknown committed
2134
int Field_float::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
2135
{
2136
  int err;
2137 2138
  Field_float::store(my_strntod(cs,(char*) from,len,(char**)NULL,&err));
  if (err || current_thd->count_cuted_fields && !test_if_real(from,len,cs))
2139
  {
2140
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
2141 2142
    return 1;
  }
2143
  return (err) ? 1 : 0;
unknown's avatar
unknown committed
2144 2145 2146
}


unknown's avatar
unknown committed
2147
int Field_float::store(double nr)
unknown's avatar
unknown committed
2148 2149
{
  float j;
unknown's avatar
unknown committed
2150
  int error= 0;
unknown's avatar
unknown committed
2151 2152
  if (dec < NOT_FIXED_DEC)
    nr=floor(nr*log_10[dec]+0.5)/log_10[dec]; // To fixed point
unknown's avatar
unknown committed
2153 2154
  if (unsigned_flag && nr < 0)
  {
2155
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
2156
    nr=0;
unknown's avatar
unknown committed
2157
    error= 1;
unknown's avatar
unknown committed
2158
  }
unknown's avatar
unknown committed
2159 2160 2161
  if (nr < -FLT_MAX)
  {
    j= -FLT_MAX;
2162
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
2163
    error= 1;
unknown's avatar
unknown committed
2164 2165 2166 2167
  }
  else if (nr > FLT_MAX)
  {
    j=FLT_MAX;
2168
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
2169
    error= 1;
unknown's avatar
unknown committed
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
  }
  else
    j= (float) nr;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4store(ptr,j);
  }
  else
#endif
    memcpy_fixed(ptr,(byte*) &j,sizeof(j));
2181
  return error;
unknown's avatar
unknown committed
2182 2183 2184
}


unknown's avatar
unknown committed
2185
int Field_float::store(longlong nr)
unknown's avatar
unknown committed
2186
{
unknown's avatar
unknown committed
2187
  int error= 0;
unknown's avatar
unknown committed
2188
  float j= (float) nr;
unknown's avatar
unknown committed
2189 2190
  if (unsigned_flag && j < 0)
  {
2191
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
2192
    j=0;
unknown's avatar
unknown committed
2193
    error= 1;
unknown's avatar
unknown committed
2194
  }
unknown's avatar
unknown committed
2195 2196 2197 2198 2199 2200 2201 2202
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4store(ptr,j);
  }
  else
#endif
    memcpy_fixed(ptr,(byte*) &j,sizeof(j));
2203
  return error;
unknown's avatar
unknown committed
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
}


double Field_float::val_real(void)
{
  float j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4get(j,ptr);
  }
  else
#endif
    memcpy_fixed((byte*) &j,ptr,sizeof(j));
  return ((double) j);
}

longlong Field_float::val_int(void)
{
  float j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4get(j,ptr);
  }
  else
#endif
    memcpy_fixed((byte*) &j,ptr,sizeof(j));
  return ((longlong) j);
}


String *Field_float::val_str(String *val_buffer,
			     String *val_ptr __attribute__((unused)))
{
  float nr;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4get(nr,ptr);
  }
  else
#endif
    memcpy_fixed((byte*) &nr,ptr,sizeof(nr));

2249 2250
  uint to_length=max(field_length,70);
  val_buffer->alloc(to_length);
unknown's avatar
unknown committed
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298
  char *to=(char*) val_buffer->ptr();

  if (dec >= NOT_FIXED_DEC)
  {
    sprintf(to,"%-*.*g",(int) field_length,FLT_DIG,nr);
    to=strcend(to,' ');
    *to=0;
  }
  else
  {
#ifdef HAVE_FCONVERT
    char buff[70],*pos=buff;
    int decpt,sign,tmp_dec=dec;

    VOID(sfconvert(&nr,tmp_dec,&decpt,&sign,buff));
    if (sign)
    {
      *to++='-';
    }
    if (decpt < 0)
    {					/* val_buffer is < 0 */
      *to++='0';
      if (!tmp_dec)
	goto end;
      *to++='.';
      if (-decpt > tmp_dec)
	decpt= - (int) tmp_dec;
      tmp_dec=(uint) ((int) tmp_dec+decpt);
      while (decpt++ < 0)
	*to++='0';
    }
    else if (decpt == 0)
    {
      *to++= '0';
      if (!tmp_dec)
	goto end;
      *to++='.';
    }
    else
    {
      while (decpt-- > 0)
	*to++= *pos++;
      if (!tmp_dec)
	goto end;
      *to++='.';
    }
    while (tmp_dec--)
      *to++= *pos++;
unknown's avatar
unknown committed
2299
#else
2300 2301 2302
#ifdef HAVE_SNPRINTF
    to[to_length-1]=0;			// Safety
    snprintf(to,to_length-1,"%.*f",dec,nr);
2303
    to=strend(to);
unknown's avatar
unknown committed
2304
#else
2305
    to+= my_sprintf(to,(to,"%.*f",dec,nr));
unknown's avatar
unknown committed
2306
#endif
unknown's avatar
unknown committed
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367
#endif
  }
#ifdef HAVE_FCONVERT
 end:
#endif
  val_buffer->length((uint) (to-val_buffer->ptr()));
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


int Field_float::cmp(const char *a_ptr, const char *b_ptr)
{
  float a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4get(a,a_ptr);
    float4get(b,b_ptr);
  }
  else
#endif
  {
    memcpy_fixed(&a,a_ptr,sizeof(float));
    memcpy_fixed(&b,b_ptr,sizeof(float));
  }
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

#define FLT_EXP_DIG (sizeof(float)*8-FLT_MANT_DIG)

void Field_float::sort_string(char *to,uint length __attribute__((unused)))
{
  float nr;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4get(nr,ptr);
  }
  else
#endif
    memcpy_fixed(&nr,ptr,sizeof(float));

  uchar *tmp= (uchar*) to;
  if (nr == (float) 0.0)
  {						/* Change to zero string */
    tmp[0]=(uchar) 128;
    bzero((char*) tmp+1,sizeof(nr)-1);
  }
  else
  {
#ifdef WORDS_BIGENDIAN
    memcpy_fixed(tmp,&nr,sizeof(nr));
#else
    tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
#endif
    if (tmp[0] & 128)				/* Negative */
    {						/* make complement */
      uint i;
      for (i=0 ; i < sizeof(nr); i++)
unknown's avatar
unknown committed
2368
	tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
unknown's avatar
unknown committed
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
    }
    else
    {
      ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
		       (ushort) 32768);
      exp_part+= (ushort) 1 << (16-1-FLT_EXP_DIG);
      tmp[0]= (uchar) (exp_part >> 8);
      tmp[1]= (uchar) exp_part;
    }
  }
}


2382 2383 2384 2385 2386 2387
bool Field_float::send_binary(Protocol *protocol)
{
  return protocol->store((float) Field_float::val_real(), dec, (String*) 0);
}


unknown's avatar
unknown committed
2388 2389 2390
void Field_float::sql_type(String &res) const
{
  if (dec == NOT_FIXED_DEC)
2391
  {
2392
    res.set_ascii("float", 5);
2393
  }
unknown's avatar
unknown committed
2394
  else
2395
  {
2396
    CHARSET_INFO *cs= res.charset();
2397
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2398
			    "float(%d,%d)",(int) field_length,dec));
2399
  }
unknown's avatar
unknown committed
2400 2401 2402 2403 2404 2405 2406
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
** double precision floating point numbers
****************************************************************************/

unknown's avatar
unknown committed
2407
int Field_double::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
2408
{
2409
  int err;
2410 2411
  double j= my_strntod(cs,(char*) from,len,(char**)0,&err);
  if (err || current_thd->count_cuted_fields && !test_if_real(from,len,cs))
2412
  {
2413
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
2414
    err= 1;
2415
  }
unknown's avatar
unknown committed
2416 2417
  if (unsigned_flag && j < 0)
  {
2418
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
2419
    j=0;
2420
    err= 1;
unknown's avatar
unknown committed
2421
  }
unknown's avatar
unknown committed
2422 2423 2424 2425 2426 2427 2428 2429
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8store(ptr,j);
  }
  else
#endif
    doublestore(ptr,j);
2430
  return err;
unknown's avatar
unknown committed
2431 2432 2433
}


unknown's avatar
unknown committed
2434
int Field_double::store(double nr)
unknown's avatar
unknown committed
2435
{
unknown's avatar
unknown committed
2436
  int error= 0;
unknown's avatar
unknown committed
2437 2438
  if (dec < NOT_FIXED_DEC)
    nr=floor(nr*log_10[dec]+0.5)/log_10[dec]; // To fixed point
unknown's avatar
unknown committed
2439 2440
  if (unsigned_flag && nr < 0)
  {
2441
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
2442
    nr=0;
unknown's avatar
unknown committed
2443
    error= 1;
unknown's avatar
unknown committed
2444
  }
unknown's avatar
unknown committed
2445 2446 2447 2448 2449 2450 2451 2452
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8store(ptr,nr);
  }
  else
#endif
    doublestore(ptr,nr);
2453
  return error;
unknown's avatar
unknown committed
2454 2455 2456
}


unknown's avatar
unknown committed
2457
int Field_double::store(longlong nr)
unknown's avatar
unknown committed
2458 2459
{
  double j= (double) nr;
unknown's avatar
unknown committed
2460
  int error= 0;
unknown's avatar
unknown committed
2461 2462
  if (unsigned_flag && j < 0)
  {
2463
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
2464
    error= 1;
unknown's avatar
unknown committed
2465 2466
    j=0;
  }
unknown's avatar
unknown committed
2467 2468 2469 2470 2471 2472 2473 2474
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8store(ptr,j);
  }
  else
#endif
    doublestore(ptr,j);
2475
  return error;
unknown's avatar
unknown committed
2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571
}


double Field_double::val_real(void)
{
  double j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8get(j,ptr);
  }
  else
#endif
    doubleget(j,ptr);
  return j;
}

longlong Field_double::val_int(void)
{
  double j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8get(j,ptr);
  }
  else
#endif
    doubleget(j,ptr);
  return ((longlong) j);
}


String *Field_double::val_str(String *val_buffer,
			      String *val_ptr __attribute__((unused)))
{
  double nr;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8get(nr,ptr);
  }
  else
#endif
    doubleget(nr,ptr);

  uint to_length=max(field_length,320);
  val_buffer->alloc(to_length);
  char *to=(char*) val_buffer->ptr();

  if (dec >= NOT_FIXED_DEC)
  {
    sprintf(to,"%-*.*g",(int) field_length,DBL_DIG,nr);
    to=strcend(to,' ');
  }
  else
  {
#ifdef HAVE_FCONVERT
    char buff[320],*pos=buff;
    int decpt,sign,tmp_dec=dec;

    VOID(fconvert(nr,tmp_dec,&decpt,&sign,buff));
    if (sign)
    {
      *to++='-';
    }
    if (decpt < 0)
    {					/* val_buffer is < 0 */
      *to++='0';
      if (!tmp_dec)
	goto end;
      *to++='.';
      if (-decpt > tmp_dec)
	decpt= - (int) tmp_dec;
      tmp_dec=(uint) ((int) tmp_dec+decpt);
      while (decpt++ < 0)
	*to++='0';
    }
    else if (decpt == 0)
    {
      *to++= '0';
      if (!tmp_dec)
	goto end;
      *to++='.';
    }
    else
    {
      while (decpt-- > 0)
	*to++= *pos++;
      if (!tmp_dec)
	goto end;
      *to++='.';
    }
    while (tmp_dec--)
      *to++= *pos++;
#else
#ifdef HAVE_SNPRINTF
unknown's avatar
unknown committed
2572
    to[to_length-1]=0;			// Safety
unknown's avatar
unknown committed
2573
    snprintf(to,to_length-1,"%.*f",dec,nr);
2574
    to=strend(to);
unknown's avatar
unknown committed
2575
#else
2576
    to+= my_sprintf(to,(to,"%.*f",dec,nr));
unknown's avatar
unknown committed
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589
#endif
#endif
  }
#ifdef HAVE_FCONVERT
 end:
#endif

  val_buffer->length((uint) (to-val_buffer->ptr()));
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}

2590 2591
bool Field_double::send_binary(Protocol *protocol)
{
unknown's avatar
unknown committed
2592
  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
2593 2594
}

unknown's avatar
unknown committed
2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607

int Field_double::cmp(const char *a_ptr, const char *b_ptr)
{
  double a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8get(a,a_ptr);
    float8get(b,b_ptr);
  }
  else
#endif
  {
unknown's avatar
unknown committed
2608
/* could this ALWAYS be 2 calls to doubleget() ?? */
2609
#if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
unknown's avatar
unknown committed
2610 2611 2612
    doubleget(a, a_ptr);
    doubleget(b, b_ptr);
#else
unknown's avatar
unknown committed
2613 2614
    memcpy_fixed(&a,a_ptr,sizeof(double));
    memcpy_fixed(&b,b_ptr,sizeof(double));
unknown's avatar
unknown committed
2615
#endif
unknown's avatar
unknown committed
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634
  }
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}


#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)

/* The following should work for IEEE */

void Field_double::sort_string(char *to,uint length __attribute__((unused)))
{
  double nr;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8get(nr,ptr);
  }
  else
#endif
unknown's avatar
unknown committed
2635
/* could this ALWAYS be 2 calls to doubleget() ?? */
2636
#if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
unknown's avatar
unknown committed
2637 2638
    doubleget(nr,ptr);
#else
unknown's avatar
unknown committed
2639
    memcpy_fixed(&nr,ptr,sizeof(nr));
unknown's avatar
unknown committed
2640
#endif
unknown's avatar
unknown committed
2641 2642 2643 2644 2645 2646
  change_double_for_sort(nr, (byte*) to);
}


void Field_double::sql_type(String &res) const
{
unknown's avatar
unknown committed
2647
  CHARSET_INFO *cs=res.charset();
unknown's avatar
unknown committed
2648
  if (dec == NOT_FIXED_DEC)
2649
  {
2650
    res.set_ascii("double",6);
2651
  }
unknown's avatar
unknown committed
2652
  else
2653
  {
2654
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2655
			    "double(%d,%d)",(int) field_length,dec));
2656
  }
unknown's avatar
unknown committed
2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669
  add_zerofill_and_unsigned(res);
}


/****************************************************************************
** timestamp
** The first timestamp in the table is automaticly updated
** by handler.cc.  The form->timestamp points at the automatic timestamp.
****************************************************************************/

Field_timestamp::Field_timestamp(char *ptr_arg, uint32 len_arg,
				 enum utype unireg_check_arg,
				 const char *field_name_arg,
2670 2671 2672 2673
				 struct st_table *table_arg,
				 CHARSET_INFO *cs)
  :Field_str(ptr_arg, 19, (uchar*) 0,0,
	     unireg_check_arg, field_name_arg, table_arg, cs)
unknown's avatar
unknown committed
2674 2675 2676 2677 2678 2679 2680 2681 2682 2683
{
  if (table && !table->timestamp_field)
  {
    table->timestamp_field= this;		// Automatic timestamp
    table->time_stamp=(ulong) (ptr_arg - (char*) table->record[0])+1;
    flags|=TIMESTAMP_FLAG;
  }
}


unknown's avatar
unknown committed
2684
int Field_timestamp::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694
{
  long tmp=(long) str_to_timestamp(from,len);
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
2695
  return 0;
unknown's avatar
unknown committed
2696 2697
}

unknown's avatar
unknown committed
2698
int Field_timestamp::store(double nr)
unknown's avatar
unknown committed
2699
{
unknown's avatar
unknown committed
2700
  int error= 0;
unknown's avatar
unknown committed
2701 2702
  if (nr < 0 || nr > 99991231235959.0)
  {
unknown's avatar
unknown committed
2703
    nr= 0;					// Avoid overflow on buff
2704
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
2705
    error= 1;
unknown's avatar
unknown committed
2706
  }
unknown's avatar
unknown committed
2707
  error|= Field_timestamp::store((longlong) rint(nr));
2708
  return error;
unknown's avatar
unknown committed
2709 2710 2711 2712
}


/*
2713 2714 2715
  Convert a datetime of formats YYMMDD, YYYYMMDD or YYMMDDHHMSS to
  YYYYMMDDHHMMSS.  The high date '99991231235959' is checked before this
  function.
unknown's avatar
unknown committed
2716 2717 2718 2719
*/

static longlong fix_datetime(longlong nr)
{
2720
  current_thd->last_cuted_field= 0;
unknown's avatar
unknown committed
2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
  if (nr == LL(0) || nr >= LL(10000101000000))
    return nr;					// Normal datetime >= Year 1000
  if (nr < 101)
    goto err;
  if (nr <= (YY_PART_YEAR-1)*10000L+1231L)
    return (nr+20000000L)*1000000L;		// YYMMDD, year: 2000-2069
  if (nr < (YY_PART_YEAR)*10000L+101L)
    goto err;
  if (nr <= 991231L)
    return (nr+19000000L)*1000000L;		// YYMMDD, year: 1970-1999
  if (nr < 10000101L)
    goto err;
  if (nr <= 99991231L)
    return nr*1000000L;
  if (nr < 101000000L)
    goto err;
  if (nr <= (YY_PART_YEAR-1)*LL(10000000000)+LL(1231235959))
    return nr+LL(20000000000000);		// YYMMDDHHMMSS, 2000-2069
  if (nr <  YY_PART_YEAR*LL(10000000000)+ LL(101000000))
    goto err;
  if (nr <= LL(991231235959))
    return nr+LL(19000000000000);		// YYMMDDHHMMSS, 1970-1999

 err:
2745
    current_thd->last_cuted_field= 1;
unknown's avatar
unknown committed
2746 2747 2748 2749
  return LL(0);
}


unknown's avatar
unknown committed
2750
int Field_timestamp::store(longlong nr)
unknown's avatar
unknown committed
2751 2752 2753 2754 2755 2756 2757 2758 2759
{
  TIME l_time;
  time_t timestamp;
  long part1,part2;

  if ((nr=fix_datetime(nr)))
  {
    part1=(long) (nr/LL(1000000));
    part2=(long) (nr - (longlong) part1*LL(1000000));
unknown's avatar
unknown committed
2760
    l_time.year=  (int) (part1/10000L);  part1%=10000L;
unknown's avatar
unknown committed
2761
    l_time.month= (int) part1 / 100;
unknown's avatar
unknown committed
2762 2763
    l_time.day=	  (int) part1 % 100; 
    l_time.hour=  (int) (part2/10000L);  part2%=10000L;
unknown's avatar
unknown committed
2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776
    l_time.minute=(int) part2 / 100;
    l_time.second=(int) part2 % 100; 
    timestamp=my_gmt_sec(&l_time);
  }
  else
    timestamp=0;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,timestamp);
  }
  else
#endif
unknown's avatar
unknown committed
2777
    longstore(ptr,(uint32) timestamp);
2778 2779
  if (current_thd->last_cuted_field)    
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
2780
  return 0;
unknown's avatar
unknown committed
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809
}


double Field_timestamp::val_real(void)
{
  return (double) Field_timestamp::val_int();
}

longlong Field_timestamp::val_int(void)
{
  int part_time;
  uint32 temp;
  time_t time_arg;
  struct tm *l_time;
  longlong res;
  struct tm tm_tmp;

#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    temp=uint4korr(ptr);
  else
#endif
    longget(temp,ptr);

  if (temp == 0L)				// No time
    return(0);					/* purecov: inspected */
  time_arg=(time_t) temp;
  localtime_r(&time_arg,&tm_tmp);
  l_time=&tm_tmp;
2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823

  part_time= l_time->tm_year % 100;
  res= ((longlong) (part_time+ ((part_time < YY_PART_YEAR) ? 2000 : 1900))*
	LL(10000000000));
  part_time= l_time->tm_mon+1;
  res+= (longlong) part_time * LL(100000000);
  part_time=l_time->tm_mday;
  res+= (longlong) ((long) part_time * 1000000L);
  part_time=l_time->tm_hour;
  res+= (longlong) (part_time * 10000L);
  part_time=l_time->tm_min;
  res+= (longlong) (part_time * 100);
  part_time=l_time->tm_sec;
  return res+part_time;
unknown's avatar
unknown committed
2824 2825 2826 2827 2828 2829
}


String *Field_timestamp::val_str(String *val_buffer,
				 String *val_ptr __attribute__((unused)))
{
2830
  uint32 temp, temp2;
unknown's avatar
unknown committed
2831 2832 2833 2834 2835 2836
  time_t time_arg;
  struct tm *l_time;
  struct tm tm_tmp;

  val_buffer->alloc(field_length+1);
  char *to=(char*) val_buffer->ptr(),*end=to+field_length;
2837
  val_buffer->length(field_length);
unknown's avatar
unknown committed
2838 2839 2840 2841 2842 2843 2844 2845 2846 2847

#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    temp=uint4korr(ptr);
  else
#endif
    longget(temp,ptr);

  if (temp == 0L)
  {				      /* Zero time is "000000" */
unknown's avatar
unknown committed
2848
    val_ptr->set("0000-00-00 00:00:00", 19, &my_charset_bin);
2849
    return val_ptr;
unknown's avatar
unknown committed
2850
  }
unknown's avatar
unknown committed
2851
  val_buffer->set_charset(&my_charset_bin);	// Safety
unknown's avatar
unknown committed
2852 2853 2854
  time_arg=(time_t) temp;
  localtime_r(&time_arg,&tm_tmp);
  l_time=&tm_tmp;
2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895

  temp= l_time->tm_year % 100;
  if (temp < YY_PART_YEAR)
  {
    *to++= '2';
    *to++= '0';
  }
  else
  {
    *to++= '1';
    *to++= '9';
  }
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= '-';
  temp=l_time->tm_mon+1;
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= '-';
  temp=l_time->tm_mday;
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ' ';
  temp=l_time->tm_hour;
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ':';
  temp=l_time->tm_min;
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ':';
  temp=l_time->tm_sec;
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to= 0;
unknown's avatar
unknown committed
2896 2897 2898
  return val_buffer;
}

2899

unknown's avatar
unknown committed
2900
bool Field_timestamp::get_date(TIME *ltime, bool fuzzydate)
unknown's avatar
unknown committed
2901 2902 2903 2904 2905 2906 2907 2908 2909 2910
{
  long temp;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    temp=uint4korr(ptr);
  else
#endif
    longget(temp,ptr);
  if (temp == 0L)
  {				      /* Zero time is "000000" */
unknown's avatar
unknown committed
2911 2912
    if (!fuzzydate)
      return 1;
unknown's avatar
unknown committed
2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928
    bzero((char*) ltime,sizeof(*ltime));
  }
  else
  {
    struct tm tm_tmp;
    time_t time_arg= (time_t) temp;
    localtime_r(&time_arg,&tm_tmp);
    struct tm *start= &tm_tmp;
    ltime->year=	start->tm_year+1900;
    ltime->month=	start->tm_mon+1;
    ltime->day=		start->tm_mday;
    ltime->hour=	start->tm_hour;
    ltime->minute=	start->tm_min;
    ltime->second=	start->tm_sec;
    ltime->second_part=	0;
    ltime->neg=		0;
2929
    ltime->time_type=TIMESTAMP_FULL;
unknown's avatar
unknown committed
2930 2931 2932 2933 2934 2935
  }
  return 0;
}

bool Field_timestamp::get_time(TIME *ltime)
{
unknown's avatar
unknown committed
2936
  return Field_timestamp::get_date(ltime,0);
unknown's avatar
unknown committed
2937 2938
}

2939 2940 2941 2942 2943 2944 2945 2946 2947

bool Field_timestamp::send_binary(Protocol *protocol)
{
  TIME tm;
  Field_timestamp::get_date(&tm, 1);
  return protocol->store(&tm);
}


unknown's avatar
unknown committed
2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
int Field_timestamp::cmp(const char *a_ptr, const char *b_ptr)
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    a=sint4korr(a_ptr);
    b=sint4korr(b_ptr);
  }
  else
#endif
  {
  longget(a,a_ptr);
  longget(b,b_ptr);
  }
  return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
}

2966

unknown's avatar
unknown committed
2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989
void Field_timestamp::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
  if (!table->db_low_byte_first)
  {
    to[0] = ptr[0];
    to[1] = ptr[1];
    to[2] = ptr[2];
    to[3] = ptr[3];
  }
  else
#endif
  {
    to[0] = ptr[3];
    to[1] = ptr[2];
    to[2] = ptr[1];
    to[3] = ptr[0];
  }
}


void Field_timestamp::sql_type(String &res) const
{
2990
  res.set_ascii("timestamp", 9);
unknown's avatar
unknown committed
2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013
}


void Field_timestamp::set_time()
{
  long tmp= (long) current_thd->query_start();
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
}

/****************************************************************************
** time type
** In string context: HH:MM:SS
** In number context: HHMMSS
** Stored as a 3 byte unsigned int
****************************************************************************/

unknown's avatar
unknown committed
3014
int Field_time::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
3015 3016 3017
{
  TIME ltime;
  long tmp;
unknown's avatar
unknown committed
3018
  int error= 0;
unknown's avatar
unknown committed
3019
  if (str_to_time(from,len,&ltime))
3020
  {
unknown's avatar
unknown committed
3021
    tmp=0L;
unknown's avatar
unknown committed
3022
    error= 1;
3023
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
3024
  }
unknown's avatar
unknown committed
3025 3026 3027 3028 3029 3030 3031 3032
  else
  {
    if (ltime.month)
      ltime.day=0;
    tmp=(ltime.day*24L+ltime.hour)*10000L+(ltime.minute*100+ltime.second);
    if (tmp > 8385959)
    {
      tmp=8385959;
3033
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
3034
      error= 1;
unknown's avatar
unknown committed
3035 3036 3037 3038
    }
  }
  if (ltime.neg)
    tmp= -tmp;
3039 3040
  error |= Field_time::store((longlong) tmp);
  return error;
unknown's avatar
unknown committed
3041 3042 3043
}


unknown's avatar
unknown committed
3044
int Field_time::store(double nr)
unknown's avatar
unknown committed
3045 3046
{
  long tmp;
unknown's avatar
unknown committed
3047
  int error= 0;
unknown's avatar
unknown committed
3048 3049 3050
  if (nr > 8385959.0)
  {
    tmp=8385959L;
3051
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
3052
    error= 1;
unknown's avatar
unknown committed
3053 3054 3055 3056
  }
  else if (nr < -8385959.0)
  {
    tmp= -8385959L;
3057
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
3058
    error= 1;
unknown's avatar
unknown committed
3059 3060 3061 3062 3063 3064 3065 3066 3067
  }
  else
  {
    tmp=(long) floor(fabs(nr));			// Remove fractions
    if (nr < 0)
      tmp= -tmp;
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
    {
      tmp=0;
3068
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
3069
      error= 1;
unknown's avatar
unknown committed
3070 3071 3072
    }
  }
  int3store(ptr,tmp);
3073
  return error;
unknown's avatar
unknown committed
3074 3075 3076
}


unknown's avatar
unknown committed
3077
int Field_time::store(longlong nr)
unknown's avatar
unknown committed
3078 3079
{
  long tmp;
unknown's avatar
unknown committed
3080
  int error= 0;
unknown's avatar
unknown committed
3081 3082 3083
  if (nr > (longlong) 8385959L)
  {
    tmp=8385959L;
3084
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
3085
    error= 1;
unknown's avatar
unknown committed
3086 3087 3088 3089
  }
  else if (nr < (longlong) -8385959L)
  {
    tmp= -8385959L;
3090
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
3091
    error= 1;
unknown's avatar
unknown committed
3092 3093 3094 3095 3096 3097 3098
  }
  else
  {
    tmp=(long) nr;
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
    {
      tmp=0;
3099
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
3100
      error= 1;
unknown's avatar
unknown committed
3101 3102 3103
    }
  }
  int3store(ptr,tmp);
3104
  return error;
unknown's avatar
unknown committed
3105 3106 3107 3108 3109
}


double Field_time::val_real(void)
{
unknown's avatar
unknown committed
3110
  uint32 j= (uint32) uint3korr(ptr);
unknown's avatar
unknown committed
3111 3112 3113 3114 3115 3116 3117 3118
  return (double) j;
}

longlong Field_time::val_int(void)
{
  return (longlong) sint3korr(ptr);
}

3119 3120 3121 3122 3123 3124

/*
  This function is multi-byte safe as the result string is always of type
  my_charset_bin
*/

unknown's avatar
unknown committed
3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135
String *Field_time::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
  val_buffer->alloc(16);
  long tmp=(long) sint3korr(ptr);
  const char *sign="";
  if (tmp < 0)
  {
    tmp= -tmp;
    sign= "-";
  }
3136 3137 3138 3139 3140
  long length= my_sprintf((char*) val_buffer->ptr(),
			  ((char*) val_buffer->ptr(),"%s%02d:%02d:%02d",
			   sign,(int) (tmp/10000), (int) (tmp/100 % 100),
			   (int) (tmp % 100)));
  val_buffer->length(length);
unknown's avatar
unknown committed
3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152
  return val_buffer;
}

bool Field_time::get_time(TIME *ltime)
{
  long tmp=(long) sint3korr(ptr);
  ltime->neg=0;
  if (tmp < 0)
  {
    ltime->neg= 1;
    tmp=-tmp;
  }
unknown's avatar
unknown committed
3153
  ltime->hour=   (int) (tmp/10000);
unknown's avatar
unknown committed
3154
  tmp-=ltime->hour*10000;
unknown's avatar
unknown committed
3155 3156
  ltime->minute= (int) tmp/100;
  ltime->second= (int) tmp % 100;
unknown's avatar
unknown committed
3157 3158 3159 3160
  ltime->second_part=0;
  return 0;
}

3161 3162 3163 3164 3165 3166 3167

bool Field_time::send_binary(Protocol *protocol)
{
  TIME tm;
  Field_time::get_time(&tm);
  tm.day= tm.hour/3600;				// Move hours to days
  tm.hour-= tm.day*3600;
unknown's avatar
unknown committed
3168
  return protocol->store_time(&tm);
3169 3170 3171
}


unknown's avatar
unknown committed
3172 3173
int Field_time::cmp(const char *a_ptr, const char *b_ptr)
{
unknown's avatar
unknown committed
3174 3175 3176
  int32 a,b;
  a=(int32) sint3korr(a_ptr);
  b=(int32) sint3korr(b_ptr);
unknown's avatar
unknown committed
3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_time::sort_string(char *to,uint length __attribute__((unused)))
{
  to[0] = (uchar) (ptr[2] ^ 128);
  to[1] = ptr[1];
  to[2] = ptr[0];
}

void Field_time::sql_type(String &res) const
{
3189
  res.set_ascii("time", 4);
unknown's avatar
unknown committed
3190 3191 3192 3193 3194 3195 3196 3197
}

/****************************************************************************
** year type
** Save in a byte the year 0, 1901->2155
** Can handle 2 byte or 4 byte years!
****************************************************************************/

unknown's avatar
unknown committed
3198
int Field_year::store(const char *from, uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
3199
{
3200
  int not_used;				// We can ignore result from str2int
3201
  char *end;
3202
  long nr= my_strntol(cs, from, len, 10, &end, &not_used);
unknown's avatar
unknown committed
3203 3204 3205 3206

  if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155)
  {
    *ptr=0;
3207
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
3208
    return 1;
unknown's avatar
unknown committed
3209
  }
3210
  else if (current_thd->count_cuted_fields && !test_if_int(from,len,end,cs))
3211
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
3212 3213 3214 3215 3216 3217 3218 3219
  if (nr != 0 || len != 4)
  {
    if (nr < YY_PART_YEAR)
      nr+=100;					// 2000 - 2069
    else if (nr > 1900)
      nr-= 1900;
  }
  *ptr= (char) (unsigned char) nr;
3220
  return 0;
unknown's avatar
unknown committed
3221 3222
}

unknown's avatar
unknown committed
3223
int Field_year::store(double nr)
unknown's avatar
unknown committed
3224 3225
{
  if (nr < 0.0 || nr >= 2155.0)
3226 3227 3228 3229
  {
    (void) Field_year::store((longlong) -1);
    return 1;
  }
unknown's avatar
unknown committed
3230
  else
3231
    return Field_year::store((longlong) nr);
unknown's avatar
unknown committed
3232 3233
}

unknown's avatar
unknown committed
3234
int Field_year::store(longlong nr)
unknown's avatar
unknown committed
3235 3236 3237 3238
{
  if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155)
  {
    *ptr=0;
3239
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
3240
    return 1;
unknown's avatar
unknown committed
3241 3242 3243 3244 3245 3246 3247 3248 3249
  }
  if (nr != 0 || field_length != 4)		// 0000 -> 0; 00 -> 2000
  {
    if (nr < YY_PART_YEAR)
      nr+=100;					// 2000 - 2069
    else if (nr > 1900)
      nr-= 1900;
  }
  *ptr= (char) (unsigned char) nr;
3250
  return 0;
unknown's avatar
unknown committed
3251 3252
}

unknown's avatar
unknown committed
3253 3254 3255
bool Field_year::send_binary(Protocol *protocol)
{
  ulonglong tmp= Field_year::val_int();
3256
  return protocol->store_short(tmp);
unknown's avatar
unknown committed
3257
}
unknown's avatar
unknown committed
3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285

double Field_year::val_real(void)
{
  return (double) Field_year::val_int();
}

longlong Field_year::val_int(void)
{
  int tmp= (int) ((uchar*) ptr)[0];
  if (field_length != 4)
    tmp%=100;					// Return last 2 char
  else if (tmp)
    tmp+=1900;
  return (longlong) tmp;
}

String *Field_year::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
  val_buffer->alloc(5);
  val_buffer->length(field_length);
  char *to=(char*) val_buffer->ptr();
  sprintf(to,field_length == 2 ? "%02d" : "%04d",(int) Field_year::val_int());
  return val_buffer;
}

void Field_year::sql_type(String &res) const
{
unknown's avatar
unknown committed
3286
  CHARSET_INFO *cs=res.charset();
3287
  res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(),
3288
			  "year(%d)",(int) field_length));
unknown's avatar
unknown committed
3289 3290 3291 3292 3293 3294 3295 3296 3297 3298
}


/****************************************************************************
** date type
** In string context: YYYY-MM-DD
** In number context: YYYYMMDD
** Stored as a 4 byte unsigned int
****************************************************************************/

unknown's avatar
unknown committed
3299
int Field_date::store(const char *from, uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
3300 3301
{
  TIME l_time;
unknown's avatar
unknown committed
3302
  uint32 tmp;
unknown's avatar
unknown committed
3303
  int error= 0;
unknown's avatar
unknown committed
3304
  if (str_to_TIME(from,len,&l_time,1) == TIMESTAMP_NONE)
3305
  {
unknown's avatar
unknown committed
3306
    tmp=0;
unknown's avatar
unknown committed
3307
    error= 1;
3308
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
3309
  }
unknown's avatar
unknown committed
3310
  else
unknown's avatar
unknown committed
3311
    tmp=(uint32) l_time.year*10000L + (uint32) (l_time.month*100+l_time.day);
unknown's avatar
unknown committed
3312 3313 3314 3315 3316 3317 3318 3319
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
3320
  return error;
unknown's avatar
unknown committed
3321 3322 3323
}


unknown's avatar
unknown committed
3324
int Field_date::store(double nr)
unknown's avatar
unknown committed
3325 3326
{
  long tmp;
unknown's avatar
unknown committed
3327
  int error= 0;
unknown's avatar
unknown committed
3328 3329 3330 3331 3332
  if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
    nr=floor(nr/1000000.0);			// Timestamp to date
  if (nr < 0.0 || nr > 99991231.0)
  {
    tmp=0L;
3333
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
3334
    error= 1;
unknown's avatar
unknown committed
3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345
  }
  else
    tmp=(long) rint(nr);
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
3346
  return error;
unknown's avatar
unknown committed
3347 3348 3349
}


unknown's avatar
unknown committed
3350
int Field_date::store(longlong nr)
unknown's avatar
unknown committed
3351 3352
{
  long tmp;
unknown's avatar
unknown committed
3353
  int error= 0;
unknown's avatar
unknown committed
3354 3355 3356 3357 3358
  if (nr >= LL(19000000000000) && nr < LL(99991231235959))
    nr=nr/LL(1000000);			// Timestamp to date
  if (nr < 0 || nr > LL(99991231))
  {
    tmp=0L;
3359
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
3360
    error= 1;
unknown's avatar
unknown committed
3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371
  }
  else
    tmp=(long) nr;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
3372
  return error;
unknown's avatar
unknown committed
3373 3374 3375
}


unknown's avatar
unknown committed
3376 3377 3378 3379 3380 3381 3382 3383 3384 3385
bool Field_date::send_binary(Protocol *protocol)
{
  longlong tmp= Field_date::val_int();
  TIME tm;
  tm.year= (uint32) tmp/10000L % 10000;
  tm.month= (uint32) tmp/100 % 100;
  tm.day= (uint32) tmp % 100;
  return protocol->store_date(&tm);
}

unknown's avatar
unknown committed
3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469

double Field_date::val_real(void)
{
  int32 j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);
  return (double) (uint32) j;
}

longlong Field_date::val_int(void)
{
  int32 j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);
  return (longlong) (uint32) j;
}

String *Field_date::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
  val_buffer->alloc(field_length);
  val_buffer->length(field_length);
  int32 tmp;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    tmp=sint4korr(ptr);
  else
#endif
    longget(tmp,ptr);
  sprintf((char*) val_buffer->ptr(),"%04d-%02d-%02d",
	  (int) ((uint32) tmp/10000L % 10000), (int) ((uint32) tmp/100 % 100),
	  (int) ((uint32) tmp % 100));
  return val_buffer;
}

int Field_date::cmp(const char *a_ptr, const char *b_ptr)
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    a=sint4korr(a_ptr);
    b=sint4korr(b_ptr);
  }
  else
#endif
  {
    longget(a,a_ptr);
    longget(b,b_ptr);
  }
  return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
}


void Field_date::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
  if (!table->db_low_byte_first)
  {
    to[0] = ptr[0];
    to[1] = ptr[1];
    to[2] = ptr[2];
    to[3] = ptr[3];
  }
  else
#endif
  {
    to[0] = ptr[3];
    to[1] = ptr[2];
    to[2] = ptr[1];
    to[3] = ptr[0];
  }
}

void Field_date::sql_type(String &res) const
{
3470
  res.set_ascii("date", 4);
unknown's avatar
unknown committed
3471 3472 3473 3474 3475 3476 3477 3478
}

/****************************************************************************
** The new date type
** This is identical to the old date type, but stored on 3 bytes instead of 4
** In number context: YYYYMMDD
****************************************************************************/

unknown's avatar
unknown committed
3479
int Field_newdate::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
3480 3481 3482
{
  TIME l_time;
  long tmp;
unknown's avatar
unknown committed
3483
  int error= 0;
unknown's avatar
unknown committed
3484
  if (str_to_TIME(from,len,&l_time,1) == TIMESTAMP_NONE)
3485
  {
unknown's avatar
unknown committed
3486
    tmp=0L;
unknown's avatar
unknown committed
3487
    error= 1;
3488
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
3489
  }
unknown's avatar
unknown committed
3490 3491 3492
  else
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
  int3store(ptr,tmp);
3493
  return error;
unknown's avatar
unknown committed
3494 3495
}

unknown's avatar
unknown committed
3496
int Field_newdate::store(double nr)
unknown's avatar
unknown committed
3497 3498
{
  if (nr < 0.0 || nr > 99991231235959.0)
3499 3500
  {
    (void) Field_newdate::store((longlong) -1);
3501
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
3502 3503
    return 1;
  }
unknown's avatar
unknown committed
3504
  else
3505
    return Field_newdate::store((longlong) rint(nr));
unknown's avatar
unknown committed
3506 3507 3508
}


unknown's avatar
unknown committed
3509
int Field_newdate::store(longlong nr)
unknown's avatar
unknown committed
3510
{
unknown's avatar
unknown committed
3511
  int32 tmp;
unknown's avatar
unknown committed
3512
  int error= 0;
unknown's avatar
unknown committed
3513 3514 3515 3516 3517
  if (nr >= LL(100000000) && nr <= LL(99991231235959))
    nr=nr/LL(1000000);			// Timestamp to date
  if (nr < 0L || nr > 99991231L)
  {
    tmp=0;
3518
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
3519
    error= 1;
unknown's avatar
unknown committed
3520 3521 3522
  }
  else
  {
unknown's avatar
unknown committed
3523
    tmp=(int32) nr;
unknown's avatar
unknown committed
3524 3525 3526
    if (tmp)
    {
      if (tmp < YY_PART_YEAR*10000L)			// Fix short dates
unknown's avatar
unknown committed
3527
	tmp+= (uint32) 20000000L;
unknown's avatar
unknown committed
3528
      else if (tmp < 999999L)
unknown's avatar
unknown committed
3529
	tmp+= (uint32) 19000000L;
unknown's avatar
unknown committed
3530
    }
unknown's avatar
unknown committed
3531 3532
    uint month= (uint) ((tmp/100) % 100);
    uint day=   (uint) (tmp%100);
unknown's avatar
unknown committed
3533 3534 3535
    if (month > 12 || day > 31)
    {
      tmp=0L;					// Don't allow date to change
3536
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
3537
      error= 1;
unknown's avatar
unknown committed
3538 3539 3540 3541
    }
    else
      tmp= day + month*32 + (tmp/10000)*16*32;
  }
unknown's avatar
unknown committed
3542
  int3store(ptr,(int32) tmp);
3543
  return error;
unknown's avatar
unknown committed
3544 3545 3546 3547 3548 3549 3550 3551 3552 3553
}

void Field_newdate::store_time(TIME *ltime,timestamp_type type)
{
  long tmp;
  if (type == TIMESTAMP_DATE || type == TIMESTAMP_FULL)
    tmp=ltime->year*16*32+ltime->month*32+ltime->day;
  else
  {
    tmp=0;
3554
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
3555 3556 3557 3558
  }
  int3store(ptr,tmp);
}

unknown's avatar
unknown committed
3559 3560 3561 3562 3563 3564
bool Field_newdate::send_binary(Protocol *protocol)
{
  TIME tm;
  Field_newdate::get_date(&tm,0);
  return protocol->store_date(&tm);
}
unknown's avatar
unknown committed
3565 3566 3567 3568 3569 3570 3571 3572

double Field_newdate::val_real(void)
{
  return (double) Field_newdate::val_int();
}

longlong Field_newdate::val_int(void)
{
unknown's avatar
unknown committed
3573
  ulong j= uint3korr(ptr);
unknown's avatar
unknown committed
3574 3575 3576 3577 3578 3579 3580 3581 3582
  j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
  return (longlong) j;
}

String *Field_newdate::val_str(String *val_buffer,
			       String *val_ptr __attribute__((unused)))
{
  val_buffer->alloc(field_length);
  val_buffer->length(field_length);
unknown's avatar
unknown committed
3583
  uint32 tmp=(uint32) uint3korr(ptr);
unknown's avatar
unknown committed
3584 3585 3586 3587
  int part;
  char *pos=(char*) val_buffer->ptr()+10;

  /* Open coded to get more speed */
unknown's avatar
unknown committed
3588
  *pos--=0;					// End NULL
unknown's avatar
unknown committed
3589
  part=(int) (tmp & 31);
unknown's avatar
unknown committed
3590 3591 3592
  *pos--= (char) ('0'+part%10);
  *pos--= (char) ('0'+part/10);
  *pos--= '-';
unknown's avatar
unknown committed
3593
  part=(int) (tmp >> 5 & 15);
unknown's avatar
unknown committed
3594 3595 3596
  *pos--= (char) ('0'+part%10);
  *pos--= (char) ('0'+part/10);
  *pos--= '-';
unknown's avatar
unknown committed
3597
  part=(int) (tmp >> 9);
unknown's avatar
unknown committed
3598 3599 3600 3601
  *pos--= (char) ('0'+part%10); part/=10;
  *pos--= (char) ('0'+part%10); part/=10;
  *pos--= (char) ('0'+part%10); part/=10;
  *pos=   (char) ('0'+part);
unknown's avatar
unknown committed
3602 3603 3604 3605 3606 3607 3608
  return val_buffer;
}

bool Field_newdate::get_date(TIME *ltime,bool fuzzydate)
{
  if (is_null())
    return 1;
unknown's avatar
unknown committed
3609
  uint32 tmp=(uint32) uint3korr(ptr);
unknown's avatar
unknown committed
3610 3611 3612
  ltime->day=   tmp & 31;
  ltime->month= (tmp >> 5) & 15;
  ltime->year=  (tmp >> 9);
3613
  ltime->time_type=TIMESTAMP_DATE;
unknown's avatar
unknown committed
3614
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= 0;
unknown's avatar
unknown committed
3615
  return (!fuzzydate && (!ltime->month || !ltime->day)) ? 1 : 0;
unknown's avatar
unknown committed
3616 3617 3618 3619
}

bool Field_newdate::get_time(TIME *ltime)
{
unknown's avatar
unknown committed
3620
  return Field_newdate::get_date(ltime,0);
unknown's avatar
unknown committed
3621 3622 3623 3624
}

int Field_newdate::cmp(const char *a_ptr, const char *b_ptr)
{
unknown's avatar
unknown committed
3625 3626 3627
  uint32 a,b;
  a=(uint32) uint3korr(a_ptr);
  b=(uint32) uint3korr(b_ptr);
unknown's avatar
unknown committed
3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_newdate::sort_string(char *to,uint length __attribute__((unused)))
{
  to[0] = ptr[2];
  to[1] = ptr[1];
  to[2] = ptr[0];
}

void Field_newdate::sql_type(String &res) const
{
3640
  res.set_ascii("date", 4);
unknown's avatar
unknown committed
3641 3642 3643 3644 3645 3646 3647 3648 3649 3650
}


/****************************************************************************
** datetime type
** In string context: YYYY-MM-DD HH:MM:DD
** In number context: YYYYMMDDHHMMDD
** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
****************************************************************************/

unknown's avatar
unknown committed
3651
int Field_datetime::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
3652 3653 3654 3655 3656 3657 3658 3659 3660 3661
{
  longlong tmp=str_to_datetime(from,len,1);
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
3662
  return 0;
unknown's avatar
unknown committed
3663 3664 3665
}


unknown's avatar
unknown committed
3666
int Field_datetime::store(double nr)
unknown's avatar
unknown committed
3667
{
unknown's avatar
unknown committed
3668
  int error= 0;
unknown's avatar
unknown committed
3669 3670 3671
  if (nr < 0.0 || nr > 99991231235959.0)
  {
    nr=0.0;
3672
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
3673
    error= 1;
unknown's avatar
unknown committed
3674
  }
3675 3676
  error |= Field_datetime::store((longlong) rint(nr));
  return error;
unknown's avatar
unknown committed
3677 3678 3679
}


unknown's avatar
unknown committed
3680
int Field_datetime::store(longlong nr)
unknown's avatar
unknown committed
3681
{
unknown's avatar
unknown committed
3682
  int error= 0;
unknown's avatar
unknown committed
3683 3684 3685
  if (nr < 0 || nr > LL(99991231235959))
  {
    nr=0;
3686
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE);
unknown's avatar
unknown committed
3687
    error= 1;
unknown's avatar
unknown committed
3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698
  }
  else
    nr=fix_datetime(nr);
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,nr);
  }
  else
#endif
    longlongstore(ptr,nr);
3699 3700
  if (current_thd->last_cuted_field)    
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
3701
  return error;
unknown's avatar
unknown committed
3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712
}

void Field_datetime::store_time(TIME *ltime,timestamp_type type)
{
  longlong tmp;
  if (type == TIMESTAMP_DATE || type == TIMESTAMP_FULL)
    tmp=((ltime->year*10000L+ltime->month*100+ltime->day)*LL(1000000)+
	 (ltime->hour*10000L+ltime->minute*100+ltime->second));
  else
  {
    tmp=0;
3713
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
}

unknown's avatar
unknown committed
3725 3726 3727 3728 3729 3730 3731
bool Field_datetime::send_binary(Protocol *protocol)
{
  TIME tm;
  Field_datetime::get_date(&tm, 1);
  return protocol->store(&tm);
}

unknown's avatar
unknown committed
3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776

double Field_datetime::val_real(void)
{
  return (double) Field_datetime::val_int();
}

longlong Field_datetime::val_int(void)
{
  longlong j;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    j=sint8korr(ptr);
  else
#endif
    longlongget(j,ptr);
  return j;
}


String *Field_datetime::val_str(String *val_buffer,
				String *val_ptr __attribute__((unused)))
{
  val_buffer->alloc(field_length);
  val_buffer->length(field_length);
  ulonglong tmp;
  long part1,part2;
  char *pos;
  int part3;

#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    tmp=sint8korr(ptr);
  else
#endif
    longlongget(tmp,ptr);

  /*
    Avoid problem with slow longlong aritmetic and sprintf
  */

  part1=(long) (tmp/LL(1000000));
  part2=(long) (tmp - (ulonglong) part1*LL(1000000));

  pos=(char*) val_buffer->ptr()+19;
  *pos--=0;
unknown's avatar
unknown committed
3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795
  *pos--= (char) ('0'+(char) (part2%10)); part2/=10;
  *pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
  *pos--= ':';
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= ':';
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= (char) ('0'+(char) part3);
  *pos--= ' ';
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
  *pos--= '-';
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
  *pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
  *pos--= '-';
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos=(char) ('0'+(char) part3);
unknown's avatar
unknown committed
3796 3797 3798 3799 3800 3801
  return val_buffer;
}

bool Field_datetime::get_date(TIME *ltime,bool fuzzydate)
{
  longlong tmp=Field_datetime::val_int();
unknown's avatar
unknown committed
3802 3803 3804
  uint32 part1,part2;
  part1=(uint32) (tmp/LL(1000000));
  part2=(uint32) (tmp - (ulonglong) part1*LL(1000000));
unknown's avatar
unknown committed
3805

3806
  ltime->time_type=	TIMESTAMP_FULL;
unknown's avatar
unknown committed
3807 3808 3809 3810 3811 3812 3813 3814
  ltime->neg=		0;
  ltime->second_part=	0;
  ltime->second=	(int) (part2%100);
  ltime->minute=	(int) (part2/100%100);
  ltime->hour=		(int) (part2/10000);
  ltime->day=		(int) (part1%100);
  ltime->month= 	(int) (part1/100%100);
  ltime->year= 		(int) (part1/10000);
unknown's avatar
unknown committed
3815
  return (!fuzzydate && (!ltime->month || !ltime->day)) ? 1 : 0;
unknown's avatar
unknown committed
3816 3817 3818 3819
}

bool Field_datetime::get_time(TIME *ltime)
{
unknown's avatar
unknown committed
3820
  return Field_datetime::get_date(ltime,0);
unknown's avatar
unknown committed
3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872
}

int Field_datetime::cmp(const char *a_ptr, const char *b_ptr)
{
  longlong a,b;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    a=sint8korr(a_ptr);
    b=sint8korr(b_ptr);
  }
  else
#endif
  {
    longlongget(a,a_ptr);
    longlongget(b,b_ptr);
  }
  return ((ulonglong) a < (ulonglong) b) ? -1 :
    ((ulonglong) a > (ulonglong) b) ? 1 : 0;
}

void Field_datetime::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
  if (!table->db_low_byte_first)
  {
    to[0] = ptr[0];
    to[1] = ptr[1];
    to[2] = ptr[2];
    to[3] = ptr[3];
    to[4] = ptr[4];
    to[5] = ptr[5];
    to[6] = ptr[6];
    to[7] = ptr[7];
  }
  else
#endif
  {
    to[0] = ptr[7];
    to[1] = ptr[6];
    to[2] = ptr[5];
    to[3] = ptr[4];
    to[4] = ptr[3];
    to[5] = ptr[2];
    to[6] = ptr[1];
    to[7] = ptr[0];
  }
}


void Field_datetime::sql_type(String &res) const
{
3873
  res.set_ascii("datetime", 8);
unknown's avatar
unknown committed
3874 3875 3876 3877 3878 3879 3880 3881 3882
}

/****************************************************************************
** string type
** A string may be varchar or binary
****************************************************************************/

	/* Copy a string and fill with space */

3883 3884 3885 3886 3887
static bool use_conversion(CHARSET_INFO *cs1, CHARSET_INFO *cs2)
{
  return (cs1 != &my_charset_bin) && (cs2 != &my_charset_bin) && (cs1!=cs2);
}

unknown's avatar
unknown committed
3888
int Field_string::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
3889
{
unknown's avatar
unknown committed
3890
  int error= 0;
unknown's avatar
unknown committed
3891 3892 3893
  char buff[80];
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
  /* Convert character set if nesessary */
3894
  if (use_conversion(cs, field_charset))
unknown's avatar
unknown committed
3895 3896 3897 3898 3899
  { 
    tmpstr.copy(from, length, cs, field_charset);
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
unknown's avatar
unknown committed
3900 3901 3902 3903
  if (length <= field_length)
  {
    memcpy(ptr,from,length);
    if (length < field_length)
3904
      field_charset->cset->fill(field_charset,ptr+length,field_length-length,' ');
unknown's avatar
unknown committed
3905 3906 3907 3908 3909 3910 3911
  }
  else
  {
    memcpy(ptr,from,field_length);
    if (current_thd->count_cuted_fields)
    {						// Check if we loosed some info
      const char *end=from+length;
3912
      from+= field_length;
3913
      from+= field_charset->cset->scan(field_charset, from, end, MY_SEQ_SPACES);
3914
      if (from != end)
unknown's avatar
unknown committed
3915
      {
3916
        set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
3917
	error=1;
unknown's avatar
unknown committed
3918 3919 3920
      }
    }
  }
3921
  return error;
unknown's avatar
unknown committed
3922 3923 3924
}


unknown's avatar
unknown committed
3925
int Field_string::store(double nr)
unknown's avatar
unknown committed
3926 3927 3928 3929 3930
{
  char buff[MAX_FIELD_WIDTH],*end;
  int width=min(field_length,DBL_DIG+5);
  sprintf(buff,"%-*.*g",width,max(width-5,0),nr);
  end=strcend(buff,' ');
unknown's avatar
unknown committed
3931
  return Field_string::store(buff,(uint) (end - buff), &my_charset_bin);
unknown's avatar
unknown committed
3932 3933 3934
}


unknown's avatar
unknown committed
3935
int Field_string::store(longlong nr)
unknown's avatar
unknown committed
3936
{
unknown's avatar
unknown committed
3937 3938 3939
  char buff[64];
  int  l;
  CHARSET_INFO *cs=charset();
3940
  l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),-10,nr);
unknown's avatar
unknown committed
3941
  return Field_string::store(buff,(uint)l,cs);
unknown's avatar
unknown committed
3942 3943 3944 3945 3946
}


double Field_string::val_real(void)
{
3947
  int not_used;
unknown's avatar
unknown committed
3948
  CHARSET_INFO *cs=charset();
3949
  return my_strntod(cs,ptr,field_length,(char**)0,&not_used);
unknown's avatar
unknown committed
3950 3951 3952 3953 3954
}


longlong Field_string::val_int(void)
{
3955
  int not_used;
3956
  CHARSET_INFO *cs=charset();
3957
  return my_strntoll(cs,ptr,field_length,10,NULL,&not_used);
unknown's avatar
unknown committed
3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969
}


String *Field_string::val_str(String *val_buffer __attribute__((unused)),
			      String *val_ptr)
{
  char *end=ptr+field_length;
#ifdef WANT_TRUE_BINARY_STRINGS
  if (!binary)
#endif
    while (end > ptr && end[-1] == ' ')
      end--;
3970
  val_ptr->set((const char*) ptr,(uint) (end - ptr),field_charset);
unknown's avatar
unknown committed
3971 3972 3973 3974 3975 3976
  return val_ptr;
}


int Field_string::cmp(const char *a_ptr, const char *b_ptr)
{
unknown's avatar
unknown committed
3977
  if (field_charset->strxfrm_multiply > 1)
3978 3979 3980 3981 3982
  {
    /*
      We have to remove end space to be able to compare multi-byte-characters
      like in latin_de 'ae' and 0xe4
    */
3983
    return field_charset->coll->strnncollsp(field_charset,
unknown's avatar
unknown committed
3984 3985
				      (const uchar*) a_ptr, field_length,
				      (const uchar*) b_ptr, field_length);
3986
  }
3987
  return field_charset->coll->strnncoll(field_charset,
unknown's avatar
unknown committed
3988 3989
				  (const uchar*) a_ptr, field_length,
				  (const uchar*) b_ptr, field_length);
unknown's avatar
unknown committed
3990 3991 3992 3993
}

void Field_string::sort_string(char *to,uint length)
{
3994
  uint tmp=my_strnxfrm(field_charset,
3995 3996
                          (unsigned char *)to, length,
                          (unsigned char *) ptr, field_length);
3997 3998
  if (tmp < length)
    bzero(to + tmp, length - tmp);
unknown's avatar
unknown committed
3999 4000 4001 4002 4003
}


void Field_string::sql_type(String &res) const
{
unknown's avatar
unknown committed
4004
  CHARSET_INFO *cs=res.charset();
4005
  ulong length= cs->cset->snprintf(cs,(char*) res.ptr(),
unknown's avatar
unknown committed
4006
			    res.alloced_length(), "%s(%d)",
4007 4008 4009 4010
			    (field_length > 3 &&
			     (table->db_options_in_use &
			      HA_OPTION_PACK_RECORD) ?
			     "varchar" : "char"),
unknown's avatar
unknown committed
4011
			    (int) field_length);
4012
  res.length(length);
unknown's avatar
unknown committed
4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040
}


char *Field_string::pack(char *to, const char *from, uint max_length)
{
  const char *end=from+min(field_length,max_length);
  uchar length;
  while (end > from && end[-1] == ' ')
    end--;
  *to= length=(uchar) (end-from);
  memcpy(to+1, from, (int) length);
  return to+1+length;
}


const char *Field_string::unpack(char *to, const char *from)
{
  uint length= (uint) (uchar) *from++;
  memcpy(to, from, (int) length);
  bfill(to+length, field_length - length, ' ');
  return from+length;
}


int Field_string::pack_cmp(const char *a, const char *b, uint length)
{
  uint a_length= (uint) (uchar) *a++;
  uint b_length= (uint) (uchar) *b++;
unknown's avatar
unknown committed
4041 4042 4043
  return my_strnncoll(field_charset,
		      (const uchar*)a,a_length,
		      (const uchar*)b,b_length);
unknown's avatar
unknown committed
4044 4045 4046
}


4047 4048 4049 4050 4051 4052 4053
int Field_string::pack_cmp(const char *b, uint length)
{
  uint b_length= (uint) (uchar) *b++;
  char *end= ptr + field_length;
  while (end > ptr && end[-1] == ' ')
    end--;
  uint a_length = (uint) (end - ptr);
unknown's avatar
unknown committed
4054 4055 4056
  return my_strnncoll(field_charset,
		     (const uchar*)ptr,a_length,
		     (const uchar*)b, b_length);
4057 4058 4059
}


4060
uint Field_string::packed_col_length(const char *data_ptr, uint length)
unknown's avatar
unknown committed
4061
{
4062
  if (length > 255)
4063
    return uint2korr(data_ptr)+2;
unknown's avatar
unknown committed
4064
  else
4065
    return (uint) ((uchar) *data_ptr)+1;
unknown's avatar
unknown committed
4066 4067 4068 4069
}

uint Field_string::max_packed_col_length(uint max_length)
{
4070
  return (max_length > 255 ? 2 : 1)+max_length;
unknown's avatar
unknown committed
4071 4072 4073 4074 4075 4076 4077 4078
}


/****************************************************************************
** VARCHAR type  (Not available for the end user yet)
****************************************************************************/


unknown's avatar
unknown committed
4079
int Field_varstring::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
4080
{
unknown's avatar
unknown committed
4081
  int error= 0;
unknown's avatar
unknown committed
4082 4083 4084
  char buff[80];
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
  /* Convert character set if nesessary */
4085
  if (use_conversion(cs, field_charset))
unknown's avatar
unknown committed
4086 4087 4088 4089 4090
  { 
    tmpstr.copy(from, length, cs, field_charset);
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
4091
  if (length > field_length)
unknown's avatar
unknown committed
4092 4093
  {
    length=field_length;
4094
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
4095
    error= 1;
unknown's avatar
unknown committed
4096
  }
4097 4098
  memcpy(ptr+2,from,length);
  int2store(ptr, length);
4099
  return error;
unknown's avatar
unknown committed
4100 4101 4102
}


unknown's avatar
unknown committed
4103
int Field_varstring::store(double nr)
unknown's avatar
unknown committed
4104 4105 4106 4107 4108
{
  char buff[MAX_FIELD_WIDTH],*end;
  int width=min(field_length,DBL_DIG+5);
  sprintf(buff,"%-*.*g",width,max(width-5,0),nr);
  end=strcend(buff,' ');
unknown's avatar
unknown committed
4109
  return Field_varstring::store(buff,(uint) (end - buff), &my_charset_bin);
unknown's avatar
unknown committed
4110 4111 4112
}


unknown's avatar
unknown committed
4113
int Field_varstring::store(longlong nr)
unknown's avatar
unknown committed
4114
{
unknown's avatar
unknown committed
4115 4116 4117
  char buff[64];
  int  l;
  CHARSET_INFO *cs=charset();
4118
  l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),-10,nr);
unknown's avatar
unknown committed
4119
  return Field_varstring::store(buff,(uint)l,cs);
unknown's avatar
unknown committed
4120 4121 4122 4123 4124
}


double Field_varstring::val_real(void)
{
4125
  int not_used;
unknown's avatar
unknown committed
4126
  uint length=uint2korr(ptr)+2;
unknown's avatar
unknown committed
4127
  CHARSET_INFO *cs=charset();
4128
  return my_strntod(cs,ptr+2,length,(char**)0, &not_used);
unknown's avatar
unknown committed
4129 4130 4131 4132 4133
}


longlong Field_varstring::val_int(void)
{
4134
  int not_used;
unknown's avatar
unknown committed
4135
  uint length=uint2korr(ptr)+2;
4136
  CHARSET_INFO *cs=charset();
4137
  return my_strntoll(cs,ptr+2,length,10,NULL, &not_used);
unknown's avatar
unknown committed
4138 4139 4140 4141 4142 4143 4144
}


String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
				 String *val_ptr)
{
  uint length=uint2korr(ptr);
4145
  val_ptr->set((const char*) ptr+2,length,field_charset);
unknown's avatar
unknown committed
4146 4147 4148 4149 4150 4151 4152 4153 4154
  return val_ptr;
}


int Field_varstring::cmp(const char *a_ptr, const char *b_ptr)
{
  uint a_length=uint2korr(a_ptr);
  uint b_length=uint2korr(b_ptr);
  int diff;
4155
  diff=my_strnncoll(field_charset,
unknown's avatar
unknown committed
4156 4157
		      (const uchar*)a_ptr+2,min(a_length,b_length),
		      (const uchar*)b_ptr+2,min(a_length,b_length));
unknown's avatar
unknown committed
4158 4159 4160 4161 4162 4163
  return diff ? diff : (int) (a_length - b_length);
}

void Field_varstring::sort_string(char *to,uint length)
{
  uint tot_length=uint2korr(ptr);
4164
  tot_length=my_strnxfrm(field_charset,
4165 4166
                             (unsigned char *) to, length,
                             (unsigned char *)ptr+2, tot_length);
unknown's avatar
unknown committed
4167 4168 4169 4170 4171 4172 4173
  if (tot_length < length)
    bzero(to+tot_length,length-tot_length);
}


void Field_varstring::sql_type(String &res) const
{
unknown's avatar
unknown committed
4174
  CHARSET_INFO *cs=res.charset();
4175
  ulong length= cs->cset->snprintf(cs,(char*) res.ptr(),
unknown's avatar
unknown committed
4176 4177
			     res.alloced_length(),"varchar(%u)",
			     field_length);
4178
  res.length(length);
unknown's avatar
unknown committed
4179 4180 4181 4182
}

char *Field_varstring::pack(char *to, const char *from, uint max_length)
{
4183
  uint length=uint2korr(from);
unknown's avatar
unknown committed
4184 4185
  if (length > max_length)
    length=max_length;
unknown's avatar
unknown committed
4186
  *to++= (char) (length & 255);
unknown's avatar
unknown committed
4187
  if (max_length > 255)
unknown's avatar
unknown committed
4188
    *to++= (char) (length >> 8);
unknown's avatar
unknown committed
4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228
  if (length)
    memcpy(to, from+2, length);
  return to+length;
}


const char *Field_varstring::unpack(char *to, const char *from)
{
  uint length;
  if (field_length > 255)
  {
    length= (uint) (uchar) (*to= *from++);
    to[1]=0;
  }
  else
  {
    length=uint2korr(from);
    to[0] = *from++;
    to[1] = *from++;
  }
  if (length)
    memcpy(to+2, from, length);
  return from+length;
}


int Field_varstring::pack_cmp(const char *a, const char *b, uint key_length)
{
  uint a_length;
  uint b_length;
  if (key_length > 255)
  {
    a_length=uint2korr(a); a+=2;
    b_length=uint2korr(b); b+=2;
  }
  else
  {
    a_length= (uint) (uchar) *a++;
    b_length= (uint) (uchar) *b++;
  }
unknown's avatar
unknown committed
4229 4230 4231
  return my_strnncoll(field_charset,
		     (const uchar *)a,a_length,
		     (const uchar *)b,b_length);
unknown's avatar
unknown committed
4232 4233
}

4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246
int Field_varstring::pack_cmp(const char *b, uint key_length)
{
  char *a=ptr+2;
  uint a_length=uint2korr(ptr);
  uint b_length;
  if (key_length > 255)
  {
    b_length=uint2korr(b); b+=2;
  }
  else
  {
    b_length= (uint) (uchar) *b++;
  }
unknown's avatar
unknown committed
4247 4248 4249
  return my_strnncoll(field_charset,
		     (const uchar *)a,a_length,
		     (const uchar *)b,b_length);
4250 4251
}

4252
uint Field_varstring::packed_col_length(const char *data_ptr, uint length)
unknown's avatar
unknown committed
4253
{
4254
  if (length > 255)
4255
    return uint2korr(data_ptr)+2;
unknown's avatar
unknown committed
4256
  else
4257
    return (uint) ((uchar) *data_ptr)+1;
unknown's avatar
unknown committed
4258 4259 4260 4261
}

uint Field_varstring::max_packed_col_length(uint max_length)
{
4262
  return (max_length > 255 ? 2 : 1)+max_length;
unknown's avatar
unknown committed
4263 4264
}

4265 4266
void Field_varstring::get_key_image(char *buff, uint length, CHARSET_INFO *cs,
				    imagetype type)
4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279
{
  length-= HA_KEY_BLOB_LENGTH;
  uint f_length=uint2korr(ptr);
  if (f_length > length)
    f_length= length;
  int2store(buff,length);
  memcpy(buff+2,ptr+2,length);
#ifdef HAVE_purify
  if (f_length < length)
    bzero(buff+2+f_length, (length-f_length));
#endif
}

unknown's avatar
unknown committed
4280
void Field_varstring::set_key_image(char *buff,uint length, CHARSET_INFO *cs)
4281 4282
{
  length=uint2korr(buff);			// Real length is here
unknown's avatar
unknown committed
4283
  (void) Field_varstring::store(buff+2, length, cs);
4284 4285 4286 4287
}



unknown's avatar
unknown committed
4288 4289 4290 4291 4292 4293
/****************************************************************************
** blob type
** A blob is saved as a length and a pointer. The length is stored in the
** packlength slot and may be from 1-4.
****************************************************************************/

unknown's avatar
unknown committed
4294
Field_blob::Field_blob(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
4295 4296
		       enum utype unireg_check_arg, const char *field_name_arg,
		       struct st_table *table_arg,uint blob_pack_length,
4297
		       CHARSET_INFO *cs)
unknown's avatar
unknown committed
4298 4299
  :Field_str(ptr_arg, (1L << min(blob_pack_length,3)*8)-1L,
	     null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
4300
	     table_arg, cs),
unknown's avatar
unknown committed
4301
   geom_flag(true), packlength(blob_pack_length)
unknown's avatar
unknown committed
4302 4303 4304 4305 4306 4307 4308
{
  flags|= BLOB_FLAG;
  if (table)
    table->blob_fields++;
}


unknown's avatar
unknown committed
4309
void Field_blob::store_length(uint32 number)
unknown's avatar
unknown committed
4310 4311 4312 4313 4314 4315
{
  switch (packlength) {
  case 1:
    if (number > 255)
    {
      number=255;
4316
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
4317 4318 4319 4320 4321 4322 4323
    }
    ptr[0]= (uchar) number;
    break;
  case 2:
    if (number > (uint16) ~0)
    {
      number= (uint16) ~0;
4324
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335
    }
#ifdef WORDS_BIGENDIAN
    if (table->db_low_byte_first)
    {
      int2store(ptr,(unsigned short) number);
    }
    else
#endif
      shortstore(ptr,(unsigned short) number);
    break;
  case 3:
unknown's avatar
unknown committed
4336
    if (number > (uint32) (1L << 24))
unknown's avatar
unknown committed
4337
    {
unknown's avatar
unknown committed
4338
      number= (uint32) (1L << 24)-1L;
4339
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355
    }
    int3store(ptr,number);
    break;
  case 4:
#ifdef WORDS_BIGENDIAN
    if (table->db_low_byte_first)
    {
      int4store(ptr,number);
    }
    else
#endif
      longstore(ptr,number);
  }
}


unknown's avatar
unknown committed
4356
uint32 Field_blob::get_length(const char *pos)
unknown's avatar
unknown committed
4357 4358 4359
{
  switch (packlength) {
  case 1:
unknown's avatar
unknown committed
4360
    return (uint32) (uchar) pos[0];
unknown's avatar
unknown committed
4361 4362 4363 4364 4365 4366 4367 4368 4369
  case 2:
    {
      uint16 tmp;
#ifdef WORDS_BIGENDIAN
      if (table->db_low_byte_first)
	tmp=sint2korr(pos);
      else
#endif
	shortget(tmp,pos);
unknown's avatar
unknown committed
4370
      return (uint32) tmp;
unknown's avatar
unknown committed
4371 4372
    }
  case 3:
unknown's avatar
unknown committed
4373
    return (uint32) uint3korr(pos);
unknown's avatar
unknown committed
4374 4375 4376 4377 4378 4379 4380 4381 4382
  case 4:
    {
      uint32 tmp;
#ifdef WORDS_BIGENDIAN
      if (table->db_low_byte_first)
	tmp=uint4korr(pos);
      else
#endif
	longget(tmp,pos);
unknown's avatar
unknown committed
4383
      return (uint32) tmp;
unknown's avatar
unknown committed
4384 4385 4386 4387 4388 4389
    }
  }
  return 0;					// Impossible
}


unknown's avatar
unknown committed
4390
int Field_blob::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
4391
{
unknown's avatar
unknown committed
4392
  if (!length)
unknown's avatar
unknown committed
4393 4394 4395 4396 4397
  {
    bzero(ptr,Field_blob::pack_length());
  }
  else
  {
unknown's avatar
unknown committed
4398 4399 4400
    char buff[80];
    String tmpstr(buff,sizeof(buff), &my_charset_bin);
    /* Convert character set if nesessary */
4401
    if (use_conversion(cs, field_charset))
unknown's avatar
unknown committed
4402 4403 4404 4405 4406
    { 
      tmpstr.copy(from, length, cs, field_charset);
      from= tmpstr.ptr();
      length=  tmpstr.length();
    }
unknown's avatar
unknown committed
4407 4408
    Field_blob::store_length(length);
    if (table->copy_blobs || length <= MAX_FIELD_WIDTH)
unknown's avatar
unknown committed
4409
    {						// Must make a copy
unknown's avatar
unknown committed
4410
      value.copy(from,length,charset());
unknown's avatar
unknown committed
4411 4412 4413 4414
      from=value.ptr();
    }
    bmove(ptr+packlength,(char*) &from,sizeof(char*));
  }
4415
  return 0;
unknown's avatar
unknown committed
4416 4417 4418
}


unknown's avatar
unknown committed
4419
int Field_blob::store(double nr)
unknown's avatar
unknown committed
4420
{
4421 4422 4423
  CHARSET_INFO *cs=charset();
  value.set(nr, 2, cs);
  return Field_blob::store(value.ptr(),(uint) value.length(), cs);
unknown's avatar
unknown committed
4424 4425 4426
}


unknown's avatar
unknown committed
4427
int Field_blob::store(longlong nr)
unknown's avatar
unknown committed
4428
{
4429 4430 4431
  CHARSET_INFO *cs=charset();
  value.set(nr, cs);
  return Field_blob::store(value.ptr(), (uint) value.length(), cs);
unknown's avatar
unknown committed
4432 4433 4434 4435 4436
}


double Field_blob::val_real(void)
{
4437
  int not_used;
unknown's avatar
unknown committed
4438 4439 4440 4441
  char *blob;
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
    return 0.0;
unknown's avatar
unknown committed
4442
  uint32 length=get_length(ptr);
unknown's avatar
unknown committed
4443
  CHARSET_INFO *cs=charset();
4444
  return my_strntod(cs,blob,length,(char**)0, &not_used);
unknown's avatar
unknown committed
4445 4446 4447 4448 4449
}


longlong Field_blob::val_int(void)
{
4450
  int not_used;
unknown's avatar
unknown committed
4451 4452 4453 4454
  char *blob;
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
    return 0;
unknown's avatar
unknown committed
4455
  uint32 length=get_length(ptr);
4456
  return my_strntoll(charset(),blob,length,10,NULL,&not_used);
unknown's avatar
unknown committed
4457 4458 4459 4460 4461 4462 4463 4464 4465
}


String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
			    String *val_ptr)
{
  char *blob;
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
4466
    val_ptr->set("",0,charset());	// A bit safer than ->length(0)
unknown's avatar
unknown committed
4467
  else
4468
    val_ptr->set((const char*) blob,get_length(ptr),charset());
unknown's avatar
unknown committed
4469 4470 4471 4472
  return val_ptr;
}


unknown's avatar
unknown committed
4473 4474
int Field_blob::cmp(const char *a,uint32 a_length, const char *b,
		    uint32 b_length)
unknown's avatar
unknown committed
4475
{
4476 4477 4478
  int diff=my_strnncoll(field_charset,
			(const uchar*)a,min(a_length,b_length),
			(const uchar*)b,min(a_length,b_length));
unknown's avatar
unknown committed
4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505
  return diff ? diff : (int) (a_length - b_length);
}


int Field_blob::cmp(const char *a_ptr, const char *b_ptr)
{
  char *blob1,*blob2;
  memcpy_fixed(&blob1,a_ptr+packlength,sizeof(char*));
  memcpy_fixed(&blob2,b_ptr+packlength,sizeof(char*));
  return Field_blob::cmp(blob1,get_length(a_ptr),
			 blob2,get_length(b_ptr));
}


int Field_blob::cmp_offset(uint row_offset)
{
  return Field_blob::cmp(ptr,ptr+row_offset);
}


int Field_blob::cmp_binary_offset(uint row_offset)
{
  return cmp_binary(ptr, ptr+row_offset);
}


int Field_blob::cmp_binary(const char *a_ptr, const char *b_ptr,
unknown's avatar
unknown committed
4506
			   uint32 max_length)
unknown's avatar
unknown committed
4507 4508 4509
{
  char *a,*b;
  uint diff;
unknown's avatar
unknown committed
4510
  uint32 a_length,b_length;
unknown's avatar
unknown committed
4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525
  memcpy_fixed(&a,a_ptr+packlength,sizeof(char*));
  memcpy_fixed(&b,b_ptr+packlength,sizeof(char*));
  a_length=get_length(a_ptr);
  if (a_length > max_length)
    a_length=max_length;
  b_length=get_length(b_ptr);
  if (b_length > max_length)
    b_length=max_length;
  diff=memcmp(a,b,min(a_length,b_length));
  return diff ? diff : (int) (a_length - b_length);
}


/* The following is used only when comparing a key */

4526
void Field_blob::get_key_image(char *buff,uint length,
4527
			       CHARSET_INFO *cs, imagetype type)
unknown's avatar
unknown committed
4528
{
unknown's avatar
unknown committed
4529 4530
  length-= HA_KEY_BLOB_LENGTH;
  uint32 blob_length= get_length(ptr);
unknown's avatar
unknown committed
4531
  char *blob;
unknown's avatar
unknown committed
4532 4533

  if (type == itMBR)
unknown's avatar
unknown committed
4534
  {
unknown's avatar
unknown committed
4535
    if (!blob_length)
unknown's avatar
unknown committed
4536
      return;
unknown's avatar
unknown committed
4537
    get_ptr(&blob);
unknown's avatar
unknown committed
4538 4539 4540

    MBR mbr;
    Geometry gobj;
unknown's avatar
unknown committed
4541
    gobj.create_from_wkb(blob + SRID_SIZE, blob_length - SRID_SIZE);
unknown's avatar
unknown committed
4542 4543 4544 4545 4546 4547 4548 4549
    gobj.get_mbr(&mbr);
    float8store(buff,    mbr.xmin);
    float8store(buff+8,  mbr.xmax);
    float8store(buff+16, mbr.ymin);
    float8store(buff+24, mbr.ymax);
    return;
  }

unknown's avatar
unknown committed
4550
  if ((uint32) length > blob_length)
unknown's avatar
unknown committed
4551
  {
4552 4553 4554 4555
    /*
      Must clear this as we do a memcmp in opt_range.cc to detect
      identical keys
    */
unknown's avatar
unknown committed
4556
    bzero(buff+2+blob_length, (length-blob_length));
unknown's avatar
unknown committed
4557
    length=(uint) blob_length;
unknown's avatar
unknown committed
4558
  }
unknown's avatar
unknown committed
4559 4560 4561 4562 4563
  int2store(buff,length);
  get_ptr(&blob);
  memcpy(buff+2,blob,length);
}

unknown's avatar
unknown committed
4564
void Field_blob::set_key_image(char *buff,uint length, CHARSET_INFO *cs)
unknown's avatar
unknown committed
4565 4566
{
  length=uint2korr(buff);
unknown's avatar
unknown committed
4567
  (void) Field_blob::store(buff+2,length,cs);
unknown's avatar
unknown committed
4568 4569
}

unknown's avatar
unknown committed
4570

unknown's avatar
unknown committed
4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591
int Field_blob::key_cmp(const byte *key_ptr, uint max_key_length)
{
  char *blob1;
  uint blob_length=get_length(ptr);
  max_key_length-=2;
  memcpy_fixed(&blob1,ptr+packlength,sizeof(char*));
  return Field_blob::cmp(blob1,min(blob_length, max_key_length),
			 (char*) key_ptr+2,uint2korr(key_ptr));
}

int Field_blob::key_cmp(const byte *a,const byte *b)
{
  return Field_blob::cmp((char*) a+2,uint2korr(a),
			 (char*) b+2,uint2korr(b));
}


void Field_blob::sort_string(char *to,uint length)
{
  char *blob;
  uint blob_length=get_length();
unknown's avatar
unknown committed
4592

unknown's avatar
unknown committed
4593 4594 4595 4596 4597
  if (!blob_length)
    bzero(to,length);
  else
  {
    memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
unknown's avatar
unknown committed
4598 4599 4600 4601
    
    blob_length=my_strnxfrm(field_charset,
                            (unsigned char *)to, length, 
                            (unsigned char *)blob, blob_length);
4602 4603
    if (blob_length < length)
      bzero(to+blob_length, length-blob_length);
unknown's avatar
unknown committed
4604 4605 4606 4607 4608 4609 4610
  }
}


void Field_blob::sql_type(String &res) const
{
  const char *str;
4611
  uint length;
unknown's avatar
unknown committed
4612
  switch (packlength) {
4613 4614 4615 4616 4617
  default: str="tiny"; length=4; break;
  case 2:  str="";     length=0; break;
  case 3:  str="medium"; length= 6; break;
  case 4:  str="long";  length=4; break;
  }
4618
  res.set_ascii(str,length);
4619
  if (charset() == &my_charset_bin)
4620 4621 4622 4623
    res.append("blob");
  else
  {
    res.append("text");
unknown's avatar
unknown committed
4624 4625 4626 4627
  }
}


4628 4629 4630 4631
char *Field_blob::pack(char *to, const char *from, uint max_length)
{
  char *save=ptr;
  ptr=(char*) from;
unknown's avatar
unknown committed
4632
  uint32 length=get_length();			// Length of from string
4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654
  if (length > max_length)
  {
    ptr=to;
    length=max_length;
    store_length(length);			// Store max length
    ptr=(char*) from;
  }
  else
    memcpy(to,from,packlength);			// Copy length
  if (length)
  {
    get_ptr((char**) &from);
    memcpy(to+packlength, from,length);
  }
  ptr=save;					// Restore org row pointer
  return to+packlength+length;
}


const char *Field_blob::unpack(char *to, const char *from)
{
  memcpy(to,from,packlength);
unknown's avatar
unknown committed
4655
  uint32 length=get_length(from);
4656 4657 4658 4659 4660 4661 4662 4663
  from+=packlength;
  if (length)
    memcpy_fixed(to+packlength, &from, sizeof(from));
  else
    bzero(to+packlength,sizeof(from));
  return from+length;
}

unknown's avatar
unknown committed
4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679
/* Keys for blobs are like keys on varchars */

int Field_blob::pack_cmp(const char *a, const char *b, uint key_length)
{
  uint a_length;
  uint b_length;
  if (key_length > 255)
  {
    a_length=uint2korr(a); a+=2;
    b_length=uint2korr(b); b+=2;
  }
  else
  {
    a_length= (uint) (uchar) *a++;
    b_length= (uint) (uchar) *b++;
  }
unknown's avatar
unknown committed
4680 4681 4682
  return my_strnncoll(field_charset,
		     (const uchar *)a,a_length,
		     (const uchar *)b,b_length);
unknown's avatar
unknown committed
4683 4684
}

4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702

int Field_blob::pack_cmp(const char *b, uint key_length)
{
  char *a;
  memcpy_fixed(&a,ptr+packlength,sizeof(char*));
  if (!a)
    return key_length > 0 ? -1 : 0;
  uint a_length=get_length(ptr);
  uint b_length;

  if (key_length > 255)
  {
    b_length=uint2korr(b); b+=2;
  }
  else
  {
    b_length= (uint) (uchar) *b++;
  }
unknown's avatar
unknown committed
4703 4704 4705
  return my_strnncoll(field_charset,
		     (const uchar *)a,a_length,
		     (const uchar *)b,b_length);
4706 4707
}

4708
/* Create a packed key that will be used for storage from a MySQL row */
4709

unknown's avatar
unknown committed
4710 4711
char *Field_blob::pack_key(char *to, const char *from, uint max_length)
{
4712 4713
  char *save=ptr;
  ptr=(char*) from;
unknown's avatar
unknown committed
4714
  uint32 length=get_length();			// Length of from string
4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734
  if (length > max_length)
    length=max_length;
  *to++= (uchar) length;
  if (max_length > 255)				// 2 byte length
    *to++= (uchar) (length >> 8);
  if (length)
  {
    get_ptr((char**) &from);
    memcpy(to, from, length);
  }
  ptr=save;					// Restore org row pointer
  return to+length;
}

/* Create a packed key that will be used for storage from a MySQL key */

char *Field_blob::pack_key_from_key_image(char *to, const char *from,
					  uint max_length)
{
  uint length=uint2korr(from);
unknown's avatar
unknown committed
4735 4736
  if (length > max_length)
    length=max_length;
unknown's avatar
unknown committed
4737
  *to++= (char) (length & 255);
unknown's avatar
unknown committed
4738
  if (max_length > 255)
unknown's avatar
unknown committed
4739
    *to++= (char) (length >> 8);
unknown's avatar
unknown committed
4740 4741 4742 4743 4744
  if (length)
    memcpy(to, from+2, length);
  return to+length;
}

4745
uint Field_blob::packed_col_length(const char *data_ptr, uint length)
4746 4747
{
  if (length > 255)
4748
    return uint2korr(data_ptr)+2;
4749
  else
4750
    return (uint) ((uchar) *data_ptr)+1;
4751 4752 4753 4754 4755 4756
}

uint Field_blob::max_packed_col_length(uint max_length)
{
  return (max_length > 255 ? 2 : 1)+max_length;
}
4757

unknown's avatar
unknown committed
4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785

void Field_geom::get_key_image(char *buff, uint length, CHARSET_INFO *cs,
			       imagetype type)
{
  length-= HA_KEY_BLOB_LENGTH;
  ulong blob_length= get_length(ptr);
  char *blob;
  get_ptr(&blob);

  MBR mbr;
  Geometry gobj;
  gobj.create_from_wkb(blob + SRID_SIZE, blob_length - SRID_SIZE);
  gobj.get_mbr(&mbr);
  float8store(buff, mbr.xmin);
  float8store(buff + 8, mbr.xmax);
  float8store(buff + 16, mbr.ymin);
  float8store(buff + 24, mbr.ymax);
  return;
}


void Field_geom::set_key_image(char *buff, uint length, CHARSET_INFO *cs)
{
  Field_blob::set_key_image(buff, length, cs);
}

void Field_geom::sql_type(String &res) const
{
unknown's avatar
unknown committed
4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812
  CHARSET_INFO *cs= &my_charset_latin1;
  switch (geom_type)
  {
    case GEOM_POINT:
     res.set("point", 5, cs);
     break;
    case GEOM_LINESTRING:
     res.set("linestring", 10, cs);
     break;
    case GEOM_POLYGON:
     res.set("polygon", 7, cs);
     break;
    case GEOM_MULTIPOINT:
     res.set("multipoint", 10, cs);
     break;
    case GEOM_MULTILINESTRING:
     res.set("multilinestring", 15, cs);
     break;
    case GEOM_MULTIPOLYGON:
     res.set("multipolygon", 12, cs);
     break;
    case GEOM_GEOMETRYCOLLECTION:
     res.set("geometrycollection", 18, cs);
     break;
    default:
     res.set("geometry", 8, cs);
  }
unknown's avatar
unknown committed
4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841
}


int Field_geom::store(const char *from, uint length, CHARSET_INFO *cs)
{
  if (!length)
  {
    bzero(ptr, Field_blob::pack_length());
  }
  else
  {
    // Should check given WKB
    if (length < 4 + 1 + 4 + 8 + 8)		// SRID + WKB_HEADER + X + Y
      return 1;
    uint32 wkb_type= uint4korr(from + 5);
    if (wkb_type < 1 || wkb_type > 7)
      return 1;
    Field_blob::store_length(length);
    if (table->copy_blobs || length <= MAX_FIELD_WIDTH)
    {						// Must make a copy
      value.copy(from, length, cs);
      from= value.ptr();
    }
    bmove(ptr + packlength, (char*) &from, sizeof(char*));
  }
  return 0;
}


unknown's avatar
unknown committed
4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899
/****************************************************************************
** enum type.
** This is a string which only can have a selection of different values.
** If one uses this string in a number context one gets the type number.
****************************************************************************/

enum ha_base_keytype Field_enum::key_type() const
{
  switch (packlength) {
  default: return HA_KEYTYPE_BINARY;
  case 2: return HA_KEYTYPE_USHORT_INT;
  case 3: return HA_KEYTYPE_UINT24;
  case 4: return HA_KEYTYPE_ULONG_INT;
  case 8: return HA_KEYTYPE_ULONGLONG;
  }
}

void Field_enum::store_type(ulonglong value)
{
  switch (packlength) {
  case 1: ptr[0]= (uchar) value;  break;
  case 2:
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int2store(ptr,(unsigned short) value);
  }
  else
#endif
    shortstore(ptr,(unsigned short) value);
  break;
  case 3: int3store(ptr,(long) value); break;
  case 4:
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,value);
  }
  else
#endif
    longstore(ptr,(long) value);
  break;
  case 8:
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,value);
  }
  else
#endif
    longlongstore(ptr,value); break;
  }
}


uint find_enum(TYPELIB *lib,const char *x, uint length)
{
  const char *end=x+length;
4900
  while (end > x && my_isspace(system_charset_info,end[-1]))
unknown's avatar
unknown committed
4901 4902 4903 4904 4905 4906
    end--;

  const char *i;
  const char *j;
  for (uint pos=0 ; (j=lib->type_names[pos]) ; pos++)
  {
4907 4908 4909
    for (i=x ; i != end && 
               my_toupper(system_charset_info,*i) == 
               my_toupper(system_charset_info,*j) ; i++, j++) ;
unknown's avatar
unknown committed
4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921
    if (i == end && ! *j)
      return(pos+1);
  }
  return(0);
}


/*
** Note. Storing a empty string in a enum field gives a warning
** (if there isn't a empty value in the enum)
*/

unknown's avatar
unknown committed
4922
int Field_enum::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
4923
{
4924
  int err= 0;
unknown's avatar
unknown committed
4925 4926 4927
  char buff[80];
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
  /* Convert character set if nesessary */
4928
  if (use_conversion(cs, field_charset))
unknown's avatar
unknown committed
4929 4930 4931 4932 4933
  { 
    tmpstr.copy(from, length, cs, field_charset);
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
unknown's avatar
unknown committed
4934
  uint tmp=find_enum(typelib,from,length);
unknown's avatar
unknown committed
4935
  if (!tmp)
unknown's avatar
unknown committed
4936
  {
unknown's avatar
unknown committed
4937
    if (length < 6)			// Can't be more than 99999 enums
unknown's avatar
unknown committed
4938
    {
unknown's avatar
unknown committed
4939
      /* This is for reading numbers with LOAD DATA INFILE */
4940
      char *end;
4941 4942
      tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
      if (err || end != from+length || tmp > typelib->count)
unknown's avatar
unknown committed
4943 4944
      {
	tmp=0;
4945
	set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
4946
      }
unknown's avatar
unknown committed
4947 4948
    }
    else
4949
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
4950
  }
unknown's avatar
unknown committed
4951
  store_type((ulonglong) tmp);
4952
  return err;
unknown's avatar
unknown committed
4953 4954 4955
}


unknown's avatar
unknown committed
4956
int Field_enum::store(double nr)
unknown's avatar
unknown committed
4957
{
4958
  return Field_enum::store((longlong) nr);
unknown's avatar
unknown committed
4959 4960 4961
}


unknown's avatar
unknown committed
4962
int Field_enum::store(longlong nr)
unknown's avatar
unknown committed
4963
{
unknown's avatar
unknown committed
4964
  int error= 0;
unknown's avatar
unknown committed
4965 4966
  if ((uint) nr > typelib->count || nr == 0)
  {
4967
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
unknown's avatar
unknown committed
4968
    nr=0;
4969
    error=1;
unknown's avatar
unknown committed
4970 4971
  }
  store_type((ulonglong) (uint) nr);
4972
  return error;
unknown's avatar
unknown committed
4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034
}


double Field_enum::val_real(void)
{
  return (double) Field_enum::val_int();
}


longlong Field_enum::val_int(void)
{
  switch (packlength) {
  case 1:
    return (longlong) (uchar) ptr[0];
  case 2:
    {
      uint16 tmp;
#ifdef WORDS_BIGENDIAN
      if (table->db_low_byte_first)
	tmp=sint2korr(ptr);
      else
#endif
	shortget(tmp,ptr);
      return (longlong) tmp;
    }
  case 3:
    return (longlong) uint3korr(ptr);
  case 4:
    {
      uint32 tmp;
#ifdef WORDS_BIGENDIAN
      if (table->db_low_byte_first)
	tmp=uint4korr(ptr);
      else
#endif
	longget(tmp,ptr);
      return (longlong) tmp;
    }
  case 8:
    {
      longlong tmp;
#ifdef WORDS_BIGENDIAN
      if (table->db_low_byte_first)
	tmp=sint8korr(ptr);
      else
#endif
	longlongget(tmp,ptr);
      return tmp;
    }
  }
  return 0;					// impossible
}


String *Field_enum::val_str(String *val_buffer __attribute__((unused)),
			    String *val_ptr)
{
  uint tmp=(uint) Field_enum::val_int();
  if (!tmp || tmp > typelib->count)
    val_ptr->length(0);
  else
    val_ptr->set((const char*) typelib->type_names[tmp-1],
5035
		 (uint) strlen(typelib->type_names[tmp-1]),
5036
		 field_charset);
unknown's avatar
unknown committed
5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072
  return val_ptr;
}

int Field_enum::cmp(const char *a_ptr, const char *b_ptr)
{
  char *old=ptr;
  ptr=(char*) a_ptr;
  ulonglong a=Field_enum::val_int();
  ptr=(char*) b_ptr;
  ulonglong b=Field_enum::val_int();
  ptr=old;
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_enum::sort_string(char *to,uint length __attribute__((unused)))
{
  ulonglong value=Field_enum::val_int();
  to+=packlength-1;
  for (uint i=0 ; i < packlength ; i++)
  {
    *to-- = (uchar) (value & 255);
    value>>=8;
  }
}


void Field_enum::sql_type(String &res) const
{
  res.length(0);
  res.append("enum(");

  bool flag=0;
  for (const char **pos=typelib->type_names; *pos ; pos++)
  {
    if (flag)
      res.append(',');
5073
    append_unescaped(&res, *pos, strlen(*pos));
unknown's avatar
unknown committed
5074 5075 5076 5077 5078 5079
    flag=1;
  }
  res.append(')');
}


5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091
/*
   set type.
   This is a string which can have a collection of different values.
   Each string value is separated with a ','.
   For example "One,two,five"
   If one uses this string in a number context one gets the bits as a longlong
   number.

   If there was a value in string that wasn't in set, the 'err_pos' points to
   the last invalid value found. 'err_len' will be set to length of the
   error string.
*/
unknown's avatar
unknown committed
5092

5093
ulonglong find_set(TYPELIB *lib, const char *x, uint length, char **err_pos,
5094
                   uint *err_len, bool *set_warning)
unknown's avatar
unknown committed
5095
{
5096 5097
  const char *end= x + length;
  *err_pos= 0;                  // No error yet
5098
  while (end > x && my_isspace(system_charset_info, end[-1]))
unknown's avatar
unknown committed
5099 5100
    end--;

5101 5102
  *err_len= 0;
  ulonglong found= 0;
unknown's avatar
unknown committed
5103 5104
  if (x != end)
  {
5105
    const char *start= x;    
unknown's avatar
unknown committed
5106 5107
    for (;;)
    {
5108 5109 5110 5111 5112 5113
      const char *pos= start;
      uint var_len;

      for (; pos != end && *pos != field_separator; pos++) ;
      var_len= (uint) (pos - start);
      uint find= find_enum(lib, start, var_len);
unknown's avatar
unknown committed
5114
      if (!find)
5115 5116 5117
      {
        *err_pos= (char*) start;
        *err_len= var_len;
5118
        *set_warning= 1;
5119
      }
unknown's avatar
unknown committed
5120
      else
5121
        found|= ((longlong) 1 << (find - 1));
unknown's avatar
unknown committed
5122
      if (pos == end)
5123 5124
        break;
      start= pos + 1;
unknown's avatar
unknown committed
5125 5126 5127 5128 5129 5130
    }
  }
  return found;
}


unknown's avatar
unknown committed
5131
int Field_set::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
5132
{
5133
  bool set_warning= 0;
5134
  int err= 0;
5135 5136
  char *not_used;
  uint not_used2;
unknown's avatar
unknown committed
5137 5138 5139
  char buff[80];
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
  /* Convert character set if nesessary */
5140
  if (use_conversion(cs, field_charset))
unknown's avatar
unknown committed
5141 5142 5143 5144 5145
  { 
    tmpstr.copy(from, length, cs, field_charset);
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
5146
  ulonglong tmp= find_set(typelib, from, length, &not_used, &not_used2, &set_warning);
unknown's avatar
unknown committed
5147 5148 5149
  if (!tmp && length && length < 22)
  {
    /* This is for reading numbers with LOAD DATA INFILE */
5150
    char *end;
5151 5152
    tmp=my_strntoull(cs,from,length,10,&end,&err);
    if (err || end != from+length ||
unknown's avatar
unknown committed
5153
	tmp > (ulonglong) (((longlong) 1 << typelib->count) - (longlong) 1))
5154
    {
5155 5156 5157 5158 5159
      tmp=0;      
      current_thd->cuted_fields++;
      push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, 
                          ER_WARN_DATA_TRUNCATED, ER(ER_WARN_DATA_TRUNCATED),
                          field_name, 0);
5160
    }
unknown's avatar
unknown committed
5161 5162
  }
  store_type(tmp);
5163
  return err;
unknown's avatar
unknown committed
5164 5165 5166
}


unknown's avatar
unknown committed
5167
int Field_set::store(longlong nr)
unknown's avatar
unknown committed
5168
{
unknown's avatar
unknown committed
5169
  int error= 0;
unknown's avatar
unknown committed
5170 5171 5172
  if ((ulonglong) nr > (ulonglong) (((longlong) 1 << typelib->count) -
				    (longlong) 1))
  {
5173 5174
    nr&= (longlong) (((longlong) 1 << typelib->count) - (longlong) 1);    
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED);
5175
    error=1;
unknown's avatar
unknown committed
5176 5177
  }
  store_type((ulonglong) nr);
5178
  return error;
unknown's avatar
unknown committed
5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195
}


String *Field_set::val_str(String *val_buffer,
			   String *val_ptr __attribute__((unused)))
{
  ulonglong tmp=(ulonglong) Field_enum::val_int();
  uint bitnr=0;

  val_buffer->length(0);
  while (tmp && bitnr < (uint) typelib->count)
  {
    if (tmp & 1)
    {
      if (val_buffer->length())
	val_buffer->append(field_separator);
      String str(typelib->type_names[bitnr],
5196
		 (uint) strlen(typelib->type_names[bitnr]),
5197
		 field_charset);
unknown's avatar
unknown committed
5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216
      val_buffer->append(str);
    }
    tmp>>=1;
    bitnr++;
  }
  return val_buffer;
}


void Field_set::sql_type(String &res) const
{
  res.length(0);
  res.append("set(");

  bool flag=0;
  for (const char **pos=typelib->type_names; *pos ; pos++)
  {
    if (flag)
      res.append(',');
5217
    append_unescaped(&res, *pos, strlen(*pos));
unknown's avatar
unknown committed
5218 5219 5220 5221 5222 5223 5224 5225 5226
    flag=1;
  }
  res.append(')');
}

/* returns 1 if the fields are equally defined */

bool Field::eq_def(Field *field)
{
5227
  if (real_type() != field->real_type() || charset() != field->charset() ||
unknown's avatar
unknown committed
5228 5229 5230 5231 5232 5233 5234 5235 5236 5237
      pack_length() != field->pack_length())
    return 0;
  return 1;
}

bool Field_enum::eq_def(Field *field)
{
  if (!Field::eq_def(field))
    return 0;
  TYPELIB *from_lib=((Field_enum*) field)->typelib;
unknown's avatar
unknown committed
5238

unknown's avatar
unknown committed
5239 5240 5241
  if (typelib->count < from_lib->count)
    return 0;
  for (uint i=0 ; i < from_lib->count ; i++)
5242 5243
    if (my_strcasecmp(field_charset,
                      typelib->type_names[i],from_lib->type_names[i]))
unknown's avatar
unknown committed
5244 5245 5246 5247 5248
      return 0;
  return 1;
}

bool Field_num::eq_def(Field *field)
unknown's avatar
unknown committed
5249
{
unknown's avatar
unknown committed
5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266
  if (!Field::eq_def(field))
    return 0;
  Field_num *from_num= (Field_num*) field;

  if (unsigned_flag != from_num->unsigned_flag ||
      zerofill && !from_num->zerofill && !zero_pack() ||
      dec != from_num->dec)
    return 0;
  return 1;
}


/*****************************************************************************
** Handling of field and create_field
*****************************************************************************/

/*
unknown's avatar
unknown committed
5267
  Make a field from the .frm file info
unknown's avatar
unknown committed
5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290
*/

uint32 calc_pack_length(enum_field_types type,uint32 length)
{
  switch (type) {
  case FIELD_TYPE_STRING:
  case FIELD_TYPE_DECIMAL: return (length);
  case FIELD_TYPE_VAR_STRING: return (length+2);
  case FIELD_TYPE_YEAR:
  case FIELD_TYPE_TINY	: return 1;
  case FIELD_TYPE_SHORT : return 2;
  case FIELD_TYPE_INT24:
  case FIELD_TYPE_NEWDATE:
  case FIELD_TYPE_TIME:   return 3;
  case FIELD_TYPE_TIMESTAMP:
  case FIELD_TYPE_DATE:
  case FIELD_TYPE_LONG	: return 4;
  case FIELD_TYPE_FLOAT : return sizeof(float);
  case FIELD_TYPE_DOUBLE: return sizeof(double);
  case FIELD_TYPE_DATETIME:
  case FIELD_TYPE_LONGLONG: return 8;	/* Don't crash if no longlong */
  case FIELD_TYPE_NULL	: return 0;
  case FIELD_TYPE_TINY_BLOB:	return 1+portable_sizeof_char_ptr;
unknown's avatar
unknown committed
5291
  case FIELD_TYPE_BLOB:		return 2+portable_sizeof_char_ptr;
unknown's avatar
unknown committed
5292 5293
  case FIELD_TYPE_MEDIUM_BLOB:	return 3+portable_sizeof_char_ptr;
  case FIELD_TYPE_LONG_BLOB:	return 4+portable_sizeof_char_ptr;
unknown's avatar
unknown committed
5294
  case FIELD_TYPE_GEOMETRY:	return 4+portable_sizeof_char_ptr;
unknown's avatar
unknown committed
5295 5296
  case FIELD_TYPE_SET:
  case FIELD_TYPE_ENUM: abort(); return 0;	// This shouldn't happen
unknown's avatar
unknown committed
5297
  default: return 0;
unknown's avatar
unknown committed
5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316
  }
  return 0;					// This shouldn't happen
}


uint pack_length_to_packflag(uint type)
{
  switch (type) {
    case 1: return f_settype((uint) FIELD_TYPE_TINY);
    case 2: return f_settype((uint) FIELD_TYPE_SHORT);
    case 3: return f_settype((uint) FIELD_TYPE_INT24);
    case 4: return f_settype((uint) FIELD_TYPE_LONG);
    case 8: return f_settype((uint) FIELD_TYPE_LONGLONG);
  }
  return 0;					// This shouldn't happen
}


Field *make_field(char *ptr, uint32 field_length,
unknown's avatar
unknown committed
5317
		  uchar *null_pos, uchar null_bit,
unknown's avatar
unknown committed
5318
		  uint pack_flag,
5319
		  enum_field_types field_type,
5320
		  CHARSET_INFO *field_charset,
unknown's avatar
unknown committed
5321
		  Field::geometry_type geom_type,
unknown's avatar
unknown committed
5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335
		  Field::utype unireg_check,
		  TYPELIB *interval,
		  const char *field_name,
		  struct st_table *table)
{
  if (!f_maybe_null(pack_flag))
  {
    null_pos=0;
    null_bit=0;
  }
  if (f_is_alpha(pack_flag))
  {
    if (!f_is_packed(pack_flag))
      return new Field_string(ptr,field_length,null_pos,null_bit,
5336
			      unireg_check, field_name, table, field_charset);
unknown's avatar
unknown committed
5337 5338 5339 5340 5341

    uint pack_length=calc_pack_length((enum_field_types)
				      f_packtype(pack_flag),
				      field_length);

unknown's avatar
unknown committed
5342 5343
    if (f_is_geom(pack_flag))
      return new Field_geom(ptr,null_pos,null_bit,
unknown's avatar
unknown committed
5344
			    unireg_check, field_name, table,
unknown's avatar
unknown committed
5345
			    pack_length, geom_type);
unknown's avatar
unknown committed
5346 5347 5348
    if (f_is_blob(pack_flag))
      return new Field_blob(ptr,null_pos,null_bit,
			    unireg_check, field_name, table,
unknown's avatar
unknown committed
5349
			    pack_length, field_charset);
unknown's avatar
unknown committed
5350 5351 5352 5353 5354
    if (interval)
    {
      if (f_is_enum(pack_flag))
	return new Field_enum(ptr,field_length,null_pos,null_bit,
				  unireg_check, field_name, table,
5355
				  pack_length, interval, field_charset);
unknown's avatar
unknown committed
5356 5357 5358
      else
	return new Field_set(ptr,field_length,null_pos,null_bit,
			     unireg_check, field_name, table,
5359
			     pack_length, interval, field_charset);
unknown's avatar
unknown committed
5360 5361 5362
    }
  }

5363
  switch (field_type) {
unknown's avatar
unknown committed
5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408
  case FIELD_TYPE_DECIMAL:
    return new Field_decimal(ptr,field_length,null_pos,null_bit,
			     unireg_check, field_name, table,
			     f_decimals(pack_flag),
			     f_is_zerofill(pack_flag) != 0,
			     f_is_dec(pack_flag) == 0);
  case FIELD_TYPE_FLOAT:
    return new Field_float(ptr,field_length,null_pos,null_bit,
			   unireg_check, field_name, table,
			   f_decimals(pack_flag),
			   f_is_zerofill(pack_flag) != 0,
			   f_is_dec(pack_flag)== 0);
  case FIELD_TYPE_DOUBLE:
    return new Field_double(ptr,field_length,null_pos,null_bit,
			    unireg_check, field_name, table,
			    f_decimals(pack_flag),
			    f_is_zerofill(pack_flag) != 0,
			    f_is_dec(pack_flag)== 0);
  case FIELD_TYPE_TINY:
    return new Field_tiny(ptr,field_length,null_pos,null_bit,
			  unireg_check, field_name, table,
			  f_is_zerofill(pack_flag) != 0,
			  f_is_dec(pack_flag) == 0);
  case FIELD_TYPE_SHORT:
    return new Field_short(ptr,field_length,null_pos,null_bit,
			   unireg_check, field_name, table,
			   f_is_zerofill(pack_flag) != 0,
			   f_is_dec(pack_flag) == 0);
  case FIELD_TYPE_INT24:
    return new Field_medium(ptr,field_length,null_pos,null_bit,
			    unireg_check, field_name, table,
			    f_is_zerofill(pack_flag) != 0,
			    f_is_dec(pack_flag) == 0);
  case FIELD_TYPE_LONG:
    return new Field_long(ptr,field_length,null_pos,null_bit,
			   unireg_check, field_name, table,
			   f_is_zerofill(pack_flag) != 0,
			   f_is_dec(pack_flag) == 0);
  case FIELD_TYPE_LONGLONG:
    return new Field_longlong(ptr,field_length,null_pos,null_bit,
			      unireg_check, field_name, table,
			      f_is_zerofill(pack_flag) != 0,
			      f_is_dec(pack_flag) == 0);
  case FIELD_TYPE_TIMESTAMP:
    return new Field_timestamp(ptr,field_length,
5409
			       unireg_check, field_name, table, field_charset);
unknown's avatar
unknown committed
5410 5411 5412 5413 5414
  case FIELD_TYPE_YEAR:
    return new Field_year(ptr,field_length,null_pos,null_bit,
			  unireg_check, field_name, table);
  case FIELD_TYPE_DATE:
    return new Field_date(ptr,null_pos,null_bit,
5415
			  unireg_check, field_name, table, field_charset);
unknown's avatar
unknown committed
5416 5417
  case FIELD_TYPE_NEWDATE:
    return new Field_newdate(ptr,null_pos,null_bit,
5418
			     unireg_check, field_name, table, field_charset);
unknown's avatar
unknown committed
5419 5420
  case FIELD_TYPE_TIME:
    return new Field_time(ptr,null_pos,null_bit,
5421
			  unireg_check, field_name, table, field_charset);
unknown's avatar
unknown committed
5422 5423
  case FIELD_TYPE_DATETIME:
    return new Field_datetime(ptr,null_pos,null_bit,
5424
			      unireg_check, field_name, table, field_charset);
unknown's avatar
unknown committed
5425
  case FIELD_TYPE_NULL:
5426
    return new Field_null(ptr,field_length,unireg_check,field_name,table, field_charset);
5427 5428
  default:					// Impossible (Wrong version)
    break;
unknown's avatar
unknown committed
5429
  }
unknown's avatar
unknown committed
5430
  return 0;
unknown's avatar
unknown committed
5431 5432 5433 5434 5435
}


/* Create a field suitable for create of table */

5436
create_field::create_field(Field *old_field,Field *orig_field)
unknown's avatar
unknown committed
5437 5438 5439 5440 5441 5442 5443 5444
{
  field=      old_field;
  field_name=change=old_field->field_name;
  length=     old_field->field_length;
  flags=      old_field->flags;
  unireg_check=old_field->unireg_check;
  pack_length=old_field->pack_length();
  sql_type=   old_field->real_type();
unknown's avatar
unknown committed
5445 5446
  charset=    old_field->charset();		// May be NULL ptr
  comment=    old_field->comment;
unknown's avatar
unknown committed
5447 5448 5449 5450 5451 5452 5453 5454

  /* Fix if the original table had 4 byte pointer blobs */
  if (flags & BLOB_FLAG)
    pack_length= (pack_length- old_field->table->blob_ptr_size +
		  portable_sizeof_char_ptr);
  decimals= old_field->decimals();
  if (sql_type == FIELD_TYPE_STRING)
  {
unknown's avatar
unknown committed
5455
    /* Change CHAR -> VARCHAR if dynamic record length */
unknown's avatar
unknown committed
5456 5457 5458 5459 5460 5461 5462
    sql_type=old_field->type();
    decimals=0;
  }
  if (flags & (ENUM_FLAG | SET_FLAG))
    interval= ((Field_enum*) old_field)->typelib;
  else
    interval=0;
5463
  def=0;
5464 5465 5466
  if (!old_field->is_real_null() && ! (flags & BLOB_FLAG) &&
      old_field->type() != FIELD_TYPE_TIMESTAMP && old_field->ptr &&
      orig_field)
unknown's avatar
unknown committed
5467 5468
  {
    char buff[MAX_FIELD_WIDTH],*pos;
5469
    String tmp(buff,sizeof(buff), charset);
5470

unknown's avatar
unknown committed
5471
    /* Get the value from default_values */
5472
    my_ptrdiff_t diff= (my_ptrdiff_t) (orig_field->table->rec_buff_length*2);
unknown's avatar
unknown committed
5473
    orig_field->move_field(diff);		// Points now at default_values
5474
    bool is_null=orig_field->is_real_null();
5475
    orig_field->val_str(&tmp,&tmp);
5476
    orig_field->move_field(-diff);		// Back to record[0]
5477
    if (!is_null)
5478 5479 5480
    {
      pos= (char*) sql_memdup(tmp.ptr(),tmp.length()+1);
      pos[tmp.length()]=0;
5481
      def=new Item_string(pos,tmp.length(), charset);
5482
    }
unknown's avatar
unknown committed
5483
  }
5484 5485 5486 5487
  if (sql_type == FIELD_TYPE_GEOMETRY)
  {
    geom_type= ((Field_geom*)old_field)->geom_type;
  }
unknown's avatar
unknown committed
5488
}
5489 5490 5491 5492 5493 5494 5495


/* Warning handling */
void Field::set_warning(const uint level, const uint code)
{
  THD *thd= current_thd;
  thd->cuted_fields++;
5496
  push_warning_printf(thd, (MYSQL_ERROR::enum_warning_level) level, 
5497 5498
                      code, ER(code), field_name, thd->row_count);
}