~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
17
/**
18
  @file
19
20
  @brief
21
  This file implements classes defined in field.h
22
*/
23
24
#ifdef USE_PRAGMA_IMPLEMENTATION
25
#pragma implementation				// gcc: Class implementation
26
#endif
27
28
#include "mysql_priv.h"
29
#include "sql_select.h"
30
#include <m_ctype.h>
31
#include <errno.h>
32
#ifdef HAVE_FCONVERT
33
#include <floatingpoint.h>
34
#endif
35
36
// Maximum allowed exponent value for converting string to decimal
37
#define MAX_EXPONENT 1024
38
39
/*****************************************************************************
40
  Instansiate templates and static variables
41
*****************************************************************************/
42
43
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
44
template class List<Create_field>;
45
template class List_iterator<Create_field>;
46
#endif
47
48
uchar Field_null::null[1]={1};
49
const char field_separator=',';
50
51
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
52
#define LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE 128
53
#define DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE 128
54
#define BLOB_PACK_LENGTH_TO_MAX_LENGH(arg) \
80.1.1 by Brian Aker
LL() cleanup
55
((ulong) ((1LL << min(arg, 4) * 8) - 1LL))
1 by brian
clean slate
56
57
#define ASSERT_COLUMN_MARKED_FOR_READ DBUG_ASSERT(!table || (!table->read_set || bitmap_is_set(table->read_set, field_index)))
58
#define ASSERT_COLUMN_MARKED_FOR_WRITE DBUG_ASSERT(!table || (!table->write_set || bitmap_is_set(table->write_set, field_index)))
59
60
/*
61
  Rules for merging different types of fields in UNION
62
63
  NOTE: to avoid 256*256 table, gap in table types numeration is skiped
64
  following #defines describe that gap and how to canculate number of fields
65
  and index of field in thia array.
66
*/
65 by Brian Aker
Removing final piece of BIT type
67
#define FIELDTYPE_TEAR_FROM (MYSQL_TYPE_VARCHAR + 1)
1 by brian
clean slate
68
#define FIELDTYPE_TEAR_TO   (MYSQL_TYPE_NEWDECIMAL - 1)
69
#define FIELDTYPE_NUM (FIELDTYPE_TEAR_FROM + (255 - FIELDTYPE_TEAR_TO))
70
inline int field_type2index (enum_field_types field_type)
71
{
72
  return (field_type < FIELDTYPE_TEAR_FROM ?
73
          field_type :
74
          ((int)FIELDTYPE_TEAR_FROM) + (field_type - FIELDTYPE_TEAR_TO) - 1);
75
}
76
77
78
static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]=
79
{
80
  /* MYSQL_TYPE_DECIMAL -> */
81
  {
82
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
83
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
84
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
85
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
86
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
87
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
88
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
89
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
90
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
91
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_DECIMAL,
92
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
93
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
94
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
95
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
96
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
97
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
98
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
99
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
100
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
101
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
102
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
103
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
104
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
105
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
106
  //MYSQL_TYPE_STRING       
107
    MYSQL_TYPE_STRING
1 by brian
clean slate
108
  },
109
  /* MYSQL_TYPE_TINY -> */
110
  {
111
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
112
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_TINY,
113
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
114
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
115
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
116
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
117
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
118
    MYSQL_TYPE_TINY,        MYSQL_TYPE_VARCHAR,
119
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
120
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
121
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
122
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
123
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
124
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY,
125
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
126
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
127
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
128
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
129
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
130
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
131
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
132
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
133
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
134
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
135
  //MYSQL_TYPE_STRING       
136
    MYSQL_TYPE_STRING     
1 by brian
clean slate
137
  },
138
  /* MYSQL_TYPE_SHORT -> */
139
  {
140
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
141
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_SHORT,
142
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
143
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
144
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
145
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
146
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
147
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_VARCHAR,
148
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
149
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
150
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
151
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
152
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
153
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_SHORT,
154
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
155
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
156
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
157
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
158
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
159
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
160
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
161
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
162
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
163
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
164
  //MYSQL_TYPE_STRING
165
    MYSQL_TYPE_STRING
1 by brian
clean slate
166
  },
167
  /* MYSQL_TYPE_LONG -> */
168
  {
169
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
170
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_LONG,
171
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
172
    MYSQL_TYPE_LONG,        MYSQL_TYPE_LONG,
173
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
174
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
175
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
176
    MYSQL_TYPE_LONG,         MYSQL_TYPE_VARCHAR,
177
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
178
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
179
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
180
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
181
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
182
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_LONG,
183
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
184
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
185
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
186
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
187
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
188
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
189
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
190
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
191
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
192
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
193
  //MYSQL_TYPE_STRING
194
    MYSQL_TYPE_STRING
1 by brian
clean slate
195
  },
196
  /* MYSQL_TYPE_FLOAT -> */
197
  {
198
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
199
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_FLOAT,
200
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
201
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
202
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
203
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
204
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
205
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_VARCHAR,
206
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
207
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_INT24,
208
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
209
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
210
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
211
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_FLOAT,
212
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
213
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
214
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
215
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
216
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
217
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
218
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
219
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
220
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
221
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
222
  //MYSQL_TYPE_STRING
223
    MYSQL_TYPE_STRING
1 by brian
clean slate
224
  },
225
  /* MYSQL_TYPE_DOUBLE -> */
226
  {
227
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
228
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
229
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
230
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
231
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
232
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
233
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
234
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
235
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
236
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_INT24,
237
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
238
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
239
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
240
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_DOUBLE,
241
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
242
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
243
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
244
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_VARCHAR,
245
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
246
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
247
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
248
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
249
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
250
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
251
  //MYSQL_TYPE_STRING
252
    MYSQL_TYPE_STRING
1 by brian
clean slate
253
  },
254
  /* MYSQL_TYPE_NULL -> */
255
  {
256
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
257
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_TINY,
258
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
259
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
260
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
261
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
262
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
263
    MYSQL_TYPE_NULL,        MYSQL_TYPE_TIMESTAMP,
264
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
265
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
266
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
267
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_TIME,
268
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
269
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_YEAR,
270
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
271
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
272
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
273
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_ENUM,
274
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
275
    MYSQL_TYPE_SET,         MYSQL_TYPE_TINY_BLOB,
276
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
277
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
278
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
279
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
280
  //MYSQL_TYPE_STRING
281
    MYSQL_TYPE_STRING
1 by brian
clean slate
282
  },
283
  /* MYSQL_TYPE_TIMESTAMP -> */
284
  {
285
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
286
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
287
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
288
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
289
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
290
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
291
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
292
    MYSQL_TYPE_TIMESTAMP,   MYSQL_TYPE_TIMESTAMP,
293
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
294
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
295
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
296
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
297
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
298
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
299
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
300
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
301
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
302
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
303
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
304
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
305
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
306
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
307
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
308
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
309
  //MYSQL_TYPE_STRING
310
    MYSQL_TYPE_STRING
1 by brian
clean slate
311
  },
312
  /* MYSQL_TYPE_LONGLONG -> */
313
  {
314
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
315
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_LONGLONG,
316
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
317
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_LONGLONG,
318
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
319
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
320
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
321
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_VARCHAR,
322
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
323
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_LONG,
324
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
325
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
326
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
327
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_LONGLONG,
328
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
329
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
330
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
331
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
332
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
333
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
334
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
335
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
336
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
337
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
338
  //MYSQL_TYPE_STRING
339
    MYSQL_TYPE_STRING
1 by brian
clean slate
340
  },
341
  /* MYSQL_TYPE_INT24 -> */
342
  {
343
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
344
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_INT24,
345
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
346
    MYSQL_TYPE_INT24,       MYSQL_TYPE_LONG,
347
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
348
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
349
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
350
    MYSQL_TYPE_INT24,       MYSQL_TYPE_VARCHAR,
351
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
352
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
353
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
354
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
355
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
356
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_INT24,
357
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
358
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
359
  //MYSQL_TYPE_NEWDECIMAL    MYSQL_TYPE_ENUM
360
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
361
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
362
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
363
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
364
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
365
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
366
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
367
  //MYSQL_TYPE_STRING
368
    MYSQL_TYPE_STRING
1 by brian
clean slate
369
  },
370
  /* MYSQL_TYPE_DATE -> */
371
  {
372
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
373
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
374
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
375
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
376
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
377
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
378
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
379
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
380
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
381
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
382
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
383
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
384
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
385
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
386
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
387
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
388
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
389
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
390
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
391
    MYSQL_TYPE_VARCHAR,  MYSQL_TYPE_TINY_BLOB,
392
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
393
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
394
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
395
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
396
  //MYSQL_TYPE_STRING
397
    MYSQL_TYPE_STRING
1 by brian
clean slate
398
  },
399
  /* MYSQL_TYPE_TIME -> */
400
  {
401
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
402
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
403
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
404
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
405
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
406
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
407
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
408
    MYSQL_TYPE_TIME,        MYSQL_TYPE_DATETIME,
409
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
410
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
411
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
412
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_TIME,
413
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
414
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
415
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
416
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
417
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
418
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
419
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
420
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
421
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
422
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
423
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
424
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
425
  //MYSQL_TYPE_STRING
426
    MYSQL_TYPE_STRING
1 by brian
clean slate
427
  },
428
  /* MYSQL_TYPE_DATETIME -> */
429
  {
430
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
431
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
432
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
433
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
434
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
435
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
436
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
437
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
438
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
439
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
440
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
441
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_DATETIME,
442
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
443
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
444
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
445
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
446
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
447
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
448
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
449
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
450
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
451
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
452
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
453
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
454
  //MYSQL_TYPE_STRING
455
    MYSQL_TYPE_STRING
1 by brian
clean slate
456
  },
457
  /* MYSQL_TYPE_YEAR -> */
458
  {
459
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
460
    MYSQL_TYPE_DECIMAL,     MYSQL_TYPE_TINY,
461
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
462
    MYSQL_TYPE_SHORT,       MYSQL_TYPE_LONG,
463
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
464
    MYSQL_TYPE_FLOAT,       MYSQL_TYPE_DOUBLE,
465
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
466
    MYSQL_TYPE_YEAR,        MYSQL_TYPE_VARCHAR,
467
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
468
    MYSQL_TYPE_LONGLONG,    MYSQL_TYPE_INT24,
469
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
470
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
471
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
472
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_YEAR,
473
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
474
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
475
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
476
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
477
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
478
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
479
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
480
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
481
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
482
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
483
  //MYSQL_TYPE_STRING
484
    MYSQL_TYPE_STRING
1 by brian
clean slate
485
  },
486
  /* MYSQL_TYPE_NEWDATE -> */
487
  {
488
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
489
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
490
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
491
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
492
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
493
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
494
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
495
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
496
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
497
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
498
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
499
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_DATETIME,
500
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
501
    MYSQL_TYPE_DATETIME,    MYSQL_TYPE_VARCHAR,
502
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
503
    MYSQL_TYPE_NEWDATE,     MYSQL_TYPE_VARCHAR,
504
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
505
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
506
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
507
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
508
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
509
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
510
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
511
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
512
  //MYSQL_TYPE_STRING
513
    MYSQL_TYPE_STRING
1 by brian
clean slate
514
  },
515
  /* MYSQL_TYPE_VARCHAR -> */
516
  {
517
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
518
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
519
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
520
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
521
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
522
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
523
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
524
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
525
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
526
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
527
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
528
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
529
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
530
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
531
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
532
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
533
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
534
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
535
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
536
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
537
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
538
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
539
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
540
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
541
  //MYSQL_TYPE_STRING
542
    MYSQL_TYPE_VARCHAR
1 by brian
clean slate
543
  },
544
  /* MYSQL_TYPE_NEWDECIMAL -> */
545
  {
546
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
547
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
548
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
549
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
550
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
551
    MYSQL_TYPE_DOUBLE,      MYSQL_TYPE_DOUBLE,
552
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
553
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
554
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
555
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_NEWDECIMAL,
556
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
557
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
558
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
559
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_NEWDECIMAL,
560
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
561
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
562
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
563
    MYSQL_TYPE_NEWDECIMAL,  MYSQL_TYPE_VARCHAR,
564
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
565
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
566
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
567
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
568
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
569
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
570
  //MYSQL_TYPE_STRING
571
    MYSQL_TYPE_STRING
1 by brian
clean slate
572
  },
573
  /* MYSQL_TYPE_ENUM -> */
574
  {
575
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
576
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
577
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
578
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
579
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
580
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
581
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
582
    MYSQL_TYPE_ENUM,        MYSQL_TYPE_VARCHAR,
583
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
584
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
585
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
586
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
587
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
588
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
589
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
590
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
591
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
592
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
593
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
594
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
595
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
596
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
597
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
598
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
599
  //MYSQL_TYPE_STRING
600
    MYSQL_TYPE_STRING
1 by brian
clean slate
601
  },
602
  /* MYSQL_TYPE_SET -> */
603
  {
604
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
605
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
606
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
607
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
608
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
609
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
610
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
611
    MYSQL_TYPE_SET,         MYSQL_TYPE_VARCHAR,
612
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
613
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
614
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
615
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
616
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
617
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
618
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
619
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
620
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
621
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
622
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
623
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
624
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
625
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
626
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
627
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
628
  //MYSQL_TYPE_STRING
629
    MYSQL_TYPE_STRING
1 by brian
clean slate
630
  },
631
  /* MYSQL_TYPE_TINY_BLOB -> */
632
  {
633
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
634
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
635
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
636
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
637
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
638
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
639
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
640
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
641
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
642
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
643
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
644
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
645
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
646
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
647
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
648
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
649
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
650
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
651
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
652
    MYSQL_TYPE_TINY_BLOB,   MYSQL_TYPE_TINY_BLOB,
653
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
654
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
655
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
656
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_TINY_BLOB,
66 by Brian Aker
Removal of GEOM type
657
  //MYSQL_TYPE_STRING
658
    MYSQL_TYPE_TINY_BLOB
1 by brian
clean slate
659
  },
660
  /* MYSQL_TYPE_MEDIUM_BLOB -> */
661
  {
662
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
663
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
664
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
665
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
666
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
667
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
668
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
669
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
670
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
671
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
672
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
673
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
674
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
675
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
676
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
677
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
678
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
679
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
680
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
681
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
682
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
683
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
684
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
685
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
66 by Brian Aker
Removal of GEOM type
686
  //MYSQL_TYPE_STRING
687
    MYSQL_TYPE_MEDIUM_BLOB
1 by brian
clean slate
688
  },
689
  /* MYSQL_TYPE_LONG_BLOB -> */
690
  {
691
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
692
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
693
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
694
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
695
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
696
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
697
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
698
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
699
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
700
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
701
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
702
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
703
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
704
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
705
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
706
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
707
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
708
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
709
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
710
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
711
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
712
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
713
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
714
    MYSQL_TYPE_LONG_BLOB,   MYSQL_TYPE_LONG_BLOB,
66 by Brian Aker
Removal of GEOM type
715
  //MYSQL_TYPE_STRING
716
    MYSQL_TYPE_LONG_BLOB
1 by brian
clean slate
717
  },
718
  /* MYSQL_TYPE_BLOB -> */
719
  {
720
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
721
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
722
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
723
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
724
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
725
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
726
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
727
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
728
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
729
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
730
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
731
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
732
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
733
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
734
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
735
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
736
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
737
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
738
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
739
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
740
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
741
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
742
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
743
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_BLOB,
66 by Brian Aker
Removal of GEOM type
744
  //MYSQL_TYPE_STRING
745
    MYSQL_TYPE_BLOB
1 by brian
clean slate
746
  },
747
  /* MYSQL_TYPE_VAR_STRING -> */
748
  {
749
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
750
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
751
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
752
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
753
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
754
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
755
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
756
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
757
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
758
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
759
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
760
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
761
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
762
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
763
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
764
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
765
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
766
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_VARCHAR,
767
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
768
    MYSQL_TYPE_VARCHAR,     MYSQL_TYPE_TINY_BLOB,
769
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
770
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
771
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
772
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
773
  //MYSQL_TYPE_STRING
774
    MYSQL_TYPE_VARCHAR
1 by brian
clean slate
775
  },
776
  /* MYSQL_TYPE_STRING -> */
777
  {
778
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
779
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
780
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
781
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
782
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
783
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
784
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
785
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
786
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
787
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
788
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
789
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
790
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
791
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
792
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
793
    MYSQL_TYPE_STRING,      MYSQL_TYPE_VARCHAR,
794
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
795
    MYSQL_TYPE_STRING,      MYSQL_TYPE_STRING,
796
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
797
    MYSQL_TYPE_STRING,      MYSQL_TYPE_TINY_BLOB,
798
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
799
    MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
800
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
801
    MYSQL_TYPE_BLOB,        MYSQL_TYPE_VARCHAR,
66 by Brian Aker
Removal of GEOM type
802
  //MYSQL_TYPE_STRING
803
    MYSQL_TYPE_STRING
1 by brian
clean slate
804
  }
805
};
806
807
/**
808
  Return type of which can carry value of both given types in UNION result.
809
810
  @param a  type for merging
811
  @param b  type for merging
812
813
  @return
814
    type of field
815
*/
816
817
enum_field_types Field::field_type_merge(enum_field_types a,
818
                                         enum_field_types b)
819
{
820
  DBUG_ASSERT(a < FIELDTYPE_TEAR_FROM || a > FIELDTYPE_TEAR_TO);
821
  DBUG_ASSERT(b < FIELDTYPE_TEAR_FROM || b > FIELDTYPE_TEAR_TO);
822
  return field_types_merge_rules[field_type2index(a)]
823
                                [field_type2index(b)];
824
}
825
826
827
static Item_result field_types_result_type [FIELDTYPE_NUM]=
828
{
829
  //MYSQL_TYPE_DECIMAL      MYSQL_TYPE_TINY
830
  DECIMAL_RESULT,           INT_RESULT,
831
  //MYSQL_TYPE_SHORT        MYSQL_TYPE_LONG
832
  INT_RESULT,               INT_RESULT,
833
  //MYSQL_TYPE_FLOAT        MYSQL_TYPE_DOUBLE
834
  REAL_RESULT,              REAL_RESULT,
835
  //MYSQL_TYPE_NULL         MYSQL_TYPE_TIMESTAMP
836
  STRING_RESULT,            STRING_RESULT,
837
  //MYSQL_TYPE_LONGLONG     MYSQL_TYPE_INT24
838
  INT_RESULT,               INT_RESULT,
839
  //MYSQL_TYPE_DATE         MYSQL_TYPE_TIME
840
  STRING_RESULT,            STRING_RESULT,
841
  //MYSQL_TYPE_DATETIME     MYSQL_TYPE_YEAR
842
  STRING_RESULT,            INT_RESULT,
843
  //MYSQL_TYPE_NEWDATE      MYSQL_TYPE_VARCHAR
844
  STRING_RESULT,            STRING_RESULT,
845
  //MYSQL_TYPE_NEWDECIMAL   MYSQL_TYPE_ENUM
846
  DECIMAL_RESULT,           STRING_RESULT,
847
  //MYSQL_TYPE_SET          MYSQL_TYPE_TINY_BLOB
848
  STRING_RESULT,            STRING_RESULT,
849
  //MYSQL_TYPE_MEDIUM_BLOB  MYSQL_TYPE_LONG_BLOB
850
  STRING_RESULT,            STRING_RESULT,
851
  //MYSQL_TYPE_BLOB         MYSQL_TYPE_VAR_STRING
852
  STRING_RESULT,            STRING_RESULT,
66 by Brian Aker
Removal of GEOM type
853
  //MYSQL_TYPE_STRING
854
  STRING_RESULT
1 by brian
clean slate
855
};
856
857
858
/*
859
  Test if the given string contains important data:
860
  not spaces for character string,
861
  or any data for binary string.
862
863
  SYNOPSIS
864
    test_if_important_data()
865
    cs          Character set
866
    str         String to test
867
    strend      String end
868
869
  RETURN
870
    FALSE - If string does not have important data
871
    TRUE  - If string has some important data
872
*/
873
874
static bool
875
test_if_important_data(CHARSET_INFO *cs, const char *str, const char *strend)
876
{
877
  if (cs != &my_charset_bin)
878
    str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
879
  return (str < strend);
880
}
881
882
883
/**
884
  Detect Item_result by given field type of UNION merge result.
885
886
  @param field_type  given field type
887
888
  @return
889
    Item_result (type of internal MySQL expression result)
890
*/
891
892
Item_result Field::result_merge_type(enum_field_types field_type)
893
{
894
  DBUG_ASSERT(field_type < FIELDTYPE_TEAR_FROM || field_type
895
              > FIELDTYPE_TEAR_TO);
896
  return field_types_result_type[field_type2index(field_type)];
897
}
898
899
/*****************************************************************************
900
  Static help functions
901
*****************************************************************************/
902
903
904
/**
905
  Check whether a field type can be partially indexed by a key.
906
907
  This is a static method, rather than a virtual function, because we need
908
  to check the type of a non-Field in mysql_alter_table().
909
910
  @param type  field type
911
912
  @retval
913
    TRUE  Type can have a prefixed key
914
  @retval
915
    FALSE Type can not have a prefixed key
916
*/
917
918
bool Field::type_can_have_key_part(enum enum_field_types type)
919
{
920
  switch (type) {
921
  case MYSQL_TYPE_VARCHAR:
922
  case MYSQL_TYPE_TINY_BLOB:
923
  case MYSQL_TYPE_MEDIUM_BLOB:
924
  case MYSQL_TYPE_LONG_BLOB:
925
  case MYSQL_TYPE_BLOB:
926
  case MYSQL_TYPE_VAR_STRING:
927
  case MYSQL_TYPE_STRING:
928
    return TRUE;
929
  default:
930
    return FALSE;
931
  }
932
}
933
934
935
/**
936
  Numeric fields base class constructor.
937
*/
938
Field_num::Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
939
                     uchar null_bit_arg, utype unireg_check_arg,
940
                     const char *field_name_arg,
941
                     uint8 dec_arg, bool zero_arg, bool unsigned_arg)
942
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
943
         unireg_check_arg, field_name_arg),
944
  dec(dec_arg),zerofill(zero_arg),unsigned_flag(unsigned_arg)
945
{
946
  if (zerofill)
947
    flags|=ZEROFILL_FLAG;
948
  if (unsigned_flag)
949
    flags|=UNSIGNED_FLAG;
950
}
951
952
953
void Field_num::prepend_zeros(String *value)
954
{
955
  int diff;
956
  if ((diff= (int) (field_length - value->length())) > 0)
957
  {
958
    bmove_upp((uchar*) value->ptr()+field_length,
959
              (uchar*) value->ptr()+value->length(),
960
	      value->length());
961
    bfill((uchar*) value->ptr(),diff,'0');
962
    value->length(field_length);
963
    (void) value->c_ptr_quick();		// Avoid warnings in purify
964
  }
965
}
966
967
/**
968
  Test if given number is a int.
969
970
  @todo
971
    Make this multi-byte-character safe
972
973
  @param str		String to test
974
  @param length        Length of 'str'
975
  @param int_end	Pointer to char after last used digit
976
  @param cs		Character set
977
978
  @note
979
    This is called after one has called strntoull10rnd() function.
980
981
  @retval
982
    0	OK
983
  @retval
984
    1	error: empty string or wrong integer.
985
  @retval
986
    2   error: garbage at the end of string.
987
*/
988
989
int Field_num::check_int(CHARSET_INFO *cs, const char *str, int length, 
990
                         const char *int_end, int error)
991
{
992
  /* Test if we get an empty string or wrong integer */
993
  if (str == int_end || error == MY_ERRNO_EDOM)
994
  {
995
    char buff[128];
996
    String tmp(buff, (uint32) sizeof(buff), system_charset_info);
997
    tmp.copy(str, length, system_charset_info);
998
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
999
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 
1000
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1001
                        "integer", tmp.c_ptr(), field_name,
1002
                        (ulong) table->in_use->row_count);
1003
    return 1;
1004
  }
1005
  /* Test if we have garbage at the end of the given string. */
1006
  if (test_if_important_data(cs, int_end, str + length))
1007
  {
1008
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
1009
    return 2;
1010
  }
1011
  return 0;
1012
}
1013
1014
1015
/*
1016
  Conver a string to an integer then check bounds.
1017
  
1018
  SYNOPSIS
1019
    Field_num::get_int
1020
    cs            Character set
1021
    from          String to convert
1022
    len           Length of the string
1023
    rnd           OUT longlong value
1024
    unsigned_max  max unsigned value
1025
    signed_min    min signed value
1026
    signed_max    max signed value
1027
1028
  DESCRIPTION
1029
    The function calls strntoull10rnd() to get an integer value then
1030
    check bounds and errors returned. In case of any error a warning
1031
    is raised.
1032
1033
  RETURN
1034
    0   ok
1035
    1   error
1036
*/
1037
1038
bool Field_num::get_int(CHARSET_INFO *cs, const char *from, uint len,
1039
                        longlong *rnd, uint64_t unsigned_max, 
1040
                        longlong signed_min, longlong signed_max)
1041
{
1042
  char *end;
1043
  int error;
1044
  
1045
  *rnd= (longlong) cs->cset->strntoull10rnd(cs, from, len,
1046
                                            unsigned_flag, &end,
1047
                                            &error);
1048
  if (unsigned_flag)
1049
  {
1050
1051
    if ((((uint64_t) *rnd > unsigned_max) && (*rnd= (longlong) unsigned_max)) ||
1052
        error == MY_ERRNO_ERANGE)
1053
    {
1054
      goto out_of_range;
1055
    }
1056
  }
1057
  else
1058
  {
1059
    if (*rnd < signed_min)
1060
    {
1061
      *rnd= signed_min;
1062
      goto out_of_range;
1063
    }
1064
    else if (*rnd > signed_max)
1065
    {
1066
      *rnd= signed_max;
1067
      goto out_of_range;
1068
    }
1069
  }
1070
  if (table->in_use->count_cuted_fields &&
1071
      check_int(cs, from, len, end, error))
1072
    return 1;
1073
  return 0;
1074
1075
out_of_range:
1076
  set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1077
  return 1;
1078
}
1079
1080
/**
1081
  Process decimal library return codes and issue warnings for overflow and
1082
  truncation.
1083
1084
  @param op_result  decimal library return code (E_DEC_* see include/decimal.h)
1085
1086
  @retval
1087
    1  there was overflow
1088
  @retval
1089
    0  no error or some other errors except overflow
1090
*/
1091
1092
int Field::warn_if_overflow(int op_result)
1093
{
1094
  if (op_result == E_DEC_OVERFLOW)
1095
  {
1096
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1097
    return 1;
1098
  }
1099
  if (op_result == E_DEC_TRUNCATED)
1100
  {
1101
    set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
1102
    /* We return 0 here as this is not a critical issue */
1103
  }
1104
  return 0;
1105
}
1106
1107
1108
#ifdef NOT_USED
1109
static bool test_if_real(const char *str,int length, CHARSET_INFO *cs)
1110
{
1111
  cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
1112
1113
  while (length && my_isspace(cs,*str))
1114
  {						// Allow start space
1115
    length--; str++;
1116
  }
1117
  if (!length)
1118
    return 0;
1119
  if (*str == '+' || *str == '-')
1120
  {
1121
    length--; str++;
1122
    if (!length || !(my_isdigit(cs,*str) || *str == '.'))
1123
      return 0;
1124
  }
1125
  while (length && my_isdigit(cs,*str))
1126
  {
1127
    length--; str++;
1128
  }
1129
  if (!length)
1130
    return 1;
1131
  if (*str == '.')
1132
  {
1133
    length--; str++;
1134
    while (length && my_isdigit(cs,*str))
1135
    {
1136
      length--; str++;
1137
    }
1138
  }
1139
  if (!length)
1140
    return 1;
1141
  if (*str == 'E' || *str == 'e')
1142
  {
1143
    if (length < 3 || (str[1] != '+' && str[1] != '-') || 
1144
        !my_isdigit(cs,str[2]))
1145
      return 0;
1146
    length-=3;
1147
    str+=3;
1148
    while (length && my_isdigit(cs,*str))
1149
    {
1150
      length--; str++;
1151
    }
1152
  }
1153
  for (; length ; length--, str++)
1154
  {						// Allow end space
1155
    if (!my_isspace(cs,*str))
1156
      return 0;
1157
  }
1158
  return 1;
1159
}
1160
#endif
1161
1162
1163
/**
1164
  Interpret field value as an integer but return the result as a string.
1165
1166
  This is used for printing bit_fields as numbers while debugging.
1167
*/
1168
1169
String *Field::val_int_as_str(String *val_buffer, my_bool unsigned_val)
1170
{
1171
  ASSERT_COLUMN_MARKED_FOR_READ;
1172
  CHARSET_INFO *cs= &my_charset_bin;
1173
  uint length;
1174
  longlong value= val_int();
1175
1176
  if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
1177
    return 0;
1178
  length= (uint) (*cs->cset->longlong10_to_str)(cs, (char*) val_buffer->ptr(),
1179
                                                MY_INT64_NUM_DECIMAL_DIGITS,
1180
                                                unsigned_val ? 10 : -10,
1181
                                                value);
1182
  val_buffer->length(length);
1183
  return val_buffer;
1184
}
1185
1186
1187
/// This is used as a table name when the table structure is not set up
1188
Field::Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
1189
	     uchar null_bit_arg,
1190
	     utype unireg_check_arg, const char *field_name_arg)
1191
  :ptr(ptr_arg), null_ptr(null_ptr_arg),
1192
   table(0), orig_table(0), table_name(0),
1193
   field_name(field_name_arg),
1194
   key_start(0), part_of_key(0), part_of_key_not_clustered(0),
1195
   part_of_sortkey(0), unireg_check(unireg_check_arg),
1196
   field_length(length_arg), null_bit(null_bit_arg), 
1197
   is_created_from_null_item(FALSE)
1198
{
1199
  flags=null_ptr ? 0: NOT_NULL_FLAG;
1200
  comment.str= (char*) "";
1201
  comment.length=0;
1202
  field_index= 0;
1203
}
1204
1205
1206
void Field::hash(ulong *nr, ulong *nr2)
1207
{
1208
  if (is_null())
1209
  {
1210
    *nr^= (*nr << 1) | 1;
1211
  }
1212
  else
1213
  {
1214
    uint len= pack_length();
1215
    CHARSET_INFO *cs= charset();
1216
    cs->coll->hash_sort(cs, ptr, len, nr, nr2);
1217
  }
1218
}
1219
1220
size_t
1221
Field::do_last_null_byte() const
1222
{
1223
  DBUG_ASSERT(null_ptr == NULL || null_ptr >= table->record[0]);
1224
  if (null_ptr)
1225
    return (size_t) (null_ptr - table->record[0]) + 1;
1226
  return LAST_NULL_BYTE_UNDEF;
1227
}
1228
1229
1230
void Field::copy_from_tmp(int row_offset)
1231
{
1232
  memcpy(ptr,ptr+row_offset,pack_length());
1233
  if (null_ptr)
1234
  {
1235
    *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) | (null_ptr[row_offset] & (uchar) null_bit));
1236
  }
1237
}
1238
1239
1240
bool Field::send_binary(Protocol *protocol)
1241
{
1242
  char buff[MAX_FIELD_WIDTH];
1243
  String tmp(buff,sizeof(buff),charset());
1244
  val_str(&tmp);
1245
  return protocol->store(tmp.ptr(), tmp.length(), tmp.charset());
1246
}
1247
1248
1249
/**
1250
   Check to see if field size is compatible with destination.
1251
1252
   This method is used in row-based replication to verify that the slave's
1253
   field size is less than or equal to the master's field size. The 
1254
   encoded field metadata (from the master or source) is decoded and compared
1255
   to the size of this field (the slave or destination). 
1256
1257
   @param   field_metadata   Encoded size in field metadata
1258
1259
   @retval 0 if this field's size is < the source field's size
1260
   @retval 1 if this field's size is >= the source field's size
1261
*/
1262
int Field::compatible_field_size(uint field_metadata)
1263
{
1264
  uint const source_size= pack_length_from_metadata(field_metadata);
1265
  uint const destination_size= row_pack_length();
1266
  return (source_size <= destination_size);
1267
}
1268
1269
1270
int Field::store(const char *to, uint length, CHARSET_INFO *cs,
1271
                 enum_check_fields check_level)
1272
{
1273
  int res;
1274
  enum_check_fields old_check_level= table->in_use->count_cuted_fields;
1275
  table->in_use->count_cuted_fields= check_level;
1276
  res= store(to, length, cs);
1277
  table->in_use->count_cuted_fields= old_check_level;
1278
  return res;
1279
}
1280
1281
1282
/**
1283
   Pack the field into a format suitable for storage and transfer.
1284
1285
   To implement packing functionality, only the virtual function
1286
   should be overridden. The other functions are just convenience
1287
   functions and hence should not be overridden.
1288
1289
   The value of <code>low_byte_first</code> is dependent on how the
1290
   packed data is going to be used: for local use, e.g., temporary
1291
   store on disk or in memory, use the native format since that is
1292
   faster. For data that is going to be transfered to other machines
1293
   (e.g., when writing data to the binary log), data should always be
1294
   stored in little-endian format.
1295
1296
   @note The default method for packing fields just copy the raw bytes
1297
   of the record into the destination, but never more than
1298
   <code>max_length</code> characters.
1299
1300
   @param to
1301
   Pointer to memory area where representation of field should be put.
1302
1303
   @param from
1304
   Pointer to memory area where record representation of field is
1305
   stored.
1306
1307
   @param max_length
1308
   Maximum length of the field, as given in the column definition. For
1309
   example, for <code>CHAR(1000)</code>, the <code>max_length</code>
1310
   is 1000. This information is sometimes needed to decide how to pack
1311
   the data.
1312
1313
   @param low_byte_first
1314
   @c TRUE if integers should be stored little-endian, @c FALSE if
1315
   native format should be used. Note that for little-endian machines,
1316
   the value of this flag is a moot point since the native format is
1317
   little-endian.
1318
*/
1319
uchar *
1320
Field::pack(uchar *to, const uchar *from, uint max_length,
1321
            bool low_byte_first __attribute__((unused)))
1322
{
1323
  uint32 length= pack_length();
1324
  set_if_smaller(length, max_length);
1325
  memcpy(to, from, length);
1326
  return to+length;
1327
}
1328
1329
/**
1330
   Unpack a field from row data.
1331
1332
   This method is used to unpack a field from a master whose size of
1333
   the field is less than that of the slave.
1334
1335
   The <code>param_data</code> parameter is a two-byte integer (stored
1336
   in the least significant 16 bits of the unsigned integer) usually
1337
   consisting of two parts: the real type in the most significant byte
1338
   and a original pack length in the least significant byte.
1339
1340
   The exact layout of the <code>param_data</code> field is given by
1341
   the <code>Table_map_log_event::save_field_metadata()</code>.
1342
1343
   This is the default method for unpacking a field. It just copies
1344
   the memory block in byte order (of original pack length bytes or
1345
   length of field, whichever is smaller).
1346
1347
   @param   to         Destination of the data
1348
   @param   from       Source of the data
1349
   @param   param_data Real type and original pack length of the field
1350
                       data
1351
1352
   @param low_byte_first
1353
   If this flag is @c true, all composite entities (e.g., lengths)
1354
   should be unpacked in little-endian format; otherwise, the entities
1355
   are unpacked in native order.
1356
1357
   @return  New pointer into memory based on from + length of the data
1358
*/
1359
const uchar *
1360
Field::unpack(uchar* to, const uchar *from, uint param_data,
1361
              bool low_byte_first __attribute__((unused)))
1362
{
1363
  uint length=pack_length();
1364
  int from_type= 0;
1365
  /*
1366
    If from length is > 255, it has encoded data in the upper bits. Need
1367
    to mask it out.
1368
  */
1369
  if (param_data > 255)
1370
  {
1371
    from_type= (param_data & 0xff00) >> 8U;  // real_type.
1372
    param_data= param_data & 0x00ff;        // length.
1373
  }
1374
1375
  if ((param_data == 0) ||
1376
      (length == param_data) ||
1377
      (from_type != real_type()))
1378
  {
1379
    memcpy(to, from, length);
1380
    return from+length;
1381
  }
1382
1383
  uint len= (param_data && (param_data < length)) ?
1384
            param_data : length;
1385
1386
  memcpy(to, from, param_data > length ? length : len);
1387
  return from+len;
1388
}
1389
1390
1391
my_decimal *Field::val_decimal(my_decimal *decimal)
1392
{
1393
  /* This never have to be called */
1394
  DBUG_ASSERT(0);
1395
  return 0;
1396
}
1397
1398
1399
void Field_num::add_zerofill_and_unsigned(String &res) const
1400
{
1401
  if (unsigned_flag)
1402
    res.append(STRING_WITH_LEN(" unsigned"));
1403
  if (zerofill)
1404
    res.append(STRING_WITH_LEN(" zerofill"));
1405
}
1406
1407
1408
void Field::make_field(Send_field *field)
1409
{
1410
  if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
1411
  {
1412
    field->db_name= orig_table->s->db.str;
1413
    field->org_table_name= orig_table->s->table_name.str;
1414
  }
1415
  else
1416
    field->org_table_name= field->db_name= "";
1417
  if (orig_table)
1418
  {
1419
    field->table_name= orig_table->alias;
1420
    field->org_col_name= field_name;
1421
  }
1422
  else
1423
  {
1424
    field->table_name= "";
1425
    field->org_col_name= "";
1426
  }
1427
  field->col_name= field_name;
1428
  field->charsetnr= charset()->number;
1429
  field->length=field_length;
1430
  field->type=type();
1431
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
1432
  field->decimals= 0;
1433
}
1434
1435
1436
/**
1437
  Conversion from decimal to longlong with checking overflow and
1438
  setting correct value (min/max) in case of overflow.
1439
1440
  @param val             value which have to be converted
1441
  @param unsigned_flag   type of integer in which we convert val
1442
  @param err             variable to pass error code
1443
1444
  @return
1445
    value converted from val
1446
*/
1447
longlong Field::convert_decimal2longlong(const my_decimal *val,
1448
                                         bool unsigned_flag, int *err)
1449
{
1450
  longlong i;
1451
  if (unsigned_flag)
1452
  {
1453
    if (val->sign())
1454
    {
1455
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1456
      i= 0;
1457
      *err= 1;
1458
    }
1459
    else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
1460
                                           ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
1461
                                           val, TRUE, &i)))
1462
    {
1463
      i= ~(longlong) 0;
1464
      *err= 1;
1465
    }
1466
  }
1467
  else if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
1468
                                         ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
1469
                                         val, FALSE, &i)))
1470
  {
1471
    i= (val->sign() ? LONGLONG_MIN : LONGLONG_MAX);
1472
    *err= 1;
1473
  }
1474
  return i;
1475
}
1476
1477
1478
/**
1479
  Storing decimal in integer fields.
1480
1481
  @param val       value for storing
1482
1483
  @note
1484
    This method is used by all integer fields, real/decimal redefine it
1485
1486
  @retval
1487
    0     OK
1488
  @retval
1489
    !=0  error
1490
*/
1491
1492
int Field_num::store_decimal(const my_decimal *val)
1493
{
1494
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1495
  int err= 0;
1496
  longlong i= convert_decimal2longlong(val, unsigned_flag, &err);
1497
  return test(err | store(i, unsigned_flag));
1498
}
1499
1500
1501
/**
1502
  Return decimal value of integer field.
1503
1504
  @param decimal_value     buffer for storing decimal value
1505
1506
  @note
1507
    This method is used by all integer fields, real/decimal redefine it.
1508
    All longlong values fit in our decimal buffer which cal store 8*9=72
1509
    digits of integer number
1510
1511
  @return
1512
    pointer to decimal buffer with value of field
1513
*/
1514
1515
my_decimal* Field_num::val_decimal(my_decimal *decimal_value)
1516
{
1517
  ASSERT_COLUMN_MARKED_FOR_READ;
1518
  DBUG_ASSERT(result_type() == INT_RESULT);
1519
  longlong nr= val_int();
1520
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
1521
  return decimal_value;
1522
}
1523
1524
1525
Field_str::Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
1526
                     uchar null_bit_arg, utype unireg_check_arg,
1527
                     const char *field_name_arg, CHARSET_INFO *charset_arg)
1528
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1529
         unireg_check_arg, field_name_arg)
1530
{
1531
  field_charset= charset_arg;
1532
  if (charset_arg->state & MY_CS_BINSORT)
1533
    flags|=BINARY_FLAG;
1534
  field_derivation= DERIVATION_IMPLICIT;
1535
}
1536
1537
1538
void Field_num::make_field(Send_field *field)
1539
{
1540
  Field::make_field(field);
1541
  field->decimals= dec;
1542
}
1543
1544
/**
1545
  Decimal representation of Field_str.
1546
1547
  @param d         value for storing
1548
1549
  @note
1550
    Field_str is the base class for fields like Field_enum,
1551
    Field_date and some similar. Some dates use fraction and also
1552
    string value should be converted to floating point value according
1553
    our rules, so we use double to store value of decimal in string.
1554
1555
  @todo
1556
    use decimal2string?
1557
1558
  @retval
1559
    0     OK
1560
  @retval
1561
    !=0  error
1562
*/
1563
1564
int Field_str::store_decimal(const my_decimal *d)
1565
{
1566
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1567
  double val;
1568
  /* TODO: use decimal2string? */
1569
  int err= warn_if_overflow(my_decimal2double(E_DEC_FATAL_ERROR &
1570
                                            ~E_DEC_OVERFLOW, d, &val));
1571
  return err | store(val);
1572
}
1573
1574
1575
my_decimal *Field_str::val_decimal(my_decimal *decimal_value)
1576
{
1577
  ASSERT_COLUMN_MARKED_FOR_READ;
1578
  longlong nr= val_int();
1579
  int2my_decimal(E_DEC_FATAL_ERROR, nr, 0, decimal_value);
1580
  return decimal_value;
1581
}
1582
1583
1584
uint Field::fill_cache_field(CACHE_FIELD *copy)
1585
{
1586
  uint store_length;
1587
  copy->str=ptr;
1588
  copy->length=pack_length();
1589
  copy->blob_field=0;
1590
  if (flags & BLOB_FLAG)
1591
  {
1592
    copy->blob_field=(Field_blob*) this;
1593
    copy->strip=0;
1594
    copy->length-= table->s->blob_ptr_size;
1595
    return copy->length;
1596
  }
1597
  else if (!zero_pack() &&
1598
           (type() == MYSQL_TYPE_STRING && copy->length >= 4 &&
1599
            copy->length < 256))
1600
  {
1601
    copy->strip=1;				/* Remove end space */
1602
    store_length= 2;
1603
  }
1604
  else
1605
  {
1606
    copy->strip=0;
1607
    store_length= 0;
1608
  }
1609
  return copy->length+ store_length;
1610
}
1611
1612
1613
bool Field::get_date(MYSQL_TIME *ltime,uint fuzzydate)
1614
{
1615
  char buff[40];
1616
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1617
  if (!(res=val_str(&tmp)) ||
1618
      str_to_datetime_with_warn(res->ptr(), res->length(),
1619
                                ltime, fuzzydate) <= MYSQL_TIMESTAMP_ERROR)
1620
    return 1;
1621
  return 0;
1622
}
1623
1624
bool Field::get_time(MYSQL_TIME *ltime)
1625
{
1626
  char buff[40];
1627
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1628
  if (!(res=val_str(&tmp)) ||
1629
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
1630
    return 1;
1631
  return 0;
1632
}
1633
1634
/**
1635
  This is called when storing a date in a string.
1636
1637
  @note
1638
    Needs to be changed if/when we want to support different time formats.
1639
*/
1640
1641
int Field::store_time(MYSQL_TIME *ltime, timestamp_type type_arg)
1642
{
1643
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1644
  char buff[MAX_DATE_STRING_REP_LENGTH];
1645
  uint length= (uint) my_TIME_to_str(ltime, buff);
1646
  return store(buff, length, &my_charset_bin);
1647
}
1648
1649
1650
bool Field::optimize_range(uint idx, uint part)
1651
{
1652
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
1653
}
1654
1655
1656
Field *Field::new_field(MEM_ROOT *root, struct st_table *new_table,
1657
                        bool keep_type __attribute__((unused)))
1658
{
1659
  Field *tmp;
1660
  if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1661
    return 0;
1662
1663
  if (tmp->table->maybe_null)
1664
    tmp->flags&= ~NOT_NULL_FLAG;
1665
  tmp->table= new_table;
1666
  tmp->key_start.init(0);
1667
  tmp->part_of_key.init(0);
1668
  tmp->part_of_sortkey.init(0);
1669
  tmp->unireg_check= Field::NONE;
1670
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG |
1671
                ZEROFILL_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1672
  tmp->reset_fields();
1673
  return tmp;
1674
}
1675
1676
1677
Field *Field::new_key_field(MEM_ROOT *root, struct st_table *new_table,
1678
                            uchar *new_ptr, uchar *new_null_ptr,
1679
                            uint new_null_bit)
1680
{
1681
  Field *tmp;
1682
  if ((tmp= new_field(root, new_table, table == new_table)))
1683
  {
1684
    tmp->ptr=      new_ptr;
1685
    tmp->null_ptr= new_null_ptr;
1686
    tmp->null_bit= new_null_bit;
1687
  }
1688
  return tmp;
1689
}
1690
1691
1692
/* This is used to generate a field in TABLE from TABLE_SHARE */
1693
1694
Field *Field::clone(MEM_ROOT *root, struct st_table *new_table)
1695
{
1696
  Field *tmp;
1697
  if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1698
  {
1699
    tmp->init(new_table);
1700
    tmp->move_field_offset((my_ptrdiff_t) (new_table->record[0] -
1701
                                           new_table->s->default_values));
1702
  }
1703
  return tmp;
1704
}
1705
1706
1707
/****************************************************************************
1708
  Field_null, a field that always return NULL
1709
****************************************************************************/
1710
1711
void Field_null::sql_type(String &res) const
1712
{
1713
  res.set_ascii(STRING_WITH_LEN("null"));
1714
}
1715
1716
1717
/****************************************************************************
1718
** Field_new_decimal
1719
****************************************************************************/
1720
1721
Field_new_decimal::Field_new_decimal(uchar *ptr_arg,
1722
                                     uint32 len_arg, uchar *null_ptr_arg,
1723
                                     uchar null_bit_arg,
1724
                                     enum utype unireg_check_arg,
1725
                                     const char *field_name_arg,
1726
                                     uint8 dec_arg,bool zero_arg,
1727
                                     bool unsigned_arg)
1728
  :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1729
             unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg)
1730
{
1731
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
1732
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
1733
  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
1734
              (dec <= DECIMAL_MAX_SCALE));
1735
  bin_size= my_decimal_get_binary_size(precision, dec);
1736
}
1737
1738
1739
Field_new_decimal::Field_new_decimal(uint32 len_arg,
1740
                                     bool maybe_null_arg,
1741
                                     const char *name,
1742
                                     uint8 dec_arg,
1743
                                     bool unsigned_arg)
1744
  :Field_num((uchar*) 0, len_arg,
1745
             maybe_null_arg ? (uchar*) "": 0, 0,
1746
             NONE, name, dec_arg, 0, unsigned_arg)
1747
{
1748
  precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
1749
  set_if_smaller(precision, DECIMAL_MAX_PRECISION);
1750
  DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
1751
              (dec <= DECIMAL_MAX_SCALE));
1752
  bin_size= my_decimal_get_binary_size(precision, dec);
1753
}
1754
1755
1756
int Field_new_decimal::reset(void)
1757
{
1758
  store_value(&decimal_zero);
1759
  return 0;
1760
}
1761
1762
1763
/**
1764
  Generate max/min decimal value in case of overflow.
1765
1766
  @param decimal_value     buffer for value
1767
  @param sign              sign of value which caused overflow
1768
*/
1769
1770
void Field_new_decimal::set_value_on_overflow(my_decimal *decimal_value,
1771
                                              bool sign)
1772
{
1773
  DBUG_ENTER("Field_new_decimal::set_value_on_overflow");
1774
  max_my_decimal(decimal_value, precision, decimals());
1775
  if (sign)
1776
  {
1777
    if (unsigned_flag)
1778
      my_decimal_set_zero(decimal_value);
1779
    else
1780
      decimal_value->sign(TRUE);
1781
  }
1782
  DBUG_VOID_RETURN;
1783
}
1784
1785
1786
/**
1787
  Store decimal value in the binary buffer.
1788
1789
  Checks if decimal_value fits into field size.
1790
  If it does, stores the decimal in the buffer using binary format.
1791
  Otherwise sets maximal number that can be stored in the field.
1792
1793
  @param decimal_value   my_decimal
1794
1795
  @retval
1796
    0 ok
1797
  @retval
1798
    1 error
1799
*/
1800
1801
bool Field_new_decimal::store_value(const my_decimal *decimal_value)
1802
{
1803
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1804
  int error= 0;
1805
  DBUG_ENTER("Field_new_decimal::store_value");
1806
#ifndef DBUG_OFF
1807
  {
1808
    char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
1809
    DBUG_PRINT("enter", ("value: %s", dbug_decimal_as_string(dbug_buff, decimal_value)));
1810
  }
1811
#endif
1812
1813
  /* check that we do not try to write negative value in unsigned field */
1814
  if (unsigned_flag && decimal_value->sign())
1815
  {
1816
    DBUG_PRINT("info", ("unsigned overflow"));
1817
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1818
    error= 1;
1819
    decimal_value= &decimal_zero;
1820
  }
1821
#ifndef DBUG_OFF
1822
  {
1823
    char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
1824
    DBUG_PRINT("info", ("saving with precision %d  scale: %d  value %s",
1825
                        (int)precision, (int)dec,
1826
                        dbug_decimal_as_string(dbug_buff, decimal_value)));
1827
  }
1828
#endif
1829
1830
  if (warn_if_overflow(my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
1831
                                         decimal_value, ptr, precision, dec)))
1832
  {
1833
    my_decimal buff;
1834
    DBUG_PRINT("info", ("overflow"));
1835
    set_value_on_overflow(&buff, decimal_value->sign());
1836
    my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
1837
    error= 1;
1838
  }
1839
  DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
1840
                                          bin_size););
1841
  DBUG_RETURN(error);
1842
}
1843
1844
1845
int Field_new_decimal::store(const char *from, uint length,
1846
                             CHARSET_INFO *charset_arg)
1847
{
1848
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1849
  int err;
1850
  my_decimal decimal_value;
1851
  DBUG_ENTER("Field_new_decimal::store(char*)");
1852
1853
  if ((err= str2my_decimal(E_DEC_FATAL_ERROR &
1854
                           ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
1855
                           from, length, charset_arg,
1856
                           &decimal_value)) &&
1857
      table->in_use->abort_on_warning)
1858
  {
1859
    /* Because "from" is not NUL-terminated and we use %s in the ER() */
1860
    String from_as_str;
1861
    from_as_str.copy(from, length, &my_charset_bin);
1862
1863
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_ERROR,
1864
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
1865
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1866
                        "decimal", from_as_str.c_ptr(), field_name,
1867
                        (ulong) table->in_use->row_count);
1868
1869
    DBUG_RETURN(err);
1870
  }
1871
1872
  switch (err) {
1873
  case E_DEC_TRUNCATED:
1874
    set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
1875
    break;
1876
  case E_DEC_OVERFLOW:
1877
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1878
    set_value_on_overflow(&decimal_value, decimal_value.sign());
1879
    break;
1880
  case E_DEC_BAD_NUM:
1881
    {
1882
      /* Because "from" is not NUL-terminated and we use %s in the ER() */
1883
      String from_as_str;
1884
      from_as_str.copy(from, length, &my_charset_bin);
1885
1886
    push_warning_printf(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
1887
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
1888
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1889
                          "decimal", from_as_str.c_ptr(), field_name,
1890
                        (ulong) table->in_use->row_count);
1891
    my_decimal_set_zero(&decimal_value);
1892
1893
    break;
1894
    }
1895
  }
1896
1897
#ifndef DBUG_OFF
1898
  char dbug_buff[DECIMAL_MAX_STR_LENGTH+1];
1899
  DBUG_PRINT("enter", ("value: %s",
1900
                       dbug_decimal_as_string(dbug_buff, &decimal_value)));
1901
#endif
1902
  store_value(&decimal_value);
1903
  DBUG_RETURN(err);
1904
}
1905
1906
1907
/**
1908
  @todo
1909
  Fix following when double2my_decimal when double2decimal
1910
  will return E_DEC_TRUNCATED always correctly
1911
*/
1912
1913
int Field_new_decimal::store(double nr)
1914
{
1915
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1916
  my_decimal decimal_value;
1917
  int err;
1918
  DBUG_ENTER("Field_new_decimal::store(double)");
1919
1920
  err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
1921
                         &decimal_value);
1922
  if (err)
1923
  {
1924
    if (check_overflow(err))
1925
      set_value_on_overflow(&decimal_value, decimal_value.sign());
1926
    /* Only issue a warning if store_value doesn't issue an warning */
1927
    table->in_use->got_warning= 0;
1928
  }
1929
  if (store_value(&decimal_value))
1930
    err= 1;
1931
  else if (err && !table->in_use->got_warning)
1932
    err= warn_if_overflow(err);
1933
  DBUG_RETURN(err);
1934
}
1935
1936
1937
int Field_new_decimal::store(longlong nr, bool unsigned_val)
1938
{
1939
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1940
  my_decimal decimal_value;
1941
  int err;
1942
1943
  if ((err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
1944
                           nr, unsigned_val, &decimal_value)))
1945
  {
1946
    if (check_overflow(err))
1947
      set_value_on_overflow(&decimal_value, decimal_value.sign());
1948
    /* Only issue a warning if store_value doesn't issue an warning */
1949
    table->in_use->got_warning= 0;
1950
  }
1951
  if (store_value(&decimal_value))
1952
    err= 1;
1953
  else if (err && !table->in_use->got_warning)
1954
    err= warn_if_overflow(err);
1955
  return err;
1956
}
1957
1958
1959
int Field_new_decimal::store_decimal(const my_decimal *decimal_value)
1960
{
1961
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1962
  return store_value(decimal_value);
1963
}
1964
1965
1966
int Field_new_decimal::store_time(MYSQL_TIME *ltime, timestamp_type t_type)
1967
{
1968
    my_decimal decimal_value;
1969
    return store_value(date2my_decimal(ltime, &decimal_value));
1970
}
1971
1972
1973
double Field_new_decimal::val_real(void)
1974
{
1975
  ASSERT_COLUMN_MARKED_FOR_READ;
1976
  double dbl;
1977
  my_decimal decimal_value;
1978
  my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
1979
  return dbl;
1980
}
1981
1982
1983
longlong Field_new_decimal::val_int(void)
1984
{
1985
  ASSERT_COLUMN_MARKED_FOR_READ;
1986
  longlong i;
1987
  my_decimal decimal_value;
1988
  my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
1989
                 unsigned_flag, &i);
1990
  return i;
1991
}
1992
1993
1994
my_decimal* Field_new_decimal::val_decimal(my_decimal *decimal_value)
1995
{
1996
  ASSERT_COLUMN_MARKED_FOR_READ;
1997
  DBUG_ENTER("Field_new_decimal::val_decimal");
1998
  binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
1999
                    precision, dec);
2000
  DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
2001
                                          bin_size););
2002
  DBUG_RETURN(decimal_value);
2003
}
2004
2005
2006
String *Field_new_decimal::val_str(String *val_buffer,
2007
                                   String *val_ptr __attribute__((unused)))
2008
{
2009
  ASSERT_COLUMN_MARKED_FOR_READ;
2010
  my_decimal decimal_value;
2011
  uint fixed_precision= zerofill ? precision : 0;
2012
  my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
2013
                    fixed_precision, dec, '0', val_buffer);
2014
  return val_buffer;
2015
}
2016
2017
2018
int Field_new_decimal::cmp(const uchar *a,const uchar*b)
2019
{
2020
  return memcmp(a, b, bin_size);
2021
}
2022
2023
2024
void Field_new_decimal::sort_string(uchar *buff,
2025
                                    uint length __attribute__((unused)))
2026
{
2027
  memcpy(buff, ptr, bin_size);
2028
}
2029
2030
2031
void Field_new_decimal::sql_type(String &str) const
2032
{
2033
  CHARSET_INFO *cs= str.charset();
2034
  str.length(cs->cset->snprintf(cs, (char*) str.ptr(), str.alloced_length(),
2035
                                "decimal(%d,%d)", precision, (int)dec));
2036
  add_zerofill_and_unsigned(str);
2037
}
2038
2039
2040
/**
2041
   Save the field metadata for new decimal fields.
2042
2043
   Saves the precision in the first byte and decimals() in the second
2044
   byte of the field metadata array at index of *metadata_ptr and 
2045
   *(metadata_ptr + 1).
2046
2047
   @param   metadata_ptr   First byte of field metadata
2048
2049
   @returns number of bytes written to metadata_ptr
2050
*/
2051
int Field_new_decimal::do_save_field_metadata(uchar *metadata_ptr)
2052
{
2053
  *metadata_ptr= precision;
2054
  *(metadata_ptr + 1)= decimals();
2055
  return 2;
2056
}
2057
2058
2059
/**
2060
   Returns the number of bytes field uses in row-based replication 
2061
   row packed size.
2062
2063
   This method is used in row-based replication to determine the number
2064
   of bytes that the field consumes in the row record format. This is
2065
   used to skip fields in the master that do not exist on the slave.
2066
2067
   @param   field_metadata   Encoded size in field metadata
2068
2069
   @returns The size of the field based on the field metadata.
2070
*/
2071
uint Field_new_decimal::pack_length_from_metadata(uint field_metadata)
2072
{
2073
  uint const source_precision= (field_metadata >> 8U) & 0x00ff;
2074
  uint const source_decimal= field_metadata & 0x00ff; 
2075
  uint const source_size= my_decimal_get_binary_size(source_precision, 
2076
                                                     source_decimal);
2077
  return (source_size);
2078
}
2079
2080
2081
/**
2082
   Check to see if field size is compatible with destination.
2083
2084
   This method is used in row-based replication to verify that the slave's
2085
   field size is less than or equal to the master's field size. The 
2086
   encoded field metadata (from the master or source) is decoded and compared
2087
   to the size of this field (the slave or destination). 
2088
2089
   @param   field_metadata   Encoded size in field metadata
2090
2091
   @retval 0 if this field's size is < the source field's size
2092
   @retval 1 if this field's size is >= the source field's size
2093
*/
2094
int Field_new_decimal::compatible_field_size(uint field_metadata)
2095
{
2096
  int compatible= 0;
2097
  uint const source_precision= (field_metadata >> 8U) & 0x00ff;
2098
  uint const source_decimal= field_metadata & 0x00ff; 
2099
  uint const source_size= my_decimal_get_binary_size(source_precision, 
2100
                                                     source_decimal);
2101
  uint const destination_size= row_pack_length();
2102
  compatible= (source_size <= destination_size);
2103
  if (compatible)
2104
    compatible= (source_precision <= precision) &&
2105
                (source_decimal <= decimals());
2106
  return (compatible);
2107
}
2108
2109
2110
uint Field_new_decimal::is_equal(Create_field *new_field)
2111
{
2112
  return ((new_field->sql_type == real_type()) &&
2113
          ((new_field->flags & UNSIGNED_FLAG) == 
2114
           (uint) (flags & UNSIGNED_FLAG)) &&
2115
          ((new_field->flags & AUTO_INCREMENT_FLAG) ==
2116
           (uint) (flags & AUTO_INCREMENT_FLAG)) &&
2117
          (new_field->length == max_display_length()) &&
2118
          (new_field->decimals == dec));
2119
}
2120
2121
2122
/**
2123
   Unpack a decimal field from row data.
2124
2125
   This method is used to unpack a decimal or numeric field from a master
2126
   whose size of the field is less than that of the slave.
2127
  
2128
   @param   to         Destination of the data
2129
   @param   from       Source of the data
2130
   @param   param_data Precision (upper) and decimal (lower) values
2131
2132
   @return  New pointer into memory based on from + length of the data
2133
*/
2134
const uchar *
2135
Field_new_decimal::unpack(uchar* to,
2136
                          const uchar *from,
2137
                          uint param_data,
2138
                          bool low_byte_first)
2139
{
2140
  if (param_data == 0)
2141
    return Field::unpack(to, from, param_data, low_byte_first);
2142
2143
  uint from_precision= (param_data & 0xff00) >> 8U;
2144
  uint from_decimal= param_data & 0x00ff;
2145
  uint length=pack_length();
2146
  uint from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
2147
  uint len= (param_data && (from_pack_len < length)) ?
2148
            from_pack_len : length;
2149
  if ((from_pack_len && (from_pack_len < length)) ||
2150
      (from_precision < precision) ||
2151
      (from_decimal < decimals()))
2152
  {
2153
    /*
2154
      If the master's data is smaller than the slave, we need to convert
2155
      the binary to decimal then resize the decimal converting it back to
2156
      a decimal and write that to the raw data buffer.
2157
    */
2158
    decimal_digit_t dec_buf[DECIMAL_MAX_PRECISION];
2159
    decimal_t dec;
2160
    dec.len= from_precision;
2161
    dec.buf= dec_buf;
2162
    /*
2163
      Note: bin2decimal does not change the length of the field. So it is
2164
      just the first step the resizing operation. The second step does the
2165
      resizing using the precision and decimals from the slave.
2166
    */
2167
    bin2decimal((uchar *)from, &dec, from_precision, from_decimal);
2168
    decimal2bin(&dec, to, precision, decimals());
2169
  }
2170
  else
2171
    memcpy(to, from, len); // Sizes are the same, just copy the data.
2172
  return from+len;
2173
}
2174
2175
/****************************************************************************
2176
** tiny int
2177
****************************************************************************/
2178
2179
int Field_tiny::store(const char *from,uint len,CHARSET_INFO *cs)
2180
{
2181
  ASSERT_COLUMN_MARKED_FOR_WRITE;
2182
  int error;
2183
  longlong rnd;
2184
  
2185
  error= get_int(cs, from, len, &rnd, 255, -128, 127);
2186
  ptr[0]= unsigned_flag ? (char) (uint64_t) rnd : (char) rnd;
2187
  return error;
2188
}
2189
2190
2191
int Field_tiny::store(double nr)
2192
{
2193
  ASSERT_COLUMN_MARKED_FOR_WRITE;
2194
  int error= 0;
2195
  nr=rint(nr);
2196
  if (unsigned_flag)
2197
  {
2198
    if (nr < 0.0)
2199
    {
2200
      *ptr=0;
2201
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2202
      error= 1;
2203
    }
2204
    else if (nr > 255.0)
2205
    {
2206
      *ptr=(char) 255;
2207
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2208
      error= 1;
2209
    }
2210
    else
2211
      *ptr=(char) nr;
2212
  }
2213
  else
2214
  {
2215
    if (nr < -128.0)
2216
    {
2217
      *ptr= (char) -128;
2218
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2219
      error= 1;
2220
    }
2221
    else if (nr > 127.0)
2222
    {
2223
      *ptr=127;
2224
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2225
      error= 1;
2226
    }
2227
    else
2228
      *ptr=(char) (int) nr;
2229
  }
2230
  return error;
2231
}
2232
2233
2234
int Field_tiny::store(longlong nr, bool unsigned_val)
2235
{
2236
  ASSERT_COLUMN_MARKED_FOR_WRITE;
2237
  int error= 0;
2238
2239
  if (unsigned_flag)
2240
  {
2241
    if (nr < 0 && !unsigned_val)
2242
    {
2243
      *ptr= 0;
2244
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2245
      error= 1;
2246
    }
2247
    else if ((uint64_t) nr > (uint64_t) 255)
2248
    {
2249
      *ptr= (char) 255;
2250
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2251
      error= 1;
2252
    }
2253
    else
2254
      *ptr=(char) nr;
2255
  }
2256
  else
2257
  {
2258
    if (nr < 0 && unsigned_val)
2259
      nr= 256;                                    // Generate overflow
2260
    if (nr < -128)
2261
    {
2262
      *ptr= (char) -128;
2263
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2264
      error= 1;
2265
    }
2266
    else if (nr > 127)
2267
    {
2268
      *ptr=127;
2269
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2270
      error= 1;
2271
    }
2272
    else
2273
      *ptr=(char) nr;
2274
  }
2275
  return error;
2276
}
2277
2278
2279
double Field_tiny::val_real(void)
2280
{
2281
  ASSERT_COLUMN_MARKED_FOR_READ;
2282
  int tmp= unsigned_flag ? (int) ptr[0] :
2283
    (int) ((signed char*) ptr)[0];
2284
  return (double) tmp;
2285
}
2286
2287
2288
longlong Field_tiny::val_int(void)
2289
{
2290
  ASSERT_COLUMN_MARKED_FOR_READ;
2291
  int tmp= unsigned_flag ? (int) ptr[0] :
2292
    (int) ((signed char*) ptr)[0];
2293
  return (longlong) tmp;
2294
}
2295
2296
2297
String *Field_tiny::val_str(String *val_buffer,
2298
			    String *val_ptr __attribute__((unused)))
2299
{
2300
  ASSERT_COLUMN_MARKED_FOR_READ;
2301
  CHARSET_INFO *cs= &my_charset_bin;
2302
  uint length;
2303
  uint mlength=max(field_length+1,5*cs->mbmaxlen);
2304
  val_buffer->alloc(mlength);
2305
  char *to=(char*) val_buffer->ptr();
2306
2307
  if (unsigned_flag)
2308
    length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
2309
					   (long) *ptr);
2310
  else
2311
    length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
2312
					   (long) *((signed char*) ptr));
2313
  
2314
  val_buffer->length(length);
2315
  if (zerofill)
2316
    prepend_zeros(val_buffer);
2317
  return val_buffer;
2318
}
2319
2320
bool Field_tiny::send_binary(Protocol *protocol)
2321
{
2322
  return protocol->store_tiny((longlong) (int8) ptr[0]);
2323
}
2324
2325
int Field_tiny::cmp(const uchar *a_ptr, const uchar *b_ptr)
2326
{
2327
  signed char a,b;
2328
  a=(signed char) a_ptr[0]; b= (signed char) b_ptr[0];
2329
  if (unsigned_flag)
2330
    return ((uchar) a < (uchar) b) ? -1 : ((uchar) a > (uchar) b) ? 1 : 0;
2331
  return (a < b) ? -1 : (a > b) ? 1 : 0;
2332
}
2333
2334
void Field_tiny::sort_string(uchar *to,uint length __attribute__((unused)))
2335
{
2336
  if (unsigned_flag)
2337
    *to= *ptr;
2338
  else
2339
    to[0] = (char) (ptr[0] ^ (uchar) 128);	/* Revers signbit */
2340
}
2341
2342
void Field_tiny::sql_type(String &res) const
2343
{
2344
  CHARSET_INFO *cs=res.charset();
2345
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2346
			  "tinyint(%d)",(int) field_length));
2347
  add_zerofill_and_unsigned(res);
2348
}
2349
2350
/****************************************************************************
2351
 Field type short int (2 byte)
2352
****************************************************************************/
2353
2354
int Field_short::store(const char *from,uint len,CHARSET_INFO *cs)
2355
{
2356
  ASSERT_COLUMN_MARKED_FOR_WRITE;
2357
  int store_tmp;
2358
  int error;
2359
  longlong rnd;
2360
  
2361
  error= get_int(cs, from, len, &rnd, UINT_MAX16, INT_MIN16, INT_MAX16);
2362
  store_tmp= unsigned_flag ? (int) (uint64_t) rnd : (int) rnd;
2363
#ifdef WORDS_BIGENDIAN
2364
  if (table->s->db_low_byte_first)
2365
  {
2366
    int2store(ptr, store_tmp);
2367
  }
2368
  else
2369
#endif
2370
    shortstore(ptr, (short) store_tmp);
2371
  return error;
2372
}
2373
2374
2375
int Field_short::store(double nr)
2376
{
2377
  ASSERT_COLUMN_MARKED_FOR_WRITE;
2378
  int error= 0;
2379
  int16 res;
2380
  nr=rint(nr);
2381
  if (unsigned_flag)
2382
  {
2383
    if (nr < 0)
2384
    {
2385
      res=0;
2386
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2387
      error= 1;
2388
    }
2389
    else if (nr > (double) UINT_MAX16)
2390
    {
2391
      res=(int16) UINT_MAX16;
2392
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2393
      error= 1;
2394
    }
2395
    else
2396
      res=(int16) (uint16) nr;
2397
  }
2398
  else
2399
  {
2400
    if (nr < (double) INT_MIN16)
2401
    {
2402
      res=INT_MIN16;
2403
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2404
      error= 1;
2405
    }
2406
    else if (nr > (double) INT_MAX16)
2407
    {
2408
      res=INT_MAX16;
2409
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2410
      error= 1;
2411
    }
2412
    else
2413
      res=(int16) (int) nr;
2414
  }
2415
#ifdef WORDS_BIGENDIAN
2416
  if (table->s->db_low_byte_first)
2417
  {
2418
    int2store(ptr,res);
2419
  }
2420
  else
2421
#endif
2422
    shortstore(ptr,res);
2423
  return error;
2424
}
2425
2426
2427
int Field_short::store(longlong nr, bool unsigned_val)
2428
{
2429
  ASSERT_COLUMN_MARKED_FOR_WRITE;
2430
  int error= 0;
2431
  int16 res;
2432
2433
  if (unsigned_flag)
2434
  {
2435
    if (nr < 0L && !unsigned_val)
2436
    {
2437
      res=0;
2438
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2439
      error= 1;
2440
    }
2441
    else if ((uint64_t) nr > (uint64_t) UINT_MAX16)
2442
    {
2443
      res=(int16) UINT_MAX16;
2444
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2445
      error= 1;
2446
    }
2447
    else
2448
      res=(int16) (uint16) nr;
2449
  }
2450
  else
2451
  {
2452
    if (nr < 0 && unsigned_val)
2453
      nr= UINT_MAX16+1;                         // Generate overflow
2454
2455
    if (nr < INT_MIN16)
2456
    {
2457
      res=INT_MIN16;
2458
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2459
      error= 1;
2460
    }
2461
    else if (nr > (longlong) INT_MAX16)
2462
    {
2463
      res=INT_MAX16;
2464
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2465
      error= 1;
2466
    }
2467
    else
2468
      res=(int16) nr;
2469
  }
2470
#ifdef WORDS_BIGENDIAN
2471
  if (table->s->db_low_byte_first)
2472
  {
2473
    int2store(ptr,res);
2474
  }
2475
  else
2476
#endif
2477
    shortstore(ptr,res);
2478
  return error;
2479
}
2480
2481
2482
double Field_short::val_real(void)
2483
{
2484
  ASSERT_COLUMN_MARKED_FOR_READ;
2485
  short j;
2486
#ifdef WORDS_BIGENDIAN
2487
  if (table->s->db_low_byte_first)
2488
    j=sint2korr(ptr);
2489
  else
2490
#endif
2491
    shortget(j,ptr);
2492
  return unsigned_flag ? (double) (unsigned short) j : (double) j;
2493
}
2494
2495
longlong Field_short::val_int(void)
2496
{
2497
  ASSERT_COLUMN_MARKED_FOR_READ;
2498
  short j;
2499
#ifdef WORDS_BIGENDIAN
2500
  if (table->s->db_low_byte_first)
2501
    j=sint2korr(ptr);
2502
  else
2503
#endif
2504
    shortget(j,ptr);
2505
  return unsigned_flag ? (longlong) (unsigned short) j : (longlong) j;
2506
}
2507
2508
2509
String *Field_short::val_str(String *val_buffer,
2510
			     String *val_ptr __attribute__((unused)))
2511
{
2512
  ASSERT_COLUMN_MARKED_FOR_READ;
2513
  CHARSET_INFO *cs= &my_charset_bin;
2514
  uint length;
2515
  uint mlength=max(field_length+1,7*cs->mbmaxlen);
2516
  val_buffer->alloc(mlength);
2517
  char *to=(char*) val_buffer->ptr();
2518
  short j;
2519
#ifdef WORDS_BIGENDIAN
2520
  if (table->s->db_low_byte_first)
2521
    j=sint2korr(ptr);
2522
  else
2523
#endif
2524
    shortget(j,ptr);
2525
2526
  if (unsigned_flag)
2527
    length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10, 
2528
					  (long) (uint16) j);
2529
  else
2530
    length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
2531
  val_buffer->length(length);
2532
  if (zerofill)
2533
    prepend_zeros(val_buffer);
2534
  return val_buffer;
2535
}
2536
2537
2538
bool Field_short::send_binary(Protocol *protocol)
2539
{
2540
  return protocol->store_short(Field_short::val_int());
2541
}
2542
2543
2544
int Field_short::cmp(const uchar *a_ptr, const uchar *b_ptr)
2545
{
2546
  short a,b;
2547
#ifdef WORDS_BIGENDIAN
2548
  if (table->s->db_low_byte_first)
2549
  {
2550
    a=sint2korr(a_ptr);
2551
    b=sint2korr(b_ptr);
2552
  }
2553
  else
2554
#endif
2555
  {
2556
    shortget(a,a_ptr);
2557
    shortget(b,b_ptr);
2558
  }
2559
2560
  if (unsigned_flag)
2561
    return ((unsigned short) a < (unsigned short) b) ? -1 :
2562
    ((unsigned short) a > (unsigned short) b) ? 1 : 0;
2563
  return (a < b) ? -1 : (a > b) ? 1 : 0;
2564
}
2565
2566
void Field_short::sort_string(uchar *to,uint length __attribute__((unused)))
2567
{
2568
#ifdef WORDS_BIGENDIAN
2569
  if (!table->s->db_low_byte_first)
2570
  {
2571
    if (unsigned_flag)
2572
      to[0] = ptr[0];
2573
    else
2574
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
2575
    to[1]   = ptr[1];
2576
  }
2577
  else
2578
#endif
2579
  {
2580
    if (unsigned_flag)
2581
      to[0] = ptr[1];
2582
    else
2583
      to[0] = (char) (ptr[1] ^ 128);		/* Revers signbit */
2584
    to[1]   = ptr[0];
2585
  }
2586
}
2587
2588
void Field_short::sql_type(String &res) const
2589
{
2590
  CHARSET_INFO *cs=res.charset();
2591
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2592
			  "smallint(%d)",(int) field_length));
2593
  add_zerofill_and_unsigned(res);
2594
}
2595
2596
2597
/****************************************************************************
2598
  Field type medium int (3 byte)
2599
****************************************************************************/
2600
2601
int Field_medium::store(const char *from,uint len,CHARSET_INFO *cs)
2602
{
2603
  ASSERT_COLUMN_MARKED_FOR_WRITE;
2604
  int store_tmp;
2605
  int error;
2606
  longlong rnd;
2607
  
2608
  error= get_int(cs, from, len, &rnd, UINT_MAX24, INT_MIN24, INT_MAX24);
2609
  store_tmp= unsigned_flag ? (int) (uint64_t) rnd : (int) rnd;
2610
  int3store(ptr, store_tmp);
2611
  return error;
2612
}
2613
2614
2615
int Field_medium::store(double nr)
2616
{
2617
  ASSERT_COLUMN_MARKED_FOR_WRITE;
2618
  int error= 0;
2619
  nr=rint(nr);
2620
  if (unsigned_flag)
2621
  {
2622
    if (nr < 0)
2623
    {
2624
      int3store(ptr,0);
2625
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2626
      error= 1;
2627
    }
2628
    else if (nr >= (double) (long) (1L << 24))
2629
    {
2630
      uint32 tmp=(uint32) (1L << 24)-1L;
2631
      int3store(ptr,tmp);
2632
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2633
      error= 1;
2634
    }
2635
    else
2636
      int3store(ptr,(uint32) nr);
2637
  }
2638
  else
2639
  {
2640
    if (nr < (double) INT_MIN24)
2641
    {
2642
      long tmp=(long) INT_MIN24;
2643
      int3store(ptr,tmp);
2644
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2645
      error= 1;
2646
    }
2647
    else if (nr > (double) INT_MAX24)
2648
    {
2649
      long tmp=(long) INT_MAX24;
2650
      int3store(ptr,tmp);
2651
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2652
      error= 1;
2653
    }
2654
    else
2655
      int3store(ptr,(long) nr);
2656
  }
2657
  return error;
2658
}
2659
2660
2661
int Field_medium::store(longlong nr, bool unsigned_val)
2662
{
2663
  ASSERT_COLUMN_MARKED_FOR_WRITE;
2664
  int error= 0;
2665
2666
  if (unsigned_flag)
2667
  {
2668
    if (nr < 0 && !unsigned_val)
2669
    {
2670
      int3store(ptr,0);
2671
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2672
      error= 1;
2673
    }
2674
    else if ((uint64_t) nr >= (uint64_t) (long) (1L << 24))
2675
    {
2676
      long tmp= (long) (1L << 24)-1L;
2677
      int3store(ptr,tmp);
2678
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2679
      error= 1;
2680
    }
2681
    else
2682
      int3store(ptr,(uint32) nr);
2683
  }
2684
  else
2685
  {
2686
    if (nr < 0 && unsigned_val)
2687
      nr= (uint64_t) (long) (1L << 24);        // Generate overflow
2688
2689
    if (nr < (longlong) INT_MIN24)
2690
    {
2691
      long tmp= (long) INT_MIN24;
2692
      int3store(ptr,tmp);
2693
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2694
      error= 1;
2695
    }
2696
    else if (nr > (longlong) INT_MAX24)
2697
    {
2698
      long tmp=(long) INT_MAX24;
2699
      int3store(ptr,tmp);
2700
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2701
      error= 1;
2702
    }
2703
    else
2704
      int3store(ptr,(long) nr);
2705
  }
2706
  return error;
2707
}
2708
2709
2710
double Field_medium::val_real(void)
2711
{
2712
  ASSERT_COLUMN_MARKED_FOR_READ;
2713
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
2714
  return (double) j;
2715
}
2716
2717
2718
longlong Field_medium::val_int(void)
2719
{
2720
  ASSERT_COLUMN_MARKED_FOR_READ;
2721
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
2722
  return (longlong) j;
2723
}
2724
2725
2726
String *Field_medium::val_str(String *val_buffer,
2727
			      String *val_ptr __attribute__((unused)))
2728
{
2729
  ASSERT_COLUMN_MARKED_FOR_READ;
2730
  CHARSET_INFO *cs= &my_charset_bin;
2731
  uint length;
2732
  uint mlength=max(field_length+1,10*cs->mbmaxlen);
2733
  val_buffer->alloc(mlength);
2734
  char *to=(char*) val_buffer->ptr();
2735
  long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
2736
2737
  length=(uint) cs->cset->long10_to_str(cs,to,mlength,-10,j);
2738
  val_buffer->length(length);
2739
  if (zerofill)
2740
    prepend_zeros(val_buffer); /* purecov: inspected */
2741
  return val_buffer;
2742
}
2743
2744
2745
bool Field_medium::send_binary(Protocol *protocol)
2746
{
2747
  ASSERT_COLUMN_MARKED_FOR_READ;
2748
  return protocol->store_long(Field_medium::val_int());
2749
}
2750
2751
2752
int Field_medium::cmp(const uchar *a_ptr, const uchar *b_ptr)
2753
{
2754
  long a,b;
2755
  if (unsigned_flag)
2756
  {
2757
    a=uint3korr(a_ptr);
2758
    b=uint3korr(b_ptr);
2759
  }
2760
  else
2761
  {
2762
    a=sint3korr(a_ptr);
2763
    b=sint3korr(b_ptr);
2764
  }
2765
  return (a < b) ? -1 : (a > b) ? 1 : 0;
2766
}
2767
2768
void Field_medium::sort_string(uchar *to,uint length __attribute__((unused)))
2769
{
2770
  if (unsigned_flag)
2771
    to[0] = ptr[2];
2772
  else
2773
    to[0] = (uchar) (ptr[2] ^ 128);		/* Revers signbit */
2774
  to[1] = ptr[1];
2775
  to[2] = ptr[0];
2776
}
2777
2778
2779
void Field_medium::sql_type(String &res) const
2780
{
2781
  CHARSET_INFO *cs=res.charset();
2782
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(), 
2783
			  "mediumint(%d)",(int) field_length));
2784
  add_zerofill_and_unsigned(res);
2785
}
2786
2787
/****************************************************************************
2788
** long int
2789
****************************************************************************/
2790
2791
int Field_long::store(const char *from,uint len,CHARSET_INFO *cs)
2792
{
2793
  ASSERT_COLUMN_MARKED_FOR_WRITE;
2794
  long store_tmp;
2795
  int error;
2796
  longlong rnd;
2797
  
2798
  error= get_int(cs, from, len, &rnd, UINT_MAX32, INT_MIN32, INT_MAX32);
2799
  store_tmp= unsigned_flag ? (long) (uint64_t) rnd : (long) rnd;
2800
#ifdef WORDS_BIGENDIAN
2801
  if (table->s->db_low_byte_first)
2802
  {
2803
    int4store(ptr, store_tmp);
2804
  }
2805
  else
2806
#endif
2807
    longstore(ptr, store_tmp);
2808
  return error;
2809
}
2810
2811
2812
int Field_long::store(double nr)
2813
{
2814
  ASSERT_COLUMN_MARKED_FOR_WRITE;
2815
  int error= 0;
2816
  int32 res;
2817
  nr=rint(nr);
2818
  if (unsigned_flag)
2819
  {
2820
    if (nr < 0)
2821
    {
2822
      res=0;
2823
      error= 1;
2824
    }
2825
    else if (nr > (double) UINT_MAX32)
2826
    {
2827
      res= UINT_MAX32;
2828
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2829
      error= 1;
2830
    }
2831
    else
2832
      res=(int32) (ulong) nr;
2833
  }
2834
  else
2835
  {
2836
    if (nr < (double) INT_MIN32)
2837
    {
2838
      res=(int32) INT_MIN32;
2839
      error= 1;
2840
    }
2841
    else if (nr > (double) INT_MAX32)
2842
    {
2843
      res=(int32) INT_MAX32;
2844
      error= 1;
2845
    }
2846
    else
2847
      res=(int32) (longlong) nr;
2848
  }
2849
  if (error)
2850
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2851
2852
#ifdef WORDS_BIGENDIAN
2853
  if (table->s->db_low_byte_first)
2854
  {
2855
    int4store(ptr,res);
2856
  }
2857
  else
2858
#endif
2859
    longstore(ptr,res);
2860
  return error;
2861
}
2862
2863
2864
int Field_long::store(longlong nr, bool unsigned_val)
2865
{
2866
  ASSERT_COLUMN_MARKED_FOR_WRITE;
2867
  int error= 0;
2868
  int32 res;
2869
2870
  if (unsigned_flag)
2871
  {
2872
    if (nr < 0 && !unsigned_val)
2873
    {
2874
      res=0;
2875
      error= 1;
2876
    }
80.1.1 by Brian Aker
LL() cleanup
2877
    else if ((uint64_t) nr >= (1LL << 32))
1 by brian
clean slate
2878
    {
2879
      res=(int32) (uint32) ~0L;
2880
      error= 1;
2881
    }
2882
    else
2883
      res=(int32) (uint32) nr;
2884
  }
2885
  else
2886
  {
2887
    if (nr < 0 && unsigned_val)
2888
      nr= ((longlong) INT_MAX32) + 1;           // Generate overflow
2889
    if (nr < (longlong) INT_MIN32) 
2890
    {
2891
      res=(int32) INT_MIN32;
2892
      error= 1;
2893
    }
2894
    else if (nr > (longlong) INT_MAX32)
2895
    {
2896
      res=(int32) INT_MAX32;
2897
      error= 1;
2898
    }
2899
    else
2900
      res=(int32) nr;
2901
  }
2902
  if (error)
2903
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2904
2905
#ifdef WORDS_BIGENDIAN
2906
  if (table->s->db_low_byte_first)
2907
  {
2908
    int4store(ptr,res);
2909
  }
2910
  else
2911
#endif
2912
    longstore(ptr,res);
2913
  return error;
2914
}
2915
2916
2917
double Field_long::val_real(void)
2918
{
2919
  ASSERT_COLUMN_MARKED_FOR_READ;
2920
  int32 j;
2921
#ifdef WORDS_BIGENDIAN
2922
  if (table->s->db_low_byte_first)
2923
    j=sint4korr(ptr);
2924
  else
2925
#endif
2926
    longget(j,ptr);
2927
  return unsigned_flag ? (double) (uint32) j : (double) j;
2928
}
2929
2930
longlong Field_long::val_int(void)
2931
{
2932
  ASSERT_COLUMN_MARKED_FOR_READ;
2933
  int32 j;
2934
  /* See the comment in Field_long::store(long long) */
2935
  DBUG_ASSERT(table->in_use == current_thd);
2936
#ifdef WORDS_BIGENDIAN
2937
  if (table->s->db_low_byte_first)
2938
    j=sint4korr(ptr);
2939
  else
2940
#endif
2941
    longget(j,ptr);
2942
  return unsigned_flag ? (longlong) (uint32) j : (longlong) j;
2943
}
2944
2945
String *Field_long::val_str(String *val_buffer,
2946
			    String *val_ptr __attribute__((unused)))
2947
{
2948
  ASSERT_COLUMN_MARKED_FOR_READ;
2949
  CHARSET_INFO *cs= &my_charset_bin;
2950
  uint length;
2951
  uint mlength=max(field_length+1,12*cs->mbmaxlen);
2952
  val_buffer->alloc(mlength);
2953
  char *to=(char*) val_buffer->ptr();
2954
  int32 j;
2955
#ifdef WORDS_BIGENDIAN
2956
  if (table->s->db_low_byte_first)
2957
    j=sint4korr(ptr);
2958
  else
2959
#endif
2960
    longget(j,ptr);
2961
2962
  if (unsigned_flag)
2963
    length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32)j);
2964
  else
2965
    length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
2966
  val_buffer->length(length);
2967
  if (zerofill)
2968
    prepend_zeros(val_buffer);
2969
  return val_buffer;
2970
}
2971
2972
2973
bool Field_long::send_binary(Protocol *protocol)
2974
{
2975
  ASSERT_COLUMN_MARKED_FOR_READ;
2976
  return protocol->store_long(Field_long::val_int());
2977
}
2978
2979
int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr)
2980
{
2981
  int32 a,b;
2982
#ifdef WORDS_BIGENDIAN
2983
  if (table->s->db_low_byte_first)
2984
  {
2985
    a=sint4korr(a_ptr);
2986
    b=sint4korr(b_ptr);
2987
  }
2988
  else
2989
#endif
2990
  {
2991
    longget(a,a_ptr);
2992
    longget(b,b_ptr);
2993
  }
2994
  if (unsigned_flag)
2995
    return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
2996
  return (a < b) ? -1 : (a > b) ? 1 : 0;
2997
}
2998
2999
void Field_long::sort_string(uchar *to,uint length __attribute__((unused)))
3000
{
3001
#ifdef WORDS_BIGENDIAN
3002
  if (!table->s->db_low_byte_first)
3003
  {
3004
    if (unsigned_flag)
3005
      to[0] = ptr[0];
3006
    else
3007
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
3008
    to[1]   = ptr[1];
3009
    to[2]   = ptr[2];
3010
    to[3]   = ptr[3];
3011
  }
3012
  else
3013
#endif
3014
  {
3015
    if (unsigned_flag)
3016
      to[0] = ptr[3];
3017
    else
3018
      to[0] = (char) (ptr[3] ^ 128);		/* Revers signbit */
3019
    to[1]   = ptr[2];
3020
    to[2]   = ptr[1];
3021
    to[3]   = ptr[0];
3022
  }
3023
}
3024
3025
3026
void Field_long::sql_type(String &res) const
3027
{
3028
  CHARSET_INFO *cs=res.charset();
3029
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3030
			  "int(%d)",(int) field_length));
3031
  add_zerofill_and_unsigned(res);
3032
}
3033
3034
/****************************************************************************
3035
 Field type longlong int (8 bytes)
3036
****************************************************************************/
3037
3038
int Field_longlong::store(const char *from,uint len,CHARSET_INFO *cs)
3039
{
3040
  ASSERT_COLUMN_MARKED_FOR_WRITE;
3041
  int error= 0;
3042
  char *end;
3043
  uint64_t tmp;
3044
3045
  tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&error);
3046
  if (error == MY_ERRNO_ERANGE)
3047
  {
3048
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3049
    error= 1;
3050
  }
3051
  else if (table->in_use->count_cuted_fields && 
3052
           check_int(cs, from, len, end, error))
3053
    error= 1;
3054
  else
3055
    error= 0;
3056
#ifdef WORDS_BIGENDIAN
3057
  if (table->s->db_low_byte_first)
3058
  {
3059
    int8store(ptr,tmp);
3060
  }
3061
  else
3062
#endif
3063
    longlongstore(ptr,tmp);
3064
  return error;
3065
}
3066
3067
3068
int Field_longlong::store(double nr)
3069
{
3070
  ASSERT_COLUMN_MARKED_FOR_WRITE;
3071
  int error= 0;
3072
  longlong res;
3073
3074
  nr= rint(nr);
3075
  if (unsigned_flag)
3076
  {
3077
    if (nr < 0)
3078
    {
3079
      res=0;
3080
      error= 1;
3081
    }
3082
    else if (nr >= (double) ULONGLONG_MAX)
3083
    {
3084
      res= ~(longlong) 0;
3085
      error= 1;
3086
    }
3087
    else
3088
      res=(longlong) (uint64_t) nr;
3089
  }
3090
  else
3091
  {
3092
    if (nr <= (double) LONGLONG_MIN)
3093
    {
3094
      res= LONGLONG_MIN;
3095
      error= (nr < (double) LONGLONG_MIN);
3096
    }
3097
    else if (nr >= (double) (uint64_t) LONGLONG_MAX)
3098
    {
3099
      res= LONGLONG_MAX;
3100
      error= (nr > (double) LONGLONG_MAX);
3101
    }
3102
    else
3103
      res=(longlong) nr;
3104
  }
3105
  if (error)
3106
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3107
3108
#ifdef WORDS_BIGENDIAN
3109
  if (table->s->db_low_byte_first)
3110
  {
3111
    int8store(ptr,res);
3112
  }
3113
  else
3114
#endif
3115
    longlongstore(ptr,res);
3116
  return error;
3117
}
3118
3119
3120
int Field_longlong::store(longlong nr, bool unsigned_val)
3121
{
3122
  ASSERT_COLUMN_MARKED_FOR_WRITE;
3123
  int error= 0;
3124
3125
  if (nr < 0)                                   // Only possible error
3126
  {
3127
    /*
3128
      if field is unsigned and value is signed (< 0) or
3129
      if field is signed and value is unsigned we have an overflow
3130
    */
3131
    if (unsigned_flag != unsigned_val)
3132
    {
3133
      nr= unsigned_flag ? (uint64_t) 0 : (uint64_t) LONGLONG_MAX;
3134
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3135
      error= 1;
3136
    }
3137
  }
3138
3139
#ifdef WORDS_BIGENDIAN
3140
  if (table->s->db_low_byte_first)
3141
  {
3142
    int8store(ptr,nr);
3143
  }
3144
  else
3145
#endif
3146
    longlongstore(ptr,nr);
3147
  return error;
3148
}
3149
3150
3151
double Field_longlong::val_real(void)
3152
{
3153
  ASSERT_COLUMN_MARKED_FOR_READ;
3154
  longlong j;
3155
#ifdef WORDS_BIGENDIAN
3156
  if (table->s->db_low_byte_first)
3157
  {
3158
    j=sint8korr(ptr);
3159
  }
3160
  else
3161
#endif
3162
    longlongget(j,ptr);
3163
  /* The following is open coded to avoid a bug in gcc 3.3 */
3164
  if (unsigned_flag)
3165
  {
3166
    uint64_t tmp= (uint64_t) j;
3167
    return ulonglong2double(tmp);
3168
  }
3169
  return (double) j;
3170
}
3171
3172
3173
longlong Field_longlong::val_int(void)
3174
{
3175
  ASSERT_COLUMN_MARKED_FOR_READ;
3176
  longlong j;
3177
#ifdef WORDS_BIGENDIAN
3178
  if (table->s->db_low_byte_first)
3179
    j=sint8korr(ptr);
3180
  else
3181
#endif
3182
    longlongget(j,ptr);
3183
  return j;
3184
}
3185
3186
3187
String *Field_longlong::val_str(String *val_buffer,
3188
				String *val_ptr __attribute__((unused)))
3189
{
3190
  CHARSET_INFO *cs= &my_charset_bin;
3191
  uint length;
3192
  uint mlength=max(field_length+1,22*cs->mbmaxlen);
3193
  val_buffer->alloc(mlength);
3194
  char *to=(char*) val_buffer->ptr();
3195
  longlong j;
3196
#ifdef WORDS_BIGENDIAN
3197
  if (table->s->db_low_byte_first)
3198
    j=sint8korr(ptr);
3199
  else
3200
#endif
3201
    longlongget(j,ptr);
3202
3203
  length=(uint) (cs->cset->longlong10_to_str)(cs,to,mlength,
3204
					unsigned_flag ? 10 : -10, j);
3205
  val_buffer->length(length);
3206
  if (zerofill)
3207
    prepend_zeros(val_buffer);
3208
  return val_buffer;
3209
}
3210
3211
3212
bool Field_longlong::send_binary(Protocol *protocol)
3213
{
3214
  ASSERT_COLUMN_MARKED_FOR_READ;
3215
  return protocol->store_longlong(Field_longlong::val_int(), unsigned_flag);
3216
}
3217
3218
3219
int Field_longlong::cmp(const uchar *a_ptr, const uchar *b_ptr)
3220
{
3221
  longlong a,b;
3222
#ifdef WORDS_BIGENDIAN
3223
  if (table->s->db_low_byte_first)
3224
  {
3225
    a=sint8korr(a_ptr);
3226
    b=sint8korr(b_ptr);
3227
  }
3228
  else
3229
#endif
3230
  {
3231
    longlongget(a,a_ptr);
3232
    longlongget(b,b_ptr);
3233
  }
3234
  if (unsigned_flag)
3235
    return ((uint64_t) a < (uint64_t) b) ? -1 :
3236
    ((uint64_t) a > (uint64_t) b) ? 1 : 0;
3237
  return (a < b) ? -1 : (a > b) ? 1 : 0;
3238
}
3239
3240
void Field_longlong::sort_string(uchar *to,uint length __attribute__((unused)))
3241
{
3242
#ifdef WORDS_BIGENDIAN
3243
  if (!table->s->db_low_byte_first)
3244
  {
3245
    if (unsigned_flag)
3246
      to[0] = ptr[0];
3247
    else
3248
      to[0] = (char) (ptr[0] ^ 128);		/* Revers signbit */
3249
    to[1]   = ptr[1];
3250
    to[2]   = ptr[2];
3251
    to[3]   = ptr[3];
3252
    to[4]   = ptr[4];
3253
    to[5]   = ptr[5];
3254
    to[6]   = ptr[6];
3255
    to[7]   = ptr[7];
3256
  }
3257
  else
3258
#endif
3259
  {
3260
    if (unsigned_flag)
3261
      to[0] = ptr[7];
3262
    else
3263
      to[0] = (char) (ptr[7] ^ 128);		/* Revers signbit */
3264
    to[1]   = ptr[6];
3265
    to[2]   = ptr[5];
3266
    to[3]   = ptr[4];
3267
    to[4]   = ptr[3];
3268
    to[5]   = ptr[2];
3269
    to[6]   = ptr[1];
3270
    to[7]   = ptr[0];
3271
  }
3272
}
3273
3274
3275
void Field_longlong::sql_type(String &res) const
3276
{
3277
  CHARSET_INFO *cs=res.charset();
3278
  res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3279
			  "bigint(%d)",(int) field_length));
3280
  add_zerofill_and_unsigned(res);
3281
}
3282
3283
3284
/*
3285
  Floating-point numbers
3286
 */
3287
3288
uchar *
3289
Field_real::pack(uchar *to, const uchar *from,
3290
                 uint max_length, bool low_byte_first)
3291
{
3292
  DBUG_ENTER("Field_real::pack");
3293
  DBUG_ASSERT(max_length >= pack_length());
3294
  DBUG_PRINT("debug", ("pack_length(): %u", pack_length()));
3295
#ifdef WORDS_BIGENDIAN
3296
  if (low_byte_first != table->s->db_low_byte_first)
3297
  {
3298
    const uchar *dptr= from + pack_length();
3299
    while (dptr-- > from)
3300
      *to++ = *dptr;
3301
    DBUG_RETURN(to);
3302
  }
3303
  else
3304
#endif
3305
    DBUG_RETURN(Field::pack(to, from, max_length, low_byte_first));
3306
}
3307
3308
const uchar *
3309
Field_real::unpack(uchar *to, const uchar *from,
3310
                   uint param_data, bool low_byte_first)
3311
{
3312
  DBUG_ENTER("Field_real::unpack");
3313
  DBUG_PRINT("debug", ("pack_length(): %u", pack_length()));
3314
#ifdef WORDS_BIGENDIAN
3315
  if (low_byte_first != table->s->db_low_byte_first)
3316
  {
3317
    const uchar *dptr= from + pack_length();
3318
    while (dptr-- > from)
3319
      *to++ = *dptr;
3320
    DBUG_RETURN(from + pack_length());
3321
  }
3322
  else
3323
#endif
3324
    DBUG_RETURN(Field::unpack(to, from, param_data, low_byte_first));
3325
}
3326
3327
/****************************************************************************
3328
  single precision float
3329
****************************************************************************/
3330
3331
int Field_float::store(const char *from,uint len,CHARSET_INFO *cs)
3332
{
3333
  int error;
3334
  char *end;
3335
  double nr= my_strntod(cs,(char*) from,len,&end,&error);
3336
  if (error || (!len || (((uint)(end-from) != len) && table->in_use->count_cuted_fields)))
3337
  {
3338
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
3339
                (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
3340
    error= error ? 1 : 2;
3341
  }
3342
  Field_float::store(nr);
3343
  return error;
3344
}
3345
3346
3347
int Field_float::store(double nr)
3348
{
3349
  ASSERT_COLUMN_MARKED_FOR_WRITE;
3350
  int error= truncate(&nr, FLT_MAX);
3351
  float j= (float)nr;
3352
3353
#ifdef WORDS_BIGENDIAN
3354
  if (table->s->db_low_byte_first)
3355
  {
3356
    float4store(ptr,j);
3357
  }
3358
  else
3359
#endif
3360
    memcpy_fixed(ptr,(uchar*) &j,sizeof(j));
3361
  return error;
3362
}
3363
3364
3365
int Field_float::store(longlong nr, bool unsigned_val)
3366
{
3367
  return Field_float::store(unsigned_val ? ulonglong2double((uint64_t) nr) :
3368
                            (double) nr);
3369
}
3370
3371
3372
double Field_float::val_real(void)
3373
{
3374
  ASSERT_COLUMN_MARKED_FOR_READ;
3375
  float j;
3376
#ifdef WORDS_BIGENDIAN
3377
  if (table->s->db_low_byte_first)
3378
  {
3379
    float4get(j,ptr);
3380
  }
3381
  else
3382
#endif
3383
    memcpy_fixed((uchar*) &j,ptr,sizeof(j));
3384
  return ((double) j);
3385
}
3386
3387
longlong Field_float::val_int(void)
3388
{
3389
  float j;
3390
#ifdef WORDS_BIGENDIAN
3391
  if (table->s->db_low_byte_first)
3392
  {
3393
    float4get(j,ptr);
3394
  }
3395
  else
3396
#endif
3397
    memcpy_fixed((uchar*) &j,ptr,sizeof(j));
3398
  return (longlong) rint(j);
3399
}
3400
3401
3402
String *Field_float::val_str(String *val_buffer,
3403
			     String *val_ptr __attribute__((unused)))
3404
{
3405
  ASSERT_COLUMN_MARKED_FOR_READ;
3406
  float nr;
3407
#ifdef WORDS_BIGENDIAN
3408
  if (table->s->db_low_byte_first)
3409
  {
3410
    float4get(nr,ptr);
3411
  }
3412
  else
3413
#endif
3414
    memcpy_fixed((uchar*) &nr,ptr,sizeof(nr));
3415
3416
  uint to_length=max(field_length,70);
3417
  val_buffer->alloc(to_length);
3418
  char *to=(char*) val_buffer->ptr();
3419
  size_t len;
3420
3421
  if (dec >= NOT_FIXED_DEC)
3422
    len= my_gcvt(nr, MY_GCVT_ARG_FLOAT, to_length - 1, to, NULL);
3423
  else
3424
  {
3425
    /*
3426
      We are safe here because the buffer length is >= 70, and
3427
      fabs(float) < 10^39, dec < NOT_FIXED_DEC. So the resulting string
3428
      will be not longer than 69 chars + terminating '\0'.
3429
    */
3430
    len= my_fcvt(nr, dec, to, NULL);
3431
  }
3432
  val_buffer->length((uint) len);
3433
  if (zerofill)
3434
    prepend_zeros(val_buffer);
3435
  return val_buffer;
3436
}
3437
3438
3439
int Field_float::cmp(const uchar *a_ptr, const uchar *b_ptr)
3440
{
3441
  float a,b;
3442
#ifdef WORDS_BIGENDIAN
3443
  if (table->s->db_low_byte_first)
3444
  {
3445
    float4get(a,a_ptr);
3446
    float4get(b,b_ptr);
3447
  }
3448
  else
3449
#endif
3450
  {
3451
    memcpy_fixed(&a,a_ptr,sizeof(float));
3452
    memcpy_fixed(&b,b_ptr,sizeof(float));
3453
  }
3454
  return (a < b) ? -1 : (a > b) ? 1 : 0;
3455
}
3456
3457
#define FLT_EXP_DIG (sizeof(float)*8-FLT_MANT_DIG)
3458
3459
void Field_float::sort_string(uchar *to,uint length __attribute__((unused)))
3460
{
3461
  float nr;
3462
#ifdef WORDS_BIGENDIAN
3463
  if (table->s->db_low_byte_first)
3464
  {
3465
    float4get(nr,ptr);
3466
  }
3467
  else
3468
#endif
3469
    memcpy_fixed(&nr,ptr,sizeof(float));
3470
3471
  uchar *tmp= to;
3472
  if (nr == (float) 0.0)
3473
  {						/* Change to zero string */
3474
    tmp[0]=(uchar) 128;
3475
    bzero((char*) tmp+1,sizeof(nr)-1);
3476
  }
3477
  else
3478
  {
3479
#ifdef WORDS_BIGENDIAN
3480
    memcpy_fixed(tmp,&nr,sizeof(nr));
3481
#else
3482
    tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
3483
#endif
3484
    if (tmp[0] & 128)				/* Negative */
3485
    {						/* make complement */
3486
      uint i;
3487
      for (i=0 ; i < sizeof(nr); i++)
3488
	tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
3489
    }
3490
    else
3491
    {
3492
      ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
3493
		       (ushort) 32768);
3494
      exp_part+= (ushort) 1 << (16-1-FLT_EXP_DIG);
3495
      tmp[0]= (uchar) (exp_part >> 8);
3496
      tmp[1]= (uchar) exp_part;
3497
    }
3498
  }
3499
}
3500
3501
3502
bool Field_float::send_binary(Protocol *protocol)
3503
{
3504
  ASSERT_COLUMN_MARKED_FOR_READ;
3505
  return protocol->store((float) Field_float::val_real(), dec, (String*) 0);
3506
}
3507
3508
3509
/**
3510
   Save the field metadata for float fields.
3511
3512
   Saves the pack length in the first byte.
3513
3514
   @param   metadata_ptr   First byte of field metadata
3515
3516
   @returns number of bytes written to metadata_ptr
3517
*/
3518
int Field_float::do_save_field_metadata(uchar *metadata_ptr)
3519
{
3520
  *metadata_ptr= pack_length();
3521
  return 1;
3522
}
3523
3524
3525
void Field_float::sql_type(String &res) const
3526
{
3527
  if (dec == NOT_FIXED_DEC)
3528
  {
3529
    res.set_ascii(STRING_WITH_LEN("float"));
3530
  }
3531
  else
3532
  {
3533
    CHARSET_INFO *cs= res.charset();
3534
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3535
			    "float(%d,%d)",(int) field_length,dec));
3536
  }
3537
  add_zerofill_and_unsigned(res);
3538
}
3539
3540
3541
/****************************************************************************
3542
  double precision floating point numbers
3543
****************************************************************************/
3544
3545
int Field_double::store(const char *from,uint len,CHARSET_INFO *cs)
3546
{
3547
  int error;
3548
  char *end;
3549
  double nr= my_strntod(cs,(char*) from, len, &end, &error);
3550
  if (error || (!len || (((uint) (end-from) != len) && table->in_use->count_cuted_fields)))
3551
  {
3552
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
3553
                (error ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED), 1);
3554
    error= error ? 1 : 2;
3555
  }
3556
  Field_double::store(nr);
3557
  return error;
3558
}
3559
3560
3561
int Field_double::store(double nr)
3562
{
3563
  ASSERT_COLUMN_MARKED_FOR_WRITE;
3564
  int error= truncate(&nr, DBL_MAX);
3565
3566
#ifdef WORDS_BIGENDIAN
3567
  if (table->s->db_low_byte_first)
3568
  {
3569
    float8store(ptr,nr);
3570
  }
3571
  else
3572
#endif
3573
    doublestore(ptr,nr);
3574
  return error;
3575
}
3576
3577
3578
int Field_double::store(longlong nr, bool unsigned_val)
3579
{
3580
  return Field_double::store(unsigned_val ? ulonglong2double((uint64_t) nr) :
3581
                             (double) nr);
3582
}
3583
3584
/*
3585
  If a field has fixed length, truncate the double argument pointed to by 'nr'
3586
  appropriately.
3587
  Also ensure that the argument is within [-max_value; max_value] range.
3588
*/
3589
3590
int Field_real::truncate(double *nr, double max_value)
3591
{
3592
  int error= 1;
3593
  double res= *nr;
3594
  
3595
  if (isnan(res))
3596
  {
3597
    res= 0;
3598
    set_null();
3599
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3600
    goto end;
3601
  }
3602
  else if (unsigned_flag && res < 0)
3603
  {
3604
    res= 0;
3605
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3606
    goto end;
3607
  }
3608
3609
  if (!not_fixed)
3610
  {
3611
    uint order= field_length - dec;
3612
    uint step= array_elements(log_10) - 1;
3613
    max_value= 1.0;
3614
    for (; order > step; order-= step)
3615
      max_value*= log_10[step];
3616
    max_value*= log_10[order];
3617
    max_value-= 1.0 / log_10[dec];
3618
3619
    /* Check for infinity so we don't get NaN in calculations */
3620
    if (!my_isinf(res))
3621
    {
3622
      double tmp= rint((res - floor(res)) * log_10[dec]) / log_10[dec];
3623
      res= floor(res) + tmp;
3624
    }
3625
  }
3626
  
3627
  if (res < -max_value)
3628
  {
3629
   res= -max_value;
3630
   set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3631
  }
3632
  else if (res > max_value)
3633
  {
3634
    res= max_value;
3635
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3636
  }
3637
  else
3638
    error= 0;
3639
3640
end:
3641
  *nr= res;
3642
  return error;
3643
}
3644
3645
3646
int Field_real::store_decimal(const my_decimal *dm)
3647
{
3648
  double dbl;
3649
  my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
3650
  return store(dbl);
3651
}
3652
3653
double Field_double::val_real(void)
3654
{
3655
  ASSERT_COLUMN_MARKED_FOR_READ;
3656
  double j;
3657
#ifdef WORDS_BIGENDIAN
3658
  if (table->s->db_low_byte_first)
3659
  {
3660
    float8get(j,ptr);
3661
  }
3662
  else
3663
#endif
3664
    doubleget(j,ptr);
3665
  return j;
3666
}
3667
3668
longlong Field_double::val_int(void)
3669
{
3670
  ASSERT_COLUMN_MARKED_FOR_READ;
3671
  double j;
3672
  longlong res;
3673
#ifdef WORDS_BIGENDIAN
3674
  if (table->s->db_low_byte_first)
3675
  {
3676
    float8get(j,ptr);
3677
  }
3678
  else
3679
#endif
3680
    doubleget(j,ptr);
3681
  /* Check whether we fit into longlong range */
3682
  if (j <= (double) LONGLONG_MIN)
3683
  {
3684
    res= (longlong) LONGLONG_MIN;
3685
    goto warn;
3686
  }
3687
  if (j >= (double) (uint64_t) LONGLONG_MAX)
3688
  {
3689
    res= (longlong) LONGLONG_MAX;
3690
    goto warn;
3691
  }
3692
  return (longlong) rint(j);
3693
3694
warn:
3695
  {
3696
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
3697
    String tmp(buf, sizeof(buf), &my_charset_latin1), *str;
3698
    str= val_str(&tmp, 0);
3699
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
3700
                        ER_TRUNCATED_WRONG_VALUE,
3701
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
3702
                        str->c_ptr());
3703
  }
3704
  return res;
3705
}
3706
3707
3708
my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
3709
{
3710
  ASSERT_COLUMN_MARKED_FOR_READ;
3711
  double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
3712
  return decimal_value;
3713
}
3714
3715
3716
String *Field_double::val_str(String *val_buffer,
3717
			      String *val_ptr __attribute__((unused)))
3718
{
3719
  ASSERT_COLUMN_MARKED_FOR_READ;
3720
  double nr;
3721
#ifdef WORDS_BIGENDIAN
3722
  if (table->s->db_low_byte_first)
3723
  {
3724
    float8get(nr,ptr);
3725
  }
3726
  else
3727
#endif
3728
    doubleget(nr,ptr);
3729
3730
  uint to_length=max(field_length, DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
3731
  val_buffer->alloc(to_length);
3732
  char *to=(char*) val_buffer->ptr();
3733
  size_t len;
3734
3735
  if (dec >= NOT_FIXED_DEC)
3736
    len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
3737
  else
3738
    len= my_fcvt(nr, dec, to, NULL);
3739
3740
  val_buffer->length((uint) len);
3741
  if (zerofill)
3742
    prepend_zeros(val_buffer);
3743
  return val_buffer;
3744
}
3745
3746
bool Field_double::send_binary(Protocol *protocol)
3747
{
3748
  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
3749
}
3750
3751
3752
int Field_double::cmp(const uchar *a_ptr, const uchar *b_ptr)
3753
{
3754
  ASSERT_COLUMN_MARKED_FOR_READ;
3755
  double a,b;
3756
#ifdef WORDS_BIGENDIAN
3757
  if (table->s->db_low_byte_first)
3758
  {
3759
    float8get(a,a_ptr);
3760
    float8get(b,b_ptr);
3761
  }
3762
  else
3763
#endif
3764
  {
3765
    doubleget(a, a_ptr);
3766
    doubleget(b, b_ptr);
3767
  }
3768
  return (a < b) ? -1 : (a > b) ? 1 : 0;
3769
}
3770
3771
3772
#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
3773
3774
/* The following should work for IEEE */
3775
3776
void Field_double::sort_string(uchar *to,uint length __attribute__((unused)))
3777
{
3778
  double nr;
3779
#ifdef WORDS_BIGENDIAN
3780
  if (table->s->db_low_byte_first)
3781
  {
3782
    float8get(nr,ptr);
3783
  }
3784
  else
3785
#endif
3786
    doubleget(nr,ptr);
3787
  change_double_for_sort(nr, to);
3788
}
3789
3790
3791
/**
3792
   Save the field metadata for double fields.
3793
3794
   Saves the pack length in the first byte of the field metadata array
3795
   at index of *metadata_ptr.
3796
3797
   @param   metadata_ptr   First byte of field metadata
3798
3799
   @returns number of bytes written to metadata_ptr
3800
*/
3801
int Field_double::do_save_field_metadata(uchar *metadata_ptr)
3802
{
3803
  *metadata_ptr= pack_length();
3804
  return 1;
3805
}
3806
3807
3808
void Field_double::sql_type(String &res) const
3809
{
3810
  CHARSET_INFO *cs=res.charset();
3811
  if (dec == NOT_FIXED_DEC)
3812
  {
3813
    res.set_ascii(STRING_WITH_LEN("double"));
3814
  }
3815
  else
3816
  {
3817
    res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3818
			    "double(%d,%d)",(int) field_length,dec));
3819
  }
3820
  add_zerofill_and_unsigned(res);
3821
}
3822
3823
3824
/**
3825
  TIMESTAMP type holds datetime values in range from 1970-01-01 00:00:01 UTC to 
3826
  2038-01-01 00:00:00 UTC stored as number of seconds since Unix 
3827
  Epoch in UTC.
3828
  
3829
  Up to one of timestamps columns in the table can be automatically 
3830
  set on row update and/or have NOW() as default value.
3831
  TABLE::timestamp_field points to Field object for such timestamp with 
3832
  auto-set-on-update. TABLE::time_stamp holds offset in record + 1 for this
3833
  field, and is used by handler code which performs updates required.
3834
  
3835
  Actually SQL-99 says that we should allow niladic functions (like NOW())
3836
  as defaults for any field. Current limitations (only NOW() and only 
3837
  for one TIMESTAMP field) are because of restricted binary .frm format 
3838
  and should go away in the future.
3839
  
3840
  Also because of this limitation of binary .frm format we use 5 different
3841
  unireg_check values with TIMESTAMP field to distinguish various cases of
3842
  DEFAULT or ON UPDATE values. These values are:
3843
  
3844
  TIMESTAMP_OLD_FIELD - old timestamp, if there was not any fields with
3845
    auto-set-on-update (or now() as default) in this table before, then this 
3846
    field has NOW() as default and is updated when row changes, else it is 
3847
    field which has 0 as default value and is not automatically updated.
3848
  TIMESTAMP_DN_FIELD - field with NOW() as default but not set on update
3849
    automatically (TIMESTAMP DEFAULT NOW())
3850
  TIMESTAMP_UN_FIELD - field which is set on update automatically but has not 
3851
    NOW() as default (but it may has 0 or some other const timestamp as 
3852
    default) (TIMESTAMP ON UPDATE NOW()).
3853
  TIMESTAMP_DNUN_FIELD - field which has now() as default and is auto-set on 
3854
    update. (TIMESTAMP DEFAULT NOW() ON UPDATE NOW())
3855
  NONE - field which is not auto-set on update with some other than NOW() 
3856
    default value (TIMESTAMP DEFAULT 0).
3857
3858
  Note that TIMESTAMP_OLD_FIELDs are never created explicitly now, they are 
3859
  left only for preserving ability to read old tables. Such fields replaced 
3860
  with their newer analogs in CREATE TABLE and in SHOW CREATE TABLE. This is 
3861
  because we want to prefer NONE unireg_check before TIMESTAMP_OLD_FIELD for 
3862
  "TIMESTAMP DEFAULT 'Const'" field. (Old timestamps allowed such 
3863
  specification too but ignored default value for first timestamp, which of 
3864
  course is non-standard.) In most cases user won't notice any change, only
3865
  exception is different behavior of old/new timestamps during ALTER TABLE.
3866
 */
3867
3868
Field_timestamp::Field_timestamp(uchar *ptr_arg, uint32 len_arg,
3869
                                 uchar *null_ptr_arg, uchar null_bit_arg,
3870
				 enum utype unireg_check_arg,
3871
				 const char *field_name_arg,
3872
				 TABLE_SHARE *share,
3873
				 CHARSET_INFO *cs)
3874
  :Field_str(ptr_arg, MAX_DATETIME_WIDTH, null_ptr_arg, null_bit_arg,
3875
	     unireg_check_arg, field_name_arg, cs)
3876
{
3877
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
3878
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
3879
  if (!share->timestamp_field && unireg_check != NONE)
3880
  {
3881
    /* This timestamp has auto-update */
3882
    share->timestamp_field= this;
3883
    flags|= TIMESTAMP_FLAG;
3884
    if (unireg_check != TIMESTAMP_DN_FIELD)
3885
      flags|= ON_UPDATE_NOW_FLAG;
3886
  }
3887
}
3888
3889
3890
Field_timestamp::Field_timestamp(bool maybe_null_arg,
3891
                                 const char *field_name_arg,
3892
                                 CHARSET_INFO *cs)
3893
  :Field_str((uchar*) 0, MAX_DATETIME_WIDTH,
3894
             maybe_null_arg ? (uchar*) "": 0, 0,
3895
	     NONE, field_name_arg, cs)
3896
{
3897
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
3898
  flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
3899
    if (unireg_check != TIMESTAMP_DN_FIELD)
3900
      flags|= ON_UPDATE_NOW_FLAG;
3901
}
3902
3903
3904
/**
3905
  Get auto-set type for TIMESTAMP field.
3906
3907
  Returns value indicating during which operations this TIMESTAMP field
3908
  should be auto-set to current timestamp.
3909
*/
3910
timestamp_auto_set_type Field_timestamp::get_auto_set_type() const
3911
{
3912
  switch (unireg_check)
3913
  {
3914
  case TIMESTAMP_DN_FIELD:
3915
    return TIMESTAMP_AUTO_SET_ON_INSERT;
3916
  case TIMESTAMP_UN_FIELD:
3917
    return TIMESTAMP_AUTO_SET_ON_UPDATE;
3918
  case TIMESTAMP_OLD_FIELD:
3919
    /*
3920
      Although we can have several such columns in legacy tables this
3921
      function should be called only for first of them (i.e. the one
3922
      having auto-set property).
3923
    */
3924
    DBUG_ASSERT(table->timestamp_field == this);
3925
    /* Fall-through */
3926
  case TIMESTAMP_DNUN_FIELD:
3927
    return TIMESTAMP_AUTO_SET_ON_BOTH;
3928
  default:
3929
    /*
3930
      Normally this function should not be called for TIMESTAMPs without
3931
      auto-set property.
3932
    */
3933
    DBUG_ASSERT(0);
3934
    return TIMESTAMP_NO_AUTO_SET;
3935
  }
3936
}
3937
3938
3939
int Field_timestamp::store(const char *from,uint len,CHARSET_INFO *cs)
3940
{
3941
  ASSERT_COLUMN_MARKED_FOR_WRITE;
3942
  MYSQL_TIME l_time;
3943
  my_time_t tmp= 0;
3944
  int error;
3945
  bool have_smth_to_conv;
3946
  my_bool in_dst_time_gap;
3947
  THD *thd= table ? table->in_use : current_thd;
3948
3949
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
3950
  have_smth_to_conv= (str_to_datetime(from, len, &l_time, 1, &error) >
3951
                      MYSQL_TIMESTAMP_ERROR);
3952
3953
  if (error || !have_smth_to_conv)
3954
  {
3955
    error= 1;
3956
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
3957
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
3958
  }
3959
3960
  /* Only convert a correct date (not a zero date) */
3961
  if (have_smth_to_conv && l_time.month)
3962
  {
3963
    if (!(tmp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
3964
    {
3965
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
3966
                           ER_WARN_DATA_OUT_OF_RANGE,
3967
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
3968
      error= 1;
3969
    }
3970
    else if (in_dst_time_gap)
3971
    {
3972
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
3973
                           ER_WARN_INVALID_TIMESTAMP,
3974
                           from, len, MYSQL_TIMESTAMP_DATETIME, !error);
3975
      error= 1;
3976
    }
3977
  }
3978
  store_timestamp(tmp);
3979
  return error;
3980
}
3981
3982
3983
int Field_timestamp::store(double nr)
3984
{
3985
  int error= 0;
3986
  if (nr < 0 || nr > 99991231235959.0)
3987
  {
3988
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
3989
                         ER_WARN_DATA_OUT_OF_RANGE,
3990
                         nr, MYSQL_TIMESTAMP_DATETIME);
3991
    nr= 0;					// Avoid overflow on buff
3992
    error= 1;
3993
  }
3994
  error|= Field_timestamp::store((longlong) rint(nr), FALSE);
3995
  return error;
3996
}
3997
3998
3999
int Field_timestamp::store(longlong nr, bool unsigned_val)
4000
{
4001
  ASSERT_COLUMN_MARKED_FOR_WRITE;
4002
  MYSQL_TIME l_time;
4003
  my_time_t timestamp= 0;
4004
  int error;
4005
  my_bool in_dst_time_gap;
4006
  THD *thd= table ? table->in_use : current_thd;
4007
4008
  /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
4009
  longlong tmp= number_to_datetime(nr, &l_time, (thd->variables.sql_mode &
4010
                                                 MODE_NO_ZERO_DATE) |
4011
                                   MODE_NO_ZERO_IN_DATE, &error);
80.1.1 by Brian Aker
LL() cleanup
4012
  if (tmp == -1LL)
1 by brian
clean slate
4013
  {
4014
    error= 2;
4015
  }
4016
4017
  if (!error && tmp)
4018
  {
4019
    if (!(timestamp= TIME_to_timestamp(thd, &l_time, &in_dst_time_gap)))
4020
    {
4021
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4022
                           ER_WARN_DATA_OUT_OF_RANGE,
4023
                           nr, MYSQL_TIMESTAMP_DATETIME, 1);
4024
      error= 1;
4025
    }
4026
    if (in_dst_time_gap)
4027
    {
4028
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4029
                           ER_WARN_INVALID_TIMESTAMP,
4030
                           nr, MYSQL_TIMESTAMP_DATETIME, 1);
4031
      error= 1;
4032
    }
4033
  } else if (error)
4034
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4035
                         WARN_DATA_TRUNCATED,
4036
                         nr, MYSQL_TIMESTAMP_DATETIME, 1);
4037
4038
  store_timestamp(timestamp);
4039
  return error;
4040
}
4041
4042
double Field_timestamp::val_real(void)
4043
{
4044
  ASSERT_COLUMN_MARKED_FOR_READ;
4045
  return (double) Field_timestamp::val_int();
4046
}
4047
4048
longlong Field_timestamp::val_int(void)
4049
{
4050
  ASSERT_COLUMN_MARKED_FOR_READ;
4051
  uint32 temp;
4052
  MYSQL_TIME time_tmp;
4053
  THD  *thd= table ? table->in_use : current_thd;
4054
4055
  thd->time_zone_used= 1;
4056
#ifdef WORDS_BIGENDIAN
4057
  if (table && table->s->db_low_byte_first)
4058
    temp=uint4korr(ptr);
4059
  else
4060
#endif
4061
    longget(temp,ptr);
4062
4063
  if (temp == 0L)				// No time
4064
    return(0);					/* purecov: inspected */
4065
  
4066
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp, (my_time_t)temp);
4067
  
80.1.1 by Brian Aker
LL() cleanup
4068
  return time_tmp.year * 10000000000LL + time_tmp.month * 100000000LL +
1 by brian
clean slate
4069
         time_tmp.day * 1000000L + time_tmp.hour * 10000L +
4070
         time_tmp.minute * 100 + time_tmp.second;
4071
}
4072
4073
4074
String *Field_timestamp::val_str(String *val_buffer, String *val_ptr)
4075
{
4076
  ASSERT_COLUMN_MARKED_FOR_READ;
4077
  uint32 temp, temp2;
4078
  MYSQL_TIME time_tmp;
4079
  THD *thd= table ? table->in_use : current_thd;
4080
  char *to;
4081
4082
  val_buffer->alloc(field_length+1);
4083
  to= (char*) val_buffer->ptr();
4084
  val_buffer->length(field_length);
4085
4086
  thd->time_zone_used= 1;
4087
#ifdef WORDS_BIGENDIAN
4088
  if (table && table->s->db_low_byte_first)
4089
    temp=uint4korr(ptr);
4090
  else
4091
#endif
4092
    longget(temp,ptr);
4093
4094
  if (temp == 0L)
4095
  {				      /* Zero time is "000000" */
4096
    val_ptr->set(STRING_WITH_LEN("0000-00-00 00:00:00"), &my_charset_bin);
4097
    return val_ptr;
4098
  }
4099
  val_buffer->set_charset(&my_charset_bin);	// Safety
4100
  
4101
  thd->variables.time_zone->gmt_sec_to_TIME(&time_tmp,(my_time_t)temp);
4102
4103
  temp= time_tmp.year % 100;
4104
  if (temp < YY_PART_YEAR - 1)
4105
  {
4106
    *to++= '2';
4107
    *to++= '0';
4108
  }
4109
  else
4110
  {
4111
    *to++= '1';
4112
    *to++= '9';
4113
  }
4114
  temp2=temp/10; temp=temp-temp2*10;
4115
  *to++= (char) ('0'+(char) (temp2));
4116
  *to++= (char) ('0'+(char) (temp));
4117
  *to++= '-';
4118
  temp=time_tmp.month;
4119
  temp2=temp/10; temp=temp-temp2*10;
4120
  *to++= (char) ('0'+(char) (temp2));
4121
  *to++= (char) ('0'+(char) (temp));
4122
  *to++= '-';
4123
  temp=time_tmp.day;
4124
  temp2=temp/10; temp=temp-temp2*10;
4125
  *to++= (char) ('0'+(char) (temp2));
4126
  *to++= (char) ('0'+(char) (temp));
4127
  *to++= ' ';
4128
  temp=time_tmp.hour;
4129
  temp2=temp/10; temp=temp-temp2*10;
4130
  *to++= (char) ('0'+(char) (temp2));
4131
  *to++= (char) ('0'+(char) (temp));
4132
  *to++= ':';
4133
  temp=time_tmp.minute;
4134
  temp2=temp/10; temp=temp-temp2*10;
4135
  *to++= (char) ('0'+(char) (temp2));
4136
  *to++= (char) ('0'+(char) (temp));
4137
  *to++= ':';
4138
  temp=time_tmp.second;
4139
  temp2=temp/10; temp=temp-temp2*10;
4140
  *to++= (char) ('0'+(char) (temp2));
4141
  *to++= (char) ('0'+(char) (temp));
4142
  *to= 0;
4143
  return val_buffer;
4144
}
4145
4146
4147
bool Field_timestamp::get_date(MYSQL_TIME *ltime, uint fuzzydate)
4148
{
4149
  long temp;
4150
  THD *thd= table ? table->in_use : current_thd;
4151
  thd->time_zone_used= 1;
4152
#ifdef WORDS_BIGENDIAN
4153
  if (table && table->s->db_low_byte_first)
4154
    temp=uint4korr(ptr);
4155
  else
4156
#endif
4157
    longget(temp,ptr);
4158
  if (temp == 0L)
4159
  {				      /* Zero time is "000000" */
4160
    if (fuzzydate & TIME_NO_ZERO_DATE)
4161
      return 1;
4162
    bzero((char*) ltime,sizeof(*ltime));
4163
  }
4164
  else
4165
  {
4166
    thd->variables.time_zone->gmt_sec_to_TIME(ltime, (my_time_t)temp);
4167
  }
4168
  return 0;
4169
}
4170
4171
bool Field_timestamp::get_time(MYSQL_TIME *ltime)
4172
{
4173
  return Field_timestamp::get_date(ltime,0);
4174
}
4175
4176
4177
bool Field_timestamp::send_binary(Protocol *protocol)
4178
{
4179
  MYSQL_TIME tm;
4180
  Field_timestamp::get_date(&tm, 0);
4181
  return protocol->store(&tm);
4182
}
4183
4184
4185
int Field_timestamp::cmp(const uchar *a_ptr, const uchar *b_ptr)
4186
{
4187
  int32 a,b;
4188
#ifdef WORDS_BIGENDIAN
4189
  if (table && table->s->db_low_byte_first)
4190
  {
4191
    a=sint4korr(a_ptr);
4192
    b=sint4korr(b_ptr);
4193
  }
4194
  else
4195
#endif
4196
  {
4197
  longget(a,a_ptr);
4198
  longget(b,b_ptr);
4199
  }
4200
  return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
4201
}
4202
4203
4204
void Field_timestamp::sort_string(uchar *to,uint length __attribute__((unused)))
4205
{
4206
#ifdef WORDS_BIGENDIAN
4207
  if (!table || !table->s->db_low_byte_first)
4208
  {
4209
    to[0] = ptr[0];
4210
    to[1] = ptr[1];
4211
    to[2] = ptr[2];
4212
    to[3] = ptr[3];
4213
  }
4214
  else
4215
#endif
4216
  {
4217
    to[0] = ptr[3];
4218
    to[1] = ptr[2];
4219
    to[2] = ptr[1];
4220
    to[3] = ptr[0];
4221
  }
4222
}
4223
4224
4225
void Field_timestamp::sql_type(String &res) const
4226
{
4227
  res.set_ascii(STRING_WITH_LEN("timestamp"));
4228
}
4229
4230
4231
void Field_timestamp::set_time()
4232
{
4233
  THD *thd= table ? table->in_use : current_thd;
4234
  long tmp= (long) thd->query_start();
4235
  set_notnull();
4236
  store_timestamp(tmp);
4237
}
4238
4239
/****************************************************************************
4240
** time type
4241
** In string context: HH:MM:SS
4242
** In number context: HHMMSS
4243
** Stored as a 3 byte unsigned int
4244
****************************************************************************/
4245
4246
int Field_time::store(const char *from,uint len,CHARSET_INFO *cs)
4247
{
4248
  MYSQL_TIME ltime;
4249
  long tmp;
4250
  int error= 0;
4251
  int warning;
4252
4253
  if (str_to_time(from, len, &ltime, &warning))
4254
  {
4255
    tmp=0L;
4256
    error= 2;
4257
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
4258
                         from, len, MYSQL_TIMESTAMP_TIME, 1);
4259
  }
4260
  else
4261
  {
4262
    if (warning & MYSQL_TIME_WARN_TRUNCATED)
4263
    {
4264
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4265
                           WARN_DATA_TRUNCATED,
4266
                           from, len, MYSQL_TIMESTAMP_TIME, 1);
4267
      error= 1;
4268
    }
4269
    if (warning & MYSQL_TIME_WARN_OUT_OF_RANGE)
4270
    {
4271
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4272
                           ER_WARN_DATA_OUT_OF_RANGE,
4273
                           from, len, MYSQL_TIMESTAMP_TIME, !error);
4274
      error= 1;
4275
    }
4276
    if (ltime.month)
4277
      ltime.day=0;
4278
    tmp=(ltime.day*24L+ltime.hour)*10000L+(ltime.minute*100+ltime.second);
4279
  }
4280
  
4281
  if (ltime.neg)
4282
    tmp= -tmp;
4283
  int3store(ptr,tmp);
4284
  return error;
4285
}
4286
4287
4288
int Field_time::store_time(MYSQL_TIME *ltime, timestamp_type time_type)
4289
{
4290
  long tmp= ((ltime->month ? 0 : ltime->day * 24L) + ltime->hour) * 10000L +
4291
            (ltime->minute * 100 + ltime->second);
4292
  if (ltime->neg)
4293
    tmp= -tmp;
4294
  return Field_time::store((longlong) tmp, FALSE);
4295
}
4296
4297
4298
int Field_time::store(double nr)
4299
{
4300
  ASSERT_COLUMN_MARKED_FOR_WRITE;
4301
  long tmp;
4302
  int error= 0;
4303
  if (nr > (double)TIME_MAX_VALUE)
4304
  {
4305
    tmp= TIME_MAX_VALUE;
4306
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4307
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
4308
    error= 1;
4309
  }
4310
  else if (nr < (double)-TIME_MAX_VALUE)
4311
  {
4312
    tmp= -TIME_MAX_VALUE;
4313
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4314
                         ER_WARN_DATA_OUT_OF_RANGE, nr, MYSQL_TIMESTAMP_TIME);
4315
    error= 1;
4316
  }
4317
  else
4318
  {
4319
    tmp=(long) floor(fabs(nr));			// Remove fractions
4320
    if (nr < 0)
4321
      tmp= -tmp;
4322
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
4323
    {
4324
      tmp=0;
4325
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4326
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
4327
                           MYSQL_TIMESTAMP_TIME);
4328
      error= 1;
4329
    }
4330
  }
4331
  int3store(ptr,tmp);
4332
  return error;
4333
}
4334
4335
4336
int Field_time::store(longlong nr, bool unsigned_val)
4337
{
4338
  ASSERT_COLUMN_MARKED_FOR_WRITE;
4339
  long tmp;
4340
  int error= 0;
4341
  if (nr < (longlong) -TIME_MAX_VALUE && !unsigned_val)
4342
  {
4343
    tmp= -TIME_MAX_VALUE;
4344
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4345
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
4346
                         MYSQL_TIMESTAMP_TIME, 1);
4347
    error= 1;
4348
  }
4349
  else if (nr > (longlong) TIME_MAX_VALUE || (nr < 0 && unsigned_val))
4350
  {
4351
    tmp= TIME_MAX_VALUE;
4352
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4353
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
4354
                         MYSQL_TIMESTAMP_TIME, 1);
4355
    error= 1;
4356
  }
4357
  else
4358
  {
4359
    tmp=(long) nr;
4360
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
4361
    {
4362
      tmp=0;
4363
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
4364
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
4365
                           MYSQL_TIMESTAMP_TIME, 1);
4366
      error= 1;
4367
    }
4368
  }
4369
  int3store(ptr,tmp);
4370
  return error;
4371
}
4372
4373
4374
double Field_time::val_real(void)
4375
{
4376
  ASSERT_COLUMN_MARKED_FOR_READ;
4377
  uint32 j= (uint32) uint3korr(ptr);
4378
  return (double) j;
4379
}
4380
4381
longlong Field_time::val_int(void)
4382
{
4383
  ASSERT_COLUMN_MARKED_FOR_READ;
4384
  return (longlong) sint3korr(ptr);
4385
}
4386
4387
4388
/**
4389
  @note
4390
  This function is multi-byte safe as the result string is always of type
4391
  my_charset_bin
4392
*/
4393
4394
String *Field_time::val_str(String *val_buffer,
4395
			    String *val_ptr __attribute__((unused)))
4396
{
4397
  ASSERT_COLUMN_MARKED_FOR_READ;
4398
  MYSQL_TIME ltime;
4399
  val_buffer->alloc(MAX_DATE_STRING_REP_LENGTH);
4400
  long tmp=(long) sint3korr(ptr);
4401
  ltime.neg= 0;
4402
  if (tmp < 0)
4403
  {
4404
    tmp= -tmp;
4405
    ltime.neg= 1;
4406
  }
4407
  ltime.day= (uint) 0;
4408
  ltime.hour= (uint) (tmp/10000);
4409
  ltime.minute= (uint) (tmp/100 % 100);
4410
  ltime.second= (uint) (tmp % 100);
4411
  make_time((DATE_TIME_FORMAT*) 0, &ltime, val_buffer);
4412
  return val_buffer;
4413
}
4414
4415
4416
/**
4417
  @note
4418
  Normally we would not consider 'time' as a valid date, but we allow
4419
  get_date() here to be able to do things like
4420
  DATE_FORMAT(time, "%l.%i %p")
4421
*/
4422
 
4423
bool Field_time::get_date(MYSQL_TIME *ltime, uint fuzzydate)
4424
{
4425
  long tmp;
4426
  THD *thd= table ? table->in_use : current_thd;
4427
  if (!(fuzzydate & TIME_FUZZY_DATE))
4428
  {
4429
    push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
4430
                        ER_WARN_DATA_OUT_OF_RANGE,
4431
                        ER(ER_WARN_DATA_OUT_OF_RANGE), field_name,
4432
                        thd->row_count);
4433
    return 1;
4434
  }
4435
  tmp=(long) sint3korr(ptr);
4436
  ltime->neg=0;
4437
  if (tmp < 0)
4438
  {
4439
    ltime->neg= 1;
4440
    tmp=-tmp;
4441
  }
4442
  ltime->hour=tmp/10000;
4443
  tmp-=ltime->hour*10000;
4444
  ltime->minute=   tmp/100;
4445
  ltime->second= tmp % 100;
4446
  ltime->year= ltime->month= ltime->day= ltime->second_part= 0;
4447
  return 0;
4448
}
4449
4450
4451
bool Field_time::get_time(MYSQL_TIME *ltime)
4452
{
4453
  long tmp=(long) sint3korr(ptr);
4454
  ltime->neg=0;
4455
  if (tmp < 0)
4456
  {
4457
    ltime->neg= 1;
4458
    tmp=-tmp;
4459
  }
4460
  ltime->day= 0;
4461
  ltime->hour=   (int) (tmp/10000);
4462
  tmp-=ltime->hour*10000;
4463
  ltime->minute= (int) tmp/100;
4464
  ltime->second= (int) tmp % 100;
4465
  ltime->second_part=0;
4466
  ltime->time_type= MYSQL_TIMESTAMP_TIME;
4467
  return 0;
4468
}
4469
4470
4471
bool Field_time::send_binary(Protocol *protocol)
4472
{
4473
  MYSQL_TIME tm;
4474
  Field_time::get_time(&tm);
4475
  tm.day= tm.hour/24;				// Move hours to days
4476
  tm.hour-= tm.day*24;
4477
  return protocol->store_time(&tm);
4478
}
4479
4480
4481
int Field_time::cmp(const uchar *a_ptr, const uchar *b_ptr)
4482
{
4483
  int32 a,b;
4484
  a=(int32) sint3korr(a_ptr);
4485
  b=(int32) sint3korr(b_ptr);
4486
  return (a < b) ? -1 : (a > b) ? 1 : 0;
4487
}
4488
4489
void Field_time::sort_string(uchar *to,uint length __attribute__((unused)))
4490
{
4491
  to[0] = (uchar) (ptr[2] ^ 128);
4492
  to[1] = ptr[1];
4493
  to[2] = ptr[0];
4494
}
4495
4496
void Field_time::sql_type(String &res) const
4497
{
4498
  res.set_ascii(STRING_WITH_LEN("time"));
4499
}
4500
4501
/****************************************************************************
4502
** year type
4503
** Save in a byte the year 0, 1901->2155
4504
** Can handle 2 byte or 4 byte years!
4505
****************************************************************************/
4506
4507
int Field_year::store(const char *from, uint len,CHARSET_INFO *cs)
4508
{
4509
  ASSERT_COLUMN_MARKED_FOR_WRITE;
4510
  char *end;
4511
  int error;
4512
  longlong nr= cs->cset->strntoull10rnd(cs, from, len, 0, &end, &error);
4513
4514
  if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155 || 
4515
      error == MY_ERRNO_ERANGE)
4516
  {
4517
    *ptr=0;
4518
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4519
    return 1;
4520
  }
4521
  if (table->in_use->count_cuted_fields && 
4522
      (error= check_int(cs, from, len, end, error)))
4523
  {
4524
    if (error == 1)  /* empty or incorrect string */
4525
    {
4526
      *ptr= 0;
4527
      return 1;
4528
    }
4529
    error= 1;
4530
  }
4531
4532
  if (nr != 0 || len != 4)
4533
  {
4534
    if (nr < YY_PART_YEAR)
4535
      nr+=100;					// 2000 - 2069
4536
    else if (nr > 1900)
4537
      nr-= 1900;
4538
  }
4539
  *ptr= (char) (uchar) nr;
4540
  return error;
4541
}
4542
4543
4544
int Field_year::store(double nr)
4545
{
4546
  if (nr < 0.0 || nr >= 2155.0)
4547
  {
4548
    (void) Field_year::store((longlong) -1, FALSE);
4549
    return 1;
4550
  }
4551
  return Field_year::store((longlong) nr, FALSE);
4552
}
4553
4554
4555
int Field_year::store(longlong nr, bool unsigned_val)
4556
{
4557
  ASSERT_COLUMN_MARKED_FOR_WRITE;
4558
  if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155)
4559
  {
4560
    *ptr= 0;
4561
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4562
    return 1;
4563
  }
4564
  if (nr != 0 || field_length != 4)		// 0000 -> 0; 00 -> 2000
4565
  {
4566
    if (nr < YY_PART_YEAR)
4567
      nr+=100;					// 2000 - 2069
4568
    else if (nr > 1900)
4569
      nr-= 1900;
4570
  }
4571
  *ptr= (char) (uchar) nr;
4572
  return 0;
4573
}
4574
4575
4576
bool Field_year::send_binary(Protocol *protocol)
4577
{
4578
  ASSERT_COLUMN_MARKED_FOR_READ;
4579
  uint64_t tmp= Field_year::val_int();
4580
  return protocol->store_short(tmp);
4581
}
4582
4583
4584
double Field_year::val_real(void)
4585
{
4586
  return (double) Field_year::val_int();
4587
}
4588
4589
4590
longlong Field_year::val_int(void)
4591
{
4592
  ASSERT_COLUMN_MARKED_FOR_READ;
4593
  int tmp= (int) ptr[0];
4594
  if (field_length != 4)
4595
    tmp%=100;					// Return last 2 char
4596
  else if (tmp)
4597
    tmp+=1900;
4598
  return (longlong) tmp;
4599
}
4600
4601
4602
String *Field_year::val_str(String *val_buffer,
4603
			    String *val_ptr __attribute__((unused)))
4604
{
4605
  val_buffer->alloc(5);
4606
  val_buffer->length(field_length);
4607
  char *to=(char*) val_buffer->ptr();
4608
  sprintf(to,field_length == 2 ? "%02d" : "%04d",(int) Field_year::val_int());
4609
  return val_buffer;
4610
}
4611
4612
4613
void Field_year::sql_type(String &res) const
4614
{
4615
  CHARSET_INFO *cs=res.charset();
4616
  res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(),
4617
			  "year(%d)",(int) field_length));
4618
}
4619
4620
4621
/****************************************************************************
4622
** date type
4623
** In string context: YYYY-MM-DD
4624
** In number context: YYYYMMDD
4625
** Stored as a 4 byte unsigned int
4626
****************************************************************************/
4627
4628
int Field_date::store(const char *from, uint len,CHARSET_INFO *cs)
4629
{
4630
  ASSERT_COLUMN_MARKED_FOR_WRITE;
4631
  MYSQL_TIME l_time;
4632
  uint32 tmp;
4633
  int error;
4634
  THD *thd= table ? table->in_use : current_thd;
4635
4636
  if (str_to_datetime(from, len, &l_time, TIME_FUZZY_DATE |
4637
                      (thd->variables.sql_mode &
4638
                       (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
4639
                        MODE_INVALID_DATES)),
4640
                      &error) <= MYSQL_TIMESTAMP_ERROR)
4641
  {
4642
    tmp= 0;
4643
    error= 2;
4644
  }
4645
  else
4646
    tmp=(uint32) l_time.year*10000L + (uint32) (l_time.month*100+l_time.day);
4647
4648
  if (error)
4649
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
4650
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
4651
4652
#ifdef WORDS_BIGENDIAN
4653
  if (table && table->s->db_low_byte_first)
4654
  {
4655
    int4store(ptr,tmp);
4656
  }
4657
  else
4658
#endif
4659
    longstore(ptr,tmp);
4660
  return error;
4661
}
4662
4663
4664
int Field_date::store(double nr)
4665
{
4666
  longlong tmp;
4667
  int error= 0;
4668
  if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
4669
    nr=floor(nr/1000000.0);			// Timestamp to date
4670
  if (nr < 0.0 || nr > 99991231.0)
4671
  {
80.1.1 by Brian Aker
LL() cleanup
4672
    tmp= 0LL;
1 by brian
clean slate
4673
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4674
                         ER_WARN_DATA_OUT_OF_RANGE,
4675
                         nr, MYSQL_TIMESTAMP_DATE);
4676
    error= 1;
4677
  }
4678
  else
4679
    tmp= (longlong) rint(nr);
4680
4681
  return Field_date::store(tmp, TRUE);
4682
}
4683
4684
4685
int Field_date::store(longlong nr, bool unsigned_val)
4686
{
4687
  ASSERT_COLUMN_MARKED_FOR_WRITE;
4688
  MYSQL_TIME not_used;
4689
  int error;
4690
  longlong initial_nr= nr;
4691
  THD *thd= table ? table->in_use : current_thd;
4692
4693
  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
4694
                                         (thd->variables.sql_mode &
4695
                                          (MODE_NO_ZERO_IN_DATE |
4696
                                           MODE_NO_ZERO_DATE |
4697
                                           MODE_INVALID_DATES))), &error);
4698
80.1.1 by Brian Aker
LL() cleanup
4699
  if (nr == -1LL)
1 by brian
clean slate
4700
  {
4701
    nr= 0;
4702
    error= 2;
4703
  }
4704
4705
  if (nr >= 19000000000000.0 && nr <= 99991231235959.0)
4706
    nr= (longlong) floor(nr/1000000.0);         // Timestamp to date
4707
4708
  if (error)
4709
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4710
                         error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
4711
                         WARN_DATA_TRUNCATED, initial_nr,
4712
                         MYSQL_TIMESTAMP_DATETIME, 1);
4713
4714
#ifdef WORDS_BIGENDIAN
4715
  if (table && table->s->db_low_byte_first)
4716
  {
4717
    int4store(ptr, nr);
4718
  }
4719
  else
4720
#endif
4721
    longstore(ptr, nr);
4722
  return error;
4723
}
4724
4725
4726
bool Field_date::send_binary(Protocol *protocol)
4727
{
4728
  longlong tmp= Field_date::val_int();
4729
  MYSQL_TIME tm;
4730
  tm.year= (uint32) tmp/10000L % 10000;
4731
  tm.month= (uint32) tmp/100 % 100;
4732
  tm.day= (uint32) tmp % 100;
4733
  return protocol->store_date(&tm);
4734
}
4735
4736
4737
double Field_date::val_real(void)
4738
{
4739
  ASSERT_COLUMN_MARKED_FOR_READ;
4740
  int32 j;
4741
#ifdef WORDS_BIGENDIAN
4742
  if (table && table->s->db_low_byte_first)
4743
    j=sint4korr(ptr);
4744
  else
4745
#endif
4746
    longget(j,ptr);
4747
  return (double) (uint32) j;
4748
}
4749
4750
4751
longlong Field_date::val_int(void)
4752
{
4753
  ASSERT_COLUMN_MARKED_FOR_READ;
4754
  int32 j;
4755
#ifdef WORDS_BIGENDIAN
4756
  if (table && table->s->db_low_byte_first)
4757
    j=sint4korr(ptr);
4758
  else
4759
#endif
4760
    longget(j,ptr);
4761
  return (longlong) (uint32) j;
4762
}
4763
4764
4765
String *Field_date::val_str(String *val_buffer,
4766
			    String *val_ptr __attribute__((unused)))
4767
{
4768
  ASSERT_COLUMN_MARKED_FOR_READ;
4769
  MYSQL_TIME ltime;
4770
  val_buffer->alloc(field_length);
4771
  int32 tmp;
4772
#ifdef WORDS_BIGENDIAN
4773
  if (table && table->s->db_low_byte_first)
4774
    tmp=sint4korr(ptr);
4775
  else
4776
#endif
4777
    longget(tmp,ptr);
4778
  ltime.neg= 0;
4779
  ltime.year= (int) ((uint32) tmp/10000L % 10000);
4780
  ltime.month= (int) ((uint32) tmp/100 % 100);
4781
  ltime.day= (int) ((uint32) tmp % 100);
4782
  make_date((DATE_TIME_FORMAT *) 0, &ltime, val_buffer);
4783
  return val_buffer;
4784
}
4785
4786
4787
bool Field_date::get_time(MYSQL_TIME *ltime)
4788
{
4789
  bzero((char *)ltime, sizeof(MYSQL_TIME));
4790
  return 0;
4791
}
4792
4793
4794
int Field_date::cmp(const uchar *a_ptr, const uchar *b_ptr)
4795
{
4796
  int32 a,b;
4797
#ifdef WORDS_BIGENDIAN
4798
  if (table && table->s->db_low_byte_first)
4799
  {
4800
    a=sint4korr(a_ptr);
4801
    b=sint4korr(b_ptr);
4802
  }
4803
  else
4804
#endif
4805
  {
4806
    longget(a,a_ptr);
4807
    longget(b,b_ptr);
4808
  }
4809
  return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
4810
}
4811
4812
4813
void Field_date::sort_string(uchar *to,uint length __attribute__((unused)))
4814
{
4815
#ifdef WORDS_BIGENDIAN
4816
  if (!table || !table->s->db_low_byte_first)
4817
  {
4818
    to[0] = ptr[0];
4819
    to[1] = ptr[1];
4820
    to[2] = ptr[2];
4821
    to[3] = ptr[3];
4822
  }
4823
  else
4824
#endif
4825
  {
4826
    to[0] = ptr[3];
4827
    to[1] = ptr[2];
4828
    to[2] = ptr[1];
4829
    to[3] = ptr[0];
4830
  }
4831
}
4832
4833
void Field_date::sql_type(String &res) const
4834
{
4835
  res.set_ascii(STRING_WITH_LEN("date"));
4836
}
4837
4838
4839
/****************************************************************************
4840
** The new date type
4841
** This is identical to the old date type, but stored on 3 bytes instead of 4
4842
** In number context: YYYYMMDD
4843
****************************************************************************/
4844
4845
/*
4846
  Store string into a date field
4847
4848
  SYNOPSIS
4849
    Field_newdate::store()
4850
    from                Date string
4851
    len                 Length of date field
4852
    cs                  Character set (not used)
4853
4854
  RETURN
4855
    0  ok
4856
    1  Value was cut during conversion
4857
    2  Wrong date string
4858
    3  Datetime value that was cut (warning level NOTE)
4859
       This is used by opt_range.cc:get_mm_leaf(). Note that there is a
4860
       nearly-identical class Field_date doesn't ever return 3 from its
4861
       store function.
4862
*/
4863
4864
int Field_newdate::store(const char *from,uint len,CHARSET_INFO *cs)
4865
{
4866
  ASSERT_COLUMN_MARKED_FOR_WRITE;
4867
  long tmp;
4868
  MYSQL_TIME l_time;
4869
  int error;
4870
  THD *thd= table ? table->in_use : current_thd;
4871
  enum enum_mysql_timestamp_type ret;
4872
  if ((ret= str_to_datetime(from, len, &l_time,
4873
                            (TIME_FUZZY_DATE |
4874
                             (thd->variables.sql_mode &
4875
                              (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
4876
                               MODE_INVALID_DATES))),
4877
                            &error)) <= MYSQL_TIMESTAMP_ERROR)
4878
  {
4879
    tmp= 0;
4880
    error= 2;
4881
  }
4882
  else
4883
  {
4884
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
4885
    if (!error && (ret != MYSQL_TIMESTAMP_DATE) &&
4886
        (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
4887
      error= 3;                                 // Datetime was cut (note)
4888
  }
4889
4890
  if (error)
4891
    set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
4892
                         MYSQL_ERROR::WARN_LEVEL_WARN,
4893
                         WARN_DATA_TRUNCATED,
4894
                         from, len, MYSQL_TIMESTAMP_DATE, 1);
4895
4896
  int3store(ptr, tmp);
4897
  return error;
4898
}
4899
4900
4901
int Field_newdate::store(double nr)
4902
{
4903
  if (nr < 0.0 || nr > 99991231235959.0)
4904
  {
4905
    int3store(ptr,(int32) 0);
4906
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
4907
                         WARN_DATA_TRUNCATED, nr, MYSQL_TIMESTAMP_DATE);
4908
    return 1;
4909
  }
4910
  return Field_newdate::store((longlong) rint(nr), FALSE);
4911
}
4912
4913
4914
int Field_newdate::store(longlong nr, bool unsigned_val)
4915
{
4916
  ASSERT_COLUMN_MARKED_FOR_WRITE;
4917
  MYSQL_TIME l_time;
4918
  longlong tmp;
4919
  int error;
4920
  THD *thd= table ? table->in_use : current_thd;
4921
  if (number_to_datetime(nr, &l_time,
4922
                         (TIME_FUZZY_DATE |
4923
                          (thd->variables.sql_mode &
4924
                           (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
4925
                            MODE_INVALID_DATES))),
80.1.1 by Brian Aker
LL() cleanup
4926
                         &error) == -1LL)
1 by brian
clean slate
4927
  {
4928
    tmp= 0L;
4929
    error= 2;
4930
  }
4931
  else
4932
    tmp= l_time.day + l_time.month*32 + l_time.year*16*32;
4933
4934
  if (!error && l_time.time_type != MYSQL_TIMESTAMP_DATE &&
4935
      (l_time.hour || l_time.minute || l_time.second || l_time.second_part))
4936
    error= 3;
4937
4938
  if (error)
4939
    set_datetime_warning(error == 3 ? MYSQL_ERROR::WARN_LEVEL_NOTE :
4940
                         MYSQL_ERROR::WARN_LEVEL_WARN,
4941
                         error == 2 ? 
4942
                         ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED,
4943
                         nr,MYSQL_TIMESTAMP_DATE, 1);
4944
4945
  int3store(ptr,tmp);
4946
  return error;
4947
}
4948
4949
4950
int Field_newdate::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
4951
{
4952
  ASSERT_COLUMN_MARKED_FOR_WRITE;
4953
  long tmp;
4954
  int error= 0;
4955
  if (time_type == MYSQL_TIMESTAMP_DATE ||
4956
      time_type == MYSQL_TIMESTAMP_DATETIME)
4957
  {
4958
    tmp=ltime->year*16*32+ltime->month*32+ltime->day;
4959
    if (check_date(ltime, tmp != 0,
4960
                   (TIME_FUZZY_DATE |
4961
                    (current_thd->variables.sql_mode &
4962
                     (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
4963
                      MODE_INVALID_DATES))), &error))
4964
    {
4965
      char buff[MAX_DATE_STRING_REP_LENGTH];
4966
      String str(buff, sizeof(buff), &my_charset_latin1);
4967
      make_date((DATE_TIME_FORMAT *) 0, ltime, &str);
4968
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
4969
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATE, 1);
4970
    }
4971
    if (!error && ltime->time_type != MYSQL_TIMESTAMP_DATE &&
4972
        (ltime->hour || ltime->minute || ltime->second || ltime->second_part))
4973
    {
4974
      char buff[MAX_DATE_STRING_REP_LENGTH];
4975
      String str(buff, sizeof(buff), &my_charset_latin1);
4976
      make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
4977
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_NOTE,
4978
                           WARN_DATA_TRUNCATED,
4979
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATE, 1);
4980
      error= 3;
4981
    }
4982
  }
4983
  else
4984
  {
4985
    tmp=0;
4986
    error= 1;
4987
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
4988
  }
4989
  int3store(ptr,tmp);
4990
  return error;
4991
}
4992
4993
4994
bool Field_newdate::send_binary(Protocol *protocol)
4995
{
4996
  MYSQL_TIME tm;
4997
  Field_newdate::get_date(&tm,0);
4998
  return protocol->store_date(&tm);
4999
}
5000
5001
5002
double Field_newdate::val_real(void)
5003
{
5004
  ASSERT_COLUMN_MARKED_FOR_READ;
5005
  return (double) Field_newdate::val_int();
5006
}
5007
5008
5009
longlong Field_newdate::val_int(void)
5010
{
5011
  ASSERT_COLUMN_MARKED_FOR_READ;
5012
  ulong j= uint3korr(ptr);
5013
  j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
5014
  return (longlong) j;
5015
}
5016
5017
5018
String *Field_newdate::val_str(String *val_buffer,
5019
			       String *val_ptr __attribute__((unused)))
5020
{
5021
  ASSERT_COLUMN_MARKED_FOR_READ;
5022
  val_buffer->alloc(field_length);
5023
  val_buffer->length(field_length);
5024
  uint32 tmp=(uint32) uint3korr(ptr);
5025
  int part;
5026
  char *pos=(char*) val_buffer->ptr()+10;
5027
5028
  /* Open coded to get more speed */
5029
  *pos--=0;					// End NULL
5030
  part=(int) (tmp & 31);
5031
  *pos--= (char) ('0'+part%10);
5032
  *pos--= (char) ('0'+part/10);
5033
  *pos--= '-';
5034
  part=(int) (tmp >> 5 & 15);
5035
  *pos--= (char) ('0'+part%10);
5036
  *pos--= (char) ('0'+part/10);
5037
  *pos--= '-';
5038
  part=(int) (tmp >> 9);
5039
  *pos--= (char) ('0'+part%10); part/=10;
5040
  *pos--= (char) ('0'+part%10); part/=10;
5041
  *pos--= (char) ('0'+part%10); part/=10;
5042
  *pos=   (char) ('0'+part);
5043
  return val_buffer;
5044
}
5045
5046
5047
bool Field_newdate::get_date(MYSQL_TIME *ltime,uint fuzzydate)
5048
{
5049
  uint32 tmp=(uint32) uint3korr(ptr);
5050
  ltime->day=   tmp & 31;
5051
  ltime->month= (tmp >> 5) & 15;
5052
  ltime->year=  (tmp >> 9);
5053
  ltime->time_type= MYSQL_TIMESTAMP_DATE;
5054
  ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
5055
  return ((!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ?
5056
          1 : 0);
5057
}
5058
5059
5060
bool Field_newdate::get_time(MYSQL_TIME *ltime)
5061
{
5062
  return Field_newdate::get_date(ltime,0);
5063
}
5064
5065
5066
int Field_newdate::cmp(const uchar *a_ptr, const uchar *b_ptr)
5067
{
5068
  uint32 a,b;
5069
  a=(uint32) uint3korr(a_ptr);
5070
  b=(uint32) uint3korr(b_ptr);
5071
  return (a < b) ? -1 : (a > b) ? 1 : 0;
5072
}
5073
5074
5075
void Field_newdate::sort_string(uchar *to,uint length __attribute__((unused)))
5076
{
5077
  to[0] = ptr[2];
5078
  to[1] = ptr[1];
5079
  to[2] = ptr[0];
5080
}
5081
5082
5083
void Field_newdate::sql_type(String &res) const
5084
{
5085
  res.set_ascii(STRING_WITH_LEN("date"));
5086
}
5087
5088
5089
/****************************************************************************
5090
** datetime type
5091
** In string context: YYYY-MM-DD HH:MM:DD
5092
** In number context: YYYYMMDDHHMMDD
5093
** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
5094
****************************************************************************/
5095
5096
int Field_datetime::store(const char *from,uint len,CHARSET_INFO *cs)
5097
{
5098
  ASSERT_COLUMN_MARKED_FOR_WRITE;
5099
  MYSQL_TIME time_tmp;
5100
  int error;
5101
  uint64_t tmp= 0;
5102
  enum enum_mysql_timestamp_type func_res;
5103
  THD *thd= table ? table->in_use : current_thd;
5104
5105
  func_res= str_to_datetime(from, len, &time_tmp,
5106
                            (TIME_FUZZY_DATE |
5107
                             (thd->variables.sql_mode &
5108
                              (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
5109
                               MODE_INVALID_DATES))),
5110
                            &error);
5111
  if ((int) func_res > (int) MYSQL_TIMESTAMP_ERROR)
5112
    tmp= TIME_to_ulonglong_datetime(&time_tmp);
5113
  else
5114
    error= 1;                                 // Fix if invalid zero date
5115
5116
  if (error)
5117
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5118
                         ER_WARN_DATA_OUT_OF_RANGE,
5119
                         from, len, MYSQL_TIMESTAMP_DATETIME, 1);
5120
5121
#ifdef WORDS_BIGENDIAN
5122
  if (table && table->s->db_low_byte_first)
5123
  {
5124
    int8store(ptr,tmp);
5125
  }
5126
  else
5127
#endif
5128
    longlongstore(ptr,tmp);
5129
  return error;
5130
}
5131
5132
5133
int Field_datetime::store(double nr)
5134
{
5135
  int error= 0;
5136
  if (nr < 0.0 || nr > 99991231235959.0)
5137
  {
5138
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, 
5139
                         ER_WARN_DATA_OUT_OF_RANGE,
5140
                         nr, MYSQL_TIMESTAMP_DATETIME);
5141
    nr= 0.0;
5142
    error= 1;
5143
  }
5144
  error|= Field_datetime::store((longlong) rint(nr), FALSE);
5145
  return error;
5146
}
5147
5148
5149
int Field_datetime::store(longlong nr, bool unsigned_val)
5150
{
5151
  ASSERT_COLUMN_MARKED_FOR_WRITE;
5152
  MYSQL_TIME not_used;
5153
  int error;
5154
  longlong initial_nr= nr;
5155
  THD *thd= table ? table->in_use : current_thd;
5156
5157
  nr= number_to_datetime(nr, &not_used, (TIME_FUZZY_DATE |
5158
                                         (thd->variables.sql_mode &
5159
                                          (MODE_NO_ZERO_IN_DATE |
5160
                                           MODE_NO_ZERO_DATE |
5161
                                           MODE_INVALID_DATES))), &error);
5162
80.1.1 by Brian Aker
LL() cleanup
5163
  if (nr == -1LL)
1 by brian
clean slate
5164
  {
5165
    nr= 0;
5166
    error= 2;
5167
  }
5168
5169
  if (error)
5170
    set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN,
5171
                         error == 2 ? ER_WARN_DATA_OUT_OF_RANGE :
5172
                         WARN_DATA_TRUNCATED, initial_nr,
5173
                         MYSQL_TIMESTAMP_DATETIME, 1);
5174
5175
#ifdef WORDS_BIGENDIAN
5176
  if (table && table->s->db_low_byte_first)
5177
  {
5178
    int8store(ptr,nr);
5179
  }
5180
  else
5181
#endif
5182
    longlongstore(ptr,nr);
5183
  return error;
5184
}
5185
5186
5187
int Field_datetime::store_time(MYSQL_TIME *ltime,timestamp_type time_type)
5188
{
5189
  ASSERT_COLUMN_MARKED_FOR_WRITE;
5190
  longlong tmp;
5191
  int error= 0;
5192
  /*
5193
    We don't perform range checking here since values stored in TIME
5194
    structure always fit into DATETIME range.
5195
  */
5196
  if (time_type == MYSQL_TIMESTAMP_DATE ||
5197
      time_type == MYSQL_TIMESTAMP_DATETIME)
5198
  {
80.1.1 by Brian Aker
LL() cleanup
5199
    tmp=((ltime->year*10000L+ltime->month*100+ltime->day)*1000000LL+
1 by brian
clean slate
5200
	 (ltime->hour*10000L+ltime->minute*100+ltime->second));
5201
    if (check_date(ltime, tmp != 0,
5202
                   (TIME_FUZZY_DATE |
5203
                    (current_thd->variables.sql_mode &
5204
                     (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
5205
                      MODE_INVALID_DATES))), &error))
5206
    {
5207
      char buff[MAX_DATE_STRING_REP_LENGTH];
5208
      String str(buff, sizeof(buff), &my_charset_latin1);
5209
      make_datetime((DATE_TIME_FORMAT *) 0, ltime, &str);
5210
      set_datetime_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
5211
                           str.ptr(), str.length(), MYSQL_TIMESTAMP_DATETIME,1);
5212
    }
5213
  }
5214
  else
5215
  {
5216
    tmp=0;
5217
    error= 1;
5218
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
5219
  }
5220
#ifdef WORDS_BIGENDIAN
5221
  if (table && table->s->db_low_byte_first)
5222
  {
5223
    int8store(ptr,tmp);
5224
  }
5225
  else
5226
#endif
5227
    longlongstore(ptr,tmp);
5228
  return error;
5229
}
5230
5231
bool Field_datetime::send_binary(Protocol *protocol)
5232
{
5233
  MYSQL_TIME tm;
5234
  Field_datetime::get_date(&tm, TIME_FUZZY_DATE);
5235
  return protocol->store(&tm);
5236
}
5237
5238
5239
double Field_datetime::val_real(void)
5240
{
5241
  return (double) Field_datetime::val_int();
5242
}
5243
5244
longlong Field_datetime::val_int(void)
5245
{
5246
  ASSERT_COLUMN_MARKED_FOR_READ;
5247
  longlong j;
5248
#ifdef WORDS_BIGENDIAN
5249
  if (table && table->s->db_low_byte_first)
5250
    j=sint8korr(ptr);
5251
  else
5252
#endif
5253
    longlongget(j,ptr);
5254
  return j;
5255
}
5256
5257
5258
String *Field_datetime::val_str(String *val_buffer,
5259
				String *val_ptr __attribute__((unused)))
5260
{
5261
  ASSERT_COLUMN_MARKED_FOR_READ;
5262
  val_buffer->alloc(field_length);
5263
  val_buffer->length(field_length);
5264
  uint64_t tmp;
5265
  long part1,part2;
5266
  char *pos;
5267
  int part3;
5268
5269
#ifdef WORDS_BIGENDIAN
5270
  if (table && table->s->db_low_byte_first)
5271
    tmp=sint8korr(ptr);
5272
  else
5273
#endif
5274
    longlongget(tmp,ptr);
5275
5276
  /*
5277
    Avoid problem with slow longlong arithmetic and sprintf
5278
  */
5279
80.1.1 by Brian Aker
LL() cleanup
5280
  part1=(long) (tmp/1000000LL);
5281
  part2=(long) (tmp - (uint64_t) part1*1000000LL);
1 by brian
clean slate
5282
5283
  pos=(char*) val_buffer->ptr() + MAX_DATETIME_WIDTH;
5284
  *pos--=0;
5285
  *pos--= (char) ('0'+(char) (part2%10)); part2/=10;
5286
  *pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
5287
  *pos--= ':';
5288
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
5289
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
5290
  *pos--= ':';
5291
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
5292
  *pos--= (char) ('0'+(char) part3);
5293
  *pos--= ' ';
5294
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
5295
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
5296
  *pos--= '-';
5297
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
5298
  *pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
5299
  *pos--= '-';
5300
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
5301
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
5302
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
5303
  *pos=(char) ('0'+(char) part3);
5304
  return val_buffer;
5305
}
5306
5307
bool Field_datetime::get_date(MYSQL_TIME *ltime, uint fuzzydate)
5308
{
5309
  longlong tmp=Field_datetime::val_int();
5310
  uint32 part1,part2;
80.1.1 by Brian Aker
LL() cleanup
5311
  part1=(uint32) (tmp/1000000LL);
5312
  part2=(uint32) (tmp - (uint64_t) part1*1000000LL);
1 by brian
clean slate
5313
5314
  ltime->time_type=	MYSQL_TIMESTAMP_DATETIME;
5315
  ltime->neg=		0;
5316
  ltime->second_part=	0;
5317
  ltime->second=	(int) (part2%100);
5318
  ltime->minute=	(int) (part2/100%100);
5319
  ltime->hour=		(int) (part2/10000);
5320
  ltime->day=		(int) (part1%100);
5321
  ltime->month= 	(int) (part1/100%100);
5322
  ltime->year= 		(int) (part1/10000);
5323
  return (!(fuzzydate & TIME_FUZZY_DATE) && (!ltime->month || !ltime->day)) ? 1 : 0;
5324
}
5325
5326
bool Field_datetime::get_time(MYSQL_TIME *ltime)
5327
{
5328
  return Field_datetime::get_date(ltime,0);
5329
}
5330
5331
int Field_datetime::cmp(const uchar *a_ptr, const uchar *b_ptr)
5332
{
5333
  longlong a,b;
5334
#ifdef WORDS_BIGENDIAN
5335
  if (table && table->s->db_low_byte_first)
5336
  {
5337
    a=sint8korr(a_ptr);
5338
    b=sint8korr(b_ptr);
5339
  }
5340
  else
5341
#endif
5342
  {
5343
    longlongget(a,a_ptr);
5344
    longlongget(b,b_ptr);
5345
  }
5346
  return ((uint64_t) a < (uint64_t) b) ? -1 :
5347
    ((uint64_t) a > (uint64_t) b) ? 1 : 0;
5348
}
5349
5350
void Field_datetime::sort_string(uchar *to,uint length __attribute__((unused)))
5351
{
5352
#ifdef WORDS_BIGENDIAN
5353
  if (!table || !table->s->db_low_byte_first)
5354
  {
5355
    to[0] = ptr[0];
5356
    to[1] = ptr[1];
5357
    to[2] = ptr[2];
5358
    to[3] = ptr[3];
5359
    to[4] = ptr[4];
5360
    to[5] = ptr[5];
5361
    to[6] = ptr[6];
5362
    to[7] = ptr[7];
5363
  }
5364
  else
5365
#endif
5366
  {
5367
    to[0] = ptr[7];
5368
    to[1] = ptr[6];
5369
    to[2] = ptr[5];
5370
    to[3] = ptr[4];
5371
    to[4] = ptr[3];
5372
    to[5] = ptr[2];
5373
    to[6] = ptr[1];
5374
    to[7] = ptr[0];
5375
  }
5376
}
5377
5378
5379
void Field_datetime::sql_type(String &res) const
5380
{
5381
  res.set_ascii(STRING_WITH_LEN("datetime"));
5382
}
5383
5384
/****************************************************************************
5385
** string type
5386
** A string may be varchar or binary
5387
****************************************************************************/
5388
5389
/*
5390
  Report "not well formed" or "cannot convert" error
5391
  after storing a character string info a field.
5392
5393
  SYNOPSIS
5394
    check_string_copy_error()
5395
    field                    - Field
5396
    well_formed_error_pos    - where not well formed data was first met
5397
    cannot_convert_error_pos - where a not-convertable character was first met
5398
    end                      - end of the string
5399
    cs                       - character set of the string
5400
5401
  NOTES
5402
    As of version 5.0 both cases return the same error:
5403
  
5404
      "Invalid string value: 'xxx' for column 't' at row 1"
5405
  
5406
  Future versions will possibly introduce a new error message:
5407
5408
      "Cannot convert character string: 'xxx' for column 't' at row 1"
5409
5410
  RETURN
5411
    FALSE - If errors didn't happen
5412
    TRUE  - If an error happened
5413
*/
5414
5415
static bool
5416
check_string_copy_error(Field_str *field,
5417
                        const char *well_formed_error_pos,
5418
                        const char *cannot_convert_error_pos,
5419
                        const char *end,
5420
                        CHARSET_INFO *cs)
5421
{
5422
  const char *pos, *end_orig;
5423
  char tmp[64], *t;
5424
  
5425
  if (!(pos= well_formed_error_pos) &&
5426
      !(pos= cannot_convert_error_pos))
5427
    return FALSE;
5428
5429
  end_orig= end;
5430
  set_if_smaller(end, pos + 6);
5431
5432
  for (t= tmp; pos < end; pos++)
5433
  {
5434
    /*
5435
      If the source string is ASCII compatible (mbminlen==1)
5436
      and the source character is in ASCII printable range (0x20..0x7F),
5437
      then display the character as is.
5438
      
5439
      Otherwise, if the source string is not ASCII compatible (e.g. UCS2),
5440
      or the source character is not in the printable range,
5441
      then print the character using HEX notation.
5442
    */
5443
    if (((unsigned char) *pos) >= 0x20 &&
5444
        ((unsigned char) *pos) <= 0x7F &&
5445
        cs->mbminlen == 1)
5446
    {
5447
      *t++= *pos;
5448
    }
5449
    else
5450
    {
5451
      *t++= '\\';
5452
      *t++= 'x';
5453
      *t++= _dig_vec_upper[((unsigned char) *pos) >> 4];
5454
      *t++= _dig_vec_upper[((unsigned char) *pos) & 15];
5455
    }
5456
  }
5457
  if (end_orig > end)
5458
  {
5459
    *t++= '.';
5460
    *t++= '.';
5461
    *t++= '.';
5462
  }
5463
  *t= '\0';
5464
  push_warning_printf(field->table->in_use, 
5465
                      field->table->in_use->abort_on_warning ?
5466
                      MYSQL_ERROR::WARN_LEVEL_ERROR :
5467
                      MYSQL_ERROR::WARN_LEVEL_WARN,
5468
                      ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 
5469
                      ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
5470
                      "string", tmp, field->field_name,
5471
                      (ulong) field->table->in_use->row_count);
5472
  return TRUE;
5473
}
5474
5475
5476
/*
5477
  Check if we lost any important data and send a truncation error/warning
5478
5479
  SYNOPSIS
5480
    Field_longstr::report_if_important_data()
5481
    ptr                      - Truncated rest of string
5482
    end                      - End of truncated string
5483
5484
  RETURN VALUES
5485
    0   - None was truncated (or we don't count cut fields)
5486
    2   - Some bytes was truncated
5487
5488
  NOTE
5489
    Check if we lost any important data (anything in a binary string,
5490
    or any non-space in others). If only trailing spaces was lost,
5491
    send a truncation note, otherwise send a truncation error.
5492
*/
5493
5494
int
5495
Field_longstr::report_if_important_data(const char *ptr, const char *end)
5496
{
5497
  if ((ptr < end) && table->in_use->count_cuted_fields)
5498
  {
5499
    if (test_if_important_data(field_charset, ptr, end))
5500
    {
5501
      if (table->in_use->abort_on_warning)
5502
        set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
5503
      else
5504
        set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
5505
    }
5506
    else /* If we lost only spaces then produce a NOTE, not a WARNING */
5507
      set_warning(MYSQL_ERROR::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
5508
    return 2;
5509
  }
5510
  return 0;
5511
}
5512
5513
5514
	/* Copy a string and fill with space */
5515
5516
int Field_string::store(const char *from,uint length,CHARSET_INFO *cs)
5517
{
5518
  ASSERT_COLUMN_MARKED_FOR_WRITE;
5519
  uint copy_length;
5520
  const char *well_formed_error_pos;
5521
  const char *cannot_convert_error_pos;
5522
  const char *from_end_pos;
5523
5524
  /* See the comment for Field_long::store(long long) */
5525
  DBUG_ASSERT(table->in_use == current_thd);
5526
5527
  copy_length= well_formed_copy_nchars(field_charset,
5528
                                       (char*) ptr, field_length,
5529
                                       cs, from, length,
5530
                                       field_length / field_charset->mbmaxlen,
5531
                                       &well_formed_error_pos,
5532
                                       &cannot_convert_error_pos,
5533
                                       &from_end_pos);
5534
5535
  /* Append spaces if the string was shorter than the field. */
5536
  if (copy_length < field_length)
5537
    field_charset->cset->fill(field_charset,(char*) ptr+copy_length,
5538
                              field_length-copy_length,
5539
                              field_charset->pad_char);
5540
5541
  if (check_string_copy_error(this, well_formed_error_pos,
5542
                              cannot_convert_error_pos, from + length, cs))
5543
    return 2;
5544
5545
  return report_if_important_data(from_end_pos, from + length);
5546
}
5547
5548
5549
/**
5550
  Store double value in Field_string or Field_varstring.
5551
5552
  Pretty prints double number into field_length characters buffer.
5553
5554
  @param nr            number
5555
*/
5556
5557
int Field_str::store(double nr)
5558
{
5559
  ASSERT_COLUMN_MARKED_FOR_WRITE;
5560
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
5561
  uint local_char_length= field_length / charset()->mbmaxlen;
5562
  size_t length;
5563
  my_bool error;
5564
5565
  length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
5566
  if (error)
5567
  {
5568
    if (table->in_use->abort_on_warning)
5569
      set_warning(MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
5570
    else
5571
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
5572
  }
5573
  return store(buff, length, charset());
5574
}
5575
5576
5577
uint Field::is_equal(Create_field *new_field)
5578
{
5579
  return (new_field->sql_type == real_type());
5580
}
5581
5582
5583
/* If one of the fields is binary and the other one isn't return 1 else 0 */
5584
5585
bool Field_str::compare_str_field_flags(Create_field *new_field, uint32 flag_arg)
5586
{
5587
  return (((new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
5588
          !(flag_arg & (BINCMP_FLAG | BINARY_FLAG))) ||
5589
         (!(new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
5590
          (flag_arg & (BINCMP_FLAG | BINARY_FLAG))));
5591
}
5592
5593
5594
uint Field_str::is_equal(Create_field *new_field)
5595
{
5596
  if (compare_str_field_flags(new_field, flags))
5597
    return 0;
5598
5599
  return ((new_field->sql_type == real_type()) &&
5600
	  new_field->charset == field_charset &&
5601
	  new_field->length == max_display_length());
5602
}
5603
5604
5605
int Field_string::store(longlong nr, bool unsigned_val)
5606
{
5607
  char buff[64];
5608
  int  l;
5609
  CHARSET_INFO *cs=charset();
5610
  l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),
5611
                                   unsigned_val ? 10 : -10, nr);
5612
  return Field_string::store(buff,(uint)l,cs);
5613
}
5614
5615
5616
int Field_longstr::store_decimal(const my_decimal *d)
5617
{
5618
  char buff[DECIMAL_MAX_STR_LENGTH+1];
5619
  String str(buff, sizeof(buff), &my_charset_bin);
5620
  my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
5621
  return store(str.ptr(), str.length(), str.charset());
5622
}
5623
5624
uint32 Field_longstr::max_data_length() const
5625
{
5626
  return field_length + (field_length > 255 ? 2 : 1);
5627
}
5628
5629
5630
double Field_string::val_real(void)
5631
{
5632
  ASSERT_COLUMN_MARKED_FOR_READ;
5633
  int error;
5634
  char *end;
5635
  CHARSET_INFO *cs= charset();
5636
  double result;
5637
  
5638
  result=  my_strntod(cs,(char*) ptr,field_length,&end,&error);
5639
  if (!table->in_use->no_errors &&
5640
      (error || (field_length != (uint32)(end - (char*) ptr) && 
5641
                 !check_if_only_end_space(cs, end,
5642
                                          (char*) ptr + field_length))))
5643
  {
5644
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
5645
    String tmp(buf, sizeof(buf), cs);
5646
    tmp.copy((char*) ptr, field_length, cs);
5647
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
5648
                        ER_TRUNCATED_WRONG_VALUE, 
5649
                        ER(ER_TRUNCATED_WRONG_VALUE),
5650
                        "DOUBLE", tmp.c_ptr());
5651
  }
5652
  return result;
5653
}
5654
5655
5656
longlong Field_string::val_int(void)
5657
{
5658
  ASSERT_COLUMN_MARKED_FOR_READ;
5659
  int error;
5660
  char *end;
5661
  CHARSET_INFO *cs= charset();
5662
  longlong result;
5663
5664
  result= my_strntoll(cs, (char*) ptr,field_length,10,&end,&error);
5665
  if (!table->in_use->no_errors &&
5666
      (error || (field_length != (uint32)(end - (char*) ptr) && 
5667
                 !check_if_only_end_space(cs, end,
5668
                                          (char*) ptr + field_length))))
5669
  {
5670
    char buf[LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE];
5671
    String tmp(buf, sizeof(buf), cs);
5672
    tmp.copy((char*) ptr, field_length, cs);
5673
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
5674
                        ER_TRUNCATED_WRONG_VALUE, 
5675
                        ER(ER_TRUNCATED_WRONG_VALUE),
5676
                        "INTEGER", tmp.c_ptr());
5677
  }
5678
  return result;
5679
}
5680
5681
5682
String *Field_string::val_str(String *val_buffer __attribute__((unused)),
5683
			      String *val_ptr)
5684
{
5685
  ASSERT_COLUMN_MARKED_FOR_READ;
5686
  /* See the comment for Field_long::store(long long) */
5687
  DBUG_ASSERT(table->in_use == current_thd);
5688
  uint length;
5689
  if (table->in_use->variables.sql_mode &
5690
      MODE_PAD_CHAR_TO_FULL_LENGTH)
5691
    length= my_charpos(field_charset, ptr, ptr + field_length, field_length);
5692
  else
5693
    length= field_charset->cset->lengthsp(field_charset, (const char*) ptr,
5694
                                          field_length);
5695
  val_ptr->set((const char*) ptr, length, field_charset);
5696
  return val_ptr;
5697
}
5698
5699
5700
my_decimal *Field_string::val_decimal(my_decimal *decimal_value)
5701
{
5702
  ASSERT_COLUMN_MARKED_FOR_READ;
5703
  int err= str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr, field_length,
5704
                          charset(), decimal_value);
5705
  if (!table->in_use->no_errors && err)
5706
  {
5707
    char buf[DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE];
5708
    CHARSET_INFO *cs= charset();
5709
    String tmp(buf, sizeof(buf), cs);
5710
    tmp.copy((char*) ptr, field_length, cs);
5711
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
5712
                        ER_TRUNCATED_WRONG_VALUE, 
5713
                        ER(ER_TRUNCATED_WRONG_VALUE),
5714
                        "DECIMAL", tmp.c_ptr());
5715
  }
5716
5717
  return decimal_value;
5718
}
5719
5720
5721
int Field_string::cmp(const uchar *a_ptr, const uchar *b_ptr)
5722
{
5723
  uint a_len, b_len;
5724
5725
  if (field_charset->mbmaxlen != 1)
5726
  {
5727
    uint char_len= field_length/field_charset->mbmaxlen;
5728
    a_len= my_charpos(field_charset, a_ptr, a_ptr + field_length, char_len);
5729
    b_len= my_charpos(field_charset, b_ptr, b_ptr + field_length, char_len);
5730
  }
5731
  else
5732
    a_len= b_len= field_length;
5733
  /*
5734
    We have to remove end space to be able to compare multi-byte-characters
5735
    like in latin_de 'ae' and 0xe4
5736
  */
5737
  return field_charset->coll->strnncollsp(field_charset,
5738
                                          a_ptr, a_len,
5739
                                          b_ptr, b_len,
5740
                                          0);
5741
}
5742
5743
5744
void Field_string::sort_string(uchar *to,uint length)
5745
{
5746
  IF_DBUG(uint tmp=) my_strnxfrm(field_charset,
5747
                                 to, length,
5748
                                 ptr, field_length);
5749
  DBUG_ASSERT(tmp == length);
5750
}
5751
5752
5753
void Field_string::sql_type(String &res) const
5754
{
5755
  THD *thd= table->in_use;
5756
  CHARSET_INFO *cs=res.charset();
5757
  ulong length;
5758
5759
  length= cs->cset->snprintf(cs,(char*) res.ptr(),
5760
                             res.alloced_length(), "%s(%d)",
5761
                             ((type() == MYSQL_TYPE_VAR_STRING &&
5762
                               !thd->variables.new_mode) ?
5763
                              (has_charset() ? "varchar" : "varbinary") :
5764
			      (has_charset() ? "char" : "binary")),
5765
                             (int) field_length / charset()->mbmaxlen);
5766
  res.length(length);
5767
  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
5768
      has_charset() && (charset()->state & MY_CS_BINSORT))
5769
    res.append(STRING_WITH_LEN(" binary"));
5770
}
5771
5772
5773
uchar *Field_string::pack(uchar *to, const uchar *from,
5774
                          uint max_length,
5775
                          bool low_byte_first __attribute__((unused)))
5776
{
5777
  uint length=      min(field_length,max_length);
5778
  uint local_char_length= max_length/field_charset->mbmaxlen;
5779
  if (length > local_char_length)
5780
    local_char_length= my_charpos(field_charset, from, from+length,
5781
                                  local_char_length);
5782
  set_if_smaller(length, local_char_length);
5783
  while (length && from[length-1] == field_charset->pad_char)
5784
    length--;
5785
5786
  // Length always stored little-endian
5787
  *to++= (uchar) length;
5788
  if (field_length > 255)
5789
    *to++= (uchar) (length >> 8);
5790
5791
  // Store the actual bytes of the string
5792
  memcpy(to, from, length);
5793
  return to+length;
5794
}
5795
5796
5797
/**
5798
   Unpack a string field from row data.
5799
5800
   This method is used to unpack a string field from a master whose size 
5801
   of the field is less than that of the slave. Note that there can be a
5802
   variety of field types represented with this class. Certain types like
5803
   ENUM or SET are processed differently. Hence, the upper byte of the 
5804
   @c param_data argument contains the result of field->real_type() from
5805
   the master.
5806
5807
   @param   to         Destination of the data
5808
   @param   from       Source of the data
5809
   @param   param_data Real type (upper) and length (lower) values
5810
5811
   @return  New pointer into memory based on from + length of the data
5812
*/
5813
const uchar *
5814
Field_string::unpack(uchar *to,
5815
                     const uchar *from,
5816
                     uint param_data,
5817
                     bool low_byte_first __attribute__((unused)))
5818
{
5819
  uint from_length=
5820
    param_data ? min(param_data & 0x00ff, field_length) : field_length;
5821
  uint length;
5822
5823
  if (from_length > 255)
5824
  {
5825
    length= uint2korr(from);
5826
    from+= 2;
5827
  }
5828
  else
5829
    length= (uint) *from++;
5830
5831
  memcpy(to, from, length);
5832
  // Pad the string with the pad character of the fields charset
5833
  bfill(to + length, field_length - length, field_charset->pad_char);
5834
  return from+length;
5835
}
5836
5837
5838
/**
5839
   Save the field metadata for string fields.
5840
5841
   Saves the real type in the first byte and the field length in the 
5842
   second byte of the field metadata array at index of *metadata_ptr and
5843
   *(metadata_ptr + 1).
5844
5845
   @param   metadata_ptr   First byte of field metadata
5846
5847
   @returns number of bytes written to metadata_ptr
5848
*/
5849
int Field_string::do_save_field_metadata(uchar *metadata_ptr)
5850
{
5851
  *metadata_ptr= real_type();
5852
  *(metadata_ptr + 1)= field_length;
5853
  return 2;
5854
}
5855
5856
5857
/*
5858
  Compare two packed keys
5859
5860
  SYNOPSIS
5861
    pack_cmp()
5862
     a			New key
5863
     b			Original key
5864
     length		Key length
5865
     insert_or_update	1 if this is an insert or update
5866
5867
  RETURN
5868
    < 0	  a < b
5869
    0	  a = b
5870
    > 0   a > b
5871
*/
5872
5873
int Field_string::pack_cmp(const uchar *a, const uchar *b, uint length,
5874
                           my_bool insert_or_update)
5875
{
5876
  uint a_length, b_length;
5877
  if (length > 255)
5878
  {
5879
    a_length= uint2korr(a);
5880
    b_length= uint2korr(b);
5881
    a+= 2;
5882
    b+= 2;
5883
  }
5884
  else
5885
  {
5886
    a_length= (uint) *a++;
5887
    b_length= (uint) *b++;
5888
  }
5889
  return field_charset->coll->strnncollsp(field_charset,
5890
                                          a, a_length,
5891
                                          b, b_length,
5892
                                          insert_or_update);
5893
}
5894
5895
5896
/**
5897
  Compare a packed key against row.
5898
5899
  @param key		        Original key
5900
  @param length		Key length. (May be less than field length)
5901
  @param insert_or_update	1 if this is an insert or update
5902
5903
  @return
5904
    < 0	  row < key
5905
  @return
5906
    0	  row = key
5907
  @return
5908
    > 0   row > key
5909
*/
5910
5911
int Field_string::pack_cmp(const uchar *key, uint length,
5912
                           my_bool insert_or_update)
5913
{
5914
  uint row_length, local_key_length;
5915
  uchar *end;
5916
  if (length > 255)
5917
  {
5918
    local_key_length= uint2korr(key);
5919
    key+= 2;
5920
  }
5921
  else
5922
    local_key_length= (uint) *key++;
5923
  
5924
  /* Only use 'length' of key, not field_length */
5925
  end= ptr + length;
5926
  while (end > ptr && end[-1] == ' ')
5927
    end--;
5928
  row_length= (uint) (end - ptr);
5929
5930
  return field_charset->coll->strnncollsp(field_charset,
5931
                                          ptr, row_length,
5932
                                          key, local_key_length,
5933
                                          insert_or_update);
5934
}
5935
5936
5937
uint Field_string::packed_col_length(const uchar *data_ptr, uint length)
5938
{
5939
  if (length > 255)
5940
    return uint2korr(data_ptr)+2;
5941
  return (uint) *data_ptr + 1;
5942
}
5943
5944
5945
uint Field_string::max_packed_col_length(uint max_length)
5946
{
5947
  return (max_length > 255 ? 2 : 1)+max_length;
5948
}
5949
5950
5951
uint Field_string::get_key_image(uchar *buff, uint length, imagetype type_arg)
5952
{
5953
  uint bytes = my_charpos(field_charset, (char*) ptr,
5954
                          (char*) ptr + field_length,
5955
                          length / field_charset->mbmaxlen);
5956
  memcpy(buff, ptr, bytes);
5957
  if (bytes < length)
5958
    field_charset->cset->fill(field_charset, (char*) buff + bytes,
5959
                              length - bytes, field_charset->pad_char);
5960
  return bytes;
5961
}
5962
5963
5964
Field *Field_string::new_field(MEM_ROOT *root, struct st_table *new_table,
5965
                               bool keep_type)
5966
{
5967
  Field *field;
5968
  if (type() != MYSQL_TYPE_VAR_STRING || keep_type)
5969
    field= Field::new_field(root, new_table, keep_type);
5970
  else if ((field= new Field_varstring(field_length, maybe_null(), field_name,
5971
                                       new_table->s, charset())))
5972
  {
5973
    /*
5974
      Old VARCHAR field which should be modified to a VARCHAR on copy
5975
      This is done to ensure that ALTER TABLE will convert old VARCHAR fields
5976
      to now VARCHAR fields.
5977
    */
5978
    field->init(new_table);
5979
    /*
5980
      Normally orig_table is different from table only if field was created
5981
      via ::new_field.  Here we alter the type of field, so ::new_field is
5982
      not applicable. But we still need to preserve the original field
5983
      metadata for the client-server protocol.
5984
    */
5985
    field->orig_table= orig_table;
5986
  }
5987
  return field;
5988
}
5989
5990
5991
/****************************************************************************
5992
  VARCHAR type
5993
  Data in field->ptr is stored as:
5994
    1 or 2 bytes length-prefix-header  (from Field_varstring::length_bytes)
5995
    data
5996
5997
  NOTE:
5998
  When VARCHAR is stored in a key (for handler::index_read() etc) it's always
5999
  stored with a 2 byte prefix. (Just like blob keys).
6000
6001
  Normally length_bytes is calculated as (field_length < 256 : 1 ? 2)
6002
  The exception is if there is a prefix key field that is part of a long
6003
  VARCHAR, in which case field_length for this may be 1 but the length_bytes
6004
  is 2.
6005
****************************************************************************/
6006
6007
const uint Field_varstring::MAX_SIZE= UINT_MAX16;
6008
6009
/**
6010
   Save the field metadata for varstring fields.
6011
6012
   Saves the field length in the first byte. Note: may consume
6013
   2 bytes. Caller must ensure second byte is contiguous with
6014
   first byte (e.g. array index 0,1).
6015
6016
   @param   metadata_ptr   First byte of field metadata
6017
6018
   @returns number of bytes written to metadata_ptr
6019
*/
6020
int Field_varstring::do_save_field_metadata(uchar *metadata_ptr)
6021
{
6022
  char *ptr= (char *)metadata_ptr;
6023
  DBUG_ASSERT(field_length <= 65535);
6024
  int2store(ptr, field_length);
6025
  return 2;
6026
}
6027
6028
int Field_varstring::store(const char *from,uint length,CHARSET_INFO *cs)
6029
{
6030
  ASSERT_COLUMN_MARKED_FOR_WRITE;
6031
  uint copy_length;
6032
  const char *well_formed_error_pos;
6033
  const char *cannot_convert_error_pos;
6034
  const char *from_end_pos;
6035
6036
  copy_length= well_formed_copy_nchars(field_charset,
6037
                                       (char*) ptr + length_bytes,
6038
                                       field_length,
6039
                                       cs, from, length,
6040
                                       field_length / field_charset->mbmaxlen,
6041
                                       &well_formed_error_pos,
6042
                                       &cannot_convert_error_pos,
6043
                                       &from_end_pos);
6044
6045
  if (length_bytes == 1)
6046
    *ptr= (uchar) copy_length;
6047
  else
6048
    int2store(ptr, copy_length);
6049
6050
  if (check_string_copy_error(this, well_formed_error_pos,
6051
                              cannot_convert_error_pos, from + length, cs))
6052
    return 2;
6053
6054
  return report_if_important_data(from_end_pos, from + length);
6055
}
6056
6057
6058
int Field_varstring::store(longlong nr, bool unsigned_val)
6059
{
6060
  char buff[64];
6061
  uint  length;
6062
  length= (uint) (field_charset->cset->longlong10_to_str)(field_charset,
6063
                                                          buff,
6064
                                                          sizeof(buff),
6065
                                                          (unsigned_val ? 10:
6066
                                                           -10),
6067
                                                           nr);
6068
  return Field_varstring::store(buff, length, field_charset);
6069
}
6070
6071
6072
double Field_varstring::val_real(void)
6073
{
6074
  ASSERT_COLUMN_MARKED_FOR_READ;
6075
  int not_used;
6076
  char *end_not_used;
6077
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6078
  return my_strntod(field_charset, (char*) ptr+length_bytes, length,
6079
                    &end_not_used, &not_used);
6080
}
6081
6082
6083
longlong Field_varstring::val_int(void)
6084
{
6085
  ASSERT_COLUMN_MARKED_FOR_READ;
6086
  int not_used;
6087
  char *end_not_used;
6088
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6089
  return my_strntoll(field_charset, (char*) ptr+length_bytes, length, 10,
6090
                     &end_not_used, &not_used);
6091
}
6092
6093
String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
6094
				 String *val_ptr)
6095
{
6096
  ASSERT_COLUMN_MARKED_FOR_READ;
6097
  uint length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6098
  val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
6099
  return val_ptr;
6100
}
6101
6102
6103
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
6104
{
6105
  ASSERT_COLUMN_MARKED_FOR_READ;
6106
  uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6107
  str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
6108
                 charset(), decimal_value);
6109
  return decimal_value;
6110
}
6111
6112
6113
int Field_varstring::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
6114
                             uint max_len)
6115
{
6116
  uint a_length, b_length;
6117
  int diff;
6118
6119
  if (length_bytes == 1)
6120
  {
6121
    a_length= (uint) *a_ptr;
6122
    b_length= (uint) *b_ptr;
6123
  }
6124
  else
6125
  {
6126
    a_length= uint2korr(a_ptr);
6127
    b_length= uint2korr(b_ptr);
6128
  }
6129
  set_if_smaller(a_length, max_len);
6130
  set_if_smaller(b_length, max_len);
6131
  diff= field_charset->coll->strnncollsp(field_charset,
6132
                                         a_ptr+
6133
                                         length_bytes,
6134
                                         a_length,
6135
                                         b_ptr+
6136
                                         length_bytes,
6137
                                         b_length,0);
6138
  return diff;
6139
}
6140
6141
6142
/**
6143
  @note
6144
    varstring and blob keys are ALWAYS stored with a 2 byte length prefix
6145
*/
6146
6147
int Field_varstring::key_cmp(const uchar *key_ptr, uint max_key_length)
6148
{
6149
  uint length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6150
  uint local_char_length= max_key_length / field_charset->mbmaxlen;
6151
6152
  local_char_length= my_charpos(field_charset, ptr + length_bytes,
6153
                          ptr + length_bytes + length, local_char_length);
6154
  set_if_smaller(length, local_char_length);
6155
  return field_charset->coll->strnncollsp(field_charset, 
6156
                                          ptr + length_bytes,
6157
                                          length,
6158
                                          key_ptr+
6159
                                          HA_KEY_BLOB_LENGTH,
6160
                                          uint2korr(key_ptr), 0);
6161
}
6162
6163
6164
/**
6165
  Compare to key segments (always 2 byte length prefix).
6166
6167
  @note
6168
    This is used only to compare key segments created for index_read().
6169
    (keys are created and compared in key.cc)
6170
*/
6171
6172
int Field_varstring::key_cmp(const uchar *a,const uchar *b)
6173
{
6174
  return field_charset->coll->strnncollsp(field_charset,
6175
                                          a + HA_KEY_BLOB_LENGTH,
6176
                                          uint2korr(a),
6177
                                          b + HA_KEY_BLOB_LENGTH,
6178
                                          uint2korr(b),
6179
                                          0);
6180
}
6181
6182
6183
void Field_varstring::sort_string(uchar *to,uint length)
6184
{
6185
  uint tot_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6186
6187
  if (field_charset == &my_charset_bin)
6188
  {
6189
    /* Store length last in high-byte order to sort longer strings first */
6190
    if (length_bytes == 1)
6191
      to[length-1]= tot_length;
6192
    else
6193
      mi_int2store(to+length-2, tot_length);
6194
    length-= length_bytes;
6195
  }
6196
 
6197
  tot_length= my_strnxfrm(field_charset,
6198
			  to, length, ptr + length_bytes,
6199
			  tot_length);
6200
  DBUG_ASSERT(tot_length == length);
6201
}
6202
6203
6204
enum ha_base_keytype Field_varstring::key_type() const
6205
{
6206
  enum ha_base_keytype res;
6207
6208
  if (binary())
6209
    res= length_bytes == 1 ? HA_KEYTYPE_VARBINARY1 : HA_KEYTYPE_VARBINARY2;
6210
  else
6211
    res= length_bytes == 1 ? HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2;
6212
  return res;
6213
}
6214
6215
6216
void Field_varstring::sql_type(String &res) const
6217
{
6218
  THD *thd= table->in_use;
6219
  CHARSET_INFO *cs=res.charset();
6220
  ulong length;
6221
6222
  length= cs->cset->snprintf(cs,(char*) res.ptr(),
6223
                             res.alloced_length(), "%s(%d)",
6224
                              (has_charset() ? "varchar" : "varbinary"),
6225
                             (int) field_length / charset()->mbmaxlen);
6226
  res.length(length);
6227
  if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
6228
      has_charset() && (charset()->state & MY_CS_BINSORT))
6229
    res.append(STRING_WITH_LEN(" binary"));
6230
}
6231
6232
6233
uint32 Field_varstring::data_length()
6234
{
6235
  return length_bytes == 1 ? (uint32) *ptr : uint2korr(ptr);
6236
}
6237
6238
uint32 Field_varstring::used_length()
6239
{
6240
  return length_bytes == 1 ? 1 + (uint32) (uchar) *ptr : 2 + uint2korr(ptr);
6241
}
6242
6243
/*
6244
  Functions to create a packed row.
6245
  Here the number of length bytes are depending on the given max_length
6246
*/
6247
6248
uchar *Field_varstring::pack(uchar *to, const uchar *from,
6249
                             uint max_length,
6250
                             bool low_byte_first __attribute__((unused)))
6251
{
6252
  uint length= length_bytes == 1 ? (uint) *from : uint2korr(from);
6253
  set_if_smaller(max_length, field_length);
6254
  if (length > max_length)
6255
    length=max_length;
6256
6257
  /* Length always stored little-endian */
6258
  *to++= length & 0xFF;
6259
  if (max_length > 255)
6260
    *to++= (length >> 8) & 0xFF;
6261
6262
  /* Store bytes of string */
6263
  if (length > 0)
6264
    memcpy(to, from+length_bytes, length);
6265
  return to+length;
6266
}
6267
6268
6269
uchar *
6270
Field_varstring::pack_key(uchar *to, const uchar *key, uint max_length,
6271
                          bool low_byte_first __attribute__((unused)))
6272
{
6273
  uint length=  length_bytes == 1 ? (uint) *key : uint2korr(key);
6274
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
6275
                     max_length/field_charset->mbmaxlen : max_length);
6276
  key+= length_bytes;
6277
  if (length > local_char_length)
6278
  {
6279
    local_char_length= my_charpos(field_charset, key, key+length,
6280
                                  local_char_length);
6281
    set_if_smaller(length, local_char_length);
6282
  }
6283
  *to++= (char) (length & 255);
6284
  if (max_length > 255)
6285
    *to++= (char) (length >> 8);
6286
  if (length)
6287
    memcpy(to, key, length);
6288
  return to+length;
6289
}
6290
6291
6292
/**
6293
  Unpack a key into a record buffer.
6294
6295
  A VARCHAR key has a maximum size of 64K-1.
6296
  In its packed form, the length field is one or two bytes long,
6297
  depending on 'max_length'.
6298
6299
  @param to                          Pointer into the record buffer.
6300
  @param key                         Pointer to the packed key.
6301
  @param max_length                  Key length limit from key description.
6302
6303
  @return
6304
    Pointer to end of 'key' (To the next key part if multi-segment key)
6305
*/
6306
6307
const uchar *
6308
Field_varstring::unpack_key(uchar *to, const uchar *key, uint max_length,
6309
                            bool low_byte_first __attribute__((unused)))
6310
{
6311
  /* get length of the blob key */
6312
  uint32 length= *key++;
6313
  if (max_length > 255)
6314
    length+= (*key++) << 8;
6315
6316
  /* put the length into the record buffer */
6317
  if (length_bytes == 1)
6318
    *ptr= (uchar) length;
6319
  else
6320
    int2store(ptr, length);
6321
  memcpy(ptr + length_bytes, key, length);
6322
  return key + length;
6323
}
6324
6325
/**
6326
  Create a packed key that will be used for storage in the index tree.
6327
6328
  @param to		Store packed key segment here
6329
  @param from		Key segment (as given to index_read())
6330
  @param max_length  	Max length of key
6331
6332
  @return
6333
    end of key storage
6334
*/
6335
6336
uchar *
6337
Field_varstring::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
6338
                                         bool low_byte_first __attribute__((unused)))
6339
{
6340
  /* Key length is always stored as 2 bytes */
6341
  uint length= uint2korr(from);
6342
  if (length > max_length)
6343
    length= max_length;
6344
  *to++= (char) (length & 255);
6345
  if (max_length > 255)
6346
    *to++= (char) (length >> 8);
6347
  if (length)
6348
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
6349
  return to+length;
6350
}
6351
6352
6353
/**
6354
   Unpack a varstring field from row data.
6355
6356
   This method is used to unpack a varstring field from a master
6357
   whose size of the field is less than that of the slave.
6358
6359
   @note
6360
   The string length is always packed little-endian.
6361
  
6362
   @param   to         Destination of the data
6363
   @param   from       Source of the data
6364
   @param   param_data Length bytes from the master's field data
6365
6366
   @return  New pointer into memory based on from + length of the data
6367
*/
6368
const uchar *
6369
Field_varstring::unpack(uchar *to, const uchar *from,
6370
                        uint param_data,
6371
                        bool low_byte_first __attribute__((unused)))
6372
{
6373
  uint length;
6374
  uint l_bytes= (param_data && (param_data < field_length)) ? 
6375
                (param_data <= 255) ? 1 : 2 : length_bytes;
6376
  if (l_bytes == 1)
6377
  {
6378
    to[0]= *from++;
6379
    length= to[0];
6380
    if (length_bytes == 2)
6381
      to[1]= 0;
6382
  }
6383
  else /* l_bytes == 2 */
6384
  {
6385
    length= uint2korr(from);
6386
    to[0]= *from++;
6387
    to[1]= *from++;
6388
  }
6389
  if (length)
6390
    memcpy(to+ length_bytes, from, length);
6391
  return from+length;
6392
}
6393
6394
6395
int Field_varstring::pack_cmp(const uchar *a, const uchar *b,
6396
                              uint key_length_arg,
6397
                              my_bool insert_or_update)
6398
{
6399
  uint a_length, b_length;
6400
  if (key_length_arg > 255)
6401
  {
6402
    a_length=uint2korr(a); a+= 2;
6403
    b_length=uint2korr(b); b+= 2;
6404
  }
6405
  else
6406
  {
6407
    a_length= (uint) *a++;
6408
    b_length= (uint) *b++;
6409
  }
6410
  return field_charset->coll->strnncollsp(field_charset,
6411
                                          a, a_length,
6412
                                          b, b_length,
6413
                                          insert_or_update);
6414
}
6415
6416
6417
int Field_varstring::pack_cmp(const uchar *b, uint key_length_arg,
6418
                              my_bool insert_or_update)
6419
{
6420
  uchar *a= ptr+ length_bytes;
6421
  uint a_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6422
  uint b_length;
6423
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
6424
                           key_length_arg / field_charset->mbmaxlen :
6425
                           key_length_arg);
6426
6427
  if (key_length_arg > 255)
6428
  {
6429
    b_length=uint2korr(b); b+= HA_KEY_BLOB_LENGTH;
6430
  }
6431
  else
6432
    b_length= (uint) *b++;
6433
6434
  if (a_length > local_char_length)
6435
  {
6436
    local_char_length= my_charpos(field_charset, a, a+a_length,
6437
                                  local_char_length);
6438
    set_if_smaller(a_length, local_char_length);
6439
  }
6440
6441
  return field_charset->coll->strnncollsp(field_charset,
6442
                                          a, a_length,
6443
                                          b, b_length,
6444
                                          insert_or_update);
6445
}
6446
6447
6448
uint Field_varstring::packed_col_length(const uchar *data_ptr, uint length)
6449
{
6450
  if (length > 255)
6451
    return uint2korr(data_ptr)+2;
6452
  return (uint) *data_ptr + 1;
6453
}
6454
6455
6456
uint Field_varstring::max_packed_col_length(uint max_length)
6457
{
6458
  return (max_length > 255 ? 2 : 1)+max_length;
6459
}
6460
6461
uint Field_varstring::get_key_image(uchar *buff, uint length, imagetype type)
6462
{
6463
  uint f_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6464
  uint local_char_length= length / field_charset->mbmaxlen;
6465
  uchar *pos= ptr+length_bytes;
6466
  local_char_length= my_charpos(field_charset, pos, pos + f_length,
6467
                                local_char_length);
6468
  set_if_smaller(f_length, local_char_length);
6469
  /* Key is always stored with 2 bytes */
6470
  int2store(buff,f_length);
6471
  memcpy(buff+HA_KEY_BLOB_LENGTH, pos, f_length);
6472
  if (f_length < length)
6473
  {
6474
    /*
6475
      Must clear this as we do a memcmp in opt_range.cc to detect
6476
      identical keys
6477
    */
6478
    bzero(buff+HA_KEY_BLOB_LENGTH+f_length, (length-f_length));
6479
  }
6480
  return HA_KEY_BLOB_LENGTH+f_length;
6481
}
6482
6483
6484
void Field_varstring::set_key_image(const uchar *buff,uint length)
6485
{
6486
  length= uint2korr(buff);			// Real length is here
6487
  (void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
6488
                                field_charset);
6489
}
6490
6491
6492
int Field_varstring::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
6493
                                uint32 max_length)
6494
{
6495
  uint32 a_length,b_length;
6496
6497
  if (length_bytes == 1)
6498
  {
6499
    a_length= (uint) *a_ptr;
6500
    b_length= (uint) *b_ptr;
6501
  }
6502
  else
6503
  {
6504
    a_length= uint2korr(a_ptr);
6505
    b_length= uint2korr(b_ptr);
6506
  }
6507
  set_if_smaller(a_length, max_length);
6508
  set_if_smaller(b_length, max_length);
6509
  if (a_length != b_length)
6510
    return 1;
6511
  return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
6512
}
6513
6514
6515
Field *Field_varstring::new_field(MEM_ROOT *root, struct st_table *new_table,
6516
                                  bool keep_type)
6517
{
6518
  Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
6519
                                                            keep_type);
6520
  if (res)
6521
    res->length_bytes= length_bytes;
6522
  return res;
6523
}
6524
6525
6526
Field *Field_varstring::new_key_field(MEM_ROOT *root,
6527
                                      struct st_table *new_table,
6528
                                      uchar *new_ptr, uchar *new_null_ptr,
6529
                                      uint new_null_bit)
6530
{
6531
  Field_varstring *res;
6532
  if ((res= (Field_varstring*) Field::new_key_field(root,
6533
                                                    new_table,
6534
                                                    new_ptr,
6535
                                                    new_null_ptr,
6536
                                                    new_null_bit)))
6537
  {
6538
    /* Keys length prefixes are always packed with 2 bytes */
6539
    res->length_bytes= 2;
6540
  }
6541
  return res;
6542
}
6543
6544
6545
uint Field_varstring::is_equal(Create_field *new_field)
6546
{
6547
  if (new_field->sql_type == real_type() &&
6548
      new_field->charset == field_charset)
6549
  {
6550
    if (new_field->length == max_display_length())
6551
      return IS_EQUAL_YES;
6552
    if (new_field->length > max_display_length() &&
6553
	((new_field->length <= 255 && max_display_length() <= 255) ||
6554
	 (new_field->length > 255 && max_display_length() > 255)))
6555
      return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
6556
  }
6557
  return IS_EQUAL_NO;
6558
}
6559
6560
6561
void Field_varstring::hash(ulong *nr, ulong *nr2)
6562
{
6563
  if (is_null())
6564
  {
6565
    *nr^= (*nr << 1) | 1;
6566
  }
6567
  else
6568
  {
6569
    uint len=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
6570
    CHARSET_INFO *cs= charset();
6571
    cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);
6572
  }
6573
}
6574
6575
6576
/****************************************************************************
6577
** blob type
6578
** A blob is saved as a length and a pointer. The length is stored in the
6579
** packlength slot and may be from 1-4.
6580
****************************************************************************/
6581
6582
Field_blob::Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
6583
		       enum utype unireg_check_arg, const char *field_name_arg,
6584
                       TABLE_SHARE *share, uint blob_pack_length,
6585
		       CHARSET_INFO *cs)
6586
  :Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
6587
                 null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
6588
                 cs),
6589
   packlength(blob_pack_length)
6590
{
6591
  flags|= BLOB_FLAG;
6592
  share->blob_fields++;
6593
  /* TODO: why do not fill table->s->blob_field array here? */
6594
}
6595
6596
6597
void Field_blob::store_length(uchar *i_ptr, 
6598
                              uint i_packlength, 
6599
                              uint32 i_number, 
6600
                              bool low_byte_first)
6601
{
6602
  switch (i_packlength) {
6603
  case 1:
6604
    i_ptr[0]= (uchar) i_number;
6605
    break;
6606
  case 2:
6607
#ifdef WORDS_BIGENDIAN
6608
    if (low_byte_first)
6609
    {
6610
      int2store(i_ptr,(unsigned short) i_number);
6611
    }
6612
    else
6613
#endif
6614
      shortstore(i_ptr,(unsigned short) i_number);
6615
    break;
6616
  case 3:
6617
    int3store(i_ptr,i_number);
6618
    break;
6619
  case 4:
6620
#ifdef WORDS_BIGENDIAN
6621
    if (low_byte_first)
6622
    {
6623
      int4store(i_ptr,i_number);
6624
    }
6625
    else
6626
#endif
6627
      longstore(i_ptr,i_number);
6628
  }
6629
}
6630
6631
6632
uint32 Field_blob::get_length(const uchar *pos, uint packlength_arg, bool low_byte_first)
6633
{
6634
  switch (packlength_arg) {
6635
  case 1:
6636
    return (uint32) pos[0];
6637
  case 2:
6638
    {
6639
      uint16 tmp;
6640
#ifdef WORDS_BIGENDIAN
6641
      if (low_byte_first)
6642
	tmp=sint2korr(pos);
6643
      else
6644
#endif
6645
	shortget(tmp,pos);
6646
      return (uint32) tmp;
6647
    }
6648
  case 3:
6649
    return (uint32) uint3korr(pos);
6650
  case 4:
6651
    {
6652
      uint32 tmp;
6653
#ifdef WORDS_BIGENDIAN
6654
      if (low_byte_first)
6655
	tmp=uint4korr(pos);
6656
      else
6657
#endif
6658
	longget(tmp,pos);
6659
      return (uint32) tmp;
6660
    }
6661
  }
6662
  return 0;					// Impossible
6663
}
6664
6665
6666
/**
6667
  Put a blob length field into a record buffer.
6668
6669
  Depending on the maximum length of a blob, its length field is
6670
  put into 1 to 4 bytes. This is a property of the blob object,
6671
  described by 'packlength'.
6672
6673
  @param pos                 Pointer into the record buffer.
6674
  @param length              The length value to put.
6675
*/
6676
6677
void Field_blob::put_length(uchar *pos, uint32 length)
6678
{
6679
  switch (packlength) {
6680
  case 1:
6681
    *pos= (char) length;
6682
    break;
6683
  case 2:
6684
    int2store(pos, length);
6685
    break;
6686
  case 3:
6687
    int3store(pos, length);
6688
    break;
6689
  case 4:
6690
    int4store(pos, length);
6691
    break;
6692
  }
6693
}
6694
6695
6696
int Field_blob::store(const char *from,uint length,CHARSET_INFO *cs)
6697
{
6698
  ASSERT_COLUMN_MARKED_FOR_WRITE;
6699
  uint copy_length, new_length;
6700
  const char *well_formed_error_pos;
6701
  const char *cannot_convert_error_pos;
6702
  const char *from_end_pos, *tmp;
6703
  char buff[STRING_BUFFER_USUAL_SIZE];
6704
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
6705
6706
  if (!length)
6707
  {
6708
    bzero(ptr,Field_blob::pack_length());
6709
    return 0;
6710
  }
6711
6712
  if (from == value.ptr())
6713
  {
6714
    uint32 dummy_offset;
6715
    if (!String::needs_conversion(length, cs, field_charset, &dummy_offset))
6716
    {
6717
      Field_blob::store_length(length);
6718
      bmove(ptr+packlength,(char*) &from,sizeof(char*));
6719
      return 0;
6720
    }
6721
    if (tmpstr.copy(from, length, cs))
6722
      goto oom_error;
6723
    from= tmpstr.ptr();
6724
  }
6725
6726
  new_length= min(max_data_length(), field_charset->mbmaxlen * length);
6727
  if (value.alloc(new_length))
6728
    goto oom_error;
6729
6730
6731
  if (f_is_hex_escape(flags))
6732
  {
6733
    copy_length= my_copy_with_hex_escaping(field_charset,
6734
                                           (char*) value.ptr(), new_length,
6735
                                            from, length);
6736
    Field_blob::store_length(copy_length);
6737
    tmp= value.ptr();
6738
    bmove(ptr + packlength, (uchar*) &tmp, sizeof(char*));
6739
    return 0;
6740
  }
6741
  /*
6742
    "length" is OK as "nchars" argument to well_formed_copy_nchars as this
6743
    is never used to limit the length of the data. The cut of long data
6744
    is done with the new_length value.
6745
  */
6746
  copy_length= well_formed_copy_nchars(field_charset,
6747
                                       (char*) value.ptr(), new_length,
6748
                                       cs, from, length,
6749
                                       length,
6750
                                       &well_formed_error_pos,
6751
                                       &cannot_convert_error_pos,
6752
                                       &from_end_pos);
6753
6754
  Field_blob::store_length(copy_length);
6755
  tmp= value.ptr();
6756
  bmove(ptr+packlength,(uchar*) &tmp,sizeof(char*));
6757
6758
  if (check_string_copy_error(this, well_formed_error_pos,
6759
                              cannot_convert_error_pos, from + length, cs))
6760
    return 2;
6761
6762
  return report_if_important_data(from_end_pos, from + length);
6763
6764
oom_error:
6765
  /* Fatal OOM error */
6766
  bzero(ptr,Field_blob::pack_length());
6767
  return -1; 
6768
}
6769
6770
6771
int Field_blob::store(double nr)
6772
{
6773
  CHARSET_INFO *cs=charset();
6774
  value.set_real(nr, NOT_FIXED_DEC, cs);
6775
  return Field_blob::store(value.ptr(),(uint) value.length(), cs);
6776
}
6777
6778
6779
int Field_blob::store(longlong nr, bool unsigned_val)
6780
{
6781
  CHARSET_INFO *cs=charset();
6782
  value.set_int(nr, unsigned_val, cs);
6783
  return Field_blob::store(value.ptr(), (uint) value.length(), cs);
6784
}
6785
6786
6787
double Field_blob::val_real(void)
6788
{
6789
  ASSERT_COLUMN_MARKED_FOR_READ;
6790
  int not_used;
6791
  char *end_not_used, *blob;
6792
  uint32 length;
6793
  CHARSET_INFO *cs;
6794
6795
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
6796
  if (!blob)
6797
    return 0.0;
6798
  length= get_length(ptr);
6799
  cs= charset();
6800
  return my_strntod(cs, blob, length, &end_not_used, &not_used);
6801
}
6802
6803
6804
longlong Field_blob::val_int(void)
6805
{
6806
  ASSERT_COLUMN_MARKED_FOR_READ;
6807
  int not_used;
6808
  char *blob;
6809
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
6810
  if (!blob)
6811
    return 0;
6812
  uint32 length=get_length(ptr);
6813
  return my_strntoll(charset(),blob,length,10,NULL,&not_used);
6814
}
6815
6816
String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
6817
			    String *val_ptr)
6818
{
6819
  ASSERT_COLUMN_MARKED_FOR_READ;
6820
  char *blob;
6821
  memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
6822
  if (!blob)
6823
    val_ptr->set("",0,charset());	// A bit safer than ->length(0)
6824
  else
6825
    val_ptr->set((const char*) blob,get_length(ptr),charset());
6826
  return val_ptr;
6827
}
6828
6829
6830
my_decimal *Field_blob::val_decimal(my_decimal *decimal_value)
6831
{
6832
  ASSERT_COLUMN_MARKED_FOR_READ;
6833
  const char *blob;
6834
  size_t length;
6835
  memcpy_fixed(&blob, ptr+packlength, sizeof(const uchar*));
6836
  if (!blob)
6837
  {
6838
    blob= "";
6839
    length= 0;
6840
  }
6841
  else
6842
    length= get_length(ptr);
6843
6844
  str2my_decimal(E_DEC_FATAL_ERROR, blob, length, charset(),
6845
                 decimal_value);
6846
  return decimal_value;
6847
}
6848
6849
6850
int Field_blob::cmp(const uchar *a,uint32 a_length, const uchar *b,
6851
		    uint32 b_length)
6852
{
6853
  return field_charset->coll->strnncollsp(field_charset, 
6854
                                          a, a_length, b, b_length,
6855
                                          0);
6856
}
6857
6858
6859
int Field_blob::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
6860
                        uint max_length)
6861
{
6862
  uchar *blob1,*blob2;
6863
  memcpy_fixed(&blob1,a_ptr+packlength,sizeof(char*));
6864
  memcpy_fixed(&blob2,b_ptr+packlength,sizeof(char*));
6865
  uint a_len= get_length(a_ptr), b_len= get_length(b_ptr);
6866
  set_if_smaller(a_len, max_length);
6867
  set_if_smaller(b_len, max_length);
6868
  return Field_blob::cmp(blob1,a_len,blob2,b_len);
6869
}
6870
6871
6872
int Field_blob::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
6873
			   uint32 max_length)
6874
{
6875
  char *a,*b;
6876
  uint diff;
6877
  uint32 a_length,b_length;
6878
  memcpy_fixed(&a,a_ptr+packlength,sizeof(char*));
6879
  memcpy_fixed(&b,b_ptr+packlength,sizeof(char*));
6880
  a_length=get_length(a_ptr);
6881
  if (a_length > max_length)
6882
    a_length=max_length;
6883
  b_length=get_length(b_ptr);
6884
  if (b_length > max_length)
6885
    b_length=max_length;
6886
  diff=memcmp(a,b,min(a_length,b_length));
6887
  return diff ? diff : (int) (a_length - b_length);
6888
}
6889
6890
6891
/* The following is used only when comparing a key */
6892
6893
uint Field_blob::get_key_image(uchar *buff,uint length, imagetype type_arg)
6894
{
6895
  uint32 blob_length= get_length(ptr);
6896
  uchar *blob;
6897
6898
  get_ptr(&blob);
6899
  uint local_char_length= length / field_charset->mbmaxlen;
6900
  local_char_length= my_charpos(field_charset, blob, blob + blob_length,
6901
                          local_char_length);
6902
  set_if_smaller(blob_length, local_char_length);
6903
6904
  if ((uint32) length > blob_length)
6905
  {
6906
    /*
6907
      Must clear this as we do a memcmp in opt_range.cc to detect
6908
      identical keys
6909
    */
6910
    bzero(buff+HA_KEY_BLOB_LENGTH+blob_length, (length-blob_length));
6911
    length=(uint) blob_length;
6912
  }
6913
  int2store(buff,length);
6914
  memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
6915
  return HA_KEY_BLOB_LENGTH+length;
6916
}
6917
6918
6919
void Field_blob::set_key_image(const uchar *buff,uint length)
6920
{
6921
  length= uint2korr(buff);
6922
  (void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
6923
                           field_charset);
6924
}
6925
6926
6927
int Field_blob::key_cmp(const uchar *key_ptr, uint max_key_length)
6928
{
6929
  uchar *blob1;
6930
  uint blob_length=get_length(ptr);
6931
  memcpy_fixed(&blob1,ptr+packlength,sizeof(char*));
6932
  CHARSET_INFO *cs= charset();
6933
  uint local_char_length= max_key_length / cs->mbmaxlen;
6934
  local_char_length= my_charpos(cs, blob1, blob1+blob_length,
6935
                                local_char_length);
6936
  set_if_smaller(blob_length, local_char_length);
6937
  return Field_blob::cmp(blob1, blob_length,
6938
			 key_ptr+HA_KEY_BLOB_LENGTH,
6939
			 uint2korr(key_ptr));
6940
}
6941
6942
int Field_blob::key_cmp(const uchar *a,const uchar *b)
6943
{
6944
  return Field_blob::cmp(a+HA_KEY_BLOB_LENGTH, uint2korr(a),
6945
			 b+HA_KEY_BLOB_LENGTH, uint2korr(b));
6946
}
6947
6948
6949
/**
6950
   Save the field metadata for blob fields.
6951
6952
   Saves the pack length in the first byte of the field metadata array
6953
   at index of *metadata_ptr.
6954
6955
   @param   metadata_ptr   First byte of field metadata
6956
6957
   @returns number of bytes written to metadata_ptr
6958
*/
6959
int Field_blob::do_save_field_metadata(uchar *metadata_ptr)
6960
{
6961
  *metadata_ptr= pack_length_no_ptr();
6962
  return 1;
6963
}
6964
6965
6966
uint32 Field_blob::sort_length() const
6967
{
6968
  return (uint32) (current_thd->variables.max_sort_length + 
6969
                   (field_charset == &my_charset_bin ? 0 : packlength));
6970
}
6971
6972
6973
void Field_blob::sort_string(uchar *to,uint length)
6974
{
6975
  uchar *blob;
6976
  uint blob_length=get_length();
6977
6978
  if (!blob_length)
6979
    bzero(to,length);
6980
  else
6981
  {
6982
    if (field_charset == &my_charset_bin)
6983
    {
6984
      uchar *pos;
6985
6986
      /*
6987
        Store length of blob last in blob to shorter blobs before longer blobs
6988
      */
6989
      length-= packlength;
6990
      pos= to+length;
6991
6992
      switch (packlength) {
6993
      case 1:
6994
        *pos= (char) blob_length;
6995
        break;
6996
      case 2:
6997
        mi_int2store(pos, blob_length);
6998
        break;
6999
      case 3:
7000
        mi_int3store(pos, blob_length);
7001
        break;
7002
      case 4:
7003
        mi_int4store(pos, blob_length);
7004
        break;
7005
      }
7006
    }
7007
    memcpy_fixed(&blob,ptr+packlength,sizeof(char*));
7008
    
7009
    blob_length=my_strnxfrm(field_charset,
7010
                            to, length, blob, blob_length);
7011
    DBUG_ASSERT(blob_length == length);
7012
  }
7013
}
7014
7015
7016
void Field_blob::sql_type(String &res) const
7017
{
7018
  const char *str;
7019
  uint length;
7020
  switch (packlength) {
7021
  default: str="tiny"; length=4; break;
7022
  case 2:  str="";     length=0; break;
7023
  case 3:  str="medium"; length= 6; break;
7024
  case 4:  str="long";  length=4; break;
7025
  }
7026
  res.set_ascii(str,length);
7027
  if (charset() == &my_charset_bin)
7028
    res.append(STRING_WITH_LEN("blob"));
7029
  else
7030
  {
7031
    res.append(STRING_WITH_LEN("text"));
7032
  }
7033
}
7034
7035
uchar *Field_blob::pack(uchar *to, const uchar *from,
7036
                        uint max_length, bool low_byte_first)
7037
{
7038
  DBUG_ENTER("Field_blob::pack");
7039
  DBUG_PRINT("enter", ("to: 0x%lx; from: 0x%lx;"
7040
                       " max_length: %u; low_byte_first: %d",
7041
                       (ulong) to, (ulong) from,
7042
                       max_length, low_byte_first));
7043
  DBUG_DUMP("record", from, table->s->reclength);
7044
  uchar *save= ptr;
7045
  ptr= (uchar*) from;
7046
  uint32 length=get_length();			// Length of from string
7047
7048
  /*
7049
    Store max length, which will occupy packlength bytes. If the max
7050
    length given is smaller than the actual length of the blob, we
7051
    just store the initial bytes of the blob.
7052
  */
7053
  store_length(to, packlength, min(length, max_length), low_byte_first);
7054
7055
  /*
7056
    Store the actual blob data, which will occupy 'length' bytes.
7057
   */
7058
  if (length > 0)
7059
  {
7060
    get_ptr((uchar**) &from);
7061
    memcpy(to+packlength, from,length);
7062
  }
7063
  ptr=save;					// Restore org row pointer
7064
  DBUG_DUMP("packed", to, packlength + length);
7065
  DBUG_RETURN(to+packlength+length);
7066
}
7067
7068
7069
/**
7070
   Unpack a blob field from row data.
7071
7072
   This method is used to unpack a blob field from a master whose size of 
7073
   the field is less than that of the slave. Note: This method is included
7074
   to satisfy inheritance rules, but is not needed for blob fields. It
7075
   simply is used as a pass-through to the original unpack() method for
7076
   blob fields.
7077
7078
   @param   to         Destination of the data
7079
   @param   from       Source of the data
7080
   @param   param_data @c TRUE if base types should be stored in little-
7081
                       endian format, @c FALSE if native format should
7082
                       be used.
7083
7084
   @return  New pointer into memory based on from + length of the data
7085
*/
7086
const uchar *Field_blob::unpack(uchar *to, 
7087
                                const uchar *from,
7088
                                uint param_data,
7089
                                bool low_byte_first)
7090
{
7091
  DBUG_ENTER("Field_blob::unpack");
7092
  DBUG_PRINT("enter", ("to: 0x%lx; from: 0x%lx;"
7093
                       " param_data: %u; low_byte_first: %d",
7094
                       (ulong) to, (ulong) from, param_data, low_byte_first));
7095
  uint const master_packlength=
7096
    param_data > 0 ? param_data & 0xFF : packlength;
7097
  uint32 const length= get_length(from, master_packlength, low_byte_first);
7098
  DBUG_DUMP("packed", from, length + master_packlength);
7099
  bitmap_set_bit(table->write_set, field_index);
7100
  store(reinterpret_cast<const char*>(from) + master_packlength,
7101
        length, field_charset);
7102
  DBUG_DUMP("record", to, table->s->reclength);
7103
  DBUG_RETURN(from + master_packlength + length);
7104
}
7105
7106
/* Keys for blobs are like keys on varchars */
7107
7108
int Field_blob::pack_cmp(const uchar *a, const uchar *b, uint key_length_arg,
7109
                         my_bool insert_or_update)
7110
{
7111
  uint a_length, b_length;
7112
  if (key_length_arg > 255)
7113
  {
7114
    a_length=uint2korr(a); a+=2;
7115
    b_length=uint2korr(b); b+=2;
7116
  }
7117
  else
7118
  {
7119
    a_length= (uint) *a++;
7120
    b_length= (uint) *b++;
7121
  }
7122
  return field_charset->coll->strnncollsp(field_charset,
7123
                                          a, a_length,
7124
                                          b, b_length,
7125
                                          insert_or_update);
7126
}
7127
7128
7129
int Field_blob::pack_cmp(const uchar *b, uint key_length_arg,
7130
                         my_bool insert_or_update)
7131
{
7132
  uchar *a;
7133
  uint a_length, b_length;
7134
  memcpy_fixed(&a,ptr+packlength,sizeof(char*));
7135
  if (!a)
7136
    return key_length_arg > 0 ? -1 : 0;
7137
7138
  a_length= get_length(ptr);
7139
  if (key_length_arg > 255)
7140
  {
7141
    b_length= uint2korr(b); b+=2;
7142
  }
7143
  else
7144
    b_length= (uint) *b++;
7145
  return field_charset->coll->strnncollsp(field_charset,
7146
                                          a, a_length,
7147
                                          b, b_length,
7148
                                          insert_or_update);
7149
}
7150
7151
/** Create a packed key that will be used for storage from a MySQL row. */
7152
7153
uchar *
7154
Field_blob::pack_key(uchar *to, const uchar *from, uint max_length,
7155
                     bool low_byte_first __attribute__((unused)))
7156
{
7157
  uchar *save= ptr;
7158
  ptr= (uchar*) from;
7159
  uint32 length=get_length();        // Length of from string
7160
  uint local_char_length= ((field_charset->mbmaxlen > 1) ?
7161
                           max_length/field_charset->mbmaxlen : max_length);
7162
  if (length)
7163
    get_ptr((uchar**) &from);
7164
  if (length > local_char_length)
7165
    local_char_length= my_charpos(field_charset, from, from+length,
7166
                                  local_char_length);
7167
  set_if_smaller(length, local_char_length);
7168
  *to++= (uchar) length;
7169
  if (max_length > 255)				// 2 byte length
7170
    *to++= (uchar) (length >> 8);
7171
  memcpy(to, from, length);
7172
  ptr=save;					// Restore org row pointer
7173
  return to+length;
7174
}
7175
7176
7177
/**
7178
  Unpack a blob key into a record buffer.
7179
7180
  A blob key has a maximum size of 64K-1.
7181
  In its packed form, the length field is one or two bytes long,
7182
  depending on 'max_length'.
7183
  Depending on the maximum length of a blob, its length field is
7184
  put into 1 to 4 bytes. This is a property of the blob object,
7185
  described by 'packlength'.
7186
  Blobs are internally stored apart from the record buffer, which
7187
  contains a pointer to the blob buffer.
7188
7189
7190
  @param to                          Pointer into the record buffer.
7191
  @param from                        Pointer to the packed key.
7192
  @param max_length                  Key length limit from key description.
7193
7194
  @return
7195
    Pointer into 'from' past the last byte copied from packed key.
7196
*/
7197
7198
const uchar *
7199
Field_blob::unpack_key(uchar *to, const uchar *from, uint max_length,
7200
                       bool low_byte_first __attribute__((unused)))
7201
{
7202
  /* get length of the blob key */
7203
  uint32 length= *from++;
7204
  if (max_length > 255)
7205
    length+= *from++ << 8;
7206
7207
  /* put the length into the record buffer */
7208
  put_length(to, length);
7209
7210
  /* put the address of the blob buffer or NULL */
7211
  if (length)
7212
    memcpy_fixed(to + packlength, &from, sizeof(from));
7213
  else
7214
    bzero(to + packlength, sizeof(from));
7215
7216
  /* point to first byte of next field in 'from' */
7217
  return from + length;
7218
}
7219
7220
7221
/** Create a packed key that will be used for storage from a MySQL key. */
7222
7223
uchar *
7224
Field_blob::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
7225
                                    bool low_byte_first __attribute__((unused)))
7226
{
7227
  uint length=uint2korr(from);
7228
  if (length > max_length)
7229
    length=max_length;
7230
  *to++= (char) (length & 255);
7231
  if (max_length > 255)
7232
    *to++= (char) (length >> 8);
7233
  if (length)
7234
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
7235
  return to+length;
7236
}
7237
7238
7239
uint Field_blob::packed_col_length(const uchar *data_ptr, uint length)
7240
{
7241
  if (length > 255)
7242
    return uint2korr(data_ptr)+2;
7243
  return (uint) *data_ptr + 1;
7244
}
7245
7246
7247
uint Field_blob::max_packed_col_length(uint max_length)
7248
{
7249
  return (max_length > 255 ? 2 : 1)+max_length;
7250
}
7251
7252
7253
uint Field_blob::is_equal(Create_field *new_field)
7254
{
7255
  if (compare_str_field_flags(new_field, flags))
7256
    return 0;
7257
7258
  return ((new_field->sql_type == get_blob_type_from_length(max_data_length()))
7259
          && new_field->charset == field_charset &&
7260
          ((Field_blob *)new_field->field)->max_data_length() ==
7261
          max_data_length());
7262
}
7263
7264
7265
/****************************************************************************
7266
** enum type.
7267
** This is a string which only can have a selection of different values.
7268
** If one uses this string in a number context one gets the type number.
7269
****************************************************************************/
7270
7271
enum ha_base_keytype Field_enum::key_type() const
7272
{
7273
  switch (packlength) {
7274
  default: return HA_KEYTYPE_BINARY;
7275
  case 2: return HA_KEYTYPE_USHORT_INT;
7276
  case 3: return HA_KEYTYPE_UINT24;
7277
  case 4: return HA_KEYTYPE_ULONG_INT;
7278
  case 8: return HA_KEYTYPE_ULONGLONG;
7279
  }
7280
}
7281
7282
void Field_enum::store_type(uint64_t value)
7283
{
7284
  switch (packlength) {
7285
  case 1: ptr[0]= (uchar) value;  break;
7286
  case 2:
7287
#ifdef WORDS_BIGENDIAN
7288
  if (table->s->db_low_byte_first)
7289
  {
7290
    int2store(ptr,(unsigned short) value);
7291
  }
7292
  else
7293
#endif
7294
    shortstore(ptr,(unsigned short) value);
7295
  break;
7296
  case 3: int3store(ptr,(long) value); break;
7297
  case 4:
7298
#ifdef WORDS_BIGENDIAN
7299
  if (table->s->db_low_byte_first)
7300
  {
7301
    int4store(ptr,value);
7302
  }
7303
  else
7304
#endif
7305
    longstore(ptr,(long) value);
7306
  break;
7307
  case 8:
7308
#ifdef WORDS_BIGENDIAN
7309
  if (table->s->db_low_byte_first)
7310
  {
7311
    int8store(ptr,value);
7312
  }
7313
  else
7314
#endif
7315
    longlongstore(ptr,value); break;
7316
  }
7317
}
7318
7319
7320
/**
7321
  @note
7322
    Storing a empty string in a enum field gives a warning
7323
    (if there isn't a empty value in the enum)
7324
*/
7325
7326
int Field_enum::store(const char *from,uint length,CHARSET_INFO *cs)
7327
{
7328
  ASSERT_COLUMN_MARKED_FOR_WRITE;
7329
  int err= 0;
7330
  uint32 not_used;
7331
  char buff[STRING_BUFFER_USUAL_SIZE];
7332
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
7333
7334
  /* Convert character set if necessary */
7335
  if (String::needs_conversion(length, cs, field_charset, &not_used))
7336
  { 
7337
    uint dummy_errors;
7338
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
7339
    from= tmpstr.ptr();
7340
    length=  tmpstr.length();
7341
  }
7342
7343
  /* Remove end space */
7344
  length= field_charset->cset->lengthsp(field_charset, from, length);
7345
  uint tmp=find_type2(typelib, from, length, field_charset);
7346
  if (!tmp)
7347
  {
7348
    if (length < 6) // Can't be more than 99999 enums
7349
    {
7350
      /* This is for reading numbers with LOAD DATA INFILE */
7351
      char *end;
7352
      tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
7353
      if (err || end != from+length || tmp > typelib->count)
7354
      {
7355
	tmp=0;
7356
	set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
7357
      }
7358
      if (!table->in_use->count_cuted_fields)
7359
        err= 0;
7360
    }
7361
    else
7362
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
7363
  }
7364
  store_type((uint64_t) tmp);
7365
  return err;
7366
}
7367
7368
7369
int Field_enum::store(double nr)
7370
{
7371
  return Field_enum::store((longlong) nr, FALSE);
7372
}
7373
7374
7375
int Field_enum::store(longlong nr, bool unsigned_val)
7376
{
7377
  ASSERT_COLUMN_MARKED_FOR_WRITE;
7378
  int error= 0;
7379
  if ((uint64_t) nr > typelib->count || nr == 0)
7380
  {
7381
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
7382
    if (nr != 0 || table->in_use->count_cuted_fields)
7383
    {
7384
      nr= 0;
7385
      error= 1;
7386
    }
7387
  }
7388
  store_type((uint64_t) (uint) nr);
7389
  return error;
7390
}
7391
7392
7393
double Field_enum::val_real(void)
7394
{
7395
  return (double) Field_enum::val_int();
7396
}
7397
7398
7399
longlong Field_enum::val_int(void)
7400
{
7401
  ASSERT_COLUMN_MARKED_FOR_READ;
7402
  switch (packlength) {
7403
  case 1:
7404
    return (longlong) ptr[0];
7405
  case 2:
7406
  {
7407
    uint16 tmp;
7408
#ifdef WORDS_BIGENDIAN
7409
    if (table->s->db_low_byte_first)
7410
      tmp=sint2korr(ptr);
7411
    else
7412
#endif
7413
      shortget(tmp,ptr);
7414
    return (longlong) tmp;
7415
  }
7416
  case 3:
7417
    return (longlong) uint3korr(ptr);
7418
  case 4:
7419
  {
7420
    uint32 tmp;
7421
#ifdef WORDS_BIGENDIAN
7422
    if (table->s->db_low_byte_first)
7423
      tmp=uint4korr(ptr);
7424
    else
7425
#endif
7426
      longget(tmp,ptr);
7427
    return (longlong) tmp;
7428
  }
7429
  case 8:
7430
  {
7431
    longlong tmp;
7432
#ifdef WORDS_BIGENDIAN
7433
    if (table->s->db_low_byte_first)
7434
      tmp=sint8korr(ptr);
7435
    else
7436
#endif
7437
      longlongget(tmp,ptr);
7438
    return tmp;
7439
  }
7440
  }
7441
  return 0;					// impossible
7442
}
7443
7444
7445
/**
7446
   Save the field metadata for enum fields.
7447
7448
   Saves the real type in the first byte and the pack length in the 
7449
   second byte of the field metadata array at index of *metadata_ptr and
7450
   *(metadata_ptr + 1).
7451
7452
   @param   metadata_ptr   First byte of field metadata
7453
7454
   @returns number of bytes written to metadata_ptr
7455
*/
7456
int Field_enum::do_save_field_metadata(uchar *metadata_ptr)
7457
{
7458
  *metadata_ptr= real_type();
7459
  *(metadata_ptr + 1)= pack_length();
7460
  return 2;
7461
}
7462
7463
7464
String *Field_enum::val_str(String *val_buffer __attribute__((unused)),
7465
			    String *val_ptr)
7466
{
7467
  uint tmp=(uint) Field_enum::val_int();
7468
  if (!tmp || tmp > typelib->count)
7469
    val_ptr->set("", 0, field_charset);
7470
  else
7471
    val_ptr->set((const char*) typelib->type_names[tmp-1],
7472
		 typelib->type_lengths[tmp-1],
7473
		 field_charset);
7474
  return val_ptr;
7475
}
7476
7477
int Field_enum::cmp(const uchar *a_ptr, const uchar *b_ptr)
7478
{
7479
  uchar *old= ptr;
7480
  ptr= (uchar*) a_ptr;
7481
  uint64_t a=Field_enum::val_int();
7482
  ptr= (uchar*) b_ptr;
7483
  uint64_t b=Field_enum::val_int();
7484
  ptr= old;
7485
  return (a < b) ? -1 : (a > b) ? 1 : 0;
7486
}
7487
7488
void Field_enum::sort_string(uchar *to,uint length __attribute__((unused)))
7489
{
7490
  uint64_t value=Field_enum::val_int();
7491
  to+=packlength-1;
7492
  for (uint i=0 ; i < packlength ; i++)
7493
  {
7494
    *to-- = (uchar) (value & 255);
7495
    value>>=8;
7496
  }
7497
}
7498
7499
7500
void Field_enum::sql_type(String &res) const
7501
{
7502
  char buffer[255];
7503
  String enum_item(buffer, sizeof(buffer), res.charset());
7504
7505
  res.length(0);
7506
  res.append(STRING_WITH_LEN("enum("));
7507
7508
  bool flag=0;
7509
  uint *len= typelib->type_lengths;
7510
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
7511
  {
7512
    uint dummy_errors;
7513
    if (flag)
7514
      res.append(',');
7515
    /* convert to res.charset() == utf8, then quote */
7516
    enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
7517
    append_unescaped(&res, enum_item.ptr(), enum_item.length());
7518
    flag= 1;
7519
  }
7520
  res.append(')');
7521
}
7522
7523
7524
Field *Field_enum::new_field(MEM_ROOT *root, struct st_table *new_table,
7525
                             bool keep_type)
7526
{
7527
  Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
7528
  if (res)
7529
    res->typelib= copy_typelib(root, typelib);
7530
  return res;
7531
}
7532
7533
7534
/*
7535
   set type.
7536
   This is a string which can have a collection of different values.
7537
   Each string value is separated with a ','.
7538
   For example "One,two,five"
7539
   If one uses this string in a number context one gets the bits as a longlong
7540
   number.
7541
*/
7542
7543
7544
int Field_set::store(const char *from,uint length,CHARSET_INFO *cs)
7545
{
7546
  ASSERT_COLUMN_MARKED_FOR_WRITE;
7547
  bool got_warning= 0;
7548
  int err= 0;
7549
  char *not_used;
7550
  uint not_used2;
7551
  uint32 not_used_offset;
7552
  char buff[STRING_BUFFER_USUAL_SIZE];
7553
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
7554
7555
  /* Convert character set if necessary */
7556
  if (String::needs_conversion(length, cs, field_charset, &not_used_offset))
7557
  { 
7558
    uint dummy_errors;
7559
    tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
7560
    from= tmpstr.ptr();
7561
    length=  tmpstr.length();
7562
  }
7563
  uint64_t tmp= find_set(typelib, from, length, field_charset,
7564
                          &not_used, &not_used2, &got_warning);
7565
  if (!tmp && length && length < 22)
7566
  {
7567
    /* This is for reading numbers with LOAD DATA INFILE */
7568
    char *end;
7569
    tmp=my_strntoull(cs,from,length,10,&end,&err);
7570
    if (err || end != from+length ||
7571
	tmp > (uint64_t) (((longlong) 1 << typelib->count) - (longlong) 1))
7572
    {
7573
      tmp=0;      
7574
      set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
7575
    }
7576
  }
7577
  else if (got_warning)
7578
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
7579
  store_type(tmp);
7580
  return err;
7581
}
7582
7583
7584
int Field_set::store(longlong nr, bool unsigned_val)
7585
{
7586
  ASSERT_COLUMN_MARKED_FOR_WRITE;
7587
  int error= 0;
7588
  uint64_t max_nr= set_bits(uint64_t, typelib->count);
7589
  if ((uint64_t) nr > max_nr)
7590
  {
7591
    nr&= max_nr;
7592
    set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
7593
    error=1;
7594
  }
7595
  store_type((uint64_t) nr);
7596
  return error;
7597
}
7598
7599
7600
String *Field_set::val_str(String *val_buffer,
7601
			   String *val_ptr __attribute__((unused)))
7602
{
7603
  uint64_t tmp=(uint64_t) Field_enum::val_int();
7604
  uint bitnr=0;
7605
7606
  val_buffer->length(0);
7607
  val_buffer->set_charset(field_charset);
7608
  while (tmp && bitnr < (uint) typelib->count)
7609
  {
7610
    if (tmp & 1)
7611
    {
7612
      if (val_buffer->length())
7613
	val_buffer->append(&field_separator, 1, &my_charset_latin1);
7614
      String str(typelib->type_names[bitnr],
7615
		 typelib->type_lengths[bitnr],
7616
		 field_charset);
7617
      val_buffer->append(str);
7618
    }
7619
    tmp>>=1;
7620
    bitnr++;
7621
  }
7622
  return val_buffer;
7623
}
7624
7625
7626
void Field_set::sql_type(String &res) const
7627
{
7628
  char buffer[255];
7629
  String set_item(buffer, sizeof(buffer), res.charset());
7630
7631
  res.length(0);
7632
  res.append(STRING_WITH_LEN("set("));
7633
7634
  bool flag=0;
7635
  uint *len= typelib->type_lengths;
7636
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
7637
  {
7638
    uint dummy_errors;
7639
    if (flag)
7640
      res.append(',');
7641
    /* convert to res.charset() == utf8, then quote */
7642
    set_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
7643
    append_unescaped(&res, set_item.ptr(), set_item.length());
7644
    flag= 1;
7645
  }
7646
  res.append(')');
7647
}
7648
7649
/**
7650
  @retval
7651
    1  if the fields are equally defined
7652
  @retval
7653
    0  if the fields are unequally defined
7654
*/
7655
7656
bool Field::eq_def(Field *field)
7657
{
7658
  if (real_type() != field->real_type() || charset() != field->charset() ||
7659
      pack_length() != field->pack_length())
7660
    return 0;
7661
  return 1;
7662
}
7663
7664
/**
7665
  @return
7666
  returns 1 if the fields are equally defined
7667
*/
7668
bool Field_enum::eq_def(Field *field)
7669
{
7670
  if (!Field::eq_def(field))
7671
    return 0;
7672
  TYPELIB *from_lib=((Field_enum*) field)->typelib;
7673
7674
  if (typelib->count < from_lib->count)
7675
    return 0;
7676
  for (uint i=0 ; i < from_lib->count ; i++)
7677
    if (my_strnncoll(field_charset,
7678
                     (const uchar*)typelib->type_names[i],
7679
                     strlen(typelib->type_names[i]),
7680
                     (const uchar*)from_lib->type_names[i],
7681
                     strlen(from_lib->type_names[i])))
7682
      return 0;
7683
  return 1;
7684
}
7685
7686
/**
7687
  @return
7688
  returns 1 if the fields are equally defined
7689
*/
7690
bool Field_num::eq_def(Field *field)
7691
{
7692
  if (!Field::eq_def(field))
7693
    return 0;
7694
  Field_num *from_num= (Field_num*) field;
7695
7696
  if (unsigned_flag != from_num->unsigned_flag ||
7697
      (zerofill && !from_num->zerofill && !zero_pack()) ||
7698
      dec != from_num->dec)
7699
    return 0;
7700
  return 1;
7701
}
7702
7703
7704
uint Field_num::is_equal(Create_field *new_field)
7705
{
7706
  return ((new_field->sql_type == real_type()) &&
7707
	  ((new_field->flags & UNSIGNED_FLAG) == (uint) (flags &
7708
							 UNSIGNED_FLAG)) &&
7709
	  ((new_field->flags & AUTO_INCREMENT_FLAG) ==
7710
	   (uint) (flags & AUTO_INCREMENT_FLAG)) &&
7711
	  (new_field->length <= max_display_length()));
7712
}
7713
7714
7715
/*****************************************************************************
7716
  Handling of field and Create_field
7717
*****************************************************************************/
7718
7719
/**
7720
  Convert create_field::length from number of characters to number of bytes.
7721
*/
7722
7723
void Create_field::create_length_to_internal_length(void)
7724
{
7725
  switch (sql_type) {
7726
  case MYSQL_TYPE_TINY_BLOB:
7727
  case MYSQL_TYPE_MEDIUM_BLOB:
7728
  case MYSQL_TYPE_LONG_BLOB:
7729
  case MYSQL_TYPE_BLOB:
7730
  case MYSQL_TYPE_VAR_STRING:
7731
  case MYSQL_TYPE_STRING:
7732
  case MYSQL_TYPE_VARCHAR:
7733
    length*= charset->mbmaxlen;
7734
    key_length= length;
7735
    pack_length= calc_pack_length(sql_type, length);
7736
    break;
7737
  case MYSQL_TYPE_ENUM:
7738
  case MYSQL_TYPE_SET:
7739
    /* Pack_length already calculated in sql_parse.cc */
7740
    length*= charset->mbmaxlen;
7741
    key_length= pack_length;
7742
    break;
7743
  case MYSQL_TYPE_NEWDECIMAL:
7744
    key_length= pack_length=
7745
      my_decimal_get_binary_size(my_decimal_length_to_precision(length,
7746
								decimals,
7747
								flags &
7748
								UNSIGNED_FLAG),
7749
				 decimals);
7750
    break;
7751
  default:
7752
    key_length= pack_length= calc_pack_length(sql_type, length);
7753
    break;
7754
  }
7755
}
7756
7757
7758
/**
7759
  Init for a tmp table field. To be extended if need be.
7760
*/
7761
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
7762
                                      uint32 length_arg, uint32 decimals_arg,
7763
                                      bool maybe_null, bool is_unsigned)
7764
{
7765
  field_name= "";
7766
  sql_type= sql_type_arg;
7767
  char_length= length= length_arg;;
7768
  unireg_check= Field::NONE;
7769
  interval= 0;
7770
  charset= &my_charset_bin;
7771
  pack_flag= (FIELDFLAG_NUMBER |
7772
              ((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
7773
              (maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
7774
              (is_unsigned ? 0 : FIELDFLAG_DECIMAL));
7775
}
7776
7777
7778
/**
7779
  Initialize field definition for create.
7780
7781
  @param thd                   Thread handle
7782
  @param fld_name              Field name
7783
  @param fld_type              Field type
7784
  @param fld_length            Field length
7785
  @param fld_decimals          Decimal (if any)
7786
  @param fld_type_modifier     Additional type information
7787
  @param fld_default_value     Field default value (if any)
7788
  @param fld_on_update_value   The value of ON UPDATE clause
7789
  @param fld_comment           Field comment
7790
  @param fld_change            Field change
7791
  @param fld_interval_list     Interval list (if any)
7792
  @param fld_charset           Field charset
7793
7794
  @retval
7795
    FALSE on success
7796
  @retval
7797
    TRUE  on error
7798
*/
7799
7800
bool Create_field::init(THD *thd, char *fld_name, enum_field_types fld_type,
7801
                        char *fld_length, char *fld_decimals,
7802
                        uint fld_type_modifier, Item *fld_default_value,
7803
                        Item *fld_on_update_value, LEX_STRING *fld_comment,
7804
                        char *fld_change, List<String> *fld_interval_list,
7805
                        CHARSET_INFO *fld_charset, uint fld_geom_type,
7806
                        enum ha_storage_media storage_type,
7807
                        enum column_format_type column_format)
7808
{
7809
  uint sign_len, allowed_type_modifier= 0;
7810
  ulong max_field_charlength= MAX_FIELD_CHARLENGTH;
7811
7812
  DBUG_ENTER("Create_field::init()");
7813
  
7814
  field= 0;
7815
  field_name= fld_name;
7816
  def= fld_default_value;
7817
  flags= fld_type_modifier;
7818
  flags|= (((uint)storage_type & STORAGE_TYPE_MASK) << FIELD_STORAGE_FLAGS);
7819
  flags|= (((uint)column_format & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
7820
  unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
7821
                 Field::NEXT_NUMBER : Field::NONE);
7822
  decimals= fld_decimals ? (uint)atoi(fld_decimals) : 0;
7823
  if (decimals >= NOT_FIXED_DEC)
7824
  {
7825
    my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
7826
             NOT_FIXED_DEC-1);
7827
    DBUG_RETURN(TRUE);
7828
  }
7829
7830
  sql_type= fld_type;
7831
  length= 0;
7832
  change= fld_change;
7833
  interval= 0;
7834
  pack_length= key_length= 0;
7835
  charset= fld_charset;
7836
  interval_list.empty();
7837
7838
  comment= *fld_comment;
7839
  /*
7840
    Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
7841
    it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
7842
  */
7843
  if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
7844
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != MYSQL_TYPE_TIMESTAMP)
7845
    flags|= NO_DEFAULT_VALUE_FLAG;
7846
7847
  if (fld_length && !(length= (uint) atoi(fld_length)))
7848
    fld_length= 0; /* purecov: inspected */
7849
  sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
7850
7851
  switch (fld_type) {
7852
  case MYSQL_TYPE_TINY:
7853
    if (!fld_length)
7854
      length= MAX_TINYINT_WIDTH+sign_len;
7855
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
7856
    break;
7857
  case MYSQL_TYPE_SHORT:
7858
    if (!fld_length)
7859
      length= MAX_SMALLINT_WIDTH+sign_len;
7860
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
7861
    break;
7862
  case MYSQL_TYPE_INT24:
7863
    if (!fld_length)
7864
      length= MAX_MEDIUMINT_WIDTH+sign_len;
7865
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
7866
    break;
7867
  case MYSQL_TYPE_LONG:
7868
    if (!fld_length)
7869
      length= MAX_INT_WIDTH+sign_len;
7870
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
7871
    break;
7872
  case MYSQL_TYPE_LONGLONG:
7873
    if (!fld_length)
7874
      length= MAX_BIGINT_WIDTH;
7875
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
7876
    break;
7877
  case MYSQL_TYPE_NULL:
7878
    break;
7879
  case MYSQL_TYPE_NEWDECIMAL:
7880
    my_decimal_trim(&length, &decimals);
7881
    if (length > DECIMAL_MAX_PRECISION)
7882
    {
7883
      my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
7884
               DECIMAL_MAX_PRECISION);
7885
      DBUG_RETURN(TRUE);
7886
    }
7887
    if (length < decimals)
7888
    {
7889
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
7890
      DBUG_RETURN(TRUE);
7891
    }
7892
    length=
7893
      my_decimal_precision_to_length(length, decimals,
7894
                                     fld_type_modifier & UNSIGNED_FLAG);
7895
    pack_length=
7896
      my_decimal_get_binary_size(length, decimals);
7897
    break;
7898
  case MYSQL_TYPE_VARCHAR:
7899
    /*
7900
      Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
7901
      if they don't have a default value
7902
    */
7903
    max_field_charlength= MAX_FIELD_VARCHARLENGTH;
7904
    break;
7905
  case MYSQL_TYPE_STRING:
7906
    break;
7907
  case MYSQL_TYPE_BLOB:
7908
  case MYSQL_TYPE_TINY_BLOB:
7909
  case MYSQL_TYPE_LONG_BLOB:
7910
  case MYSQL_TYPE_MEDIUM_BLOB:
7911
    if (fld_default_value)
7912
    {
7913
      /* Allow empty as default value. */
7914
      String str,*res;
7915
      res= fld_default_value->val_str(&str);
7916
      /*
7917
        A default other than '' is always an error, and any non-NULL
7918
        specified default is an error in strict mode.
7919
      */
7920
      if (res->length() || (thd->variables.sql_mode &
7921
                            (MODE_STRICT_TRANS_TABLES |
7922
                             MODE_STRICT_ALL_TABLES)))
7923
      {
7924
        my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
7925
                 fld_name); /* purecov: inspected */
7926
        DBUG_RETURN(TRUE);
7927
      }
7928
      else
7929
      {
7930
        /*
7931
          Otherwise a default of '' is just a warning.
7932
        */
7933
        push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
7934
                            ER_BLOB_CANT_HAVE_DEFAULT,
7935
                            ER(ER_BLOB_CANT_HAVE_DEFAULT),
7936
                            fld_name);
7937
      }
7938
      def= 0;
7939
    }
7940
    flags|= BLOB_FLAG;
7941
    break;
7942
  case MYSQL_TYPE_YEAR:
7943
    if (!fld_length || length != 2)
7944
      length= 4; /* Default length */
7945
    flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
7946
    break;
7947
  case MYSQL_TYPE_FLOAT:
7948
    /* change FLOAT(precision) to FLOAT or DOUBLE */
7949
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
7950
    if (fld_length && !fld_decimals)
7951
    {
7952
      uint tmp_length= length;
7953
      if (tmp_length > PRECISION_FOR_DOUBLE)
7954
      {
7955
        my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
7956
        DBUG_RETURN(TRUE);
7957
      }
7958
      else if (tmp_length > PRECISION_FOR_FLOAT)
7959
      {
7960
        sql_type= MYSQL_TYPE_DOUBLE;
7961
        length= DBL_DIG+7; /* -[digits].E+### */
7962
      }
7963
      else
7964
        length= FLT_DIG+6; /* -[digits].E+## */
7965
      decimals= NOT_FIXED_DEC;
7966
      break;
7967
    }
7968
    if (!fld_length && !fld_decimals)
7969
    {
7970
      length=  FLT_DIG+6;
7971
      decimals= NOT_FIXED_DEC;
7972
    }
7973
    if (length < decimals &&
7974
        decimals != NOT_FIXED_DEC)
7975
    {
7976
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
7977
      DBUG_RETURN(TRUE);
7978
    }
7979
    break;
7980
  case MYSQL_TYPE_DOUBLE:
7981
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
7982
    if (!fld_length && !fld_decimals)
7983
    {
7984
      length= DBL_DIG+7;
7985
      decimals= NOT_FIXED_DEC;
7986
    }
7987
    if (length < decimals &&
7988
        decimals != NOT_FIXED_DEC)
7989
    {
7990
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
7991
      DBUG_RETURN(TRUE);
7992
    }
7993
    break;
7994
  case MYSQL_TYPE_TIMESTAMP:
7995
    if (!fld_length)
7996
    {
7997
      /* Compressed date YYYYMMDDHHMMSS */
7998
      length= MAX_DATETIME_COMPRESSED_WIDTH;
7999
    }
8000
    else if (length != MAX_DATETIME_WIDTH)
8001
    {
8002
      /*
8003
        We support only even TIMESTAMP lengths less or equal than 14
8004
        and 19 as length of 4.1 compatible representation.
8005
      */
8006
      length= ((length+1)/2)*2; /* purecov: inspected */
8007
      length= min(length, MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
8008
    }
8009
    flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
8010
    if (fld_default_value)
8011
    {
8012
      /* Grammar allows only NOW() value for ON UPDATE clause */
8013
      if (fld_default_value->type() == Item::FUNC_ITEM && 
8014
          ((Item_func*)fld_default_value)->functype() == Item_func::NOW_FUNC)
8015
      {
8016
        unireg_check= (fld_on_update_value ? Field::TIMESTAMP_DNUN_FIELD:
8017
                                             Field::TIMESTAMP_DN_FIELD);
8018
        /*
8019
          We don't need default value any longer moreover it is dangerous.
8020
          Everything handled by unireg_check further.
8021
        */
8022
        def= 0;
8023
      }
8024
      else
8025
        unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
8026
                                             Field::NONE);
8027
    }
8028
    else
8029
    {
8030
      /*
8031
        If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
8032
        or ON UPDATE values then for the sake of compatiblity we should treat
8033
        this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
8034
        have another TIMESTAMP column with auto-set option before this one)
8035
        or DEFAULT 0 (in other cases).
8036
        So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
8037
        replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
8038
        information about all TIMESTAMP fields in table will be availiable.
8039
8040
        If we have TIMESTAMP NULL column without explicit DEFAULT value
8041
        we treat it as having DEFAULT NULL attribute.
8042
      */
8043
      unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD :
8044
                     (flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
8045
                                              Field::NONE));
8046
    }
8047
    break;
8048
  case MYSQL_TYPE_DATE:
8049
    /* Old date type. */
8050
    sql_type= MYSQL_TYPE_NEWDATE;
8051
    /* fall trough */
8052
  case MYSQL_TYPE_NEWDATE:
8053
    length= 10;
8054
    break;
8055
  case MYSQL_TYPE_TIME:
8056
    length= 10;
8057
    break;
8058
  case MYSQL_TYPE_DATETIME:
8059
    length= MAX_DATETIME_WIDTH;
8060
    break;
8061
  case MYSQL_TYPE_SET:
8062
    {
8063
      pack_length= get_set_pack_length(fld_interval_list->elements);
8064
8065
      List_iterator<String> it(*fld_interval_list);
8066
      String *tmp;
8067
      while ((tmp= it++))
8068
        interval_list.push_back(tmp);
8069
      /*
8070
        Set fake length to 1 to pass the below conditions.
8071
        Real length will be set in mysql_prepare_table()
8072
        when we know the character set of the column
8073
      */
8074
      length= 1;
8075
      break;
8076
    }
8077
  case MYSQL_TYPE_ENUM:
8078
    {
8079
      /* Should be safe. */
8080
      pack_length= get_enum_pack_length(fld_interval_list->elements);
8081
8082
      List_iterator<String> it(*fld_interval_list);
8083
      String *tmp;
8084
      while ((tmp= it++))
8085
        interval_list.push_back(tmp);
8086
      length= 1; /* See comment for MYSQL_TYPE_SET above. */
8087
      break;
8088
   }
8089
  case MYSQL_TYPE_VAR_STRING:
8090
    DBUG_ASSERT(0);  /* Impossible. */
8091
    break;
8092
  }
8093
  /* Remember the value of length */
8094
  char_length= length;
8095
8096
  if (!(flags & BLOB_FLAG) &&
8097
      ((length > max_field_charlength && fld_type != MYSQL_TYPE_SET &&
8098
        fld_type != MYSQL_TYPE_ENUM &&
8099
        (fld_type != MYSQL_TYPE_VARCHAR || fld_default_value)) ||
8100
       (!length &&
8101
        fld_type != MYSQL_TYPE_STRING &&
8102
        fld_type != MYSQL_TYPE_VARCHAR)))
8103
  {
8104
    my_error((fld_type == MYSQL_TYPE_VAR_STRING ||
8105
              fld_type == MYSQL_TYPE_VARCHAR ||
8106
              fld_type == MYSQL_TYPE_STRING) ?  ER_TOO_BIG_FIELDLENGTH :
8107
                                                ER_TOO_BIG_DISPLAYWIDTH,
8108
              MYF(0),
8109
              fld_name, max_field_charlength); /* purecov: inspected */
8110
    DBUG_RETURN(TRUE);
8111
  }
8112
  fld_type_modifier&= AUTO_INCREMENT_FLAG;
8113
  if ((~allowed_type_modifier) & fld_type_modifier)
8114
  {
8115
    my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
8116
    DBUG_RETURN(TRUE);
8117
  }
8118
8119
  DBUG_RETURN(FALSE); /* success */
8120
}
8121
8122
8123
enum_field_types get_blob_type_from_length(ulong length)
8124
{
8125
  enum_field_types type;
8126
  if (length < 256)
8127
    type= MYSQL_TYPE_TINY_BLOB;
8128
  else if (length < 65536)
8129
    type= MYSQL_TYPE_BLOB;
8130
  else if (length < 256L*256L*256L)
8131
    type= MYSQL_TYPE_MEDIUM_BLOB;
8132
  else
8133
    type= MYSQL_TYPE_LONG_BLOB;
8134
  return type;
8135
}
8136
8137
8138
/*
8139
  Make a field from the .frm file info
8140
*/
8141
8142
uint32 calc_pack_length(enum_field_types type,uint32 length)
8143
{
8144
  switch (type) {
8145
  case MYSQL_TYPE_VAR_STRING:
8146
  case MYSQL_TYPE_STRING:
8147
  case MYSQL_TYPE_VARCHAR:     return (length + (length < 256 ? 1: 2));
8148
  case MYSQL_TYPE_YEAR:
8149
  case MYSQL_TYPE_TINY	: return 1;
8150
  case MYSQL_TYPE_SHORT : return 2;
8151
  case MYSQL_TYPE_INT24:
8152
  case MYSQL_TYPE_NEWDATE:
8153
  case MYSQL_TYPE_TIME:   return 3;
8154
  case MYSQL_TYPE_TIMESTAMP:
8155
  case MYSQL_TYPE_DATE:
8156
  case MYSQL_TYPE_LONG	: return 4;
8157
  case MYSQL_TYPE_FLOAT : return sizeof(float);
8158
  case MYSQL_TYPE_DOUBLE: return sizeof(double);
8159
  case MYSQL_TYPE_DATETIME:
8160
  case MYSQL_TYPE_LONGLONG: return 8;	/* Don't crash if no longlong */
8161
  case MYSQL_TYPE_NULL	: return 0;
8162
  case MYSQL_TYPE_TINY_BLOB:	return 1+portable_sizeof_char_ptr;
8163
  case MYSQL_TYPE_BLOB:		return 2+portable_sizeof_char_ptr;
8164
  case MYSQL_TYPE_MEDIUM_BLOB:	return 3+portable_sizeof_char_ptr;
8165
  case MYSQL_TYPE_LONG_BLOB:	return 4+portable_sizeof_char_ptr;
8166
  case MYSQL_TYPE_SET:
8167
  case MYSQL_TYPE_ENUM:
8168
  case MYSQL_TYPE_NEWDECIMAL:
8169
    abort(); return 0;                          // This shouldn't happen
8170
  default:
8171
    return 0;
8172
  }
8173
}
8174
8175
8176
uint pack_length_to_packflag(uint type)
8177
{
8178
  switch (type) {
8179
    case 1: return f_settype((uint) MYSQL_TYPE_TINY);
8180
    case 2: return f_settype((uint) MYSQL_TYPE_SHORT);
8181
    case 3: return f_settype((uint) MYSQL_TYPE_INT24);
8182
    case 4: return f_settype((uint) MYSQL_TYPE_LONG);
8183
    case 8: return f_settype((uint) MYSQL_TYPE_LONGLONG);
8184
  }
8185
  return 0;					// This shouldn't happen
8186
}
8187
8188
8189
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
8190
		  uchar *null_pos, uchar null_bit,
8191
		  uint pack_flag,
8192
		  enum_field_types field_type,
8193
		  CHARSET_INFO *field_charset,
8194
		  Field::utype unireg_check,
8195
		  TYPELIB *interval,
8196
		  const char *field_name)
8197
{
8198
  if (!f_maybe_null(pack_flag))
8199
  {
8200
    null_pos=0;
8201
    null_bit=0;
8202
  }
8203
  else
8204
  {
8205
    null_bit= ((uchar) 1) << null_bit;
8206
  }
8207
8208
  switch (field_type) {
8209
  case MYSQL_TYPE_DATE:
8210
  case MYSQL_TYPE_NEWDATE:
8211
  case MYSQL_TYPE_TIME:
8212
  case MYSQL_TYPE_DATETIME:
8213
  case MYSQL_TYPE_TIMESTAMP:
8214
    field_charset= &my_charset_bin;
8215
  default: break;
8216
  }
8217
8218
  if (f_is_alpha(pack_flag))
8219
  {
8220
    if (!f_is_packed(pack_flag))
8221
    {
8222
      if (field_type == MYSQL_TYPE_STRING ||
8223
          field_type == MYSQL_TYPE_VAR_STRING)
8224
        return new Field_string(ptr,field_length,null_pos,null_bit,
8225
                                unireg_check, field_name,
8226
                                field_charset);
8227
      if (field_type == MYSQL_TYPE_VARCHAR)
8228
        return new Field_varstring(ptr,field_length,
8229
                                   HA_VARCHAR_PACKLENGTH(field_length),
8230
                                   null_pos,null_bit,
8231
                                   unireg_check, field_name,
8232
                                   share,
8233
                                   field_charset);
8234
      return 0;                                 // Error
8235
    }
8236
8237
    uint pack_length=calc_pack_length((enum_field_types)
8238
				      f_packtype(pack_flag),
8239
				      field_length);
8240
8241
    if (f_is_blob(pack_flag))
8242
      return new Field_blob(ptr,null_pos,null_bit,
8243
			    unireg_check, field_name, share,
8244
			    pack_length, field_charset);
8245
    if (interval)
8246
    {
8247
      if (f_is_enum(pack_flag))
8248
	return new Field_enum(ptr,field_length,null_pos,null_bit,
8249
				  unireg_check, field_name,
8250
				  pack_length, interval, field_charset);
8251
      else
8252
	return new Field_set(ptr,field_length,null_pos,null_bit,
8253
			     unireg_check, field_name,
8254
			     pack_length, interval, field_charset);
8255
    }
8256
  }
8257
8258
  switch (field_type) {
8259
  case MYSQL_TYPE_NEWDECIMAL:
8260
    return new Field_new_decimal(ptr,field_length,null_pos,null_bit,
8261
                                 unireg_check, field_name,
8262
                                 f_decimals(pack_flag),
8263
                                 f_is_zerofill(pack_flag) != 0,
8264
                                 f_is_dec(pack_flag) == 0);
8265
  case MYSQL_TYPE_FLOAT:
8266
    return new Field_float(ptr,field_length,null_pos,null_bit,
8267
			   unireg_check, field_name,
8268
			   f_decimals(pack_flag),
8269
			   f_is_zerofill(pack_flag) != 0,
8270
			   f_is_dec(pack_flag)== 0);
8271
  case MYSQL_TYPE_DOUBLE:
8272
    return new Field_double(ptr,field_length,null_pos,null_bit,
8273
			    unireg_check, field_name,
8274
			    f_decimals(pack_flag),
8275
			    f_is_zerofill(pack_flag) != 0,
8276
			    f_is_dec(pack_flag)== 0);
8277
  case MYSQL_TYPE_TINY:
8278
    return new Field_tiny(ptr,field_length,null_pos,null_bit,
8279
			  unireg_check, field_name,
8280
			  f_is_zerofill(pack_flag) != 0,
8281
			  f_is_dec(pack_flag) == 0);
8282
  case MYSQL_TYPE_SHORT:
8283
    return new Field_short(ptr,field_length,null_pos,null_bit,
8284
			   unireg_check, field_name,
8285
			   f_is_zerofill(pack_flag) != 0,
8286
			   f_is_dec(pack_flag) == 0);
8287
  case MYSQL_TYPE_INT24:
8288
    return new Field_medium(ptr,field_length,null_pos,null_bit,
8289
			    unireg_check, field_name,
8290
			    f_is_zerofill(pack_flag) != 0,
8291
			    f_is_dec(pack_flag) == 0);
8292
  case MYSQL_TYPE_LONG:
8293
    return new Field_long(ptr,field_length,null_pos,null_bit,
8294
			   unireg_check, field_name,
8295
			   f_is_zerofill(pack_flag) != 0,
8296
			   f_is_dec(pack_flag) == 0);
8297
  case MYSQL_TYPE_LONGLONG:
8298
    return new Field_longlong(ptr,field_length,null_pos,null_bit,
8299
			      unireg_check, field_name,
8300
			      f_is_zerofill(pack_flag) != 0,
8301
			      f_is_dec(pack_flag) == 0);
8302
  case MYSQL_TYPE_TIMESTAMP:
8303
    return new Field_timestamp(ptr,field_length, null_pos, null_bit,
8304
                               unireg_check, field_name, share,
8305
                               field_charset);
8306
  case MYSQL_TYPE_YEAR:
8307
    return new Field_year(ptr,field_length,null_pos,null_bit,
8308
			  unireg_check, field_name);
8309
  case MYSQL_TYPE_DATE:
8310
    return new Field_date(ptr,null_pos,null_bit,
8311
			  unireg_check, field_name, field_charset);
8312
  case MYSQL_TYPE_NEWDATE:
8313
    return new Field_newdate(ptr,null_pos,null_bit,
8314
			     unireg_check, field_name, field_charset);
8315
  case MYSQL_TYPE_TIME:
8316
    return new Field_time(ptr,null_pos,null_bit,
8317
			  unireg_check, field_name, field_charset);
8318
  case MYSQL_TYPE_DATETIME:
8319
    return new Field_datetime(ptr,null_pos,null_bit,
8320
			      unireg_check, field_name, field_charset);
8321
  case MYSQL_TYPE_NULL:
8322
    return new Field_null(ptr, field_length, unireg_check, field_name,
8323
                          field_charset);
8324
  default:					// Impossible (Wrong version)
8325
    break;
8326
  }
8327
  return 0;
8328
}
8329
8330
8331
/** Create a field suitable for create of table. */
8332
8333
Create_field::Create_field(Field *old_field,Field *orig_field)
8334
{
8335
  field=      old_field;
8336
  field_name=change=old_field->field_name;
8337
  length=     old_field->field_length;
8338
  flags=      old_field->flags;
8339
  unireg_check=old_field->unireg_check;
8340
  pack_length=old_field->pack_length();
8341
  key_length= old_field->key_length();
8342
  sql_type=   old_field->real_type();
8343
  charset=    old_field->charset();		// May be NULL ptr
8344
  comment=    old_field->comment;
8345
  decimals=   old_field->decimals();
8346
8347
  /* Fix if the original table had 4 byte pointer blobs */
8348
  if (flags & BLOB_FLAG)
8349
    pack_length= (pack_length- old_field->table->s->blob_ptr_size +
8350
		  portable_sizeof_char_ptr);
8351
8352
  switch (sql_type) {
8353
  case MYSQL_TYPE_BLOB:
8354
    switch (pack_length - portable_sizeof_char_ptr) {
8355
    case  1: sql_type= MYSQL_TYPE_TINY_BLOB; break;
8356
    case  2: sql_type= MYSQL_TYPE_BLOB; break;
8357
    case  3: sql_type= MYSQL_TYPE_MEDIUM_BLOB; break;
8358
    default: sql_type= MYSQL_TYPE_LONG_BLOB; break;
8359
    }
8360
    length/= charset->mbmaxlen;
8361
    key_length/= charset->mbmaxlen;
8362
    break;
8363
  case MYSQL_TYPE_STRING:
8364
    /* Change CHAR -> VARCHAR if dynamic record length */
8365
    if (old_field->type() == MYSQL_TYPE_VAR_STRING)
8366
      sql_type= MYSQL_TYPE_VARCHAR;
8367
    /* fall through */
8368
8369
  case MYSQL_TYPE_ENUM:
8370
  case MYSQL_TYPE_SET:
8371
  case MYSQL_TYPE_VARCHAR:
8372
  case MYSQL_TYPE_VAR_STRING:
8373
    /* This is corrected in create_length_to_internal_length */
8374
    length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
8375
    break;
8376
  default:
8377
    break;
8378
  }
8379
8380
  if (flags & (ENUM_FLAG | SET_FLAG))
8381
    interval= ((Field_enum*) old_field)->typelib;
8382
  else
8383
    interval=0;
8384
  def=0;
8385
  char_length= length;
8386
8387
  if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
8388
      old_field->ptr && orig_field &&
8389
      (sql_type != MYSQL_TYPE_TIMESTAMP ||                /* set def only if */
8390
       old_field->table->timestamp_field != old_field ||  /* timestamp field */ 
8391
       unireg_check == Field::TIMESTAMP_UN_FIELD))        /* has default val */
8392
  {
8393
    char buff[MAX_FIELD_WIDTH];
8394
    String tmp(buff,sizeof(buff), charset);
8395
    my_ptrdiff_t diff;
8396
8397
    /* Get the value from default_values */
8398
    diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
8399
                          orig_field->table->record[0]);
8400
    orig_field->move_field_offset(diff);	// Points now at default_values
8401
    if (!orig_field->is_real_null())
8402
    {
8403
      char buff[MAX_FIELD_WIDTH], *pos;
8404
      String tmp(buff, sizeof(buff), charset), *res;
8405
      res= orig_field->val_str(&tmp);
8406
      pos= (char*) sql_strmake(res->ptr(), res->length());
8407
      def= new Item_string(pos, res->length(), charset);
8408
    }
8409
    orig_field->move_field_offset(-diff);	// Back to record[0]
8410
  }
8411
}
8412
8413
8414
/**
8415
  maximum possible display length for blob.
8416
8417
  @return
8418
    length
8419
*/
8420
8421
uint32 Field_blob::max_display_length()
8422
{
8423
  switch (packlength)
8424
  {
8425
  case 1:
8426
    return 255 * field_charset->mbmaxlen;
8427
  case 2:
8428
    return 65535 * field_charset->mbmaxlen;
8429
  case 3:
8430
    return 16777215 * field_charset->mbmaxlen;
8431
  case 4:
8432
    return (uint32) 4294967295U;
8433
  default:
8434
    DBUG_ASSERT(0); // we should never go here
8435
    return 0;
8436
  }
8437
}
8438
8439
8440
/*****************************************************************************
8441
 Warning handling
8442
*****************************************************************************/
8443
8444
/**
8445
  Produce warning or note about data saved into field.
8446
8447
  @param level            - level of message (Note/Warning/Error)
8448
  @param code             - error code of message to be produced
8449
  @param cuted_increment  - whenever we should increase cut fields count or not
8450
8451
  @note
8452
    This function won't produce warning and increase cut fields counter
8453
    if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
8454
8455
    if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
8456
    This allows us to avoid notes in optimisation, like convert_constant_item().
8457
8458
  @retval
8459
    1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
8460
  @retval
8461
    0 otherwise
8462
*/
8463
8464
bool 
8465
Field::set_warning(MYSQL_ERROR::enum_warning_level level, uint code,
8466
                   int cuted_increment)
8467
{
8468
  /*
8469
    If this field was created only for type conversion purposes it
8470
    will have table == NULL.
8471
  */
8472
  THD *thd= table ? table->in_use : current_thd;
8473
  if (thd->count_cuted_fields)
8474
  {
8475
    thd->cuted_fields+= cuted_increment;
8476
    push_warning_printf(thd, level, code, ER(code), field_name,
8477
                        thd->row_count);
8478
    return 0;
8479
  }
8480
  return level >= MYSQL_ERROR::WARN_LEVEL_WARN;
8481
}
8482
8483
8484
/**
8485
  Produce warning or note about datetime string data saved into field.
8486
8487
  @param level            level of message (Note/Warning/Error)
8488
  @param code             error code of message to be produced
8489
  @param str              string value which we tried to save
8490
  @param str_length       length of string which we tried to save
8491
  @param ts_type          type of datetime value (datetime/date/time)
8492
  @param cuted_increment  whenever we should increase cut fields count or not
8493
8494
  @note
8495
    This function will always produce some warning but won't increase cut
8496
    fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
8497
    thread.
8498
*/
8499
8500
void 
8501
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
8502
                            const char *str, uint str_length, 
8503
                            timestamp_type ts_type, int cuted_increment)
8504
{
8505
  THD *thd= table ? table->in_use : current_thd;
8506
  if ((thd->really_abort_on_warning() &&
8507
       level >= MYSQL_ERROR::WARN_LEVEL_WARN) ||
8508
      set_warning(level, code, cuted_increment))
8509
    make_truncated_value_warning(thd, level, str, str_length, ts_type,
8510
                                 field_name);
8511
}
8512
8513
8514
/**
8515
  Produce warning or note about integer datetime value saved into field.
8516
8517
  @param level            level of message (Note/Warning/Error)
8518
  @param code             error code of message to be produced
8519
  @param nr               numeric value which we tried to save
8520
  @param ts_type          type of datetime value (datetime/date/time)
8521
  @param cuted_increment  whenever we should increase cut fields count or not
8522
8523
  @note
8524
    This function will always produce some warning but won't increase cut
8525
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
8526
    thread.
8527
*/
8528
8529
void 
8530
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
8531
                            longlong nr, timestamp_type ts_type,
8532
                            int cuted_increment)
8533
{
8534
  THD *thd= table ? table->in_use : current_thd;
8535
  if (thd->really_abort_on_warning() ||
8536
      set_warning(level, code, cuted_increment))
8537
  {
8538
    char str_nr[22];
8539
    char *str_end= longlong10_to_str(nr, str_nr, -10);
8540
    make_truncated_value_warning(thd, level, str_nr, (uint) (str_end - str_nr), 
8541
                                 ts_type, field_name);
8542
  }
8543
}
8544
8545
8546
/**
8547
  Produce warning or note about double datetime data saved into field.
8548
8549
  @param level            level of message (Note/Warning/Error)
8550
  @param code             error code of message to be produced
8551
  @param nr               double value which we tried to save
8552
  @param ts_type          type of datetime value (datetime/date/time)
8553
8554
  @note
8555
    This function will always produce some warning but won't increase cut
8556
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
8557
    thread.
8558
*/
8559
8560
void 
8561
Field::set_datetime_warning(MYSQL_ERROR::enum_warning_level level, uint code, 
8562
                            double nr, timestamp_type ts_type)
8563
{
8564
  THD *thd= table ? table->in_use : current_thd;
8565
  if (thd->really_abort_on_warning() ||
8566
      set_warning(level, code, 1))
8567
  {
8568
    /* DBL_DIG is enough to print '-[digits].E+###' */
8569
    char str_nr[DBL_DIG + 8];
8570
    uint str_len= my_sprintf(str_nr, (str_nr, "%g", nr));
8571
    make_truncated_value_warning(thd, level, str_nr, str_len, ts_type,
8572
                                 field_name);
8573
  }
8574
}