field.cc 250 KB
Newer Older
1
/* Copyright (C) 2000-2006 MySQL 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
   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
5
   the Free Software Foundation; version 2 of the License.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
6

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7 8 9 10
   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
11

bk@work.mysql.com's avatar
bk@work.mysql.com committed
12 13 14 15 16 17 18 19 20
   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
*****************************************************************************/

21
#ifdef USE_PRAGMA_IMPLEMENTATION
bk@work.mysql.com's avatar
bk@work.mysql.com committed
22 23 24 25 26 27 28 29 30 31 32
#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
33 34 35
// Maximum allowed exponent value for converting string to decimal
#define MAX_EXPONENT 1024

bk@work.mysql.com's avatar
bk@work.mysql.com committed
36
/*****************************************************************************
37
  Instansiate templates and static variables
bk@work.mysql.com's avatar
bk@work.mysql.com committed
38 39
*****************************************************************************/

40
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
bk@work.mysql.com's avatar
bk@work.mysql.com committed
41 42 43 44 45 46 47
template class List<create_field>;
template class List_iterator<create_field>;
#endif

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

48
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE 320
49 50
#define LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE 128
#define DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE 128
51 52
#define BLOB_PACK_LENGTH_TO_MAX_LENGH(arg) \
((ulong) ((LL(1) << min(arg, 4) * 8) - LL(1)))
53

54 55 56 57 58 59 60
/*
  Rules for merging different types of fields in UNION

  NOTE: to avoid 256*256 table, gap in table types numeration is skiped
  following #defines describe that gap and how to canculate number of fields
  and index of field in thia array.
*/
61 62 63
#define FIELDTYPE_TEAR_FROM (MYSQL_TYPE_BIT + 1)
#define FIELDTYPE_TEAR_TO   (MYSQL_TYPE_NEWDECIMAL - 1)
#define FIELDTYPE_NUM (FIELDTYPE_TEAR_FROM + (255 - FIELDTYPE_TEAR_TO))
64 65 66 67 68 69 70 71 72 73 74 75
inline int field_type2index (enum_field_types field_type)
{
  return (field_type < FIELDTYPE_TEAR_FROM ?
          field_type :
          ((int)FIELDTYPE_TEAR_FROM) + (field_type - FIELDTYPE_TEAR_TO) - 1);
}

static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]=
{
  /* MYSQL_TYPE_DECIMAL -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
76
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
77
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
78
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
79 80 81
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
82
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
83 84 85
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_DECIMAL,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
86
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
87
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
88 89 90 91 92 93 94
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
95
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
96
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
97 98 99
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
100
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
101
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
102
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
103 104 105 106
  },
  /* MYSQL_TYPE_TINY -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
107
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_TINY,
108 109 110 111 112
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
113
    MYSQL_TYPE_TINY,        MYSQL_TYPE_VARCHAR,
114 115 116
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
117
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
118
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
119 120 121 122 123 124 125
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
126
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
127
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
128 129 130
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
131
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
132
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
133
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
134 135 136 137
  },
  /* MYSQL_TYPE_SHORT -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
138
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_SHORT,
139 140 141 142 143
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
144
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_VARCHAR,
145 146 147
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
148
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
149
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
150 151 152 153 154 155 156
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_SHORT,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
157
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
158
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
159 160 161
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
162
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
163
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
164
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
165 166 167 168
  },
  /* MYSQL_TYPE_LONG -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
169
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_LONG,
170 171 172 173 174
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_LONG,        MYSQL_TYPE_LONG,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
175
    MYSQL_TYPE_LONG,         MYSQL_TYPE_VARCHAR,
176
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
177
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
178
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
179
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
180
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
181 182 183 184 185 186 187
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_LONG,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
188
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
189
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
190 191 192
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
193
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
194
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
195
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
196 197 198 199 200 201 202 203 204 205
  },
  /* MYSQL_TYPE_FLOAT -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_FLOAT,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
206
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_VARCHAR,
207
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
208
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_INT24,
209
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
210
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
211
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
212 213 214 215 216 217 218
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_FLOAT,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
219
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
220
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
221 222 223
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
224
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
225
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
226
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
227 228 229 230 231 232 233 234 235 236
  },
  /* MYSQL_TYPE_DOUBLE -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
237
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
238
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
239
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_INT24,
240
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
241
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
242
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
243 244 245 246 247 248 249
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
250
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
251
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
252 253 254
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
255
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
256
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
257
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
258 259 260 261
  },
  /* MYSQL_TYPE_NULL -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
262
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_TINY,
263 264 265 266 267 268 269 270 271 272 273 274
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_NULL,        MYSQL_TYPE_TIMESTAMP,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_TIME,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_YEAR,
275 276 277 278 279 280
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_BIT,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_ENUM,
281 282 283 284 285
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_SET,         MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
286
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
287 288 289 290 291 292
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_GEOMETRY
  },
  /* MYSQL_TYPE_TIMESTAMP -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
293
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
294
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
295
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
296
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
297
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
298 299 300
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_TIMESTAMP,   MYSQL_TYPE_TIMESTAMP,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
301
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
302 303 304
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
305 306 307 308 309 310 311
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
312
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
313
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
314 315 316
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
317
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
318
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
319
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
320 321 322 323
  },
  /* MYSQL_TYPE_LONGLONG -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
324
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_LONGLONG,
325 326 327 328 329
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_LONGLONG,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
330
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_VARCHAR,
331 332 333
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_LONG,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
334
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
335
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
336 337 338 339 340 341 342
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_LONGLONG,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
343
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
344
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
345 346 347
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
348
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
349
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
350
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
351 352 353 354
  },
  /* MYSQL_TYPE_INT24 -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
355
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_INT24,
356 357 358 359 360
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_INT24,       MYSQL_TYPE_LONG,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
361
    MYSQL_TYPE_INT24,       MYSQL_TYPE_VARCHAR,
362 363 364
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
365
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
366
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
367 368 369 370 371 372 373
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_INT24,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL    MYSQL_TYPE_ENUM
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
374
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
375
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
376 377 378
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
379
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
380
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
381
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
382 383 384 385
  },
  /* MYSQL_TYPE_DATE -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
386
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
387
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
388
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
389
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
390
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
391 392 393
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
394
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
395 396 397
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
398 399 400 401 402 403 404
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
405
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
406
    MYSQL_TYPE_VARCHAR,  MYSQL_TYPE_TINY_BLOB,
407 408 409
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
410
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
411
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
412
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
413 414 415 416
  },
  /* MYSQL_TYPE_TIME -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
417
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
418
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
419
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
420
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
421
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
422 423 424
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_TIME,        MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
425
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
426 427 428
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_TIME,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
429 430 431 432 433 434 435
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
436
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
437
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
438 439 440
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
441
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
442
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
443
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
444 445 446 447
  },
  /* MYSQL_TYPE_DATETIME -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
448
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
449
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
450
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
451
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
452
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
453 454 455
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
456
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
457 458 459
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
460 461 462 463 464 465 466
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
467
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
468
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
469 470 471
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
472
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
473
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
474
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
475 476 477 478 479 480 481 482 483 484
  },
  /* MYSQL_TYPE_YEAR -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_TINY,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
485
    MYSQL_TYPE_YEAR,        MYSQL_TYPE_VARCHAR,
486 487 488
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
489
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
490
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
491 492 493 494 495 496 497
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_YEAR,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
498
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
499
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
500 501 502
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
503
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
504
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
505
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
506 507 508 509
  },
  /* MYSQL_TYPE_NEWDATE -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
510
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
511
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
512
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
513
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
514
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
515 516 517
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
518
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
519 520 521
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
  },
  /* MYSQL_TYPE_VARCHAR -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR
  },
  /* MYSQL_TYPE_BIT -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_BIT,         MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_BIT,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
  },
  /* MYSQL_TYPE_NEWDECIMAL -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_NEWDECIMAL,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
622
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
623
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
624 625 626
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
627
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
628
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
629
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
630 631 632 633
  },
  /* MYSQL_TYPE_ENUM -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
634
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
635
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
636
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
637
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
638
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
639
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
640
    MYSQL_TYPE_ENUM,        MYSQL_TYPE_VARCHAR,
641
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
642
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
643
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
644
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
645
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
646 647 648 649 650 651 652
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
653
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
654
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
655 656 657
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
658
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
659
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
660
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
661 662 663 664
  },
  /* MYSQL_TYPE_SET -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
665
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
666
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
667
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
668
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
669
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
670
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
671
    MYSQL_TYPE_SET,         MYSQL_TYPE_VARCHAR,
672
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
673
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
674
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
675
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
676
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
677 678 679 680 681 682 683
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
684
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
685
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
686 687 688
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
689
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
690
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
691
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
  },
  /* MYSQL_TYPE_TINY_BLOB -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
709 710 711
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_BIT          <16>-<245>
712
    MYSQL_TYPE_TINY_BLOB,
713 714
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB
  },
  /* MYSQL_TYPE_MEDIUM_BLOB -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
740 741 742
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_BIT          <16>-<245>
743
    MYSQL_TYPE_MEDIUM_BLOB,
744 745
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB
  },
  /* MYSQL_TYPE_LONG_BLOB -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
771 772 773
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BIT          <16>-<245>
774
    MYSQL_TYPE_LONG_BLOB,
775 776
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB
  },
  /* MYSQL_TYPE_BLOB -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
802 803 804
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_BIT          <16>-<245>
805
    MYSQL_TYPE_BLOB,
806 807
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
808 809 810 811 812 813 814 815 816 817 818 819
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB
  },
  /* MYSQL_TYPE_VAR_STRING -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
820
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
821
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
822
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
823
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
824
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
825
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
826
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
827
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
828
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
829
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
830
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
831
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
832 833 834 835 836 837 838
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
839
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
840
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
841 842 843
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
844
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
845
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
846
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
  },
  /* MYSQL_TYPE_STRING -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
864 865 866
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
867
    MYSQL_TYPE_STRING,
868 869
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
870 871 872 873 874
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
    MYSQL_TYPE_STRING,      MYSQL_TYPE_TINY_BLOB,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
875
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
876 877 878 879 880 881
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING
  },
  /* MYSQL_TYPE_GEOMETRY -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
882
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
883
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
884
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
885
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
886
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
887
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
888
    MYSQL_TYPE_GEOMETRY,    MYSQL_TYPE_VARCHAR,
889
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
890
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
891
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
892
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
893
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
894 895 896 897 898 899 900
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
    MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
901
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
902
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
903 904 905
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
906
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
    MYSQL_TYPE_STRING,      MYSQL_TYPE_GEOMETRY
  }
};

/*
  Return type of which can carry value of both given types in UNION result

  SYNOPSIS
    Field::field_type_merge()
    a, b  types for merging

  RETURN
    type of field
*/

enum_field_types Field::field_type_merge(enum_field_types a,
                                         enum_field_types b)
{
  DBUG_ASSERT(a < FIELDTYPE_TEAR_FROM || a > FIELDTYPE_TEAR_TO);
  DBUG_ASSERT(b < FIELDTYPE_TEAR_FROM || b > FIELDTYPE_TEAR_TO);
  return field_types_merge_rules[field_type2index(a)]
                                [field_type2index(b)];
}


static Item_result field_types_result_type [FIELDTYPE_NUM]=
{
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
936
  DECIMAL_RESULT,           INT_RESULT,
937 938 939 940 941 942 943 944 945 946 947 948
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
  INT_RESULT,               INT_RESULT,
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
  REAL_RESULT,              REAL_RESULT,
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
  STRING_RESULT,            STRING_RESULT,
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
  INT_RESULT,               INT_RESULT,
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
  STRING_RESULT,            STRING_RESULT,
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
  STRING_RESULT,            INT_RESULT,
949 950 951
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
  STRING_RESULT,            STRING_RESULT,
  //MYSQL_TYPE_BIT          <16>-<245>
952
  STRING_RESULT,
953 954
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
  DECIMAL_RESULT,           STRING_RESULT,
955 956 957 958 959 960 961 962 963 964 965
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
  STRING_RESULT,            STRING_RESULT,
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
  STRING_RESULT,            STRING_RESULT,
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
  STRING_RESULT,            STRING_RESULT,
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
  STRING_RESULT,            STRING_RESULT
};


966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
/*
  Test if the given string contains important data:
  not spaces for character string,
  or any data for binary string.

  SYNOPSIS
    test_if_important_data()
    cs          Character set
    str         String to test
    strend      String end

  RETURN
    FALSE - If string does not have important data
    TRUE  - If string has some important data
*/

static bool
test_if_important_data(CHARSET_INFO *cs, const char *str, const char *strend)
{
  if (cs != &my_charset_bin)
    str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
  return (str < strend);
}


991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
/*
  Detect Item_result by given field type of UNION merge result

  SYNOPSIS
    Field::result_merge_type()
    field_type  given field type

  RETURN
    Item_result (type of internal MySQL expression result)
*/

Item_result Field::result_merge_type(enum_field_types field_type)
{
  DBUG_ASSERT(field_type < FIELDTYPE_TEAR_FROM || field_type
              > FIELDTYPE_TEAR_TO);
  return field_types_result_type[field_type2index(field_type)];
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1009
/*****************************************************************************
1010
  Static help functions
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1011 1012
*****************************************************************************/

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038

/*
  Check whether a field type can be partially indexed by a key

  This is a static method, rather than a virtual function, because we need
  to check the type of a non-Field in mysql_alter_table().

  SYNOPSIS
   type_can_have_key_part()
   type                 field type

  RETURN
    TRUE  Type can have a prefixed key
    FALSE Type can not have a prefixed key
*/

bool Field::type_can_have_key_part(enum enum_field_types type)
{
  switch (type) {
  case MYSQL_TYPE_VARCHAR:
  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:
gkodinov/kgeorge@magare.gmz's avatar
gkodinov/kgeorge@magare.gmz committed
1039
  case MYSQL_TYPE_GEOMETRY:
1040 1041 1042 1043 1044 1045 1046
    return TRUE;
  default:
    return FALSE;
  }
}


1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
/*
  Numeric fields base class constructor
*/
Field_num::Field_num(char *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
                     uchar null_bit_arg, utype unireg_check_arg,
                     const char *field_name_arg,
                     struct st_table *table_arg,
                     uint8 dec_arg, bool zero_arg, bool unsigned_arg)
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
         unireg_check_arg, field_name_arg, table_arg),
  dec(dec_arg),zerofill(zero_arg),unsigned_flag(unsigned_arg)
{
  if (zerofill)
    flags|=ZEROFILL_FLAG;
  if (unsigned_flag)
    flags|=UNSIGNED_FLAG;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
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
  }
}

/*
1080
  Test if given number is a int.
1081 1082

  SYNOPSIS
1083 1084
    Field_num::check_int
    cs          Character set
1085 1086
    str		String to test
    end		Pointer to char after last used digit
1087 1088
    length      String length
    error       Error returned by strntoull10rnd()
1089

1090 1091
  NOTE
    This is called after one has called strntoull10rnd() function.
1092 1093

  RETURN
1094 1095 1096
    0	ok
    1	error: empty string or wrong integer.
    2   error: garbage at the end of string.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1097 1098
*/

1099 1100
int Field_num::check_int(CHARSET_INFO *cs, const char *str, int length, 
                         const char *int_end, int error)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1101
{
1102 1103
  /* Test if we get an empty string or wrong integer */
  if (str == int_end || error == MY_ERRNO_EDOM)
1104 1105
  {
    char buff[128];
1106
    String tmp(buff, (uint32) sizeof(buff), system_charset_info);
1107 1108 1109 1110 1111 1112
    tmp.copy(str, length, system_charset_info);
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
                        "integer", tmp.c_ptr(), field_name,
                        (ulong) table->in_use->row_count);
1113
    return 1;
1114
  }
1115 1116 1117 1118 1119 1120 1121 1122
  /* Test if we have garbage at the end of the given string. */
  if (test_if_important_data(cs, int_end, str + length))
  {
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
    return 2;
  }
  return 0;
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1123

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157

/*
  Conver a string to an integer then check bounds.
  
  SYNOPSIS
    Field_num::get_int
    cs            Character set
    from          String to convert
    len           Length of the string
    rnd           OUT longlong value
    unsigned_max  max unsigned value
    signed_min    min signed value
    signed_max    max signed value

  DESCRIPTION
    The function calls strntoull10rnd() to get an integer value then
    check bounds and errors returned. In case of any error a warning
    is raised.

  RETURN
    0   ok
    1   error
*/

bool Field_num::get_int(CHARSET_INFO *cs, const char *from, uint len,
                        longlong *rnd, ulonglong unsigned_max, 
                        longlong signed_min, longlong signed_max)
{
  char *end;
  int error;
  
  *rnd= (longlong) cs->cset->strntoull10rnd(cs, from, len, unsigned_flag, &end,
                                            &error);
  if (unsigned_flag)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1158
  {
1159 1160 1161 1162 1163 1164

    if (((ulonglong) *rnd > unsigned_max) && (*rnd= (longlong) unsigned_max) ||
        error == MY_ERRNO_ERANGE)
    {
      goto out_of_range;
    }
1165
  }
1166
  else
1167
  {
1168
    if (*rnd < signed_min)
1169
    {
1170 1171 1172 1173 1174 1175 1176
      *rnd= signed_min;
      goto out_of_range;
    }
    else if (*rnd > signed_max)
    {
      *rnd= signed_max;
      goto out_of_range;
1177
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1178
  }
1179 1180
  if (table->in_use->count_cuted_fields && check_int(cs, from, len, end, error))
    return 1;
1181
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1182

1183 1184 1185 1186
out_of_range:
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
  return 1;
}
1187

1188 1189 1190 1191 1192
/*
  Process decimal library return codes and issue warnings for overflow and
  truncation.

  SYNOPSIS
1193
    Field::warn_if_overflow()
1194 1195 1196 1197 1198 1199 1200
    op_result  decimal library return code (E_DEC_* see include/decimal.h)

  RETURN
    1  there was overflow
    0  no error or some other errors except overflow
*/

1201
int Field::warn_if_overflow(int op_result)
1202 1203 1204 1205 1206 1207
{
  if (op_result == E_DEC_OVERFLOW)
  {
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
    return 1;
  }
1208 1209
  if (op_result == E_DEC_TRUNCATED)
  {
1210
    set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
1211 1212
    /* We return 0 here as this is not a critical issue */
  }
1213 1214 1215
  return 0;
}

1216

serg@serg.mylan's avatar
serg@serg.mylan committed
1217
#ifdef NOT_USED
1218
static bool test_if_real(const char *str,int length, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1219
{
1220
  cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
1221 1222

  while (length && my_isspace(cs,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1223 1224 1225 1226 1227 1228 1229 1230
  {						// Allow start space
    length--; str++;
  }
  if (!length)
    return 0;
  if (*str == '+' || *str == '-')
  {
    length--; str++;
1231
    if (!length || !(my_isdigit(cs,*str) || *str == '.'))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1232 1233
      return 0;
  }
1234
  while (length && my_isdigit(cs,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1235 1236 1237 1238 1239 1240 1241 1242
  {
    length--; str++;
  }
  if (!length)
    return 1;
  if (*str == '.')
  {
    length--; str++;
1243
    while (length && my_isdigit(cs,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1244 1245 1246 1247 1248 1249 1250 1251
    {
      length--; str++;
    }
  }
  if (!length)
    return 1;
  if (*str == 'E' || *str == 'e')
  {
1252
    if (length < 3 || (str[1] != '+' && str[1] != '-') || 
1253
        !my_isdigit(cs,str[2]))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1254 1255 1256
      return 0;
    length-=3;
    str+=3;
1257
    while (length && my_isdigit(cs,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1258 1259 1260 1261
    {
      length--; str++;
    }
  }
1262
  for (; length ; length--, str++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1263
  {						// Allow end space
1264
    if (!my_isspace(cs,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1265 1266 1267 1268
      return 0;
  }
  return 1;
}
serg@serg.mylan's avatar
serg@serg.mylan committed
1269
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1270

1271

monty@mysql.com's avatar
monty@mysql.com committed
1272 1273 1274 1275 1276 1277
/*
  Interpret field value as an integer but return the result as a string.

  This is used for printing bit_fields as numbers while debugging
*/

1278
String *Field::val_int_as_str(String *val_buffer, my_bool unsigned_val)
monty@mysql.com's avatar
monty@mysql.com committed
1279 1280
{
  CHARSET_INFO *cs= &my_charset_bin;
1281
  uint length;
monty@mysql.com's avatar
monty@mysql.com committed
1282
  longlong value= val_int();
1283
  if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
monty@mysql.com's avatar
monty@mysql.com committed
1284
    return 0;
monty@mysql.com's avatar
monty@mysql.com committed
1285
  length= (uint) (*cs->cset->longlong10_to_str)(cs, (char*) val_buffer->ptr(),
1286
                                                MY_INT64_NUM_DECIMAL_DIGITS,
1287
                                                unsigned_val ? 10 : -10,
monty@mysql.com's avatar
monty@mysql.com committed
1288
                                                value);
monty@mysql.com's avatar
monty@mysql.com committed
1289 1290 1291 1292 1293
  val_buffer->length(length);
  return val_buffer;
}


1294 1295
/* This is used as a table name when the table structure is not set up */
const char *unknown_table_name= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1296 1297

Field::Field(char *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
1298
	     uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1299 1300
	     utype unireg_check_arg, const char *field_name_arg,
	     struct st_table *table_arg)
1301
  :ptr(ptr_arg), null_ptr(null_ptr_arg),
1302
   table(table_arg),orig_table(table_arg),
1303
   table_name(table_arg ? &table_arg->alias : &unknown_table_name),
1304
   field_name(field_name_arg),
serg@serg.mylan's avatar
serg@serg.mylan committed
1305 1306
   query_id(0), key_start(0), part_of_key(0), part_of_sortkey(0),
   unireg_check(unireg_check_arg),
1307
   field_length(length_arg), null_bit(null_bit_arg) 
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1308 1309
{
  flags=null_ptr ? 0: NOT_NULL_FLAG;
1310 1311
  comment.str= (char*) "";
  comment.length=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
}

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)
  {
1325 1326
    *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
1327 1328 1329 1330
  }
}


1331
bool Field::send_binary(Protocol *protocol)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1332 1333
{
  char buff[MAX_FIELD_WIDTH];
1334
  String tmp(buff,sizeof(buff),charset());
1335
  val_str(&tmp);
1336
  return protocol->store(tmp.ptr(), tmp.length(), tmp.charset());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1337 1338 1339
}


1340 1341 1342 1343 1344 1345 1346 1347
my_decimal *Field::val_decimal(my_decimal *decimal)
{
  /* This never have to be called */
  DBUG_ASSERT(0);
  return 0;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
1348 1349
void Field_num::add_zerofill_and_unsigned(String &res) const
{
1350
  if (unsigned_flag)
1351
    res.append(STRING_WITH_LEN(" unsigned"));
1352
  if (zerofill)
1353
    res.append(STRING_WITH_LEN(" zerofill"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1354 1355
}

1356

1357
void Field::make_field(Send_field *field)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1358
{
1359 1360
  if (orig_table && orig_table->s->table_cache_key &&
      *(orig_table->s->table_cache_key))
1361 1362
  {
    field->org_table_name= orig_table->s->table_name;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1363
    field->db_name= orig_table->s->table_cache_key;
1364 1365
  }
  else
1366
    field->org_table_name= field->db_name= "";
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
  if (orig_table)
  {
    field->table_name= orig_table->alias;
    field->org_col_name= field_name;
  }
  else
  {
    field->table_name= "";
    field->org_col_name= "";
  }
  field->col_name= field_name;
1378
  field->charsetnr= charset()->number;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1379 1380 1381
  field->length=field_length;
  field->type=type();
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
1382
  field->decimals= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1383 1384 1385
}


1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
/*
  Conversion from decimal to longlong with checking overflow and
  setting correct value (min/max) in case of overflow

  SYNOPSIS
    Field::convert_decimal2longlong()
    val             value which have to be converted
    unsigned_flag   type of integer in which we convert val
    err             variable to pass error code

  RETURN
    value converted from val
*/
longlong Field::convert_decimal2longlong(const my_decimal *val,
                                         bool unsigned_flag, int *err)
{
  longlong i;
  if (unsigned_flag)
  {
    if (val->sign())
    {
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
      i= 0;
      *err= 1;
    }
1411
    else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
1412 1413 1414 1415 1416 1417 1418
                                           ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
                                           val, TRUE, &i)))
    {
      i= ~(longlong) 0;
      *err= 1;
    }
  }
1419
  else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
                                         ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
                                         val, FALSE, &i)))
  {
    i= (val->sign() ? LONGLONG_MIN : LONGLONG_MAX);
    *err= 1;
  }
  return i;
}


/*
  Storing decimal in integer fields.

  SYNOPSIS
    Field_num::store_decimal()
    val       value for storing

  NOTE
    This method is used by all integer fields, real/decimal redefine it

  RETURN
    0     OK
    != 0  error
*/

int Field_num::store_decimal(const my_decimal *val)
{
  int err= 0;
  longlong i= convert_decimal2longlong(val, unsigned_flag, &err);
1449
  return test(err | store(i, unsigned_flag));
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
}


/*
  Return decimal value of integer field

  SYNOPSIS
    Field_num::val_decimal()
    decimal_value     buffer for storing decimal value

  NOTE
    This method is used by all integer fields, real/decimal redefine it
    All longlong values fit in our decimal buffer which cal store 8*9=72
    digits of integer number

  RETURN
    pointer to decimal buffer with value of field
*/

my_decimal* Field_num::val_decimal(my_decimal *decimal_value)
{
  DBUG_ASSERT(result_type() == INT_RESULT);
  longlong nr= val_int();
1473
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
1474 1475 1476 1477 1478 1479 1480
  return decimal_value;
}


Field_str::Field_str(char *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
                     uchar null_bit_arg, utype unireg_check_arg,
                     const char *field_name_arg,
1481
                     struct st_table *table_arg, CHARSET_INFO *charset_arg)
1482 1483 1484
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
         unireg_check_arg, field_name_arg, table_arg)
{
1485 1486
  field_charset= charset_arg;
  if (charset_arg->state & MY_CS_BINSORT)
1487
    flags|=BINARY_FLAG;
1488
  field_derivation= DERIVATION_IMPLICIT;
1489 1490 1491
}


1492
void Field_num::make_field(Send_field *field)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1493
{
1494 1495
  Field::make_field(field);
  field->decimals= dec;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1496 1497
}

1498 1499 1500 1501 1502 1503 1504 1505
/*
  Decimal representation of Field_str

  SYNOPSIS
    Field_str::store_decimal()
    d         value for storing

  NOTE
1506
    Field_str is the base class for fields like Field_enum, Field_date and some
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
    similar.  Some dates use fraction and also string value should be
    converted to floating point value according our rules, so we use double
    to store value of decimal in string

  RETURN
    0     OK
    != 0  error
*/

int Field_str::store_decimal(const my_decimal *d)
{
  double val;
  /* TODO: use decimal2string? */
1520
  int err= warn_if_overflow(my_decimal2double(E_DEC_FATAL_ERROR &
1521 1522 1523 1524 1525 1526 1527 1528
                                            ~E_DEC_OVERFLOW, d, &val));
  return err | store(val);
}


my_decimal *Field_str::val_decimal(my_decimal *decimal_value)
{
  longlong nr= val_int();
1529
  int2my_decimal(E_DEC_FATAL_ERROR, nr, 0, decimal_value);
1530 1531 1532
  return decimal_value;
}

1533

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1534 1535
uint Field::fill_cache_field(CACHE_FIELD *copy)
{
1536
  uint store_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1537 1538 1539 1540 1541 1542 1543
  copy->str=ptr;
  copy->length=pack_length();
  copy->blob_field=0;
  if (flags & BLOB_FLAG)
  {
    copy->blob_field=(Field_blob*) this;
    copy->strip=0;
1544
    copy->length-= table->s->blob_ptr_size;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1545 1546
    return copy->length;
  }
1547 1548 1549
  else if (!zero_pack() &&
           (type() == MYSQL_TYPE_STRING && copy->length >= 4 &&
            copy->length < 256))
1550
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1551
    copy->strip=1;				/* Remove end space */
1552 1553
    store_length= 2;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1554
  else
1555
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1556
    copy->strip=0;
1557 1558 1559
    store_length= 0;
  }
  return copy->length+ store_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1560 1561
}

1562

1563
bool Field::get_date(MYSQL_TIME *ltime,uint fuzzydate)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1564 1565
{
  char buff[40];
1566 1567
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
  if (!(res=val_str(&tmp)) ||
1568 1569
      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
1570 1571 1572 1573
    return 1;
  return 0;
}

1574
bool Field::get_time(MYSQL_TIME *ltime)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1575 1576
{
  char buff[40];
1577 1578
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
  if (!(res=val_str(&tmp)) ||
1579
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1580 1581 1582 1583
    return 1;
  return 0;
}

1584 1585 1586 1587 1588 1589
/*
  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
1590

1591
int Field::store_time(MYSQL_TIME *ltime, timestamp_type type_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1592
{
1593 1594
  char buff[MAX_DATE_STRING_REP_LENGTH];
  uint length= (uint) my_TIME_to_str(ltime, buff);
1595
  return store(buff, length, &my_charset_bin);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1596 1597 1598
}


1599
bool Field::optimize_range(uint idx, uint part)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1600
{
1601
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1602 1603
}

1604

1605 1606
Field *Field::new_field(MEM_ROOT *root, struct st_table *new_table,
                        bool keep_type __attribute__((unused)))
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
{
  Field *tmp;
  if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
    return 0;

  if (tmp->table->maybe_null)
    tmp->flags&= ~NOT_NULL_FLAG;
  tmp->table= new_table;
  tmp->key_start.init(0);
  tmp->part_of_key.init(0);
  tmp->part_of_sortkey.init(0);
  tmp->unireg_check=Field::NONE;
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG |
                ZEROFILL_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
  tmp->reset_fields();
  return tmp;
}

monty@mysql.com's avatar
monty@mysql.com committed
1625 1626 1627 1628 1629 1630

Field *Field::new_key_field(MEM_ROOT *root, struct st_table *new_table,
                            char *new_ptr, uchar *new_null_ptr,
                            uint new_null_bit)
{
  Field *tmp;
1631
  if ((tmp= new_field(root, new_table, table == new_table)))
monty@mysql.com's avatar
monty@mysql.com committed
1632 1633 1634 1635 1636 1637 1638 1639
  {
    tmp->ptr=      new_ptr;
    tmp->null_ptr= new_null_ptr;
    tmp->null_bit= new_null_bit;
  }
  return tmp;
}

1640

1641
/****************************************************************************
1642
  Field_null, a field that always return NULL
1643 1644 1645 1646
****************************************************************************/

void Field_null::sql_type(String &res) const
{
1647
  res.set_ascii(STRING_WITH_LEN("null"));
1648 1649 1650
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
1651
/****************************************************************************
1652 1653
  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
1654 1655
****************************************************************************/

1656
int
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1657 1658
Field_decimal::reset(void)
{
1659
  Field_decimal::store(STRING_WITH_LEN("0"),&my_charset_bin);
1660
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1661 1662 1663 1664 1665
}

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

1668
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1669
  if (negative)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1670
  {
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
    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
1694
  }
1695
  bfill(to, len, filler);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1696 1697 1698 1699 1700 1701
  if (dec)
    ptr[field_length-dec-1]='.';
  return;
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1702
int Field_decimal::store(const char *from, uint len, CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1703
{
1704
  char buff[STRING_BUFFER_USUAL_SIZE];
1705
  String tmp(buff,sizeof(buff), &my_charset_bin);
1706

1707 1708 1709
  /* Convert character set if the old one is multi byte */
  if (cs->mbmaxlen > 1)
  { 
1710 1711
    uint dummy_errors;
    tmp.copy(from, len, cs, &my_charset_bin, &dummy_errors);
1712 1713 1714
    from= tmp.ptr();
    len=  tmp.length();
  }
1715

1716 1717 1718 1719 1720 1721 1722 1723 1724
  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
1725
  /* The pointers where prezeros start and stop */
1726
  const char *pre_zeros_from, *pre_zeros_end;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1727
  /* The pointers where digits at the left of '.' start and stop */
1728
  const char *int_digits_from, *int_digits_end;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1729
  /* The pointers where digits at the right of '.' start and stop */
1730
  const char *frac_digits_from, *frac_digits_end;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1731
  /* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
1732
  char expo_sign_char=0;
peter@mysql.com's avatar
peter@mysql.com committed
1733
  uint exponent=0;                                // value of the exponent
1734 1735 1736 1737 1738 1739 1740
  /*
    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
1741 1742 1743 1744
  /*
    Pointer used when digits move from the right of the '.' to the left
    of the '.'
  */
1745
  const char *frac_digits_head_end;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1746
  /* Number of 0 that need to be added at the right of the '.' (for 1E-3) */
1747 1748 1749 1750 1751
  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
1752
  /*
1753
    To remember if table->in_use->cuted_fields has already been incremented,
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1754 1755
    to do that only once
  */
1756 1757 1758 1759 1760 1761 1762 1763 1764
  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
1765 1766
    - parse the input string
    - modify the position of digits around the decimal dot '.' 
1767
      according to the exponent value (if specified)
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1768
    - write the formatted number
1769 1770 1771 1772 1773
  */

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

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1774
  /* skip pre-space */
1775
  while (from != end && my_isspace(&my_charset_bin,*from))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1776
    from++;
1777
  if (from == end)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1778
  {
1779
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
1780
    is_cuted_fields_incr=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1781
  }
1782
  else if (*from == '+' || *from == '-')	// Found some sign ?
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1783
  {
1784
    sign_char= *from++;
1785
    /*
1786 1787 1788
      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
1789 1790 1791 1792
    */ 
    if (unsigned_flag)  
    { 
      if (sign_char=='-')
1793
      {
1794
        Field_decimal::overflow(1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1795
        return 1;
1796
      }
1797
      /* 
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1798 1799
	 Defining this will not store "+" for unsigned decimal type even if
	 it is passed in numeric string. This will make some tests to fail
1800 1801
      */	 
#ifdef DONT_ALLOW_UNSIGNED_PLUS      
1802 1803
      else 
        sign_char=0;
1804
#endif 	
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1805
    }
1806
  }
1807

1808 1809 1810 1811
  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 '.'*/
1812
  for (; from != end && my_isdigit(&my_charset_bin, *from) ; from++) ;
1813 1814 1815 1816 1817
  int_digits_end=from;
  if (from!=end && *from == '.')		// Some '.' ?
    from++;
  frac_digits_from= from;
  /* Read digits at the right of '.' */
1818
  for (;from!=end && my_isdigit(&my_charset_bin, *from); from++) ;
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
  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= '+';
    /*
1829 1830 1831 1832
      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).
1833
    */
1834
    for (;from!=end && my_isdigit(&my_charset_bin, *from); from++)
1835
    {
1836
      exponent=10*exponent+(*from-'0');
peter@mysql.com's avatar
peter@mysql.com committed
1837
      if (exponent>MAX_EXPONENT)
1838 1839
        break;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1840
  }
1841
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1842
  /*
1843 1844
    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.
1845
    Even if this flag is not set, it's OK to increment warnings, if
1846
    it makes the code easer to read.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1847
  */
1848

1849
  if (table->in_use->count_cuted_fields)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1850
  {
1851
    // Skip end spaces
1852
    for (;from != end && my_isspace(&my_charset_bin, *from); from++) ;
1853
    if (from != end)                     // If still something left, warn
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1854
    {
1855
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
1856
      is_cuted_fields_incr=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1857
    }
1858 1859 1860 1861 1862 1863
  }
  
  /*
    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
1864 1865 1866
    - 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'
1867
    - 1234.5E-3 : '234' moves at the right of '.'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1868 1869
    These moves are implemented with pointers which point at the begin
    and end of each moved segment. Examples :
1870
    - 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
1871 1872 1873
    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'.
1874
    - 1234.5E3 : before the code below is executed, the int_digits part is
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1875 1876 1877 1878
    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).
1879 1880
  */
  
1881
  /* 
peter@mysql.com's avatar
peter@mysql.com committed
1882
     Below tmp_uint cannot overflow with small enough MAX_EXPONENT setting,
1883
     as int_digits_added_zeros<=exponent<4G and 
1884 1885
     (int_digits_end-int_digits_from)<=max_allowed_packet<=2G and
     (frac_digits_from-int_digits_tail_from)<=max_allowed_packet<=2G
1886 1887
  */

1888 1889 1890 1891 1892 1893 1894 1895
  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
1896
    tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);     
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
  }
  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
1922
    }
1923
    tmp_uint= (uint) (tmp_dec+(int_digits_end-int_digits_from)+
1924 1925
               (uint)(frac_digits_from-int_digits_tail_from)+
               int_digits_added_zeros);
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
  }
  
  /*
    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
  */

1936
  if (field_length < tmp_uint + (int) (sign_char == '-')) 
1937
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1938
    // too big number, change to max or min number
1939
    Field_decimal::overflow(sign_char == '-');
1940
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1941
  }
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
 
  /*
    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
1959
  }
1960 1961 1962 1963 1964 1965
  /*
    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
1966 1967

  /*
1968 1969
    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
1970
  */
1971 1972 1973 1974
  pos=tmp_left_pos-1;
  if (zerofill)
  {
    left_wall=to-1;
1975
    while (pos > left_wall)			// Fill with zeros
1976 1977 1978 1979
      *pos--='0';
  }
  else
  {
1980
    left_wall=to+(sign_char != 0)-1;
1981 1982
    if (!expo_sign_char)	// If exponent was specified, ignore prezeros
    {
1983
      for (;pos > left_wall && pre_zeros_from !=pre_zeros_end;
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
	   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
  }
  
1999 2000
  /*
    Write digits of the frac_% parts ;
2001
    Depending on table->in_use->count_cutted_fields, we may also want
2002 2003 2004 2005 2006
    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)
  */
2007
      
2008 2009 2010
  pos=to+(uint)(field_length-tmp_dec);	// Calculate post to '.'
  right_wall=to+field_length;
  if (pos != right_wall) 
2011 2012
    *pos++='.';

2013 2014 2015
  if (expo_sign_char == '-')
  {
    while (frac_digits_added_zeros-- > 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2016
    {
2017
      if (pos == right_wall) 
2018
      {
2019
        if (table->in_use->count_cuted_fields && !is_cuted_fields_incr) 
2020
          break; // Go on below to see if we lose non zero digits
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2021
        return 0;
2022
      }
2023 2024 2025 2026 2027 2028
      *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
2029
      {
2030 2031 2032
        if (tmp_char != '0')			// Losing a non zero digit ?
        {
          if (!is_cuted_fields_incr)
2033
            set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
2034
                        WARN_DATA_TRUNCATED, 1);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2035
          return 0;
2036 2037
        }
        continue;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2038
      }
2039
      *pos++= tmp_char;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2040
    }
2041
  }
2042

2043 2044 2045 2046
  for (;frac_digits_from!=frac_digits_end;) 
  {
    tmp_char= *frac_digits_from++;
    if (pos == right_wall)
2047
    {
2048
      if (tmp_char != '0')			// Losing a non zero digit ?
2049
      {
2050
        if (!is_cuted_fields_incr)
2051 2052 2053 2054 2055
        {
          /*
            This is a note, not a warning, as we don't want to abort
            when we cut decimals in strict mode
          */
2056
	  set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
2057
        }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2058
        return 0;
2059
      }
2060
      continue;
2061
    }
2062
    *pos++= tmp_char;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2063
  }
2064 2065 2066
      
  while (pos != right_wall)
   *pos++='0';			// Fill with zeros at right of '.'
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2067
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2068 2069 2070
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2071
int Field_decimal::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2072 2073 2074 2075
{
  if (unsigned_flag && nr < 0)
  {
    overflow(1);
2076
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2077
  }
2078
  
2079 2080
#ifdef HAVE_FINITE
  if (!finite(nr)) // Handle infinity as special case
2081 2082
  {
    overflow(nr < 0.0);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2083
    return 1;
2084
  }
2085
#endif
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2086

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2087 2088
  reg4 uint i,length;
  char fyllchar,*to;
2089
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2090 2091

  fyllchar = zerofill ? (char) '0' : (char) ' ';
serg@serg.mylan's avatar
serg@serg.mylan committed
2092
#ifdef HAVE_SNPRINTF
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
2093 2094
  buff[sizeof(buff)-1]=0;			// Safety
  snprintf(buff,sizeof(buff)-1, "%.*f",(int) dec,nr);
2095
  length=(uint) strlen(buff);
2096
#else
2097
  length=(uint) my_sprintf(buff,(buff,"%.*f",dec,nr));
2098
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2099 2100 2101 2102

  if (length > field_length)
  {
    overflow(nr < 0.0);
2103
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2104 2105 2106 2107 2108 2109 2110
  }
  else
  {
    to=ptr;
    for (i=field_length-length ; i-- > 0 ;)
      *to++ = fyllchar;
    memcpy(to,buff,length);
2111
    return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2112 2113 2114 2115
  }
}


2116
int Field_decimal::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2117
{
2118 2119 2120 2121 2122
  char buff[22];
  uint length, int_part;
  char fyllchar, *to;

  if (nr < 0 && unsigned_flag && !unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2123 2124
  {
    overflow(1);
2125
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2126
  }
2127 2128
  length= (uint) (longlong10_to_str(nr,buff,unsigned_val ? 10 : -10) - buff);
  int_part= field_length- (dec  ? dec+1 : 0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2129 2130 2131

  if (length > int_part)
  {
2132
    overflow(!unsigned_val && nr < 0L);		/* purecov: inspected */
2133
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2134
  }
2135 2136 2137 2138 2139 2140 2141

  fyllchar = zerofill ? (char) '0' : (char) ' ';
  to= ptr;
  for (uint i=int_part-length ; i-- > 0 ;)
    *to++ = fyllchar;
  memcpy(to,buff,length);
  if (dec)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2142
  {
2143 2144
    to[length]='.';
    bfill(to+length+1,dec,'0');
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2145
  }
2146
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2147 2148 2149 2150 2151
}


double Field_decimal::val_real(void)
{
2152
  int not_used;
2153 2154 2155
  char *end_not_used;
  return my_strntod(&my_charset_bin, ptr, field_length, &end_not_used,
                    &not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2156 2157 2158 2159
}

longlong Field_decimal::val_int(void)
{
2160
  int not_used;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2161
  if (unsigned_flag)
2162
    return my_strntoull(&my_charset_bin, ptr, field_length, 10, NULL,
2163
			&not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2164
  else
2165
    return my_strntoll(&my_charset_bin, ptr, field_length, 10, NULL,
2166
			&not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2167 2168
}

2169

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2170 2171 2172 2173 2174 2175
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);
2176
  val_ptr->set_charset(&my_charset_bin);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2177 2178 2179
  if (field_length < tmp_length)		// Error in data
    val_ptr->length(0);
  else
2180
    val_ptr->set_ascii((const char*) str, field_length-tmp_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
  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;
2192
  int swap=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2193 2194 2195 2196
  /* First remove prefixes '0', ' ', and '-' */
  for (end=a_ptr+field_length;
       a_ptr != end &&
	 (*a_ptr == *b_ptr ||
2197
	  ((my_isspace(&my_charset_bin,*a_ptr)  || *a_ptr == '+' || 
2198
            *a_ptr == '0') &&
2199
	   (my_isspace(&my_charset_bin,*b_ptr) || *b_ptr == '+' || 
2200
            *b_ptr == '0')));
2201 2202 2203 2204 2205
       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
2206 2207 2208
  if (a_ptr == end)
    return 0;
  if (*a_ptr == '-')
2209
    return -1;
2210
  if (*b_ptr == '-')
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
    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 &&
2227
	 ((my_isspace(&my_charset_bin,*str) || *str == '+' ||
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2228
	   *str == '0')) ;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238
       str++)
    *to++=' ';
  if (str == end)
    return;					/* purecov: inspected */

  if (*str == '-')
  {
    *to++=1;					// Smaller than any number
    str++;
    while (str != end)
2239
      if (my_isdigit(&my_charset_bin,*str))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2240 2241 2242 2243 2244 2245 2246
	*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
2247

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2248 2249
void Field_decimal::sql_type(String &res) const
{
2250
  CHARSET_INFO *cs=res.charset();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2251 2252 2253 2254 2255
  uint tmp=field_length;
  if (!unsigned_flag)
    tmp--;
  if (dec)
    tmp--;
2256
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2257
			  "decimal(%d,%d)",tmp,dec));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2258 2259 2260 2261
  add_zerofill_and_unsigned(res);
}


2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
/****************************************************************************
** Field_new_decimal
****************************************************************************/

Field_new_decimal::Field_new_decimal(char *ptr_arg,
                                     uint32 len_arg, uchar *null_ptr_arg,
                                     uchar null_bit_arg,
                                     enum utype unireg_check_arg,
                                     const char *field_name_arg,
                                     struct st_table *table_arg,
                                     uint8 dec_arg,bool zero_arg,
                                     bool unsigned_arg)
2274
  :Field_num(ptr_arg, len_arg,
2275 2276
             null_ptr_arg, null_bit_arg,
             unireg_check_arg, field_name_arg, table_arg,
2277
             dec_arg, zero_arg, unsigned_arg)
2278
{
2279
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
2280
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
2281 2282 2283
  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
              (dec <= DECIMAL_MAX_SCALE));
  bin_size= my_decimal_get_binary_size(precision, dec);
2284 2285 2286 2287
}


Field_new_decimal::Field_new_decimal(uint32 len_arg,
2288
                                     bool maybe_null_arg,
2289 2290
                                     const char *name,
                                     struct st_table *t_arg,
2291 2292 2293
                                     uint8 dec_arg,
                                     bool unsigned_arg)
  :Field_num((char*) 0, len_arg,
2294
             maybe_null_arg ? (uchar*) "": 0, 0,
2295
             NONE, name, t_arg,
2296 2297
             dec_arg,
             0, unsigned_arg)
2298
{
2299
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
2300
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
2301 2302 2303
  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
              (dec <= DECIMAL_MAX_SCALE));
  bin_size= my_decimal_get_binary_size(precision, dec);
2304 2305 2306
}


holyfoot/hf@mysql.com/deer.(none)'s avatar
holyfoot/hf@mysql.com/deer.(none) committed
2307
int Field_new_decimal::reset(void)
2308 2309
{
  store_value(&decimal_zero);
holyfoot/hf@mysql.com/deer.(none)'s avatar
holyfoot/hf@mysql.com/deer.(none) committed
2310
  return 0;
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
}


/*
  Generate max/min decimal value in case of overflow.

  SYNOPSIS
    Field_new_decimal::set_value_on_overflow();
    decimal_value     buffer for value
    sign              sign of value which caused overflow
*/

void Field_new_decimal::set_value_on_overflow(my_decimal *decimal_value,
                                              bool sign)
{
  DBUG_ENTER("Field_new_decimal::set_value_on_overflow");
2327
  max_my_decimal(decimal_value, precision, decimals());
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
  if (sign)
  {
    if (unsigned_flag)
      my_decimal_set_zero(decimal_value);
    else
      decimal_value->sign(TRUE);
  }
  DBUG_VOID_RETURN;
}


/*
  Store decimal value in the binary buffer

  SYNOPSIS
    store_value(const my_decimal *decimal_value)
    decimal_value   my_decimal

  DESCRIPTION
    checks if decimal_value fits into field size.
    if it does, stores the decimal in the buffer using binary format.
    Otherwise sets maximal number that can be stored in the field.
2350 2351 2352 2353

  RETURN
   0 ok
   1 error
2354 2355 2356 2357
*/

bool Field_new_decimal::store_value(const my_decimal *decimal_value)
{
2358 2359
  int error= 0;
  DBUG_ENTER("Field_new_decimal::store_value");
2360 2361 2362 2363 2364 2365
#ifndef DBUG_OFF
  {
    char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
    DBUG_PRINT("enter", ("value: %s", dbug_decimal_as_string(dbug_buff, decimal_value)));
  }
#endif
2366 2367 2368 2369 2370 2371

  /* check that we do not try to write negative value in unsigned field */
  if (unsigned_flag && decimal_value->sign())
  {
    DBUG_PRINT("info", ("unsigned overflow"));
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2372
    error= 1;
2373 2374 2375 2376 2377 2378 2379 2380
    decimal_value= &decimal_zero;
  }
#ifndef DBUG_OFF
  {
    char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
    DBUG_PRINT("info", ("saving with precision %d, scale: %d, value %s",
                        (int)precision, (int)dec,
                        dbug_decimal_as_string(dbug_buff, decimal_value)));
2381
  }
2382
#endif
2383

2384 2385
  if (warn_if_overflow(my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
                                         decimal_value, ptr, precision, dec)))
2386 2387 2388
  {
    my_decimal buff;
    DBUG_PRINT("info", ("overflow"));
2389 2390
    set_value_on_overflow(&buff, decimal_value->sign());
    my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
2391
    error= 1;
2392
  }
2393
  DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (byte *) ptr, bin_size););
2394
  DBUG_RETURN(error);
2395 2396 2397 2398
}


int Field_new_decimal::store(const char *from, uint length,
2399
                             CHARSET_INFO *charset_arg)
2400 2401 2402
{
  int err;
  my_decimal decimal_value;
2403 2404
  DBUG_ENTER("Field_new_decimal::store(char*)");

2405 2406
  if ((err= str2my_decimal(E_DEC_FATAL_ERROR &
                      ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
2407
                      from, length, charset_arg,  &decimal_value)) &&
2408 2409
      table->in_use->abort_on_warning)
  {
2410 2411 2412 2413
    /* Because "from" is not NUL-terminated and we use %s in the ER() */
    String from_as_str;
    from_as_str.copy(from, length, &my_charset_bin);

2414 2415 2416
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_ERROR,
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
2417
                        "decimal", from_as_str.c_ptr(), field_name,
2418
                        (ulong) table->in_use->row_count);
2419

2420 2421 2422 2423
    DBUG_RETURN(err);
  }

  switch (err) {
2424 2425 2426 2427 2428 2429 2430 2431
  case E_DEC_TRUNCATED:
    set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
    break;
  case E_DEC_OVERFLOW:
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
    set_value_on_overflow(&decimal_value, decimal_value.sign());
    break;
  case E_DEC_BAD_NUM:
2432 2433 2434 2435 2436
    {
      /* Because "from" is not NUL-terminated and we use %s in the ER() */
      String from_as_str;
      from_as_str.copy(from, length, &my_charset_bin);

2437 2438 2439
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
2440
                          "decimal", from_as_str.c_ptr(), field_name,
2441 2442
                        (ulong) table->in_use->row_count);
    my_decimal_set_zero(&decimal_value);
2443

2444
    break;
2445
    }
2446 2447
  }

2448 2449 2450 2451 2452
#ifndef DBUG_OFF
  char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
  DBUG_PRINT("enter", ("value: %s",
                       dbug_decimal_as_string(dbug_buff, &decimal_value)));
#endif
2453 2454 2455 2456 2457 2458 2459 2460
  store_value(&decimal_value);
  DBUG_RETURN(err);
}


int Field_new_decimal::store(double nr)
{
  my_decimal decimal_value;
2461 2462 2463 2464 2465
  int err;
  DBUG_ENTER("Field_new_decimal::store(double)");

  err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
                         &decimal_value);
2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476
  /*
    TODO: fix following when double2my_decimal when double2decimal
    will return E_DEC_TRUNCATED always correctly
  */
  if (!err)
  {
    double nr2;
    my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &nr2);
    if (nr2 != nr)
      err= E_DEC_TRUNCATED;
  }
2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
  if (err)
  {
    if (check_overflow(err))
      set_value_on_overflow(&decimal_value, decimal_value.sign());
    /* Only issue a warning if store_value doesn't issue an warning */
    table->in_use->got_warning= 0;
  }
  if (store_value(&decimal_value))
    err= 1;
  else if (err && !table->in_use->got_warning)
    err= warn_if_overflow(err);
  DBUG_RETURN(err);
2489 2490 2491
}


2492
int Field_new_decimal::store(longlong nr, bool unsigned_val)
2493 2494 2495
{
  my_decimal decimal_value;
  int err;
2496 2497

  if ((err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
2498
                           nr, unsigned_val, &decimal_value)))
2499 2500 2501 2502 2503 2504 2505 2506 2507 2508
  {
    if (check_overflow(err))
      set_value_on_overflow(&decimal_value, decimal_value.sign());
    /* Only issue a warning if store_value doesn't issue an warning */
    table->in_use->got_warning= 0;
  }
  if (store_value(&decimal_value))
    err= 1;
  else if (err && !table->in_use->got_warning)
    err= warn_if_overflow(err);
2509 2510 2511 2512 2513 2514 2515 2516 2517 2518
  return err;
}


int Field_new_decimal::store_decimal(const my_decimal *decimal_value)
{
  return store_value(decimal_value);
}


2519
int Field_new_decimal::store_time(MYSQL_TIME *ltime, timestamp_type t_type)
2520 2521 2522
{
    my_decimal decimal_value;
    return store_value(date2my_decimal(ltime, &decimal_value));
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
}


double Field_new_decimal::val_real(void)
{
  double dbl;
  my_decimal decimal_value;
  my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
  return dbl;
}


longlong Field_new_decimal::val_int(void)
{
  longlong i;
  my_decimal decimal_value;
  my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
                 unsigned_flag, &i);
  return i;
}


my_decimal* Field_new_decimal::val_decimal(my_decimal *decimal_value)
{
  DBUG_ENTER("Field_new_decimal::val_decimal");
  binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
2549
                    precision, dec);
2550 2551
  DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (byte *) ptr,
                                          bin_size););
2552 2553 2554 2555 2556
  DBUG_RETURN(decimal_value);
}


String *Field_new_decimal::val_str(String *val_buffer,
2557
                                   String *val_ptr __attribute__((unused)))
2558 2559
{
  my_decimal decimal_value;
2560
  uint fixed_precision= zerofill ? precision : 0;
2561
  my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
2562
                    fixed_precision, dec, '0', val_buffer);
2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583
  return val_buffer;
}


int Field_new_decimal::cmp(const char *a,const char*b)
{
  return memcmp(a, b, bin_size);
}


void Field_new_decimal::sort_string(char *buff,
                                    uint length __attribute__((unused)))
{
  memcpy(buff, ptr, bin_size);
}


void Field_new_decimal::sql_type(String &str) const
{
  CHARSET_INFO *cs= str.charset();
  str.length(cs->cset->snprintf(cs, (char*) str.ptr(), str.alloced_length(),
2584
                                "decimal(%d,%d)", precision, (int)dec));
2585 2586 2587
  add_zerofill_and_unsigned(str);
}

2588

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2589 2590 2591 2592
/****************************************************************************
** tiny int
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2593
int Field_tiny::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2594
{
2595
  int error;
2596 2597 2598 2599
  longlong rnd;
  
  error= get_int(cs, from, len, &rnd, 255, -128, 127);
  ptr[0]= unsigned_flag ? (char) (ulonglong) rnd : (char) rnd;
2600
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2601 2602 2603
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2604
int Field_tiny::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2605
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2606
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2607 2608 2609 2610 2611 2612
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0.0)
    {
      *ptr=0;
2613
      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
2614
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2615 2616 2617 2618
    }
    else if (nr > 255.0)
    {
      *ptr=(char) 255;
2619
      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
2620
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2621 2622 2623 2624 2625 2626 2627 2628 2629
    }
    else
      *ptr=(char) nr;
  }
  else
  {
    if (nr < -128.0)
    {
      *ptr= (char) -128;
2630
      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
2631
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2632 2633 2634 2635
    }
    else if (nr > 127.0)
    {
      *ptr=127;
2636
      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
2637
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2638 2639
    }
    else
serg@serg.mylan's avatar
serg@serg.mylan committed
2640
      *ptr=(char) (int) nr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2641
  }
2642
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2643 2644
}

2645 2646

int Field_tiny::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2647
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2648
  int error= 0;
2649

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2650 2651
  if (unsigned_flag)
  {
2652
    if (nr < 0 && !unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2653
    {
2654
      *ptr= 0;
2655
      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
2656
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2657
    }
2658
    else if ((ulonglong) nr > (ulonglong) 255)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2659 2660
    {
      *ptr= (char) 255;
2661
      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
2662
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2663 2664 2665 2666 2667 2668
    }
    else
      *ptr=(char) nr;
  }
  else
  {
2669 2670 2671
    if (nr < 0 && unsigned_val)
      nr= 256;                                    // Generate overflow
    if (nr < -128)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2672 2673
    {
      *ptr= (char) -128;
2674
      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
2675
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2676
    }
2677
    else if (nr > 127)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2678 2679
    {
      *ptr=127;
2680
      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
2681
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2682 2683 2684 2685
    }
    else
      *ptr=(char) nr;
  }
2686
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
}


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

2697

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2698 2699 2700 2701 2702 2703 2704
longlong Field_tiny::val_int(void)
{
  int tmp= unsigned_flag ? (int) ((uchar*) ptr)[0] :
    (int) ((signed char*) ptr)[0];
  return (longlong) tmp;
}

2705

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2706 2707 2708
String *Field_tiny::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
2709
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2710
  uint length;
2711 2712
  uint mlength=max(field_length+1,5*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2713
  char *to=(char*) val_buffer->ptr();
2714

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2715
  if (unsigned_flag)
2716 2717
    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
2718
  else
2719 2720
    length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
					   (long) *((signed char*) ptr));
2721
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2722 2723 2724 2725 2726 2727
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}

2728 2729 2730 2731
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
2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751

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
{
2752
  CHARSET_INFO *cs=res.charset();
2753
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2754
			  "tinyint(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2755 2756 2757 2758
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
2759
 Field type short int (2 byte)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2760 2761
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2762
int Field_short::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2763
{
2764
  int store_tmp;
2765
  int error;
2766 2767 2768 2769
  longlong rnd;
  
  error= get_int(cs, from, len, &rnd, UINT_MAX16, INT_MIN16, INT_MAX16);
  store_tmp= unsigned_flag ? (int) (ulonglong) rnd : (int) rnd;
2770
#ifdef WORDS_BIGENDIAN
2771 2772 2773
  if (table->s->db_low_byte_first)
  {
    int2store(ptr, store_tmp);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2774 2775 2776
  }
  else
#endif
2777
    shortstore(ptr, (short) store_tmp);
2778
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2779 2780 2781
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2782
int Field_short::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2783
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2784
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2785 2786 2787 2788 2789 2790 2791
  int16 res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
2792
      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
2793
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2794
    }
serg@serg.mylan's avatar
serg@serg.mylan committed
2795
    else if (nr > (double) UINT_MAX16)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2796
    {
serg@serg.mylan's avatar
serg@serg.mylan committed
2797
      res=(int16) UINT_MAX16;
2798
      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
2799
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2800 2801 2802 2803 2804 2805 2806 2807 2808
    }
    else
      res=(int16) (uint16) nr;
  }
  else
  {
    if (nr < (double) INT_MIN16)
    {
      res=INT_MIN16;
2809
      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
2810
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2811 2812 2813 2814
    }
    else if (nr > (double) INT_MAX16)
    {
      res=INT_MAX16;
2815
      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
2816
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2817 2818
    }
    else
serg@serg.mylan's avatar
serg@serg.mylan committed
2819
      res=(int16) (int) nr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2820 2821
  }
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
2822
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2823 2824 2825 2826 2827 2828
  {
    int2store(ptr,res);
  }
  else
#endif
    shortstore(ptr,res);
2829
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2830 2831
}

2832 2833

int Field_short::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2834
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
2835
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2836
  int16 res;
2837

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2838 2839
  if (unsigned_flag)
  {
2840
    if (nr < 0L && !unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2841 2842
    {
      res=0;
2843
      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
2844
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2845
    }
2846
    else if ((ulonglong) nr > (ulonglong) UINT_MAX16)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2847
    {
serg@serg.mylan's avatar
serg@serg.mylan committed
2848
      res=(int16) UINT_MAX16;
2849
      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
2850
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2851 2852 2853 2854 2855 2856
    }
    else
      res=(int16) (uint16) nr;
  }
  else
  {
2857 2858 2859
    if (nr < 0 && unsigned_val)
      nr= UINT_MAX16+1;                         // Generate overflow

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2860 2861 2862
    if (nr < INT_MIN16)
    {
      res=INT_MIN16;
2863
      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
2864
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2865
    }
2866
    else if (nr > (longlong) INT_MAX16)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2867 2868
    {
      res=INT_MAX16;
2869
      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
2870
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2871 2872 2873 2874 2875
    }
    else
      res=(int16) nr;
  }
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
2876
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2877 2878 2879 2880 2881 2882
  {
    int2store(ptr,res);
  }
  else
#endif
    shortstore(ptr,res);
2883
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2884 2885 2886 2887 2888 2889 2890
}


double Field_short::val_real(void)
{
  short j;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
2891
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902
    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
kent@mysql.com's avatar
kent@mysql.com committed
2903
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2904 2905 2906 2907 2908 2909 2910
    j=sint2korr(ptr);
  else
#endif
    shortget(j,ptr);
  return unsigned_flag ? (longlong) (unsigned short) j : (longlong) j;
}

2911

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2912 2913 2914
String *Field_short::val_str(String *val_buffer,
			     String *val_ptr __attribute__((unused)))
{
2915
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2916
  uint length;
2917 2918
  uint mlength=max(field_length+1,7*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2919 2920 2921
  char *to=(char*) val_buffer->ptr();
  short j;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
2922
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2923 2924 2925 2926 2927 2928
    j=sint2korr(ptr);
  else
#endif
    shortget(j,ptr);

  if (unsigned_flag)
2929 2930
    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
2931
  else
2932
    length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2933 2934 2935 2936 2937 2938 2939
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


2940 2941 2942 2943 2944 2945
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
2946 2947 2948 2949
int Field_short::cmp(const char *a_ptr, const char *b_ptr)
{
  short a,b;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
2950
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970
  {
    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
kent@mysql.com's avatar
kent@mysql.com committed
2971
  if (!table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2972 2973 2974 2975
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
2976
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2977 2978 2979 2980 2981 2982 2983 2984
    to[1]   = ptr[1];
  }
  else
#endif
  {
    if (unsigned_flag)
      to[0] = ptr[1];
    else
2985
      to[0] = (char) (ptr[1] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2986 2987 2988 2989 2990 2991
    to[1]   = ptr[0];
  }
}

void Field_short::sql_type(String &res) const
{
2992
  CHARSET_INFO *cs=res.charset();
2993
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2994
			  "smallint(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2995 2996 2997 2998 2999
  add_zerofill_and_unsigned(res);
}


/****************************************************************************
3000
  Field type medium int (3 byte)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3001 3002
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3003
int Field_medium::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3004
{
3005
  int store_tmp;
3006
  int error;
3007 3008 3009 3010 3011
  longlong rnd;
  
  error= get_int(cs, from, len, &rnd, UINT_MAX24, INT_MIN24, INT_MAX24);
  store_tmp= unsigned_flag ? (int) (ulonglong) rnd : (int) rnd;
  int3store(ptr, store_tmp);
3012
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3013 3014 3015
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3016
int Field_medium::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3017
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3018
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3019 3020 3021 3022 3023 3024
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      int3store(ptr,0);
3025
      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
3026
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3027 3028 3029
    }
    else if (nr >= (double) (long) (1L << 24))
    {
3030
      uint32 tmp=(uint32) (1L << 24)-1L;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3031
      int3store(ptr,tmp);
3032
      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
3033
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3034 3035
    }
    else
3036
      int3store(ptr,(uint32) nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3037 3038 3039 3040 3041 3042 3043
  }
  else
  {
    if (nr < (double) INT_MIN24)
    {
      long tmp=(long) INT_MIN24;
      int3store(ptr,tmp);
3044
      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
3045
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3046 3047 3048 3049 3050
    }
    else if (nr > (double) INT_MAX24)
    {
      long tmp=(long) INT_MAX24;
      int3store(ptr,tmp);
3051
      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
3052
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3053 3054 3055 3056
    }
    else
      int3store(ptr,(long) nr);
  }
3057
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3058 3059
}

3060 3061

int Field_medium::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3062
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3063
  int error= 0;
3064

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3065 3066
  if (unsigned_flag)
  {
3067
    if (nr < 0 && !unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3068 3069
    {
      int3store(ptr,0);
3070
      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
3071
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3072
    }
3073
    else if ((ulonglong) nr >= (ulonglong) (long) (1L << 24))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3074
    {
3075
      long tmp= (long) (1L << 24)-1L;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3076
      int3store(ptr,tmp);
3077
      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
3078
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3079 3080
    }
    else
3081
      int3store(ptr,(uint32) nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3082 3083 3084
  }
  else
  {
3085 3086 3087
    if (nr < 0 && unsigned_val)
      nr= (ulonglong) (long) (1L << 24);        // Generate overflow

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3088 3089
    if (nr < (longlong) INT_MIN24)
    {
3090
      long tmp= (long) INT_MIN24;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3091
      int3store(ptr,tmp);
3092
      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
3093
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3094 3095 3096 3097 3098
    }
    else if (nr > (longlong) INT_MAX24)
    {
      long tmp=(long) INT_MAX24;
      int3store(ptr,tmp);
3099
      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
3100
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3101 3102 3103 3104
    }
    else
      int3store(ptr,(long) nr);
  }
3105
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3106 3107 3108 3109 3110 3111 3112 3113 3114
}


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

3115

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3116 3117 3118 3119 3120 3121
longlong Field_medium::val_int(void)
{
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
  return (longlong) j;
}

3122

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3123 3124 3125
String *Field_medium::val_str(String *val_buffer,
			      String *val_ptr __attribute__((unused)))
{
3126
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3127
  uint length;
3128 3129
  uint mlength=max(field_length+1,10*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3130 3131 3132
  char *to=(char*) val_buffer->ptr();
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);

3133
  length=(uint) cs->cset->long10_to_str(cs,to,mlength,-10,j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3134 3135 3136 3137 3138 3139 3140
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer); /* purecov: inspected */
  return val_buffer;
}


3141 3142 3143 3144 3145 3146
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
3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175
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
{
3176
  CHARSET_INFO *cs=res.charset();
3177
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), 
3178
			  "mediumint(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3179 3180 3181 3182 3183 3184 3185
  add_zerofill_and_unsigned(res);
}

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

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3186
int Field_long::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3187
{
3188 3189
  long store_tmp;
  int error;
3190 3191 3192 3193
  longlong rnd;
  
  error= get_int(cs, from, len, &rnd, UINT_MAX32, INT_MIN32, INT_MAX32);
  store_tmp= unsigned_flag ? (long) (ulonglong) rnd : (long) rnd;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3194
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3195
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3196
  {
3197
    int4store(ptr, store_tmp);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3198 3199 3200
  }
  else
#endif
3201
    longstore(ptr, store_tmp);
3202
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3203 3204 3205
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3206
int Field_long::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3207
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3208
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3209 3210 3211 3212 3213 3214 3215
  int32 res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3216
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3217
    }
3218
    else if (nr > (double) UINT_MAX32)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3219
    {
3220
      res= UINT_MAX32;
3221
      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
3222
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3223 3224 3225 3226 3227 3228 3229 3230 3231
    }
    else
      res=(int32) (ulong) nr;
  }
  else
  {
    if (nr < (double) INT_MIN32)
    {
      res=(int32) INT_MIN32;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3232
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3233 3234 3235 3236
    }
    else if (nr > (double) INT_MAX32)
    {
      res=(int32) INT_MAX32;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3237
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3238 3239
    }
    else
serg@serg.mylan's avatar
serg@serg.mylan committed
3240
      res=(int32) (longlong) nr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3241
  }
3242 3243 3244
  if (error)
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3245
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3246
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3247 3248 3249 3250 3251 3252
  {
    int4store(ptr,res);
  }
  else
#endif
    longstore(ptr,res);
3253
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3254 3255 3256
}


3257
int Field_long::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3258
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3259
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3260
  int32 res;
3261
  DBUG_ASSERT(table->in_use == current_thd);    // General safety
3262
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3263 3264
  if (unsigned_flag)
  {
3265
    if (nr < 0 && !unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3266 3267
    {
      res=0;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3268
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3269
    }
3270
    else if ((ulonglong) nr >= (LL(1) << 32))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3271 3272
    {
      res=(int32) (uint32) ~0L;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3273
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3274 3275 3276 3277 3278 3279
    }
    else
      res=(int32) (uint32) nr;
  }
  else
  {
3280
    if (nr < 0 && unsigned_val)
3281
      nr= ((longlong) INT_MAX32) + 1;           // Generate overflow
3282
    if (nr < (longlong) INT_MIN32) 
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3283 3284
    {
      res=(int32) INT_MIN32;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3285
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3286 3287 3288 3289
    }
    else if (nr > (longlong) INT_MAX32)
    {
      res=(int32) INT_MAX32;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3290
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3291 3292 3293 3294
    }
    else
      res=(int32) nr;
  }
3295 3296 3297
  if (error)
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3298
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3299
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3300 3301 3302 3303 3304 3305
  {
    int4store(ptr,res);
  }
  else
#endif
    longstore(ptr,res);
3306
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3307 3308 3309 3310 3311 3312 3313
}


double Field_long::val_real(void)
{
  int32 j;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3314
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3315 3316 3317 3318 3319 3320 3321 3322 3323 3324
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);
  return unsigned_flag ? (double) (uint32) j : (double) j;
}

longlong Field_long::val_int(void)
{
  int32 j;
3325 3326
  /* 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
3327
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3328
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3329 3330 3331 3332 3333 3334 3335 3336 3337 3338
    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)))
{
3339
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3340
  uint length;
3341 3342
  uint mlength=max(field_length+1,12*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3343 3344 3345
  char *to=(char*) val_buffer->ptr();
  int32 j;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3346
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3347 3348 3349 3350 3351
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);

3352
  if (unsigned_flag)
3353
    length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32)j);
3354
  else
3355
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3356 3357 3358 3359 3360 3361 3362
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


3363 3364 3365 3366 3367
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
3368 3369 3370 3371
int Field_long::cmp(const char *a_ptr, const char *b_ptr)
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3372
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383
  {
    a=sint4korr(a_ptr);
    b=sint4korr(b_ptr);
  }
  else
#endif
  {
    longget(a,a_ptr);
    longget(b,b_ptr);
  }
  if (unsigned_flag)
3384
    return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3385 3386 3387 3388 3389 3390
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

void Field_long::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3391
  if (!table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3392 3393 3394 3395
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
3396
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3397 3398 3399 3400 3401 3402 3403 3404 3405 3406
    to[1]   = ptr[1];
    to[2]   = ptr[2];
    to[3]   = ptr[3];
  }
  else
#endif
  {
    if (unsigned_flag)
      to[0] = ptr[3];
    else
3407
      to[0] = (char) (ptr[3] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3408 3409 3410 3411 3412 3413 3414 3415 3416
    to[1]   = ptr[2];
    to[2]   = ptr[1];
    to[3]   = ptr[0];
  }
}


void Field_long::sql_type(String &res) const
{
3417
  CHARSET_INFO *cs=res.charset();
3418
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3419
			  "int(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3420 3421 3422 3423
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
3424
 Field type longlong int (8 bytes)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3425 3426
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3427
int Field_longlong::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3428
{
3429
  int error;
3430
  char *end;
3431
  ulonglong tmp;
3432

3433 3434
  tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&error);
  if (error == MY_ERRNO_ERANGE)
3435
  {
3436
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3437 3438
    error= 1;
  }
3439 3440
  else if (table->in_use->count_cuted_fields && 
           check_int(cs, from, len, end, error))
3441 3442 3443
    error= 1;
  else
    error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3444
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3445
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3446 3447 3448 3449 3450 3451
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
3452
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3453 3454 3455
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3456
int Field_longlong::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3457
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3458
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3459
  longlong res;
3460 3461

  nr= rint(nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3462 3463 3464 3465 3466
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3467
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3468
    }
serg@serg.mylan's avatar
serg@serg.mylan committed
3469
    else if (nr >= (double) ULONGLONG_MAX)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3470 3471
    {
      res= ~(longlong) 0;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3472
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3473 3474 3475 3476 3477 3478
    }
    else
      res=(longlong) (ulonglong) nr;
  }
  else
  {
serg@serg.mylan's avatar
serg@serg.mylan committed
3479
    if (nr <= (double) LONGLONG_MIN)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3480
    {
3481
      res= LONGLONG_MIN;
serg@serg.mylan's avatar
serg@serg.mylan committed
3482
      error= (nr < (double) LONGLONG_MIN);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3483
    }
3484
    else if (nr >= (double) (ulonglong) LONGLONG_MAX)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3485
    {
3486
      res= LONGLONG_MAX;
serg@serg.mylan's avatar
serg@serg.mylan committed
3487
      error= (nr > (double) LONGLONG_MAX);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3488 3489 3490 3491
    }
    else
      res=(longlong) nr;
  }
3492 3493 3494
  if (error)
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3495
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3496
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3497 3498 3499 3500 3501 3502
  {
    int8store(ptr,res);
  }
  else
#endif
    longlongstore(ptr,res);
3503
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3504 3505 3506
}


3507
int Field_longlong::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3508
{
3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524
  int error= 0;

  if (nr < 0)                                   // Only possible error
  {
    /*
      if field is unsigned and value is signed (< 0) or
      if field is signed and value is unsigned we have an overflow
    */
    if (unsigned_flag != unsigned_val)
    {
      nr= unsigned_flag ? (ulonglong) 0 : (ulonglong) LONGLONG_MAX;
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
      error= 1;
    }
  }

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3525
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3526
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3527 3528 3529 3530 3531 3532
  {
    int8store(ptr,nr);
  }
  else
#endif
    longlongstore(ptr,nr);
3533
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3534 3535 3536 3537 3538 3539 3540
}


double Field_longlong::val_real(void)
{
  longlong j;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3541
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3542 3543 3544 3545 3546 3547
  {
    j=sint8korr(ptr);
  }
  else
#endif
    longlongget(j,ptr);
3548 3549 3550 3551 3552 3553 3554
  /* 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
3555 3556
}

3557

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3558 3559 3560 3561
longlong Field_longlong::val_int(void)
{
  longlong j;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3562
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573
    j=sint8korr(ptr);
  else
#endif
    longlongget(j,ptr);
  return j;
}


String *Field_longlong::val_str(String *val_buffer,
				String *val_ptr __attribute__((unused)))
{
3574
  CHARSET_INFO *cs= &my_charset_bin;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3575
  uint length;
3576 3577
  uint mlength=max(field_length+1,22*cs->mbmaxlen);
  val_buffer->alloc(mlength);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3578 3579 3580
  char *to=(char*) val_buffer->ptr();
  longlong j;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3581
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3582 3583 3584 3585 3586
    j=sint8korr(ptr);
  else
#endif
    longlongget(j,ptr);

3587
  length=(uint) (cs->cset->longlong10_to_str)(cs,to,mlength,
3588
					unsigned_flag ? 10 : -10, j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3589 3590 3591 3592 3593 3594 3595
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
  return val_buffer;
}


3596 3597 3598 3599 3600 3601
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
3602 3603 3604 3605
int Field_longlong::cmp(const char *a_ptr, const char *b_ptr)
{
  longlong a,b;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3606
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625
  {
    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
kent@mysql.com's avatar
kent@mysql.com committed
3626
  if (!table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3627 3628 3629 3630
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
3631
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645
    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
3646
      to[0] = (char) (ptr[7] ^ 128);		/* Revers signbit */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659
    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
{
3660
  CHARSET_INFO *cs=res.charset();
3661
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3662
			  "bigint(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3663 3664 3665
  add_zerofill_and_unsigned(res);
}

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3667
/****************************************************************************
monty@mysql.com's avatar
monty@mysql.com committed
3668
  single precision float
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3669 3670
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3671
int Field_float::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3672
{
monty@mysql.com's avatar
monty@mysql.com committed
3673
  int error;
3674
  char *end;
monty@mysql.com's avatar
monty@mysql.com committed
3675
  double nr= my_strntod(cs,(char*) from,len,&end,&error);
3676 3677
  if (error || (!len || (uint) (end-from) != len &&
                table->in_use->count_cuted_fields))
3678
  {
3679
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
3680
                (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3681
    error= error ? 1 : 2;
3682
  }
monty@mysql.com's avatar
monty@mysql.com committed
3683 3684
  Field_float::store(nr);
  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_float::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3689
{
3690
  int error= truncate(&nr, FLT_MAX);
3691
  float j= (float)nr;
3692

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3693
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3694
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3695 3696 3697 3698 3699 3700
  {
    float4store(ptr,j);
  }
  else
#endif
    memcpy_fixed(ptr,(byte*) &j,sizeof(j));
3701
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3702 3703 3704
}


3705
int Field_float::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3706
{
3707
  return store(unsigned_val ? ulonglong2double((ulonglong) nr) : (double) nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3708 3709
}

3710

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3711 3712 3713 3714
double Field_float::val_real(void)
{
  float j;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3715
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728
  {
    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
kent@mysql.com's avatar
kent@mysql.com committed
3729
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3730 3731 3732 3733 3734 3735
  {
    float4get(j,ptr);
  }
  else
#endif
    memcpy_fixed((byte*) &j,ptr,sizeof(j));
3736
  return (longlong) rint(j);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3737 3738 3739 3740 3741 3742 3743 3744
}


String *Field_float::val_str(String *val_buffer,
			     String *val_ptr __attribute__((unused)))
{
  float nr;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3745
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3746 3747 3748 3749 3750 3751 3752
  {
    float4get(nr,ptr);
  }
  else
#endif
    memcpy_fixed((byte*) &nr,ptr,sizeof(nr));

3753 3754
  uint to_length=max(field_length,70);
  val_buffer->alloc(to_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 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
  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++;
3803
#else
3804 3805 3806
#ifdef HAVE_SNPRINTF
    to[to_length-1]=0;			// Safety
    snprintf(to,to_length-1,"%.*f",dec,nr);
3807
    to=strend(to);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3808
#else
3809
    to+= my_sprintf(to,(to,"%.*f",dec,nr));
3810
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826
#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
kent@mysql.com's avatar
kent@mysql.com committed
3827
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846
  {
    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
kent@mysql.com's avatar
kent@mysql.com committed
3847
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871
  {
    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++)
3872
	tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885
    }
    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;
    }
  }
}


3886 3887 3888 3889 3890 3891
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
3892 3893 3894
void Field_float::sql_type(String &res) const
{
  if (dec == NOT_FIXED_DEC)
3895
  {
3896
    res.set_ascii(STRING_WITH_LEN("float"));
3897
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3898
  else
3899
  {
3900
    CHARSET_INFO *cs= res.charset();
3901
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3902
			    "float(%d,%d)",(int) field_length,dec));
3903
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3904 3905 3906
  add_zerofill_and_unsigned(res);
}

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3908
/****************************************************************************
monty@mysql.com's avatar
monty@mysql.com committed
3909
  double precision floating point numbers
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3910 3911
****************************************************************************/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3912
int Field_double::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3913
{
monty@mysql.com's avatar
monty@mysql.com committed
3914
  int error;
3915
  char *end;
monty@mysql.com's avatar
monty@mysql.com committed
3916
  double nr= my_strntod(cs,(char*) from, len, &end, &error);
3917 3918
  if (error || (!len || (uint) (end-from) != len &&
                table->in_use->count_cuted_fields))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3919
  {
3920
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
3921
                (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3922
    error= error ? 1 : 2;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3923
  }
monty@mysql.com's avatar
monty@mysql.com committed
3924 3925
  Field_double::store(nr);
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3926 3927 3928
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
3929
int Field_double::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3930
{
3931
  int error= truncate(&nr, DBL_MAX);
3932

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3933
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
3934
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3935 3936 3937 3938 3939 3940
  {
    float8store(ptr,nr);
  }
  else
#endif
    doublestore(ptr,nr);
3941
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3942 3943 3944
}


3945
int Field_double::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3946
{
3947
  return store(unsigned_val ? ulonglong2double((ulonglong) nr) : (double) nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
3948 3949
}

3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006
/*
  If a field has fixed length, truncate the double argument pointed to by 'nr'
  appropriately.
  Also ensure that the argument is within [-max_value; max_value] range.
*/

int Field_real::truncate(double *nr, double max_value)
{
  int error= 1;
  double res= *nr;
  
  if (isnan(res))
  {
    res= 0;
    set_null();
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
    goto end;
  }
  else if (unsigned_flag && res < 0)
  {
    res= 0;
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
    goto end;
  }

  if (!not_fixed)
  {
    uint order= field_length - dec;
    uint step= array_elements(log_10) - 1;
    max_value= 1.0;
    for (; order > step; order-= step)
      max_value*= log_10[step];
    max_value*= log_10[order];
    max_value-= 1.0 / log_10[dec];

    double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
    res= floor(res) + tmp;
  }
  
  if (res < -max_value)
  {
   res= -max_value;
   set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
  }
  else if (res > max_value)
  {
    res= max_value;
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
  }
  else
    error= 0;

end:
  *nr= res;
  return error;
}

4007

4008 4009 4010 4011 4012 4013
int Field_real::store_decimal(const my_decimal *dm)
{
  double dbl;
  my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
  return store(dbl);
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4014 4015 4016 4017 4018

double Field_double::val_real(void)
{
  double j;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
4019
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031
  {
    float8get(j,ptr);
  }
  else
#endif
    doubleget(j,ptr);
  return j;
}

longlong Field_double::val_int(void)
{
  double j;
bar@mysql.com's avatar
bar@mysql.com committed
4032
  longlong res;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4033
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
4034
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4035 4036 4037 4038 4039 4040
  {
    float8get(j,ptr);
  }
  else
#endif
    doubleget(j,ptr);
4041 4042
  /* Check whether we fit into longlong range */
  if (j <= (double) LONGLONG_MIN)
bar@mysql.com's avatar
bar@mysql.com committed
4043 4044 4045 4046
  {
    res= (longlong) LONGLONG_MIN;
    goto warn;
  }
4047
  if (j >= (double) (ulonglong) LONGLONG_MAX)
bar@mysql.com's avatar
bar@mysql.com committed
4048 4049 4050 4051
  {
    res= (longlong) LONGLONG_MAX;
    goto warn;
  }
4052
  return (longlong) rint(j);
bar@mysql.com's avatar
bar@mysql.com committed
4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064

warn:
  {
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
    String tmp(buf, sizeof(buf), &my_charset_latin1), *str;
    str= val_str(&tmp, 0);
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_TRUNCATED_WRONG_VALUE,
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
                        str->c_ptr());
  }
  return res;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4065 4066 4067
}


4068 4069 4070 4071 4072 4073 4074
my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
{
  double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
  return decimal_value;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
4075 4076 4077 4078 4079
String *Field_double::val_str(String *val_buffer,
			      String *val_ptr __attribute__((unused)))
{
  double nr;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
4080
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4081 4082 4083 4084 4085 4086 4087
  {
    float8get(nr,ptr);
  }
  else
#endif
    doubleget(nr,ptr);

4088
  uint to_length=max(field_length, DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099
  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
4100
    char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
4101
    char *pos= buff;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
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
    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
4140
    to[to_length-1]=0;			// Safety
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
4141
    snprintf(to,to_length-1,"%.*f",dec,nr);
4142
    to=strend(to);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4143
#else
4144
    to+= my_sprintf(to,(to,"%.*f",dec,nr));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157
#endif
#endif
  }
#ifdef HAVE_FCONVERT
 end:
#endif

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

4158 4159
bool Field_double::send_binary(Protocol *protocol)
{
4160
  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
4161 4162
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4163 4164 4165 4166 4167

int Field_double::cmp(const char *a_ptr, const char *b_ptr)
{
  double a,b;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
4168
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4169 4170 4171 4172 4173 4174 4175
  {
    float8get(a,a_ptr);
    float8get(b,b_ptr);
  }
  else
#endif
  {
4176 4177
    doubleget(a, a_ptr);
    doubleget(b, b_ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190
  }
  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
kent@mysql.com's avatar
kent@mysql.com committed
4191
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4192 4193 4194 4195 4196
  {
    float8get(nr,ptr);
  }
  else
#endif
4197
    doubleget(nr,ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4198 4199 4200 4201 4202 4203
  change_double_for_sort(nr, (byte*) to);
}


void Field_double::sql_type(String &res) const
{
4204
  CHARSET_INFO *cs=res.charset();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4205
  if (dec == NOT_FIXED_DEC)
4206
  {
4207
    res.set_ascii(STRING_WITH_LEN("double"));
4208
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4209
  else
4210
  {
4211
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4212
			    "double(%d,%d)",(int) field_length,dec));
4213
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4214 4215 4216 4217
  add_zerofill_and_unsigned(res);
}


4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241
/*
  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 
4242
    field which has 0 as default value and is not automatically updated.
4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261
  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
4262 4263

Field_timestamp::Field_timestamp(char *ptr_arg, uint32 len_arg,
4264
                                 uchar *null_ptr_arg, uchar null_bit_arg,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4265 4266
				 enum utype unireg_check_arg,
				 const char *field_name_arg,
4267 4268
				 struct st_table *table_arg,
				 CHARSET_INFO *cs)
4269
  :Field_str(ptr_arg, MAX_DATETIME_WIDTH, null_ptr_arg, null_bit_arg,
4270
	     unireg_check_arg, field_name_arg, table_arg, cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4271
{
4272 4273
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
4274 4275
  if (table && !table->timestamp_field && 
      unireg_check != NONE)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4276
  {
4277 4278
    /* This timestamp has auto-update */
    table->timestamp_field= this;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4279 4280 4281 4282 4283
    flags|=TIMESTAMP_FLAG;
  }
}


4284 4285 4286
Field_timestamp::Field_timestamp(bool maybe_null_arg,
                                 const char *field_name_arg,
                                 struct st_table *table_arg, CHARSET_INFO *cs)
4287 4288
  :Field_str((char*) 0, MAX_DATETIME_WIDTH,
             maybe_null_arg ? (uchar*) "": 0, 0,
4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302
	     NONE, field_name_arg, table_arg, cs)
{
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
  if (table && !table->timestamp_field &&
      unireg_check != NONE)
  {
    /* This timestamp has auto-update */
    table->timestamp_field= this;
    flags|=TIMESTAMP_FLAG;
  }
}


4303
/*
4304 4305
  Get auto-set type for TIMESTAMP field.

4306
  SYNOPSIS
4307 4308 4309 4310 4311
    get_auto_set_type()

  DESCRIPTION
    Returns value indicating during which operations this TIMESTAMP field
    should be auto-set to current timestamp.
4312
*/
4313
timestamp_auto_set_type Field_timestamp::get_auto_set_type() const
4314
{
4315 4316 4317 4318 4319 4320
  switch (unireg_check)
  {
  case TIMESTAMP_DN_FIELD:
    return TIMESTAMP_AUTO_SET_ON_INSERT;
  case TIMESTAMP_UN_FIELD:
    return TIMESTAMP_AUTO_SET_ON_UPDATE;
4321 4322
  case TIMESTAMP_OLD_FIELD:
    /*
4323
      Although we can have several such columns in legacy tables this
4324 4325 4326 4327 4328
      function should be called only for first of them (i.e. the one
      having auto-set property).
    */
    DBUG_ASSERT(table->timestamp_field == this);
    /* Fall-through */
4329 4330 4331 4332 4333 4334 4335 4336 4337 4338
  case TIMESTAMP_DNUN_FIELD:
    return TIMESTAMP_AUTO_SET_ON_BOTH;
  default:
    /*
      Normally this function should not be called for TIMESTAMPs without
      auto-set property.
    */
    DBUG_ASSERT(0);
    return TIMESTAMP_NO_AUTO_SET;
  }
4339 4340 4341
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4342
int Field_timestamp::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4343
{
4344 4345
  
  MYSQL_TIME l_time;
4346 4347 4348
  my_time_t tmp= 0;
  int error;
  bool have_smth_to_conv;
4349
  my_bool in_dst_time_gap;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4350
  THD *thd= table ? table->in_use : current_thd;
4351

4352
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
4353
  have_smth_to_conv= (str_to_datetime(from, len, &l_time,
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4354
                                      (thd->variables.sql_mode &
4355 4356
                                       MODE_NO_ZERO_DATE) |
                                      MODE_NO_ZERO_IN_DATE, &error) >
4357
                      MYSQL_TIMESTAMP_ERROR);
4358

4359 4360 4361
  if (error || !have_smth_to_conv)
  {
    error= 1;
4362
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
4363
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
4364
  }
4365

4366 4367
  /* Only convert a correct date (not a zero date) */
  if (have_smth_to_conv && l_time.month)
4368 4369 4370
  {
    if (!(tmp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
    {
4371
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4372
                           ER_WARN_DATA_OUT_OF_RANGE,
4373
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
4374 4375 4376 4377 4378
      error= 1;
    }
    else if (in_dst_time_gap)
    {
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4379
                           ER_WARN_INVALID_TIMESTAMP,
4380
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
4381 4382 4383 4384
      error= 1;
    }
  }

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4385
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4386
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4387 4388 4389 4390 4391 4392
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
4393
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4394 4395
}

4396

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4397
int Field_timestamp::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4398
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4399
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4400 4401
  if (nr < 0 || nr > 99991231235959.0)
  {
4402 4403
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
                         ER_WARN_DATA_OUT_OF_RANGE,
4404
                         nr, MYSQL_TIMESTAMP_DATETIME);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4405 4406
    nr= 0;					// Avoid overflow on buff
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4407
  }
4408
  error|= Field_timestamp::store((longlong) rint(nr), FALSE);
4409
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4410 4411 4412
}


4413
int Field_timestamp::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4414
{
4415
  MYSQL_TIME l_time;
4416 4417
  my_time_t timestamp= 0;
  int error;
4418
  my_bool in_dst_time_gap;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4419
  THD *thd= table ? table->in_use : current_thd;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4420

4421
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
4422 4423 4424
  longlong tmp= number_to_datetime(nr, &l_time, (thd->variables.sql_mode &
                                                 MODE_NO_ZERO_DATE) |
                                   MODE_NO_ZERO_IN_DATE, &error);
4425
  if (tmp == LL(-1))
4426 4427 4428 4429 4430
  {
    error= 2;
  }

  if (!error && tmp)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4431
  {
4432 4433
    if (!(timestamp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
    {
4434 4435 4436
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
                           ER_WARN_DATA_OUT_OF_RANGE,
                           nr, MYSQL_TIMESTAMP_DATETIME, 1);
4437 4438 4439 4440 4441
      error= 1;
    }
    if (in_dst_time_gap)
    {
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4442 4443
                           ER_WARN_INVALID_TIMESTAMP,
                           nr, MYSQL_TIMESTAMP_DATETIME, 1);
4444 4445
      error= 1;
    }
4446 4447
  } else if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4448
                         WARN_DATA_TRUNCATED,
4449
                         nr, MYSQL_TIMESTAMP_DATETIME, 1);
4450

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4451
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4452
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4453 4454 4455 4456 4457
  {
    int4store(ptr,timestamp);
  }
  else
#endif
4458
    longstore(ptr,(uint32) timestamp);
4459

monty@mysql.com's avatar
monty@mysql.com committed
4460
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471
}


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

longlong Field_timestamp::val_int(void)
{
  uint32 temp;
4472
  MYSQL_TIME time_tmp;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4473
  THD  *thd= table ? table->in_use : current_thd;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4474

4475
  thd->time_zone_used= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4476
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4477
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4478 4479 4480 4481 4482 4483 4484
    temp=uint4korr(ptr);
  else
#endif
    longget(temp,ptr);

  if (temp == 0L)				// No time
    return(0);					/* purecov: inspected */
4485 4486 4487 4488 4489 4490
  
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp, (my_time_t)temp);
  
  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
4491 4492 4493
}


serg@serg.mylan's avatar
serg@serg.mylan committed
4494
String *Field_timestamp::val_str(String *val_buffer, String *val_ptr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4495
{
4496
  uint32 temp, temp2;
4497
  MYSQL_TIME time_tmp;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4498
  THD *thd= table ? table->in_use : current_thd;
monty@mysql.com's avatar
monty@mysql.com committed
4499
  char *to;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4500 4501

  val_buffer->alloc(field_length+1);
monty@mysql.com's avatar
monty@mysql.com committed
4502
  to= (char*) val_buffer->ptr();
4503
  val_buffer->length(field_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4504

4505
  thd->time_zone_used= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4506
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4507
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4508 4509 4510 4511 4512 4513 4514
    temp=uint4korr(ptr);
  else
#endif
    longget(temp,ptr);

  if (temp == 0L)
  {				      /* Zero time is "000000" */
4515
    val_ptr->set(STRING_WITH_LEN("0000-00-00 00:00:00"), &my_charset_bin);
4516
    return val_ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4517
  }
4518
  val_buffer->set_charset(&my_charset_bin);	// Safety
4519 4520
  
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp,(my_time_t)temp);
4521

4522
  temp= time_tmp.year % 100;
4523
  if (temp < YY_PART_YEAR - 1)
4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536
  {
    *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++= '-';
4537
  temp=time_tmp.month;
4538 4539 4540 4541
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= '-';
4542
  temp=time_tmp.day;
4543 4544 4545 4546
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ' ';
4547
  temp=time_tmp.hour;
4548 4549 4550 4551
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ':';
4552
  temp=time_tmp.minute;
4553 4554 4555 4556
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ':';
4557
  temp=time_tmp.second;
4558 4559 4560 4561
  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
4562 4563 4564
  return val_buffer;
}

4565

4566
bool Field_timestamp::get_date(MYSQL_TIME *ltime, uint fuzzydate)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4567 4568
{
  long temp;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4569
  THD *thd= table ? table->in_use : current_thd;
4570
  thd->time_zone_used= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4571
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4572
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4573 4574 4575 4576 4577 4578
    temp=uint4korr(ptr);
  else
#endif
    longget(temp,ptr);
  if (temp == 0L)
  {				      /* Zero time is "000000" */
4579
    if (fuzzydate & TIME_NO_ZERO_DATE)
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4580
      return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4581 4582 4583 4584
    bzero((char*) ltime,sizeof(*ltime));
  }
  else
  {
4585
    thd->variables.time_zone->gmt_sec_to_TIME(ltime, (my_time_t)temp);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4586 4587 4588 4589
  }
  return 0;
}

4590
bool Field_timestamp::get_time(MYSQL_TIME *ltime)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4591
{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
4592
  return Field_timestamp::get_date(ltime,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4593 4594
}

4595 4596 4597

bool Field_timestamp::send_binary(Protocol *protocol)
{
4598
  MYSQL_TIME tm;
4599
  Field_timestamp::get_date(&tm, 0);
4600 4601 4602 4603
  return protocol->store(&tm);
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
4604 4605 4606 4607
int Field_timestamp::cmp(const char *a_ptr, const char *b_ptr)
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4608
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621
  {
    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;
}

4622

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4623 4624 4625
void Field_timestamp::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4626
  if (!table || !table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645
  {
    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
{
4646
  res.set_ascii(STRING_WITH_LEN("timestamp"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4647 4648 4649 4650 4651
}


void Field_timestamp::set_time()
{
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4652 4653
  THD *thd= table ? table->in_use : current_thd;
  long tmp= (long) thd->query_start();
4654
  set_notnull();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4655
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4656
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671
  {
    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
4672
int Field_time::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4673
{
4674
  MYSQL_TIME ltime;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4675
  long tmp;
4676 4677
  int error= 0;
  int warning;
4678

4679
  if (str_to_time(from, len, &ltime, &warning))
4680
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4681
    tmp=0L;
4682
    error= 2;
4683
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
4684
                         from, len, MYSQL_TIMESTAMP_TIME, 1);
4685
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4686 4687
  else
  {
4688
    if (warning & MYSQL_TIME_WARN_TRUNCATED)
4689 4690
    {
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4691
                           WARN_DATA_TRUNCATED,
4692
                           from, len, MYSQL_TIMESTAMP_TIME, 1);
4693 4694
      error= 1;
    }
4695
    if (warning & MYSQL_TIME_WARN_OUT_OF_RANGE)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4696
    {
4697 4698
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                           ER_WARN_DATA_OUT_OF_RANGE,
4699
                           from, len, MYSQL_TIMESTAMP_TIME, !error);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4700
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4701
    }
4702 4703 4704
    if (ltime.month)
      ltime.day=0;
    tmp=(ltime.day*24L+ltime.hour)*10000L+(ltime.minute*100+ltime.second);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4705
  }
4706
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4707 4708
  if (ltime.neg)
    tmp= -tmp;
4709
  int3store(ptr,tmp);
4710
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4711 4712 4713
}


4714
int Field_time::store_time(MYSQL_TIME *ltime, timestamp_type time_type)
4715 4716 4717 4718 4719
{
  long tmp= ((ltime->month ? 0 : ltime->day * 24L) + ltime->hour) * 10000L +
            (ltime->minute * 100 + ltime->second);
  if (ltime->neg)
    tmp= -tmp;
4720
  return Field_time::store((longlong) tmp, FALSE);
4721 4722 4723
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4724
int Field_time::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4725 4726
{
  long tmp;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4727
  int error= 0;
4728
  if (nr > (double)TIME_MAX_VALUE)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4729
  {
4730
    tmp= TIME_MAX_VALUE;
4731
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4732
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4733
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4734
  }
4735
  else if (nr < (double)-TIME_MAX_VALUE)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4736
  {
4737
    tmp= -TIME_MAX_VALUE;
4738
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4739
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4740
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4741 4742 4743 4744 4745 4746 4747 4748 4749
  }
  else
  {
    tmp=(long) floor(fabs(nr));			// Remove fractions
    if (nr < 0)
      tmp= -tmp;
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
    {
      tmp=0;
4750
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4751 4752
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
                           MYSQL_TIMESTAMP_TIME);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4753
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4754 4755 4756
    }
  }
  int3store(ptr,tmp);
4757
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4758 4759 4760
}


4761
int Field_time::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4762 4763
{
  long tmp;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4764
  int error= 0;
4765
  if (nr < (longlong) -TIME_MAX_VALUE && !unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4766
  {
4767
    tmp= -TIME_MAX_VALUE;
4768
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4769 4770
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
                         MYSQL_TIMESTAMP_TIME, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4771
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4772
  }
4773
  else if (nr > (longlong) TIME_MAX_VALUE || nr < 0 && unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4774
  {
4775
    tmp= TIME_MAX_VALUE;
4776
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4777 4778
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
                         MYSQL_TIMESTAMP_TIME, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4779
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4780 4781 4782 4783 4784 4785 4786
  }
  else
  {
    tmp=(long) nr;
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
    {
      tmp=0;
4787
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4788 4789
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
                           MYSQL_TIMESTAMP_TIME, 1);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4790
      error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4791 4792 4793
    }
  }
  int3store(ptr,tmp);
4794
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4795 4796 4797 4798 4799
}


double Field_time::val_real(void)
{
4800
  uint32 j= (uint32) uint3korr(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4801 4802 4803 4804 4805 4806 4807 4808
  return (double) j;
}

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

4809 4810 4811 4812 4813 4814

/*
  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
4815 4816 4817
String *Field_time::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
4818
  MYSQL_TIME ltime;
4819
  val_buffer->alloc(MAX_DATE_STRING_REP_LENGTH);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4820
  long tmp=(long) sint3korr(ptr);
4821
  ltime.neg= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4822 4823 4824
  if (tmp < 0)
  {
    tmp= -tmp;
4825 4826 4827 4828 4829 4830
    ltime.neg= 1;
  }
  ltime.day= (uint) 0;
  ltime.hour= (uint) (tmp/10000);
  ltime.minute= (uint) (tmp/100 % 100);
  ltime.second= (uint) (tmp % 100);
4831
  make_time((DATE_TIME_FORMAT*) 0, &ltime, val_buffer);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4832 4833 4834
  return val_buffer;
}

4835

monty@mysql.com's avatar
monty@mysql.com committed
4836
/*
4837
  Normally we would not consider 'time' as a valid date, but we allow
monty@mysql.com's avatar
monty@mysql.com committed
4838 4839 4840 4841
  get_date() here to be able to do things like
  DATE_FORMAT(time, "%l.%i %p")
*/
 
4842
bool Field_time::get_date(MYSQL_TIME *ltime, uint fuzzydate)
4843
{
monty@mysql.com's avatar
monty@mysql.com committed
4844
  long tmp;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4845
  THD *thd= table ? table->in_use : current_thd;
4846
  if (!(fuzzydate & TIME_FUZZY_DATE))
monty@mysql.com's avatar
monty@mysql.com committed
4847
  {
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4848
    push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
4849 4850
                        ER_WARN_DATA_OUT_OF_RANGE,
                        ER(ER_WARN_DATA_OUT_OF_RANGE), field_name,
evgen@moonbone.local's avatar
evgen@moonbone.local committed
4851
                        thd->row_count);
monty@mysql.com's avatar
monty@mysql.com committed
4852 4853 4854
    return 1;
  }
  tmp=(long) sint3korr(ptr);
4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868
  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
4869

4870
bool Field_time::get_time(MYSQL_TIME *ltime)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4871 4872 4873 4874 4875 4876 4877 4878
{
  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
4879
  ltime->day= 0;
4880
  ltime->hour=   (int) (tmp/10000);
4881
  tmp-=ltime->hour*10000;
4882 4883
  ltime->minute= (int) tmp/100;
  ltime->second= (int) tmp % 100;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4884
  ltime->second_part=0;
4885
  ltime->time_type= MYSQL_TIMESTAMP_TIME;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4886 4887 4888
  return 0;
}

4889 4890 4891

bool Field_time::send_binary(Protocol *protocol)
{
4892
  MYSQL_TIME tm;
4893
  Field_time::get_time(&tm);
4894 4895
  tm.day= tm.hour/24;				// Move hours to days
  tm.hour-= tm.day*24;
4896
  return protocol->store_time(&tm);
4897 4898 4899
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
4900 4901
int Field_time::cmp(const char *a_ptr, const char *b_ptr)
{
4902 4903 4904
  int32 a,b;
  a=(int32) sint3korr(a_ptr);
  b=(int32) sint3korr(b_ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916
  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
{
4917
  res.set_ascii(STRING_WITH_LEN("time"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4918 4919 4920 4921 4922 4923 4924 4925
}

/****************************************************************************
** 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
4926
int Field_year::store(const char *from, uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4927
{
4928
  char *end;
4929
  int error;
4930
  longlong nr= cs->cset->strntoull10rnd(cs, from, len, 0, &end, &error);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4931

4932 4933
  if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155 || 
      error == MY_ERRNO_ERANGE)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4934 4935
  {
    *ptr=0;
4936
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4937
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4938
  }
4939 4940 4941 4942 4943 4944 4945 4946
  if (table->in_use->count_cuted_fields && 
      (error= check_int(cs, from, len, end, error)))
  {
    if (error == 1)  /* empty or incorrect string */
    {
      *ptr= 0;
      return 1;
    }
4947
    error= 1;
4948
  }
4949

bk@work.mysql.com's avatar
bk@work.mysql.com committed
4950 4951 4952 4953 4954 4955 4956 4957
  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;
4958
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4959 4960
}

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

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
4962
int Field_year::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4963 4964
{
  if (nr < 0.0 || nr >= 2155.0)
4965
  {
4966
    (void) Field_year::store((longlong) -1, FALSE);
4967 4968
    return 1;
  }
4969
  return Field_year::store((longlong) nr, FALSE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4970 4971
}

4972 4973

int Field_year::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4974 4975 4976
{
  if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155)
  {
4977
    *ptr= 0;
4978
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4979
    return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4980 4981 4982 4983 4984 4985 4986 4987 4988
  }
  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;
4989
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4990 4991
}

4992

4993 4994 4995
bool Field_year::send_binary(Protocol *protocol)
{
  ulonglong tmp= Field_year::val_int();
4996
  return protocol->store_short(tmp);
4997
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
4998

4999

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5000 5001 5002 5003 5004
double Field_year::val_real(void)
{
  return (double) Field_year::val_int();
}

5005

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5006 5007 5008 5009 5010 5011 5012 5013 5014 5015
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;
}

5016

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5017 5018 5019 5020 5021 5022 5023 5024 5025 5026
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;
}

5027

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5028 5029
void Field_year::sql_type(String &res) const
{
5030
  CHARSET_INFO *cs=res.charset();
5031
  res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(),
5032
			  "year(%d)",(int) field_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5033 5034 5035 5036 5037 5038 5039 5040 5041 5042
}


/****************************************************************************
** 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
5043
int Field_date::store(const char *from, uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5044
{
5045
  MYSQL_TIME l_time;
5046
  uint32 tmp;
5047
  int error;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5048
  THD *thd= table ? table->in_use : current_thd;
5049

5050
  if (str_to_datetime(from, len, &l_time, TIME_FUZZY_DATE |
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5051
                      (thd->variables.sql_mode &
5052 5053 5054
                       (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
                        MODE_INVALID_DATES)),
                      &error) <= MYSQL_TIMESTAMP_ERROR)
5055
  {
5056
    tmp= 0;
5057
    error= 2;
5058
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5059
  else
5060
    tmp=(uint32) l_time.year*10000L + (uint32) (l_time.month*100+l_time.day);
5061 5062

  if (error)
5063
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
5064
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
5065

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5066
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5067
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5068 5069 5070 5071 5072 5073
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
5074
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5075 5076 5077
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5078
int Field_date::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5079
{
5080
  longlong tmp;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5081
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5082 5083 5084 5085
  if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
    nr=floor(nr/1000000.0);			// Timestamp to date
  if (nr < 0.0 || nr > 99991231.0)
  {
5086
    tmp= LL(0);
5087 5088
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
                         ER_WARN_DATA_OUT_OF_RANGE,
5089
                         nr, MYSQL_TIMESTAMP_DATE);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5090
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5091 5092
  }
  else
5093
    tmp= (longlong) rint(nr);
5094

5095
  return Field_date::store(tmp, TRUE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5096 5097 5098
}


5099
int Field_date::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5100
{
5101
  MYSQL_TIME not_used;
5102 5103
  int error;
  longlong initial_nr= nr;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5104
  THD *thd= table ? table->in_use : current_thd;
5105 5106

  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5107
                                         (thd->variables.sql_mode &
5108 5109 5110 5111
                                          (MODE_NO_ZERO_IN_DATE |
                                           MODE_NO_ZERO_DATE |
                                           MODE_INVALID_DATES))), &error);

5112
  if (nr == LL(-1))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5113
  {
5114 5115
    nr= 0;
    error= 2;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5116
  }
5117

5118
  if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
5119
    nr= (longlong) floor(nr/1000000.0);         // Timestamp to date
5120

5121 5122 5123 5124 5125
  if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
                         error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
                         WARN_DATA_TRUNCATED, initial_nr,
                         MYSQL_TIMESTAMP_DATETIME, 1);
5126

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5127
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5128
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5129
  {
5130
    int4store(ptr, nr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5131 5132 5133
  }
  else
#endif
5134
    longstore(ptr, nr);
5135
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5136 5137 5138
}


5139 5140 5141
bool Field_date::send_binary(Protocol *protocol)
{
  longlong tmp= Field_date::val_int();
5142
  MYSQL_TIME tm;
5143 5144 5145 5146 5147 5148
  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
5149 5150 5151 5152 5153

double Field_date::val_real(void)
{
  int32 j;
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5154
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5155 5156 5157 5158 5159 5160 5161
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);
  return (double) (uint32) j;
}

5162

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5163 5164 5165 5166
longlong Field_date::val_int(void)
{
  int32 j;
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5167
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5168 5169 5170 5171 5172 5173 5174
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);
  return (longlong) (uint32) j;
}

5175

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5176 5177 5178
String *Field_date::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
5179
  MYSQL_TIME ltime;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5180 5181 5182
  val_buffer->alloc(field_length);
  int32 tmp;
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5183
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5184 5185 5186 5187
    tmp=sint4korr(ptr);
  else
#endif
    longget(tmp,ptr);
5188 5189 5190 5191
  ltime.neg= 0;
  ltime.year= (int) ((uint32) tmp/10000L % 10000);
  ltime.month= (int) ((uint32) tmp/100 % 100);
  ltime.day= (int) ((uint32) tmp % 100);
5192
  make_date((DATE_TIME_FORMAT *) 0, &ltime, val_buffer);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5193 5194 5195
  return val_buffer;
}

5196

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5197 5198 5199 5200
int Field_date::cmp(const char *a_ptr, const char *b_ptr)
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5201
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218
  {
    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
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5219
  if (!table || !table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237
  {
    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
{
5238
  res.set_ascii(STRING_WITH_LEN("date"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5239 5240
}

5241

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5242 5243 5244 5245 5246 5247
/****************************************************************************
** The new date type
** This is identical to the old date type, but stored on 3 bytes instead of 4
** In number context: YYYYMMDD
****************************************************************************/

5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261
/*
  Store string into a date field

  SYNOPSIS
    Field_newdate::store()
    from                Date string
    len                 Length of date field
    cs                  Character set (not used)

  RETURN
    0  ok
    1  Value was cut during conversion
    2  Wrong date string
    3  Datetime value that was cut (warning level NOTE)
5262 5263 5264
       This is used by opt_range.cc:get_mm_leaf(). Note that there is a
       nearly-identical class Field_date doesn't ever return 3 from its
       store function.
5265 5266
*/

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5267
int Field_newdate::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5268
{
5269 5270
  long tmp;
  MYSQL_TIME l_time;
5271
  int error;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5272
  THD *thd= table ? table->in_use : current_thd;
5273 5274 5275 5276 5277 5278 5279 5280
  enum enum_mysql_timestamp_type ret;
  if ((ret= str_to_datetime(from, len, &l_time,
                            (TIME_FUZZY_DATE |
                             (thd->variables.sql_mode &
                              (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
                               MODE_INVALID_DATES))),
                            &error)) <= MYSQL_TIMESTAMP_ERROR)
  {
5281
    tmp= 0;
5282
    error= 2;
5283
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5284
  else
5285
  {
5286
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
5287
    if (!error && (ret != MYSQL_TIMESTAMP_DATE) &&
5288
        (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
5289
      error= 3;                                 // Datetime was cut (note)
5290
  }
5291 5292

  if (error)
5293 5294 5295
    set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
                         MYSQL_ERROR::WARN_LEVEL_WARN,
                         WARN_DATA_TRUNCATED,
5296
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
5297

5298
  int3store(ptr, tmp);
5299
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5300 5301
}

5302

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5303
int Field_newdate::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5304 5305
{
  if (nr < 0.0 || nr > 99991231235959.0)
5306
  {
5307
    int3store(ptr,(int32) 0);
5308
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5309
                         WARN_DATA_TRUNCATED, nr, MYSQL_TIMESTAMP_DATE);
5310 5311
    return 1;
  }
5312
  return Field_newdate::store((longlong) rint(nr), FALSE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5313 5314 5315
}


5316
int Field_newdate::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5317
{
5318
  MYSQL_TIME l_time;
5319
  longlong tmp;
5320
  int error;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5321
  THD *thd= table ? table->in_use : current_thd;
5322 5323
  if (number_to_datetime(nr, &l_time,
                         (TIME_FUZZY_DATE |
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5324
                          (thd->variables.sql_mode &
5325 5326 5327
                           (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
                            MODE_INVALID_DATES))),
                         &error) == LL(-1))
5328 5329 5330
  {
    tmp= 0L;
    error= 2;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5331 5332
  }
  else
5333
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
5334

5335 5336 5337 5338
  if (!error && l_time.time_type != MYSQL_TIMESTAMP_DATE &&
      (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
    error= 3;

5339
  if (error)
5340 5341 5342 5343 5344
    set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
                         MYSQL_ERROR::WARN_LEVEL_WARN,
                         error == 2 ? 
                         ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED,
                         nr,MYSQL_TIMESTAMP_DATE, 1);
5345 5346

  int3store(ptr,tmp);
5347
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5348 5349
}

5350

5351
int Field_newdate::store_time(MYSQL_TIME *ltime, timestamp_type time_type)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5352 5353
{
  long tmp;
5354
  int error= 0;
5355 5356
  if (time_type == MYSQL_TIMESTAMP_DATE ||
      time_type == MYSQL_TIMESTAMP_DATETIME)
5357
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5358
    tmp=ltime->year*16*32+ltime->month*32+ltime->day;
5359 5360 5361 5362 5363
    if (check_date(ltime, tmp != 0,
                   (TIME_FUZZY_DATE |
                    (current_thd->variables.sql_mode &
                     (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
                      MODE_INVALID_DATES))), &error))
5364
    {
5365
      char buff[MAX_DATE_STRING_REP_LENGTH];
5366 5367 5368 5369 5370
      String str(buff, sizeof(buff), &my_charset_latin1);
      make_date((DATE_TIME_FORMAT *) 0, ltime, &str);
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATE, 1);
    }
5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381
    if (!error && ltime->time_type != MYSQL_TIMESTAMP_DATE &&
        (ltime->hour || ltime->minute || ltime->second || ltime->second_part))
    {
      char buff[MAX_DATE_STRING_REP_LENGTH];
      String str(buff, sizeof(buff), &my_charset_latin1);
      make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_NOTE,
                           WARN_DATA_TRUNCATED,
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATE, 1);
      error= 3;
    }
5382
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5383 5384 5385
  else
  {
    tmp=0;
5386
    error= 1;
5387
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5388 5389
  }
  int3store(ptr,tmp);
5390
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5391 5392
}

5393

5394 5395
bool Field_newdate::send_binary(Protocol *protocol)
{
5396
  MYSQL_TIME tm;
5397 5398 5399
  Field_newdate::get_date(&tm,0);
  return protocol->store_date(&tm);
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5400

5401

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5402 5403 5404 5405 5406
double Field_newdate::val_real(void)
{
  return (double) Field_newdate::val_int();
}

5407

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5408 5409
longlong Field_newdate::val_int(void)
{
5410
  ulong j= uint3korr(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5411 5412 5413 5414
  j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
  return (longlong) j;
}

5415

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5416 5417 5418 5419 5420
String *Field_newdate::val_str(String *val_buffer,
			       String *val_ptr __attribute__((unused)))
{
  val_buffer->alloc(field_length);
  val_buffer->length(field_length);
5421
  uint32 tmp=(uint32) uint3korr(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5422 5423 5424 5425
  int part;
  char *pos=(char*) val_buffer->ptr()+10;

  /* Open coded to get more speed */
5426
  *pos--=0;					// End NULL
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5427
  part=(int) (tmp & 31);
5428 5429 5430
  *pos--= (char) ('0'+part%10);
  *pos--= (char) ('0'+part/10);
  *pos--= '-';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5431
  part=(int) (tmp >> 5 & 15);
5432 5433 5434
  *pos--= (char) ('0'+part%10);
  *pos--= (char) ('0'+part/10);
  *pos--= '-';
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5435
  part=(int) (tmp >> 9);
5436 5437 5438 5439
  *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
5440 5441 5442
  return val_buffer;
}

5443

5444
bool Field_newdate::get_date(MYSQL_TIME *ltime,uint fuzzydate)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5445
{
5446
  uint32 tmp=(uint32) uint3korr(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5447 5448 5449
  ltime->day=   tmp & 31;
  ltime->month= (tmp >> 5) & 15;
  ltime->year=  (tmp >> 9);
5450
  ltime->time_type= MYSQL_TIMESTAMP_DATE;
5451
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
5452 5453
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ?
          1 : 0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5454 5455
}

5456

5457
bool Field_newdate::get_time(MYSQL_TIME *ltime)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5458
{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5459
  return Field_newdate::get_date(ltime,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5460 5461
}

5462

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5463 5464
int Field_newdate::cmp(const char *a_ptr, const char *b_ptr)
{
5465 5466 5467
  uint32 a,b;
  a=(uint32) uint3korr(a_ptr);
  b=(uint32) uint3korr(b_ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5468 5469 5470
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

5471

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5472 5473 5474 5475 5476 5477 5478
void Field_newdate::sort_string(char *to,uint length __attribute__((unused)))
{
  to[0] = ptr[2];
  to[1] = ptr[1];
  to[2] = ptr[0];
}

5479

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5480 5481
void Field_newdate::sql_type(String &res) const
{
5482
  res.set_ascii(STRING_WITH_LEN("date"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5483 5484 5485 5486 5487 5488 5489 5490 5491 5492
}


/****************************************************************************
** 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
5493
int Field_datetime::store(const char *from,uint len,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5494
{
5495
  MYSQL_TIME time_tmp;
5496 5497
  int error;
  ulonglong tmp= 0;
5498
  enum enum_mysql_timestamp_type func_res;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5499
  THD *thd= table ? table->in_use : current_thd;
5500

5501 5502
  func_res= str_to_datetime(from, len, &time_tmp,
                            (TIME_FUZZY_DATE |
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5503
                             (thd->variables.sql_mode &
5504 5505 5506 5507
                              (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
                               MODE_INVALID_DATES))),
                            &error);
  if ((int) func_res > (int) MYSQL_TIMESTAMP_ERROR)
5508
    tmp= TIME_to_ulonglong_datetime(&time_tmp);
5509 5510 5511
  else
    error= 1;                                 // Fix if invalid zero date

5512
  if (error)
5513
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5514
                         ER_WARN_DATA_OUT_OF_RANGE,
5515
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
5516

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5517
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5518
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5519 5520 5521 5522 5523 5524
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
5525
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5526 5527 5528
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5529
int Field_datetime::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5530
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5531
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5532 5533
  if (nr < 0.0 || nr > 99991231235959.0)
  {
5534 5535
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_OUT_OF_RANGE,
5536
                         nr, MYSQL_TIMESTAMP_DATETIME);
5537
    nr= 0.0;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5538
    error= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5539
  }
5540
  error|= Field_datetime::store((longlong) rint(nr), FALSE);
5541
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5542 5543 5544
}


5545
int Field_datetime::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5546
{
5547
  MYSQL_TIME not_used;
5548 5549
  int error;
  longlong initial_nr= nr;
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5550
  THD *thd= table ? table->in_use : current_thd;
5551

5552
  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5553
                                         (thd->variables.sql_mode &
5554 5555 5556 5557
                                          (MODE_NO_ZERO_IN_DATE |
                                           MODE_NO_ZERO_DATE |
                                           MODE_INVALID_DATES))), &error);

5558
  if (nr == LL(-1))
5559
  {
5560 5561
    nr= 0;
    error= 2;
5562
  }
5563

5564 5565 5566 5567 5568 5569
  if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
                         error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
                         WARN_DATA_TRUNCATED, initial_nr,
                         MYSQL_TIMESTAMP_DATETIME, 1);

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5570
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5571
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5572 5573 5574 5575 5576 5577
  {
    int8store(ptr,nr);
  }
  else
#endif
    longlongstore(ptr,nr);
5578
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5579 5580
}

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

5582
int Field_datetime::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5583 5584
{
  longlong tmp;
5585
  int error= 0;
5586
  /*
5587
    We don't perform range checking here since values stored in MYSQL_TIME
5588 5589
    structure always fit into DATETIME range.
  */
5590 5591
  if (time_type == MYSQL_TIMESTAMP_DATE ||
      time_type == MYSQL_TIMESTAMP_DATETIME)
5592
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5593 5594
    tmp=((ltime->year*10000L+ltime->month*100+ltime->day)*LL(1000000)+
	 (ltime->hour*10000L+ltime->minute*100+ltime->second));
5595 5596 5597 5598 5599
    if (check_date(ltime, tmp != 0,
                   (TIME_FUZZY_DATE |
                    (current_thd->variables.sql_mode &
                     (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
                      MODE_INVALID_DATES))), &error))
5600
    {
5601
      char buff[MAX_DATE_STRING_REP_LENGTH];
5602 5603 5604 5605 5606 5607
      String str(buff, sizeof(buff), &my_charset_latin1);
      make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATETIME,1);
    }
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5608 5609 5610
  else
  {
    tmp=0;
5611
    error= 1;
5612
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5613 5614
  }
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5615
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5616 5617 5618 5619 5620 5621
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
5622
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5623 5624
}

5625 5626
bool Field_datetime::send_binary(Protocol *protocol)
{
5627
  MYSQL_TIME tm;
5628
  Field_datetime::get_date(&tm, TIME_FUZZY_DATE);
5629 5630 5631
  return protocol->store(&tm);
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5632 5633 5634 5635 5636 5637 5638 5639 5640 5641

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

longlong Field_datetime::val_int(void)
{
  longlong j;
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5642
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661
    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
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5662
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5663 5664 5665 5666 5667 5668
    tmp=sint8korr(ptr);
  else
#endif
    longlongget(tmp,ptr);

  /*
5669
    Avoid problem with slow longlong arithmetic and sprintf
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5670 5671 5672 5673 5674
  */

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

5675
  pos= (char*) val_buffer->ptr() + MAX_DATETIME_WIDTH;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5676
  *pos--=0;
5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695
  *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
5696 5697 5698
  return val_buffer;
}

5699
bool Field_datetime::get_date(MYSQL_TIME *ltime, uint fuzzydate)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5700 5701
{
  longlong tmp=Field_datetime::val_int();
5702 5703 5704
  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
5705

5706
  ltime->time_type=	MYSQL_TIMESTAMP_DATETIME;
5707 5708 5709 5710 5711 5712 5713 5714
  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);
5715
  return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ? 1 : 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5716 5717
}

5718
bool Field_datetime::get_time(MYSQL_TIME *ltime)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5719
{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
5720
  return Field_datetime::get_date(ltime,0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5721 5722 5723 5724 5725 5726
}

int Field_datetime::cmp(const char *a_ptr, const char *b_ptr)
{
  longlong a,b;
#ifdef WORDS_BIGENDIAN
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5727
  if (table && table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744
  {
    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
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5745
  if (!table || !table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772
  {
    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
{
5773
  res.set_ascii(STRING_WITH_LEN("datetime"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5774 5775 5776 5777 5778 5779 5780
}

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

5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878
/*
  Report "not well formed" or "cannot convert" error
  after storing a character string info a field.

  SYNOPSIS
    check_string_copy_error()
    field                    - Field
    well_formed_error_pos    - where not well formed data was first met
    cannot_convert_error_pos - where a not-convertable character was first met
    end                      - end of the string

  NOTES
    As of version 5.0 both cases return the same error:
  
      "Invalid string value: 'xxx' for column 't' at row 1"
  
  Future versions will possibly introduce a new error message:

      "Cannot convert character string: 'xxx' for column 't' at row 1"

  RETURN
    FALSE - If errors didn't happen
    TRUE  - If an error happened
*/

static bool
check_string_copy_error(Field_str *field,
                        const char *well_formed_error_pos,
                        const char *cannot_convert_error_pos,
                        const char *end)
{
  const char *pos, *end_orig;
  char tmp[64], *t;
  
  if (!(pos= well_formed_error_pos) &&
      !(pos= cannot_convert_error_pos))
    return FALSE;

  end_orig= end;
  set_if_smaller(end, pos + 6);

  for (t= tmp; pos < end; pos++)
  {
    if (((unsigned char) *pos) >= 0x20 &&
        ((unsigned char) *pos) <= 0x7F)
    {
      *t++= *pos;
    }
    else
    {
      *t++= '\\';
      *t++= 'x';
      *t++= _dig_vec_upper[((unsigned char) *pos) >> 4];
      *t++= _dig_vec_upper[((unsigned char) *pos) & 15];
    }
  }
  if (end_orig > end)
  {
    *t++= '.';
    *t++= '.';
    *t++= '.';
  }
  *t= '\0';
  push_warning_printf(field->table->in_use, 
                      field->table->in_use->abort_on_warning ?
                      MYSQL_ERROR::WARN_LEVEL_ERROR :
                      MYSQL_ERROR::WARN_LEVEL_WARN,
                      ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 
                      ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
                      "string", tmp, field->field_name,
                      (ulong) field->table->in_use->row_count);
  return TRUE;
}



/*
  Send a truncation warning or a truncation error
  after storing a too long character string info a field.

  SYNOPSIS
    report_data_too_long()
    field                    - Field

  RETURN
    N/A
*/

inline void
report_data_too_long(Field_str *field)
{
  if (field->table->in_use->abort_on_warning)
    field->set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
  else
    field->set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
5879 5880
	/* Copy a string and fill with space */

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
5881
int Field_string::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5882
{
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
5883
  uint copy_length;
5884 5885 5886
  const char *well_formed_error_pos;
  const char *cannot_convert_error_pos;
  const char *from_end_pos;
5887

5888 5889
  /* See the comment for Field_long::store(long long) */
  DBUG_ASSERT(table->in_use == current_thd);
5890

5891 5892 5893 5894 5895 5896 5897
  copy_length= well_formed_copy_nchars(field_charset,
                                       ptr, field_length,
                                       cs, from, length,
                                       field_length / field_charset->mbmaxlen,
                                       &well_formed_error_pos,
                                       &cannot_convert_error_pos,
                                       &from_end_pos);
5898 5899 5900

  /* Append spaces if the string was shorter than the field. */
  if (copy_length < field_length)
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
5901
    field_charset->cset->fill(field_charset,ptr+copy_length,
5902
                              field_length-copy_length,
5903
                              field_charset->pad_char);
5904

5905 5906 5907 5908
  if (check_string_copy_error(this, well_formed_error_pos,
                              cannot_convert_error_pos, from + length))
    return 2;

5909 5910 5911 5912
  /*
    Check if we lost any important data (anything in a binary string,
    or any non-space in others).
  */
5913
  if ((from_end_pos < from + length) && table->in_use->count_cuted_fields)
5914
  {
5915
    if (test_if_important_data(field_charset, from_end_pos, from + length))
5916
    {
5917 5918
      report_data_too_long(this);
      return 2;
5919
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5920
  }
5921
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5922 5923 5924
}


5925 5926 5927 5928
/*
  Store double value in Field_string or Field_varstring.

  SYNOPSIS
serg@serg.mylan's avatar
serg@serg.mylan committed
5929
    store(double nr)
5930 5931 5932 5933 5934 5935
    nr            number

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

serg@serg.mylan's avatar
serg@serg.mylan committed
5936
int Field_str::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5937
{
5938
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
5939
  uint length;
monty@mysql.com's avatar
monty@mysql.com committed
5940
  bool use_scientific_notation= TRUE;
5941
  uint local_char_length= field_length / charset()->mbmaxlen;
5942 5943 5944 5945 5946 5947
  /*
    Check fabs(nr) against longest value that can be stored in field,
    which depends on whether the value is < 1 or not, and negative or not
  */
  double anr= fabs(nr);
  int neg= (nr < 0.0) ? 1 : 0;
5948 5949 5950
  if (local_char_length > 4 && local_char_length < 32 &&
      (anr < 1.0 ? anr > 1/(log_10[max(0,(int) local_char_length-neg-2)]) /* -2 for "0." */
                 : anr < log_10[local_char_length-neg]-1))
monty@mysql.com's avatar
monty@mysql.com committed
5951
    use_scientific_notation= FALSE;
konstantin@mysql.com's avatar
konstantin@mysql.com committed
5952

monty@mysql.com's avatar
monty@mysql.com committed
5953 5954
  length= (uint) my_sprintf(buff, (buff, "%-.*g",
                                   (use_scientific_notation ?
5955 5956
                                    max(0, (int)local_char_length-neg-5) :
                                    local_char_length),
monty@mysql.com's avatar
monty@mysql.com committed
5957
                                   nr));
5958 5959 5960 5961 5962
  /*
    +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)
5963
    the test for local_char_length < 5 is for extreme cases,
5964
    like inserting 500.0 in char(1)
5965
  */
5966
  DBUG_ASSERT(local_char_length < 5 || length <= local_char_length+1);
5967
  return store((const char *) buff, length, charset());
5968 5969
}

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

5971
int Field_string::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5972
{
5973 5974 5975
  char buff[64];
  int  l;
  CHARSET_INFO *cs=charset();
5976 5977
  l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),
                                   unsigned_val ? 10 : -10, nr);
5978
  return Field_string::store(buff,(uint)l,cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5979 5980
}

5981

5982 5983
int Field_longstr::store_decimal(const my_decimal *d)
{
5984 5985
  char buff[DECIMAL_MAX_STR_LENGTH+1];
  String str(buff, sizeof(buff), &my_charset_bin);
5986
  my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
5987
  return store(str.ptr(), str.length(), str.charset());
5988
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5989

5990

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5991 5992
double Field_string::val_real(void)
{
5993 5994
  int error;
  char *end;
5995
  CHARSET_INFO *cs= charset();
5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011
  double result;
  
  result=  my_strntod(cs,ptr,field_length,&end,&error);
  if (!table->in_use->no_errors &&
      (error || (field_length != (uint32)(end - ptr) && 
                !check_if_only_end_space(cs, end, ptr + field_length))))
  {
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
    String tmp(buf, sizeof(buf), cs);
    tmp.copy(ptr, field_length, cs);
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_TRUNCATED_WRONG_VALUE, 
                        ER(ER_TRUNCATED_WRONG_VALUE),
                        "DOUBLE", tmp.c_ptr());
  }
  return result;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6012 6013 6014 6015 6016
}


longlong Field_string::val_int(void)
{
6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035
  int error;
  char *end;
  CHARSET_INFO *cs= charset();
  longlong result;

  result= my_strntoll(cs,ptr,field_length,10,&end,&error);
  if (!table->in_use->no_errors &&
      (error || (field_length != (uint32)(end - ptr) && 
                !check_if_only_end_space(cs, end, ptr + field_length))))
  {
    char buf[LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE];
    String tmp(buf, sizeof(buf), cs);
    tmp.copy(ptr, field_length, cs);
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_TRUNCATED_WRONG_VALUE, 
                        ER(ER_TRUNCATED_WRONG_VALUE),
                        "INTEGER", tmp.c_ptr());
  }
  return result;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6036 6037 6038 6039 6040 6041
}


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
6042
  uint length= field_charset->cset->lengthsp(field_charset, ptr, field_length);
6043 6044
  /* 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
6045
  val_ptr->set((const char*) ptr, length, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6046 6047 6048 6049
  return val_ptr;
}


6050 6051
my_decimal *Field_string::val_decimal(my_decimal *decimal_value)
{
6052
  int err= str2my_decimal(E_DEC_FATAL_ERROR, ptr, field_length, charset(),
6053
                 decimal_value);
6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065
  if (!table->in_use->no_errors && err)
  {
    char buf[DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE];
    CHARSET_INFO *cs= charset();
    String tmp(buf, sizeof(buf), cs);
    tmp.copy(ptr, field_length, cs);
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_TRUNCATED_WRONG_VALUE, 
                        ER(ER_TRUNCATED_WRONG_VALUE),
                        "DECIMAL", tmp.c_ptr());
  }

6066 6067 6068 6069
  return decimal_value;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6070 6071
int Field_string::cmp(const char *a_ptr, const char *b_ptr)
{
monty@mysql.com's avatar
monty@mysql.com committed
6072 6073 6074 6075 6076 6077 6078 6079 6080 6081
  uint a_len, b_len;

  if (field_charset->mbmaxlen != 1)
  {
    uint char_len= field_length/field_charset->mbmaxlen;
    a_len= my_charpos(field_charset, a_ptr, a_ptr + field_length, char_len);
    b_len= my_charpos(field_charset, b_ptr, b_ptr + field_length, char_len);
  }
  else
    a_len= b_len= field_length;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6082 6083 6084 6085 6086 6087
  /*
    We have to remove end space to be able to compare multi-byte-characters
    like in latin_de 'ae' and 0xe4
  */
  return field_charset->coll->strnncollsp(field_charset,
                                          (const uchar*) a_ptr, a_len,
6088 6089
                                          (const uchar*) b_ptr, b_len,
                                          0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6090 6091
}

6092

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6093 6094
void Field_string::sort_string(char *to,uint length)
{
6095 6096 6097
  IF_DBUG(uint tmp=) my_strnxfrm(field_charset,
                                 (unsigned char *) to, length,
                                 (unsigned char *) ptr, field_length);
6098
  DBUG_ASSERT(tmp == length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6099 6100 6101 6102 6103
}


void Field_string::sql_type(String &res) const
{
6104
  THD *thd= table->in_use;
6105
  CHARSET_INFO *cs=res.charset();
6106 6107 6108 6109 6110 6111 6112
  ulong length;

  length= cs->cset->snprintf(cs,(char*) res.ptr(),
                             res.alloced_length(), "%s(%d)",
                             ((type() == MYSQL_TYPE_VAR_STRING &&
                               !thd->variables.new_mode) ?
                              (has_charset() ? "varchar" : "varbinary") :
6113
			      (has_charset() ? "char" : "binary")),
6114
                             (int) field_length / charset()->mbmaxlen);
6115
  res.length(length);
6116 6117
  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
      has_charset() && (charset()->state & MY_CS_BINSORT))
6118
    res.append(STRING_WITH_LEN(" binary"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6119 6120
}

6121

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6122
char *Field_string::pack(char *to, const char *from, uint max_length)
6123
{
monty@mysql.com's avatar
monty@mysql.com committed
6124
  uint length=      min(field_length,max_length);
6125 6126 6127 6128 6129
  uint local_char_length= max_length/field_charset->mbmaxlen;
  if (length > local_char_length)
    local_char_length= my_charpos(field_charset, from, from+length,
                                  local_char_length);
  set_if_smaller(length, local_char_length);
serg@serg.mylan's avatar
serg@serg.mylan committed
6130 6131
  while (length && from[length-1] == ' ')
    length--;
6132
  *to++= (char) (uchar) length;
6133
  if (field_length > 255)
6134
    *to++= (char) (uchar) (length >> 8);
6135 6136
  memcpy(to, from, length);
  return to+length;
6137 6138 6139
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6140 6141
const char *Field_string::unpack(char *to, const char *from)
{
6142 6143 6144 6145 6146 6147 6148 6149
  uint length;
  if (field_length > 255)
  {
    length= uint2korr(from);
    from+= 2;
  }
  else
    length= (uint) (uchar) *from++;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6150 6151 6152 6153 6154 6155
  memcpy(to, from, (int) length);
  bfill(to+length, field_length - length, ' ');
  return from+length;
}


6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173
/*
  Compare two packed keys

  SYNOPSIS
    pack_cmp()
     a			New key
     b			Original key
     length		Key length
     insert_or_update	1 if this is an insert or update

  RETURN
    < 0	  a < b
    0	  a = b
    > 0   a > b
*/

int Field_string::pack_cmp(const char *a, const char *b, uint length,
                           my_bool insert_or_update)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6174
{
6175
  uint a_length, b_length;
6176
  if (length > 255)
6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187
  {
    a_length= uint2korr(a);
    b_length= uint2korr(b);
    a+= 2;
    b+= 2;
  }
  else
  {
    a_length= (uint) (uchar) *a++;
    b_length= (uint) (uchar) *b++;
  }
6188 6189 6190 6191
  return field_charset->coll->strnncollsp(field_charset,
                                          (const uchar*) a, a_length,
                                          (const uchar*) b, b_length,
                                          insert_or_update);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6192 6193 6194
}


6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211
/*
  Compare a packed key against row

  SYNOPSIS
    pack_cmp()
     key		Original key
     length		Key length. (May be less than field length)
     insert_or_update	1 if this is an insert or update

  RETURN
    < 0	  row < key
    0	  row = key
    > 0   row > key
*/

int Field_string::pack_cmp(const char *key, uint length,
                           my_bool insert_or_update)
6212
{
6213
  uint row_length, local_key_length;
6214 6215
  char *end;
  if (length > 255)
6216
  {
6217
    local_key_length= uint2korr(key);
6218
    key+= 2;
6219 6220
  }
  else
6221
    local_key_length= (uint) (uchar) *key++;
6222 6223 6224
  
  /* Only use 'length' of key, not field_length */
  end= ptr + length;
6225 6226
  while (end > ptr && end[-1] == ' ')
    end--;
6227 6228 6229 6230
  row_length= (uint) (end - ptr);

  return field_charset->coll->strnncollsp(field_charset,
                                          (const uchar*) ptr, row_length,
6231
                                          (const uchar*) key, local_key_length,
6232
                                          insert_or_update);
6233 6234 6235
}


6236
uint Field_string::packed_col_length(const char *data_ptr, uint length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6237
{
6238
  if (length > 255)
6239
    return uint2korr(data_ptr)+2;
6240
  return (uint) ((uchar) *data_ptr)+1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6241 6242
}

6243

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6244 6245
uint Field_string::max_packed_col_length(uint max_length)
{
6246
  return (max_length > 255 ? 2 : 1)+max_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6247 6248
}

6249
uint Field_string::get_key_image(char *buff, uint length, imagetype type_arg)
6250
{
6251
  uint bytes = my_charpos(field_charset, ptr, ptr + field_length,
6252 6253 6254
                          length / field_charset->mbmaxlen);
  memcpy(buff, ptr, bytes);
  if (bytes < length)
6255 6256
    field_charset->cset->fill(field_charset, buff + bytes, length - bytes, 
                              field_charset->pad_char);
6257 6258
  return bytes;
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6259

6260 6261
Field *Field_string::new_field(MEM_ROOT *root, struct st_table *new_table,
                               bool keep_type)
6262
{
6263
  Field *field;
6264

6265
  if (type() != MYSQL_TYPE_VAR_STRING || keep_type)
6266
    field= Field::new_field(root, new_table, keep_type);
6267 6268
  else
  {
6269

6270 6271 6272 6273 6274
    /*
      Old VARCHAR field which should be modified to a VARCHAR on copy
      This is done to ensure that ALTER TABLE will convert old VARCHAR fields
      to now VARCHAR fields.
    */
6275
    field= new Field_varstring(field_length, maybe_null(),
6276 6277 6278 6279 6280 6281 6282
                                   field_name, new_table, charset());
    /*
      Normally orig_table is different from table only if field was created
      via ::new_field.  Here we alter the type of field, so ::new_field is
      not applicable. But we still need to preserve the original field
      metadata for the client-server protocol.
    */
6283
    field->orig_table= orig_table;
6284
  }
6285
  return field;
6286 6287
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6288
/****************************************************************************
6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301
  VARCHAR type
  Data in field->ptr is stored as:
    1 or 2 bytes length-prefix-header  (from Field_varstring::length_bytes)
    data

  NOTE:
  When VARCHAR is stored in a key (for handler::index_read() etc) it's always
  stored with a 2 byte prefix. (Just like blob keys).

  Normally length_bytes is calculated as (field_length < 256 : 1 ? 2)
  The exception is if there is a prefix key field that is part of a long
  VARCHAR, in which case field_length for this may be 1 but the length_bytes
  is 2.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6302 6303
****************************************************************************/

6304
const uint Field_varstring::MAX_SIZE= UINT_MAX16;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6305

monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6306
int Field_varstring::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6307
{
6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319
  uint copy_length;
  const char *well_formed_error_pos;
  const char *cannot_convert_error_pos;
  const char *from_end_pos;

  copy_length= well_formed_copy_nchars(field_charset,
                                       ptr + length_bytes, field_length,
                                       cs, from, length,
                                       field_length / field_charset->mbmaxlen,
                                       &well_formed_error_pos,
                                       &cannot_convert_error_pos,
                                       &from_end_pos);
6320

6321 6322 6323 6324
  if (length_bytes == 1)
    *ptr= (uchar) copy_length;
  else
    int2store(ptr, copy_length);
6325

6326 6327 6328 6329
  if (check_string_copy_error(this, well_formed_error_pos,
                              cannot_convert_error_pos, from + length))
    return 2;

6330
  // Check if we lost something other than just trailing spaces
6331
  if ((from_end_pos < from + length) && table->in_use->count_cuted_fields)
6332
  {
6333 6334 6335 6336
    if (test_if_important_data(field_charset, from_end_pos, from + length))
      report_data_too_long(this);
    else /* If we lost only spaces then produce a NOTE, not a WARNING */
      set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6337
    return 2;
6338
  }
monty@mysql.com's avatar
monty@mysql.com committed
6339
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6340 6341 6342
}


6343
int Field_varstring::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6344
{
6345
  char buff[64];
6346 6347 6348 6349
  uint  length;
  length= (uint) (field_charset->cset->longlong10_to_str)(field_charset,
                                                          buff,
                                                          sizeof(buff),
6350 6351 6352
                                                          (unsigned_val ? 10:
                                                           -10),
                                                           nr);
6353
  return Field_varstring::store(buff, length, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6354 6355 6356 6357 6358
}


double Field_varstring::val_real(void)
{
6359
  int not_used;
6360
  char *end_not_used;
6361
  uint length= length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
6362
  return my_strntod(field_charset, ptr+length_bytes, length, &end_not_used,
6363
                    &not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6364 6365 6366 6367 6368
}


longlong Field_varstring::val_int(void)
{
6369
  int not_used;
6370
  char *end_not_used;
6371
  uint length= length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
6372 6373
  return my_strntoll(field_charset, ptr+length_bytes, length, 10,
                     &end_not_used, &not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6374 6375 6376 6377 6378
}

String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
				 String *val_ptr)
{
6379 6380
  uint length=  length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
  val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6381 6382 6383 6384
  return val_ptr;
}


6385 6386 6387 6388 6389 6390 6391 6392 6393
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
{
  uint length= length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
  str2my_decimal(E_DEC_FATAL_ERROR, ptr+length_bytes, length, charset(),
                 decimal_value);
  return decimal_value;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6394 6395
int Field_varstring::cmp(const char *a_ptr, const char *b_ptr)
{
6396
  uint a_length, b_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6397
  int diff;
6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408

  if (length_bytes == 1)
  {
    a_length= (uint) (uchar) *a_ptr;
    b_length= (uint) (uchar) *b_ptr;
  }
  else
  {
    a_length= uint2korr(a_ptr);
    b_length= uint2korr(b_ptr);
  }
6409 6410
  diff= field_charset->coll->strnncollsp(field_charset,
                                         (const uchar*) a_ptr+
6411
                                         length_bytes,
6412 6413
                                         a_length,
                                         (const uchar*) b_ptr+
6414
                                         length_bytes,
6415 6416
                                         b_length,0);
  return diff;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6417 6418
}

6419

6420 6421 6422 6423
/*
  NOTE: varstring and blob keys are ALWAYS stored with a 2 byte length prefix
*/

6424 6425
int Field_varstring::key_cmp(const byte *key_ptr, uint max_key_length)
{
6426
  uint length=  length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
6427
  uint local_char_length= max_key_length / field_charset->mbmaxlen;
6428

6429 6430 6431
  local_char_length= my_charpos(field_charset, ptr + length_bytes,
                          ptr + length_bytes + length, local_char_length);
  set_if_smaller(length, local_char_length);
6432 6433 6434 6435 6436 6437
  return field_charset->coll->strnncollsp(field_charset, 
                                          (const uchar*) ptr + length_bytes,
                                          length,
                                          (const uchar*) key_ptr+
                                          HA_KEY_BLOB_LENGTH,
                                          uint2korr(key_ptr), 0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6438 6439
}

6440

6441 6442 6443 6444 6445 6446 6447 6448
/*
  Compare to key segments (always 2 byte length prefix)

  NOTE
    This is used only to compare key segments created for index_read().
    (keys are created and compared in key.cc)
*/

6449 6450
int Field_varstring::key_cmp(const byte *a,const byte *b)
{
6451 6452 6453 6454 6455 6456 6457 6458
  return field_charset->coll->strnncollsp(field_charset,
                                          (const uchar*) a +
                                          HA_KEY_BLOB_LENGTH,
                                          uint2korr(a),
                                          (const uchar*) b +
                                          HA_KEY_BLOB_LENGTH,
                                          uint2korr(b),
                                          0);
6459 6460 6461
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6462 6463
void Field_varstring::sort_string(char *to,uint length)
{
6464
  uint tot_length=  length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475

  if (field_charset == &my_charset_bin)
  {
    /* Store length last in high-byte order to sort longer strings first */
    if (length_bytes == 1)
      to[length-1]= tot_length;
    else
      mi_int2store(to+length-2, tot_length);
    length-= length_bytes;
  }
 
6476 6477
  tot_length= my_strnxfrm(field_charset,
			  (uchar*) to, length,
6478
			  (uchar*) ptr + length_bytes,
6479
			  tot_length);
monty@mysql.com's avatar
monty@mysql.com committed
6480
  DBUG_ASSERT(tot_length == length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6481 6482 6483
}


6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495
enum ha_base_keytype Field_varstring::key_type() const
{
  enum ha_base_keytype res;

  if (binary())
    res= length_bytes == 1 ? HA_KEYTYPE_VARBINARY1 : HA_KEYTYPE_VARBINARY2;
  else
    res= length_bytes == 1 ? HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2;
  return res;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6496 6497
void Field_varstring::sql_type(String &res) const
{
6498
  THD *thd= table->in_use;
6499
  CHARSET_INFO *cs=res.charset();
6500 6501 6502 6503 6504 6505
  ulong length;

  length= cs->cset->snprintf(cs,(char*) res.ptr(),
                             res.alloced_length(), "%s(%d)",
                              (has_charset() ? "varchar" : "varbinary"),
                             (int) field_length / charset()->mbmaxlen);
6506
  res.length(length);
6507 6508
  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
      has_charset() && (charset()->state & MY_CS_BINSORT))
6509
    res.append(STRING_WITH_LEN(" binary"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6510 6511
}

6512

6513
uint32 Field_varstring::data_length(const char *from)
6514 6515 6516 6517
{
  return length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
}

6518 6519 6520 6521 6522
/*
  Functions to create a packed row.
  Here the number of length bytes are depending on the given max_length
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6523 6524
char *Field_varstring::pack(char *to, const char *from, uint max_length)
{
6525
  uint length= length_bytes == 1 ? (uint) (uchar) *from : uint2korr(from);
6526
  set_if_smaller(max_length, field_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6527 6528
  if (length > max_length)
    length=max_length;
6529
  *to++= (char) (length & 255);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6530
  if (max_length > 255)
6531
    *to++= (char) (length >> 8);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6532
  if (length)
6533
    memcpy(to, from+length_bytes, length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6534 6535 6536 6537
  return to+length;
}


6538
char *Field_varstring::pack_key(char *to, const char *key, uint max_length)
6539
{
6540
  uint length=  length_bytes == 1 ? (uint) (uchar) *key : uint2korr(key);
6541
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
6542
                     max_length/field_charset->mbmaxlen : max_length);
6543
  key+= length_bytes;
6544
  if (length > local_char_length)
6545
  {
6546 6547 6548
    local_char_length= my_charpos(field_charset, key, key+length,
                                  local_char_length);
    set_if_smaller(length, local_char_length);
6549
  }
6550 6551 6552 6553
  *to++= (char) (length & 255);
  if (max_length > 255)
    *to++= (char) (length >> 8);
  if (length)
6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618
    memcpy(to, key, length);
  return to+length;
}


/*
  Unpack a key into a record buffer.

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

  DESCRIPTION
    A VARCHAR 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'.

  RETURN
    Pointer to end of 'key' (To the next key part if multi-segment key)
*/

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

  /* put the length into the record buffer */
  if (length_bytes == 1)
    *ptr= (uchar) length;
  else
    int2store(ptr, length);
  memcpy(ptr + length_bytes, key, length);
  return key + length;
}

/*
  Create a packed key that will be used for storage in the index tree

  SYNOPSIS
    pack_key_from_key_image()
    to			Store packed key segment here
    from		Key segment (as given to index_read())
    max_length  	Max length of key

  RETURN
    end of key storage
*/

char *Field_varstring::pack_key_from_key_image(char *to, const char *from,
                                               uint max_length)
{
  /* Key length is always stored as 2 bytes */
  uint length= uint2korr(from);
  if (length > max_length)
    length= max_length;
  *to++= (char) (length & 255);
  if (max_length > 255)
    *to++= (char) (length >> 8);
  if (length)
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
6619 6620 6621 6622
  return to+length;
}


6623 6624 6625 6626
/*
  unpack field packed with Field_varstring::pack()
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6627 6628 6629
const char *Field_varstring::unpack(char *to, const char *from)
{
  uint length;
6630
  if (length_bytes == 1)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6631 6632 6633
    length= (uint) (uchar) (*to= *from++);
  else
  {
6634 6635 6636
    length= uint2korr(from);
    to[0]= *from++;
    to[1]= *from++;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6637 6638
  }
  if (length)
6639
    memcpy(to+ length_bytes, from, length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6640 6641 6642 6643
  return from+length;
}


6644 6645
int Field_varstring::pack_cmp(const char *a, const char *b,
                              uint key_length_arg,
6646
                              my_bool insert_or_update)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6647
{
6648
  uint a_length, b_length;
6649
  if (key_length_arg > 255)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6650
  {
6651 6652
    a_length=uint2korr(a); a+= 2;
    b_length=uint2korr(b); b+= 2;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6653 6654 6655 6656 6657 6658
  }
  else
  {
    a_length= (uint) (uchar) *a++;
    b_length= (uint) (uchar) *b++;
  }
6659 6660 6661 6662
  return field_charset->coll->strnncollsp(field_charset,
                                          (const uchar*) a, a_length,
                                          (const uchar*) b, b_length,
                                          insert_or_update);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6663 6664
}

6665

6666
int Field_varstring::pack_cmp(const char *b, uint key_length_arg,
6667
                              my_bool insert_or_update)
6668
{
6669 6670
  char *a= ptr+ length_bytes;
  uint a_length=  length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
6671
  uint b_length;
6672 6673 6674
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
                           key_length_arg / field_charset->mbmaxlen :
                           key_length_arg);
6675

6676
  if (key_length_arg > 255)
6677
  {
6678
    b_length=uint2korr(b); b+= HA_KEY_BLOB_LENGTH;
6679 6680 6681
  }
  else
    b_length= (uint) (uchar) *b++;
6682

6683
  if (a_length > local_char_length)
6684
  {
6685 6686 6687
    local_char_length= my_charpos(field_charset, a, a+a_length,
                                  local_char_length);
    set_if_smaller(a_length, local_char_length);
6688
  }
6689 6690 6691 6692 6693 6694

  return field_charset->coll->strnncollsp(field_charset,
                                          (const uchar*) a,
                                          a_length,
                                          (const uchar*) b, b_length,
                                          insert_or_update);
6695 6696
}

6697

6698
uint Field_varstring::packed_col_length(const char *data_ptr, uint length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6699
{
6700
  if (length > 255)
6701
    return uint2korr(data_ptr)+2;
6702
  return (uint) ((uchar) *data_ptr)+1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6703 6704
}

6705

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6706 6707
uint Field_varstring::max_packed_col_length(uint max_length)
{
6708
  return (max_length > 255 ? 2 : 1)+max_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6709 6710
}

6711
uint Field_varstring::get_key_image(char *buff, uint length, imagetype type)
6712
{
6713
  uint f_length=  length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
6714
  uint local_char_length= length / field_charset->mbmaxlen;
6715
  char *pos= ptr+length_bytes;
6716 6717 6718
  local_char_length= my_charpos(field_charset, pos, pos + f_length,
                                local_char_length);
  set_if_smaller(f_length, local_char_length);
6719
  /* Key is always stored with 2 bytes */
6720
  int2store(buff,f_length);
6721
  memcpy(buff+HA_KEY_BLOB_LENGTH, pos, f_length);
6722
  if (f_length < length)
6723 6724 6725 6726 6727
  {
    /*
      Must clear this as we do a memcmp in opt_range.cc to detect
      identical keys
    */
6728
    bzero(buff+HA_KEY_BLOB_LENGTH+f_length, (length-f_length));
6729
  }
6730
  return HA_KEY_BLOB_LENGTH+f_length;
6731 6732
}

6733 6734

void Field_varstring::set_key_image(char *buff,uint length)
6735
{
6736
  length= uint2korr(buff);			// Real length is here
6737 6738
  (void) Field_varstring::store(buff+HA_KEY_BLOB_LENGTH, length,
                                field_charset);
6739 6740 6741
}


6742 6743 6744 6745 6746
int Field_varstring::cmp_binary(const char *a_ptr, const char *b_ptr,
                                uint32 max_length)
{
  uint32 a_length,b_length;

6747 6748 6749 6750 6751 6752 6753 6754 6755 6756
  if (length_bytes == 1)
  {
    a_length= (uint) (uchar) *a_ptr;
    b_length= (uint) (uchar) *b_ptr;
  }
  else
  {
    a_length= uint2korr(a_ptr);
    b_length= uint2korr(b_ptr);
  }
6757 6758 6759 6760
  set_if_smaller(a_length, max_length);
  set_if_smaller(b_length, max_length);
  if (a_length != b_length)
    return 1;
6761 6762 6763 6764
  return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
}


6765 6766
Field *Field_varstring::new_field(MEM_ROOT *root, struct st_table *new_table,
                                  bool keep_type)
6767
{
6768 6769
  Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
                                                            keep_type);
6770 6771 6772 6773 6774 6775 6776
  if (res)
    res->length_bytes= length_bytes;
  return res;
}


Field *Field_varstring::new_key_field(MEM_ROOT *root,
monty@mysql.com's avatar
monty@mysql.com committed
6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788
                                      struct st_table *new_table,
                                      char *new_ptr, uchar *new_null_ptr,
                                      uint new_null_bit)
{
  Field_varstring *res;
  if ((res= (Field_varstring*) Field::new_key_field(root,
                                                    new_table,
                                                    new_ptr,
                                                    new_null_ptr,
                                                    new_null_bit)))
  {
    /* Keys length prefixes are always packed with 2 bytes */
6789
    res->length_bytes= 2;
monty@mysql.com's avatar
monty@mysql.com committed
6790
  }
6791
  return res;
6792 6793
}

6794

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6795 6796 6797 6798 6799 6800
/****************************************************************************
** 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.
****************************************************************************/

6801
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
6802 6803
		       enum utype unireg_check_arg, const char *field_name_arg,
		       struct st_table *table_arg,uint blob_pack_length,
6804
		       CHARSET_INFO *cs)
6805
  :Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6806
	     null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
6807
	     table_arg, cs),
hf@deer.(none)'s avatar
hf@deer.(none) committed
6808
   packlength(blob_pack_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6809 6810 6811
{
  flags|= BLOB_FLAG;
  if (table)
6812
  {
6813
    table->s->blob_fields++;
6814 6815
    /* TODO: why do not fill table->s->blob_field array here? */
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6816 6817 6818
}


6819
void Field_blob::store_length(uint32 number)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6820 6821 6822 6823 6824 6825 6826
{
  switch (packlength) {
  case 1:
    ptr[0]= (uchar) number;
    break;
  case 2:
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
6827
    if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839
    {
      int2store(ptr,(unsigned short) number);
    }
    else
#endif
      shortstore(ptr,(unsigned short) number);
    break;
  case 3:
    int3store(ptr,number);
    break;
  case 4:
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
6840
    if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6841 6842 6843 6844 6845 6846 6847 6848 6849 6850
    {
      int4store(ptr,number);
    }
    else
#endif
      longstore(ptr,number);
  }
}


6851
uint32 Field_blob::get_length(const char *pos)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6852 6853 6854
{
  switch (packlength) {
  case 1:
6855
    return (uint32) (uchar) pos[0];
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6856 6857 6858 6859
  case 2:
    {
      uint16 tmp;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
6860
      if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6861 6862 6863 6864
	tmp=sint2korr(pos);
      else
#endif
	shortget(tmp,pos);
6865
      return (uint32) tmp;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6866 6867
    }
  case 3:
6868
    return (uint32) uint3korr(pos);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6869 6870 6871 6872
  case 4:
    {
      uint32 tmp;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
6873
      if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6874 6875 6876 6877
	tmp=uint4korr(pos);
      else
#endif
	longget(tmp,pos);
6878
      return (uint32) tmp;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6879 6880 6881 6882 6883 6884
    }
  }
  return 0;					// Impossible
}


6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920
/*
  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
6921
int Field_blob::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6922
{
6923 6924 6925 6926 6927 6928 6929
  uint copy_length, new_length;
  const char *well_formed_error_pos;
  const char *cannot_convert_error_pos;
  const char *from_end_pos, *tmp;
  char buff[STRING_BUFFER_USUAL_SIZE];
  String tmpstr(buff,sizeof(buff), &my_charset_bin);

bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
6930
  if (!length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6931 6932
  {
    bzero(ptr,Field_blob::pack_length());
6933
    return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6934
  }
bar@bar.intranet.mysql.r18.ru's avatar
bar@bar.intranet.mysql.r18.ru committed
6935

6936 6937 6938 6939 6940 6941 6942 6943
  if (from == value.ptr())
  {
    uint32 dummy_offset;
    if (!String::needs_conversion(length, cs, field_charset, &dummy_offset))
    {
      Field_blob::store_length(length);
      bmove(ptr+packlength,(char*) &from,sizeof(char*));
      return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6944
    }
6945 6946 6947
    if (tmpstr.copy(from, length, cs))
      goto oom_error;
    from= tmpstr.ptr();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6948
  }
6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974

  new_length= min(max_data_length(), field_charset->mbmaxlen * length);
  if (value.alloc(new_length))
    goto oom_error;

  /*
    "length" is OK as "nchars" argument to well_formed_copy_nchars as this
    is never used to limit the length of the data. The cut of long data
    is done with the new_length value.
  */
  copy_length= well_formed_copy_nchars(field_charset,
                                       (char*) value.ptr(), new_length,
                                       cs, from, length,
                                       length,
                                       &well_formed_error_pos,
                                       &cannot_convert_error_pos,
                                       &from_end_pos);

  Field_blob::store_length(copy_length);
  tmp= value.ptr();
  bmove(ptr+packlength,(char*) &tmp,sizeof(char*));

  if (check_string_copy_error(this, well_formed_error_pos,
                              cannot_convert_error_pos, from + length))
    return 2;

6975
  if (from_end_pos < from + length)
6976
  {
6977 6978
    report_data_too_long(this);
    return 2;
6979
  }
6980

6981
  return 0;
6982 6983 6984 6985 6986

oom_error:
  /* Fatal OOM error */
  bzero(ptr,Field_blob::pack_length());
  return -1; 
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6987 6988 6989
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6990
int Field_blob::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6991
{
6992 6993 6994
  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
6995 6996 6997
}


6998
int Field_blob::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6999
{
7000
  CHARSET_INFO *cs=charset();
7001 7002 7003 7004
  if (unsigned_val)
    value.set((ulonglong) nr, cs);
  else
    value.set(nr, cs);
7005
  return Field_blob::store(value.ptr(), (uint) value.length(), cs);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7006 7007
}

7008

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7009 7010
double Field_blob::val_real(void)
{
7011
  int not_used;
7012 7013 7014 7015
  char *end_not_used, *blob;
  uint32 length;
  CHARSET_INFO *cs;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7016 7017 7018
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
    return 0.0;
7019 7020
  length= get_length(ptr);
  cs= charset();
monty@mysql.com's avatar
monty@mysql.com committed
7021
  return my_strntod(cs, blob, length, &end_not_used, &not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7022 7023 7024 7025 7026
}


longlong Field_blob::val_int(void)
{
7027
  int not_used;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7028 7029 7030 7031
  char *blob;
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
    return 0;
7032
  uint32 length=get_length(ptr);
7033
  return my_strntoll(charset(),blob,length,10,NULL,&not_used);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7034 7035 7036 7037 7038 7039 7040 7041
}

String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
			    String *val_ptr)
{
  char *blob;
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
7042
    val_ptr->set("",0,charset());	// A bit safer than ->length(0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7043
  else
7044
    val_ptr->set((const char*) blob,get_length(ptr),charset());
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7045 7046 7047 7048
  return val_ptr;
}


7049 7050
my_decimal *Field_blob::val_decimal(my_decimal *decimal_value)
{
7051 7052
  const char *blob;
  memcpy_fixed(&blob, ptr+packlength, sizeof(const char*));
7053 7054 7055 7056 7057 7058 7059 7060
  if (!blob)
    blob= "";
  str2my_decimal(E_DEC_FATAL_ERROR, blob, get_length(ptr), charset(),
                 decimal_value);
  return decimal_value;
}


7061 7062
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
7063
{
7064 7065 7066 7067
  return field_charset->coll->strnncollsp(field_charset, 
                                          (const uchar*)a, a_length,
                                          (const uchar*)b, b_length,
                                          0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081
}


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_binary(const char *a_ptr, const char *b_ptr,
7082
			   uint32 max_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7083 7084 7085
{
  char *a,*b;
  uint diff;
7086
  uint32 a_length,b_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101
  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 */

7102
uint Field_blob::get_key_image(char *buff,uint length, imagetype type_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7103
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7104
  uint32 blob_length= get_length(ptr);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7105
  char *blob;
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7106

hf@deer.(none)'s avatar
hf@deer.(none) committed
7107
#ifdef HAVE_SPATIAL
7108
  if (type_arg == itMBR)
7109
  {
7110
    const char *dummy;
7111
    MBR mbr;
7112 7113
    Geometry_buffer buffer;
    Geometry *gobj;
7114
    const uint image_length= SIZEOF_STORED_DOUBLE*4;
7115 7116 7117

    if (blob_length < SRID_SIZE)
    {
7118 7119
      bzero(buff, image_length);
      return image_length;
7120 7121
    }
    get_ptr(&blob);
hf@deer.(none)'s avatar
hf@deer.(none) committed
7122
    gobj= Geometry::construct(&buffer, blob, blob_length);
7123
    if (!gobj || gobj->get_mbr(&mbr, &dummy))
7124
      bzero(buff, image_length);
7125 7126 7127 7128 7129 7130 7131
    else
    {
      float8store(buff,    mbr.xmin);
      float8store(buff+8,  mbr.xmax);
      float8store(buff+16, mbr.ymin);
      float8store(buff+24, mbr.ymax);
    }
7132
    return image_length;
7133
  }
hf@deer.(none)'s avatar
hf@deer.(none) committed
7134
#endif /*HAVE_SPATIAL*/
7135

7136
  get_ptr(&blob);
7137 7138 7139 7140
  uint local_char_length= length / field_charset->mbmaxlen;
  local_char_length= my_charpos(field_charset, blob, blob + blob_length,
                          local_char_length);
  set_if_smaller(blob_length, local_char_length);
7141

7142
  if ((uint32) length > blob_length)
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7143
  {
7144 7145 7146 7147
    /*
      Must clear this as we do a memcmp in opt_range.cc to detect
      identical keys
    */
7148
    bzero(buff+HA_KEY_BLOB_LENGTH+blob_length, (length-blob_length));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7149
    length=(uint) blob_length;
monty@donna.mysql.com's avatar
monty@donna.mysql.com committed
7150
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7151
  int2store(buff,length);
7152
  memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
7153
  return HA_KEY_BLOB_LENGTH+length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7154 7155
}

7156 7157

void Field_blob::set_key_image(char *buff,uint length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7158
{
7159
  length= uint2korr(buff);
7160
  (void) Field_blob::store(buff+HA_KEY_BLOB_LENGTH, length, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7161 7162
}

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7164 7165 7166 7167 7168
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*));
7169
  CHARSET_INFO *cs= charset();
7170 7171 7172 7173
  uint local_char_length= max_key_length / cs->mbmaxlen;
  local_char_length= my_charpos(cs, blob1, blob1+blob_length,
                                local_char_length);
  set_if_smaller(blob_length, local_char_length);
7174
  return Field_blob::cmp(blob1, blob_length,
7175 7176
			 (char*) key_ptr+HA_KEY_BLOB_LENGTH,
			 uint2korr(key_ptr));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7177 7178 7179 7180
}

int Field_blob::key_cmp(const byte *a,const byte *b)
{
7181 7182
  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
7183 7184 7185
}


7186 7187 7188 7189 7190 7191 7192
uint32 Field_blob::sort_length() const
{
  return (uint32) (current_thd->variables.max_sort_length + 
                   (field_charset == &my_charset_bin ? 0 : packlength));
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
7193 7194 7195 7196
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
7197

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7198 7199 7200 7201
  if (!blob_length)
    bzero(to,length);
  else
  {
7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226
    if (field_charset == &my_charset_bin)
    {
      char *pos;

      /*
        Store length of blob last in blob to shorter blobs before longer blobs
      */
      length-= packlength;
      pos= to+length;

      switch (packlength) {
      case 1:
        *pos= (char) blob_length;
        break;
      case 2:
        mi_int2store(pos, blob_length);
        break;
      case 3:
        mi_int3store(pos, blob_length);
        break;
      case 4:
        mi_int4store(pos, blob_length);
        break;
      }
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7227
    memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7228 7229
    
    blob_length=my_strnxfrm(field_charset,
7230 7231
                            (uchar*) to, length, 
                            (uchar*) blob, blob_length);
7232
    DBUG_ASSERT(blob_length == length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7233 7234 7235 7236 7237 7238 7239
  }
}


void Field_blob::sql_type(String &res) const
{
  const char *str;
7240
  uint length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7241
  switch (packlength) {
7242 7243 7244 7245 7246
  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;
  }
7247
  res.set_ascii(str,length);
7248
  if (charset() == &my_charset_bin)
7249
    res.append(STRING_WITH_LEN("blob"));
7250 7251
  else
  {
7252
    res.append(STRING_WITH_LEN("text"));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7253 7254 7255 7256
  }
}


7257 7258 7259 7260
char *Field_blob::pack(char *to, const char *from, uint max_length)
{
  char *save=ptr;
  ptr=(char*) from;
7261
  uint32 length=get_length();			// Length of from string
7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283
  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);
7284
  uint32 length=get_length(from);
7285 7286 7287 7288 7289 7290 7291 7292
  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
7293 7294
/* Keys for blobs are like keys on varchars */

7295
int Field_blob::pack_cmp(const char *a, const char *b, uint key_length_arg,
7296
                         my_bool insert_or_update)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7297
{
7298
  uint a_length, b_length;
7299
  if (key_length_arg > 255)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7300 7301 7302 7303 7304 7305 7306 7307 7308
  {
    a_length=uint2korr(a); a+=2;
    b_length=uint2korr(b); b+=2;
  }
  else
  {
    a_length= (uint) (uchar) *a++;
    b_length= (uint) (uchar) *b++;
  }
7309 7310 7311 7312
  return field_charset->coll->strnncollsp(field_charset,
                                          (const uchar*) a, a_length,
                                          (const uchar*) b, b_length,
                                          insert_or_update);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7313 7314
}

7315

7316
int Field_blob::pack_cmp(const char *b, uint key_length_arg,
7317
                         my_bool insert_or_update)
7318 7319
{
  char *a;
7320
  uint a_length, b_length;
7321 7322
  memcpy_fixed(&a,ptr+packlength,sizeof(char*));
  if (!a)
7323
    return key_length_arg > 0 ? -1 : 0;
7324

7325 7326
  a_length= get_length(ptr);
  if (key_length_arg > 255)
7327
  {
7328
    b_length= uint2korr(b); b+=2;
7329 7330 7331
  }
  else
    b_length= (uint) (uchar) *b++;
7332 7333 7334 7335
  return field_charset->coll->strnncollsp(field_charset,
                                          (const uchar*) a, a_length,
                                          (const uchar*) b, b_length,
                                          insert_or_update);
7336 7337
}

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7340 7341
char *Field_blob::pack_key(char *to, const char *from, uint max_length)
{
7342 7343
  char *save=ptr;
  ptr=(char*) from;
7344
  uint32 length=get_length();			// Length of from string
7345
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
7346
                     max_length/field_charset->mbmaxlen : max_length);
7347 7348
  if (length)
    get_ptr((char**) &from);
7349 7350 7351 7352
  if (length > local_char_length)
    local_char_length= my_charpos(field_charset, from, from+length,
                                  local_char_length);
  set_if_smaller(length, local_char_length);
7353 7354 7355
  *to++= (uchar) length;
  if (max_length > 255)				// 2 byte length
    *to++= (uchar) (length >> 8);
7356
  memcpy(to, from, length);
7357 7358 7359 7360
  ptr=save;					// Restore org row pointer
  return to+length;
}

7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404

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

7405

7406 7407 7408 7409 7410 7411
/* 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
7412 7413
  if (length > max_length)
    length=max_length;
7414
  *to++= (char) (length & 255);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7415
  if (max_length > 255)
7416
    *to++= (char) (length >> 8);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7417
  if (length)
7418
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7419 7420 7421
  return to+length;
}

7422

7423
uint Field_blob::packed_col_length(const char *data_ptr, uint length)
7424 7425
{
  if (length > 255)
7426
    return uint2korr(data_ptr)+2;
7427
  return (uint) ((uchar) *data_ptr)+1;
7428 7429
}

7430

7431 7432 7433 7434
uint Field_blob::max_packed_col_length(uint max_length)
{
  return (max_length > 255 ? 2 : 1)+max_length;
}
7435

7436

hf@deer.(none)'s avatar
hf@deer.(none) committed
7437
#ifdef HAVE_SPATIAL
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
7438 7439 7440

void Field_geom::sql_type(String &res) const
{
7441 7442 7443 7444
  CHARSET_INFO *cs= &my_charset_latin1;
  switch (geom_type)
  {
    case GEOM_POINT:
7445
     res.set(STRING_WITH_LEN("point"), cs);
7446 7447
     break;
    case GEOM_LINESTRING:
7448
     res.set(STRING_WITH_LEN("linestring"), cs);
7449 7450
     break;
    case GEOM_POLYGON:
7451
     res.set(STRING_WITH_LEN("polygon"), cs);
7452 7453
     break;
    case GEOM_MULTIPOINT:
7454
     res.set(STRING_WITH_LEN("multipoint"), cs);
7455 7456
     break;
    case GEOM_MULTILINESTRING:
7457
     res.set(STRING_WITH_LEN("multilinestring"), cs);
7458 7459
     break;
    case GEOM_MULTIPOLYGON:
7460
     res.set(STRING_WITH_LEN("multipolygon"), cs);
7461 7462
     break;
    case GEOM_GEOMETRYCOLLECTION:
7463
     res.set(STRING_WITH_LEN("geometrycollection"), cs);
7464 7465
     break;
    default:
7466
     res.set(STRING_WITH_LEN("geometry"), cs);
7467
  }
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
7468 7469 7470
}


7471 7472 7473 7474 7475 7476 7477 7478
int Field_geom::store(double nr)
{
  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
             ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
  return -1;
}


7479
int Field_geom::store(longlong nr, bool unsigned_val)
7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494
{
  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
             ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
  return -1;
}


int Field_geom::store_decimal(const my_decimal *)
{
  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
             ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
  return -1;
}


ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
7495 7496 7497 7498 7499 7500
int Field_geom::store(const char *from, uint length, CHARSET_INFO *cs)
{
  if (!length)
    bzero(ptr, Field_blob::pack_length());
  else
  {
7501 7502
    if (from == Geometry::bad_geometry_data.ptr())
      goto err;
7503 7504 7505 7506
    // Check given WKB
    uint32 wkb_type;
    if (length < SRID_SIZE + WKB_HEADER_SIZE + SIZEOF_STORED_DOUBLE*2)
      goto err;
7507
    wkb_type= uint4korr(from + SRID_SIZE + 1);
7508
    if (wkb_type < (uint32) Geometry::wkb_point ||
7509
	wkb_type > (uint32) Geometry::wkb_last)
7510
      goto err;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
7511 7512 7513 7514 7515 7516 7517 7518 7519
    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;
7520 7521 7522

err:
  bzero(ptr, Field_blob::pack_length());  
7523 7524
  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
             ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
7525
  return -1;
ram@mysql.r18.ru's avatar
ram@mysql.r18.ru committed
7526 7527
}

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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552
/****************************************************************************
** 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
kent@mysql.com's avatar
kent@mysql.com committed
7553
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7554 7555 7556 7557 7558 7559 7560 7561 7562 7563
  {
    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
kent@mysql.com's avatar
kent@mysql.com committed
7564
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7565 7566 7567 7568 7569 7570 7571 7572 7573
  {
    int4store(ptr,value);
  }
  else
#endif
    longstore(ptr,(long) value);
  break;
  case 8:
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
7574
  if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589
  {
    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
7590
int Field_enum::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7591
{
7592
  int err= 0;
7593
  uint32 not_used;
7594
  char buff[STRING_BUFFER_USUAL_SIZE];
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7595
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
7596

7597
  /* Convert character set if necessary */
7598
  if (String::needs_conversion(length, cs, field_charset, &not_used))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7599
  { 
7600 7601
    uint dummy_errors;
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7602 7603 7604
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
7605 7606

  /* Remove end space */
7607
  length= field_charset->cset->lengthsp(field_charset, from, length);
7608
  uint tmp=find_type2(typelib, from, length, field_charset);
7609
  if (!tmp)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7610
  {
7611
    if (length < 6) // Can't be more than 99999 enums
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7612
    {
7613
      /* This is for reading numbers with LOAD DATA INFILE */
7614
      char *end;
7615 7616
      tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
      if (err || end != from+length || tmp > typelib->count)
7617 7618
      {
	tmp=0;
7619
	set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
7620
      }
7621 7622
      if (!table->in_use->count_cuted_fields)
        err= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7623 7624
    }
    else
7625
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7626
  }
7627
  store_type((ulonglong) tmp);
7628
  return err;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7629 7630 7631
}


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7632
int Field_enum::store(double nr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7633
{
7634
  return Field_enum::store((longlong) nr, FALSE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7635 7636 7637
}


7638
int Field_enum::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7639
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7640
  int error= 0;
7641
  if ((ulonglong) nr > typelib->count || nr == 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7642
  {
7643
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
7644 7645 7646 7647 7648
    if (nr != 0 || table->in_use->count_cuted_fields)
    {
      nr= 0;
      error= 1;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7649 7650
  }
  store_type((ulonglong) (uint) nr);
7651
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669
}


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
kent@mysql.com's avatar
kent@mysql.com committed
7670
      if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682
	tmp=sint2korr(ptr);
      else
#endif
	shortget(tmp,ptr);
      return (longlong) tmp;
    }
  case 3:
    return (longlong) uint3korr(ptr);
  case 4:
    {
      uint32 tmp;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
7683
      if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7684 7685 7686 7687 7688 7689 7690 7691 7692 7693
	tmp=uint4korr(ptr);
      else
#endif
	longget(tmp,ptr);
      return (longlong) tmp;
    }
  case 8:
    {
      longlong tmp;
#ifdef WORDS_BIGENDIAN
kent@mysql.com's avatar
kent@mysql.com committed
7694
      if (table->s->db_low_byte_first)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710
	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
7711
    val_ptr->set("", 0, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7712 7713
  else
    val_ptr->set((const char*) typelib->type_names[tmp-1],
7714
		 typelib->type_lengths[tmp-1],
7715
		 field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743
  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
{
7744 7745 7746
  char buffer[255];
  String enum_item(buffer, sizeof(buffer), res.charset());

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7747
  res.length(0);
7748
  res.append(STRING_WITH_LEN("enum("));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7749 7750

  bool flag=0;
7751 7752
  uint *len= typelib->type_lengths;
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7753
  {
7754
    uint dummy_errors;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7755 7756
    if (flag)
      res.append(',');
7757
    /* convert to res.charset() == utf8, then quote */
7758
    enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
7759 7760
    append_unescaped(&res, enum_item.ptr(), enum_item.length());
    flag= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7761 7762 7763 7764 7765
  }
  res.append(')');
}


7766 7767 7768 7769 7770 7771 7772 7773 7774 7775
Field *Field_enum::new_field(MEM_ROOT *root, struct st_table *new_table,
                             bool keep_type)
{
  Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
  if (res)
    res->typelib= copy_typelib(root, typelib);
  return res;
}


7776 7777 7778 7779 7780 7781 7782 7783
/*
   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
7784 7785


monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7786
int Field_set::store(const char *from,uint length,CHARSET_INFO *cs)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7787
{
monty@mysql.com's avatar
monty@mysql.com committed
7788
  bool got_warning= 0;
7789
  int err= 0;
7790 7791
  char *not_used;
  uint not_used2;
7792
  uint32 not_used_offset;
7793
  char buff[STRING_BUFFER_USUAL_SIZE];
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7794
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7795

7796
  /* Convert character set if necessary */
7797
  if (String::needs_conversion(length, cs, field_charset, &not_used_offset))
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7798
  { 
7799 7800
    uint dummy_errors;
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
7801 7802 7803
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
7804 7805
  ulonglong tmp= find_set(typelib, from, length, field_charset,
                          &not_used, &not_used2, &got_warning);
7806 7807 7808
  if (!tmp && length && length < 22)
  {
    /* This is for reading numbers with LOAD DATA INFILE */
7809
    char *end;
7810 7811
    tmp=my_strntoull(cs,from,length,10,&end,&err);
    if (err || end != from+length ||
7812
	tmp > (ulonglong) (((longlong) 1 << typelib->count) - (longlong) 1))
7813
    {
7814
      tmp=0;      
7815
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
7816
    }
7817
  }
monty@mysql.com's avatar
monty@mysql.com committed
7818
  else if (got_warning)
7819
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
7820
  store_type(tmp);
7821
  return err;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7822 7823 7824
}


7825
int Field_set::store(longlong nr, bool unsigned_val)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7826
{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7827
  int error= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7828 7829 7830
  if ((ulonglong) nr > (ulonglong) (((longlong) 1 << typelib->count) -
				    (longlong) 1))
  {
7831
    nr&= (longlong) (((longlong) 1 << typelib->count) - (longlong) 1);    
7832
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
7833
    error=1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7834 7835
  }
  store_type((ulonglong) nr);
7836
  return error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7837 7838 7839 7840 7841 7842 7843 7844 7845 7846
}


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);
7847
  val_buffer->set_charset(field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7848 7849 7850 7851 7852
  while (tmp && bitnr < (uint) typelib->count)
  {
    if (tmp & 1)
    {
      if (val_buffer->length())
7853
	val_buffer->append(&field_separator, 1, &my_charset_latin1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7854
      String str(typelib->type_names[bitnr],
7855
		 typelib->type_lengths[bitnr],
7856
		 field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867
      val_buffer->append(str);
    }
    tmp>>=1;
    bitnr++;
  }
  return val_buffer;
}


void Field_set::sql_type(String &res) const
{
7868 7869 7870
  char buffer[255];
  String set_item(buffer, sizeof(buffer), res.charset());

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7871
  res.length(0);
7872
  res.append(STRING_WITH_LEN("set("));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7873 7874

  bool flag=0;
7875 7876
  uint *len= typelib->type_lengths;
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7877
  {
7878
    uint dummy_errors;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7879 7880
    if (flag)
      res.append(',');
7881
    /* convert to res.charset() == utf8, then quote */
7882
    set_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
7883 7884
    append_unescaped(&res, set_item.ptr(), set_item.length());
    flag= 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7885 7886 7887 7888 7889 7890 7891 7892
  }
  res.append(')');
}

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

bool Field::eq_def(Field *field)
{
7893
  if (real_type() != field->real_type() || charset() != field->charset() ||
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7894 7895 7896 7897 7898 7899 7900 7901 7902 7903
      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
7904

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7905 7906 7907
  if (typelib->count < from_lib->count)
    return 0;
  for (uint i=0 ; i < from_lib->count ; i++)
7908 7909
    if (my_strnncoll(field_charset,
                     (const uchar*)typelib->type_names[i],
7910
                     (uint) strlen(typelib->type_names[i]),
7911
                     (const uchar*)from_lib->type_names[i],
7912
                     (uint) strlen(from_lib->type_names[i])))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7913 7914 7915 7916 7917
      return 0;
  return 1;
}

bool Field_num::eq_def(Field *field)
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7918
{
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930
  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;
}


ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959
/*
  Bit field.

  We store the first 0 - 6 uneven bits among the null bits 
  at the start of the record. The rest bytes are stored in 
  the record itself.

  For example:

  CREATE TABLE t1 (a int, b bit(17), c bit(21) not null, d bit(8));
  We would store data  as follows in the record:

  Byte        Bit
  1           7 - reserve for delete
              6 - null bit for 'a'
              5 - null bit for 'b'
              4 - first (high) bit of 'b'
              3 - first (high) bit of 'c'
              2 - second bit of 'c'
              1 - third bit of 'c'
              0 - forth bit of 'c'
  2           7 - firth bit of 'c'
              6 - null bit for 'd'
  3 - 6       four bytes for 'a'
  7 - 8       two bytes for 'b'
  9 - 10      two bytes for 'c'
  11          one byte for 'd'
*/

monty@mysql.com's avatar
monty@mysql.com committed
7960 7961 7962 7963
Field_bit::Field_bit(char *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
                     uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
                     enum utype unireg_check_arg, const char *field_name_arg,
                     struct st_table *table_arg)
7964
  : Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
monty@mysql.com's avatar
monty@mysql.com committed
7965
          unireg_check_arg, field_name_arg, table_arg),
7966 7967
    bit_ptr(bit_ptr_arg), bit_ofs(bit_ofs_arg), bit_len(len_arg & 7),
    bytes_in_rec(len_arg / 8)
monty@mysql.com's avatar
monty@mysql.com committed
7968
{
7969
  flags|= UNSIGNED_FLAG;
monty@mysql.com's avatar
monty@mysql.com committed
7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998
  /*
    Ensure that Field::eq() can distinguish between two different bit fields.
    (two bit fields that are not null, may have same ptr and null_ptr)
  */
  if (!null_ptr_arg)
    null_bit= bit_ofs_arg;
}


Field *Field_bit::new_key_field(MEM_ROOT *root,
                                struct st_table *new_table,
                                char *new_ptr, uchar *new_null_ptr,
                                uint new_null_bit)
{
  Field_bit *res;
  if ((res= (Field_bit*) Field::new_key_field(root, new_table,
                                              new_ptr, new_null_ptr,
                                              new_null_bit)))
  {
    /* Move bits normally stored in null_pointer to new_ptr */
    res->bit_ptr= (uchar*) new_ptr;
    res->bit_ofs= 0;
    if (bit_len)
      res->ptr++;                               // Store rest of data here
  }
  return res;
}


ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
7999 8000 8001 8002
int Field_bit::store(const char *from, uint length, CHARSET_INFO *cs)
{
  int delta;

8003
  for (; length && !*from; from++, length--);          // skip left 0's
8004
  delta= bytes_in_rec - length;
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8005 8006 8007 8008 8009

  if (delta < -1 ||
      (delta == -1 && (uchar) *from > ((1 << bit_len) - 1)) ||
      (!bit_len && delta < 0))
  {
8010
    set_rec_bits((1 << bit_len) - 1, bit_ptr, bit_ofs, bit_len);
8011
    memset(ptr, 0xff, bytes_in_rec);
8012 8013 8014 8015
    if (table->in_use->really_abort_on_warning())
      set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
    else
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038
    return 1;
  }
  /* delta is >= -1 here */
  if (delta > 0)
  {
    if (bit_len)
      clr_rec_bits(bit_ptr, bit_ofs, bit_len);
    bzero(ptr, delta);
    memcpy(ptr + delta, from, length);
  }
  else if (delta == 0)
  {
    if (bit_len)
      clr_rec_bits(bit_ptr, bit_ofs, bit_len);
    memcpy(ptr, from, length);
  }
  else
  {
    if (bit_len)
    {
      set_rec_bits((uchar) *from, bit_ptr, bit_ofs, bit_len);
      from++;
    }
8039
    memcpy(ptr, from, bytes_in_rec);
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8040 8041 8042 8043 8044 8045 8046
  }
  return 0;
}


int Field_bit::store(double nr)
{
8047
  return store((longlong) nr, FALSE);
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8048 8049 8050
}


8051
int Field_bit::store(longlong nr, bool unsigned_val)
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8052 8053 8054 8055 8056 8057 8058 8059
{
  char buf[8];

  mi_int8store(buf, nr);
  return store(buf, 8, NULL);
}


8060 8061 8062 8063
int Field_bit::store_decimal(const my_decimal *val)
{
  int err= 0;
  longlong i= convert_decimal2longlong(val, 1, &err);
8064
  return test(err | store(i, TRUE));
8065 8066 8067
}


ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8068 8069 8070 8071 8072 8073 8074 8075 8076 8077
double Field_bit::val_real(void)
{
  return (double) Field_bit::val_int();
}


longlong Field_bit::val_int(void)
{
  ulonglong bits= 0;
  if (bit_len)
8078
  {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8079
    bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
8080
    bits<<= (bytes_in_rec * 8);
8081
  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8082

8083
  switch (bytes_in_rec) {
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8084 8085 8086 8087 8088 8089 8090 8091
  case 0: return bits;
  case 1: return bits | (ulonglong) (uchar) ptr[0];
  case 2: return bits | mi_uint2korr(ptr);
  case 3: return bits | mi_uint3korr(ptr);
  case 4: return bits | mi_uint4korr(ptr);
  case 5: return bits | mi_uint5korr(ptr);
  case 6: return bits | mi_uint6korr(ptr);
  case 7: return bits | mi_uint7korr(ptr);
8092
  default: return mi_uint8korr(ptr + bytes_in_rec - sizeof(longlong));
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8093 8094 8095 8096 8097 8098 8099
  }
}  


String *Field_bit::val_str(String *val_buffer,
                           String *val_ptr __attribute__((unused)))
{
monty@mysql.com's avatar
monty@mysql.com committed
8100
  char buff[sizeof(longlong)];
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8101 8102
  uint length= min(pack_length(), sizeof(longlong));
  ulonglong bits= val_int();
monty@mysql.com's avatar
monty@mysql.com committed
8103
  mi_int8store(buff,bits);
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8104 8105

  val_buffer->alloc(length);
monty@mysql.com's avatar
monty@mysql.com committed
8106
  memcpy_fixed((char*) val_buffer->ptr(), buff+8-length, length);
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8107 8108 8109 8110 8111 8112
  val_buffer->length(length);
  val_buffer->set_charset(&my_charset_bin);
  return val_buffer;
}


8113 8114 8115 8116 8117 8118 8119
my_decimal *Field_bit::val_decimal(my_decimal *deciaml_value)
{
  int2my_decimal(E_DEC_FATAL_ERROR, val_int(), 1, deciaml_value);
  return deciaml_value;
}


ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8120 8121 8122 8123 8124 8125
int Field_bit::key_cmp(const byte *str, uint length)
{
  if (bit_len)
  {
    int flag;
    uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
monty@mysql.com's avatar
monty@mysql.com committed
8126
    if ((flag= (int) (bits - *(uchar*) str)))
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8127 8128 8129 8130
      return flag;
    str++;
    length--;
  }
ramil@mysql.com's avatar
ramil@mysql.com committed
8131
  return memcmp(ptr, str, length);
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144
}


int Field_bit::cmp_offset(uint row_offset)
{
  if (bit_len)
  {
    int flag;
    uchar bits_a= get_rec_bits(bit_ptr, bit_ofs, bit_len);
    uchar bits_b= get_rec_bits(bit_ptr + row_offset, bit_ofs, bit_len);
    if ((flag= (int) (bits_a - bits_b)))
      return flag;
  }
8145
  return memcmp(ptr, ptr + row_offset, bytes_in_rec);
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8146 8147 8148
}


8149
uint Field_bit::get_key_image(char *buff, uint length, imagetype type_arg)
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8150 8151 8152 8153 8154 8155 8156
{
  if (bit_len)
  {
    uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
    *buff++= bits;
    length--;
  }
8157 8158 8159
  uint data_length = min(length, bytes_in_rec);
  memcpy(buff, ptr, data_length);
  return data_length + 1;
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8160 8161 8162 8163 8164 8165 8166
}


void Field_bit::sql_type(String &res) const
{
  CHARSET_INFO *cs= res.charset();
  ulong length= cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
8167
                                   "bit(%d)", (int) field_length);
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8168 8169 8170 8171 8172 8173
  res.length((uint) length);
}


char *Field_bit::pack(char *to, const char *from, uint max_length)
{
8174 8175
  DBUG_ASSERT(max_length);
  uint length;
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8176 8177 8178 8179 8180
  if (bit_len)
  {
    uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
    *to++= bits;
  }
8181 8182
  length= min(bytes_in_rec, max_length - (bit_len > 0));
  memcpy(to, from, length);
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193
  return to + length;
}


const char *Field_bit::unpack(char *to, const char *from)
{
  if (bit_len)
  {
    set_rec_bits(*from, bit_ptr, bit_ofs, bit_len);
    from++;
  }
8194 8195
  memcpy(to, from, bytes_in_rec);
  return from + bytes_in_rec;
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8196 8197 8198
}


8199 8200 8201 8202 8203
/*
  Bit field support for non-MyISAM tables.
*/

Field_bit_as_char::Field_bit_as_char(char *ptr_arg, uint32 len_arg,
8204 8205
                                     uchar *null_ptr_arg, uchar null_bit_arg,
                                     enum utype unireg_check_arg,
8206 8207
                                     const char *field_name_arg,
                                     struct st_table *table_arg)
8208
  : Field_bit(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, 0,
8209
              0, unireg_check_arg, field_name_arg, table_arg)
8210
{
8211
  flags|= UNSIGNED_FLAG;
8212
  bit_len= 0;
8213
  bytes_in_rec= (len_arg + 7) / 8;
8214 8215 8216 8217 8218 8219
}


int Field_bit_as_char::store(const char *from, uint length, CHARSET_INFO *cs)
{
  int delta;
8220
  uchar bits= (uchar) (field_length & 7);
8221

8222
  for (; length && !*from; from++, length--);          // skip left 0's
8223
  delta= bytes_in_rec - length;
8224 8225 8226 8227

  if (delta < 0 ||
      (delta == 0 && bits && (uint) (uchar) *from >= (uint) (1 << bits)))
  {
8228
    memset(ptr, 0xff, bytes_in_rec);
8229 8230
    if (bits)
      *ptr&= ((1 << bits) - 1); /* set first byte */
8231 8232 8233 8234
    if (table->in_use->really_abort_on_warning())
      set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
    else
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246
    return 1;
  }
  bzero(ptr, delta);
  memcpy(ptr + delta, from, length);
  return 0;
}


void Field_bit_as_char::sql_type(String &res) const
{
  CHARSET_INFO *cs= res.charset();
  ulong length= cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
8247
                                   "bit(%d)", (int) field_length);
8248 8249 8250 8251
  res.length((uint) length);
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
8252
/*****************************************************************************
8253
  Handling of field and create_field
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8254 8255
*****************************************************************************/

8256 8257 8258 8259 8260 8261 8262
/*
  Convert create_field::length from number of characters to number of bytes

  SYNOPSIS
    create_field::create_length_to_internal_length()
  
  DESCRIPTION
8263
    Convert create_field::length from number of characters to number of bytes.
8264 8265
*/

8266 8267
void create_field::create_length_to_internal_length(void)
{
8268 8269 8270 8271 8272 8273 8274
  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:
8275
  case MYSQL_TYPE_VARCHAR:
8276
    length*= charset->mbmaxlen;
8277
    key_length= length;
8278
    pack_length= calc_pack_length(sql_type, length);
8279 8280 8281
    break;
  case MYSQL_TYPE_ENUM:
  case MYSQL_TYPE_SET:
8282
    /* Pack_length already calculated in sql_parse.cc */
8283
    length*= charset->mbmaxlen;
8284
    key_length= pack_length;
8285
    break;
monty@mysql.com's avatar
monty@mysql.com committed
8286
  case MYSQL_TYPE_BIT:
8287 8288 8289 8290 8291 8292 8293 8294 8295 8296
    if (f_bit_as_char(pack_flag))
    {
      key_length= pack_length= ((length + 7) & ~7) / 8;
    }
    else
    {
      pack_length= length / 8;
      /* We need one extra byte to store the bits we save among the null bits */
      key_length= pack_length + test(length & 7);
    }
monty@mysql.com's avatar
monty@mysql.com committed
8297
    break;
8298
  case MYSQL_TYPE_NEWDECIMAL:
8299 8300 8301 8302 8303 8304
    key_length= pack_length=
      my_decimal_get_binary_size(my_decimal_length_to_precision(length,
								decimals,
								flags &
								UNSIGNED_FLAG),
				 decimals);
8305
    break;
8306
  default:
8307
    key_length= pack_length= calc_pack_length(sql_type, length);
8308
    break;
8309 8310 8311
  }
}

8312

8313
void create_field::init_for_tmp_table(enum_field_types sql_type_arg,
8314
                                      uint32 length_arg, uint32 decimals_arg,
8315 8316 8317 8318
                                      bool maybe_null, bool is_unsigned)
{
  field_name= "";
  sql_type= sql_type_arg;
8319
  char_length= length= length_arg;;
8320 8321 8322 8323 8324
  unireg_check= Field::NONE;
  interval= 0;
  charset= &my_charset_bin;
  geom_type= Field::GEOM_GEOMETRY;
  pack_flag= (FIELDFLAG_NUMBER |
8325
              ((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
8326 8327 8328 8329 8330
              (maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
              (is_unsigned ? 0 : FIELDFLAG_DECIMAL));
}


8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390
/*
  Initialize field definition for create

  SYNOPSIS
    thd                   Thread handle
    fld_name              Field name
    fld_type              Field type
    fld_length            Field length
    fld_decimals          Decimal (if any)
    fld_type_modifier     Additional type information
    fld_default_value     Field default value (if any)
    fld_on_update_value   The value of ON UPDATE clause
    fld_comment           Field comment
    fld_change            Field change
    fld_interval_list     Interval list (if any)
    fld_charset           Field charset
    fld_geom_type         Field geometry type (if any)

  RETURN
    FALSE on success
    TRUE  on error
*/

bool create_field::init(THD *thd, char *fld_name, enum_field_types fld_type,
                        char *fld_length, char *fld_decimals,
                        uint fld_type_modifier, Item *fld_default_value,
                        Item *fld_on_update_value, LEX_STRING *fld_comment,
                        char *fld_change, List<String> *fld_interval_list,
                        CHARSET_INFO *fld_charset, uint fld_geom_type)
{
  uint sign_len, allowed_type_modifier= 0;
  ulong max_field_charlength= MAX_FIELD_CHARLENGTH;

  DBUG_ENTER("create_field::init()");
  
  field= 0;
  field_name= fld_name;
  def= fld_default_value;
  flags= fld_type_modifier;
  unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
                 Field::NEXT_NUMBER : Field::NONE);
  decimals= fld_decimals ? (uint)atoi(fld_decimals) : 0;
  if (decimals >= NOT_FIXED_DEC)
  {
    my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
             NOT_FIXED_DEC-1);
    DBUG_RETURN(TRUE);
  }

  sql_type= fld_type;
  length= 0;
  change= fld_change;
  interval= 0;
  pack_length= key_length= 0;
  charset= fld_charset;
  geom_type= (Field::geometry_type) fld_geom_type;
  interval_list.empty();

  comment= *fld_comment;
  /*
8391 8392
    Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
    it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430
  */
  if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != FIELD_TYPE_TIMESTAMP)
    flags|= NO_DEFAULT_VALUE_FLAG;

  if (fld_length && !(length= (uint) atoi(fld_length)))
    fld_length= 0; /* purecov: inspected */
  sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;

  switch (fld_type) {
  case FIELD_TYPE_TINY:
    if (!fld_length)
      length= MAX_TINYINT_WIDTH+sign_len;
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
    break;
  case FIELD_TYPE_SHORT:
    if (!fld_length)
      length= MAX_SMALLINT_WIDTH+sign_len;
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
    break;
  case FIELD_TYPE_INT24:
    if (!fld_length)
      length= MAX_MEDIUMINT_WIDTH+sign_len;
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
    break;
  case FIELD_TYPE_LONG:
    if (!fld_length)
      length= MAX_INT_WIDTH+sign_len;
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
    break;
  case FIELD_TYPE_LONGLONG:
    if (!fld_length)
      length= MAX_BIGINT_WIDTH;
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
    break;
  case FIELD_TYPE_NULL:
    break;
  case FIELD_TYPE_NEWDECIMAL:
8431
    my_decimal_trim(&length, &decimals);
8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467
    if (length > DECIMAL_MAX_PRECISION)
    {
      my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
               DECIMAL_MAX_PRECISION);
      DBUG_RETURN(TRUE);
    }
    if (length < decimals)
    {
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
      DBUG_RETURN(TRUE);
    }
    length=
      my_decimal_precision_to_length(length, decimals,
                                     fld_type_modifier & UNSIGNED_FLAG);
    pack_length=
      my_decimal_get_binary_size(length, decimals);
    break;
  case MYSQL_TYPE_VARCHAR:
    /*
      Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
      if they don't have a default value
    */
    max_field_charlength= MAX_FIELD_VARCHARLENGTH;
    break;
  case MYSQL_TYPE_STRING:
    break;
  case FIELD_TYPE_BLOB:
  case FIELD_TYPE_TINY_BLOB:
  case FIELD_TYPE_LONG_BLOB:
  case FIELD_TYPE_MEDIUM_BLOB:
  case FIELD_TYPE_GEOMETRY:
    if (fld_default_value)
    {
      /* Allow empty as default value. */
      String str,*res;
      res= fld_default_value->val_str(&str);
8468 8469 8470 8471 8472 8473 8474
      /*
        A default other than '' is always an error, and any non-NULL
        specified default is an error in strict mode.
      */
      if (res->length() || (thd->variables.sql_mode &
                            (MODE_STRICT_TRANS_TABLES |
                             MODE_STRICT_ALL_TABLES)))
8475 8476 8477 8478 8479
      {
        my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
                 fld_name); /* purecov: inspected */
        DBUG_RETURN(TRUE);
      }
8480 8481 8482 8483 8484 8485 8486 8487 8488 8489
      else
      {
        /*
          Otherwise a default of '' is just a warning.
        */
        push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                            ER_BLOB_CANT_HAVE_DEFAULT,
                            ER(ER_BLOB_CANT_HAVE_DEFAULT),
                            fld_name);
      }
8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547
      def= 0;
    }
    flags|= BLOB_FLAG;
    break;
  case FIELD_TYPE_YEAR:
    if (!fld_length || length != 2)
      length= 4; /* Default length */
    flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
    break;
  case FIELD_TYPE_FLOAT:
    /* change FLOAT(precision) to FLOAT or DOUBLE */
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
    if (fld_length && !fld_decimals)
    {
      uint tmp_length= length;
      if (tmp_length > PRECISION_FOR_DOUBLE)
      {
        my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
        DBUG_RETURN(TRUE);
      }
      else if (tmp_length > PRECISION_FOR_FLOAT)
      {
        sql_type= FIELD_TYPE_DOUBLE;
        length= DBL_DIG+7; /* -[digits].E+### */
      }
      else
        length= FLT_DIG+6; /* -[digits].E+## */
      decimals= NOT_FIXED_DEC;
      break;
    }
    if (!fld_length && !fld_decimals)
    {
      length=  FLT_DIG+6;
      decimals= NOT_FIXED_DEC;
    }
    if (length < decimals &&
        decimals != NOT_FIXED_DEC)
    {
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
      DBUG_RETURN(TRUE);
    }
    break;
  case FIELD_TYPE_DOUBLE:
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
    if (!fld_length && !fld_decimals)
    {
      length= DBL_DIG+7;
      decimals= NOT_FIXED_DEC;
    }
    if (length < decimals &&
        decimals != NOT_FIXED_DEC)
    {
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
      DBUG_RETURN(TRUE);
    }
    break;
  case FIELD_TYPE_TIMESTAMP:
    if (!fld_length)
8548 8549 8550 8551 8552
    {
      /* Compressed date YYYYMMDDHHMMSS */
      length= MAX_DATETIME_COMPRESSED_WIDTH;
    }
    else if (length != MAX_DATETIME_WIDTH)
8553 8554 8555 8556 8557 8558
    {
      /*
        We support only even TIMESTAMP lengths less or equal than 14
        and 19 as length of 4.1 compatible representation.
      */
      length= ((length+1)/2)*2; /* purecov: inspected */
8559
      length= min(length, MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611
    }
    flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
    if (fld_default_value)
    {
      /* Grammar allows only NOW() value for ON UPDATE clause */
      if (fld_default_value->type() == Item::FUNC_ITEM && 
          ((Item_func*)fld_default_value)->functype() == Item_func::NOW_FUNC)
      {
        unireg_check= (fld_on_update_value ? Field::TIMESTAMP_DNUN_FIELD:
                                             Field::TIMESTAMP_DN_FIELD);
        /*
          We don't need default value any longer moreover it is dangerous.
          Everything handled by unireg_check further.
        */
        def= 0;
      }
      else
        unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
                                             Field::NONE);
    }
    else
    {
      /*
        If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
        or ON UPDATE values then for the sake of compatiblity we should treat
        this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
        have another TIMESTAMP column with auto-set option before this one)
        or DEFAULT 0 (in other cases).
        So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
        replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
        information about all TIMESTAMP fields in table will be availiable.

        If we have TIMESTAMP NULL column without explicit DEFAULT value
        we treat it as having DEFAULT NULL attribute.
      */
      unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD :
                     (flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
                                              Field::NONE));
    }
    break;
  case FIELD_TYPE_DATE:
    /* Old date type. */
    if (protocol_version != PROTOCOL_VERSION-1)
      sql_type= FIELD_TYPE_NEWDATE;
    /* fall trough */
  case FIELD_TYPE_NEWDATE:
    length= 10;
    break;
  case FIELD_TYPE_TIME:
    length= 10;
    break;
  case FIELD_TYPE_DATETIME:
8612
    length= MAX_DATETIME_WIDTH;
8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660
    break;
  case FIELD_TYPE_SET:
    {
      pack_length= get_set_pack_length(fld_interval_list->elements);

      List_iterator<String> it(*fld_interval_list);
      String *tmp;
      while ((tmp= it++))
        interval_list.push_back(tmp);
      /*
        Set fake length to 1 to pass the below conditions.
        Real length will be set in mysql_prepare_table()
        when we know the character set of the column
      */
      length= 1;
      break;
    }
  case FIELD_TYPE_ENUM:
    {
      /* Should be safe. */
      pack_length= get_enum_pack_length(fld_interval_list->elements);

      List_iterator<String> it(*fld_interval_list);
      String *tmp;
      while ((tmp= it++))
        interval_list.push_back(tmp);
      length= 1; /* See comment for FIELD_TYPE_SET above. */
      break;
   }
  case MYSQL_TYPE_VAR_STRING:
    DBUG_ASSERT(0);  /* Impossible. */
    break;
  case MYSQL_TYPE_BIT:
    {
      if (!fld_length)
        length= 1;
      if (length > MAX_BIT_FIELD_LENGTH)
      {
        my_error(ER_TOO_BIG_DISPLAYWIDTH, MYF(0), fld_name,
                 MAX_BIT_FIELD_LENGTH);
        DBUG_RETURN(TRUE);
      }
      pack_length= (length + 7) / 8;
      break;
    }
  case FIELD_TYPE_DECIMAL:
    DBUG_ASSERT(0); /* Was obsolete */
  }
8661 8662
  /* Remember the value of length */
  char_length= length;
8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690

  if (!(flags & BLOB_FLAG) &&
      ((length > max_field_charlength && fld_type != FIELD_TYPE_SET &&
        fld_type != FIELD_TYPE_ENUM &&
        (fld_type != MYSQL_TYPE_VARCHAR || fld_default_value)) ||
       (!length &&
        fld_type != MYSQL_TYPE_STRING &&
        fld_type != MYSQL_TYPE_VARCHAR && fld_type != FIELD_TYPE_GEOMETRY)))
  {
    my_error((fld_type == MYSQL_TYPE_VAR_STRING ||
              fld_type == MYSQL_TYPE_VARCHAR ||
              fld_type == MYSQL_TYPE_STRING) ?  ER_TOO_BIG_FIELDLENGTH :
                                                ER_TOO_BIG_DISPLAYWIDTH,
              MYF(0),
              fld_name, max_field_charlength); /* purecov: inspected */
    DBUG_RETURN(TRUE);
  }
  fld_type_modifier&= AUTO_INCREMENT_FLAG;
  if ((~allowed_type_modifier) & fld_type_modifier)
  {
    my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
    DBUG_RETURN(TRUE);
  }

  DBUG_RETURN(FALSE); /* success */
}


8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705
enum_field_types get_blob_type_from_length(ulong length)
{
  enum_field_types type;
  if (length < 256)
    type= FIELD_TYPE_TINY_BLOB;
  else if (length < 65536)
    type= FIELD_TYPE_BLOB;
  else if (length < 256L*256L*256L)
    type= FIELD_TYPE_MEDIUM_BLOB;
  else
    type= FIELD_TYPE_LONG_BLOB;
  return type;
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
8706
/*
8707
  Make a field from the .frm file info
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8708 8709 8710 8711 8712
*/

uint32 calc_pack_length(enum_field_types type,uint32 length)
{
  switch (type) {
8713
  case MYSQL_TYPE_VAR_STRING:
8714
  case MYSQL_TYPE_STRING:
8715
  case FIELD_TYPE_DECIMAL:     return (length);
8716
  case MYSQL_TYPE_VARCHAR:     return (length + (length < 256 ? 1: 2));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731
  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
8732
  case FIELD_TYPE_BLOB:		return 2+portable_sizeof_char_ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8733 8734
  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
8735
  case FIELD_TYPE_GEOMETRY:	return 4+portable_sizeof_char_ptr;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8736
  case FIELD_TYPE_SET:
8737
  case FIELD_TYPE_ENUM:
8738 8739
  case FIELD_TYPE_NEWDECIMAL:
    abort(); return 0;                          // This shouldn't happen
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8740
  case FIELD_TYPE_BIT: return length / 8;
8741 8742
  default:
    return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760
  }
}


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,
8761
		  uchar *null_pos, uchar null_bit,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8762
		  uint pack_flag,
8763
		  enum_field_types field_type,
8764
		  CHARSET_INFO *field_charset,
8765
		  Field::geometry_type geom_type,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8766 8767 8768 8769 8770
		  Field::utype unireg_check,
		  TYPELIB *interval,
		  const char *field_name,
		  struct st_table *table)
{
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8771 8772 8773 8774
  uchar *bit_ptr;
  uchar bit_offset;
  LINT_INIT(bit_ptr);
  LINT_INIT(bit_offset);
8775
  if (field_type == FIELD_TYPE_BIT && !f_bit_as_char(pack_flag))
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8776 8777 8778 8779 8780 8781 8782 8783 8784 8785
  {
    bit_ptr= null_pos;
    bit_offset= null_bit;
    if (f_maybe_null(pack_flag))         // if null field
    {
       bit_ptr+= (null_bit == 7);        // shift bit_ptr and bit_offset
       bit_offset= (bit_offset + 1) & 7;
    }
  }

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8786 8787 8788 8789 8790
  if (!f_maybe_null(pack_flag))
  {
    null_pos=0;
    null_bit=0;
  }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8791 8792 8793 8794
  else
  {
    null_bit= ((uchar) 1) << null_bit;
  }
8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806

  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
8807 8808 8809
  if (f_is_alpha(pack_flag))
  {
    if (!f_is_packed(pack_flag))
monty@mysql.com's avatar
monty@mysql.com committed
8810
    {
8811
      if (field_type == MYSQL_TYPE_STRING ||
8812
          field_type == FIELD_TYPE_DECIMAL ||   // 3.23 or 4.0 string
8813
          field_type == MYSQL_TYPE_VAR_STRING)
monty@mysql.com's avatar
monty@mysql.com committed
8814 8815 8816
        return new Field_string(ptr,field_length,null_pos,null_bit,
                                unireg_check, field_name, table,
                                field_charset);
8817
      if (field_type == MYSQL_TYPE_VARCHAR)
8818 8819 8820
        return new Field_varstring(ptr,field_length,
                                   HA_VARCHAR_PACKLENGTH(field_length),
                                   null_pos,null_bit,
8821 8822
                                   unireg_check, field_name, table,
                                   field_charset);
monty@mysql.com's avatar
monty@mysql.com committed
8823 8824
      return 0;                                 // Error
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8825 8826 8827 8828 8829

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

hf@deer.(none)'s avatar
hf@deer.(none) committed
8830
#ifdef HAVE_SPATIAL
8831 8832
    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
8833
			    unireg_check, field_name, table,
8834
			    pack_length, geom_type);
hf@deer.(none)'s avatar
hf@deer.(none) committed
8835
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8836 8837 8838
    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
8839
			    pack_length, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8840 8841 8842 8843 8844
    if (interval)
    {
      if (f_is_enum(pack_flag))
	return new Field_enum(ptr,field_length,null_pos,null_bit,
				  unireg_check, field_name, table,
8845
				  pack_length, interval, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8846 8847 8848
      else
	return new Field_set(ptr,field_length,null_pos,null_bit,
			     unireg_check, field_name, table,
8849
			     pack_length, interval, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8850 8851 8852
    }
  }

8853
  switch (field_type) {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8854 8855 8856 8857 8858 8859
  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);
8860 8861 8862 8863 8864 8865
  case FIELD_TYPE_NEWDECIMAL:
    return new Field_new_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);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903
  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:
8904 8905 8906
    return new Field_timestamp(ptr,field_length, null_pos, null_bit,
                               unireg_check, field_name, table,
                               field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8907 8908 8909 8910 8911
  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,
8912
			  unireg_check, field_name, table, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8913 8914
  case FIELD_TYPE_NEWDATE:
    return new Field_newdate(ptr,null_pos,null_bit,
8915
			     unireg_check, field_name, table, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8916 8917
  case FIELD_TYPE_TIME:
    return new Field_time(ptr,null_pos,null_bit,
8918
			  unireg_check, field_name, table, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8919 8920
  case FIELD_TYPE_DATETIME:
    return new Field_datetime(ptr,null_pos,null_bit,
8921
			      unireg_check, field_name, table, field_charset);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8922
  case FIELD_TYPE_NULL:
8923
    return new Field_null(ptr,field_length,unireg_check,field_name,table, field_charset);
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8924
  case FIELD_TYPE_BIT:
8925
    return f_bit_as_char(pack_flag) ?
8926 8927
           new Field_bit_as_char(ptr, field_length, null_pos, null_bit,
                                 unireg_check, field_name, table) :
8928
           new Field_bit(ptr, field_length, null_pos, null_bit, bit_ptr,
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
8929
                         bit_offset, unireg_check, field_name, table);
8930 8931
  default:					// Impossible (Wrong version)
    break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8932
  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
8933
  return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8934 8935 8936 8937 8938
}


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

8939
create_field::create_field(Field *old_field,Field *orig_field)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8940 8941 8942 8943 8944 8945 8946
{
  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();
8947
  key_length= old_field->key_length();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8948
  sql_type=   old_field->real_type();
8949 8950
  charset=    old_field->charset();		// May be NULL ptr
  comment=    old_field->comment;
8951
  decimals=   old_field->decimals();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8952 8953 8954

  /* Fix if the original table had 4 byte pointer blobs */
  if (flags & BLOB_FLAG)
8955
    pack_length= (pack_length- old_field->table->s->blob_ptr_size +
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8956
		  portable_sizeof_char_ptr);
8957

8958 8959 8960 8961 8962 8963 8964 8965
  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;
    }
8966
    length/= charset->mbmaxlen;
8967 8968
    key_length/= charset->mbmaxlen;
    break;
8969
  case MYSQL_TYPE_STRING:
8970
    /* Change CHAR -> VARCHAR if dynamic record length */
8971 8972 8973 8974 8975 8976 8977 8978
    if (old_field->type() == MYSQL_TYPE_VAR_STRING)
      sql_type= MYSQL_TYPE_VARCHAR;
    /* fall through */

  case MYSQL_TYPE_ENUM:
  case MYSQL_TYPE_SET:
  case MYSQL_TYPE_VARCHAR:
  case MYSQL_TYPE_VAR_STRING:
8979
    /* This is corrected in create_length_to_internal_length */
8980 8981 8982 8983 8984 8985 8986 8987 8988
    length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
    break;
#ifdef HAVE_SPATIAL
  case FIELD_TYPE_GEOMETRY:
    geom_type= ((Field_geom*)old_field)->geom_type;
    break;
#endif
  default:
    break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8989
  }
8990

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8991 8992 8993 8994
  if (flags & (ENUM_FLAG | SET_FLAG))
    interval= ((Field_enum*) old_field)->typelib;
  else
    interval=0;
8995
  def=0;
8996
  char_length= length;
8997

8998
  if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
8999 9000 9001 9002
      old_field->ptr && orig_field &&
      (sql_type != FIELD_TYPE_TIMESTAMP ||                /* set def only if */
       old_field->table->timestamp_field != old_field ||  /* timestamp field */ 
       unireg_check == Field::TIMESTAMP_UN_FIELD))        /* has default val */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9003
  {
9004
    my_ptrdiff_t diff;
9005

9006
    /* Get the value from default_values */
9007 9008
    diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
                          orig_field->table->record[0]);
9009
    orig_field->move_field(diff);		// Points now at default_values
9010
    if (!orig_field->is_real_null())
9011
    {
9012 9013 9014
      char buff[MAX_FIELD_WIDTH],*pos;
      String tmp(buff,sizeof(buff), charset), *res;
      res= orig_field->val_str(&tmp);
9015 9016
      pos= (char*) sql_strmake(res->ptr(), res->length());
      def= new Item_string(pos, res->length(), charset);
9017
    }
9018
    orig_field->move_field(-diff);		// Back to record[0]
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9019 9020
  }
}
9021 9022


9023 9024 9025 9026
/*
  maximum possible display length for blob

  SYNOPSIS
9027
    Field_blob::max_display_length()
9028 9029 9030 9031

  RETURN
    length
*/
9032 9033

uint32 Field_blob::max_display_length()
9034 9035 9036 9037
{
  switch (packlength)
  {
  case 1:
9038
    return 255 * field_charset->mbmaxlen;
9039
  case 2:
9040
    return 65535 * field_charset->mbmaxlen;
9041
  case 3:
9042
    return 16777215 * field_charset->mbmaxlen;
9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054
  case 4:
    return (uint32) 4294967295U;
  default:
    DBUG_ASSERT(0); // we should never go here
    return 0;
  }
}


/*****************************************************************************
 Warning handling
*****************************************************************************/
9055 9056 9057 9058

/*
  Produce warning or note about data saved into field

9059
  SYNOPSIS
9060 9061 9062 9063 9064 9065 9066
    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
9067 9068 9069 9070
    if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.

    if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
    This allows us to avoid notes in optimisation, like convert_constant_item().
9071 9072

  RETURN VALUE
9073
    1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
9074
    0 otherwise
9075
*/
9076

9077
bool 
9078 9079
Field::set_warning(MYSQL_ERROR::enum_warning_level level, uint code,
                   int cuted_increment)
9080
{
9081 9082 9083 9084 9085
  /*
    If this field was created only for type conversion purposes it
    will have table == NULL.
  */
  THD *thd= table ? table->in_use : current_thd;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9086 9087
  if (thd->count_cuted_fields)
  {
9088
    thd->cuted_fields+= cuted_increment;
9089 9090
    push_warning_printf(thd, level, code, ER(code), field_name,
                        thd->row_count);
9091 9092
    return 0;
  }
9093
  return level >= MYSQL_ERROR::WARN_LEVEL_WARN;
9094 9095 9096 9097 9098 9099
}


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

9100
  SYNOPSIS
9101
    set_datime_warning()
9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113
      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.
*/
9114

9115
void 
9116
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
9117 9118 9119
                            const char *str, uint str_length, 
                            timestamp_type ts_type, int cuted_increment)
{
evgen@moonbone.local's avatar
evgen@moonbone.local committed
9120
  THD *thd= table ? table->in_use : current_thd;
9121 9122
  if ((thd->really_abort_on_warning() &&
       level >= MYSQL_ERROR::WARN_LEVEL_WARN) ||
9123
      set_warning(level, code, cuted_increment))
9124
    make_truncated_value_warning(thd, level, str, str_length, ts_type,
9125
                                 field_name);
9126 9127 9128 9129 9130 9131
}


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

9132
  SYNOPSIS
9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144
    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.
*/
9145

9146
void 
9147
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
9148 9149 9150
                            longlong nr, timestamp_type ts_type,
                            int cuted_increment)
{
evgen@moonbone.local's avatar
evgen@moonbone.local committed
9151 9152
  THD *thd= table ? table->in_use : current_thd;
  if (thd->really_abort_on_warning() ||
9153
      set_warning(level, code, cuted_increment))
9154 9155 9156
  {
    char str_nr[22];
    char *str_end= longlong10_to_str(nr, str_nr, -10);
9157
    make_truncated_value_warning(thd, level, str_nr, (uint) (str_end - str_nr), 
9158
                                 ts_type, field_name);
9159 9160 9161 9162 9163 9164 9165
  }
}


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

9166
  SYNOPSIS
9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177
    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.
*/
9178

9179
void 
9180
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
9181 9182
                            double nr, timestamp_type ts_type)
{
evgen@moonbone.local's avatar
evgen@moonbone.local committed
9183 9184
  THD *thd= table ? table->in_use : current_thd;
  if (thd->really_abort_on_warning() ||
9185
      set_warning(level, code, 1))
9186 9187 9188 9189
  {
    /* 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));
9190
    make_truncated_value_warning(thd, level, str_nr, str_len, ts_type,
9191
                                 field_name);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
9192
  }
9193
}
9194