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