field.cc 284 KB
Newer Older
1
/* Copyright (c) 2000, 2010 Oracle and/or its affiliates. All rights reserved.
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
   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 */


17 18 19 20 21 22
/**
  @file

  @brief
  This file implements classes defined in field.h
*/
unknown's avatar
unknown committed
23

24
#ifdef USE_PRAGMA_IMPLEMENTATION
unknown's avatar
unknown committed
25 26 27
#pragma implementation				// gcc: Class implementation
#endif

28
#include "sql_priv.h"
unknown's avatar
unknown committed
29
#include "sql_select.h"
30 31
#include "rpl_rli.h"                            // Pull in Relay_log_info
#include "slave.h"                              // Pull in rpl_master_has_bug()
32 33 34 35 36 37 38 39 40
#include "strfunc.h"                            // find_type2, find_set
#include "sql_time.h"                    // str_to_datetime_with_warn,
                                         // str_to_time_with_warn,
                                         // TIME_to_timestamp,
                                         // make_time, make_date,
                                         // make_truncated_value_warning
#include "tztime.h"                      // struct Time_zone
#include "filesort.h"                    // change_double_for_sort
#include "log_event.h"                   // class Table_map_log_event
unknown's avatar
unknown committed
41 42 43
#include <m_ctype.h>
#include <errno.h>

unknown's avatar
unknown committed
44 45 46
// Maximum allowed exponent value for converting string to decimal
#define MAX_EXPONENT 1024

unknown's avatar
unknown committed
47
/*****************************************************************************
48
  Instansiate templates and static variables
unknown's avatar
unknown committed
49 50
*****************************************************************************/

51
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
unknown's avatar
unknown committed
52 53
template class List<Create_field>;
template class List_iterator<Create_field>;
unknown's avatar
unknown committed
54 55 56 57 58
#endif

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

59
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
60 61
#define LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE 128
#define DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE 128
62 63
#define BLOB_PACK_LENGTH_TO_MAX_LENGH(arg) \
((ulong) ((LL(1) << min(arg, 4) * 8) - LL(1)))
64

unknown's avatar
unknown committed
65 66
#define ASSERT_COLUMN_MARKED_FOR_READ DBUG_ASSERT(!table || (!table->read_set || bitmap_is_set(table->read_set, field_index)))
#define ASSERT_COLUMN_MARKED_FOR_WRITE DBUG_ASSERT(!table || (!table->write_set || bitmap_is_set(table->write_set, field_index)))
67

68 69
#define FLAGSTR(S,F) ((S) & (F) ? #F " " : "")

70 71 72 73 74 75 76
/*
  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
77 78 79
#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))
80 81 82 83 84 85 86
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);
}

unknown's avatar
unknown committed
87

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

929 930
/**
  Return type of which can carry value of both given types in UNION result.
931

932 933
  @param a  type for merging
  @param b  type for merging
934

935
  @return
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
    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
952
  DECIMAL_RESULT,           INT_RESULT,
953 954 955 956 957 958 959 960 961 962 963 964
  //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
965 966 967
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
  STRING_RESULT,            STRING_RESULT,
  //MYSQL_TYPE_BIT          <16>-<245>
968
  STRING_RESULT,
unknown's avatar
unknown committed
969 970
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
  DECIMAL_RESULT,           STRING_RESULT,
971 972 973 974 975 976 977 978 979 980 981
  //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
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
/*
  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);
}


1007
/**
1008 1009 1010
   Function to compare two unsigned integers for their relative order.
   Used below. In an anonymous namespace to not clash with definitions
   in other files.
1011
 */
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

CPP_UNNAMED_NS_START

int compare(unsigned int a, unsigned int b)
{
  if (a < b)
    return -1;
  if (b < a)
    return 1;
  return 0;
1022 1023
}

1024 1025
CPP_UNNAMED_NS_END

1026 1027
/**
  Detect Item_result by given field type of UNION merge result.
1028

1029
  @param field_type  given field type
1030

1031
  @return
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
    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
1042
/*****************************************************************************
1043
  Static help functions
unknown's avatar
unknown committed
1044 1045
*****************************************************************************/

1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
/**
  Output a warning for erroneous conversion of strings to numerical 
  values. For use with ER_TRUNCATED_WRONG_VALUE[_FOR_FIELD] 
  
  @param thd         THD object
  @param str         pointer to string that failed to be converted
  @param length      length of string
  @param cs          charset for string
  @param typestr     string describing type converted to
  @param error       error value to output
  @param field_name  (for *_FOR_FIELD) name of field
  @param row_num     (for *_FOR_FIELD) row number
 */
static void push_numerical_conversion_warning(THD* thd, const char* str, 
                                              uint length, CHARSET_INFO* cs,
                                              const char* typestr, int error,
                                              const char* field_name="UNKNOWN",
                                              ulong row_num=0)
{
    char buf[max(max(DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE,
      LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE), 
      DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE)];

    String tmp(buf, sizeof(buf), cs);
    tmp.copy(str, length, cs);
    push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                        error, ER(error), typestr, tmp.c_ptr(),
                        field_name, row_num);
}

1076

1077 1078
/**
  Check whether a field type can be partially indexed by a key.
1079 1080 1081 1082

  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().

1083
  @param type  field type
1084

1085
  @retval
1086
    TRUE  Type can have a prefixed key
1087
  @retval
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
    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
1101
  case MYSQL_TYPE_GEOMETRY:
1102 1103 1104 1105 1106 1107 1108
    return TRUE;
  default:
    return FALSE;
  }
}


1109 1110
/**
  Numeric fields base class constructor.
unknown's avatar
unknown committed
1111
*/
1112
Field_num::Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
1113 1114 1115 1116
                     uchar null_bit_arg, utype unireg_check_arg,
                     const char *field_name_arg,
                     uint8 dec_arg, bool zero_arg, bool unsigned_arg)
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1117
         unireg_check_arg, field_name_arg),
unknown's avatar
unknown committed
1118 1119 1120 1121 1122 1123 1124 1125 1126
  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
1127 1128 1129 1130 1131
void Field_num::prepend_zeros(String *value)
{
  int diff;
  if ((diff= (int) (field_length - value->length())) > 0)
  {
1132 1133
    bmove_upp((uchar*) value->ptr()+field_length,
              (uchar*) value->ptr()+value->length(),
unknown's avatar
unknown committed
1134
	      value->length());
1135
    bfill((uchar*) value->ptr(),diff,'0');
unknown's avatar
unknown committed
1136 1137 1138 1139 1140
    value->length(field_length);
    (void) value->c_ptr_quick();		// Avoid warnings in purify
  }
}

1141
/**
unknown's avatar
unknown committed
1142
  Test if given number is a int.
1143

1144 1145
  @todo
    Make this multi-byte-character safe
1146

1147 1148 1149 1150 1151 1152
  @param str		String to test
  @param length        Length of 'str'
  @param int_end	Pointer to char after last used digit
  @param cs		Character set

  @note
unknown's avatar
unknown committed
1153
    This is called after one has called strntoull10rnd() function.
1154

1155 1156 1157
  @retval
    0	OK
  @retval
unknown's avatar
unknown committed
1158
    1	error: empty string or wrong integer.
1159
  @retval
unknown's avatar
unknown committed
1160
    2   error: garbage at the end of string.
unknown's avatar
unknown committed
1161 1162
*/

unknown's avatar
unknown committed
1163 1164
int Field_num::check_int(CHARSET_INFO *cs, const char *str, int length, 
                         const char *int_end, int error)
unknown's avatar
unknown committed
1165
{
unknown's avatar
unknown committed
1166 1167
  /* Test if we get an empty string or wrong integer */
  if (str == int_end || error == MY_ERRNO_EDOM)
unknown's avatar
unknown committed
1168
  {
1169
    ErrConvString err(str, length, cs);
unknown's avatar
unknown committed
1170 1171 1172
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1173
                        "integer", err.ptr(), field_name,
Marc Alff's avatar
Marc Alff committed
1174
                        (ulong) table->in_use->warning_info->current_row_for_warning());
unknown's avatar
unknown committed
1175
    return 1;
unknown's avatar
unknown committed
1176
  }
unknown's avatar
unknown committed
1177 1178 1179 1180 1181 1182 1183 1184
  /* 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
1185

unknown's avatar
unknown committed
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216

/*
  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;
  
1217 1218
  *rnd= (longlong) cs->cset->strntoull10rnd(cs, from, len,
                                            unsigned_flag, &end,
unknown's avatar
unknown committed
1219 1220
                                            &error);
  if (unsigned_flag)
unknown's avatar
unknown committed
1221
  {
unknown's avatar
unknown committed
1222

1223
    if ((((ulonglong) *rnd > unsigned_max) && (*rnd= (longlong) unsigned_max)) ||
unknown's avatar
unknown committed
1224 1225 1226 1227
        error == MY_ERRNO_ERANGE)
    {
      goto out_of_range;
    }
1228
  }
unknown's avatar
unknown committed
1229
  else
1230
  {
unknown's avatar
unknown committed
1231
    if (*rnd < signed_min)
unknown's avatar
unknown committed
1232
    {
unknown's avatar
unknown committed
1233 1234 1235 1236 1237 1238 1239
      *rnd= signed_min;
      goto out_of_range;
    }
    else if (*rnd > signed_max)
    {
      *rnd= signed_max;
      goto out_of_range;
unknown's avatar
unknown committed
1240
    }
unknown's avatar
unknown committed
1241
  }
1242 1243
  if (table->in_use->count_cuted_fields &&
      check_int(cs, from, len, end, error))
unknown's avatar
unknown committed
1244
    return 1;
unknown's avatar
unknown committed
1245
  return 0;
unknown's avatar
unknown committed
1246

unknown's avatar
unknown committed
1247 1248 1249 1250
out_of_range:
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
  return 1;
}
unknown's avatar
unknown committed
1251

1252
/**
unknown's avatar
unknown committed
1253 1254 1255
  Process decimal library return codes and issue warnings for overflow and
  truncation.

1256
  @param op_result  decimal library return code (E_DEC_* see include/decimal.h)
unknown's avatar
unknown committed
1257

1258
  @retval
unknown's avatar
unknown committed
1259
    1  there was overflow
1260
  @retval
unknown's avatar
unknown committed
1261 1262 1263
    0  no error or some other errors except overflow
*/

unknown's avatar
unknown committed
1264
int Field::warn_if_overflow(int op_result)
unknown's avatar
unknown committed
1265 1266 1267 1268 1269 1270
{
  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
1271 1272
  if (op_result == E_DEC_TRUNCATED)
  {
unknown's avatar
unknown committed
1273
    set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
1274 1275
    /* We return 0 here as this is not a critical issue */
  }
unknown's avatar
unknown committed
1276 1277 1278
  return 0;
}

unknown's avatar
unknown committed
1279

1280
/**
unknown's avatar
unknown committed
1281 1282
  Interpret field value as an integer but return the result as a string.

1283
  This is used for printing bit_fields as numbers while debugging.
unknown's avatar
unknown committed
1284 1285
*/

1286
String *Field::val_int_as_str(String *val_buffer, my_bool unsigned_val)
unknown's avatar
unknown committed
1287
{
1288
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
1289
  CHARSET_INFO *cs= &my_charset_bin;
1290
  uint length;
unknown's avatar
unknown committed
1291
  longlong value= val_int();
1292

1293
  if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
unknown's avatar
unknown committed
1294
    return 0;
unknown's avatar
unknown committed
1295
  length= (uint) (*cs->cset->longlong10_to_str)(cs, (char*) val_buffer->ptr(),
1296
                                                MY_INT64_NUM_DECIMAL_DIGITS,
1297
                                                unsigned_val ? 10 : -10,
unknown's avatar
unknown committed
1298
                                                value);
unknown's avatar
unknown committed
1299 1300 1301 1302 1303
  val_buffer->length(length);
  return val_buffer;
}


1304
/// This is used as a table name when the table structure is not set up
1305
Field::Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
unknown's avatar
unknown committed
1306
	     uchar null_bit_arg,
unknown's avatar
unknown committed
1307
	     utype unireg_check_arg, const char *field_name_arg)
1308
  :ptr(ptr_arg), null_ptr(null_ptr_arg),
unknown's avatar
unknown committed
1309
   table(0), orig_table(0), table_name(0),
unknown's avatar
unknown committed
1310
   field_name(field_name_arg),
1311 1312
   key_start(0), part_of_key(0), part_of_key_not_clustered(0),
   part_of_sortkey(0), unireg_check(unireg_check_arg),
1313 1314
   field_length(length_arg), null_bit(null_bit_arg), 
   is_created_from_null_item(FALSE)
unknown's avatar
unknown committed
1315 1316
{
  flags=null_ptr ? 0: NOT_NULL_FLAG;
unknown's avatar
unknown committed
1317 1318
  comment.str= (char*) "";
  comment.length=0;
1319
  field_index= 0;
unknown's avatar
unknown committed
1320 1321
}

1322

1323 1324 1325 1326 1327 1328 1329 1330 1331
void Field::hash(ulong *nr, ulong *nr2)
{
  if (is_null())
  {
    *nr^= (*nr << 1) | 1;
  }
  else
  {
    uint len= pack_length();
1332
    CHARSET_INFO *cs= sort_charset();
1333
    cs->coll->hash_sort(cs, ptr, len, nr, nr2);
1334 1335 1336
  }
}

1337
size_t
1338 1339
Field::do_last_null_byte() const
{
1340
  DBUG_ASSERT(null_ptr == NULL || null_ptr >= table->record[0]);
1341
  if (null_ptr)
1342 1343
    return (size_t) (null_ptr - table->record[0]) + 1;
  return LAST_NULL_BYTE_UNDEF;
1344 1345 1346
}


unknown's avatar
unknown committed
1347 1348 1349 1350 1351
void Field::copy_from_tmp(int row_offset)
{
  memcpy(ptr,ptr+row_offset,pack_length());
  if (null_ptr)
  {
unknown's avatar
unknown committed
1352
    *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) |
1353
			(null_ptr[row_offset] & (uchar) null_bit));
unknown's avatar
unknown committed
1354 1355 1356 1357
  }
}


1358
bool Field::send_binary(Protocol *protocol)
unknown's avatar
unknown committed
1359 1360
{
  char buff[MAX_FIELD_WIDTH];
1361
  String tmp(buff,sizeof(buff),charset());
1362
  val_str(&tmp);
1363
  return protocol->store(tmp.ptr(), tmp.length(), tmp.charset());
unknown's avatar
unknown committed
1364 1365 1366
}


1367 1368 1369
/**
   Check to see if field size is compatible with destination.

1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
   This method is used in row-based replication to verify that the
   slave's field size is less than or equal to the master's field
   size. The encoded field metadata (from the master or source) is
   decoded and compared to the size of this field (the slave or
   destination).

   @note

   The comparison is made so that if the source data (from the master)
   is less than the target data (on the slave), -1 is returned in @c
   <code>*order_var</code>. This implies that a conversion is
   necessary, but that it is lossy and can result in truncation of the
   value.

   If the source data is strictly greater than the target data, 1 is
   returned in <code>*order_var</code>. This implies that the source
   type can is contained in the target type and that a conversion is
   necessary but is non-lossy.

   If no conversion is required to fit the source type in the target
   type, 0 is returned in <code>*order_var</code>.
1391 1392

   @param   field_metadata   Encoded size in field metadata
1393
   @param   mflags           Flags from the table map event for the table.
1394 1395 1396
   @param   order_var        Pointer to variable where the order
                             between the source field and this field
                             will be returned.
1397

1398 1399
   @return @c true if this field's size is compatible with the
   master's field size, @c false otherwise.
1400
*/
1401 1402
bool Field::compatible_field_size(uint field_metadata,
                                  Relay_log_info *rli_arg __attribute__((unused)),
1403
                                  uint16 mflags __attribute__((unused)),
1404
                                  int *order_var)
1405 1406 1407
{
  uint const source_size= pack_length_from_metadata(field_metadata);
  uint const destination_size= row_pack_length();
1408 1409 1410 1411
  DBUG_PRINT("debug", ("real_type: %d, source_size: %u, destination_size: %u",
                       real_type(), source_size, destination_size));
  *order_var = compare(source_size, destination_size);
  return true;
1412 1413 1414
}


1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
int Field::store(const char *to, uint length, CHARSET_INFO *cs,
                 enum_check_fields check_level)
{
  int res;
  enum_check_fields old_check_level= table->in_use->count_cuted_fields;
  table->in_use->count_cuted_fields= check_level;
  res= store(to, length, cs);
  table->in_use->count_cuted_fields= old_check_level;
  return res;
}


1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
/**
   Pack the field into a format suitable for storage and transfer.

   To implement packing functionality, only the virtual function
   should be overridden. The other functions are just convenience
   functions and hence should not be overridden.

   The value of <code>low_byte_first</code> is dependent on how the
   packed data is going to be used: for local use, e.g., temporary
   store on disk or in memory, use the native format since that is
   faster. For data that is going to be transfered to other machines
   (e.g., when writing data to the binary log), data should always be
   stored in little-endian format.

   @note The default method for packing fields just copy the raw bytes
   of the record into the destination, but never more than
   <code>max_length</code> characters.

   @param to
   Pointer to memory area where representation of field should be put.

   @param from
   Pointer to memory area where record representation of field is
   stored.

   @param max_length
   Maximum length of the field, as given in the column definition. For
   example, for <code>CHAR(1000)</code>, the <code>max_length</code>
   is 1000. This information is sometimes needed to decide how to pack
   the data.

   @param low_byte_first
   @c TRUE if integers should be stored little-endian, @c FALSE if
   native format should be used. Note that for little-endian machines,
   the value of this flag is a moot point since the native format is
   little-endian.
*/
uchar *
Field::pack(uchar *to, const uchar *from, uint max_length,
            bool low_byte_first __attribute__((unused)))
{
  uint32 length= pack_length();
  set_if_smaller(length, max_length);
  memcpy(to, from, length);
  return to+length;
}

1474 1475 1476
/**
   Unpack a field from row data.

1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
   This method is used to unpack a field from a master whose size of
   the field is less than that of the slave.

   The <code>param_data</code> parameter is a two-byte integer (stored
   in the least significant 16 bits of the unsigned integer) usually
   consisting of two parts: the real type in the most significant byte
   and a original pack length in the least significant byte.

   The exact layout of the <code>param_data</code> field is given by
   the <code>Table_map_log_event::save_field_metadata()</code>.

   This is the default method for unpacking a field. It just copies
   the memory block in byte order (of original pack length bytes or
   length of field, whichever is smaller).

1492 1493
   @param   to         Destination of the data
   @param   from       Source of the data
1494 1495 1496 1497 1498 1499 1500
   @param   param_data Real type and original pack length of the field
                       data

   @param low_byte_first
   If this flag is @c true, all composite entities (e.g., lengths)
   should be unpacked in little-endian format; otherwise, the entities
   are unpacked in native order.
1501 1502 1503

   @return  New pointer into memory based on from + length of the data
*/
1504 1505 1506
const uchar *
Field::unpack(uchar* to, const uchar *from, uint param_data,
              bool low_byte_first __attribute__((unused)))
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
{
  uint length=pack_length();
  int from_type= 0;
  /*
    If from length is > 255, it has encoded data in the upper bits. Need
    to mask it out.
  */
  if (param_data > 255)
  {
    from_type= (param_data & 0xff00) >> 8U;  // real_type.
    param_data= param_data & 0x00ff;        // length.
  }
1519 1520 1521 1522 1523 1524 1525 1526 1527

  if ((param_data == 0) ||
      (length == param_data) ||
      (from_type != real_type()))
  {
    memcpy(to, from, length);
    return from+length;
  }

1528 1529
  uint len= (param_data && (param_data < length)) ?
            param_data : length;
1530

1531 1532 1533 1534 1535
  memcpy(to, from, param_data > length ? length : len);
  return from+len;
}


unknown's avatar
unknown committed
1536 1537 1538 1539 1540 1541 1542 1543
my_decimal *Field::val_decimal(my_decimal *decimal)
{
  /* This never have to be called */
  DBUG_ASSERT(0);
  return 0;
}


unknown's avatar
unknown committed
1544 1545
void Field_num::add_zerofill_and_unsigned(String &res) const
{
1546
  if (unsigned_flag)
1547
    res.append(STRING_WITH_LEN(" unsigned"));
1548
  if (zerofill)
1549
    res.append(STRING_WITH_LEN(" zerofill"));
unknown's avatar
unknown committed
1550 1551
}

unknown's avatar
unknown committed
1552

1553
void Field::make_field(Send_field *field)
unknown's avatar
unknown committed
1554
{
unknown's avatar
unknown committed
1555
  if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
unknown's avatar
unknown committed
1556
  {
unknown's avatar
unknown committed
1557
    field->db_name= orig_table->s->db.str;
1558 1559 1560 1561 1562 1563
    if (orig_table->pos_in_table_list && 
        orig_table->pos_in_table_list->schema_table)
      field->org_table_name= (orig_table->pos_in_table_list->
                              schema_table->table_name);
    else
      field->org_table_name= orig_table->s->table_name.str;
1564 1565
  }
  else
unknown's avatar
unknown committed
1566
    field->org_table_name= field->db_name= "";
1567
  if (orig_table && orig_table->alias)
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
  {
    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;
1578
  field->charsetnr= charset()->number;
unknown's avatar
unknown committed
1579 1580 1581
  field->length=field_length;
  field->type=type();
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
1582
  field->decimals= 0;
unknown's avatar
unknown committed
1583 1584 1585
}


1586
/**
unknown's avatar
unknown committed
1587
  Conversion from decimal to longlong with checking overflow and
1588
  setting correct value (min/max) in case of overflow.
unknown's avatar
unknown committed
1589

1590 1591 1592
  @param val             value which have to be converted
  @param unsigned_flag   type of integer in which we convert val
  @param err             variable to pass error code
unknown's avatar
unknown committed
1593

1594
  @return
unknown's avatar
unknown committed
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
    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
1609
    else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
unknown's avatar
unknown committed
1610 1611 1612 1613 1614 1615 1616
                                           ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
                                           val, TRUE, &i)))
    {
      i= ~(longlong) 0;
      *err= 1;
    }
  }
unknown's avatar
unknown committed
1617
  else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
unknown's avatar
unknown committed
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
                                         ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
                                         val, FALSE, &i)))
  {
    i= (val->sign() ? LONGLONG_MIN : LONGLONG_MAX);
    *err= 1;
  }
  return i;
}


1628
/**
unknown's avatar
unknown committed
1629 1630
  Storing decimal in integer fields.

1631
  @param val       value for storing
unknown's avatar
unknown committed
1632

1633
  @note
unknown's avatar
unknown committed
1634 1635
    This method is used by all integer fields, real/decimal redefine it

1636
  @retval
unknown's avatar
unknown committed
1637
    0     OK
1638 1639
  @retval
    !=0  error
unknown's avatar
unknown committed
1640 1641 1642 1643
*/

int Field_num::store_decimal(const my_decimal *val)
{
1644
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
1645 1646
  int err= 0;
  longlong i= convert_decimal2longlong(val, unsigned_flag, &err);
1647
  return test(err | store(i, unsigned_flag));
unknown's avatar
unknown committed
1648 1649 1650
}


1651 1652
/**
  Return decimal value of integer field.
unknown's avatar
unknown committed
1653

1654
  @param decimal_value     buffer for storing decimal value
unknown's avatar
unknown committed
1655

1656 1657
  @note
    This method is used by all integer fields, real/decimal redefine it.
unknown's avatar
unknown committed
1658 1659 1660
    All longlong values fit in our decimal buffer which cal store 8*9=72
    digits of integer number

1661
  @return
unknown's avatar
unknown committed
1662 1663 1664 1665 1666
    pointer to decimal buffer with value of field
*/

my_decimal* Field_num::val_decimal(my_decimal *decimal_value)
{
1667
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
1668 1669
  DBUG_ASSERT(result_type() == INT_RESULT);
  longlong nr= val_int();
1670
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
unknown's avatar
unknown committed
1671 1672 1673 1674
  return decimal_value;
}


1675
Field_str::Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
1676
                     uchar null_bit_arg, utype unireg_check_arg,
unknown's avatar
unknown committed
1677
                     const char *field_name_arg, CHARSET_INFO *charset_arg)
unknown's avatar
unknown committed
1678
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
1679
         unireg_check_arg, field_name_arg)
unknown's avatar
unknown committed
1680
{
1681 1682
  field_charset= charset_arg;
  if (charset_arg->state & MY_CS_BINSORT)
unknown's avatar
unknown committed
1683
    flags|=BINARY_FLAG;
1684
  field_derivation= DERIVATION_IMPLICIT;
unknown's avatar
unknown committed
1685 1686 1687
}


1688
void Field_num::make_field(Send_field *field)
unknown's avatar
unknown committed
1689
{
1690 1691
  Field::make_field(field);
  field->decimals= dec;
unknown's avatar
unknown committed
1692 1693
}

1694 1695
/**
  Decimal representation of Field_str.
unknown's avatar
unknown committed
1696

1697
  @param d         value for storing
unknown's avatar
unknown committed
1698

1699 1700 1701 1702 1703
  @note
    Field_str is the base class for fields like Field_enum,
    Field_date and some 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.
unknown's avatar
unknown committed
1704

1705 1706 1707 1708
  @todo
    use decimal2string?

  @retval
unknown's avatar
unknown committed
1709
    0     OK
1710 1711
  @retval
    !=0  error
unknown's avatar
unknown committed
1712 1713 1714 1715
*/

int Field_str::store_decimal(const my_decimal *d)
{
1716
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
1717 1718
  double val;
  /* TODO: use decimal2string? */
unknown's avatar
unknown committed
1719
  int err= warn_if_overflow(my_decimal2double(E_DEC_FATAL_ERROR &
unknown's avatar
unknown committed
1720 1721 1722 1723 1724 1725 1726
                                            ~E_DEC_OVERFLOW, d, &val));
  return err | store(val);
}


my_decimal *Field_str::val_decimal(my_decimal *decimal_value)
{
1727
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
1728
  longlong nr= val_int();
1729
  int2my_decimal(E_DEC_FATAL_ERROR, nr, 0, decimal_value);
unknown's avatar
unknown committed
1730 1731 1732
  return decimal_value;
}

1733

unknown's avatar
unknown committed
1734 1735
uint Field::fill_cache_field(CACHE_FIELD *copy)
{
unknown's avatar
unknown committed
1736
  uint store_length;
unknown's avatar
unknown committed
1737 1738
  copy->str=ptr;
  copy->length=pack_length();
1739
  copy->field= this;
unknown's avatar
unknown committed
1740 1741
  if (flags & BLOB_FLAG)
  {
1742
    copy->type= CACHE_BLOB;
1743
    copy->length-= table->s->blob_ptr_size;
unknown's avatar
unknown committed
1744 1745
    return copy->length;
  }
1746 1747 1748
  else if (!zero_pack() &&
           (type() == MYSQL_TYPE_STRING && copy->length >= 4 &&
            copy->length < 256))
unknown's avatar
unknown committed
1749
  {
1750
    copy->type= CACHE_STRIPPED;
unknown's avatar
unknown committed
1751 1752
    store_length= 2;
  }
unknown's avatar
unknown committed
1753
  else
unknown's avatar
unknown committed
1754
  {
1755
    copy->type= 0;
unknown's avatar
unknown committed
1756 1757
    store_length= 0;
  }
1758
  return copy->length + store_length;
unknown's avatar
unknown committed
1759 1760
}

1761

1762
bool Field::get_date(MYSQL_TIME *ltime,uint fuzzydate)
unknown's avatar
unknown committed
1763 1764
{
  char buff[40];
1765 1766
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
  if (!(res=val_str(&tmp)) ||
1767
      str_to_datetime_with_warn(res->charset(), res->ptr(), res->length(),
1768
                                ltime, fuzzydate) <= MYSQL_TIMESTAMP_ERROR)
unknown's avatar
unknown committed
1769 1770 1771 1772
    return 1;
  return 0;
}

1773
bool Field::get_time(MYSQL_TIME *ltime)
unknown's avatar
unknown committed
1774 1775
{
  char buff[40];
1776 1777
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
  if (!(res=val_str(&tmp)) ||
1778
      str_to_time_with_warn(res->charset(), res->ptr(), res->length(), ltime))
unknown's avatar
unknown committed
1779 1780 1781 1782
    return 1;
  return 0;
}

1783 1784
/**
  This is called when storing a date in a string.
1785

1786 1787
  @note
    Needs to be changed if/when we want to support different time formats.
1788
*/
unknown's avatar
unknown committed
1789

1790
int Field::store_time(MYSQL_TIME *ltime, timestamp_type type_arg)
unknown's avatar
unknown committed
1791
{
1792
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1793 1794
  char buff[MAX_DATE_STRING_REP_LENGTH];
  uint length= (uint) my_TIME_to_str(ltime, buff);
1795 1796 1797
  /* Avoid conversion when field character set is ASCII compatible */
  return store(buff, length, (charset()->state & MY_CS_NONASCII) ?
                              &my_charset_latin1 : charset());
unknown's avatar
unknown committed
1798 1799 1800
}


1801
bool Field::optimize_range(uint idx, uint part)
unknown's avatar
unknown committed
1802
{
1803
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
unknown's avatar
unknown committed
1804 1805
}

1806

Konstantin Osipov's avatar
Konstantin Osipov committed
1807
Field *Field::new_field(MEM_ROOT *root, TABLE *new_table,
1808
                        bool keep_type __attribute__((unused)))
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
{
  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);
unknown's avatar
unknown committed
1820
  tmp->unireg_check= Field::NONE;
1821 1822 1823 1824 1825 1826
  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
1827

Konstantin Osipov's avatar
Konstantin Osipov committed
1828
Field *Field::new_key_field(MEM_ROOT *root, TABLE *new_table,
1829
                            uchar *new_ptr, uchar *new_null_ptr,
unknown's avatar
unknown committed
1830 1831 1832
                            uint new_null_bit)
{
  Field *tmp;
1833
  if ((tmp= new_field(root, new_table, table == new_table)))
unknown's avatar
unknown committed
1834 1835 1836 1837 1838 1839 1840 1841
  {
    tmp->ptr=      new_ptr;
    tmp->null_ptr= new_null_ptr;
    tmp->null_bit= new_null_bit;
  }
  return tmp;
}

1842

unknown's avatar
unknown committed
1843 1844
/* This is used to generate a field in TABLE from TABLE_SHARE */

Konstantin Osipov's avatar
Konstantin Osipov committed
1845
Field *Field::clone(MEM_ROOT *root, TABLE *new_table)
unknown's avatar
unknown committed
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
{
  Field *tmp;
  if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
  {
    tmp->init(new_table);
    tmp->move_field_offset((my_ptrdiff_t) (new_table->record[0] -
                                           new_table->s->default_values));
  }
  return tmp;
}


1858
/****************************************************************************
1859
  Field_null, a field that always return NULL
1860 1861 1862 1863
****************************************************************************/

void Field_null::sql_type(String &res) const
{
1864
  res.set_ascii(STRING_WITH_LEN("null"));
1865 1866 1867
}


unknown's avatar
unknown committed
1868
/****************************************************************************
unknown's avatar
unknown committed
1869 1870
  Functions for the Field_decimal class
  This is an number stored as a pre-space (or pre-zero) string
unknown's avatar
unknown committed
1871 1872
****************************************************************************/

1873
int
unknown's avatar
unknown committed
1874 1875
Field_decimal::reset(void)
{
1876
  Field_decimal::store(STRING_WITH_LEN("0"),&my_charset_bin);
1877
  return 0;
unknown's avatar
unknown committed
1878 1879 1880 1881 1882
}

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

1885
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1886
  if (negative)
unknown's avatar
unknown committed
1887
  {
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
    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
1911
  }
1912
  bfill(to, len, filler);
unknown's avatar
unknown committed
1913 1914 1915 1916 1917 1918
  if (dec)
    ptr[field_length-dec-1]='.';
  return;
}


1919
int Field_decimal::store(const char *from_arg, uint len, CHARSET_INFO *cs)
unknown's avatar
unknown committed
1920
{
1921
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
1922
  char buff[STRING_BUFFER_USUAL_SIZE];
unknown's avatar
unknown committed
1923
  String tmp(buff,sizeof(buff), &my_charset_bin);
1924
  const uchar *from= (uchar*) from_arg;
1925

1926
  /* Convert character set if the old one is multi uchar */
1927 1928
  if (cs->mbmaxlen > 1)
  { 
1929
    uint dummy_errors;
1930 1931
    tmp.copy((char*) from, len, cs, &my_charset_bin, &dummy_errors);
    from= (uchar*) tmp.ptr();
1932 1933
    len=  tmp.length();
  }
1934

1935
  const uchar *end= from+len;
1936
  /* The pointer where the field value starts (i.e., "where to write") */
1937
  uchar *to= ptr;
1938 1939 1940 1941 1942
  uint tmp_dec, tmp_uint;
  /*
    The sign of the number : will be 0 (means positive but sign not
    specified), '+' or '-'
  */
1943
  uchar sign_char=0;
unknown's avatar
unknown committed
1944
  /* The pointers where prezeros start and stop */
1945
  const uchar *pre_zeros_from, *pre_zeros_end;
unknown's avatar
unknown committed
1946
  /* The pointers where digits at the left of '.' start and stop */
1947
  const uchar *int_digits_from, *int_digits_end;
unknown's avatar
unknown committed
1948
  /* The pointers where digits at the right of '.' start and stop */
1949
  const uchar *frac_digits_from, *frac_digits_end;
unknown's avatar
unknown committed
1950
  /* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
1951
  char expo_sign_char=0;
unknown's avatar
unknown committed
1952
  uint exponent=0;                                // value of the exponent
1953 1954 1955 1956
  /*
    Pointers used when digits move from the left of the '.' to the
    right of the '.' (explained below)
  */
Staale Smedseng's avatar
Staale Smedseng committed
1957
  const uchar *UNINIT_VAR(int_digits_tail_from);
1958
  /* Number of 0 that need to be added at the left of the '.' (1E3: 3 zeros) */
1959
  uint UNINIT_VAR(int_digits_added_zeros);
unknown's avatar
unknown committed
1960 1961 1962 1963
  /*
    Pointer used when digits move from the right of the '.' to the left
    of the '.'
  */
Staale Smedseng's avatar
Staale Smedseng committed
1964
  const uchar *UNINIT_VAR(frac_digits_head_end);
unknown's avatar
unknown committed
1965
  /* Number of 0 that need to be added at the right of the '.' (for 1E-3) */
1966
  uint UNINIT_VAR(frac_digits_added_zeros);
1967
  uchar *pos,*tmp_left_pos,*tmp_right_pos;
1968
  /* Pointers that are used as limits (begin and end of the field buffer) */
1969 1970
  uchar *left_wall,*right_wall;
  uchar tmp_char;
unknown's avatar
unknown committed
1971
  /*
1972
    To remember if table->in_use->cuted_fields has already been incremented,
unknown's avatar
unknown committed
1973 1974
    to do that only once
  */
1975 1976 1977 1978
  bool is_cuted_fields_incr=0;

  /*
    There are three steps in this function :
unknown's avatar
unknown committed
1979 1980
    - parse the input string
    - modify the position of digits around the decimal dot '.' 
1981
      according to the exponent value (if specified)
unknown's avatar
unknown committed
1982
    - write the formatted number
1983 1984 1985 1986 1987
  */

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

unknown's avatar
unknown committed
1988
  /* skip pre-space */
unknown's avatar
unknown committed
1989
  while (from != end && my_isspace(&my_charset_bin,*from))
unknown's avatar
unknown committed
1990
    from++;
1991
  if (from == end)
unknown's avatar
unknown committed
1992
  {
1993
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
1994
    is_cuted_fields_incr=1;
unknown's avatar
unknown committed
1995
  }
1996
  else if (*from == '+' || *from == '-')	// Found some sign ?
unknown's avatar
unknown committed
1997
  {
1998
    sign_char= *from++;
unknown's avatar
unknown committed
1999
    /*
2000 2001 2002
      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
2003 2004 2005 2006
    */ 
    if (unsigned_flag)  
    { 
      if (sign_char=='-')
2007
      {
2008
        Field_decimal::overflow(1);
unknown's avatar
unknown committed
2009
        return 1;
2010
      }
2011
      /* 
unknown's avatar
unknown committed
2012 2013
	 Defining this will not store "+" for unsigned decimal type even if
	 it is passed in numeric string. This will make some tests to fail
2014 2015
      */	 
#ifdef DONT_ALLOW_UNSIGNED_PLUS      
2016 2017
      else 
        sign_char=0;
2018
#endif 	
unknown's avatar
unknown committed
2019
    }
2020
  }
unknown's avatar
unknown committed
2021

2022 2023 2024 2025
  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
2026
  for (; from != end && my_isdigit(&my_charset_bin, *from) ; from++) ;
2027 2028 2029 2030 2031
  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
2032
  for (;from!=end && my_isdigit(&my_charset_bin, *from); from++) ;
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
  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= '+';
    /*
2043 2044 2045 2046
      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).
2047
    */
unknown's avatar
unknown committed
2048
    for (;from!=end && my_isdigit(&my_charset_bin, *from); from++)
2049
    {
2050
      exponent=10*exponent+(*from-'0');
unknown's avatar
unknown committed
2051
      if (exponent>MAX_EXPONENT)
2052 2053
        break;
    }
unknown's avatar
unknown committed
2054
  }
2055
  
unknown's avatar
unknown committed
2056
  /*
2057 2058
    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
2059
    Even if this flag is not set, it's OK to increment warnings, if
2060
    it makes the code easer to read.
unknown's avatar
unknown committed
2061
  */
2062

2063
  if (table->in_use->count_cuted_fields)
unknown's avatar
unknown committed
2064
  {
2065
    // Skip end spaces
unknown's avatar
unknown committed
2066
    for (;from != end && my_isspace(&my_charset_bin, *from); from++) ;
2067
    if (from != end)                     // If still something left, warn
unknown's avatar
unknown committed
2068
    {
2069
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
2070
      is_cuted_fields_incr=1;
unknown's avatar
unknown committed
2071
    }
2072 2073 2074 2075 2076 2077
  }
  
  /*
    Now "move" digits around the decimal dot according to the exponent value,
    and add necessary zeros.
    Examples :
unknown's avatar
unknown committed
2078 2079 2080
    - 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'
2081
    - 1234.5E-3 : '234' moves at the right of '.'
unknown's avatar
unknown committed
2082 2083
    These moves are implemented with pointers which point at the begin
    and end of each moved segment. Examples :
2084
    - 1234.5E-3 : before the code below is executed, the int_digits part is
unknown's avatar
unknown committed
2085 2086 2087
    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'.
2088
    - 1234.5E3 : before the code below is executed, the int_digits part is
unknown's avatar
unknown committed
2089 2090 2091 2092
    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).
2093 2094
  */
  
2095
  /* 
unknown's avatar
unknown committed
2096
     Below tmp_uint cannot overflow with small enough MAX_EXPONENT setting,
2097
     as int_digits_added_zeros<=exponent<4G and 
2098 2099
     (int_digits_end-int_digits_from)<=max_allowed_packet<=2G and
     (frac_digits_from-int_digits_tail_from)<=max_allowed_packet<=2G
2100 2101
  */

2102 2103 2104 2105 2106 2107 2108 2109
  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
2110
    tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);     
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
  }
  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
2136
    }
2137
    tmp_uint= (uint) (tmp_dec+(int_digits_end-int_digits_from)+
2138 2139
               (uint)(frac_digits_from-int_digits_tail_from)+
               int_digits_added_zeros);
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
  }
  
  /*
    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
  */

2150
  if (field_length < tmp_uint + (int) (sign_char == '-')) 
2151
  {
unknown's avatar
unknown committed
2152
    // too big number, change to max or min number
2153
    Field_decimal::overflow(sign_char == '-');
2154
    return 1;
unknown's avatar
unknown committed
2155
  }
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
 
  /*
    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
2173
  }
2174 2175 2176 2177 2178 2179
  /*
    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
2180 2181

  /*
2182 2183
    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
2184
  */
2185 2186 2187 2188
  pos=tmp_left_pos-1;
  if (zerofill)
  {
    left_wall=to-1;
2189
    while (pos > left_wall)			// Fill with zeros
2190 2191 2192 2193
      *pos--='0';
  }
  else
  {
unknown's avatar
unknown committed
2194
    left_wall=to+(sign_char != 0)-1;
2195 2196
    if (!expo_sign_char)	// If exponent was specified, ignore prezeros
    {
2197
      for (;pos > left_wall && pre_zeros_from !=pre_zeros_end;
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
	   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
  }
  
2213 2214
  /*
    Write digits of the frac_% parts ;
2215
    Depending on table->in_use->count_cutted_fields, we may also want
2216 2217 2218 2219 2220
    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)
  */
2221
      
2222 2223 2224
  pos=to+(uint)(field_length-tmp_dec);	// Calculate post to '.'
  right_wall=to+field_length;
  if (pos != right_wall) 
2225 2226
    *pos++='.';

2227 2228 2229
  if (expo_sign_char == '-')
  {
    while (frac_digits_added_zeros-- > 0)
unknown's avatar
unknown committed
2230
    {
2231
      if (pos == right_wall) 
2232
      {
2233
        if (table->in_use->count_cuted_fields && !is_cuted_fields_incr) 
2234
          break; // Go on below to see if we lose non zero digits
unknown's avatar
unknown committed
2235
        return 0;
2236
      }
2237 2238 2239 2240 2241 2242
      *pos++='0';
    }
    while (int_digits_end != frac_digits_head_end)
    {
      tmp_char= *int_digits_end++;
      if (pos == right_wall)
unknown's avatar
unknown committed
2243
      {
2244 2245 2246
        if (tmp_char != '0')			// Losing a non zero digit ?
        {
          if (!is_cuted_fields_incr)
2247
            set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
2248
                        WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
2249
          return 0;
2250 2251
        }
        continue;
unknown's avatar
unknown committed
2252
      }
2253
      *pos++= tmp_char;
unknown's avatar
unknown committed
2254
    }
2255
  }
2256

2257 2258 2259 2260
  for (;frac_digits_from!=frac_digits_end;) 
  {
    tmp_char= *frac_digits_from++;
    if (pos == right_wall)
2261
    {
2262
      if (tmp_char != '0')			// Losing a non zero digit ?
2263
      {
2264
        if (!is_cuted_fields_incr)
unknown's avatar
unknown committed
2265 2266 2267 2268 2269
        {
          /*
            This is a note, not a warning, as we don't want to abort
            when we cut decimals in strict mode
          */
2270
	  set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
2271
        }
unknown's avatar
unknown committed
2272
        return 0;
2273
      }
2274
      continue;
2275
    }
2276
    *pos++= tmp_char;
unknown's avatar
unknown committed
2277
  }
2278 2279 2280
      
  while (pos != right_wall)
   *pos++='0';			// Fill with zeros at right of '.'
unknown's avatar
unknown committed
2281
  return 0;
unknown's avatar
unknown committed
2282 2283 2284
}


unknown's avatar
unknown committed
2285
int Field_decimal::store(double nr)
unknown's avatar
unknown committed
2286
{
2287
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
2288 2289 2290
  if (unsigned_flag && nr < 0)
  {
    overflow(1);
2291
    return 1;
unknown's avatar
unknown committed
2292
  }
2293
  
2294
  if (!isfinite(nr)) // Handle infinity as special case
2295 2296
  {
    overflow(nr < 0.0);
unknown's avatar
unknown committed
2297
    return 1;
2298
  }
unknown's avatar
unknown committed
2299

2300 2301 2302
  reg4 uint i;
  size_t length;
  uchar fyllchar,*to;
2303
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
unknown's avatar
unknown committed
2304 2305

  fyllchar = zerofill ? (char) '0' : (char) ' ';
2306
  length= my_fcvt(nr, dec, buff, NULL);
unknown's avatar
unknown committed
2307 2308 2309 2310

  if (length > field_length)
  {
    overflow(nr < 0.0);
2311
    return 1;
unknown's avatar
unknown committed
2312 2313 2314 2315 2316 2317 2318
  }
  else
  {
    to=ptr;
    for (i=field_length-length ; i-- > 0 ;)
      *to++ = fyllchar;
    memcpy(to,buff,length);
2319
    return 0;
unknown's avatar
unknown committed
2320 2321 2322 2323
  }
}


2324
int Field_decimal::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
2325
{
2326
  ASSERT_COLUMN_MARKED_FOR_WRITE;
2327 2328
  char buff[22];
  uint length, int_part;
2329 2330
  char fyllchar;
  uchar *to;
2331 2332

  if (nr < 0 && unsigned_flag && !unsigned_val)
unknown's avatar
unknown committed
2333 2334
  {
    overflow(1);
2335
    return 1;
unknown's avatar
unknown committed
2336
  }
2337 2338
  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
2339 2340 2341

  if (length > int_part)
  {
2342
    overflow(!unsigned_val && nr < 0L);		/* purecov: inspected */
2343
    return 1;
unknown's avatar
unknown committed
2344
  }
2345 2346 2347 2348 2349 2350 2351

  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
2352
  {
2353 2354
    to[length]='.';
    bfill(to+length+1,dec,'0');
unknown's avatar
unknown committed
2355
  }
2356
  return 0;
unknown's avatar
unknown committed
2357 2358 2359 2360 2361
}


double Field_decimal::val_real(void)
{
2362
  ASSERT_COLUMN_MARKED_FOR_READ;
2363
  int not_used;
2364
  char *end_not_used;
2365
  return my_strntod(&my_charset_bin, (char*) ptr, field_length, &end_not_used,
2366
                    &not_used);
unknown's avatar
unknown committed
2367 2368 2369 2370
}

longlong Field_decimal::val_int(void)
{
2371
  ASSERT_COLUMN_MARKED_FOR_READ;
2372
  int not_used;
unknown's avatar
unknown committed
2373
  if (unsigned_flag)
2374
    return my_strntoull(&my_charset_bin, (char*) ptr, field_length, 10, NULL,
2375
			&not_used);
2376 2377
  return my_strntoll(&my_charset_bin, (char*) ptr, field_length, 10, NULL,
                     &not_used);
unknown's avatar
unknown committed
2378 2379
}

2380

unknown's avatar
unknown committed
2381 2382 2383
String *Field_decimal::val_str(String *val_buffer __attribute__((unused)),
			       String *val_ptr)
{
2384
  ASSERT_COLUMN_MARKED_FOR_READ;
2385 2386 2387
  uchar *str;
  size_t tmp_length;

unknown's avatar
unknown committed
2388
  for (str=ptr ; *str == ' ' ; str++) ;
2389
  val_ptr->set_charset(&my_charset_numeric);
2390
  tmp_length= (size_t) (str-ptr);
unknown's avatar
unknown committed
2391 2392 2393
  if (field_length < tmp_length)		// Error in data
    val_ptr->length(0);
  else
2394
    val_ptr->set_ascii((const char*) str, field_length-tmp_length);
unknown's avatar
unknown committed
2395 2396 2397
  return val_ptr;
}

2398 2399 2400
/**
  Should be able to handle at least the following fixed decimal formats:
  5.00 , -1.0,  05,  -05, +5 with optional pre/end space
unknown's avatar
unknown committed
2401 2402
*/

2403
int Field_decimal::cmp(const uchar *a_ptr,const uchar *b_ptr)
unknown's avatar
unknown committed
2404
{
2405
  const uchar *end;
2406
  int swap=0;
unknown's avatar
unknown committed
2407 2408 2409 2410
  /* First remove prefixes '0', ' ', and '-' */
  for (end=a_ptr+field_length;
       a_ptr != end &&
	 (*a_ptr == *b_ptr ||
unknown's avatar
unknown committed
2411
	  ((my_isspace(&my_charset_bin,*a_ptr)  || *a_ptr == '+' || 
2412
            *a_ptr == '0') &&
unknown's avatar
unknown committed
2413
	   (my_isspace(&my_charset_bin,*b_ptr) || *b_ptr == '+' || 
2414
            *b_ptr == '0')));
2415 2416 2417 2418 2419
       a_ptr++,b_ptr++)
  {
    if (*a_ptr == '-')				// If both numbers are negative
      swap= -1 ^ 1;				// Swap result      
  }
unknown's avatar
unknown committed
2420 2421 2422
  if (a_ptr == end)
    return 0;
  if (*a_ptr == '-')
2423
    return -1;
2424
  if (*b_ptr == '-')
unknown's avatar
unknown committed
2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435
    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;
}


2436
void Field_decimal::sort_string(uchar *to,uint length)
unknown's avatar
unknown committed
2437
{
2438
  uchar *str,*end;
unknown's avatar
unknown committed
2439 2440
  for (str=ptr,end=ptr+length;
       str != end &&
unknown's avatar
unknown committed
2441
	 ((my_isspace(&my_charset_bin,*str) || *str == '+' ||
unknown's avatar
unknown committed
2442
	   *str == '0')) ;
unknown's avatar
unknown committed
2443 2444 2445 2446 2447 2448 2449 2450 2451 2452
       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
2453
      if (my_isdigit(&my_charset_bin,*str))
unknown's avatar
unknown committed
2454 2455 2456 2457 2458 2459 2460
	*to++= (char) ('9' - *str++);
      else
	*to++= *str++;
  }
  else memcpy(to,str,(uint) (end-str));
}

unknown's avatar
unknown committed
2461

unknown's avatar
unknown committed
2462 2463
void Field_decimal::sql_type(String &res) const
{
unknown's avatar
unknown committed
2464
  CHARSET_INFO *cs=res.charset();
unknown's avatar
unknown committed
2465 2466 2467 2468 2469
  uint tmp=field_length;
  if (!unsigned_flag)
    tmp--;
  if (dec)
    tmp--;
2470
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2471
			  "decimal(%d,%d)",tmp,dec));
unknown's avatar
unknown committed
2472 2473 2474 2475
  add_zerofill_and_unsigned(res);
}


unknown's avatar
unknown committed
2476 2477 2478 2479
/****************************************************************************
** Field_new_decimal
****************************************************************************/

2480
Field_new_decimal::Field_new_decimal(uchar *ptr_arg,
unknown's avatar
unknown committed
2481 2482 2483 2484 2485 2486
                                     uint32 len_arg, uchar *null_ptr_arg,
                                     uchar null_bit_arg,
                                     enum utype unireg_check_arg,
                                     const char *field_name_arg,
                                     uint8 dec_arg,bool zero_arg,
                                     bool unsigned_arg)
unknown's avatar
unknown committed
2487 2488
  :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
             unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg)
unknown's avatar
unknown committed
2489
{
unknown's avatar
unknown committed
2490
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
unknown's avatar
unknown committed
2491
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
unknown's avatar
unknown committed
2492 2493 2494
  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
              (dec <= DECIMAL_MAX_SCALE));
  bin_size= my_decimal_get_binary_size(precision, dec);
unknown's avatar
unknown committed
2495 2496 2497 2498
}


Field_new_decimal::Field_new_decimal(uint32 len_arg,
2499
                                     bool maybe_null_arg,
unknown's avatar
unknown committed
2500
                                     const char *name,
unknown's avatar
unknown committed
2501 2502
                                     uint8 dec_arg,
                                     bool unsigned_arg)
2503
  :Field_num((uchar*) 0, len_arg,
2504
             maybe_null_arg ? (uchar*) "": 0, 0,
unknown's avatar
unknown committed
2505
             NONE, name, dec_arg, 0, unsigned_arg)
unknown's avatar
unknown committed
2506
{
unknown's avatar
unknown committed
2507
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
unknown's avatar
unknown committed
2508
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
unknown's avatar
unknown committed
2509 2510 2511
  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
              (dec <= DECIMAL_MAX_SCALE));
  bin_size= my_decimal_get_binary_size(precision, dec);
unknown's avatar
unknown committed
2512 2513 2514
}


2515 2516 2517 2518
Field *Field_new_decimal::create_from_item (Item *item)
{
  uint8 dec= item->decimals;
  uint8 intg= item->decimal_precision() - dec;
2519
  uint32 len= item->max_char_length();
2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558

  DBUG_ASSERT (item->result_type() == DECIMAL_RESULT);

  /*
    Trying to put too many digits overall in a DECIMAL(prec,dec)
    will always throw a warning. We must limit dec to
    DECIMAL_MAX_SCALE however to prevent an assert() later.
  */

  if (dec > 0)
  {
    signed int overflow;

    dec= min(dec, DECIMAL_MAX_SCALE);

    /*
      If the value still overflows the field with the corrected dec,
      we'll throw out decimals rather than integers. This is still
      bad and of course throws a truncation warning.
      +1: for decimal point
      */

    const int required_length=
      my_decimal_precision_to_length(intg + dec, dec,
                                     item->unsigned_flag);

    overflow= required_length - len;

    if (overflow > 0)
      dec= max(0, dec - overflow);            // too long, discard fract
    else
      /* Corrected value fits. */
      len= required_length;
  }
  return new Field_new_decimal(len, item->maybe_null, item->name,
                               dec, item->unsigned_flag);
}


unknown's avatar
unknown committed
2559
int Field_new_decimal::reset(void)
unknown's avatar
unknown committed
2560 2561
{
  store_value(&decimal_zero);
unknown's avatar
unknown committed
2562
  return 0;
unknown's avatar
unknown committed
2563 2564 2565
}


2566
/**
unknown's avatar
unknown committed
2567 2568
  Generate max/min decimal value in case of overflow.

2569 2570
  @param decimal_value     buffer for value
  @param sign              sign of value which caused overflow
unknown's avatar
unknown committed
2571 2572 2573 2574 2575 2576
*/

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
2577
  max_my_decimal(decimal_value, precision, decimals());
unknown's avatar
unknown committed
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588
  if (sign)
  {
    if (unsigned_flag)
      my_decimal_set_zero(decimal_value);
    else
      decimal_value->sign(TRUE);
  }
  DBUG_VOID_RETURN;
}


2589 2590
/**
  Store decimal value in the binary buffer.
unknown's avatar
unknown committed
2591

2592 2593 2594
  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
2595

2596
  @param decimal_value   my_decimal
unknown's avatar
unknown committed
2597

2598 2599 2600 2601
  @retval
    0 ok
  @retval
    1 error
unknown's avatar
unknown committed
2602 2603 2604 2605
*/

bool Field_new_decimal::store_value(const my_decimal *decimal_value)
{
2606
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
2607 2608
  int error= 0;
  DBUG_ENTER("Field_new_decimal::store_value");
unknown's avatar
unknown committed
2609 2610 2611 2612 2613 2614
#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
2615 2616 2617 2618 2619 2620

  /* 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
2621
    error= 1;
unknown's avatar
unknown committed
2622 2623 2624 2625 2626
    decimal_value= &decimal_zero;
  }
#ifndef DBUG_OFF
  {
    char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
2627
    DBUG_PRINT("info", ("saving with precision %d  scale: %d  value %s",
unknown's avatar
unknown committed
2628 2629
                        (int)precision, (int)dec,
                        dbug_decimal_as_string(dbug_buff, decimal_value)));
unknown's avatar
unknown committed
2630
  }
unknown's avatar
unknown committed
2631
#endif
unknown's avatar
unknown committed
2632

unknown's avatar
unknown committed
2633 2634
  if (warn_if_overflow(my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
                                         decimal_value, ptr, precision, dec)))
unknown's avatar
unknown committed
2635 2636 2637
  {
    my_decimal buff;
    DBUG_PRINT("info", ("overflow"));
unknown's avatar
unknown committed
2638 2639
    set_value_on_overflow(&buff, decimal_value->sign());
    my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
2640
    error= 1;
unknown's avatar
unknown committed
2641
  }
2642
  DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
2643
                                          bin_size););
unknown's avatar
unknown committed
2644
  DBUG_RETURN(error);
unknown's avatar
unknown committed
2645 2646 2647 2648
}


int Field_new_decimal::store(const char *from, uint length,
2649
                             CHARSET_INFO *charset_arg)
unknown's avatar
unknown committed
2650
{
2651
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
2652 2653
  int err;
  my_decimal decimal_value;
unknown's avatar
unknown committed
2654 2655
  DBUG_ENTER("Field_new_decimal::store(char*)");

2656
  if ((err= str2my_decimal(E_DEC_FATAL_ERROR &
2657 2658 2659
                           ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
                           from, length, charset_arg,
                           &decimal_value)) &&
2660 2661
      table->in_use->abort_on_warning)
  {
2662
    ErrConvString errmsg(from, length, &my_charset_bin);
Marc Alff's avatar
Marc Alff committed
2663
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
2664 2665
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
2666
                        "decimal", errmsg.ptr(), field_name,
Marc Alff's avatar
Marc Alff committed
2667
                        (ulong) table->in_use->warning_info->current_row_for_warning());
2668

2669 2670 2671 2672
    DBUG_RETURN(err);
  }

  switch (err) {
unknown's avatar
unknown committed
2673 2674 2675 2676 2677 2678 2679 2680
  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:
2681
    {
2682 2683 2684 2685 2686 2687 2688
      ErrConvString errmsg(from, length, &my_charset_bin);
      push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
                          ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
                          ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
                          "decimal", errmsg.ptr(), field_name,
                          (ulong) table->in_use->warning_info->
                          current_row_for_warning());
2689 2690
      my_decimal_set_zero(&decimal_value);
      break;
2691
    }
unknown's avatar
unknown committed
2692 2693
  }

unknown's avatar
unknown committed
2694 2695 2696 2697 2698
#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
2699 2700 2701 2702 2703
  store_value(&decimal_value);
  DBUG_RETURN(err);
}


2704 2705 2706 2707 2708 2709
/**
  @todo
  Fix following when double2my_decimal when double2decimal
  will return E_DEC_TRUNCATED always correctly
*/

unknown's avatar
unknown committed
2710 2711
int Field_new_decimal::store(double nr)
{
2712
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
2713
  my_decimal decimal_value;
unknown's avatar
unknown committed
2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
  int err;
  DBUG_ENTER("Field_new_decimal::store(double)");

  err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
                         &decimal_value);
  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
2731 2732 2733
}


2734
int Field_new_decimal::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
2735
{
2736
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
2737 2738
  my_decimal decimal_value;
  int err;
unknown's avatar
unknown committed
2739 2740

  if ((err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
2741
                           nr, unsigned_val, &decimal_value)))
unknown's avatar
unknown committed
2742 2743 2744 2745 2746 2747 2748 2749 2750 2751
  {
    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
2752 2753 2754 2755 2756 2757
  return err;
}


int Field_new_decimal::store_decimal(const my_decimal *decimal_value)
{
2758
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
2759 2760 2761 2762
  return store_value(decimal_value);
}


2763
int Field_new_decimal::store_time(MYSQL_TIME *ltime, timestamp_type t_type)
2764 2765 2766
{
    my_decimal decimal_value;
    return store_value(date2my_decimal(ltime, &decimal_value));
unknown's avatar
unknown committed
2767 2768 2769 2770 2771
}


double Field_new_decimal::val_real(void)
{
2772
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
2773 2774 2775 2776 2777 2778 2779 2780 2781
  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)
{
2782
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792
  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)
{
2793
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
2794 2795
  DBUG_ENTER("Field_new_decimal::val_decimal");
  binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
unknown's avatar
unknown committed
2796
                    precision, dec);
2797
  DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
2798
                                          bin_size););
unknown's avatar
unknown committed
2799 2800 2801 2802 2803
  DBUG_RETURN(decimal_value);
}


String *Field_new_decimal::val_str(String *val_buffer,
unknown's avatar
unknown committed
2804
                                   String *val_ptr __attribute__((unused)))
unknown's avatar
unknown committed
2805
{
2806
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
2807
  my_decimal decimal_value;
unknown's avatar
unknown committed
2808
  uint fixed_precision= zerofill ? precision : 0;
unknown's avatar
unknown committed
2809
  my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
unknown's avatar
unknown committed
2810
                    fixed_precision, dec, '0', val_buffer);
2811
  val_buffer->set_charset(&my_charset_numeric);
unknown's avatar
unknown committed
2812 2813 2814 2815
  return val_buffer;
}


2816
int Field_new_decimal::cmp(const uchar *a,const uchar*b)
unknown's avatar
unknown committed
2817 2818 2819 2820 2821
{
  return memcmp(a, b, bin_size);
}


2822
void Field_new_decimal::sort_string(uchar *buff,
unknown's avatar
unknown committed
2823 2824 2825 2826 2827 2828 2829 2830 2831 2832
                                    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
2833
                                "decimal(%d,%d)", precision, (int)dec));
unknown's avatar
unknown committed
2834 2835 2836
  add_zerofill_and_unsigned(str);
}

unknown's avatar
unknown committed
2837

2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878
/**
   Save the field metadata for new decimal fields.

   Saves the precision in the first byte and decimals() in the second
   byte of the field metadata array at index of *metadata_ptr and 
   *(metadata_ptr + 1).

   @param   metadata_ptr   First byte of field metadata

   @returns number of bytes written to metadata_ptr
*/
int Field_new_decimal::do_save_field_metadata(uchar *metadata_ptr)
{
  *metadata_ptr= precision;
  *(metadata_ptr + 1)= decimals();
  return 2;
}


/**
   Returns the number of bytes field uses in row-based replication 
   row packed size.

   This method is used in row-based replication to determine the number
   of bytes that the field consumes in the row record format. This is
   used to skip fields in the master that do not exist on the slave.

   @param   field_metadata   Encoded size in field metadata

   @returns The size of the field based on the field metadata.
*/
uint Field_new_decimal::pack_length_from_metadata(uint field_metadata)
{
  uint const source_precision= (field_metadata >> 8U) & 0x00ff;
  uint const source_decimal= field_metadata & 0x00ff; 
  uint const source_size= my_decimal_get_binary_size(source_precision, 
                                                     source_decimal);
  return (source_size);
}


2879 2880
bool Field_new_decimal::compatible_field_size(uint field_metadata,
                                              Relay_log_info * __attribute__((unused)),
2881
                                              uint16 mflags __attribute__((unused)),
2882
                                              int *order_var)
2883 2884 2885
{
  uint const source_precision= (field_metadata >> 8U) & 0x00ff;
  uint const source_decimal= field_metadata & 0x00ff; 
2886 2887 2888
  int order= compare(source_precision, precision);
  *order_var= order != 0 ? order : compare(source_decimal, dec);
  return true;
2889 2890 2891
}


unknown's avatar
unknown committed
2892
uint Field_new_decimal::is_equal(Create_field *new_field)
2893 2894 2895 2896 2897 2898
{
  return ((new_field->sql_type == real_type()) &&
          ((new_field->flags & UNSIGNED_FLAG) == 
           (uint) (flags & UNSIGNED_FLAG)) &&
          ((new_field->flags & AUTO_INCREMENT_FLAG) ==
           (uint) (flags & AUTO_INCREMENT_FLAG)) &&
unknown's avatar
unknown committed
2899
          (new_field->length == max_display_length()) &&
2900 2901 2902 2903
          (new_field->decimals == dec));
}


2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915
/**
   Unpack a decimal field from row data.

   This method is used to unpack a decimal or numeric field from a master
   whose size of the field is less than that of the slave.
  
   @param   to         Destination of the data
   @param   from       Source of the data
   @param   param_data Precision (upper) and decimal (lower) values

   @return  New pointer into memory based on from + length of the data
*/
2916 2917 2918 2919 2920
const uchar *
Field_new_decimal::unpack(uchar* to,
                          const uchar *from,
                          uint param_data,
                          bool low_byte_first)
2921
{
2922 2923 2924
  if (param_data == 0)
    return Field::unpack(to, from, param_data, low_byte_first);

2925 2926 2927 2928 2929 2930
  uint from_precision= (param_data & 0xff00) >> 8U;
  uint from_decimal= param_data & 0x00ff;
  uint length=pack_length();
  uint from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
  uint len= (param_data && (from_pack_len < length)) ?
            from_pack_len : length;
2931 2932 2933
  if ((from_pack_len && (from_pack_len < length)) ||
      (from_precision < precision) ||
      (from_decimal < decimals()))
2934 2935 2936 2937 2938 2939 2940
  {
    /*
      If the master's data is smaller than the slave, we need to convert
      the binary to decimal then resize the decimal converting it back to
      a decimal and write that to the raw data buffer.
    */
    decimal_digit_t dec_buf[DECIMAL_MAX_PRECISION];
2941 2942 2943
    decimal_t dec_val;
    dec_val.len= from_precision;
    dec_val.buf= dec_buf;
2944 2945 2946 2947 2948
    /*
      Note: bin2decimal does not change the length of the field. So it is
      just the first step the resizing operation. The second step does the
      resizing using the precision and decimals from the slave.
    */
2949 2950
    bin2decimal((uchar *)from, &dec_val, from_precision, from_decimal);
    decimal2bin(&dec_val, to, precision, decimals());
2951 2952 2953 2954 2955 2956
  }
  else
    memcpy(to, from, len); // Sizes are the same, just copy the data.
  return from+len;
}

unknown's avatar
unknown committed
2957 2958 2959 2960
/****************************************************************************
** tiny int
****************************************************************************/

unknown's avatar
unknown committed
2961
int Field_tiny::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
2962
{
2963
  ASSERT_COLUMN_MARKED_FOR_WRITE;
2964
  int error;
unknown's avatar
unknown committed
2965 2966 2967 2968
  longlong rnd;
  
  error= get_int(cs, from, len, &rnd, 255, -128, 127);
  ptr[0]= unsigned_flag ? (char) (ulonglong) rnd : (char) rnd;
2969
  return error;
unknown's avatar
unknown committed
2970 2971 2972
}


unknown's avatar
unknown committed
2973
int Field_tiny::store(double nr)
unknown's avatar
unknown committed
2974
{
2975
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
2976
  int error= 0;
unknown's avatar
unknown committed
2977 2978 2979 2980 2981 2982
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0.0)
    {
      *ptr=0;
2983
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
2984
      error= 1;
unknown's avatar
unknown committed
2985 2986 2987 2988
    }
    else if (nr > 255.0)
    {
      *ptr=(char) 255;
2989
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
2990
      error= 1;
unknown's avatar
unknown committed
2991 2992 2993 2994 2995 2996 2997 2998 2999
    }
    else
      *ptr=(char) nr;
  }
  else
  {
    if (nr < -128.0)
    {
      *ptr= (char) -128;
3000
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3001
      error= 1;
unknown's avatar
unknown committed
3002 3003 3004 3005
    }
    else if (nr > 127.0)
    {
      *ptr=127;
3006
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3007
      error= 1;
unknown's avatar
unknown committed
3008 3009
    }
    else
unknown's avatar
unknown committed
3010
      *ptr=(char) (int) nr;
unknown's avatar
unknown committed
3011
  }
3012
  return error;
unknown's avatar
unknown committed
3013 3014
}

3015 3016

int Field_tiny::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
3017
{
3018
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
3019
  int error= 0;
3020

unknown's avatar
unknown committed
3021 3022
  if (unsigned_flag)
  {
3023
    if (nr < 0 && !unsigned_val)
unknown's avatar
unknown committed
3024
    {
3025
      *ptr= 0;
3026
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3027
      error= 1;
unknown's avatar
unknown committed
3028
    }
3029
    else if ((ulonglong) nr > (ulonglong) 255)
unknown's avatar
unknown committed
3030 3031
    {
      *ptr= (char) 255;
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 3036 3037 3038 3039
    }
    else
      *ptr=(char) nr;
  }
  else
  {
3040 3041 3042
    if (nr < 0 && unsigned_val)
      nr= 256;                                    // Generate overflow
    if (nr < -128)
unknown's avatar
unknown committed
3043 3044
    {
      *ptr= (char) -128;
3045
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3046
      error= 1;
unknown's avatar
unknown committed
3047
    }
3048
    else if (nr > 127)
unknown's avatar
unknown committed
3049 3050
    {
      *ptr=127;
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
      *ptr=(char) nr;
  }
3057
  return error;
unknown's avatar
unknown committed
3058 3059 3060 3061 3062
}


double Field_tiny::val_real(void)
{
3063
  ASSERT_COLUMN_MARKED_FOR_READ;
3064
  int tmp= unsigned_flag ? (int) ptr[0] :
unknown's avatar
unknown committed
3065 3066 3067 3068
    (int) ((signed char*) ptr)[0];
  return (double) tmp;
}

3069

unknown's avatar
unknown committed
3070 3071
longlong Field_tiny::val_int(void)
{
3072
  ASSERT_COLUMN_MARKED_FOR_READ;
3073
  int tmp= unsigned_flag ? (int) ptr[0] :
unknown's avatar
unknown committed
3074 3075 3076 3077
    (int) ((signed char*) ptr)[0];
  return (longlong) tmp;
}

3078

unknown's avatar
unknown committed
3079 3080 3081
String *Field_tiny::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
3082
  ASSERT_COLUMN_MARKED_FOR_READ;
3083
  CHARSET_INFO *cs= &my_charset_numeric;
unknown's avatar
unknown committed
3084
  uint length;
3085 3086
  uint mlength=max(field_length+1,5*cs->mbmaxlen);
  val_buffer->alloc(mlength);
unknown's avatar
unknown committed
3087
  char *to=(char*) val_buffer->ptr();
3088

unknown's avatar
unknown committed
3089
  if (unsigned_flag)
3090
    length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
3091
					   (long) *ptr);
unknown's avatar
unknown committed
3092
  else
3093 3094
    length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
					   (long) *((signed char*) ptr));
3095
  
unknown's avatar
unknown committed
3096 3097 3098
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
3099
  val_buffer->set_charset(cs);
unknown's avatar
unknown committed
3100 3101 3102
  return val_buffer;
}

3103 3104 3105 3106
bool Field_tiny::send_binary(Protocol *protocol)
{
  return protocol->store_tiny((longlong) (int8) ptr[0]);
}
unknown's avatar
unknown committed
3107

3108
int Field_tiny::cmp(const uchar *a_ptr, const uchar *b_ptr)
unknown's avatar
unknown committed
3109 3110 3111 3112 3113 3114 3115 3116
{
  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;
}

3117
void Field_tiny::sort_string(uchar *to,uint length __attribute__((unused)))
unknown's avatar
unknown committed
3118 3119 3120 3121
{
  if (unsigned_flag)
    *to= *ptr;
  else
3122
    to[0] = (char) (ptr[0] ^ (uchar) 128);	/* Revers signbit */
unknown's avatar
unknown committed
3123 3124 3125 3126
}

void Field_tiny::sql_type(String &res) const
{
unknown's avatar
unknown committed
3127
  CHARSET_INFO *cs=res.charset();
3128
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3129
			  "tinyint(%d)",(int) field_length));
unknown's avatar
unknown committed
3130 3131 3132 3133
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
3134
 Field type short int (2 byte)
unknown's avatar
unknown committed
3135 3136
****************************************************************************/

unknown's avatar
unknown committed
3137
int Field_short::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
3138
{
3139
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
3140
  int store_tmp;
3141
  int error;
unknown's avatar
unknown committed
3142 3143 3144 3145
  longlong rnd;
  
  error= get_int(cs, from, len, &rnd, UINT_MAX16, INT_MIN16, INT_MAX16);
  store_tmp= unsigned_flag ? (int) (ulonglong) rnd : (int) rnd;
3146
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3147 3148 3149
  if (table->s->db_low_byte_first)
  {
    int2store(ptr, store_tmp);
unknown's avatar
unknown committed
3150 3151 3152
  }
  else
#endif
unknown's avatar
unknown committed
3153
    shortstore(ptr, (short) store_tmp);
3154
  return error;
unknown's avatar
unknown committed
3155 3156 3157
}


unknown's avatar
unknown committed
3158
int Field_short::store(double nr)
unknown's avatar
unknown committed
3159
{
3160
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
3161
  int error= 0;
unknown's avatar
unknown committed
3162 3163 3164 3165 3166 3167 3168
  int16 res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
3169
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3170
      error= 1;
unknown's avatar
unknown committed
3171
    }
unknown's avatar
unknown committed
3172
    else if (nr > (double) UINT_MAX16)
unknown's avatar
unknown committed
3173
    {
unknown's avatar
unknown committed
3174
      res=(int16) UINT_MAX16;
3175
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3176
      error= 1;
unknown's avatar
unknown committed
3177 3178 3179 3180 3181 3182 3183 3184 3185
    }
    else
      res=(int16) (uint16) nr;
  }
  else
  {
    if (nr < (double) INT_MIN16)
    {
      res=INT_MIN16;
3186
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3187
      error= 1;
unknown's avatar
unknown committed
3188 3189 3190 3191
    }
    else if (nr > (double) INT_MAX16)
    {
      res=INT_MAX16;
3192
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3193
      error= 1;
unknown's avatar
unknown committed
3194 3195
    }
    else
unknown's avatar
unknown committed
3196
      res=(int16) (int) nr;
unknown's avatar
unknown committed
3197 3198
  }
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3199
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3200 3201 3202 3203 3204 3205
  {
    int2store(ptr,res);
  }
  else
#endif
    shortstore(ptr,res);
3206
  return error;
unknown's avatar
unknown committed
3207 3208
}

3209 3210

int Field_short::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
3211
{
3212
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
3213
  int error= 0;
unknown's avatar
unknown committed
3214
  int16 res;
3215

unknown's avatar
unknown committed
3216 3217
  if (unsigned_flag)
  {
3218
    if (nr < 0L && !unsigned_val)
unknown's avatar
unknown committed
3219 3220
    {
      res=0;
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
    else if ((ulonglong) nr > (ulonglong) UINT_MAX16)
unknown's avatar
unknown committed
3225
    {
unknown's avatar
unknown committed
3226
      res=(int16) UINT_MAX16;
3227
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3228
      error= 1;
unknown's avatar
unknown committed
3229 3230 3231 3232 3233 3234
    }
    else
      res=(int16) (uint16) nr;
  }
  else
  {
3235 3236 3237
    if (nr < 0 && unsigned_val)
      nr= UINT_MAX16+1;                         // Generate overflow

unknown's avatar
unknown committed
3238 3239 3240
    if (nr < INT_MIN16)
    {
      res=INT_MIN16;
3241
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3242
      error= 1;
unknown's avatar
unknown committed
3243
    }
3244
    else if (nr > (longlong) INT_MAX16)
unknown's avatar
unknown committed
3245 3246
    {
      res=INT_MAX16;
3247
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3248
      error= 1;
unknown's avatar
unknown committed
3249 3250 3251 3252 3253
    }
    else
      res=(int16) nr;
  }
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3254
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3255 3256 3257 3258 3259 3260
  {
    int2store(ptr,res);
  }
  else
#endif
    shortstore(ptr,res);
3261
  return error;
unknown's avatar
unknown committed
3262 3263 3264 3265 3266
}


double Field_short::val_real(void)
{
3267
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
3268 3269
  short j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3270
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3271 3272 3273 3274 3275 3276 3277 3278 3279
    j=sint2korr(ptr);
  else
#endif
    shortget(j,ptr);
  return unsigned_flag ? (double) (unsigned short) j : (double) j;
}

longlong Field_short::val_int(void)
{
3280
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
3281 3282
  short j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3283
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3284 3285 3286 3287 3288 3289 3290
    j=sint2korr(ptr);
  else
#endif
    shortget(j,ptr);
  return unsigned_flag ? (longlong) (unsigned short) j : (longlong) j;
}

3291

unknown's avatar
unknown committed
3292 3293 3294
String *Field_short::val_str(String *val_buffer,
			     String *val_ptr __attribute__((unused)))
{
3295
  ASSERT_COLUMN_MARKED_FOR_READ;
3296
  CHARSET_INFO *cs= &my_charset_numeric;
unknown's avatar
unknown committed
3297
  uint length;
3298 3299
  uint mlength=max(field_length+1,7*cs->mbmaxlen);
  val_buffer->alloc(mlength);
unknown's avatar
unknown committed
3300 3301 3302
  char *to=(char*) val_buffer->ptr();
  short j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3303
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3304 3305 3306 3307 3308 3309
    j=sint2korr(ptr);
  else
#endif
    shortget(j,ptr);

  if (unsigned_flag)
3310 3311
    length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10, 
					  (long) (uint16) j);
unknown's avatar
unknown committed
3312
  else
3313
    length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
unknown's avatar
unknown committed
3314 3315 3316
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
3317
  val_buffer->set_charset(cs);
unknown's avatar
unknown committed
3318 3319 3320 3321
  return val_buffer;
}


3322 3323 3324 3325 3326 3327
bool Field_short::send_binary(Protocol *protocol)
{
  return protocol->store_short(Field_short::val_int());
}


3328
int Field_short::cmp(const uchar *a_ptr, const uchar *b_ptr)
unknown's avatar
unknown committed
3329 3330 3331
{
  short a,b;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3332
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349
  {
    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;
}

3350
void Field_short::sort_string(uchar *to,uint length __attribute__((unused)))
unknown's avatar
unknown committed
3351 3352
{
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3353
  if (!table->s->db_low_byte_first)
unknown's avatar
unknown committed
3354 3355 3356 3357
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
unknown's avatar
unknown committed
3358
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
3359 3360 3361 3362 3363 3364 3365 3366
    to[1]   = ptr[1];
  }
  else
#endif
  {
    if (unsigned_flag)
      to[0] = ptr[1];
    else
unknown's avatar
unknown committed
3367
      to[0] = (char) (ptr[1] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
3368 3369 3370 3371 3372 3373
    to[1]   = ptr[0];
  }
}

void Field_short::sql_type(String &res) const
{
unknown's avatar
unknown committed
3374
  CHARSET_INFO *cs=res.charset();
3375
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3376
			  "smallint(%d)",(int) field_length));
unknown's avatar
unknown committed
3377 3378 3379 3380 3381
  add_zerofill_and_unsigned(res);
}


/****************************************************************************
3382
  Field type medium int (3 byte)
unknown's avatar
unknown committed
3383 3384
****************************************************************************/

unknown's avatar
unknown committed
3385
int Field_medium::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
3386
{
3387
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
3388
  int store_tmp;
3389
  int error;
unknown's avatar
unknown committed
3390 3391 3392 3393 3394
  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);
3395
  return error;
unknown's avatar
unknown committed
3396 3397 3398
}


unknown's avatar
unknown committed
3399
int Field_medium::store(double nr)
unknown's avatar
unknown committed
3400
{
3401
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
3402
  int error= 0;
unknown's avatar
unknown committed
3403 3404 3405 3406 3407 3408
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      int3store(ptr,0);
3409
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3410
      error= 1;
unknown's avatar
unknown committed
3411 3412 3413
    }
    else if (nr >= (double) (long) (1L << 24))
    {
unknown's avatar
unknown committed
3414
      uint32 tmp=(uint32) (1L << 24)-1L;
unknown's avatar
unknown committed
3415
      int3store(ptr,tmp);
3416
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3417
      error= 1;
unknown's avatar
unknown committed
3418 3419
    }
    else
unknown's avatar
unknown committed
3420
      int3store(ptr,(uint32) nr);
unknown's avatar
unknown committed
3421 3422 3423 3424 3425 3426 3427
  }
  else
  {
    if (nr < (double) INT_MIN24)
    {
      long tmp=(long) INT_MIN24;
      int3store(ptr,tmp);
3428
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3429
      error= 1;
unknown's avatar
unknown committed
3430 3431 3432 3433 3434
    }
    else if (nr > (double) INT_MAX24)
    {
      long tmp=(long) INT_MAX24;
      int3store(ptr,tmp);
3435
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3436
      error= 1;
unknown's avatar
unknown committed
3437 3438 3439 3440
    }
    else
      int3store(ptr,(long) nr);
  }
3441
  return error;
unknown's avatar
unknown committed
3442 3443
}

3444 3445

int Field_medium::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
3446
{
3447
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
3448
  int error= 0;
3449

unknown's avatar
unknown committed
3450 3451
  if (unsigned_flag)
  {
3452
    if (nr < 0 && !unsigned_val)
unknown's avatar
unknown committed
3453 3454
    {
      int3store(ptr,0);
3455
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3456
      error= 1;
unknown's avatar
unknown committed
3457
    }
3458
    else if ((ulonglong) nr >= (ulonglong) (long) (1L << 24))
unknown's avatar
unknown committed
3459
    {
3460
      long tmp= (long) (1L << 24)-1L;
unknown's avatar
unknown committed
3461
      int3store(ptr,tmp);
3462
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3463
      error= 1;
unknown's avatar
unknown committed
3464 3465
    }
    else
unknown's avatar
unknown committed
3466
      int3store(ptr,(uint32) nr);
unknown's avatar
unknown committed
3467 3468 3469
  }
  else
  {
3470 3471 3472
    if (nr < 0 && unsigned_val)
      nr= (ulonglong) (long) (1L << 24);        // Generate overflow

unknown's avatar
unknown committed
3473 3474
    if (nr < (longlong) INT_MIN24)
    {
3475
      long tmp= (long) INT_MIN24;
unknown's avatar
unknown committed
3476
      int3store(ptr,tmp);
3477
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3478
      error= 1;
unknown's avatar
unknown committed
3479 3480 3481 3482 3483
    }
    else if (nr > (longlong) INT_MAX24)
    {
      long tmp=(long) INT_MAX24;
      int3store(ptr,tmp);
3484
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3485
      error= 1;
unknown's avatar
unknown committed
3486 3487 3488 3489
    }
    else
      int3store(ptr,(long) nr);
  }
3490
  return error;
unknown's avatar
unknown committed
3491 3492 3493 3494 3495
}


double Field_medium::val_real(void)
{
3496
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
3497 3498 3499 3500
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
  return (double) j;
}

3501

unknown's avatar
unknown committed
3502 3503
longlong Field_medium::val_int(void)
{
3504
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
3505 3506 3507 3508
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
  return (longlong) j;
}

3509

unknown's avatar
unknown committed
3510 3511 3512
String *Field_medium::val_str(String *val_buffer,
			      String *val_ptr __attribute__((unused)))
{
3513
  ASSERT_COLUMN_MARKED_FOR_READ;
3514
  CHARSET_INFO *cs= &my_charset_numeric;
unknown's avatar
unknown committed
3515
  uint length;
3516 3517
  uint mlength=max(field_length+1,10*cs->mbmaxlen);
  val_buffer->alloc(mlength);
unknown's avatar
unknown committed
3518 3519 3520
  char *to=(char*) val_buffer->ptr();
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);

3521
  length=(uint) cs->cset->long10_to_str(cs,to,mlength,-10,j);
unknown's avatar
unknown committed
3522 3523 3524
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer); /* purecov: inspected */
3525
  val_buffer->set_charset(cs);
unknown's avatar
unknown committed
3526 3527 3528 3529
  return val_buffer;
}


3530 3531
bool Field_medium::send_binary(Protocol *protocol)
{
3532
  ASSERT_COLUMN_MARKED_FOR_READ;
3533 3534 3535 3536
  return protocol->store_long(Field_medium::val_int());
}


3537
int Field_medium::cmp(const uchar *a_ptr, const uchar *b_ptr)
unknown's avatar
unknown committed
3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552
{
  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;
}

3553
void Field_medium::sort_string(uchar *to,uint length __attribute__((unused)))
unknown's avatar
unknown committed
3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565
{
  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
3566
  CHARSET_INFO *cs=res.charset();
3567
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), 
3568
			  "mediumint(%d)",(int) field_length));
unknown's avatar
unknown committed
3569 3570 3571 3572 3573 3574 3575
  add_zerofill_and_unsigned(res);
}

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

unknown's avatar
unknown committed
3576
int Field_long::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
3577
{
3578
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
3579 3580
  long store_tmp;
  int error;
unknown's avatar
unknown committed
3581 3582 3583 3584
  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
3585
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3586
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3587
  {
unknown's avatar
unknown committed
3588
    int4store(ptr, store_tmp);
unknown's avatar
unknown committed
3589 3590 3591
  }
  else
#endif
unknown's avatar
unknown committed
3592
    longstore(ptr, store_tmp);
3593
  return error;
unknown's avatar
unknown committed
3594 3595 3596
}


unknown's avatar
unknown committed
3597
int Field_long::store(double nr)
unknown's avatar
unknown committed
3598
{
3599
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
3600
  int error= 0;
unknown's avatar
unknown committed
3601 3602 3603 3604 3605 3606 3607
  int32 res;
  nr=rint(nr);
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
unknown's avatar
unknown committed
3608
      error= 1;
unknown's avatar
unknown committed
3609
    }
3610
    else if (nr > (double) UINT_MAX32)
unknown's avatar
unknown committed
3611
    {
3612
      res= UINT_MAX32;
3613
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
3614
      error= 1;
unknown's avatar
unknown committed
3615 3616 3617 3618 3619 3620 3621 3622 3623
    }
    else
      res=(int32) (ulong) nr;
  }
  else
  {
    if (nr < (double) INT_MIN32)
    {
      res=(int32) INT_MIN32;
unknown's avatar
unknown committed
3624
      error= 1;
unknown's avatar
unknown committed
3625 3626 3627 3628
    }
    else if (nr > (double) INT_MAX32)
    {
      res=(int32) INT_MAX32;
unknown's avatar
unknown committed
3629
      error= 1;
unknown's avatar
unknown committed
3630 3631
    }
    else
unknown's avatar
unknown committed
3632
      res=(int32) (longlong) nr;
unknown's avatar
unknown committed
3633
  }
unknown's avatar
unknown committed
3634 3635 3636
  if (error)
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);

unknown's avatar
unknown committed
3637
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3638
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3639 3640 3641 3642 3643 3644
  {
    int4store(ptr,res);
  }
  else
#endif
    longstore(ptr,res);
3645
  return error;
unknown's avatar
unknown committed
3646 3647 3648
}


3649
int Field_long::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
3650
{
3651
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
3652
  int error= 0;
unknown's avatar
unknown committed
3653
  int32 res;
unknown's avatar
unknown committed
3654

unknown's avatar
unknown committed
3655 3656
  if (unsigned_flag)
  {
3657
    if (nr < 0 && !unsigned_val)
unknown's avatar
unknown committed
3658 3659
    {
      res=0;
unknown's avatar
unknown committed
3660
      error= 1;
unknown's avatar
unknown committed
3661
    }
3662
    else if ((ulonglong) nr >= (LL(1) << 32))
unknown's avatar
unknown committed
3663 3664
    {
      res=(int32) (uint32) ~0L;
unknown's avatar
unknown committed
3665
      error= 1;
unknown's avatar
unknown committed
3666 3667 3668 3669 3670 3671
    }
    else
      res=(int32) (uint32) nr;
  }
  else
  {
3672
    if (nr < 0 && unsigned_val)
3673
      nr= ((longlong) INT_MAX32) + 1;           // Generate overflow
3674
    if (nr < (longlong) INT_MIN32) 
unknown's avatar
unknown committed
3675 3676
    {
      res=(int32) INT_MIN32;
unknown's avatar
unknown committed
3677
      error= 1;
unknown's avatar
unknown committed
3678 3679 3680 3681
    }
    else if (nr > (longlong) INT_MAX32)
    {
      res=(int32) INT_MAX32;
unknown's avatar
unknown committed
3682
      error= 1;
unknown's avatar
unknown committed
3683 3684 3685 3686
    }
    else
      res=(int32) nr;
  }
unknown's avatar
unknown committed
3687 3688 3689
  if (error)
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);

unknown's avatar
unknown committed
3690
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3691
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3692 3693 3694 3695 3696 3697
  {
    int4store(ptr,res);
  }
  else
#endif
    longstore(ptr,res);
3698
  return error;
unknown's avatar
unknown committed
3699 3700 3701 3702 3703
}


double Field_long::val_real(void)
{
3704
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
3705 3706
  int32 j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3707
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3708 3709 3710 3711 3712 3713 3714 3715 3716
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);
  return unsigned_flag ? (double) (uint32) j : (double) j;
}

longlong Field_long::val_int(void)
{
3717
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
3718
  int32 j;
3719 3720
  /* See the comment in Field_long::store(long long) */
  DBUG_ASSERT(table->in_use == current_thd);
unknown's avatar
unknown committed
3721
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3722
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3723 3724 3725 3726 3727 3728 3729 3730 3731 3732
    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)))
{
3733
  ASSERT_COLUMN_MARKED_FOR_READ;
3734
  CHARSET_INFO *cs= &my_charset_numeric;
unknown's avatar
unknown committed
3735
  uint length;
3736 3737
  uint mlength=max(field_length+1,12*cs->mbmaxlen);
  val_buffer->alloc(mlength);
unknown's avatar
unknown committed
3738 3739 3740
  char *to=(char*) val_buffer->ptr();
  int32 j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3741
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3742 3743 3744 3745 3746
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);

3747
  if (unsigned_flag)
3748
    length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32)j);
3749
  else
3750
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
unknown's avatar
unknown committed
3751 3752 3753
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
3754
  val_buffer->set_charset(cs);
unknown's avatar
unknown committed
3755 3756 3757 3758
  return val_buffer;
}


3759 3760
bool Field_long::send_binary(Protocol *protocol)
{
3761
  ASSERT_COLUMN_MARKED_FOR_READ;
3762 3763 3764
  return protocol->store_long(Field_long::val_int());
}

3765
int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr)
unknown's avatar
unknown committed
3766 3767 3768
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3769
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780
  {
    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
3781
    return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
unknown's avatar
unknown committed
3782 3783 3784
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

3785
void Field_long::sort_string(uchar *to,uint length __attribute__((unused)))
unknown's avatar
unknown committed
3786 3787
{
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3788
  if (!table->s->db_low_byte_first)
unknown's avatar
unknown committed
3789 3790 3791 3792
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
unknown's avatar
unknown committed
3793
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
3794 3795 3796 3797 3798 3799 3800 3801 3802 3803
    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
3804
      to[0] = (char) (ptr[3] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
3805 3806 3807 3808 3809 3810 3811 3812 3813
    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
3814
  CHARSET_INFO *cs=res.charset();
3815
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3816
			  "int(%d)",(int) field_length));
unknown's avatar
unknown committed
3817 3818 3819 3820
  add_zerofill_and_unsigned(res);
}

/****************************************************************************
3821
 Field type longlong int (8 bytes)
unknown's avatar
unknown committed
3822 3823
****************************************************************************/

unknown's avatar
unknown committed
3824
int Field_longlong::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
3825
{
3826
  ASSERT_COLUMN_MARKED_FOR_WRITE;
3827
  int error= 0;
3828
  char *end;
3829
  ulonglong tmp;
3830

3831 3832
  tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&error);
  if (error == MY_ERRNO_ERANGE)
3833
  {
unknown's avatar
unknown committed
3834
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3835 3836
    error= 1;
  }
unknown's avatar
unknown committed
3837 3838
  else if (table->in_use->count_cuted_fields && 
           check_int(cs, from, len, end, error))
3839 3840 3841
    error= 1;
  else
    error= 0;
unknown's avatar
unknown committed
3842
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3843
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3844 3845 3846 3847 3848 3849
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
3850
  return error;
unknown's avatar
unknown committed
3851 3852 3853
}


unknown's avatar
unknown committed
3854
int Field_longlong::store(double nr)
unknown's avatar
unknown committed
3855
{
3856
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
3857
  int error= 0;
unknown's avatar
unknown committed
3858
  longlong res;
unknown's avatar
unknown committed
3859 3860

  nr= rint(nr);
unknown's avatar
unknown committed
3861 3862 3863 3864 3865
  if (unsigned_flag)
  {
    if (nr < 0)
    {
      res=0;
unknown's avatar
unknown committed
3866
      error= 1;
unknown's avatar
unknown committed
3867
    }
unknown's avatar
unknown committed
3868
    else if (nr >= (double) ULONGLONG_MAX)
unknown's avatar
unknown committed
3869 3870
    {
      res= ~(longlong) 0;
unknown's avatar
unknown committed
3871
      error= 1;
unknown's avatar
unknown committed
3872 3873
    }
    else
3874
      res=(longlong) double2ulonglong(nr);
unknown's avatar
unknown committed
3875 3876 3877
  }
  else
  {
unknown's avatar
unknown committed
3878
    if (nr <= (double) LONGLONG_MIN)
unknown's avatar
unknown committed
3879
    {
unknown's avatar
unknown committed
3880
      res= LONGLONG_MIN;
unknown's avatar
unknown committed
3881
      error= (nr < (double) LONGLONG_MIN);
unknown's avatar
unknown committed
3882
    }
3883
    else if (nr >= (double) (ulonglong) LONGLONG_MAX)
unknown's avatar
unknown committed
3884
    {
unknown's avatar
unknown committed
3885
      res= LONGLONG_MAX;
unknown's avatar
unknown committed
3886
      error= (nr > (double) LONGLONG_MAX);
unknown's avatar
unknown committed
3887 3888 3889 3890
    }
    else
      res=(longlong) nr;
  }
unknown's avatar
unknown committed
3891 3892 3893
  if (error)
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);

unknown's avatar
unknown committed
3894
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3895
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3896 3897 3898 3899 3900 3901
  {
    int8store(ptr,res);
  }
  else
#endif
    longlongstore(ptr,res);
3902
  return error;
unknown's avatar
unknown committed
3903 3904 3905
}


3906
int Field_longlong::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
3907
{
3908
  ASSERT_COLUMN_MARKED_FOR_WRITE;
3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924
  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
3925
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3926
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3927 3928 3929 3930 3931 3932
  {
    int8store(ptr,nr);
  }
  else
#endif
    longlongstore(ptr,nr);
3933
  return error;
unknown's avatar
unknown committed
3934 3935 3936 3937 3938
}


double Field_longlong::val_real(void)
{
3939
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
3940 3941
  longlong j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3942
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3943 3944 3945 3946 3947 3948
  {
    j=sint8korr(ptr);
  }
  else
#endif
    longlongget(j,ptr);
3949 3950 3951 3952 3953 3954 3955
  /* 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
3956 3957
}

3958

unknown's avatar
unknown committed
3959 3960
longlong Field_longlong::val_int(void)
{
3961
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
3962 3963
  longlong j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3964
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975
    j=sint8korr(ptr);
  else
#endif
    longlongget(j,ptr);
  return j;
}


String *Field_longlong::val_str(String *val_buffer,
				String *val_ptr __attribute__((unused)))
{
3976
  CHARSET_INFO *cs= &my_charset_numeric;
unknown's avatar
unknown committed
3977
  uint length;
3978 3979
  uint mlength=max(field_length+1,22*cs->mbmaxlen);
  val_buffer->alloc(mlength);
unknown's avatar
unknown committed
3980 3981 3982
  char *to=(char*) val_buffer->ptr();
  longlong j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
3983
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
3984 3985 3986 3987 3988
    j=sint8korr(ptr);
  else
#endif
    longlongget(j,ptr);

3989
  length=(uint) (cs->cset->longlong10_to_str)(cs,to,mlength,
unknown's avatar
unknown committed
3990
					unsigned_flag ? 10 : -10, j);
unknown's avatar
unknown committed
3991 3992 3993
  val_buffer->length(length);
  if (zerofill)
    prepend_zeros(val_buffer);
3994
  val_buffer->set_charset(cs);
unknown's avatar
unknown committed
3995 3996 3997 3998
  return val_buffer;
}


3999 4000
bool Field_longlong::send_binary(Protocol *protocol)
{
4001
  ASSERT_COLUMN_MARKED_FOR_READ;
4002 4003 4004 4005
  return protocol->store_longlong(Field_longlong::val_int(), unsigned_flag);
}


4006
int Field_longlong::cmp(const uchar *a_ptr, const uchar *b_ptr)
unknown's avatar
unknown committed
4007 4008 4009
{
  longlong a,b;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4010
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026
  {
    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;
}

4027
void Field_longlong::sort_string(uchar *to,uint length __attribute__((unused)))
unknown's avatar
unknown committed
4028 4029
{
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4030
  if (!table->s->db_low_byte_first)
unknown's avatar
unknown committed
4031 4032 4033 4034
  {
    if (unsigned_flag)
      to[0] = ptr[0];
    else
unknown's avatar
unknown committed
4035
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049
    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
4050
      to[0] = (char) (ptr[7] ^ 128);		/* Revers signbit */
unknown's avatar
unknown committed
4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063
    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
4064
  CHARSET_INFO *cs=res.charset();
4065
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4066
			  "bigint(%d)",(int) field_length));
unknown's avatar
unknown committed
4067 4068 4069
  add_zerofill_and_unsigned(res);
}

unknown's avatar
unknown committed
4070

4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111
/*
  Floating-point numbers
 */

uchar *
Field_real::pack(uchar *to, const uchar *from,
                 uint max_length, bool low_byte_first)
{
  DBUG_ENTER("Field_real::pack");
  DBUG_ASSERT(max_length >= pack_length());
#ifdef WORDS_BIGENDIAN
  if (low_byte_first != table->s->db_low_byte_first)
  {
    const uchar *dptr= from + pack_length();
    while (dptr-- > from)
      *to++ = *dptr;
    DBUG_RETURN(to);
  }
  else
#endif
    DBUG_RETURN(Field::pack(to, from, max_length, low_byte_first));
}

const uchar *
Field_real::unpack(uchar *to, const uchar *from,
                   uint param_data, bool low_byte_first)
{
  DBUG_ENTER("Field_real::unpack");
#ifdef WORDS_BIGENDIAN
  if (low_byte_first != table->s->db_low_byte_first)
  {
    const uchar *dptr= from + pack_length();
    while (dptr-- > from)
      *to++ = *dptr;
    DBUG_RETURN(from + pack_length());
  }
  else
#endif
    DBUG_RETURN(Field::unpack(to, from, param_data, low_byte_first));
}

unknown's avatar
unknown committed
4112
/****************************************************************************
unknown's avatar
unknown committed
4113
  single precision float
unknown's avatar
unknown committed
4114 4115
****************************************************************************/

unknown's avatar
unknown committed
4116
int Field_float::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
4117
{
unknown's avatar
unknown committed
4118
  int error;
4119
  char *end;
unknown's avatar
unknown committed
4120
  double nr= my_strntod(cs,(char*) from,len,&end,&error);
4121 4122
  if (error || (!len || ((uint) (end-from) != len &&
                table->in_use->count_cuted_fields)))
4123
  {
unknown's avatar
unknown committed
4124
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4125
                (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
unknown's avatar
unknown committed
4126
    error= error ? 1 : 2;
4127
  }
unknown's avatar
unknown committed
4128 4129
  Field_float::store(nr);
  return error;
unknown's avatar
unknown committed
4130 4131 4132
}


unknown's avatar
unknown committed
4133
int Field_float::store(double nr)
unknown's avatar
unknown committed
4134
{
4135
  ASSERT_COLUMN_MARKED_FOR_WRITE;
4136
  int error= truncate(&nr, FLT_MAX);
4137
  float j= (float)nr;
4138

unknown's avatar
unknown committed
4139
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4140
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4141 4142 4143 4144 4145
  {
    float4store(ptr,j);
  }
  else
#endif
4146
    memcpy(ptr, &j, sizeof(j));
4147
  return error;
unknown's avatar
unknown committed
4148 4149 4150
}


4151
int Field_float::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
4152
{
4153 4154
  return Field_float::store(unsigned_val ? ulonglong2double((ulonglong) nr) :
                            (double) nr);
unknown's avatar
unknown committed
4155 4156
}

4157

unknown's avatar
unknown committed
4158 4159
double Field_float::val_real(void)
{
4160
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
4161 4162
  float j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4163
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4164 4165 4166 4167 4168
  {
    float4get(j,ptr);
  }
  else
#endif
4169
    memcpy(&j, ptr, sizeof(j));
unknown's avatar
unknown committed
4170 4171 4172 4173 4174 4175 4176
  return ((double) j);
}

longlong Field_float::val_int(void)
{
  float j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4177
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4178 4179 4180 4181 4182
  {
    float4get(j,ptr);
  }
  else
#endif
4183
    memcpy(&j, ptr, sizeof(j));
4184
  return (longlong) rint(j);
unknown's avatar
unknown committed
4185 4186 4187 4188 4189 4190
}


String *Field_float::val_str(String *val_buffer,
			     String *val_ptr __attribute__((unused)))
{
4191
  ASSERT_COLUMN_MARKED_FOR_READ;
4192
  DBUG_ASSERT(!zerofill || field_length <= MAX_FIELD_CHARLENGTH);
unknown's avatar
unknown committed
4193 4194
  float nr;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4195
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4196 4197 4198 4199 4200
  {
    float4get(nr,ptr);
  }
  else
#endif
4201
    memcpy(&nr, ptr, sizeof(nr));
unknown's avatar
unknown committed
4202

4203 4204 4205 4206 4207 4208 4209
  uint to_length= 70;
  if (val_buffer->alloc(to_length))
  {
    my_error(ER_OUT_OF_RESOURCES, MYF(0));
    return val_buffer;
  }

unknown's avatar
unknown committed
4210
  char *to=(char*) val_buffer->ptr();
4211
  size_t len;
unknown's avatar
unknown committed
4212 4213

  if (dec >= NOT_FIXED_DEC)
4214
    len= my_gcvt(nr, MY_GCVT_ARG_FLOAT, to_length - 1, to, NULL);
unknown's avatar
unknown committed
4215 4216
  else
  {
4217
    /*
4218
      We are safe here because the buffer length is 70, and
4219 4220 4221 4222
      fabs(float) < 10^39, dec < NOT_FIXED_DEC. So the resulting string
      will be not longer than 69 chars + terminating '\0'.
    */
    len= my_fcvt(nr, dec, to, NULL);
unknown's avatar
unknown committed
4223
  }
4224
  val_buffer->length((uint) len);
unknown's avatar
unknown committed
4225 4226
  if (zerofill)
    prepend_zeros(val_buffer);
4227
  val_buffer->set_charset(&my_charset_numeric);
unknown's avatar
unknown committed
4228 4229 4230 4231
  return val_buffer;
}


4232
int Field_float::cmp(const uchar *a_ptr, const uchar *b_ptr)
unknown's avatar
unknown committed
4233 4234 4235
{
  float a,b;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4236
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4237 4238 4239 4240 4241 4242 4243
  {
    float4get(a,a_ptr);
    float4get(b,b_ptr);
  }
  else
#endif
  {
4244 4245
    memcpy(&a, a_ptr, sizeof(float));
    memcpy(&b, b_ptr, sizeof(float));
unknown's avatar
unknown committed
4246 4247 4248 4249 4250 4251
  }
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

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

4252
void Field_float::sort_string(uchar *to,uint length __attribute__((unused)))
unknown's avatar
unknown committed
4253 4254 4255
{
  float nr;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4256
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4257 4258 4259 4260 4261
  {
    float4get(nr,ptr);
  }
  else
#endif
4262
    memcpy(&nr, ptr, sizeof(float));
unknown's avatar
unknown committed
4263

4264
  uchar *tmp= to;
unknown's avatar
unknown committed
4265 4266 4267 4268 4269 4270 4271 4272
  if (nr == (float) 0.0)
  {						/* Change to zero string */
    tmp[0]=(uchar) 128;
    bzero((char*) tmp+1,sizeof(nr)-1);
  }
  else
  {
#ifdef WORDS_BIGENDIAN
4273
    memcpy(tmp, &nr, sizeof(nr));
unknown's avatar
unknown committed
4274 4275 4276 4277 4278 4279 4280
#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
4281
	tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
unknown's avatar
unknown committed
4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294
    }
    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;
    }
  }
}


4295 4296
bool Field_float::send_binary(Protocol *protocol)
{
4297
  ASSERT_COLUMN_MARKED_FOR_READ;
4298 4299 4300 4301
  return protocol->store((float) Field_float::val_real(), dec, (String*) 0);
}


4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317
/**
   Save the field metadata for float fields.

   Saves the pack length in the first byte.

   @param   metadata_ptr   First byte of field metadata

   @returns number of bytes written to metadata_ptr
*/
int Field_float::do_save_field_metadata(uchar *metadata_ptr)
{
  *metadata_ptr= pack_length();
  return 1;
}


unknown's avatar
unknown committed
4318 4319 4320
void Field_float::sql_type(String &res) const
{
  if (dec == NOT_FIXED_DEC)
4321
  {
4322
    res.set_ascii(STRING_WITH_LEN("float"));
4323
  }
unknown's avatar
unknown committed
4324
  else
4325
  {
4326
    CHARSET_INFO *cs= res.charset();
4327
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4328
			    "float(%d,%d)",(int) field_length,dec));
4329
  }
unknown's avatar
unknown committed
4330 4331 4332
  add_zerofill_and_unsigned(res);
}

unknown's avatar
unknown committed
4333

unknown's avatar
unknown committed
4334
/****************************************************************************
unknown's avatar
unknown committed
4335
  double precision floating point numbers
unknown's avatar
unknown committed
4336 4337
****************************************************************************/

unknown's avatar
unknown committed
4338
int Field_double::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
4339
{
unknown's avatar
unknown committed
4340
  int error;
4341
  char *end;
unknown's avatar
unknown committed
4342
  double nr= my_strntod(cs,(char*) from, len, &end, &error);
4343 4344
  if (error || (!len || ((uint) (end-from) != len &&
                table->in_use->count_cuted_fields)))
unknown's avatar
unknown committed
4345
  {
unknown's avatar
unknown committed
4346
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4347
                (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
unknown's avatar
unknown committed
4348
    error= error ? 1 : 2;
unknown's avatar
unknown committed
4349
  }
unknown's avatar
unknown committed
4350 4351
  Field_double::store(nr);
  return error;
unknown's avatar
unknown committed
4352 4353 4354
}


unknown's avatar
unknown committed
4355
int Field_double::store(double nr)
unknown's avatar
unknown committed
4356
{
4357
  ASSERT_COLUMN_MARKED_FOR_WRITE;
4358
  int error= truncate(&nr, DBL_MAX);
4359

unknown's avatar
unknown committed
4360
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4361
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4362 4363 4364 4365 4366 4367
  {
    float8store(ptr,nr);
  }
  else
#endif
    doublestore(ptr,nr);
4368
  return error;
unknown's avatar
unknown committed
4369 4370 4371
}


4372
int Field_double::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
4373
{
4374 4375
  return Field_double::store(unsigned_val ? ulonglong2double((ulonglong) nr) :
                             (double) nr);
unknown's avatar
unknown committed
4376 4377
}

4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412
/*
  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];

4413 4414 4415 4416 4417 4418
    /* Check for infinity so we don't get NaN in calculations */
    if (!my_isinf(res))
    {
      double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
      res= floor(res) + tmp;
    }
4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438
  }
  
  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;
}

4439

unknown's avatar
unknown committed
4440 4441 4442 4443 4444 4445
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
4446 4447 4448

double Field_double::val_real(void)
{
4449
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
4450 4451
  double j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4452
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463
  {
    float8get(j,ptr);
  }
  else
#endif
    doubleget(j,ptr);
  return j;
}

longlong Field_double::val_int(void)
{
4464
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
4465
  double j;
unknown's avatar
unknown committed
4466
  longlong res;
unknown's avatar
unknown committed
4467
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4468
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4469 4470 4471 4472 4473 4474
  {
    float8get(j,ptr);
  }
  else
#endif
    doubleget(j,ptr);
4475 4476
  /* Check whether we fit into longlong range */
  if (j <= (double) LONGLONG_MIN)
unknown's avatar
unknown committed
4477 4478 4479 4480
  {
    res= (longlong) LONGLONG_MIN;
    goto warn;
  }
4481
  if (j >= (double) (ulonglong) LONGLONG_MAX)
unknown's avatar
unknown committed
4482 4483 4484 4485
  {
    res= (longlong) LONGLONG_MAX;
    goto warn;
  }
4486
  return (longlong) rint(j);
unknown's avatar
unknown committed
4487 4488 4489 4490 4491 4492

warn:
  {
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
    String tmp(buf, sizeof(buf), &my_charset_latin1), *str;
    str= val_str(&tmp, 0);
4493
    ErrConvString err(str);
unknown's avatar
unknown committed
4494 4495 4496
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_TRUNCATED_WRONG_VALUE,
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
4497
                        err.ptr());
unknown's avatar
unknown committed
4498 4499
  }
  return res;
unknown's avatar
unknown committed
4500 4501 4502
}


unknown's avatar
unknown committed
4503 4504
my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
{
4505
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
4506 4507 4508 4509 4510
  double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
  return decimal_value;
}


unknown's avatar
unknown committed
4511 4512 4513
String *Field_double::val_str(String *val_buffer,
			      String *val_ptr __attribute__((unused)))
{
4514
  ASSERT_COLUMN_MARKED_FOR_READ;
4515
  DBUG_ASSERT(!zerofill || field_length <= MAX_FIELD_CHARLENGTH);
unknown's avatar
unknown committed
4516 4517
  double nr;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4518
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4519 4520 4521 4522 4523 4524
  {
    float8get(nr,ptr);
  }
  else
#endif
    doubleget(nr,ptr);
4525 4526 4527 4528 4529 4530
  uint to_length= DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE;
  if (val_buffer->alloc(to_length))
  {
    my_error(ER_OUT_OF_RESOURCES, MYF(0));
    return val_buffer;
  }
unknown's avatar
unknown committed
4531 4532

  char *to=(char*) val_buffer->ptr();
4533
  size_t len;
unknown's avatar
unknown committed
4534 4535

  if (dec >= NOT_FIXED_DEC)
4536
    len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
unknown's avatar
unknown committed
4537
  else
4538
    len= my_fcvt(nr, dec, to, NULL);
unknown's avatar
unknown committed
4539

4540
  val_buffer->length((uint) len);
unknown's avatar
unknown committed
4541 4542
  if (zerofill)
    prepend_zeros(val_buffer);
4543
  val_buffer->set_charset(&my_charset_numeric);
unknown's avatar
unknown committed
4544 4545 4546
  return val_buffer;
}

4547 4548
bool Field_double::send_binary(Protocol *protocol)
{
unknown's avatar
unknown committed
4549
  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
4550 4551
}

unknown's avatar
unknown committed
4552

4553
int Field_double::cmp(const uchar *a_ptr, const uchar *b_ptr)
unknown's avatar
unknown committed
4554 4555 4556
{
  double a,b;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4557
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4558 4559 4560 4561 4562 4563 4564
  {
    float8get(a,a_ptr);
    float8get(b,b_ptr);
  }
  else
#endif
  {
unknown's avatar
unknown committed
4565 4566
    doubleget(a, a_ptr);
    doubleget(b, b_ptr);
unknown's avatar
unknown committed
4567 4568 4569 4570 4571 4572 4573 4574 4575
  }
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}


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

/* The following should work for IEEE */

4576
void Field_double::sort_string(uchar *to,uint length __attribute__((unused)))
unknown's avatar
unknown committed
4577 4578 4579
{
  double nr;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4580
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
4581 4582 4583 4584 4585
  {
    float8get(nr,ptr);
  }
  else
#endif
unknown's avatar
unknown committed
4586
    doubleget(nr,ptr);
4587
  change_double_for_sort(nr, to);
unknown's avatar
unknown committed
4588 4589 4590
}


4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607
/**
   Save the field metadata for double fields.

   Saves the pack length in the first byte of the field metadata array
   at index of *metadata_ptr.

   @param   metadata_ptr   First byte of field metadata

   @returns number of bytes written to metadata_ptr
*/
int Field_double::do_save_field_metadata(uchar *metadata_ptr)
{
  *metadata_ptr= pack_length();
  return 1;
}


unknown's avatar
unknown committed
4608 4609
void Field_double::sql_type(String &res) const
{
unknown's avatar
unknown committed
4610
  CHARSET_INFO *cs=res.charset();
unknown's avatar
unknown committed
4611
  if (dec == NOT_FIXED_DEC)
4612
  {
4613
    res.set_ascii(STRING_WITH_LEN("double"));
4614
  }
unknown's avatar
unknown committed
4615
  else
4616
  {
4617
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4618
			    "double(%d,%d)",(int) field_length,dec));
4619
  }
unknown's avatar
unknown committed
4620 4621 4622 4623
  add_zerofill_and_unsigned(res);
}


4624 4625
/**
  TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to 
4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646
  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
4647
    field which has 0 as default value and is not automatically updated.
4648 4649 4650 4651 4652 4653 4654 4655 4656 4657
  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).

4658
  Note that TIMESTAMP_OLD_FIELDs are never created explicitly now, they are 
4659 4660 4661 4662 4663 4664 4665 4666
  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
4667

4668
Field_timestamp::Field_timestamp(uchar *ptr_arg, uint32 len_arg,
4669
                                 uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
4670 4671
				 enum utype unireg_check_arg,
				 const char *field_name_arg,
unknown's avatar
unknown committed
4672
				 TABLE_SHARE *share,
4673
				 CHARSET_INFO *cs)
4674
  :Field_str(ptr_arg, MAX_DATETIME_WIDTH, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
4675
	     unireg_check_arg, field_name_arg, cs)
unknown's avatar
unknown committed
4676
{
unknown's avatar
unknown committed
4677
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
4678
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG | BINARY_FLAG;
unknown's avatar
unknown committed
4679
  if (!share->timestamp_field && unireg_check != NONE)
unknown's avatar
unknown committed
4680
  {
4681
    /* This timestamp has auto-update */
unknown's avatar
unknown committed
4682 4683
    share->timestamp_field= this;
    flags|= TIMESTAMP_FLAG;
4684 4685
    if (unireg_check != TIMESTAMP_DN_FIELD)
      flags|= ON_UPDATE_NOW_FLAG;
unknown's avatar
unknown committed
4686 4687 4688 4689
  }
}


4690 4691
Field_timestamp::Field_timestamp(bool maybe_null_arg,
                                 const char *field_name_arg,
unknown's avatar
unknown committed
4692
                                 CHARSET_INFO *cs)
4693 4694
  :Field_str((uchar*) 0, MAX_DATETIME_WIDTH,
             maybe_null_arg ? (uchar*) "": 0, 0,
unknown's avatar
unknown committed
4695
	     NONE, field_name_arg, cs)
4696 4697
{
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
4698
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG | BINARY_FLAG;
4699 4700
    if (unireg_check != TIMESTAMP_DN_FIELD)
      flags|= ON_UPDATE_NOW_FLAG;
4701 4702 4703
}


4704
/**
4705 4706
  Get auto-set type for TIMESTAMP field.

4707 4708
  Returns value indicating during which operations this TIMESTAMP field
  should be auto-set to current timestamp.
4709
*/
4710
timestamp_auto_set_type Field_timestamp::get_auto_set_type() const
4711
{
4712 4713 4714 4715 4716 4717
  switch (unireg_check)
  {
  case TIMESTAMP_DN_FIELD:
    return TIMESTAMP_AUTO_SET_ON_INSERT;
  case TIMESTAMP_UN_FIELD:
    return TIMESTAMP_AUTO_SET_ON_UPDATE;
4718 4719
  case TIMESTAMP_OLD_FIELD:
    /*
unknown's avatar
unknown committed
4720
      Although we can have several such columns in legacy tables this
4721 4722 4723 4724 4725
      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 */
4726 4727 4728 4729 4730 4731 4732 4733 4734 4735
  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;
  }
4736 4737 4738
}


unknown's avatar
unknown committed
4739
int Field_timestamp::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
4740
{
4741
  ASSERT_COLUMN_MARKED_FOR_WRITE;
4742
  MYSQL_TIME l_time;
4743 4744 4745
  my_time_t tmp= 0;
  int error;
  bool have_smth_to_conv;
4746
  my_bool in_dst_time_gap;
unknown's avatar
unknown committed
4747
  THD *thd= table ? table->in_use : current_thd;
4748

4749
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
4750
  have_smth_to_conv= (str_to_datetime(cs, from, len, &l_time,
unknown's avatar
unknown committed
4751
                                      (thd->variables.sql_mode &
4752 4753
                                       MODE_NO_ZERO_DATE) |
                                      MODE_NO_ZERO_IN_DATE, &error) >
4754
                      MYSQL_TIMESTAMP_ERROR);
4755

unknown's avatar
unknown committed
4756 4757 4758
  if (error || !have_smth_to_conv)
  {
    error= 1;
4759
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
4760
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
unknown's avatar
unknown committed
4761
  }
4762

unknown's avatar
unknown committed
4763 4764
  /* Only convert a correct date (not a zero date) */
  if (have_smth_to_conv && l_time.month)
4765 4766 4767
  {
    if (!(tmp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
    {
4768
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4769
                           ER_WARN_DATA_OUT_OF_RANGE,
4770
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
4771 4772 4773 4774 4775
      error= 1;
    }
    else if (in_dst_time_gap)
    {
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4776
                           ER_WARN_INVALID_TIMESTAMP,
4777
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
4778 4779 4780
      error= 1;
    }
  }
4781
  store_timestamp(tmp);
4782
  return error;
unknown's avatar
unknown committed
4783 4784
}

unknown's avatar
unknown committed
4785

unknown's avatar
unknown committed
4786
int Field_timestamp::store(double nr)
unknown's avatar
unknown committed
4787
{
unknown's avatar
unknown committed
4788
  int error= 0;
unknown's avatar
unknown committed
4789 4790
  if (nr < 0 || nr > 99991231235959.0)
  {
4791 4792
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
                         ER_WARN_DATA_OUT_OF_RANGE,
4793
                         nr, MYSQL_TIMESTAMP_DATETIME);
unknown's avatar
unknown committed
4794 4795
    nr= 0;					// Avoid overflow on buff
    error= 1;
unknown's avatar
unknown committed
4796
  }
4797
  error|= Field_timestamp::store((longlong) rint(nr), FALSE);
4798
  return error;
unknown's avatar
unknown committed
4799 4800 4801
}


4802
int Field_timestamp::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
4803
{
4804
  ASSERT_COLUMN_MARKED_FOR_WRITE;
4805
  MYSQL_TIME l_time;
4806 4807
  my_time_t timestamp= 0;
  int error;
4808
  my_bool in_dst_time_gap;
unknown's avatar
unknown committed
4809
  THD *thd= table ? table->in_use : current_thd;
unknown's avatar
unknown committed
4810

4811
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
4812 4813 4814
  longlong tmp= number_to_datetime(nr, &l_time, (thd->variables.sql_mode &
                                                 MODE_NO_ZERO_DATE) |
                                   MODE_NO_ZERO_IN_DATE, &error);
4815
  if (tmp == LL(-1))
4816 4817 4818 4819 4820
  {
    error= 2;
  }

  if (!error && tmp)
unknown's avatar
unknown committed
4821
  {
4822 4823
    if (!(timestamp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
    {
4824 4825 4826
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
                           ER_WARN_DATA_OUT_OF_RANGE,
                           nr, MYSQL_TIMESTAMP_DATETIME, 1);
4827 4828 4829 4830 4831
      error= 1;
    }
    if (in_dst_time_gap)
    {
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4832 4833
                           ER_WARN_INVALID_TIMESTAMP,
                           nr, MYSQL_TIMESTAMP_DATETIME, 1);
4834 4835
      error= 1;
    }
4836 4837
  } else if (error)
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4838
                         WARN_DATA_TRUNCATED,
4839
                         nr, MYSQL_TIMESTAMP_DATETIME, 1);
4840

4841
  store_timestamp(timestamp);
unknown's avatar
unknown committed
4842
  return error;
unknown's avatar
unknown committed
4843 4844 4845 4846
}

double Field_timestamp::val_real(void)
{
4847
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
4848 4849 4850 4851 4852
  return (double) Field_timestamp::val_int();
}

longlong Field_timestamp::val_int(void)
{
4853
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
4854
  uint32 temp;
4855
  MYSQL_TIME time_tmp;
unknown's avatar
unknown committed
4856
  THD  *thd= table ? table->in_use : current_thd;
unknown's avatar
unknown committed
4857

4858
  thd->time_zone_used= 1;
unknown's avatar
unknown committed
4859
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4860
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
4861 4862 4863 4864 4865 4866 4867
    temp=uint4korr(ptr);
  else
#endif
    longget(temp,ptr);

  if (temp == 0L)				// No time
    return(0);					/* purecov: inspected */
4868 4869 4870 4871 4872 4873
  
  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
4874 4875 4876
}


unknown's avatar
unknown committed
4877
String *Field_timestamp::val_str(String *val_buffer, String *val_ptr)
unknown's avatar
unknown committed
4878
{
4879
  ASSERT_COLUMN_MARKED_FOR_READ;
4880
  uint32 temp, temp2;
4881
  MYSQL_TIME time_tmp;
unknown's avatar
unknown committed
4882
  THD *thd= table ? table->in_use : current_thd;
unknown's avatar
unknown committed
4883
  char *to;
unknown's avatar
unknown committed
4884 4885

  val_buffer->alloc(field_length+1);
unknown's avatar
unknown committed
4886
  to= (char*) val_buffer->ptr();
4887
  val_buffer->length(field_length);
unknown's avatar
unknown committed
4888

4889
  thd->time_zone_used= 1;
unknown's avatar
unknown committed
4890
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4891
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
4892 4893 4894 4895 4896 4897 4898
    temp=uint4korr(ptr);
  else
#endif
    longget(temp,ptr);

  if (temp == 0L)
  {				      /* Zero time is "000000" */
4899
    val_ptr->set(STRING_WITH_LEN("0000-00-00 00:00:00"), &my_charset_numeric);
4900
    return val_ptr;
unknown's avatar
unknown committed
4901
  }
4902
  val_buffer->set_charset(&my_charset_numeric);	// Safety
4903 4904
  
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp,(my_time_t)temp);
4905

4906
  temp= time_tmp.year % 100;
4907
  if (temp < YY_PART_YEAR - 1)
4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920
  {
    *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++= '-';
4921
  temp=time_tmp.month;
4922 4923 4924 4925
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= '-';
4926
  temp=time_tmp.day;
4927 4928 4929 4930
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ' ';
4931
  temp=time_tmp.hour;
4932 4933 4934 4935
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ':';
4936
  temp=time_tmp.minute;
4937 4938 4939 4940
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to++= ':';
4941
  temp=time_tmp.second;
4942 4943 4944 4945
  temp2=temp/10; temp=temp-temp2*10;
  *to++= (char) ('0'+(char) (temp2));
  *to++= (char) ('0'+(char) (temp));
  *to= 0;
4946
  val_buffer->set_charset(&my_charset_numeric);
unknown's avatar
unknown committed
4947 4948 4949
  return val_buffer;
}

4950

4951
bool Field_timestamp::get_date(MYSQL_TIME *ltime, uint fuzzydate)
unknown's avatar
unknown committed
4952 4953
{
  long temp;
unknown's avatar
unknown committed
4954
  THD *thd= table ? table->in_use : current_thd;
4955
  thd->time_zone_used= 1;
unknown's avatar
unknown committed
4956
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4957
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
4958 4959 4960 4961 4962 4963
    temp=uint4korr(ptr);
  else
#endif
    longget(temp,ptr);
  if (temp == 0L)
  {				      /* Zero time is "000000" */
unknown's avatar
unknown committed
4964
    if (fuzzydate & TIME_NO_ZERO_DATE)
unknown's avatar
unknown committed
4965
      return 1;
unknown's avatar
unknown committed
4966 4967 4968 4969
    bzero((char*) ltime,sizeof(*ltime));
  }
  else
  {
4970
    thd->variables.time_zone->gmt_sec_to_TIME(ltime, (my_time_t)temp);
unknown's avatar
unknown committed
4971 4972 4973 4974
  }
  return 0;
}

4975
bool Field_timestamp::get_time(MYSQL_TIME *ltime)
unknown's avatar
unknown committed
4976
{
unknown's avatar
unknown committed
4977
  return Field_timestamp::get_date(ltime,0);
unknown's avatar
unknown committed
4978 4979
}

4980 4981 4982

bool Field_timestamp::send_binary(Protocol *protocol)
{
4983
  MYSQL_TIME tm;
unknown's avatar
unknown committed
4984
  Field_timestamp::get_date(&tm, 0);
4985 4986 4987 4988
  return protocol->store(&tm);
}


4989
int Field_timestamp::cmp(const uchar *a_ptr, const uchar *b_ptr)
unknown's avatar
unknown committed
4990 4991 4992
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
4993
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006
  {
    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;
}

5007

5008
void Field_timestamp::sort_string(uchar *to,uint length __attribute__((unused)))
unknown's avatar
unknown committed
5009 5010
{
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5011
  if (!table || !table->s->db_low_byte_first)
unknown's avatar
unknown committed
5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030
  {
    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
{
5031
  res.set_ascii(STRING_WITH_LEN("timestamp"));
unknown's avatar
unknown committed
5032 5033 5034 5035 5036
}


void Field_timestamp::set_time()
{
unknown's avatar
unknown committed
5037 5038
  THD *thd= table ? table->in_use : current_thd;
  long tmp= (long) thd->query_start();
5039
  set_notnull();
5040
  store_timestamp(tmp);
unknown's avatar
unknown committed
5041 5042 5043 5044 5045 5046 5047 5048 5049
}

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

unknown's avatar
unknown committed
5050
int Field_time::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
5051
{
5052
  MYSQL_TIME ltime;
unknown's avatar
unknown committed
5053
  long tmp;
5054 5055
  int error= 0;
  int warning;
5056

5057
  if (str_to_time(cs, from, len, &ltime, &warning))
5058
  {
unknown's avatar
unknown committed
5059
    tmp=0L;
unknown's avatar
unknown committed
5060
    error= 2;
5061
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
5062
                         from, len, MYSQL_TIMESTAMP_TIME, 1);
5063
  }
unknown's avatar
unknown committed
5064 5065
  else
  {
5066
    if (warning & MYSQL_TIME_WARN_TRUNCATED)
5067 5068
    {
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5069
                           WARN_DATA_TRUNCATED,
5070
                           from, len, MYSQL_TIMESTAMP_TIME, 1);
5071 5072
      error= 1;
    }
5073
    if (warning & MYSQL_TIME_WARN_OUT_OF_RANGE)
unknown's avatar
unknown committed
5074
    {
5075 5076
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                           ER_WARN_DATA_OUT_OF_RANGE,
5077
                           from, len, MYSQL_TIMESTAMP_TIME, !error);
unknown's avatar
unknown committed
5078
      error= 1;
unknown's avatar
unknown committed
5079
    }
5080 5081 5082
    if (ltime.month)
      ltime.day=0;
    tmp=(ltime.day*24L+ltime.hour)*10000L+(ltime.minute*100+ltime.second);
unknown's avatar
unknown committed
5083
  }
5084
  
unknown's avatar
unknown committed
5085 5086
  if (ltime.neg)
    tmp= -tmp;
5087
  int3store(ptr,tmp);
5088
  return error;
unknown's avatar
unknown committed
5089 5090 5091
}


5092
int Field_time::store_time(MYSQL_TIME *ltime, timestamp_type time_type)
5093 5094 5095 5096 5097
{
  long tmp= ((ltime->month ? 0 : ltime->day * 24L) + ltime->hour) * 10000L +
            (ltime->minute * 100 + ltime->second);
  if (ltime->neg)
    tmp= -tmp;
unknown's avatar
unknown committed
5098
  return Field_time::store((longlong) tmp, FALSE);
5099 5100 5101
}


unknown's avatar
unknown committed
5102
int Field_time::store(double nr)
unknown's avatar
unknown committed
5103
{
5104
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
5105
  long tmp;
unknown's avatar
unknown committed
5106
  int error= 0;
5107
  if (nr > (double)TIME_MAX_VALUE)
unknown's avatar
unknown committed
5108
  {
5109
    tmp= TIME_MAX_VALUE;
5110
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5111
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
unknown's avatar
unknown committed
5112
    error= 1;
unknown's avatar
unknown committed
5113
  }
5114
  else if (nr < (double)-TIME_MAX_VALUE)
unknown's avatar
unknown committed
5115
  {
5116
    tmp= -TIME_MAX_VALUE;
5117
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
5118
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
unknown's avatar
unknown committed
5119
    error= 1;
unknown's avatar
unknown committed
5120 5121 5122 5123 5124 5125 5126 5127 5128
  }
  else
  {
    tmp=(long) floor(fabs(nr));			// Remove fractions
    if (nr < 0)
      tmp= -tmp;
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
    {
      tmp=0;
5129
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
5130 5131
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
                           MYSQL_TIMESTAMP_TIME);
unknown's avatar
unknown committed
5132
      error= 1;
unknown's avatar
unknown committed
5133 5134 5135
    }
  }
  int3store(ptr,tmp);
5136
  return error;
unknown's avatar
unknown committed
5137 5138 5139
}


5140
int Field_time::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
5141
{
5142
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
5143
  long tmp;
unknown's avatar
unknown committed
5144
  int error= 0;
5145
  if (nr < (longlong) -TIME_MAX_VALUE && !unsigned_val)
unknown's avatar
unknown committed
5146
  {
5147
    tmp= -TIME_MAX_VALUE;
5148
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
5149 5150
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
                         MYSQL_TIMESTAMP_TIME, 1);
unknown's avatar
unknown committed
5151
    error= 1;
unknown's avatar
unknown committed
5152
  }
5153
  else if (nr > (longlong) TIME_MAX_VALUE || (nr < 0 && unsigned_val))
unknown's avatar
unknown committed
5154
  {
5155
    tmp= TIME_MAX_VALUE;
5156
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
5157 5158
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
                         MYSQL_TIMESTAMP_TIME, 1);
unknown's avatar
unknown committed
5159
    error= 1;
unknown's avatar
unknown committed
5160 5161 5162 5163 5164 5165 5166
  }
  else
  {
    tmp=(long) nr;
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
    {
      tmp=0;
5167
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
5168 5169
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
                           MYSQL_TIMESTAMP_TIME, 1);
unknown's avatar
unknown committed
5170
      error= 1;
unknown's avatar
unknown committed
5171 5172 5173
    }
  }
  int3store(ptr,tmp);
5174
  return error;
unknown's avatar
unknown committed
5175 5176 5177 5178 5179
}


double Field_time::val_real(void)
{
5180
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
5181
  uint32 j= (uint32) uint3korr(ptr);
unknown's avatar
unknown committed
5182 5183 5184 5185 5186
  return (double) j;
}

longlong Field_time::val_int(void)
{
5187
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
5188 5189 5190
  return (longlong) sint3korr(ptr);
}

5191

5192 5193
/**
  @note
5194 5195 5196 5197
  This function is multi-byte safe as the result string is always of type
  my_charset_bin
*/

unknown's avatar
unknown committed
5198 5199 5200
String *Field_time::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
5201
  ASSERT_COLUMN_MARKED_FOR_READ;
5202
  MYSQL_TIME ltime;
5203
  val_buffer->alloc(MAX_DATE_STRING_REP_LENGTH);
unknown's avatar
unknown committed
5204
  long tmp=(long) sint3korr(ptr);
5205
  ltime.neg= 0;
unknown's avatar
unknown committed
5206 5207 5208
  if (tmp < 0)
  {
    tmp= -tmp;
5209 5210 5211 5212 5213 5214
    ltime.neg= 1;
  }
  ltime.day= (uint) 0;
  ltime.hour= (uint) (tmp/10000);
  ltime.minute= (uint) (tmp/100 % 100);
  ltime.second= (uint) (tmp % 100);
5215
  make_time((DATE_TIME_FORMAT*) 0, &ltime, val_buffer);
5216
  val_buffer->set_charset(&my_charset_numeric);
unknown's avatar
unknown committed
5217 5218 5219
  return val_buffer;
}

5220

5221 5222
/**
  @note
unknown's avatar
unknown committed
5223
  Normally we would not consider 'time' as a valid date, but we allow
unknown's avatar
unknown committed
5224 5225 5226 5227
  get_date() here to be able to do things like
  DATE_FORMAT(time, "%l.%i %p")
*/
 
5228
bool Field_time::get_date(MYSQL_TIME *ltime, uint fuzzydate)
5229
{
unknown's avatar
unknown committed
5230
  THD *thd= table ? table->in_use : current_thd;
unknown's avatar
unknown committed
5231
  if (!(fuzzydate & TIME_FUZZY_DATE))
unknown's avatar
unknown committed
5232
  {
unknown's avatar
unknown committed
5233
    push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
5234 5235
                        ER_WARN_DATA_OUT_OF_RANGE,
                        ER(ER_WARN_DATA_OUT_OF_RANGE), field_name,
Marc Alff's avatar
Marc Alff committed
5236
                        thd->warning_info->current_row_for_warning());
unknown's avatar
unknown committed
5237 5238
    return 1;
  }
5239
  return Field_time::get_time(ltime);
5240 5241
}

unknown's avatar
unknown committed
5242

5243
bool Field_time::get_time(MYSQL_TIME *ltime)
unknown's avatar
unknown committed
5244 5245 5246 5247 5248 5249 5250 5251
{
  long tmp=(long) sint3korr(ptr);
  ltime->neg=0;
  if (tmp < 0)
  {
    ltime->neg= 1;
    tmp=-tmp;
  }
5252
  ltime->year= ltime->month= ltime->day= 0;
unknown's avatar
unknown committed
5253
  ltime->hour=   (int) (tmp/10000);
unknown's avatar
unknown committed
5254
  tmp-=ltime->hour*10000;
unknown's avatar
unknown committed
5255 5256
  ltime->minute= (int) tmp/100;
  ltime->second= (int) tmp % 100;
unknown's avatar
unknown committed
5257
  ltime->second_part=0;
5258
  ltime->time_type= MYSQL_TIMESTAMP_TIME;
unknown's avatar
unknown committed
5259 5260 5261
  return 0;
}

5262 5263 5264

bool Field_time::send_binary(Protocol *protocol)
{
5265
  MYSQL_TIME tm;
5266
  Field_time::get_time(&tm);
5267 5268
  tm.day= tm.hour/24;				// Move hours to days
  tm.hour-= tm.day*24;
unknown's avatar
unknown committed
5269
  return protocol->store_time(&tm);
5270 5271 5272
}


5273
int Field_time::cmp(const uchar *a_ptr, const uchar *b_ptr)
unknown's avatar
unknown committed
5274
{
unknown's avatar
unknown committed
5275 5276 5277
  int32 a,b;
  a=(int32) sint3korr(a_ptr);
  b=(int32) sint3korr(b_ptr);
unknown's avatar
unknown committed
5278 5279 5280
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

5281
void Field_time::sort_string(uchar *to,uint length __attribute__((unused)))
unknown's avatar
unknown committed
5282 5283 5284 5285 5286 5287 5288 5289
{
  to[0] = (uchar) (ptr[2] ^ 128);
  to[1] = ptr[1];
  to[2] = ptr[0];
}

void Field_time::sql_type(String &res) const
{
5290
  res.set_ascii(STRING_WITH_LEN("time"));
unknown's avatar
unknown committed
5291 5292 5293 5294 5295 5296 5297 5298
}

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

unknown's avatar
unknown committed
5299
int Field_year::store(const char *from, uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
5300
{
5301
  ASSERT_COLUMN_MARKED_FOR_WRITE;
5302
  char *end;
unknown's avatar
unknown committed
5303
  int error;
unknown's avatar
unknown committed
5304
  longlong nr= cs->cset->strntoull10rnd(cs, from, len, 0, &end, &error);
unknown's avatar
unknown committed
5305

5306
  if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155 ||
unknown's avatar
unknown committed
5307
      error == MY_ERRNO_ERANGE)
unknown's avatar
unknown committed
5308 5309
  {
    *ptr=0;
5310
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
5311
    return 1;
unknown's avatar
unknown committed
5312
  }
unknown's avatar
unknown committed
5313 5314 5315 5316 5317 5318 5319 5320
  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
5321
    error= 1;
unknown's avatar
unknown committed
5322
  }
unknown's avatar
unknown committed
5323

unknown's avatar
unknown committed
5324 5325 5326 5327 5328 5329 5330
  if (nr != 0 || len != 4)
  {
    if (nr < YY_PART_YEAR)
      nr+=100;					// 2000 - 2069
    else if (nr > 1900)
      nr-= 1900;
  }
unknown's avatar
unknown committed
5331
  *ptr= (char) (uchar) nr;
unknown's avatar
unknown committed
5332
  return error;
unknown's avatar
unknown committed
5333 5334
}

unknown's avatar
unknown committed
5335

unknown's avatar
unknown committed
5336
int Field_year::store(double nr)
unknown's avatar
unknown committed
5337 5338
{
  if (nr < 0.0 || nr >= 2155.0)
5339
  {
5340
    (void) Field_year::store((longlong) -1, FALSE);
5341 5342
    return 1;
  }
5343
  return Field_year::store((longlong) nr, FALSE);
unknown's avatar
unknown committed
5344 5345
}

5346 5347

int Field_year::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
5348
{
5349
  ASSERT_COLUMN_MARKED_FOR_WRITE;
5350
  if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155)
unknown's avatar
unknown committed
5351
  {
5352
    *ptr= 0;
5353
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
5354
    return 1;
unknown's avatar
unknown committed
5355 5356 5357 5358 5359 5360 5361 5362
  }
  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;
  }
unknown's avatar
unknown committed
5363
  *ptr= (char) (uchar) nr;
5364
  return 0;
unknown's avatar
unknown committed
5365 5366
}

5367

unknown's avatar
unknown committed
5368 5369
bool Field_year::send_binary(Protocol *protocol)
{
5370
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
5371
  ulonglong tmp= Field_year::val_int();
5372
  return protocol->store_short(tmp);
unknown's avatar
unknown committed
5373
}
unknown's avatar
unknown committed
5374

5375

unknown's avatar
unknown committed
5376 5377 5378 5379 5380
double Field_year::val_real(void)
{
  return (double) Field_year::val_int();
}

5381

unknown's avatar
unknown committed
5382 5383
longlong Field_year::val_int(void)
{
5384
  ASSERT_COLUMN_MARKED_FOR_READ;
5385
  int tmp= (int) ptr[0];
unknown's avatar
unknown committed
5386 5387 5388 5389 5390 5391 5392
  if (field_length != 4)
    tmp%=100;					// Return last 2 char
  else if (tmp)
    tmp+=1900;
  return (longlong) tmp;
}

5393

unknown's avatar
unknown committed
5394 5395 5396 5397 5398 5399 5400
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());
5401
  val_buffer->set_charset(&my_charset_numeric);
unknown's avatar
unknown committed
5402 5403 5404
  return val_buffer;
}

5405

unknown's avatar
unknown committed
5406 5407
void Field_year::sql_type(String &res) const
{
unknown's avatar
unknown committed
5408
  CHARSET_INFO *cs=res.charset();
5409
  res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(),
5410
			  "year(%d)",(int) field_length));
unknown's avatar
unknown committed
5411 5412 5413 5414 5415 5416 5417 5418 5419 5420
}


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

unknown's avatar
unknown committed
5421
int Field_date::store(const char *from, uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
5422
{
5423
  ASSERT_COLUMN_MARKED_FOR_WRITE;
5424
  MYSQL_TIME l_time;
unknown's avatar
unknown committed
5425
  uint32 tmp;
5426
  int error;
unknown's avatar
unknown committed
5427
  THD *thd= table ? table->in_use : current_thd;
5428

5429
  if (str_to_datetime(cs, from, len, &l_time, TIME_FUZZY_DATE |
unknown's avatar
unknown committed
5430
                      (thd->variables.sql_mode &
unknown's avatar
unknown committed
5431 5432 5433
                       (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
                        MODE_INVALID_DATES)),
                      &error) <= MYSQL_TIMESTAMP_ERROR)
5434
  {
5435
    tmp= 0;
unknown's avatar
unknown committed
5436
    error= 2;
5437
  }
unknown's avatar
unknown committed
5438
  else
unknown's avatar
unknown committed
5439
    tmp=(uint32) l_time.year*10000L + (uint32) (l_time.month*100+l_time.day);
5440 5441

  if (error)
5442
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
5443
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
5444

unknown's avatar
unknown committed
5445
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5446
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5447 5448 5449 5450 5451 5452
  {
    int4store(ptr,tmp);
  }
  else
#endif
    longstore(ptr,tmp);
5453
  return error;
unknown's avatar
unknown committed
5454 5455 5456
}


unknown's avatar
unknown committed
5457
int Field_date::store(double nr)
unknown's avatar
unknown committed
5458
{
5459
  longlong tmp;
unknown's avatar
unknown committed
5460 5461 5462 5463
  if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
    nr=floor(nr/1000000.0);			// Timestamp to date
  if (nr < 0.0 || nr > 99991231.0)
  {
5464
    tmp= LL(0);
5465 5466
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
                         ER_WARN_DATA_OUT_OF_RANGE,
5467
                         nr, MYSQL_TIMESTAMP_DATE);
unknown's avatar
unknown committed
5468 5469
  }
  else
5470
    tmp= (longlong) rint(nr);
5471

5472
  return Field_date::store(tmp, TRUE);
unknown's avatar
unknown committed
5473 5474 5475
}


5476
int Field_date::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
5477
{
5478
  ASSERT_COLUMN_MARKED_FOR_WRITE;
5479
  MYSQL_TIME not_used;
5480 5481
  int error;
  longlong initial_nr= nr;
unknown's avatar
unknown committed
5482
  THD *thd= table ? table->in_use : current_thd;
5483 5484

  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
unknown's avatar
unknown committed
5485
                                         (thd->variables.sql_mode &
5486 5487 5488 5489
                                          (MODE_NO_ZERO_IN_DATE |
                                           MODE_NO_ZERO_DATE |
                                           MODE_INVALID_DATES))), &error);

5490
  if (nr == LL(-1))
unknown's avatar
unknown committed
5491
  {
5492 5493
    nr= 0;
    error= 2;
unknown's avatar
unknown committed
5494
  }
5495

5496
  if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
5497
    nr= (longlong) floor(nr/1000000.0);         // Timestamp to date
5498

5499 5500 5501 5502 5503
  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);
5504

unknown's avatar
unknown committed
5505
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5506
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5507
  {
5508
    int4store(ptr, nr);
unknown's avatar
unknown committed
5509 5510 5511
  }
  else
#endif
5512
    longstore(ptr, nr);
5513
  return error;
unknown's avatar
unknown committed
5514 5515 5516
}


unknown's avatar
unknown committed
5517 5518 5519
bool Field_date::send_binary(Protocol *protocol)
{
  longlong tmp= Field_date::val_int();
5520
  MYSQL_TIME tm;
unknown's avatar
unknown committed
5521 5522 5523 5524 5525 5526
  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
5527 5528 5529

double Field_date::val_real(void)
{
5530
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
5531 5532
  int32 j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5533
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5534 5535 5536 5537 5538 5539 5540
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);
  return (double) (uint32) j;
}

5541

unknown's avatar
unknown committed
5542 5543
longlong Field_date::val_int(void)
{
5544
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
5545 5546
  int32 j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5547
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5548 5549 5550 5551 5552 5553 5554
    j=sint4korr(ptr);
  else
#endif
    longget(j,ptr);
  return (longlong) (uint32) j;
}

5555

unknown's avatar
unknown committed
5556 5557 5558
String *Field_date::val_str(String *val_buffer,
			    String *val_ptr __attribute__((unused)))
{
5559
  ASSERT_COLUMN_MARKED_FOR_READ;
5560
  MYSQL_TIME ltime;
unknown's avatar
unknown committed
5561 5562 5563
  val_buffer->alloc(field_length);
  int32 tmp;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5564
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5565 5566 5567 5568
    tmp=sint4korr(ptr);
  else
#endif
    longget(tmp,ptr);
5569 5570 5571 5572
  ltime.neg= 0;
  ltime.year= (int) ((uint32) tmp/10000L % 10000);
  ltime.month= (int) ((uint32) tmp/100 % 100);
  ltime.day= (int) ((uint32) tmp % 100);
5573
  make_date((DATE_TIME_FORMAT *) 0, &ltime, val_buffer);
5574
  val_buffer->set_charset(&my_charset_numeric);
unknown's avatar
unknown committed
5575 5576 5577
  return val_buffer;
}

5578

5579 5580 5581 5582 5583 5584 5585
bool Field_date::get_time(MYSQL_TIME *ltime)
{
  bzero((char *)ltime, sizeof(MYSQL_TIME));
  return 0;
}


5586
int Field_date::cmp(const uchar *a_ptr, const uchar *b_ptr)
unknown's avatar
unknown committed
5587 5588 5589
{
  int32 a,b;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5590
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604
  {
    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;
}


5605
void Field_date::sort_string(uchar *to,uint length __attribute__((unused)))
unknown's avatar
unknown committed
5606 5607
{
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5608
  if (!table || !table->s->db_low_byte_first)
unknown's avatar
unknown committed
5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626
  {
    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
{
5627
  res.set_ascii(STRING_WITH_LEN("date"));
unknown's avatar
unknown committed
5628 5629
}

5630

unknown's avatar
unknown committed
5631 5632 5633 5634 5635 5636
/****************************************************************************
** The new date type
** This is identical to the old date type, but stored on 3 bytes instead of 4
** In number context: YYYYMMDD
****************************************************************************/

5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650
/*
  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)
5651 5652 5653
       This is used by opt_range.cc:get_mm_leaf(). Note that there is a
       nearly-identical class Field_date doesn't ever return 3 from its
       store function.
5654 5655
*/

unknown's avatar
unknown committed
5656
int Field_newdate::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
5657
{
5658
  ASSERT_COLUMN_MARKED_FOR_WRITE;
5659 5660
  long tmp;
  MYSQL_TIME l_time;
5661
  int error;
unknown's avatar
unknown committed
5662
  THD *thd= table ? table->in_use : current_thd;
5663
  enum enum_mysql_timestamp_type ret;
5664
  if ((ret= str_to_datetime(cs, from, len, &l_time,
5665 5666 5667 5668 5669 5670
                            (TIME_FUZZY_DATE |
                             (thd->variables.sql_mode &
                              (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
                               MODE_INVALID_DATES))),
                            &error)) <= MYSQL_TIMESTAMP_ERROR)
  {
5671
    tmp= 0;
unknown's avatar
unknown committed
5672
    error= 2;
5673
  }
unknown's avatar
unknown committed
5674
  else
5675
  {
5676
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
5677
    if (!error && (ret != MYSQL_TIMESTAMP_DATE) &&
5678
        (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
5679
      error= 3;                                 // Datetime was cut (note)
5680
  }
5681 5682

  if (error)
5683 5684 5685
    set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
                         MYSQL_ERROR::WARN_LEVEL_WARN,
                         WARN_DATA_TRUNCATED,
5686
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
5687

5688
  int3store(ptr, tmp);
5689
  return error;
unknown's avatar
unknown committed
5690 5691
}

5692

unknown's avatar
unknown committed
5693
int Field_newdate::store(double nr)
unknown's avatar
unknown committed
5694 5695
{
  if (nr < 0.0 || nr > 99991231235959.0)
5696
  {
5697
    int3store(ptr,(int32) 0);
5698
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5699
                         WARN_DATA_TRUNCATED, nr, MYSQL_TIMESTAMP_DATE);
5700 5701
    return 1;
  }
5702
  return Field_newdate::store((longlong) rint(nr), FALSE);
unknown's avatar
unknown committed
5703 5704 5705
}


5706
int Field_newdate::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
5707
{
5708
  ASSERT_COLUMN_MARKED_FOR_WRITE;
5709
  MYSQL_TIME l_time;
5710
  longlong tmp;
5711
  int error;
unknown's avatar
unknown committed
5712
  THD *thd= table ? table->in_use : current_thd;
5713 5714
  if (number_to_datetime(nr, &l_time,
                         (TIME_FUZZY_DATE |
unknown's avatar
unknown committed
5715
                          (thd->variables.sql_mode &
5716 5717 5718
                           (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
                            MODE_INVALID_DATES))),
                         &error) == LL(-1))
5719 5720 5721
  {
    tmp= 0L;
    error= 2;
unknown's avatar
unknown committed
5722 5723
  }
  else
5724
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
5725

5726 5727 5728 5729
  if (!error && l_time.time_type != MYSQL_TIMESTAMP_DATE &&
      (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
    error= 3;

5730
  if (error)
5731 5732 5733 5734 5735
    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);
5736 5737

  int3store(ptr,tmp);
5738
  return error;
unknown's avatar
unknown committed
5739 5740
}

5741

5742
int Field_newdate::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
unknown's avatar
unknown committed
5743
{
5744
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
5745
  long tmp;
5746
  int error= 0;
5747 5748
  if (time_type == MYSQL_TIMESTAMP_DATE ||
      time_type == MYSQL_TIMESTAMP_DATETIME)
5749
  {
unknown's avatar
unknown committed
5750
    tmp=ltime->year*16*32+ltime->month*32+ltime->day;
5751 5752 5753 5754 5755
    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))
5756
    {
5757
      char buff[MAX_DATE_STRING_REP_LENGTH];
5758
      String str(buff, sizeof(buff), &my_charset_latin1);
5759
      tmp= 0;
5760 5761 5762 5763
      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);
    }
5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774
    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;
    }
5775
  }
unknown's avatar
unknown committed
5776 5777 5778
  else
  {
    tmp=0;
5779
    error= 1;
5780
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
5781 5782
  }
  int3store(ptr,tmp);
5783
  return error;
unknown's avatar
unknown committed
5784 5785
}

5786

unknown's avatar
unknown committed
5787 5788
bool Field_newdate::send_binary(Protocol *protocol)
{
5789
  MYSQL_TIME tm;
unknown's avatar
unknown committed
5790 5791 5792
  Field_newdate::get_date(&tm,0);
  return protocol->store_date(&tm);
}
unknown's avatar
unknown committed
5793

5794

unknown's avatar
unknown committed
5795 5796
double Field_newdate::val_real(void)
{
5797
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
5798 5799 5800
  return (double) Field_newdate::val_int();
}

5801

unknown's avatar
unknown committed
5802 5803
longlong Field_newdate::val_int(void)
{
5804
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
5805
  ulong j= uint3korr(ptr);
unknown's avatar
unknown committed
5806 5807 5808 5809
  j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
  return (longlong) j;
}

5810

unknown's avatar
unknown committed
5811 5812 5813
String *Field_newdate::val_str(String *val_buffer,
			       String *val_ptr __attribute__((unused)))
{
5814
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
5815 5816
  val_buffer->alloc(field_length);
  val_buffer->length(field_length);
unknown's avatar
unknown committed
5817
  uint32 tmp=(uint32) uint3korr(ptr);
unknown's avatar
unknown committed
5818 5819 5820 5821
  int part;
  char *pos=(char*) val_buffer->ptr()+10;

  /* Open coded to get more speed */
unknown's avatar
unknown committed
5822
  *pos--=0;					// End NULL
unknown's avatar
unknown committed
5823
  part=(int) (tmp & 31);
unknown's avatar
unknown committed
5824 5825 5826
  *pos--= (char) ('0'+part%10);
  *pos--= (char) ('0'+part/10);
  *pos--= '-';
unknown's avatar
unknown committed
5827
  part=(int) (tmp >> 5 & 15);
unknown's avatar
unknown committed
5828 5829 5830
  *pos--= (char) ('0'+part%10);
  *pos--= (char) ('0'+part/10);
  *pos--= '-';
unknown's avatar
unknown committed
5831
  part=(int) (tmp >> 9);
unknown's avatar
unknown committed
5832 5833 5834 5835
  *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);
5836
  val_buffer->set_charset(&my_charset_numeric);
unknown's avatar
unknown committed
5837 5838 5839
  return val_buffer;
}

5840

5841
bool Field_newdate::get_date(MYSQL_TIME *ltime,uint fuzzydate)
unknown's avatar
unknown committed
5842
{
unknown's avatar
unknown committed
5843
  uint32 tmp=(uint32) uint3korr(ptr);
unknown's avatar
unknown committed
5844 5845 5846
  ltime->day=   tmp & 31;
  ltime->month= (tmp >> 5) & 15;
  ltime->year=  (tmp >> 9);
5847
  ltime->time_type= MYSQL_TIMESTAMP_DATE;
5848
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
unknown's avatar
unknown committed
5849 5850
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ?
          1 : 0);
unknown's avatar
unknown committed
5851 5852
}

5853

5854
bool Field_newdate::get_time(MYSQL_TIME *ltime)
unknown's avatar
unknown committed
5855
{
unknown's avatar
unknown committed
5856
  return Field_newdate::get_date(ltime,0);
unknown's avatar
unknown committed
5857 5858
}

5859

5860
int Field_newdate::cmp(const uchar *a_ptr, const uchar *b_ptr)
unknown's avatar
unknown committed
5861
{
unknown's avatar
unknown committed
5862 5863 5864
  uint32 a,b;
  a=(uint32) uint3korr(a_ptr);
  b=(uint32) uint3korr(b_ptr);
unknown's avatar
unknown committed
5865 5866 5867
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

5868

5869
void Field_newdate::sort_string(uchar *to,uint length __attribute__((unused)))
unknown's avatar
unknown committed
5870 5871 5872 5873 5874 5875
{
  to[0] = ptr[2];
  to[1] = ptr[1];
  to[2] = ptr[0];
}

5876

unknown's avatar
unknown committed
5877 5878
void Field_newdate::sql_type(String &res) const
{
5879
  res.set_ascii(STRING_WITH_LEN("date"));
unknown's avatar
unknown committed
5880 5881 5882 5883 5884 5885 5886 5887 5888 5889
}


/****************************************************************************
** 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
5890
int Field_datetime::store(const char *from,uint len,CHARSET_INFO *cs)
unknown's avatar
unknown committed
5891
{
5892
  ASSERT_COLUMN_MARKED_FOR_WRITE;
5893
  MYSQL_TIME time_tmp;
5894 5895
  int error;
  ulonglong tmp= 0;
5896
  enum enum_mysql_timestamp_type func_res;
unknown's avatar
unknown committed
5897
  THD *thd= table ? table->in_use : current_thd;
5898

5899
  func_res= str_to_datetime(cs, from, len, &time_tmp,
5900
                            (TIME_FUZZY_DATE |
unknown's avatar
unknown committed
5901
                             (thd->variables.sql_mode &
5902 5903 5904 5905
                              (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
                               MODE_INVALID_DATES))),
                            &error);
  if ((int) func_res > (int) MYSQL_TIMESTAMP_ERROR)
5906
    tmp= TIME_to_ulonglong_datetime(&time_tmp);
5907 5908 5909
  else
    error= 1;                                 // Fix if invalid zero date

5910
  if (error)
5911
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5912
                         ER_WARN_DATA_OUT_OF_RANGE,
5913
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
5914

unknown's avatar
unknown committed
5915
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5916
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5917 5918 5919 5920 5921 5922
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
5923
  return error;
unknown's avatar
unknown committed
5924 5925 5926
}


unknown's avatar
unknown committed
5927
int Field_datetime::store(double nr)
unknown's avatar
unknown committed
5928
{
unknown's avatar
unknown committed
5929
  int error= 0;
unknown's avatar
unknown committed
5930 5931
  if (nr < 0.0 || nr > 99991231235959.0)
  {
5932 5933
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
                         ER_WARN_DATA_OUT_OF_RANGE,
5934
                         nr, MYSQL_TIMESTAMP_DATETIME);
5935
    nr= 0.0;
unknown's avatar
unknown committed
5936
    error= 1;
unknown's avatar
unknown committed
5937
  }
5938
  error|= Field_datetime::store((longlong) rint(nr), FALSE);
5939
  return error;
unknown's avatar
unknown committed
5940 5941 5942
}


5943
int Field_datetime::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
5944
{
5945
  ASSERT_COLUMN_MARKED_FOR_WRITE;
5946
  MYSQL_TIME not_used;
5947 5948
  int error;
  longlong initial_nr= nr;
unknown's avatar
unknown committed
5949
  THD *thd= table ? table->in_use : current_thd;
5950

5951
  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
unknown's avatar
unknown committed
5952
                                         (thd->variables.sql_mode &
5953 5954 5955 5956
                                          (MODE_NO_ZERO_IN_DATE |
                                           MODE_NO_ZERO_DATE |
                                           MODE_INVALID_DATES))), &error);

5957
  if (nr == LL(-1))
5958
  {
5959 5960
    nr= 0;
    error= 2;
5961
  }
5962

5963 5964 5965 5966 5967 5968
  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
5969
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
5970
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
5971 5972 5973 5974 5975 5976
  {
    int8store(ptr,nr);
  }
  else
#endif
    longlongstore(ptr,nr);
5977
  return error;
unknown's avatar
unknown committed
5978 5979
}

unknown's avatar
unknown committed
5980

5981
int Field_datetime::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
unknown's avatar
unknown committed
5982
{
5983
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
5984
  longlong tmp;
5985
  int error= 0;
5986 5987 5988 5989
  /*
    We don't perform range checking here since values stored in TIME
    structure always fit into DATETIME range.
  */
5990 5991
  if (time_type == MYSQL_TIMESTAMP_DATE ||
      time_type == MYSQL_TIMESTAMP_DATETIME)
5992
  {
unknown's avatar
unknown committed
5993 5994
    tmp=((ltime->year*10000L+ltime->month*100+ltime->day)*LL(1000000)+
	 (ltime->hour*10000L+ltime->minute*100+ltime->second));
5995 5996 5997 5998 5999
    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))
6000
    {
6001
      char buff[MAX_DATE_STRING_REP_LENGTH];
6002
      String str(buff, sizeof(buff), &my_charset_latin1);
6003
      tmp= 0;
6004 6005 6006 6007 6008
      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
6009 6010 6011
  else
  {
    tmp=0;
6012
    error= 1;
6013
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
6014 6015
  }
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
6016
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
6017 6018 6019 6020 6021 6022
  {
    int8store(ptr,tmp);
  }
  else
#endif
    longlongstore(ptr,tmp);
6023
  return error;
unknown's avatar
unknown committed
6024 6025
}

unknown's avatar
unknown committed
6026 6027
bool Field_datetime::send_binary(Protocol *protocol)
{
6028
  MYSQL_TIME tm;
6029
  Field_datetime::get_date(&tm, TIME_FUZZY_DATE);
unknown's avatar
unknown committed
6030 6031 6032
  return protocol->store(&tm);
}

unknown's avatar
unknown committed
6033 6034 6035 6036 6037 6038 6039 6040

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

longlong Field_datetime::val_int(void)
{
6041
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
6042 6043
  longlong j;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
6044
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055
    j=sint8korr(ptr);
  else
#endif
    longlongget(j,ptr);
  return j;
}


String *Field_datetime::val_str(String *val_buffer,
				String *val_ptr __attribute__((unused)))
{
6056
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
6057 6058 6059 6060 6061 6062 6063 6064
  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
6065
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
6066 6067 6068 6069 6070 6071
    tmp=sint8korr(ptr);
  else
#endif
    longlongget(tmp,ptr);

  /*
unknown's avatar
unknown committed
6072
    Avoid problem with slow longlong arithmetic and sprintf
unknown's avatar
unknown committed
6073 6074 6075 6076 6077
  */

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

6078
  pos=(char*) val_buffer->ptr() + MAX_DATETIME_WIDTH;
unknown's avatar
unknown committed
6079
  *pos--=0;
unknown's avatar
unknown committed
6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098
  *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);
6099
  val_buffer->set_charset(&my_charset_numeric);
unknown's avatar
unknown committed
6100 6101 6102
  return val_buffer;
}

6103
bool Field_datetime::get_date(MYSQL_TIME *ltime, uint fuzzydate)
unknown's avatar
unknown committed
6104 6105
{
  longlong tmp=Field_datetime::val_int();
unknown's avatar
unknown committed
6106 6107 6108
  uint32 part1,part2;
  part1=(uint32) (tmp/LL(1000000));
  part2=(uint32) (tmp - (ulonglong) part1*LL(1000000));
unknown's avatar
unknown committed
6109

6110
  ltime->time_type=	MYSQL_TIMESTAMP_DATETIME;
unknown's avatar
unknown committed
6111 6112 6113 6114 6115 6116 6117 6118
  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
6119
  return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ? 1 : 0;
unknown's avatar
unknown committed
6120 6121
}

6122
bool Field_datetime::get_time(MYSQL_TIME *ltime)
unknown's avatar
unknown committed
6123
{
unknown's avatar
unknown committed
6124
  return Field_datetime::get_date(ltime,0);
unknown's avatar
unknown committed
6125 6126
}

6127
int Field_datetime::cmp(const uchar *a_ptr, const uchar *b_ptr)
unknown's avatar
unknown committed
6128 6129 6130
{
  longlong a,b;
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
6131
  if (table && table->s->db_low_byte_first)
unknown's avatar
unknown committed
6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145
  {
    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;
}

6146
void Field_datetime::sort_string(uchar *to,uint length __attribute__((unused)))
unknown's avatar
unknown committed
6147 6148
{
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
6149
  if (!table || !table->s->db_low_byte_first)
unknown's avatar
unknown committed
6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176
  {
    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
{
6177
  res.set_ascii(STRING_WITH_LEN("datetime"));
unknown's avatar
unknown committed
6178 6179 6180 6181 6182 6183 6184
}

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

6185 6186 6187 6188 6189 6190 6191 6192 6193 6194
/*
  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
6195
    cs                       - character set of the string
6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214

  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,
6215 6216
                        const char *end,
                        CHARSET_INFO *cs)
6217
{
6218 6219
  const char *pos;
  char tmp[32];
Marc Alff's avatar
Marc Alff committed
6220 6221
  THD *thd= field->table->in_use;

6222 6223 6224 6225
  if (!(pos= well_formed_error_pos) &&
      !(pos= cannot_convert_error_pos))
    return FALSE;

6226
  convert_to_printable(tmp, sizeof(tmp), pos, (end - pos), cs, 6);
6227

Marc Alff's avatar
Marc Alff committed
6228
  push_warning_printf(thd,
6229
                      MYSQL_ERROR::WARN_LEVEL_WARN,
Marc Alff's avatar
Marc Alff committed
6230
                      ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
6231 6232
                      ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
                      "string", tmp, field->field_name,
Marc Alff's avatar
Marc Alff committed
6233
                      thd->warning_info->current_row_for_warning());
6234 6235 6236 6237 6238
  return TRUE;
}


/*
unknown's avatar
unknown committed
6239
  Check if we lost any important data and send a truncation error/warning
6240 6241

  SYNOPSIS
unknown's avatar
unknown committed
6242
    Field_longstr::report_if_important_data()
6243
    pstr                     - Truncated rest of string
unknown's avatar
unknown committed
6244
    end                      - End of truncated string
6245
    count_spaces             - Treat traling spaces as important data
6246

unknown's avatar
unknown committed
6247 6248 6249 6250 6251 6252 6253 6254
  RETURN VALUES
    0   - None was truncated (or we don't count cut fields)
    2   - Some bytes was truncated

  NOTE
    Check if we lost any important data (anything in a binary string,
    or any non-space in others). If only trailing spaces was lost,
    send a truncation note, otherwise send a truncation error.
6255
    Silently ignore traling spaces if the count_space parameter is FALSE.
6256 6257
*/

unknown's avatar
unknown committed
6258
int
6259
Field_longstr::report_if_important_data(const char *pstr, const char *end,
6260
                                        bool count_spaces)
6261
{
6262
  if ((pstr < end) && table->in_use->count_cuted_fields)
unknown's avatar
unknown committed
6263
  {
6264
    if (test_if_important_data(field_charset, pstr, end))
unknown's avatar
unknown committed
6265 6266
    {
      if (table->in_use->abort_on_warning)
Marc Alff's avatar
Marc Alff committed
6267
        set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_DATA_TOO_LONG, 1);
unknown's avatar
unknown committed
6268 6269
      else
        set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
6270
      return 2;
unknown's avatar
unknown committed
6271
    }
6272 6273
    else if (count_spaces)
    { /* If we lost only spaces then produce a NOTE, not a WARNING */
unknown's avatar
unknown committed
6274
      set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
6275 6276
      return 2;
    }
unknown's avatar
unknown committed
6277 6278
  }
  return 0;
6279 6280 6281
}


unknown's avatar
unknown committed
6282 6283
	/* Copy a string and fill with space */

unknown's avatar
unknown committed
6284
int Field_string::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
6285
{
6286
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
6287
  uint copy_length;
6288 6289 6290
  const char *well_formed_error_pos;
  const char *cannot_convert_error_pos;
  const char *from_end_pos;
6291

6292 6293
  /* See the comment for Field_long::store(long long) */
  DBUG_ASSERT(table->in_use == current_thd);
6294

6295
  copy_length= well_formed_copy_nchars(field_charset,
6296
                                       (char*) ptr, field_length,
6297 6298 6299 6300 6301
                                       cs, from, length,
                                       field_length / field_charset->mbmaxlen,
                                       &well_formed_error_pos,
                                       &cannot_convert_error_pos,
                                       &from_end_pos);
6302 6303 6304

  /* Append spaces if the string was shorter than the field. */
  if (copy_length < field_length)
6305
    field_charset->cset->fill(field_charset,(char*) ptr+copy_length,
6306
                              field_length-copy_length,
6307
                              field_charset->pad_char);
6308

6309
  if (check_string_copy_error(this, well_formed_error_pos,
6310
                              cannot_convert_error_pos, from + length, cs))
6311 6312
    return 2;

6313
  return report_if_important_data(from_end_pos, from + length, FALSE);
unknown's avatar
unknown committed
6314 6315 6316
}


6317
/**
6318 6319
  Store double value in Field_string or Field_varstring.

6320
  Pretty prints double number into field_length characters buffer.
6321

6322
  @param nr            number
6323 6324
*/

unknown's avatar
unknown committed
6325
int Field_str::store(double nr)
unknown's avatar
unknown committed
6326
{
6327
  ASSERT_COLUMN_MARKED_FOR_WRITE;
6328
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
6329
  uint local_char_length= field_length / charset()->mbmaxlen;
6330 6331
  size_t length;
  my_bool error;
6332

6333 6334
  length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
  if (error)
6335
  {
6336 6337 6338 6339
    if (table->in_use->abort_on_warning)
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_DATA_TOO_LONG, 1);
    else
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
6340
  }
6341
  return store(buff, length, &my_charset_numeric);
6342 6343
}

unknown's avatar
unknown committed
6344

unknown's avatar
unknown committed
6345
uint Field::is_equal(Create_field *new_field)
unknown's avatar
unknown committed
6346
{
6347
  return (new_field->sql_type == real_type());
unknown's avatar
unknown committed
6348 6349 6350
}


unknown's avatar
unknown committed
6351
uint Field_str::is_equal(Create_field *new_field)
unknown's avatar
unknown committed
6352
{
6353
  if (field_flags_are_binary() != new_field->field_flags_are_binary())
6354
    return 0;
unknown's avatar
unknown committed
6355

6356
  return ((new_field->sql_type == real_type()) &&
unknown's avatar
unknown committed
6357
	  new_field->charset == field_charset &&
unknown's avatar
unknown committed
6358
	  new_field->length == max_display_length());
unknown's avatar
unknown committed
6359 6360 6361
}


6362
int Field_string::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
6363
{
unknown's avatar
unknown committed
6364 6365 6366
  char buff[64];
  int  l;
  CHARSET_INFO *cs=charset();
6367 6368
  l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),
                                   unsigned_val ? 10 : -10, nr);
unknown's avatar
unknown committed
6369
  return Field_string::store(buff,(uint)l,cs);
unknown's avatar
unknown committed
6370 6371
}

6372

unknown's avatar
unknown committed
6373 6374
int Field_longstr::store_decimal(const my_decimal *d)
{
6375
  char buff[DECIMAL_MAX_STR_LENGTH+1];
6376
  String str(buff, sizeof(buff), &my_charset_numeric);
unknown's avatar
unknown committed
6377
  my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
6378
  return store(str.ptr(), str.length(), str.charset());
unknown's avatar
unknown committed
6379
}
unknown's avatar
unknown committed
6380

6381 6382 6383 6384 6385
uint32 Field_longstr::max_data_length() const
{
  return field_length + (field_length > 255 ? 2 : 1);
}

6386

unknown's avatar
unknown committed
6387 6388
double Field_string::val_real(void)
{
6389
  ASSERT_COLUMN_MARKED_FOR_READ;
6390 6391
  int error;
  char *end;
6392
  CHARSET_INFO *cs= charset();
6393 6394
  double result;
  
6395
  result=  my_strntod(cs,(char*) ptr,field_length,&end,&error);
6396
  if (!table->in_use->no_errors &&
6397 6398 6399
      (error || (field_length != (uint32)(end - (char*) ptr) && 
                 !check_if_only_end_space(cs, end,
                                          (char*) ptr + field_length))))
6400
  {
6401
    ErrConvString err((char*) ptr, field_length, cs);
6402
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
6403 6404 6405
                        ER_TRUNCATED_WRONG_VALUE,
                        ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
                        err.ptr());
6406 6407
  }
  return result;
unknown's avatar
unknown committed
6408 6409 6410 6411 6412
}


longlong Field_string::val_int(void)
{
6413
  ASSERT_COLUMN_MARKED_FOR_READ;
6414 6415 6416 6417 6418
  int error;
  char *end;
  CHARSET_INFO *cs= charset();
  longlong result;

6419
  result= my_strntoll(cs, (char*) ptr,field_length,10,&end,&error);
6420
  if (!table->in_use->no_errors &&
6421 6422 6423
      (error || (field_length != (uint32)(end - (char*) ptr) && 
                 !check_if_only_end_space(cs, end,
                                          (char*) ptr + field_length))))
6424
  {
6425
    ErrConvString err((char*) ptr, field_length, cs);
6426 6427 6428
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_TRUNCATED_WRONG_VALUE, 
                        ER(ER_TRUNCATED_WRONG_VALUE),
6429
                        "INTEGER", err.ptr());
6430 6431
  }
  return result;
unknown's avatar
unknown committed
6432 6433 6434 6435 6436 6437
}


String *Field_string::val_str(String *val_buffer __attribute__((unused)),
			      String *val_ptr)
{
6438
  ASSERT_COLUMN_MARKED_FOR_READ;
6439 6440
  /* See the comment for Field_long::store(long long) */
  DBUG_ASSERT(table->in_use == current_thd);
6441 6442 6443
  uint length;
  if (table->in_use->variables.sql_mode &
      MODE_PAD_CHAR_TO_FULL_LENGTH)
6444 6445
    length= my_charpos(field_charset, ptr, ptr + field_length,
                       field_length / field_charset->mbmaxlen);
6446
  else
6447 6448
    length= field_charset->cset->lengthsp(field_charset, (const char*) ptr,
                                          field_length);
unknown's avatar
unknown committed
6449
  val_ptr->set((const char*) ptr, length, field_charset);
unknown's avatar
unknown committed
6450 6451 6452 6453
  return val_ptr;
}


unknown's avatar
unknown committed
6454 6455
my_decimal *Field_string::val_decimal(my_decimal *decimal_value)
{
6456
  ASSERT_COLUMN_MARKED_FOR_READ;
6457 6458
  int err= str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr, field_length,
                          charset(), decimal_value);
6459 6460
  if (!table->in_use->no_errors && err)
  {
6461
    ErrConvString errmsg((char*) ptr, field_length, charset());
6462 6463 6464
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_TRUNCATED_WRONG_VALUE, 
                        ER(ER_TRUNCATED_WRONG_VALUE),
6465
                        "DECIMAL", errmsg.ptr());
6466 6467
  }

unknown's avatar
unknown committed
6468 6469 6470 6471
  return decimal_value;
}


6472 6473 6474 6475
struct Check_field_param {
  Field *field;
};

6476
#ifdef HAVE_REPLICATION
6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487
static bool
check_field_for_37426(const void *param_arg)
{
  Check_field_param *param= (Check_field_param*) param_arg;
  DBUG_ASSERT(param->field->real_type() == MYSQL_TYPE_STRING);
  DBUG_PRINT("debug", ("Field %s - type: %d, size: %d",
                       param->field->field_name,
                       param->field->real_type(),
                       param->field->row_pack_length()));
  return param->field->row_pack_length() > 255;
}
6488
#endif
6489

6490 6491 6492
bool
Field_string::compatible_field_size(uint field_metadata,
                                    Relay_log_info *rli_arg,
6493
                                    uint16 mflags __attribute__((unused)),
6494
                                    int *order_var)
6495 6496 6497 6498 6499 6500 6501
{
#ifdef HAVE_REPLICATION
  const Check_field_param check_param = { this };
  if (rpl_master_has_bug(rli_arg, 37426, TRUE,
                         check_field_for_37426, &check_param))
    return FALSE;                        // Not compatible field sizes
#endif
6502
  return Field::compatible_field_size(field_metadata, rli_arg, mflags, order_var);
6503 6504 6505
}


6506
int Field_string::cmp(const uchar *a_ptr, const uchar *b_ptr)
unknown's avatar
unknown committed
6507
{
unknown's avatar
unknown committed
6508 6509 6510 6511 6512 6513 6514 6515 6516 6517
  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
6518 6519 6520 6521 6522
  /*
    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,
6523 6524
                                          a_ptr, a_len,
                                          b_ptr, b_len,
6525
                                          0);
unknown's avatar
unknown committed
6526 6527
}

6528

6529
void Field_string::sort_string(uchar *to,uint length)
unknown's avatar
unknown committed
6530
{
6531 6532
  uint tmp __attribute__((unused))=
    my_strnxfrm(field_charset, to, length, ptr, field_length);
6533
  DBUG_ASSERT(tmp == length);
unknown's avatar
unknown committed
6534 6535 6536 6537 6538
}


void Field_string::sql_type(String &res) const
{
6539
  THD *thd= table->in_use;
unknown's avatar
unknown committed
6540
  CHARSET_INFO *cs=res.charset();
6541 6542 6543 6544 6545 6546 6547
  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") :
6548
			      (has_charset() ? "char" : "binary")),
6549
                             (int) field_length / charset()->mbmaxlen);
6550
  res.length(length);
6551 6552
  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
      has_charset() && (charset()->state & MY_CS_BINSORT))
6553
    res.append(STRING_WITH_LEN(" binary"));
unknown's avatar
unknown committed
6554 6555
}

6556

6557 6558 6559
uchar *Field_string::pack(uchar *to, const uchar *from,
                          uint max_length,
                          bool low_byte_first __attribute__((unused)))
6560
{
unknown's avatar
unknown committed
6561
  uint length=      min(field_length,max_length);
6562
  uint local_char_length= max_length/field_charset->mbmaxlen;
6563 6564
  DBUG_PRINT("debug", ("Packing field '%s' - length: %u ", field_name, length));

6565 6566 6567 6568
  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);
Luis Soares's avatar
Luis Soares committed
6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582
 
  /*
     TODO: change charset interface to add a new function that does 
           the following or add a flag to lengthsp to do it itself 
           (this is for not packing padding adding bytes in BINARY 
           fields).
  */
  if (field_charset->mbmaxlen == 1)
  {
    while (length && from[length-1] == field_charset->pad_char)
      length --;
  }
  else
    length= field_charset->cset->lengthsp(field_charset, (const char*) from, length);
6583 6584

  // Length always stored little-endian
6585
  *to++= (uchar) length;
6586
  if (field_length > 255)
6587
    *to++= (uchar) (length >> 8);
6588 6589

  // Store the actual bytes of the string
6590 6591
  memcpy(to, from, length);
  return to+length;
6592 6593 6594
}


6595 6596 6597 6598 6599 6600 6601 6602 6603 6604
/**
   Unpack a string field from row data.

   This method is used to unpack a string field from a master whose size 
   of the field is less than that of the slave. Note that there can be a
   variety of field types represented with this class. Certain types like
   ENUM or SET are processed differently. Hence, the upper byte of the 
   @c param_data argument contains the result of field->real_type() from
   the master.

6605 6606 6607
   @note For information about how the length is packed, see @c
   Field_string::do_save_field_metadata

6608 6609 6610 6611 6612 6613
   @param   to         Destination of the data
   @param   from       Source of the data
   @param   param_data Real type (upper) and length (lower) values

   @return  New pointer into memory based on from + length of the data
*/
6614 6615 6616 6617 6618 6619
const uchar *
Field_string::unpack(uchar *to,
                     const uchar *from,
                     uint param_data,
                     bool low_byte_first __attribute__((unused)))
{
6620
  uint from_length, length;
6621

6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637
  /*
    Compute the declared length of the field on the master. This is
    used to decide if one or two bytes should be read as length.
   */
  if (param_data)
    from_length= (((param_data >> 4) & 0x300) ^ 0x300) + (param_data & 0x00ff);
  else
    from_length= field_length;

  DBUG_PRINT("debug",
             ("param_data: 0x%x, field_length: %u, from_length: %u",
              param_data, field_length, from_length));
  /*
    Compute the actual length of the data by reading one or two bits
    (depending on the declared field length on the master).
   */
6638
  if (from_length > 255)
6639 6640 6641 6642 6643
  {
    length= uint2korr(from);
    from+= 2;
  }
  else
6644
    length= (uint) *from++;
6645 6646 6647

  memcpy(to, from, length);
  // Pad the string with the pad character of the fields charset
6648
  field_charset->cset->fill(field_charset, (char*) to + length, field_length - length, field_charset->pad_char);
unknown's avatar
unknown committed
6649 6650 6651 6652
  return from+length;
}


6653 6654 6655 6656 6657 6658 6659
/**
   Save the field metadata for string fields.

   Saves the real type in the first byte and the field length in the 
   second byte of the field metadata array at index of *metadata_ptr and
   *(metadata_ptr + 1).

6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678
   @note In order to be able to handle lengths exceeding 255 and be
   backwards-compatible with pre-5.1.26 servers, an extra two bits of
   the length has been added to the metadata in such a way that if
   they are set, a new unrecognized type is generated.  This will
   cause pre-5.1-26 servers to stop due to a field type mismatch,
   while new servers will be able to extract the extra bits. If the
   length is <256, there will be no difference and both a new and an
   old server will be able to handle it.

   @note The extra two bits are added to bits 13 and 14 of the
   parameter data (with 1 being the least siginficant bit and 16 the
   most significant bit of the word) by xoring the extra length bits
   with the real type.  Since all allowable types have 0xF as most
   significant bits of the metadata word, lengths <256 will not affect
   the real type at all, while all other values will result in a
   non-existant type in the range 17-244.

   @see Field_string::unpack

6679 6680 6681 6682 6683 6684
   @param   metadata_ptr   First byte of field metadata

   @returns number of bytes written to metadata_ptr
*/
int Field_string::do_save_field_metadata(uchar *metadata_ptr)
{
6685 6686 6687 6688 6689 6690
  DBUG_ASSERT(field_length < 1024);
  DBUG_ASSERT((real_type() & 0xF0) == 0xF0);
  DBUG_PRINT("debug", ("field_length: %u, real_type: %u",
                       field_length, real_type()));
  *metadata_ptr= (real_type() ^ ((field_length & 0x300) >> 4));
  *(metadata_ptr + 1)= field_length & 0xFF;
6691 6692 6693 6694
  return 2;
}


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

6702

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

6708 6709

uint Field_string::get_key_image(uchar *buff, uint length, imagetype type_arg)
unknown's avatar
unknown committed
6710
{
6711 6712
  uint bytes = my_charpos(field_charset, (char*) ptr,
                          (char*) ptr + field_length,
unknown's avatar
unknown committed
6713 6714 6715
                          length / field_charset->mbmaxlen);
  memcpy(buff, ptr, bytes);
  if (bytes < length)
6716 6717
    field_charset->cset->fill(field_charset, (char*) buff + bytes,
                              length - bytes, field_charset->pad_char);
unknown's avatar
unknown committed
6718 6719
  return bytes;
}
unknown's avatar
unknown committed
6720

6721

Konstantin Osipov's avatar
Konstantin Osipov committed
6722
Field *Field_string::new_field(MEM_ROOT *root, TABLE *new_table,
6723
                               bool keep_type)
6724
{
unknown's avatar
unknown committed
6725
  Field *field;
6726
  if (type() != MYSQL_TYPE_VAR_STRING || keep_type)
unknown's avatar
unknown committed
6727
    field= Field::new_field(root, new_table, keep_type);
6728 6729
  else if ((field= new Field_varstring(field_length, maybe_null(), field_name,
                                       new_table->s, charset())))
6730 6731 6732 6733 6734 6735
  {
    /*
      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.
    */
unknown's avatar
unknown committed
6736
    field->init(new_table);
6737 6738 6739 6740 6741 6742
    /*
      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.
    */
unknown's avatar
unknown committed
6743
    field->orig_table= orig_table;
6744
  }
unknown's avatar
unknown committed
6745
  return field;
6746 6747
}

unknown's avatar
unknown committed
6748

unknown's avatar
unknown committed
6749
/****************************************************************************
6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762
  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
6763 6764
****************************************************************************/

unknown's avatar
unknown committed
6765
const uint Field_varstring::MAX_SIZE= UINT_MAX16;
unknown's avatar
unknown committed
6766

6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779
/**
   Save the field metadata for varstring fields.

   Saves the field length in the first byte. Note: may consume
   2 bytes. Caller must ensure second byte is contiguous with
   first byte (e.g. array index 0,1).

   @param   metadata_ptr   First byte of field metadata

   @returns number of bytes written to metadata_ptr
*/
int Field_varstring::do_save_field_metadata(uchar *metadata_ptr)
{
6780
  DBUG_ASSERT(field_length <= 65535);
6781
  int2store((char*)metadata_ptr, field_length);
6782 6783 6784
  return 2;
}

unknown's avatar
unknown committed
6785
int Field_varstring::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
6786
{
6787
  ASSERT_COLUMN_MARKED_FOR_WRITE;
6788 6789 6790 6791 6792 6793
  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,
6794 6795
                                       (char*) ptr + length_bytes,
                                       field_length,
6796 6797 6798 6799 6800
                                       cs, from, length,
                                       field_length / field_charset->mbmaxlen,
                                       &well_formed_error_pos,
                                       &cannot_convert_error_pos,
                                       &from_end_pos);
unknown's avatar
unknown committed
6801

6802 6803 6804 6805
  if (length_bytes == 1)
    *ptr= (uchar) copy_length;
  else
    int2store(ptr, copy_length);
6806

6807
  if (check_string_copy_error(this, well_formed_error_pos,
6808
                              cannot_convert_error_pos, from + length, cs))
6809 6810
    return 2;

6811
  return report_if_important_data(from_end_pos, from + length, TRUE);
unknown's avatar
unknown committed
6812 6813 6814
}


6815
int Field_varstring::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
6816
{
unknown's avatar
unknown committed
6817
  char buff[64];
6818 6819 6820 6821
  uint  length;
  length= (uint) (field_charset->cset->longlong10_to_str)(field_charset,
                                                          buff,
                                                          sizeof(buff),
6822 6823 6824
                                                          (unsigned_val ? 10:
                                                           -10),
                                                           nr);
6825
  return Field_varstring::store(buff, length, field_charset);
unknown's avatar
unknown committed
6826 6827 6828 6829 6830
}


double Field_varstring::val_real(void)
{
6831
  ASSERT_COLUMN_MARKED_FOR_READ;
6832 6833 6834 6835 6836
  int error;
  char *end;
  double result;
  CHARSET_INFO* cs= charset();
  
6837
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848
  result= my_strntod(cs, (char*)ptr+length_bytes, length, &end, &error);
  
  if (!table->in_use->no_errors && 
       (error || (length != (uint)(end - (char*)ptr+length_bytes) && 
         !check_if_only_end_space(cs, end, (char*)ptr+length_bytes+length)))) 
  {
    push_numerical_conversion_warning(current_thd, (char*)ptr+length_bytes, 
                                      length, cs,"DOUBLE", 
                                      ER_TRUNCATED_WRONG_VALUE);
  }
  return result;
unknown's avatar
unknown committed
6849 6850 6851 6852 6853
}


longlong Field_varstring::val_int(void)
{
6854
  ASSERT_COLUMN_MARKED_FOR_READ;
6855 6856 6857 6858
  int error;
  char *end;
  CHARSET_INFO *cs= charset();
  
6859
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871
  longlong result= my_strntoll(cs, (char*) ptr+length_bytes, length, 10,
                     &end, &error);
		     
  if (!table->in_use->no_errors && 
       (error || (length != (uint)(end - (char*)ptr+length_bytes) && 
         !check_if_only_end_space(cs, end, (char*)ptr+length_bytes+length)))) 
  {
    push_numerical_conversion_warning(current_thd, (char*)ptr+length_bytes, 
                                      length, cs, "INTEGER", 
                                      ER_TRUNCATED_WRONG_VALUE);  
  }
  return result;
unknown's avatar
unknown committed
6872 6873 6874 6875 6876
}

String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
				 String *val_ptr)
{
6877
  ASSERT_COLUMN_MARKED_FOR_READ;
6878
  uint length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6879
  val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
unknown's avatar
unknown committed
6880 6881 6882 6883
  return val_ptr;
}


unknown's avatar
unknown committed
6884 6885
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
{
6886
  ASSERT_COLUMN_MARKED_FOR_READ;
6887
  CHARSET_INFO *cs= charset();
6888
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6889 6890 6891 6892 6893 6894 6895 6896 6897
  int error= str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
                 cs, decimal_value);

  if (!table->in_use->no_errors && error)
  {
    push_numerical_conversion_warning(current_thd, (char*)ptr+length_bytes, 
                                      length, cs, "DECIMAL", 
                                      ER_TRUNCATED_WRONG_VALUE); 
  }
unknown's avatar
unknown committed
6898 6899 6900 6901
  return decimal_value;
}


6902
int Field_varstring::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
6903
                             uint max_len)
unknown's avatar
unknown committed
6904
{
6905
  uint a_length, b_length;
unknown's avatar
unknown committed
6906
  int diff;
6907 6908 6909

  if (length_bytes == 1)
  {
6910 6911
    a_length= (uint) *a_ptr;
    b_length= (uint) *b_ptr;
6912 6913 6914 6915 6916 6917
  }
  else
  {
    a_length= uint2korr(a_ptr);
    b_length= uint2korr(b_ptr);
  }
6918 6919
  set_if_smaller(a_length, max_len);
  set_if_smaller(b_length, max_len);
6920
  diff= field_charset->coll->strnncollsp(field_charset,
6921
                                         a_ptr+
6922
                                         length_bytes,
6923
                                         a_length,
6924
                                         b_ptr+
6925
                                         length_bytes,
6926 6927
                                         b_length,0);
  return diff;
unknown's avatar
unknown committed
6928 6929
}

6930

6931 6932 6933
/**
  @note
    varstring and blob keys are ALWAYS stored with a 2 byte length prefix
6934 6935
*/

6936
int Field_varstring::key_cmp(const uchar *key_ptr, uint max_key_length)
6937
{
6938
  uint length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6939
  uint local_char_length= max_key_length / field_charset->mbmaxlen;
6940

6941 6942 6943
  local_char_length= my_charpos(field_charset, ptr + length_bytes,
                          ptr + length_bytes + length, local_char_length);
  set_if_smaller(length, local_char_length);
6944
  return field_charset->coll->strnncollsp(field_charset, 
6945
                                          ptr + length_bytes,
6946
                                          length,
6947
                                          key_ptr+
6948 6949
                                          HA_KEY_BLOB_LENGTH,
                                          uint2korr(key_ptr), 0);
unknown's avatar
unknown committed
6950 6951
}

6952

6953 6954
/**
  Compare to key segments (always 2 byte length prefix).
6955

6956
  @note
6957 6958 6959 6960
    This is used only to compare key segments created for index_read().
    (keys are created and compared in key.cc)
*/

6961
int Field_varstring::key_cmp(const uchar *a,const uchar *b)
6962
{
6963
  return field_charset->coll->strnncollsp(field_charset,
6964
                                          a + HA_KEY_BLOB_LENGTH,
6965
                                          uint2korr(a),
6966
                                          b + HA_KEY_BLOB_LENGTH,
6967 6968
                                          uint2korr(b),
                                          0);
6969 6970 6971
}


6972
void Field_varstring::sort_string(uchar *to,uint length)
unknown's avatar
unknown committed
6973
{
6974
  uint tot_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985

  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;
  }
 
6986
  tot_length= my_strnxfrm(field_charset,
6987
			  to, length, ptr + length_bytes,
6988
			  tot_length);
unknown's avatar
unknown committed
6989
  DBUG_ASSERT(tot_length == length);
unknown's avatar
unknown committed
6990 6991 6992
}


6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004
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
7005 7006
void Field_varstring::sql_type(String &res) const
{
7007
  THD *thd= table->in_use;
unknown's avatar
unknown committed
7008
  CHARSET_INFO *cs=res.charset();
7009 7010 7011 7012 7013 7014
  ulong length;

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

7021

7022
uint32 Field_varstring::data_length()
7023
{
7024
  return length_bytes == 1 ? (uint32) *ptr : uint2korr(ptr);
7025 7026
}

7027 7028 7029 7030 7031
/*
  Functions to create a packed row.
  Here the number of length bytes are depending on the given max_length
*/

7032 7033 7034
uchar *Field_varstring::pack(uchar *to, const uchar *from,
                             uint max_length,
                             bool low_byte_first __attribute__((unused)))
unknown's avatar
unknown committed
7035
{
7036
  uint length= length_bytes == 1 ? (uint) *from : uint2korr(from);
7037
  set_if_smaller(max_length, field_length);
unknown's avatar
unknown committed
7038 7039
  if (length > max_length)
    length=max_length;
7040 7041 7042

  /* Length always stored little-endian */
  *to++= length & 0xFF;
unknown's avatar
unknown committed
7043
  if (max_length > 255)
7044 7045 7046 7047
    *to++= (length >> 8) & 0xFF;

  /* Store bytes of string */
  if (length > 0)
7048
    memcpy(to, from+length_bytes, length);
unknown's avatar
unknown committed
7049 7050 7051 7052
  return to+length;
}


7053 7054 7055 7056 7057
/**
   Unpack a varstring field from row data.

   This method is used to unpack a varstring field from a master
   whose size of the field is less than that of the slave.
7058 7059 7060

   @note
   The string length is always packed little-endian.
7061 7062 7063 7064 7065 7066 7067
  
   @param   to         Destination of the data
   @param   from       Source of the data
   @param   param_data Length bytes from the master's field data

   @return  New pointer into memory based on from + length of the data
*/
7068 7069 7070 7071
const uchar *
Field_varstring::unpack(uchar *to, const uchar *from,
                        uint param_data,
                        bool low_byte_first __attribute__((unused)))
7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082
{
  uint length;
  uint l_bytes= (param_data && (param_data < field_length)) ? 
                (param_data <= 255) ? 1 : 2 : length_bytes;
  if (l_bytes == 1)
  {
    to[0]= *from++;
    length= to[0];
    if (length_bytes == 2)
      to[1]= 0;
  }
7083
  else /* l_bytes == 2 */
unknown's avatar
unknown committed
7084
  {
7085 7086 7087
    length= uint2korr(from);
    to[0]= *from++;
    to[1]= *from++;
unknown's avatar
unknown committed
7088 7089
  }
  if (length)
7090
    memcpy(to+ length_bytes, from, length);
unknown's avatar
unknown committed
7091 7092 7093 7094
  return from+length;
}


7095
uint Field_varstring::packed_col_length(const uchar *data_ptr, uint length)
unknown's avatar
unknown committed
7096
{
7097
  if (length > 255)
7098
    return uint2korr(data_ptr)+2;
7099
  return (uint) *data_ptr + 1;
unknown's avatar
unknown committed
7100 7101
}

7102

unknown's avatar
unknown committed
7103 7104
uint Field_varstring::max_packed_col_length(uint max_length)
{
7105
  return (max_length > 255 ? 2 : 1)+max_length;
unknown's avatar
unknown committed
7106 7107
}

7108
uint Field_varstring::get_key_image(uchar *buff, uint length, imagetype type)
7109
{
7110
  uint f_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7111
  uint local_char_length= length / field_charset->mbmaxlen;
7112
  uchar *pos= ptr+length_bytes;
7113 7114 7115
  local_char_length= my_charpos(field_charset, pos, pos + f_length,
                                local_char_length);
  set_if_smaller(f_length, local_char_length);
7116
  /* Key is always stored with 2 bytes */
7117
  int2store(buff,f_length);
7118
  memcpy(buff+HA_KEY_BLOB_LENGTH, pos, f_length);
7119
  if (f_length < length)
7120 7121 7122 7123 7124
  {
    /*
      Must clear this as we do a memcmp in opt_range.cc to detect
      identical keys
    */
7125
    bzero(buff+HA_KEY_BLOB_LENGTH+f_length, (length-f_length));
7126
  }
unknown's avatar
unknown committed
7127
  return HA_KEY_BLOB_LENGTH+f_length;
7128 7129
}

7130

7131
void Field_varstring::set_key_image(const uchar *buff,uint length)
7132
{
7133
  length= uint2korr(buff);			// Real length is here
7134
  (void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
7135
                                field_charset);
7136 7137 7138
}


7139
int Field_varstring::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
7140 7141 7142 7143
                                uint32 max_length)
{
  uint32 a_length,b_length;

7144 7145
  if (length_bytes == 1)
  {
7146 7147
    a_length= (uint) *a_ptr;
    b_length= (uint) *b_ptr;
7148 7149 7150 7151 7152 7153
  }
  else
  {
    a_length= uint2korr(a_ptr);
    b_length= uint2korr(b_ptr);
  }
7154 7155 7156 7157
  set_if_smaller(a_length, max_length);
  set_if_smaller(b_length, max_length);
  if (a_length != b_length)
    return 1;
7158 7159 7160 7161
  return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
}


Konstantin Osipov's avatar
Konstantin Osipov committed
7162
Field *Field_varstring::new_field(MEM_ROOT *root, TABLE *new_table,
7163
                                  bool keep_type)
7164
{
7165 7166
  Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
                                                            keep_type);
7167 7168 7169 7170 7171 7172 7173
  if (res)
    res->length_bytes= length_bytes;
  return res;
}


Field *Field_varstring::new_key_field(MEM_ROOT *root,
Konstantin Osipov's avatar
Konstantin Osipov committed
7174
                                      TABLE *new_table,
7175
                                      uchar *new_ptr, uchar *new_null_ptr,
unknown's avatar
unknown committed
7176 7177 7178 7179 7180 7181 7182 7183 7184 7185
                                      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 */
7186
    res->length_bytes= 2;
unknown's avatar
unknown committed
7187
  }
7188
  return res;
7189 7190
}

7191

unknown's avatar
unknown committed
7192
uint Field_varstring::is_equal(Create_field *new_field)
unknown's avatar
unknown committed
7193
{
7194
  if (new_field->sql_type == real_type() &&
unknown's avatar
unknown committed
7195 7196
      new_field->charset == field_charset)
  {
unknown's avatar
unknown committed
7197
    if (new_field->length == max_display_length())
unknown's avatar
unknown committed
7198
      return IS_EQUAL_YES;
unknown's avatar
unknown committed
7199 7200 7201
    if (new_field->length > max_display_length() &&
	((new_field->length <= 255 && max_display_length() <= 255) ||
	 (new_field->length > 255 && max_display_length() > 255)))
unknown's avatar
unknown committed
7202 7203 7204 7205 7206 7207
      return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
  }
  return IS_EQUAL_NO;
}


7208 7209 7210 7211 7212 7213 7214 7215
void Field_varstring::hash(ulong *nr, ulong *nr2)
{
  if (is_null())
  {
    *nr^= (*nr << 1) | 1;
  }
  else
  {
7216
    uint len=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7217
    CHARSET_INFO *cs= charset();
7218
    cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);
7219 7220 7221 7222
  }
}


unknown's avatar
unknown committed
7223 7224 7225 7226 7227 7228
/****************************************************************************
** 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.
****************************************************************************/

7229
Field_blob::Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
unknown's avatar
unknown committed
7230
		       enum utype unireg_check_arg, const char *field_name_arg,
unknown's avatar
unknown committed
7231
                       TABLE_SHARE *share, uint blob_pack_length,
7232
		       CHARSET_INFO *cs)
7233
  :Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
unknown's avatar
unknown committed
7234 7235
                 null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
                 cs),
unknown's avatar
unknown committed
7236
   packlength(blob_pack_length)
unknown's avatar
unknown committed
7237
{
7238
  DBUG_ASSERT(blob_pack_length <= 4); // Only pack lengths 1-4 supported currently
unknown's avatar
unknown committed
7239
  flags|= BLOB_FLAG;
unknown's avatar
unknown committed
7240
  share->blob_fields++;
7241
  /* TODO: why do not fill table->s->blob_field array here? */
unknown's avatar
unknown committed
7242 7243 7244
}


7245 7246 7247 7248
void Field_blob::store_length(uchar *i_ptr, 
                              uint i_packlength, 
                              uint32 i_number, 
                              bool low_byte_first)
unknown's avatar
unknown committed
7249
{
unknown's avatar
unknown committed
7250
  switch (i_packlength) {
unknown's avatar
unknown committed
7251
  case 1:
unknown's avatar
unknown committed
7252
    i_ptr[0]= (uchar) i_number;
unknown's avatar
unknown committed
7253 7254 7255
    break;
  case 2:
#ifdef WORDS_BIGENDIAN
7256
    if (low_byte_first)
unknown's avatar
unknown committed
7257
    {
unknown's avatar
unknown committed
7258
      int2store(i_ptr,(unsigned short) i_number);
unknown's avatar
unknown committed
7259 7260 7261
    }
    else
#endif
unknown's avatar
unknown committed
7262
      shortstore(i_ptr,(unsigned short) i_number);
unknown's avatar
unknown committed
7263 7264
    break;
  case 3:
unknown's avatar
unknown committed
7265
    int3store(i_ptr,i_number);
unknown's avatar
unknown committed
7266 7267 7268
    break;
  case 4:
#ifdef WORDS_BIGENDIAN
7269
    if (low_byte_first)
unknown's avatar
unknown committed
7270
    {
unknown's avatar
unknown committed
7271
      int4store(i_ptr,i_number);
unknown's avatar
unknown committed
7272 7273 7274
    }
    else
#endif
unknown's avatar
unknown committed
7275
      longstore(i_ptr,i_number);
unknown's avatar
unknown committed
7276 7277 7278 7279
  }
}


7280
uint32 Field_blob::get_length(const uchar *pos, uint packlength_arg, bool low_byte_first)
unknown's avatar
unknown committed
7281
{
7282
  switch (packlength_arg) {
unknown's avatar
unknown committed
7283
  case 1:
7284
    return (uint32) pos[0];
unknown's avatar
unknown committed
7285 7286 7287 7288
  case 2:
    {
      uint16 tmp;
#ifdef WORDS_BIGENDIAN
7289
      if (low_byte_first)
unknown's avatar
unknown committed
7290 7291 7292 7293
	tmp=sint2korr(pos);
      else
#endif
	shortget(tmp,pos);
unknown's avatar
unknown committed
7294
      return (uint32) tmp;
unknown's avatar
unknown committed
7295 7296
    }
  case 3:
unknown's avatar
unknown committed
7297
    return (uint32) uint3korr(pos);
unknown's avatar
unknown committed
7298 7299 7300 7301
  case 4:
    {
      uint32 tmp;
#ifdef WORDS_BIGENDIAN
7302
      if (low_byte_first)
unknown's avatar
unknown committed
7303 7304 7305 7306
	tmp=uint4korr(pos);
      else
#endif
	longget(tmp,pos);
unknown's avatar
unknown committed
7307
      return (uint32) tmp;
unknown's avatar
unknown committed
7308 7309
    }
  }
7310
  /* When expanding this, see also MAX_FIELD_BLOBLENGTH. */
unknown's avatar
unknown committed
7311 7312 7313 7314
  return 0;					// Impossible
}


7315
/**
7316 7317
  Put a blob length field into a record buffer.

7318 7319 7320
  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'.
7321

7322 7323
  @param pos                 Pointer into the record buffer.
  @param length              The length value to put.
7324 7325
*/

7326
void Field_blob::put_length(uchar *pos, uint32 length)
7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344
{
  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
7345
int Field_blob::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
7346
{
7347
  ASSERT_COLUMN_MARKED_FOR_WRITE;
7348 7349 7350 7351 7352 7353 7354
  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
7355
  if (!length)
unknown's avatar
unknown committed
7356 7357
  {
    bzero(ptr,Field_blob::pack_length());
7358
    return 0;
unknown's avatar
unknown committed
7359
  }
unknown's avatar
unknown committed
7360

7361 7362 7363 7364 7365 7366 7367
  /*
    If the 'from' address is in the range of the temporary 'value'-
    object we need to copy the content to a different location or it will be
    invalidated when the 'value'-object is reallocated to make room for
    the new character set.
  */
  if (from >= value.ptr() && from <= value.ptr()+value.length())
7368
  {
7369 7370 7371 7372
    /*
      If content of the 'from'-address is cached in the 'value'-object
      it is possible that the content needs a character conversion.
    */
7373 7374 7375 7376 7377 7378
    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
7379
    }
7380 7381 7382
    if (tmpstr.copy(from, length, cs))
      goto oom_error;
    from= tmpstr.ptr();
unknown's avatar
unknown committed
7383
  }
7384 7385 7386 7387 7388

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

7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399

  if (f_is_hex_escape(flags))
  {
    copy_length= my_copy_with_hex_escaping(field_charset,
                                           (char*) value.ptr(), new_length,
                                            from, length);
    Field_blob::store_length(copy_length);
    tmp= value.ptr();
    bmove(ptr + packlength, (uchar*) &tmp, sizeof(char*));
    return 0;
  }
7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414
  /*
    "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();
7415
  bmove(ptr+packlength,(uchar*) &tmp,sizeof(char*));
7416 7417

  if (check_string_copy_error(this, well_formed_error_pos,
7418
                              cannot_convert_error_pos, from + length, cs))
7419 7420
    return 2;

7421
  return report_if_important_data(from_end_pos, from + length, TRUE);
7422 7423 7424 7425 7426

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


unknown's avatar
unknown committed
7430
int Field_blob::store(double nr)
unknown's avatar
unknown committed
7431
{
7432
  CHARSET_INFO *cs=charset();
7433
  value.set_real(nr, NOT_FIXED_DEC, cs);
7434
  return Field_blob::store(value.ptr(),(uint) value.length(), cs);
unknown's avatar
unknown committed
7435 7436 7437
}


7438
int Field_blob::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
7439
{
7440
  CHARSET_INFO *cs=charset();
7441
  value.set_int(nr, unsigned_val, cs);
7442
  return Field_blob::store(value.ptr(), (uint) value.length(), cs);
unknown's avatar
unknown committed
7443 7444
}

7445

unknown's avatar
unknown committed
7446 7447
double Field_blob::val_real(void)
{
7448
  ASSERT_COLUMN_MARKED_FOR_READ;
7449
  int not_used;
7450 7451 7452 7453
  char *end_not_used, *blob;
  uint32 length;
  CHARSET_INFO *cs;

7454
  memcpy(&blob, ptr+packlength, sizeof(char*));
unknown's avatar
unknown committed
7455 7456
  if (!blob)
    return 0.0;
7457 7458
  length= get_length(ptr);
  cs= charset();
unknown's avatar
unknown committed
7459
  return my_strntod(cs, blob, length, &end_not_used, &not_used);
unknown's avatar
unknown committed
7460 7461 7462 7463 7464
}


longlong Field_blob::val_int(void)
{
7465
  ASSERT_COLUMN_MARKED_FOR_READ;
7466
  int not_used;
unknown's avatar
unknown committed
7467
  char *blob;
7468
  memcpy(&blob, ptr+packlength, sizeof(char*));
unknown's avatar
unknown committed
7469 7470
  if (!blob)
    return 0;
unknown's avatar
unknown committed
7471
  uint32 length=get_length(ptr);
7472
  return my_strntoll(charset(),blob,length,10,NULL,&not_used);
unknown's avatar
unknown committed
7473 7474 7475 7476 7477
}

String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
			    String *val_ptr)
{
7478
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
7479
  char *blob;
7480
  memcpy(&blob, ptr+packlength, sizeof(char*));
unknown's avatar
unknown committed
7481
  if (!blob)
7482
    val_ptr->set("",0,charset());	// A bit safer than ->length(0)
unknown's avatar
unknown committed
7483
  else
7484
    val_ptr->set((const char*) blob,get_length(ptr),charset());
unknown's avatar
unknown committed
7485 7486 7487 7488
  return val_ptr;
}


unknown's avatar
unknown committed
7489 7490
my_decimal *Field_blob::val_decimal(my_decimal *decimal_value)
{
7491
  ASSERT_COLUMN_MARKED_FOR_READ;
7492
  const char *blob;
7493
  size_t length;
7494
  memcpy(&blob, ptr+packlength, sizeof(const uchar*));
unknown's avatar
unknown committed
7495
  if (!blob)
7496
  {
unknown's avatar
unknown committed
7497
    blob= "";
7498 7499 7500 7501 7502 7503
    length= 0;
  }
  else
    length= get_length(ptr);

  str2my_decimal(E_DEC_FATAL_ERROR, blob, length, charset(),
unknown's avatar
unknown committed
7504 7505 7506 7507 7508
                 decimal_value);
  return decimal_value;
}


7509
int Field_blob::cmp(const uchar *a,uint32 a_length, const uchar *b,
unknown's avatar
unknown committed
7510
		    uint32 b_length)
unknown's avatar
unknown committed
7511
{
7512
  return field_charset->coll->strnncollsp(field_charset, 
7513
                                          a, a_length, b, b_length,
7514
                                          0);
unknown's avatar
unknown committed
7515 7516 7517
}


7518
int Field_blob::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
7519
                        uint max_length)
unknown's avatar
unknown committed
7520
{
7521
  uchar *blob1,*blob2;
7522 7523
  memcpy(&blob1, a_ptr+packlength, sizeof(char*));
  memcpy(&blob2, b_ptr+packlength, sizeof(char*));
7524 7525 7526 7527
  uint a_len= get_length(a_ptr), b_len= get_length(b_ptr);
  set_if_smaller(a_len, max_length);
  set_if_smaller(b_len, max_length);
  return Field_blob::cmp(blob1,a_len,blob2,b_len);
unknown's avatar
unknown committed
7528 7529 7530
}


7531
int Field_blob::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
unknown's avatar
unknown committed
7532
			   uint32 max_length)
unknown's avatar
unknown committed
7533 7534 7535
{
  char *a,*b;
  uint diff;
unknown's avatar
unknown committed
7536
  uint32 a_length,b_length;
7537 7538
  memcpy(&a, a_ptr+packlength, sizeof(char*));
  memcpy(&b, b_ptr+packlength, sizeof(char*));
unknown's avatar
unknown committed
7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551
  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 */

7552
uint Field_blob::get_key_image(uchar *buff,uint length, imagetype type_arg)
unknown's avatar
unknown committed
7553
{
unknown's avatar
unknown committed
7554
  uint32 blob_length= get_length(ptr);
7555
  uchar *blob;
unknown's avatar
unknown committed
7556

unknown's avatar
unknown committed
7557
#ifdef HAVE_SPATIAL
7558
  if (type_arg == itMBR)
unknown's avatar
unknown committed
7559
  {
unknown's avatar
unknown committed
7560
    const char *dummy;
unknown's avatar
unknown committed
7561
    MBR mbr;
7562 7563
    Geometry_buffer buffer;
    Geometry *gobj;
unknown's avatar
unknown committed
7564
    const uint image_length= SIZEOF_STORED_DOUBLE*4;
unknown's avatar
unknown committed
7565 7566 7567

    if (blob_length < SRID_SIZE)
    {
unknown's avatar
unknown committed
7568 7569
      bzero(buff, image_length);
      return image_length;
unknown's avatar
unknown committed
7570 7571
    }
    get_ptr(&blob);
7572
    gobj= Geometry::construct(&buffer, (char*) blob, blob_length);
7573
    if (!gobj || gobj->get_mbr(&mbr, &dummy))
unknown's avatar
unknown committed
7574
      bzero(buff, image_length);
unknown's avatar
unknown committed
7575 7576 7577 7578 7579 7580 7581
    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
7582
    return image_length;
unknown's avatar
unknown committed
7583
  }
unknown's avatar
unknown committed
7584
#endif /*HAVE_SPATIAL*/
unknown's avatar
unknown committed
7585

7586
  get_ptr(&blob);
7587 7588 7589 7590
  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);
7591

unknown's avatar
unknown committed
7592
  if ((uint32) length > blob_length)
unknown's avatar
unknown committed
7593
  {
7594 7595 7596 7597
    /*
      Must clear this as we do a memcmp in opt_range.cc to detect
      identical keys
    */
7598
    bzero(buff+HA_KEY_BLOB_LENGTH+blob_length, (length-blob_length));
unknown's avatar
unknown committed
7599
    length=(uint) blob_length;
unknown's avatar
unknown committed
7600
  }
unknown's avatar
unknown committed
7601
  int2store(buff,length);
7602
  memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
unknown's avatar
unknown committed
7603
  return HA_KEY_BLOB_LENGTH+length;
unknown's avatar
unknown committed
7604 7605
}

7606

7607
void Field_blob::set_key_image(const uchar *buff,uint length)
unknown's avatar
unknown committed
7608
{
7609
  length= uint2korr(buff);
7610 7611
  (void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
                           field_charset);
unknown's avatar
unknown committed
7612 7613
}

unknown's avatar
unknown committed
7614

7615
int Field_blob::key_cmp(const uchar *key_ptr, uint max_key_length)
unknown's avatar
unknown committed
7616
{
7617
  uchar *blob1;
unknown's avatar
unknown committed
7618
  uint blob_length=get_length(ptr);
7619
  memcpy(&blob1, ptr+packlength, sizeof(char*));
7620
  CHARSET_INFO *cs= charset();
7621 7622 7623 7624
  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);
7625
  return Field_blob::cmp(blob1, blob_length,
7626
			 key_ptr+HA_KEY_BLOB_LENGTH,
7627
			 uint2korr(key_ptr));
unknown's avatar
unknown committed
7628 7629
}

7630
int Field_blob::key_cmp(const uchar *a,const uchar *b)
unknown's avatar
unknown committed
7631
{
7632 7633
  return Field_blob::cmp(a+HA_KEY_BLOB_LENGTH, uint2korr(a),
			 b+HA_KEY_BLOB_LENGTH, uint2korr(b));
unknown's avatar
unknown committed
7634 7635 7636
}


7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648
/**
   Save the field metadata for blob fields.

   Saves the pack length in the first byte of the field metadata array
   at index of *metadata_ptr.

   @param   metadata_ptr   First byte of field metadata

   @returns number of bytes written to metadata_ptr
*/
int Field_blob::do_save_field_metadata(uchar *metadata_ptr)
{
7649
  DBUG_ENTER("Field_blob::do_save_field_metadata");
7650
  *metadata_ptr= pack_length_no_ptr();
7651 7652
  DBUG_PRINT("debug", ("metadata: %u (pack_length_no_ptr)", *metadata_ptr));
  DBUG_RETURN(1);
7653 7654 7655
}


7656 7657 7658 7659 7660 7661 7662
uint32 Field_blob::sort_length() const
{
  return (uint32) (current_thd->variables.max_sort_length + 
                   (field_charset == &my_charset_bin ? 0 : packlength));
}


7663
void Field_blob::sort_string(uchar *to,uint length)
unknown's avatar
unknown committed
7664
{
7665
  uchar *blob;
unknown's avatar
unknown committed
7666
  uint blob_length=get_length();
unknown's avatar
unknown committed
7667

unknown's avatar
unknown committed
7668 7669 7670 7671
  if (!blob_length)
    bzero(to,length);
  else
  {
7672 7673
    if (field_charset == &my_charset_bin)
    {
7674
      uchar *pos;
7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696

      /*
        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;
      }
    }
7697
    memcpy(&blob, ptr+packlength, sizeof(char*));
unknown's avatar
unknown committed
7698 7699
    
    blob_length=my_strnxfrm(field_charset,
7700
                            to, length, blob, blob_length);
7701
    DBUG_ASSERT(blob_length == length);
unknown's avatar
unknown committed
7702 7703 7704 7705 7706 7707 7708
  }
}


void Field_blob::sql_type(String &res) const
{
  const char *str;
7709
  uint length;
unknown's avatar
unknown committed
7710
  switch (packlength) {
7711 7712 7713 7714 7715
  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;
  }
7716
  res.set_ascii(str,length);
7717
  if (charset() == &my_charset_bin)
7718
    res.append(STRING_WITH_LEN("blob"));
7719 7720
  else
  {
7721
    res.append(STRING_WITH_LEN("text"));
unknown's avatar
unknown committed
7722 7723 7724
  }
}

7725 7726
uchar *Field_blob::pack(uchar *to, const uchar *from,
                        uint max_length, bool low_byte_first)
7727
{
7728 7729
  uchar *save= ptr;
  ptr= (uchar*) from;
unknown's avatar
unknown committed
7730
  uint32 length=get_length();			// Length of from string
7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742

  /*
    Store max length, which will occupy packlength bytes. If the max
    length given is smaller than the actual length of the blob, we
    just store the initial bytes of the blob.
  */
  store_length(to, packlength, min(length, max_length), low_byte_first);

  /*
    Store the actual blob data, which will occupy 'length' bytes.
   */
  if (length > 0)
7743
  {
7744
    get_ptr((uchar**) &from);
7745 7746 7747
    memcpy(to+packlength, from,length);
  }
  ptr=save;					// Restore org row pointer
7748
  return to+packlength+length;
7749 7750 7751
}


7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762
/**
   Unpack a blob field from row data.

   This method is used to unpack a blob field from a master whose size of 
   the field is less than that of the slave. Note: This method is included
   to satisfy inheritance rules, but is not needed for blob fields. It
   simply is used as a pass-through to the original unpack() method for
   blob fields.

   @param   to         Destination of the data
   @param   from       Source of the data
7763 7764 7765
   @param   param_data @c TRUE if base types should be stored in little-
                       endian format, @c FALSE if native format should
                       be used.
7766 7767 7768 7769 7770

   @return  New pointer into memory based on from + length of the data
*/
const uchar *Field_blob::unpack(uchar *to, 
                                const uchar *from,
7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781
                                uint param_data,
                                bool low_byte_first)
{
  DBUG_ENTER("Field_blob::unpack");
  DBUG_PRINT("enter", ("to: 0x%lx; from: 0x%lx;"
                       " param_data: %u; low_byte_first: %d",
                       (ulong) to, (ulong) from, param_data, low_byte_first));
  uint const master_packlength=
    param_data > 0 ? param_data & 0xFF : packlength;
  uint32 const length= get_length(from, master_packlength, low_byte_first);
  DBUG_DUMP("packed", from, length + master_packlength);
7782
  bitmap_set_bit(table->write_set, field_index);
7783 7784 7785 7786
  store(reinterpret_cast<const char*>(from) + master_packlength,
        length, field_charset);
  DBUG_DUMP("record", to, table->s->reclength);
  DBUG_RETURN(from + master_packlength + length);
7787 7788
}

7789
uint Field_blob::packed_col_length(const uchar *data_ptr, uint length)
7790 7791
{
  if (length > 255)
7792
    return uint2korr(data_ptr)+2;
7793
  return (uint) *data_ptr + 1;
7794 7795
}

7796

7797 7798 7799 7800
uint Field_blob::max_packed_col_length(uint max_length)
{
  return (max_length > 255 ? 2 : 1)+max_length;
}
7801

unknown's avatar
unknown committed
7802

unknown's avatar
unknown committed
7803
uint Field_blob::is_equal(Create_field *new_field)
7804
{
7805
  if (field_flags_are_binary() != new_field->field_flags_are_binary())
7806 7807 7808 7809
    return 0;

  return ((new_field->sql_type == get_blob_type_from_length(max_data_length()))
          && new_field->charset == field_charset &&
7810
          new_field->pack_length == pack_length());
7811 7812 7813
}


unknown's avatar
unknown committed
7814
#ifdef HAVE_SPATIAL
unknown's avatar
unknown committed
7815 7816 7817

void Field_geom::sql_type(String &res) const
{
unknown's avatar
unknown committed
7818 7819 7820 7821
  CHARSET_INFO *cs= &my_charset_latin1;
  switch (geom_type)
  {
    case GEOM_POINT:
7822
     res.set(STRING_WITH_LEN("point"), cs);
unknown's avatar
unknown committed
7823 7824
     break;
    case GEOM_LINESTRING:
7825
     res.set(STRING_WITH_LEN("linestring"), cs);
unknown's avatar
unknown committed
7826 7827
     break;
    case GEOM_POLYGON:
7828
     res.set(STRING_WITH_LEN("polygon"), cs);
unknown's avatar
unknown committed
7829 7830
     break;
    case GEOM_MULTIPOINT:
7831
     res.set(STRING_WITH_LEN("multipoint"), cs);
unknown's avatar
unknown committed
7832 7833
     break;
    case GEOM_MULTILINESTRING:
7834
     res.set(STRING_WITH_LEN("multilinestring"), cs);
unknown's avatar
unknown committed
7835 7836
     break;
    case GEOM_MULTIPOLYGON:
7837
     res.set(STRING_WITH_LEN("multipolygon"), cs);
unknown's avatar
unknown committed
7838 7839
     break;
    case GEOM_GEOMETRYCOLLECTION:
7840
     res.set(STRING_WITH_LEN("geometrycollection"), cs);
unknown's avatar
unknown committed
7841 7842
     break;
    default:
7843
     res.set(STRING_WITH_LEN("geometry"), cs);
unknown's avatar
unknown committed
7844
  }
unknown's avatar
unknown committed
7845 7846 7847
}


7848 7849 7850 7851 7852 7853 7854 7855
int Field_geom::store(double nr)
{
  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
             ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
  return -1;
}


7856
int Field_geom::store(longlong nr, bool unsigned_val)
7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871
{
  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
7872 7873 7874 7875 7876 7877
int Field_geom::store(const char *from, uint length, CHARSET_INFO *cs)
{
  if (!length)
    bzero(ptr, Field_blob::pack_length());
  else
  {
7878 7879
    if (from == Geometry::bad_geometry_data.ptr())
      goto err;
unknown's avatar
unknown committed
7880 7881 7882 7883
    // Check given WKB
    uint32 wkb_type;
    if (length < SRID_SIZE + WKB_HEADER_SIZE + SIZEOF_STORED_DOUBLE*2)
      goto err;
7884
    wkb_type= uint4korr(from + SRID_SIZE + 1);
7885
    if (wkb_type < (uint32) Geometry::wkb_point ||
7886
	wkb_type > (uint32) Geometry::wkb_last)
7887
      goto err;
unknown's avatar
unknown committed
7888 7889 7890 7891 7892 7893 7894 7895 7896
    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
7897 7898 7899

err:
  bzero(ptr, Field_blob::pack_length());  
7900 7901
  my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
             ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
unknown's avatar
unknown committed
7902
  return -1;
unknown's avatar
unknown committed
7903 7904
}

unknown's avatar
unknown committed
7905
#endif /*HAVE_SPATIAL*/
unknown's avatar
unknown committed
7906

unknown's avatar
unknown committed
7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929
/****************************************************************************
** 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
7930
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
7931 7932 7933 7934 7935 7936 7937 7938 7939 7940
  {
    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
7941
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
7942 7943 7944 7945 7946 7947 7948 7949 7950
  {
    int4store(ptr,value);
  }
  else
#endif
    longstore(ptr,(long) value);
  break;
  case 8:
#ifdef WORDS_BIGENDIAN
unknown's avatar
unknown committed
7951
  if (table->s->db_low_byte_first)
unknown's avatar
unknown committed
7952 7953 7954 7955 7956 7957 7958 7959 7960 7961
  {
    int8store(ptr,value);
  }
  else
#endif
    longlongstore(ptr,value); break;
  }
}


7962 7963 7964 7965
/**
  @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
7966 7967
*/

unknown's avatar
unknown committed
7968
int Field_enum::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
7969
{
7970
  ASSERT_COLUMN_MARKED_FOR_WRITE;
7971
  int err= 0;
unknown's avatar
unknown committed
7972
  uint32 not_used;
unknown's avatar
unknown committed
7973
  char buff[STRING_BUFFER_USUAL_SIZE];
unknown's avatar
unknown committed
7974
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
unknown's avatar
unknown committed
7975

unknown's avatar
unknown committed
7976
  /* Convert character set if necessary */
unknown's avatar
unknown committed
7977
  if (String::needs_conversion(length, cs, field_charset, &not_used))
unknown's avatar
unknown committed
7978
  { 
7979 7980
    uint dummy_errors;
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
unknown's avatar
unknown committed
7981 7982 7983
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
7984 7985

  /* Remove end space */
7986
  length= field_charset->cset->lengthsp(field_charset, from, length);
7987
  uint tmp=find_type2(typelib, from, length, field_charset);
unknown's avatar
unknown committed
7988
  if (!tmp)
unknown's avatar
unknown committed
7989
  {
7990
    if (length < 6) // Can't be more than 99999 enums
unknown's avatar
unknown committed
7991
    {
unknown's avatar
unknown committed
7992
      /* This is for reading numbers with LOAD DATA INFILE */
7993
      char *end;
7994 7995
      tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
      if (err || end != from+length || tmp > typelib->count)
unknown's avatar
unknown committed
7996 7997
      {
	tmp=0;
7998
	set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
7999
      }
8000 8001
      if (!table->in_use->count_cuted_fields)
        err= 0;
unknown's avatar
unknown committed
8002 8003
    }
    else
8004
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
8005
  }
unknown's avatar
unknown committed
8006
  store_type((ulonglong) tmp);
8007
  return err;
unknown's avatar
unknown committed
8008 8009 8010
}


unknown's avatar
unknown committed
8011
int Field_enum::store(double nr)
unknown's avatar
unknown committed
8012
{
8013
  return Field_enum::store((longlong) nr, FALSE);
unknown's avatar
unknown committed
8014 8015 8016
}


8017
int Field_enum::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
8018
{
8019
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
8020
  int error= 0;
8021
  if ((ulonglong) nr > typelib->count || nr == 0)
unknown's avatar
unknown committed
8022
  {
8023
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
8024 8025 8026 8027 8028
    if (nr != 0 || table->in_use->count_cuted_fields)
    {
      nr= 0;
      error= 1;
    }
unknown's avatar
unknown committed
8029 8030
  }
  store_type((ulonglong) (uint) nr);
8031
  return error;
unknown's avatar
unknown committed
8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042
}


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


longlong Field_enum::val_int(void)
{
8043
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
8044 8045
  switch (packlength) {
  case 1:
8046
    return (longlong) ptr[0];
unknown's avatar
unknown committed
8047
  case 2:
8048 8049
  {
    uint16 tmp;
unknown's avatar
unknown committed
8050
#ifdef WORDS_BIGENDIAN
8051 8052 8053
    if (table->s->db_low_byte_first)
      tmp=sint2korr(ptr);
    else
unknown's avatar
unknown committed
8054
#endif
8055 8056 8057
      shortget(tmp,ptr);
    return (longlong) tmp;
  }
unknown's avatar
unknown committed
8058 8059 8060
  case 3:
    return (longlong) uint3korr(ptr);
  case 4:
8061 8062
  {
    uint32 tmp;
unknown's avatar
unknown committed
8063
#ifdef WORDS_BIGENDIAN
8064 8065 8066
    if (table->s->db_low_byte_first)
      tmp=uint4korr(ptr);
    else
unknown's avatar
unknown committed
8067
#endif
8068 8069 8070
      longget(tmp,ptr);
    return (longlong) tmp;
  }
unknown's avatar
unknown committed
8071
  case 8:
8072 8073
  {
    longlong tmp;
unknown's avatar
unknown committed
8074
#ifdef WORDS_BIGENDIAN
8075 8076 8077
    if (table->s->db_low_byte_first)
      tmp=sint8korr(ptr);
    else
unknown's avatar
unknown committed
8078
#endif
8079 8080 8081
      longlongget(tmp,ptr);
    return tmp;
  }
unknown's avatar
unknown committed
8082 8083 8084 8085 8086
  }
  return 0;					// impossible
}


8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105
/**
   Save the field metadata for enum fields.

   Saves the real type in the first byte and the pack length in the 
   second byte of the field metadata array at index of *metadata_ptr and
   *(metadata_ptr + 1).

   @param   metadata_ptr   First byte of field metadata

   @returns number of bytes written to metadata_ptr
*/
int Field_enum::do_save_field_metadata(uchar *metadata_ptr)
{
  *metadata_ptr= real_type();
  *(metadata_ptr + 1)= pack_length();
  return 2;
}


unknown's avatar
unknown committed
8106 8107 8108 8109 8110
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
8111
    val_ptr->set("", 0, field_charset);
unknown's avatar
unknown committed
8112 8113
  else
    val_ptr->set((const char*) typelib->type_names[tmp-1],
8114
		 typelib->type_lengths[tmp-1],
8115
		 field_charset);
unknown's avatar
unknown committed
8116 8117 8118
  return val_ptr;
}

8119
int Field_enum::cmp(const uchar *a_ptr, const uchar *b_ptr)
unknown's avatar
unknown committed
8120
{
8121 8122
  uchar *old= ptr;
  ptr= (uchar*) a_ptr;
unknown's avatar
unknown committed
8123
  ulonglong a=Field_enum::val_int();
8124
  ptr= (uchar*) b_ptr;
unknown's avatar
unknown committed
8125
  ulonglong b=Field_enum::val_int();
8126
  ptr= old;
unknown's avatar
unknown committed
8127 8128 8129
  return (a < b) ? -1 : (a > b) ? 1 : 0;
}

8130
void Field_enum::sort_string(uchar *to,uint length __attribute__((unused)))
unknown's avatar
unknown committed
8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143
{
  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
{
8144 8145 8146
  char buffer[255];
  String enum_item(buffer, sizeof(buffer), res.charset());

unknown's avatar
unknown committed
8147
  res.length(0);
8148
  res.append(STRING_WITH_LEN("enum("));
unknown's avatar
unknown committed
8149 8150

  bool flag=0;
8151 8152
  uint *len= typelib->type_lengths;
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
unknown's avatar
unknown committed
8153
  {
8154
    uint dummy_errors;
unknown's avatar
unknown committed
8155 8156
    if (flag)
      res.append(',');
8157
    /* convert to res.charset() == utf8, then quote */
8158
    enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
8159 8160
    append_unescaped(&res, enum_item.ptr(), enum_item.length());
    flag= 1;
unknown's avatar
unknown committed
8161 8162 8163 8164 8165
  }
  res.append(')');
}


Konstantin Osipov's avatar
Konstantin Osipov committed
8166
Field *Field_enum::new_field(MEM_ROOT *root, TABLE *new_table,
8167 8168 8169 8170 8171 8172 8173 8174 8175
                             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;
}


8176 8177 8178 8179 8180 8181 8182 8183
/*
   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
8184 8185


unknown's avatar
unknown committed
8186
int Field_set::store(const char *from,uint length,CHARSET_INFO *cs)
unknown's avatar
unknown committed
8187
{
8188
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
8189
  bool got_warning= 0;
8190
  int err= 0;
8191 8192
  char *not_used;
  uint not_used2;
unknown's avatar
unknown committed
8193
  uint32 not_used_offset;
unknown's avatar
unknown committed
8194
  char buff[STRING_BUFFER_USUAL_SIZE];
unknown's avatar
unknown committed
8195
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
unknown's avatar
unknown committed
8196

unknown's avatar
unknown committed
8197
  /* Convert character set if necessary */
unknown's avatar
unknown committed
8198
  if (String::needs_conversion(length, cs, field_charset, &not_used_offset))
unknown's avatar
unknown committed
8199
  { 
8200 8201
    uint dummy_errors;
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
unknown's avatar
unknown committed
8202 8203 8204
    from= tmpstr.ptr();
    length=  tmpstr.length();
  }
8205 8206
  ulonglong tmp= find_set(typelib, from, length, field_charset,
                          &not_used, &not_used2, &got_warning);
unknown's avatar
unknown committed
8207 8208 8209
  if (!tmp && length && length < 22)
  {
    /* This is for reading numbers with LOAD DATA INFILE */
8210
    char *end;
8211 8212
    tmp=my_strntoull(cs,from,length,10,&end,&err);
    if (err || end != from+length ||
unknown's avatar
unknown committed
8213
	tmp > (ulonglong) (((longlong) 1 << typelib->count) - (longlong) 1))
8214
    {
8215
      tmp=0;      
8216
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8217
    }
unknown's avatar
unknown committed
8218
  }
unknown's avatar
unknown committed
8219
  else if (got_warning)
8220
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
unknown's avatar
unknown committed
8221
  store_type(tmp);
8222
  return err;
unknown's avatar
unknown committed
8223 8224 8225
}


8226
int Field_set::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
8227
{
8228
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
8229
  int error= 0;
8230 8231 8232 8233 8234 8235 8236
  ulonglong max_nr;

  if (sizeof(ulonglong)*8 <= typelib->count)
    max_nr= ULONGLONG_MAX;
  else
    max_nr= (ULL(1) << typelib->count) - 1;

8237
  if ((ulonglong) nr > max_nr)
unknown's avatar
unknown committed
8238
  {
8239
    nr&= max_nr;
8240
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8241
    error=1;
unknown's avatar
unknown committed
8242 8243
  }
  store_type((ulonglong) nr);
8244
  return error;
unknown's avatar
unknown committed
8245 8246 8247 8248 8249 8250 8251 8252 8253 8254
}


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);
8255
  val_buffer->set_charset(field_charset);
unknown's avatar
unknown committed
8256 8257 8258 8259 8260
  while (tmp && bitnr < (uint) typelib->count)
  {
    if (tmp & 1)
    {
      if (val_buffer->length())
8261
	val_buffer->append(&field_separator, 1, &my_charset_latin1);
unknown's avatar
unknown committed
8262
      String str(typelib->type_names[bitnr],
8263
		 typelib->type_lengths[bitnr],
8264
		 field_charset);
unknown's avatar
unknown committed
8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275
      val_buffer->append(str);
    }
    tmp>>=1;
    bitnr++;
  }
  return val_buffer;
}


void Field_set::sql_type(String &res) const
{
8276 8277 8278
  char buffer[255];
  String set_item(buffer, sizeof(buffer), res.charset());

unknown's avatar
unknown committed
8279
  res.length(0);
8280
  res.append(STRING_WITH_LEN("set("));
unknown's avatar
unknown committed
8281 8282

  bool flag=0;
8283 8284
  uint *len= typelib->type_lengths;
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
unknown's avatar
unknown committed
8285
  {
8286
    uint dummy_errors;
unknown's avatar
unknown committed
8287 8288
    if (flag)
      res.append(',');
8289
    /* convert to res.charset() == utf8, then quote */
8290
    set_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
8291 8292
    append_unescaped(&res, set_item.ptr(), set_item.length());
    flag= 1;
unknown's avatar
unknown committed
8293 8294 8295 8296
  }
  res.append(')');
}

8297 8298 8299 8300 8301 8302
/**
  @retval
    1  if the fields are equally defined
  @retval
    0  if the fields are unequally defined
*/
unknown's avatar
unknown committed
8303 8304 8305

bool Field::eq_def(Field *field)
{
8306
  if (real_type() != field->real_type() || charset() != field->charset() ||
unknown's avatar
unknown committed
8307 8308 8309 8310 8311
      pack_length() != field->pack_length())
    return 0;
  return 1;
}

8312

8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330
/**
  Compare the first t1::count type names.

  @return TRUE if the type names of t1 match those of t2. FALSE otherwise.
*/

static bool compare_type_names(CHARSET_INFO *charset, TYPELIB *t1, TYPELIB *t2)
{
  for (uint i= 0; i < t1->count; i++)
    if (my_strnncoll(charset,
                     (const uchar*) t1->type_names[i],
                     t1->type_lengths[i],
                     (const uchar*) t2->type_names[i],
                     t2->type_lengths[i]))
      return FALSE;
  return TRUE;
}

8331 8332 8333 8334
/**
  @return
  returns 1 if the fields are equally defined
*/
8335

unknown's avatar
unknown committed
8336 8337
bool Field_enum::eq_def(Field *field)
{
8338 8339
  TYPELIB *values;

unknown's avatar
unknown committed
8340
  if (!Field::eq_def(field))
8341
    return FALSE;
unknown's avatar
unknown committed
8342

8343
  values= ((Field_enum*) field)->typelib;
8344

8345
  /* Definition must be strictly equal. */
8346 8347
  if (typelib->count != values->count)
    return FALSE;
8348 8349

  return compare_type_names(field_charset, typelib, values);
8350 8351 8352
}


8353 8354 8355 8356 8357 8358 8359 8360 8361
/**
  Check whether two fields can be considered 'equal' for table
  alteration purposes. Fields are equal if they retain the same
  pack length and if new members are added to the end of the list.

  @return IS_EQUAL_YES if fields are compatible.
          IS_EQUAL_NO otherwise.
*/

8362 8363
uint Field_enum::is_equal(Create_field *new_field)
{
8364 8365 8366 8367 8368 8369
  TYPELIB *values= new_field->interval;

  /*
    The fields are compatible if they have the same flags,
    type, charset and have the same underlying length.
  */
8370
  if (new_field->field_flags_are_binary() != field_flags_are_binary() ||
8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388
      new_field->sql_type != real_type() ||
      new_field->charset != field_charset ||
      new_field->pack_length != pack_length())
    return IS_EQUAL_NO;

  /*
    Changing the definition of an ENUM or SET column by adding a new
    enumeration or set members to the end of the list of valid member
    values only alters table metadata and not table data.
  */
  if (typelib->count > values->count)
    return IS_EQUAL_NO;

  /* Check whether there are modification before the end. */
  if (! compare_type_names(field_charset, typelib, new_field->interval))
    return IS_EQUAL_NO;

  return IS_EQUAL_YES;
unknown's avatar
unknown committed
8389 8390
}

8391

8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410
uchar *Field_enum::pack(uchar *to, const uchar *from,
                        uint max_length, bool low_byte_first)
{
  DBUG_ENTER("Field_enum::pack");
  DBUG_PRINT("debug", ("packlength: %d", packlength));
  DBUG_DUMP("from", from, packlength);

  switch (packlength)
  {
  case 1:
    *to = *from;
    DBUG_RETURN(to + 1);
  case 2: DBUG_RETURN(pack_int16(to, from, low_byte_first));
  case 3: DBUG_RETURN(pack_int24(to, from, low_byte_first));
  case 4: DBUG_RETURN(pack_int32(to, from, low_byte_first));
  case 8: DBUG_RETURN(pack_int64(to, from, low_byte_first));
  default:
    DBUG_ASSERT(0);
  }
8411 8412
  MY_ASSERT_UNREACHABLE();
  DBUG_RETURN(NULL);
8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434
}

const uchar *Field_enum::unpack(uchar *to, const uchar *from,
                                uint param_data, bool low_byte_first)
{
  DBUG_ENTER("Field_enum::unpack");
  DBUG_PRINT("debug", ("packlength: %d", packlength));
  DBUG_DUMP("from", from, packlength);

  switch (packlength)
  {
  case 1:
    *to = *from;
    DBUG_RETURN(from + 1);

  case 2: DBUG_RETURN(unpack_int16(to, from, low_byte_first));
  case 3: DBUG_RETURN(unpack_int24(to, from, low_byte_first));
  case 4: DBUG_RETURN(unpack_int32(to, from, low_byte_first));
  case 8: DBUG_RETURN(unpack_int64(to, from, low_byte_first));
  default:
    DBUG_ASSERT(0);
  }
8435 8436
  MY_ASSERT_UNREACHABLE();
  DBUG_RETURN(NULL);
8437 8438 8439
}


8440 8441 8442 8443
/**
  @return
  returns 1 if the fields are equally defined
*/
unknown's avatar
unknown committed
8444
bool Field_num::eq_def(Field *field)
unknown's avatar
unknown committed
8445
{
unknown's avatar
unknown committed
8446 8447 8448 8449 8450
  if (!Field::eq_def(field))
    return 0;
  Field_num *from_num= (Field_num*) field;

  if (unsigned_flag != from_num->unsigned_flag ||
8451
      (zerofill && !from_num->zerofill && !zero_pack()) ||
unknown's avatar
unknown committed
8452 8453 8454 8455 8456 8457
      dec != from_num->dec)
    return 0;
  return 1;
}


8458 8459 8460 8461 8462 8463
/**
  Check whether two numeric fields can be considered 'equal' for table
  alteration purposes. Fields are equal if they are of the same type
  and retain the same pack length.
*/

unknown's avatar
unknown committed
8464
uint Field_num::is_equal(Create_field *new_field)
unknown's avatar
unknown committed
8465
{
8466
  return ((new_field->sql_type == real_type()) &&
8467 8468
          ((new_field->flags & UNSIGNED_FLAG) == 
           (uint) (flags & UNSIGNED_FLAG)) &&
unknown's avatar
unknown committed
8469 8470
	  ((new_field->flags & AUTO_INCREMENT_FLAG) ==
	   (uint) (flags & AUTO_INCREMENT_FLAG)) &&
8471
          (new_field->pack_length == pack_length()));
unknown's avatar
unknown committed
8472 8473 8474
}


unknown's avatar
unknown committed
8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503
/*
  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'
*/

8504
Field_bit::Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
unknown's avatar
unknown committed
8505
                     uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
unknown's avatar
unknown committed
8506
                     enum utype unireg_check_arg, const char *field_name_arg)
unknown's avatar
unknown committed
8507
  : Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
unknown's avatar
unknown committed
8508
          unireg_check_arg, field_name_arg),
unknown's avatar
unknown committed
8509 8510
    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
8511
{
8512 8513 8514
  DBUG_ENTER("Field_bit::Field_bit");
  DBUG_PRINT("enter", ("ptr_arg: %p, null_ptr_arg: %p, len_arg: %u, bit_len: %u, bytes_in_rec: %u",
                       ptr_arg, null_ptr_arg, len_arg, bit_len, bytes_in_rec));
unknown's avatar
unknown committed
8515
  flags|= UNSIGNED_FLAG;
unknown's avatar
unknown committed
8516 8517 8518 8519 8520 8521
  /*
    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;
8522
  DBUG_VOID_RETURN;
unknown's avatar
unknown committed
8523 8524 8525
}


8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542
void Field_bit::hash(ulong *nr, ulong *nr2)
{
  if (is_null())
  {
    *nr^= (*nr << 1) | 1;
  }
  else
  {
    CHARSET_INFO *cs= &my_charset_bin;
    longlong value= Field_bit::val_int();
    uchar tmp[8];
    mi_int8store(tmp,value);
    cs->coll->hash_sort(cs, tmp, 8, nr, nr2);
  }
}


8543
size_t
8544 8545 8546 8547 8548 8549 8550 8551 8552 8553
Field_bit::do_last_null_byte() const
{
  /*
    Code elsewhere is assuming that bytes are 8 bits, so I'm using
    that value instead of the correct one: CHAR_BIT.

    REFACTOR SUGGESTION (Matz): Change to use the correct number of
    bits. On systems with CHAR_BIT > 8 (not very common), the storage
    will lose the extra bits.
  */
unknown's avatar
unknown committed
8554 8555
  DBUG_PRINT("test", ("bit_ofs: %d, bit_len: %d  bit_ptr: 0x%lx",
                      bit_ofs, bit_len, (long) bit_ptr));
8556 8557 8558 8559 8560 8561 8562 8563
  uchar *result;
  if (bit_len == 0)
    result= null_ptr;
  else if (bit_ofs + bit_len > 8)
    result= bit_ptr + 1;
  else
    result= bit_ptr;

8564
  if (result)
8565 8566
    return (size_t) (result - table->record[0]) + 1;
  return LAST_NULL_BYTE_UNDEF;
8567 8568
}

8569

unknown's avatar
unknown committed
8570
Field *Field_bit::new_key_field(MEM_ROOT *root,
Konstantin Osipov's avatar
Konstantin Osipov committed
8571
                                TABLE *new_table,
8572
                                uchar *new_ptr, uchar *new_null_ptr,
unknown's avatar
unknown committed
8573 8574 8575 8576 8577 8578 8579 8580
                                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 */
8581
    res->bit_ptr= new_ptr;
unknown's avatar
unknown committed
8582 8583 8584 8585 8586 8587 8588 8589
    res->bit_ofs= 0;
    if (bit_len)
      res->ptr++;                               // Store rest of data here
  }
  return res;
}


unknown's avatar
unknown committed
8590
uint Field_bit::is_equal(Create_field *new_field) 
8591 8592 8593 8594 8595 8596
{
  return (new_field->sql_type == real_type() &&
          new_field->length == max_display_length());
}

                       
unknown's avatar
unknown committed
8597 8598
int Field_bit::store(const char *from, uint length, CHARSET_INFO *cs)
{
8599
  ASSERT_COLUMN_MARKED_FOR_WRITE;
unknown's avatar
unknown committed
8600 8601
  int delta;

8602
  for (; length && !*from; from++, length--) ;         // skip left 0's
unknown's avatar
unknown committed
8603
  delta= bytes_in_rec - length;
unknown's avatar
unknown committed
8604 8605 8606 8607 8608

  if (delta < -1 ||
      (delta == -1 && (uchar) *from > ((1 << bit_len) - 1)) ||
      (!bit_len && delta < 0))
  {
8609
    set_rec_bits((1 << bit_len) - 1, bit_ptr, bit_ofs, bit_len);
unknown's avatar
unknown committed
8610
    memset(ptr, 0xff, bytes_in_rec);
8611
    if (table->in_use->really_abort_on_warning())
Marc Alff's avatar
Marc Alff committed
8612
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_DATA_TOO_LONG, 1);
8613 8614
    else
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
unknown's avatar
unknown committed
8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637
    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
8638
    memcpy(ptr, from, bytes_in_rec);
unknown's avatar
unknown committed
8639 8640 8641 8642 8643 8644 8645
  }
  return 0;
}


int Field_bit::store(double nr)
{
8646
  return Field_bit::store((longlong) nr, FALSE);
unknown's avatar
unknown committed
8647 8648 8649
}


8650
int Field_bit::store(longlong nr, bool unsigned_val)
unknown's avatar
unknown committed
8651 8652 8653 8654 8655 8656 8657 8658
{
  char buf[8];

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


unknown's avatar
unknown committed
8659 8660 8661 8662
int Field_bit::store_decimal(const my_decimal *val)
{
  int err= 0;
  longlong i= convert_decimal2longlong(val, 1, &err);
8663
  return test(err | store(i, TRUE));
unknown's avatar
unknown committed
8664 8665 8666
}


unknown's avatar
unknown committed
8667 8668 8669 8670 8671 8672 8673 8674
double Field_bit::val_real(void)
{
  return (double) Field_bit::val_int();
}


longlong Field_bit::val_int(void)
{
8675
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
8676 8677
  ulonglong bits= 0;
  if (bit_len)
8678
  {
unknown's avatar
unknown committed
8679
    bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
unknown's avatar
unknown committed
8680
    bits<<= (bytes_in_rec * 8);
8681
  }
unknown's avatar
unknown committed
8682

unknown's avatar
unknown committed
8683
  switch (bytes_in_rec) {
unknown's avatar
unknown committed
8684
  case 0: return bits;
8685
  case 1: return bits | (ulonglong) ptr[0];
unknown's avatar
unknown committed
8686 8687 8688 8689 8690 8691
  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
8692
  default: return mi_uint8korr(ptr + bytes_in_rec - sizeof(longlong));
unknown's avatar
unknown committed
8693 8694 8695 8696 8697 8698 8699
  }
}  


String *Field_bit::val_str(String *val_buffer,
                           String *val_ptr __attribute__((unused)))
{
8700
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
8701
  char buff[sizeof(longlong)];
unknown's avatar
unknown committed
8702 8703
  uint length= min(pack_length(), sizeof(longlong));
  ulonglong bits= val_int();
unknown's avatar
unknown committed
8704
  mi_int8store(buff,bits);
unknown's avatar
unknown committed
8705 8706

  val_buffer->alloc(length);
8707
  memcpy((char *) val_buffer->ptr(), buff+8-length, length);
unknown's avatar
unknown committed
8708 8709 8710 8711 8712 8713
  val_buffer->length(length);
  val_buffer->set_charset(&my_charset_bin);
  return val_buffer;
}


unknown's avatar
unknown committed
8714 8715
my_decimal *Field_bit::val_decimal(my_decimal *deciaml_value)
{
8716
  ASSERT_COLUMN_MARKED_FOR_READ;
unknown's avatar
unknown committed
8717 8718 8719 8720 8721
  int2my_decimal(E_DEC_FATAL_ERROR, val_int(), 1, deciaml_value);
  return deciaml_value;
}


8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734
/*
  Compare two bit fields using pointers within the record.
  SYNOPSIS
    cmp_max()
    a                 Pointer to field->ptr in first record
    b                 Pointer to field->ptr in second record
    max_len           Maximum length used in index
  DESCRIPTION
    This method is used from key_rec_cmp used by merge sorts used
    by partitioned index read and later other similar places.
    The a and b pointer must be pointers to the field in a record
    (not the table->record[0] necessarily)
*/
8735
int Field_bit::cmp_max(const uchar *a, const uchar *b, uint max_len)
8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750
{
  my_ptrdiff_t a_diff= a - ptr;
  my_ptrdiff_t b_diff= b - ptr;
  if (bit_len)
  {
    int flag;
    uchar bits_a= get_rec_bits(bit_ptr+a_diff, bit_ofs, bit_len);
    uchar bits_b= get_rec_bits(bit_ptr+b_diff, bit_ofs, bit_len);
    if ((flag= (int) (bits_a - bits_b)))
      return flag;
  }
  return memcmp(a, b, field_length);
}


8751
int Field_bit::key_cmp(const uchar *str, uint length)
unknown's avatar
unknown committed
8752 8753 8754 8755 8756
{
  if (bit_len)
  {
    int flag;
    uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
8757
    if ((flag= (int) (bits - *str)))
unknown's avatar
unknown committed
8758 8759 8760 8761
      return flag;
    str++;
    length--;
  }
unknown's avatar
unknown committed
8762
  return memcmp(ptr, str, length);
unknown's avatar
unknown committed
8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775
}


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
8776
  return memcmp(ptr, ptr + row_offset, bytes_in_rec);
unknown's avatar
unknown committed
8777 8778 8779
}


8780
uint Field_bit::get_key_image(uchar *buff, uint length, imagetype type_arg)
unknown's avatar
unknown committed
8781 8782 8783 8784 8785 8786 8787
{
  if (bit_len)
  {
    uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
    *buff++= bits;
    length--;
  }
8788 8789 8790
  uint data_length = min(length, bytes_in_rec);
  memcpy(buff, ptr, data_length);
  return data_length + 1;
unknown's avatar
unknown committed
8791 8792 8793
}


8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806
/**
   Save the field metadata for bit fields.

   Saves the bit length in the first byte and bytes in record in the
   second byte of the field metadata array at index of *metadata_ptr and
   *(metadata_ptr + 1).

   @param   metadata_ptr   First byte of field metadata

   @returns number of bytes written to metadata_ptr
*/
int Field_bit::do_save_field_metadata(uchar *metadata_ptr)
{
8807 8808 8809
  DBUG_ENTER("Field_bit::do_save_field_metadata");
  DBUG_PRINT("debug", ("bit_len: %d, bytes_in_rec: %d",
                       bit_len, bytes_in_rec));
8810 8811 8812 8813 8814 8815 8816
  /*
    Since this class and Field_bit_as_char have different ideas of
    what should be stored here, we compute the values of the metadata
    explicitly using the field_length.
   */
  metadata_ptr[0]= field_length % 8;
  metadata_ptr[1]= field_length / 8;
8817
  DBUG_RETURN(2);
8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841
}


/**
   Returns the number of bytes field uses in row-based replication 
   row packed size.

   This method is used in row-based replication to determine the number
   of bytes that the field consumes in the row record format. This is
   used to skip fields in the master that do not exist on the slave.

   @param   field_metadata   Encoded size in field metadata

   @returns The size of the field based on the field metadata.
*/
uint Field_bit::pack_length_from_metadata(uint field_metadata)
{
  uint const from_len= (field_metadata >> 8U) & 0x00ff;
  uint const from_bit_len= field_metadata & 0x00ff;
  uint const source_size= from_len + ((from_bit_len > 0) ? 1 : 0);
  return (source_size);
}


8842 8843 8844
bool
Field_bit::compatible_field_size(uint field_metadata,
                                 Relay_log_info * __attribute__((unused)),
8845
                                 uint16 mflags,
8846
                                 int *order_var)
8847
{
8848 8849
  DBUG_ENTER("Field_bit::compatible_field_size");
  DBUG_ASSERT((field_metadata >> 16) == 0);
8850
  uint from_bit_len=
8851
    8 * (field_metadata >> 8) + (field_metadata & 0xff);
8852
  uint to_bit_len= max_display_length();
8853 8854
  DBUG_PRINT("debug", ("from_bit_len: %u, to_bit_len: %u",
                       from_bit_len, to_bit_len));
8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867
  /*
    If the bit length exact flag is clear, we are dealing with an old
    master, so we allow some less strict behaviour if replicating by
    moving both bit lengths to an even multiple of 8.

    We do this by computing the number of bytes to store the field
    instead, and then compare the result.
   */
  if (!(mflags & Table_map_log_event::TM_BIT_LEN_EXACT_F)) {
    from_bit_len= (from_bit_len + 7) / 8;
    to_bit_len= (to_bit_len + 7) / 8;
  }

8868 8869
  *order_var= compare(from_bit_len, to_bit_len);
  DBUG_RETURN(TRUE);
8870 8871 8872 8873
}



unknown's avatar
unknown committed
8874 8875 8876 8877
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
8878
                                   "bit(%d)", (int) field_length);
unknown's avatar
unknown committed
8879 8880 8881 8882
  res.length((uint) length);
}


8883 8884 8885
uchar *
Field_bit::pack(uchar *to, const uchar *from, uint max_length,
                bool low_byte_first __attribute__((unused)))
unknown's avatar
unknown committed
8886
{
8887
  DBUG_ASSERT(max_length > 0);
unknown's avatar
unknown committed
8888
  uint length;
unknown's avatar
unknown committed
8889
  if (bit_len > 0)
unknown's avatar
unknown committed
8890
  {
unknown's avatar
unknown committed
8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910
    /*
      We have the following:

      ptr        Points into a field in record R1
      from       Points to a field in a record R2
      bit_ptr    Points to the byte (in the null bytes) that holds the
                 odd bits of R1
      from_bitp  Points to the byte that holds the odd bits of R2

      We have the following:

          ptr - bit_ptr = from - from_bitp

      We want to isolate 'from_bitp', so this gives:

          ptr - bit_ptr - from = - from_bitp
          - ptr + bit_ptr + from = from_bitp
          bit_ptr + from - ptr = from_bitp
     */
    uchar bits= get_rec_bits(bit_ptr + (from - ptr), bit_ofs, bit_len);
unknown's avatar
unknown committed
8911 8912
    *to++= bits;
  }
unknown's avatar
unknown committed
8913 8914
  length= min(bytes_in_rec, max_length - (bit_len > 0));
  memcpy(to, from, length);
unknown's avatar
unknown committed
8915 8916 8917 8918
  return to + length;
}


8919 8920 8921
/**
   Unpack a bit field from row data.

8922
   This method is used to unpack a bit field from a master whose size
8923
   of the field is less than that of the slave.
8924

8925 8926 8927 8928 8929 8930
   @param   to         Destination of the data
   @param   from       Source of the data
   @param   param_data Bit length (upper) and length (lower) values

   @return  New pointer into memory based on from + length of the data
*/
8931 8932 8933
const uchar *
Field_bit::unpack(uchar *to, const uchar *from, uint param_data,
                  bool low_byte_first __attribute__((unused)))
8934
{
8935 8936 8937 8938 8939
  DBUG_ENTER("Field_bit::unpack");
  DBUG_PRINT("enter", ("to: %p, from: %p, param_data: 0x%x",
                       to, from, param_data));
  DBUG_PRINT("debug", ("bit_ptr: %p, bit_len: %u, bit_ofs: %u",
                       bit_ptr, bit_len, bit_ofs));
8940 8941
  uint const from_len= (param_data >> 8U) & 0x00ff;
  uint const from_bit_len= param_data & 0x00ff;
8942 8943
  DBUG_PRINT("debug", ("from_len: %u, from_bit_len: %u",
                       from_len, from_bit_len));
8944
  /*
8945 8946
    If the parameter data is zero (i.e., undefined), or if the master
    and slave have the same sizes, then use the old unpack() method.
8947
  */
8948
  if (param_data == 0 ||
8949
      ((from_bit_len == bit_len) && (from_len == bytes_in_rec)))
8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963
  {
    if (bit_len > 0)
    {
      /*
        set_rec_bits is a macro, don't put the post-increment in the
        argument since that might cause strange side-effects.

        For the choice of the second argument, see the explanation for
        Field_bit::pack().
      */
      set_rec_bits(*from, bit_ptr + (to - ptr), bit_ofs, bit_len);
      from++;
    }
    memcpy(to, from, bytes_in_rec);
8964
    DBUG_RETURN(from + bytes_in_rec);
8965 8966
  }

8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989
  /*
    We are converting a smaller bit field to a larger one here.
    To do that, we first need to construct a raw value for the original
    bit value stored in the from buffer. Then that needs to be converted
    to the larger field then sent to store() for writing to the field.
    Lastly the odd bits need to be masked out if the bytes_in_rec > 0.
    Otherwise stray bits can cause spurious values.
  */
  uint new_len= (field_length + 7) / 8;
  char *value= (char *)my_alloca(new_len);
  bzero(value, new_len);
  uint len= from_len + ((from_bit_len > 0) ? 1 : 0);
  memcpy(value + (new_len - len), from, len);
  /*
    Mask out the unused bits in the partial byte. 
    TODO: Add code to the master to always mask these bits and remove
          the following.
  */
  if ((from_bit_len > 0) && (from_len > 0))
    value[new_len - len]= value[new_len - len] & ((1U << from_bit_len) - 1);
  bitmap_set_bit(table->write_set,field_index);
  store(value, new_len, system_charset_info);
  my_afree(value);
8990
  DBUG_RETURN(from + len);
8991 8992 8993
}


8994 8995
void Field_bit::set_default()
{
8996 8997 8998 8999 9000 9001
  if (bit_len > 0)
  {
    my_ptrdiff_t const offset= table->s->default_values - table->record[0];
    uchar bits= get_rec_bits(bit_ptr + offset, bit_ofs, bit_len);
    set_rec_bits(bits, bit_ptr, bit_ofs, bit_len);
  }
9002 9003 9004
  Field::set_default();
}

9005 9006 9007 9008
/*
  Bit field support for non-MyISAM tables.
*/

9009
Field_bit_as_char::Field_bit_as_char(uchar *ptr_arg, uint32 len_arg,
9010 9011
                                     uchar *null_ptr_arg, uchar null_bit_arg,
                                     enum utype unireg_check_arg,
unknown's avatar
unknown committed
9012
                                     const char *field_name_arg)
9013
  :Field_bit(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, 0, 0,
unknown's avatar
unknown committed
9014
             unireg_check_arg, field_name_arg)
9015
{
unknown's avatar
unknown committed
9016
  flags|= UNSIGNED_FLAG;
9017
  bit_len= 0;
unknown's avatar
unknown committed
9018
  bytes_in_rec= (len_arg + 7) / 8;
9019 9020 9021 9022 9023
}


int Field_bit_as_char::store(const char *from, uint length, CHARSET_INFO *cs)
{
9024
  ASSERT_COLUMN_MARKED_FOR_WRITE;
9025
  int delta;
9026
  uchar bits= (uchar) (field_length & 7);
9027

9028
  for (; length && !*from; from++, length--) ;         // skip left 0's
unknown's avatar
unknown committed
9029
  delta= bytes_in_rec - length;
9030 9031 9032 9033

  if (delta < 0 ||
      (delta == 0 && bits && (uint) (uchar) *from >= (uint) (1 << bits)))
  {
unknown's avatar
unknown committed
9034
    memset(ptr, 0xff, bytes_in_rec);
9035
    if (bits)
9036
      *ptr&= ((1 << bits) - 1); /* set first uchar */
9037
    if (table->in_use->really_abort_on_warning())
Marc Alff's avatar
Marc Alff committed
9038
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_DATA_TOO_LONG, 1);
9039 9040
    else
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052
    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
9053
                                   "bit(%d)", (int) field_length);
9054 9055 9056 9057
  res.length((uint) length);
}


unknown's avatar
unknown committed
9058
/*****************************************************************************
unknown's avatar
unknown committed
9059
  Handling of field and Create_field
unknown's avatar
unknown committed
9060 9061
*****************************************************************************/

9062 9063
/**
  Convert create_field::length from number of characters to number of bytes.
9064 9065
*/

unknown's avatar
unknown committed
9066
void Create_field::create_length_to_internal_length(void)
9067
{
unknown's avatar
unknown committed
9068 9069 9070 9071 9072 9073 9074
  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:
9075
  case MYSQL_TYPE_VARCHAR:
unknown's avatar
unknown committed
9076
    length*= charset->mbmaxlen;
9077
    key_length= length;
9078
    pack_length= calc_pack_length(sql_type, length);
unknown's avatar
unknown committed
9079 9080 9081
    break;
  case MYSQL_TYPE_ENUM:
  case MYSQL_TYPE_SET:
9082
    /* Pack_length already calculated in sql_parse.cc */
unknown's avatar
unknown committed
9083
    length*= charset->mbmaxlen;
9084
    key_length= pack_length;
unknown's avatar
unknown committed
9085
    break;
unknown's avatar
unknown committed
9086
  case MYSQL_TYPE_BIT:
9087 9088 9089 9090 9091 9092 9093 9094 9095 9096
    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
9097
    break;
unknown's avatar
unknown committed
9098
  case MYSQL_TYPE_NEWDECIMAL:
unknown's avatar
unknown committed
9099 9100 9101 9102 9103 9104
    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
9105
    break;
unknown's avatar
unknown committed
9106
  default:
9107
    key_length= pack_length= calc_pack_length(sql_type, length);
unknown's avatar
unknown committed
9108
    break;
9109 9110 9111
  }
}

9112

9113 9114 9115
/**
  Init for a tmp table field. To be extended if need be.
*/
unknown's avatar
unknown committed
9116
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
9117
                                      uint32 length_arg, uint32 decimals_arg,
9118
                                      bool maybe_null, bool is_unsigned,
9119
                                      uint pack_length_arg)
9120
{
9121 9122
  DBUG_ENTER("Create_field::init_for_tmp_table");

9123 9124
  field_name= "";
  sql_type= sql_type_arg;
unknown's avatar
unknown committed
9125
  char_length= length= length_arg;;
9126 9127 9128 9129
  unireg_check= Field::NONE;
  interval= 0;
  charset= &my_charset_bin;
  geom_type= Field::GEOM_GEOMETRY;
9130 9131

  DBUG_PRINT("enter", ("sql_type: %d, length: %u, pack_length: %u",
9132
                       sql_type_arg, length_arg, pack_length_arg));
9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158

  /*
    These pack flags are crafted to get it correctly through the
    branches of make_field().
   */
  switch (sql_type_arg)
  {
  case MYSQL_TYPE_VARCHAR:
  case MYSQL_TYPE_VAR_STRING:
  case MYSQL_TYPE_STRING:
  case MYSQL_TYPE_SET:
    pack_flag= 0;
    break;

  case MYSQL_TYPE_GEOMETRY:
    pack_flag= FIELDFLAG_GEOM;
    break;

  case MYSQL_TYPE_ENUM:
    pack_flag= FIELDFLAG_INTERVAL;
    break;

  case MYSQL_TYPE_DECIMAL:
  case MYSQL_TYPE_NEWDECIMAL:
  case MYSQL_TYPE_FLOAT:
  case MYSQL_TYPE_DOUBLE:
9159
    pack_flag= FIELDFLAG_NUMBER |
9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195
      (decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT;
    break;

  case MYSQL_TYPE_TINY_BLOB:
  case MYSQL_TYPE_MEDIUM_BLOB:
  case MYSQL_TYPE_LONG_BLOB:
  case MYSQL_TYPE_BLOB:
    pack_flag= FIELDFLAG_BLOB;
    break;

  case MYSQL_TYPE_BIT:
    pack_flag= FIELDFLAG_NUMBER | FIELDFLAG_TREAT_BIT_AS_CHAR;
    break;

  default:
    pack_flag= FIELDFLAG_NUMBER;
    break;
  }

  /*
    Set the pack flag correctly for the blob-like types. This sets the
    packtype to something that make_field can use. If the pack type is
    not set correctly, the packlength will be reeeeally wierd (like
    129 or so).
   */
  switch (sql_type_arg)
  {
  case MYSQL_TYPE_ENUM:
  case MYSQL_TYPE_SET:
  case MYSQL_TYPE_TINY_BLOB:
  case MYSQL_TYPE_MEDIUM_BLOB:
  case MYSQL_TYPE_LONG_BLOB:
  case MYSQL_TYPE_BLOB:
  case MYSQL_TYPE_GEOMETRY:
    // If you are going to use the above types, you have to pass a
    // pack_length as parameter. Assert that is really done.
9196 9197
    DBUG_ASSERT(pack_length_arg != ~0U);
    pack_flag|= pack_length_to_packflag(pack_length_arg);
9198 9199 9200 9201 9202 9203 9204 9205 9206 9207
    break;
  default:
    /* Nothing */
    break;
  }

  pack_flag|=
    (maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
    (is_unsigned ? 0 : FIELDFLAG_DECIMAL);

9208
  DBUG_PRINT("debug", ("pack_flag: %s%s%s%s%s%s, pack_type: %d",
9209 9210 9211 9212 9213
                       FLAGSTR(pack_flag, FIELDFLAG_BINARY),
                       FLAGSTR(pack_flag, FIELDFLAG_NUMBER),
                       FLAGSTR(pack_flag, FIELDFLAG_INTERVAL),
                       FLAGSTR(pack_flag, FIELDFLAG_GEOM),
                       FLAGSTR(pack_flag, FIELDFLAG_BLOB),
9214
                       FLAGSTR(pack_flag, FIELDFLAG_DECIMAL),
9215 9216
                       f_packtype(pack_flag)));
  DBUG_VOID_RETURN;
9217 9218 9219
}


9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237
/**
  Initialize field definition for create.

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

  @retval
9238
    FALSE on success
9239
  @retval
9240 9241 9242
    TRUE  on error
*/

unknown's avatar
unknown committed
9243
bool Create_field::init(THD *thd, char *fld_name, enum_field_types fld_type,
9244 9245 9246 9247 9248 9249 9250 9251 9252
                        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;

unknown's avatar
unknown committed
9253
  DBUG_ENTER("Create_field::init()");
9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279
  
  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;
  /*
9280 9281
    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.
9282 9283
  */
  if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
9284
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != MYSQL_TYPE_TIMESTAMP)
9285 9286
    flags|= NO_DEFAULT_VALUE_FLAG;

9287
  if (fld_length != NULL)
9288 9289 9290
  {
    errno= 0;
    length= strtoul(fld_length, NULL, 10);
9291
    if ((errno != 0) || (length > MAX_FIELD_BLOBLENGTH))
9292 9293 9294 9295 9296 9297
    {
      my_error(ER_TOO_BIG_DISPLAYWIDTH, MYF(0), fld_name, MAX_FIELD_BLOBLENGTH);
      DBUG_RETURN(TRUE);
    }

    if (length == 0)
9298
      fld_length= NULL; /* purecov: inspected */
9299 9300
  }

9301 9302 9303
  sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;

  switch (fld_type) {
9304
  case MYSQL_TYPE_TINY:
9305 9306 9307 9308
    if (!fld_length)
      length= MAX_TINYINT_WIDTH+sign_len;
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
    break;
9309
  case MYSQL_TYPE_SHORT:
9310 9311 9312 9313
    if (!fld_length)
      length= MAX_SMALLINT_WIDTH+sign_len;
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
    break;
9314
  case MYSQL_TYPE_INT24:
9315 9316 9317 9318
    if (!fld_length)
      length= MAX_MEDIUMINT_WIDTH+sign_len;
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
    break;
9319
  case MYSQL_TYPE_LONG:
9320 9321 9322 9323
    if (!fld_length)
      length= MAX_INT_WIDTH+sign_len;
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
    break;
9324
  case MYSQL_TYPE_LONGLONG:
9325 9326 9327 9328
    if (!fld_length)
      length= MAX_BIGINT_WIDTH;
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
    break;
9329
  case MYSQL_TYPE_NULL:
9330
    break;
9331
  case MYSQL_TYPE_NEWDECIMAL:
9332
    my_decimal_trim(&length, &decimals);
9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358
    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;
9359 9360 9361 9362 9363
  case MYSQL_TYPE_BLOB:
  case MYSQL_TYPE_TINY_BLOB:
  case MYSQL_TYPE_LONG_BLOB:
  case MYSQL_TYPE_MEDIUM_BLOB:
  case MYSQL_TYPE_GEOMETRY:
9364 9365 9366 9367 9368
    if (fld_default_value)
    {
      /* Allow empty as default value. */
      String str,*res;
      res= fld_default_value->val_str(&str);
9369 9370 9371 9372 9373 9374 9375
      /*
        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)))
9376 9377 9378 9379 9380
      {
        my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
                 fld_name); /* purecov: inspected */
        DBUG_RETURN(TRUE);
      }
9381 9382 9383 9384 9385 9386 9387 9388 9389 9390
      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);
      }
9391 9392 9393 9394
      def= 0;
    }
    flags|= BLOB_FLAG;
    break;
9395
  case MYSQL_TYPE_YEAR:
9396 9397 9398 9399
    if (!fld_length || length != 2)
      length= 4; /* Default length */
    flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
    break;
9400
  case MYSQL_TYPE_FLOAT:
9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412
    /* 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)
      {
9413
        sql_type= MYSQL_TYPE_DOUBLE;
9414
        length= MAX_DOUBLE_STR_LENGTH; 
9415 9416
      }
      else
9417
        length= MAX_FLOAT_STR_LENGTH; 
9418 9419 9420 9421 9422
      decimals= NOT_FIXED_DEC;
      break;
    }
    if (!fld_length && !fld_decimals)
    {
9423
      length=  MAX_FLOAT_STR_LENGTH;
9424 9425 9426 9427 9428 9429 9430 9431 9432
      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;
9433
  case MYSQL_TYPE_DOUBLE:
9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446
    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;
9447
  case MYSQL_TYPE_TIMESTAMP:
9448
    if (fld_length == NULL)
9449
    {
9450
      length= MAX_DATETIME_WIDTH;
9451 9452
    }
    else if (length != MAX_DATETIME_WIDTH)
9453 9454 9455
    {
      /*
        We support only even TIMESTAMP lengths less or equal than 14
9456 9457
        and 19 as length of 4.1 compatible representation.  Silently 
        shrink it to MAX_DATETIME_COMPRESSED_WIDTH.
9458
      */
unknown's avatar
unknown committed
9459
      DBUG_ASSERT(MAX_DATETIME_COMPRESSED_WIDTH < UINT_MAX);
9460 9461 9462
      if (length != UINT_MAX)  /* avoid overflow; is safe because of min() */
        length= ((length+1)/2)*2;
      length= min(length, MAX_DATETIME_COMPRESSED_WIDTH);
9463 9464
    }
    flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
9465 9466 9467 9468 9469 9470
    /*
      Since we silently rewrite down to MAX_DATETIME_COMPRESSED_WIDTH bytes,
      the parser should not raise errors unless bizzarely large. 
     */
    max_field_charlength= UINT_MAX;

9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508
    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;
9509
  case MYSQL_TYPE_DATE:
9510
    /* Old date type. */
9511
    sql_type= MYSQL_TYPE_NEWDATE;
9512
    /* fall trough */
9513
  case MYSQL_TYPE_NEWDATE:
9514
    length= MAX_DATE_WIDTH;
9515
    break;
9516
  case MYSQL_TYPE_TIME:
9517 9518
    length= 10;
    break;
9519
  case MYSQL_TYPE_DATETIME:
9520
    length= MAX_DATETIME_WIDTH;
9521
    break;
9522
  case MYSQL_TYPE_SET:
9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537
    {
      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;
    }
9538
  case MYSQL_TYPE_ENUM:
9539 9540 9541 9542 9543 9544 9545 9546
    {
      /* 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);
9547
      length= 1; /* See comment for MYSQL_TYPE_SET above. */
9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565
      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;
    }
9566
  case MYSQL_TYPE_DECIMAL:
9567 9568
    DBUG_ASSERT(0); /* Was obsolete */
  }
unknown's avatar
unknown committed
9569 9570
  /* Remember the value of length */
  char_length= length;
9571 9572

  if (!(flags & BLOB_FLAG) &&
9573 9574
      ((length > max_field_charlength && fld_type != MYSQL_TYPE_SET &&
        fld_type != MYSQL_TYPE_ENUM &&
9575
        (fld_type != MYSQL_TYPE_VARCHAR || fld_default_value)) ||
9576
       ((length == 0) &&
9577
        fld_type != MYSQL_TYPE_STRING &&
9578
        fld_type != MYSQL_TYPE_VARCHAR && fld_type != MYSQL_TYPE_GEOMETRY)))
9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594
  {
    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);
  }

9595 9596 9597 9598 9599 9600
  switch (fld_type) {
  case MYSQL_TYPE_DATE:
  case MYSQL_TYPE_NEWDATE:
  case MYSQL_TYPE_TIME:
  case MYSQL_TYPE_DATETIME:
  case MYSQL_TYPE_TIMESTAMP:
9601 9602
    charset= &my_charset_numeric;
    flags|= BINARY_FLAG;
9603 9604 9605
  default: break;
  }

9606 9607 9608 9609
  DBUG_RETURN(FALSE); /* success */
}


9610 9611 9612 9613
enum_field_types get_blob_type_from_length(ulong length)
{
  enum_field_types type;
  if (length < 256)
9614
    type= MYSQL_TYPE_TINY_BLOB;
9615
  else if (length < 65536)
9616
    type= MYSQL_TYPE_BLOB;
9617
  else if (length < 256L*256L*256L)
9618
    type= MYSQL_TYPE_MEDIUM_BLOB;
9619
  else
9620
    type= MYSQL_TYPE_LONG_BLOB;
9621 9622 9623 9624
  return type;
}


unknown's avatar
unknown committed
9625
/*
unknown's avatar
unknown committed
9626
  Make a field from the .frm file info
unknown's avatar
unknown committed
9627 9628 9629 9630 9631
*/

uint32 calc_pack_length(enum_field_types type,uint32 length)
{
  switch (type) {
9632
  case MYSQL_TYPE_VAR_STRING:
9633
  case MYSQL_TYPE_STRING:
9634
  case MYSQL_TYPE_DECIMAL:     return (length);
9635
  case MYSQL_TYPE_VARCHAR:     return (length + (length < 256 ? 1: 2));
9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657
  case MYSQL_TYPE_YEAR:
  case MYSQL_TYPE_TINY	: return 1;
  case MYSQL_TYPE_SHORT : return 2;
  case MYSQL_TYPE_INT24:
  case MYSQL_TYPE_NEWDATE:
  case MYSQL_TYPE_TIME:   return 3;
  case MYSQL_TYPE_TIMESTAMP:
  case MYSQL_TYPE_DATE:
  case MYSQL_TYPE_LONG	: return 4;
  case MYSQL_TYPE_FLOAT : return sizeof(float);
  case MYSQL_TYPE_DOUBLE: return sizeof(double);
  case MYSQL_TYPE_DATETIME:
  case MYSQL_TYPE_LONGLONG: return 8;	/* Don't crash if no longlong */
  case MYSQL_TYPE_NULL	: return 0;
  case MYSQL_TYPE_TINY_BLOB:	return 1+portable_sizeof_char_ptr;
  case MYSQL_TYPE_BLOB:		return 2+portable_sizeof_char_ptr;
  case MYSQL_TYPE_MEDIUM_BLOB:	return 3+portable_sizeof_char_ptr;
  case MYSQL_TYPE_LONG_BLOB:	return 4+portable_sizeof_char_ptr;
  case MYSQL_TYPE_GEOMETRY:	return 4+portable_sizeof_char_ptr;
  case MYSQL_TYPE_SET:
  case MYSQL_TYPE_ENUM:
  case MYSQL_TYPE_NEWDECIMAL:
9658
    abort(); return 0;                          // This shouldn't happen
9659
  case MYSQL_TYPE_BIT: return length / 8;
9660 9661
  default:
    return 0;
unknown's avatar
unknown committed
9662 9663 9664 9665 9666 9667 9668
  }
}


uint pack_length_to_packflag(uint type)
{
  switch (type) {
9669 9670 9671 9672 9673
    case 1: return f_settype((uint) MYSQL_TYPE_TINY);
    case 2: return f_settype((uint) MYSQL_TYPE_SHORT);
    case 3: return f_settype((uint) MYSQL_TYPE_INT24);
    case 4: return f_settype((uint) MYSQL_TYPE_LONG);
    case 8: return f_settype((uint) MYSQL_TYPE_LONGLONG);
unknown's avatar
unknown committed
9674 9675 9676 9677 9678
  }
  return 0;					// This shouldn't happen
}


9679
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
unknown's avatar
unknown committed
9680
		  uchar *null_pos, uchar null_bit,
unknown's avatar
unknown committed
9681
		  uint pack_flag,
9682
		  enum_field_types field_type,
9683
		  CHARSET_INFO *field_charset,
unknown's avatar
unknown committed
9684
		  Field::geometry_type geom_type,
unknown's avatar
unknown committed
9685 9686
		  Field::utype unireg_check,
		  TYPELIB *interval,
unknown's avatar
unknown committed
9687
		  const char *field_name)
unknown's avatar
unknown committed
9688
{
9689 9690
  uchar *UNINIT_VAR(bit_ptr);
  uchar UNINIT_VAR(bit_offset);
9691
  if (field_type == MYSQL_TYPE_BIT && !f_bit_as_char(pack_flag))
unknown's avatar
unknown committed
9692 9693 9694 9695 9696 9697 9698 9699 9700 9701
  {
    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
9702 9703 9704 9705 9706
  if (!f_maybe_null(pack_flag))
  {
    null_pos=0;
    null_bit=0;
  }
unknown's avatar
unknown committed
9707 9708 9709 9710
  else
  {
    null_bit= ((uchar) 1) << null_bit;
  }
unknown's avatar
unknown committed
9711

9712
  switch (field_type) {
9713 9714 9715 9716 9717
  case MYSQL_TYPE_DATE:
  case MYSQL_TYPE_NEWDATE:
  case MYSQL_TYPE_TIME:
  case MYSQL_TYPE_DATETIME:
  case MYSQL_TYPE_TIMESTAMP:
9718
    field_charset= &my_charset_numeric;
9719
  default: break;
unknown's avatar
unknown committed
9720 9721
  }

9722 9723 9724 9725 9726 9727 9728 9729
  DBUG_PRINT("debug", ("field_type: %d, field_length: %u, interval: %p, pack_flag: %s%s%s%s%s",
                       field_type, field_length, interval,
                       FLAGSTR(pack_flag, FIELDFLAG_BINARY),
                       FLAGSTR(pack_flag, FIELDFLAG_INTERVAL),
                       FLAGSTR(pack_flag, FIELDFLAG_NUMBER),
                       FLAGSTR(pack_flag, FIELDFLAG_PACK),
                       FLAGSTR(pack_flag, FIELDFLAG_BLOB)));

unknown's avatar
unknown committed
9730 9731 9732
  if (f_is_alpha(pack_flag))
  {
    if (!f_is_packed(pack_flag))
unknown's avatar
unknown committed
9733
    {
9734
      if (field_type == MYSQL_TYPE_STRING ||
9735
          field_type == MYSQL_TYPE_DECIMAL ||   // 3.23 or 4.0 string
9736
          field_type == MYSQL_TYPE_VAR_STRING)
unknown's avatar
unknown committed
9737
        return new Field_string(ptr,field_length,null_pos,null_bit,
unknown's avatar
unknown committed
9738
                                unireg_check, field_name,
unknown's avatar
unknown committed
9739
                                field_charset);
9740
      if (field_type == MYSQL_TYPE_VARCHAR)
9741 9742 9743
        return new Field_varstring(ptr,field_length,
                                   HA_VARCHAR_PACKLENGTH(field_length),
                                   null_pos,null_bit,
unknown's avatar
unknown committed
9744 9745
                                   unireg_check, field_name,
                                   share,
9746
                                   field_charset);
unknown's avatar
unknown committed
9747 9748
      return 0;                                 // Error
    }
unknown's avatar
unknown committed
9749 9750 9751 9752 9753

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

unknown's avatar
unknown committed
9754
#ifdef HAVE_SPATIAL
unknown's avatar
unknown committed
9755 9756
    if (f_is_geom(pack_flag))
      return new Field_geom(ptr,null_pos,null_bit,
unknown's avatar
unknown committed
9757
			    unireg_check, field_name, share,
unknown's avatar
unknown committed
9758
			    pack_length, geom_type);
unknown's avatar
unknown committed
9759
#endif
unknown's avatar
unknown committed
9760 9761
    if (f_is_blob(pack_flag))
      return new Field_blob(ptr,null_pos,null_bit,
unknown's avatar
unknown committed
9762
			    unireg_check, field_name, share,
unknown's avatar
unknown committed
9763
			    pack_length, field_charset);
unknown's avatar
unknown committed
9764 9765 9766 9767
    if (interval)
    {
      if (f_is_enum(pack_flag))
	return new Field_enum(ptr,field_length,null_pos,null_bit,
unknown's avatar
unknown committed
9768
				  unireg_check, field_name,
9769
				  pack_length, interval, field_charset);
unknown's avatar
unknown committed
9770 9771
      else
	return new Field_set(ptr,field_length,null_pos,null_bit,
unknown's avatar
unknown committed
9772
			     unireg_check, field_name,
9773
			     pack_length, interval, field_charset);
unknown's avatar
unknown committed
9774 9775 9776
    }
  }

9777
  switch (field_type) {
9778
  case MYSQL_TYPE_DECIMAL:
unknown's avatar
unknown committed
9779
    return new Field_decimal(ptr,field_length,null_pos,null_bit,
unknown's avatar
unknown committed
9780
			     unireg_check, field_name,
unknown's avatar
unknown committed
9781 9782 9783
			     f_decimals(pack_flag),
			     f_is_zerofill(pack_flag) != 0,
			     f_is_dec(pack_flag) == 0);
9784
  case MYSQL_TYPE_NEWDECIMAL:
unknown's avatar
unknown committed
9785
    return new Field_new_decimal(ptr,field_length,null_pos,null_bit,
unknown's avatar
unknown committed
9786
                                 unireg_check, field_name,
unknown's avatar
unknown committed
9787 9788 9789
                                 f_decimals(pack_flag),
                                 f_is_zerofill(pack_flag) != 0,
                                 f_is_dec(pack_flag) == 0);
9790
  case MYSQL_TYPE_FLOAT:
unknown's avatar
unknown committed
9791
    return new Field_float(ptr,field_length,null_pos,null_bit,
unknown's avatar
unknown committed
9792
			   unireg_check, field_name,
unknown's avatar
unknown committed
9793 9794 9795
			   f_decimals(pack_flag),
			   f_is_zerofill(pack_flag) != 0,
			   f_is_dec(pack_flag)== 0);
9796
  case MYSQL_TYPE_DOUBLE:
unknown's avatar
unknown committed
9797
    return new Field_double(ptr,field_length,null_pos,null_bit,
unknown's avatar
unknown committed
9798
			    unireg_check, field_name,
unknown's avatar
unknown committed
9799 9800 9801
			    f_decimals(pack_flag),
			    f_is_zerofill(pack_flag) != 0,
			    f_is_dec(pack_flag)== 0);
9802
  case MYSQL_TYPE_TINY:
unknown's avatar
unknown committed
9803
    return new Field_tiny(ptr,field_length,null_pos,null_bit,
unknown's avatar
unknown committed
9804
			  unireg_check, field_name,
unknown's avatar
unknown committed
9805 9806
			  f_is_zerofill(pack_flag) != 0,
			  f_is_dec(pack_flag) == 0);
9807
  case MYSQL_TYPE_SHORT:
unknown's avatar
unknown committed
9808
    return new Field_short(ptr,field_length,null_pos,null_bit,
unknown's avatar
unknown committed
9809
			   unireg_check, field_name,
unknown's avatar
unknown committed
9810 9811
			   f_is_zerofill(pack_flag) != 0,
			   f_is_dec(pack_flag) == 0);
9812
  case MYSQL_TYPE_INT24:
unknown's avatar
unknown committed
9813
    return new Field_medium(ptr,field_length,null_pos,null_bit,
unknown's avatar
unknown committed
9814
			    unireg_check, field_name,
unknown's avatar
unknown committed
9815 9816
			    f_is_zerofill(pack_flag) != 0,
			    f_is_dec(pack_flag) == 0);
9817
  case MYSQL_TYPE_LONG:
unknown's avatar
unknown committed
9818
    return new Field_long(ptr,field_length,null_pos,null_bit,
unknown's avatar
unknown committed
9819
			   unireg_check, field_name,
unknown's avatar
unknown committed
9820 9821
			   f_is_zerofill(pack_flag) != 0,
			   f_is_dec(pack_flag) == 0);
9822
  case MYSQL_TYPE_LONGLONG:
unknown's avatar
unknown committed
9823
    return new Field_longlong(ptr,field_length,null_pos,null_bit,
unknown's avatar
unknown committed
9824
			      unireg_check, field_name,
unknown's avatar
unknown committed
9825 9826
			      f_is_zerofill(pack_flag) != 0,
			      f_is_dec(pack_flag) == 0);
9827
  case MYSQL_TYPE_TIMESTAMP:
9828
    return new Field_timestamp(ptr,field_length, null_pos, null_bit,
unknown's avatar
unknown committed
9829
                               unireg_check, field_name, share,
9830
                               field_charset);
9831
  case MYSQL_TYPE_YEAR:
unknown's avatar
unknown committed
9832
    return new Field_year(ptr,field_length,null_pos,null_bit,
unknown's avatar
unknown committed
9833
			  unireg_check, field_name);
9834
  case MYSQL_TYPE_DATE:
unknown's avatar
unknown committed
9835
    return new Field_date(ptr,null_pos,null_bit,
unknown's avatar
unknown committed
9836
			  unireg_check, field_name, field_charset);
9837
  case MYSQL_TYPE_NEWDATE:
unknown's avatar
unknown committed
9838
    return new Field_newdate(ptr,null_pos,null_bit,
unknown's avatar
unknown committed
9839
			     unireg_check, field_name, field_charset);
9840
  case MYSQL_TYPE_TIME:
unknown's avatar
unknown committed
9841
    return new Field_time(ptr,null_pos,null_bit,
unknown's avatar
unknown committed
9842
			  unireg_check, field_name, field_charset);
9843
  case MYSQL_TYPE_DATETIME:
unknown's avatar
unknown committed
9844
    return new Field_datetime(ptr,null_pos,null_bit,
unknown's avatar
unknown committed
9845
			      unireg_check, field_name, field_charset);
9846
  case MYSQL_TYPE_NULL:
unknown's avatar
unknown committed
9847 9848
    return new Field_null(ptr, field_length, unireg_check, field_name,
                          field_charset);
9849
  case MYSQL_TYPE_BIT:
9850
    return f_bit_as_char(pack_flag) ?
9851
           new Field_bit_as_char(ptr, field_length, null_pos, null_bit,
9852
                                 unireg_check, field_name) :
9853
           new Field_bit(ptr, field_length, null_pos, null_bit, bit_ptr,
9854
                         bit_offset, unireg_check, field_name);
unknown's avatar
unknown committed
9855

9856 9857
  default:					// Impossible (Wrong version)
    break;
unknown's avatar
unknown committed
9858
  }
unknown's avatar
unknown committed
9859
  return 0;
unknown's avatar
unknown committed
9860 9861 9862
}


9863
/** Create a field suitable for create of table. */
unknown's avatar
unknown committed
9864

unknown's avatar
unknown committed
9865
Create_field::Create_field(Field *old_field,Field *orig_field)
unknown's avatar
unknown committed
9866 9867 9868 9869 9870 9871 9872
{
  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();
9873
  key_length= old_field->key_length();
unknown's avatar
unknown committed
9874
  sql_type=   old_field->real_type();
unknown's avatar
unknown committed
9875 9876
  charset=    old_field->charset();		// May be NULL ptr
  comment=    old_field->comment;
9877
  decimals=   old_field->decimals();
unknown's avatar
unknown committed
9878 9879 9880

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

9884
  switch (sql_type) {
9885
  case MYSQL_TYPE_BLOB:
9886
    switch (pack_length - portable_sizeof_char_ptr) {
9887 9888 9889 9890
    case  1: sql_type= MYSQL_TYPE_TINY_BLOB; break;
    case  2: sql_type= MYSQL_TYPE_BLOB; break;
    case  3: sql_type= MYSQL_TYPE_MEDIUM_BLOB; break;
    default: sql_type= MYSQL_TYPE_LONG_BLOB; break;
9891
    }
9892
    length/= charset->mbmaxlen;
9893 9894
    key_length/= charset->mbmaxlen;
    break;
9895
  case MYSQL_TYPE_STRING:
unknown's avatar
unknown committed
9896
    /* Change CHAR -> VARCHAR if dynamic record length */
9897 9898 9899 9900 9901 9902 9903 9904
    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:
9905
    /* This is corrected in create_length_to_internal_length */
9906 9907 9908
    length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
    break;
#ifdef HAVE_SPATIAL
9909
  case MYSQL_TYPE_GEOMETRY:
9910 9911 9912 9913 9914
    geom_type= ((Field_geom*)old_field)->geom_type;
    break;
#endif
  default:
    break;
unknown's avatar
unknown committed
9915
  }
9916

unknown's avatar
unknown committed
9917 9918 9919 9920
  if (flags & (ENUM_FLAG | SET_FLAG))
    interval= ((Field_enum*) old_field)->typelib;
  else
    interval=0;
9921
  def=0;
9922
  char_length= length;
9923

9924
  if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
9925
      old_field->ptr && orig_field &&
9926
      (sql_type != MYSQL_TYPE_TIMESTAMP ||                /* set def only if */
9927 9928
       old_field->table->timestamp_field != old_field ||  /* timestamp field */ 
       unireg_check == Field::TIMESTAMP_UN_FIELD))        /* has default val */
unknown's avatar
unknown committed
9929
  {
9930 9931
    char buff[MAX_FIELD_WIDTH];
    String tmp(buff,sizeof(buff), charset);
9932
    my_ptrdiff_t diff;
9933

unknown's avatar
unknown committed
9934
    /* Get the value from default_values */
9935 9936
    diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
                          orig_field->table->record[0]);
unknown's avatar
unknown committed
9937
    orig_field->move_field_offset(diff);	// Points now at default_values
9938
    if (!orig_field->is_real_null())
9939
    {
9940 9941
      char buff[MAX_FIELD_WIDTH], *pos;
      String tmp(buff, sizeof(buff), charset), *res;
9942
      res= orig_field->val_str(&tmp);
9943 9944
      pos= (char*) sql_strmake(res->ptr(), res->length());
      def= new Item_string(pos, res->length(), charset);
9945
    }
9946
    orig_field->move_field_offset(-diff);	// Back to record[0]
unknown's avatar
unknown committed
9947 9948
  }
}
9949 9950


9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983
/**
  maximum possible character length for blob.
  
  This method is used in Item_field::set_field to calculate
  max_length for Item.
  
  For example:
    CREATE TABLE t2 SELECT CONCAT(tinyblob_utf8_column) FROM t1;
  must create a "VARCHAR(255) CHARACTER SET utf8" column.
  
  @return
    length
*/

uint32 Field_blob::char_length()
{
  switch (packlength)
  {
  case 1:
    return 255;
  case 2:
    return 65535;
  case 3:
    return 16777215;
  case 4:
    return (uint32) 4294967295U;
  default:
    DBUG_ASSERT(0); // we should never go here
    return 0;
  }
}


9984 9985
/**
  maximum possible display length for blob.
9986

9987
  @return
9988 9989
    length
*/
9990 9991

uint32 Field_blob::max_display_length()
9992 9993 9994 9995
{
  switch (packlength)
  {
  case 1:
9996
    return 255 * field_charset->mbmaxlen;
9997
  case 2:
9998
    return 65535 * field_charset->mbmaxlen;
9999
  case 3:
10000
    return 16777215 * field_charset->mbmaxlen;
10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012
  case 4:
    return (uint32) 4294967295U;
  default:
    DBUG_ASSERT(0); // we should never go here
    return 0;
  }
}


/*****************************************************************************
 Warning handling
*****************************************************************************/
10013

10014 10015
/**
  Produce warning or note about data saved into field.
10016

10017 10018 10019
  @param level            - level of message (Note/Warning/Error)
  @param code             - error code of message to be produced
  @param cuted_increment  - whenever we should increase cut fields count or not
10020

10021
  @note
10022
    This function won't produce warning and increase cut fields counter
10023 10024 10025 10026
    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().
10027

10028
  @retval
10029
    1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
10030
  @retval
10031
    0 otherwise
10032
*/
10033

10034
bool 
10035 10036
Field::set_warning(MYSQL_ERROR::enum_warning_level level, uint code,
                   int cuted_increment)
10037
{
10038 10039 10040 10041 10042
  /*
    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
10043 10044
  if (thd->count_cuted_fields)
  {
10045
    thd->cuted_fields+= cuted_increment;
10046
    push_warning_printf(thd, level, code, ER(code), field_name,
Marc Alff's avatar
Marc Alff committed
10047
                        thd->warning_info->current_row_for_warning());
10048 10049
    return 0;
  }
10050
  return level >= MYSQL_ERROR::WARN_LEVEL_WARN;
10051 10052 10053
}


10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066
/**
  Produce warning or note about datetime string data saved into field.

  @param level            level of message (Note/Warning/Error)
  @param code             error code of message to be produced
  @param str              string value which we tried to save
  @param str_length       length of string which we tried to save
  @param ts_type          type of datetime value (datetime/date/time)
  @param 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
10067 10068
    thread.
*/
10069

10070
void 
10071
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
10072 10073 10074
                            const char *str, uint str_length, 
                            timestamp_type ts_type, int cuted_increment)
{
unknown's avatar
unknown committed
10075
  THD *thd= table ? table->in_use : current_thd;
10076 10077
  if ((thd->really_abort_on_warning() &&
       level >= MYSQL_ERROR::WARN_LEVEL_WARN) ||
unknown's avatar
unknown committed
10078
      set_warning(level, code, cuted_increment))
10079
    make_truncated_value_warning(thd, level, str, str_length, ts_type,
unknown's avatar
unknown committed
10080
                                 field_name);
10081 10082 10083
}


10084 10085
/**
  Produce warning or note about integer datetime value saved into field.
10086

10087 10088 10089 10090 10091 10092 10093 10094 10095
  @param level            level of message (Note/Warning/Error)
  @param code             error code of message to be produced
  @param nr               numeric value which we tried to save
  @param ts_type          type of datetime value (datetime/date/time)
  @param 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
10096 10097
    thread.
*/
10098

10099
void 
10100
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
10101 10102 10103
                            longlong nr, timestamp_type ts_type,
                            int cuted_increment)
{
unknown's avatar
unknown committed
10104 10105
  THD *thd= table ? table->in_use : current_thd;
  if (thd->really_abort_on_warning() ||
unknown's avatar
unknown committed
10106
      set_warning(level, code, cuted_increment))
10107 10108 10109
  {
    char str_nr[22];
    char *str_end= longlong10_to_str(nr, str_nr, -10);
10110
    make_truncated_value_warning(thd, level, str_nr, (uint) (str_end - str_nr), 
unknown's avatar
unknown committed
10111
                                 ts_type, field_name);
10112 10113 10114 10115
  }
}


10116 10117
/**
  Produce warning or note about double datetime data saved into field.
10118

10119 10120 10121 10122 10123 10124 10125 10126
  @param level            level of message (Note/Warning/Error)
  @param code             error code of message to be produced
  @param nr               double value which we tried to save
  @param 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
10127 10128
    thread.
*/
10129

10130
void 
10131
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
10132 10133
                            double nr, timestamp_type ts_type)
{
unknown's avatar
unknown committed
10134 10135
  THD *thd= table ? table->in_use : current_thd;
  if (thd->really_abort_on_warning() ||
unknown's avatar
unknown committed
10136
      set_warning(level, code, 1))
10137 10138 10139
  {
    /* DBL_DIG is enough to print '-[digits].E+###' */
    char str_nr[DBL_DIG + 8];
10140
    uint str_len= sprintf(str_nr, "%g", nr);
10141
    make_truncated_value_warning(thd, level, str_nr, str_len, ts_type,
unknown's avatar
unknown committed
10142
                                 field_name);
unknown's avatar
unknown committed
10143
  }
10144
}