field.cc 111 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 34 35 36 37 38 39 40 41 42 43
   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 */


/*
  NOTES:
  Some of the number class uses the system functions strtol(), strtoll()...
  To avoid patching the end \0 or copying the buffer unnecessary, all calls
  to system functions are wrapped to a String object that adds the end null
  if it only if it isn't there.
  This adds some overhead when assigning numbers from strings but makes
  everything simpler.
  */

/*****************************************************************************
** 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
44 45 46 47 48
// Maximum allowed exponent value for converting string to decimal
#define MAX_EXPONENT 1024



unknown's avatar
unknown committed
49
/*****************************************************************************
50
  Instansiate templates and static variables
unknown's avatar
unknown committed
51 52 53 54 55 56 57 58 59 60 61
*****************************************************************************/

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

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

/*****************************************************************************
62
  Static help functions
unknown's avatar
unknown committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
*****************************************************************************/

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
  }
}

/*
79 80
  Test if given number is a int (or a fixed format float with .000)
  This is only used to give warnings in ALTER TABLE or LOAD DATA...
unknown's avatar
unknown committed
81 82 83 84 85 86 87 88 89 90 91 92
*/

bool test_if_int(const char *str,int length)
{
  const char *end=str+length;

  while (str != end && isspace(*str))	// Allow start space
    str++; /* purecov: inspected */
  if (str != end && (*str == '-' || *str == '+'))
    str++;
  if (str == end)
    return 0;					// Error: Empty string
93
  for (; str != end ; str++)
unknown's avatar
unknown committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  {
    if (!isdigit(*str))
    {
      if (*str == '.')
      {						// Allow '.0000'
	for (str++ ; str != end && *str == '0'; str++) ;
	if (str == end)
	  return 1;
      }
      if (!isspace(*str))
	return 0;
      for (str++ ; str != end ; str++)
	if (!isspace(*str))
	  return 0;
      return 1;
    }
  }
  return 1;
}


static bool test_if_real(const char *str,int length)
{
  while (length && isspace(*str))
  {						// Allow start space
    length--; str++;
  }
  if (!length)
    return 0;
  if (*str == '+' || *str == '-')
  {
    length--; str++;
    if (!length || !(isdigit(*str) || *str == '.'))
      return 0;
  }
  while (length && isdigit(*str))
  {
    length--; str++;
  }
  if (!length)
    return 1;
  if (*str == '.')
  {
    length--; str++;
    while (length && isdigit(*str))
    {
      length--; str++;
    }
  }
  if (!length)
    return 1;
  if (*str == 'E' || *str == 'e')
  {
    if (length < 3 || (str[1] != '+' && str[1] != '-') || !isdigit(str[2]))
      return 0;
    length-=3;
    str+=3;
    while (length && isdigit(*str))
    {
      length--; str++;
    }
  }
156
  for (; length ; length--, str++)
unknown's avatar
unknown committed
157 158 159 160 161 162 163 164 165 166
  {						// Allow end space
    if (!isspace(*str))
      return 0;
  }
  return 1;
}


/****************************************************************************
** Functions for the base classes
unknown's avatar
unknown committed
167
** This is an unpacked number.
unknown's avatar
unknown committed
168 169 170
****************************************************************************/

Field::Field(char *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
unknown's avatar
unknown committed
171
	     uchar null_bit_arg,
unknown's avatar
unknown committed
172 173
	     utype unireg_check_arg, const char *field_name_arg,
	     struct st_table *table_arg)
unknown's avatar
unknown committed
174 175 176 177 178 179
  :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
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
{
  flags=null_ptr ? 0: NOT_NULL_FLAG;
}

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
195 196
    *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) |
			null_ptr[row_offset] & (uchar) null_bit);
unknown's avatar
unknown committed
197 198 199 200
  }
}


201
bool Field::send(THD *thd, String *packet)
unknown's avatar
unknown committed
202 203 204 205 206 207 208
{
  if (is_null())
    return net_store_null(packet);
  char buff[MAX_FIELD_WIDTH];
  String tmp(buff,sizeof(buff));
  val_str(&tmp,&tmp);
  CONVERT *convert;
unknown's avatar
unknown committed
209
  if ((convert=thd->variables.convert_set))
unknown's avatar
unknown committed
210 211 212 213 214 215 216
    return convert->store(packet,tmp.ptr(),tmp.length());
  return net_store_data(packet,tmp.ptr(),tmp.length());
}


void Field_num::add_zerofill_and_unsigned(String &res) const
{
unknown's avatar
unknown committed
217
  res.length((uint) strlen(res.ptr()));		// Fix length
unknown's avatar
unknown committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
  if (unsigned_flag)
    res.append(" unsigned");
  if (zerofill)
    res.append(" zerofill");
}

void Field_num::make_field(Send_field *field)
{
  field->table_name=table_name;
  field->col_name=field_name;
  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)
{
  field->table_name=table_name;
  field->col_name=field_name;
  field->length=field_length;
  field->type=type();
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
  field->decimals=0;
}


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];
  String tmp(buff,sizeof(buff)),tmp2,*res;
  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];
  String tmp(buff,sizeof(buff)),tmp2,*res;
  if (!(res=val_str(&tmp,&tmp2)) ||
unknown's avatar
unknown committed
281
      str_to_time(res->ptr(),res->length(),ltime))
unknown's avatar
unknown committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    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:
    store("",0);			// Probably an error
    break;
  case TIMESTAMP_DATE:
    sprintf(buff,"%04d-%02d-%02d", ltime->year,ltime->month,ltime->day);
    store(buff,10);
    break;
  case TIMESTAMP_FULL:
    sprintf(buff,"%04d-%02d-%02d %02d:%02d:%02d",
	    ltime->year,ltime->month,ltime->day,
	    ltime->hour,ltime->minute,ltime->second);
    store(buff,19);
    break;
  case TIMESTAMP_TIME:
    sprintf(buff, "%02d:%02d:%02d",
	    ltime->hour,ltime->minute,ltime->second);
unknown's avatar
unknown committed
308
    store(buff,(uint) strlen(buff));
unknown's avatar
unknown committed
309 310 311 312 313
    break;
  }
}


314
bool Field::optimize_range(uint idx)
unknown's avatar
unknown committed
315
{
316
  return test(table->file->index_flags(idx) & HA_READ_NEXT);
unknown's avatar
unknown committed
317 318 319
}

/****************************************************************************
unknown's avatar
unknown committed
320 321
  Functions for the Field_decimal class
  This is an number stored as a pre-space (or pre-zero) string
unknown's avatar
unknown committed
322 323 324 325 326 327 328 329 330 331 332
****************************************************************************/

void
Field_decimal::reset(void)
{
  Field_decimal::store("0",1);
}

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

  current_thd->cuted_fields++;
336
  if (negative)
unknown's avatar
unknown committed
337
  {
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    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
361
  }
362
  bfill(to, len, filler);
unknown's avatar
unknown committed
363 364 365 366 367 368 369 370
  if (dec)
    ptr[field_length-dec-1]='.';
  return;
}


void Field_decimal::store(const char *from,uint len)
{
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  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;
 /* The pointers where prezeros start and stop */
  const char *pre_zeros_from, *pre_zeros_end;
 /* The pointers where digits at the left of '.' start and stop */
  const char *int_digits_from, *int_digits_end;
 /* The pointers where digits at the right of '.' start and stop */
  const char *frac_digits_from, *frac_digits_end;
 /* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
  char expo_sign_char=0;
unknown's avatar
unknown committed
388
  uint exponent=0;                                // value of the exponent
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
  /*
    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;
 /*
   Pointer used when digits move from the right of the '.' to the left
   of the '.'
 */
  const char *frac_digits_head_end;
 /* Number of 0 that need to be added at the right of the '.' (for 1E-3) */
  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;
 /*
   To remember if current_thd->cuted_fields has already been incremented,
   to do that only once
 */
  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 :
      - parse the input string
      - modify the position of digits around the decimal dot '.' 
        according to the exponent value (if specified)
	- write the formatted number
  */

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

unknown's avatar
unknown committed
429
  for (; from !=end && isspace(*from); from++) ; // Read spaces
430
  if (from == end)
unknown's avatar
unknown committed
431
  {
432 433
    current_thd->cuted_fields++;
    is_cuted_fields_incr=1;
unknown's avatar
unknown committed
434
  }
435
  else if (*from == '+' || *from == '-')	// Found some sign ?
unknown's avatar
unknown committed
436
  {
437
    sign_char= *from++;
unknown's avatar
unknown committed
438
    /*
439 440 441
      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
442 443 444 445
    */ 
    if (unsigned_flag)  
    { 
      if (sign_char=='-')
446
      {
447 448
        Field_decimal::overflow(1);
        return;
449
      }
450 451 452 453 454
      /* 
        Defining this will not store "+" for unsigned decimal type even if
	it is passed in numeric string. This will make some tests to fail
      */	 
#ifdef DONT_ALLOW_UNSIGNED_PLUS      
455 456
      else 
        sign_char=0;
457
#endif 	
unknown's avatar
unknown committed
458
    }
459
  }
unknown's avatar
unknown committed
460

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
  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 '.'*/
  for (; from!=end && isdigit(*from);from++) ;
  int_digits_end=from;
  if (from!=end && *from == '.')		// Some '.' ?
    from++;
  frac_digits_from= from;
  /* Read digits at the right of '.' */
  for (;from!=end && isdigit(*from); from++) ;
  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= '+';
    /*
482 483 484 485
      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).
486
    */
487 488
    for (;from!=end && isdigit(*from); from++)
    {
unknown's avatar
unknown committed
489 490
      exponent=10*exponent+(*from-'0');
      if (exponent>MAX_EXPONENT)
491 492
        break;
    }
unknown's avatar
unknown committed
493
  }
494
  
unknown's avatar
unknown committed
495
  /*
496 497 498 499
    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
500
  */
501 502

  if (current_thd->count_cuted_fields)
unknown's avatar
unknown committed
503
  {
unknown's avatar
unknown committed
504
    for (;from != end && isspace(*from); from++) ;   // Read end spaces
505
    if (from != end)                     // If still something left, warn
unknown's avatar
unknown committed
506
    {
507 508
      current_thd->cuted_fields++; 
      is_cuted_fields_incr=1;
unknown's avatar
unknown committed
509
    }
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
  }
  
  /*
    Now "move" digits around the decimal dot according to the exponent value,
    and add necessary zeros.
    Examples :
      - 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'
    - 1234.5E-3 : '234' moves at the right of '.'
      These moves are implemented with pointers which point at the begin
      and end of each moved segment. Examples :
    - 1234.5E-3 : before the code below is executed, the int_digits part is
      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'.
    - 1234.5E3 : before the code below is executed, the int_digits part is
      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).
  */
  
533
  /* 
unknown's avatar
unknown committed
534
     Below tmp_uint cannot overflow with small enough MAX_EXPONENT setting,
535
     as int_digits_added_zeros<=exponent<4G and 
536 537
     (int_digits_end-int_digits_from)<=max_allowed_packet<=2G and
     (frac_digits_from-int_digits_tail_from)<=max_allowed_packet<=2G
538 539
  */

540
  if (!expo_sign_char)
unknown's avatar
unknown committed
541
    tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);
542 543 544 545 546 547
  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
548
    tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);     
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
  }
  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
574
    }
575 576 577
    tmp_uint= (tmp_dec+(int_digits_end-int_digits_from)+
               (uint)(frac_digits_from-int_digits_tail_from)+
               int_digits_added_zeros);
578 579 580 581 582 583 584 585 586 587
  }
  
  /*
    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
  */

588
  if (field_length < tmp_uint + (int) (sign_char == '-')) 
589
  {
unknown's avatar
unknown committed
590
    // too big number, change to max or min number
591
    Field_decimal::overflow(sign_char == '-');
unknown's avatar
unknown committed
592 593
    return;
  }
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
 
  /*
    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
611
  }
612 613 614 615 616 617
  /*
    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
618 619

  /*
620 621
    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
622
  */
623 624 625 626 627 628 629 630 631
  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
632
    left_wall=to+(sign_char != 0)-1;
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
    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
  }
  
651 652 653 654 655 656 657 658
  /*
    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)
  */
659
      
660 661 662 663
  pos=to+(uint)(field_length-tmp_dec);	// Calculate post to '.'
  right_wall=to+field_length;
  if (pos != right_wall) 
    *pos++='.';
664

665 666 667
  if (expo_sign_char == '-')
  {
    while (frac_digits_added_zeros-- > 0)
unknown's avatar
unknown committed
668
    {
669
      if (pos == right_wall) 
670
      {
671 672 673
        if (current_thd->count_cuted_fields && !is_cuted_fields_incr) 
          break; // Go on below to see if we lose non zero digits
        return;
674
      }
675 676 677 678 679 680
      *pos++='0';
    }
    while (int_digits_end != frac_digits_head_end)
    {
      tmp_char= *int_digits_end++;
      if (pos == right_wall)
unknown's avatar
unknown committed
681
      {
682 683 684 685 686 687 688
        if (tmp_char != '0')			// Losing a non zero digit ?
        {
          if (!is_cuted_fields_incr)
            current_thd->cuted_fields++;
          return;
        }
        continue;
unknown's avatar
unknown committed
689
      }
690
      *pos++= tmp_char;
unknown's avatar
unknown committed
691
    }
692
  }
693

694 695 696 697
  for (;frac_digits_from!=frac_digits_end;) 
  {
    tmp_char= *frac_digits_from++;
    if (pos == right_wall)
698
    {
699
      if (tmp_char != '0')			// Losing a non zero digit ?
700
      {
701 702 703
        if (!is_cuted_fields_incr)
	  current_thd->cuted_fields++;
        return;
704
      }
705
      continue;
706
    }
707
    *pos++= tmp_char;
unknown's avatar
unknown committed
708
  }
709 710 711 712
      
  while (pos != right_wall)
   *pos++='0';			// Fill with zeros at right of '.'
  
unknown's avatar
unknown committed
713 714 715 716 717 718 719 720 721 722
}


void Field_decimal::store(double nr)
{
  if (unsigned_flag && nr < 0)
  {
    overflow(1);
    return;
  }
723
  
724 725
#ifdef HAVE_FINITE
  if (!finite(nr)) // Handle infinity as special case
726 727 728 729
  {
    overflow(nr < 0.0);
    return;      
  }
730
#endif
731
  
unknown's avatar
unknown committed
732 733 734 735 736
  reg4 uint i,length;
  char fyllchar,*to;
  char buff[320];

  fyllchar = zerofill ? (char) '0' : (char) ' ';
unknown's avatar
unknown committed
737
#ifdef HAVE_SNPRINTF_
unknown's avatar
unknown committed
738 739
  buff[sizeof(buff)-1]=0;			// Safety
  snprintf(buff,sizeof(buff)-1, "%.*f",(int) dec,nr);
unknown's avatar
unknown committed
740
#else
unknown's avatar
unknown committed
741
  sprintf(buff,"%.*f",dec,nr);
unknown's avatar
unknown committed
742 743
#endif
  length=(uint) strlen(buff);
unknown's avatar
unknown committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826

  if (length > field_length)
    overflow(nr < 0.0);
  else
  {
    to=ptr;
    for (i=field_length-length ; i-- > 0 ;)
      *to++ = fyllchar;
    memcpy(to,buff,length);
  }
}


void Field_decimal::store(longlong nr)
{
  if (unsigned_flag && nr < 0)
  {
    overflow(1);
    return;
  }
  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 */
  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');
    }
  }
}


double Field_decimal::val_real(void)
{
  char temp= *(ptr+field_length); *(ptr+field_length) = '\0';
  double nr=atod(ptr);
  *(ptr+field_length)=temp;
  return(nr);
}

longlong Field_decimal::val_int(void)
{
  char temp= *(ptr+field_length); *(ptr+field_length) = '\0';
  longlong nr;
  if (unsigned_flag)
    nr=(longlong) strtoull(ptr,NULL,10);
  else
    nr=strtoll(ptr,NULL,10);
  *(ptr+field_length)=temp;
  return(nr);
}

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);
  if (field_length < tmp_length)		// Error in data
    val_ptr->length(0);
  else
    val_ptr->set((const char*) str,field_length-tmp_length);
  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;
827
  int swap=0;
unknown's avatar
unknown committed
828 829 830 831 832 833
  /* First remove prefixes '0', ' ', and '-' */
  for (end=a_ptr+field_length;
       a_ptr != end &&
	 (*a_ptr == *b_ptr ||
	  ((isspace(*a_ptr)  || *a_ptr == '+' || *a_ptr == '0') &&
	   (isspace(*b_ptr) || *b_ptr == '+' || *b_ptr == '0')));
834 835 836 837 838
       a_ptr++,b_ptr++)
  {
    if (*a_ptr == '-')				// If both numbers are negative
      swap= -1 ^ 1;				// Swap result      
  }
unknown's avatar
unknown committed
839 840 841
  if (a_ptr == end)
    return 0;
  if (*a_ptr == '-')
842 843
    return -1;
  else if (*b_ptr == '-')
unknown's avatar
unknown committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
    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 &&
	 ((isspace(*str) || *str == '+' || *str == '0')) ;

       str++)
    *to++=' ';
  if (str == end)
    return;					/* purecov: inspected */

  if (*str == '-')
  {
    *to++=1;					// Smaller than any number
    str++;
    while (str != end)
      if (isdigit(*str))
	*to++= (char) ('9' - *str++);
      else
	*to++= *str++;
  }
  else memcpy(to,str,(uint) (end-str));
}

void Field_decimal::sql_type(String &res) const
{
  uint tmp=field_length;
  if (!unsigned_flag)
    tmp--;
  if (dec)
    tmp--;
  sprintf((char*) res.ptr(),"decimal(%d,%d)",tmp,dec);
  add_zerofill_and_unsigned(res);
}


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

void Field_tiny::store(const char *from,uint len)
{
  String tmp_str(from,len);
  long tmp= strtol(tmp_str.c_ptr(),NULL,10);

  if (unsigned_flag)
  {
    if (tmp < 0)
    {
      tmp=0; /* purecov: inspected */
      current_thd->cuted_fields++; /* purecov: inspected */
    }
    else if (tmp > 255)
    {
      tmp= 255;
      current_thd->cuted_fields++;
    }
    else if (current_thd->count_cuted_fields && !test_if_int(from,len))
      current_thd->cuted_fields++;
  }
  else
  {
    if (tmp < -128)
    {
      tmp= -128;
      current_thd->cuted_fields++;
    }
    else if (tmp >= 128)
    {
      tmp= 127;
      current_thd->cuted_fields++;
    }
    else if (current_thd->count_cuted_fields && !test_if_int(from,len))
      current_thd->cuted_fields++;
  }
  ptr[0]= (char) tmp;
}


void Field_tiny::store(double nr)
{
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0.0)
    {
      *ptr=0;
      current_thd->cuted_fields++;
    }
    else if (nr > 255.0)
    {
      *ptr=(char) 255;
      current_thd->cuted_fields++;
    }
    else
      *ptr=(char) nr;
  }
  else
  {
    if (nr < -128.0)
    {
      *ptr= (char) -128;
      current_thd->cuted_fields++;
    }
    else if (nr > 127.0)
    {
      *ptr=127;
      current_thd->cuted_fields++;
    }
    else
      *ptr=(char) nr;
  }
}

void Field_tiny::store(longlong nr)
{
  if (unsigned_flag)
  {
    if (nr < 0L)
    {
      *ptr=0;
      current_thd->cuted_fields++;
    }
    else if (nr > 255L)
    {
      *ptr= (char) 255;
      current_thd->cuted_fields++;
    }
    else
      *ptr=(char) nr;
  }
  else
  {
    if (nr < -128L)
    {
      *ptr= (char) -128;
      current_thd->cuted_fields++;
    }
    else if (nr > 127L)
    {
      *ptr=127;
      current_thd->cuted_fields++;
    }
    else
      *ptr=(char) nr;
  }
}


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)))
{
  uint length;
  val_buffer->alloc(max(field_length+1,5));
  char *to=(char*) val_buffer->ptr();
  if (unsigned_flag)
    length= (uint) (int10_to_str((long) *((uchar*) ptr),to,10)-to);
  else
unknown's avatar
unknown committed
1028
    length= (uint) (int10_to_str((long) *((signed char*) ptr),to,-10)-to);
unknown's avatar
unknown committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


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
{
  sprintf((char*) res.ptr(),"tinyint(%d)",(int) field_length);
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
** short int
****************************************************************************/


// Note:  Sometimes this should be fixed to use one strtol() to use
// len and check for garbage after number.

void Field_short::store(const char *from,uint len)
{
  String tmp_str(from,len);
  long tmp= strtol(tmp_str.c_ptr(),NULL,10);
  if (unsigned_flag)
  {
    if (tmp < 0)
    {
      tmp=0;
      current_thd->cuted_fields++;
    }
    else if (tmp > (uint16) ~0)
    {
      tmp=(uint16) ~0;
      current_thd->cuted_fields++;
    }
    else if (current_thd->count_cuted_fields && !test_if_int(from,len))
      current_thd->cuted_fields++;
  }
  else
  {
    if (tmp < INT_MIN16)
    {
      tmp= INT_MIN16;
      current_thd->cuted_fields++;
    }
    else if (tmp > INT_MAX16)
    {
      tmp=INT_MAX16;
      current_thd->cuted_fields++;
    }
    else if (current_thd->count_cuted_fields && !test_if_int(from,len))
      current_thd->cuted_fields++;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int2store(ptr,tmp);
  }
  else
#endif
    shortstore(ptr,(short) tmp);
}


void Field_short::store(double nr)
{
  int16 res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
      current_thd->cuted_fields++;
    }
    else if (nr > (double) (uint16) ~0)
    {
      res=(int16) (uint16) ~0;
      current_thd->cuted_fields++;
    }
    else
      res=(int16) (uint16) nr;
  }
  else
  {
    if (nr < (double) INT_MIN16)
    {
      res=INT_MIN16;
      current_thd->cuted_fields++;
    }
    else if (nr > (double) INT_MAX16)
    {
      res=INT_MAX16;
      current_thd->cuted_fields++;
    }
    else
      res=(int16) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int2store(ptr,res);
  }
  else
#endif
    shortstore(ptr,res);
}

void Field_short::store(longlong nr)
{
  int16 res;
  if (unsigned_flag)
  {
    if (nr < 0L)
    {
      res=0;
      current_thd->cuted_fields++;
    }
    else if (nr > (longlong) (uint16) ~0)
    {
      res=(int16) (uint16) ~0;
      current_thd->cuted_fields++;
    }
    else
      res=(int16) (uint16) nr;
  }
  else
  {
    if (nr < INT_MIN16)
    {
      res=INT_MIN16;
      current_thd->cuted_fields++;
    }
    else if (nr > INT_MAX16)
    {
      res=INT_MAX16;
      current_thd->cuted_fields++;
    }
    else
      res=(int16) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int2store(ptr,res);
  }
  else
#endif
    shortstore(ptr,res);
}


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;
}

String *Field_short::val_str(String *val_buffer,
			     String *val_ptr __attribute__((unused)))
{
  uint length;
  val_buffer->alloc(max(field_length+1,7));
  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)
    length=(uint) (int10_to_str((long) (uint16) j,to,10)-to);
  else
    length=(uint) (int10_to_str((long) j,to,-10)-to);
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


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
1279
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
1280 1281 1282 1283 1284 1285 1286 1287
    to[1]   = ptr[1];
  }
  else
#endif
  {
    if (unsigned_flag)
      to[0] = ptr[1];
    else
unknown's avatar
unknown committed
1288
      to[0] = (char) (ptr[1] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 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 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
    to[1]   = ptr[0];
  }
}

void Field_short::sql_type(String &res) const
{
  sprintf((char*) res.ptr(),"smallint(%d)",(int) field_length);
  add_zerofill_and_unsigned(res);
}


/****************************************************************************
** medium int
****************************************************************************/

// Note:  Sometimes this should be fixed to use one strtol() to use
// len and check for garbage after number.

void Field_medium::store(const char *from,uint len)
{
  String tmp_str(from,len);
  long tmp= strtol(tmp_str.c_ptr(),NULL,10);

  if (unsigned_flag)
  {
    if (tmp < 0)
    {
      tmp=0;
      current_thd->cuted_fields++;
    }
    else if (tmp >= (long) (1L << 24))
    {
      tmp=(long) (1L << 24)-1L;
      current_thd->cuted_fields++;
    }
    else if (current_thd->count_cuted_fields && !test_if_int(from,len))
      current_thd->cuted_fields++;
  }
  else
  {
    if (tmp < INT_MIN24)
    {
      tmp= INT_MIN24;
      current_thd->cuted_fields++;
    }
    else if (tmp > INT_MAX24)
    {
      tmp=INT_MAX24;
      current_thd->cuted_fields++;
    }
    else if (current_thd->count_cuted_fields && !test_if_int(from,len))
      current_thd->cuted_fields++;
  }

  int3store(ptr,tmp);
}


void Field_medium::store(double nr)
{
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      int3store(ptr,0);
      current_thd->cuted_fields++;
    }
    else if (nr >= (double) (long) (1L << 24))
    {
unknown's avatar
unknown committed
1359
      uint32 tmp=(uint32) (1L << 24)-1L;
unknown's avatar
unknown committed
1360 1361 1362 1363
      int3store(ptr,tmp);
      current_thd->cuted_fields++;
    }
    else
unknown's avatar
unknown committed
1364
      int3store(ptr,(uint32) nr);
unknown's avatar
unknown committed
1365 1366 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 1397 1398 1399 1400
  }
  else
  {
    if (nr < (double) INT_MIN24)
    {
      long tmp=(long) INT_MIN24;
      int3store(ptr,tmp);
      current_thd->cuted_fields++;
    }
    else if (nr > (double) INT_MAX24)
    {
      long tmp=(long) INT_MAX24;
      int3store(ptr,tmp);
      current_thd->cuted_fields++;
    }
    else
      int3store(ptr,(long) nr);
  }
}

void Field_medium::store(longlong nr)
{
  if (unsigned_flag)
  {
    if (nr < 0L)
    {
      int3store(ptr,0);
      current_thd->cuted_fields++;
    }
    else if (nr >= (longlong) (long) (1L << 24))
    {
      long tmp=(long) (1L << 24)-1L;;
      int3store(ptr,tmp);
      current_thd->cuted_fields++;
    }
    else
unknown's avatar
unknown committed
1401
      int3store(ptr,(uint32) nr);
unknown's avatar
unknown committed
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 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 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
  }
  else
  {
    if (nr < (longlong) INT_MIN24)
    {
      long tmp=(long) INT_MIN24;
      int3store(ptr,tmp);
      current_thd->cuted_fields++;
    }
    else if (nr > (longlong) INT_MAX24)
    {
      long tmp=(long) INT_MAX24;
      int3store(ptr,tmp);
      current_thd->cuted_fields++;
    }
    else
      int3store(ptr,(long) nr);
  }
}


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

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

String *Field_medium::val_str(String *val_buffer,
			      String *val_ptr __attribute__((unused)))
{
  uint length;
  val_buffer->alloc(max(field_length+1,10));
  char *to=(char*) val_buffer->ptr();
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);

  length=(uint) (int10_to_str(j,to,-10)-to);
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer); /* purecov: inspected */
  return val_buffer;
}


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
{
  sprintf((char*) res.ptr(),"mediumint(%d)",(int) field_length);
  add_zerofill_and_unsigned(res);
}

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


// Note:  Sometimes this should be fixed to use one strtol() to use
// len and check for garbage after number.

void Field_long::store(const char *from,uint len)
{
  while (len && isspace(*from))
  {
    len--; from++;
  }
  long tmp;
  String tmp_str(from,len);
  errno=0;
  if (unsigned_flag)
  {
    if (!len || *from == '-')
    {
      tmp=0;					// Set negative to 0
      errno=ERANGE;
    }
    else
      tmp=(long) strtoul(tmp_str.c_ptr(),NULL,10);
  }
  else
    tmp=strtol(tmp_str.c_ptr(),NULL,10);
  if (errno || current_thd->count_cuted_fields && !test_if_int(from,len))
    current_thd->cuted_fields++;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
}


void Field_long::store(double nr)
{
  int32 res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
      current_thd->cuted_fields++;
    }
    else if (nr > (double) (ulong) ~0L)
    {
      res=(int32) (uint32) ~0L;
      current_thd->cuted_fields++;
    }
    else
      res=(int32) (ulong) nr;
  }
  else
  {
    if (nr < (double) INT_MIN32)
    {
      res=(int32) INT_MIN32;
      current_thd->cuted_fields++;
    }
    else if (nr > (double) INT_MAX32)
    {
      res=(int32) INT_MAX32;
      current_thd->cuted_fields++;
    }
    else
      res=(int32) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,res);
  }
  else
#endif
    longstore(ptr,res);
}


void Field_long::store(longlong nr)
{
  int32 res;
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
      current_thd->cuted_fields++;
    }
    else if (nr >= (LL(1) << 32))
    {
      res=(int32) (uint32) ~0L;
      current_thd->cuted_fields++;
    }
    else
      res=(int32) (uint32) nr;
  }
  else
  {
    if (nr < (longlong) INT_MIN32)
    {
      res=(int32) INT_MIN32;
      current_thd->cuted_fields++;
    }
    else if (nr > (longlong) INT_MAX32)
    {
      res=(int32) INT_MAX32;
      current_thd->cuted_fields++;
    }
    else
      res=(int32) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,res);
  }
  else
#endif
    longstore(ptr,res);
}


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)))
{
  uint length;
  val_buffer->alloc(max(field_length+1,12));
  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);

  length=(uint) (int10_to_str((unsigned_flag ? (long) (uint32) j : (long) j),
			 to,
			 unsigned_flag ? 10 : -10)-to);
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


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
1679
    return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
unknown's avatar
unknown committed
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
  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
1691
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
    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
1702
      to[0] = (char) (ptr[3] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 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 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822
    to[1]   = ptr[2];
    to[2]   = ptr[1];
    to[3]   = ptr[0];
  }
}


void Field_long::sql_type(String &res) const
{
  sprintf((char*) res.ptr(),"int(%d)",(int) field_length);
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
** longlong int
****************************************************************************/

void Field_longlong::store(const char *from,uint len)
{
  while (len && isspace(*from))
  {						// For easy error check
    len--; from++;
  }
  longlong tmp;
  String tmp_str(from,len);
  errno=0;
  if (unsigned_flag)
  {
    if (!len || *from == '-')
    {
      tmp=0;					// Set negative to 0
      errno=ERANGE;
    }
    else
      tmp=(longlong) strtoull(tmp_str.c_ptr(),NULL,10);
  }
  else
    tmp=strtoll(tmp_str.c_ptr(),NULL,10);
  if (errno || current_thd->count_cuted_fields && !test_if_int(from,len))
    current_thd->cuted_fields++;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
}


void Field_longlong::store(double nr)
{
  longlong res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
      current_thd->cuted_fields++;
    }
    else if (nr >= (double) ~ (ulonglong) 0)
    {
      res= ~(longlong) 0;
      current_thd->cuted_fields++;
    }
    else
      res=(longlong) (ulonglong) nr;
  }
  else
  {
    if (nr <= (double) LONGLONG_MIN)
    {
      res=(longlong) LONGLONG_MIN;
      current_thd->cuted_fields++;
    }
    else if (nr >= (double) LONGLONG_MAX)
    {
      res=(longlong) LONGLONG_MAX;
      current_thd->cuted_fields++;
    }
    else
      res=(longlong) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,res);
  }
  else
#endif
    longlongstore(ptr,res);
}


void Field_longlong::store(longlong nr)
{
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,nr);
  }
  else
#endif
    longlongstore(ptr,nr);
}


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);
1823
  return unsigned_flag ? ulonglong2double((ulonglong) j) : (double) j;
unknown's avatar
unknown committed
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
}

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)))
{
  uint length;
  val_buffer->alloc(max(field_length+1,22));
  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);

  length=(uint) (longlong10_to_str(j,to,unsigned_flag ? 10 : -10)-to);
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


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
1890
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
    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
1905
      to[0] = (char) (ptr[7] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
    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
{
  sprintf((char*) res.ptr(),"bigint(%d)",(int) field_length);
  add_zerofill_and_unsigned(res);
}

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

void Field_float::store(const char *from,uint len)
{
  String tmp_str(from,len);
  errno=0;
  Field_float::store(atof(tmp_str.c_ptr()));
  if (errno || current_thd->count_cuted_fields && !test_if_real(from,len))
    current_thd->cuted_fields++;
}


void Field_float::store(double nr)
{
  float j;
  if (dec < NOT_FIXED_DEC)
    nr=floor(nr*log_10[dec]+0.5)/log_10[dec]; // To fixed point
unknown's avatar
unknown committed
1942 1943 1944 1945 1946
  if (unsigned_flag && nr < 0)
  {
    current_thd->cuted_fields++;
    nr=0;
  }
unknown's avatar
unknown committed
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
  if (nr < -FLT_MAX)
  {
    j= -FLT_MAX;
    current_thd->cuted_fields++;
  }
  else if (nr > FLT_MAX)
  {
    j=FLT_MAX;
    current_thd->cuted_fields++;
  }
  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));
}


void Field_float::store(longlong nr)
{
  float j= (float) nr;
unknown's avatar
unknown committed
1973 1974 1975 1976 1977
  if (unsigned_flag && j < 0)
  {
    current_thd->cuted_fields++;
    j=0;
  }
unknown's avatar
unknown committed
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4store(ptr,j);
  }
  else
#endif
    memcpy_fixed(ptr,(byte*) &j,sizeof(j));
}


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));

2031 2032
  uint to_length=max(field_length,70);
  val_buffer->alloc(to_length);
unknown's avatar
unknown committed
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
  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
2081
#else
2082 2083 2084
#ifdef HAVE_SNPRINTF
    to[to_length-1]=0;			// Safety
    snprintf(to,to_length-1,"%.*f",dec,nr);
unknown's avatar
unknown committed
2085 2086
#else
    sprintf(to,"%.*f",dec,nr);
unknown's avatar
unknown committed
2087
#endif
unknown's avatar
unknown committed
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
    to=strend(to);
#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
2150
	tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
unknown's avatar
unknown committed
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
    }
    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;
    }
  }
}


void Field_float::sql_type(String &res) const
{
  if (dec == NOT_FIXED_DEC)
    strmov((char*) res.ptr(),"float");
  else
    sprintf((char*) res.ptr(),"float(%d,%d)",(int) field_length,dec);
  add_zerofill_and_unsigned(res);
}

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

void Field_double::store(const char *from,uint len)
{
  String tmp_str(from,len);
  errno=0;
  double j= atof(tmp_str.c_ptr());
  if (errno || current_thd->count_cuted_fields && !test_if_real(from,len))
    current_thd->cuted_fields++;
unknown's avatar
unknown committed
2184 2185 2186 2187 2188
  if (unsigned_flag && j < 0)
  {
    current_thd->cuted_fields++;
    j=0;
  }
unknown's avatar
unknown committed
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8store(ptr,j);
  }
  else
#endif
    doublestore(ptr,j);
}


void Field_double::store(double nr)
{
  if (dec < NOT_FIXED_DEC)
    nr=floor(nr*log_10[dec]+0.5)/log_10[dec]; // To fixed point
unknown's avatar
unknown committed
2204 2205 2206 2207 2208
  if (unsigned_flag && nr < 0)
  {
    current_thd->cuted_fields++;
    nr=0;
  }
unknown's avatar
unknown committed
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8store(ptr,nr);
  }
  else
#endif
    doublestore(ptr,nr);
}


void Field_double::store(longlong nr)
{
  double j= (double) nr;
unknown's avatar
unknown committed
2223 2224 2225 2226 2227
  if (unsigned_flag && j < 0)
  {
    current_thd->cuted_fields++;
    j=0;
  }
unknown's avatar
unknown committed
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 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 2299 2300 2301 2302 2303 2304 2305 2306 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
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8store(ptr,j);
  }
  else
#endif
    doublestore(ptr,j);
}


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
2332
    to[to_length-1]=0;			// Safety
unknown's avatar
unknown committed
2333
    snprintf(to,to_length-1,"%.*f",dec,nr);
unknown's avatar
unknown committed
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
#else
    sprintf(to,"%.*f",dec,nr);
#endif
    to=strend(to);
#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_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
2363
/* could this ALWAYS be 2 calls to doubleget() ?? */
2364
#if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
unknown's avatar
unknown committed
2365 2366 2367
    doubleget(a, a_ptr);
    doubleget(b, b_ptr);
#else
unknown's avatar
unknown committed
2368 2369
    memcpy_fixed(&a,a_ptr,sizeof(double));
    memcpy_fixed(&b,b_ptr,sizeof(double));
unknown's avatar
unknown committed
2370
#endif
unknown's avatar
unknown committed
2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389
  }
  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
2390
/* could this ALWAYS be 2 calls to doubleget() ?? */
2391
#if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
unknown's avatar
unknown committed
2392 2393
    doubleget(nr,ptr);
#else
unknown's avatar
unknown committed
2394
    memcpy_fixed(&nr,ptr,sizeof(nr));
unknown's avatar
unknown committed
2395
#endif
unknown's avatar
unknown committed
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 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
  change_double_for_sort(nr, (byte*) to);
}


void Field_double::sql_type(String &res) const
{
  if (dec == NOT_FIXED_DEC)
    strmov((char*) res.ptr(),"double");
  else
    sprintf((char*) res.ptr(),"double(%d,%d)",(int) field_length,dec);
  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,
				 struct st_table *table_arg)
    :Field_num(ptr_arg, len_arg, (uchar*) 0,0,
	       unireg_check_arg, field_name_arg, table_arg,
	       0, 1, 1)
{
  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;
  }
}


void Field_timestamp::store(const char *from,uint len)
{
  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);
}

void Field_timestamp::fill_and_store(char *from,uint len)
{
  uint res_length;
  if (len <= field_length)
    res_length=field_length;
  else if (len <= 12)
    res_length=12;				/* purecov: inspected */
  else if (len <= 14)
    res_length=14;				/* purecov: inspected */
  else
    res_length=(len+1)/2*2;			// must be even
  if (res_length != len)
  {
    bmove_upp(from+res_length,from+len,len);
    bfill(from,res_length-len,'0');
    len=res_length;
  }
  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);
}


void Field_timestamp::store(double nr)
{
  if (nr < 0 || nr > 99991231235959.0)
  {
    nr=0;					// Avoid overflow on buff
    current_thd->cuted_fields++;
  }
  Field_timestamp::store((longlong) rint(nr));
}


/*
** Convert a datetime of formats YYMMDD, YYYYMMDD or YYMMDDHHMSS to
** YYYYMMDDHHMMSS.  The high date '99991231235959' is checked before this
** function.
*/

static longlong fix_datetime(longlong nr)
{
  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:
  current_thd->cuted_fields++;
  return LL(0);
}


void Field_timestamp::store(longlong nr)
{
  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
2533
    l_time.year=  (int) (part1/10000L);  part1%=10000L;
unknown's avatar
unknown committed
2534
    l_time.month= (int) part1 / 100;
unknown's avatar
unknown committed
2535 2536
    l_time.day=	  (int) part1 % 100; 
    l_time.hour=  (int) (part2/10000L);  part2%=10000L;
unknown's avatar
unknown committed
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549
    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
2550
    longstore(ptr,(uint32) timestamp);
unknown's avatar
unknown committed
2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666
}


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

longlong Field_timestamp::val_int(void)
{
  uint len,pos;
  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;
  res=(longlong) 0;
  for (pos=len=0; len+1 < (uint) field_length ; len+=2,pos++)
  {
    bool year_flag=0;
    switch (dayord.pos[pos]) {
    case 0: part_time=l_time->tm_year % 100; year_flag=1 ; break;
    case 1: part_time=l_time->tm_mon+1; break;
    case 2: part_time=l_time->tm_mday; break;
    case 3: part_time=l_time->tm_hour; break;
    case 4: part_time=l_time->tm_min; break;
    case 5: part_time=l_time->tm_sec; break;
    default: part_time=0; break; /* purecov: deadcode */
    }
    if (year_flag && (field_length == 8 || field_length == 14))
    {
      res=res*(longlong) 10000+(part_time+
				((part_time < YY_PART_YEAR) ? 2000 : 1900));
      len+=2;
    }
    else
      res=res*(longlong) 100+part_time;
  }
  return (longlong) res;
}


String *Field_timestamp::val_str(String *val_buffer,
				 String *val_ptr __attribute__((unused)))
{
  uint pos;
  int part_time;
  uint32 temp;
  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;

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

  if (temp == 0L)
  {				      /* Zero time is "000000" */
    VOID(strfill(to,field_length,'0'));
    val_buffer->length(field_length);
    return val_buffer;
  }
  time_arg=(time_t) temp;
  localtime_r(&time_arg,&tm_tmp);
  l_time=&tm_tmp;
  for (pos=0; to < end ; pos++)
  {
    bool year_flag=0;
    switch (dayord.pos[pos]) {
    case 0: part_time=l_time->tm_year % 100; year_flag=1; break;
    case 1: part_time=l_time->tm_mon+1; break;
    case 2: part_time=l_time->tm_mday; break;
    case 3: part_time=l_time->tm_hour; break;
    case 4: part_time=l_time->tm_min; break;
    case 5: part_time=l_time->tm_sec; break;
    default: part_time=0; break; /* purecov: deadcode */
    }
    if (year_flag && (field_length == 8 || field_length == 14))
    {
      if (part_time < YY_PART_YEAR)
      {
	*to++='2'; *to++='0'; /* purecov: inspected */
      }
      else
      {
	*to++='1'; *to++='9';
      }
    }
    *to++=(char) ('0'+((uint) part_time/10));
    *to++=(char) ('0'+((uint) part_time % 10));
  }
  *to=0;					// Safeguard
  val_buffer->length((uint) (to-val_buffer->ptr()));
  return val_buffer;
}

unknown's avatar
unknown committed
2667
bool Field_timestamp::get_date(TIME *ltime, bool fuzzydate)
unknown's avatar
unknown committed
2668 2669 2670 2671 2672 2673 2674 2675 2676 2677
{
  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
2678 2679
    if (!fuzzydate)
      return 1;
unknown's avatar
unknown committed
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695
    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;
2696
    ltime->time_type=TIMESTAMP_FULL;
unknown's avatar
unknown committed
2697 2698 2699 2700 2701 2702
  }
  return 0;
}

bool Field_timestamp::get_time(TIME *ltime)
{
unknown's avatar
unknown committed
2703
  return Field_timestamp::get_date(ltime,0);
unknown's avatar
unknown committed
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747
}

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;
}

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
{
  sprintf((char*) res.ptr(),"timestamp(%d)",(int) field_length);
unknown's avatar
unknown committed
2748
  res.length((uint) strlen(res.ptr()));
unknown's avatar
unknown committed
2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 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 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850
}


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
****************************************************************************/

void Field_time::store(const char *from,uint len)
{
  TIME ltime;
  long tmp;
  if (str_to_time(from,len,&ltime))
    tmp=0L;
  else
  {
    if (ltime.month)
      ltime.day=0;
    tmp=(ltime.day*24L+ltime.hour)*10000L+(ltime.minute*100+ltime.second);
    if (tmp > 8385959)
    {
      tmp=8385959;
      current_thd->cuted_fields++;
    }
  }
  if (ltime.neg)
    tmp= -tmp;
  Field_time::store((longlong) tmp);
}


void Field_time::store(double nr)
{
  long tmp;
  if (nr > 8385959.0)
  {
    tmp=8385959L;
    current_thd->cuted_fields++;
  }
  else if (nr < -8385959.0)
  {
    tmp= -8385959L;
    current_thd->cuted_fields++;
  }
  else
  {
    tmp=(long) floor(fabs(nr));			// Remove fractions
    if (nr < 0)
      tmp= -tmp;
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
    {
      tmp=0;
      current_thd->cuted_fields++;
    }
  }
  int3store(ptr,tmp);
}


void Field_time::store(longlong nr)
{
  long tmp;
  if (nr > (longlong) 8385959L)
  {
    tmp=8385959L;
    current_thd->cuted_fields++;
  }
  else if (nr < (longlong) -8385959L)
  {
    tmp= -8385959L;
    current_thd->cuted_fields++;
  }
  else
  {
    tmp=(long) nr;
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
    {
      tmp=0;
      current_thd->cuted_fields++;
    }
  }
  int3store(ptr,tmp);
}


double Field_time::val_real(void)
{
unknown's avatar
unknown committed
2851
  uint32 j= (uint32) uint3korr(ptr);
unknown's avatar
unknown committed
2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873
  return (double) j;
}

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

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= "-";
  }
  sprintf((char*) val_buffer->ptr(),"%s%02d:%02d:%02d",
	  sign,(int) (tmp/10000), (int) (tmp/100 % 100),
	  (int) (tmp % 100));
unknown's avatar
unknown committed
2874
  val_buffer->length((uint) strlen(val_buffer->ptr()));
unknown's avatar
unknown committed
2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886
  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
2887
  ltime->hour=   (int) (tmp/10000);
unknown's avatar
unknown committed
2888
  tmp-=ltime->hour*10000;
unknown's avatar
unknown committed
2889 2890
  ltime->minute= (int) tmp/100;
  ltime->second= (int) tmp % 100;
unknown's avatar
unknown committed
2891 2892 2893 2894 2895 2896
  ltime->second_part=0;
  return 0;
}

int Field_time::cmp(const char *a_ptr, const char *b_ptr)
{
unknown's avatar
unknown committed
2897 2898 2899
  int32 a,b;
  a=(int32) sint3korr(a_ptr);
  b=(int32) sint3korr(b_ptr);
unknown's avatar
unknown committed
2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998
  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
{
  res.set("time",4);
}

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

void Field_year::store(const char *from, uint len)
{
  String tmp_str(from,len);
  long nr= strtol(tmp_str.c_ptr(),NULL,10);

  if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155)
  {
    *ptr=0;
    current_thd->cuted_fields++;
    return;
  }
  else if (current_thd->count_cuted_fields && !test_if_int(from,len))
    current_thd->cuted_fields++;
  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;
}

void Field_year::store(double nr)
{
  if (nr < 0.0 || nr >= 2155.0)
    Field_year::store((longlong) -1);
  else
    Field_year::store((longlong) nr);
}

void Field_year::store(longlong nr)
{
  if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155)
  {
    *ptr=0;
    current_thd->cuted_fields++;
    return;
  }
  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;
}


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
{
  sprintf((char*) res.ptr(),"year(%d)",(int) field_length);
unknown's avatar
unknown committed
2999
  res.length((uint) strlen(res.ptr()));
unknown's avatar
unknown committed
3000 3001 3002 3003 3004 3005 3006 3007 3008 3009
}


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

unknown's avatar
unknown committed
3010
void Field_date::store(const char *from, uint len)
unknown's avatar
unknown committed
3011 3012
{
  TIME l_time;
unknown's avatar
unknown committed
3013
  uint32 tmp;
unknown's avatar
unknown committed
3014 3015 3016
  if (str_to_TIME(from,len,&l_time,1) == TIMESTAMP_NONE)
    tmp=0;
  else
unknown's avatar
unknown committed
3017
    tmp=(uint32) l_time.year*10000L + (uint32) (l_time.month*100+l_time.day);
unknown's avatar
unknown committed
3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
}


void Field_date::store(double nr)
{
  long tmp;
  if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
    nr=floor(nr/1000000.0);			// Timestamp to date
  if (nr < 0.0 || nr > 99991231.0)
  {
    tmp=0L;
    current_thd->cuted_fields++;
  }
  else
    tmp=(long) rint(nr);
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
}


void Field_date::store(longlong nr)
{
  long tmp;
  if (nr >= LL(19000000000000) && nr < LL(99991231235959))
    nr=nr/LL(1000000);			// Timestamp to date
  if (nr < 0 || nr > LL(99991231))
  {
    tmp=0L;
    current_thd->cuted_fields++;
  }
  else
    tmp=(long) nr;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
}


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
{
  res.set("date",4);
}

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

void Field_newdate::store(const char *from,uint len)
{
  TIME l_time;
  long tmp;
  if (str_to_TIME(from,len,&l_time,1) == TIMESTAMP_NONE)
    tmp=0L;
  else
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
  int3store(ptr,tmp);
}

void Field_newdate::store(double nr)
{
  if (nr < 0.0 || nr > 99991231235959.0)
    Field_newdate::store((longlong) -1);
  else
    Field_newdate::store((longlong) rint(nr));
}


void Field_newdate::store(longlong nr)
{
unknown's avatar
unknown committed
3189
  int32 tmp;
unknown's avatar
unknown committed
3190 3191 3192 3193 3194 3195 3196 3197 3198
  if (nr >= LL(100000000) && nr <= LL(99991231235959))
    nr=nr/LL(1000000);			// Timestamp to date
  if (nr < 0L || nr > 99991231L)
  {
    tmp=0;
    current_thd->cuted_fields++;
  }
  else
  {
unknown's avatar
unknown committed
3199
    tmp=(int32) nr;
unknown's avatar
unknown committed
3200 3201 3202
    if (tmp)
    {
      if (tmp < YY_PART_YEAR*10000L)			// Fix short dates
unknown's avatar
unknown committed
3203
	tmp+= (uint32) 20000000L;
unknown's avatar
unknown committed
3204
      else if (tmp < 999999L)
unknown's avatar
unknown committed
3205
	tmp+= (uint32) 19000000L;
unknown's avatar
unknown committed
3206
    }
unknown's avatar
unknown committed
3207 3208
    uint month= (uint) ((tmp/100) % 100);
    uint day=   (uint) (tmp%100);
unknown's avatar
unknown committed
3209 3210 3211 3212 3213 3214 3215 3216
    if (month > 12 || day > 31)
    {
      tmp=0L;					// Don't allow date to change
      current_thd->cuted_fields++;
    }
    else
      tmp= day + month*32 + (tmp/10000)*16*32;
  }
unknown's avatar
unknown committed
3217
  int3store(ptr,(int32) tmp);
unknown's avatar
unknown committed
3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241
}

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;
    current_thd->cuted_fields++;
  }
  int3store(ptr,tmp);
}



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

longlong Field_newdate::val_int(void)
{
unknown's avatar
unknown committed
3242
  ulong j= uint3korr(ptr);
unknown's avatar
unknown committed
3243 3244 3245 3246 3247 3248 3249 3250 3251
  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
3252
  uint32 tmp=(uint32) uint3korr(ptr);
unknown's avatar
unknown committed
3253 3254 3255 3256
  int part;
  char *pos=(char*) val_buffer->ptr()+10;

  /* Open coded to get more speed */
unknown's avatar
unknown committed
3257
  *pos--=0;					// End NULL
unknown's avatar
unknown committed
3258
  part=(int) (tmp & 31);
unknown's avatar
unknown committed
3259 3260 3261
  *pos--= (char) ('0'+part%10);
  *pos--= (char) ('0'+part/10);
  *pos--= '-';
unknown's avatar
unknown committed
3262
  part=(int) (tmp >> 5 & 15);
unknown's avatar
unknown committed
3263 3264 3265
  *pos--= (char) ('0'+part%10);
  *pos--= (char) ('0'+part/10);
  *pos--= '-';
unknown's avatar
unknown committed
3266
  part=(int) (tmp >> 9);
unknown's avatar
unknown committed
3267 3268 3269 3270
  *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
3271 3272 3273 3274 3275 3276 3277
  return val_buffer;
}

bool Field_newdate::get_date(TIME *ltime,bool fuzzydate)
{
  if (is_null())
    return 1;
unknown's avatar
unknown committed
3278
  uint32 tmp=(uint32) uint3korr(ptr);
unknown's avatar
unknown committed
3279 3280 3281 3282
  bzero((char*) ltime,sizeof(*ltime));
  ltime->day=   tmp & 31;
  ltime->month= (tmp >> 5) & 15;
  ltime->year=  (tmp >> 9);
3283
  ltime->time_type=TIMESTAMP_DATE;
unknown's avatar
unknown committed
3284
  return (!fuzzydate && (!ltime->month || !ltime->day)) ? 1 : 0;
unknown's avatar
unknown committed
3285 3286 3287 3288
}

bool Field_newdate::get_time(TIME *ltime)
{
unknown's avatar
unknown committed
3289
  return Field_newdate::get_date(ltime,0);
unknown's avatar
unknown committed
3290 3291 3292 3293
}

int Field_newdate::cmp(const char *a_ptr, const char *b_ptr)
{
unknown's avatar
unknown committed
3294 3295 3296
  uint32 a,b;
  a=(uint32) uint3korr(a_ptr);
  b=(uint32) uint3korr(b_ptr);
unknown's avatar
unknown committed
3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 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
  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
{
  res.set("date",4);
}


/****************************************************************************
** 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.
****************************************************************************/

void Field_datetime::store(const char *from,uint len)
{
  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);
}


void Field_datetime::store(double nr)
{
  if (nr < 0.0 || nr > 99991231235959.0)
  {
    nr=0.0;
    current_thd->cuted_fields++;
  }
  Field_datetime::store((longlong) rint(nr));
}


void Field_datetime::store(longlong nr)
{
  if (nr < 0 || nr > LL(99991231235959))
  {
    nr=0;
    current_thd->cuted_fields++;
  }
  else
    nr=fix_datetime(nr);
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,nr);
  }
  else
#endif
    longlongstore(ptr,nr);
}

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;
    current_thd->cuted_fields++;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
}


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
3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448
  *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
3449 3450 3451 3452 3453 3454
  return val_buffer;
}

bool Field_datetime::get_date(TIME *ltime,bool fuzzydate)
{
  longlong tmp=Field_datetime::val_int();
unknown's avatar
unknown committed
3455 3456 3457
  uint32 part1,part2;
  part1=(uint32) (tmp/LL(1000000));
  part2=(uint32) (tmp - (ulonglong) part1*LL(1000000));
unknown's avatar
unknown committed
3458

3459
  ltime->time_type=	TIMESTAMP_FULL;
unknown's avatar
unknown committed
3460 3461 3462 3463 3464 3465 3466 3467
  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
3468
  return (!fuzzydate && (!ltime->month || !ltime->day)) ? 1 : 0;
unknown's avatar
unknown committed
3469 3470 3471 3472
}

bool Field_datetime::get_time(TIME *ltime)
{
unknown's avatar
unknown committed
3473
  return Field_datetime::get_date(ltime,0);
unknown's avatar
unknown committed
3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538
}

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
{
  res.set("datetime",8);
}

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

	/* Copy a string and fill with space */

void Field_string::store(const char *from,uint length)
{
#ifdef USE_TIS620
3539
  if (!binary_flag) {
unknown's avatar
unknown committed
3540
    ThNormalize((uchar *)ptr, field_length, (uchar *)from, length);
3541
    if (length < field_length) {
unknown's avatar
unknown committed
3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585
      bfill(ptr + length, field_length - length, ' ');
    }
  }
#else
  if (length <= field_length)
  {
    memcpy(ptr,from,length);
    if (length < field_length)
      bfill(ptr+length,field_length-length,' ');
  }
  else
  {
    memcpy(ptr,from,field_length);
    if (current_thd->count_cuted_fields)
    {						// Check if we loosed some info
      const char *end=from+length;
      for (from+=field_length ; from != end ; from++)
      {
	if (!isspace(*from))
	{
	  current_thd->cuted_fields++;
	  break;
	}
      }
    }
  }
#endif /* USE_TIS620 */
}


void Field_string::store(double nr)
{
  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,' ');
  Field_string::store(buff,(uint) (end - buff));
}


void Field_string::store(longlong nr)
{
  char buff[22];
  char *end=longlong10_to_str(nr,buff,-10);
unknown's avatar
unknown committed
3586
  Field_string::store(buff,(uint) (end-buff));
unknown's avatar
unknown committed
3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662
}


double Field_string::val_real(void)
{
  double value;
  char save=ptr[field_length];			// Ok to patch record
  ptr[field_length]=0;
  value=atof(ptr);
  ptr[field_length]=save;
  return value;
}


longlong Field_string::val_int(void)
{
  longlong value;
  char save=ptr[field_length];			// Ok to patch record
  ptr[field_length]=0;
  value=strtoll(ptr,NULL,10);
  ptr[field_length]=save;
  return value;
}


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--;
  val_ptr->set((const char*) ptr,(uint) (end - ptr));
  return val_ptr;
}


int Field_string::cmp(const char *a_ptr, const char *b_ptr)
{
  if (binary_flag)
    return memcmp(a_ptr,b_ptr,field_length);
  else
    return my_sortcmp(a_ptr,b_ptr,field_length);
}

void Field_string::sort_string(char *to,uint length)
{
  if (binary_flag)
    memcpy((byte*) to,(byte*) ptr,(size_t) length);
  else
  {
#ifdef USE_STRCOLL
    if (use_strcoll(default_charset_info)) {
      uint tmp=my_strnxfrm(default_charset_info,
                          (unsigned char *)to, (unsigned char *) ptr,
                          length, field_length);
      if (tmp < length)
        bzero(to + tmp, length - tmp);
    }
    else
#endif
      for (char *from=ptr,*end=ptr+length ; from != end ;)
        *to++=(char) my_sort_order[(uint) (uchar) *from++];
  }
}


void Field_string::sql_type(String &res) const
{
  sprintf((char*) res.ptr(),"%s(%d)",
	  field_length > 3 &&
	  (table->db_options_in_use & HA_OPTION_PACK_RECORD) ?
	  "varchar" : "char",
	  (int) field_length);
unknown's avatar
unknown committed
3663
  res.length((uint) strlen(res.ptr()));
unknown's avatar
unknown committed
3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703
  if (binary_flag)
    res.append(" binary");
}


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++;

  if (binary_flag)
  {
    int cmp= memcmp(a,b,min(a_length,b_length));
    return cmp ? cmp : (int) (a_length - b_length);
  }
  return my_sortncmp(a,a_length, b,b_length);
}


3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720
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);

  if (binary_flag)
  {
    int cmp= memcmp(ptr,b,min(a_length,b_length));
    return cmp ? cmp : (int) (a_length - b_length);
  }
  return my_sortncmp(ptr,a_length, b, b_length);
}


3721
uint Field_string::packed_col_length(const char *ptr, uint length)
unknown's avatar
unknown committed
3722
{
3723
  if (length > 255)
unknown's avatar
unknown committed
3724 3725 3726 3727 3728 3729 3730
    return uint2korr(ptr)+2;
  else
    return (uint) ((uchar) *ptr)+1;
}

uint Field_string::max_packed_col_length(uint max_length)
{
3731
  return (max_length > 255 ? 2 : 1)+max_length;
unknown's avatar
unknown committed
3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742
}


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


void Field_varstring::store(const char *from,uint length)
{
#ifdef USE_TIS620
3743
  if (!binary_flag)
unknown's avatar
unknown committed
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
  {
    ThNormalize((uchar *) ptr+2, field_length, (uchar *) from, length);
  }
#else
  if (length <= field_length)
  {
    memcpy(ptr+2,from,length);
  }
  else
  {
    length=field_length;
    memcpy(ptr+2,from,field_length);
    current_thd->cuted_fields++;
  }
#endif /* USE_TIS620 */
  int2store(ptr,length);
}


void Field_varstring::store(double nr)
{
  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,' ');
  Field_varstring::store(buff,(uint) (end - buff));
}


void Field_varstring::store(longlong nr)
{
  char buff[22];
  char *end=longlong10_to_str(nr,buff,-10);
unknown's avatar
unknown committed
3777
  Field_varstring::store(buff,(uint) (end-buff));
unknown's avatar
unknown committed
3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 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
}


double Field_varstring::val_real(void)
{
  double value;
  uint length=uint2korr(ptr)+2;
  char save=ptr[length];			// Ok to patch record
  ptr[length]=0;
  value=atof(ptr+2);
  ptr[length]=save;
  return value;
}


longlong Field_varstring::val_int(void)
{
  longlong value;
  uint length=uint2korr(ptr)+2;
  char save=ptr[length];			// Ok to patch record
  ptr[length]=0;
  value=strtoll(ptr+2,NULL,10);
  ptr[length]=save;
  return value;
}


String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
				 String *val_ptr)
{
  uint length=uint2korr(ptr);
  val_ptr->set((const char*) ptr+2,length);
  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;
  if (binary_flag)
    diff=memcmp(a_ptr+2,b_ptr+2,min(a_length,b_length));
  else
    diff=my_sortcmp(a_ptr+2,b_ptr+2,min(a_length,b_length));
  return diff ? diff : (int) (a_length - b_length);
}

void Field_varstring::sort_string(char *to,uint length)
{
  uint tot_length=uint2korr(ptr);
  if (binary_flag)
    memcpy((byte*) to,(byte*) ptr+2,(size_t) tot_length);
  else
  {
#ifdef USE_STRCOLL
    if (use_strcoll(default_charset_info))
      tot_length=my_strnxfrm(default_charset_info,
                             (unsigned char *) to, (unsigned char *)ptr+2,
                             length, tot_length);
    else
    {
#endif
      char *tmp=to;
      if (tot_length > length)
        tot_length=length;
      for (char *from=ptr+2,*end=from+tot_length ; from != end ;)
        *tmp++=(char) my_sort_order[(uint) (uchar) *from++];
#ifdef USE_STRCOLL
    }
#endif
  }
  if (tot_length < length)
    bzero(to+tot_length,length-tot_length);
}


void Field_varstring::sql_type(String &res) const
{
  sprintf((char*) res.ptr(),"varchar(%d)",(int) field_length);
unknown's avatar
unknown committed
3858
  res.length((uint) strlen(res.ptr()));
unknown's avatar
unknown committed
3859 3860 3861 3862 3863 3864
  if (binary_flag)
    res.append(" binary");
}

char *Field_varstring::pack(char *to, const char *from, uint max_length)
{
3865
  uint length=uint2korr(from);
unknown's avatar
unknown committed
3866 3867
  if (length > max_length)
    length=max_length;
unknown's avatar
unknown committed
3868
  *to++= (char) (length & 255);
unknown's avatar
unknown committed
3869
  if (max_length > 255)
unknown's avatar
unknown committed
3870
    *to++= (char) (length >> 8);
unknown's avatar
unknown committed
3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918
  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++;
  }
  if (binary_flag)
  {
    int cmp= memcmp(a,b,min(a_length,b_length));
    return cmp ? cmp : (int) (a_length - b_length);
  }
  return my_sortncmp(a,a_length, b,b_length);
}

3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939
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++;
  }
  if (binary_flag)
  {
    int cmp= memcmp(a,b,min(a_length,b_length));
    return cmp ? cmp : (int) (a_length - b_length);
  }
  return my_sortncmp(a,a_length, b,b_length);
}

3940
uint Field_varstring::packed_col_length(const char *ptr, uint length)
unknown's avatar
unknown committed
3941
{
3942
  if (length > 255)
unknown's avatar
unknown committed
3943 3944 3945 3946 3947 3948 3949
    return uint2korr(ptr)+2;
  else
    return (uint) ((uchar) *ptr)+1;
}

uint Field_varstring::max_packed_col_length(uint max_length)
{
3950
  return (max_length > 255 ? 2 : 1)+max_length;
unknown's avatar
unknown committed
3951 3952 3953 3954 3955 3956 3957 3958
}

/****************************************************************************
** 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
3959
Field_blob::Field_blob(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975
		       enum utype unireg_check_arg, const char *field_name_arg,
		       struct st_table *table_arg,uint blob_pack_length,
		       bool binary_arg)
  :Field_str(ptr_arg, (1L << min(blob_pack_length,3)*8)-1L,
	     null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
	     table_arg),
   packlength(blob_pack_length),binary_flag(binary_arg)
{
  flags|= BLOB_FLAG;
  if (binary_arg)
    flags|=BINARY_FLAG;
  if (table)
    table->blob_fields++;
}


unknown's avatar
unknown committed
3976
void Field_blob::store_length(uint32 number)
unknown's avatar
unknown committed
3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002
{
  switch (packlength) {
  case 1:
    if (number > 255)
    {
      number=255;
      current_thd->cuted_fields++;
    }
    ptr[0]= (uchar) number;
    break;
  case 2:
    if (number > (uint16) ~0)
    {
      number= (uint16) ~0;
      current_thd->cuted_fields++;
    }
#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
4003
    if (number > (uint32) (1L << 24))
unknown's avatar
unknown committed
4004
    {
unknown's avatar
unknown committed
4005
      number= (uint32) (1L << 24)-1L;
unknown's avatar
unknown committed
4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022
      current_thd->cuted_fields++;
    }
    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
4023
uint32 Field_blob::get_length(const char *pos)
unknown's avatar
unknown committed
4024 4025 4026
{
  switch (packlength) {
  case 1:
unknown's avatar
unknown committed
4027
    return (uint32) (uchar) pos[0];
unknown's avatar
unknown committed
4028 4029 4030 4031 4032 4033 4034 4035 4036
  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
4037
      return (uint32) tmp;
unknown's avatar
unknown committed
4038 4039
    }
  case 3:
unknown's avatar
unknown committed
4040
    return (uint32) uint3korr(pos);
unknown's avatar
unknown committed
4041 4042 4043 4044 4045 4046 4047 4048 4049
  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
4050
      return (uint32) tmp;
unknown's avatar
unknown committed
4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071
    }
  }
  return 0;					// Impossible
}


void Field_blob::store(const char *from,uint len)
{
  if (!len)
  {
    bzero(ptr,Field_blob::pack_length());
  }
  else
  {
#ifdef USE_TIS620
    char *th_ptr=0;
#endif
    Field_blob::store_length(len);
    if (table->copy_blobs || len <= MAX_FIELD_WIDTH)
    {						// Must make a copy
#ifdef USE_TIS620
4072
      if (!binary_flag)
unknown's avatar
unknown committed
4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095
      {
	/* If there isn't enough memory, use original string */
	if ((th_ptr=(char * ) my_malloc(sizeof(char) * len,MYF(0))))
	{
	  ThNormalize((uchar *) th_ptr, len, (uchar *) from, len);
	  from= (const char*) th_ptr;
	}
      }
#endif /* USE_TIS620 */
      value.copy(from,len);
      from=value.ptr();
#ifdef USE_TIS620
      my_free(th_ptr,MYF(MY_ALLOW_ZERO_PTR));
#endif
    }
    bmove(ptr+packlength,(char*) &from,sizeof(char*));
  }
}


void Field_blob::store(double nr)
{
  value.set(nr);
unknown's avatar
unknown committed
4096
  Field_blob::store(value.ptr(),(uint) value.length());
unknown's avatar
unknown committed
4097 4098 4099 4100 4101 4102
}


void Field_blob::store(longlong nr)
{
  value.set(nr);
unknown's avatar
unknown committed
4103
  Field_blob::store(value.ptr(), (uint) value.length());
unknown's avatar
unknown committed
4104 4105 4106 4107 4108 4109 4110 4111 4112 4113
}


double Field_blob::val_real(void)
{
  char *blob;

  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
    return 0.0;
unknown's avatar
unknown committed
4114
  uint32 length=get_length(ptr);
unknown's avatar
unknown committed
4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129

  char save=blob[length];			// Ok to patch blob in NISAM
  blob[length]=0;
  double nr=atof(blob);
  blob[length]=save;
  return nr;
}


longlong Field_blob::val_int(void)
{
  char *blob;
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
    return 0;
unknown's avatar
unknown committed
4130
  uint32 length=get_length(ptr);
unknown's avatar
unknown committed
4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145

  char save=blob[length];			// Ok to patch blob in NISAM
  blob[length]=0;
  longlong nr=strtoll(blob,NULL,10);
  blob[length]=save;
  return nr;
}


String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
			    String *val_ptr)
{
  char *blob;
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
4146
    val_ptr->set("",0);				// A bit safer than ->length(0)
unknown's avatar
unknown committed
4147 4148 4149 4150 4151 4152
  else
    val_ptr->set((const char*) blob,get_length(ptr));
  return val_ptr;
}


unknown's avatar
unknown committed
4153 4154
int Field_blob::cmp(const char *a,uint32 a_length, const char *b,
		    uint32 b_length)
unknown's avatar
unknown committed
4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187
{
  int diff;
  if (binary_flag)
    diff=memcmp(a,b,min(a_length,b_length));
  else
    diff=my_sortcmp(a,b,min(a_length,b_length));
  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
4188
			   uint32 max_length)
unknown's avatar
unknown committed
4189 4190 4191
{
  char *a,*b;
  uint diff;
unknown's avatar
unknown committed
4192
  uint32 a_length,b_length;
unknown's avatar
unknown committed
4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210
  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 */

void Field_blob::get_key_image(char *buff,uint length)
{
  length-=HA_KEY_BLOB_LENGTH;
unknown's avatar
unknown committed
4211
  uint32 blob_length=get_length(ptr);
unknown's avatar
unknown committed
4212
  char *blob;
unknown's avatar
unknown committed
4213
  if ((uint32) length > blob_length)
unknown's avatar
unknown committed
4214 4215 4216 4217
  {
#ifdef HAVE_purify
    bzero(buff+2+blob_length, (length-blob_length));
#endif
unknown's avatar
unknown committed
4218
    length=(uint) blob_length;
unknown's avatar
unknown committed
4219
  }
unknown's avatar
unknown committed
4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297
  int2store(buff,length);
  get_ptr(&blob);
  memcpy(buff+2,blob,length);
}

void Field_blob::set_key_image(char *buff,uint length)
{
  length=uint2korr(buff);
  Field_blob::store(buff+2,length);
}

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();
#ifdef USE_STRCOLL
  uint blob_org_length=blob_length;
#endif
  if (!blob_length)
    bzero(to,length);
  else
  {
    if (blob_length > length)
      blob_length=length;
    memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
    if (binary_flag)
    {
      memcpy(to,blob,blob_length);
      to+=blob_length;
    }
    else
    {
#ifdef USE_STRCOLL
      if (use_strcoll(default_charset_info))
      {
        blob_length=my_strnxfrm(default_charset_info,
                                (unsigned char *)to,(unsigned char *)blob,
                                length,blob_org_length);
        if (blob_length >= length)
          return;
        to+=blob_length;
      }
      else
#endif
        for (char *end=blob+blob_length ; blob != end ;)
          *to++=(char) my_sort_order[(uint) (uchar) *blob++];
    }
    bzero(to,length-blob_length);
  }
}


void Field_blob::sql_type(String &res) const
{
  const char *str;
  switch (packlength) {
  default: str="tiny"; break;
  case 2:  str=""; break;
  case 3:  str="medium"; break;
  case 4:  str="long"; break;
  }
unknown's avatar
unknown committed
4298
  res.set(str,(uint) strlen(str));
unknown's avatar
unknown committed
4299 4300 4301 4302
  res.append(binary_flag ? "blob" : "text");
}


4303 4304 4305 4306
char *Field_blob::pack(char *to, const char *from, uint max_length)
{
  char *save=ptr;
  ptr=(char*) from;
unknown's avatar
unknown committed
4307
  uint32 length=get_length();			// Length of from string
4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329
  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
4330
  uint32 length=get_length(from);
4331 4332 4333 4334 4335 4336 4337 4338
  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
4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362
/* 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++;
  }
  if (binary_flag)
  {
    int cmp= memcmp(a,b,min(a_length,b_length));
    return cmp ? cmp : (int) (a_length - b_length);
  }
  return my_sortncmp(a,a_length, b,b_length);
}

4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388

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++;
  }
  if (binary_flag)
  {
    int cmp= memcmp(a,b,min(a_length,b_length));
    return cmp ? cmp : (int) (a_length - b_length);
  }
  return my_sortncmp(a,a_length, b,b_length);
}

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

unknown's avatar
unknown committed
4391 4392
char *Field_blob::pack_key(char *to, const char *from, uint max_length)
{
4393 4394
  char *save=ptr;
  ptr=(char*) from;
unknown's avatar
unknown committed
4395
  uint32 length=get_length();			// Length of from string
4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415
  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
4416 4417
  if (length > max_length)
    length=max_length;
unknown's avatar
unknown committed
4418
  *to++= (char) (length & 255);
unknown's avatar
unknown committed
4419
  if (max_length > 255)
unknown's avatar
unknown committed
4420
    *to++= (char) (length >> 8);
unknown's avatar
unknown committed
4421 4422 4423 4424 4425
  if (length)
    memcpy(to, from+2, length);
  return to+length;
}

4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437
uint Field_blob::packed_col_length(const char *ptr, uint length)
{
  if (length > 255)
    return uint2korr(ptr)+2;
  else
    return (uint) ((uchar) *ptr)+1;
}

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

unknown's avatar
unknown committed
4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 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 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519
/****************************************************************************
** 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;
  while (end > x && isspace(end[-1]))
    end--;

  const char *i;
  const char *j;
  for (uint pos=0 ; (j=lib->type_names[pos]) ; pos++)
  {
    for (i=x ; i != end && toupper(*i) == toupper(*j) ; i++, j++) ;
    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)
*/

void Field_enum::store(const char *from,uint length)
{
  uint tmp=find_enum(typelib,from,length);
unknown's avatar
unknown committed
4520
  if (!tmp)
unknown's avatar
unknown committed
4521
  {
unknown's avatar
unknown committed
4522
    if (length < 6)			// Can't be more than 99999 enums
unknown's avatar
unknown committed
4523
    {
unknown's avatar
unknown committed
4524 4525 4526 4527 4528 4529 4530 4531 4532
      /* This is for reading numbers with LOAD DATA INFILE */
      char buff[7], *end;
      const char *conv=from;
      if (from[length])
      {
	strmake(buff, from, length);
	conv=buff;
      }
      my_errno=0;
unknown's avatar
unknown committed
4533
      tmp=(uint) strtoul(conv,&end,10);
unknown's avatar
unknown committed
4534 4535 4536 4537 4538
      if (my_errno || end != conv+length || tmp > typelib->count)
      {
	tmp=0;
	current_thd->cuted_fields++;
      }
unknown's avatar
unknown committed
4539 4540
    }
    else
unknown's avatar
unknown committed
4541
      current_thd->cuted_fields++;
unknown's avatar
unknown committed
4542
  }
unknown's avatar
unknown committed
4543
  store_type((ulonglong) tmp);
unknown's avatar
unknown committed
4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622
}


void Field_enum::store(double nr)
{
  Field_enum::store((longlong) nr);
}


void Field_enum::store(longlong nr)
{
  if ((uint) nr > typelib->count || nr == 0)
  {
    current_thd->cuted_fields++;
    nr=0;
  }
  store_type((ulonglong) (uint) nr);
}


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],
unknown's avatar
unknown committed
4623
		 (uint) strlen(typelib->type_names[tmp-1]));
unknown's avatar
unknown committed
4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691
  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(',');
    res.append('\'');
    append_unescaped(&res,*pos);
    res.append('\'');
    flag=1;
  }
  res.append(')');
}


/****************************************************************************
** 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.
****************************************************************************/

ulonglong find_set(TYPELIB *lib,const char *x,uint length)
{
  const char *end=x+length;
  while (end > x && isspace(end[-1]))
    end--;

  ulonglong found=0;
  if (x != end)
  {
    const char *start=x;
    bool error=0;
    for (;;)
    {
      const char *pos=start;
4692
      for (; pos != end && *pos != field_separator ; pos++) ;
unknown's avatar
unknown committed
4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710
      uint find=find_enum(lib,start,(uint) (pos-start));
      if (!find)
	error=1;
      else
	found|= ((longlong) 1 << (find-1));
      if (pos == end)
	break;
      start=pos+1;
    }
    if (error)
      current_thd->cuted_fields++;
  }
  return found;
}


void Field_set::store(const char *from,uint length)
{
unknown's avatar
unknown committed
4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730
  ulonglong tmp=find_set(typelib,from,length);
  if (!tmp && length && length < 22)
  {
    /* This is for reading numbers with LOAD DATA INFILE */
    char buff[22], *end;
    const char *conv=from;
    if (from[length])
    {
      strmake(buff, from, length);
      conv=buff;
    }
    my_errno=0;
    tmp=strtoull(conv,&end,10);
    if (my_errno || end != conv+length ||
	tmp > (ulonglong) (((longlong) 1 << typelib->count) - (longlong) 1))
      tmp=0;
    else
      current_thd->cuted_fields--;		// Remove warning from find_set
  }
  store_type(tmp);
unknown's avatar
unknown committed
4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759
}


void Field_set::store(longlong nr)
{
  if ((ulonglong) nr > (ulonglong) (((longlong) 1 << typelib->count) -
				    (longlong) 1))
  {
    nr&= (longlong) (((longlong) 1 << typelib->count) - (longlong) 1);
    current_thd->cuted_fields++;
  }
  store_type((ulonglong) nr);
}


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],
unknown's avatar
unknown committed
4760
		 (uint) strlen(typelib->type_names[bitnr]));
unknown's avatar
unknown committed
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 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802
      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(',');
    res.append('\'');
    append_unescaped(&res,*pos);
    res.append('\'');
    flag=1;
  }
  res.append(')');
}

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

bool Field::eq_def(Field *field)
{
  if (real_type() != field->real_type() || binary() != field->binary() ||
      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
4803

unknown's avatar
unknown committed
4804 4805 4806 4807 4808 4809 4810 4811 4812
  if (typelib->count < from_lib->count)
    return 0;
  for (uint i=0 ; i < from_lib->count ; i++)
    if (my_strcasecmp(typelib->type_names[i],from_lib->type_names[i]))
      return 0;
  return 1;
}

bool Field_num::eq_def(Field *field)
unknown's avatar
unknown committed
4813
{
unknown's avatar
unknown committed
4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830
  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
4831
  Make a field from the .frm file info
unknown's avatar
unknown committed
4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859
*/

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;
  case FIELD_TYPE_BLOB:		return 2+portable_sizeof_char_ptr;
  case FIELD_TYPE_MEDIUM_BLOB:	return 3+portable_sizeof_char_ptr;
  case FIELD_TYPE_LONG_BLOB:	return 4+portable_sizeof_char_ptr;
  case FIELD_TYPE_SET:
  case FIELD_TYPE_ENUM: abort(); return 0;	// This shouldn't happen
unknown's avatar
unknown committed
4860
  default: return 0;
unknown's avatar
unknown committed
4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879
  }
  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
4880
		  uchar *null_pos, uchar null_bit,
unknown's avatar
unknown committed
4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991
		  uint pack_flag,
		  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,
			      unireg_check, field_name, table,
			      f_is_binary(pack_flag) != 0);

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

    if (f_is_blob(pack_flag))
      return new Field_blob(ptr,null_pos,null_bit,
			    unireg_check, field_name, table,
			    pack_length,f_is_binary(pack_flag) != 0);
    if (interval)
    {
      if (f_is_enum(pack_flag))
	return new Field_enum(ptr,field_length,null_pos,null_bit,
				  unireg_check, field_name, table,
				  pack_length, interval);
      else
	return new Field_set(ptr,field_length,null_pos,null_bit,
			     unireg_check, field_name, table,
			     pack_length, interval);
    }
  }

  switch ((enum enum_field_types) f_packtype(pack_flag)) {
  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,
			       unireg_check, field_name, table);
  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,
			  unireg_check, field_name, table);
  case FIELD_TYPE_NEWDATE:
    return new Field_newdate(ptr,null_pos,null_bit,
			     unireg_check, field_name, table);
  case FIELD_TYPE_TIME:
    return new Field_time(ptr,null_pos,null_bit,
			  unireg_check, field_name, table);
  case FIELD_TYPE_DATETIME:
    return new Field_datetime(ptr,null_pos,null_bit,
			      unireg_check, field_name, table);
  case FIELD_TYPE_NULL:
    default:					// Impossible (Wrong version)
    return new Field_null(ptr,field_length,unireg_check,field_name,table);
  }
  return 0;					// Impossible (Wrong version)
}


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

4992
create_field::create_field(Field *old_field,Field *orig_field)
unknown's avatar
unknown committed
4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015
{
  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();

  /* 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)
  {
    sql_type=old_field->type();
    decimals=0;
  }
  if (flags & (ENUM_FLAG | SET_FLAG))
    interval= ((Field_enum*) old_field)->typelib;
  else
    interval=0;
5016
  def=0;
5017 5018 5019
  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
5020 5021
  {
    char buff[MAX_FIELD_WIDTH],*pos;
5022
    String tmp(buff,sizeof(buff));
5023 5024 5025 5026

    /* Get the value from record[2] (the default value row) */
    my_ptrdiff_t diff= (my_ptrdiff_t) (orig_field->table->rec_buff_length*2);
    orig_field->move_field(diff);		// Points now at record[2]
5027
    bool is_null=orig_field->is_real_null();
5028
    orig_field->val_str(&tmp,&tmp);
5029
    orig_field->move_field(-diff);		// Back to record[0]
5030
    if (!is_null)
5031 5032 5033 5034 5035
    {
      pos= (char*) sql_memdup(tmp.ptr(),tmp.length()+1);
      pos[tmp.length()]=0;
      def=new Item_string(pos,tmp.length());
    }
unknown's avatar
unknown committed
5036 5037
  }
}