field.cc 250 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000-2006 MySQL AB
unknown's avatar
unknown committed
2

unknown's avatar
unknown 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
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
6

unknown's avatar
unknown 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.
unknown's avatar
unknown committed
11

unknown's avatar
unknown 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
unknown's avatar
unknown 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

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

unknown's avatar
unknown committed
36
/*****************************************************************************
37
  Instansiate templates and static variables
unknown's avatar
unknown committed
38 39
*****************************************************************************/

40
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
unknown's avatar
unknown 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.
*/
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
76
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
77
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
86
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
87
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
100
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
101
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
102
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
103 104 105 106
  },
  /* MYSQL_TYPE_TINY -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
117
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
118
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
131
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
132
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
133
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
134 135 136 137
  },
  /* MYSQL_TYPE_SHORT -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
148
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
149
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
162
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
163
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
164
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
165 166 167 168
  },
  /* MYSQL_TYPE_LONG -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
175
    MYSQL_TYPE_LONG,         MYSQL_TYPE_VARCHAR,
176
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
unknown's avatar
unknown committed
177
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
178
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
unknown's avatar
unknown committed
179
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
180
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
193
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
194
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
206
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_VARCHAR,
207
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
unknown's avatar
unknown committed
208
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_INT24,
209
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
unknown's avatar
unknown committed
210
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
211
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
224
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
225
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
237
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
238
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
unknown's avatar
unknown committed
239
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_INT24,
240
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
unknown's avatar
unknown committed
241
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
242
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
255
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
256
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
257
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
258 259 260 261
  },
  /* MYSQL_TYPE_NULL -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
unknown's avatar
unknown committed
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,
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
293
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
294
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
unknown's avatar
unknown committed
295
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
296
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
317
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
318
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
319
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
320 321 322 323
  },
  /* MYSQL_TYPE_LONGLONG -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
334
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
335
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
348
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
349
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
350
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
351 352 353 354
  },
  /* MYSQL_TYPE_INT24 -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
365
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
366
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
379
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
380
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
381
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
382 383 384 385
  },
  /* MYSQL_TYPE_DATE -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
unknown's avatar
unknown committed
386
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
387
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
unknown's avatar
unknown committed
388
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
389
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
410
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
411
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
412
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
413 414 415 416
  },
  /* MYSQL_TYPE_TIME -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
unknown's avatar
unknown committed
417
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
418
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
unknown's avatar
unknown committed
419
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
420
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
441
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
442
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
443
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
444 445 446 447
  },
  /* MYSQL_TYPE_DATETIME -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
unknown's avatar
unknown committed
448
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
449
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
unknown's avatar
unknown committed
450
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
451
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
472
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
473
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
489
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
490
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
503
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
504
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
505
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
506 507 508 509
  },
  /* MYSQL_TYPE_NEWDATE -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
unknown's avatar
unknown committed
510
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
511
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
unknown's avatar
unknown committed
512
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
513
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
627
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
628
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
629
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
630 631 632 633
  },
  /* MYSQL_TYPE_ENUM -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
unknown's avatar
unknown committed
634
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
635
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
unknown's avatar
unknown committed
636
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
637
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
unknown's avatar
unknown committed
638
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
639
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
unknown's avatar
unknown committed
640
    MYSQL_TYPE_ENUM,        MYSQL_TYPE_VARCHAR,
641
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
unknown's avatar
unknown committed
642
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
643
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
unknown's avatar
unknown committed
644
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
645
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
658
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
659
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
660
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR
661 662 663 664
  },
  /* MYSQL_TYPE_SET -> */
  {
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
unknown's avatar
unknown committed
665
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
666
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
unknown's avatar
unknown committed
667
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
668
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
unknown's avatar
unknown committed
669
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
670
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
unknown's avatar
unknown committed
671
    MYSQL_TYPE_SET,         MYSQL_TYPE_VARCHAR,
672
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
unknown's avatar
unknown committed
673
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
674
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
unknown's avatar
unknown committed
675
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
676
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
689
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
690
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
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,
unknown's avatar
unknown committed
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,
unknown's avatar
unknown committed
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,
unknown's avatar
unknown committed
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,
unknown's avatar
unknown committed
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,
unknown's avatar
unknown committed
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,
unknown's avatar
unknown committed
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,
unknown's avatar
unknown committed
802 803 804
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
  //MYSQL_TYPE_BIT          <16>-<245>
805
    MYSQL_TYPE_BLOB,
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
820
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
821
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
unknown's avatar
unknown committed
822
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
823
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
unknown's avatar
unknown committed
824
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
825
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
unknown's avatar
unknown committed
826
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
827
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
unknown's avatar
unknown committed
828
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
829
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
unknown's avatar
unknown committed
830
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
831
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
844
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
845
  //MYSQL_TYPE_STRING       MYSQL_TYPE_GEOMETRY
unknown's avatar
unknown committed
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,
unknown's avatar
unknown committed
864 865 866
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR,
  //MYSQL_TYPE_BIT          <16>-<245>
867
    MYSQL_TYPE_STRING,
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
882
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
883
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
unknown's avatar
unknown committed
884
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
885
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
unknown's avatar
unknown committed
886
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
887
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
unknown's avatar
unknown committed
888
    MYSQL_TYPE_GEOMETRY,    MYSQL_TYPE_VARCHAR,
889
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
unknown's avatar
unknown committed
890
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
891
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
unknown's avatar
unknown committed
892
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
893
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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,
unknown's avatar
unknown committed
949 950 951
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
  STRING_RESULT,            STRING_RESULT,
  //MYSQL_TYPE_BIT          <16>-<245>
952
  STRING_RESULT,
unknown's avatar
unknown committed
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
};


unknown's avatar
unknown committed
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)];
}

unknown's avatar
unknown committed
1009
/*****************************************************************************
1010
  Static help functions
unknown's avatar
unknown 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:
unknown's avatar
unknown committed
1039
  case MYSQL_TYPE_GEOMETRY:
1040 1041 1042 1043 1044 1045 1046
    return TRUE;
  default:
    return FALSE;
  }
}


unknown's avatar
unknown committed
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;
}


unknown's avatar
unknown 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
  }
}

/*
unknown's avatar
unknown committed
1080
  Test if given number is a int.
1081 1082

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

unknown's avatar
unknown committed
1090 1091
  NOTE
    This is called after one has called strntoull10rnd() function.
1092 1093

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

unknown's avatar
unknown committed
1099 1100
int Field_num::check_int(CHARSET_INFO *cs, const char *str, int length, 
                         const char *int_end, int error)
unknown's avatar
unknown committed
1101
{
unknown's avatar
unknown committed
1102 1103
  /* Test if we get an empty string or wrong integer */
  if (str == int_end || error == MY_ERRNO_EDOM)
unknown's avatar
unknown committed
1104 1105
  {
    char buff[128];
unknown's avatar
unknown committed
1106
    String tmp(buff, (uint32) sizeof(buff), system_charset_info);
unknown's avatar
unknown committed
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);
unknown's avatar
unknown committed
1113
    return 1;
unknown's avatar
unknown committed
1114
  }
unknown's avatar
unknown committed
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;
}
unknown's avatar
unknown committed
1123

unknown's avatar
unknown committed
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)
unknown's avatar
unknown committed
1158
  {
unknown's avatar
unknown committed
1159 1160 1161 1162 1163 1164

    if (((ulonglong) *rnd > unsigned_max) && (*rnd= (longlong) unsigned_max) ||
        error == MY_ERRNO_ERANGE)
    {
      goto out_of_range;
    }
1165
  }
unknown's avatar
unknown committed
1166
  else
1167
  {
unknown's avatar
unknown committed
1168
    if (*rnd < signed_min)
unknown's avatar
unknown committed
1169
    {
unknown's avatar
unknown committed
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;
unknown's avatar
unknown committed
1177
    }
unknown's avatar
unknown committed
1178
  }
unknown's avatar
unknown committed
1179 1180
  if (table->in_use->count_cuted_fields && check_int(cs, from, len, end, error))
    return 1;
unknown's avatar
unknown committed
1181
  return 0;
unknown's avatar
unknown committed
1182

unknown's avatar
unknown committed
1183 1184 1185 1186
out_of_range:
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
  return 1;
}
unknown's avatar
unknown committed
1187

unknown's avatar
unknown committed
1188 1189 1190 1191 1192
/*
  Process decimal library return codes and issue warnings for overflow and
  truncation.

  SYNOPSIS
unknown's avatar
unknown committed
1193
    Field::warn_if_overflow()
unknown's avatar
unknown committed
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
*/

unknown's avatar
unknown committed
1201
int Field::warn_if_overflow(int op_result)
unknown's avatar
unknown committed
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;
  }
unknown's avatar
unknown committed
1208 1209
  if (op_result == E_DEC_TRUNCATED)
  {
unknown's avatar
unknown committed
1210
    set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
1211 1212
    /* We return 0 here as this is not a critical issue */
  }
unknown's avatar
unknown committed
1213 1214 1215
  return 0;
}

unknown's avatar
unknown committed
1216

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

  while (length && my_isspace(cs,*str))
unknown's avatar
unknown 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 == '.'))
unknown's avatar
unknown committed
1232 1233
      return 0;
  }
1234
  while (length && my_isdigit(cs,*str))
unknown's avatar
unknown 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))
unknown's avatar
unknown 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]))
unknown's avatar
unknown committed
1254 1255 1256
      return 0;
    length-=3;
    str+=3;
1257
    while (length && my_isdigit(cs,*str))
unknown's avatar
unknown committed
1258 1259 1260 1261
    {
      length--; str++;
    }
  }
1262
  for (; length ; length--, str++)
unknown's avatar
unknown committed
1263
  {						// Allow end space
1264
    if (!my_isspace(cs,*str))
unknown's avatar
unknown committed
1265 1266 1267 1268
      return 0;
  }
  return 1;
}
unknown's avatar
unknown committed
1269
#endif
unknown's avatar
unknown committed
1270

1271

unknown's avatar
unknown 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)
unknown's avatar
unknown committed
1279 1280
{
  CHARSET_INFO *cs= &my_charset_bin;
1281
  uint length;
unknown's avatar
unknown committed
1282
  longlong value= val_int();
1283
  if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
unknown's avatar
unknown committed
1284
    return 0;
unknown's avatar
unknown 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,
unknown's avatar
unknown committed
1288
                                                value);
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
1296 1297

Field::Field(char *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
unknown's avatar
unknown committed
1298
	     uchar null_bit_arg,
unknown's avatar
unknown 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),
unknown's avatar
unknown committed
1304
   field_name(field_name_arg),
unknown's avatar
unknown 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) 
unknown's avatar
unknown committed
1308 1309
{
  flags=null_ptr ? 0: NOT_NULL_FLAG;
unknown's avatar
unknown committed
1310 1311
  comment.str= (char*) "";
  comment.length=0;
unknown's avatar
unknown 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)
  {
unknown's avatar
unknown committed
1325 1326
    *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) |
			null_ptr[row_offset] & (uchar) null_bit);
unknown's avatar
unknown committed
1327 1328 1329 1330
  }
}


1331
bool Field::send_binary(Protocol *protocol)
unknown's avatar
unknown 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());
unknown's avatar
unknown committed
1337 1338 1339
}


unknown's avatar
unknown committed
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;
}


unknown's avatar
unknown 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"));
unknown's avatar
unknown committed
1354 1355
}

unknown's avatar
unknown committed
1356

1357
void Field::make_field(Send_field *field)
unknown's avatar
unknown committed
1358
{
1359 1360
  if (orig_table && orig_table->s->table_cache_key &&
      *(orig_table->s->table_cache_key))
unknown's avatar
unknown committed
1361 1362
  {
    field->org_table_name= orig_table->s->table_name;
unknown's avatar
unknown committed
1363
    field->db_name= orig_table->s->table_cache_key;
1364 1365
  }
  else
unknown's avatar
unknown committed
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;
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
1383 1384 1385
}


unknown's avatar
unknown committed
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;
    }
unknown's avatar
unknown committed
1411
    else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
unknown's avatar
unknown committed
1412 1413 1414 1415 1416 1417 1418
                                           ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
                                           val, TRUE, &i)))
    {
      i= ~(longlong) 0;
      *err= 1;
    }
  }
unknown's avatar
unknown committed
1419
  else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
unknown's avatar
unknown committed
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));
unknown's avatar
unknown committed
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);
unknown's avatar
unknown committed
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)
unknown's avatar
unknown committed
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)
unknown's avatar
unknown committed
1487
    flags|=BINARY_FLAG;
1488
  field_derivation= DERIVATION_IMPLICIT;
unknown's avatar
unknown committed
1489 1490 1491
}


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

unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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? */
unknown's avatar
unknown committed
1520
  int err= warn_if_overflow(my_decimal2double(E_DEC_FATAL_ERROR &
unknown's avatar
unknown committed
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);
unknown's avatar
unknown committed
1530 1531 1532
  return decimal_value;
}

1533

unknown's avatar
unknown committed
1534 1535
uint Field::fill_cache_field(CACHE_FIELD *copy)
{
unknown's avatar
unknown committed
1536
  uint store_length;
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
1545 1546
    return copy->length;
  }
1547 1548 1549
  else if (!zero_pack() &&
           (type() == MYSQL_TYPE_STRING && copy->length >= 4 &&
            copy->length < 256))
unknown's avatar
unknown committed
1550
  {
unknown's avatar
unknown committed
1551
    copy->strip=1;				/* Remove end space */
unknown's avatar
unknown committed
1552 1553
    store_length= 2;
  }
unknown's avatar
unknown committed
1554
  else
unknown's avatar
unknown committed
1555
  {
unknown's avatar
unknown committed
1556
    copy->strip=0;
unknown's avatar
unknown committed
1557 1558 1559
    store_length= 0;
  }
  return copy->length+ store_length;
unknown's avatar
unknown committed
1560 1561
}

1562

1563
bool Field::get_date(MYSQL_TIME *ltime,uint fuzzydate)
unknown's avatar
unknown 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)
unknown's avatar
unknown committed
1570 1571 1572 1573
    return 1;
  return 0;
}

1574
bool Field::get_time(MYSQL_TIME *ltime)
unknown's avatar
unknown 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))
unknown's avatar
unknown 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
*/
unknown's avatar
unknown committed
1590

1591
int Field::store_time(MYSQL_TIME *ltime, timestamp_type type_arg)
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
1596 1597 1598
}


1599
bool Field::optimize_range(uint idx, uint part)
unknown's avatar
unknown committed
1600
{
1601
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
unknown's avatar
unknown 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;
}

unknown's avatar
unknown 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)))
unknown's avatar
unknown 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
}


unknown's avatar
unknown committed
1651
/****************************************************************************
unknown's avatar
unknown committed
1652 1653
  Functions for the Field_decimal class
  This is an number stored as a pre-space (or pre-zero) string
unknown's avatar
unknown committed
1654 1655
****************************************************************************/

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

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

1668
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1669
  if (negative)
unknown's avatar
unknown 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
      }
    }
unknown's avatar
unknown committed
1694
  }
1695
  bfill(to, len, filler);
unknown's avatar
unknown committed
1696 1697 1698 1699 1700 1701
  if (dec)
    ptr[field_length-dec-1]='.';
  return;
}


unknown's avatar
unknown committed
1702
int Field_decimal::store(const char *from, uint len, CHARSET_INFO *cs)
unknown's avatar
unknown committed
1703
{
unknown's avatar
unknown committed
1704
  char buff[STRING_BUFFER_USUAL_SIZE];
unknown's avatar
unknown committed
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;
unknown's avatar
unknown committed
1725
  /* The pointers where prezeros start and stop */
1726
  const char *pre_zeros_from, *pre_zeros_end;
unknown's avatar
unknown committed
1727
  /* The pointers where digits at the left of '.' start and stop */
1728
  const char *int_digits_from, *int_digits_end;
unknown's avatar
unknown committed
1729
  /* The pointers where digits at the right of '.' start and stop */
1730
  const char *frac_digits_from, *frac_digits_end;
unknown's avatar
unknown committed
1731
  /* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
1732
  char expo_sign_char=0;
unknown's avatar
unknown 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;
unknown's avatar
unknown 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;
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
1752
  /*
1753
    To remember if table->in_use->cuted_fields has already been incremented,
unknown's avatar
unknown 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 :
unknown's avatar
unknown committed
1765 1766
    - parse the input string
    - modify the position of digits around the decimal dot '.' 
1767
      according to the exponent value (if specified)
unknown's avatar
unknown committed
1768
    - write the formatted number
1769 1770 1771 1772 1773
  */

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

unknown's avatar
unknown committed
1774
  /* skip pre-space */
unknown's avatar
unknown committed
1775
  while (from != end && my_isspace(&my_charset_bin,*from))
unknown's avatar
unknown committed
1776
    from++;
1777
  if (from == end)
unknown's avatar
unknown committed
1778
  {
1779
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
1780
    is_cuted_fields_incr=1;
unknown's avatar
unknown committed
1781
  }
1782
  else if (*from == '+' || *from == '-')	// Found some sign ?
unknown's avatar
unknown committed
1783
  {
1784
    sign_char= *from++;
unknown's avatar
unknown committed
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);
unknown's avatar
unknown committed
1795
        return 1;
1796
      }
1797
      /* 
unknown's avatar
unknown 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 	
unknown's avatar
unknown committed
1805
    }
1806
  }
unknown's avatar
unknown committed
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 '.'*/
unknown's avatar
unknown committed
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 '.' */
unknown's avatar
unknown committed
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
    */
unknown's avatar
unknown committed
1834
    for (;from!=end && my_isdigit(&my_charset_bin, *from); from++)
1835
    {
1836
      exponent=10*exponent+(*from-'0');
unknown's avatar
unknown committed
1837
      if (exponent>MAX_EXPONENT)
1838 1839
        break;
    }
unknown's avatar
unknown committed
1840
  }
1841
  
unknown's avatar
unknown 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.
unknown's avatar
unknown committed
1845
    Even if this flag is not set, it's OK to increment warnings, if
1846
    it makes the code easer to read.
unknown's avatar
unknown committed
1847
  */
1848

1849
  if (table->in_use->count_cuted_fields)
unknown's avatar
unknown committed
1850
  {
1851
    // Skip end spaces
unknown's avatar
unknown committed
1852
    for (;from != end && my_isspace(&my_charset_bin, *from); from++) ;
1853
    if (from != end)                     // If still something left, warn
unknown's avatar
unknown committed
1854
    {
1855
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
1856
      is_cuted_fields_incr=1;
unknown's avatar
unknown 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 :
unknown's avatar
unknown 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 '.'
unknown's avatar
unknown 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
unknown's avatar
unknown 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
unknown's avatar
unknown 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
  /* 
unknown's avatar
unknown 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;
unknown's avatar
unknown 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;
      }
unknown's avatar
unknown 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
  {
unknown's avatar
unknown committed
1938
    // too big number, change to max or min number
1939
    Field_decimal::overflow(sign_char == '-');
1940
    return 1;
unknown's avatar
unknown 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';  
unknown's avatar
unknown 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;
unknown's avatar
unknown 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.
unknown's avatar
unknown 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
  {
unknown's avatar
unknown committed
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)
unknown's avatar
unknown 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
unknown's avatar
unknown 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)
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
2035
          return 0;
2036 2037
        }
        continue;
unknown's avatar
unknown committed
2038
      }
2039
      *pos++= tmp_char;
unknown's avatar
unknown 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)
unknown's avatar
unknown committed
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);
unknown's avatar
unknown committed
2057
        }
unknown's avatar
unknown committed
2058
        return 0;
2059
      }
2060
      continue;
2061
    }
2062
    *pos++= tmp_char;
unknown's avatar
unknown committed
2063
  }
2064 2065 2066
      
  while (pos != right_wall)
   *pos++='0';			// Fill with zeros at right of '.'
unknown's avatar
unknown committed
2067
  return 0;
unknown's avatar
unknown committed
2068 2069 2070
}


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

unknown's avatar
unknown committed
2087 2088
  reg4 uint i,length;
  char fyllchar,*to;
2089
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
unknown's avatar
unknown committed
2090 2091

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

  if (length > field_length)
  {
    overflow(nr < 0.0);
2103
    return 1;
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
2112 2113 2114 2115
  }
}


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

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

  if (length > int_part)
  {
2132
    overflow(!unsigned_val && nr < 0L);		/* purecov: inspected */
2133
    return 1;
unknown's avatar
unknown 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)
unknown's avatar
unknown committed
2142
  {
2143 2144
    to[length]='.';
    bfill(to+length+1,dec,'0');
unknown's avatar
unknown committed
2145
  }
2146
  return 0;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
2156 2157 2158 2159
}

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

2169

unknown's avatar
unknown 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);
unknown's avatar
unknown committed
2176
  val_ptr->set_charset(&my_charset_bin);
unknown's avatar
unknown 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);
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
2193 2194 2195 2196
  /* First remove prefixes '0', ' ', and '-' */
  for (end=a_ptr+field_length;
       a_ptr != end &&
	 (*a_ptr == *b_ptr ||
unknown's avatar
unknown committed
2197
	  ((my_isspace(&my_charset_bin,*a_ptr)  || *a_ptr == '+' || 
2198
            *a_ptr == '0') &&
unknown's avatar
unknown committed
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      
  }
unknown's avatar
unknown committed
2206 2207 2208
  if (a_ptr == end)
    return 0;
  if (*a_ptr == '-')
2209
    return -1;
2210
  if (*b_ptr == '-')
unknown's avatar
unknown 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 &&
unknown's avatar
unknown committed
2227
	 ((my_isspace(&my_charset_bin,*str) || *str == '+' ||
unknown's avatar
unknown committed
2228
	   *str == '0')) ;
unknown's avatar
unknown 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)
unknown's avatar
unknown committed
2239
      if (my_isdigit(&my_charset_bin,*str))
unknown's avatar
unknown committed
2240 2241 2242 2243 2244 2245 2246
	*to++= (char) ('9' - *str++);
      else
	*to++= *str++;
  }
  else memcpy(to,str,(uint) (end-str));
}

unknown's avatar
unknown committed
2247

unknown's avatar
unknown committed
2248 2249
void Field_decimal::sql_type(String &res) const
{
unknown's avatar
unknown committed
2250
  CHARSET_INFO *cs=res.charset();
unknown's avatar
unknown 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));
unknown's avatar
unknown committed
2258 2259 2260 2261
  add_zerofill_and_unsigned(res);
}


unknown's avatar
unknown committed
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)
unknown's avatar
unknown committed
2274
  :Field_num(ptr_arg, len_arg,
unknown's avatar
unknown committed
2275 2276
             null_ptr_arg, null_bit_arg,
             unireg_check_arg, field_name_arg, table_arg,
unknown's avatar
unknown committed
2277
             dec_arg, zero_arg, unsigned_arg)
unknown's avatar
unknown committed
2278
{
unknown's avatar
unknown committed
2279
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
unknown's avatar
unknown committed
2280
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
unknown's avatar
unknown committed
2281 2282 2283
  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
              (dec <= DECIMAL_MAX_SCALE));
  bin_size= my_decimal_get_binary_size(precision, dec);
unknown's avatar
unknown committed
2284 2285 2286 2287
}


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


unknown's avatar
unknown committed
2307
int Field_new_decimal::reset(void)
unknown's avatar
unknown committed
2308 2309
{
  store_value(&decimal_zero);
unknown's avatar
unknown committed
2310
  return 0;
unknown's avatar
unknown committed
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");
unknown's avatar
unknown committed
2327
  max_my_decimal(decimal_value, precision, decimals());
unknown's avatar
unknown committed
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.
unknown's avatar
unknown committed
2350 2351 2352 2353

  RETURN
   0 ok
   1 error
unknown's avatar
unknown committed
2354 2355 2356 2357
*/

bool Field_new_decimal::store_value(const my_decimal *decimal_value)
{
unknown's avatar
unknown committed
2358 2359
  int error= 0;
  DBUG_ENTER("Field_new_decimal::store_value");
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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);
unknown's avatar
unknown committed
2372
    error= 1;
unknown's avatar
unknown committed
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)));
unknown's avatar
unknown committed
2381
  }
unknown's avatar
unknown committed
2382
#endif
unknown's avatar
unknown committed
2383

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


int Field_new_decimal::store(const char *from, uint length,
2399
                             CHARSET_INFO *charset_arg)
unknown's avatar
unknown committed
2400 2401 2402
{
  int err;
  my_decimal decimal_value;
unknown's avatar
unknown committed
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) {
unknown's avatar
unknown committed
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);

unknown's avatar
unknown committed
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,
unknown's avatar
unknown committed
2441 2442
                        (ulong) table->in_use->row_count);
    my_decimal_set_zero(&decimal_value);
2443

unknown's avatar
unknown committed
2444
    break;
2445
    }
unknown's avatar
unknown committed
2446 2447
  }

unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
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;
unknown's avatar
unknown committed
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);
unknown's avatar
unknown committed
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;
  }
unknown's avatar
unknown committed
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);
unknown's avatar
unknown committed
2489 2490 2491
}


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

  if ((err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
2498
                           nr, unsigned_val, &decimal_value)))
unknown's avatar
unknown committed
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);
unknown's avatar
unknown committed
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));
unknown's avatar
unknown committed
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,
unknown's avatar
unknown committed
2549
                    precision, dec);
2550 2551
  DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (byte *) ptr,
                                          bin_size););
unknown's avatar
unknown committed
2552 2553 2554 2555 2556
  DBUG_RETURN(decimal_value);
}


String *Field_new_decimal::val_str(String *val_buffer,
unknown's avatar
unknown committed
2557
                                   String *val_ptr __attribute__((unused)))
unknown's avatar
unknown committed
2558 2559
{
  my_decimal decimal_value;
unknown's avatar
unknown committed
2560
  uint fixed_precision= zerofill ? precision : 0;
unknown's avatar
unknown committed
2561
  my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
unknown's avatar
unknown committed
2562
                    fixed_precision, dec, '0', val_buffer);
unknown's avatar
unknown committed
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(),
unknown's avatar
unknown committed
2584
                                "decimal(%d,%d)", precision, (int)dec));
unknown's avatar
unknown committed
2585 2586 2587
  add_zerofill_and_unsigned(str);
}

unknown's avatar
unknown committed
2588

unknown's avatar
unknown committed
2589 2590 2591 2592
/****************************************************************************
** tiny int
****************************************************************************/

unknown's avatar
unknown committed
2593
int Field_tiny::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
2594
{
2595
  int error;
unknown's avatar
unknown committed
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;
unknown's avatar
unknown committed
2601 2602 2603
}


unknown's avatar
unknown committed
2604
int Field_tiny::store(double nr)
unknown's avatar
unknown committed
2605
{
unknown's avatar
unknown committed
2606
  int error= 0;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
2614
      error= 1;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
2620
      error= 1;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
2631
      error= 1;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
2637
      error= 1;
unknown's avatar
unknown committed
2638 2639
    }
    else
unknown's avatar
unknown committed
2640
      *ptr=(char) (int) nr;
unknown's avatar
unknown committed
2641
  }
2642
  return error;
unknown's avatar
unknown committed
2643 2644
}

2645 2646

int Field_tiny::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
2647
{
unknown's avatar
unknown committed
2648
  int error= 0;
2649

unknown's avatar
unknown committed
2650 2651
  if (unsigned_flag)
  {
2652
    if (nr < 0 && !unsigned_val)
unknown's avatar
unknown committed
2653
    {
2654
      *ptr= 0;
2655
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
2656
      error= 1;
unknown's avatar
unknown committed
2657
    }
2658
    else if ((ulonglong) nr > (ulonglong) 255)
unknown's avatar
unknown committed
2659 2660
    {
      *ptr= (char) 255;
2661
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
2662
      error= 1;
unknown's avatar
unknown 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)
unknown's avatar
unknown committed
2672 2673
    {
      *ptr= (char) -128;
2674
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
2675
      error= 1;
unknown's avatar
unknown committed
2676
    }
2677
    else if (nr > 127)
unknown's avatar
unknown committed
2678 2679
    {
      *ptr=127;
2680
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
2681
      error= 1;
unknown's avatar
unknown committed
2682 2683 2684 2685
    }
    else
      *ptr=(char) nr;
  }
2686
  return error;
unknown's avatar
unknown 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

unknown's avatar
unknown 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

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

unknown's avatar
unknown committed
2715
  if (unsigned_flag)
2716 2717
    length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
					   (long) *((uchar*) ptr));
unknown's avatar
unknown committed
2718
  else
2719 2720
    length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
					   (long) *((signed char*) ptr));
2721
  
unknown's avatar
unknown 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]);
}
unknown's avatar
unknown 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
{
unknown's avatar
unknown committed
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));
unknown's avatar
unknown committed
2755 2756 2757 2758
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
2759
 Field type short int (2 byte)
unknown's avatar
unknown committed
2760 2761
****************************************************************************/

unknown's avatar
unknown committed
2762
int Field_short::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
2763
{
unknown's avatar
unknown committed
2764
  int store_tmp;
2765
  int error;
unknown's avatar
unknown committed
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
unknown's avatar
unknown committed
2771 2772 2773
  if (table->s->db_low_byte_first)
  {
    int2store(ptr, store_tmp);
unknown's avatar
unknown committed
2774 2775 2776
  }
  else
#endif
unknown's avatar
unknown committed
2777
    shortstore(ptr, (short) store_tmp);
2778
  return error;
unknown's avatar
unknown committed
2779 2780 2781
}


unknown's avatar
unknown committed
2782
int Field_short::store(double nr)
unknown's avatar
unknown committed
2783
{
unknown's avatar
unknown committed
2784
  int error= 0;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
2793
      error= 1;
unknown's avatar
unknown committed
2794
    }
unknown's avatar
unknown committed
2795
    else if (nr > (double) UINT_MAX16)
unknown's avatar
unknown committed
2796
    {
unknown's avatar
unknown committed
2797
      res=(int16) UINT_MAX16;
2798
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
2799
      error= 1;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
2810
      error= 1;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
2816
      error= 1;
unknown's avatar
unknown committed
2817 2818
    }
    else
unknown's avatar
unknown committed
2819
      res=(int16) (int) nr;
unknown's avatar
unknown committed
2820 2821
  }
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
2822
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
2823 2824 2825 2826 2827 2828
  {
    int2store(ptr,res);
  }
  else
#endif
    shortstore(ptr,res);
2829
  return error;
unknown's avatar
unknown committed
2830 2831
}

2832 2833

int Field_short::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
2834
{
unknown's avatar
unknown committed
2835
  int error= 0;
unknown's avatar
unknown committed
2836
  int16 res;
2837

unknown's avatar
unknown committed
2838 2839
  if (unsigned_flag)
  {
2840
    if (nr < 0L && !unsigned_val)
unknown's avatar
unknown committed
2841 2842
    {
      res=0;
2843
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
2844
      error= 1;
unknown's avatar
unknown committed
2845
    }
2846
    else if ((ulonglong) nr > (ulonglong) UINT_MAX16)
unknown's avatar
unknown committed
2847
    {
unknown's avatar
unknown committed
2848
      res=(int16) UINT_MAX16;
2849
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
2850
      error= 1;
unknown's avatar
unknown 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

unknown's avatar
unknown 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);
unknown's avatar
unknown committed
2864
      error= 1;
unknown's avatar
unknown committed
2865
    }
2866
    else if (nr > (longlong) INT_MAX16)
unknown's avatar
unknown committed
2867 2868
    {
      res=INT_MAX16;
2869
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
2870
      error= 1;
unknown's avatar
unknown committed
2871 2872 2873 2874 2875
    }
    else
      res=(int16) nr;
  }
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
2876
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
2877 2878 2879 2880 2881 2882
  {
    int2store(ptr,res);
  }
  else
#endif
    shortstore(ptr,res);
2883
  return error;
unknown's avatar
unknown committed
2884 2885 2886 2887 2888 2889 2890
}


double Field_short::val_real(void)
{
  short j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
2891
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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
unknown's avatar
unknown committed
2903
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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

unknown's avatar
unknown committed
2912 2913 2914
String *Field_short::val_str(String *val_buffer,
			     String *val_ptr __attribute__((unused)))
{
unknown's avatar
unknown committed
2915
  CHARSET_INFO *cs= &my_charset_bin;
unknown's avatar
unknown committed
2916
  uint length;
2917 2918
  uint mlength=max(field_length+1,7*cs->mbmaxlen);
  val_buffer->alloc(mlength);
unknown's avatar
unknown committed
2919 2920 2921
  char *to=(char*) val_buffer->ptr();
  short j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
2922
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
2931
  else
2932
    length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
unknown's avatar
unknown 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());
}


unknown's avatar
unknown committed
2946 2947 2948 2949
int Field_short::cmp(const char *a_ptr, const char *b_ptr)
{
  short a,b;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
2950
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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
unknown's avatar
unknown committed
2971
  if (!table->s->db_low_byte_first)
unknown's avatar
unknown committed
2972 2973 2974 2975
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
unknown's avatar
unknown committed
2976
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
2977 2978 2979 2980 2981 2982 2983 2984
    to[1]   = ptr[1];
  }
  else
#endif
  {
    if (unsigned_flag)
      to[0] = ptr[1];
    else
unknown's avatar
unknown committed
2985
      to[0] = (char) (ptr[1] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
2986 2987 2988 2989 2990 2991
    to[1]   = ptr[0];
  }
}

void Field_short::sql_type(String &res) const
{
unknown's avatar
unknown committed
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));
unknown's avatar
unknown committed
2995 2996 2997 2998 2999
  add_zerofill_and_unsigned(res);
}


/****************************************************************************
3000
  Field type medium int (3 byte)
unknown's avatar
unknown committed
3001 3002
****************************************************************************/

unknown's avatar
unknown committed
3003
int Field_medium::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
3004
{
unknown's avatar
unknown committed
3005
  int store_tmp;
3006
  int error;
unknown's avatar
unknown committed
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;
unknown's avatar
unknown committed
3013 3014 3015
}


unknown's avatar
unknown committed
3016
int Field_medium::store(double nr)
unknown's avatar
unknown committed
3017
{
unknown's avatar
unknown committed
3018
  int error= 0;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
3026
      error= 1;
unknown's avatar
unknown committed
3027 3028 3029
    }
    else if (nr >= (double) (long) (1L << 24))
    {
unknown's avatar
unknown committed
3030
      uint32 tmp=(uint32) (1L << 24)-1L;
unknown's avatar
unknown committed
3031
      int3store(ptr,tmp);
3032
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3033
      error= 1;
unknown's avatar
unknown committed
3034 3035
    }
    else
unknown's avatar
unknown committed
3036
      int3store(ptr,(uint32) nr);
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
3045
      error= 1;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
3052
      error= 1;
unknown's avatar
unknown committed
3053 3054 3055 3056
    }
    else
      int3store(ptr,(long) nr);
  }
3057
  return error;
unknown's avatar
unknown committed
3058 3059
}

3060 3061

int Field_medium::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
3062
{
unknown's avatar
unknown committed
3063
  int error= 0;
3064

unknown's avatar
unknown committed
3065 3066
  if (unsigned_flag)
  {
3067
    if (nr < 0 && !unsigned_val)
unknown's avatar
unknown committed
3068 3069
    {
      int3store(ptr,0);
3070
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3071
      error= 1;
unknown's avatar
unknown committed
3072
    }
3073
    else if ((ulonglong) nr >= (ulonglong) (long) (1L << 24))
unknown's avatar
unknown committed
3074
    {
3075
      long tmp= (long) (1L << 24)-1L;
unknown's avatar
unknown committed
3076
      int3store(ptr,tmp);
3077
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3078
      error= 1;
unknown's avatar
unknown committed
3079 3080
    }
    else
unknown's avatar
unknown committed
3081
      int3store(ptr,(uint32) nr);
unknown's avatar
unknown committed
3082 3083 3084
  }
  else
  {
3085 3086 3087
    if (nr < 0 && unsigned_val)
      nr= (ulonglong) (long) (1L << 24);        // Generate overflow

unknown's avatar
unknown committed
3088 3089
    if (nr < (longlong) INT_MIN24)
    {
3090
      long tmp= (long) INT_MIN24;
unknown's avatar
unknown committed
3091
      int3store(ptr,tmp);
3092
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3093
      error= 1;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
3100
      error= 1;
unknown's avatar
unknown committed
3101 3102 3103 3104
    }
    else
      int3store(ptr,(long) nr);
  }
3105
  return error;
unknown's avatar
unknown 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

unknown's avatar
unknown 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

unknown's avatar
unknown committed
3123 3124 3125
String *Field_medium::val_str(String *val_buffer,
			      String *val_ptr __attribute__((unused)))
{
unknown's avatar
unknown committed
3126
  CHARSET_INFO *cs= &my_charset_bin;
unknown's avatar
unknown committed
3127
  uint length;
3128 3129
  uint mlength=max(field_length+1,10*cs->mbmaxlen);
  val_buffer->alloc(mlength);
unknown's avatar
unknown 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);
unknown's avatar
unknown 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());
}


unknown's avatar
unknown 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
{
unknown's avatar
unknown committed
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));
unknown's avatar
unknown committed
3179 3180 3181 3182 3183 3184 3185
  add_zerofill_and_unsigned(res);
}

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

unknown's avatar
unknown committed
3186
int Field_long::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
3187
{
unknown's avatar
unknown committed
3188 3189
  long store_tmp;
  int error;
unknown's avatar
unknown committed
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;
unknown's avatar
unknown committed
3194
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3195
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3196
  {
unknown's avatar
unknown committed
3197
    int4store(ptr, store_tmp);
unknown's avatar
unknown committed
3198 3199 3200
  }
  else
#endif
unknown's avatar
unknown committed
3201
    longstore(ptr, store_tmp);
3202
  return error;
unknown's avatar
unknown committed
3203 3204 3205
}


unknown's avatar
unknown committed
3206
int Field_long::store(double nr)
unknown's avatar
unknown committed
3207
{
unknown's avatar
unknown committed
3208
  int error= 0;
unknown's avatar
unknown committed
3209 3210 3211 3212 3213 3214 3215
  int32 res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
unknown's avatar
unknown committed
3216
      error= 1;
unknown's avatar
unknown committed
3217
    }
3218
    else if (nr > (double) UINT_MAX32)
unknown's avatar
unknown committed
3219
    {
3220
      res= UINT_MAX32;
3221
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3222
      error= 1;
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
3232
      error= 1;
unknown's avatar
unknown committed
3233 3234 3235 3236
    }
    else if (nr > (double) INT_MAX32)
    {
      res=(int32) INT_MAX32;
unknown's avatar
unknown committed
3237
      error= 1;
unknown's avatar
unknown committed
3238 3239
    }
    else
unknown's avatar
unknown committed
3240
      res=(int32) (longlong) nr;
unknown's avatar
unknown committed
3241
  }
unknown's avatar
unknown committed
3242 3243 3244
  if (error)
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);

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


3257
int Field_long::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
3258
{
unknown's avatar
unknown committed
3259
  int error= 0;
unknown's avatar
unknown committed
3260
  int32 res;
unknown's avatar
unknown committed
3261
  DBUG_ASSERT(table->in_use == current_thd);    // General safety
3262
  
unknown's avatar
unknown committed
3263 3264
  if (unsigned_flag)
  {
3265
    if (nr < 0 && !unsigned_val)
unknown's avatar
unknown committed
3266 3267
    {
      res=0;
unknown's avatar
unknown committed
3268
      error= 1;
unknown's avatar
unknown committed
3269
    }
3270
    else if ((ulonglong) nr >= (LL(1) << 32))
unknown's avatar
unknown committed
3271 3272
    {
      res=(int32) (uint32) ~0L;
unknown's avatar
unknown committed
3273
      error= 1;
unknown's avatar
unknown 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) 
unknown's avatar
unknown committed
3283 3284
    {
      res=(int32) INT_MIN32;
unknown's avatar
unknown committed
3285
      error= 1;
unknown's avatar
unknown committed
3286 3287 3288 3289
    }
    else if (nr > (longlong) INT_MAX32)
    {
      res=(int32) INT_MAX32;
unknown's avatar
unknown committed
3290
      error= 1;
unknown's avatar
unknown committed
3291 3292 3293 3294
    }
    else
      res=(int32) nr;
  }
unknown's avatar
unknown committed
3295 3296 3297
  if (error)
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);

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


double Field_long::val_real(void)
{
  int32 j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3314
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
3327
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3328
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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)))
{
unknown's avatar
unknown committed
3339
  CHARSET_INFO *cs= &my_charset_bin;
unknown's avatar
unknown committed
3340
  uint length;
3341 3342
  uint mlength=max(field_length+1,12*cs->mbmaxlen);
  val_buffer->alloc(mlength);
unknown's avatar
unknown committed
3343 3344 3345
  char *to=(char*) val_buffer->ptr();
  int32 j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3346
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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);
unknown's avatar
unknown 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());
}

unknown's avatar
unknown committed
3368 3369 3370 3371
int Field_long::cmp(const char *a_ptr, const char *b_ptr)
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3372
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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)
unknown's avatar
unknown committed
3384
    return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
unknown's avatar
unknown 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
unknown's avatar
unknown committed
3391
  if (!table->s->db_low_byte_first)
unknown's avatar
unknown committed
3392 3393 3394 3395
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
unknown's avatar
unknown committed
3396
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
unknown's avatar
unknown 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
unknown's avatar
unknown committed
3407
      to[0] = (char) (ptr[3] ^ 128);		/* Revers signbit */
unknown's avatar
unknown 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
{
unknown's avatar
unknown committed
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));
unknown's avatar
unknown committed
3420 3421 3422 3423
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
3424
 Field type longlong int (8 bytes)
unknown's avatar
unknown committed
3425 3426
****************************************************************************/

unknown's avatar
unknown committed
3427
int Field_longlong::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown 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
  {
unknown's avatar
unknown committed
3436
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3437 3438
    error= 1;
  }
unknown's avatar
unknown committed
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;
unknown's avatar
unknown committed
3444
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3445
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3446 3447 3448 3449 3450 3451
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
3452
  return error;
unknown's avatar
unknown committed
3453 3454 3455
}


unknown's avatar
unknown committed
3456
int Field_longlong::store(double nr)
unknown's avatar
unknown committed
3457
{
unknown's avatar
unknown committed
3458
  int error= 0;
unknown's avatar
unknown committed
3459
  longlong res;
unknown's avatar
unknown committed
3460 3461

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

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


3507
int Field_longlong::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown 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;
    }
  }

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


double Field_longlong::val_real(void)
{
  longlong j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3541
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
3555 3556
}

3557

unknown's avatar
unknown committed
3558 3559 3560 3561
longlong Field_longlong::val_int(void)
{
  longlong j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3562
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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)))
{
unknown's avatar
unknown committed
3574
  CHARSET_INFO *cs= &my_charset_bin;
unknown's avatar
unknown committed
3575
  uint length;
3576 3577
  uint mlength=max(field_length+1,22*cs->mbmaxlen);
  val_buffer->alloc(mlength);
unknown's avatar
unknown committed
3578 3579 3580
  char *to=(char*) val_buffer->ptr();
  longlong j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3581
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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,
unknown's avatar
unknown committed
3588
					unsigned_flag ? 10 : -10, j);
unknown's avatar
unknown 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);
}


unknown's avatar
unknown committed
3602 3603 3604 3605
int Field_longlong::cmp(const char *a_ptr, const char *b_ptr)
{
  longlong a,b;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3606
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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
unknown's avatar
unknown committed
3626
  if (!table->s->db_low_byte_first)
unknown's avatar
unknown committed
3627 3628 3629 3630
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
unknown's avatar
unknown committed
3631
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
unknown's avatar
unknown 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
unknown's avatar
unknown committed
3646
      to[0] = (char) (ptr[7] ^ 128);		/* Revers signbit */
unknown's avatar
unknown 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
{
unknown's avatar
unknown committed
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));
unknown's avatar
unknown committed
3663 3664 3665
  add_zerofill_and_unsigned(res);
}

unknown's avatar
unknown committed
3666

unknown's avatar
unknown committed
3667
/****************************************************************************
unknown's avatar
unknown committed
3668
  single precision float
unknown's avatar
unknown committed
3669 3670
****************************************************************************/

unknown's avatar
unknown committed
3671
int Field_float::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
3672
{
unknown's avatar
unknown committed
3673
  int error;
3674
  char *end;
unknown's avatar
unknown committed
3675
  double nr= my_strntod(cs,(char*) from,len,&end,&error);
unknown's avatar
unknown committed
3676 3677
  if (error || (!len || (uint) (end-from) != len &&
                table->in_use->count_cuted_fields))
3678
  {
unknown's avatar
unknown committed
3679
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
3680
                (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
unknown's avatar
unknown committed
3681
    error= error ? 1 : 2;
3682
  }
unknown's avatar
unknown committed
3683 3684
  Field_float::store(nr);
  return error;
unknown's avatar
unknown committed
3685 3686 3687
}


unknown's avatar
unknown committed
3688
int Field_float::store(double nr)
unknown's avatar
unknown committed
3689
{
3690
  int error= truncate(&nr, FLT_MAX);
3691
  float j= (float)nr;
3692

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


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

3710

unknown's avatar
unknown committed
3711 3712 3713 3714
double Field_float::val_real(void)
{
  float j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3715
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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
unknown's avatar
unknown committed
3729
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3730 3731 3732 3733 3734 3735
  {
    float4get(j,ptr);
  }
  else
#endif
    memcpy_fixed((byte*) &j,ptr,sizeof(j));
3736
  return (longlong) rint(j);
unknown's avatar
unknown 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
unknown's avatar
unknown committed
3745
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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);
unknown's avatar
unknown 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++;
unknown's avatar
unknown committed
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);
unknown's avatar
unknown committed
3808
#else
3809
    to+= my_sprintf(to,(to,"%.*f",dec,nr));
unknown's avatar
unknown committed
3810
#endif
unknown's avatar
unknown 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
unknown's avatar
unknown committed
3827
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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
unknown's avatar
unknown committed
3847
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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++)
unknown's avatar
unknown committed
3872
	tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
unknown's avatar
unknown 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);
}


unknown's avatar
unknown 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
  }
unknown's avatar
unknown 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
  }
unknown's avatar
unknown committed
3904 3905 3906
  add_zerofill_and_unsigned(res);
}

unknown's avatar
unknown committed
3907

unknown's avatar
unknown committed
3908
/****************************************************************************
unknown's avatar
unknown committed
3909
  double precision floating point numbers
unknown's avatar
unknown committed
3910 3911
****************************************************************************/

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


unknown's avatar
unknown committed
3929
int Field_double::store(double nr)
unknown's avatar
unknown committed
3930
{
3931
  int error= truncate(&nr, DBL_MAX);
3932

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


3945
int Field_double::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
3946
{
3947
  return store(unsigned_val ? ulonglong2double((ulonglong) nr) : (double) nr);
unknown's avatar
unknown 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

unknown's avatar
unknown committed
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);
}
unknown's avatar
unknown committed
4014 4015 4016 4017 4018

double Field_double::val_real(void)
{
  double j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4019
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
4032
  longlong res;
unknown's avatar
unknown committed
4033
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4034
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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)
unknown's avatar
unknown committed
4043 4044 4045 4046
  {
    res= (longlong) LONGLONG_MIN;
    goto warn;
  }
4047
  if (j >= (double) (ulonglong) LONGLONG_MAX)
unknown's avatar
unknown committed
4048 4049 4050 4051
  {
    res= (longlong) LONGLONG_MAX;
    goto warn;
  }
4052
  return (longlong) rint(j);
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
4065 4066 4067
}


unknown's avatar
unknown committed
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;
}


unknown's avatar
unknown committed
4075 4076 4077 4078 4079
String *Field_double::val_str(String *val_buffer,
			      String *val_ptr __attribute__((unused)))
{
  double nr;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4080
  if (table->s->db_low_byte_first)
unknown's avatar
unknown 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);
unknown's avatar
unknown 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
unknown's avatar
unknown committed
4100
    char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
4101
    char *pos= buff;
unknown's avatar
unknown 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
unknown's avatar
unknown committed
4140
    to[to_length-1]=0;			// Safety
unknown's avatar
unknown committed
4141
    snprintf(to,to_length-1,"%.*f",dec,nr);
4142
    to=strend(to);
unknown's avatar
unknown committed
4143
#else
4144
    to+= my_sprintf(to,(to,"%.*f",dec,nr));
unknown's avatar
unknown 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)
{
unknown's avatar
unknown committed
4160
  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
4161 4162
}

unknown's avatar
unknown committed
4163 4164 4165 4166 4167

int Field_double::cmp(const char *a_ptr, const char *b_ptr)
{
  double a,b;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4168
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4169 4170 4171 4172 4173 4174 4175
  {
    float8get(a,a_ptr);
    float8get(b,b_ptr);
  }
  else
#endif
  {
unknown's avatar
unknown committed
4176 4177
    doubleget(a, a_ptr);
    doubleget(b, b_ptr);
unknown's avatar
unknown 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
unknown's avatar
unknown committed
4191
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4192 4193 4194 4195 4196
  {
    float8get(nr,ptr);
  }
  else
#endif
unknown's avatar
unknown committed
4197
    doubleget(nr,ptr);
unknown's avatar
unknown committed
4198 4199 4200 4201 4202 4203
  change_double_for_sort(nr, (byte*) to);
}


void Field_double::sql_type(String &res) const
{
unknown's avatar
unknown committed
4204
  CHARSET_INFO *cs=res.charset();
unknown's avatar
unknown committed
4205
  if (dec == NOT_FIXED_DEC)
4206
  {
4207
    res.set_ascii(STRING_WITH_LEN("double"));
4208
  }
unknown's avatar
unknown 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
  }
unknown's avatar
unknown 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 
unknown's avatar
unknown committed
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.
 */
unknown's avatar
unknown committed
4262 4263

Field_timestamp::Field_timestamp(char *ptr_arg, uint32 len_arg,
4264
                                 uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown 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)
unknown's avatar
unknown committed
4271
{
unknown's avatar
unknown committed
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)
unknown's avatar
unknown committed
4276
  {
4277 4278
    /* This timestamp has auto-update */
    table->timestamp_field= this;
unknown's avatar
unknown 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:
    /*
unknown's avatar
unknown committed
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
}


unknown's avatar
unknown committed
4342
int Field_timestamp::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown 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;
unknown's avatar
unknown 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 */
unknown's avatar
unknown committed
4353
  have_smth_to_conv= (str_to_datetime(from, len, &l_time,
unknown's avatar
unknown committed
4354
                                      (thd->variables.sql_mode &
4355 4356
                                       MODE_NO_ZERO_DATE) |
                                      MODE_NO_ZERO_IN_DATE, &error) >
4357
                      MYSQL_TIMESTAMP_ERROR);
4358

unknown's avatar
unknown committed
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);
unknown's avatar
unknown committed
4364
  }
4365

unknown's avatar
unknown committed
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;
    }
  }

unknown's avatar
unknown committed
4385
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4386
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
4387 4388 4389 4390 4391 4392
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
4393
  return error;
unknown's avatar
unknown committed
4394 4395
}

unknown's avatar
unknown committed
4396

unknown's avatar
unknown committed
4397
int Field_timestamp::store(double nr)
unknown's avatar
unknown committed
4398
{
unknown's avatar
unknown committed
4399
  int error= 0;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
4405 4406
    nr= 0;					// Avoid overflow on buff
    error= 1;
unknown's avatar
unknown committed
4407
  }
4408
  error|= Field_timestamp::store((longlong) rint(nr), FALSE);
4409
  return error;
unknown's avatar
unknown committed
4410 4411 4412
}


4413
int Field_timestamp::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
4414
{
4415
  MYSQL_TIME l_time;
4416 4417
  my_time_t timestamp= 0;
  int error;
4418
  my_bool in_dst_time_gap;
unknown's avatar
unknown committed
4419
  THD *thd= table ? table->in_use : current_thd;
unknown's avatar
unknown 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)
unknown's avatar
unknown 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

unknown's avatar
unknown committed
4451
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4452
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
4453 4454 4455 4456 4457
  {
    int4store(ptr,timestamp);
  }
  else
#endif
unknown's avatar
unknown committed
4458
    longstore(ptr,(uint32) timestamp);
4459

unknown's avatar
unknown committed
4460
  return error;
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
4473
  THD  *thd= table ? table->in_use : current_thd;
unknown's avatar
unknown committed
4474

4475
  thd->time_zone_used= 1;
unknown's avatar
unknown committed
4476
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4477
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
4491 4492 4493
}


unknown's avatar
unknown committed
4494
String *Field_timestamp::val_str(String *val_buffer, String *val_ptr)
unknown's avatar
unknown committed
4495
{
4496
  uint32 temp, temp2;
4497
  MYSQL_TIME time_tmp;
unknown's avatar
unknown committed
4498
  THD *thd= table ? table->in_use : current_thd;
unknown's avatar
unknown committed
4499
  char *to;
unknown's avatar
unknown committed
4500 4501

  val_buffer->alloc(field_length+1);
unknown's avatar
unknown committed
4502
  to= (char*) val_buffer->ptr();
4503
  val_buffer->length(field_length);
unknown's avatar
unknown committed
4504

4505
  thd->time_zone_used= 1;
unknown's avatar
unknown committed
4506
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4507
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
4517
  }
unknown's avatar
unknown committed
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;
unknown's avatar
unknown committed
4562 4563 4564
  return val_buffer;
}

4565

4566
bool Field_timestamp::get_date(MYSQL_TIME *ltime, uint fuzzydate)
unknown's avatar
unknown committed
4567 4568
{
  long temp;
unknown's avatar
unknown committed
4569
  THD *thd= table ? table->in_use : current_thd;
4570
  thd->time_zone_used= 1;
unknown's avatar
unknown committed
4571
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4572
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
4573 4574 4575 4576 4577 4578
    temp=uint4korr(ptr);
  else
#endif
    longget(temp,ptr);
  if (temp == 0L)
  {				      /* Zero time is "000000" */
unknown's avatar
unknown committed
4579
    if (fuzzydate & TIME_NO_ZERO_DATE)
unknown's avatar
unknown committed
4580
      return 1;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
4586 4587 4588 4589
  }
  return 0;
}

4590
bool Field_timestamp::get_time(MYSQL_TIME *ltime)
unknown's avatar
unknown committed
4591
{
unknown's avatar
unknown committed
4592
  return Field_timestamp::get_date(ltime,0);
unknown's avatar
unknown committed
4593 4594
}

4595 4596 4597

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


unknown's avatar
unknown committed
4604 4605 4606 4607
int Field_timestamp::cmp(const char *a_ptr, const char *b_ptr)
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4608
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown 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

unknown's avatar
unknown committed
4623 4624 4625
void Field_timestamp::sort_string(char *to,uint length __attribute__((unused)))
{
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4626
  if (!table || !table->s->db_low_byte_first)
unknown's avatar
unknown 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"));
unknown's avatar
unknown committed
4647 4648 4649 4650 4651
}


void Field_timestamp::set_time()
{
unknown's avatar
unknown committed
4652 4653
  THD *thd= table ? table->in_use : current_thd;
  long tmp= (long) thd->query_start();
4654
  set_notnull();
unknown's avatar
unknown committed
4655
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4656
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown 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
****************************************************************************/

unknown's avatar
unknown committed
4672
int Field_time::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
4673
{
4674
  MYSQL_TIME ltime;
unknown's avatar
unknown committed
4675
  long tmp;
4676 4677
  int error= 0;
  int warning;
4678

4679
  if (str_to_time(from, len, &ltime, &warning))
4680
  {
unknown's avatar
unknown committed
4681
    tmp=0L;
unknown's avatar
unknown committed
4682
    error= 2;
4683
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
4684
                         from, len, MYSQL_TIMESTAMP_TIME, 1);
4685
  }
unknown's avatar
unknown 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)
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
4700
      error= 1;
unknown's avatar
unknown committed
4701
    }
4702 4703 4704
    if (ltime.month)
      ltime.day=0;
    tmp=(ltime.day*24L+ltime.hour)*10000L+(ltime.minute*100+ltime.second);
unknown's avatar
unknown committed
4705
  }
4706
  
unknown's avatar
unknown committed
4707 4708
  if (ltime.neg)
    tmp= -tmp;
4709
  int3store(ptr,tmp);
4710
  return error;
unknown's avatar
unknown 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
}


unknown's avatar
unknown committed
4724
int Field_time::store(double nr)
unknown's avatar
unknown committed
4725 4726
{
  long tmp;
unknown's avatar
unknown committed
4727
  int error= 0;
4728
  if (nr > (double)TIME_MAX_VALUE)
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
4733
    error= 1;
unknown's avatar
unknown committed
4734
  }
4735
  else if (nr < (double)-TIME_MAX_VALUE)
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
4740
    error= 1;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
4753
      error= 1;
unknown's avatar
unknown committed
4754 4755 4756
    }
  }
  int3store(ptr,tmp);
4757
  return error;
unknown's avatar
unknown committed
4758 4759 4760
}


4761
int Field_time::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
4762 4763
{
  long tmp;
unknown's avatar
unknown committed
4764
  int error= 0;
4765
  if (nr < (longlong) -TIME_MAX_VALUE && !unsigned_val)
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
4771
    error= 1;
unknown's avatar
unknown committed
4772
  }
4773
  else if (nr > (longlong) TIME_MAX_VALUE || nr < 0 && unsigned_val)
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
4779
    error= 1;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
4790
      error= 1;
unknown's avatar
unknown committed
4791 4792 4793
    }
  }
  int3store(ptr,tmp);
4794
  return error;
unknown's avatar
unknown committed
4795 4796 4797 4798 4799
}


double Field_time::val_real(void)
{
unknown's avatar
unknown committed
4800
  uint32 j= (uint32) uint3korr(ptr);
unknown's avatar
unknown 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
*/

unknown's avatar
unknown 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);
unknown's avatar
unknown committed
4820
  long tmp=(long) sint3korr(ptr);
4821
  ltime.neg= 0;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
4832 4833 4834
  return val_buffer;
}

4835

unknown's avatar
unknown committed
4836
/*
unknown's avatar
unknown committed
4837
  Normally we would not consider 'time' as a valid date, but we allow
unknown's avatar
unknown 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
{
unknown's avatar
unknown committed
4844
  long tmp;
unknown's avatar
unknown committed
4845
  THD *thd= table ? table->in_use : current_thd;
unknown's avatar
unknown committed
4846
  if (!(fuzzydate & TIME_FUZZY_DATE))
unknown's avatar
unknown committed
4847
  {
unknown's avatar
unknown 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,
unknown's avatar
unknown committed
4851
                        thd->row_count);
unknown's avatar
unknown 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;
}

unknown's avatar
unknown committed
4869

4870
bool Field_time::get_time(MYSQL_TIME *ltime)
unknown's avatar
unknown 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;
  }
unknown's avatar
unknown committed
4879
  ltime->day= 0;
unknown's avatar
unknown committed
4880
  ltime->hour=   (int) (tmp/10000);
unknown's avatar
unknown committed
4881
  tmp-=ltime->hour*10000;
unknown's avatar
unknown committed
4882 4883
  ltime->minute= (int) tmp/100;
  ltime->second= (int) tmp % 100;
unknown's avatar
unknown committed
4884
  ltime->second_part=0;
4885
  ltime->time_type= MYSQL_TIMESTAMP_TIME;
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
4896
  return protocol->store_time(&tm);
4897 4898 4899
}


unknown's avatar
unknown committed
4900 4901
int Field_time::cmp(const char *a_ptr, const char *b_ptr)
{
unknown's avatar
unknown committed
4902 4903 4904
  int32 a,b;
  a=(int32) sint3korr(a_ptr);
  b=(int32) sint3korr(b_ptr);
unknown's avatar
unknown 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"));
unknown's avatar
unknown 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!
****************************************************************************/

unknown's avatar
unknown committed
4926
int Field_year::store(const char *from, uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
4927
{
4928
  char *end;
unknown's avatar
unknown committed
4929
  int error;
unknown's avatar
unknown committed
4930
  longlong nr= cs->cset->strntoull10rnd(cs, from, len, 0, &end, &error);
unknown's avatar
unknown committed
4931

unknown's avatar
unknown committed
4932 4933
  if (nr < 0 || nr >= 100 && nr <= 1900 || nr > 2155 || 
      error == MY_ERRNO_ERANGE)
unknown's avatar
unknown committed
4934 4935
  {
    *ptr=0;
4936
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4937
    return 1;
unknown's avatar
unknown committed
4938
  }
unknown's avatar
unknown committed
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;
    }
unknown's avatar
unknown committed
4947
    error= 1;
unknown's avatar
unknown committed
4948
  }
unknown's avatar
unknown committed
4949

unknown's avatar
unknown 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;
unknown's avatar
unknown committed
4958
  return error;
unknown's avatar
unknown committed
4959 4960
}

unknown's avatar
unknown committed
4961

unknown's avatar
unknown committed
4962
int Field_year::store(double nr)
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
4970 4971
}

4972 4973

int Field_year::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown 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;
unknown's avatar
unknown 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;
unknown's avatar
unknown committed
4990 4991
}

4992

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

4999

unknown's avatar
unknown committed
5000 5001 5002 5003 5004
double Field_year::val_real(void)
{
  return (double) Field_year::val_int();
}

5005

unknown's avatar
unknown 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

unknown's avatar
unknown 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

unknown's avatar
unknown committed
5028 5029
void Field_year::sql_type(String &res) const
{
unknown's avatar
unknown committed
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));
unknown's avatar
unknown 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
****************************************************************************/

unknown's avatar
unknown committed
5043
int Field_date::store(const char *from, uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
5044
{
5045
  MYSQL_TIME l_time;
unknown's avatar
unknown committed
5046
  uint32 tmp;
5047
  int error;
unknown's avatar
unknown committed
5048
  THD *thd= table ? table->in_use : current_thd;
5049

unknown's avatar
unknown committed
5050
  if (str_to_datetime(from, len, &l_time, TIME_FUZZY_DATE |
unknown's avatar
unknown committed
5051
                      (thd->variables.sql_mode &
unknown's avatar
unknown committed
5052 5053 5054
                       (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
                        MODE_INVALID_DATES)),
                      &error) <= MYSQL_TIMESTAMP_ERROR)
5055
  {
5056
    tmp= 0;
unknown's avatar
unknown committed
5057
    error= 2;
5058
  }
unknown's avatar
unknown committed
5059
  else
unknown's avatar
unknown committed
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

unknown's avatar
unknown committed
5066
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5067
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5068 5069 5070 5071 5072 5073
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
5074
  return error;
unknown's avatar
unknown committed
5075 5076 5077
}


unknown's avatar
unknown committed
5078
int Field_date::store(double nr)
unknown's avatar
unknown committed
5079
{
5080
  longlong tmp;
unknown's avatar
unknown committed
5081
  int error= 0;
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
5090
    error= 1;
unknown's avatar
unknown committed
5091 5092
  }
  else
5093
    tmp= (longlong) rint(nr);
5094

5095
  return Field_date::store(tmp, TRUE);
unknown's avatar
unknown committed
5096 5097 5098
}


5099
int Field_date::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
5100
{
5101
  MYSQL_TIME not_used;
5102 5103
  int error;
  longlong initial_nr= nr;
unknown's avatar
unknown committed
5104
  THD *thd= table ? table->in_use : current_thd;
5105 5106

  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
unknown's avatar
unknown 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))
unknown's avatar
unknown committed
5113
  {
5114 5115
    nr= 0;
    error= 2;
unknown's avatar
unknown 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

unknown's avatar
unknown committed
5127
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5128
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5129
  {
5130
    int4store(ptr, nr);
unknown's avatar
unknown committed
5131 5132 5133
  }
  else
#endif
5134
    longstore(ptr, nr);
5135
  return error;
unknown's avatar
unknown committed
5136 5137 5138
}


unknown's avatar
unknown committed
5139 5140 5141
bool Field_date::send_binary(Protocol *protocol)
{
  longlong tmp= Field_date::val_int();
5142
  MYSQL_TIME tm;
unknown's avatar
unknown committed
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);
}

unknown's avatar
unknown committed
5149 5150 5151 5152 5153

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

5162

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

5175

unknown's avatar
unknown committed
5176 5177 5178
String *Field_date::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
5179
  MYSQL_TIME ltime;
unknown's avatar
unknown committed
5180 5181 5182
  val_buffer->alloc(field_length);
  int32 tmp;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5183
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown 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);
unknown's avatar
unknown committed
5193 5194 5195
  return val_buffer;
}

5196

unknown's avatar
unknown committed
5197 5198 5199 5200
int Field_date::cmp(const char *a_ptr, const char *b_ptr)
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5201
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown 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
unknown's avatar
unknown committed
5219
  if (!table || !table->s->db_low_byte_first)
unknown's avatar
unknown 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"));
unknown's avatar
unknown committed
5239 5240
}

5241

unknown's avatar
unknown 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 5262 5263
/*
  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)
*/

unknown's avatar
unknown committed
5264
int Field_newdate::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
5265
{
5266 5267
  long tmp;
  MYSQL_TIME l_time;
5268
  int error;
unknown's avatar
unknown committed
5269
  THD *thd= table ? table->in_use : current_thd;
5270 5271 5272 5273 5274 5275 5276 5277
  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)
  {
5278
    tmp= 0;
unknown's avatar
unknown committed
5279
    error= 2;
5280
  }
unknown's avatar
unknown committed
5281
  else
5282
  {
5283
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
5284
    if (!error && (ret != MYSQL_TIMESTAMP_DATE) &&
5285
        (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
5286
      error= 3;                                 // Datetime was cut (note)
5287
  }
5288 5289

  if (error)
5290 5291 5292
    set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
                         MYSQL_ERROR::WARN_LEVEL_WARN,
                         WARN_DATA_TRUNCATED,
5293
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
5294

5295
  int3store(ptr, tmp);
5296
  return error;
unknown's avatar
unknown committed
5297 5298
}

5299

unknown's avatar
unknown committed
5300
int Field_newdate::store(double nr)
unknown's avatar
unknown committed
5301 5302
{
  if (nr < 0.0 || nr > 99991231235959.0)
5303
  {
5304
    int3store(ptr,(int32) 0);
5305
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5306
                         WARN_DATA_TRUNCATED, nr, MYSQL_TIMESTAMP_DATE);
5307 5308
    return 1;
  }
5309
  return Field_newdate::store((longlong) rint(nr), FALSE);
unknown's avatar
unknown committed
5310 5311 5312
}


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

5332 5333 5334 5335
  if (!error && l_time.time_type != MYSQL_TIMESTAMP_DATE &&
      (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
    error= 3;

5336
  if (error)
5337 5338 5339 5340 5341
    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);
5342 5343

  int3store(ptr,tmp);
5344
  return error;
unknown's avatar
unknown committed
5345 5346
}

5347

5348
int Field_newdate::store_time(MYSQL_TIME *ltime, timestamp_type time_type)
unknown's avatar
unknown committed
5349 5350
{
  long tmp;
5351
  int error= 0;
5352 5353
  if (time_type == MYSQL_TIMESTAMP_DATE ||
      time_type == MYSQL_TIMESTAMP_DATETIME)
5354
  {
unknown's avatar
unknown committed
5355
    tmp=ltime->year*16*32+ltime->month*32+ltime->day;
5356 5357 5358 5359 5360
    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))
5361
    {
5362
      char buff[MAX_DATE_STRING_REP_LENGTH];
5363 5364 5365 5366 5367
      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);
    }
5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378
    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;
    }
5379
  }
unknown's avatar
unknown committed
5380 5381 5382
  else
  {
    tmp=0;
5383
    error= 1;
5384
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
5385 5386
  }
  int3store(ptr,tmp);
5387
  return error;
unknown's avatar
unknown committed
5388 5389
}

5390

unknown's avatar
unknown committed
5391 5392
bool Field_newdate::send_binary(Protocol *protocol)
{
5393
  MYSQL_TIME tm;
unknown's avatar
unknown committed
5394 5395 5396
  Field_newdate::get_date(&tm,0);
  return protocol->store_date(&tm);
}
unknown's avatar
unknown committed
5397

5398

unknown's avatar
unknown committed
5399 5400 5401 5402 5403
double Field_newdate::val_real(void)
{
  return (double) Field_newdate::val_int();
}

5404

unknown's avatar
unknown committed
5405 5406
longlong Field_newdate::val_int(void)
{
unknown's avatar
unknown committed
5407
  ulong j= uint3korr(ptr);
unknown's avatar
unknown committed
5408 5409 5410 5411
  j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
  return (longlong) j;
}

5412

unknown's avatar
unknown committed
5413 5414 5415 5416 5417
String *Field_newdate::val_str(String *val_buffer,
			       String *val_ptr __attribute__((unused)))
{
  val_buffer->alloc(field_length);
  val_buffer->length(field_length);
unknown's avatar
unknown committed
5418
  uint32 tmp=(uint32) uint3korr(ptr);
unknown's avatar
unknown committed
5419 5420 5421 5422
  int part;
  char *pos=(char*) val_buffer->ptr()+10;

  /* Open coded to get more speed */
unknown's avatar
unknown committed
5423
  *pos--=0;					// End NULL
unknown's avatar
unknown committed
5424
  part=(int) (tmp & 31);
unknown's avatar
unknown committed
5425 5426 5427
  *pos--= (char) ('0'+part%10);
  *pos--= (char) ('0'+part/10);
  *pos--= '-';
unknown's avatar
unknown committed
5428
  part=(int) (tmp >> 5 & 15);
unknown's avatar
unknown committed
5429 5430 5431
  *pos--= (char) ('0'+part%10);
  *pos--= (char) ('0'+part/10);
  *pos--= '-';
unknown's avatar
unknown committed
5432
  part=(int) (tmp >> 9);
unknown's avatar
unknown committed
5433 5434 5435 5436
  *pos--= (char) ('0'+part%10); part/=10;
  *pos--= (char) ('0'+part%10); part/=10;
  *pos--= (char) ('0'+part%10); part/=10;
  *pos=   (char) ('0'+part);
unknown's avatar
unknown committed
5437 5438 5439
  return val_buffer;
}

5440

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

5453

5454
bool Field_newdate::get_time(MYSQL_TIME *ltime)
unknown's avatar
unknown committed
5455
{
unknown's avatar
unknown committed
5456
  return Field_newdate::get_date(ltime,0);
unknown's avatar
unknown committed
5457 5458
}

5459

unknown's avatar
unknown committed
5460 5461
int Field_newdate::cmp(const char *a_ptr, const char *b_ptr)
{
unknown's avatar
unknown committed
5462 5463 5464
  uint32 a,b;
  a=(uint32) uint3korr(a_ptr);
  b=(uint32) uint3korr(b_ptr);
unknown's avatar
unknown committed
5465 5466 5467
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

5468

unknown's avatar
unknown committed
5469 5470 5471 5472 5473 5474 5475
void Field_newdate::sort_string(char *to,uint length __attribute__((unused)))
{
  to[0] = ptr[2];
  to[1] = ptr[1];
  to[2] = ptr[0];
}

5476

unknown's avatar
unknown committed
5477 5478
void Field_newdate::sql_type(String &res) const
{
5479
  res.set_ascii(STRING_WITH_LEN("date"));
unknown's avatar
unknown committed
5480 5481 5482 5483 5484 5485 5486 5487 5488 5489
}


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

unknown's avatar
unknown committed
5490
int Field_datetime::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
5491
{
5492
  MYSQL_TIME time_tmp;
5493 5494
  int error;
  ulonglong tmp= 0;
5495
  enum enum_mysql_timestamp_type func_res;
unknown's avatar
unknown committed
5496
  THD *thd= table ? table->in_use : current_thd;
5497

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

5509
  if (error)
5510
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5511
                         ER_WARN_DATA_OUT_OF_RANGE,
5512
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
5513

unknown's avatar
unknown committed
5514
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5515
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5516 5517 5518 5519 5520 5521
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
5522
  return error;
unknown's avatar
unknown committed
5523 5524 5525
}


unknown's avatar
unknown committed
5526
int Field_datetime::store(double nr)
unknown's avatar
unknown committed
5527
{
unknown's avatar
unknown committed
5528
  int error= 0;
unknown's avatar
unknown committed
5529 5530
  if (nr < 0.0 || nr > 99991231235959.0)
  {
5531 5532
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_OUT_OF_RANGE,
5533
                         nr, MYSQL_TIMESTAMP_DATETIME);
5534
    nr= 0.0;
unknown's avatar
unknown committed
5535
    error= 1;
unknown's avatar
unknown committed
5536
  }
5537
  error|= Field_datetime::store((longlong) rint(nr), FALSE);
5538
  return error;
unknown's avatar
unknown committed
5539 5540 5541
}


5542
int Field_datetime::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
5543
{
5544
  MYSQL_TIME not_used;
5545 5546
  int error;
  longlong initial_nr= nr;
unknown's avatar
unknown committed
5547
  THD *thd= table ? table->in_use : current_thd;
5548

5549
  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
unknown's avatar
unknown committed
5550
                                         (thd->variables.sql_mode &
5551 5552 5553 5554
                                          (MODE_NO_ZERO_IN_DATE |
                                           MODE_NO_ZERO_DATE |
                                           MODE_INVALID_DATES))), &error);

5555
  if (nr == LL(-1))
5556
  {
5557 5558
    nr= 0;
    error= 2;
5559
  }
5560

5561 5562 5563 5564 5565 5566
  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);

unknown's avatar
unknown committed
5567
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5568
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5569 5570 5571 5572 5573 5574
  {
    int8store(ptr,nr);
  }
  else
#endif
    longlongstore(ptr,nr);
5575
  return error;
unknown's avatar
unknown committed
5576 5577
}

unknown's avatar
unknown committed
5578

5579
int Field_datetime::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
unknown's avatar
unknown committed
5580 5581
{
  longlong tmp;
5582
  int error= 0;
5583
  /*
5584
    We don't perform range checking here since values stored in MYSQL_TIME
5585 5586
    structure always fit into DATETIME range.
  */
5587 5588
  if (time_type == MYSQL_TIMESTAMP_DATE ||
      time_type == MYSQL_TIMESTAMP_DATETIME)
5589
  {
unknown's avatar
unknown committed
5590 5591
    tmp=((ltime->year*10000L+ltime->month*100+ltime->day)*LL(1000000)+
	 (ltime->hour*10000L+ltime->minute*100+ltime->second));
5592 5593 5594 5595 5596
    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))
5597
    {
5598
      char buff[MAX_DATE_STRING_REP_LENGTH];
5599 5600 5601 5602 5603 5604
      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);
    }
  }
unknown's avatar
unknown committed
5605 5606 5607
  else
  {
    tmp=0;
5608
    error= 1;
5609
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
5610 5611
  }
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5612
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5613 5614 5615 5616 5617 5618
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
5619
  return error;
unknown's avatar
unknown committed
5620 5621
}

unknown's avatar
unknown committed
5622 5623
bool Field_datetime::send_binary(Protocol *protocol)
{
5624
  MYSQL_TIME tm;
5625
  Field_datetime::get_date(&tm, TIME_FUZZY_DATE);
unknown's avatar
unknown committed
5626 5627 5628
  return protocol->store(&tm);
}

unknown's avatar
unknown committed
5629 5630 5631 5632 5633 5634 5635 5636 5637 5638

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

longlong Field_datetime::val_int(void)
{
  longlong j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5639
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658
    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
unknown's avatar
unknown committed
5659
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5660 5661 5662 5663 5664 5665
    tmp=sint8korr(ptr);
  else
#endif
    longlongget(tmp,ptr);

  /*
unknown's avatar
unknown committed
5666
    Avoid problem with slow longlong arithmetic and sprintf
unknown's avatar
unknown committed
5667 5668 5669 5670 5671
  */

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

5672
  pos= (char*) val_buffer->ptr() + MAX_DATETIME_WIDTH;
unknown's avatar
unknown committed
5673
  *pos--=0;
unknown's avatar
unknown committed
5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692
  *pos--= (char) ('0'+(char) (part2%10)); part2/=10;
  *pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
  *pos--= ':';
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= ':';
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= (char) ('0'+(char) part3);
  *pos--= ' ';
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
  *pos--= '-';
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
  *pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
  *pos--= '-';
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
  *pos=(char) ('0'+(char) part3);
unknown's avatar
unknown committed
5693 5694 5695
  return val_buffer;
}

5696
bool Field_datetime::get_date(MYSQL_TIME *ltime, uint fuzzydate)
unknown's avatar
unknown committed
5697 5698
{
  longlong tmp=Field_datetime::val_int();
unknown's avatar
unknown committed
5699 5700 5701
  uint32 part1,part2;
  part1=(uint32) (tmp/LL(1000000));
  part2=(uint32) (tmp - (ulonglong) part1*LL(1000000));
unknown's avatar
unknown committed
5702

5703
  ltime->time_type=	MYSQL_TIMESTAMP_DATETIME;
unknown's avatar
unknown committed
5704 5705 5706 5707 5708 5709 5710 5711
  ltime->neg=		0;
  ltime->second_part=	0;
  ltime->second=	(int) (part2%100);
  ltime->minute=	(int) (part2/100%100);
  ltime->hour=		(int) (part2/10000);
  ltime->day=		(int) (part1%100);
  ltime->month= 	(int) (part1/100%100);
  ltime->year= 		(int) (part1/10000);
unknown's avatar
unknown committed
5712
  return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ? 1 : 0;
unknown's avatar
unknown committed
5713 5714
}

5715
bool Field_datetime::get_time(MYSQL_TIME *ltime)
unknown's avatar
unknown committed
5716
{
unknown's avatar
unknown committed
5717
  return Field_datetime::get_date(ltime,0);
unknown's avatar
unknown committed
5718 5719 5720 5721 5722 5723
}

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

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

5778 5779 5780 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
/*
  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);
}


unknown's avatar
unknown committed
5876 5877
	/* Copy a string and fill with space */

unknown's avatar
unknown committed
5878
int Field_string::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
5879
{
unknown's avatar
unknown committed
5880
  uint copy_length;
5881 5882 5883
  const char *well_formed_error_pos;
  const char *cannot_convert_error_pos;
  const char *from_end_pos;
5884

5885 5886
  /* See the comment for Field_long::store(long long) */
  DBUG_ASSERT(table->in_use == current_thd);
5887

5888 5889 5890 5891 5892 5893 5894
  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);
5895 5896 5897

  /* Append spaces if the string was shorter than the field. */
  if (copy_length < field_length)
unknown's avatar
unknown committed
5898
    field_charset->cset->fill(field_charset,ptr+copy_length,
5899
                              field_length-copy_length,
5900
                              field_charset->pad_char);
5901

5902 5903 5904 5905
  if (check_string_copy_error(this, well_formed_error_pos,
                              cannot_convert_error_pos, from + length))
    return 2;

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


5922 5923 5924 5925
/*
  Store double value in Field_string or Field_varstring.

  SYNOPSIS
unknown's avatar
unknown committed
5926
    store(double nr)
5927 5928 5929 5930 5931 5932
    nr            number

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

unknown's avatar
unknown committed
5933
int Field_str::store(double nr)
unknown's avatar
unknown committed
5934
{
5935
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
5936
  uint length;
unknown's avatar
unknown committed
5937
  bool use_scientific_notation= TRUE;
5938
  uint local_char_length= field_length / charset()->mbmaxlen;
5939 5940 5941 5942 5943 5944
  /*
    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;
5945 5946 5947
  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))
unknown's avatar
unknown committed
5948
    use_scientific_notation= FALSE;
unknown's avatar
unknown committed
5949

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

unknown's avatar
unknown committed
5967

5968
int Field_string::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
5969
{
unknown's avatar
unknown committed
5970 5971 5972
  char buff[64];
  int  l;
  CHARSET_INFO *cs=charset();
5973 5974
  l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),
                                   unsigned_val ? 10 : -10, nr);
unknown's avatar
unknown committed
5975
  return Field_string::store(buff,(uint)l,cs);
unknown's avatar
unknown committed
5976 5977
}

5978

unknown's avatar
unknown committed
5979 5980
int Field_longstr::store_decimal(const my_decimal *d)
{
5981 5982
  char buff[DECIMAL_MAX_STR_LENGTH+1];
  String str(buff, sizeof(buff), &my_charset_bin);
unknown's avatar
unknown committed
5983
  my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
5984
  return store(str.ptr(), str.length(), str.charset());
unknown's avatar
unknown committed
5985
}
unknown's avatar
unknown committed
5986

5987

unknown's avatar
unknown committed
5988 5989
double Field_string::val_real(void)
{
5990 5991
  int error;
  char *end;
5992
  CHARSET_INFO *cs= charset();
5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008
  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;
unknown's avatar
unknown committed
6009 6010 6011 6012 6013
}


longlong Field_string::val_int(void)
{
6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032
  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;
unknown's avatar
unknown committed
6033 6034 6035 6036 6037 6038
}


String *Field_string::val_str(String *val_buffer __attribute__((unused)),
			      String *val_ptr)
{
unknown's avatar
unknown committed
6039
  uint length= field_charset->cset->lengthsp(field_charset, ptr, field_length);
6040 6041
  /* See the comment for Field_long::store(long long) */
  DBUG_ASSERT(table->in_use == current_thd);
unknown's avatar
unknown committed
6042
  val_ptr->set((const char*) ptr, length, field_charset);
unknown's avatar
unknown committed
6043 6044 6045 6046
  return val_ptr;
}


unknown's avatar
unknown committed
6047 6048
my_decimal *Field_string::val_decimal(my_decimal *decimal_value)
{
6049
  int err= str2my_decimal(E_DEC_FATAL_ERROR, ptr, field_length, charset(),
unknown's avatar
unknown committed
6050
                 decimal_value);
6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062
  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());
  }

unknown's avatar
unknown committed
6063 6064 6065 6066
  return decimal_value;
}


unknown's avatar
unknown committed
6067 6068
int Field_string::cmp(const char *a_ptr, const char *b_ptr)
{
unknown's avatar
unknown committed
6069 6070 6071 6072 6073 6074 6075 6076 6077 6078
  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;
unknown's avatar
unknown committed
6079 6080 6081 6082 6083 6084
  /*
    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,
6085 6086
                                          (const uchar*) b_ptr, b_len,
                                          0);
unknown's avatar
unknown committed
6087 6088
}

6089

unknown's avatar
unknown committed
6090 6091
void Field_string::sort_string(char *to,uint length)
{
unknown's avatar
unknown committed
6092 6093 6094
  IF_DBUG(uint tmp=) my_strnxfrm(field_charset,
                                 (unsigned char *) to, length,
                                 (unsigned char *) ptr, field_length);
6095
  DBUG_ASSERT(tmp == length);
unknown's avatar
unknown committed
6096 6097 6098 6099 6100
}


void Field_string::sql_type(String &res) const
{
6101
  THD *thd= table->in_use;
unknown's avatar
unknown committed
6102
  CHARSET_INFO *cs=res.charset();
6103 6104 6105 6106 6107 6108 6109
  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") :
6110
			      (has_charset() ? "char" : "binary")),
6111
                             (int) field_length / charset()->mbmaxlen);
6112
  res.length(length);
6113 6114
  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
      has_charset() && (charset()->state & MY_CS_BINSORT))
6115
    res.append(STRING_WITH_LEN(" binary"));
unknown's avatar
unknown committed
6116 6117
}

6118

unknown's avatar
unknown committed
6119
char *Field_string::pack(char *to, const char *from, uint max_length)
6120
{
unknown's avatar
unknown committed
6121
  uint length=      min(field_length,max_length);
6122 6123 6124 6125 6126
  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);
unknown's avatar
unknown committed
6127 6128
  while (length && from[length-1] == ' ')
    length--;
6129
  *to++= (char) (uchar) length;
6130
  if (field_length > 255)
6131
    *to++= (char) (uchar) (length >> 8);
6132 6133
  memcpy(to, from, length);
  return to+length;
6134 6135 6136
}


unknown's avatar
unknown committed
6137 6138
const char *Field_string::unpack(char *to, const char *from)
{
6139 6140 6141 6142 6143 6144 6145 6146
  uint length;
  if (field_length > 255)
  {
    length= uint2korr(from);
    from+= 2;
  }
  else
    length= (uint) (uchar) *from++;
unknown's avatar
unknown committed
6147 6148 6149 6150 6151 6152
  memcpy(to, from, (int) length);
  bfill(to+length, field_length - length, ' ');
  return from+length;
}


6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170
/*
  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)
unknown's avatar
unknown committed
6171
{
6172
  uint a_length, b_length;
6173
  if (length > 255)
6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184
  {
    a_length= uint2korr(a);
    b_length= uint2korr(b);
    a+= 2;
    b+= 2;
  }
  else
  {
    a_length= (uint) (uchar) *a++;
    b_length= (uint) (uchar) *b++;
  }
6185 6186 6187 6188
  return field_charset->coll->strnncollsp(field_charset,
                                          (const uchar*) a, a_length,
                                          (const uchar*) b, b_length,
                                          insert_or_update);
unknown's avatar
unknown committed
6189 6190 6191
}


6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208
/*
  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)
6209
{
6210
  uint row_length, local_key_length;
6211 6212
  char *end;
  if (length > 255)
6213
  {
6214
    local_key_length= uint2korr(key);
6215
    key+= 2;
6216 6217
  }
  else
6218
    local_key_length= (uint) (uchar) *key++;
6219 6220 6221
  
  /* Only use 'length' of key, not field_length */
  end= ptr + length;
6222 6223
  while (end > ptr && end[-1] == ' ')
    end--;
6224 6225 6226 6227
  row_length= (uint) (end - ptr);

  return field_charset->coll->strnncollsp(field_charset,
                                          (const uchar*) ptr, row_length,
6228
                                          (const uchar*) key, local_key_length,
6229
                                          insert_or_update);
6230 6231 6232
}


6233
uint Field_string::packed_col_length(const char *data_ptr, uint length)
unknown's avatar
unknown committed
6234
{
6235
  if (length > 255)
6236
    return uint2korr(data_ptr)+2;
6237
  return (uint) ((uchar) *data_ptr)+1;
unknown's avatar
unknown committed
6238 6239
}

6240

unknown's avatar
unknown committed
6241 6242
uint Field_string::max_packed_col_length(uint max_length)
{
6243
  return (max_length > 255 ? 2 : 1)+max_length;
unknown's avatar
unknown committed
6244 6245
}

6246
uint Field_string::get_key_image(char *buff, uint length, imagetype type_arg)
unknown's avatar
unknown committed
6247
{
6248
  uint bytes = my_charpos(field_charset, ptr, ptr + field_length,
unknown's avatar
unknown committed
6249 6250 6251
                          length / field_charset->mbmaxlen);
  memcpy(buff, ptr, bytes);
  if (bytes < length)
6252 6253
    field_charset->cset->fill(field_charset, buff + bytes, length - bytes, 
                              field_charset->pad_char);
unknown's avatar
unknown committed
6254 6255
  return bytes;
}
unknown's avatar
unknown committed
6256

6257 6258
Field *Field_string::new_field(MEM_ROOT *root, struct st_table *new_table,
                               bool keep_type)
6259
{
6260
  Field *field;
6261

6262
  if (type() != MYSQL_TYPE_VAR_STRING || keep_type)
6263
    field= Field::new_field(root, new_table, keep_type);
6264 6265
  else
  {
6266

6267 6268 6269 6270 6271
    /*
      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.
    */
6272
    field= new Field_varstring(field_length, maybe_null(),
6273 6274 6275 6276 6277 6278 6279
                                   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.
    */
6280
    field->orig_table= orig_table;
6281
  }
6282
  return field;
6283 6284
}

unknown's avatar
unknown committed
6285
/****************************************************************************
6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298
  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.
unknown's avatar
unknown committed
6299 6300
****************************************************************************/

unknown's avatar
unknown committed
6301
const uint Field_varstring::MAX_SIZE= UINT_MAX16;
unknown's avatar
unknown committed
6302

unknown's avatar
unknown committed
6303
int Field_varstring::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
6304
{
6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316
  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);
unknown's avatar
unknown committed
6317

6318 6319 6320 6321
  if (length_bytes == 1)
    *ptr= (uchar) copy_length;
  else
    int2store(ptr, copy_length);
6322

6323 6324 6325 6326
  if (check_string_copy_error(this, well_formed_error_pos,
                              cannot_convert_error_pos, from + length))
    return 2;

6327
  // Check if we lost something other than just trailing spaces
6328
  if ((from_end_pos < from + length) && table->in_use->count_cuted_fields)
6329
  {
6330 6331 6332 6333
    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);
unknown's avatar
unknown committed
6334
    return 2;
6335
  }
unknown's avatar
unknown committed
6336
  return 0;
unknown's avatar
unknown committed
6337 6338 6339
}


6340
int Field_varstring::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
6341
{
unknown's avatar
unknown committed
6342
  char buff[64];
6343 6344 6345 6346
  uint  length;
  length= (uint) (field_charset->cset->longlong10_to_str)(field_charset,
                                                          buff,
                                                          sizeof(buff),
6347 6348 6349
                                                          (unsigned_val ? 10:
                                                           -10),
                                                           nr);
6350
  return Field_varstring::store(buff, length, field_charset);
unknown's avatar
unknown committed
6351 6352 6353 6354 6355
}


double Field_varstring::val_real(void)
{
6356
  int not_used;
6357
  char *end_not_used;
6358
  uint length= length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
6359
  return my_strntod(field_charset, ptr+length_bytes, length, &end_not_used,
6360
                    &not_used);
unknown's avatar
unknown committed
6361 6362 6363 6364 6365
}


longlong Field_varstring::val_int(void)
{
6366
  int not_used;
6367
  char *end_not_used;
6368
  uint length= length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
6369 6370
  return my_strntoll(field_charset, ptr+length_bytes, length, 10,
                     &end_not_used, &not_used);
unknown's avatar
unknown committed
6371 6372 6373 6374 6375
}

String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
				 String *val_ptr)
{
6376 6377
  uint length=  length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
  val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
unknown's avatar
unknown committed
6378 6379 6380 6381
  return val_ptr;
}


unknown's avatar
unknown committed
6382 6383 6384 6385 6386 6387 6388 6389 6390
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;
}


unknown's avatar
unknown committed
6391 6392
int Field_varstring::cmp(const char *a_ptr, const char *b_ptr)
{
6393
  uint a_length, b_length;
unknown's avatar
unknown committed
6394
  int diff;
6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405

  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);
  }
6406 6407
  diff= field_charset->coll->strnncollsp(field_charset,
                                         (const uchar*) a_ptr+
6408
                                         length_bytes,
6409 6410
                                         a_length,
                                         (const uchar*) b_ptr+
6411
                                         length_bytes,
6412 6413
                                         b_length,0);
  return diff;
unknown's avatar
unknown committed
6414 6415
}

6416

6417 6418 6419 6420
/*
  NOTE: varstring and blob keys are ALWAYS stored with a 2 byte length prefix
*/

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

6426 6427 6428
  local_char_length= my_charpos(field_charset, ptr + length_bytes,
                          ptr + length_bytes + length, local_char_length);
  set_if_smaller(length, local_char_length);
6429 6430 6431 6432 6433 6434
  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);
unknown's avatar
unknown committed
6435 6436
}

6437

6438 6439 6440 6441 6442 6443 6444 6445
/*
  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)
*/

6446 6447
int Field_varstring::key_cmp(const byte *a,const byte *b)
{
6448 6449 6450 6451 6452 6453 6454 6455
  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);
6456 6457 6458
}


unknown's avatar
unknown committed
6459 6460
void Field_varstring::sort_string(char *to,uint length)
{
6461
  uint tot_length=  length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472

  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;
  }
 
6473 6474
  tot_length= my_strnxfrm(field_charset,
			  (uchar*) to, length,
6475
			  (uchar*) ptr + length_bytes,
6476
			  tot_length);
unknown's avatar
unknown committed
6477
  DBUG_ASSERT(tot_length == length);
unknown's avatar
unknown committed
6478 6479 6480
}


6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492
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;
}


unknown's avatar
unknown committed
6493 6494
void Field_varstring::sql_type(String &res) const
{
6495
  THD *thd= table->in_use;
unknown's avatar
unknown committed
6496
  CHARSET_INFO *cs=res.charset();
6497 6498 6499 6500 6501 6502
  ulong length;

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

6509

unknown's avatar
unknown committed
6510
uint32 Field_varstring::data_length(const char *from)
6511 6512 6513 6514
{
  return length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
}

6515 6516 6517 6518 6519
/*
  Functions to create a packed row.
  Here the number of length bytes are depending on the given max_length
*/

unknown's avatar
unknown committed
6520 6521
char *Field_varstring::pack(char *to, const char *from, uint max_length)
{
6522
  uint length= length_bytes == 1 ? (uint) (uchar) *from : uint2korr(from);
6523
  set_if_smaller(max_length, field_length);
unknown's avatar
unknown committed
6524 6525
  if (length > max_length)
    length=max_length;
unknown's avatar
unknown committed
6526
  *to++= (char) (length & 255);
unknown's avatar
unknown committed
6527
  if (max_length > 255)
unknown's avatar
unknown committed
6528
    *to++= (char) (length >> 8);
unknown's avatar
unknown committed
6529
  if (length)
6530
    memcpy(to, from+length_bytes, length);
unknown's avatar
unknown committed
6531 6532 6533 6534
  return to+length;
}


6535
char *Field_varstring::pack_key(char *to, const char *key, uint max_length)
6536
{
6537
  uint length=  length_bytes == 1 ? (uint) (uchar) *key : uint2korr(key);
6538
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
6539
                     max_length/field_charset->mbmaxlen : max_length);
6540
  key+= length_bytes;
6541
  if (length > local_char_length)
6542
  {
6543 6544 6545
    local_char_length= my_charpos(field_charset, key, key+length,
                                  local_char_length);
    set_if_smaller(length, local_char_length);
6546
  }
6547 6548 6549 6550
  *to++= (char) (length & 255);
  if (max_length > 255)
    *to++= (char) (length >> 8);
  if (length)
6551 6552 6553 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
    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);
6616 6617 6618 6619
  return to+length;
}


6620 6621 6622 6623
/*
  unpack field packed with Field_varstring::pack()
*/

unknown's avatar
unknown committed
6624 6625 6626
const char *Field_varstring::unpack(char *to, const char *from)
{
  uint length;
6627
  if (length_bytes == 1)
unknown's avatar
unknown committed
6628 6629 6630
    length= (uint) (uchar) (*to= *from++);
  else
  {
6631 6632 6633
    length= uint2korr(from);
    to[0]= *from++;
    to[1]= *from++;
unknown's avatar
unknown committed
6634 6635
  }
  if (length)
6636
    memcpy(to+ length_bytes, from, length);
unknown's avatar
unknown committed
6637 6638 6639 6640
  return from+length;
}


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

6662

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

6673
  if (key_length_arg > 255)
6674
  {
6675
    b_length=uint2korr(b); b+= HA_KEY_BLOB_LENGTH;
6676 6677 6678
  }
  else
    b_length= (uint) (uchar) *b++;
6679

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

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

6694

6695
uint Field_varstring::packed_col_length(const char *data_ptr, uint length)
unknown's avatar
unknown committed
6696
{
6697
  if (length > 255)
6698
    return uint2korr(data_ptr)+2;
6699
  return (uint) ((uchar) *data_ptr)+1;
unknown's avatar
unknown committed
6700 6701
}

6702

unknown's avatar
unknown committed
6703 6704
uint Field_varstring::max_packed_col_length(uint max_length)
{
6705
  return (max_length > 255 ? 2 : 1)+max_length;
unknown's avatar
unknown committed
6706 6707
}

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

6730 6731

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


6739 6740 6741 6742 6743
int Field_varstring::cmp_binary(const char *a_ptr, const char *b_ptr,
                                uint32 max_length)
{
  uint32 a_length,b_length;

6744 6745 6746 6747 6748 6749 6750 6751 6752 6753
  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);
  }
6754 6755 6756 6757
  set_if_smaller(a_length, max_length);
  set_if_smaller(b_length, max_length);
  if (a_length != b_length)
    return 1;
6758 6759 6760 6761
  return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
}


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


Field *Field_varstring::new_key_field(MEM_ROOT *root,
unknown's avatar
unknown committed
6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785
                                      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 */
6786
    res->length_bytes= 2;
unknown's avatar
unknown committed
6787
  }
6788
  return res;
6789 6790
}

6791

unknown's avatar
unknown committed
6792 6793 6794 6795 6796 6797
/****************************************************************************
** blob type
** A blob is saved as a length and a pointer. The length is stored in the
** packlength slot and may be from 1-4.
****************************************************************************/

unknown's avatar
unknown committed
6798
Field_blob::Field_blob(char *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
6799 6800
		       enum utype unireg_check_arg, const char *field_name_arg,
		       struct st_table *table_arg,uint blob_pack_length,
6801
		       CHARSET_INFO *cs)
6802
  :Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
unknown's avatar
unknown committed
6803
	     null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
6804
	     table_arg, cs),
unknown's avatar
unknown committed
6805
   packlength(blob_pack_length)
unknown's avatar
unknown committed
6806 6807 6808
{
  flags|= BLOB_FLAG;
  if (table)
6809
  {
6810
    table->s->blob_fields++;
6811 6812
    /* TODO: why do not fill table->s->blob_field array here? */
  }
unknown's avatar
unknown committed
6813 6814 6815
}


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


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


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


unknown's avatar
unknown committed
6918
int Field_blob::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
6919
{
6920 6921 6922 6923 6924 6925 6926
  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);

unknown's avatar
unknown committed
6927
  if (!length)
unknown's avatar
unknown committed
6928 6929
  {
    bzero(ptr,Field_blob::pack_length());
6930
    return 0;
unknown's avatar
unknown committed
6931
  }
unknown's avatar
unknown committed
6932

6933 6934 6935 6936 6937 6938 6939 6940
  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;
unknown's avatar
unknown committed
6941
    }
6942 6943 6944
    if (tmpstr.copy(from, length, cs))
      goto oom_error;
    from= tmpstr.ptr();
unknown's avatar
unknown committed
6945
  }
6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971

  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;

6972
  if (from_end_pos < from + length)
6973
  {
6974 6975
    report_data_too_long(this);
    return 2;
6976
  }
6977

6978
  return 0;
6979 6980 6981 6982 6983

oom_error:
  /* Fatal OOM error */
  bzero(ptr,Field_blob::pack_length());
  return -1; 
unknown's avatar
unknown committed
6984 6985 6986
}


unknown's avatar
unknown committed
6987
int Field_blob::store(double nr)
unknown's avatar
unknown committed
6988
{
6989 6990 6991
  CHARSET_INFO *cs=charset();
  value.set(nr, 2, cs);
  return Field_blob::store(value.ptr(),(uint) value.length(), cs);
unknown's avatar
unknown committed
6992 6993 6994
}


6995
int Field_blob::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
6996
{
6997
  CHARSET_INFO *cs=charset();
6998 6999 7000 7001
  if (unsigned_val)
    value.set((ulonglong) nr, cs);
  else
    value.set(nr, cs);
7002
  return Field_blob::store(value.ptr(), (uint) value.length(), cs);
unknown's avatar
unknown committed
7003 7004
}

7005

unknown's avatar
unknown committed
7006 7007
double Field_blob::val_real(void)
{
7008
  int not_used;
7009 7010 7011 7012
  char *end_not_used, *blob;
  uint32 length;
  CHARSET_INFO *cs;

unknown's avatar
unknown committed
7013 7014 7015
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
    return 0.0;
7016 7017
  length= get_length(ptr);
  cs= charset();
unknown's avatar
unknown committed
7018
  return my_strntod(cs, blob, length, &end_not_used, &not_used);
unknown's avatar
unknown committed
7019 7020 7021 7022 7023
}


longlong Field_blob::val_int(void)
{
7024
  int not_used;
unknown's avatar
unknown committed
7025 7026 7027 7028
  char *blob;
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
    return 0;
unknown's avatar
unknown committed
7029
  uint32 length=get_length(ptr);
7030
  return my_strntoll(charset(),blob,length,10,NULL,&not_used);
unknown's avatar
unknown committed
7031 7032 7033 7034 7035 7036 7037 7038
}

String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
			    String *val_ptr)
{
  char *blob;
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
  if (!blob)
7039
    val_ptr->set("",0,charset());	// A bit safer than ->length(0)
unknown's avatar
unknown committed
7040
  else
7041
    val_ptr->set((const char*) blob,get_length(ptr),charset());
unknown's avatar
unknown committed
7042 7043 7044 7045
  return val_ptr;
}


unknown's avatar
unknown committed
7046 7047
my_decimal *Field_blob::val_decimal(my_decimal *decimal_value)
{
7048 7049
  const char *blob;
  memcpy_fixed(&blob, ptr+packlength, sizeof(const char*));
unknown's avatar
unknown committed
7050 7051 7052 7053 7054 7055 7056 7057
  if (!blob)
    blob= "";
  str2my_decimal(E_DEC_FATAL_ERROR, blob, get_length(ptr), charset(),
                 decimal_value);
  return decimal_value;
}


unknown's avatar
unknown committed
7058 7059
int Field_blob::cmp(const char *a,uint32 a_length, const char *b,
		    uint32 b_length)
unknown's avatar
unknown committed
7060
{
7061 7062 7063 7064
  return field_charset->coll->strnncollsp(field_charset, 
                                          (const uchar*)a, a_length,
                                          (const uchar*)b, b_length,
                                          0);
unknown's avatar
unknown committed
7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078
}


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,
unknown's avatar
unknown committed
7079
			   uint32 max_length)
unknown's avatar
unknown committed
7080 7081 7082
{
  char *a,*b;
  uint diff;
unknown's avatar
unknown committed
7083
  uint32 a_length,b_length;
unknown's avatar
unknown committed
7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098
  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 */

7099
uint Field_blob::get_key_image(char *buff,uint length, imagetype type_arg)
unknown's avatar
unknown committed
7100
{
unknown's avatar
unknown committed
7101
  uint32 blob_length= get_length(ptr);
unknown's avatar
unknown committed
7102
  char *blob;
unknown's avatar
unknown committed
7103

unknown's avatar
unknown committed
7104
#ifdef HAVE_SPATIAL
7105
  if (type_arg == itMBR)
unknown's avatar
unknown committed
7106
  {
unknown's avatar
unknown committed
7107
    const char *dummy;
unknown's avatar
unknown committed
7108
    MBR mbr;
7109 7110
    Geometry_buffer buffer;
    Geometry *gobj;
unknown's avatar
unknown committed
7111
    const uint image_length= SIZEOF_STORED_DOUBLE*4;
unknown's avatar
unknown committed
7112 7113 7114

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

7133
  get_ptr(&blob);
7134 7135 7136 7137
  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);
7138

unknown's avatar
unknown committed
7139
  if ((uint32) length > blob_length)
unknown's avatar
unknown committed
7140
  {
7141 7142 7143 7144
    /*
      Must clear this as we do a memcmp in opt_range.cc to detect
      identical keys
    */
7145
    bzero(buff+HA_KEY_BLOB_LENGTH+blob_length, (length-blob_length));
unknown's avatar
unknown committed
7146
    length=(uint) blob_length;
unknown's avatar
unknown committed
7147
  }
unknown's avatar
unknown committed
7148
  int2store(buff,length);
7149
  memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
unknown's avatar
unknown committed
7150
  return HA_KEY_BLOB_LENGTH+length;
unknown's avatar
unknown committed
7151 7152
}

7153 7154

void Field_blob::set_key_image(char *buff,uint length)
unknown's avatar
unknown committed
7155
{
7156
  length= uint2korr(buff);
7157
  (void) Field_blob::store(buff+HA_KEY_BLOB_LENGTH, length, field_charset);
unknown's avatar
unknown committed
7158 7159
}

unknown's avatar
unknown committed
7160

unknown's avatar
unknown committed
7161 7162 7163 7164 7165
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*));
7166
  CHARSET_INFO *cs= charset();
7167 7168 7169 7170
  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);
7171
  return Field_blob::cmp(blob1, blob_length,
7172 7173
			 (char*) key_ptr+HA_KEY_BLOB_LENGTH,
			 uint2korr(key_ptr));
unknown's avatar
unknown committed
7174 7175 7176 7177
}

int Field_blob::key_cmp(const byte *a,const byte *b)
{
7178 7179
  return Field_blob::cmp((char*) a+HA_KEY_BLOB_LENGTH, uint2korr(a),
			 (char*) b+HA_KEY_BLOB_LENGTH, uint2korr(b));
unknown's avatar
unknown committed
7180 7181 7182
}


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


unknown's avatar
unknown committed
7190 7191 7192 7193
void Field_blob::sort_string(char *to,uint length)
{
  char *blob;
  uint blob_length=get_length();
unknown's avatar
unknown committed
7194

unknown's avatar
unknown committed
7195 7196 7197 7198
  if (!blob_length)
    bzero(to,length);
  else
  {
7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223
    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;
      }
    }
unknown's avatar
unknown committed
7224
    memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
unknown's avatar
unknown committed
7225 7226
    
    blob_length=my_strnxfrm(field_charset,
7227 7228
                            (uchar*) to, length, 
                            (uchar*) blob, blob_length);
7229
    DBUG_ASSERT(blob_length == length);
unknown's avatar
unknown committed
7230 7231 7232 7233 7234 7235 7236
  }
}


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


7254 7255 7256 7257
char *Field_blob::pack(char *to, const char *from, uint max_length)
{
  char *save=ptr;
  ptr=(char*) from;
unknown's avatar
unknown committed
7258
  uint32 length=get_length();			// Length of from string
7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280
  if (length > max_length)
  {
    ptr=to;
    length=max_length;
    store_length(length);			// Store max length
    ptr=(char*) from;
  }
  else
    memcpy(to,from,packlength);			// Copy length
  if (length)
  {
    get_ptr((char**) &from);
    memcpy(to+packlength, from,length);
  }
  ptr=save;					// Restore org row pointer
  return to+packlength+length;
}


const char *Field_blob::unpack(char *to, const char *from)
{
  memcpy(to,from,packlength);
unknown's avatar
unknown committed
7281
  uint32 length=get_length(from);
7282 7283 7284 7285 7286 7287 7288 7289
  from+=packlength;
  if (length)
    memcpy_fixed(to+packlength, &from, sizeof(from));
  else
    bzero(to+packlength,sizeof(from));
  return from+length;
}

unknown's avatar
unknown committed
7290 7291
/* Keys for blobs are like keys on varchars */

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

7312

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

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

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

unknown's avatar
unknown committed
7337 7338
char *Field_blob::pack_key(char *to, const char *from, uint max_length)
{
7339 7340
  char *save=ptr;
  ptr=(char*) from;
unknown's avatar
unknown committed
7341
  uint32 length=get_length();			// Length of from string
7342
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
7343
                     max_length/field_charset->mbmaxlen : max_length);
7344 7345
  if (length)
    get_ptr((char**) &from);
7346 7347 7348 7349
  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);
7350 7351 7352
  *to++= (uchar) length;
  if (max_length > 255)				// 2 byte length
    *to++= (uchar) (length >> 8);
7353
  memcpy(to, from, length);
7354 7355 7356 7357
  ptr=save;					// Restore org row pointer
  return to+length;
}

7358 7359 7360 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

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

7402

7403 7404 7405 7406 7407 7408
/* Create a packed key that will be used for storage from a MySQL key */

char *Field_blob::pack_key_from_key_image(char *to, const char *from,
					  uint max_length)
{
  uint length=uint2korr(from);
unknown's avatar
unknown committed
7409 7410
  if (length > max_length)
    length=max_length;
unknown's avatar
unknown committed
7411
  *to++= (char) (length & 255);
unknown's avatar
unknown committed
7412
  if (max_length > 255)
unknown's avatar
unknown committed
7413
    *to++= (char) (length >> 8);
unknown's avatar
unknown committed
7414
  if (length)
7415
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
unknown's avatar
unknown committed
7416 7417 7418
  return to+length;
}

7419

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

7427

7428 7429 7430 7431
uint Field_blob::max_packed_col_length(uint max_length)
{
  return (max_length > 255 ? 2 : 1)+max_length;
}
7432

unknown's avatar
unknown committed
7433

unknown's avatar
unknown committed
7434
#ifdef HAVE_SPATIAL
unknown's avatar
unknown committed
7435

7436
uint Field_geom::get_key_image(char *buff, uint length, imagetype type)
unknown's avatar
unknown committed
7437 7438
{
  char *blob;
unknown's avatar
unknown committed
7439
  const char *dummy;
unknown's avatar
unknown committed
7440
  MBR mbr;
7441 7442 7443
  ulong blob_length= get_length(ptr);
  Geometry_buffer buffer;
  Geometry *gobj;
unknown's avatar
unknown committed
7444
  const uint image_length= SIZEOF_STORED_DOUBLE*4;
unknown's avatar
unknown committed
7445 7446 7447

  if (blob_length < SRID_SIZE)
  {
unknown's avatar
unknown committed
7448 7449
    bzero(buff, image_length);
    return image_length;
unknown's avatar
unknown committed
7450 7451
  }
  get_ptr(&blob);
7452
  gobj= Geometry::construct(&buffer, blob, blob_length);
7453
  if (!gobj || gobj->get_mbr(&mbr, &dummy))
unknown's avatar
unknown committed
7454
    bzero(buff, image_length);
unknown's avatar
unknown committed
7455 7456 7457 7458 7459 7460 7461
  else
  {
    float8store(buff, mbr.xmin);
    float8store(buff + 8, mbr.xmax);
    float8store(buff + 16, mbr.ymin);
    float8store(buff + 24, mbr.ymax);
  }
unknown's avatar
unknown committed
7462
  return image_length;
unknown's avatar
unknown committed
7463 7464 7465 7466 7467
}


void Field_geom::sql_type(String &res) const
{
unknown's avatar
unknown committed
7468 7469 7470 7471
  CHARSET_INFO *cs= &my_charset_latin1;
  switch (geom_type)
  {
    case GEOM_POINT:
7472
     res.set(STRING_WITH_LEN("point"), cs);
unknown's avatar
unknown committed
7473 7474
     break;
    case GEOM_LINESTRING:
7475
     res.set(STRING_WITH_LEN("linestring"), cs);
unknown's avatar
unknown committed
7476 7477
     break;
    case GEOM_POLYGON:
7478
     res.set(STRING_WITH_LEN("polygon"), cs);
unknown's avatar
unknown committed
7479 7480
     break;
    case GEOM_MULTIPOINT:
7481
     res.set(STRING_WITH_LEN("multipoint"), cs);
unknown's avatar
unknown committed
7482 7483
     break;
    case GEOM_MULTILINESTRING:
7484
     res.set(STRING_WITH_LEN("multilinestring"), cs);
unknown's avatar
unknown committed
7485 7486
     break;
    case GEOM_MULTIPOLYGON:
7487
     res.set(STRING_WITH_LEN("multipolygon"), cs);
unknown's avatar
unknown committed
7488 7489
     break;
    case GEOM_GEOMETRYCOLLECTION:
7490
     res.set(STRING_WITH_LEN("geometrycollection"), cs);
unknown's avatar
unknown committed
7491 7492
     break;
    default:
7493
     res.set(STRING_WITH_LEN("geometry"), cs);
unknown's avatar
unknown committed
7494
  }
unknown's avatar
unknown committed
7495 7496 7497
}


7498 7499 7500 7501 7502 7503 7504 7505
int Field_geom::store(double nr)
{
  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
             ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
  return -1;
}


7506
int Field_geom::store(longlong nr, bool unsigned_val)
7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521
{
  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;
}


unknown's avatar
unknown committed
7522 7523 7524 7525 7526 7527
int Field_geom::store(const char *from, uint length, CHARSET_INFO *cs)
{
  if (!length)
    bzero(ptr, Field_blob::pack_length());
  else
  {
7528 7529
    if (from == Geometry::bad_geometry_data.ptr())
      goto err;
unknown's avatar
unknown committed
7530 7531 7532 7533
    // Check given WKB
    uint32 wkb_type;
    if (length < SRID_SIZE + WKB_HEADER_SIZE + SIZEOF_STORED_DOUBLE*2)
      goto err;
7534
    wkb_type= uint4korr(from + SRID_SIZE + 1);
7535
    if (wkb_type < (uint32) Geometry::wkb_point ||
7536
	wkb_type > (uint32) Geometry::wkb_last)
7537
      goto err;
unknown's avatar
unknown committed
7538 7539 7540 7541 7542 7543 7544 7545 7546
    Field_blob::store_length(length);
    if (table->copy_blobs || length <= MAX_FIELD_WIDTH)
    {						// Must make a copy
      value.copy(from, length, cs);
      from= value.ptr();
    }
    bmove(ptr + packlength, (char*) &from, sizeof(char*));
  }
  return 0;
unknown's avatar
unknown committed
7547 7548 7549

err:
  bzero(ptr, Field_blob::pack_length());  
7550 7551
  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
             ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
unknown's avatar
unknown committed
7552
  return -1;
unknown's avatar
unknown committed
7553 7554
}

unknown's avatar
unknown committed
7555
#endif /*HAVE_SPATIAL*/
unknown's avatar
unknown committed
7556

unknown's avatar
unknown committed
7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579
/****************************************************************************
** 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
unknown's avatar
unknown committed
7580
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
7581 7582 7583 7584 7585 7586 7587 7588 7589 7590
  {
    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
unknown's avatar
unknown committed
7591
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
7592 7593 7594 7595 7596 7597 7598 7599 7600
  {
    int4store(ptr,value);
  }
  else
#endif
    longstore(ptr,(long) value);
  break;
  case 8:
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
7601
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616
  {
    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)
*/

unknown's avatar
unknown committed
7617
int Field_enum::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
7618
{
7619
  int err= 0;
unknown's avatar
unknown committed
7620
  uint32 not_used;
unknown's avatar
unknown committed
7621
  char buff[STRING_BUFFER_USUAL_SIZE];
unknown's avatar
unknown committed
7622
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
unknown's avatar
unknown committed
7623

unknown's avatar
unknown committed
7624
  /* Convert character set if necessary */
unknown's avatar
unknown committed
7625
  if (String::needs_conversion(length, cs, field_charset, &not_used))
unknown's avatar
unknown committed
7626
  { 
7627 7628
    uint dummy_errors;
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
unknown's avatar
unknown committed
7629 7630 7631
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
7632 7633

  /* Remove end space */
7634
  length= field_charset->cset->lengthsp(field_charset, from, length);
7635
  uint tmp=find_type2(typelib, from, length, field_charset);
unknown's avatar
unknown committed
7636
  if (!tmp)
unknown's avatar
unknown committed
7637
  {
7638
    if (length < 6) // Can't be more than 99999 enums
unknown's avatar
unknown committed
7639
    {
unknown's avatar
unknown committed
7640
      /* This is for reading numbers with LOAD DATA INFILE */
7641
      char *end;
7642 7643
      tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
      if (err || end != from+length || tmp > typelib->count)
unknown's avatar
unknown committed
7644 7645
      {
	tmp=0;
7646
	set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
7647
      }
7648 7649
      if (!table->in_use->count_cuted_fields)
        err= 0;
unknown's avatar
unknown committed
7650 7651
    }
    else
7652
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
7653
  }
unknown's avatar
unknown committed
7654
  store_type((ulonglong) tmp);
7655
  return err;
unknown's avatar
unknown committed
7656 7657 7658
}


unknown's avatar
unknown committed
7659
int Field_enum::store(double nr)
unknown's avatar
unknown committed
7660
{
7661
  return Field_enum::store((longlong) nr, FALSE);
unknown's avatar
unknown committed
7662 7663 7664
}


7665
int Field_enum::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
7666
{
unknown's avatar
unknown committed
7667
  int error= 0;
7668
  if ((ulonglong) nr > typelib->count || nr == 0)
unknown's avatar
unknown committed
7669
  {
7670
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
7671 7672 7673 7674 7675
    if (nr != 0 || table->in_use->count_cuted_fields)
    {
      nr= 0;
      error= 1;
    }
unknown's avatar
unknown committed
7676 7677
  }
  store_type((ulonglong) (uint) nr);
7678
  return error;
unknown's avatar
unknown committed
7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696
}


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
unknown's avatar
unknown committed
7697
      if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709
	tmp=sint2korr(ptr);
      else
#endif
	shortget(tmp,ptr);
      return (longlong) tmp;
    }
  case 3:
    return (longlong) uint3korr(ptr);
  case 4:
    {
      uint32 tmp;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
7710
      if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
7711 7712 7713 7714 7715 7716 7717 7718 7719 7720
	tmp=uint4korr(ptr);
      else
#endif
	longget(tmp,ptr);
      return (longlong) tmp;
    }
  case 8:
    {
      longlong tmp;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
7721
      if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737
	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)
unknown's avatar
unknown committed
7738
    val_ptr->set("", 0, field_charset);
unknown's avatar
unknown committed
7739 7740
  else
    val_ptr->set((const char*) typelib->type_names[tmp-1],
7741
		 typelib->type_lengths[tmp-1],
7742
		 field_charset);
unknown's avatar
unknown committed
7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770
  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
{
7771 7772 7773
  char buffer[255];
  String enum_item(buffer, sizeof(buffer), res.charset());

unknown's avatar
unknown committed
7774
  res.length(0);
7775
  res.append(STRING_WITH_LEN("enum("));
unknown's avatar
unknown committed
7776 7777

  bool flag=0;
7778 7779
  uint *len= typelib->type_lengths;
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
unknown's avatar
unknown committed
7780
  {
7781
    uint dummy_errors;
unknown's avatar
unknown committed
7782 7783
    if (flag)
      res.append(',');
7784
    /* convert to res.charset() == utf8, then quote */
7785
    enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
7786 7787
    append_unescaped(&res, enum_item.ptr(), enum_item.length());
    flag= 1;
unknown's avatar
unknown committed
7788 7789 7790 7791 7792
  }
  res.append(')');
}


7793 7794 7795 7796 7797 7798 7799 7800 7801 7802
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;
}


7803 7804 7805 7806 7807 7808 7809 7810
/*
   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.
*/
unknown's avatar
unknown committed
7811 7812


unknown's avatar
unknown committed
7813
int Field_set::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
7814
{
unknown's avatar
unknown committed
7815
  bool got_warning= 0;
7816
  int err= 0;
7817 7818
  char *not_used;
  uint not_used2;
unknown's avatar
unknown committed
7819
  uint32 not_used_offset;
unknown's avatar
unknown committed
7820
  char buff[STRING_BUFFER_USUAL_SIZE];
unknown's avatar
unknown committed
7821
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
unknown's avatar
unknown committed
7822

unknown's avatar
unknown committed
7823
  /* Convert character set if necessary */
unknown's avatar
unknown committed
7824
  if (String::needs_conversion(length, cs, field_charset, &not_used_offset))
unknown's avatar
unknown committed
7825
  { 
7826 7827
    uint dummy_errors;
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
unknown's avatar
unknown committed
7828 7829 7830
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
7831 7832
  ulonglong tmp= find_set(typelib, from, length, field_charset,
                          &not_used, &not_used2, &got_warning);
unknown's avatar
unknown committed
7833 7834 7835
  if (!tmp && length && length < 22)
  {
    /* This is for reading numbers with LOAD DATA INFILE */
7836
    char *end;
7837 7838
    tmp=my_strntoull(cs,from,length,10,&end,&err);
    if (err || end != from+length ||
unknown's avatar
unknown committed
7839
	tmp > (ulonglong) (((longlong) 1 << typelib->count) - (longlong) 1))
7840
    {
7841
      tmp=0;      
7842
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
7843
    }
unknown's avatar
unknown committed
7844
  }
unknown's avatar
unknown committed
7845
  else if (got_warning)
7846
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
7847
  store_type(tmp);
7848
  return err;
unknown's avatar
unknown committed
7849 7850 7851
}


7852
int Field_set::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
7853
{
unknown's avatar
unknown committed
7854
  int error= 0;
unknown's avatar
unknown committed
7855 7856 7857
  if ((ulonglong) nr > (ulonglong) (((longlong) 1 << typelib->count) -
				    (longlong) 1))
  {
7858
    nr&= (longlong) (((longlong) 1 << typelib->count) - (longlong) 1);    
7859
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
7860
    error=1;
unknown's avatar
unknown committed
7861 7862
  }
  store_type((ulonglong) nr);
7863
  return error;
unknown's avatar
unknown committed
7864 7865 7866 7867 7868 7869 7870 7871 7872 7873
}


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);
7874
  val_buffer->set_charset(field_charset);
unknown's avatar
unknown committed
7875 7876 7877 7878 7879
  while (tmp && bitnr < (uint) typelib->count)
  {
    if (tmp & 1)
    {
      if (val_buffer->length())
7880
	val_buffer->append(&field_separator, 1, &my_charset_latin1);
unknown's avatar
unknown committed
7881
      String str(typelib->type_names[bitnr],
7882
		 typelib->type_lengths[bitnr],
7883
		 field_charset);
unknown's avatar
unknown committed
7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894
      val_buffer->append(str);
    }
    tmp>>=1;
    bitnr++;
  }
  return val_buffer;
}


void Field_set::sql_type(String &res) const
{
7895 7896 7897
  char buffer[255];
  String set_item(buffer, sizeof(buffer), res.charset());

unknown's avatar
unknown committed
7898
  res.length(0);
7899
  res.append(STRING_WITH_LEN("set("));
unknown's avatar
unknown committed
7900 7901

  bool flag=0;
7902 7903
  uint *len= typelib->type_lengths;
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
unknown's avatar
unknown committed
7904
  {
7905
    uint dummy_errors;
unknown's avatar
unknown committed
7906 7907
    if (flag)
      res.append(',');
7908
    /* convert to res.charset() == utf8, then quote */
7909
    set_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
7910 7911
    append_unescaped(&res, set_item.ptr(), set_item.length());
    flag= 1;
unknown's avatar
unknown committed
7912 7913 7914 7915 7916 7917 7918 7919
  }
  res.append(')');
}

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

bool Field::eq_def(Field *field)
{
7920
  if (real_type() != field->real_type() || charset() != field->charset() ||
unknown's avatar
unknown committed
7921 7922 7923 7924 7925 7926 7927 7928 7929 7930
      pack_length() != field->pack_length())
    return 0;
  return 1;
}

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

unknown's avatar
unknown committed
7932 7933 7934
  if (typelib->count < from_lib->count)
    return 0;
  for (uint i=0 ; i < from_lib->count ; i++)
7935 7936
    if (my_strnncoll(field_charset,
                     (const uchar*)typelib->type_names[i],
7937
                     (uint) strlen(typelib->type_names[i]),
7938
                     (const uchar*)from_lib->type_names[i],
7939
                     (uint) strlen(from_lib->type_names[i])))
unknown's avatar
unknown committed
7940 7941 7942 7943 7944
      return 0;
  return 1;
}

bool Field_num::eq_def(Field *field)
unknown's avatar
unknown committed
7945
{
unknown's avatar
unknown committed
7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957
  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;
}


unknown's avatar
unknown committed
7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986
/*
  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'
*/

unknown's avatar
unknown committed
7987 7988 7989 7990
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)
unknown's avatar
unknown committed
7991
  : Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
7992
          unireg_check_arg, field_name_arg, table_arg),
unknown's avatar
unknown committed
7993 7994
    bit_ptr(bit_ptr_arg), bit_ofs(bit_ofs_arg), bit_len(len_arg & 7),
    bytes_in_rec(len_arg / 8)
unknown's avatar
unknown committed
7995
{
unknown's avatar
unknown committed
7996
  flags|= UNSIGNED_FLAG;
unknown's avatar
unknown committed
7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025
  /*
    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;
}


unknown's avatar
unknown committed
8026 8027 8028 8029
int Field_bit::store(const char *from, uint length, CHARSET_INFO *cs)
{
  int delta;

8030
  for (; length && !*from; from++, length--);          // skip left 0's
unknown's avatar
unknown committed
8031
  delta= bytes_in_rec - length;
unknown's avatar
unknown committed
8032 8033 8034 8035 8036

  if (delta < -1 ||
      (delta == -1 && (uchar) *from > ((1 << bit_len) - 1)) ||
      (!bit_len && delta < 0))
  {
8037
    set_rec_bits((1 << bit_len) - 1, bit_ptr, bit_ofs, bit_len);
unknown's avatar
unknown committed
8038
    memset(ptr, 0xff, bytes_in_rec);
8039 8040 8041 8042
    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);
unknown's avatar
unknown committed
8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065
    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++;
    }
unknown's avatar
unknown committed
8066
    memcpy(ptr, from, bytes_in_rec);
unknown's avatar
unknown committed
8067 8068 8069 8070 8071 8072 8073
  }
  return 0;
}


int Field_bit::store(double nr)
{
8074
  return store((longlong) nr, FALSE);
unknown's avatar
unknown committed
8075 8076 8077
}


8078
int Field_bit::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
8079 8080 8081 8082 8083 8084 8085 8086
{
  char buf[8];

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


unknown's avatar
unknown committed
8087 8088 8089 8090
int Field_bit::store_decimal(const my_decimal *val)
{
  int err= 0;
  longlong i= convert_decimal2longlong(val, 1, &err);
8091
  return test(err | store(i, TRUE));
unknown's avatar
unknown committed
8092 8093 8094
}


unknown's avatar
unknown committed
8095 8096 8097 8098 8099 8100 8101 8102 8103 8104
double Field_bit::val_real(void)
{
  return (double) Field_bit::val_int();
}


longlong Field_bit::val_int(void)
{
  ulonglong bits= 0;
  if (bit_len)
8105
  {
unknown's avatar
unknown committed
8106
    bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
unknown's avatar
unknown committed
8107
    bits<<= (bytes_in_rec * 8);
8108
  }
unknown's avatar
unknown committed
8109

unknown's avatar
unknown committed
8110
  switch (bytes_in_rec) {
unknown's avatar
unknown committed
8111 8112 8113 8114 8115 8116 8117 8118
  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);
unknown's avatar
unknown committed
8119
  default: return mi_uint8korr(ptr + bytes_in_rec - sizeof(longlong));
unknown's avatar
unknown committed
8120 8121 8122 8123 8124 8125 8126
  }
}  


String *Field_bit::val_str(String *val_buffer,
                           String *val_ptr __attribute__((unused)))
{
unknown's avatar
unknown committed
8127
  char buff[sizeof(longlong)];
unknown's avatar
unknown committed
8128 8129
  uint length= min(pack_length(), sizeof(longlong));
  ulonglong bits= val_int();
unknown's avatar
unknown committed
8130
  mi_int8store(buff,bits);
unknown's avatar
unknown committed
8131 8132

  val_buffer->alloc(length);
unknown's avatar
unknown committed
8133
  memcpy_fixed((char*) val_buffer->ptr(), buff+8-length, length);
unknown's avatar
unknown committed
8134 8135 8136 8137 8138 8139
  val_buffer->length(length);
  val_buffer->set_charset(&my_charset_bin);
  return val_buffer;
}


unknown's avatar
unknown committed
8140 8141 8142 8143 8144 8145 8146
my_decimal *Field_bit::val_decimal(my_decimal *deciaml_value)
{
  int2my_decimal(E_DEC_FATAL_ERROR, val_int(), 1, deciaml_value);
  return deciaml_value;
}


unknown's avatar
unknown committed
8147 8148 8149 8150 8151 8152
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);
unknown's avatar
unknown committed
8153
    if ((flag= (int) (bits - *(uchar*) str)))
unknown's avatar
unknown committed
8154 8155 8156 8157
      return flag;
    str++;
    length--;
  }
unknown's avatar
unknown committed
8158
  return memcmp(ptr, str, length);
unknown's avatar
unknown committed
8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171
}


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;
  }
unknown's avatar
unknown committed
8172
  return memcmp(ptr, ptr + row_offset, bytes_in_rec);
unknown's avatar
unknown committed
8173 8174 8175
}


8176
uint Field_bit::get_key_image(char *buff, uint length, imagetype type_arg)
unknown's avatar
unknown committed
8177 8178 8179 8180 8181 8182 8183
{
  if (bit_len)
  {
    uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
    *buff++= bits;
    length--;
  }
8184 8185 8186
  uint data_length = min(length, bytes_in_rec);
  memcpy(buff, ptr, data_length);
  return data_length + 1;
unknown's avatar
unknown committed
8187 8188 8189 8190 8191 8192 8193
}


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(),
unknown's avatar
unknown committed
8194
                                   "bit(%d)", (int) field_length);
unknown's avatar
unknown committed
8195 8196 8197 8198 8199 8200
  res.length((uint) length);
}


char *Field_bit::pack(char *to, const char *from, uint max_length)
{
unknown's avatar
unknown committed
8201 8202
  DBUG_ASSERT(max_length);
  uint length;
unknown's avatar
unknown committed
8203 8204 8205 8206 8207
  if (bit_len)
  {
    uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
    *to++= bits;
  }
unknown's avatar
unknown committed
8208 8209
  length= min(bytes_in_rec, max_length - (bit_len > 0));
  memcpy(to, from, length);
unknown's avatar
unknown committed
8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220
  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++;
  }
unknown's avatar
unknown committed
8221 8222
  memcpy(to, from, bytes_in_rec);
  return from + bytes_in_rec;
unknown's avatar
unknown committed
8223 8224 8225
}


8226 8227 8228 8229 8230
/*
  Bit field support for non-MyISAM tables.
*/

Field_bit_as_char::Field_bit_as_char(char *ptr_arg, uint32 len_arg,
8231 8232
                                     uchar *null_ptr_arg, uchar null_bit_arg,
                                     enum utype unireg_check_arg,
8233 8234
                                     const char *field_name_arg,
                                     struct st_table *table_arg)
8235
  : Field_bit(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, 0,
unknown's avatar
unknown committed
8236
              0, unireg_check_arg, field_name_arg, table_arg)
8237
{
unknown's avatar
unknown committed
8238
  flags|= UNSIGNED_FLAG;
8239
  bit_len= 0;
unknown's avatar
unknown committed
8240
  bytes_in_rec= (len_arg + 7) / 8;
8241 8242 8243 8244 8245 8246
}


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

8249
  for (; length && !*from; from++, length--);          // skip left 0's
unknown's avatar
unknown committed
8250
  delta= bytes_in_rec - length;
8251 8252 8253 8254

  if (delta < 0 ||
      (delta == 0 && bits && (uint) (uchar) *from >= (uint) (1 << bits)))
  {
unknown's avatar
unknown committed
8255
    memset(ptr, 0xff, bytes_in_rec);
8256 8257
    if (bits)
      *ptr&= ((1 << bits) - 1); /* set first byte */
8258 8259 8260 8261
    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);
8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273
    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(),
unknown's avatar
unknown committed
8274
                                   "bit(%d)", (int) field_length);
8275 8276 8277 8278
  res.length((uint) length);
}


unknown's avatar
unknown committed
8279
/*****************************************************************************
8280
  Handling of field and create_field
unknown's avatar
unknown committed
8281 8282
*****************************************************************************/

8283 8284 8285 8286 8287 8288 8289
/*
  Convert create_field::length from number of characters to number of bytes

  SYNOPSIS
    create_field::create_length_to_internal_length()
  
  DESCRIPTION
8290
    Convert create_field::length from number of characters to number of bytes.
8291 8292
*/

8293 8294
void create_field::create_length_to_internal_length(void)
{
unknown's avatar
unknown committed
8295 8296 8297 8298 8299 8300 8301
  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:
8302
  case MYSQL_TYPE_VARCHAR:
unknown's avatar
unknown committed
8303
    length*= charset->mbmaxlen;
8304
    key_length= length;
8305
    pack_length= calc_pack_length(sql_type, length);
unknown's avatar
unknown committed
8306 8307 8308
    break;
  case MYSQL_TYPE_ENUM:
  case MYSQL_TYPE_SET:
8309
    /* Pack_length already calculated in sql_parse.cc */
unknown's avatar
unknown committed
8310
    length*= charset->mbmaxlen;
8311
    key_length= pack_length;
unknown's avatar
unknown committed
8312
    break;
unknown's avatar
unknown committed
8313
  case MYSQL_TYPE_BIT:
8314 8315 8316 8317 8318 8319 8320 8321 8322 8323
    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);
    }
unknown's avatar
unknown committed
8324
    break;
unknown's avatar
unknown committed
8325
  case MYSQL_TYPE_NEWDECIMAL:
unknown's avatar
unknown committed
8326 8327 8328 8329 8330 8331
    key_length= pack_length=
      my_decimal_get_binary_size(my_decimal_length_to_precision(length,
								decimals,
								flags &
								UNSIGNED_FLAG),
				 decimals);
unknown's avatar
unknown committed
8332
    break;
unknown's avatar
unknown committed
8333
  default:
8334
    key_length= pack_length= calc_pack_length(sql_type, length);
unknown's avatar
unknown committed
8335
    break;
8336 8337 8338
  }
}

8339

8340
void create_field::init_for_tmp_table(enum_field_types sql_type_arg,
8341
                                      uint32 length_arg, uint32 decimals_arg,
8342 8343 8344 8345
                                      bool maybe_null, bool is_unsigned)
{
  field_name= "";
  sql_type= sql_type_arg;
unknown's avatar
unknown committed
8346
  char_length= length= length_arg;;
8347 8348 8349 8350 8351
  unireg_check= Field::NONE;
  interval= 0;
  charset= &my_charset_bin;
  geom_type= Field::GEOM_GEOMETRY;
  pack_flag= (FIELDFLAG_NUMBER |
8352
              ((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
8353 8354 8355 8356 8357
              (maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
              (is_unsigned ? 0 : FIELDFLAG_DECIMAL));
}


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 8391 8392 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
/*
  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;
  /*
8418 8419
    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.
8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 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
  */
  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:
8458
    my_decimal_trim(&length, &decimals);
8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494
    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);
8495 8496 8497 8498 8499 8500 8501
      /*
        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)))
8502 8503 8504 8505 8506
      {
        my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
                 fld_name); /* purecov: inspected */
        DBUG_RETURN(TRUE);
      }
8507 8508 8509 8510 8511 8512 8513 8514 8515 8516
      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);
      }
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 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574
      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)
8575 8576 8577 8578 8579
    {
      /* Compressed date YYYYMMDDHHMMSS */
      length= MAX_DATETIME_COMPRESSED_WIDTH;
    }
    else if (length != MAX_DATETIME_WIDTH)
8580 8581 8582 8583 8584 8585
    {
      /*
        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 */
8586
      length= min(length, MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
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 8612 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
    }
    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:
8639
    length= MAX_DATETIME_WIDTH;
8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 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
    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 */
  }
unknown's avatar
unknown committed
8688 8689
  /* Remember the value of length */
  char_length= length;
8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717

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


8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732
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;
}


unknown's avatar
unknown committed
8733
/*
unknown's avatar
unknown committed
8734
  Make a field from the .frm file info
unknown's avatar
unknown committed
8735 8736 8737 8738 8739
*/

uint32 calc_pack_length(enum_field_types type,uint32 length)
{
  switch (type) {
8740
  case MYSQL_TYPE_VAR_STRING:
8741
  case MYSQL_TYPE_STRING:
8742
  case FIELD_TYPE_DECIMAL:     return (length);
8743
  case MYSQL_TYPE_VARCHAR:     return (length + (length < 256 ? 1: 2));
unknown's avatar
unknown committed
8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758
  case FIELD_TYPE_YEAR:
  case FIELD_TYPE_TINY	: return 1;
  case FIELD_TYPE_SHORT : return 2;
  case FIELD_TYPE_INT24:
  case FIELD_TYPE_NEWDATE:
  case FIELD_TYPE_TIME:   return 3;
  case FIELD_TYPE_TIMESTAMP:
  case FIELD_TYPE_DATE:
  case FIELD_TYPE_LONG	: return 4;
  case FIELD_TYPE_FLOAT : return sizeof(float);
  case FIELD_TYPE_DOUBLE: return sizeof(double);
  case FIELD_TYPE_DATETIME:
  case FIELD_TYPE_LONGLONG: return 8;	/* Don't crash if no longlong */
  case FIELD_TYPE_NULL	: return 0;
  case FIELD_TYPE_TINY_BLOB:	return 1+portable_sizeof_char_ptr;
unknown's avatar
unknown committed
8759
  case FIELD_TYPE_BLOB:		return 2+portable_sizeof_char_ptr;
unknown's avatar
unknown committed
8760 8761
  case FIELD_TYPE_MEDIUM_BLOB:	return 3+portable_sizeof_char_ptr;
  case FIELD_TYPE_LONG_BLOB:	return 4+portable_sizeof_char_ptr;
unknown's avatar
unknown committed
8762
  case FIELD_TYPE_GEOMETRY:	return 4+portable_sizeof_char_ptr;
unknown's avatar
unknown committed
8763
  case FIELD_TYPE_SET:
unknown's avatar
unknown committed
8764
  case FIELD_TYPE_ENUM:
8765 8766
  case FIELD_TYPE_NEWDECIMAL:
    abort(); return 0;                          // This shouldn't happen
unknown's avatar
unknown committed
8767
  case FIELD_TYPE_BIT: return length / 8;
8768 8769
  default:
    return 0;
unknown's avatar
unknown committed
8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787
  }
}


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


Field *make_field(char *ptr, uint32 field_length,
unknown's avatar
unknown committed
8788
		  uchar *null_pos, uchar null_bit,
unknown's avatar
unknown committed
8789
		  uint pack_flag,
8790
		  enum_field_types field_type,
8791
		  CHARSET_INFO *field_charset,
unknown's avatar
unknown committed
8792
		  Field::geometry_type geom_type,
unknown's avatar
unknown committed
8793 8794 8795 8796 8797
		  Field::utype unireg_check,
		  TYPELIB *interval,
		  const char *field_name,
		  struct st_table *table)
{
unknown's avatar
unknown committed
8798 8799 8800 8801
  uchar *bit_ptr;
  uchar bit_offset;
  LINT_INIT(bit_ptr);
  LINT_INIT(bit_offset);
8802
  if (field_type == FIELD_TYPE_BIT && !f_bit_as_char(pack_flag))
unknown's avatar
unknown committed
8803 8804 8805 8806 8807 8808 8809 8810 8811 8812
  {
    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;
    }
  }

unknown's avatar
unknown committed
8813 8814 8815 8816 8817
  if (!f_maybe_null(pack_flag))
  {
    null_pos=0;
    null_bit=0;
  }
unknown's avatar
unknown committed
8818 8819 8820 8821
  else
  {
    null_bit= ((uchar) 1) << null_bit;
  }
unknown's avatar
unknown committed
8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833

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

unknown's avatar
unknown committed
8834 8835 8836
  if (f_is_alpha(pack_flag))
  {
    if (!f_is_packed(pack_flag))
unknown's avatar
unknown committed
8837
    {
8838
      if (field_type == MYSQL_TYPE_STRING ||
8839
          field_type == FIELD_TYPE_DECIMAL ||   // 3.23 or 4.0 string
8840
          field_type == MYSQL_TYPE_VAR_STRING)
unknown's avatar
unknown committed
8841 8842 8843
        return new Field_string(ptr,field_length,null_pos,null_bit,
                                unireg_check, field_name, table,
                                field_charset);
8844
      if (field_type == MYSQL_TYPE_VARCHAR)
8845 8846 8847
        return new Field_varstring(ptr,field_length,
                                   HA_VARCHAR_PACKLENGTH(field_length),
                                   null_pos,null_bit,
8848 8849
                                   unireg_check, field_name, table,
                                   field_charset);
unknown's avatar
unknown committed
8850 8851
      return 0;                                 // Error
    }
unknown's avatar
unknown committed
8852 8853 8854 8855 8856

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

unknown's avatar
unknown committed
8857
#ifdef HAVE_SPATIAL
unknown's avatar
unknown committed
8858 8859
    if (f_is_geom(pack_flag))
      return new Field_geom(ptr,null_pos,null_bit,
unknown's avatar
unknown committed
8860
			    unireg_check, field_name, table,
unknown's avatar
unknown committed
8861
			    pack_length, geom_type);
unknown's avatar
unknown committed
8862
#endif
unknown's avatar
unknown committed
8863 8864 8865
    if (f_is_blob(pack_flag))
      return new Field_blob(ptr,null_pos,null_bit,
			    unireg_check, field_name, table,
unknown's avatar
unknown committed
8866
			    pack_length, field_charset);
unknown's avatar
unknown committed
8867 8868 8869 8870 8871
    if (interval)
    {
      if (f_is_enum(pack_flag))
	return new Field_enum(ptr,field_length,null_pos,null_bit,
				  unireg_check, field_name, table,
8872
				  pack_length, interval, field_charset);
unknown's avatar
unknown committed
8873 8874 8875
      else
	return new Field_set(ptr,field_length,null_pos,null_bit,
			     unireg_check, field_name, table,
8876
			     pack_length, interval, field_charset);
unknown's avatar
unknown committed
8877 8878 8879
    }
  }

8880
  switch (field_type) {
unknown's avatar
unknown committed
8881 8882 8883 8884 8885 8886
  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);
unknown's avatar
unknown committed
8887 8888 8889 8890 8891 8892
  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);
unknown's avatar
unknown committed
8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930
  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:
8931 8932 8933
    return new Field_timestamp(ptr,field_length, null_pos, null_bit,
                               unireg_check, field_name, table,
                               field_charset);
unknown's avatar
unknown committed
8934 8935 8936 8937 8938
  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,
8939
			  unireg_check, field_name, table, field_charset);
unknown's avatar
unknown committed
8940 8941
  case FIELD_TYPE_NEWDATE:
    return new Field_newdate(ptr,null_pos,null_bit,
8942
			     unireg_check, field_name, table, field_charset);
unknown's avatar
unknown committed
8943 8944
  case FIELD_TYPE_TIME:
    return new Field_time(ptr,null_pos,null_bit,
8945
			  unireg_check, field_name, table, field_charset);
unknown's avatar
unknown committed
8946 8947
  case FIELD_TYPE_DATETIME:
    return new Field_datetime(ptr,null_pos,null_bit,
8948
			      unireg_check, field_name, table, field_charset);
unknown's avatar
unknown committed
8949
  case FIELD_TYPE_NULL:
8950
    return new Field_null(ptr,field_length,unireg_check,field_name,table, field_charset);
unknown's avatar
unknown committed
8951
  case FIELD_TYPE_BIT:
8952
    return f_bit_as_char(pack_flag) ?
8953 8954
           new Field_bit_as_char(ptr, field_length, null_pos, null_bit,
                                 unireg_check, field_name, table) :
8955
           new Field_bit(ptr, field_length, null_pos, null_bit, bit_ptr,
unknown's avatar
unknown committed
8956
                         bit_offset, unireg_check, field_name, table);
8957 8958
  default:					// Impossible (Wrong version)
    break;
unknown's avatar
unknown committed
8959
  }
unknown's avatar
unknown committed
8960
  return 0;
unknown's avatar
unknown committed
8961 8962 8963 8964 8965
}


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

8966
create_field::create_field(Field *old_field,Field *orig_field)
unknown's avatar
unknown committed
8967 8968 8969 8970 8971 8972 8973
{
  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();
8974
  key_length= old_field->key_length();
unknown's avatar
unknown committed
8975
  sql_type=   old_field->real_type();
unknown's avatar
unknown committed
8976 8977
  charset=    old_field->charset();		// May be NULL ptr
  comment=    old_field->comment;
8978
  decimals=   old_field->decimals();
unknown's avatar
unknown committed
8979 8980 8981

  /* Fix if the original table had 4 byte pointer blobs */
  if (flags & BLOB_FLAG)
8982
    pack_length= (pack_length- old_field->table->s->blob_ptr_size +
unknown's avatar
unknown committed
8983
		  portable_sizeof_char_ptr);
8984

8985 8986 8987 8988 8989 8990 8991 8992
  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;
    }
8993
    length/= charset->mbmaxlen;
8994 8995
    key_length/= charset->mbmaxlen;
    break;
8996
  case MYSQL_TYPE_STRING:
unknown's avatar
unknown committed
8997
    /* Change CHAR -> VARCHAR if dynamic record length */
8998 8999 9000 9001 9002 9003 9004 9005
    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:
9006
    /* This is corrected in create_length_to_internal_length */
9007 9008 9009 9010 9011 9012 9013 9014 9015
    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;
unknown's avatar
unknown committed
9016
  }
9017

unknown's avatar
unknown committed
9018 9019 9020 9021
  if (flags & (ENUM_FLAG | SET_FLAG))
    interval= ((Field_enum*) old_field)->typelib;
  else
    interval=0;
9022
  def=0;
9023
  char_length= length;
9024

9025
  if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
9026 9027 9028 9029
      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 */
unknown's avatar
unknown committed
9030
  {
9031
    my_ptrdiff_t diff;
9032

unknown's avatar
unknown committed
9033
    /* Get the value from default_values */
9034 9035
    diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
                          orig_field->table->record[0]);
unknown's avatar
unknown committed
9036
    orig_field->move_field(diff);		// Points now at default_values
9037
    if (!orig_field->is_real_null())
9038
    {
9039 9040 9041
      char buff[MAX_FIELD_WIDTH],*pos;
      String tmp(buff,sizeof(buff), charset), *res;
      res= orig_field->val_str(&tmp);
9042 9043
      pos= (char*) sql_strmake(res->ptr(), res->length());
      def= new Item_string(pos, res->length(), charset);
9044
    }
9045
    orig_field->move_field(-diff);		// Back to record[0]
unknown's avatar
unknown committed
9046 9047
  }
}
9048 9049


9050 9051 9052 9053
/*
  maximum possible display length for blob

  SYNOPSIS
9054
    Field_blob::max_display_length()
9055 9056 9057 9058

  RETURN
    length
*/
9059 9060

uint32 Field_blob::max_display_length()
9061 9062 9063 9064
{
  switch (packlength)
  {
  case 1:
9065
    return 255 * field_charset->mbmaxlen;
9066
  case 2:
9067
    return 65535 * field_charset->mbmaxlen;
9068
  case 3:
9069
    return 16777215 * field_charset->mbmaxlen;
9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081
  case 4:
    return (uint32) 4294967295U;
  default:
    DBUG_ASSERT(0); // we should never go here
    return 0;
  }
}


/*****************************************************************************
 Warning handling
*****************************************************************************/
9082 9083 9084 9085

/*
  Produce warning or note about data saved into field

unknown's avatar
unknown committed
9086
  SYNOPSIS
9087 9088 9089 9090 9091 9092 9093
    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
9094 9095 9096 9097
    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().
9098 9099

  RETURN VALUE
9100
    1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
9101
    0 otherwise
9102
*/
9103

9104
bool 
9105 9106
Field::set_warning(MYSQL_ERROR::enum_warning_level level, uint code,
                   int cuted_increment)
9107
{
9108 9109 9110 9111 9112
  /*
    If this field was created only for type conversion purposes it
    will have table == NULL.
  */
  THD *thd= table ? table->in_use : current_thd;
unknown's avatar
unknown committed
9113 9114
  if (thd->count_cuted_fields)
  {
9115
    thd->cuted_fields+= cuted_increment;
9116 9117
    push_warning_printf(thd, level, code, ER(code), field_name,
                        thd->row_count);
9118 9119
    return 0;
  }
9120
  return level >= MYSQL_ERROR::WARN_LEVEL_WARN;
9121 9122 9123 9124 9125 9126
}


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

unknown's avatar
unknown committed
9127
  SYNOPSIS
unknown's avatar
unknown committed
9128
    set_datime_warning()
9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140
      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.
*/
9141

9142
void 
9143
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
9144 9145 9146
                            const char *str, uint str_length, 
                            timestamp_type ts_type, int cuted_increment)
{
unknown's avatar
unknown committed
9147
  THD *thd= table ? table->in_use : current_thd;
9148 9149
  if ((thd->really_abort_on_warning() &&
       level >= MYSQL_ERROR::WARN_LEVEL_WARN) ||
unknown's avatar
unknown committed
9150
      set_warning(level, code, cuted_increment))
9151
    make_truncated_value_warning(thd, level, str, str_length, ts_type,
unknown's avatar
unknown committed
9152
                                 field_name);
9153 9154 9155 9156 9157 9158
}


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

unknown's avatar
unknown committed
9159
  SYNOPSIS
9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171
    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.
*/
9172

9173
void 
9174
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
9175 9176 9177
                            longlong nr, timestamp_type ts_type,
                            int cuted_increment)
{
unknown's avatar
unknown committed
9178 9179
  THD *thd= table ? table->in_use : current_thd;
  if (thd->really_abort_on_warning() ||
unknown's avatar
unknown committed
9180
      set_warning(level, code, cuted_increment))
9181 9182 9183
  {
    char str_nr[22];
    char *str_end= longlong10_to_str(nr, str_nr, -10);
9184
    make_truncated_value_warning(thd, level, str_nr, (uint) (str_end - str_nr), 
unknown's avatar
unknown committed
9185
                                 ts_type, field_name);
9186 9187 9188 9189 9190 9191 9192
  }
}


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

unknown's avatar
unknown committed
9193
  SYNOPSIS
9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204
    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.
*/
9205

9206
void 
9207
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
9208 9209
                            double nr, timestamp_type ts_type)
{
unknown's avatar
unknown committed
9210 9211
  THD *thd= table ? table->in_use : current_thd;
  if (thd->really_abort_on_warning() ||
unknown's avatar
unknown committed
9212
      set_warning(level, code, 1))
9213 9214 9215 9216
  {
    /* 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));
9217
    make_truncated_value_warning(thd, level, str_nr, str_len, ts_type,
unknown's avatar
unknown committed
9218
                                 field_name);
unknown's avatar
unknown committed
9219
  }
9220
}
unknown's avatar
unknown committed
9221