field.cc 149 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2

bk@work.mysql.com's avatar
bk@work.mysql.com 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.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7

bk@work.mysql.com's avatar
bk@work.mysql.com 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.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
12

bk@work.mysql.com's avatar
bk@work.mysql.com committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


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

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

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

peter@mysql.com's avatar
peter@mysql.com committed
34 35 36
// Maximum allowed exponent value for converting string to decimal
#define MAX_EXPONENT 1024

bk@work.mysql.com's avatar
bk@work.mysql.com committed
37
/*****************************************************************************
38
  Instansiate templates and static variables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
39 40 41 42 43 44 45 46 47 48
*****************************************************************************/

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

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

49 50
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE 320

bk@work.mysql.com's avatar
bk@work.mysql.com committed
51
/*****************************************************************************
52
  Static help functions
bk@work.mysql.com's avatar
bk@work.mysql.com committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
*****************************************************************************/

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

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

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

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

  TODO
    Make this multi-byte-character safe

  RETURN
    0	ok
    1	error
bk@work.mysql.com's avatar
bk@work.mysql.com committed
87 88
*/

89 90
bool test_if_int(const char *str, int length, const char *int_end,
		 CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
91
{
92 93
  if (str == int_end)
    return 0;					// Empty string
bk@work.mysql.com's avatar
bk@work.mysql.com committed
94
  const char *end=str+length;
95 96
  if ((str= int_end) == end)
    return 1;					// All digits was used
bk@work.mysql.com's avatar
bk@work.mysql.com committed
97

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

serg@serg.mylan's avatar
serg@serg.mylan committed
112
#ifdef NOT_USED
113
static bool test_if_real(const char *str,int length, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
114
{
115
  cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
116 117

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

166

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
167 168 169 170 171 172 173 174
/*
 Tables of filed type compatibility.

 There are tables for every type, table consist of list of types in which
 given type can be converted without data lost, list should be ended with
 FIELD_CAST_STOP
*/
static Field::field_cast_enum field_cast_decimal[]=
175 176
{Field::FIELD_CAST_DECIMAL,
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
177 178
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_tiny[]=
179 180
{Field::FIELD_CAST_TINY,
 Field::FIELD_CAST_SHORT, Field::FIELD_CAST_MEDIUM, Field::FIELD_CAST_LONG,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
181 182 183 184 185
 Field::FIELD_CAST_LONGLONG,
 Field::FIELD_CAST_FLOAT, Field::FIELD_CAST_DOUBLE,
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_short[]=
186 187
{Field::FIELD_CAST_SHORT,
 Field::FIELD_CAST_MEDIUM, Field::FIELD_CAST_LONG, Field::FIELD_CAST_LONGLONG,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
188 189 190 191
 Field::FIELD_CAST_FLOAT, Field::FIELD_CAST_DOUBLE,
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_medium[]=
192 193
{Field::FIELD_CAST_MEDIUM,
 Field::FIELD_CAST_LONG, Field::FIELD_CAST_LONGLONG,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
194 195 196 197
 Field::FIELD_CAST_DOUBLE,
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_long[]=
198 199
{Field::FIELD_CAST_LONG,
 Field::FIELD_CAST_LONGLONG,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
200 201 202 203
 Field::FIELD_CAST_DOUBLE,
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_longlong[]=
204 205
{Field::FIELD_CAST_LONGLONG,
 Field::FIELD_CAST_DOUBLE,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
206 207 208
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_float[]=
209 210
{Field::FIELD_CAST_FLOAT,
 Field::FIELD_CAST_DOUBLE,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
211 212 213
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_double[]=
214 215
{Field::FIELD_CAST_DOUBLE,
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
216 217
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_null[]=
218 219
{Field::FIELD_CAST_NULL,
 Field::FIELD_CAST_DECIMAL, Field::FIELD_CAST_TINY, Field::FIELD_CAST_SHORT,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
220 221 222 223 224 225 226 227 228 229
 Field::FIELD_CAST_MEDIUM, Field::FIELD_CAST_LONG, Field::FIELD_CAST_LONGLONG,
 Field::FIELD_CAST_FLOAT, Field::FIELD_CAST_DOUBLE,
 Field::FIELD_CAST_TIMESTAMP, Field::FIELD_CAST_YEAR,
 Field::FIELD_CAST_DATE, Field::FIELD_CAST_NEWDATE,
 Field::FIELD_CAST_TIME, Field::FIELD_CAST_DATETIME,
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
 Field::FIELD_CAST_BLOB,
 Field::FIELD_CAST_GEOM, Field::FIELD_CAST_ENUM, Field::FIELD_CAST_SET,
 Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_timestamp[]=
230 231
{Field::FIELD_CAST_TIMESTAMP,
 Field::FIELD_CAST_DATETIME,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
232 233 234
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_year[]=
235 236
{Field::FIELD_CAST_YEAR,
 Field::FIELD_CAST_SHORT, Field::FIELD_CAST_MEDIUM, Field::FIELD_CAST_LONG,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
237 238 239 240 241
 Field::FIELD_CAST_LONGLONG,
 Field::FIELD_CAST_FLOAT, Field::FIELD_CAST_DOUBLE,
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_date[]=
242 243
{Field::FIELD_CAST_DATE,
 Field::FIELD_CAST_DATETIME,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
244 245 246
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_newdate[]=
247 248
{Field::FIELD_CAST_NEWDATE,
 Field::FIELD_CAST_DATETIME,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
249 250 251
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_time[]=
252 253
{Field::FIELD_CAST_TIME,
 Field::FIELD_CAST_DATETIME,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
254 255 256
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_datetime[]=
257 258
{Field::FIELD_CAST_DATETIME,
 Field::FIELD_CAST_STRING, Field::FIELD_CAST_VARSTRING,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
259 260
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_string[]=
261 262
{Field::FIELD_CAST_STRING,
 Field::FIELD_CAST_VARSTRING, Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
263
static Field::field_cast_enum field_cast_varstring[]=
264 265
{Field::FIELD_CAST_VARSTRING,
 Field::FIELD_CAST_BLOB, Field::FIELD_CAST_STOP};
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
266
static Field::field_cast_enum field_cast_blob[]=
267 268 269 270 271
{Field::FIELD_CAST_BLOB,
 Field::FIELD_CAST_STOP};
/*
  Geometrical, enum and set fields can be casted only to expressions
*/
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
272 273 274
static Field::field_cast_enum field_cast_geom[]=
{Field::FIELD_CAST_STOP};
static Field::field_cast_enum field_cast_enum[]=
275
{Field::FIELD_CAST_STOP};
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
276
static Field::field_cast_enum field_cast_set[]=
277
{Field::FIELD_CAST_STOP};
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291
// Array of pointers on conversion table for all fields types casting
static Field::field_cast_enum *field_cast_array[]=
{0, //FIELD_CAST_STOP
 field_cast_decimal, field_cast_tiny, field_cast_short,
 field_cast_medium, field_cast_long, field_cast_longlong,
 field_cast_float, field_cast_double,
 field_cast_null,
 field_cast_timestamp, field_cast_year, field_cast_date, field_cast_newdate,
 field_cast_time, field_cast_datetime,
 field_cast_string, field_cast_varstring, field_cast_blob,
 field_cast_geom, field_cast_enum, field_cast_set
};


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
292 293 294 295 296 297 298 299 300 301 302
/*
  Check if field of given type can store a value of this field.

  SYNOPSIS
    type   type for test

  RETURN
    1 can
    0 can not
*/

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
bool Field::field_cast_compatible(Field::field_cast_enum type)
{
  DBUG_ASSERT(type != FIELD_CAST_STOP);
  Field::field_cast_enum *array= field_cast_array[field_cast_type()];
  uint i= 0;
  Field::field_cast_enum tp;
  do
  {
    tp=  array[i++];
    if (tp == type)
      return 1;
  } while (tp != FIELD_CAST_STOP);
  return 0;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
319 320
/****************************************************************************
** Functions for the base classes
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
321
** This is an unpacked number.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
322 323 324
****************************************************************************/

Field::Field(char *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
325
	     uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
326 327
	     utype unireg_check_arg, const char *field_name_arg,
	     struct st_table *table_arg)
328
  :ptr(ptr_arg),null_ptr(null_ptr_arg),
329 330
   table(table_arg),orig_table(table_arg),
   table_name(table_arg ? table_arg->table_name : 0),
331
   field_name(field_name_arg),
serg@serg.mylan's avatar
serg@serg.mylan committed
332 333
   query_id(0), key_start(0), part_of_key(0), part_of_sortkey(0),
   unireg_check(unireg_check_arg),
334
   field_length(length_arg),null_bit(null_bit_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
335 336
{
  flags=null_ptr ? 0: NOT_NULL_FLAG;
337 338
  comment.str= (char*) "";
  comment.length=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
339 340 341 342 343 344 345 346 347 348 349 350 351
}

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)
  {
352 353
    *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) |
			null_ptr[row_offset] & (uchar) null_bit);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
354 355 356 357
  }
}


358
bool Field::send_binary(Protocol *protocol)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
359 360
{
  char buff[MAX_FIELD_WIDTH];
361
  String tmp(buff,sizeof(buff),charset());
362
  val_str(&tmp);
363
  return protocol->store(tmp.ptr(), tmp.length(), tmp.charset());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
364 365 366 367 368
}


void Field_num::add_zerofill_and_unsigned(String &res) const
{
369 370 371 372
  if (unsigned_flag)
    res.append(" unsigned");
  if (zerofill)
    res.append(" zerofill");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
373 374 375 376
}

void Field_num::make_field(Send_field *field)
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
377
  /* table_cache_key is not set for temp tables */
378 379 380 381
  field->db_name= (orig_table->table_cache_key ? orig_table->table_cache_key :
		   "");
  field->org_table_name= orig_table->real_name;
  field->table_name= orig_table->table_name;
382
  field->col_name=field->org_col_name=field_name;
383
  field->charsetnr= charset()->number;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
384 385 386 387 388 389 390 391 392
  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)
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
393
  /* table_cache_key is not set for temp tables */
394 395 396 397
  field->db_name= (orig_table->table_cache_key ? orig_table->table_cache_key :
		   "");
  field->org_table_name= orig_table->real_name;
  field->table_name= orig_table->table_name;
398
  field->col_name=field->org_col_name=field_name;
399
  field->charsetnr= charset()->number;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
400 401 402 403 404 405
  field->length=field_length;
  field->type=type();
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
  field->decimals=0;
}

406

bk@work.mysql.com's avatar
bk@work.mysql.com committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
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;
}

427 428

bool Field::get_date(TIME *ltime,uint fuzzydate)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
429 430
{
  char buff[40];
431 432
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
  if (!(res=val_str(&tmp)) ||
433 434
      str_to_datetime_with_warn(res->ptr(), res->length(),
                                ltime, fuzzydate) <= MYSQL_TIMESTAMP_ERROR)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
435 436 437 438 439 440 441
    return 1;
  return 0;
}

bool Field::get_time(TIME *ltime)
{
  char buff[40];
442 443
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
  if (!(res=val_str(&tmp)) ||
444
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
445 446 447 448
    return 1;
  return 0;
}

449 450 451 452 453 454
/*
  This is called when storing a date in a string

  NOTES
    Needs to be changed if/when we want to support different time formats
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
455 456 457

void Field::store_time(TIME *ltime,timestamp_type type)
{
458 459 460 461 462
  char buff[MAX_DATE_REP_LENGTH];
  String tmp;
  tmp.set(buff, sizeof(buff), &my_charset_bin);
  TIME_to_string(ltime, &tmp);
  store(buff, tmp.length(), &my_charset_bin);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
463 464 465
}


466
bool Field::optimize_range(uint idx, uint part)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
467
{
468
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
469 470
}

471
/****************************************************************************
472
  Field_null, a field that always return NULL
473 474 475 476
****************************************************************************/

void Field_null::sql_type(String &res) const
{
477
  res.set_ascii("null", 4);
478 479 480
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
481
/****************************************************************************
482 483
  Functions for the Field_decimal class
  This is an number stored as a pre-space (or pre-zero) string
bk@work.mysql.com's avatar
bk@work.mysql.com committed
484 485 486 487 488
****************************************************************************/

void
Field_decimal::reset(void)
{
489
  Field_decimal::store("0",1,&my_charset_bin);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
490 491 492 493 494
}

void Field_decimal::overflow(bool negative)
{
  uint len=field_length;
495
  char *to=ptr, filler= '9';
496

497
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
498
  if (negative)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
499
  {
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
    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
      }
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
523
  }
524
  bfill(to, len, filler);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
525 526 527 528 529 530
  if (dec)
    ptr[field_length-dec-1]='.';
  return;
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
531
int Field_decimal::store(const char *from, uint len, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
532
{
533
  char buff[80];
534
  String tmp(buff,sizeof(buff), &my_charset_bin);
535

536 537 538
  /* Convert character set if the old one is multi byte */
  if (cs->mbmaxlen > 1)
  { 
539
    tmp.copy(from, len, cs, &my_charset_bin);
540 541 542
    from= tmp.ptr();
    len=  tmp.length();
  }
543

544 545 546 547 548 549 550 551 552
  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;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
553
  /* The pointers where prezeros start and stop */
554
  const char *pre_zeros_from, *pre_zeros_end;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
555
  /* The pointers where digits at the left of '.' start and stop */
556
  const char *int_digits_from, *int_digits_end;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
557
  /* The pointers where digits at the right of '.' start and stop */
558
  const char *frac_digits_from, *frac_digits_end;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
559
  /* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
560
  char expo_sign_char=0;
peter@mysql.com's avatar
peter@mysql.com committed
561
  uint exponent=0;                                // value of the exponent
562 563 564 565 566 567 568
  /*
    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;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
569 570 571 572
  /*
    Pointer used when digits move from the right of the '.' to the left
    of the '.'
  */
573
  const char *frac_digits_head_end;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
574
  /* Number of 0 that need to be added at the right of the '.' (for 1E-3) */
575 576 577 578 579
  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;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
580
  /*
581
    To remember if table->in_use->cuted_fields has already been incremented,
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
582 583
    to do that only once
  */
584 585 586 587 588 589 590 591 592
  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 :
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
593 594
    - parse the input string
    - modify the position of digits around the decimal dot '.' 
595
      according to the exponent value (if specified)
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
596
    - write the formatted number
597 598 599 600 601
  */

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

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
602
  /* skip pre-space */
603
  while (from != end && my_isspace(&my_charset_bin,*from))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
604
    from++;
605
  if (from == end)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
606
  {
607
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
608
    is_cuted_fields_incr=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
609
  }
610
  else if (*from == '+' || *from == '-')	// Found some sign ?
bk@work.mysql.com's avatar
bk@work.mysql.com committed
611
  {
612
    sign_char= *from++;
613
    /*
614 615 616
      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
617 618 619 620
    */ 
    if (unsigned_flag)  
    { 
      if (sign_char=='-')
621
      {
622
        Field_decimal::overflow(1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
623
        return 1;
624
      }
625
      /* 
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
626 627
	 Defining this will not store "+" for unsigned decimal type even if
	 it is passed in numeric string. This will make some tests to fail
628 629
      */	 
#ifdef DONT_ALLOW_UNSIGNED_PLUS      
630 631
      else 
        sign_char=0;
632
#endif 	
bk@work.mysql.com's avatar
bk@work.mysql.com committed
633
    }
634
  }
635

636 637 638 639
  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 '.'*/
640
  for (; from != end && my_isdigit(&my_charset_bin, *from) ; from++) ;
641 642 643 644 645
  int_digits_end=from;
  if (from!=end && *from == '.')		// Some '.' ?
    from++;
  frac_digits_from= from;
  /* Read digits at the right of '.' */
646
  for (;from!=end && my_isdigit(&my_charset_bin, *from); from++) ;
647 648 649 650 651 652 653 654 655 656
  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= '+';
    /*
657 658 659 660
      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).
661
    */
662
    for (;from!=end && my_isdigit(&my_charset_bin, *from); from++)
663
    {
664
      exponent=10*exponent+(*from-'0');
peter@mysql.com's avatar
peter@mysql.com committed
665
      if (exponent>MAX_EXPONENT)
666 667
        break;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
668
  }
669
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
670
  /*
671 672 673 674
    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.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
675
  */
676

677
  if (table->in_use->count_cuted_fields)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
678
  {
679
    // Skip end spaces
680
    for (;from != end && my_isspace(&my_charset_bin, *from); from++) ;
681
    if (from != end)                     // If still something left, warn
bk@work.mysql.com's avatar
bk@work.mysql.com committed
682
    {
683
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
684
      is_cuted_fields_incr=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
685
    }
686 687 688 689 690 691
  }
  
  /*
    Now "move" digits around the decimal dot according to the exponent value,
    and add necessary zeros.
    Examples :
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
692 693 694
    - 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'
695
    - 1234.5E-3 : '234' moves at the right of '.'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
696 697
    These moves are implemented with pointers which point at the begin
    and end of each moved segment. Examples :
698
    - 1234.5E-3 : before the code below is executed, the int_digits part is
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
699 700 701
    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'.
702
    - 1234.5E3 : before the code below is executed, the int_digits part is
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
703 704 705 706
    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).
707 708
  */
  
709
  /* 
peter@mysql.com's avatar
peter@mysql.com committed
710
     Below tmp_uint cannot overflow with small enough MAX_EXPONENT setting,
711
     as int_digits_added_zeros<=exponent<4G and 
712 713
     (int_digits_end-int_digits_from)<=max_allowed_packet<=2G and
     (frac_digits_from-int_digits_tail_from)<=max_allowed_packet<=2G
714 715
  */

716 717 718 719 720 721 722 723
  if (!expo_sign_char)
    tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);
  else if (expo_sign_char == '-') 
  {
    tmp_uint=min(exponent,(uint)(int_digits_end-int_digits_from));
    frac_digits_added_zeros=exponent-tmp_uint;
    int_digits_end -= tmp_uint;
    frac_digits_head_end=int_digits_end+tmp_uint;
peter@mysql.com's avatar
peter@mysql.com committed
724
    tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);     
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
  }
  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;
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
750
    }
751 752 753
    tmp_uint= (tmp_dec+(int_digits_end-int_digits_from)+
               (uint)(frac_digits_from-int_digits_tail_from)+
               int_digits_added_zeros);
754 755 756 757 758 759 760 761 762 763
  }
  
  /*
    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
  */

764
  if (field_length < tmp_uint + (int) (sign_char == '-')) 
765
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
766
    // too big number, change to max or min number
767
    Field_decimal::overflow(sign_char == '-');
768
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
769
  }
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
 
  /*
    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';  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
787
  }
788 789 790 791 792 793
  /*
    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;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
794 795

  /*
796 797
    Step back to the position of the leftmost digit of the int_% parts,
    to write sign and fill with zeros or blanks or prezeros.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
798
  */
799 800 801 802
  pos=tmp_left_pos-1;
  if (zerofill)
  {
    left_wall=to-1;
803
    while (pos > left_wall)			// Fill with zeros
804 805 806 807
      *pos--='0';
  }
  else
  {
808
    left_wall=to+(sign_char != 0)-1;
809 810
    if (!expo_sign_char)	// If exponent was specified, ignore prezeros
    {
811
      for (;pos > left_wall && pre_zeros_from !=pre_zeros_end;
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
	   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
  }
  
827 828
  /*
    Write digits of the frac_% parts ;
829
    Depending on table->in_use->count_cutted_fields, we may also want
830 831 832 833 834
    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)
  */
835
      
836 837 838
  pos=to+(uint)(field_length-tmp_dec);	// Calculate post to '.'
  right_wall=to+field_length;
  if (pos != right_wall) 
839 840
    *pos++='.';

841 842 843
  if (expo_sign_char == '-')
  {
    while (frac_digits_added_zeros-- > 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
844
    {
845
      if (pos == right_wall) 
846
      {
847
        if (table->in_use->count_cuted_fields && !is_cuted_fields_incr) 
848
          break; // Go on below to see if we lose non zero digits
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
849
        return 0;
850
      }
851 852 853 854 855 856
      *pos++='0';
    }
    while (int_digits_end != frac_digits_head_end)
    {
      tmp_char= *int_digits_end++;
      if (pos == right_wall)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
857
      {
858 859 860
        if (tmp_char != '0')			// Losing a non zero digit ?
        {
          if (!is_cuted_fields_incr)
861 862
            set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                        ER_WARN_DATA_TRUNCATED, 1);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
863
          return 0;
864 865
        }
        continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
866
      }
867
      *pos++= tmp_char;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
868
    }
869
  }
870

871 872 873 874
  for (;frac_digits_from!=frac_digits_end;) 
  {
    tmp_char= *frac_digits_from++;
    if (pos == right_wall)
875
    {
876
      if (tmp_char != '0')			// Losing a non zero digit ?
877
      {
878
        if (!is_cuted_fields_incr)
879
	  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
880
        return 0;
881
      }
882
      continue;
883
    }
884
    *pos++= tmp_char;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
885
  }
886 887 888
      
  while (pos != right_wall)
   *pos++='0';			// Fill with zeros at right of '.'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
889
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
890 891 892
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
893
int Field_decimal::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
894 895 896 897
{
  if (unsigned_flag && nr < 0)
  {
    overflow(1);
898
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
899
  }
900
  
901 902
#ifdef HAVE_FINITE
  if (!finite(nr)) // Handle infinity as special case
903 904
  {
    overflow(nr < 0.0);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
905
    return 1;
906
  }
907
#endif
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
908

bk@work.mysql.com's avatar
bk@work.mysql.com committed
909 910
  reg4 uint i,length;
  char fyllchar,*to;
911
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
bk@work.mysql.com's avatar
bk@work.mysql.com committed
912 913

  fyllchar = zerofill ? (char) '0' : (char) ' ';
serg@serg.mylan's avatar
serg@serg.mylan committed
914
#ifdef HAVE_SNPRINTF
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
915 916
  buff[sizeof(buff)-1]=0;			// Safety
  snprintf(buff,sizeof(buff)-1, "%.*f",(int) dec,nr);
917
  length=(uint) strlen(buff);
918
#else
919
  length=(uint) my_sprintf(buff,(buff,"%.*f",dec,nr));
920
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
921 922 923 924

  if (length > field_length)
  {
    overflow(nr < 0.0);
925
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
926 927 928 929 930 931 932
  }
  else
  {
    to=ptr;
    for (i=field_length-length ; i-- > 0 ;)
      *to++ = fyllchar;
    memcpy(to,buff,length);
933
    return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
934 935 936 937
  }
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
938
int Field_decimal::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
939 940 941 942
{
  if (unsigned_flag && nr < 0)
  {
    overflow(1);
943
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
944 945 946 947 948 949 950 951
  }
  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 */
952
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
953 954 955 956 957 958 959 960 961 962 963 964 965
  }
  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');
    }
966
    return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
967 968 969 970 971 972
  }
}


double Field_decimal::val_real(void)
{
973
  int not_used;
974
  return my_strntod(&my_charset_bin, ptr, field_length, NULL, &not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
975 976 977 978
}

longlong Field_decimal::val_int(void)
{
979
  int not_used;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
980
  if (unsigned_flag)
981
    return my_strntoull(&my_charset_bin, ptr, field_length, 10, NULL,
982
			&not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
983
  else
984
    return my_strntoll(&my_charset_bin, ptr, field_length, 10, NULL,
985
			&not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
986 987
}

988

bk@work.mysql.com's avatar
bk@work.mysql.com committed
989 990 991 992 993 994
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);
995
  val_ptr->set_charset(&my_charset_bin);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
996 997 998
  if (field_length < tmp_length)		// Error in data
    val_ptr->length(0);
  else
999
    val_ptr->set_ascii((const char*) str, field_length-tmp_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
  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;
1011
  int swap=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1012 1013 1014 1015
  /* First remove prefixes '0', ' ', and '-' */
  for (end=a_ptr+field_length;
       a_ptr != end &&
	 (*a_ptr == *b_ptr ||
1016
	  ((my_isspace(&my_charset_bin,*a_ptr)  || *a_ptr == '+' || 
1017
            *a_ptr == '0') &&
1018
	   (my_isspace(&my_charset_bin,*b_ptr) || *b_ptr == '+' || 
1019
            *b_ptr == '0')));
1020 1021 1022 1023 1024
       a_ptr++,b_ptr++)
  {
    if (*a_ptr == '-')				// If both numbers are negative
      swap= -1 ^ 1;				// Swap result      
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1025 1026 1027
  if (a_ptr == end)
    return 0;
  if (*a_ptr == '-')
1028
    return -1;
1029
  if (*b_ptr == '-')
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
    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 &&
1046
	 ((my_isspace(&my_charset_bin,*str) || *str == '+' ||
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1047
	   *str == '0')) ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
       str++)
    *to++=' ';
  if (str == end)
    return;					/* purecov: inspected */

  if (*str == '-')
  {
    *to++=1;					// Smaller than any number
    str++;
    while (str != end)
1058
      if (my_isdigit(&my_charset_bin,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1059 1060 1061 1062 1063 1064 1065
	*to++= (char) ('9' - *str++);
      else
	*to++= *str++;
  }
  else memcpy(to,str,(uint) (end-str));
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1066

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1067 1068
void Field_decimal::sql_type(String &res) const
{
1069
  CHARSET_INFO *cs=res.charset();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1070 1071 1072 1073 1074
  uint tmp=field_length;
  if (!unsigned_flag)
    tmp--;
  if (dec)
    tmp--;
1075
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
1076
			  "decimal(%d,%d)",tmp,dec));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1077 1078 1079 1080 1081 1082 1083 1084
  add_zerofill_and_unsigned(res);
}


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

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1085
int Field_tiny::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1086
{
1087
  int not_used;				// We can ignore result from str2int
1088
  char *end;
1089
  long tmp= my_strntol(cs, from, len, 10, &end, &not_used);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1090
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1091 1092 1093 1094 1095 1096

  if (unsigned_flag)
  {
    if (tmp < 0)
    {
      tmp=0; /* purecov: inspected */
1097
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1098
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1099 1100 1101 1102
    }
    else if (tmp > 255)
    {
      tmp= 255;
1103
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1104
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1105
    }
1106
    else if (table->in_use->count_cuted_fields && !test_if_int(from,len,end,cs))
1107
    {
1108
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1109
      error= 1;
1110
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1111 1112 1113 1114 1115 1116
  }
  else
  {
    if (tmp < -128)
    {
      tmp= -128;
1117
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1118
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1119 1120 1121 1122
    }
    else if (tmp >= 128)
    {
      tmp= 127;
1123
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1124
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1125
    }
1126
    else if (table->in_use->count_cuted_fields && !test_if_int(from,len,end,cs))
1127
    {
1128
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1129
      error= 1;
1130
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1131 1132
  }
  ptr[0]= (char) tmp;
1133
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1134 1135 1136
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1137
int Field_tiny::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1138
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1139
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1140 1141 1142 1143 1144 1145
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0.0)
    {
      *ptr=0;
1146
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1147
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1148 1149 1150 1151
    }
    else if (nr > 255.0)
    {
      *ptr=(char) 255;
1152
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1153
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1154 1155 1156 1157 1158 1159 1160 1161 1162
    }
    else
      *ptr=(char) nr;
  }
  else
  {
    if (nr < -128.0)
    {
      *ptr= (char) -128;
1163
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1164
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1165 1166 1167 1168
    }
    else if (nr > 127.0)
    {
      *ptr=127;
1169
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1170
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1171 1172 1173 1174
    }
    else
      *ptr=(char) nr;
  }
1175
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1176 1177
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1178
int Field_tiny::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1179
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1180
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1181 1182 1183 1184 1185
  if (unsigned_flag)
  {
    if (nr < 0L)
    {
      *ptr=0;
1186
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1187
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1188 1189 1190 1191
    }
    else if (nr > 255L)
    {
      *ptr= (char) 255;
1192
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1193
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1194 1195 1196 1197 1198 1199 1200 1201 1202
    }
    else
      *ptr=(char) nr;
  }
  else
  {
    if (nr < -128L)
    {
      *ptr= (char) -128;
1203
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1204
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1205 1206 1207 1208
    }
    else if (nr > 127L)
    {
      *ptr=127;
1209
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1210
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1211 1212 1213 1214
    }
    else
      *ptr=(char) nr;
  }
1215
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
}


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)))
{
1236
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1237
  uint length;
1238 1239
  uint mlength=max(field_length+1,5*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1240
  char *to=(char*) val_buffer->ptr();
1241

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1242
  if (unsigned_flag)
1243 1244
    length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
					   (long) *((uchar*) ptr));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1245
  else
1246 1247
    length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
					   (long) *((signed char*) ptr));
1248
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1249 1250 1251 1252 1253 1254
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}

1255 1256 1257 1258
bool Field_tiny::send_binary(Protocol *protocol)
{
  return protocol->store_tiny((longlong) (int8) ptr[0]);
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278

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
{
1279
  CHARSET_INFO *cs=res.charset();
1280
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
1281
			  "tinyint(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1282 1283 1284 1285
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
1286
 Field type short int (2 byte)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1287 1288
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1289
int Field_short::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1290
{
1291
  int not_used;				// We can ignore result from str2int
1292
  char *end;
1293
  long tmp= my_strntol(cs, from, len, 10, &end, &not_used);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1294
  int error= 0;
1295

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1296 1297 1298 1299 1300
  if (unsigned_flag)
  {
    if (tmp < 0)
    {
      tmp=0;
1301
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1302
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1303 1304 1305 1306
    }
    else if (tmp > (uint16) ~0)
    {
      tmp=(uint16) ~0;
1307
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1308
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1309
    }
1310
    else if (table->in_use->count_cuted_fields && !test_if_int(from,len,end,cs))
1311
    {
1312
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1313
      error= 1;
1314
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1315 1316 1317 1318 1319 1320
  }
  else
  {
    if (tmp < INT_MIN16)
    {
      tmp= INT_MIN16;
1321
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1322
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1323 1324 1325 1326
    }
    else if (tmp > INT_MAX16)
    {
      tmp=INT_MAX16;
1327
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1328
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1329
    }
1330
    else if (table->in_use->count_cuted_fields && !test_if_int(from,len,end,cs))
1331
    {
1332
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1333
      error= 1;
1334
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1335 1336 1337 1338 1339 1340 1341 1342 1343
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int2store(ptr,tmp);
  }
  else
#endif
    shortstore(ptr,(short) tmp);
1344
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1345 1346 1347
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1348
int Field_short::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1349
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1350
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1351 1352 1353 1354 1355 1356 1357
  int16 res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
1358
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1359
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1360 1361 1362 1363
    }
    else if (nr > (double) (uint16) ~0)
    {
      res=(int16) (uint16) ~0;
1364
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1365
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1366 1367 1368 1369 1370 1371 1372 1373 1374
    }
    else
      res=(int16) (uint16) nr;
  }
  else
  {
    if (nr < (double) INT_MIN16)
    {
      res=INT_MIN16;
1375
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1376
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1377 1378 1379 1380
    }
    else if (nr > (double) INT_MAX16)
    {
      res=INT_MAX16;
1381
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1382
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
    }
    else
      res=(int16) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int2store(ptr,res);
  }
  else
#endif
    shortstore(ptr,res);
1395
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1396 1397
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1398
int Field_short::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1399
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1400
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1401 1402 1403 1404 1405 1406
  int16 res;
  if (unsigned_flag)
  {
    if (nr < 0L)
    {
      res=0;
1407
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1408
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1409 1410 1411 1412
    }
    else if (nr > (longlong) (uint16) ~0)
    {
      res=(int16) (uint16) ~0;
1413
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1414
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1415 1416 1417 1418 1419 1420 1421 1422 1423
    }
    else
      res=(int16) (uint16) nr;
  }
  else
  {
    if (nr < INT_MIN16)
    {
      res=INT_MIN16;
1424
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1425
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1426 1427 1428 1429
    }
    else if (nr > INT_MAX16)
    {
      res=INT_MAX16;
1430
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1431
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
    }
    else
      res=(int16) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int2store(ptr,res);
  }
  else
#endif
    shortstore(ptr,res);
1444
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
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
}


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

1472

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1473 1474 1475
String *Field_short::val_str(String *val_buffer,
			     String *val_ptr __attribute__((unused)))
{
1476
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1477
  uint length;
1478 1479
  uint mlength=max(field_length+1,7*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
  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)
1490 1491
    length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10, 
					  (long) (uint16) j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1492
  else
1493
    length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1494 1495 1496 1497 1498 1499 1500
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


1501 1502 1503 1504 1505 1506
bool Field_short::send_binary(Protocol *protocol)
{
  return protocol->store_short(Field_short::val_int());
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
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
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
1537
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1538 1539 1540 1541 1542 1543 1544 1545
    to[1]   = ptr[1];
  }
  else
#endif
  {
    if (unsigned_flag)
      to[0] = ptr[1];
    else
1546
      to[0] = (char) (ptr[1] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1547 1548 1549 1550 1551 1552
    to[1]   = ptr[0];
  }
}

void Field_short::sql_type(String &res) const
{
1553
  CHARSET_INFO *cs=res.charset();
1554
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
1555
			  "smallint(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1556 1557 1558 1559 1560
  add_zerofill_and_unsigned(res);
}


/****************************************************************************
1561
  Field type medium int (3 byte)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1562 1563
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1564
int Field_medium::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1565
{
1566
  int not_used;				// We can ignore result from str2int
1567
  char *end;
1568
  long tmp= my_strntol(cs, from, len, 10, &end, &not_used);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1569
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1570 1571 1572 1573 1574 1575

  if (unsigned_flag)
  {
    if (tmp < 0)
    {
      tmp=0;
1576
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1577
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1578 1579 1580 1581
    }
    else if (tmp >= (long) (1L << 24))
    {
      tmp=(long) (1L << 24)-1L;
1582
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1583
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1584
    }
1585
    else if (table->in_use->count_cuted_fields && !test_if_int(from,len,end,cs))
1586
    {
1587
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1588
      error= 1;
1589
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1590 1591 1592 1593 1594 1595
  }
  else
  {
    if (tmp < INT_MIN24)
    {
      tmp= INT_MIN24;
1596
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1597
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1598 1599 1600 1601
    }
    else if (tmp > INT_MAX24)
    {
      tmp=INT_MAX24;
1602
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1603
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1604
    }
1605
    else if (table->in_use->count_cuted_fields && !test_if_int(from,len,end,cs))
1606
    {
1607
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1608
      error= 1;
1609
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1610 1611 1612
  }

  int3store(ptr,tmp);
1613
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1614 1615 1616
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1617
int Field_medium::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1618
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1619
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1620 1621 1622 1623 1624 1625
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      int3store(ptr,0);
1626
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1627
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1628 1629 1630
    }
    else if (nr >= (double) (long) (1L << 24))
    {
1631
      uint32 tmp=(uint32) (1L << 24)-1L;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1632
      int3store(ptr,tmp);
1633
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1634
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1635 1636
    }
    else
1637
      int3store(ptr,(uint32) nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1638 1639 1640 1641 1642 1643 1644
  }
  else
  {
    if (nr < (double) INT_MIN24)
    {
      long tmp=(long) INT_MIN24;
      int3store(ptr,tmp);
1645
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1646
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1647 1648 1649 1650 1651
    }
    else if (nr > (double) INT_MAX24)
    {
      long tmp=(long) INT_MAX24;
      int3store(ptr,tmp);
1652
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1653
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1654 1655 1656 1657
    }
    else
      int3store(ptr,(long) nr);
  }
1658
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1659 1660
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1661
int Field_medium::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1662
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1663
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1664 1665 1666 1667 1668
  if (unsigned_flag)
  {
    if (nr < 0L)
    {
      int3store(ptr,0);
1669
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1670
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1671 1672 1673 1674 1675
    }
    else if (nr >= (longlong) (long) (1L << 24))
    {
      long tmp=(long) (1L << 24)-1L;;
      int3store(ptr,tmp);
1676
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1677
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1678 1679
    }
    else
1680
      int3store(ptr,(uint32) nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1681 1682 1683 1684 1685 1686 1687
  }
  else
  {
    if (nr < (longlong) INT_MIN24)
    {
      long tmp=(long) INT_MIN24;
      int3store(ptr,tmp);
1688
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1689
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1690 1691 1692 1693 1694
    }
    else if (nr > (longlong) INT_MAX24)
    {
      long tmp=(long) INT_MAX24;
      int3store(ptr,tmp);
1695
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1696
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1697 1698 1699 1700
    }
    else
      int3store(ptr,(long) nr);
  }
1701
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1702 1703 1704 1705 1706 1707 1708 1709 1710
}


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

1711

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1712 1713 1714 1715 1716 1717
longlong Field_medium::val_int(void)
{
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
  return (longlong) j;
}

1718

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1719 1720 1721
String *Field_medium::val_str(String *val_buffer,
			      String *val_ptr __attribute__((unused)))
{
1722
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1723
  uint length;
1724 1725
  uint mlength=max(field_length+1,10*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1726 1727 1728
  char *to=(char*) val_buffer->ptr();
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);

1729
  length=(uint) cs->cset->long10_to_str(cs,to,mlength,-10,j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1730 1731 1732 1733 1734 1735 1736
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer); /* purecov: inspected */
  return val_buffer;
}


1737 1738 1739 1740 1741 1742
bool Field_medium::send_binary(Protocol *protocol)
{
  return protocol->store_long(Field_medium::val_int());
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
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
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
{
1772
  CHARSET_INFO *cs=res.charset();
1773
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), 
1774
			  "mediumint(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1775 1776 1777 1778 1779 1780 1781 1782
  add_zerofill_and_unsigned(res);
}

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


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1783
int Field_long::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1784
{
1785
  long tmp;
monty@mysql.com's avatar
monty@mysql.com committed
1786
  int error= 0;
1787
  char *end;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
1788
  
1789
  tmp= cs->cset->scan(cs, from, from+len, MY_SEQ_SPACES);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
1790 1791
  len-= tmp;
  from+= tmp;
1792
  my_errno=0;
serg@serg.mylan's avatar
serg@serg.mylan committed
1793

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1794 1795 1796 1797 1798
  if (unsigned_flag)
  {
    if (!len || *from == '-')
    {
      tmp=0;					// Set negative to 0
1799
      my_errno=ERANGE;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1800
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1801 1802
    }
    else
1803
      tmp=(long) my_strntoul(cs,from,len,10,&end,&error);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1804 1805
  }
  else
1806 1807
    tmp=my_strntol(cs,from,len,10,&end,&error);
  if (error ||
1808
      (from+len != end && table->in_use->count_cuted_fields &&
1809
       !test_if_int(from,len,end,cs)))
serg@serg.mylan's avatar
serg@serg.mylan committed
1810
    error= 1;
1811 1812 1813
#if SIZEOF_LONG > 4
  if (unsigned_flag)
  {
monty@mysql.com's avatar
monty@mysql.com committed
1814
    if ((ulong) tmp > UINT_MAX32)
1815 1816
    {
      tmp= UINT_MAX32;
serg@serg.mylan's avatar
serg@serg.mylan committed
1817 1818
      error= 1;
      my_errno=ERANGE;
1819 1820 1821 1822 1823 1824 1825
    }
  }
  else
  {
    if (tmp > INT_MAX32)
    {
      tmp= INT_MAX32;
serg@serg.mylan's avatar
serg@serg.mylan committed
1826 1827
      error= 1;
      my_errno=ERANGE;
1828 1829 1830 1831
    }
    else if (tmp < INT_MIN32)
    {
      tmp= INT_MIN32;
serg@serg.mylan's avatar
serg@serg.mylan committed
1832 1833
      error= 1;
      my_errno=ERANGE;
1834 1835 1836
    }
  }
#endif
serg@serg.mylan's avatar
serg@serg.mylan committed
1837
  if (error)
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1838
  {
1839
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
1840
    error= 1;
1841
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1842 1843 1844 1845 1846 1847 1848 1849
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
1850
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1851 1852 1853
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1854
int Field_long::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1855
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1856
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1857 1858 1859 1860 1861 1862 1863
  int32 res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
1864
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1865
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1866 1867 1868 1869
    }
    else if (nr > (double) (ulong) ~0L)
    {
      res=(int32) (uint32) ~0L;
1870
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1871
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1872 1873 1874 1875 1876 1877 1878 1879 1880
    }
    else
      res=(int32) (ulong) nr;
  }
  else
  {
    if (nr < (double) INT_MIN32)
    {
      res=(int32) INT_MIN32;
1881
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1882
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1883 1884 1885 1886
    }
    else if (nr > (double) INT_MAX32)
    {
      res=(int32) INT_MAX32;
1887
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1888
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
    }
    else
      res=(int32) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,res);
  }
  else
#endif
    longstore(ptr,res);
1901
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1902 1903 1904
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1905
int Field_long::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1906
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1907
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1908
  int32 res;
1909 1910 1911 1912 1913 1914 1915 1916

  /* 
    This assert has nothing to do with this method per se, it was put here
    only because it is one of the best places for catching places there its 
    condition is broken.
  */
  DBUG_ASSERT(table->in_use == current_thd);
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1917 1918 1919 1920 1921
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
1922
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1923
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1924 1925 1926 1927
    }
    else if (nr >= (LL(1) << 32))
    {
      res=(int32) (uint32) ~0L;
1928
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1929
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1930 1931 1932 1933 1934 1935 1936 1937 1938
    }
    else
      res=(int32) (uint32) nr;
  }
  else
  {
    if (nr < (longlong) INT_MIN32)
    {
      res=(int32) INT_MIN32;
1939
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1940
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1941 1942 1943 1944
    }
    else if (nr > (longlong) INT_MAX32)
    {
      res=(int32) INT_MAX32;
1945
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1946
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
    }
    else
      res=(int32) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,res);
  }
  else
#endif
    longstore(ptr,res);
1959
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
}


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;
1978 1979
  /* See the comment in Field_long::store(long long) */
  DBUG_ASSERT(table->in_use == current_thd);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
#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)))
{
1992
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1993
  uint length;
1994 1995
  uint mlength=max(field_length+1,12*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1996 1997 1998 1999 2000 2001 2002 2003 2004
  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);

2005
  if (unsigned_flag)
2006
    length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32)j);
2007
  else
2008
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2009 2010 2011 2012 2013 2014 2015
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


2016 2017 2018 2019 2020
bool Field_long::send_binary(Protocol *protocol)
{
  return protocol->store_long(Field_long::val_int());
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
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)
2037
    return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
  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
2049
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
    to[1]   = ptr[1];
    to[2]   = ptr[2];
    to[3]   = ptr[3];
  }
  else
#endif
  {
    if (unsigned_flag)
      to[0] = ptr[3];
    else
2060
      to[0] = (char) (ptr[3] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2061 2062 2063 2064 2065 2066 2067 2068 2069
    to[1]   = ptr[2];
    to[2]   = ptr[1];
    to[3]   = ptr[0];
  }
}


void Field_long::sql_type(String &res) const
{
2070
  CHARSET_INFO *cs=res.charset();
2071
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2072
			  "int(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2073 2074 2075 2076
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
2077
 Field type longlong int (8 bytes)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2078 2079
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2080
int Field_longlong::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2081
{
2082 2083
  longlong tmp;
  int error= 0;
2084
  char *end;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2085
  
2086
  tmp= cs->cset->scan(cs, from, from+len, MY_SEQ_SPACES);
2087
  len-= (uint)tmp;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2088
  from+= tmp;
2089
  my_errno=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2090 2091 2092 2093 2094
  if (unsigned_flag)
  {
    if (!len || *from == '-')
    {
      tmp=0;					// Set negative to 0
2095
      my_errno= ERANGE;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2096
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2097 2098
    }
    else
2099
      tmp=(longlong) my_strntoull(cs,from,len,10,&end,&error);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2100 2101
  }
  else
2102 2103
    tmp=my_strntoll(cs,from,len,10,&end,&error);
  if (error ||
2104
      (from+len != end && table->in_use->count_cuted_fields &&
2105
       !test_if_int(from,len,end,cs)))
2106
  {
2107
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
2108 2109
    error= 1;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2110 2111 2112 2113 2114 2115 2116 2117
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
2118
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2119 2120 2121
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2122
int Field_longlong::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2123
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2124
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2125 2126 2127 2128 2129 2130 2131
  longlong res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
2132
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2133
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2134 2135 2136 2137
    }
    else if (nr >= (double) ~ (ulonglong) 0)
    {
      res= ~(longlong) 0;
2138
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2139
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2140 2141 2142 2143 2144 2145 2146 2147 2148
    }
    else
      res=(longlong) (ulonglong) nr;
  }
  else
  {
    if (nr <= (double) LONGLONG_MIN)
    {
      res=(longlong) LONGLONG_MIN;
2149
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2150
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2151 2152 2153 2154
    }
    else if (nr >= (double) LONGLONG_MAX)
    {
      res=(longlong) LONGLONG_MAX;
2155
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2156
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
    }
    else
      res=(longlong) nr;
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,res);
  }
  else
#endif
    longlongstore(ptr,res);
2169
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2170 2171 2172
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2173
int Field_longlong::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2174 2175 2176 2177 2178 2179 2180 2181 2182
{
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,nr);
  }
  else
#endif
    longlongstore(ptr,nr);
2183
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197
}


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);
2198 2199 2200 2201 2202 2203 2204
  /* The following is open coded to avoid a bug in gcc 3.3 */
  if (unsigned_flag)
  {
    ulonglong tmp= (ulonglong) j;
    return ulonglong2double(tmp);
  }
  return (double) j;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2205 2206
}

2207

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223
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)))
{
2224
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2225
  uint length;
2226 2227
  uint mlength=max(field_length+1,22*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2228 2229 2230 2231 2232 2233 2234 2235 2236
  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);

2237
  length=(uint) (cs->cset->longlong10_to_str)(cs,to,mlength,
2238
					unsigned_flag ? 10 : -10, j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2239 2240 2241 2242 2243 2244 2245
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


2246 2247 2248 2249 2250 2251
bool Field_longlong::send_binary(Protocol *protocol)
{
  return protocol->store_longlong(Field_longlong::val_int(), unsigned_flag);
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
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
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
2281
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
    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
2296
      to[0] = (char) (ptr[7] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309
    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
{
2310
  CHARSET_INFO *cs=res.charset();
2311
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2312
			  "bigint(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2313 2314 2315
  add_zerofill_and_unsigned(res);
}

monty@mysql.com's avatar
monty@mysql.com committed
2316

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2317
/****************************************************************************
monty@mysql.com's avatar
monty@mysql.com committed
2318
  single precision float
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2319 2320
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2321
int Field_float::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2322
{
monty@mysql.com's avatar
monty@mysql.com committed
2323
  int error;
2324
  char *end;
monty@mysql.com's avatar
monty@mysql.com committed
2325
  double nr= my_strntod(cs,(char*) from,len,&end,&error);
2326
  if (error || ((uint) (end-from) != len && table->in_use->count_cuted_fields))
2327
  {
monty@mysql.com's avatar
monty@mysql.com committed
2328
    error= 1;
2329
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
2330
  }
monty@mysql.com's avatar
monty@mysql.com committed
2331 2332
  Field_float::store(nr);
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2333 2334 2335
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2336
int Field_float::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2337 2338
{
  float j;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2339
  int error= 0;
2340

2341
  if (isnan(nr))
2342
  {
2343 2344
    j= 0;
    set_null();
2345
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2346
    error= 1;
2347
  }
2348
  else if (unsigned_flag && nr < 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2349
  {
2350
    j= 0;
2351
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2352
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2353
  }
2354
  else
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2355
  {
2356 2357 2358 2359 2360 2361 2362
    double max_value;
    if (dec >= NOT_FIXED_DEC)
    {
      max_value= FLT_MAX;
    }
    else
    {
2363 2364
      uint tmp=min(field_length,array_elements(log_10)-1);
      max_value= (log_10[tmp]-1)/log_10[dec];
2365 2366 2367 2368 2369 2370
      /*
	The following comparison is needed to not get an overflow if nr
	is close to FLT_MAX
      */
      if (fabs(nr) < FLT_MAX/10.0e+32)
	nr= floor(nr*log_10[dec]+0.5)/log_10[dec];
2371 2372 2373 2374
    }
    if (nr < -max_value)
    {
      j= (float)-max_value;
2375
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2376 2377 2378 2379 2380
      error= 1;
    }
    else if (nr > max_value)
    {
      j= (float)max_value;
2381
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2382 2383 2384 2385
      error= 1;
    }
    else
      j= (float) nr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2386
  }
2387
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2388 2389 2390 2391 2392 2393 2394 2395
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4store(ptr,j);
  }
  else
#endif
    memcpy_fixed(ptr,(byte*) &j,sizeof(j));
2396
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2397 2398 2399
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2400
int Field_float::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2401
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2402
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2403
  float j= (float) nr;
2404 2405
  if (unsigned_flag && j < 0)
  {
2406
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2407
    j=0;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2408
    error= 1;
2409
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2410 2411 2412 2413 2414 2415 2416 2417
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float4store(ptr,j);
  }
  else
#endif
    memcpy_fixed(ptr,(byte*) &j,sizeof(j));
2418
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
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
}


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

2464 2465
  uint to_length=max(field_length,70);
  val_buffer->alloc(to_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
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
  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++;
2514
#else
2515 2516 2517
#ifdef HAVE_SNPRINTF
    to[to_length-1]=0;			// Safety
    snprintf(to,to_length-1,"%.*f",dec,nr);
2518
    to=strend(to);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2519
#else
2520
    to+= my_sprintf(to,(to,"%.*f",dec,nr));
2521
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582
#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++)
2583
	tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596
    }
    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;
    }
  }
}


2597 2598 2599 2600 2601 2602
bool Field_float::send_binary(Protocol *protocol)
{
  return protocol->store((float) Field_float::val_real(), dec, (String*) 0);
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
2603 2604 2605
void Field_float::sql_type(String &res) const
{
  if (dec == NOT_FIXED_DEC)
2606
  {
2607
    res.set_ascii("float", 5);
2608
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2609
  else
2610
  {
2611
    CHARSET_INFO *cs= res.charset();
2612
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2613
			    "float(%d,%d)",(int) field_length,dec));
2614
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2615 2616 2617
  add_zerofill_and_unsigned(res);
}

monty@mysql.com's avatar
monty@mysql.com committed
2618

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2619
/****************************************************************************
monty@mysql.com's avatar
monty@mysql.com committed
2620
  double precision floating point numbers
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2621 2622
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2623
int Field_double::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2624
{
monty@mysql.com's avatar
monty@mysql.com committed
2625
  int error;
2626
  char *end;
monty@mysql.com's avatar
monty@mysql.com committed
2627
  double nr= my_strntod(cs,(char*) from, len, &end, &error);
2628
  if (error || ((uint) (end-from) != len && table->in_use->count_cuted_fields))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2629
  {
monty@mysql.com's avatar
monty@mysql.com committed
2630
    error= 1;
2631
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2632
  }
monty@mysql.com's avatar
monty@mysql.com committed
2633 2634
  Field_double::store(nr);
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2635 2636 2637
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2638
int Field_double::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2639
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2640
  int error= 0;
2641

2642 2643 2644 2645
  if (isnan(nr))
  {
    nr= 0;
    set_null();
2646
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2647 2648 2649
    error= 1;
  }
  else if (unsigned_flag && nr < 0)
2650
  {
2651
    nr= 0;
2652
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2653
    error= 1;
2654
  }
2655 2656 2657 2658 2659 2660 2661 2662 2663
  else 
  {
    double max_value;
    if (dec >= NOT_FIXED_DEC)
    {
      max_value= DBL_MAX;
    }
    else
    {
2664 2665
      uint tmp=min(field_length,array_elements(log_10)-1);
      max_value= (log_10[tmp]-1)/log_10[dec];
2666 2667
      if (fabs(nr) < DBL_MAX/10.0e+32)
	nr= floor(nr*log_10[dec]+0.5)/log_10[dec];
2668 2669 2670 2671
    }
    if (nr < -max_value)
    {
      nr= -max_value;
2672
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2673 2674 2675 2676 2677
      error= 1;
    }
    else if (nr > max_value)
    {
      nr= max_value;
2678
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2679 2680 2681 2682
      error= 1;
    }
  }

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2683 2684 2685 2686 2687 2688 2689 2690
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8store(ptr,nr);
  }
  else
#endif
    doublestore(ptr,nr);
2691
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2692 2693 2694
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2695
int Field_double::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2696 2697
{
  double j= (double) nr;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2698
  int error= 0;
2699 2700
  if (unsigned_flag && j < 0)
  {
2701
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2702
    error= 1;
2703 2704
    j=0;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2705 2706 2707 2708 2709 2710 2711 2712
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    float8store(ptr,j);
  }
  else
#endif
    doublestore(ptr,j);
2713
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
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 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758
}


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

2759
  uint to_length=max(field_length, DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770
  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
bar@mysql.com's avatar
bar@mysql.com committed
2771
    char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
2772
    char *pos= buff;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
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
    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
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2811
    to[to_length-1]=0;			// Safety
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2812
    snprintf(to,to_length-1,"%.*f",dec,nr);
2813
    to=strend(to);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2814
#else
2815
    to+= my_sprintf(to,(to,"%.*f",dec,nr));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828
#endif
#endif
  }
#ifdef HAVE_FCONVERT
 end:
#endif

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

2829 2830
bool Field_double::send_binary(Protocol *protocol)
{
2831
  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
2832 2833
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846

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
  {
2847 2848
    doubleget(a, a_ptr);
    doubleget(b, b_ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867
  }
  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
2868
    doubleget(nr,ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2869 2870 2871 2872 2873 2874
  change_double_for_sort(nr, (byte*) to);
}


void Field_double::sql_type(String &res) const
{
2875
  CHARSET_INFO *cs=res.charset();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2876
  if (dec == NOT_FIXED_DEC)
2877
  {
2878
    res.set_ascii("double",6);
2879
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2880
  else
2881
  {
2882
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2883
			    "double(%d,%d)",(int) field_length,dec));
2884
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2885 2886 2887 2888
  add_zerofill_and_unsigned(res);
}


2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 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
/*
  TIMESTAMP type.
  Holds datetime values in range from 1970-01-01 00:00:01 UTC to 
  2038-01-01 00:00:00 UTC stored as number of seconds since Unix 
  Epoch in UTC.
  
  Up to one of timestamps columns in the table can be automatically 
  set on row update and/or have NOW() as default value.
  TABLE::timestamp_field points to Field object for such timestamp with 
  auto-set-on-update. TABLE::time_stamp holds offset in record + 1 for this
  field, and is used by handler code which performs updates required.
  
  Actually SQL-99 says that we should allow niladic functions (like NOW())
  as defaults for any field. Current limitations (only NOW() and only 
  for one TIMESTAMP field) are because of restricted binary .frm format 
  and should go away in the future.
  
  Also because of this limitation of binary .frm format we use 5 different
  unireg_check values with TIMESTAMP field to distinguish various cases of
  DEFAULT or ON UPDATE values. These values are:
  
  TIMESTAMP_OLD_FIELD - old timestamp, if there was not any fields with
    auto-set-on-update (or now() as default) in this table before, then this 
    field has NOW() as default and is updated when row changes, else it is 
    field which has 0 as default value and is not automaitcally updated.
  TIMESTAMP_DN_FIELD - field with NOW() as default but not set on update
    automatically (TIMESTAMP DEFAULT NOW())
  TIMESTAMP_UN_FIELD - field which is set on update automatically but has not 
    NOW() as default (but it may has 0 or some other const timestamp as 
    default) (TIMESTAMP ON UPDATE NOW()).
  TIMESTAMP_DNUN_FIELD - field which has now() as default and is auto-set on 
    update. (TIMESTAMP DEFAULT NOW() ON UPDATE NOW())
  NONE - field which is not auto-set on update with some other than NOW() 
    default value (TIMESTAMP DEFAULT 0).

  Note that TIMESTAMP_OLD_FIELD's are never created explicitly now, they are 
  left only for preserving ability to read old tables. Such fields replaced 
  with their newer analogs in CREATE TABLE and in SHOW CREATE TABLE. This is 
  because we want to prefer NONE unireg_check before TIMESTAMP_OLD_FIELD for 
  "TIMESTAMP DEFAULT 'Const'" field. (Old timestamps allowed such 
  specification too but ignored default value for first timestamp, which of 
  course is non-standard.) In most cases user won't notice any change, only
  exception is different behavior of old/new timestamps during ALTER TABLE.
 */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2933 2934 2935 2936

Field_timestamp::Field_timestamp(char *ptr_arg, uint32 len_arg,
				 enum utype unireg_check_arg,
				 const char *field_name_arg,
2937 2938 2939 2940
				 struct st_table *table_arg,
				 CHARSET_INFO *cs)
  :Field_str(ptr_arg, 19, (uchar*) 0,0,
	     unireg_check_arg, field_name_arg, table_arg, cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2941
{
2942 2943
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
2944 2945
  if (table && !table->timestamp_field && 
      unireg_check != NONE)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2946
  {
2947 2948
    /* This timestamp has auto-update */
    table->timestamp_field= this;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2949 2950 2951 2952 2953
    flags|=TIMESTAMP_FLAG;
  }
}


2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974
/*
    Sets TABLE::timestamp_default_now and TABLE::timestamp_on_update_now 
    members according to unireg type of this TIMESTAMP field.
  
  SYNOPSIS
    Field_timestamp::set_timestamp_offsets()
  
*/
void Field_timestamp::set_timestamp_offsets()
{
  ulong timestamp= (ulong) (ptr - (char*) table->record[0]) + 1;
  
  DBUG_ASSERT(table->timestamp_field == this && unireg_check != NONE);

  table->timestamp_default_now= 
    (unireg_check == TIMESTAMP_UN_FIELD)? 0 : timestamp;
  table->timestamp_on_update_now= 
    (unireg_check == TIMESTAMP_DN_FIELD)? 0 : timestamp;
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2975
int Field_timestamp::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2976
{
2977 2978 2979 2980 2981 2982 2983
  TIME l_time;
  my_time_t tmp= 0;
  int error;
  bool have_smth_to_conv;
  bool in_dst_time_gap;
  THD *thd= table->in_use;

2984 2985
  have_smth_to_conv= (str_to_datetime(from, len, &l_time, 0, &error) >
                      MYSQL_TIMESTAMP_ERROR);
2986 2987 2988
  
  if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
2989
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
2990 2991 2992 2993 2994 2995 2996

  if (have_smth_to_conv)
  {
    if (!(tmp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
    {
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                           ER_WARN_DATA_OUT_OF_RANGE,
2997
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
2998 2999 3000 3001 3002 3003 3004
      
      error= 1;
    }
    else if (in_dst_time_gap)
    {
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
                           ER_WARN_INVALID_TIMESTAMP, 
3005
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
3006 3007 3008 3009
      error= 1;
    }
  }

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3010 3011 3012 3013 3014 3015 3016 3017
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
3018
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3019 3020
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3021
int Field_timestamp::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3022
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3023
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3024 3025
  if (nr < 0 || nr > 99991231235959.0)
  {
3026 3027
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_OUT_OF_RANGE, 
3028
                         nr, MYSQL_TIMESTAMP_DATETIME);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3029 3030
    nr= 0;					// Avoid overflow on buff
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3031
  }
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3032
  error|= Field_timestamp::store((longlong) rint(nr));
3033
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3034 3035 3036
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3037
int Field_timestamp::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3038 3039
{
  TIME l_time;
3040 3041 3042 3043
  my_time_t timestamp= 0;
  int error;
  bool in_dst_time_gap;
  THD *thd= table->in_use;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3044

3045
  if (number_to_TIME(nr, &l_time, 0, &error))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3046
  {
3047 3048 3049 3050
    if (!(timestamp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
    {
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                          ER_WARN_DATA_OUT_OF_RANGE,
3051
                          nr, MYSQL_TIMESTAMP_DATETIME, 1);
3052 3053
      error= 1;
    }
3054
  
3055 3056 3057 3058
    if (in_dst_time_gap)
    {
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
                           ER_WARN_INVALID_TIMESTAMP, 
3059
                           nr, MYSQL_TIMESTAMP_DATETIME, !error);
3060 3061 3062 3063 3064 3065
      error= 1;
    }
  }
  else if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_TRUNCATED,
3066
                         nr, MYSQL_TIMESTAMP_DATETIME, 1);
3067

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3068 3069 3070 3071 3072 3073 3074
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,timestamp);
  }
  else
#endif
3075
    longstore(ptr,(uint32) timestamp);
3076
    
monty@mysql.com's avatar
monty@mysql.com committed
3077
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
}


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

longlong Field_timestamp::val_int(void)
{
  uint32 temp;
3089 3090
  TIME time_tmp;
  THD  *thd= table->in_use;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3091 3092 3093 3094 3095 3096 3097 3098 3099 3100

#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 */
3101 3102 3103 3104 3105 3106 3107
  
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp, (my_time_t)temp);
  thd->time_zone_used= 1;
  
  return time_tmp.year * LL(10000000000) + time_tmp.month * LL(100000000) +
         time_tmp.day * 1000000L + time_tmp.hour * 10000L +
         time_tmp.minute * 100 + time_tmp.second;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3108 3109 3110
}


serg@serg.mylan's avatar
serg@serg.mylan committed
3111
String *Field_timestamp::val_str(String *val_buffer, String *val_ptr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3112
{
3113
  uint32 temp, temp2;
3114 3115
  TIME time_tmp;
  THD *thd= table->in_use;
monty@mysql.com's avatar
monty@mysql.com committed
3116
  char *to;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3117 3118

  val_buffer->alloc(field_length+1);
monty@mysql.com's avatar
monty@mysql.com committed
3119
  to= (char*) val_buffer->ptr();
3120
  val_buffer->length(field_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3121 3122 3123 3124 3125 3126 3127 3128 3129 3130

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

  if (temp == 0L)
  {				      /* Zero time is "000000" */
3131
    val_ptr->set("0000-00-00 00:00:00", 19, &my_charset_bin);
3132
    return val_ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3133
  }
3134
  val_buffer->set_charset(&my_charset_bin);	// Safety
3135 3136 3137
  
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp,(my_time_t)temp);
  thd->time_zone_used= 1;
3138

3139
  temp= time_tmp.year % 100;
3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153
  if (temp < YY_PART_YEAR)
  {
    *to++= '2';
    *to++= '0';
  }
  else
  {
    *to++= '1';
    *to++= '9';
  }
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= '-';
3154
  temp=time_tmp.month;
3155 3156 3157 3158
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= '-';
3159
  temp=time_tmp.day;
3160 3161 3162 3163
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ' ';
3164
  temp=time_tmp.hour;
3165 3166 3167 3168
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ':';
3169
  temp=time_tmp.minute;
3170 3171 3172 3173
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ':';
3174
  temp=time_tmp.second;
3175 3176 3177 3178
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3179 3180 3181
  return val_buffer;
}

3182

3183
bool Field_timestamp::get_date(TIME *ltime, uint fuzzydate)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3184 3185
{
  long temp;
3186
  THD *thd= table->in_use;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3187 3188 3189 3190 3191 3192 3193 3194
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    temp=uint4korr(ptr);
  else
#endif
    longget(temp,ptr);
  if (temp == 0L)
  {				      /* Zero time is "000000" */
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3195 3196
    if (!fuzzydate)
      return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3197 3198 3199 3200
    bzero((char*) ltime,sizeof(*ltime));
  }
  else
  {
3201 3202
    thd->variables.time_zone->gmt_sec_to_TIME(ltime, (my_time_t)temp);
    thd->time_zone_used= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3203 3204 3205 3206 3207 3208
  }
  return 0;
}

bool Field_timestamp::get_time(TIME *ltime)
{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3209
  return Field_timestamp::get_date(ltime,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3210 3211
}

3212 3213 3214 3215

bool Field_timestamp::send_binary(Protocol *protocol)
{
  TIME tm;
3216
  Field_timestamp::get_date(&tm, TIME_FUZZY_DATE);
3217 3218 3219 3220
  return protocol->store(&tm);
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238
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;
}

3239

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262
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
{
3263
  res.set_ascii("timestamp", 9);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3264 3265 3266 3267 3268
}


void Field_timestamp::set_time()
{
3269
  long tmp= (long) table->in_use->query_start();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286
#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
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3287
int Field_time::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3288 3289 3290
{
  TIME ltime;
  long tmp;
3291 3292 3293
  int error;

  if (str_to_time(from, len, &ltime, &error))
3294
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3295
    tmp=0L;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3296
    error= 1;
3297
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
3298
                         from, len, MYSQL_TIMESTAMP_TIME, 1);
3299
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3300 3301
  else
  {
3302 3303 3304
    if (error)
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                           ER_WARN_DATA_TRUNCATED,
3305
                           from, len, MYSQL_TIMESTAMP_TIME, 1);
3306

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3307 3308 3309 3310 3311 3312
    if (ltime.month)
      ltime.day=0;
    tmp=(ltime.day*24L+ltime.hour)*10000L+(ltime.minute*100+ltime.second);
    if (tmp > 8385959)
    {
      tmp=8385959;
3313 3314
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                           ER_WARN_DATA_OUT_OF_RANGE,
3315
                           from, len, MYSQL_TIMESTAMP_TIME, !error);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3316
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3317 3318
    }
  }
3319
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3320 3321
  if (ltime.neg)
    tmp= -tmp;
3322 3323
  error |= Field_time::store((longlong) tmp);
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3324 3325 3326
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3327
int Field_time::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3328 3329
{
  long tmp;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3330
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3331 3332 3333
  if (nr > 8385959.0)
  {
    tmp=8385959L;
3334
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
3335
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3336
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3337 3338 3339 3340
  }
  else if (nr < -8385959.0)
  {
    tmp= -8385959L;
3341
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
3342
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3343
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3344 3345 3346 3347 3348 3349 3350 3351 3352
  }
  else
  {
    tmp=(long) floor(fabs(nr));			// Remove fractions
    if (nr < 0)
      tmp= -tmp;
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
    {
      tmp=0;
3353
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
3354 3355
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
                           MYSQL_TIMESTAMP_TIME);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3356
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3357 3358 3359
    }
  }
  int3store(ptr,tmp);
3360
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3361 3362 3363
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3364
int Field_time::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3365 3366
{
  long tmp;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3367
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3368 3369 3370
  if (nr > (longlong) 8385959L)
  {
    tmp=8385959L;
3371
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
3372 3373
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
                         MYSQL_TIMESTAMP_TIME, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3374
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3375 3376 3377 3378
  }
  else if (nr < (longlong) -8385959L)
  {
    tmp= -8385959L;
3379
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
3380 3381
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
                         MYSQL_TIMESTAMP_TIME, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3382
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3383 3384 3385 3386 3387 3388 3389
  }
  else
  {
    tmp=(long) nr;
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
    {
      tmp=0;
3390
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
3391 3392
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
                           MYSQL_TIMESTAMP_TIME, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3393
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3394 3395 3396
    }
  }
  int3store(ptr,tmp);
3397
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3398 3399 3400 3401 3402
}


double Field_time::val_real(void)
{
3403
  uint32 j= (uint32) uint3korr(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3404 3405 3406 3407 3408 3409 3410 3411
  return (double) j;
}

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

3412 3413 3414 3415 3416 3417

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3418 3419 3420
String *Field_time::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
3421
  TIME ltime;
3422
  val_buffer->alloc(19);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3423
  long tmp=(long) sint3korr(ptr);
3424
  ltime.neg= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3425 3426 3427
  if (tmp < 0)
  {
    tmp= -tmp;
3428 3429 3430 3431 3432 3433
    ltime.neg= 1;
  }
  ltime.day= (uint) 0;
  ltime.hour= (uint) (tmp/10000);
  ltime.minute= (uint) (tmp/100 % 100);
  ltime.second= (uint) (tmp % 100);
3434
  make_time((DATE_TIME_FORMAT*) 0, &ltime, val_buffer);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3435 3436 3437
  return val_buffer;
}

3438

monty@mysql.com's avatar
monty@mysql.com committed
3439 3440 3441 3442 3443 3444 3445
/*
  Normally we would not consider 'time' as a vaild date, but we allow
  get_date() here to be able to do things like
  DATE_FORMAT(time, "%l.%i %p")
*/
 
bool Field_time::get_date(TIME *ltime, uint fuzzydate)
3446
{
monty@mysql.com's avatar
monty@mysql.com committed
3447 3448 3449
  long tmp;
  if (!fuzzydate)
  {
3450 3451 3452 3453
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_WARN_DATA_OUT_OF_RANGE,
                        ER(ER_WARN_DATA_OUT_OF_RANGE), field_name,
                        table->in_use->row_count);
monty@mysql.com's avatar
monty@mysql.com committed
3454 3455 3456
    return 1;
  }
  tmp=(long) sint3korr(ptr);
3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470
  ltime->neg=0;
  if (tmp < 0)
  {
    ltime->neg= 1;
    tmp=-tmp;
  }
  ltime->hour=tmp/10000;
  tmp-=ltime->hour*10000;
  ltime->minute=   tmp/100;
  ltime->second= tmp % 100;
  ltime->year= ltime->month= ltime->day= ltime->second_part= 0;
  return 0;
}

monty@mysql.com's avatar
monty@mysql.com committed
3471

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3472 3473 3474 3475 3476 3477 3478 3479 3480
bool Field_time::get_time(TIME *ltime)
{
  long tmp=(long) sint3korr(ptr);
  ltime->neg=0;
  if (tmp < 0)
  {
    ltime->neg= 1;
    tmp=-tmp;
  }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
3481
  ltime->day= 0;
3482
  ltime->hour=   (int) (tmp/10000);
3483
  tmp-=ltime->hour*10000;
3484 3485
  ltime->minute= (int) tmp/100;
  ltime->second= (int) tmp % 100;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3486
  ltime->second_part=0;
3487
  ltime->time_type= MYSQL_TIMESTAMP_TIME;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3488 3489 3490
  return 0;
}

3491 3492 3493 3494 3495 3496 3497

bool Field_time::send_binary(Protocol *protocol)
{
  TIME tm;
  Field_time::get_time(&tm);
  tm.day= tm.hour/3600;				// Move hours to days
  tm.hour-= tm.day*3600;
3498
  return protocol->store_time(&tm);
3499 3500 3501
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
3502 3503
int Field_time::cmp(const char *a_ptr, const char *b_ptr)
{
3504 3505 3506
  int32 a,b;
  a=(int32) sint3korr(a_ptr);
  b=(int32) sint3korr(b_ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518
  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
{
3519
  res.set_ascii("time", 4);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3520 3521 3522 3523 3524 3525 3526 3527
}

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

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3528
int Field_year::store(const char *from, uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3529
{
3530
  int not_used;				// We can ignore result from str2int
3531
  char *end;
3532
  long nr= my_strntol(cs, from, len, 10, &end, &not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3533 3534 3535 3536

  if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155)
  {
    *ptr=0;
3537
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3538
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3539
  }
3540 3541
  if (table->in_use->count_cuted_fields && !test_if_int(from,len,end,cs))
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3542 3543 3544 3545 3546 3547 3548 3549
  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;
3550
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3551 3552
}

monty@mysql.com's avatar
monty@mysql.com committed
3553

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3554
int Field_year::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3555 3556
{
  if (nr < 0.0 || nr >= 2155.0)
3557 3558 3559 3560
  {
    (void) Field_year::store((longlong) -1);
    return 1;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3561
  else
3562
    return Field_year::store((longlong) nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3563 3564
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3565
int Field_year::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3566 3567 3568 3569
{
  if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155)
  {
    *ptr=0;
3570
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3571
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3572 3573 3574 3575 3576 3577 3578 3579 3580
  }
  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;
3581
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3582 3583
}

3584 3585 3586
bool Field_year::send_binary(Protocol *protocol)
{
  ulonglong tmp= Field_year::val_int();
3587
  return protocol->store_short(tmp);
3588
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
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

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
{
3617
  CHARSET_INFO *cs=res.charset();
3618
  res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(),
3619
			  "year(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3620 3621 3622 3623 3624 3625 3626 3627 3628 3629
}


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

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3630
int Field_date::store(const char *from, uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3631 3632
{
  TIME l_time;
3633
  uint32 tmp;
3634 3635
  int error;
  
3636
  if (str_to_datetime(from, len, &l_time, 1, &error) <= MYSQL_TIMESTAMP_ERROR)
3637
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3638
    tmp=0;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3639
    error= 1;
3640
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3641
  else
3642
    tmp=(uint32) l_time.year*10000L + (uint32) (l_time.month*100+l_time.day);
3643 3644 3645

  if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
3646
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
3647

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3648 3649 3650 3651 3652 3653 3654 3655
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
3656
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3657 3658 3659
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3660
int Field_date::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3661 3662
{
  long tmp;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3663
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3664 3665 3666 3667 3668
  if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
    nr=floor(nr/1000000.0);			// Timestamp to date
  if (nr < 0.0 || nr > 99991231.0)
  {
    tmp=0L;
3669 3670
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_OUT_OF_RANGE, 
3671
                         nr, MYSQL_TIMESTAMP_DATE);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3672
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683
  }
  else
    tmp=(long) rint(nr);
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
3684
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3685 3686 3687
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3688
int Field_date::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3689 3690
{
  long tmp;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3691
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3692 3693 3694 3695 3696
  if (nr >= LL(19000000000000) && nr < LL(99991231235959))
    nr=nr/LL(1000000);			// Timestamp to date
  if (nr < 0 || nr > LL(99991231))
  {
    tmp=0L;
3697 3698
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_OUT_OF_RANGE,
3699
                         nr, MYSQL_TIMESTAMP_DATE, 0);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3700
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711
  }
  else
    tmp=(long) nr;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
3712
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3713 3714 3715
}


3716 3717 3718 3719 3720 3721 3722 3723 3724 3725
bool Field_date::send_binary(Protocol *protocol)
{
  longlong tmp= Field_date::val_int();
  TIME tm;
  tm.year= (uint32) tmp/10000L % 10000;
  tm.month= (uint32) tmp/100 % 100;
  tm.day= (uint32) tmp % 100;
  return protocol->store_date(&tm);
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753

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)))
{
3754
  TIME ltime;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3755 3756 3757 3758 3759 3760 3761 3762
  val_buffer->alloc(field_length);
  int32 tmp;
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
    tmp=sint4korr(ptr);
  else
#endif
    longget(tmp,ptr);
3763 3764 3765 3766
  ltime.neg= 0;
  ltime.year= (int) ((uint32) tmp/10000L % 10000);
  ltime.month= (int) ((uint32) tmp/100 % 100);
  ltime.day= (int) ((uint32) tmp % 100);
3767
  make_date((DATE_TIME_FORMAT *) 0, &ltime, val_buffer);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3768 3769 3770
  return val_buffer;
}

3771

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3772 3773 3774 3775 3776 3777 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
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
{
3813
  res.set_ascii("date", 4);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3814 3815 3816 3817 3818 3819 3820 3821
}

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

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3822
int Field_newdate::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3823 3824 3825
{
  TIME l_time;
  long tmp;
3826
  int error;
3827
  if (str_to_datetime(from, len, &l_time, 1, &error) <= MYSQL_TIMESTAMP_ERROR)
3828
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3829
    tmp=0L;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3830
    error= 1;
3831
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3832 3833
  else
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
3834 3835 3836

  if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
3837
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
3838
    
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3839
  int3store(ptr,tmp);
3840
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3841 3842
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3843
int Field_newdate::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3844 3845
{
  if (nr < 0.0 || nr > 99991231235959.0)
3846 3847
  {
    (void) Field_newdate::store((longlong) -1);
3848
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
3849
                         ER_WARN_DATA_TRUNCATED, nr, MYSQL_TIMESTAMP_DATE);
3850 3851
    return 1;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3852
  else
3853
    return Field_newdate::store((longlong) rint(nr));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3854 3855 3856
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3857
int Field_newdate::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3858
{
3859
  int32 tmp;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3860
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3861 3862 3863 3864 3865
  if (nr >= LL(100000000) && nr <= LL(99991231235959))
    nr=nr/LL(1000000);			// Timestamp to date
  if (nr < 0L || nr > 99991231L)
  {
    tmp=0;
3866
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
3867 3868
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
                         MYSQL_TIMESTAMP_DATE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3869
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3870 3871 3872
  }
  else
  {
3873
    tmp=(int32) nr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3874 3875 3876
    if (tmp)
    {
      if (tmp < YY_PART_YEAR*10000L)			// Fix short dates
3877
	tmp+= (uint32) 20000000L;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3878
      else if (tmp < 999999L)
3879
	tmp+= (uint32) 19000000L;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3880
    }
3881 3882
    uint month= (uint) ((tmp/100) % 100);
    uint day=   (uint) (tmp%100);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3883 3884 3885
    if (month > 12 || day > 31)
    {
      tmp=0L;					// Don't allow date to change
3886
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
3887 3888
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
                           MYSQL_TIMESTAMP_DATE, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3889
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3890 3891 3892 3893
    }
    else
      tmp= day + month*32 + (tmp/10000)*16*32;
  }
3894
  int3store(ptr,(int32) tmp);
3895
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3896 3897 3898 3899 3900
}

void Field_newdate::store_time(TIME *ltime,timestamp_type type)
{
  long tmp;
3901
  if (type == MYSQL_TIMESTAMP_DATE || type == MYSQL_TIMESTAMP_DATETIME)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3902 3903 3904 3905
    tmp=ltime->year*16*32+ltime->month*32+ltime->day;
  else
  {
    tmp=0;
3906
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3907 3908 3909 3910
  }
  int3store(ptr,tmp);
}

3911 3912 3913 3914 3915 3916
bool Field_newdate::send_binary(Protocol *protocol)
{
  TIME tm;
  Field_newdate::get_date(&tm,0);
  return protocol->store_date(&tm);
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3917 3918 3919 3920 3921 3922 3923 3924

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

longlong Field_newdate::val_int(void)
{
3925
  ulong j= uint3korr(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3926 3927 3928 3929 3930 3931 3932 3933 3934
  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);
3935
  uint32 tmp=(uint32) uint3korr(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3936 3937 3938 3939
  int part;
  char *pos=(char*) val_buffer->ptr()+10;

  /* Open coded to get more speed */
3940
  *pos--=0;					// End NULL
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3941
  part=(int) (tmp & 31);
3942 3943 3944
  *pos--= (char) ('0'+part%10);
  *pos--= (char) ('0'+part/10);
  *pos--= '-';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3945
  part=(int) (tmp >> 5 & 15);
3946 3947 3948
  *pos--= (char) ('0'+part%10);
  *pos--= (char) ('0'+part/10);
  *pos--= '-';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3949
  part=(int) (tmp >> 9);
3950 3951 3952 3953
  *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);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3954 3955 3956
  return val_buffer;
}

3957
bool Field_newdate::get_date(TIME *ltime,uint fuzzydate)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3958 3959 3960
{
  if (is_null())
    return 1;
3961
  uint32 tmp=(uint32) uint3korr(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3962 3963 3964
  ltime->day=   tmp & 31;
  ltime->month= (tmp >> 5) & 15;
  ltime->year=  (tmp >> 9);
3965
  ltime->time_type= MYSQL_TIMESTAMP_DATE;
3966
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3967
  return (!fuzzydate && (!ltime->month || !ltime->day)) ? 1 : 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3968 3969 3970 3971
}

bool Field_newdate::get_time(TIME *ltime)
{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
3972
  return Field_newdate::get_date(ltime,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3973 3974 3975 3976
}

int Field_newdate::cmp(const char *a_ptr, const char *b_ptr)
{
3977 3978 3979
  uint32 a,b;
  a=(uint32) uint3korr(a_ptr);
  b=(uint32) uint3korr(b_ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991
  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
{
3992
  res.set_ascii("date", 4);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3993 3994 3995 3996 3997 3998 3999 4000 4001 4002
}


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

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4003
int Field_datetime::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4004
{
4005 4006 4007 4008
  TIME time_tmp;
  int error;
  ulonglong tmp= 0;
  
4009
  if (str_to_datetime(from, len, &time_tmp, 1, &error) > MYSQL_TIMESTAMP_ERROR)
4010 4011 4012 4013 4014
    tmp= TIME_to_ulonglong_datetime(&time_tmp);
  
  if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_OUT_OF_RANGE,
4015
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
4016

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4017 4018 4019 4020 4021 4022 4023 4024
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
4025
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4026 4027 4028
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4029
int Field_datetime::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4030
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4031
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4032 4033
  if (nr < 0.0 || nr > 99991231235959.0)
  {
4034 4035
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_OUT_OF_RANGE,
4036
                         nr, MYSQL_TIMESTAMP_DATETIME);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4037
    nr=0.0;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4038
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4039
  }
4040 4041
  error |= Field_datetime::store((longlong) rint(nr));
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4042 4043 4044
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4045
int Field_datetime::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4046
{
4047
  TIME not_used;
4048 4049
  int error;
  longlong initial_nr= nr;
4050
  
4051 4052 4053 4054 4055
  nr= number_to_TIME(nr, &not_used, 1, &error);

  if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_TRUNCATED, initial_nr, 
4056
                         MYSQL_TIMESTAMP_DATETIME, 1);
4057

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4058 4059 4060 4061 4062 4063 4064 4065
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,nr);
  }
  else
#endif
    longlongstore(ptr,nr);
4066
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4067 4068
}

monty@mysql.com's avatar
monty@mysql.com committed
4069

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4070 4071 4072
void Field_datetime::store_time(TIME *ltime,timestamp_type type)
{
  longlong tmp;
4073
  if (type == MYSQL_TIMESTAMP_DATE || type == MYSQL_TIMESTAMP_DATETIME)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4074 4075 4076 4077 4078
    tmp=((ltime->year*10000L+ltime->month*100+ltime->day)*LL(1000000)+
	 (ltime->hour*10000L+ltime->minute*100+ltime->second));
  else
  {
    tmp=0;
4079
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090
  }
#ifdef WORDS_BIGENDIAN
  if (table->db_low_byte_first)
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
}

4091 4092 4093
bool Field_datetime::send_binary(Protocol *protocol)
{
  TIME tm;
4094
  Field_datetime::get_date(&tm, TIME_FUZZY_DATE);
4095 4096 4097
  return protocol->store(&tm);
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142

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;
4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161
  *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);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4162 4163 4164
  return val_buffer;
}

4165
bool Field_datetime::get_date(TIME *ltime, uint fuzzydate)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4166 4167
{
  longlong tmp=Field_datetime::val_int();
4168 4169 4170
  uint32 part1,part2;
  part1=(uint32) (tmp/LL(1000000));
  part2=(uint32) (tmp - (ulonglong) part1*LL(1000000));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4171

4172
  ltime->time_type=	MYSQL_TIMESTAMP_DATETIME;
4173 4174 4175 4176 4177 4178 4179 4180
  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);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4181
  return (!fuzzydate && (!ltime->month || !ltime->day)) ? 1 : 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4182 4183 4184 4185
}

bool Field_datetime::get_time(TIME *ltime)
{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4186
  return Field_datetime::get_date(ltime,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238
}

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
{
4239
  res.set_ascii("datetime", 8);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4240 4241 4242 4243 4244 4245 4246 4247 4248
}

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

	/* Copy a string and fill with space */

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4249
int Field_string::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4250
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4251
  int error= 0;
4252
  uint32 not_used;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4253 4254
  char buff[80];
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
4255
  uint copy_length;
4256 4257 4258 4259
  
  /* See the comment for Field_long::store(long long) */
  DBUG_ASSERT(table->in_use == current_thd);
  
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4260
  /* Convert character set if nesessary */
4261
  if (String::needs_conversion(length, cs, field_charset, &not_used))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4262 4263 4264 4265 4266
  { 
    tmpstr.copy(from, length, cs, field_charset);
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
4267 4268

  /* 
4269
    Make sure we don't break a multibyte sequence
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
4270 4271
    as well as don't copy a malformed data.
  */
4272 4273 4274 4275
  copy_length= field_charset->cset->well_formed_len(field_charset,
						    from,from+length,
						    field_length/
						    field_charset->mbmaxlen);
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
4276 4277 4278 4279 4280
  memcpy(ptr,from,copy_length);
  if (copy_length < field_length)	// Append spaces if shorter
    field_charset->cset->fill(field_charset,ptr+copy_length,
			      field_length-copy_length,' ');
  
4281
  if ((copy_length < length) && table->in_use->count_cuted_fields)
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
4282 4283 4284 4285 4286 4287 4288
  {					// Check if we loosed some info
    const char *end=from+length;
    from+= copy_length;
    from+= field_charset->cset->scan(field_charset, from, end,
				     MY_SEQ_SPACES);
    if (from != end)
    {
4289
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
4290
      error=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4291 4292
    }
  }
4293
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4294 4295 4296
}


4297 4298 4299 4300
/*
  Store double value in Field_string or Field_varstring.

  SYNOPSIS
serg@serg.mylan's avatar
serg@serg.mylan committed
4301
    store(double nr)
4302 4303 4304 4305 4306 4307
    nr            number

  DESCRIPTION
    Pretty prints double number into field_length characters buffer.
*/

serg@serg.mylan's avatar
serg@serg.mylan committed
4308
int Field_str::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4309
{
4310
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
4311
  uint length;
monty@mysql.com's avatar
monty@mysql.com committed
4312 4313
  bool use_scientific_notation= TRUE;
  use_scientific_notation= TRUE;
konstantin@mysql.com's avatar
konstantin@mysql.com committed
4314
  if (field_length < 32 && fabs(nr) < log_10[field_length]-1)
monty@mysql.com's avatar
monty@mysql.com committed
4315
    use_scientific_notation= FALSE;
konstantin@mysql.com's avatar
konstantin@mysql.com committed
4316

monty@mysql.com's avatar
monty@mysql.com committed
4317 4318 4319 4320 4321
  length= (uint) my_sprintf(buff, (buff, "%-.*g",
                                   (use_scientific_notation ?
                                    max(0, (int)field_length-5) :
                                    field_length),
                                   nr));
4322 4323 4324 4325 4326
  /*
    +1 below is because "precision" in %g above means the
    max. number of significant digits, not the output width.
    Thus the width can be larger than number of significant digits by 1
    (for decimal point)
4327 4328
    the test for field_length < 5 is for extreme cases,
    like inserting 500.0 in char(1)
4329
  */
4330
  DBUG_ASSERT(field_length < 5 || length <= field_length+1);
4331
  return store((const char *) buff, length, charset());
4332 4333
}

monty@mysql.com's avatar
monty@mysql.com committed
4334

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4335
int Field_string::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4336
{
4337 4338 4339
  char buff[64];
  int  l;
  CHARSET_INFO *cs=charset();
4340
  l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),-10,nr);
4341
  return Field_string::store(buff,(uint)l,cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4342 4343 4344 4345 4346
}


double Field_string::val_real(void)
{
4347
  int not_used;
4348
  CHARSET_INFO *cs=charset();
4349
  return my_strntod(cs,ptr,field_length,(char**)0,&not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4350 4351 4352 4353 4354
}


longlong Field_string::val_int(void)
{
4355
  int not_used;
4356
  CHARSET_INFO *cs=charset();
4357
  return my_strntoll(cs,ptr,field_length,10,NULL,&not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4358 4359 4360 4361 4362 4363
}


String *Field_string::val_str(String *val_buffer __attribute__((unused)),
			      String *val_ptr)
{
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4364
  uint length= field_charset->cset->lengthsp(field_charset, ptr, field_length);
4365 4366
  /* See the comment for Field_long::store(long long) */
  DBUG_ASSERT(table->in_use == current_thd);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4367
  val_ptr->set((const char*) ptr, length, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4368 4369 4370 4371 4372 4373
  return val_ptr;
}


int Field_string::cmp(const char *a_ptr, const char *b_ptr)
{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
4374
  if (field_charset->strxfrm_multiply > 1)
4375 4376 4377 4378 4379
  {
    /*
      We have to remove end space to be able to compare multi-byte-characters
      like in latin_de 'ae' and 0xe4
    */
4380
    return field_charset->coll->strnncollsp(field_charset,
4381 4382 4383
                                            (const uchar*) a_ptr, field_length,
                                            (const uchar*) b_ptr,
                                            field_length);
4384
  }
serg@serg.mylan's avatar
serg@serg.mylan committed
4385 4386
  return my_strnncoll(field_charset,(const uchar*) a_ptr, field_length,
                                    (const uchar*) b_ptr, field_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4387 4388 4389 4390
}

void Field_string::sort_string(char *to,uint length)
{
4391
  uint tmp=my_strnxfrm(field_charset,
4392 4393
		       (unsigned char *) to, length,
		       (unsigned char *) ptr, field_length);
4394
  if (tmp < length)
4395
    field_charset->cset->fill(field_charset, to + tmp, length - tmp, ' ');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4396 4397 4398 4399 4400
}


void Field_string::sql_type(String &res) const
{
4401
  CHARSET_INFO *cs=res.charset();
4402
  ulong length= cs->cset->snprintf(cs,(char*) res.ptr(),
4403
			    res.alloced_length(), "%s(%d)",
4404 4405 4406
			    (field_length > 3 &&
			     (table->db_options_in_use &
			      HA_OPTION_PACK_RECORD) ?
4407
			      (has_charset() ? "varchar" : "varbinary") :
4408
			      (has_charset() ? "char" : "binary")),
4409
			    (int) field_length / charset()->mbmaxlen);
4410
  res.length(length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425
}


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


4426 4427
char *Field_string::pack_key(char *to, const char *from, uint max_length)
{
monty@mysql.com's avatar
monty@mysql.com committed
4428 4429
  uint length=      min(field_length,max_length);
  uint char_length= max_length/field_charset->mbmaxlen;
4430
  if (length > char_length)
serg@serg.mylan's avatar
serg@serg.mylan committed
4431
    char_length= my_charpos(field_charset, from, from+length, char_length);
4432
  set_if_smaller(length, char_length);
serg@serg.mylan's avatar
serg@serg.mylan committed
4433 4434
  while (length && from[length-1] == ' ')
    length--;
4435 4436 4437 4438 4439 4440
  *to= (uchar)length;
  memcpy(to+1, from, length);
  return to+1+length;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453
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++;
4454 4455 4456
  return my_strnncoll(field_charset,
		      (const uchar*)a,a_length,
		      (const uchar*)b,b_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4457 4458 4459
}


4460 4461 4462 4463 4464 4465 4466
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);
4467 4468 4469
  return my_strnncoll(field_charset,
		     (const uchar*)ptr,a_length,
		     (const uchar*)b, b_length);
4470 4471 4472
}


4473
uint Field_string::packed_col_length(const char *data_ptr, uint length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4474
{
4475
  if (length > 255)
4476
    return uint2korr(data_ptr)+2;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4477
  else
4478
    return (uint) ((uchar) *data_ptr)+1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4479 4480 4481 4482
}

uint Field_string::max_packed_col_length(uint max_length)
{
4483
  return (max_length > 255 ? 2 : 1)+max_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4484 4485 4486 4487 4488 4489 4490 4491
}


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


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4492
int Field_varstring::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4493
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4494
  int error= 0;
4495
  uint32 not_used;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4496 4497
  char buff[80];
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
4498

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4499
  /* Convert character set if nesessary */
4500
  if (String::needs_conversion(length, cs, field_charset, &not_used))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4501 4502 4503 4504 4505
  { 
    tmpstr.copy(from, length, cs, field_charset);
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
4506
  if (length > field_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4507 4508
  {
    length=field_length;
4509
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4510
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4511
  }
4512
  memcpy(ptr+HA_KEY_BLOB_LENGTH,from,length);
4513
  int2store(ptr, length);
4514
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4515 4516 4517
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4518
int Field_varstring::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4519
{
4520 4521 4522
  char buff[64];
  int  l;
  CHARSET_INFO *cs=charset();
4523
  l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),-10,nr);
4524
  return Field_varstring::store(buff,(uint)l,cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4525 4526 4527 4528 4529
}


double Field_varstring::val_real(void)
{
4530
  int not_used;
4531
  uint length=uint2korr(ptr)+HA_KEY_BLOB_LENGTH;
4532
  CHARSET_INFO *cs=charset();
4533
  return my_strntod(cs, ptr+HA_KEY_BLOB_LENGTH, length, (char**)0, &not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4534 4535 4536 4537 4538
}


longlong Field_varstring::val_int(void)
{
4539
  int not_used;
4540
  uint length=uint2korr(ptr)+HA_KEY_BLOB_LENGTH;
4541
  CHARSET_INFO *cs=charset();
4542
  return my_strntoll(cs,ptr+HA_KEY_BLOB_LENGTH,length,10,NULL, &not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4543 4544 4545 4546 4547 4548 4549
}


String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
				 String *val_ptr)
{
  uint length=uint2korr(ptr);
4550
  val_ptr->set((const char*) ptr+HA_KEY_BLOB_LENGTH,length,field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4551 4552 4553 4554 4555 4556 4557 4558 4559
  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;
4560 4561 4562 4563 4564
  diff= my_strnncoll(field_charset,
		     (const uchar*) a_ptr+HA_KEY_BLOB_LENGTH,
		     min(a_length,b_length),
		     (const uchar*) b_ptr+HA_KEY_BLOB_LENGTH,
		     min(a_length,b_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4565 4566 4567 4568 4569 4570
  return diff ? diff : (int) (a_length - b_length);
}

void Field_varstring::sort_string(char *to,uint length)
{
  uint tot_length=uint2korr(ptr);
4571 4572 4573 4574
  tot_length= my_strnxfrm(field_charset,
			  (uchar*) to, length,
			  (uchar*) ptr+HA_KEY_BLOB_LENGTH,
			  tot_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4575
  if (tot_length < length)
4576 4577
    field_charset->cset->fill(field_charset, to+tot_length,length-tot_length,
			      binary() ? (char) 0 : ' ');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4578 4579 4580 4581 4582
}


void Field_varstring::sql_type(String &res) const
{
4583
  CHARSET_INFO *cs=res.charset();
4584
  ulong length= cs->cset->snprintf(cs,(char*) res.ptr(),
4585
			     res.alloced_length(),"varchar(%u)",
4586
			     field_length / charset()->mbmaxlen);
4587
  res.length(length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4588 4589 4590 4591
}

char *Field_varstring::pack(char *to, const char *from, uint max_length)
{
4592
  uint length=uint2korr(from);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4593 4594
  if (length > max_length)
    length=max_length;
4595
  *to++= (char) (length & 255);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4596
  if (max_length > 255)
4597
    *to++= (char) (length >> 8);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4598
  if (length)
4599
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4600 4601 4602 4603
  return to+length;
}


4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621
char *Field_varstring::pack_key(char *to, const char *from, uint max_length)
{
  uint length=uint2korr(from);
  uint char_length= (field_charset->mbmaxlen > 1) ?
              max_length/field_charset->mbmaxlen : max_length;
  from+=HA_KEY_BLOB_LENGTH;
  if (length > char_length)
    char_length= my_charpos(field_charset, from, from+length, char_length);
  set_if_smaller(length, char_length);
  *to++= (char) (length & 255);
  if (max_length > 255)
    *to++= (char) (length >> 8);
  if (length)
    memcpy(to, from, length);
  return to+length;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636
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)
4637
    memcpy(to+HA_KEY_BLOB_LENGTH, from, length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4638 4639 4640 4641 4642 4643 4644 4645 4646 4647
  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)
  {
4648 4649
    a_length=uint2korr(a); a+= 2;
    b_length=uint2korr(b); b+= 2;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4650 4651 4652 4653 4654 4655
  }
  else
  {
    a_length= (uint) (uchar) *a++;
    b_length= (uint) (uchar) *b++;
  }
4656
  return my_strnncoll(field_charset,
4657 4658
		     (const uchar*) a, a_length,
		     (const uchar*) b, b_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4659 4660
}

4661 4662
int Field_varstring::pack_cmp(const char *b, uint key_length)
{
4663 4664
  char *a= ptr+HA_KEY_BLOB_LENGTH;
  uint a_length= uint2korr(ptr);
4665 4666 4667
  uint b_length;
  if (key_length > 255)
  {
4668
    b_length=uint2korr(b); b+= 2;
4669 4670 4671 4672 4673
  }
  else
  {
    b_length= (uint) (uchar) *b++;
  }
4674
  return my_strnncoll(field_charset,
4675 4676
		     (const uchar*) a, a_length,
		     (const uchar*) b, b_length);
4677 4678
}

4679
uint Field_varstring::packed_col_length(const char *data_ptr, uint length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4680
{
4681
  if (length > 255)
4682
    return uint2korr(data_ptr)+HA_KEY_BLOB_LENGTH;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4683
  else
4684
    return (uint) ((uchar) *data_ptr)+1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4685 4686 4687 4688
}

uint Field_varstring::max_packed_col_length(uint max_length)
{
4689
  return (max_length > 255 ? 2 : 1)+max_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4690 4691
}

4692 4693
void Field_varstring::get_key_image(char *buff, uint length, CHARSET_INFO *cs,
				    imagetype type)
4694 4695 4696 4697 4698
{
  uint f_length=uint2korr(ptr);
  if (f_length > length)
    f_length= length;
  int2store(buff,length);
4699
  memcpy(buff+HA_KEY_BLOB_LENGTH, ptr+HA_KEY_BLOB_LENGTH, length);
4700 4701
#ifdef HAVE_purify
  if (f_length < length)
4702
    bzero(buff+HA_KEY_BLOB_LENGTH+f_length, (length-f_length));
4703 4704 4705
#endif
}

4706
void Field_varstring::set_key_image(char *buff,uint length, CHARSET_INFO *cs)
4707 4708
{
  length=uint2korr(buff);			// Real length is here
4709
  (void) Field_varstring::store(buff+HA_KEY_BLOB_LENGTH, length, cs);
4710 4711 4712 4713
}



bk@work.mysql.com's avatar
bk@work.mysql.com committed
4714 4715 4716 4717 4718 4719
/****************************************************************************
** 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.
****************************************************************************/

4720
Field_blob::Field_blob(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4721 4722
		       enum utype unireg_check_arg, const char *field_name_arg,
		       struct st_table *table_arg,uint blob_pack_length,
4723
		       CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4724 4725
  :Field_str(ptr_arg, (1L << min(blob_pack_length,3)*8)-1L,
	     null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
4726
	     table_arg, cs),
hf@deer.(none)'s avatar
hf@deer.(none) committed
4727
   packlength(blob_pack_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4728 4729 4730 4731 4732 4733 4734
{
  flags|= BLOB_FLAG;
  if (table)
    table->blob_fields++;
}


4735
void Field_blob::store_length(uint32 number)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766
{
  switch (packlength) {
  case 1:
    ptr[0]= (uchar) number;
    break;
  case 2:
#ifdef WORDS_BIGENDIAN
    if (table->db_low_byte_first)
    {
      int2store(ptr,(unsigned short) number);
    }
    else
#endif
      shortstore(ptr,(unsigned short) number);
    break;
  case 3:
    int3store(ptr,number);
    break;
  case 4:
#ifdef WORDS_BIGENDIAN
    if (table->db_low_byte_first)
    {
      int4store(ptr,number);
    }
    else
#endif
      longstore(ptr,number);
  }
}


4767
uint32 Field_blob::get_length(const char *pos)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4768 4769 4770
{
  switch (packlength) {
  case 1:
4771
    return (uint32) (uchar) pos[0];
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4772 4773 4774 4775 4776 4777 4778 4779 4780
  case 2:
    {
      uint16 tmp;
#ifdef WORDS_BIGENDIAN
      if (table->db_low_byte_first)
	tmp=sint2korr(pos);
      else
#endif
	shortget(tmp,pos);
4781
      return (uint32) tmp;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4782 4783
    }
  case 3:
4784
    return (uint32) uint3korr(pos);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4785 4786 4787 4788 4789 4790 4791 4792 4793
  case 4:
    {
      uint32 tmp;
#ifdef WORDS_BIGENDIAN
      if (table->db_low_byte_first)
	tmp=uint4korr(pos);
      else
#endif
	longget(tmp,pos);
4794
      return (uint32) tmp;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4795 4796 4797 4798 4799 4800
    }
  }
  return 0;					// Impossible
}


4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836
/*
  Put a blob length field into a record buffer.

  SYNOPSIS
    Field_blob::put_length()
    pos                         Pointer into the record buffer.
    length                      The length value to put.

  DESCRIPTION
    Depending on the maximum length of a blob, its length field is
    put into 1 to 4 bytes. This is a property of the blob object,
    described by 'packlength'.

  RETURN
    nothing
*/

void Field_blob::put_length(char *pos, uint32 length)
{
  switch (packlength) {
  case 1:
    *pos= (char) length;
    break;
  case 2:
    int2store(pos, length);
    break;
  case 3:
    int3store(pos, length);
    break;
  case 4:
    int4store(pos, length);
    break;
  }
}


bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4837
int Field_blob::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4838
{
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4839
  if (!length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4840 4841 4842 4843 4844
  {
    bzero(ptr,Field_blob::pack_length());
  }
  else
  {
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
4845
    bool was_conversion;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4846 4847
    char buff[80];
    String tmpstr(buff,sizeof(buff), &my_charset_bin);
4848
    uint copy_length;
4849
    uint32 not_used;
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
4850

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4851
    /* Convert character set if nesessary */
4852 4853
    if ((was_conversion= String::needs_conversion(length, cs, field_charset,
						  &not_used)))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
4854 4855 4856 4857 4858
    { 
      tmpstr.copy(from, length, cs, field_charset);
      from= tmpstr.ptr();
      length=  tmpstr.length();
    }
4859 4860
    
    copy_length= max_data_length();
4861 4862 4863 4864 4865 4866 4867 4868 4869
    /*
      copy_length is ok as last argument to well_formed_len as this is never
      used to limit the length of the data. The cut of long data is done with
      the 'min()' call below.
    */
    copy_length= field_charset->cset->well_formed_len(field_charset,
						      from,from +
						      min(length, copy_length),
						      copy_length);
4870
    if (copy_length < length)
4871
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
4872 4873 4874
    
    Field_blob::store_length(copy_length);
    if (was_conversion || table->copy_blobs || copy_length <= MAX_FIELD_WIDTH)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4875
    {						// Must make a copy
4876 4877
      if (from != value.ptr())			// For valgrind
      {
4878
	value.copy(from,copy_length,charset());
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4879
	from=value.ptr();
4880
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4881 4882 4883
    }
    bmove(ptr+packlength,(char*) &from,sizeof(char*));
  }
4884
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4885 4886 4887
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4888
int Field_blob::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4889
{
4890 4891 4892
  CHARSET_INFO *cs=charset();
  value.set(nr, 2, cs);
  return Field_blob::store(value.ptr(),(uint) value.length(), cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4893 4894 4895
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4896
int Field_blob::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4897
{
4898 4899 4900
  CHARSET_INFO *cs=charset();
  value.set(nr, cs);
  return Field_blob::store(value.ptr(), (uint) value.length(), cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4901 4902 4903 4904 4905
}


double Field_blob::val_real(void)
{
4906
  int not_used;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4907 4908 4909 4910
  char *blob;
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
    return 0.0;
4911
  uint32 length=get_length(ptr);
4912
  CHARSET_INFO *cs=charset();
4913
  return my_strntod(cs,blob,length,(char**)0, &not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4914 4915 4916 4917 4918
}


longlong Field_blob::val_int(void)
{
4919
  int not_used;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4920 4921 4922 4923
  char *blob;
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
    return 0;
4924
  uint32 length=get_length(ptr);
4925
  return my_strntoll(charset(),blob,length,10,NULL,&not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4926 4927 4928 4929 4930 4931 4932 4933 4934
}


String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
			    String *val_ptr)
{
  char *blob;
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
4935
    val_ptr->set("",0,charset());	// A bit safer than ->length(0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4936
  else
4937
    val_ptr->set((const char*) blob,get_length(ptr),charset());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4938 4939 4940 4941
  return val_ptr;
}


4942 4943
int Field_blob::cmp(const char *a,uint32 a_length, const char *b,
		    uint32 b_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4944
{
4945 4946 4947
  int diff=my_strnncoll(field_charset,
			(const uchar*)a,min(a_length,b_length),
			(const uchar*)b,min(a_length,b_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
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
  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,
4975
			   uint32 max_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4976 4977 4978
{
  char *a,*b;
  uint diff;
4979
  uint32 a_length,b_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994
  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 */

4995
void Field_blob::get_key_image(char *buff,uint length,
4996
			       CHARSET_INFO *cs, imagetype type)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4997
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4998
  uint32 blob_length= get_length(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4999
  char *blob;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5000

hf@deer.(none)'s avatar
hf@deer.(none) committed
5001
#ifdef HAVE_SPATIAL
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5002
  if (type == itMBR)
5003
  {
5004
    const char *dummy;
5005
    MBR mbr;
5006 5007
    Geometry_buffer buffer;
    Geometry *gobj;
5008 5009 5010 5011 5012 5013 5014

    if (blob_length < SRID_SIZE)
    {
      bzero(buff, SIZEOF_STORED_DOUBLE*4);
      return;
    }
    get_ptr(&blob);
5015 5016 5017
    gobj= Geometry::create_from_wkb(&buffer,
				    blob + SRID_SIZE, blob_length - SRID_SIZE);
    if (gobj->get_mbr(&mbr, &dummy))
5018 5019 5020 5021 5022 5023 5024 5025
      bzero(buff, SIZEOF_STORED_DOUBLE*4);
    else
    {
      float8store(buff,    mbr.xmin);
      float8store(buff+8,  mbr.xmax);
      float8store(buff+16, mbr.ymin);
      float8store(buff+24, mbr.ymax);
    }
5026 5027
    return;
  }
hf@deer.(none)'s avatar
hf@deer.(none) committed
5028
#endif /*HAVE_SPATIAL*/
5029

5030
  if ((uint32) length > blob_length)
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5031
  {
5032 5033 5034 5035
    /*
      Must clear this as we do a memcmp in opt_range.cc to detect
      identical keys
    */
5036
    bzero(buff+HA_KEY_BLOB_LENGTH+blob_length, (length-blob_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5037
    length=(uint) blob_length;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
5038
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5039 5040
  int2store(buff,length);
  get_ptr(&blob);
5041
  memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5042 5043
}

5044
void Field_blob::set_key_image(char *buff,uint length, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5045
{
5046 5047
  length= uint2korr(buff);
  (void) Field_blob::store(buff+HA_KEY_BLOB_LENGTH, length, cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5048 5049
}

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5050

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5051 5052 5053 5054 5055 5056
int Field_blob::key_cmp(const byte *key_ptr, uint max_key_length)
{
  char *blob1;
  uint blob_length=get_length(ptr);
  memcpy_fixed(&blob1,ptr+packlength,sizeof(char*));
  return Field_blob::cmp(blob1,min(blob_length, max_key_length),
5057 5058
			 (char*) key_ptr+HA_KEY_BLOB_LENGTH,
			 uint2korr(key_ptr));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5059 5060 5061 5062
}

int Field_blob::key_cmp(const byte *a,const byte *b)
{
5063 5064
  return Field_blob::cmp((char*) a+HA_KEY_BLOB_LENGTH, uint2korr(a),
			 (char*) b+HA_KEY_BLOB_LENGTH, uint2korr(b));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5065 5066 5067 5068 5069 5070 5071
}


void Field_blob::sort_string(char *to,uint length)
{
  char *blob;
  uint blob_length=get_length();
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5072

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5073 5074 5075 5076 5077
  if (!blob_length)
    bzero(to,length);
  else
  {
    memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5078 5079
    
    blob_length=my_strnxfrm(field_charset,
5080 5081
                            (uchar*) to, length, 
                            (uchar*) blob, blob_length);
5082
    if (blob_length < length)
5083 5084 5085
      field_charset->cset->fill(field_charset, to+blob_length,
				length-blob_length,
				binary() ? (char) 0 : ' ');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5086 5087 5088 5089 5090 5091 5092
  }
}


void Field_blob::sql_type(String &res) const
{
  const char *str;
5093
  uint length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5094
  switch (packlength) {
5095 5096 5097 5098 5099
  default: str="tiny"; length=4; break;
  case 2:  str="";     length=0; break;
  case 3:  str="medium"; length= 6; break;
  case 4:  str="long";  length=4; break;
  }
5100
  res.set_ascii(str,length);
5101
  if (charset() == &my_charset_bin)
5102 5103 5104 5105
    res.append("blob");
  else
  {
    res.append("text");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5106 5107 5108 5109
  }
}


5110 5111 5112 5113
char *Field_blob::pack(char *to, const char *from, uint max_length)
{
  char *save=ptr;
  ptr=(char*) from;
5114
  uint32 length=get_length();			// Length of from string
5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136
  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);
5137
  uint32 length=get_length(from);
5138 5139 5140 5141 5142 5143 5144 5145
  from+=packlength;
  if (length)
    memcpy_fixed(to+packlength, &from, sizeof(from));
  else
    bzero(to+packlength,sizeof(from));
  return from+length;
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161
/* 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++;
  }
5162
  return my_strnncoll(field_charset,
5163 5164
		     (const uchar*) a, a_length,
		     (const uchar*) b, b_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5165 5166
}

5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184

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++;
  }
5185
  return my_strnncoll(field_charset,
5186 5187
		     (const uchar*) a, a_length,
		     (const uchar*) b, b_length);
5188 5189
}

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5192 5193
char *Field_blob::pack_key(char *to, const char *from, uint max_length)
{
5194 5195
  char *save=ptr;
  ptr=(char*) from;
5196
  uint32 length=get_length();			// Length of from string
5197 5198 5199 5200 5201 5202 5203
  uint char_length= (field_charset->mbmaxlen > 1) ?
              max_length/field_charset->mbmaxlen : max_length;
  if (length)
    get_ptr((char**) &from);
  if (length > char_length)
    char_length= my_charpos(field_charset, from, from+length, char_length);
  set_if_smaller(length, char_length);
5204 5205 5206
  *to++= (uchar) length;
  if (max_length > 255)				// 2 byte length
    *to++= (uchar) (length >> 8);
5207
  memcpy(to, from, length);
5208 5209 5210 5211
  ptr=save;					// Restore org row pointer
  return to+length;
}

5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255

/*
  Unpack a blob key into a record buffer.

  SYNOPSIS
    Field_blob::unpack_key()
    to                          Pointer into the record buffer.
    from                        Pointer to the packed key.
    max_length                  Key length limit from key description.

  DESCRIPTION
    A blob key has a maximum size of 64K-1.
    In its packed form, the length field is one or two bytes long,
    depending on 'max_length'.
    Depending on the maximum length of a blob, its length field is
    put into 1 to 4 bytes. This is a property of the blob object,
    described by 'packlength'.
    Blobs are internally stored apart from the record buffer, which
    contains a pointer to the blob buffer.

  RETURN
    Pointer into 'from' past the last byte copied from packed key.
*/

const char *Field_blob::unpack_key(char *to, const char *from, uint max_length)
{
  /* get length of the blob key */
  uint32 length= *((uchar*) from++);
  if (max_length > 255)
    length+= (*((uchar*) from++)) << 8;

  /* put the length into the record buffer */
  put_length(to, length);

  /* put the address of the blob buffer or NULL */
  if (length)
    memcpy_fixed(to + packlength, &from, sizeof(from));
  else
    bzero(to + packlength, sizeof(from));

  /* point to first byte of next field in 'from' */
  return from + length;
}

5256 5257 5258 5259 5260 5261
/* 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);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5262 5263
  if (length > max_length)
    length=max_length;
5264
  *to++= (char) (length & 255);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5265
  if (max_length > 255)
5266
    *to++= (char) (length >> 8);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5267
  if (length)
5268
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5269 5270 5271
  return to+length;
}

5272
uint Field_blob::packed_col_length(const char *data_ptr, uint length)
5273 5274
{
  if (length > 255)
5275
    return uint2korr(data_ptr)+2;
5276
  else
5277
    return (uint) ((uchar) *data_ptr)+1;
5278 5279 5280 5281 5282 5283
}

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

5285

hf@deer.(none)'s avatar
hf@deer.(none) committed
5286
#ifdef HAVE_SPATIAL
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
5287 5288 5289 5290 5291

void Field_geom::get_key_image(char *buff, uint length, CHARSET_INFO *cs,
			       imagetype type)
{
  char *blob;
5292
  const char *dummy;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
5293
  MBR mbr;
5294 5295 5296
  ulong blob_length= get_length(ptr);
  Geometry_buffer buffer;
  Geometry *gobj;
5297 5298 5299 5300 5301 5302 5303

  if (blob_length < SRID_SIZE)
  {
    bzero(buff, SIZEOF_STORED_DOUBLE*4);
    return;
  }
  get_ptr(&blob);
5304 5305 5306
  gobj= Geometry::create_from_wkb(&buffer,
				  blob + SRID_SIZE, blob_length - SRID_SIZE);
  if (gobj->get_mbr(&mbr, &dummy))
5307 5308 5309 5310 5311 5312 5313 5314
    bzero(buff, SIZEOF_STORED_DOUBLE*4);
  else
  {
    float8store(buff, mbr.xmin);
    float8store(buff + 8, mbr.xmax);
    float8store(buff + 16, mbr.ymin);
    float8store(buff + 24, mbr.ymax);
  }
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
5315 5316 5317 5318 5319 5320 5321 5322 5323 5324
}


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

void Field_geom::sql_type(String &res) const
{
5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351
  CHARSET_INFO *cs= &my_charset_latin1;
  switch (geom_type)
  {
    case GEOM_POINT:
     res.set("point", 5, cs);
     break;
    case GEOM_LINESTRING:
     res.set("linestring", 10, cs);
     break;
    case GEOM_POLYGON:
     res.set("polygon", 7, cs);
     break;
    case GEOM_MULTIPOINT:
     res.set("multipoint", 10, cs);
     break;
    case GEOM_MULTILINESTRING:
     res.set("multilinestring", 15, cs);
     break;
    case GEOM_MULTIPOLYGON:
     res.set("multipolygon", 12, cs);
     break;
    case GEOM_GEOMETRYCOLLECTION:
     res.set("geometrycollection", 18, cs);
     break;
    default:
     res.set("geometry", 8, cs);
  }
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
5352 5353 5354 5355 5356 5357 5358 5359 5360
}


int Field_geom::store(const char *from, uint length, CHARSET_INFO *cs)
{
  if (!length)
    bzero(ptr, Field_blob::pack_length());
  else
  {
5361 5362 5363 5364 5365
    // Check given WKB
    uint32 wkb_type;
    if (length < SRID_SIZE + WKB_HEADER_SIZE + SIZEOF_STORED_DOUBLE*2)
      goto err;
    wkb_type= uint4korr(from + WKB_HEADER_SIZE);
5366
    if (wkb_type < (uint32) Geometry::wkb_point ||
5367
	wkb_type > (uint32) Geometry::wkb_end)
5368
      return -1;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
5369 5370 5371 5372 5373 5374 5375 5376 5377
    Field_blob::store_length(length);
    if (table->copy_blobs || length <= MAX_FIELD_WIDTH)
    {						// Must make a copy
      value.copy(from, length, cs);
      from= value.ptr();
    }
    bmove(ptr + packlength, (char*) &from, sizeof(char*));
  }
  return 0;
5378 5379 5380

err:
  bzero(ptr, Field_blob::pack_length());  
5381
  return -1;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
5382 5383
}

hf@deer.(none)'s avatar
hf@deer.(none) committed
5384
#endif /*HAVE_SPATIAL*/
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
5385

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445
/****************************************************************************
** 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;
  }
}


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

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5446
int Field_enum::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5447
{
5448
  int err= 0;
5449
  uint32 not_used;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5450 5451
  char buff[80];
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
5452

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5453
  /* Convert character set if nesessary */
5454
  if (String::needs_conversion(length, cs, field_charset, &not_used))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5455 5456 5457 5458 5459
  { 
    tmpstr.copy(from, length, cs, field_charset);
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
5460 5461 5462 5463 5464

  /* Remove end space */
  while (length > 0 && my_isspace(system_charset_info,from[length-1]))
    length--;
  uint tmp=find_type(typelib, from, length, 0);
5465
  if (!tmp)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5466
  {
5467
    if (length < 6) // Can't be more than 99999 enums
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5468
    {
5469
      /* This is for reading numbers with LOAD DATA INFILE */
5470
      char *end;
5471 5472
      tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
      if (err || end != from+length || tmp > typelib->count)
5473 5474
      {
	tmp=0;
5475
	set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
5476
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5477 5478
    }
    else
5479
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5480
  }
5481
  store_type((ulonglong) tmp);
5482
  return err;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5483 5484 5485
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5486
int Field_enum::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5487
{
5488
  return Field_enum::store((longlong) nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5489 5490 5491
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5492
int Field_enum::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5493
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5494
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5495 5496
  if ((uint) nr > typelib->count || nr == 0)
  {
5497
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5498
    nr=0;
5499
    error=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5500 5501
  }
  store_type((ulonglong) (uint) nr);
5502
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561
}


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)
monty@mysql.com's avatar
monty@mysql.com committed
5562
    val_ptr->set("", 0, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5563 5564
  else
    val_ptr->set((const char*) typelib->type_names[tmp-1],
5565
		 (uint) strlen(typelib->type_names[tmp-1]),
5566
		 field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594
  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
{
5595 5596 5597
  char buffer[255];
  String enum_item(buffer, sizeof(buffer), res.charset());

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5598 5599 5600 5601
  res.length(0);
  res.append("enum(");

  bool flag=0;
5602
  for (const char **pos= typelib->type_names; *pos; pos++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5603 5604 5605
  {
    if (flag)
      res.append(',');
5606 5607 5608 5609
    /* convert to res.charset() == utf8, then quote */
    enum_item.copy(*pos, strlen(*pos), charset(), res.charset());
    append_unescaped(&res, enum_item.ptr(), enum_item.length());
    flag= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5610 5611 5612 5613 5614
  }
  res.append(')');
}


5615 5616 5617 5618 5619 5620 5621 5622
/*
   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.
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5623 5624


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5625
int Field_set::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5626
{
monty@mysql.com's avatar
monty@mysql.com committed
5627
  bool got_warning= 0;
5628
  int err= 0;
5629 5630
  char *not_used;
  uint not_used2;
5631
  uint32 not_used_offset;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5632 5633
  char buff[80];
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5634

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5635
  /* Convert character set if nesessary */
5636
  if (String::needs_conversion(length, cs, field_charset, &not_used_offset))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5637 5638 5639 5640 5641
  { 
    tmpstr.copy(from, length, cs, field_charset);
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5642
  ulonglong tmp= find_set(typelib, from, length, &not_used, &not_used2,
monty@mysql.com's avatar
monty@mysql.com committed
5643
			  &got_warning);
5644 5645 5646
  if (!tmp && length && length < 22)
  {
    /* This is for reading numbers with LOAD DATA INFILE */
5647
    char *end;
5648 5649
    tmp=my_strntoull(cs,from,length,10,&end,&err);
    if (err || end != from+length ||
5650
	tmp > (ulonglong) (((longlong) 1 << typelib->count) - (longlong) 1))
5651
    {
5652
      tmp=0;      
5653
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
5654
    }
5655
  }
monty@mysql.com's avatar
monty@mysql.com committed
5656
  else if (got_warning)
5657
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
5658
  store_type(tmp);
5659
  return err;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5660 5661 5662
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5663
int Field_set::store(longlong nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5664
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5665
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5666 5667 5668
  if ((ulonglong) nr > (ulonglong) (((longlong) 1 << typelib->count) -
				    (longlong) 1))
  {
5669
    nr&= (longlong) (((longlong) 1 << typelib->count) - (longlong) 1);    
5670
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
5671
    error=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5672 5673
  }
  store_type((ulonglong) nr);
5674
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691
}


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],
5692
		 (uint) strlen(typelib->type_names[bitnr]),
5693
		 field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704
      val_buffer->append(str);
    }
    tmp>>=1;
    bitnr++;
  }
  return val_buffer;
}


void Field_set::sql_type(String &res) const
{
5705 5706 5707
  char buffer[255];
  String set_item(buffer, sizeof(buffer), res.charset());

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5708 5709 5710 5711
  res.length(0);
  res.append("set(");

  bool flag=0;
5712
  for (const char **pos= typelib->type_names; *pos; pos++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5713 5714 5715
  {
    if (flag)
      res.append(',');
5716 5717 5718 5719
    /* convert to res.charset() == utf8, then quote */
    set_item.copy(*pos, strlen(*pos), charset(), res.charset());
    append_unescaped(&res, set_item.ptr(), set_item.length());
    flag= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5720 5721 5722 5723 5724 5725 5726 5727
  }
  res.append(')');
}

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

bool Field::eq_def(Field *field)
{
5728
  if (real_type() != field->real_type() || charset() != field->charset() ||
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5729 5730 5731 5732 5733 5734 5735 5736 5737 5738
      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;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5739

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5740 5741 5742
  if (typelib->count < from_lib->count)
    return 0;
  for (uint i=0 ; i < from_lib->count ; i++)
5743 5744 5745 5746 5747
    if (my_strnncoll(field_charset,
                     (const uchar*)typelib->type_names[i],
                     strlen(typelib->type_names[i]),
                     (const uchar*)from_lib->type_names[i],
                     strlen(from_lib->type_names[i])))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5748 5749 5750 5751 5752
      return 0;
  return 1;
}

bool Field_num::eq_def(Field *field)
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5753
{
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769
  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
*****************************************************************************/

5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783
void create_field::create_length_to_internal_length(void)
{
  switch (sql_type)
  {
    case MYSQL_TYPE_TINY_BLOB:
    case MYSQL_TYPE_MEDIUM_BLOB:
    case MYSQL_TYPE_LONG_BLOB:
    case MYSQL_TYPE_BLOB:
    case MYSQL_TYPE_VAR_STRING:
    case MYSQL_TYPE_STRING:
      length*= charset->mbmaxlen;
      pack_length= calc_pack_length(sql_type == FIELD_TYPE_VAR_STRING ?
				    FIELD_TYPE_STRING : sql_type, length);
      break;
bar@mysql.com's avatar
bar@mysql.com committed
5784 5785 5786 5787
    case MYSQL_TYPE_ENUM:
    case MYSQL_TYPE_SET:
      length*= charset->mbmaxlen;
      break;
5788 5789 5790 5791 5792 5793
    default:
      /* do nothing */
      break;
  }
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5794
/*
5795
  Make a field from the .frm file info
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5796 5797 5798 5799 5800 5801 5802
*/

uint32 calc_pack_length(enum_field_types type,uint32 length)
{
  switch (type) {
  case FIELD_TYPE_STRING:
  case FIELD_TYPE_DECIMAL: return (length);
5803
  case FIELD_TYPE_VAR_STRING: return (length+HA_KEY_BLOB_LENGTH);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818
  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;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5819
  case FIELD_TYPE_BLOB:		return 2+portable_sizeof_char_ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5820 5821
  case FIELD_TYPE_MEDIUM_BLOB:	return 3+portable_sizeof_char_ptr;
  case FIELD_TYPE_LONG_BLOB:	return 4+portable_sizeof_char_ptr;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
5822
  case FIELD_TYPE_GEOMETRY:	return 4+portable_sizeof_char_ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5823 5824
  case FIELD_TYPE_SET:
  case FIELD_TYPE_ENUM: abort(); return 0;	// This shouldn't happen
5825
  default: return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5826
  }
5827
  return 0;					// Keep compiler happy
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844
}


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,
5845
		  uchar *null_pos, uchar null_bit,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5846
		  uint pack_flag,
5847
		  enum_field_types field_type,
5848
		  CHARSET_INFO *field_charset,
5849
		  Field::geometry_type geom_type,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5850 5851 5852 5853 5854 5855 5856 5857 5858 5859
		  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;
  }
5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871

  switch (field_type)
  {
    case FIELD_TYPE_DATE:
    case FIELD_TYPE_NEWDATE:
    case FIELD_TYPE_TIME:
    case FIELD_TYPE_DATETIME:
    case FIELD_TYPE_TIMESTAMP:
      field_charset= &my_charset_bin;
    default: break;
  }

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5872 5873 5874 5875
  if (f_is_alpha(pack_flag))
  {
    if (!f_is_packed(pack_flag))
      return new Field_string(ptr,field_length,null_pos,null_bit,
5876
			      unireg_check, field_name, table, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5877 5878 5879 5880 5881

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

hf@deer.(none)'s avatar
hf@deer.(none) committed
5882
#ifdef HAVE_SPATIAL
5883 5884
    if (f_is_geom(pack_flag))
      return new Field_geom(ptr,null_pos,null_bit,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5885
			    unireg_check, field_name, table,
5886
			    pack_length, geom_type);
hf@deer.(none)'s avatar
hf@deer.(none) committed
5887
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5888 5889 5890
    if (f_is_blob(pack_flag))
      return new Field_blob(ptr,null_pos,null_bit,
			    unireg_check, field_name, table,
hf@deer.mysql.r18.ru's avatar
hf@deer.mysql.r18.ru committed
5891
			    pack_length, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5892 5893 5894 5895 5896
    if (interval)
    {
      if (f_is_enum(pack_flag))
	return new Field_enum(ptr,field_length,null_pos,null_bit,
				  unireg_check, field_name, table,
5897
				  pack_length, interval, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5898 5899 5900
      else
	return new Field_set(ptr,field_length,null_pos,null_bit,
			     unireg_check, field_name, table,
5901
			     pack_length, interval, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5902 5903 5904
    }
  }

5905
  switch (field_type) {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950
  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,
5951
			       unireg_check, field_name, table, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5952 5953 5954 5955 5956
  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,
5957
			  unireg_check, field_name, table, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5958 5959
  case FIELD_TYPE_NEWDATE:
    return new Field_newdate(ptr,null_pos,null_bit,
5960
			     unireg_check, field_name, table, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5961 5962
  case FIELD_TYPE_TIME:
    return new Field_time(ptr,null_pos,null_bit,
5963
			  unireg_check, field_name, table, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5964 5965
  case FIELD_TYPE_DATETIME:
    return new Field_datetime(ptr,null_pos,null_bit,
5966
			      unireg_check, field_name, table, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5967
  case FIELD_TYPE_NULL:
5968
    return new Field_null(ptr,field_length,unireg_check,field_name,table, field_charset);
5969 5970
  default:					// Impossible (Wrong version)
    break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5971
  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
5972
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5973 5974 5975 5976 5977
}


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

5978
create_field::create_field(Field *old_field,Field *orig_field)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5979 5980 5981 5982 5983 5984 5985 5986
{
  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();
5987 5988
  charset=    old_field->charset();		// May be NULL ptr
  comment=    old_field->comment;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5989 5990 5991 5992 5993

  /* 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);
5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004

  switch (sql_type)
  {
    case FIELD_TYPE_BLOB:
      switch (pack_length - portable_sizeof_char_ptr)
      {
        case  1: sql_type= FIELD_TYPE_TINY_BLOB; break;
        case  2: sql_type= FIELD_TYPE_BLOB; break;
        case  3: sql_type= FIELD_TYPE_MEDIUM_BLOB; break;
        default: sql_type= FIELD_TYPE_LONG_BLOB; break;
      }
6005
      length=(length+charset->mbmaxlen-1)/charset->mbmaxlen; // QQ: Probably not needed
6006 6007 6008
      break;
    case FIELD_TYPE_STRING:
    case FIELD_TYPE_VAR_STRING:
6009
      length=(length+charset->mbmaxlen-1)/charset->mbmaxlen;
6010 6011 6012 6013
      break;
    default:
      break;
  }
6014

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6015 6016 6017
  decimals= old_field->decimals();
  if (sql_type == FIELD_TYPE_STRING)
  {
6018
    /* Change CHAR -> VARCHAR if dynamic record length */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6019 6020 6021 6022 6023 6024 6025
    sql_type=old_field->type();
    decimals=0;
  }
  if (flags & (ENUM_FLAG | SET_FLAG))
    interval= ((Field_enum*) old_field)->typelib;
  else
    interval=0;
6026
  def=0;
6027
  if (!old_field->is_real_null() && ! (flags & BLOB_FLAG) &&
6028
      old_field->ptr && orig_field)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6029 6030
  {
    char buff[MAX_FIELD_WIDTH],*pos;
6031
    String tmp(buff,sizeof(buff), charset);
6032

6033
    /* Get the value from default_values */
6034
    my_ptrdiff_t diff= (my_ptrdiff_t) (orig_field->table->rec_buff_length*2);
6035
    orig_field->move_field(diff);		// Points now at default_values
6036
    bool is_null=orig_field->is_real_null();
6037
    orig_field->val_str(&tmp);
6038
    orig_field->move_field(-diff);		// Back to record[0]
6039
    if (!is_null)
6040 6041 6042
    {
      pos= (char*) sql_memdup(tmp.ptr(),tmp.length()+1);
      pos[tmp.length()]=0;
6043
      def= new Item_string(pos, tmp.length(), charset);
6044
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6045
  }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6046
#ifdef HAVE_SPATIAL
6047 6048 6049 6050
  if (sql_type == FIELD_TYPE_GEOMETRY)
  {
    geom_type= ((Field_geom*)old_field)->geom_type;
  }
hf@deer.(none)'s avatar
hf@deer.(none) committed
6051
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6052
}
6053 6054 6055


/* Warning handling */
6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075

/*
  Produce warning or note about data saved into field

  SYNOPSYS
    set_warning()
      level            - level of message (Note/Warning/Error)
      code             - error code of message to be produced
      cuted_increment  - whenever we should increase cut fields count or not

  NOTE
    This function won't produce warning and increase cut fields counter
    if count_cuted_fields == FIELD_CHECK_IGNORE for current thread.

  RETURN VALUE
    true  - if count_cuted_fields == FIELD_CHECK_IGNORE
    false - otherwise
*/
bool 
Field::set_warning(const uint level, const uint code, int cuted_increment)
6076
{
6077
  THD *thd= table->in_use;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6078 6079
  if (thd->count_cuted_fields)
  {
6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171
    thd->cuted_fields+= cuted_increment;
    push_warning_printf(thd, (MYSQL_ERROR::enum_warning_level) level,
                        code, ER(code), field_name, thd->row_count);
    return 0;
  }
  return 1;
}


/*
  Produce warning or note about datetime string data saved into field

  SYNOPSYS
    set_warning()
      level            - level of message (Note/Warning/Error)
      code             - error code of message to be produced
      str              - string value which we tried to save
      str_len          - length of string which we tried to save
      ts_type          - type of datetime value (datetime/date/time)
      cuted_increment  - whenever we should increase cut fields count or not
  
  NOTE
    This function will always produce some warning but won't increase cut 
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current 
    thread.
*/
void 
Field::set_datetime_warning(const uint level, const uint code, 
                            const char *str, uint str_length, 
                            timestamp_type ts_type, int cuted_increment)
{
  if (set_warning(level, code, cuted_increment))
    make_truncated_value_warning(table->in_use, str, str_length, ts_type);
}


/*
  Produce warning or note about integer datetime value saved into field

  SYNOPSYS
    set_warning()
      level            - level of message (Note/Warning/Error)
      code             - error code of message to be produced
      nr               - numeric value which we tried to save
      ts_type          - type of datetime value (datetime/date/time)
      cuted_increment  - whenever we should increase cut fields count or not
  
  NOTE
    This function will always produce some warning but won't increase cut 
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current 
    thread.
*/
void 
Field::set_datetime_warning(const uint level, const uint code, 
                            longlong nr, timestamp_type ts_type,
                            int cuted_increment)
{
  if (set_warning(level, code, cuted_increment))
  {
    char str_nr[22];
    char *str_end= longlong10_to_str(nr, str_nr, -10);
    make_truncated_value_warning(table->in_use, str_nr, str_end - str_nr, 
                                 ts_type);
  }
}


/*
  Produce warning or note about double datetime data saved into field

  SYNOPSYS
    set_warning()
      level            - level of message (Note/Warning/Error)
      code             - error code of message to be produced
      nr               - double value which we tried to save
      ts_type          - type of datetime value (datetime/date/time)
  
  NOTE
    This function will always produce some warning but won't increase cut 
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current 
    thread.
*/
void 
Field::set_datetime_warning(const uint level, const uint code, 
                            double nr, timestamp_type ts_type)
{
  if (set_warning(level, code, 1))
  {
    /* DBL_DIG is enough to print '-[digits].E+###' */
    char str_nr[DBL_DIG + 8];
    uint str_len= my_sprintf(str_nr, (str_nr, "%g", nr));
    make_truncated_value_warning(table->in_use, str_nr, str_len, ts_type);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
6172
  }
6173
}
6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194

/*
  maximum possible display length for blob

  SYNOPSIS
    Field_blob::max_length()

  RETURN
    length
*/
uint32 Field_blob::max_length()
{
  switch (packlength)
  {
  case 1:
    return 255;
  case 2:
    return 65535;
  case 3:
    return 16777215;
  case 4:
6195
    return (uint32) 4294967295U;
6196 6197 6198 6199 6200
  default:
    DBUG_ASSERT(0); // we should never go here
    return 0;
  }
}