1 /*
2 Copyright (c) 2000, 2020, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25
26 /**
27 @file
28
29 @brief
30 This file implements classes defined in field.h
31 */
32
33 #include "sql_priv.h"
34 #include "sql_select.h"
35 #include "rpl_rli.h" // Pull in Relay_log_info
36 #include "rpl_slave.h" // Pull in rpl_master_has_bug()
37 #include "strfunc.h" // find_type2, find_set
38 #include "sql_time.h" // str_to_datetime_with_warn,
39 // str_to_time_with_warn,
40 // TIME_to_timestamp,
41 // make_time, make_date,
42 // make_truncated_value_warning
43 #include "tztime.h" // struct Time_zone
44 #include "filesort.h" // change_double_for_sort
45 #include "log_event.h" // class Table_map_log_event
46 #include <m_ctype.h>
47 #include <errno.h>
48 #include "sql_join_buffer.h" // CACHE_FIELD
49 #include "sql_base.h" // is_equal() for LEX_STRING
50
51 using std::max;
52 using std::min;
53
54 // Maximum allowed exponent value for converting string to decimal
55 #define MAX_EXPONENT 1024
56
57 /**
58 Static variables
59 */
60 uchar Field_null::null[1]={1};
61 const char field_separator=',';
62
63 #define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
64 #define LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE 128
65 #define DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE 128
66 #define BLOB_PACK_LENGTH_TO_MAX_LENGH(arg) \
67 ((ulong) ((LL(1) << MY_MIN(arg, 4) * 8) - LL(1)))
68
69 /*
70 Rules for merging different types of fields in UNION
71
72 NOTE: to avoid 256*256 table, gap in table types numeration is skiped
73 following #defines describe that gap and how to canculate number of fields
74 and index of field in thia array.
75 */
76 #define FIELDTYPE_TEAR_FROM (MYSQL_TYPE_BIT + 1)
77 #define FIELDTYPE_TEAR_TO (MYSQL_TYPE_NEWDECIMAL - 1)
78 #define FIELDTYPE_NUM (FIELDTYPE_TEAR_FROM + (255 - FIELDTYPE_TEAR_TO))
79
80
field_type2index(enum_field_types field_type)81 inline int field_type2index (enum_field_types field_type)
82 {
83 field_type= real_type_to_type(field_type);
84 DBUG_ASSERT(field_type < FIELDTYPE_TEAR_FROM ||
85 field_type > FIELDTYPE_TEAR_TO);
86 return (field_type < FIELDTYPE_TEAR_FROM ?
87 field_type :
88 ((int)FIELDTYPE_TEAR_FROM) + (field_type - FIELDTYPE_TEAR_TO) - 1);
89 }
90
91
92 static enum_field_types field_types_merge_rules [FIELDTYPE_NUM][FIELDTYPE_NUM]=
93 {
94 /* MYSQL_TYPE_DECIMAL -> */
95 {
96 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
97 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
98 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
99 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
100 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
101 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
102 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
103 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
104 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
105 MYSQL_TYPE_DECIMAL, MYSQL_TYPE_DECIMAL,
106 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
107 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
108 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
109 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
110 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
111 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
112 //MYSQL_TYPE_BIT <16>-<245>
113 MYSQL_TYPE_VARCHAR,
114 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
115 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
116 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
117 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
118 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
119 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
120 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
121 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
122 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
123 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
124 },
125 /* MYSQL_TYPE_TINY -> */
126 {
127 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
128 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_TINY,
129 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
130 MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
131 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
132 MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
133 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
134 MYSQL_TYPE_TINY, MYSQL_TYPE_VARCHAR,
135 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
136 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
137 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
138 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
139 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
140 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY,
141 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
142 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
143 //MYSQL_TYPE_BIT <16>-<245>
144 MYSQL_TYPE_VARCHAR,
145 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
146 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
147 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
148 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
149 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
150 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
151 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
152 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
153 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
154 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
155 },
156 /* MYSQL_TYPE_SHORT -> */
157 {
158 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
159 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_SHORT,
160 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
161 MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
162 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
163 MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
164 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
165 MYSQL_TYPE_SHORT, MYSQL_TYPE_VARCHAR,
166 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
167 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
168 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
169 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
170 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
171 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_SHORT,
172 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
173 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
174 //MYSQL_TYPE_BIT <16>-<245>
175 MYSQL_TYPE_VARCHAR,
176 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
177 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
178 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
179 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
180 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
181 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
182 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
183 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
184 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
185 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
186 },
187 /* MYSQL_TYPE_LONG -> */
188 {
189 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
190 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_LONG,
191 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
192 MYSQL_TYPE_LONG, MYSQL_TYPE_LONG,
193 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
194 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
195 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
196 MYSQL_TYPE_LONG, MYSQL_TYPE_VARCHAR,
197 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
198 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONG,
199 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
200 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
201 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
202 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_LONG,
203 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
204 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
205 //MYSQL_TYPE_BIT <16>-<245>
206 MYSQL_TYPE_VARCHAR,
207 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
208 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
209 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
210 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
211 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
212 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
213 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
214 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
215 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
216 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
217 },
218 /* MYSQL_TYPE_FLOAT -> */
219 {
220 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
221 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_FLOAT,
222 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
223 MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
224 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
225 MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
226 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
227 MYSQL_TYPE_FLOAT, MYSQL_TYPE_VARCHAR,
228 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
229 MYSQL_TYPE_FLOAT, MYSQL_TYPE_FLOAT,
230 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
231 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
232 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
233 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_FLOAT,
234 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
235 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
236 //MYSQL_TYPE_BIT <16>-<245>
237 MYSQL_TYPE_VARCHAR,
238 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
239 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
240 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
241 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
242 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
243 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
244 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
245 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
246 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
247 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
248 },
249 /* MYSQL_TYPE_DOUBLE -> */
250 {
251 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
252 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
253 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
254 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
255 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
256 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
257 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
258 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
259 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
260 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
261 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
262 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
263 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
264 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_DOUBLE,
265 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
266 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
267 //MYSQL_TYPE_BIT <16>-<245>
268 MYSQL_TYPE_VARCHAR,
269 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
270 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_VARCHAR,
271 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
272 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
273 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
274 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
275 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
276 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
277 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
278 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
279 },
280 /* MYSQL_TYPE_NULL -> */
281 {
282 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
283 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_TINY,
284 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
285 MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
286 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
287 MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
288 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
289 MYSQL_TYPE_NULL, MYSQL_TYPE_TIMESTAMP,
290 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
291 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONGLONG,
292 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
293 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_TIME,
294 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
295 MYSQL_TYPE_DATETIME, MYSQL_TYPE_YEAR,
296 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
297 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
298 //MYSQL_TYPE_BIT <16>-<245>
299 MYSQL_TYPE_BIT,
300 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
301 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_ENUM,
302 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
303 MYSQL_TYPE_SET, MYSQL_TYPE_TINY_BLOB,
304 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
305 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
306 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
307 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
308 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
309 MYSQL_TYPE_STRING, MYSQL_TYPE_GEOMETRY
310 },
311 /* MYSQL_TYPE_TIMESTAMP -> */
312 {
313 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
314 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
315 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
316 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
317 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
318 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
319 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
320 MYSQL_TYPE_TIMESTAMP, MYSQL_TYPE_TIMESTAMP,
321 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
322 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
323 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
324 MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
325 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
326 MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
327 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
328 MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
329 //MYSQL_TYPE_BIT <16>-<245>
330 MYSQL_TYPE_VARCHAR,
331 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
332 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
333 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
334 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
335 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
336 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
337 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
338 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
339 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
340 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
341 },
342 /* MYSQL_TYPE_LONGLONG -> */
343 {
344 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
345 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_LONGLONG,
346 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
347 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONGLONG,
348 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
349 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
350 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
351 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_VARCHAR,
352 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
353 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_LONG,
354 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
355 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
356 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
357 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_LONGLONG,
358 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
359 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
360 //MYSQL_TYPE_BIT <16>-<245>
361 MYSQL_TYPE_VARCHAR,
362 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
363 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
364 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
365 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
366 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
367 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
368 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
369 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
370 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
371 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
372 },
373 /* MYSQL_TYPE_INT24 -> */
374 {
375 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
376 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_INT24,
377 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
378 MYSQL_TYPE_INT24, MYSQL_TYPE_LONG,
379 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
380 MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
381 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
382 MYSQL_TYPE_INT24, MYSQL_TYPE_VARCHAR,
383 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
384 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
385 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
386 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
387 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
388 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_INT24,
389 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
390 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
391 //MYSQL_TYPE_BIT <16>-<245>
392 MYSQL_TYPE_VARCHAR,
393 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
394 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
395 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
396 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
397 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
398 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
399 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
400 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
401 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
402 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
403 },
404 /* MYSQL_TYPE_DATE -> */
405 {
406 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
407 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
408 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
409 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
410 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
411 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
412 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
413 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
414 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
415 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
416 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
417 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
418 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
419 MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
420 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
421 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
422 //MYSQL_TYPE_BIT <16>-<245>
423 MYSQL_TYPE_VARCHAR,
424 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
425 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
426 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
427 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
428 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
429 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
430 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
431 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
432 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
433 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
434 },
435 /* MYSQL_TYPE_TIME -> */
436 {
437 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
438 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
439 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
440 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
441 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
442 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
443 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
444 MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME,
445 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
446 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
447 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
448 MYSQL_TYPE_DATETIME, MYSQL_TYPE_TIME,
449 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
450 MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
451 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
452 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
453 //MYSQL_TYPE_BIT <16>-<245>
454 MYSQL_TYPE_VARCHAR,
455 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
456 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
457 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
458 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
459 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
460 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
461 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
462 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
463 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
464 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
465 },
466 /* MYSQL_TYPE_DATETIME -> */
467 {
468 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
469 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
470 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
471 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
472 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
473 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
474 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
475 MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
476 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
477 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
478 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
479 MYSQL_TYPE_DATETIME, MYSQL_TYPE_DATETIME,
480 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
481 MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
482 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
483 MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
484 //MYSQL_TYPE_BIT <16>-<245>
485 MYSQL_TYPE_VARCHAR,
486 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
487 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
488 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
489 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
490 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
491 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
492 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
493 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
494 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
495 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
496 },
497 /* MYSQL_TYPE_YEAR -> */
498 {
499 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
500 MYSQL_TYPE_DECIMAL, MYSQL_TYPE_TINY,
501 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
502 MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG,
503 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
504 MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE,
505 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
506 MYSQL_TYPE_YEAR, MYSQL_TYPE_VARCHAR,
507 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
508 MYSQL_TYPE_LONGLONG, MYSQL_TYPE_INT24,
509 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
510 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
511 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
512 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_YEAR,
513 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
514 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
515 //MYSQL_TYPE_BIT <16>-<245>
516 MYSQL_TYPE_VARCHAR,
517 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
518 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
519 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
520 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
521 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
522 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
523 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
524 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
525 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
526 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
527 },
528 /* MYSQL_TYPE_NEWDATE -> */
529 {
530 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
531 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
532 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
533 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
534 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
535 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
536 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
537 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
538 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
539 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
540 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
541 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_DATETIME,
542 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
543 MYSQL_TYPE_DATETIME, MYSQL_TYPE_VARCHAR,
544 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
545 MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,
546 //MYSQL_TYPE_BIT <16>-<245>
547 MYSQL_TYPE_VARCHAR,
548 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
549 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
550 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
551 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
552 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
553 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
554 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
555 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
556 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
557 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
558 },
559 /* MYSQL_TYPE_VARCHAR -> */
560 {
561 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
562 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
563 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
564 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
565 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
566 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
567 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
568 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
569 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
570 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
571 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
572 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
573 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
574 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
575 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
576 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
577 //MYSQL_TYPE_BIT <16>-<245>
578 MYSQL_TYPE_VARCHAR,
579 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
580 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
581 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
582 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
583 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
584 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
585 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
586 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
587 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
588 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR
589 },
590 /* MYSQL_TYPE_BIT -> */
591 {
592 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
593 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
594 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
595 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
596 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
597 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
598 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
599 MYSQL_TYPE_BIT, MYSQL_TYPE_VARCHAR,
600 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
601 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
602 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
603 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
604 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
605 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
606 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
607 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
608 //MYSQL_TYPE_BIT <16>-<245>
609 MYSQL_TYPE_BIT,
610 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
611 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
612 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
613 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
614 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
615 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
616 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
617 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
618 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
619 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
620 },
621 /* MYSQL_TYPE_NEWDECIMAL -> */
622 {
623 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
624 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
625 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
626 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
627 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
628 MYSQL_TYPE_DOUBLE, MYSQL_TYPE_DOUBLE,
629 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
630 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
631 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
632 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_NEWDECIMAL,
633 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
634 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
635 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
636 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_NEWDECIMAL,
637 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
638 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
639 //MYSQL_TYPE_BIT <16>-<245>
640 MYSQL_TYPE_VARCHAR,
641 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
642 MYSQL_TYPE_NEWDECIMAL, MYSQL_TYPE_VARCHAR,
643 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
644 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
645 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
646 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
647 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
648 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
649 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
650 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
651 },
652 /* MYSQL_TYPE_ENUM -> */
653 {
654 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
655 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
656 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
657 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
658 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
659 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
660 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
661 MYSQL_TYPE_ENUM, MYSQL_TYPE_VARCHAR,
662 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
663 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
664 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
665 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
666 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
667 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
668 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
669 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
670 //MYSQL_TYPE_BIT <16>-<245>
671 MYSQL_TYPE_VARCHAR,
672 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
673 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
674 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
675 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
676 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
677 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
678 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
679 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
680 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
681 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
682 },
683 /* MYSQL_TYPE_SET -> */
684 {
685 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
686 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
687 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
688 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
689 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
690 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
691 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
692 MYSQL_TYPE_SET, MYSQL_TYPE_VARCHAR,
693 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
694 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
695 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
696 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
697 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
698 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
699 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
700 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
701 //MYSQL_TYPE_BIT <16>-<245>
702 MYSQL_TYPE_VARCHAR,
703 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
704 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
705 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
706 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
707 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
708 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
709 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
710 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
711 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
712 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR
713 },
714 /* MYSQL_TYPE_TINY_BLOB -> */
715 {
716 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
717 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
718 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
719 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
720 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
721 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
722 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
723 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
724 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
725 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
726 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
727 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
728 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
729 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
730 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
731 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
732 //MYSQL_TYPE_BIT <16>-<245>
733 MYSQL_TYPE_TINY_BLOB,
734 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
735 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
736 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
737 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB,
738 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
739 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
740 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
741 MYSQL_TYPE_BLOB, MYSQL_TYPE_TINY_BLOB,
742 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
743 MYSQL_TYPE_TINY_BLOB, MYSQL_TYPE_TINY_BLOB
744 },
745 /* MYSQL_TYPE_MEDIUM_BLOB -> */
746 {
747 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
748 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
749 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
750 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
751 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
752 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
753 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
754 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
755 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
756 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
757 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
758 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
759 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
760 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
761 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
762 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
763 //MYSQL_TYPE_BIT <16>-<245>
764 MYSQL_TYPE_MEDIUM_BLOB,
765 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
766 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
767 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
768 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
769 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
770 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
771 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
772 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB,
773 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
774 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_MEDIUM_BLOB
775 },
776 /* MYSQL_TYPE_LONG_BLOB -> */
777 {
778 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
779 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
780 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
781 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
782 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
783 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
784 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
785 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
786 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
787 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
788 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
789 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
790 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
791 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
792 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
793 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
794 //MYSQL_TYPE_BIT <16>-<245>
795 MYSQL_TYPE_LONG_BLOB,
796 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
797 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
798 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
799 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
800 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
801 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
802 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
803 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB,
804 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
805 MYSQL_TYPE_LONG_BLOB, MYSQL_TYPE_LONG_BLOB
806 },
807 /* MYSQL_TYPE_BLOB -> */
808 {
809 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
810 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
811 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
812 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
813 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
814 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
815 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
816 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
817 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
818 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
819 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
820 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
821 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
822 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
823 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
824 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
825 //MYSQL_TYPE_BIT <16>-<245>
826 MYSQL_TYPE_BLOB,
827 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
828 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
829 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
830 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
831 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
832 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
833 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
834 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB,
835 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
836 MYSQL_TYPE_BLOB, MYSQL_TYPE_BLOB
837 },
838 /* MYSQL_TYPE_VAR_STRING -> */
839 {
840 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
841 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
842 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
843 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
844 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
845 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
846 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
847 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
848 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
849 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
850 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
851 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
852 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
853 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
854 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
855 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
856 //MYSQL_TYPE_BIT <16>-<245>
857 MYSQL_TYPE_VARCHAR,
858 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
859 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
860 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
861 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
862 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
863 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
864 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
865 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
866 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
867 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR
868 },
869 /* MYSQL_TYPE_STRING -> */
870 {
871 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
872 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
873 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
874 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
875 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
876 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
877 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
878 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
879 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
880 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
881 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
882 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
883 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
884 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
885 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
886 MYSQL_TYPE_STRING, MYSQL_TYPE_VARCHAR,
887 //MYSQL_TYPE_BIT <16>-<245>
888 MYSQL_TYPE_STRING,
889 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
890 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING,
891 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
892 MYSQL_TYPE_STRING, MYSQL_TYPE_TINY_BLOB,
893 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
894 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
895 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
896 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
897 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
898 MYSQL_TYPE_STRING, MYSQL_TYPE_STRING
899 },
900 /* MYSQL_TYPE_GEOMETRY -> */
901 {
902 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
903 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
904 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
905 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
906 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
907 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
908 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
909 MYSQL_TYPE_GEOMETRY, MYSQL_TYPE_VARCHAR,
910 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
911 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
912 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
913 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
914 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
915 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
916 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
917 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
918 //MYSQL_TYPE_BIT <16>-<245>
919 MYSQL_TYPE_VARCHAR,
920 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
921 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_VARCHAR,
922 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
923 MYSQL_TYPE_VARCHAR, MYSQL_TYPE_TINY_BLOB,
924 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
925 MYSQL_TYPE_MEDIUM_BLOB, MYSQL_TYPE_LONG_BLOB,
926 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
927 MYSQL_TYPE_BLOB, MYSQL_TYPE_VARCHAR,
928 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
929 MYSQL_TYPE_STRING, MYSQL_TYPE_GEOMETRY
930 }
931 };
932
933
934 /**
935 Return type of which can carry value of both given types in UNION result.
936
937 @param a type for merging
938 @param b type for merging
939
940 @return
941 type of field
942 */
943
field_type_merge(enum_field_types a,enum_field_types b)944 enum_field_types Field::field_type_merge(enum_field_types a,
945 enum_field_types b)
946 {
947 return field_types_merge_rules[field_type2index(a)]
948 [field_type2index(b)];
949 }
950
951
952 static Item_result field_types_result_type [FIELDTYPE_NUM]=
953 {
954 //MYSQL_TYPE_DECIMAL MYSQL_TYPE_TINY
955 DECIMAL_RESULT, INT_RESULT,
956 //MYSQL_TYPE_SHORT MYSQL_TYPE_LONG
957 INT_RESULT, INT_RESULT,
958 //MYSQL_TYPE_FLOAT MYSQL_TYPE_DOUBLE
959 REAL_RESULT, REAL_RESULT,
960 //MYSQL_TYPE_NULL MYSQL_TYPE_TIMESTAMP
961 STRING_RESULT, STRING_RESULT,
962 //MYSQL_TYPE_LONGLONG MYSQL_TYPE_INT24
963 INT_RESULT, INT_RESULT,
964 //MYSQL_TYPE_DATE MYSQL_TYPE_TIME
965 STRING_RESULT, STRING_RESULT,
966 //MYSQL_TYPE_DATETIME MYSQL_TYPE_YEAR
967 STRING_RESULT, INT_RESULT,
968 //MYSQL_TYPE_NEWDATE MYSQL_TYPE_VARCHAR
969 STRING_RESULT, STRING_RESULT,
970 //MYSQL_TYPE_BIT <16>-<245>
971 STRING_RESULT,
972 //MYSQL_TYPE_NEWDECIMAL MYSQL_TYPE_ENUM
973 DECIMAL_RESULT, STRING_RESULT,
974 //MYSQL_TYPE_SET MYSQL_TYPE_TINY_BLOB
975 STRING_RESULT, STRING_RESULT,
976 //MYSQL_TYPE_MEDIUM_BLOB MYSQL_TYPE_LONG_BLOB
977 STRING_RESULT, STRING_RESULT,
978 //MYSQL_TYPE_BLOB MYSQL_TYPE_VAR_STRING
979 STRING_RESULT, STRING_RESULT,
980 //MYSQL_TYPE_STRING MYSQL_TYPE_GEOMETRY
981 STRING_RESULT, STRING_RESULT
982 };
983
984
985 /*
986 Test if the given string contains important data:
987 not spaces for character string,
988 or any data for binary string.
989
990 SYNOPSIS
991 test_if_important_data()
992 cs Character set
993 str String to test
994 strend String end
995
996 RETURN
997 FALSE - If string does not have important data
998 TRUE - If string has some important data
999 */
1000
1001 static bool
test_if_important_data(const CHARSET_INFO * cs,const char * str,const char * strend)1002 test_if_important_data(const CHARSET_INFO *cs, const char *str,
1003 const char *strend)
1004 {
1005 if (cs != &my_charset_bin)
1006 str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
1007 return (str < strend);
1008 }
1009
1010
1011 /**
1012 Function to compare two unsigned integers for their relative order.
1013 Used below. In an anonymous namespace to not clash with definitions
1014 in other files.
1015 */
1016
1017 CPP_UNNAMED_NS_START
1018
compare(unsigned int a,unsigned int b)1019 int compare(unsigned int a, unsigned int b)
1020 {
1021 if (a < b)
1022 return -1;
1023 if (b < a)
1024 return 1;
1025 return 0;
1026 }
1027
1028 CPP_UNNAMED_NS_END
1029
1030 /**
1031 Detect Item_result by given field type of UNION merge result.
1032
1033 @param field_type given field type
1034
1035 @return
1036 Item_result (type of internal MySQL expression result)
1037 */
1038
result_merge_type(enum_field_types field_type)1039 Item_result Field::result_merge_type(enum_field_types field_type)
1040 {
1041 return field_types_result_type[field_type2index(field_type)];
1042 }
1043
1044 /*****************************************************************************
1045 Static help functions
1046 *****************************************************************************/
1047
1048 /**
1049 Output a warning for erroneous conversion of strings to numerical
1050 values. For use with ER_TRUNCATED_WRONG_VALUE[_FOR_FIELD]
1051
1052 @param thd THD object
1053 @param str pointer to string that failed to be converted
1054 @param length length of string
1055 @param cs charset for string
1056 @param typestr string describing type converted to
1057 @param error error value to output
1058 @param field_name (for *_FOR_FIELD) name of field
1059 @param row_num (for *_FOR_FIELD) row number
1060 */
push_numerical_conversion_warning(THD * thd,const char * str,uint length,const CHARSET_INFO * cs,const char * typestr,int error,const char * field_name="UNKNOWN",ulong row_num=0)1061 static void push_numerical_conversion_warning(THD* thd, const char* str,
1062 uint length,
1063 const CHARSET_INFO* cs,
1064 const char* typestr, int error,
1065 const char* field_name="UNKNOWN",
1066 ulong row_num=0)
1067 {
1068 char buf[MY_MAX(MY_MAX(DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE,
1069 LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE),
1070 DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE)];
1071
1072 String tmp(buf, sizeof(buf), cs);
1073 tmp.copy(str, length, cs);
1074 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
1075 error, ER(error), typestr, tmp.c_ptr(),
1076 field_name, row_num);
1077 }
1078
1079
1080 /**
1081 Emits a warning for the decimal conversion error. May modify
1082 dec_value if there was conversion overflow or bad number.
1083
1084
1085 @param dec_error decimal library return code
1086 (E_DEC_* see include/decimal.h)
1087 @param dec_value[in,out] Decimal value returned by convertion function.
1088 @param from Value converted from
1089 @param length Length of 'from'
1090 @param charset_arg Charset of 'from'
1091 */
set_decimal_warning(Field_new_decimal * field,int dec_error,my_decimal * dec_value,const char * from,uint length,const CHARSET_INFO * charset_arg)1092 static void set_decimal_warning(Field_new_decimal *field,
1093 int dec_error,
1094 my_decimal *dec_value,
1095 const char *from,
1096 uint length,
1097 const CHARSET_INFO *charset_arg)
1098 {
1099 switch (dec_error) {
1100 case E_DEC_TRUNCATED:
1101 field->set_warning(Sql_condition::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
1102 break;
1103 case E_DEC_OVERFLOW:
1104 field->set_warning(Sql_condition::WARN_LEVEL_WARN,
1105 ER_WARN_DATA_OUT_OF_RANGE, 1);
1106 field->set_value_on_overflow(dec_value, dec_value->sign());
1107 break;
1108 case E_DEC_BAD_NUM:
1109 ErrConvString errmsg(from, length, charset_arg);
1110 const Diagnostics_area *da= field->table->in_use->get_stmt_da();
1111 push_warning_printf(field->table->in_use, Sql_condition::WARN_LEVEL_WARN,
1112 ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
1113 ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1114 "decimal", errmsg.ptr(), field->field_name,
1115 static_cast<ulong>(da->current_row_for_warning()));
1116 my_decimal_set_zero(dec_value);
1117 }
1118 }
1119
1120 /**
1121 Check whether a field type can be partially indexed by a key.
1122
1123 This is a static method, rather than a virtual function, because we need
1124 to check the type of a non-Field in mysql_alter_table().
1125
1126 @param type field type
1127
1128 @retval
1129 TRUE Type can have a prefixed key
1130 @retval
1131 FALSE Type can not have a prefixed key
1132 */
1133
type_can_have_key_part(enum enum_field_types type)1134 bool Field::type_can_have_key_part(enum enum_field_types type)
1135 {
1136 switch (type) {
1137 case MYSQL_TYPE_VARCHAR:
1138 case MYSQL_TYPE_TINY_BLOB:
1139 case MYSQL_TYPE_MEDIUM_BLOB:
1140 case MYSQL_TYPE_LONG_BLOB:
1141 case MYSQL_TYPE_BLOB:
1142 case MYSQL_TYPE_VAR_STRING:
1143 case MYSQL_TYPE_STRING:
1144 case MYSQL_TYPE_GEOMETRY:
1145 return TRUE;
1146 default:
1147 return FALSE;
1148 }
1149 }
1150
1151
1152 /**
1153 Numeric fields base class constructor.
1154 */
Field_num(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,utype unireg_check_arg,const char * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)1155 Field_num::Field_num(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
1156 uchar null_bit_arg, utype unireg_check_arg,
1157 const char *field_name_arg,
1158 uint8 dec_arg, bool zero_arg, bool unsigned_arg)
1159 :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1160 unireg_check_arg, field_name_arg),
1161 dec(dec_arg),zerofill(zero_arg),unsigned_flag(unsigned_arg)
1162 {
1163 if (zerofill)
1164 flags|=ZEROFILL_FLAG;
1165 if (unsigned_flag)
1166 flags|=UNSIGNED_FLAG;
1167 }
1168
1169
prepend_zeros(String * value)1170 void Field_num::prepend_zeros(String *value)
1171 {
1172 int diff;
1173 if ((diff= (int) (field_length - value->length())) > 0)
1174 {
1175 const bool error= value->realloc(field_length);
1176 if (!error)
1177 {
1178 bmove_upp((uchar*) value->ptr()+field_length,
1179 (uchar*) value->ptr()+value->length(),
1180 value->length());
1181 memset(const_cast<char*>(value->ptr()), '0', diff);
1182 value->length(field_length);
1183 }
1184 }
1185 }
1186
1187 /**
1188 Test if given number is a int.
1189
1190 @todo
1191 Make this multi-byte-character safe
1192
1193 @param str String to test
1194 @param length Length of 'str'
1195 @param int_end Pointer to char after last used digit
1196 @param cs Character set
1197
1198 @note
1199 This is called after one has called strntoull10rnd() function.
1200
1201 @return TYPE_OK, TYPE_ERR_BAD_VALUE or TYPE_WARN_TRUNCATED
1202 */
1203
1204 type_conversion_status
check_int(const CHARSET_INFO * cs,const char * str,int length,const char * int_end,int error)1205 Field_num::check_int(const CHARSET_INFO *cs, const char *str, int length,
1206 const char *int_end, int error)
1207 {
1208 /* Test if we get an empty string or wrong integer */
1209 if (str == int_end || error == MY_ERRNO_EDOM)
1210 {
1211 ErrConvString err(str, length, cs);
1212 push_warning_printf(table->in_use, Sql_condition::WARN_LEVEL_WARN,
1213 ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
1214 ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
1215 "integer", err.ptr(), field_name,
1216 (ulong) table->in_use->get_stmt_da()->
1217 current_row_for_warning());
1218 return TYPE_ERR_BAD_VALUE;
1219 }
1220 /* Test if we have garbage at the end of the given string. */
1221 if (test_if_important_data(cs, int_end, str + length))
1222 {
1223 set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
1224 return TYPE_WARN_TRUNCATED;
1225 }
1226 return TYPE_OK;
1227 }
1228
1229
1230 /*
1231 Conver a string to an integer then check bounds.
1232
1233 SYNOPSIS
1234 Field_num::get_int
1235 cs Character set
1236 from String to convert
1237 len Length of the string
1238 rnd OUT longlong value
1239 unsigned_max max unsigned value
1240 signed_min min signed value
1241 signed_max max signed value
1242
1243 DESCRIPTION
1244 The function calls strntoull10rnd() to get an integer value then
1245 check bounds and errors returned. In case of any error a warning
1246 is raised.
1247
1248 @return TYPE_OK, TYPE_WARN_OUT_OF_RANGE, TYPE_ERR_BAD_VALUE or
1249 TYPE_WARN_TRUNCATED
1250 */
1251
1252 type_conversion_status
get_int(const CHARSET_INFO * cs,const char * from,uint len,longlong * rnd,ulonglong unsigned_max,longlong signed_min,longlong signed_max)1253 Field_num::get_int(const CHARSET_INFO *cs, const char *from, uint len,
1254 longlong *rnd, ulonglong unsigned_max,
1255 longlong signed_min, longlong signed_max)
1256 {
1257 char *end;
1258 int error;
1259
1260 *rnd= (longlong) cs->cset->strntoull10rnd(cs, from, len,
1261 unsigned_flag, &end,
1262 &error);
1263 if (unsigned_flag)
1264 {
1265 if ((((ulonglong) *rnd > unsigned_max) &&
1266 (*rnd= (longlong) unsigned_max)) ||
1267 error == MY_ERRNO_ERANGE)
1268 goto out_of_range;
1269 }
1270 else
1271 {
1272 if (*rnd < signed_min)
1273 {
1274 *rnd= signed_min;
1275 goto out_of_range;
1276 }
1277 else if (*rnd > signed_max)
1278 {
1279 *rnd= signed_max;
1280 goto out_of_range;
1281 }
1282 }
1283 if (table->in_use->count_cuted_fields != 0)
1284 return check_int(cs, from, len, end, error);
1285
1286 return TYPE_OK;
1287
1288 out_of_range:
1289 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1290 return TYPE_WARN_OUT_OF_RANGE;
1291 }
1292
1293
1294 /*
1295 This is a generic method which is executed only for
1296 Field_short, Field_medium, Field_long, Field_longlong and Field_tiny.
1297
1298 The other field types that come from Field_num override this method:
1299 Field_real (common parent for Field_decimal, Field_float, Field_double),
1300 Field_new_decimal, Field_year.
1301 */
1302 type_conversion_status
store_time(MYSQL_TIME * ltime,uint8 dec_arg MY_ATTRIBUTE ((unused)))1303 Field_num::store_time(MYSQL_TIME *ltime,
1304 uint8 dec_arg MY_ATTRIBUTE((unused)))
1305 {
1306 longlong nr= TIME_to_ulonglong_round(ltime);
1307 return store(ltime->neg ? -nr : nr, 0);
1308 }
1309
1310
1311 /**
1312 Process decimal library return codes and issue warnings for overflow and
1313 truncation.
1314
1315 @param op_result decimal library return code (E_DEC_* see include/decimal.h)
1316
1317 @retval 0 No error or some other errors except overflow
1318 @retval 1 There was overflow
1319 */
1320
warn_if_overflow(int op_result)1321 bool Field::warn_if_overflow(int op_result)
1322 {
1323 if (op_result == E_DEC_OVERFLOW)
1324 {
1325 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1326 return true;
1327 }
1328 if (op_result == E_DEC_TRUNCATED)
1329 {
1330 set_warning(Sql_condition::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
1331 /* We return 0 here as this is not a critical issue */
1332 }
1333 return false;
1334 }
1335
1336
1337 /**
1338 Interpret field value as an integer but return the result as a string.
1339
1340 This is used for printing bit_fields as numbers while debugging.
1341 */
1342
val_int_as_str(String * val_buffer,my_bool unsigned_val)1343 String *Field::val_int_as_str(String *val_buffer, my_bool unsigned_val)
1344 {
1345 ASSERT_COLUMN_MARKED_FOR_READ;
1346 const CHARSET_INFO *cs= &my_charset_bin;
1347 uint length;
1348 longlong value= val_int();
1349
1350 if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
1351 return 0;
1352 length= (uint) (*cs->cset->longlong10_to_str)(cs, (char*) val_buffer->ptr(),
1353 MY_INT64_NUM_DECIMAL_DIGITS,
1354 unsigned_val ? 10 : -10,
1355 value);
1356 val_buffer->length(length);
1357 return val_buffer;
1358 }
1359
1360
1361 /// This is used as a table name when the table structure is not set up
Field(uchar * ptr_arg,uint32 length_arg,uchar * null_ptr_arg,uchar null_bit_arg,utype unireg_check_arg,const char * field_name_arg)1362 Field::Field(uchar *ptr_arg,uint32 length_arg,uchar *null_ptr_arg,
1363 uchar null_bit_arg,
1364 utype unireg_check_arg, const char *field_name_arg)
1365 :ptr(ptr_arg), null_ptr(null_ptr_arg),
1366 table(0), orig_table(0), table_name(0),
1367 field_name(field_name_arg),
1368 unireg_check(unireg_check_arg),
1369 field_length(length_arg), null_bit(null_bit_arg),
1370 is_created_from_null_item(FALSE),
1371 zip_dict_name(null_lex_cstr),
1372 zip_dict_data(null_lex_cstr)
1373 {
1374 flags=null_ptr ? 0: NOT_NULL_FLAG;
1375 comment.str= (char*) "";
1376 comment.length=0;
1377 field_index= 0;
1378 }
1379
1380
hash(ulong * nr,ulong * nr2)1381 void Field::hash(ulong *nr, ulong *nr2)
1382 {
1383 if (is_null())
1384 {
1385 *nr^= (*nr << 1) | 1;
1386 }
1387 else
1388 {
1389 uint len= pack_length();
1390 const CHARSET_INFO *cs= sort_charset();
1391 cs->coll->hash_sort(cs, ptr, len, nr, nr2);
1392 }
1393 }
1394
1395 size_t
do_last_null_byte() const1396 Field::do_last_null_byte() const
1397 {
1398 DBUG_ASSERT(null_ptr == NULL || null_ptr >= table->record[0]);
1399 if (null_ptr)
1400 return null_offset() + 1;
1401 return LAST_NULL_BYTE_UNDEF;
1402 }
1403
1404
copy_from_tmp(int row_offset)1405 void Field::copy_from_tmp(int row_offset)
1406 {
1407 memcpy(ptr,ptr+row_offset,pack_length());
1408 if (null_ptr)
1409 {
1410 *null_ptr= (uchar) ((null_ptr[0] & (uchar) ~(uint) null_bit) |
1411 (null_ptr[row_offset] & (uchar) null_bit));
1412 }
1413 }
1414
1415
send_binary(Protocol * protocol)1416 bool Field::send_binary(Protocol *protocol)
1417 {
1418 char buff[MAX_FIELD_WIDTH];
1419 String tmp(buff,sizeof(buff),charset());
1420 val_str(&tmp);
1421 return protocol->store(tmp.ptr(), tmp.length(), tmp.charset());
1422 }
1423
1424 /**
1425 Checks if the current field definition and provided create field
1426 definition have different compression attributes.
1427
1428 @param new_field create field definition to compare with
1429
1430 @return
1431 true - if compression attributes are different
1432 false - if compression attributes are identical.
1433 */
has_different_compression_attributes_with(const Create_field & new_field) const1434 bool Field::has_different_compression_attributes_with(
1435 const Create_field& new_field) const
1436 {
1437 if (new_field.column_format() != COLUMN_FORMAT_TYPE_COMPRESSED &&
1438 column_format() != COLUMN_FORMAT_TYPE_COMPRESSED)
1439 return false;
1440
1441 if (new_field.column_format() != column_format()) return true;
1442
1443 if ((zip_dict_name.str == NULL) && (new_field.zip_dict_name.str == NULL))
1444 return false;
1445
1446 return !::is_equal(&new_field.zip_dict_name, &zip_dict_name);
1447 }
1448
1449
1450 /**
1451 Check to see if field size is compatible with destination.
1452
1453 This method is used in row-based replication to verify that the
1454 slave's field size is less than or equal to the master's field
1455 size. The encoded field metadata (from the master or source) is
1456 decoded and compared to the size of this field (the slave or
1457 destination).
1458
1459 @note
1460
1461 The comparison is made so that if the source data (from the master)
1462 is less than the target data (on the slave), -1 is returned in @c
1463 <code>*order_var</code>. This implies that a conversion is
1464 necessary, but that it is lossy and can result in truncation of the
1465 value.
1466
1467 If the source data is strictly greater than the target data, 1 is
1468 returned in <code>*order_var</code>. This implies that the source
1469 type can is contained in the target type and that a conversion is
1470 necessary but is non-lossy.
1471
1472 If no conversion is required to fit the source type in the target
1473 type, 0 is returned in <code>*order_var</code>.
1474
1475 @param field_metadata Encoded size in field metadata
1476 @param mflags Flags from the table map event for the table.
1477 @param order_var Pointer to variable where the order
1478 between the source field and this field
1479 will be returned.
1480
1481 @return @c true if this field's size is compatible with the
1482 master's field size, @c false otherwise.
1483 */
compatible_field_size(uint field_metadata,Relay_log_info * rli_arg MY_ATTRIBUTE ((unused)),uint16 mflags MY_ATTRIBUTE ((unused)),int * order_var)1484 bool Field::compatible_field_size(uint field_metadata,
1485 Relay_log_info *rli_arg MY_ATTRIBUTE((unused)),
1486 uint16 mflags MY_ATTRIBUTE((unused)),
1487 int *order_var)
1488 {
1489 uint const source_size= pack_length_from_metadata(field_metadata);
1490 uint const destination_size= row_pack_length();
1491 DBUG_PRINT("debug", ("real_type: %d, source_size: %u, destination_size: %u",
1492 real_type(), source_size, destination_size));
1493 *order_var = compare(source_size, destination_size);
1494 return true;
1495 }
1496
1497
1498 type_conversion_status
store(const char * to,uint length,const CHARSET_INFO * cs,enum_check_fields check_level)1499 Field::store(const char *to, uint length, const CHARSET_INFO *cs,
1500 enum_check_fields check_level)
1501 {
1502 enum_check_fields old_check_level= table->in_use->count_cuted_fields;
1503 table->in_use->count_cuted_fields= check_level;
1504 const type_conversion_status res= store(to, length, cs);
1505 table->in_use->count_cuted_fields= old_check_level;
1506 return res;
1507 }
1508
1509
1510 /**
1511 Pack the field into a format suitable for storage and transfer.
1512
1513 To implement packing functionality, only the virtual function
1514 should be overridden. The other functions are just convenience
1515 functions and hence should not be overridden.
1516
1517 The value of <code>low_byte_first</code> is dependent on how the
1518 packed data is going to be used: for local use, e.g., temporary
1519 store on disk or in memory, use the native format since that is
1520 faster. For data that is going to be transfered to other machines
1521 (e.g., when writing data to the binary log), data should always be
1522 stored in little-endian format.
1523
1524 @note The default method for packing fields just copy the raw bytes
1525 of the record into the destination, but never more than
1526 <code>max_length</code> characters.
1527
1528 @param to
1529 Pointer to memory area where representation of field should be put.
1530
1531 @param from
1532 Pointer to memory area where record representation of field is
1533 stored.
1534
1535 @param max_length
1536 Maximum length of the field, as given in the column definition. For
1537 example, for <code>CHAR(1000)</code>, the <code>max_length</code>
1538 is 1000. This information is sometimes needed to decide how to pack
1539 the data.
1540
1541 @param low_byte_first
1542 @c TRUE if integers should be stored little-endian, @c FALSE if
1543 native format should be used. Note that for little-endian machines,
1544 the value of this flag is a moot point since the native format is
1545 little-endian.
1546 */
1547 uchar *
pack(uchar * to,const uchar * from,uint max_length,bool low_byte_first MY_ATTRIBUTE ((unused)))1548 Field::pack(uchar *to, const uchar *from, uint max_length,
1549 bool low_byte_first MY_ATTRIBUTE((unused)))
1550 {
1551 uint32 length= pack_length();
1552 set_if_smaller(length, max_length);
1553 memcpy(to, from, length);
1554 return to+length;
1555 }
1556
1557 /**
1558 Unpack a field from row data.
1559
1560 This method is used to unpack a field from a master whose size of
1561 the field is less than that of the slave.
1562
1563 The <code>param_data</code> parameter is a two-byte integer (stored
1564 in the least significant 16 bits of the unsigned integer) usually
1565 consisting of two parts: the real type in the most significant byte
1566 and a original pack length in the least significant byte.
1567
1568 The exact layout of the <code>param_data</code> field is given by
1569 the <code>Table_map_log_event::save_field_metadata()</code>.
1570
1571 This is the default method for unpacking a field. It just copies
1572 the memory block in byte order (of original pack length bytes or
1573 length of field, whichever is smaller).
1574
1575 @param to Destination of the data
1576 @param from Source of the data
1577 @param param_data Real type and original pack length of the field
1578 data
1579
1580 @param low_byte_first
1581 If this flag is @c true, all composite entities (e.g., lengths)
1582 should be unpacked in little-endian format; otherwise, the entities
1583 are unpacked in native order.
1584
1585 @return New pointer into memory based on from + length of the data
1586 */
1587 const uchar *
unpack(uchar * to,const uchar * from,uint param_data,bool low_byte_first MY_ATTRIBUTE ((unused)))1588 Field::unpack(uchar* to, const uchar *from, uint param_data,
1589 bool low_byte_first MY_ATTRIBUTE((unused)))
1590 {
1591 uint length=pack_length();
1592 int from_type= 0;
1593 /*
1594 If from length is > 255, it has encoded data in the upper bits. Need
1595 to mask it out.
1596 */
1597 if (param_data > 255)
1598 {
1599 from_type= (param_data & 0xff00) >> 8U; // real_type.
1600 param_data= param_data & 0x00ff; // length.
1601 }
1602
1603 if ((param_data == 0) ||
1604 (length == param_data) ||
1605 (from_type != real_type()))
1606 {
1607 memcpy(to, from, length);
1608 return from+length;
1609 }
1610
1611 uint len= (param_data && (param_data < length)) ?
1612 param_data : length;
1613
1614 memcpy(to, from, param_data > length ? length : len);
1615 return from+len;
1616 }
1617
1618
add_zerofill_and_unsigned(String & res) const1619 void Field_num::add_zerofill_and_unsigned(String &res) const
1620 {
1621 if (unsigned_flag)
1622 res.append(STRING_WITH_LEN(" unsigned"));
1623 if (zerofill)
1624 res.append(STRING_WITH_LEN(" zerofill"));
1625 }
1626
1627
make_field(Send_field * field)1628 void Field::make_field(Send_field *field)
1629 {
1630 if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
1631 {
1632 field->db_name= orig_table->s->db.str;
1633 if (orig_table->pos_in_table_list &&
1634 orig_table->pos_in_table_list->schema_table)
1635 field->org_table_name= (orig_table->pos_in_table_list->
1636 schema_table->table_name);
1637 else
1638 field->org_table_name= orig_table->s->table_name.str;
1639 }
1640 else
1641 field->org_table_name= field->db_name= "";
1642 if (orig_table && orig_table->alias)
1643 {
1644 field->table_name= orig_table->alias;
1645 field->org_col_name= field_name;
1646 }
1647 else
1648 {
1649 field->table_name= "";
1650 field->org_col_name= "";
1651 }
1652 field->col_name= field_name;
1653 field->charsetnr= charset()->number;
1654 field->length=field_length;
1655 field->type=type();
1656 field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
1657 field->decimals= decimals();
1658 }
1659
1660
1661 /**
1662 Conversion from decimal to longlong. Checks overflow and returns
1663 correct value (min/max) in case of overflow.
1664
1665 @param val value to be converted
1666 @param unsigned_flag type of integer to which we convert val
1667 @param has_overflow true if there is overflow
1668
1669 @return
1670 value converted from val
1671 */
convert_decimal2longlong(const my_decimal * val,bool unsigned_flag,bool * has_overflow)1672 longlong Field::convert_decimal2longlong(const my_decimal *val,
1673 bool unsigned_flag,
1674 bool *has_overflow)
1675 {
1676 if (unsigned_flag && val->sign())
1677 {
1678 // Converting a signed decimal to unsigned int
1679 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1680 *has_overflow= true;
1681 return 0;
1682 }
1683
1684 longlong val_ll;
1685 int conversion_error= my_decimal2int(E_DEC_ERROR & ~E_DEC_OVERFLOW
1686 & ~E_DEC_TRUNCATED,
1687 val, unsigned_flag, &val_ll);
1688
1689 if (warn_if_overflow(conversion_error))
1690 {
1691 *has_overflow= true;
1692 if (unsigned_flag)
1693 return ULONGLONG_MAX;
1694
1695 return (val->sign() ? LONGLONG_MIN : LONGLONG_MAX);
1696 }
1697
1698 return val_ll;
1699 }
1700
1701
1702 /**
1703 Storing decimal in integer fields.
1704
1705 @param val value for storing
1706
1707 @note
1708 This method is used by all integer fields, real/decimal redefine it
1709
1710 @retval TYPE_OK Storage of value went fine without warnings or errors
1711 @retval !TYPE_OK Warning/error as indicated by type_conversion_status enum
1712 value
1713 */
store_decimal(const my_decimal * val)1714 type_conversion_status Field_num::store_decimal(const my_decimal *val)
1715 {
1716 ASSERT_COLUMN_MARKED_FOR_WRITE;
1717 bool has_overflow= false;
1718 longlong i= convert_decimal2longlong(val, unsigned_flag, &has_overflow);
1719 const type_conversion_status res= store(i, unsigned_flag);
1720 return has_overflow ? TYPE_WARN_OUT_OF_RANGE : res;
1721 }
1722
1723
1724 /**
1725 Return decimal value of integer field.
1726
1727 @param decimal_value buffer for storing decimal value
1728
1729 @note
1730 This method is used by all integer fields, real/decimal redefine it.
1731 All longlong values fit in our decimal buffer which cal store 8*9=72
1732 digits of integer number
1733
1734 @return
1735 pointer to decimal buffer with value of field
1736 */
1737
val_decimal(my_decimal * decimal_value)1738 my_decimal* Field_num::val_decimal(my_decimal *decimal_value)
1739 {
1740 ASSERT_COLUMN_MARKED_FOR_READ;
1741 DBUG_ASSERT(result_type() == INT_RESULT);
1742 longlong nr= val_int();
1743 int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
1744 return decimal_value;
1745 }
1746
1747
get_date(MYSQL_TIME * ltime,uint fuzzydate)1748 bool Field_num::get_date(MYSQL_TIME *ltime, uint fuzzydate)
1749 {
1750 DBUG_ASSERT(result_type() == INT_RESULT);
1751 return my_longlong_to_datetime_with_warn(val_int(), ltime, fuzzydate);
1752 }
1753
1754
get_time(MYSQL_TIME * ltime)1755 bool Field_num::get_time(MYSQL_TIME *ltime)
1756 {
1757 DBUG_ASSERT(result_type() == INT_RESULT);
1758 return my_longlong_to_time_with_warn(val_int(), ltime);
1759 }
1760
1761
Field_str(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,utype unireg_check_arg,const char * field_name_arg,const CHARSET_INFO * charset_arg)1762 Field_str::Field_str(uchar *ptr_arg,uint32 len_arg, uchar *null_ptr_arg,
1763 uchar null_bit_arg, utype unireg_check_arg,
1764 const char *field_name_arg,
1765 const CHARSET_INFO *charset_arg)
1766 :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
1767 unireg_check_arg, field_name_arg)
1768 {
1769 field_charset= charset_arg;
1770 if (charset_arg->state & MY_CS_BINSORT)
1771 flags|=BINARY_FLAG;
1772 field_derivation= DERIVATION_IMPLICIT;
1773 }
1774
1775
make_field(Send_field * field)1776 void Field_str::make_field(Send_field *field)
1777 {
1778 Field::make_field(field);
1779 field->decimals= 0;
1780 }
1781
1782 /**
1783 Decimal representation of Field_str.
1784
1785 @param d value for storing
1786
1787 @note
1788 Field_str is the base class for fields implemeting
1789 [VAR]CHAR, VAR[BINARY], BLOB/TEXT, GEOMETRY.
1790 String value should be converted to floating point value according
1791 our rules, so we use double to store value of decimal in string.
1792
1793 @todo
1794 use decimal2string?
1795
1796 @retval
1797 0 OK
1798 @retval
1799 !=0 error
1800 */
1801
store_decimal(const my_decimal * d)1802 type_conversion_status Field_str::store_decimal(const my_decimal *d)
1803 {
1804 ASSERT_COLUMN_MARKED_FOR_WRITE;
1805 double val;
1806 /* TODO: use decimal2string? */
1807 int err= my_decimal2double(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, d, &val);
1808 warn_if_overflow(err);
1809 const type_conversion_status res= store(val);
1810
1811 return (err != E_DEC_OK) ? decimal_err_to_type_conv_status(err) : res;
1812 }
1813
1814
fill_cache_field(CACHE_FIELD * copy)1815 uint Field::fill_cache_field(CACHE_FIELD *copy)
1816 {
1817 uint store_length;
1818 copy->str= ptr;
1819 copy->length= pack_length();
1820 copy->field= this;
1821 if (flags & BLOB_FLAG)
1822 {
1823 copy->type= CACHE_BLOB;
1824 copy->length-= portable_sizeof_char_ptr;
1825 return copy->length;
1826 }
1827 else if (!zero_pack() &&
1828 (type() == MYSQL_TYPE_STRING && copy->length >= 4 &&
1829 copy->length < 256))
1830 {
1831 copy->type= CACHE_STRIPPED; /* Remove end space */
1832 store_length= 2;
1833 }
1834 else if (type() == MYSQL_TYPE_VARCHAR)
1835 {
1836 copy->type= pack_length()-row_pack_length() == 1 ? CACHE_VARSTR1:
1837 CACHE_VARSTR2;
1838 store_length= 0;
1839 }
1840 else
1841 {
1842 copy->type= 0;
1843 store_length= 0;
1844 }
1845 return copy->length + store_length;
1846 }
1847
1848
get_date(MYSQL_TIME * ltime,uint fuzzydate)1849 bool Field::get_date(MYSQL_TIME *ltime,uint fuzzydate)
1850 {
1851 char buff[MAX_DATE_STRING_REP_LENGTH];
1852 String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1853 return !(res= val_str(&tmp)) ||
1854 str_to_datetime_with_warn(res, ltime, fuzzydate);
1855 }
1856
1857
get_time(MYSQL_TIME * ltime)1858 bool Field::get_time(MYSQL_TIME *ltime)
1859 {
1860 char buff[MAX_DATE_STRING_REP_LENGTH];
1861 String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1862 return !(res= val_str(&tmp)) || str_to_time_with_warn(res, ltime);
1863 }
1864
1865
get_timestamp(struct timeval * tm,int * warnings)1866 bool Field::get_timestamp(struct timeval *tm, int *warnings)
1867 {
1868 MYSQL_TIME ltime;
1869 DBUG_ASSERT(!is_null());
1870 return get_date(<ime, TIME_FUZZY_DATE) ||
1871 datetime_to_timeval(current_thd, <ime, tm, warnings);
1872 }
1873
1874
1875 /**
1876 This is called when storing a date in a string.
1877
1878 @note
1879 Needs to be changed if/when we want to support different time formats.
1880 */
1881
store_time(MYSQL_TIME * ltime,uint8 dec_arg)1882 type_conversion_status Field::store_time(MYSQL_TIME *ltime, uint8 dec_arg)
1883 {
1884 ASSERT_COLUMN_MARKED_FOR_WRITE;
1885 char buff[MAX_DATE_STRING_REP_LENGTH];
1886 uint length= (uint) my_TIME_to_str(ltime, buff,
1887 MY_MIN(dec_arg, DATETIME_MAX_DECIMALS));
1888 /* Avoid conversion when field character set is ASCII compatible */
1889 return store(buff, length, (charset()->state & MY_CS_NONASCII) ?
1890 &my_charset_latin1 : charset());
1891 }
1892
1893
optimize_range(uint idx,uint part)1894 bool Field::optimize_range(uint idx, uint part)
1895 {
1896 return MY_TEST(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
1897 }
1898
1899
new_field(MEM_ROOT * root,TABLE * new_table,bool keep_type MY_ATTRIBUTE ((unused)))1900 Field *Field::new_field(MEM_ROOT *root, TABLE *new_table,
1901 bool keep_type MY_ATTRIBUTE((unused)))
1902 {
1903 Field *tmp= clone(root);
1904 if (tmp == NULL)
1905 return 0;
1906
1907 if (tmp->table->maybe_null)
1908 tmp->flags&= ~NOT_NULL_FLAG;
1909 tmp->table= new_table;
1910 tmp->key_start.init(0);
1911 tmp->part_of_key.init(0);
1912 tmp->part_of_sortkey.init(0);
1913 /*
1914 todo: We should never alter unireg_check after an object is constructed,
1915 and the member should be made const. But a lot of code depends upon this
1916 hack, and the different utype values are completely unrelated so we can
1917 never be quite sure which parts of the server will break.
1918 */
1919 tmp->unireg_check= Field::NONE;
1920 /* COMPRESSED column format flag must not be cleared here */
1921 bool has_compressed_flag =
1922 (tmp->column_format() == COLUMN_FORMAT_TYPE_COMPRESSED);
1923 tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG |
1924 ZEROFILL_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1925 if (has_compressed_flag)
1926 tmp->set_column_format(COLUMN_FORMAT_TYPE_COMPRESSED);
1927 tmp->reset_fields();
1928 return tmp;
1929 }
1930
1931
new_key_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr,uchar * new_null_ptr,uint new_null_bit)1932 Field *Field::new_key_field(MEM_ROOT *root, TABLE *new_table,
1933 uchar *new_ptr, uchar *new_null_ptr,
1934 uint new_null_bit)
1935 {
1936 Field *tmp;
1937 if ((tmp= new_field(root, new_table, table == new_table)))
1938 {
1939 tmp->ptr= new_ptr;
1940 tmp->null_ptr= new_null_ptr;
1941 tmp->null_bit= new_null_bit;
1942 }
1943 return tmp;
1944 }
1945
evaluate_insert_default_function()1946 void Field::evaluate_insert_default_function()
1947 {
1948 if (has_insert_default_function())
1949 Item_func_now_local::store_in(this);
1950 }
1951
evaluate_update_default_function()1952 void Field::evaluate_update_default_function()
1953 {
1954 if (has_update_default_function())
1955 Item_func_now_local::store_in(this);
1956 }
1957
1958 /****************************************************************************
1959 Field_null, a field that always return NULL
1960 ****************************************************************************/
1961
sql_type(String & res) const1962 void Field_null::sql_type(String &res) const
1963 {
1964 res.set_ascii(STRING_WITH_LEN("null"));
1965 }
1966
1967
1968 /****************************************************************************
1969 Functions for the Field_decimal class
1970 This is an number stored as a pre-space (or pre-zero) string
1971 ****************************************************************************/
1972
reset(void)1973 type_conversion_status Field_decimal::reset(void)
1974 {
1975 Field_decimal::store(STRING_WITH_LEN("0"),&my_charset_bin);
1976 return TYPE_OK;
1977 }
1978
overflow(bool negative)1979 void Field_decimal::overflow(bool negative)
1980 {
1981 uint len=field_length;
1982 uchar *to=ptr, filler= '9';
1983
1984 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
1985 if (negative)
1986 {
1987 if (!unsigned_flag)
1988 {
1989 /* Put - sign as a first digit so we'll have -999..999 or 999..999 */
1990 *to++ = '-';
1991 len--;
1992 }
1993 else
1994 {
1995 filler= '0'; // Fill up with 0
1996 if (!zerofill)
1997 {
1998 /*
1999 Handle unsigned integer without zerofill, in which case
2000 the number should be of format ' 0' or ' 0.000'
2001 */
2002 uint whole_part=field_length- (dec ? dec+2 : 1);
2003 // Fill with spaces up to the first digit
2004 memset(to, ' ', whole_part);
2005 to+= whole_part;
2006 len-= whole_part;
2007 // The main code will also handle the 0 before the decimal point
2008 }
2009 }
2010 }
2011 memset(to, filler, len);
2012 if (dec)
2013 ptr[field_length-dec-1]='.';
2014 return;
2015 }
2016
2017
store(const char * from_arg,uint len,const CHARSET_INFO * cs)2018 type_conversion_status Field_decimal::store(const char *from_arg, uint len,
2019 const CHARSET_INFO *cs)
2020 {
2021 ASSERT_COLUMN_MARKED_FOR_WRITE;
2022 char buff[STRING_BUFFER_USUAL_SIZE];
2023 String tmp(buff,sizeof(buff), &my_charset_bin);
2024 const uchar *from= (uchar*) from_arg;
2025
2026 /* Convert character set if the old one is multi uchar */
2027 if (cs->mbmaxlen > 1)
2028 {
2029 uint dummy_errors;
2030 tmp.copy((char*) from, len, cs, &my_charset_bin, &dummy_errors);
2031 from= (uchar*) tmp.ptr();
2032 len= tmp.length();
2033 }
2034
2035 const uchar *end= from+len;
2036 /* The pointer where the field value starts (i.e., "where to write") */
2037 uchar *to= ptr;
2038 uint tmp_dec, tmp_uint;
2039 /*
2040 The sign of the number : will be 0 (means positive but sign not
2041 specified), '+' or '-'
2042 */
2043 uchar sign_char=0;
2044 /* The pointers where prezeros start and stop */
2045 const uchar *pre_zeros_from, *pre_zeros_end;
2046 /* The pointers where digits at the left of '.' start and stop */
2047 const uchar *int_digits_from, *int_digits_end;
2048 /* The pointers where digits at the right of '.' start and stop */
2049 const uchar *frac_digits_from, *frac_digits_end;
2050 /* The sign of the exponent : will be 0 (means no exponent), '+' or '-' */
2051 char expo_sign_char=0;
2052 uint exponent=0; // value of the exponent
2053 /*
2054 Pointers used when digits move from the left of the '.' to the
2055 right of the '.' (explained below)
2056 */
2057 const uchar *UNINIT_VAR(int_digits_tail_from);
2058 /* Number of 0 that need to be added at the left of the '.' (1E3: 3 zeros) */
2059 uint UNINIT_VAR(int_digits_added_zeros);
2060 /*
2061 Pointer used when digits move from the right of the '.' to the left
2062 of the '.'
2063 */
2064 const uchar *UNINIT_VAR(frac_digits_head_end);
2065 /* Number of 0 that need to be added at the right of the '.' (for 1E-3) */
2066 uint UNINIT_VAR(frac_digits_added_zeros);
2067 uchar *pos,*tmp_left_pos,*tmp_right_pos;
2068 /* Pointers that are used as limits (begin and end of the field buffer) */
2069 uchar *left_wall,*right_wall;
2070 uchar tmp_char;
2071 /*
2072 To remember if table->in_use->cuted_fields has already been incremented,
2073 to do that only once
2074 */
2075 bool is_cuted_fields_incr=0;
2076
2077 /*
2078 There are three steps in this function :
2079 - parse the input string
2080 - modify the position of digits around the decimal dot '.'
2081 according to the exponent value (if specified)
2082 - write the formatted number
2083 */
2084
2085 if ((tmp_dec=dec))
2086 tmp_dec++;
2087
2088 /* skip pre-space */
2089 while (from != end && my_isspace(&my_charset_bin,*from))
2090 from++;
2091 if (from == end)
2092 {
2093 set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
2094 is_cuted_fields_incr=1;
2095 }
2096 else if (*from == '+' || *from == '-') // Found some sign ?
2097 {
2098 sign_char= *from++;
2099 /*
2100 We allow "+" for unsigned decimal unless defined different
2101 Both options allowed as one may wish not to have "+" for unsigned numbers
2102 because of data processing issues
2103 */
2104 if (unsigned_flag)
2105 {
2106 if (sign_char=='-')
2107 {
2108 Field_decimal::overflow(1);
2109 return TYPE_WARN_OUT_OF_RANGE;
2110 }
2111 /*
2112 Defining this will not store "+" for unsigned decimal type even if
2113 it is passed in numeric string. This will make some tests to fail
2114 */
2115 #ifdef DONT_ALLOW_UNSIGNED_PLUS
2116 else
2117 sign_char=0;
2118 #endif
2119 }
2120 }
2121
2122 pre_zeros_from= from;
2123 for (; from!=end && *from == '0'; from++) ; // Read prezeros
2124 pre_zeros_end=int_digits_from=from;
2125 /* Read non zero digits at the left of '.'*/
2126 for (; from != end && my_isdigit(&my_charset_bin, *from) ; from++) ;
2127 int_digits_end=from;
2128 if (from!=end && *from == '.') // Some '.' ?
2129 from++;
2130 frac_digits_from= from;
2131 /* Read digits at the right of '.' */
2132 for (;from!=end && my_isdigit(&my_charset_bin, *from); from++) ;
2133 frac_digits_end=from;
2134 // Some exponentiation symbol ?
2135 if (from != end && (*from == 'e' || *from == 'E'))
2136 {
2137 from++;
2138 if (from != end && (*from == '+' || *from == '-')) // Some exponent sign ?
2139 expo_sign_char= *from++;
2140 else
2141 expo_sign_char= '+';
2142 /*
2143 Read digits of the exponent and compute its value. We must care about
2144 'exponent' overflow, because as unsigned arithmetic is "modulo", big
2145 exponents will become small (e.g. 1e4294967296 will become 1e0, and the
2146 field will finally contain 1 instead of its max possible value).
2147 */
2148 for (;from!=end && my_isdigit(&my_charset_bin, *from); from++)
2149 {
2150 exponent=10*exponent+(*from-'0');
2151 if (exponent>MAX_EXPONENT)
2152 break;
2153 }
2154 }
2155
2156 /*
2157 We only have to generate warnings if count_cuted_fields is set.
2158 This is to avoid extra checks of the number when they are not needed.
2159 Even if this flag is not set, it's OK to increment warnings, if
2160 it makes the code easer to read.
2161 */
2162
2163 if (table->in_use->count_cuted_fields)
2164 {
2165 // Skip end spaces
2166 for (;from != end && my_isspace(&my_charset_bin, *from); from++) ;
2167 if (from != end) // If still something left, warn
2168 {
2169 set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
2170 is_cuted_fields_incr=1;
2171 }
2172 }
2173
2174 /*
2175 Now "move" digits around the decimal dot according to the exponent value,
2176 and add necessary zeros.
2177 Examples :
2178 - 1E+3 : needs 3 more zeros at the left of '.' (int_digits_added_zeros=3)
2179 - 1E-3 : '1' moves at the right of '.', and 2 more zeros are needed
2180 between '.' and '1'
2181 - 1234.5E-3 : '234' moves at the right of '.'
2182 These moves are implemented with pointers which point at the begin
2183 and end of each moved segment. Examples :
2184 - 1234.5E-3 : before the code below is executed, the int_digits part is
2185 from '1' to '4' and the frac_digits part from '5' to '5'. After the code
2186 below, the int_digits part is from '1' to '1', the frac_digits_head
2187 part is from '2' to '4', and the frac_digits part from '5' to '5'.
2188 - 1234.5E3 : before the code below is executed, the int_digits part is
2189 from '1' to '4' and the frac_digits part from '5' to '5'. After the code
2190 below, the int_digits part is from '1' to '4', the int_digits_tail
2191 part is from '5' to '5', the frac_digits part is empty, and
2192 int_digits_added_zeros=2 (to make 1234500).
2193 */
2194
2195 /*
2196 Below tmp_uint cannot overflow with small enough MAX_EXPONENT setting,
2197 as int_digits_added_zeros<=exponent<4G and
2198 (int_digits_end-int_digits_from)<=max_allowed_packet<=2G and
2199 (frac_digits_from-int_digits_tail_from)<=max_allowed_packet<=2G
2200 */
2201
2202 if (!expo_sign_char)
2203 tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);
2204 else if (expo_sign_char == '-')
2205 {
2206 tmp_uint=min(exponent,(uint)(int_digits_end-int_digits_from));
2207 frac_digits_added_zeros=exponent-tmp_uint;
2208 int_digits_end -= tmp_uint;
2209 frac_digits_head_end=int_digits_end+tmp_uint;
2210 tmp_uint=tmp_dec+(uint)(int_digits_end-int_digits_from);
2211 }
2212 else // (expo_sign_char=='+')
2213 {
2214 tmp_uint=min(exponent,(uint)(frac_digits_end-frac_digits_from));
2215 int_digits_added_zeros=exponent-tmp_uint;
2216 int_digits_tail_from=frac_digits_from;
2217 frac_digits_from=frac_digits_from+tmp_uint;
2218 /*
2219 We "eat" the heading zeros of the
2220 int_digits.int_digits_tail.int_digits_added_zeros concatenation
2221 (for example 0.003e3 must become 3 and not 0003)
2222 */
2223 if (int_digits_from == int_digits_end)
2224 {
2225 /*
2226 There was nothing in the int_digits part, so continue
2227 eating int_digits_tail zeros
2228 */
2229 for (; int_digits_tail_from != frac_digits_from &&
2230 *int_digits_tail_from == '0'; int_digits_tail_from++) ;
2231 if (int_digits_tail_from == frac_digits_from)
2232 {
2233 // there were only zeros in int_digits_tail too
2234 int_digits_added_zeros=0;
2235 }
2236 }
2237 tmp_uint= (uint) (tmp_dec+(int_digits_end-int_digits_from)+
2238 (uint)(frac_digits_from-int_digits_tail_from)+
2239 int_digits_added_zeros);
2240 }
2241
2242 /*
2243 Now write the formated number
2244
2245 First the digits of the int_% parts.
2246 Do we have enough room to write these digits ?
2247 If the sign is defined and '-', we need one position for it
2248 */
2249
2250 if (field_length < tmp_uint + (int) (sign_char == '-'))
2251 {
2252 // too big number, change to max or min number
2253 Field_decimal::overflow(sign_char == '-');
2254 return TYPE_WARN_OUT_OF_RANGE;
2255 }
2256
2257 /*
2258 Tmp_left_pos is the position where the leftmost digit of
2259 the int_% parts will be written
2260 */
2261 tmp_left_pos=pos=to+(uint)(field_length-tmp_uint);
2262
2263 // Write all digits of the int_% parts
2264 while (int_digits_from != int_digits_end)
2265 *pos++ = *int_digits_from++ ;
2266
2267 if (expo_sign_char == '+')
2268 {
2269 while (int_digits_tail_from != frac_digits_from)
2270 *pos++= *int_digits_tail_from++;
2271 while (int_digits_added_zeros-- >0)
2272 *pos++= '0';
2273 }
2274 /*
2275 Note the position where the rightmost digit of the int_% parts has been
2276 written (this is to later check if the int_% parts contained nothing,
2277 meaning an extra 0 is needed).
2278 */
2279 tmp_right_pos=pos;
2280
2281 /*
2282 Step back to the position of the leftmost digit of the int_% parts,
2283 to write sign and fill with zeros or blanks or prezeros.
2284 */
2285 pos=tmp_left_pos-1;
2286 if (zerofill)
2287 {
2288 left_wall=to-1;
2289 while (pos > left_wall) // Fill with zeros
2290 *pos--='0';
2291 }
2292 else
2293 {
2294 left_wall=to+(sign_char != 0)-1;
2295 if (!expo_sign_char) // If exponent was specified, ignore prezeros
2296 {
2297 for (;pos > left_wall && pre_zeros_from !=pre_zeros_end;
2298 pre_zeros_from++)
2299 *pos--= '0';
2300 }
2301 if (pos == tmp_right_pos-1)
2302 *pos--= '0'; // no 0 has ever been written, so write one
2303 left_wall= to-1;
2304 if (sign_char && pos != left_wall)
2305 {
2306 /* Write sign if possible (it is if sign is '-') */
2307 *pos--= sign_char;
2308 }
2309 while (pos != left_wall)
2310 *pos--=' '; //fill with blanks
2311 }
2312
2313 /*
2314 Write digits of the frac_% parts ;
2315 Depending on table->in_use->count_cutted_fields, we may also want
2316 to know if some non-zero tail of these parts will
2317 be truncated (for example, 0.002->0.00 will generate a warning,
2318 while 0.000->0.00 will not)
2319 (and 0E1000000000 will not, while 1E-1000000000 will)
2320 */
2321
2322 pos=to+(uint)(field_length-tmp_dec); // Calculate post to '.'
2323 right_wall=to+field_length;
2324 if (pos != right_wall)
2325 *pos++='.';
2326
2327 if (expo_sign_char == '-')
2328 {
2329 while (frac_digits_added_zeros-- > 0)
2330 {
2331 if (pos == right_wall)
2332 {
2333 if (table->in_use->count_cuted_fields && !is_cuted_fields_incr)
2334 break; // Go on below to see if we lose non zero digits
2335 return TYPE_OK;
2336 }
2337 *pos++='0';
2338 }
2339 while (int_digits_end != frac_digits_head_end)
2340 {
2341 tmp_char= *int_digits_end++;
2342 if (pos == right_wall)
2343 {
2344 if (tmp_char != '0') // Losing a non zero digit ?
2345 {
2346 if (!is_cuted_fields_incr)
2347 set_warning(Sql_condition::WARN_LEVEL_WARN,
2348 WARN_DATA_TRUNCATED, 1);
2349 return TYPE_OK;
2350 }
2351 continue;
2352 }
2353 *pos++= tmp_char;
2354 }
2355 }
2356
2357 for (;frac_digits_from!=frac_digits_end;)
2358 {
2359 tmp_char= *frac_digits_from++;
2360 if (pos == right_wall)
2361 {
2362 if (tmp_char != '0') // Losing a non zero digit ?
2363 {
2364 if (!is_cuted_fields_incr)
2365 {
2366 /*
2367 This is a note, not a warning, as we don't want to abort
2368 when we cut decimals in strict mode
2369 */
2370 set_warning(Sql_condition::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
2371 }
2372 return TYPE_OK;
2373 }
2374 continue;
2375 }
2376 *pos++= tmp_char;
2377 }
2378
2379 while (pos != right_wall)
2380 *pos++='0'; // Fill with zeros at right of '.'
2381 return TYPE_OK;
2382 }
2383
2384
store(double nr)2385 type_conversion_status Field_decimal::store(double nr)
2386 {
2387 ASSERT_COLUMN_MARKED_FOR_WRITE;
2388 if (unsigned_flag && nr < 0)
2389 {
2390 overflow(1);
2391 return TYPE_WARN_OUT_OF_RANGE;
2392 }
2393
2394 if (!my_isfinite(nr)) // Handle infinity as special case
2395 {
2396 overflow(nr < 0.0);
2397 return TYPE_WARN_OUT_OF_RANGE;
2398 }
2399
2400 uint i;
2401 size_t length;
2402 uchar fyllchar,*to;
2403 char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
2404
2405 fyllchar = zerofill ? (char) '0' : (char) ' ';
2406 length= my_fcvt(nr, dec, buff, NULL);
2407
2408 if (length > field_length)
2409 {
2410 overflow(nr < 0.0);
2411 return TYPE_WARN_OUT_OF_RANGE;
2412 }
2413 else
2414 {
2415 to=ptr;
2416 for (i=field_length-length ; i-- > 0 ;)
2417 *to++ = fyllchar;
2418 memcpy(to,buff,length);
2419 return TYPE_OK;
2420 }
2421 }
2422
2423
store(longlong nr,bool unsigned_val)2424 type_conversion_status Field_decimal::store(longlong nr, bool unsigned_val)
2425 {
2426 ASSERT_COLUMN_MARKED_FOR_WRITE;
2427 char buff[22];
2428 uint length, int_part;
2429 char fyllchar;
2430 uchar *to;
2431
2432 if (nr < 0 && unsigned_flag && !unsigned_val)
2433 {
2434 overflow(1);
2435 return TYPE_WARN_OUT_OF_RANGE;
2436 }
2437 length= (uint) (longlong10_to_str(nr,buff,unsigned_val ? 10 : -10) - buff);
2438 int_part= field_length- (dec ? dec+1 : 0);
2439
2440 if (length > int_part)
2441 {
2442 overflow(!unsigned_val && nr < 0L); /* purecov: inspected */
2443 return TYPE_WARN_OUT_OF_RANGE;
2444 }
2445
2446 fyllchar = zerofill ? (char) '0' : (char) ' ';
2447 to= ptr;
2448 for (uint i=int_part-length ; i-- > 0 ;)
2449 *to++ = fyllchar;
2450 memcpy(to,buff,length);
2451 if (dec)
2452 {
2453 to[length]='.';
2454 memset(to + length + 1, '0', dec);
2455 }
2456 return TYPE_OK;
2457 }
2458
2459
val_real(void)2460 double Field_decimal::val_real(void)
2461 {
2462 ASSERT_COLUMN_MARKED_FOR_READ;
2463 int not_used;
2464 char *end_not_used;
2465 return my_strntod(&my_charset_bin, (char*) ptr, field_length, &end_not_used,
2466 ¬_used);
2467 }
2468
val_int(void)2469 longlong Field_decimal::val_int(void)
2470 {
2471 ASSERT_COLUMN_MARKED_FOR_READ;
2472 int not_used;
2473 if (unsigned_flag)
2474 return my_strntoull(&my_charset_bin, (char*) ptr, field_length, 10, NULL,
2475 ¬_used);
2476 return my_strntoll(&my_charset_bin, (char*) ptr, field_length, 10, NULL,
2477 ¬_used);
2478 }
2479
2480
val_str(String * val_buffer MY_ATTRIBUTE ((unused)),String * val_ptr)2481 String *Field_decimal::val_str(String *val_buffer MY_ATTRIBUTE((unused)),
2482 String *val_ptr)
2483 {
2484 ASSERT_COLUMN_MARKED_FOR_READ;
2485 uchar *str;
2486 size_t tmp_length;
2487
2488 for (str=ptr ; *str == ' ' ; str++) ;
2489 val_ptr->set_charset(&my_charset_numeric);
2490 tmp_length= (size_t) (str-ptr);
2491 if (field_length < tmp_length) // Error in data
2492 val_ptr->length(0);
2493 else
2494 val_ptr->set_ascii((const char*) str, field_length-tmp_length);
2495 return val_ptr;
2496 }
2497
2498 /**
2499 Should be able to handle at least the following fixed decimal formats:
2500 5.00 , -1.0, 05, -05, +5 with optional pre/end space
2501 */
2502
cmp(const uchar * a_ptr,const uchar * b_ptr)2503 int Field_decimal::cmp(const uchar *a_ptr,const uchar *b_ptr)
2504 {
2505 const uchar *end;
2506 int swap=0;
2507 /* First remove prefixes '0', ' ', and '-' */
2508 for (end=a_ptr+field_length;
2509 a_ptr != end &&
2510 (*a_ptr == *b_ptr ||
2511 ((my_isspace(&my_charset_bin,*a_ptr) || *a_ptr == '+' ||
2512 *a_ptr == '0') &&
2513 (my_isspace(&my_charset_bin,*b_ptr) || *b_ptr == '+' ||
2514 *b_ptr == '0')));
2515 a_ptr++,b_ptr++)
2516 {
2517 if (*a_ptr == '-') // If both numbers are negative
2518 swap= -1 ^ 1; // Swap result
2519 }
2520 if (a_ptr == end)
2521 return 0;
2522 if (*a_ptr == '-')
2523 return -1;
2524 if (*b_ptr == '-')
2525 return 1;
2526
2527 while (a_ptr != end)
2528 {
2529 if (*a_ptr++ != *b_ptr++)
2530 return swap ^ (a_ptr[-1] < b_ptr[-1] ? -1 : 1); // compare digits
2531 }
2532 return 0;
2533 }
2534
2535
make_sort_key(uchar * to,uint length)2536 void Field_decimal::make_sort_key(uchar *to, uint length)
2537 {
2538 uchar *str,*end;
2539 for (str=ptr,end=ptr+length;
2540 str != end &&
2541 ((my_isspace(&my_charset_bin,*str) || *str == '+' ||
2542 *str == '0')) ;
2543 str++)
2544 *to++=' ';
2545 if (str == end)
2546 return; /* purecov: inspected */
2547
2548 if (*str == '-')
2549 {
2550 *to++=1; // Smaller than any number
2551 str++;
2552 while (str != end)
2553 if (my_isdigit(&my_charset_bin,*str))
2554 *to++= (char) ('9' - *str++);
2555 else
2556 *to++= *str++;
2557 }
2558 else memcpy(to,str,(uint) (end-str));
2559 }
2560
2561
sql_type(String & res) const2562 void Field_decimal::sql_type(String &res) const
2563 {
2564 const CHARSET_INFO *cs=res.charset();
2565 uint tmp=field_length;
2566 if (!unsigned_flag)
2567 tmp--;
2568 if (dec)
2569 tmp--;
2570 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
2571 "decimal(%d,%d)",tmp,dec));
2572 add_zerofill_and_unsigned(res);
2573 }
2574
2575
2576 /****************************************************************************
2577 ** Field_new_decimal
2578 ****************************************************************************/
2579
Field_new_decimal(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const char * field_name_arg,uint8 dec_arg,bool zero_arg,bool unsigned_arg)2580 Field_new_decimal::Field_new_decimal(uchar *ptr_arg,
2581 uint32 len_arg, uchar *null_ptr_arg,
2582 uchar null_bit_arg,
2583 enum utype unireg_check_arg,
2584 const char *field_name_arg,
2585 uint8 dec_arg,bool zero_arg,
2586 bool unsigned_arg)
2587 :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
2588 unireg_check_arg, field_name_arg, dec_arg, zero_arg, unsigned_arg)
2589 {
2590 precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
2591 set_if_smaller(precision, DECIMAL_MAX_PRECISION);
2592 DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
2593 (dec <= DECIMAL_MAX_SCALE));
2594 bin_size= my_decimal_get_binary_size(precision, dec);
2595 }
2596
2597
Field_new_decimal(uint32 len_arg,bool maybe_null_arg,const char * name,uint8 dec_arg,bool unsigned_arg)2598 Field_new_decimal::Field_new_decimal(uint32 len_arg,
2599 bool maybe_null_arg,
2600 const char *name,
2601 uint8 dec_arg,
2602 bool unsigned_arg)
2603 :Field_num((uchar*) 0, len_arg,
2604 maybe_null_arg ? (uchar*) "": 0, 0,
2605 NONE, name, dec_arg, 0, unsigned_arg)
2606 {
2607 precision= my_decimal_length_to_precision(len_arg, dec_arg, unsigned_arg);
2608 set_if_smaller(precision, DECIMAL_MAX_PRECISION);
2609 DBUG_ASSERT((precision <= DECIMAL_MAX_PRECISION) &&
2610 (dec <= DECIMAL_MAX_SCALE));
2611 bin_size= my_decimal_get_binary_size(precision, dec);
2612 }
2613
2614
create_from_item(Item * item)2615 Field *Field_new_decimal::create_from_item (Item *item)
2616 {
2617 uint8 dec= item->decimals;
2618 uint8 intg= item->decimal_precision() - dec;
2619 uint32 len= item->max_char_length();
2620
2621 DBUG_ASSERT (item->result_type() == DECIMAL_RESULT);
2622
2623 /*
2624 Trying to put too many digits overall in a DECIMAL(prec,dec)
2625 will always throw a warning. We must limit dec to
2626 DECIMAL_MAX_SCALE however to prevent an assert() later.
2627 */
2628
2629 if (dec > 0)
2630 {
2631 signed int overflow;
2632
2633 dec= min<int>(dec, DECIMAL_MAX_SCALE);
2634
2635 /*
2636 If the value still overflows the field with the corrected dec,
2637 we'll throw out decimals rather than integers. This is still
2638 bad and of course throws a truncation warning.
2639 +1: for decimal point
2640 */
2641
2642 const int required_length=
2643 my_decimal_precision_to_length(intg + dec, dec,
2644 item->unsigned_flag);
2645
2646 overflow= required_length - len;
2647
2648 if (overflow > 0)
2649 dec= max(0, dec - overflow); // too long, discard fract
2650 else
2651 /* Corrected value fits. */
2652 len= required_length;
2653 }
2654 return new Field_new_decimal(len, item->maybe_null, item->item_name.ptr(),
2655 dec, item->unsigned_flag);
2656 }
2657
2658
reset(void)2659 type_conversion_status Field_new_decimal::reset(void)
2660 {
2661 store_value(&decimal_zero);
2662 return TYPE_OK;
2663 }
2664
2665
2666 /**
2667 Generate max/min decimal value in case of overflow.
2668
2669 @param decimal_value buffer for value
2670 @param sign sign of value which caused overflow
2671 */
2672
set_value_on_overflow(my_decimal * decimal_value,bool sign)2673 void Field_new_decimal::set_value_on_overflow(my_decimal *decimal_value,
2674 bool sign)
2675 {
2676 DBUG_ENTER("Field_new_decimal::set_value_on_overflow");
2677 max_my_decimal(decimal_value, precision, decimals());
2678 if (sign)
2679 {
2680 if (unsigned_flag)
2681 my_decimal_set_zero(decimal_value);
2682 else
2683 decimal_value->sign(TRUE);
2684 }
2685 DBUG_VOID_RETURN;
2686 }
2687
2688
2689 /**
2690 Store decimal value in the binary buffer.
2691
2692 Checks if decimal_value fits into field size.
2693 If it does, stores the decimal in the buffer using binary format.
2694 Otherwise sets maximal number that can be stored in the field.
2695
2696 @param decimal_value my_decimal
2697
2698 @retval
2699 0 ok
2700 @retval
2701 1 error
2702 */
2703 type_conversion_status
store_value(const my_decimal * decimal_value)2704 Field_new_decimal::store_value(const my_decimal *decimal_value)
2705 {
2706 ASSERT_COLUMN_MARKED_FOR_WRITE;
2707 type_conversion_status error= TYPE_OK;
2708 DBUG_ENTER("Field_new_decimal::store_value");
2709 #ifndef DBUG_OFF
2710 {
2711 char dbug_buff[DECIMAL_MAX_STR_LENGTH+2];
2712 DBUG_PRINT("enter", ("value: %s", dbug_decimal_as_string(dbug_buff, decimal_value)));
2713 }
2714 #endif
2715
2716 /* check that we do not try to write negative value in unsigned field */
2717 if (unsigned_flag && decimal_value->sign())
2718 {
2719 DBUG_PRINT("info", ("unsigned overflow"));
2720 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
2721 error= TYPE_WARN_OUT_OF_RANGE;
2722 decimal_value= &decimal_zero;
2723 }
2724 #ifndef DBUG_OFF
2725 {
2726 char dbug_buff[DECIMAL_MAX_STR_LENGTH+2];
2727 DBUG_PRINT("info", ("saving with precision %d scale: %d value %s",
2728 (int)precision, (int)dec,
2729 dbug_decimal_as_string(dbug_buff, decimal_value)));
2730 }
2731 #endif
2732
2733 int err= my_decimal2binary(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
2734 decimal_value, ptr, precision, dec);
2735 if (warn_if_overflow(err))
2736 {
2737 my_decimal buff;
2738 DBUG_PRINT("info", ("overflow"));
2739 set_value_on_overflow(&buff, decimal_value->sign());
2740 my_decimal2binary(E_DEC_FATAL_ERROR, &buff, ptr, precision, dec);
2741 }
2742 DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
2743 bin_size););
2744 DBUG_RETURN((err != E_DEC_OK) ? decimal_err_to_type_conv_status(err)
2745 : error);
2746 }
2747
2748
2749 type_conversion_status
store(const char * from,uint length,const CHARSET_INFO * charset_arg)2750 Field_new_decimal::store(const char *from, uint length,
2751 const CHARSET_INFO *charset_arg)
2752 {
2753 ASSERT_COLUMN_MARKED_FOR_WRITE;
2754 my_decimal decimal_value;
2755 DBUG_ENTER("Field_new_decimal::store(char*)");
2756
2757 int err= str2my_decimal(E_DEC_FATAL_ERROR &
2758 ~(E_DEC_OVERFLOW | E_DEC_BAD_NUM),
2759 from, length, charset_arg,
2760 &decimal_value);
2761
2762 if (err != 0 && table->in_use->abort_on_warning)
2763 {
2764 ErrConvString errmsg(from, length, charset_arg);
2765 const Diagnostics_area *da= table->in_use->get_stmt_da();
2766 push_warning_printf(table->in_use, Sql_condition::WARN_LEVEL_WARN,
2767 ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
2768 ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
2769 "decimal", errmsg.ptr(), field_name,
2770 static_cast<ulong>(da->current_row_for_warning()));
2771 DBUG_RETURN(decimal_err_to_type_conv_status(err));
2772 }
2773
2774 if (err != 0)
2775 set_decimal_warning(this, err, &decimal_value, from, length, charset_arg);
2776
2777 #ifndef DBUG_OFF
2778 char dbug_buff[DECIMAL_MAX_STR_LENGTH+2];
2779 DBUG_PRINT("enter", ("value: %s",
2780 dbug_decimal_as_string(dbug_buff, &decimal_value)));
2781 #endif
2782
2783 type_conversion_status store_stat= store_value(&decimal_value);
2784 DBUG_RETURN(err != 0 ? decimal_err_to_type_conv_status(err) : store_stat);
2785 }
2786
2787
2788 type_conversion_status
store_internal_with_error_check(Field_new_decimal * field,int err,my_decimal * value)2789 store_internal_with_error_check(Field_new_decimal *field,
2790 int err, my_decimal *value)
2791 {
2792 type_conversion_status stat= TYPE_OK;
2793 if (err != 0)
2794 {
2795 if (field->check_overflow(err))
2796 {
2797 field->set_value_on_overflow(value, value->sign());
2798 stat= TYPE_WARN_OUT_OF_RANGE;
2799 }
2800 else if (field->check_truncated(err))
2801 stat= TYPE_NOTE_TRUNCATED;
2802 /* Only issue a warning if store_value doesn't issue an warning */
2803 field->table->in_use->got_warning= 0;
2804 }
2805 type_conversion_status store_stat= field->store_value(value);
2806 if (store_stat != TYPE_OK)
2807 return store_stat;
2808 else if (err != 0 && !field->table->in_use->got_warning)
2809 field->warn_if_overflow(err);
2810 return stat;
2811 }
2812
2813 /**
2814 @todo
2815 Fix following when double2my_decimal when double2decimal
2816 will return E_DEC_TRUNCATED always correctly
2817 */
2818
store(double nr)2819 type_conversion_status Field_new_decimal::store(double nr)
2820 {
2821 ASSERT_COLUMN_MARKED_FOR_WRITE;
2822 DBUG_ENTER("Field_new_decimal::store(double)");
2823 my_decimal decimal_value;
2824
2825 int conv_err= double2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW, nr,
2826 &decimal_value);
2827 DBUG_RETURN(store_internal_with_error_check(this, conv_err, &decimal_value));
2828 }
2829
2830 type_conversion_status
store(longlong nr,bool unsigned_val)2831 Field_new_decimal::store(longlong nr, bool unsigned_val)
2832 {
2833 ASSERT_COLUMN_MARKED_FOR_WRITE;
2834 DBUG_ENTER("Field_new_decimal::store(double, unsigned_val)");
2835 my_decimal decimal_value;
2836 int conv_err= int2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_OVERFLOW,
2837 nr, unsigned_val, &decimal_value);
2838 DBUG_RETURN(store_internal_with_error_check(this, conv_err, &decimal_value));
2839 }
2840
2841
2842 type_conversion_status
store_decimal(const my_decimal * decimal_value)2843 Field_new_decimal::store_decimal(const my_decimal *decimal_value)
2844 {
2845 ASSERT_COLUMN_MARKED_FOR_WRITE;
2846 return store_value(decimal_value);
2847 }
2848
2849
2850 type_conversion_status
store_time(MYSQL_TIME * ltime,uint8 dec_arg MY_ATTRIBUTE ((unused)))2851 Field_new_decimal::store_time(MYSQL_TIME *ltime,
2852 uint8 dec_arg MY_ATTRIBUTE((unused)))
2853 {
2854 my_decimal decimal_value;
2855 return store_value(date2my_decimal(ltime, &decimal_value));
2856 }
2857
2858
val_real(void)2859 double Field_new_decimal::val_real(void)
2860 {
2861 ASSERT_COLUMN_MARKED_FOR_READ;
2862 double dbl;
2863 my_decimal decimal_value;
2864 my_decimal2double(E_DEC_FATAL_ERROR, val_decimal(&decimal_value), &dbl);
2865 return dbl;
2866 }
2867
2868
val_int(void)2869 longlong Field_new_decimal::val_int(void)
2870 {
2871 ASSERT_COLUMN_MARKED_FOR_READ;
2872 longlong i;
2873 my_decimal decimal_value;
2874 my_decimal2int(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
2875 unsigned_flag, &i);
2876 return i;
2877 }
2878
2879
val_decimal(my_decimal * decimal_value)2880 my_decimal* Field_new_decimal::val_decimal(my_decimal *decimal_value)
2881 {
2882 ASSERT_COLUMN_MARKED_FOR_READ;
2883 DBUG_ENTER("Field_new_decimal::val_decimal");
2884 binary2my_decimal(E_DEC_FATAL_ERROR, ptr, decimal_value,
2885 precision, dec);
2886 DBUG_EXECUTE("info", print_decimal_buff(decimal_value, (uchar *) ptr,
2887 bin_size););
2888 DBUG_RETURN(decimal_value);
2889 }
2890
2891
val_str(String * val_buffer,String * val_ptr MY_ATTRIBUTE ((unused)))2892 String *Field_new_decimal::val_str(String *val_buffer,
2893 String *val_ptr MY_ATTRIBUTE((unused)))
2894 {
2895 ASSERT_COLUMN_MARKED_FOR_READ;
2896 my_decimal decimal_value;
2897 uint fixed_precision= zerofill ? precision : 0;
2898 my_decimal2string(E_DEC_FATAL_ERROR, val_decimal(&decimal_value),
2899 fixed_precision, dec, '0', val_buffer);
2900 val_buffer->set_charset(&my_charset_numeric);
2901 return val_buffer;
2902 }
2903
2904
get_date(MYSQL_TIME * ltime,uint fuzzydate)2905 bool Field_new_decimal::get_date(MYSQL_TIME *ltime, uint fuzzydate)
2906 {
2907 my_decimal buf, *decimal_value= val_decimal(&buf);
2908 if (!decimal_value)
2909 {
2910 set_zero_time(ltime, MYSQL_TIMESTAMP_DATETIME);
2911 return true;
2912 }
2913 return my_decimal_to_datetime_with_warn(decimal_value, ltime, fuzzydate);
2914 }
2915
2916
get_time(MYSQL_TIME * ltime)2917 bool Field_new_decimal::get_time(MYSQL_TIME *ltime)
2918 {
2919 my_decimal buf, *decimal_value= val_decimal(&buf);
2920 if (!decimal_value)
2921 {
2922 set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
2923 return true;
2924 }
2925 return my_decimal_to_time_with_warn(decimal_value, ltime);
2926 }
2927
2928
cmp(const uchar * a,const uchar * b)2929 int Field_new_decimal::cmp(const uchar *a,const uchar*b)
2930 {
2931 return memcmp(a, b, bin_size);
2932 }
2933
2934
make_sort_key(uchar * buff,uint length)2935 void Field_new_decimal::make_sort_key(uchar *buff, uint length)
2936 {
2937 memcpy(buff, ptr, min<uint>(length, bin_size));
2938 }
2939
2940
sql_type(String & str) const2941 void Field_new_decimal::sql_type(String &str) const
2942 {
2943 const CHARSET_INFO *cs= str.charset();
2944 str.length(cs->cset->snprintf(cs, (char*) str.ptr(), str.alloced_length(),
2945 "decimal(%d,%d)", precision, (int)dec));
2946 add_zerofill_and_unsigned(str);
2947 }
2948
2949
2950 /**
2951 Save the field metadata for new decimal fields.
2952
2953 Saves the precision in the first byte and decimals() in the second
2954 byte of the field metadata array at index of *metadata_ptr and
2955 *(metadata_ptr + 1).
2956
2957 @param metadata_ptr First byte of field metadata
2958
2959 @returns number of bytes written to metadata_ptr
2960 */
do_save_field_metadata(uchar * metadata_ptr)2961 int Field_new_decimal::do_save_field_metadata(uchar *metadata_ptr)
2962 {
2963 *metadata_ptr= precision;
2964 *(metadata_ptr + 1)= decimals();
2965 return 2;
2966 }
2967
2968
2969 /**
2970 Returns the number of bytes field uses in row-based replication
2971 row packed size.
2972
2973 This method is used in row-based replication to determine the number
2974 of bytes that the field consumes in the row record format. This is
2975 used to skip fields in the master that do not exist on the slave.
2976
2977 @param field_metadata Encoded size in field metadata
2978
2979 @returns The size of the field based on the field metadata.
2980 */
pack_length_from_metadata(uint field_metadata)2981 uint Field_new_decimal::pack_length_from_metadata(uint field_metadata)
2982 {
2983 uint const source_precision= (field_metadata >> 8U) & 0x00ff;
2984 uint const source_decimal= field_metadata & 0x00ff;
2985 uint const source_size= my_decimal_get_binary_size(source_precision,
2986 source_decimal);
2987 return (source_size);
2988 }
2989
2990 /**
2991 Check to see if field size is compatible with destination.
2992
2993 This method is used in row-based replication to verify that the slave's
2994 field size is less than or equal to the master's field size. The
2995 encoded field metadata (from the master or source) is decoded and compared
2996 to the size of this field (the slave or destination).
2997
2998 @param field_metadata Encoded size in field metadata
2999 @param order_var Pointer to variable where the order
3000 between the source field and this field
3001 will be returned.
3002
3003 @return @c true
3004 */
compatible_field_size(uint field_metadata,Relay_log_info * MY_ATTRIBUTE ((unused)),uint16 mflags MY_ATTRIBUTE ((unused)),int * order_var)3005 bool Field_new_decimal::compatible_field_size(uint field_metadata,
3006 Relay_log_info * MY_ATTRIBUTE((unused)),
3007 uint16 mflags MY_ATTRIBUTE((unused)),
3008 int *order_var)
3009 {
3010 uint const source_precision= (field_metadata >> 8U) & 0x00ff;
3011 uint const source_decimal= field_metadata & 0x00ff;
3012 int order= compare(source_precision, precision);
3013 *order_var= order != 0 ? order : compare(source_decimal, dec);
3014 return true;
3015 }
3016
3017
is_equal(Create_field * new_field)3018 uint Field_new_decimal::is_equal(Create_field *new_field)
3019 {
3020 return ((new_field->sql_type == real_type()) &&
3021 ((new_field->flags & UNSIGNED_FLAG) ==
3022 (uint) (flags & UNSIGNED_FLAG)) &&
3023 ((new_field->flags & AUTO_INCREMENT_FLAG) ==
3024 (uint) (flags & AUTO_INCREMENT_FLAG)) &&
3025 (new_field->length == max_display_length()) &&
3026 (new_field->decimals == dec));
3027 }
3028
3029
3030 /**
3031 Unpack a decimal field from row data.
3032
3033 This method is used to unpack a decimal or numeric field from a master
3034 whose size of the field is less than that of the slave.
3035
3036 @param to Destination of the data
3037 @param from Source of the data
3038 @param param_data Precision (upper) and decimal (lower) values
3039
3040 @return New pointer into memory based on from + length of the data
3041 */
3042 const uchar *
unpack(uchar * to,const uchar * from,uint param_data,bool low_byte_first)3043 Field_new_decimal::unpack(uchar* to,
3044 const uchar *from,
3045 uint param_data,
3046 bool low_byte_first)
3047 {
3048 if (param_data == 0)
3049 return Field::unpack(to, from, param_data, low_byte_first);
3050
3051 uint from_precision= (param_data & 0xff00) >> 8U;
3052 uint from_decimal= param_data & 0x00ff;
3053 uint length=pack_length();
3054 uint from_pack_len= my_decimal_get_binary_size(from_precision, from_decimal);
3055 uint len= (param_data && (from_pack_len < length)) ?
3056 from_pack_len : length;
3057 if ((from_pack_len && (from_pack_len < length)) ||
3058 (from_precision < precision) ||
3059 (from_decimal < decimals()))
3060 {
3061 /*
3062 If the master's data is smaller than the slave, we need to convert
3063 the binary to decimal then resize the decimal converting it back to
3064 a decimal and write that to the raw data buffer.
3065 */
3066 decimal_digit_t dec_buf[DECIMAL_MAX_PRECISION];
3067 decimal_t dec_val;
3068 dec_val.len= from_precision;
3069 dec_val.buf= dec_buf;
3070 /*
3071 Note: bin2decimal does not change the length of the field. So it is
3072 just the first step the resizing operation. The second step does the
3073 resizing using the precision and decimals from the slave.
3074 */
3075 bin2decimal((uchar *)from, &dec_val, from_precision, from_decimal);
3076 decimal2bin(&dec_val, to, precision, decimals());
3077 }
3078 else
3079 memcpy(to, from, len); // Sizes are the same, just copy the data.
3080 return from+len;
3081 }
3082
3083 /****************************************************************************
3084 ** tiny int
3085 ****************************************************************************/
3086
3087 type_conversion_status
store(const char * from,uint len,const CHARSET_INFO * cs)3088 Field_tiny::store(const char *from,uint len, const CHARSET_INFO *cs)
3089 {
3090 ASSERT_COLUMN_MARKED_FOR_WRITE;
3091 longlong rnd;
3092
3093 const type_conversion_status error= get_int(cs, from, len, &rnd,
3094 255, -128, 127);
3095 ptr[0]= unsigned_flag ? (char) (ulonglong) rnd : (char) rnd;
3096 return error;
3097 }
3098
3099
store(double nr)3100 type_conversion_status Field_tiny::store(double nr)
3101 {
3102 ASSERT_COLUMN_MARKED_FOR_WRITE;
3103 type_conversion_status error= TYPE_OK;
3104 nr=rint(nr);
3105 if (unsigned_flag)
3106 {
3107 if (nr < 0.0)
3108 {
3109 *ptr=0;
3110 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3111 error= TYPE_WARN_OUT_OF_RANGE;
3112 }
3113 else if (nr > 255.0)
3114 {
3115 *ptr=(char) 255;
3116 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3117 error= TYPE_WARN_OUT_OF_RANGE;
3118 }
3119 else
3120 *ptr=(char) nr;
3121 }
3122 else
3123 {
3124 if (nr < -128.0)
3125 {
3126 *ptr= (char) -128;
3127 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3128 error= TYPE_WARN_OUT_OF_RANGE;
3129 }
3130 else if (nr > 127.0)
3131 {
3132 *ptr=127;
3133 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3134 error= TYPE_WARN_OUT_OF_RANGE;
3135 }
3136 else
3137 *ptr=(char) (int) nr;
3138 }
3139 return error;
3140 }
3141
3142
store(longlong nr,bool unsigned_val)3143 type_conversion_status Field_tiny::store(longlong nr, bool unsigned_val)
3144 {
3145 ASSERT_COLUMN_MARKED_FOR_WRITE;
3146 type_conversion_status error= TYPE_OK;
3147
3148 if (unsigned_flag)
3149 {
3150 if (nr < 0 && !unsigned_val)
3151 {
3152 *ptr= 0;
3153 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3154 error= TYPE_WARN_OUT_OF_RANGE;
3155 }
3156 else if ((ulonglong) nr > (ulonglong) 255)
3157 {
3158 *ptr= (char) 255;
3159 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3160 error= TYPE_WARN_OUT_OF_RANGE;
3161 }
3162 else
3163 *ptr=(char) nr;
3164 }
3165 else
3166 {
3167 if (nr < 0 && unsigned_val)
3168 nr= 256; // Generate overflow
3169 if (nr < -128)
3170 {
3171 *ptr= (char) -128;
3172 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3173 error= TYPE_WARN_OUT_OF_RANGE;
3174 }
3175 else if (nr > 127)
3176 {
3177 *ptr=127;
3178 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3179 error= TYPE_WARN_OUT_OF_RANGE;
3180 }
3181 else
3182 *ptr=(char) nr;
3183 }
3184 return error;
3185 }
3186
3187
val_real(void)3188 double Field_tiny::val_real(void)
3189 {
3190 ASSERT_COLUMN_MARKED_FOR_READ;
3191 int tmp= unsigned_flag ? (int) ptr[0] :
3192 (int) ((signed char*) ptr)[0];
3193 return (double) tmp;
3194 }
3195
3196
val_int(void)3197 longlong Field_tiny::val_int(void)
3198 {
3199 ASSERT_COLUMN_MARKED_FOR_READ;
3200 int tmp= unsigned_flag ? (int) ptr[0] :
3201 (int) ((signed char*) ptr)[0];
3202 return (longlong) tmp;
3203 }
3204
3205
val_str(String * val_buffer,String * val_ptr MY_ATTRIBUTE ((unused)))3206 String *Field_tiny::val_str(String *val_buffer,
3207 String *val_ptr MY_ATTRIBUTE((unused)))
3208 {
3209 ASSERT_COLUMN_MARKED_FOR_READ;
3210 const CHARSET_INFO *cs= &my_charset_numeric;
3211 uint length;
3212 uint mlength=max(field_length+1,5*cs->mbmaxlen);
3213 val_buffer->alloc(mlength);
3214 char *to=(char*) val_buffer->ptr();
3215
3216 if (unsigned_flag)
3217 length= (uint) cs->cset->long10_to_str(cs,to,mlength, 10,
3218 (long) *ptr);
3219 else
3220 length= (uint) cs->cset->long10_to_str(cs,to,mlength,-10,
3221 (long) *((signed char*) ptr));
3222
3223 val_buffer->length(length);
3224 if (zerofill)
3225 prepend_zeros(val_buffer);
3226 val_buffer->set_charset(cs);
3227 return val_buffer;
3228 }
3229
send_binary(Protocol * protocol)3230 bool Field_tiny::send_binary(Protocol *protocol)
3231 {
3232 return protocol->store_tiny((longlong) (int8) ptr[0]);
3233 }
3234
cmp(const uchar * a_ptr,const uchar * b_ptr)3235 int Field_tiny::cmp(const uchar *a_ptr, const uchar *b_ptr)
3236 {
3237 signed char a,b;
3238 a=(signed char) a_ptr[0]; b= (signed char) b_ptr[0];
3239 if (unsigned_flag)
3240 return ((uchar) a < (uchar) b) ? -1 : ((uchar) a > (uchar) b) ? 1 : 0;
3241 return (a < b) ? -1 : (a > b) ? 1 : 0;
3242 }
3243
make_sort_key(uchar * to,uint length)3244 void Field_tiny::make_sort_key(uchar *to, uint length)
3245 {
3246 DBUG_ASSERT(length >= 1);
3247 if (unsigned_flag)
3248 *to= *ptr;
3249 else
3250 to[0] = (char) (ptr[0] ^ (uchar) 128); /* Revers signbit */
3251 }
3252
sql_type(String & res) const3253 void Field_tiny::sql_type(String &res) const
3254 {
3255 const CHARSET_INFO *cs=res.charset();
3256 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3257 "tinyint(%d)",(int) field_length));
3258 add_zerofill_and_unsigned(res);
3259 }
3260
3261 /****************************************************************************
3262 Field type short int (2 byte)
3263 ****************************************************************************/
3264
3265 type_conversion_status
store(const char * from,uint len,const CHARSET_INFO * cs)3266 Field_short::store(const char *from,uint len, const CHARSET_INFO *cs)
3267 {
3268 ASSERT_COLUMN_MARKED_FOR_WRITE;
3269 int store_tmp;
3270 longlong rnd;
3271
3272 const type_conversion_status error=
3273 get_int(cs, from, len, &rnd, UINT_MAX16, INT_MIN16, INT_MAX16);
3274 store_tmp= unsigned_flag ? (int) (ulonglong) rnd : (int) rnd;
3275 #ifdef WORDS_BIGENDIAN
3276 if (table->s->db_low_byte_first)
3277 {
3278 int2store(ptr, store_tmp);
3279 }
3280 else
3281 #endif
3282 shortstore(ptr, (short) store_tmp);
3283 return error;
3284 }
3285
3286
store(double nr)3287 type_conversion_status Field_short::store(double nr)
3288 {
3289 ASSERT_COLUMN_MARKED_FOR_WRITE;
3290 type_conversion_status error= TYPE_OK;
3291 int16 res;
3292 nr=rint(nr);
3293 if (unsigned_flag)
3294 {
3295 if (nr < 0)
3296 {
3297 res=0;
3298 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3299 error= TYPE_WARN_OUT_OF_RANGE;
3300 }
3301 else if (nr > (double) UINT_MAX16)
3302 {
3303 res=(int16) UINT_MAX16;
3304 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3305 error= TYPE_WARN_OUT_OF_RANGE;
3306 }
3307 else
3308 res=(int16) (uint16) nr;
3309 }
3310 else
3311 {
3312 if (nr < (double) INT_MIN16)
3313 {
3314 res=INT_MIN16;
3315 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3316 error= TYPE_WARN_OUT_OF_RANGE;
3317 }
3318 else if (nr > (double) INT_MAX16)
3319 {
3320 res=INT_MAX16;
3321 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3322 error= TYPE_WARN_OUT_OF_RANGE;
3323 }
3324 else
3325 res=(int16) (int) nr;
3326 }
3327 #ifdef WORDS_BIGENDIAN
3328 if (table->s->db_low_byte_first)
3329 {
3330 int2store(ptr,res);
3331 }
3332 else
3333 #endif
3334 shortstore(ptr,res);
3335 return error;
3336 }
3337
3338
store(longlong nr,bool unsigned_val)3339 type_conversion_status Field_short::store(longlong nr, bool unsigned_val)
3340 {
3341 ASSERT_COLUMN_MARKED_FOR_WRITE;
3342 type_conversion_status error= TYPE_OK;
3343 int16 res;
3344
3345 if (unsigned_flag)
3346 {
3347 if (nr < 0L && !unsigned_val)
3348 {
3349 res=0;
3350 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3351 error= TYPE_WARN_OUT_OF_RANGE;
3352 }
3353 else if ((ulonglong) nr > (ulonglong) UINT_MAX16)
3354 {
3355 res=(int16) UINT_MAX16;
3356 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3357 error= TYPE_WARN_OUT_OF_RANGE;
3358 }
3359 else
3360 res=(int16) (uint16) nr;
3361 }
3362 else
3363 {
3364 if (nr < 0 && unsigned_val)
3365 nr= UINT_MAX16+1; // Generate overflow
3366
3367 if (nr < INT_MIN16)
3368 {
3369 res=INT_MIN16;
3370 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3371 error= TYPE_WARN_OUT_OF_RANGE;
3372 }
3373 else if (nr > (longlong) INT_MAX16)
3374 {
3375 res=INT_MAX16;
3376 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3377 error= TYPE_WARN_OUT_OF_RANGE;
3378 }
3379 else
3380 res=(int16) nr;
3381 }
3382 #ifdef WORDS_BIGENDIAN
3383 if (table->s->db_low_byte_first)
3384 {
3385 int2store(ptr,res);
3386 }
3387 else
3388 #endif
3389 shortstore(ptr,res);
3390 return error;
3391 }
3392
3393
val_real(void)3394 double Field_short::val_real(void)
3395 {
3396 ASSERT_COLUMN_MARKED_FOR_READ;
3397 short j;
3398 #ifdef WORDS_BIGENDIAN
3399 if (table->s->db_low_byte_first)
3400 j=sint2korr(ptr);
3401 else
3402 #endif
3403 shortget(j,ptr);
3404 return unsigned_flag ? (double) (unsigned short) j : (double) j;
3405 }
3406
val_int(void)3407 longlong Field_short::val_int(void)
3408 {
3409 ASSERT_COLUMN_MARKED_FOR_READ;
3410 short j;
3411 #ifdef WORDS_BIGENDIAN
3412 if (table->s->db_low_byte_first)
3413 j=sint2korr(ptr);
3414 else
3415 #endif
3416 shortget(j,ptr);
3417 return unsigned_flag ? (longlong) (unsigned short) j : (longlong) j;
3418 }
3419
3420
val_str(String * val_buffer,String * val_ptr MY_ATTRIBUTE ((unused)))3421 String *Field_short::val_str(String *val_buffer,
3422 String *val_ptr MY_ATTRIBUTE((unused)))
3423 {
3424 ASSERT_COLUMN_MARKED_FOR_READ;
3425 const CHARSET_INFO *cs= &my_charset_numeric;
3426 uint length;
3427 uint mlength=max(field_length+1,7*cs->mbmaxlen);
3428 val_buffer->alloc(mlength);
3429 char *to=(char*) val_buffer->ptr();
3430 short j;
3431 #ifdef WORDS_BIGENDIAN
3432 if (table->s->db_low_byte_first)
3433 j=sint2korr(ptr);
3434 else
3435 #endif
3436 shortget(j,ptr);
3437
3438 if (unsigned_flag)
3439 length=(uint) cs->cset->long10_to_str(cs, to, mlength, 10,
3440 (long) (uint16) j);
3441 else
3442 length=(uint) cs->cset->long10_to_str(cs, to, mlength,-10, (long) j);
3443 val_buffer->length(length);
3444 if (zerofill)
3445 prepend_zeros(val_buffer);
3446 val_buffer->set_charset(cs);
3447 return val_buffer;
3448 }
3449
3450
send_binary(Protocol * protocol)3451 bool Field_short::send_binary(Protocol *protocol)
3452 {
3453 return protocol->store_short(Field_short::val_int());
3454 }
3455
3456
cmp(const uchar * a_ptr,const uchar * b_ptr)3457 int Field_short::cmp(const uchar *a_ptr, const uchar *b_ptr)
3458 {
3459 short a,b;
3460 #ifdef WORDS_BIGENDIAN
3461 if (table->s->db_low_byte_first)
3462 {
3463 a=sint2korr(a_ptr);
3464 b=sint2korr(b_ptr);
3465 }
3466 else
3467 #endif
3468 {
3469 shortget(a,a_ptr);
3470 shortget(b,b_ptr);
3471 }
3472
3473 if (unsigned_flag)
3474 return ((unsigned short) a < (unsigned short) b) ? -1 :
3475 ((unsigned short) a > (unsigned short) b) ? 1 : 0;
3476 return (a < b) ? -1 : (a > b) ? 1 : 0;
3477 }
3478
make_sort_key(uchar * to,uint length)3479 void Field_short::make_sort_key(uchar *to, uint length)
3480 {
3481 DBUG_ASSERT(length >= 2);
3482 #ifdef WORDS_BIGENDIAN
3483 if (!table->s->db_low_byte_first)
3484 {
3485 if (unsigned_flag)
3486 to[0] = ptr[0];
3487 else
3488 to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
3489 to[1] = ptr[1];
3490 }
3491 else
3492 #endif
3493 {
3494 if (unsigned_flag)
3495 to[0] = ptr[1];
3496 else
3497 to[0] = (char) (ptr[1] ^ 128); /* Revers signbit */
3498 to[1] = ptr[0];
3499 }
3500 }
3501
sql_type(String & res) const3502 void Field_short::sql_type(String &res) const
3503 {
3504 const CHARSET_INFO *cs=res.charset();
3505 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3506 "smallint(%d)",(int) field_length));
3507 add_zerofill_and_unsigned(res);
3508 }
3509
3510
3511 /****************************************************************************
3512 Field type medium int (3 byte)
3513 ****************************************************************************/
3514
store(const char * from,uint len,const CHARSET_INFO * cs)3515 type_conversion_status Field_medium::store(const char *from,uint len,
3516 const CHARSET_INFO *cs)
3517 {
3518 ASSERT_COLUMN_MARKED_FOR_WRITE;
3519 int store_tmp;
3520 longlong rnd;
3521
3522 const type_conversion_status error=
3523 get_int(cs, from, len, &rnd, UINT_MAX24, INT_MIN24, INT_MAX24);
3524 store_tmp= unsigned_flag ? (int) (ulonglong) rnd : (int) rnd;
3525 int3store(ptr, store_tmp);
3526 return error;
3527 }
3528
3529
store(double nr)3530 type_conversion_status Field_medium::store(double nr)
3531 {
3532 ASSERT_COLUMN_MARKED_FOR_WRITE;
3533 type_conversion_status error= TYPE_OK;
3534 nr=rint(nr);
3535 if (unsigned_flag)
3536 {
3537 if (nr < 0)
3538 {
3539 int3store(ptr,0);
3540 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3541 error= TYPE_WARN_OUT_OF_RANGE;
3542 }
3543 else if (nr >= (double) (long) (1L << 24))
3544 {
3545 uint32 tmp=(uint32) (1L << 24)-1L;
3546 int3store(ptr,tmp);
3547 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3548 error= TYPE_WARN_OUT_OF_RANGE;
3549 }
3550 else
3551 int3store(ptr,(uint32) nr);
3552 }
3553 else
3554 {
3555 if (nr < (double) INT_MIN24)
3556 {
3557 long tmp=(long) INT_MIN24;
3558 int3store(ptr,tmp);
3559 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3560 error= TYPE_WARN_OUT_OF_RANGE;
3561 }
3562 else if (nr > (double) INT_MAX24)
3563 {
3564 long tmp=(long) INT_MAX24;
3565 int3store(ptr,tmp);
3566 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3567 error= TYPE_WARN_OUT_OF_RANGE;
3568 }
3569 else
3570 int3store(ptr,(long) nr);
3571 }
3572 return error;
3573 }
3574
3575
store(longlong nr,bool unsigned_val)3576 type_conversion_status Field_medium::store(longlong nr, bool unsigned_val)
3577 {
3578 ASSERT_COLUMN_MARKED_FOR_WRITE;
3579 type_conversion_status error= TYPE_OK;
3580
3581 if (unsigned_flag)
3582 {
3583 if (nr < 0 && !unsigned_val)
3584 {
3585 int3store(ptr,0);
3586 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3587 error= TYPE_WARN_OUT_OF_RANGE;
3588 }
3589 else if ((ulonglong) nr >= (ulonglong) (long) (1L << 24))
3590 {
3591 long tmp= (long) (1L << 24)-1L;
3592 int3store(ptr,tmp);
3593 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3594 error= TYPE_WARN_OUT_OF_RANGE;
3595 }
3596 else
3597 int3store(ptr,(uint32) nr);
3598 }
3599 else
3600 {
3601 if (nr < 0 && unsigned_val)
3602 nr= (ulonglong) (long) (1L << 24); // Generate overflow
3603
3604 if (nr < (longlong) INT_MIN24)
3605 {
3606 long tmp= (long) INT_MIN24;
3607 int3store(ptr,tmp);
3608 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3609 error= TYPE_WARN_OUT_OF_RANGE;
3610 }
3611 else if (nr > (longlong) INT_MAX24)
3612 {
3613 long tmp=(long) INT_MAX24;
3614 int3store(ptr,tmp);
3615 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3616 error= TYPE_WARN_OUT_OF_RANGE;
3617 }
3618 else
3619 int3store(ptr,(long) nr);
3620 }
3621 return error;
3622 }
3623
3624
val_real(void)3625 double Field_medium::val_real(void)
3626 {
3627 ASSERT_COLUMN_MARKED_FOR_READ;
3628 long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
3629 return (double) j;
3630 }
3631
3632
val_int(void)3633 longlong Field_medium::val_int(void)
3634 {
3635 ASSERT_COLUMN_MARKED_FOR_READ;
3636 long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
3637 return (longlong) j;
3638 }
3639
3640
val_str(String * val_buffer,String * val_ptr MY_ATTRIBUTE ((unused)))3641 String *Field_medium::val_str(String *val_buffer,
3642 String *val_ptr MY_ATTRIBUTE((unused)))
3643 {
3644 ASSERT_COLUMN_MARKED_FOR_READ;
3645 const CHARSET_INFO *cs= &my_charset_numeric;
3646 uint length;
3647 uint mlength=max(field_length+1,10*cs->mbmaxlen);
3648 val_buffer->alloc(mlength);
3649 char *to=(char*) val_buffer->ptr();
3650 long j= unsigned_flag ? (long) uint3korr(ptr) : sint3korr(ptr);
3651
3652 length=(uint) cs->cset->long10_to_str(cs,to,mlength,-10,j);
3653 val_buffer->length(length);
3654 if (zerofill)
3655 prepend_zeros(val_buffer); /* purecov: inspected */
3656 val_buffer->set_charset(cs);
3657 return val_buffer;
3658 }
3659
3660
send_binary(Protocol * protocol)3661 bool Field_medium::send_binary(Protocol *protocol)
3662 {
3663 ASSERT_COLUMN_MARKED_FOR_READ;
3664 return protocol->store_long(Field_medium::val_int());
3665 }
3666
3667
cmp(const uchar * a_ptr,const uchar * b_ptr)3668 int Field_medium::cmp(const uchar *a_ptr, const uchar *b_ptr)
3669 {
3670 long a,b;
3671 if (unsigned_flag)
3672 {
3673 a=uint3korr(a_ptr);
3674 b=uint3korr(b_ptr);
3675 }
3676 else
3677 {
3678 a=sint3korr(a_ptr);
3679 b=sint3korr(b_ptr);
3680 }
3681 return (a < b) ? -1 : (a > b) ? 1 : 0;
3682 }
3683
make_sort_key(uchar * to,uint length)3684 void Field_medium::make_sort_key(uchar *to, uint length)
3685 {
3686 DBUG_ASSERT(length >= 3);
3687 if (unsigned_flag)
3688 to[0] = ptr[2];
3689 else
3690 to[0] = (uchar) (ptr[2] ^ 128); /* Revers signbit */
3691 to[1] = ptr[1];
3692 to[2] = ptr[0];
3693 }
3694
3695
sql_type(String & res) const3696 void Field_medium::sql_type(String &res) const
3697 {
3698 const CHARSET_INFO *cs=res.charset();
3699 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3700 "mediumint(%d)",(int) field_length));
3701 add_zerofill_and_unsigned(res);
3702 }
3703
3704 /****************************************************************************
3705 ** long int
3706 ****************************************************************************/
3707
store(const char * from,uint len,const CHARSET_INFO * cs)3708 type_conversion_status Field_long::store(const char *from,uint len,
3709 const CHARSET_INFO *cs)
3710 {
3711 ASSERT_COLUMN_MARKED_FOR_WRITE;
3712 long store_tmp;
3713 longlong rnd;
3714
3715 const type_conversion_status error=
3716 get_int(cs, from, len, &rnd, UINT_MAX32, INT_MIN32, INT_MAX32);
3717 store_tmp= unsigned_flag ? (long) (ulonglong) rnd : (long) rnd;
3718 #ifdef WORDS_BIGENDIAN
3719 if (table->s->db_low_byte_first)
3720 {
3721 int4store(ptr, store_tmp);
3722 }
3723 else
3724 #endif
3725 longstore(ptr, store_tmp);
3726 return error;
3727 }
3728
3729
store(double nr)3730 type_conversion_status Field_long::store(double nr)
3731 {
3732 ASSERT_COLUMN_MARKED_FOR_WRITE;
3733 type_conversion_status error= TYPE_OK;
3734 int32 res;
3735 nr=rint(nr);
3736 if (unsigned_flag)
3737 {
3738 if (nr < 0)
3739 {
3740 res=0;
3741 error= TYPE_WARN_OUT_OF_RANGE;
3742 }
3743 else if (nr > (double) UINT_MAX32)
3744 {
3745 res= UINT_MAX32;
3746 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3747 error= TYPE_WARN_OUT_OF_RANGE;
3748 }
3749 else
3750 res=(int32) (ulong) nr;
3751 }
3752 else
3753 {
3754 if (nr < (double) INT_MIN32)
3755 {
3756 res=(int32) INT_MIN32;
3757 error= TYPE_WARN_OUT_OF_RANGE;
3758 }
3759 else if (nr > (double) INT_MAX32)
3760 {
3761 res=(int32) INT_MAX32;
3762 error= TYPE_WARN_OUT_OF_RANGE;
3763 }
3764 else
3765 res=(int32) (longlong) nr;
3766 }
3767 if (error)
3768 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3769
3770 #ifdef WORDS_BIGENDIAN
3771 if (table->s->db_low_byte_first)
3772 {
3773 int4store(ptr,res);
3774 }
3775 else
3776 #endif
3777 longstore(ptr,res);
3778 return error;
3779 }
3780
3781
3782 /**
3783 Store a longlong in the field
3784
3785 @param nr the value to store
3786 @param unsigned_val whether or not 'nr' should be interpreted as
3787 signed or unsigned. E.g., if 'nr' has all bits
3788 set it is interpreted as -1 if unsigned_val is
3789 false and ULONGLONG_MAX if unsigned_val is true.
3790 */
store(longlong nr,bool unsigned_val)3791 type_conversion_status Field_long::store(longlong nr, bool unsigned_val)
3792 {
3793 ASSERT_COLUMN_MARKED_FOR_WRITE;
3794 type_conversion_status error= TYPE_OK;
3795 int32 res;
3796
3797 if (unsigned_flag)
3798 {
3799 if (nr < 0 && !unsigned_val)
3800 {
3801 res=0;
3802 error= TYPE_WARN_OUT_OF_RANGE;
3803 }
3804 else if ((ulonglong) nr >= (LL(1) << 32))
3805 {
3806 res=(int32) (uint32) ~0L;
3807 error= TYPE_WARN_OUT_OF_RANGE;
3808 }
3809 else
3810 res=(int32) (uint32) nr;
3811 }
3812 else
3813 {
3814 if (nr < 0 && unsigned_val)
3815 {
3816 nr= ((longlong) INT_MAX32) + 1; // Generate overflow
3817 error= TYPE_WARN_OUT_OF_RANGE;
3818 }
3819 if (nr < (longlong) INT_MIN32)
3820 {
3821 res=(int32) INT_MIN32;
3822 error= TYPE_WARN_OUT_OF_RANGE;
3823 }
3824 else if (nr > (longlong) INT_MAX32)
3825 {
3826 res=(int32) INT_MAX32;
3827 error= TYPE_WARN_OUT_OF_RANGE;
3828 }
3829 else
3830 res=(int32) nr;
3831 }
3832 if (error)
3833 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3834
3835 #ifdef WORDS_BIGENDIAN
3836 if (table->s->db_low_byte_first)
3837 {
3838 int4store(ptr,res);
3839 }
3840 else
3841 #endif
3842 longstore(ptr,res);
3843 return error;
3844 }
3845
3846
val_real(void)3847 double Field_long::val_real(void)
3848 {
3849 ASSERT_COLUMN_MARKED_FOR_READ;
3850 int32 j;
3851 #ifdef WORDS_BIGENDIAN
3852 if (table->s->db_low_byte_first)
3853 j=sint4korr(ptr);
3854 else
3855 #endif
3856 longget(j,ptr);
3857 return unsigned_flag ? (double) (uint32) j : (double) j;
3858 }
3859
val_int(void)3860 longlong Field_long::val_int(void)
3861 {
3862 ASSERT_COLUMN_MARKED_FOR_READ;
3863 int32 j;
3864 /* See the comment in Field_long::store(long long) */
3865 DBUG_ASSERT(table->in_use == current_thd);
3866 #ifdef WORDS_BIGENDIAN
3867 if (table->s->db_low_byte_first)
3868 j=sint4korr(ptr);
3869 else
3870 #endif
3871 longget(j,ptr);
3872 return unsigned_flag ? (longlong) (uint32) j : (longlong) j;
3873 }
3874
val_str(String * val_buffer,String * val_ptr MY_ATTRIBUTE ((unused)))3875 String *Field_long::val_str(String *val_buffer,
3876 String *val_ptr MY_ATTRIBUTE((unused)))
3877 {
3878 ASSERT_COLUMN_MARKED_FOR_READ;
3879 const CHARSET_INFO *cs= &my_charset_numeric;
3880 uint length;
3881 uint mlength=max(field_length+1,12*cs->mbmaxlen);
3882 val_buffer->alloc(mlength);
3883 char *to=(char*) val_buffer->ptr();
3884 int32 j;
3885 #ifdef WORDS_BIGENDIAN
3886 if (table->s->db_low_byte_first)
3887 j=sint4korr(ptr);
3888 else
3889 #endif
3890 longget(j,ptr);
3891
3892 if (unsigned_flag)
3893 length=cs->cset->long10_to_str(cs,to,mlength, 10,(long) (uint32)j);
3894 else
3895 length=cs->cset->long10_to_str(cs,to,mlength,-10,(long) j);
3896 val_buffer->length(length);
3897 if (zerofill)
3898 prepend_zeros(val_buffer);
3899 val_buffer->set_charset(cs);
3900 return val_buffer;
3901 }
3902
3903
send_binary(Protocol * protocol)3904 bool Field_long::send_binary(Protocol *protocol)
3905 {
3906 ASSERT_COLUMN_MARKED_FOR_READ;
3907 return protocol->store_long(Field_long::val_int());
3908 }
3909
cmp(const uchar * a_ptr,const uchar * b_ptr)3910 int Field_long::cmp(const uchar *a_ptr, const uchar *b_ptr)
3911 {
3912 int32 a,b;
3913 #ifdef WORDS_BIGENDIAN
3914 if (table->s->db_low_byte_first)
3915 {
3916 a=sint4korr(a_ptr);
3917 b=sint4korr(b_ptr);
3918 }
3919 else
3920 #endif
3921 {
3922 longget(a,a_ptr);
3923 longget(b,b_ptr);
3924 }
3925 if (unsigned_flag)
3926 return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
3927 return (a < b) ? -1 : (a > b) ? 1 : 0;
3928 }
3929
make_sort_key(uchar * to,uint length)3930 void Field_long::make_sort_key(uchar *to, uint length)
3931 {
3932 DBUG_ASSERT(length >= 4);
3933 #ifdef WORDS_BIGENDIAN
3934 if (!table->s->db_low_byte_first)
3935 {
3936 if (unsigned_flag)
3937 to[0] = ptr[0];
3938 else
3939 to[0] = (char) (ptr[0] ^ 128); /* Revers signbit */
3940 to[1] = ptr[1];
3941 to[2] = ptr[2];
3942 to[3] = ptr[3];
3943 }
3944 else
3945 #endif
3946 {
3947 if (unsigned_flag)
3948 to[0] = ptr[3];
3949 else
3950 to[0] = (char) (ptr[3] ^ 128); /* Revers signbit */
3951 to[1] = ptr[2];
3952 to[2] = ptr[1];
3953 to[3] = ptr[0];
3954 }
3955 }
3956
3957
sql_type(String & res) const3958 void Field_long::sql_type(String &res) const
3959 {
3960 const CHARSET_INFO *cs=res.charset();
3961 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
3962 "int(%d)",(int) field_length));
3963 add_zerofill_and_unsigned(res);
3964 }
3965
3966 /****************************************************************************
3967 Field type longlong int (8 bytes)
3968 ****************************************************************************/
3969
3970 type_conversion_status
store(const char * from,uint len,const CHARSET_INFO * cs)3971 Field_longlong::store(const char *from,uint len, const CHARSET_INFO *cs)
3972 {
3973 ASSERT_COLUMN_MARKED_FOR_WRITE;
3974 int conv_err= 0;
3975 type_conversion_status error= TYPE_OK;
3976 char *end;
3977 ulonglong tmp;
3978
3979 tmp= cs->cset->strntoull10rnd(cs,from,len,unsigned_flag,&end,&conv_err);
3980 if (conv_err == MY_ERRNO_ERANGE)
3981 {
3982 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
3983 error= TYPE_WARN_OUT_OF_RANGE;
3984 }
3985 else if (table->in_use->count_cuted_fields &&
3986 check_int(cs, from, len, end, conv_err))
3987 error= TYPE_WARN_OUT_OF_RANGE;
3988 else
3989 error= TYPE_OK;
3990
3991 #ifdef WORDS_BIGENDIAN
3992 if (table->s->db_low_byte_first)
3993 {
3994 int8store(ptr,tmp);
3995 }
3996 else
3997 #endif
3998 longlongstore(ptr,tmp);
3999 return error;
4000 }
4001
4002
store(double nr)4003 type_conversion_status Field_longlong::store(double nr)
4004 {
4005 ASSERT_COLUMN_MARKED_FOR_WRITE;
4006 type_conversion_status error= TYPE_OK;
4007 longlong res;
4008
4009 nr= rint(nr);
4010 if (unsigned_flag)
4011 {
4012 if (nr < 0)
4013 {
4014 res=0;
4015 error= TYPE_WARN_OUT_OF_RANGE;
4016 }
4017 else if (nr >= (double) ULONGLONG_MAX)
4018 {
4019 res= ~(longlong) 0;
4020 error= TYPE_WARN_OUT_OF_RANGE;
4021 }
4022 else
4023 res=(longlong) double2ulonglong(nr);
4024 }
4025 else
4026 {
4027 if (nr <= (double) LONGLONG_MIN)
4028 {
4029 res= LONGLONG_MIN;
4030 if (nr < (double) LONGLONG_MIN)
4031 error= TYPE_WARN_OUT_OF_RANGE;
4032 }
4033 else if (nr >= (double) (ulonglong) LONGLONG_MAX)
4034 {
4035 res= LONGLONG_MAX;
4036 if (nr > (double) LONGLONG_MAX)
4037 error= TYPE_WARN_OUT_OF_RANGE;
4038 }
4039 else
4040 res=(longlong) nr;
4041 }
4042 if (error)
4043 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4044
4045 #ifdef WORDS_BIGENDIAN
4046 if (table->s->db_low_byte_first)
4047 {
4048 int8store(ptr,res);
4049 }
4050 else
4051 #endif
4052 longlongstore(ptr,res);
4053 return error;
4054 }
4055
4056
store(longlong nr,bool unsigned_val)4057 type_conversion_status Field_longlong::store(longlong nr, bool unsigned_val)
4058 {
4059 ASSERT_COLUMN_MARKED_FOR_WRITE;
4060 type_conversion_status error= TYPE_OK;
4061
4062 if (nr < 0) // Only possible error
4063 {
4064 /*
4065 if field is unsigned and value is signed (< 0) or
4066 if field is signed and value is unsigned we have an overflow
4067 */
4068 if (unsigned_flag != unsigned_val)
4069 {
4070 nr= unsigned_flag ? (ulonglong) 0 : (ulonglong) LONGLONG_MAX;
4071 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4072 error= TYPE_WARN_OUT_OF_RANGE;
4073 }
4074 }
4075
4076 #ifdef WORDS_BIGENDIAN
4077 if (table->s->db_low_byte_first)
4078 {
4079 int8store(ptr,nr);
4080 }
4081 else
4082 #endif
4083 longlongstore(ptr,nr);
4084 return error;
4085 }
4086
4087
val_real(void)4088 double Field_longlong::val_real(void)
4089 {
4090 ASSERT_COLUMN_MARKED_FOR_READ;
4091 longlong j;
4092 #ifdef WORDS_BIGENDIAN
4093 if (table->s->db_low_byte_first)
4094 {
4095 j=sint8korr(ptr);
4096 }
4097 else
4098 #endif
4099 longlongget(j,ptr);
4100 /* The following is open coded to avoid a bug in gcc 3.3 */
4101 if (unsigned_flag)
4102 {
4103 ulonglong tmp= (ulonglong) j;
4104 return ulonglong2double(tmp);
4105 }
4106 return (double) j;
4107 }
4108
4109
val_int(void)4110 longlong Field_longlong::val_int(void)
4111 {
4112 ASSERT_COLUMN_MARKED_FOR_READ;
4113 longlong j;
4114 #ifdef WORDS_BIGENDIAN
4115 if (table->s->db_low_byte_first)
4116 j=sint8korr(ptr);
4117 else
4118 #endif
4119 longlongget(j,ptr);
4120 return j;
4121 }
4122
4123
val_str(String * val_buffer,String * val_ptr MY_ATTRIBUTE ((unused)))4124 String *Field_longlong::val_str(String *val_buffer,
4125 String *val_ptr MY_ATTRIBUTE((unused)))
4126 {
4127 const CHARSET_INFO *cs= &my_charset_numeric;
4128 uint length;
4129 uint mlength=max(field_length+1,22*cs->mbmaxlen);
4130 val_buffer->alloc(mlength);
4131 char *to=(char*) val_buffer->ptr();
4132 longlong j;
4133 #ifdef WORDS_BIGENDIAN
4134 if (table->s->db_low_byte_first)
4135 j=sint8korr(ptr);
4136 else
4137 #endif
4138 longlongget(j,ptr);
4139
4140 length=(uint) (cs->cset->longlong10_to_str)(cs,to,mlength,
4141 unsigned_flag ? 10 : -10, j);
4142 val_buffer->length(length);
4143 if (zerofill)
4144 prepend_zeros(val_buffer);
4145 val_buffer->set_charset(cs);
4146 return val_buffer;
4147 }
4148
4149
send_binary(Protocol * protocol)4150 bool Field_longlong::send_binary(Protocol *protocol)
4151 {
4152 ASSERT_COLUMN_MARKED_FOR_READ;
4153 return protocol->store_longlong(Field_longlong::val_int(), unsigned_flag);
4154 }
4155
4156
cmp(const uchar * a_ptr,const uchar * b_ptr)4157 int Field_longlong::cmp(const uchar *a_ptr, const uchar *b_ptr)
4158 {
4159 longlong a,b;
4160 #ifdef WORDS_BIGENDIAN
4161 if (table->s->db_low_byte_first)
4162 {
4163 a=sint8korr(a_ptr);
4164 b=sint8korr(b_ptr);
4165 }
4166 else
4167 #endif
4168 {
4169 longlongget(a,a_ptr);
4170 longlongget(b,b_ptr);
4171 }
4172 if (unsigned_flag)
4173 return ((ulonglong) a < (ulonglong) b) ? -1 :
4174 ((ulonglong) a > (ulonglong) b) ? 1 : 0;
4175 return (a < b) ? -1 : (a > b) ? 1 : 0;
4176 }
4177
4178
make_sort_key(uchar * to,uint length)4179 void Field_longlong::make_sort_key(uchar *to, uint length)
4180 {
4181 const int from_length= PACK_LENGTH;
4182 const int to_length= min<int>(from_length, length);
4183 #ifdef WORDS_BIGENDIAN
4184 if (table == NULL || !table->s->db_low_byte_first)
4185 copy_integer<true>(to, to_length, ptr, from_length, unsigned_flag);
4186 else
4187 #endif
4188 copy_integer<false>(to, to_length, ptr, from_length, unsigned_flag);
4189 }
4190
4191
sql_type(String & res) const4192 void Field_longlong::sql_type(String &res) const
4193 {
4194 const CHARSET_INFO *cs=res.charset();
4195 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4196 "bigint(%d)",(int) field_length));
4197 add_zerofill_and_unsigned(res);
4198 }
4199
4200
4201 /*
4202 Floating-point numbers
4203 */
4204
4205 uchar *
pack(uchar * to,const uchar * from,uint max_length,bool low_byte_first)4206 Field_real::pack(uchar *to, const uchar *from,
4207 uint max_length, bool low_byte_first)
4208 {
4209 DBUG_ENTER("Field_real::pack");
4210 DBUG_ASSERT(max_length >= pack_length());
4211 #ifdef WORDS_BIGENDIAN
4212 if (low_byte_first != table->s->db_low_byte_first)
4213 {
4214 const uchar *dptr= from + pack_length();
4215 while (dptr-- > from)
4216 *to++ = *dptr;
4217 DBUG_RETURN(to);
4218 }
4219 else
4220 #endif
4221 DBUG_RETURN(Field::pack(to, from, max_length, low_byte_first));
4222 }
4223
4224 const uchar *
unpack(uchar * to,const uchar * from,uint param_data,bool low_byte_first)4225 Field_real::unpack(uchar *to, const uchar *from,
4226 uint param_data, bool low_byte_first)
4227 {
4228 DBUG_ENTER("Field_real::unpack");
4229 #ifdef WORDS_BIGENDIAN
4230 if (low_byte_first != table->s->db_low_byte_first)
4231 {
4232 const uchar *dptr= from + pack_length();
4233 while (dptr-- > from)
4234 *to++ = *dptr;
4235 DBUG_RETURN(from + pack_length());
4236 }
4237 else
4238 #endif
4239 DBUG_RETURN(Field::unpack(to, from, param_data, low_byte_first));
4240 }
4241
4242
4243 type_conversion_status
store_time(MYSQL_TIME * ltime,uint8 dec_arg MY_ATTRIBUTE ((unused)))4244 Field_real::store_time(MYSQL_TIME *ltime,
4245 uint8 dec_arg MY_ATTRIBUTE((unused)))
4246 {
4247 double nr= TIME_to_double(ltime);
4248 return store(ltime->neg ? -nr : nr);
4249 }
4250
4251
4252 /****************************************************************************
4253 single precision float
4254 ****************************************************************************/
4255
4256 type_conversion_status
store(const char * from,uint len,const CHARSET_INFO * cs)4257 Field_float::store(const char *from,uint len, const CHARSET_INFO *cs)
4258 {
4259 int conv_error;
4260 type_conversion_status err= TYPE_OK;
4261 char *end;
4262 double nr= my_strntod(cs,(char*) from,len,&end,&conv_error);
4263 if (conv_error || (!len || ((uint) (end-from) != len &&
4264 table->in_use->count_cuted_fields)))
4265 {
4266 set_warning(Sql_condition::WARN_LEVEL_WARN,
4267 (conv_error ? ER_WARN_DATA_OUT_OF_RANGE
4268 : WARN_DATA_TRUNCATED),
4269 1);
4270 err= conv_error ? TYPE_WARN_OUT_OF_RANGE : TYPE_WARN_TRUNCATED;
4271 }
4272 Field_float::store(nr);
4273 return err;
4274 }
4275
4276
store(double nr)4277 type_conversion_status Field_float::store(double nr)
4278 {
4279 ASSERT_COLUMN_MARKED_FOR_WRITE;
4280 const type_conversion_status error=
4281 truncate(&nr, FLT_MAX) ? TYPE_WARN_OUT_OF_RANGE : TYPE_OK;
4282
4283 float j= (float)nr;
4284
4285 #ifdef WORDS_BIGENDIAN
4286 if (table->s->db_low_byte_first)
4287 {
4288 float4store(ptr,j);
4289 }
4290 else
4291 #endif
4292 memcpy(ptr, &j, sizeof(j));
4293 return error;
4294 }
4295
4296
store(longlong nr,bool unsigned_val)4297 type_conversion_status Field_float::store(longlong nr, bool unsigned_val)
4298 {
4299 return Field_float::store(unsigned_val ? ulonglong2double((ulonglong) nr) :
4300 (double) nr);
4301 }
4302
4303
val_real(void)4304 double Field_float::val_real(void)
4305 {
4306 ASSERT_COLUMN_MARKED_FOR_READ;
4307 float j;
4308 #ifdef WORDS_BIGENDIAN
4309 if (table->s->db_low_byte_first)
4310 {
4311 float4get(j,ptr);
4312 }
4313 else
4314 #endif
4315 memcpy(&j, ptr, sizeof(j));
4316 return ((double) j);
4317 }
4318
val_int(void)4319 longlong Field_float::val_int(void)
4320 {
4321 float j;
4322 #ifdef WORDS_BIGENDIAN
4323 if (table->s->db_low_byte_first)
4324 {
4325 float4get(j,ptr);
4326 }
4327 else
4328 #endif
4329 memcpy(&j, ptr, sizeof(j));
4330 return (longlong) rint(j);
4331 }
4332
4333
val_str(String * val_buffer,String * val_ptr MY_ATTRIBUTE ((unused)))4334 String *Field_float::val_str(String *val_buffer,
4335 String *val_ptr MY_ATTRIBUTE((unused)))
4336 {
4337 ASSERT_COLUMN_MARKED_FOR_READ;
4338 DBUG_ASSERT(!zerofill || field_length <= MAX_FIELD_CHARLENGTH);
4339 float nr;
4340 #ifdef WORDS_BIGENDIAN
4341 if (table->s->db_low_byte_first)
4342 {
4343 float4get(nr,ptr);
4344 }
4345 else
4346 #endif
4347 memcpy(&nr, ptr, sizeof(nr));
4348
4349 uint to_length= 70;
4350 if (val_buffer->alloc(to_length))
4351 {
4352 my_error(ER_OUT_OF_RESOURCES, MYF(0));
4353 return val_buffer;
4354 }
4355
4356 char *to=(char*) val_buffer->ptr();
4357 size_t len;
4358
4359 if (dec >= NOT_FIXED_DEC)
4360 len= my_gcvt(nr, MY_GCVT_ARG_FLOAT, to_length - 1, to, NULL);
4361 else
4362 {
4363 /*
4364 We are safe here because the buffer length is 70, and
4365 fabs(float) < 10^39, dec < NOT_FIXED_DEC. So the resulting string
4366 will be not longer than 69 chars + terminating '\0'.
4367 */
4368 len= my_fcvt(nr, dec, to, NULL);
4369 }
4370 val_buffer->length((uint) len);
4371 if (zerofill)
4372 prepend_zeros(val_buffer);
4373 val_buffer->set_charset(&my_charset_numeric);
4374 return val_buffer;
4375 }
4376
4377
cmp(const uchar * a_ptr,const uchar * b_ptr)4378 int Field_float::cmp(const uchar *a_ptr, const uchar *b_ptr)
4379 {
4380 float a,b;
4381 #ifdef WORDS_BIGENDIAN
4382 if (table->s->db_low_byte_first)
4383 {
4384 float4get(a,a_ptr);
4385 float4get(b,b_ptr);
4386 }
4387 else
4388 #endif
4389 {
4390 memcpy(&a, a_ptr, sizeof(float));
4391 memcpy(&b, b_ptr, sizeof(float));
4392 }
4393 return (a < b) ? -1 : (a > b) ? 1 : 0;
4394 }
4395
4396 #define FLT_EXP_DIG (sizeof(float)*8-FLT_MANT_DIG)
4397
make_sort_key(uchar * to,uint length)4398 void Field_float::make_sort_key(uchar *to, uint length)
4399 {
4400 DBUG_ASSERT(length >= 4);
4401 float nr;
4402 #ifdef WORDS_BIGENDIAN
4403 if (table->s->db_low_byte_first)
4404 {
4405 float4get(nr,ptr);
4406 }
4407 else
4408 #endif
4409 memcpy(&nr, ptr, min<uint>(length, sizeof(float)));
4410
4411 uchar *tmp= to;
4412 if (nr == (float) 0.0)
4413 { /* Change to zero string */
4414 tmp[0]=(uchar) 128;
4415 memset(tmp + 1, 0, min<uint>(length, sizeof(nr) - 1));
4416 }
4417 else
4418 {
4419 #ifdef WORDS_BIGENDIAN
4420 memcpy(tmp, &nr, sizeof(nr));
4421 #else
4422 tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
4423 #endif
4424 if (tmp[0] & 128) /* Negative */
4425 { /* make complement */
4426 uint i;
4427 for (i=0 ; i < sizeof(nr); i++)
4428 tmp[i]= (uchar) (tmp[i] ^ (uchar) 255);
4429 }
4430 else
4431 {
4432 ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
4433 (ushort) 32768);
4434 exp_part+= (ushort) 1 << (16-1-FLT_EXP_DIG);
4435 tmp[0]= (uchar) (exp_part >> 8);
4436 tmp[1]= (uchar) exp_part;
4437 }
4438 }
4439 }
4440
4441
send_binary(Protocol * protocol)4442 bool Field_float::send_binary(Protocol *protocol)
4443 {
4444 ASSERT_COLUMN_MARKED_FOR_READ;
4445 return protocol->store((float) Field_float::val_real(), dec, (String*) 0);
4446 }
4447
4448
4449 /**
4450 Save the field metadata for float fields.
4451
4452 Saves the pack length in the first byte.
4453
4454 @param metadata_ptr First byte of field metadata
4455
4456 @returns number of bytes written to metadata_ptr
4457 */
do_save_field_metadata(uchar * metadata_ptr)4458 int Field_float::do_save_field_metadata(uchar *metadata_ptr)
4459 {
4460 *metadata_ptr= pack_length();
4461 return 1;
4462 }
4463
4464
sql_type(String & res) const4465 void Field_float::sql_type(String &res) const
4466 {
4467 if (dec == NOT_FIXED_DEC)
4468 {
4469 res.set_ascii(STRING_WITH_LEN("float"));
4470 }
4471 else
4472 {
4473 const CHARSET_INFO *cs= res.charset();
4474 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4475 "float(%d,%d)",(int) field_length,dec));
4476 }
4477 add_zerofill_and_unsigned(res);
4478 }
4479
4480
4481 /****************************************************************************
4482 double precision floating point numbers
4483 ****************************************************************************/
4484
4485 type_conversion_status
store(const char * from,uint len,const CHARSET_INFO * cs)4486 Field_double::store(const char *from,uint len, const CHARSET_INFO *cs)
4487 {
4488 int conv_error;
4489 type_conversion_status error= TYPE_OK;
4490 char *end;
4491 double nr= my_strntod(cs,(char*) from, len, &end, &conv_error);
4492 if ((conv_error != 0) || (!len || ((uint) (end-from) != len &&
4493 table->in_use->count_cuted_fields)))
4494 {
4495 set_warning(Sql_condition::WARN_LEVEL_WARN,
4496 (conv_error ? ER_WARN_DATA_OUT_OF_RANGE
4497 : WARN_DATA_TRUNCATED),
4498 1);
4499 error= conv_error ? TYPE_WARN_OUT_OF_RANGE : TYPE_WARN_TRUNCATED;
4500 }
4501 Field_double::store(nr);
4502 return error;
4503 }
4504
4505
store(double nr)4506 type_conversion_status Field_double::store(double nr)
4507 {
4508 ASSERT_COLUMN_MARKED_FOR_WRITE;
4509 const type_conversion_status error=
4510 truncate(&nr, DBL_MAX) ? TYPE_WARN_OUT_OF_RANGE : TYPE_OK;
4511
4512 #ifdef WORDS_BIGENDIAN
4513 if (table->s->db_low_byte_first)
4514 {
4515 float8store(ptr,nr);
4516 }
4517 else
4518 #endif
4519 doublestore(ptr,nr);
4520 return error;
4521 }
4522
4523
store(longlong nr,bool unsigned_val)4524 type_conversion_status Field_double::store(longlong nr, bool unsigned_val)
4525 {
4526 return Field_double::store(unsigned_val ? ulonglong2double((ulonglong) nr) :
4527 (double) nr);
4528 }
4529
4530 /*
4531 If a field has fixed length, truncate the double argument pointed to by 'nr'
4532 appropriately.
4533 Also ensure that the argument is within [-max_value; max_value] range.
4534 */
4535
truncate(double * nr,double max_value)4536 bool Field_real::truncate(double *nr, double max_value)
4537 {
4538 if (my_isnan(*nr))
4539 {
4540 *nr= 0;
4541 set_null();
4542 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4543 return true;
4544 }
4545 else if (unsigned_flag && *nr < 0)
4546 {
4547 *nr= 0;
4548 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4549 return true;
4550 }
4551
4552 if (!not_fixed)
4553 {
4554 uint order= field_length - dec;
4555 uint step= array_elements(log_10) - 1;
4556 max_value= 1.0;
4557 for (; order > step; order-= step)
4558 max_value*= log_10[step];
4559 max_value*= log_10[order];
4560 max_value-= 1.0 / log_10[dec];
4561
4562 /* Check for infinity so we don't get NaN in calculations */
4563 if (!my_isinf(*nr))
4564 {
4565 double tmp= rint((*nr - floor(*nr)) * log_10[dec]) / log_10[dec];
4566 *nr= floor(*nr) + tmp;
4567 }
4568 }
4569
4570 if (*nr < -max_value)
4571 {
4572 *nr= -max_value;
4573 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4574 return true;
4575 }
4576 else if (*nr > max_value)
4577 {
4578 *nr= max_value;
4579 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
4580 return true;
4581 }
4582
4583 return false;
4584 }
4585
4586
store_decimal(const my_decimal * dm)4587 type_conversion_status Field_real::store_decimal(const my_decimal *dm)
4588 {
4589 double dbl;
4590 my_decimal2double(E_DEC_FATAL_ERROR, dm, &dbl);
4591 return store(dbl);
4592 }
4593
val_real(void)4594 double Field_double::val_real(void)
4595 {
4596 ASSERT_COLUMN_MARKED_FOR_READ;
4597 double j;
4598 #ifdef WORDS_BIGENDIAN
4599 if (table->s->db_low_byte_first)
4600 {
4601 float8get(j,ptr);
4602 }
4603 else
4604 #endif
4605 doubleget(j,ptr);
4606 return j;
4607 }
4608
val_int(void)4609 longlong Field_double::val_int(void)
4610 {
4611 ASSERT_COLUMN_MARKED_FOR_READ;
4612 double j;
4613 longlong res;
4614 #ifdef WORDS_BIGENDIAN
4615 if (table->s->db_low_byte_first)
4616 {
4617 float8get(j,ptr);
4618 }
4619 else
4620 #endif
4621 doubleget(j,ptr);
4622 /* Check whether we fit into longlong range */
4623 if (j <= (double) LONGLONG_MIN)
4624 {
4625 res= (longlong) LONGLONG_MIN;
4626 goto warn;
4627 }
4628 if (j >= (double) (ulonglong) LONGLONG_MAX)
4629 {
4630 res= (longlong) LONGLONG_MAX;
4631 goto warn;
4632 }
4633 return (longlong) rint(j);
4634
4635 warn:
4636 {
4637 char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
4638 String tmp(buf, sizeof(buf), &my_charset_latin1), *str;
4639 str= val_str(&tmp, 0);
4640 ErrConvString err(str);
4641 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
4642 ER_TRUNCATED_WRONG_VALUE,
4643 ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
4644 err.ptr());
4645 }
4646 return res;
4647 }
4648
4649
val_decimal(my_decimal * decimal_value)4650 my_decimal *Field_real::val_decimal(my_decimal *decimal_value)
4651 {
4652 ASSERT_COLUMN_MARKED_FOR_READ;
4653 double2my_decimal(E_DEC_FATAL_ERROR, val_real(), decimal_value);
4654 return decimal_value;
4655 }
4656
4657
get_date(MYSQL_TIME * ltime,uint fuzzydate)4658 bool Field_real::get_date(MYSQL_TIME *ltime, uint fuzzydate)
4659 {
4660 return my_double_to_datetime_with_warn(val_real(), ltime, fuzzydate);
4661 }
4662
4663
get_time(MYSQL_TIME * ltime)4664 bool Field_real::get_time(MYSQL_TIME *ltime)
4665 {
4666 return my_double_to_time_with_warn(val_real(), ltime);
4667 }
4668
4669
val_str(String * val_buffer,String * val_ptr MY_ATTRIBUTE ((unused)))4670 String *Field_double::val_str(String *val_buffer,
4671 String *val_ptr MY_ATTRIBUTE((unused)))
4672 {
4673 ASSERT_COLUMN_MARKED_FOR_READ;
4674 DBUG_ASSERT(!zerofill || field_length <= MAX_FIELD_CHARLENGTH);
4675 double nr;
4676 #ifdef WORDS_BIGENDIAN
4677 if (table->s->db_low_byte_first)
4678 {
4679 float8get(nr,ptr);
4680 }
4681 else
4682 #endif
4683 doubleget(nr,ptr);
4684 uint to_length= DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE;
4685 if (val_buffer->alloc(to_length))
4686 {
4687 my_error(ER_OUT_OF_RESOURCES, MYF(0));
4688 return val_buffer;
4689 }
4690
4691 char *to=(char*) val_buffer->ptr();
4692 size_t len;
4693
4694 if (dec >= NOT_FIXED_DEC)
4695 len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
4696 else
4697 len= my_fcvt(nr, dec, to, NULL);
4698
4699 val_buffer->length((uint) len);
4700 if (zerofill)
4701 prepend_zeros(val_buffer);
4702 val_buffer->set_charset(&my_charset_numeric);
4703 return val_buffer;
4704 }
4705
send_binary(Protocol * protocol)4706 bool Field_double::send_binary(Protocol *protocol)
4707 {
4708 return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
4709 }
4710
4711
cmp(const uchar * a_ptr,const uchar * b_ptr)4712 int Field_double::cmp(const uchar *a_ptr, const uchar *b_ptr)
4713 {
4714 double a,b;
4715 #ifdef WORDS_BIGENDIAN
4716 if (table->s->db_low_byte_first)
4717 {
4718 float8get(a,a_ptr);
4719 float8get(b,b_ptr);
4720 }
4721 else
4722 #endif
4723 {
4724 doubleget(a, a_ptr);
4725 doubleget(b, b_ptr);
4726 }
4727 return (a < b) ? -1 : (a > b) ? 1 : 0;
4728 }
4729
4730
4731 #define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
4732
4733 /* The following should work for IEEE */
4734
make_sort_key(uchar * to,uint length)4735 void Field_double::make_sort_key(uchar *to, uint length)
4736 {
4737 double nr;
4738 #ifdef WORDS_BIGENDIAN
4739 if (table->s->db_low_byte_first)
4740 {
4741 float8get(nr, ptr);
4742 }
4743 else
4744 #endif
4745 doubleget(nr, ptr);
4746 if (length < 8)
4747 {
4748 uchar buff[8];
4749 change_double_for_sort(nr, buff);
4750 memcpy(to, buff, length);
4751 }
4752 else
4753 change_double_for_sort(nr, to);
4754 }
4755
4756
4757 /**
4758 Save the field metadata for double fields.
4759
4760 Saves the pack length in the first byte of the field metadata array
4761 at index of *metadata_ptr.
4762
4763 @param metadata_ptr First byte of field metadata
4764
4765 @returns number of bytes written to metadata_ptr
4766 */
do_save_field_metadata(uchar * metadata_ptr)4767 int Field_double::do_save_field_metadata(uchar *metadata_ptr)
4768 {
4769 *metadata_ptr= pack_length();
4770 return 1;
4771 }
4772
4773
sql_type(String & res) const4774 void Field_double::sql_type(String &res) const
4775 {
4776 const CHARSET_INFO *cs=res.charset();
4777 if (dec == NOT_FIXED_DEC)
4778 {
4779 res.set_ascii(STRING_WITH_LEN("double"));
4780 }
4781 else
4782 {
4783 res.length(cs->cset->snprintf(cs,(char*) res.ptr(),res.alloced_length(),
4784 "double(%d,%d)",(int) field_length,dec));
4785 }
4786 add_zerofill_and_unsigned(res);
4787 }
4788
4789
4790 /****************************************************************************
4791 ** Common code for all temporal data types: DATE, DATETIME, TIMESTAMP, TIME
4792 *****************************************************************************/
4793
is_equal(Create_field * new_field)4794 uint Field_temporal::is_equal(Create_field *new_field)
4795 {
4796 return new_field->sql_type == real_type() &&
4797 new_field->decimals == decimals();
4798 }
4799
4800
val_decimal(my_decimal * decimal_value)4801 my_decimal *Field_temporal::val_decimal(my_decimal *decimal_value)
4802 {
4803 ASSERT_COLUMN_MARKED_FOR_READ;
4804 DBUG_ASSERT(decimals() == 0);
4805 int2my_decimal(E_DEC_FATAL_ERROR, val_int(), 0, decimal_value);
4806 return decimal_value;
4807 }
4808
4809
4810 /**
4811 Set warnings from a warning vector.
4812 Note, multiple warnings can be set at the same time.
4813
4814 @param str Value.
4815 @param warnings Warning vector.
4816 */
4817 void
set_warnings(ErrConvString str,int warnings)4818 Field_temporal::set_warnings(ErrConvString str, int warnings)
4819 {
4820 int cut_incremented= 0;
4821 timestamp_type ts_type= field_type_to_timestamp_type(type());
4822
4823 if (warnings & MYSQL_TIME_WARN_TRUNCATED)
4824 {
4825 set_datetime_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED,
4826 str, ts_type, !cut_incremented);
4827 cut_incremented= 1;
4828 }
4829 if (warnings & (MYSQL_TIME_WARN_OUT_OF_RANGE | MYSQL_TIME_WARN_ZERO_DATE |
4830 MYSQL_TIME_WARN_ZERO_IN_DATE))
4831 {
4832 set_datetime_warning(Sql_condition::WARN_LEVEL_WARN,
4833 ER_WARN_DATA_OUT_OF_RANGE,
4834 str, ts_type, !cut_incremented);
4835 cut_incremented= 1;
4836 }
4837 if (warnings & MYSQL_TIME_WARN_INVALID_TIMESTAMP)
4838 {
4839 set_datetime_warning(Sql_condition::WARN_LEVEL_WARN,
4840 ER_WARN_INVALID_TIMESTAMP,
4841 str, ts_type, !cut_incremented);
4842 cut_incremented= 1;
4843 }
4844 if ((warnings & MYSQL_TIME_NOTE_TRUNCATED) &&
4845 !(warnings & MYSQL_TIME_WARN_TRUNCATED))
4846 {
4847 set_datetime_warning(Sql_condition::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED,
4848 str, ts_type, !cut_incremented);
4849 }
4850 }
4851
4852
store(longlong nr,bool unsigned_val)4853 type_conversion_status Field_temporal::store(longlong nr, bool unsigned_val)
4854 {
4855 ASSERT_COLUMN_MARKED_FOR_WRITE;
4856 int warnings= 0;
4857 MYSQL_TIME ltime;
4858 type_conversion_status error= convert_number_to_TIME(nr, unsigned_val, 0,
4859 <ime, &warnings);
4860 if (error == TYPE_OK || error == TYPE_NOTE_TRUNCATED)
4861 error= store_internal(<ime, &warnings);
4862 else
4863 {
4864 DBUG_ASSERT(warnings != 0); // Must be set by convert_number_to_TIME
4865
4866 if (warnings & (MYSQL_TIME_WARN_ZERO_DATE |
4867 MYSQL_TIME_WARN_ZERO_IN_DATE) &&
4868 !current_thd->is_strict_mode())
4869 error= TYPE_NOTE_TIME_TRUNCATED;
4870 }
4871 if (warnings)
4872 set_warnings(ErrConvString(nr, unsigned_val), warnings);
4873 return error;
4874 }
4875
4876
4877 type_conversion_status
store_lldiv_t(const lldiv_t * lld,int * warnings)4878 Field_temporal::store_lldiv_t(const lldiv_t *lld, int *warnings)
4879 {
4880 ASSERT_COLUMN_MARKED_FOR_WRITE;
4881 type_conversion_status error;
4882 MYSQL_TIME ltime;
4883 error= convert_number_to_TIME(lld->quot, 0, lld->rem, <ime, warnings);
4884 if (error == TYPE_OK || error == TYPE_NOTE_TRUNCATED)
4885 error= store_internal_with_round(<ime, warnings);
4886 else if (!*warnings)
4887 {
4888 DBUG_ASSERT(warnings != 0); // Must be set by convert_number_to_TIME
4889 if (((*warnings & MYSQL_TIME_WARN_ZERO_DATE) != 0 ||
4890 (*warnings & MYSQL_TIME_WARN_ZERO_IN_DATE) != 0) &&
4891 !current_thd->is_strict_mode())
4892 error= TYPE_NOTE_TIME_TRUNCATED;
4893 }
4894
4895 return error;
4896 }
4897
4898
store_decimal(const my_decimal * decimal)4899 type_conversion_status Field_temporal::store_decimal(const my_decimal *decimal)
4900 {
4901 ASSERT_COLUMN_MARKED_FOR_WRITE;
4902 lldiv_t lld;
4903 int warnings= 0;
4904 /* Pass 0 in the first argument, not to produce warnings automatically */
4905 my_decimal2lldiv_t(0, decimal, &lld);
4906 const type_conversion_status error= store_lldiv_t(&lld, &warnings);
4907 if (warnings)
4908 set_warnings(ErrConvString(decimal), warnings);
4909 return error;
4910 }
4911
4912
store(double nr)4913 type_conversion_status Field_temporal::store(double nr)
4914 {
4915 ASSERT_COLUMN_MARKED_FOR_WRITE;
4916 int warnings= 0;
4917 lldiv_t lld;
4918 double2lldiv_t(nr, &lld);
4919 const type_conversion_status error= store_lldiv_t(&lld, &warnings);
4920 if (warnings)
4921 set_warnings(ErrConvString(nr), warnings);
4922 return error;
4923 }
4924
4925
4926 /**
4927 Store string into a date/time/datetime field.
4928
4929 @param from Date/time string
4930 @param len Length of the string
4931 @param cs Character set of the string
4932
4933 @retval TYPE_OK Storage of value went fine without warnings or errors
4934 @retval !TYPE_OK Warning/error as indicated by type_conversion_status enum
4935 value
4936 */
4937 type_conversion_status
store(const char * str,uint len,const CHARSET_INFO * cs)4938 Field_temporal::store(const char *str, uint len, const CHARSET_INFO *cs)
4939 {
4940 ASSERT_COLUMN_MARKED_FOR_WRITE;
4941 type_conversion_status error= TYPE_OK;
4942 MYSQL_TIME ltime;
4943 MYSQL_TIME_STATUS status;
4944 if (convert_str_to_TIME(str, len, cs, <ime, &status))
4945 {
4946 /*
4947 When convert_str_to_TIME() returns error, ltime has been set to
4948 0 so there's nothing to store in the field.
4949 */
4950 reset();
4951 if (status.warnings & (MYSQL_TIME_WARN_ZERO_DATE |
4952 MYSQL_TIME_WARN_ZERO_IN_DATE) &&
4953 !current_thd->is_strict_mode())
4954 error= TYPE_NOTE_TIME_TRUNCATED;
4955 else
4956 error= TYPE_ERR_BAD_VALUE;
4957 }
4958 else
4959 {
4960 error= time_warning_to_type_conversion_status(status.warnings);
4961
4962 const type_conversion_status tmp_error= store_internal_with_round(<ime,
4963 &status.warnings);
4964
4965 // Return the most serious error of the two, see type_conversion_status
4966 if (tmp_error > error)
4967 error= tmp_error;
4968 }
4969 if (status.warnings)
4970 set_warnings(ErrConvString(str, len, cs), status.warnings);
4971 return error;
4972 }
4973
4974
4975 /**
4976 @retval -1 Timestamp with wrong values
4977 @retval anything else DATETIME as integer in YYYYMMDDHHMMSS format
4978 */
4979 longlong
convert_number_to_datetime(longlong nr,bool unsigned_val,MYSQL_TIME * ltime,int * warnings)4980 Field_temporal::convert_number_to_datetime(longlong nr, bool unsigned_val,
4981 MYSQL_TIME *ltime, int *warnings)
4982 {
4983 /*
4984 Note, number_to_datetime can return a result different from nr:
4985 e.g. 111111 -> 20111111000000
4986 */
4987 longlong tmp= number_to_datetime(nr, ltime, date_flags(), warnings);
4988 if (tmp == LL(-1))
4989 reset();
4990 return tmp;
4991 }
4992
4993
4994 /****************************************************************************
4995 ** Common code for temporal data types with date: DATE, DATETIME, TIMESTAMP
4996 *****************************************************************************/
4997
4998
get_internal_check_zero(MYSQL_TIME * ltime,uint fuzzydate)4999 bool Field_temporal_with_date::get_internal_check_zero(MYSQL_TIME *ltime,
5000 uint fuzzydate)
5001 {
5002 if (get_date_internal(ltime)) /* '0000-00-00' */
5003 {
5004 DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
5005 if (fuzzydate & TIME_NO_ZERO_DATE)
5006 return true;
5007 set_zero_time(ltime, MYSQL_TIMESTAMP_DATETIME);
5008 }
5009 return false;
5010 }
5011
5012
val_date_temporal()5013 longlong Field_temporal_with_date::val_date_temporal()
5014 {
5015 ASSERT_COLUMN_MARKED_FOR_READ;
5016 MYSQL_TIME ltime;
5017 return get_date_internal(<ime) ?
5018 0 : TIME_to_longlong_datetime_packed(<ime);
5019 }
5020
5021
val_time_temporal()5022 longlong Field_temporal_with_date::val_time_temporal()
5023 {
5024 /*
5025 There are currently no tests covering this method,
5026 as DATETIME seems to always superseed over TIME in comparison.
5027 */
5028 ASSERT_COLUMN_MARKED_FOR_READ;
5029 MYSQL_TIME ltime;
5030 return get_date_internal(<ime) ? 0 : TIME_to_longlong_time_packed(<ime);
5031 }
5032
5033
5034 /**
5035 Convert a number in format YYMMDDhhmmss to string.
5036 Straight coded to avoid problem with slow longlong arithmetic and sprintf.
5037
5038 @param OUT String - pointer to convert to.
5039 @param tmp - number with datetime value.
5040 */
5041 static inline int
my_datetime_number_to_str(char * pos,longlong tmp)5042 my_datetime_number_to_str(char *pos, longlong tmp)
5043 {
5044 long part1= (long) (tmp / 1000000LL);
5045 long part2= (long) (tmp - (ulonglong) part1 * 1000000LL);
5046 int part3;
5047 pos+= MAX_DATETIME_WIDTH; /* Start from the end */
5048 *pos--= 0;
5049 *pos--= (char) ('0' + (char) (part2 % 10)); /* Seconds */
5050 part2 /= 10;
5051 *pos--= (char) ('0' + (char) (part2 % 10));
5052 part3= (int) (part2 / 10);
5053 *pos--= ':';
5054 *pos--= (char) ('0' + (char) (part3 % 10)); /* Minutes */
5055 part3 /= 10;
5056 *pos--= (char) ('0' + (char) (part3 % 10));
5057 part3 /= 10;
5058 *pos--= ':';
5059 *pos--= (char) ('0' + (char) (part3 % 10)); /* Hours */
5060 part3 /= 10;
5061 *pos--= (char) ('0' + (char) part3);
5062 *pos--= ' ';
5063 *pos--= (char) ('0' + (char) (part1 % 10)); /* Day */
5064 part1 /= 10;
5065 *pos--= (char) ('0' + (char) (part1 % 10));
5066 part1 /= 10;
5067 *pos--= '-';
5068 *pos--= (char) ('0' + (char) (part1 % 10)); /* Month */
5069 part1 /= 10;
5070 *pos--= (char) ('0' + (char) (part1 % 10));
5071 part3= (int) (part1 / 10);
5072 *pos--= '-';
5073 *pos--= (char) ('0' + (char) (part3 % 10)); /* Year */
5074 part3 /= 10;
5075 *pos--= (char) ('0' + (char) (part3 % 10));
5076 part3 /= 10;
5077 *pos--= (char) ('0' + (char) (part3 % 10));
5078 part3 /= 10;
5079 *pos= (char) ('0'+ (char) part3);
5080 return MAX_DATETIME_WIDTH;
5081 }
5082
5083
val_str(String * val_buffer,String * val_ptr)5084 String *Field_temporal_with_date::val_str(String *val_buffer, String *val_ptr)
5085 {
5086 ASSERT_COLUMN_MARKED_FOR_READ;
5087 MYSQL_TIME ltime;
5088 val_buffer->alloc(field_length + 1);
5089 val_buffer->set_charset(&my_charset_numeric);
5090 if (get_date_internal(<ime))
5091 {
5092 val_buffer->set_ascii(my_zero_datetime6, field_length);
5093 return val_buffer;
5094 }
5095 make_datetime((DATE_TIME_FORMAT *) 0, <ime, val_buffer, dec);
5096 return val_buffer;
5097 }
5098
5099
5100 type_conversion_status
convert_number_to_TIME(longlong nr,bool unsigned_val,int nanoseconds,MYSQL_TIME * ltime,int * warnings)5101 Field_temporal_with_date::convert_number_to_TIME(longlong nr,
5102 bool unsigned_val,
5103 int nanoseconds,
5104 MYSQL_TIME *ltime,
5105 int *warnings)
5106 {
5107 if (nr < 0 || nanoseconds < 0)
5108 {
5109 reset();
5110 *warnings|= MYSQL_TIME_WARN_OUT_OF_RANGE;
5111 return TYPE_WARN_OUT_OF_RANGE;
5112 }
5113
5114 if (convert_number_to_datetime(nr, unsigned_val, ltime, warnings) == LL(-1))
5115 return TYPE_ERR_BAD_VALUE;
5116
5117 if (ltime->time_type == MYSQL_TIMESTAMP_DATE && nanoseconds)
5118 {
5119 *warnings|= MYSQL_TIME_WARN_TRUNCATED;
5120 return TYPE_NOTE_TRUNCATED;
5121 }
5122
5123 ltime->second_part= 0;
5124 if (datetime_add_nanoseconds_with_round(ltime, nanoseconds, warnings))
5125 {
5126 reset();
5127 return TYPE_WARN_OUT_OF_RANGE;
5128 }
5129 return TYPE_OK;
5130 }
5131
5132
5133 type_conversion_status
store_time(MYSQL_TIME * ltime,uint8 dec_arg MY_ATTRIBUTE ((unused)))5134 Field_temporal_with_date::store_time(MYSQL_TIME *ltime,
5135 uint8 dec_arg MY_ATTRIBUTE((unused)))
5136 {
5137 ASSERT_COLUMN_MARKED_FOR_WRITE;
5138 type_conversion_status error;
5139 int warnings= 0;
5140
5141 switch (ltime->time_type) // TS-TODO: split into separate methods?
5142 {
5143 case MYSQL_TIMESTAMP_DATETIME:
5144 case MYSQL_TIMESTAMP_DATE:
5145 if (check_date(ltime, non_zero_date(ltime), date_flags(), &warnings))
5146 {
5147 DBUG_ASSERT(warnings &
5148 (MYSQL_TIME_WARN_OUT_OF_RANGE |
5149 MYSQL_TIME_WARN_ZERO_DATE |
5150 MYSQL_TIME_WARN_ZERO_IN_DATE));
5151
5152 error= time_warning_to_type_conversion_status(warnings);
5153 reset();
5154 }
5155 else
5156 error= store_internal_with_round(ltime, &warnings);
5157 break;
5158 case MYSQL_TIMESTAMP_TIME:
5159 {
5160 /* Convert TIME to DATETIME */
5161 THD *thd= table ? table->in_use : current_thd;
5162 MYSQL_TIME ltime2;
5163 time_to_datetime(thd, ltime, <ime2);
5164 error= store_internal_with_round(<ime2, &warnings);
5165 break;
5166 }
5167 case MYSQL_TIMESTAMP_NONE:
5168 case MYSQL_TIMESTAMP_ERROR:
5169 default:
5170 warnings|= MYSQL_TIME_WARN_TRUNCATED;
5171 reset();
5172 error= TYPE_WARN_TRUNCATED;
5173 }
5174
5175 if (warnings)
5176 set_warnings(ErrConvString(ltime, decimals()), warnings);
5177 return error;
5178 }
5179
5180
5181 bool
convert_str_to_TIME(const char * str,uint len,const CHARSET_INFO * cs,MYSQL_TIME * ltime,MYSQL_TIME_STATUS * status)5182 Field_temporal_with_date::convert_str_to_TIME(const char *str, uint len,
5183 const CHARSET_INFO *cs,
5184 MYSQL_TIME *ltime,
5185 MYSQL_TIME_STATUS *status)
5186 {
5187 return str_to_datetime(cs, str, len, ltime, date_flags(), status);
5188 }
5189
5190
send_binary(Protocol * protocol)5191 bool Field_temporal_with_date::send_binary(Protocol *protocol)
5192 {
5193 MYSQL_TIME ltime;
5194 if (get_date_internal(<ime))
5195 {
5196 // Only MYSQL_TYPE_TIMESTAMP can return an error in get_date_internal()
5197 DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
5198 set_zero_time(<ime, MYSQL_TIMESTAMP_DATETIME);
5199 }
5200 return protocol->store(<ime, 0);
5201 }
5202
5203
5204 type_conversion_status
store_internal_with_round(MYSQL_TIME * ltime,int * warnings)5205 Field_temporal_with_date::store_internal_with_round(MYSQL_TIME *ltime,
5206 int *warnings)
5207 {
5208 if (my_datetime_round(ltime, dec, warnings))
5209 {
5210 reset();
5211 return time_warning_to_type_conversion_status(*warnings);
5212 }
5213 else
5214 return store_internal(ltime, warnings);
5215 }
5216
5217
5218 /**
5219 Validate date value stored in the field.
5220
5221 Now we check whether date value is zero or has zero in date or not and set
5222 warning/error message appropriately(depending on the sql_mode).
5223 */
validate_stored_val(THD * thd)5224 type_conversion_status Field_temporal_with_date::validate_stored_val(THD *thd)
5225 {
5226 MYSQL_TIME ltime;
5227 type_conversion_status error= TYPE_OK;
5228 int warnings= 0;
5229
5230 if (is_real_null())
5231 return error;
5232
5233 memset(<ime, 0, sizeof(MYSQL_TIME));
5234 get_date_internal(<ime);
5235 if (check_date(<ime, non_zero_date(<ime), date_flags(), &warnings))
5236 error= time_warning_to_type_conversion_status(warnings);
5237
5238 if (warnings)
5239 {
5240 ltime.time_type = field_type_to_timestamp_type(type());
5241 set_warnings(ErrConvString(<ime, dec), warnings);
5242 }
5243
5244 return error;
5245 }
5246
5247
5248 /****************************************************************************
5249 ** Common code for data types with date and time: DATETIME, TIMESTAMP
5250 *****************************************************************************/
5251
5252
store_timestamp(const struct timeval * tm)5253 void Field_temporal_with_date_and_time::store_timestamp(const struct timeval *tm)
5254 {
5255 ASSERT_COLUMN_MARKED_FOR_WRITE;
5256 if (!my_time_fraction_remainder(tm->tv_usec, decimals()))
5257 {
5258 store_timestamp_internal(tm);
5259 return;
5260 }
5261 struct timeval tm2= *tm;
5262 my_timeval_round(&tm2, decimals());
5263 store_timestamp_internal(&tm2);
5264 }
5265
5266
5267 bool
convert_TIME_to_timestamp(THD * thd,const MYSQL_TIME * ltime,struct timeval * tm,int * warnings)5268 Field_temporal_with_date_and_time::convert_TIME_to_timestamp(THD *thd,
5269 const MYSQL_TIME *ltime,
5270 struct timeval *tm,
5271 int *warnings)
5272 {
5273 /*
5274 No needs to do check_date(TIME_NO_ZERO_IN_DATE),
5275 because it has been done earlier in
5276 store_time(), number_to_datetime() or str_to_datetime().
5277 */
5278 if (datetime_with_no_zero_in_date_to_timeval(thd, ltime, tm, warnings))
5279 {
5280 tm->tv_sec= tm->tv_usec= 0;
5281 return true;
5282 }
5283 return false;
5284 }
5285
5286
init_timestamp_flags()5287 void Field_temporal_with_date_and_time::init_timestamp_flags()
5288 {
5289 if (unireg_check != NONE)
5290 {
5291 /*
5292 This TIMESTAMP column is hereby quietly assumed to have an insert or
5293 update default function.
5294 */
5295 flags|= TIMESTAMP_FLAG;
5296 if (unireg_check != TIMESTAMP_DN_FIELD)
5297 flags|= ON_UPDATE_NOW_FLAG;
5298 }
5299 }
5300
5301 /****************************************************************************
5302 ** Common code for DATETIME(N) and TIMESTAMP(N)
5303 *****************************************************************************/
5304
val_real()5305 double Field_temporal_with_date_and_timef::val_real()
5306 {
5307 ASSERT_COLUMN_MARKED_FOR_READ;
5308 MYSQL_TIME ltime;
5309 return get_date_internal(<ime) ? 0 : TIME_to_double_datetime(<ime);
5310 }
5311
5312
val_int()5313 longlong Field_temporal_with_date_and_timef::val_int()
5314 {
5315 ASSERT_COLUMN_MARKED_FOR_READ;
5316 MYSQL_TIME ltime;
5317 return get_date_internal(<ime) ?
5318 0 : TIME_to_ulonglong_datetime_round(<ime);
5319 }
5320
5321
val_decimal(my_decimal * dec)5322 my_decimal *Field_temporal_with_date_and_timef::val_decimal(my_decimal *dec)
5323 {
5324 ASSERT_COLUMN_MARKED_FOR_READ;
5325 MYSQL_TIME ltime;
5326 if (get_date_internal(<ime))
5327 {
5328 // Only MYSQL_TYPE_TIMESTAMP can return an error in get_date_internal()
5329 DBUG_ASSERT(type() == MYSQL_TYPE_TIMESTAMP);
5330 set_zero_time(<ime, MYSQL_TIMESTAMP_DATETIME);
5331 }
5332 return date2my_decimal(<ime, dec);
5333 }
5334
5335
5336 /**
5337 TIMESTAMP type columns hold date and time values in the range 1970-01-01
5338 00:00:01 UTC to 2038-01-01 00:00:00 UTC, stored as number of seconds since
5339 the start of the Unix Epoch (1970-01-01 00:00:01 UTC.)
5340
5341 TIMESTAMP columns can be automatically set on row updates to and/or have
5342 CURRENT_TIMESTAMP as default value for inserts.
5343
5344 The implementation of function defaults is heavily entangled with the binary
5345 .frm file format. The @c utype @c enum is part of the file format
5346 specification but is declared a member of the Field class. This constructor
5347 accepts a unireg_check value to initialize the column default expression.
5348
5349 Five distinct unireg_check values are used for TIMESTAMP columns to
5350 distinguish various cases of DEFAULT or ON UPDATE values. These values are:
5351
5352 - TIMESTAMP_OLD_FIELD - old timestamp, this has no significance when
5353 creating a the Field_timestamp.
5354
5355 - TIMESTAMP_DN_FIELD - means TIMESTAMP DEFAULT CURRENT_TIMESTAMP.
5356
5357 - TIMESTAMP_UN_FIELD - means TIMESTAMP DEFAULT <default value> ON UPDATE
5358 CURRENT_TIMESTAMP, where <default value> is an implicit or explicit
5359 expression other than CURRENT_TIMESTAMP or any synonym thereof
5360 (e.g. NOW().)
5361
5362 - TIMESTAMP_DNUN_FIELD - means DEFAULT CURRENT_TIMESTAMP ON UPDATE
5363 CURRENT_TIMESTAMP.
5364
5365 - NONE - means that the column has neither DEFAULT CURRENT_TIMESTAMP, nor ON
5366 UPDATE CURRENT_TIMESTAMP
5367
5368 Note that columns with TIMESTAMP_OLD_FIELD are no longer created explicitly,
5369 the value is meant to preserve the ability to read tables from old
5370 databases. Such columns are replaced with their newer counterparts by CREATE
5371 TABLE and SHOW CREATE TABLE. This is because we want to prefer NONE
5372 unireg_check over TIMESTAMP_OLD_FIELD for "TIMESTAMP DEFAULT 'Const'"
5373 field. (Old TIMESTAMP columns allowed such definitions as well but ignored
5374 the default value for first the TIMESTAMP column. This is, of course,
5375 non-standard.) In most cases a user won't notice any change, only exception
5376 being different behavior of old/new TIMESTAMPS columns during ALTER TABLE.
5377 */
Field_timestamp(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const char * field_name_arg)5378 Field_timestamp::Field_timestamp(uchar *ptr_arg, uint32 len_arg,
5379 uchar *null_ptr_arg, uchar null_bit_arg,
5380 enum utype unireg_check_arg,
5381 const char *field_name_arg)
5382 :Field_temporal_with_date_and_time(ptr_arg, null_ptr_arg, null_bit_arg,
5383 unireg_check_arg, field_name_arg, 0)
5384 {
5385 init_timestamp_flags();
5386 /* For 4.0 MYD and 4.0 InnoDB compatibility */
5387 flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
5388 }
5389
5390
Field_timestamp(bool maybe_null_arg,const char * field_name_arg)5391 Field_timestamp::Field_timestamp(bool maybe_null_arg,
5392 const char *field_name_arg)
5393 :Field_temporal_with_date_and_time((uchar *) 0,
5394 maybe_null_arg ? (uchar *) "" : 0, 0,
5395 NONE, field_name_arg, 0)
5396 {
5397 init_timestamp_flags();
5398 /* For 4.0 MYD and 4.0 InnoDB compatibility */
5399 flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
5400 }
5401
5402
date_flags(const THD * thd)5403 ulonglong Field_timestamp::date_flags(const THD *thd)
5404 {
5405 /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
5406 return (thd->variables.sql_mode & MODE_NO_ZERO_DATE) | MODE_NO_ZERO_IN_DATE;
5407 }
5408
5409
5410 type_conversion_status
store_internal(const MYSQL_TIME * ltime,int * warnings)5411 Field_timestamp::store_internal(const MYSQL_TIME *ltime, int *warnings)
5412 {
5413 THD *thd= table ? table->in_use : current_thd;
5414 struct timeval tm;
5415 convert_TIME_to_timestamp(thd, ltime, &tm, warnings);
5416 const type_conversion_status error=
5417 time_warning_to_type_conversion_status(*warnings);
5418 store_timestamp_internal(&tm);
5419 return error;
5420 }
5421
5422
5423 /**
5424 Get a value from record, without checking fuzzy date flags.
5425 @retval true - if timestamp is 0, ltime is not touched in this case.
5426 @retval false - if timestamp is non-zero.
5427 */
get_date_internal(MYSQL_TIME * ltime)5428 bool Field_timestamp::get_date_internal(MYSQL_TIME *ltime)
5429 {
5430 ASSERT_COLUMN_MARKED_FOR_READ;
5431 uint32 temp;
5432 THD *thd= table ? table->in_use : current_thd;
5433 #ifdef WORDS_BIGENDIAN
5434 if (table && table->s->db_low_byte_first)
5435 temp= uint4korr(ptr);
5436 else
5437 #endif
5438 longget(temp, ptr);
5439 if (!temp)
5440 return true;
5441 thd->time_zone()->gmt_sec_to_TIME(ltime, (my_time_t) temp);
5442 return false;
5443 }
5444
5445
5446 /**
5447 Get TIMESTAMP field value as seconds since begging of Unix Epoch
5448 */
get_timestamp(struct timeval * tm,int * warnings)5449 bool Field_timestamp::get_timestamp(struct timeval *tm, int *warnings)
5450 {
5451 if (is_null())
5452 return true;
5453 tm->tv_usec= 0;
5454 #ifdef WORDS_BIGENDIAN
5455 if (table && table->s->db_low_byte_first)
5456 {
5457 tm->tv_sec= sint4korr(ptr);
5458 return false;
5459 }
5460 #endif
5461 long tmp;
5462 longget(tmp,ptr);
5463 tm->tv_sec= tmp;
5464 return false;
5465 }
5466
5467
store_timestamp_internal(const struct timeval * tm)5468 void Field_timestamp::store_timestamp_internal(const struct timeval *tm)
5469 {
5470 #ifdef WORDS_BIGENDIAN
5471 if (table && table->s->db_low_byte_first)
5472 {
5473 int4store(ptr, tm->tv_sec);
5474 }
5475 else
5476 #endif
5477 longstore(ptr, (uint32) tm->tv_sec);
5478 }
5479
5480
store_packed(longlong nr)5481 type_conversion_status Field_timestamp::store_packed(longlong nr)
5482 {
5483 /* Make sure the stored value was previously properly rounded or truncated */
5484 DBUG_ASSERT((MY_PACKED_TIME_GET_FRAC_PART(nr) %
5485 (int) log_10_int[DATETIME_MAX_DECIMALS - decimals()]) == 0);
5486 MYSQL_TIME ltime;
5487 TIME_from_longlong_datetime_packed(<ime, nr);
5488 return Field_timestamp::store_time(<ime, 0);
5489 }
5490
5491
val_int()5492 longlong Field_timestamp::val_int()
5493 {
5494 ASSERT_COLUMN_MARKED_FOR_READ;
5495 MYSQL_TIME ltime;
5496 return get_date_internal(<ime) ? 0 : TIME_to_ulonglong_datetime(<ime);
5497 }
5498
5499
get_date(MYSQL_TIME * ltime,uint fuzzydate)5500 bool Field_timestamp::get_date(MYSQL_TIME *ltime, uint fuzzydate)
5501 {
5502 /* Don't do check_fuzzy_date() as month and year are never 0 for timestamp */
5503 return get_internal_check_zero(ltime, fuzzydate);
5504 }
5505
5506
cmp(const uchar * a_ptr,const uchar * b_ptr)5507 int Field_timestamp::cmp(const uchar *a_ptr, const uchar *b_ptr)
5508 {
5509 int32 a,b;
5510 #ifdef WORDS_BIGENDIAN
5511 if (table && table->s->db_low_byte_first)
5512 {
5513 a=sint4korr(a_ptr);
5514 b=sint4korr(b_ptr);
5515 }
5516 else
5517 #endif
5518 {
5519 longget(a,a_ptr);
5520 longget(b,b_ptr);
5521 }
5522 return ((uint32) a < (uint32) b) ? -1 : ((uint32) a > (uint32) b) ? 1 : 0;
5523 }
5524
5525
make_sort_key(uchar * to,uint length MY_ATTRIBUTE ((unused)))5526 void Field_timestamp::make_sort_key(uchar *to,uint length MY_ATTRIBUTE((unused)))
5527 {
5528 #ifdef WORDS_BIGENDIAN
5529 if (!table || !table->s->db_low_byte_first)
5530 {
5531 to[0] = ptr[0];
5532 to[1] = ptr[1];
5533 to[2] = ptr[2];
5534 to[3] = ptr[3];
5535 }
5536 else
5537 #endif
5538 {
5539 to[0] = ptr[3];
5540 to[1] = ptr[2];
5541 to[2] = ptr[1];
5542 to[3] = ptr[0];
5543 }
5544 }
5545
5546
sql_type(String & res) const5547 void Field_timestamp::sql_type(String &res) const
5548 {
5549 res.set_ascii(STRING_WITH_LEN("timestamp"));
5550 }
5551
5552
validate_stored_val(THD * thd)5553 type_conversion_status Field_timestamp::validate_stored_val(THD *thd)
5554 {
5555 /*
5556 While deprecating "TIMESTAMP with implicit DEFAULT value", we can
5557 remove this function implementation and depend directly on
5558 "Field_temporal_with_date::validate_stored_val"
5559 */
5560 if (!thd->variables.explicit_defaults_for_timestamp)
5561 return TYPE_OK;
5562
5563 return (Field_temporal_with_date::validate_stored_val(thd));
5564 }
5565
5566
5567 /****************************************************************************
5568 ** timestamp(N) type
5569 ** In string context: YYYY-MM-DD HH:MM:SS.FFFFFF
5570 ** In number context: YYYYMMDDHHMMSS.FFFFFF
5571 ** Stored as a 7 byte value
5572 ****************************************************************************/
Field_timestampf(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const char * field_name_arg,uint8 dec_arg)5573 Field_timestampf::Field_timestampf(uchar *ptr_arg,
5574 uchar *null_ptr_arg, uchar null_bit_arg,
5575 enum utype unireg_check_arg,
5576 const char *field_name_arg,
5577 uint8 dec_arg)
5578 :Field_temporal_with_date_and_timef(ptr_arg, null_ptr_arg, null_bit_arg,
5579 unireg_check_arg, field_name_arg,
5580 dec_arg)
5581 {
5582 init_timestamp_flags();
5583 }
5584
5585
Field_timestampf(bool maybe_null_arg,const char * field_name_arg,uint8 dec_arg)5586 Field_timestampf::Field_timestampf(bool maybe_null_arg,
5587 const char *field_name_arg,
5588 uint8 dec_arg)
5589 :Field_temporal_with_date_and_timef((uchar*) 0,
5590 maybe_null_arg ? (uchar*) "": 0, 0,
5591 NONE, field_name_arg, dec_arg)
5592 {
5593 if (unireg_check != TIMESTAMP_DN_FIELD)
5594 flags|= ON_UPDATE_NOW_FLAG;
5595 }
5596
5597
date_flags(const THD * thd)5598 ulonglong Field_timestampf::date_flags(const THD *thd)
5599 {
5600 /* We don't want to store invalid or fuzzy datetime values in TIMESTAMP */
5601 return (thd->variables.sql_mode & MODE_NO_ZERO_DATE) | MODE_NO_ZERO_IN_DATE;
5602 }
5603
5604
reset()5605 type_conversion_status Field_timestampf::reset()
5606 {
5607 memset(ptr, 0, pack_length());
5608 return TYPE_OK;
5609 }
5610
5611
store_timestamp_internal(const struct timeval * tm)5612 void Field_timestampf::store_timestamp_internal(const struct timeval *tm)
5613 {
5614 my_timestamp_to_binary(tm, ptr, dec);
5615 }
5616
5617
5618 type_conversion_status
store_internal(const MYSQL_TIME * ltime,int * warnings)5619 Field_timestampf::store_internal(const MYSQL_TIME *ltime, int *warnings)
5620 {
5621 THD *thd= table ? table->in_use : current_thd;
5622 struct timeval tm;
5623 convert_TIME_to_timestamp(thd, ltime, &tm, warnings);
5624 const type_conversion_status error=
5625 time_warning_to_type_conversion_status(*warnings);
5626 store_timestamp_internal(&tm);
5627 return error;
5628 }
5629
5630
store_packed(longlong nr)5631 type_conversion_status Field_timestampf::store_packed(longlong nr)
5632 {
5633 MYSQL_TIME ltime;
5634 TIME_from_longlong_datetime_packed(<ime, nr);
5635 return Field_timestampf::store_time(<ime, dec);
5636 }
5637
5638
get_date(MYSQL_TIME * ltime,uint fuzzydate)5639 bool Field_timestampf::get_date(MYSQL_TIME *ltime, uint fuzzydate)
5640 {
5641 /* Don't do check_fuzzy_date() as month and year are never 0 for timestamp */
5642 return get_internal_check_zero(ltime, fuzzydate);
5643 }
5644
5645
sql_type(String & res) const5646 void Field_timestampf::sql_type(String &res) const
5647 {
5648 if (dec == 0)
5649 {
5650 res.set_ascii(STRING_WITH_LEN("timestamp"));
5651 return;
5652 }
5653 const CHARSET_INFO *cs= res.charset();
5654 res.length(cs->cset->snprintf(cs, (char *) res.ptr(), res.alloced_length(),
5655 "timestamp(%d)", dec));
5656 }
5657
5658
5659 bool
get_date_internal(MYSQL_TIME * ltime)5660 Field_timestampf::get_date_internal(MYSQL_TIME *ltime)
5661 {
5662 THD *thd= table ? table->in_use : current_thd;
5663 struct timeval tm;
5664 my_timestamp_from_binary(&tm, ptr, dec);
5665 if (tm.tv_sec == 0)
5666 return true;
5667 thd->time_zone()->gmt_sec_to_TIME(ltime, tm);
5668 return false;
5669 }
5670
5671
get_timestamp(struct timeval * tm,int * warnings)5672 bool Field_timestampf::get_timestamp(struct timeval *tm, int *warnings)
5673 {
5674 THD *thd= table ? table->in_use : current_thd;
5675 thd->time_zone_used= 1;
5676 DBUG_ASSERT(!is_null());
5677 my_timestamp_from_binary(tm, ptr, dec);
5678 return false;
5679 }
5680
5681
validate_stored_val(THD * thd)5682 type_conversion_status Field_timestampf::validate_stored_val(THD *thd)
5683 {
5684 /*
5685 While deprecating "TIMESTAMP with implicit DEFAULT value", we can
5686 remove this function implementation and depend directly on
5687 "Field_temporal_with_date::validate_stored_val"
5688 */
5689 if (!thd->variables.explicit_defaults_for_timestamp)
5690 return TYPE_OK;
5691
5692 return (Field_temporal_with_date::validate_stored_val(thd));
5693 }
5694
5695
5696 /****************************************************************************
5697 ** TIME and TIME(N) common methods
5698 ****************************************************************************/
5699
5700
5701 bool
convert_str_to_TIME(const char * str,uint len,const CHARSET_INFO * cs,MYSQL_TIME * ltime,MYSQL_TIME_STATUS * status)5702 Field_time_common::convert_str_to_TIME(const char *str, uint len,
5703 const CHARSET_INFO *cs,
5704 MYSQL_TIME *ltime,
5705 MYSQL_TIME_STATUS *status)
5706 {
5707 return str_to_time(cs, str, len, ltime, 0, status);
5708 }
5709
5710
5711 type_conversion_status
convert_number_to_TIME(longlong nr,bool unsigned_val,int nanoseconds,MYSQL_TIME * ltime,int * warnings)5712 Field_time_common::convert_number_to_TIME(longlong nr, bool unsigned_val,
5713 int nanoseconds,
5714 MYSQL_TIME *ltime, int *warnings)
5715 {
5716 if (unsigned_val && nr < 0)
5717 {
5718 *warnings|= MYSQL_TIME_WARN_OUT_OF_RANGE;
5719 set_max_time(ltime, 0);
5720 store_internal(ltime, warnings);
5721 return TYPE_WARN_OUT_OF_RANGE;
5722 }
5723 if (number_to_time(nr, ltime, warnings))
5724 {
5725 store_internal(ltime, warnings);
5726 return TYPE_WARN_OUT_OF_RANGE;
5727 }
5728 /*
5729 Both number_to_time() call and negative nanoseconds value
5730 affect ltime->neg, hence "|=" to combine them:
5731 */
5732 if ((ltime->neg|= (nanoseconds < 0)))
5733 nanoseconds= -nanoseconds;
5734 ltime->second_part= 0;
5735 bool round_error= time_add_nanoseconds_with_round(ltime, nanoseconds,
5736 warnings);
5737
5738 return round_error ? time_warning_to_type_conversion_status(*warnings)
5739 : TYPE_OK;
5740 }
5741
5742
5743 type_conversion_status
store_time(MYSQL_TIME * ltime,uint8 dec_arg MY_ATTRIBUTE ((unused)))5744 Field_time_common::store_time(MYSQL_TIME *ltime,
5745 uint8 dec_arg MY_ATTRIBUTE((unused)))
5746 {
5747 /* Check if seconds or minutes are out of range */
5748 if (ltime->second >= 60 || ltime->minute >= 60)
5749 {
5750 set_warnings(ErrConvString(ltime, decimals()),
5751 MYSQL_TIME_WARN_OUT_OF_RANGE);
5752 reset();
5753 return TYPE_WARN_OUT_OF_RANGE;
5754 }
5755 int warnings= 0;
5756 return store_internal_with_round(ltime, &warnings);
5757 }
5758
5759
5760 type_conversion_status
store_internal_with_round(MYSQL_TIME * ltime,int * warnings)5761 Field_time_common::store_internal_with_round(MYSQL_TIME *ltime, int *warnings)
5762 {
5763 if (my_time_round(ltime, dec))
5764 return TYPE_WARN_OUT_OF_RANGE;
5765
5766 return store_internal(ltime, warnings);
5767 }
5768
5769
val_str(String * val_buffer,String * val_ptr MY_ATTRIBUTE ((unused)))5770 String *Field_time_common::val_str(String *val_buffer,
5771 String *val_ptr MY_ATTRIBUTE((unused)))
5772 {
5773 ASSERT_COLUMN_MARKED_FOR_READ;
5774 MYSQL_TIME ltime;
5775 val_buffer->alloc(MAX_DATE_STRING_REP_LENGTH);
5776 val_buffer->set_charset(&my_charset_numeric);
5777 if (get_time(<ime))
5778 {
5779 DBUG_ASSERT(0);
5780 set_zero_time(<ime, MYSQL_TIMESTAMP_TIME);
5781 }
5782 make_time((DATE_TIME_FORMAT *) 0, <ime, val_buffer, dec);
5783 return val_buffer;
5784 }
5785
5786
5787 /**
5788 For a column for TIME type, get_date() takes the time
5789 value of the field, adds current date to it and returns
5790 the result as a DATETIME value.
5791 */
5792
get_date(MYSQL_TIME * ltime,uint fuzzydate)5793 bool Field_time_common::get_date(MYSQL_TIME *ltime, uint fuzzydate)
5794 {
5795 ASSERT_COLUMN_MARKED_FOR_READ;
5796 MYSQL_TIME tm;
5797 if (get_time(&tm))
5798 {
5799 DBUG_ASSERT(0);
5800 set_zero_time(ltime, MYSQL_TIMESTAMP_TIME);
5801 }
5802 time_to_datetime(table ? table->in_use : current_thd, &tm, ltime);
5803 return false;
5804 }
5805
5806
val_date_temporal()5807 longlong Field_time_common::val_date_temporal()
5808 {
5809 ASSERT_COLUMN_MARKED_FOR_READ;
5810 MYSQL_TIME time, datetime;
5811 if (get_time(&time))
5812 {
5813 DBUG_ASSERT(0); // Field_time*::get_time should not fail
5814 return 0;
5815 }
5816 time_to_datetime(table ? table->in_use : current_thd, &time, &datetime);
5817 return TIME_to_longlong_datetime_packed(&datetime);
5818 }
5819
5820
send_binary(Protocol * protocol)5821 bool Field_time_common::send_binary(Protocol *protocol)
5822 {
5823 MYSQL_TIME ltime;
5824 if (get_time(<ime))
5825 {
5826 DBUG_ASSERT(0);
5827 set_zero_time(<ime, MYSQL_TIMESTAMP_TIME);
5828 }
5829 ltime.day= ltime.hour / 24; // Move hours to days
5830 ltime.hour-= ltime.day * 24;
5831 return protocol->store_time(<ime, 0);
5832 }
5833
5834
5835 /****************************************************************************
5836 ** time type
5837 ** In string context: HH:MM:SS
5838 ** In number context: HHMMSS
5839 ** Stored as a 3 byte unsigned int
5840 ****************************************************************************/
5841
5842 type_conversion_status
store_internal(const MYSQL_TIME * ltime,int * warnings)5843 Field_time::store_internal(const MYSQL_TIME *ltime, int *warnings)
5844 {
5845 long tmp= ((ltime->month ? 0 : ltime->day * 24L) + ltime->hour) * 10000L +
5846 (ltime->minute * 100 + ltime->second);
5847 if (ltime->neg)
5848 tmp= -tmp;
5849 int3store(ptr, tmp);
5850 return TYPE_OK;
5851 }
5852
5853
store_packed(longlong nr)5854 type_conversion_status Field_time::store_packed(longlong nr)
5855 {
5856 MYSQL_TIME ltime;
5857 TIME_from_longlong_time_packed(<ime, nr);
5858 return Field_time::store_time(<ime, 0);
5859 }
5860
5861
val_time_temporal()5862 longlong Field_time::val_time_temporal()
5863 {
5864 ASSERT_COLUMN_MARKED_FOR_READ;
5865 MYSQL_TIME ltime;
5866 return get_time(<ime) ? 0 : TIME_to_longlong_time_packed(<ime);
5867 }
5868
5869
val_int()5870 longlong Field_time::val_int()
5871 {
5872 ASSERT_COLUMN_MARKED_FOR_READ;
5873 return (longlong) sint3korr(ptr);
5874 }
5875
5876
get_time(MYSQL_TIME * ltime)5877 bool Field_time::get_time(MYSQL_TIME *ltime)
5878 {
5879 long tmp=(long) sint3korr(ptr);
5880 if ((ltime->neg= tmp < 0))
5881 tmp= -tmp;
5882 ltime->year= ltime->month= ltime->day= 0;
5883 TIME_set_hhmmss(ltime, tmp);
5884 ltime->second_part=0;
5885 ltime->time_type= MYSQL_TIMESTAMP_TIME;
5886 return false;
5887 }
5888
5889
cmp(const uchar * a_ptr,const uchar * b_ptr)5890 int Field_time::cmp(const uchar *a_ptr, const uchar *b_ptr)
5891 {
5892 int32 a,b;
5893 a=(int32) sint3korr(a_ptr);
5894 b=(int32) sint3korr(b_ptr);
5895 return (a < b) ? -1 : (a > b) ? 1 : 0;
5896 }
5897
5898
make_sort_key(uchar * to,uint length)5899 void Field_time::make_sort_key(uchar *to, uint length)
5900 {
5901 DBUG_ASSERT(length >= 3);
5902 to[0] = (uchar) (ptr[2] ^ 128);
5903 to[1] = ptr[1];
5904 to[2] = ptr[0];
5905 }
5906
5907
sql_type(String & res) const5908 void Field_time::sql_type(String &res) const
5909 {
5910 res.set_ascii(STRING_WITH_LEN("time"));
5911 }
5912
5913
5914 /****************************************************************************
5915 ** time type with fsp
5916 ** In string context: HH:MM:SS.FFFFFF
5917 ** In number context: HHMMSS.FFFFFF
5918 ****************************************************************************/
5919
5920
val_int()5921 longlong Field_timef::val_int()
5922 {
5923 ASSERT_COLUMN_MARKED_FOR_READ;
5924 MYSQL_TIME ltime;
5925 if (get_time(<ime))
5926 {
5927 DBUG_ASSERT(0);
5928 set_zero_time(<ime, MYSQL_TIMESTAMP_TIME);
5929 }
5930 longlong tmp= (longlong) TIME_to_ulonglong_time_round(<ime);
5931 return ltime.neg ? -tmp : tmp;
5932 }
5933
5934
val_decimal(my_decimal * decimal_value)5935 my_decimal *Field_timef::val_decimal(my_decimal *decimal_value)
5936 {
5937 ASSERT_COLUMN_MARKED_FOR_READ;
5938 MYSQL_TIME ltime;
5939 if (get_time(<ime))
5940 {
5941 DBUG_ASSERT(0);
5942 set_zero_time(<ime, MYSQL_TIMESTAMP_TIME);
5943 }
5944 return time2my_decimal(<ime, decimal_value);
5945 }
5946
5947
val_real()5948 double Field_timef::val_real()
5949 {
5950 ASSERT_COLUMN_MARKED_FOR_READ;
5951 MYSQL_TIME ltime;
5952 if (get_time(<ime))
5953 {
5954 DBUG_ASSERT(0);
5955 return 0;
5956 }
5957 double tmp= TIME_to_double_time(<ime);
5958 return ltime.neg ? -tmp : tmp;
5959 }
5960
5961
sql_type(String & res) const5962 void Field_timef::sql_type(String &res) const
5963 {
5964 if (dec == 0)
5965 {
5966 res.set_ascii(STRING_WITH_LEN("time"));
5967 return;
5968 }
5969 const CHARSET_INFO *cs= res.charset();
5970 res.length(cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
5971 "time(%d)", dec));
5972 }
5973
5974
reset()5975 type_conversion_status Field_timef::reset()
5976 {
5977 return store_packed(0);
5978 }
5979
5980
store_packed(longlong nr)5981 type_conversion_status Field_timef::store_packed(longlong nr)
5982 {
5983 my_time_packed_to_binary(nr, ptr, dec);
5984 return TYPE_OK;
5985 }
5986
5987
val_time_temporal()5988 longlong Field_timef::val_time_temporal()
5989 {
5990 ASSERT_COLUMN_MARKED_FOR_READ;
5991 return my_time_packed_from_binary(ptr, dec);
5992 }
5993
5994
5995 type_conversion_status
store_internal(const MYSQL_TIME * ltime,int * warnings)5996 Field_timef::store_internal(const MYSQL_TIME *ltime, int *warnings)
5997 {
5998 type_conversion_status rc= store_packed(TIME_to_longlong_time_packed(ltime));
5999 if (rc == TYPE_OK && non_zero_date(ltime))
6000 {
6001 /*
6002 The DATE part got lost; we warn, like in Field_newdate::store_internal,
6003 and trigger some code in get_mm_leaf()
6004 (see err==TYPE_NOTE_TIME_TRUNCATED there).
6005 */
6006 *warnings|= MYSQL_TIME_NOTE_TRUNCATED;
6007 rc= TYPE_NOTE_TIME_TRUNCATED;
6008 }
6009 return rc;
6010 }
6011
6012
get_time(MYSQL_TIME * ltime)6013 bool Field_timef::get_time(MYSQL_TIME *ltime)
6014 {
6015 longlong tmp= val_time_temporal();
6016 TIME_from_longlong_time_packed(ltime, tmp);
6017 return false;
6018 }
6019
6020
6021 /****************************************************************************
6022 ** year type
6023 ** Save in a byte the year 0, 1901->2155
6024 ** Can handle 2 byte or 4 byte years!
6025 ****************************************************************************/
6026
6027 type_conversion_status
store(const char * from,uint len,const CHARSET_INFO * cs)6028 Field_year::store(const char *from, uint len,const CHARSET_INFO *cs)
6029 {
6030 ASSERT_COLUMN_MARKED_FOR_WRITE;
6031 char *end;
6032 int conv_error;
6033 type_conversion_status ret= TYPE_OK;
6034 longlong nr= cs->cset->strntoull10rnd(cs, from, len, 0, &end, &conv_error);
6035
6036 if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155 ||
6037 conv_error == MY_ERRNO_ERANGE)
6038 {
6039 *ptr=0;
6040 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
6041 return TYPE_WARN_OUT_OF_RANGE;
6042 }
6043 else if (conv_error)
6044 ret= TYPE_ERR_BAD_VALUE;
6045
6046 if (table->in_use->count_cuted_fields)
6047 {
6048 ret= check_int(cs, from, len, end, conv_error);
6049 if (ret != TYPE_OK)
6050 {
6051 if (ret == TYPE_ERR_BAD_VALUE) /* empty or incorrect string */
6052 {
6053 *ptr= 0;
6054 return TYPE_WARN_OUT_OF_RANGE;
6055 }
6056 ret= TYPE_WARN_OUT_OF_RANGE;
6057 }
6058 }
6059
6060 if (nr != 0 || len != 4)
6061 {
6062 if (nr < YY_PART_YEAR)
6063 nr+=100; // 2000 - 2069
6064 else if (nr > 1900)
6065 nr-= 1900;
6066 }
6067 *ptr= (char) (uchar) nr;
6068 return ret;
6069 }
6070
6071
store(double nr)6072 type_conversion_status Field_year::store(double nr)
6073 {
6074 if (nr < 0.0 || nr >= 2155.0)
6075 {
6076 (void) Field_year::store((longlong) -1, FALSE);
6077 return TYPE_WARN_OUT_OF_RANGE;
6078 }
6079 return Field_year::store((longlong) nr, FALSE);
6080 }
6081
6082
6083 type_conversion_status
store_time(MYSQL_TIME * ltime,uint8 dec_arg MY_ATTRIBUTE ((unused)))6084 Field_year::store_time(MYSQL_TIME *ltime,
6085 uint8 dec_arg MY_ATTRIBUTE((unused)))
6086 {
6087 if (ltime->time_type != MYSQL_TIMESTAMP_DATETIME &&
6088 ltime->time_type != MYSQL_TIMESTAMP_DATE)
6089 {
6090 /* Convert time to datetime, then store year of the result */
6091 THD *thd= table ? table->in_use : current_thd;
6092 MYSQL_TIME ltime2;
6093 time_to_datetime(thd, ltime, <ime2);
6094 return store(ltime2.year, 0);
6095 }
6096 return store(ltime->year, 0);
6097 }
6098
6099
store(longlong nr,bool unsigned_val)6100 type_conversion_status Field_year::store(longlong nr, bool unsigned_val)
6101 {
6102 ASSERT_COLUMN_MARKED_FOR_WRITE;
6103 if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155)
6104 {
6105 *ptr= 0;
6106 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
6107 return TYPE_WARN_OUT_OF_RANGE;
6108 }
6109 if (nr != 0 || field_length != 4) // 0000 -> 0; 00 -> 2000
6110 {
6111 if (nr < YY_PART_YEAR)
6112 nr+=100; // 2000 - 2069
6113 else if (nr > 1900)
6114 nr-= 1900;
6115 }
6116 *ptr= (char) (uchar) nr;
6117 return TYPE_OK;
6118 }
6119
6120
send_binary(Protocol * protocol)6121 bool Field_year::send_binary(Protocol *protocol)
6122 {
6123 ASSERT_COLUMN_MARKED_FOR_READ;
6124 ulonglong tmp= Field_year::val_int();
6125 return protocol->store_short(tmp);
6126 }
6127
6128
val_real(void)6129 double Field_year::val_real(void)
6130 {
6131 return (double) Field_year::val_int();
6132 }
6133
6134
val_int(void)6135 longlong Field_year::val_int(void)
6136 {
6137 ASSERT_COLUMN_MARKED_FOR_READ;
6138 DBUG_ASSERT(field_length == 2 || field_length == 4);
6139 int tmp= (int) ptr[0];
6140 if (field_length != 4)
6141 tmp%=100; // Return last 2 char
6142 else if (tmp)
6143 tmp+=1900;
6144 return (longlong) tmp;
6145 }
6146
6147
val_str(String * val_buffer,String * val_ptr MY_ATTRIBUTE ((unused)))6148 String *Field_year::val_str(String *val_buffer,
6149 String *val_ptr MY_ATTRIBUTE((unused)))
6150 {
6151 DBUG_ASSERT(field_length < 5);
6152 val_buffer->alloc(5);
6153 val_buffer->length(field_length);
6154 char *to=(char*) val_buffer->ptr();
6155 sprintf(to,field_length == 2 ? "%02d" : "%04d",(int) Field_year::val_int());
6156 val_buffer->set_charset(&my_charset_numeric);
6157 return val_buffer;
6158 }
6159
6160
sql_type(String & res) const6161 void Field_year::sql_type(String &res) const
6162 {
6163 const CHARSET_INFO *cs=res.charset();
6164 res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(),
6165 "year(%d)",(int) field_length));
6166 }
6167
6168
6169 /****************************************************************************
6170 ** The new date type
6171 ** Stored as 3 bytes
6172 ** In number context: YYYYMMDD
6173 ****************************************************************************/
6174
date_flags(const THD * thd)6175 ulonglong Field_newdate::date_flags(const THD *thd)
6176 {
6177 return TIME_FUZZY_DATE | thd->datetime_flags();
6178 }
6179
6180
6181 type_conversion_status
store_internal(const MYSQL_TIME * ltime,int * warnings)6182 Field_newdate::store_internal(const MYSQL_TIME *ltime, int *warnings)
6183 {
6184 long tmp= ltime->day + ltime->month * 32 + ltime->year * 16 * 32;
6185 int3store(ptr, tmp);
6186 if (non_zero_time(ltime))
6187 {
6188 *warnings|= MYSQL_TIME_NOTE_TRUNCATED;
6189 return TYPE_NOTE_TIME_TRUNCATED;
6190 }
6191 return TYPE_OK;
6192 }
6193
6194
get_date_internal(MYSQL_TIME * ltime)6195 bool Field_newdate::get_date_internal(MYSQL_TIME *ltime)
6196 {
6197 uint32 tmp= (uint32) uint3korr(ptr);
6198 ltime->day= tmp & 31;
6199 ltime->month= (tmp >> 5) & 15;
6200 ltime->year= (tmp >> 9);
6201 ltime->time_type= MYSQL_TIMESTAMP_DATE;
6202 ltime->hour= ltime->minute= ltime->second= ltime->second_part= ltime->neg= 0;
6203 return false;
6204 }
6205
6206
store_packed(longlong nr)6207 type_conversion_status Field_newdate::store_packed(longlong nr)
6208 {
6209 int warnings= 0;
6210 MYSQL_TIME ltime;
6211 TIME_from_longlong_date_packed(<ime, nr);
6212 return store_internal(<ime, &warnings);
6213 }
6214
6215
send_binary(Protocol * protocol)6216 bool Field_newdate::send_binary(Protocol *protocol)
6217 {
6218 MYSQL_TIME ltime;
6219 get_date(<ime, 0);
6220 return protocol->store_date(<ime);
6221 }
6222
6223
val_int()6224 longlong Field_newdate::val_int()
6225 {
6226 ASSERT_COLUMN_MARKED_FOR_READ;
6227 ulong j= uint3korr(ptr);
6228 j= (j % 32L)+(j / 32L % 16L)*100L + (j/(16L*32L))*10000L;
6229 return (longlong) j;
6230 }
6231
6232
val_date_temporal()6233 longlong Field_newdate::val_date_temporal()
6234 {
6235 ASSERT_COLUMN_MARKED_FOR_READ;
6236 MYSQL_TIME ltime;
6237 return get_date_internal(<ime) ? 0 : TIME_to_longlong_date_packed(<ime);
6238 }
6239
6240
val_time_temporal()6241 longlong Field_newdate::val_time_temporal()
6242 {
6243 ASSERT_COLUMN_MARKED_FOR_READ;
6244 return 0;
6245 }
6246
6247
val_str(String * val_buffer,String * val_ptr MY_ATTRIBUTE ((unused)))6248 String *Field_newdate::val_str(String *val_buffer,
6249 String *val_ptr MY_ATTRIBUTE((unused)))
6250 {
6251 ASSERT_COLUMN_MARKED_FOR_READ;
6252 val_buffer->alloc(field_length);
6253 val_buffer->length(field_length);
6254 uint32 tmp=(uint32) uint3korr(ptr);
6255 int part;
6256 char *pos=(char*) val_buffer->ptr()+10;
6257
6258 /* Open coded to get more speed */
6259 *pos--=0; // End NULL
6260 part=(int) (tmp & 31);
6261 *pos--= (char) ('0'+part%10);
6262 *pos--= (char) ('0'+part/10);
6263 *pos--= '-';
6264 part=(int) (tmp >> 5 & 15);
6265 *pos--= (char) ('0'+part%10);
6266 *pos--= (char) ('0'+part/10);
6267 *pos--= '-';
6268 part=(int) (tmp >> 9);
6269 *pos--= (char) ('0'+part%10); part/=10;
6270 *pos--= (char) ('0'+part%10); part/=10;
6271 *pos--= (char) ('0'+part%10); part/=10;
6272 *pos= (char) ('0'+part);
6273 val_buffer->set_charset(&my_charset_numeric);
6274 return val_buffer;
6275 }
6276
6277
get_date(MYSQL_TIME * ltime,uint fuzzydate)6278 bool Field_newdate::get_date(MYSQL_TIME *ltime,uint fuzzydate)
6279 {
6280 return get_internal_check_zero(ltime, fuzzydate) ||
6281 check_fuzzy_date(ltime, fuzzydate);
6282 }
6283
6284
cmp(const uchar * a_ptr,const uchar * b_ptr)6285 int Field_newdate::cmp(const uchar *a_ptr, const uchar *b_ptr)
6286 {
6287 uint32 a,b;
6288 a=(uint32) uint3korr(a_ptr);
6289 b=(uint32) uint3korr(b_ptr);
6290 return (a < b) ? -1 : (a > b) ? 1 : 0;
6291 }
6292
6293
make_sort_key(uchar * to,uint length)6294 void Field_newdate::make_sort_key(uchar *to, uint length)
6295 {
6296 DBUG_ASSERT(length >= 3);
6297 to[0] = ptr[2];
6298 to[1] = ptr[1];
6299 to[2] = ptr[0];
6300 }
6301
6302
sql_type(String & res) const6303 void Field_newdate::sql_type(String &res) const
6304 {
6305 res.set_ascii(STRING_WITH_LEN("date"));
6306 }
6307
6308
6309 /****************************************************************************
6310 ** datetime type
6311 ** In string context: YYYY-MM-DD HH:MM:DD
6312 ** In number context: YYYYMMDDHHMMDD
6313 ** Stored as a 8 byte unsigned int. Should sometimes be change to a 6 byte int.
6314 ****************************************************************************/
6315
6316
date_flags(const THD * thd)6317 ulonglong Field_datetime::date_flags(const THD *thd)
6318 {
6319 return TIME_FUZZY_DATE | thd->datetime_flags();
6320 }
6321
6322
store_timestamp_internal(const timeval * tm)6323 void Field_datetime::store_timestamp_internal(const timeval *tm)
6324 {
6325 MYSQL_TIME mysql_time;
6326 THD *thd= current_thd;
6327 thd->variables.time_zone->gmt_sec_to_TIME(&mysql_time, *tm);
6328 thd->time_zone_used= true;
6329 int error= 0;
6330 store_internal(&mysql_time, &error);
6331 }
6332
6333
6334 /**
6335 Store a DATETIME in a 8-byte integer to record.
6336
6337 @param table Table
6338 @param tmp The number, in YYYYMMDDhhmmss format
6339 @param ptr Where to store to
6340 */
6341 static inline type_conversion_status
datetime_store_internal(TABLE * table,ulonglong tmp,uchar * ptr)6342 datetime_store_internal(TABLE *table, ulonglong tmp, uchar *ptr)
6343 {
6344 #ifdef WORDS_BIGENDIAN
6345 if (table && table->s->db_low_byte_first)
6346 {
6347 int8store(ptr, tmp);
6348 }
6349 else
6350 #endif
6351 longlongstore(ptr, tmp);
6352 return TYPE_OK;
6353 }
6354
6355
6356 /**
6357 Read a DATETIME from record to a 8-byte integer
6358
6359 @param table Table
6360 @param ptr Where to read from
6361 @retval An integer in format YYYYMMDDhhmmss
6362 */
6363 static inline longlong
datetime_get_internal(TABLE * table,uchar * ptr)6364 datetime_get_internal(TABLE *table, uchar *ptr)
6365 {
6366 longlong tmp;
6367 #ifdef WORDS_BIGENDIAN
6368 if (table && table->s->db_low_byte_first)
6369 tmp= sint8korr(ptr);
6370 else
6371 #endif
6372 longlongget(tmp, ptr);
6373 return tmp;
6374 }
6375
6376
get_date_internal(MYSQL_TIME * ltime)6377 bool Field_datetime::get_date_internal(MYSQL_TIME *ltime)
6378 {
6379 longlong tmp= datetime_get_internal(table, ptr);
6380 ltime->time_type= MYSQL_TIMESTAMP_DATETIME;
6381 ltime->neg= 0;
6382 ltime->second_part= 0;
6383 TIME_set_yymmdd(ltime, (uint) (tmp / 1000000LL));
6384 TIME_set_hhmmss(ltime, (uint) (tmp % 1000000LL));
6385 return false;
6386 }
6387
6388
6389 type_conversion_status
store_internal(const MYSQL_TIME * ltime,int * warnings)6390 Field_datetime::store_internal(const MYSQL_TIME *ltime, int *warnings)
6391 {
6392 ulonglong tmp= TIME_to_ulonglong_datetime(ltime);
6393 return datetime_store_internal(table, tmp, ptr);
6394 }
6395
6396
store(longlong nr,bool unsigned_val)6397 type_conversion_status Field_datetime::store(longlong nr, bool unsigned_val)
6398 {
6399 ASSERT_COLUMN_MARKED_FOR_WRITE;
6400 MYSQL_TIME ltime;
6401 int warnings;
6402 type_conversion_status error= TYPE_OK;
6403 longlong tmp= convert_number_to_datetime(nr, unsigned_val,
6404 <ime, &warnings);
6405 if (tmp == LL(-1))
6406 error= TYPE_ERR_BAD_VALUE;
6407 else
6408 {
6409 error= time_warning_to_type_conversion_status(warnings);
6410 datetime_store_internal(table, tmp, ptr);
6411 }
6412 if (warnings)
6413 set_warnings(ErrConvString(nr, unsigned_val), warnings);
6414 return error;
6415 }
6416
6417
store_packed(longlong nr)6418 type_conversion_status Field_datetime::store_packed(longlong nr)
6419 {
6420 MYSQL_TIME ltime;
6421 TIME_from_longlong_datetime_packed(<ime, nr);
6422 return Field_datetime::store_time(<ime, 0);
6423 }
6424
6425
val_int()6426 longlong Field_datetime::val_int()
6427 {
6428 ASSERT_COLUMN_MARKED_FOR_READ;
6429 return datetime_get_internal(table, ptr);
6430 }
6431
6432
6433 /*
6434 We don't reuse the parent method for performance purposes,
6435 to avoid convertion from number to MYSQL_TIME.
6436 Using my_datetime_number_to_str() instead of my_datetime_to_str().
6437 */
val_str(String * val_buffer,String * val_ptr MY_ATTRIBUTE ((unused)))6438 String *Field_datetime::val_str(String *val_buffer,
6439 String *val_ptr MY_ATTRIBUTE((unused)))
6440 {
6441 ASSERT_COLUMN_MARKED_FOR_READ;
6442 val_buffer->alloc(field_length + 1);
6443 val_buffer->set_charset(&my_charset_numeric);
6444 val_buffer->length(MAX_DATETIME_WIDTH);
6445 longlong tmp= datetime_get_internal(table, ptr);
6446 val_buffer->length(my_datetime_number_to_str((char *) val_buffer->ptr(),
6447 tmp));
6448 return val_buffer;
6449 }
6450
6451
get_date(MYSQL_TIME * ltime,uint fuzzydate)6452 bool Field_datetime::get_date(MYSQL_TIME *ltime, uint fuzzydate)
6453 {
6454 return get_internal_check_zero(ltime, fuzzydate) ||
6455 check_fuzzy_date(ltime, fuzzydate);
6456 }
6457
6458
cmp(const uchar * a_ptr,const uchar * b_ptr)6459 int Field_datetime::cmp(const uchar *a_ptr, const uchar *b_ptr)
6460 {
6461 longlong a,b;
6462 #ifdef WORDS_BIGENDIAN
6463 if (table && table->s->db_low_byte_first)
6464 {
6465 a=sint8korr(a_ptr);
6466 b=sint8korr(b_ptr);
6467 }
6468 else
6469 #endif
6470 {
6471 longlongget(a,a_ptr);
6472 longlongget(b,b_ptr);
6473 }
6474 return ((ulonglong) a < (ulonglong) b) ? -1 :
6475 ((ulonglong) a > (ulonglong) b) ? 1 : 0;
6476 }
6477
make_sort_key(uchar * to,uint length)6478 void Field_datetime::make_sort_key(uchar *to, uint length)
6479 {
6480 const int pack_length= PACK_LENGTH;
6481 const int to_length= min<uint>(pack_length, length);
6482 #ifdef WORDS_BIGENDIAN
6483 if (!table || !table->s->db_low_byte_first)
6484 copy_integer<true>(to, to_length, ptr, pack_length, true);
6485 else
6486 #endif
6487 copy_integer<false>(to, to_length, ptr, pack_length, true);
6488 }
6489
6490
sql_type(String & res) const6491 void Field_datetime::sql_type(String &res) const
6492 {
6493 res.set_ascii(STRING_WITH_LEN("datetime"));
6494 }
6495
6496 /****************************************************************************
6497 ** datetimef type
6498 ** In string context: YYYY-MM-DD HH:MM:DD.FFFFFF
6499 ** In number context: YYYYMMDDHHMMDD.FFFFFF
6500 ** Stored as a 8 byte value.
6501 ****************************************************************************/
6502
6503
date_flags(const THD * thd)6504 ulonglong Field_datetimef::date_flags(const THD *thd)
6505 {
6506 return TIME_FUZZY_DATE | thd->datetime_flags();
6507 }
6508
6509
store_timestamp_internal(const timeval * tm)6510 void Field_datetimef::store_timestamp_internal(const timeval *tm)
6511 {
6512 MYSQL_TIME mysql_time;
6513 THD *thd= current_thd;
6514 thd->variables.time_zone->gmt_sec_to_TIME(&mysql_time, *tm);
6515 thd->time_zone_used= true;
6516 int warnings= 0;
6517 store_internal(&mysql_time, &warnings);
6518 }
6519
6520
6521
get_date(MYSQL_TIME * ltime,uint fuzzydate)6522 bool Field_datetimef::get_date(MYSQL_TIME *ltime, uint fuzzydate)
6523 {
6524 return get_internal_check_zero(ltime, fuzzydate) ||
6525 check_fuzzy_date(ltime, fuzzydate);
6526 }
6527
6528
sql_type(String & res) const6529 void Field_datetimef::sql_type(String &res) const
6530 {
6531 if (dec == 0)
6532 {
6533 res.set_ascii(STRING_WITH_LEN("datetime"));
6534 return;
6535 }
6536 const CHARSET_INFO *cs= res.charset();
6537 res.length(cs->cset->snprintf(cs, (char *) res.ptr(), res.alloced_length(),
6538 "datetime(%d)", dec));
6539 }
6540
6541
get_date_internal(MYSQL_TIME * ltime)6542 bool Field_datetimef::get_date_internal(MYSQL_TIME *ltime)
6543 {
6544 TIME_from_longlong_datetime_packed(ltime, val_date_temporal());
6545 return false;
6546 }
6547
6548
6549 type_conversion_status
store_internal(const MYSQL_TIME * ltime,int * warnings)6550 Field_datetimef::store_internal(const MYSQL_TIME *ltime, int *warnings)
6551 {
6552 store_packed(TIME_to_longlong_datetime_packed(ltime));
6553 return TYPE_OK;
6554 }
6555
6556
reset()6557 type_conversion_status Field_datetimef::reset()
6558 {
6559 store_packed(0);
6560 return TYPE_OK;
6561 }
6562
6563
val_date_temporal()6564 longlong Field_datetimef::val_date_temporal()
6565 {
6566 return my_datetime_packed_from_binary(ptr, dec);
6567 }
6568
6569
store_packed(longlong nr)6570 type_conversion_status Field_datetimef::store_packed(longlong nr)
6571 {
6572 my_datetime_packed_to_binary(nr, ptr, dec);
6573 return TYPE_OK;
6574 }
6575
6576
6577
6578 /****************************************************************************
6579 ** string type
6580 ** A string may be varchar or binary
6581 ****************************************************************************/
6582
6583 /**
6584 Report "not well formed" or "cannot convert" error
6585 after storing a character string info a field.
6586
6587 As of version 5.0 both cases return the same error:
6588
6589 "Invalid string value: 'xxx' for column 't' at row 1"
6590
6591 Future versions will possibly introduce a new error message:
6592
6593 "Cannot convert character string: 'xxx' for column 't' at row 1"
6594
6595 @param well_formed_error_pos position of the first non-wellformed
6596 character in the source string
6597 @param cannot_convert_error_pos position of the first non-convertable
6598 character in the source string
6599 @param from_end_pos position where conversion stopped in
6600 the source string
6601 @param end end of the source string
6602 @param count_spaces treat trailing spaces as important data
6603 @param cs character set of the string
6604
6605 @return TYPE_OK, TYPE_NOTE_TRUNCATED, TYPE_WARN_TRUNCATED
6606
6607 */
6608
6609 type_conversion_status
check_string_copy_error(const char * well_formed_error_pos,const char * cannot_convert_error_pos,const char * from_end_pos,const char * end,bool count_spaces,const CHARSET_INFO * cs) const6610 Field_longstr::check_string_copy_error(const char *well_formed_error_pos,
6611 const char *cannot_convert_error_pos,
6612 const char *from_end_pos,
6613 const char *end,
6614 bool count_spaces,
6615 const CHARSET_INFO *cs) const
6616 {
6617 const char *pos;
6618 char tmp[32];
6619 THD *thd= table->in_use;
6620
6621 if (!(pos= well_formed_error_pos) &&
6622 !(pos= cannot_convert_error_pos))
6623 return report_if_important_data(from_end_pos, end, count_spaces);
6624
6625 convert_to_printable(tmp, sizeof(tmp), pos, (end - pos), cs, 6);
6626
6627 push_warning_printf(thd,
6628 Sql_condition::WARN_LEVEL_WARN,
6629 ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
6630 ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
6631 "string", tmp, field_name,
6632 thd->get_stmt_da()->current_row_for_warning());
6633 return TYPE_WARN_TRUNCATED;
6634 }
6635
6636
6637 /*
6638 Check if we lost any important data and send a truncation error/warning
6639
6640 SYNOPSIS
6641 Field_longstr::report_if_important_data()
6642 pstr - Truncated rest of string
6643 end - End of truncated string
6644 count_spaces - Treat traling spaces as important data
6645
6646 RETURN VALUES
6647 false - None was truncated (or we don't count cut fields)
6648 true - Some bytes were truncated
6649
6650 NOTE
6651 Check if we lost any important data (anything in a binary string,
6652 or any non-space in others). If only trailing spaces was lost,
6653 send a truncation note, otherwise send a truncation error.
6654 Silently ignore traling spaces if the count_space parameter is FALSE.
6655 */
6656
6657 type_conversion_status
report_if_important_data(const char * pstr,const char * end,bool count_spaces) const6658 Field_longstr::report_if_important_data(const char *pstr, const char *end,
6659 bool count_spaces) const
6660 {
6661 if ((pstr < end) && table->in_use->count_cuted_fields)
6662 {
6663 if (test_if_important_data(field_charset, pstr, end))
6664 {
6665 if (table->in_use->abort_on_warning)
6666 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_DATA_TOO_LONG, 1);
6667 else
6668 set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
6669 return TYPE_WARN_TRUNCATED;
6670 }
6671 else if (count_spaces)
6672 { /* If we lost only spaces then produce a NOTE, not a WARNING */
6673 set_warning(Sql_condition::WARN_LEVEL_NOTE, WARN_DATA_TRUNCATED, 1);
6674 return TYPE_NOTE_TRUNCATED;
6675 }
6676 }
6677 return TYPE_OK;
6678 }
6679
6680
6681 /* Copy a string and fill with space */
6682
6683 type_conversion_status
store(const char * from,uint length,const CHARSET_INFO * cs)6684 Field_string::store(const char *from,uint length,const CHARSET_INFO *cs)
6685 {
6686 ASSERT_COLUMN_MARKED_FOR_WRITE;
6687 uint copy_length;
6688 const char *well_formed_error_pos;
6689 const char *cannot_convert_error_pos;
6690 const char *from_end_pos;
6691
6692 /* See the comment for Field_long::store(long long) */
6693 DBUG_ASSERT(table->in_use == current_thd);
6694
6695 copy_length= well_formed_copy_nchars(field_charset,
6696 (char*) ptr, field_length,
6697 cs, from, length,
6698 field_length / field_charset->mbmaxlen,
6699 &well_formed_error_pos,
6700 &cannot_convert_error_pos,
6701 &from_end_pos);
6702
6703 /* Append spaces if the string was shorter than the field. */
6704 if (copy_length < field_length)
6705 field_charset->cset->fill(field_charset,(char*) ptr+copy_length,
6706 field_length-copy_length,
6707 field_charset->pad_char);
6708
6709 return check_string_copy_error(well_formed_error_pos,
6710 cannot_convert_error_pos, from_end_pos,
6711 from + length, false, cs);
6712 }
6713
6714
6715 /**
6716 Store double value in Field_string or Field_varstring.
6717
6718 Pretty prints double number into field_length characters buffer.
6719
6720 @param nr number
6721 */
6722
store(double nr)6723 type_conversion_status Field_str::store(double nr)
6724 {
6725 ASSERT_COLUMN_MARKED_FOR_WRITE;
6726 char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
6727 uint local_char_length= field_length / charset()->mbmaxlen;
6728 size_t length= 0;
6729 my_bool error= (local_char_length == 0);
6730
6731 // my_gcvt() requires width > 0, and we may have a CHAR(0) column.
6732 if (!error)
6733 length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
6734
6735 if (error)
6736 {
6737 if (table->in_use->abort_on_warning)
6738 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_DATA_TOO_LONG, 1);
6739 else
6740 set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
6741 }
6742 return store(buff, length, &my_charset_numeric);
6743 }
6744
6745
is_equal(Create_field * new_field)6746 uint Field::is_equal(Create_field *new_field)
6747 {
6748 return (new_field->sql_type == real_type());
6749 }
6750
6751
is_equal(Create_field * new_field)6752 uint Field_str::is_equal(Create_field *new_field)
6753 {
6754 return ((new_field->sql_type == real_type()) &&
6755 new_field->charset == field_charset &&
6756 new_field->length == max_display_length());
6757 }
6758
6759
store(longlong nr,bool unsigned_val)6760 type_conversion_status Field_string::store(longlong nr, bool unsigned_val)
6761 {
6762 char buff[64];
6763 int l;
6764 const CHARSET_INFO *cs=charset();
6765 l= (cs->cset->longlong10_to_str)(cs,buff,sizeof(buff),
6766 unsigned_val ? 10 : -10, nr);
6767 return Field_string::store(buff,(uint)l,cs);
6768 }
6769
6770
store_decimal(const my_decimal * d)6771 type_conversion_status Field_longstr::store_decimal(const my_decimal *d)
6772 {
6773 char buff[DECIMAL_MAX_STR_LENGTH+1];
6774 String str(buff, sizeof(buff), &my_charset_numeric);
6775 my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
6776 return store(str.ptr(), str.length(), str.charset());
6777 }
6778
max_data_length() const6779 uint32 Field_longstr::max_data_length() const
6780 {
6781 return field_length + (field_length > 255 ? 2 : 1);
6782 }
6783
6784
val_real(void)6785 double Field_string::val_real(void)
6786 {
6787 ASSERT_COLUMN_MARKED_FOR_READ;
6788 int error;
6789 char *end;
6790 const CHARSET_INFO *cs= charset();
6791 double result;
6792
6793 result= my_strntod(cs,(char*) ptr,field_length,&end,&error);
6794 if (!table->in_use->no_errors &&
6795 (error || (field_length != (uint32)(end - (char*) ptr) &&
6796 !check_if_only_end_space(cs, end,
6797 (char*) ptr + field_length))))
6798 {
6799 ErrConvString err((char*) ptr, field_length, cs);
6800 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
6801 ER_TRUNCATED_WRONG_VALUE,
6802 ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
6803 err.ptr());
6804 }
6805 return result;
6806 }
6807
6808
val_int(void)6809 longlong Field_string::val_int(void)
6810 {
6811 ASSERT_COLUMN_MARKED_FOR_READ;
6812 int error;
6813 char *end;
6814 const CHARSET_INFO *cs= charset();
6815 longlong result;
6816
6817 result= my_strntoll(cs, (char*) ptr,field_length,10,&end,&error);
6818 if (!table->in_use->no_errors &&
6819 (error || (field_length != (uint32)(end - (char*) ptr) &&
6820 !check_if_only_end_space(cs, end,
6821 (char*) ptr + field_length))))
6822 {
6823 ErrConvString err((char*) ptr, field_length, cs);
6824 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
6825 ER_TRUNCATED_WRONG_VALUE,
6826 ER(ER_TRUNCATED_WRONG_VALUE),
6827 "INTEGER", err.ptr());
6828 }
6829 return result;
6830 }
6831
6832
val_str(String * val_buffer MY_ATTRIBUTE ((unused)),String * val_ptr)6833 String *Field_string::val_str(String *val_buffer MY_ATTRIBUTE((unused)),
6834 String *val_ptr)
6835 {
6836 ASSERT_COLUMN_MARKED_FOR_READ;
6837 /* See the comment for Field_long::store(long long) */
6838 DBUG_ASSERT(table->in_use == current_thd);
6839 uint length;
6840 if (table->in_use->variables.sql_mode &
6841 MODE_PAD_CHAR_TO_FULL_LENGTH)
6842 length= my_charpos(field_charset, ptr, ptr + field_length,
6843 field_length / field_charset->mbmaxlen);
6844 else
6845 length= field_charset->cset->lengthsp(field_charset, (const char*) ptr,
6846 field_length);
6847 val_ptr->set((const char*) ptr, length, field_charset);
6848 return val_ptr;
6849 }
6850
6851
val_decimal(my_decimal * decimal_value)6852 my_decimal *Field_string::val_decimal(my_decimal *decimal_value)
6853 {
6854 ASSERT_COLUMN_MARKED_FOR_READ;
6855 int err= str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr, field_length,
6856 charset(), decimal_value);
6857 if (!table->in_use->no_errors && err)
6858 {
6859 ErrConvString errmsg((char*) ptr, field_length, charset());
6860 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
6861 ER_TRUNCATED_WRONG_VALUE,
6862 ER(ER_TRUNCATED_WRONG_VALUE),
6863 "DECIMAL", errmsg.ptr());
6864 }
6865
6866 return decimal_value;
6867 }
6868
6869
6870 struct Check_field_param {
6871 Field *field;
6872 };
6873
6874 #ifdef HAVE_REPLICATION
6875 static bool
check_field_for_37426(const void * param_arg)6876 check_field_for_37426(const void *param_arg)
6877 {
6878 Check_field_param *param= (Check_field_param*) param_arg;
6879 DBUG_ASSERT(param->field->real_type() == MYSQL_TYPE_STRING);
6880 DBUG_PRINT("debug", ("Field %s - type: %d, size: %d",
6881 param->field->field_name,
6882 param->field->real_type(),
6883 param->field->row_pack_length()));
6884 return param->field->row_pack_length() > 255;
6885 }
6886 #endif
6887
6888 bool
compatible_field_size(uint field_metadata,Relay_log_info * rli_arg,uint16 mflags MY_ATTRIBUTE ((unused)),int * order_var)6889 Field_string::compatible_field_size(uint field_metadata,
6890 Relay_log_info *rli_arg,
6891 uint16 mflags MY_ATTRIBUTE((unused)),
6892 int *order_var)
6893 {
6894 #ifdef HAVE_REPLICATION
6895 const Check_field_param check_param = { this };
6896 if (!is_mts_worker(rli_arg->info_thd) && rpl_master_has_bug(rli_arg, 37426, TRUE,
6897 check_field_for_37426, &check_param))
6898 return FALSE; // Not compatible field sizes
6899 #endif
6900 return Field::compatible_field_size(field_metadata, rli_arg, mflags, order_var);
6901 }
6902
6903
cmp(const uchar * a_ptr,const uchar * b_ptr)6904 int Field_string::cmp(const uchar *a_ptr, const uchar *b_ptr)
6905 {
6906 uint a_len, b_len;
6907
6908 if (field_charset->mbmaxlen != 1)
6909 {
6910 uint char_len= field_length/field_charset->mbmaxlen;
6911 a_len= my_charpos(field_charset, a_ptr, a_ptr + field_length, char_len);
6912 b_len= my_charpos(field_charset, b_ptr, b_ptr + field_length, char_len);
6913 }
6914 else
6915 a_len= b_len= field_length;
6916 /*
6917 We have to remove end space to be able to compare multi-byte-characters
6918 like in latin_de 'ae' and 0xe4
6919 */
6920 return field_charset->coll->strnncollsp(field_charset,
6921 a_ptr, a_len,
6922 b_ptr, b_len,
6923 0);
6924 }
6925
6926
make_sort_key(uchar * to,uint length)6927 void Field_string::make_sort_key(uchar *to, uint length)
6928 {
6929 uint tmp MY_ATTRIBUTE((unused))=
6930 field_charset->coll->strnxfrm(field_charset,
6931 to, length, char_length(),
6932 ptr, field_length,
6933 MY_STRXFRM_PAD_WITH_SPACE |
6934 MY_STRXFRM_PAD_TO_MAXLEN);
6935 DBUG_ASSERT(tmp == length);
6936 }
6937
6938
sql_type(String & res) const6939 void Field_string::sql_type(String &res) const
6940 {
6941 THD *thd= table->in_use;
6942 const CHARSET_INFO *cs=res.charset();
6943 ulong length;
6944
6945 length= cs->cset->snprintf(cs,(char*) res.ptr(),
6946 res.alloced_length(), "%s(%d)",
6947 ((type() == MYSQL_TYPE_VAR_STRING &&
6948 !thd->variables.new_mode) ?
6949 (has_charset() ? "varchar" : "varbinary") :
6950 (has_charset() ? "char" : "binary")),
6951 (int) field_length / charset()->mbmaxlen);
6952 res.length(length);
6953 if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
6954 has_charset() && (charset()->state & MY_CS_BINSORT))
6955 res.append(STRING_WITH_LEN(" binary"));
6956 }
6957
6958
pack(uchar * to,const uchar * from,uint max_length,bool low_byte_first MY_ATTRIBUTE ((unused)))6959 uchar *Field_string::pack(uchar *to, const uchar *from,
6960 uint max_length,
6961 bool low_byte_first MY_ATTRIBUTE((unused)))
6962 {
6963 uint length= min(field_length,max_length);
6964 uint local_char_length= max_length/field_charset->mbmaxlen;
6965 DBUG_PRINT("debug", ("Packing field '%s' - length: %u ", field_name, length));
6966
6967 if (length > local_char_length)
6968 local_char_length= my_charpos(field_charset, from, from+length,
6969 local_char_length);
6970 set_if_smaller(length, local_char_length);
6971
6972 /*
6973 TODO: change charset interface to add a new function that does
6974 the following or add a flag to lengthsp to do it itself
6975 (this is for not packing padding adding bytes in BINARY
6976 fields).
6977 */
6978 if (field_charset->mbmaxlen == 1)
6979 {
6980 while (length && from[length-1] == field_charset->pad_char)
6981 length --;
6982 }
6983 else
6984 length= field_charset->cset->lengthsp(field_charset, (const char*) from, length);
6985
6986 // Length always stored little-endian
6987 *to++= (uchar) length;
6988 if (field_length > 255)
6989 *to++= (uchar) (length >> 8);
6990
6991 // Store the actual bytes of the string
6992 memcpy(to, from, length);
6993 return to+length;
6994 }
6995
6996
6997 /**
6998 Unpack a string field from row data.
6999
7000 This method is used to unpack a string field from a master whose size
7001 of the field is less than that of the slave. Note that there can be a
7002 variety of field types represented with this class. Certain types like
7003 ENUM or SET are processed differently. Hence, the upper byte of the
7004 @c param_data argument contains the result of field->real_type() from
7005 the master.
7006
7007 @note For information about how the length is packed, see @c
7008 Field_string::do_save_field_metadata
7009
7010 @param to Destination of the data
7011 @param from Source of the data
7012 @param param_data Real type (upper) and length (lower) values
7013
7014 @return New pointer into memory based on from + length of the data
7015 */
7016 const uchar *
unpack(uchar * to,const uchar * from,uint param_data,bool low_byte_first MY_ATTRIBUTE ((unused)))7017 Field_string::unpack(uchar *to,
7018 const uchar *from,
7019 uint param_data,
7020 bool low_byte_first MY_ATTRIBUTE((unused)))
7021 {
7022 uint from_length, length;
7023
7024 /*
7025 Compute the declared length of the field on the master. This is
7026 used to decide if one or two bytes should be read as length.
7027 */
7028 if (param_data)
7029 from_length= (((param_data >> 4) & 0x300) ^ 0x300) + (param_data & 0x00ff);
7030 else
7031 from_length= field_length;
7032
7033 DBUG_PRINT("debug",
7034 ("param_data: 0x%x, field_length: %u, from_length: %u",
7035 param_data, field_length, from_length));
7036 /*
7037 Compute the actual length of the data by reading one or two bits
7038 (depending on the declared field length on the master).
7039 */
7040 if (from_length > 255)
7041 {
7042 length= uint2korr(from);
7043 from+= 2;
7044 }
7045 else
7046 length= (uint) *from++;
7047
7048 memcpy(to, from, length);
7049 // Pad the string with the pad character of the fields charset
7050 field_charset->cset->fill(field_charset, (char*) to + length, field_length - length, field_charset->pad_char);
7051 return from+length;
7052 }
7053
7054
7055 /**
7056 Save the field metadata for string fields.
7057
7058 Saves the real type in the first byte and the field length in the
7059 second byte of the field metadata array at index of *metadata_ptr and
7060 *(metadata_ptr + 1).
7061
7062 @note In order to be able to handle lengths exceeding 255 and be
7063 backwards-compatible with pre-5.1.26 servers, an extra two bits of
7064 the length has been added to the metadata in such a way that if
7065 they are set, a new unrecognized type is generated. This will
7066 cause pre-5.1-26 servers to stop due to a field type mismatch,
7067 while new servers will be able to extract the extra bits. If the
7068 length is <256, there will be no difference and both a new and an
7069 old server will be able to handle it.
7070
7071 @note The extra two bits are added to bits 13 and 14 of the
7072 parameter data (with 1 being the least siginficant bit and 16 the
7073 most significant bit of the word) by xoring the extra length bits
7074 with the real type. Since all allowable types have 0xF as most
7075 significant bits of the metadata word, lengths <256 will not affect
7076 the real type at all, while all other values will result in a
7077 non-existant type in the range 17-244.
7078
7079 @see Field_string::unpack
7080
7081 @param metadata_ptr First byte of field metadata
7082
7083 @returns number of bytes written to metadata_ptr
7084 */
do_save_field_metadata(uchar * metadata_ptr)7085 int Field_string::do_save_field_metadata(uchar *metadata_ptr)
7086 {
7087 DBUG_ASSERT(field_length < 1024);
7088 DBUG_ASSERT((real_type() & 0xF0) == 0xF0);
7089 DBUG_PRINT("debug", ("field_length: %u, real_type: %u",
7090 field_length, real_type()));
7091 *metadata_ptr= (real_type() ^ ((field_length & 0x300) >> 4));
7092 *(metadata_ptr + 1)= field_length & 0xFF;
7093 return 2;
7094 }
7095
7096
packed_col_length(const uchar * data_ptr,uint length)7097 uint Field_string::packed_col_length(const uchar *data_ptr, uint length)
7098 {
7099 if (length > 255)
7100 return uint2korr(data_ptr)+2;
7101 return (uint) *data_ptr + 1;
7102 }
7103
7104
max_packed_col_length(uint max_length)7105 uint Field_string::max_packed_col_length(uint max_length)
7106 {
7107 return (max_length > 255 ? 2 : 1)+max_length;
7108 }
7109
7110
get_key_image(uchar * buff,uint length,imagetype type_arg)7111 uint Field_string::get_key_image(uchar *buff, uint length, imagetype type_arg)
7112 {
7113 uint bytes = my_charpos(field_charset, (char*) ptr,
7114 (char*) ptr + field_length,
7115 length / field_charset->mbmaxlen);
7116 memcpy(buff, ptr, bytes);
7117 if (bytes < length)
7118 field_charset->cset->fill(field_charset, (char*) buff + bytes,
7119 length - bytes, field_charset->pad_char);
7120 return bytes;
7121 }
7122
7123
new_field(MEM_ROOT * root,TABLE * new_table,bool keep_type)7124 Field *Field_string::new_field(MEM_ROOT *root, TABLE *new_table,
7125 bool keep_type)
7126 {
7127 Field *field;
7128 if (type() != MYSQL_TYPE_VAR_STRING || keep_type)
7129 field= Field::new_field(root, new_table, keep_type);
7130 else if ((field= new Field_varstring(field_length, maybe_null(), field_name,
7131 new_table->s, charset())))
7132 {
7133 /*
7134 Old VARCHAR field which should be modified to a VARCHAR on copy
7135 This is done to ensure that ALTER TABLE will convert old VARCHAR fields
7136 to now VARCHAR fields.
7137 */
7138 field->init(new_table);
7139 /*
7140 Normally orig_table is different from table only if field was created
7141 via ::new_field. Here we alter the type of field, so ::new_field is
7142 not applicable. But we still need to preserve the original field
7143 metadata for the client-server protocol.
7144 */
7145 field->orig_table= orig_table;
7146 }
7147 return field;
7148 }
7149
7150
7151 /****************************************************************************
7152 VARCHAR type
7153 Data in field->ptr is stored as:
7154 1 or 2 bytes length-prefix-header (from Field_varstring::length_bytes)
7155 data
7156
7157 NOTE:
7158 When VARCHAR is stored in a key (for handler::index_read() etc) it's always
7159 stored with a 2 byte prefix. (Just like blob keys).
7160
7161 Normally length_bytes is calculated as (field_length < 256 : 1 ? 2)
7162 The exception is if there is a prefix key field that is part of a long
7163 VARCHAR, in which case field_length for this may be 1 but the length_bytes
7164 is 2.
7165 ****************************************************************************/
7166
7167 const uint Field_varstring::MAX_SIZE= UINT_MAX16;
7168
7169 /**
7170 Save the field metadata for varstring fields.
7171
7172 Saves the field length in the first byte. Note: may consume
7173 2 bytes. Caller must ensure second byte is contiguous with
7174 first byte (e.g. array index 0,1).
7175
7176 @param metadata_ptr First byte of field metadata
7177
7178 @returns number of bytes written to metadata_ptr
7179 */
do_save_field_metadata(uchar * metadata_ptr)7180 int Field_varstring::do_save_field_metadata(uchar *metadata_ptr)
7181 {
7182 DBUG_ASSERT(field_length <= 65535);
7183 int2store((char*)metadata_ptr, field_length);
7184 return 2;
7185 }
7186
store(const char * from,uint length,const CHARSET_INFO * cs)7187 type_conversion_status Field_varstring::store(const char *from,uint length,
7188 const CHARSET_INFO *cs)
7189 {
7190 ASSERT_COLUMN_MARKED_FOR_WRITE;
7191 uint copy_length;
7192 const char *well_formed_error_pos;
7193 const char *cannot_convert_error_pos;
7194 const char *from_end_pos;
7195
7196 copy_length= well_formed_copy_nchars(field_charset,
7197 (char*) ptr + length_bytes,
7198 field_length,
7199 cs, from, length,
7200 field_length / field_charset->mbmaxlen,
7201 &well_formed_error_pos,
7202 &cannot_convert_error_pos,
7203 &from_end_pos);
7204
7205 if (length_bytes == 1)
7206 *ptr= (uchar) copy_length;
7207 else
7208 int2store(ptr, copy_length);
7209
7210 return check_string_copy_error(well_formed_error_pos,
7211 cannot_convert_error_pos, from_end_pos,
7212 from + length, true, cs);
7213 }
7214
7215
store(longlong nr,bool unsigned_val)7216 type_conversion_status Field_varstring::store(longlong nr, bool unsigned_val)
7217 {
7218 char buff[64];
7219 uint length;
7220 length= (uint) (field_charset->cset->longlong10_to_str)(field_charset,
7221 buff,
7222 sizeof(buff),
7223 (unsigned_val ? 10:
7224 -10),
7225 nr);
7226 return Field_varstring::store(buff, length, field_charset);
7227 }
7228
7229
val_real(void)7230 double Field_varstring::val_real(void)
7231 {
7232 ASSERT_COLUMN_MARKED_FOR_READ;
7233 int error;
7234 char *end;
7235 double result;
7236 const CHARSET_INFO* cs= charset();
7237
7238 uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7239 result= my_strntod(cs, (char*)ptr+length_bytes, length, &end, &error);
7240
7241 if (!table->in_use->no_errors &&
7242 (error || (length != (uint)(end - (char*)ptr+length_bytes) &&
7243 !check_if_only_end_space(cs, end, (char*)ptr+length_bytes+length))))
7244 {
7245 push_numerical_conversion_warning(current_thd, (char*)ptr+length_bytes,
7246 length, cs,"DOUBLE",
7247 ER_TRUNCATED_WRONG_VALUE);
7248 }
7249 return result;
7250 }
7251
7252
val_int(void)7253 longlong Field_varstring::val_int(void)
7254 {
7255 ASSERT_COLUMN_MARKED_FOR_READ;
7256 int error;
7257 char *end;
7258 const CHARSET_INFO *cs= charset();
7259
7260 uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7261 longlong result= my_strntoll(cs, (char*) ptr+length_bytes, length, 10,
7262 &end, &error);
7263
7264 if (!table->in_use->no_errors &&
7265 (error || (length != (uint)(end - (char*)ptr+length_bytes) &&
7266 !check_if_only_end_space(cs, end, (char*)ptr+length_bytes+length))))
7267 {
7268 push_numerical_conversion_warning(current_thd, (char*)ptr+length_bytes,
7269 length, cs, "INTEGER",
7270 ER_TRUNCATED_WRONG_VALUE);
7271 }
7272 return result;
7273 }
7274
val_str(String * val_buffer MY_ATTRIBUTE ((unused)),String * val_ptr)7275 String *Field_varstring::val_str(String *val_buffer MY_ATTRIBUTE((unused)),
7276 String *val_ptr)
7277 {
7278 ASSERT_COLUMN_MARKED_FOR_READ;
7279 uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7280 val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
7281 return val_ptr;
7282 }
7283
7284
val_decimal(my_decimal * decimal_value)7285 my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
7286 {
7287 ASSERT_COLUMN_MARKED_FOR_READ;
7288 const CHARSET_INFO *cs= charset();
7289 uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7290 int error= str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
7291 cs, decimal_value);
7292
7293 if (!table->in_use->no_errors && error)
7294 {
7295 push_numerical_conversion_warning(current_thd, (char*)ptr+length_bytes,
7296 length, cs, "DECIMAL",
7297 ER_TRUNCATED_WRONG_VALUE);
7298 }
7299 return decimal_value;
7300 }
7301
7302
cmp_max(const uchar * a_ptr,const uchar * b_ptr,uint max_len)7303 int Field_varstring::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
7304 uint max_len)
7305 {
7306 uint a_length, b_length;
7307 int diff;
7308
7309 if (length_bytes == 1)
7310 {
7311 a_length= (uint) *a_ptr;
7312 b_length= (uint) *b_ptr;
7313 }
7314 else
7315 {
7316 a_length= uint2korr(a_ptr);
7317 b_length= uint2korr(b_ptr);
7318 }
7319 set_if_smaller(a_length, max_len);
7320 set_if_smaller(b_length, max_len);
7321 diff= field_charset->coll->strnncollsp(field_charset,
7322 a_ptr+
7323 length_bytes,
7324 a_length,
7325 b_ptr+
7326 length_bytes,
7327 b_length,0);
7328 return diff;
7329 }
7330
7331
7332 /**
7333 @note
7334 varstring and blob keys are ALWAYS stored with a 2 byte length prefix
7335 */
7336
key_cmp(const uchar * key_ptr,uint max_key_length)7337 int Field_varstring::key_cmp(const uchar *key_ptr, uint max_key_length)
7338 {
7339 uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7340 uint local_char_length= max_key_length / field_charset->mbmaxlen;
7341
7342 local_char_length= my_charpos(field_charset, ptr + length_bytes,
7343 ptr + length_bytes + length, local_char_length);
7344 set_if_smaller(length, local_char_length);
7345 return field_charset->coll->strnncollsp(field_charset,
7346 ptr + length_bytes,
7347 length,
7348 key_ptr+
7349 HA_KEY_BLOB_LENGTH,
7350 uint2korr(key_ptr), 0);
7351 }
7352
7353
7354 /**
7355 Compare to key segments (always 2 byte length prefix).
7356
7357 @note
7358 This is used only to compare key segments created for index_read().
7359 (keys are created and compared in key.cc)
7360 */
7361
key_cmp(const uchar * a,const uchar * b)7362 int Field_varstring::key_cmp(const uchar *a,const uchar *b)
7363 {
7364 return field_charset->coll->strnncollsp(field_charset,
7365 a + HA_KEY_BLOB_LENGTH,
7366 uint2korr(a),
7367 b + HA_KEY_BLOB_LENGTH,
7368 uint2korr(b),
7369 0);
7370 }
7371
7372
make_sort_key(uchar * to,uint length)7373 void Field_varstring::make_sort_key(uchar *to,uint length)
7374 {
7375 uint tot_length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7376
7377 if (field_charset == &my_charset_bin)
7378 {
7379 /* Store length last in high-byte order to sort longer strings first */
7380 if (length_bytes == 1)
7381 to[length-1]= tot_length;
7382 else
7383 mi_int2store(to+length-2, tot_length);
7384 length-= length_bytes;
7385 }
7386
7387 tot_length= field_charset->coll->strnxfrm(field_charset,
7388 to, length, char_length(),
7389 ptr + length_bytes, tot_length,
7390 MY_STRXFRM_PAD_WITH_SPACE |
7391 MY_STRXFRM_PAD_TO_MAXLEN);
7392 DBUG_ASSERT(tot_length == length);
7393 }
7394
7395
key_type() const7396 enum ha_base_keytype Field_varstring::key_type() const
7397 {
7398 enum ha_base_keytype res;
7399
7400 if (binary())
7401 res= length_bytes == 1 ? HA_KEYTYPE_VARBINARY1 : HA_KEYTYPE_VARBINARY2;
7402 else
7403 res= length_bytes == 1 ? HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2;
7404 return res;
7405 }
7406
7407
sql_type(String & res) const7408 void Field_varstring::sql_type(String &res) const
7409 {
7410 THD *thd= table->in_use;
7411 const CHARSET_INFO *cs=res.charset();
7412 ulong length;
7413
7414 length= cs->cset->snprintf(cs,(char*) res.ptr(),
7415 res.alloced_length(), "%s(%d)",
7416 (has_charset() ? "varchar" : "varbinary"),
7417 (int) field_length / charset()->mbmaxlen);
7418 res.length(length);
7419 if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
7420 has_charset() && (charset()->state & MY_CS_BINSORT))
7421 res.append(STRING_WITH_LEN(" binary"));
7422 }
7423
7424
data_length()7425 uint32 Field_varstring::data_length()
7426 {
7427 return length_bytes == 1 ? (uint32) *ptr : uint2korr(ptr);
7428 }
7429
7430 /*
7431 Functions to create a packed row.
7432 Here the number of length bytes are depending on the given max_length
7433 */
7434
pack(uchar * to,const uchar * from,uint max_length,bool low_byte_first MY_ATTRIBUTE ((unused)))7435 uchar *Field_varstring::pack(uchar *to, const uchar *from,
7436 uint max_length,
7437 bool low_byte_first MY_ATTRIBUTE((unused)))
7438 {
7439 uint length= length_bytes == 1 ? (uint) *from : uint2korr(from);
7440 set_if_smaller(max_length, field_length);
7441 if (length > max_length)
7442 length=max_length;
7443
7444 /* Length always stored little-endian */
7445 *to++= length & 0xFF;
7446 if (max_length > 255)
7447 *to++= (length >> 8) & 0xFF;
7448
7449 /* Store bytes of string */
7450 if (length > 0)
7451 memcpy(to, from+length_bytes, length);
7452 return to+length;
7453 }
7454
7455
7456 /**
7457 Unpack a varstring field from row data.
7458
7459 This method is used to unpack a varstring field from a master
7460 whose size of the field is less than that of the slave.
7461
7462 @note
7463 The string length is always packed little-endian.
7464
7465 @param to Destination of the data
7466 @param from Source of the data
7467 @param param_data Length bytes from the master's field data
7468
7469 @return New pointer into memory based on from + length of the data
7470 */
7471 const uchar *
unpack(uchar * to,const uchar * from,uint param_data,bool low_byte_first MY_ATTRIBUTE ((unused)))7472 Field_varstring::unpack(uchar *to, const uchar *from,
7473 uint param_data,
7474 bool low_byte_first MY_ATTRIBUTE((unused)))
7475 {
7476 uint length;
7477 uint l_bytes= (param_data && (param_data < field_length)) ?
7478 (param_data <= 255) ? 1 : 2 : length_bytes;
7479 if (l_bytes == 1)
7480 {
7481 to[0]= *from++;
7482 length= to[0];
7483 if (length_bytes == 2)
7484 to[1]= 0;
7485 }
7486 else /* l_bytes == 2 */
7487 {
7488 length= uint2korr(from);
7489 to[0]= *from++;
7490 to[1]= *from++;
7491 }
7492 if (length)
7493 memcpy(to+ length_bytes, from, length);
7494 return from+length;
7495 }
7496
7497
packed_col_length(const uchar * data_ptr,uint length)7498 uint Field_varstring::packed_col_length(const uchar *data_ptr, uint length)
7499 {
7500 if (length > 255)
7501 return uint2korr(data_ptr)+2;
7502 return (uint) *data_ptr + 1;
7503 }
7504
7505
max_packed_col_length(uint max_length)7506 uint Field_varstring::max_packed_col_length(uint max_length)
7507 {
7508 return (max_length > 255 ? 2 : 1)+max_length;
7509 }
7510
get_key_image(uchar * buff,uint length,imagetype type)7511 uint Field_varstring::get_key_image(uchar *buff, uint length, imagetype type)
7512 {
7513 uint f_length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7514 uint local_char_length= length / field_charset->mbmaxlen;
7515 uchar *pos= ptr+length_bytes;
7516 local_char_length= my_charpos(field_charset, pos, pos + f_length,
7517 local_char_length);
7518 set_if_smaller(f_length, local_char_length);
7519 /* Key is always stored with 2 bytes */
7520 int2store(buff,f_length);
7521 memcpy(buff+HA_KEY_BLOB_LENGTH, pos, f_length);
7522 if (f_length < length)
7523 {
7524 /*
7525 Must clear this as we do a memcmp in opt_range.cc to detect
7526 identical keys
7527 */
7528 memset(buff+HA_KEY_BLOB_LENGTH+f_length, 0, (length-f_length));
7529 }
7530 return HA_KEY_BLOB_LENGTH+f_length;
7531 }
7532
7533
set_key_image(const uchar * buff,uint length)7534 void Field_varstring::set_key_image(const uchar *buff,uint length)
7535 {
7536 length= uint2korr(buff); // Real length is here
7537 (void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
7538 field_charset);
7539 }
7540
7541
cmp_binary(const uchar * a_ptr,const uchar * b_ptr,uint32 max_length)7542 int Field_varstring::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
7543 uint32 max_length)
7544 {
7545 uint32 a_length,b_length;
7546
7547 if (length_bytes == 1)
7548 {
7549 a_length= (uint) *a_ptr;
7550 b_length= (uint) *b_ptr;
7551 }
7552 else
7553 {
7554 a_length= uint2korr(a_ptr);
7555 b_length= uint2korr(b_ptr);
7556 }
7557 set_if_smaller(a_length, max_length);
7558 set_if_smaller(b_length, max_length);
7559 if (a_length != b_length)
7560 return 1;
7561 return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
7562 }
7563
7564
new_field(MEM_ROOT * root,TABLE * new_table,bool keep_type)7565 Field *Field_varstring::new_field(MEM_ROOT *root, TABLE *new_table,
7566 bool keep_type)
7567 {
7568 Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
7569 keep_type);
7570 if (res)
7571 res->length_bytes= length_bytes;
7572 return res;
7573 }
7574
7575
new_key_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr,uchar * new_null_ptr,uint new_null_bit)7576 Field *Field_varstring::new_key_field(MEM_ROOT *root,
7577 TABLE *new_table,
7578 uchar *new_ptr, uchar *new_null_ptr,
7579 uint new_null_bit)
7580 {
7581 Field_varstring *res;
7582 if ((res= (Field_varstring*) Field::new_key_field(root,
7583 new_table,
7584 new_ptr,
7585 new_null_ptr,
7586 new_null_bit)))
7587 {
7588 /* Keys length prefixes are always packed with 2 bytes */
7589 res->length_bytes= 2;
7590 }
7591 return res;
7592 }
7593
7594
is_equal(Create_field * new_field)7595 uint Field_varstring::is_equal(Create_field *new_field)
7596 {
7597 /*
7598 changing column format to/from compressed or changing assiciated
7599 compression dictionary must result in table rebuild
7600 */
7601 if (new_field->sql_type == real_type() &&
7602 new_field->charset == field_charset &&
7603 !has_different_compression_attributes_with(*new_field))
7604 {
7605 if (new_field->length == max_display_length())
7606 return IS_EQUAL_YES;
7607 if (new_field->length > max_display_length() &&
7608 ((new_field->length <= 255 && max_display_length() <= 255) ||
7609 (new_field->length > 255 && max_display_length() > 255)))
7610 return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
7611 }
7612 return IS_EQUAL_NO;
7613 }
7614
7615
hash(ulong * nr,ulong * nr2)7616 void Field_varstring::hash(ulong *nr, ulong *nr2)
7617 {
7618 if (is_null())
7619 {
7620 *nr^= (*nr << 1) | 1;
7621 }
7622 else
7623 {
7624 uint len= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
7625 const CHARSET_INFO *cs= charset();
7626 cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);
7627 }
7628 }
7629
7630
7631 /****************************************************************************
7632 ** blob type
7633 ** A blob is saved as a length and a pointer. The length is stored in the
7634 ** packlength slot and may be from 1-4.
7635 ****************************************************************************/
7636
Field_blob(uchar * ptr_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const char * field_name_arg,TABLE_SHARE * share,uint blob_pack_length,const CHARSET_INFO * cs)7637 Field_blob::Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
7638 enum utype unireg_check_arg, const char *field_name_arg,
7639 TABLE_SHARE *share, uint blob_pack_length,
7640 const CHARSET_INFO *cs)
7641 :Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
7642 null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
7643 cs),
7644 packlength(blob_pack_length)
7645 {
7646 DBUG_ASSERT(blob_pack_length <= 4); // Only pack lengths 1-4 supported currently
7647 flags|= BLOB_FLAG;
7648 share->blob_fields++;
7649 /* TODO: why do not fill table->s->blob_field array here? */
7650 }
7651
7652
store_length(uchar * i_ptr,uint i_packlength,uint32 i_number,bool low_byte_first)7653 void Field_blob::store_length(uchar *i_ptr,
7654 uint i_packlength,
7655 uint32 i_number,
7656 bool low_byte_first)
7657 {
7658 switch (i_packlength) {
7659 case 1:
7660 i_ptr[0]= (uchar) i_number;
7661 break;
7662 case 2:
7663 #ifdef WORDS_BIGENDIAN
7664 if (low_byte_first)
7665 {
7666 int2store(i_ptr,(unsigned short) i_number);
7667 }
7668 else
7669 #endif
7670 shortstore(i_ptr,(unsigned short) i_number);
7671 break;
7672 case 3:
7673 int3store(i_ptr,i_number);
7674 break;
7675 case 4:
7676 #ifdef WORDS_BIGENDIAN
7677 if (low_byte_first)
7678 {
7679 int4store(i_ptr,i_number);
7680 }
7681 else
7682 #endif
7683 longstore(i_ptr,i_number);
7684 }
7685 }
7686
7687
get_length(const uchar * pos,uint packlength_arg,bool low_byte_first)7688 uint32 Field_blob::get_length(const uchar *pos, uint packlength_arg, bool low_byte_first)
7689 {
7690 switch (packlength_arg) {
7691 case 1:
7692 return (uint32) pos[0];
7693 case 2:
7694 {
7695 uint16 tmp;
7696 #ifdef WORDS_BIGENDIAN
7697 if (low_byte_first)
7698 tmp=sint2korr(pos);
7699 else
7700 #endif
7701 shortget(tmp,pos);
7702 return (uint32) tmp;
7703 }
7704 case 3:
7705 return (uint32) uint3korr(pos);
7706 case 4:
7707 {
7708 uint32 tmp;
7709 #ifdef WORDS_BIGENDIAN
7710 if (low_byte_first)
7711 tmp=uint4korr(pos);
7712 else
7713 #endif
7714 longget(tmp,pos);
7715 return (uint32) tmp;
7716 }
7717 }
7718 /* When expanding this, see also MAX_FIELD_BLOBLENGTH. */
7719 return 0; // Impossible
7720 }
7721
7722
7723 /**
7724 Put a blob length field into a record buffer.
7725
7726 Depending on the maximum length of a blob, its length field is
7727 put into 1 to 4 bytes. This is a property of the blob object,
7728 described by 'packlength'.
7729
7730 @param pos Pointer into the record buffer.
7731 @param length The length value to put.
7732 */
7733
put_length(uchar * pos,uint32 length)7734 void Field_blob::put_length(uchar *pos, uint32 length)
7735 {
7736 switch (packlength) {
7737 case 1:
7738 *pos= (char) length;
7739 break;
7740 case 2:
7741 int2store(pos, length);
7742 break;
7743 case 3:
7744 int3store(pos, length);
7745 break;
7746 case 4:
7747 int4store(pos, length);
7748 break;
7749 }
7750 }
7751
7752
7753 /**
7754 Store a blob value to memory storage.
7755 @param from - the string value to store.
7756 @param length - length of the string value.
7757 @param cs - character set of the string value.
7758 @param max_length - Cut at this length safely (multibyte aware).
7759 @param OUT blob_storage - Memory storage to put value to.
7760 */
7761 type_conversion_status
store_to_mem(const char * from,uint length,const CHARSET_INFO * cs,uint max_length,Blob_mem_storage * blob_storage)7762 Field_blob::store_to_mem(const char *from, uint length,
7763 const CHARSET_INFO *cs,
7764 uint max_length,
7765 Blob_mem_storage *blob_storage)
7766 {
7767 DBUG_ASSERT(length > 0);
7768 /*
7769 We don't need to support escaping or character set conversions here,
7770 because store_to_mem() is currently called only when we process
7771 queries having GROUP_CONCAT with ORDER BY or DISTINCT,
7772 hence some assersions:
7773 */
7774 DBUG_ASSERT(!f_is_hex_escape(flags));
7775 DBUG_ASSERT(field_charset == cs);
7776 DBUG_ASSERT(length <= max_data_length());
7777
7778 if (length > max_length)
7779 {
7780 int well_formed_error;
7781 length= cs->cset->well_formed_len(cs, from, from + max_length,
7782 length, &well_formed_error);
7783 table->blob_storage->set_truncated_value(true);
7784 }
7785 char *tmp;
7786 if (!(tmp= table->blob_storage->store(from, length)))
7787 {
7788 memset(ptr, 0, Field_blob::pack_length());
7789 return TYPE_ERR_OOM;
7790 }
7791 store_ptr_and_length(tmp, length);
7792 return TYPE_OK;
7793 }
7794
7795
7796 type_conversion_status
store_internal(const char * from,uint length,const CHARSET_INFO * cs)7797 Field_blob::store_internal(const char *from, uint length,
7798 const CHARSET_INFO *cs)
7799 {
7800 uint new_length;
7801 char buff[STRING_BUFFER_USUAL_SIZE], *tmp;
7802 String tmpstr(buff,sizeof(buff), &my_charset_bin);
7803
7804 DBUG_ASSERT(length > 0);
7805
7806 /*
7807 If the 'from' address is in the range of the temporary 'value'-
7808 object we need to copy the content to a different location or it will be
7809 invalidated when the 'value'-object is reallocated to make room for
7810 the new character set.
7811 */
7812 if (from >= value.ptr() && from <= value.ptr()+value.length())
7813 {
7814 /*
7815 If content of the 'from'-address is cached in the 'value'-object
7816 it is possible that the content needs a character conversion.
7817 */
7818 if (!String::needs_conversion_on_storage(length, cs, field_charset))
7819 {
7820 store_ptr_and_length(from, length);
7821 return TYPE_OK;
7822 }
7823 if (tmpstr.copy(from, length, cs))
7824 goto oom_error;
7825 from= tmpstr.ptr();
7826 }
7827
7828 new_length= min(max_data_length(), field_charset->mbmaxlen * length);
7829 if (value.alloc(new_length))
7830 goto oom_error;
7831 tmp= const_cast<char*>(value.ptr());
7832
7833 if (f_is_hex_escape(flags))
7834 {
7835 uint copy_length= my_copy_with_hex_escaping(field_charset,
7836 tmp, new_length,
7837 from, length);
7838 store_ptr_and_length(tmp, copy_length);
7839 return TYPE_OK;
7840 }
7841
7842
7843 {
7844 const char *well_formed_error_pos;
7845 const char *cannot_convert_error_pos;
7846 const char *from_end_pos;
7847 /*
7848 "length" is OK as "nchars" argument to well_formed_copy_nchars as this
7849 is never used to limit the length of the data. The cut of long data
7850 is done with the new_length value.
7851 */
7852 uint copy_length= well_formed_copy_nchars(field_charset,
7853 tmp, new_length,
7854 cs, from, length,
7855 length,
7856 &well_formed_error_pos,
7857 &cannot_convert_error_pos,
7858 &from_end_pos);
7859
7860 store_ptr_and_length(tmp, copy_length);
7861 return check_string_copy_error(well_formed_error_pos,
7862 cannot_convert_error_pos, from_end_pos,
7863 from + length, true, cs);
7864 }
7865
7866 oom_error:
7867 /* Fatal OOM error */
7868 memset(ptr, 0, Field_blob::pack_length());
7869 return TYPE_ERR_OOM;
7870 }
7871
7872
7873 type_conversion_status
store(const char * from,uint length,const CHARSET_INFO * cs)7874 Field_blob::store(const char *from, uint length, const CHARSET_INFO *cs)
7875 {
7876 ASSERT_COLUMN_MARKED_FOR_WRITE;
7877
7878 if (!length)
7879 {
7880 memset(ptr, 0, Field_blob::pack_length());
7881 return TYPE_OK;
7882 }
7883
7884 if (table->blob_storage) // GROUP_CONCAT with ORDER BY | DISTINCT
7885 return store_to_mem(from, length, cs,
7886 table->in_use->variables.group_concat_max_len,
7887 table->blob_storage);
7888
7889 return store_internal(from, length, cs);
7890 }
7891
7892
store(double nr)7893 type_conversion_status Field_blob::store(double nr)
7894 {
7895 const CHARSET_INFO *cs=charset();
7896 value.set_real(nr, NOT_FIXED_DEC, cs);
7897 return Field_blob::store(value.ptr(),(uint) value.length(), cs);
7898 }
7899
7900
store(longlong nr,bool unsigned_val)7901 type_conversion_status Field_blob::store(longlong nr, bool unsigned_val)
7902 {
7903 const CHARSET_INFO *cs=charset();
7904 value.set_int(nr, unsigned_val, cs);
7905 return Field_blob::store(value.ptr(), (uint) value.length(), cs);
7906 }
7907
7908
val_real(void)7909 double Field_blob::val_real(void)
7910 {
7911 ASSERT_COLUMN_MARKED_FOR_READ;
7912 int not_used;
7913 char *end_not_used, *blob;
7914 uint32 length;
7915 const CHARSET_INFO *cs;
7916
7917 memcpy(&blob, ptr+packlength, sizeof(char*));
7918 if (!blob)
7919 return 0.0;
7920 length= get_length(ptr);
7921 cs= charset();
7922 return my_strntod(cs, blob, length, &end_not_used, ¬_used);
7923 }
7924
7925
val_int(void)7926 longlong Field_blob::val_int(void)
7927 {
7928 ASSERT_COLUMN_MARKED_FOR_READ;
7929 int not_used;
7930 char *blob;
7931 memcpy(&blob, ptr+packlength, sizeof(char*));
7932 if (!blob)
7933 return 0;
7934 uint32 length=get_length(ptr);
7935 return my_strntoll(charset(),blob,length,10,NULL,¬_used);
7936 }
7937
val_str(String * val_buffer MY_ATTRIBUTE ((unused)),String * val_ptr)7938 String *Field_blob::val_str(String *val_buffer MY_ATTRIBUTE((unused)),
7939 String *val_ptr)
7940 {
7941 ASSERT_COLUMN_MARKED_FOR_READ;
7942 char *blob;
7943 memcpy(&blob, ptr+packlength, sizeof(char*));
7944 if (!blob)
7945 val_ptr->set("",0,charset()); // A bit safer than ->length(0)
7946 else
7947 val_ptr->set((const char*) blob,get_length(ptr),charset());
7948 return val_ptr;
7949 }
7950
7951
val_decimal(my_decimal * decimal_value)7952 my_decimal *Field_blob::val_decimal(my_decimal *decimal_value)
7953 {
7954 ASSERT_COLUMN_MARKED_FOR_READ;
7955 const char *blob;
7956 size_t length;
7957 memcpy(&blob, ptr+packlength, sizeof(const uchar*));
7958 if (!blob)
7959 {
7960 blob= "";
7961 length= 0;
7962 }
7963 else
7964 length= get_length(ptr);
7965
7966 str2my_decimal(E_DEC_FATAL_ERROR, blob, length, charset(),
7967 decimal_value);
7968 return decimal_value;
7969 }
7970
7971
cmp(const uchar * a,uint32 a_length,const uchar * b,uint32 b_length)7972 int Field_blob::cmp(const uchar *a,uint32 a_length, const uchar *b,
7973 uint32 b_length)
7974 {
7975 return field_charset->coll->strnncollsp(field_charset,
7976 a, a_length, b, b_length,
7977 0);
7978 }
7979
7980
cmp_max(const uchar * a_ptr,const uchar * b_ptr,uint max_length)7981 int Field_blob::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
7982 uint max_length)
7983 {
7984 uchar *blob1,*blob2;
7985 memcpy(&blob1, a_ptr+packlength, sizeof(char*));
7986 memcpy(&blob2, b_ptr+packlength, sizeof(char*));
7987 uint a_len= get_length(a_ptr), b_len= get_length(b_ptr);
7988 set_if_smaller(a_len, max_length);
7989 set_if_smaller(b_len, max_length);
7990 return Field_blob::cmp(blob1,a_len,blob2,b_len);
7991 }
7992
7993
cmp_binary(const uchar * a_ptr,const uchar * b_ptr,uint32 max_length)7994 int Field_blob::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
7995 uint32 max_length)
7996 {
7997 char *a,*b;
7998 uint diff;
7999 uint32 a_length,b_length;
8000 memcpy(&a, a_ptr+packlength, sizeof(char*));
8001 memcpy(&b, b_ptr+packlength, sizeof(char*));
8002 a_length=get_length(a_ptr);
8003 if (a_length > max_length)
8004 a_length=max_length;
8005 b_length=get_length(b_ptr);
8006 if (b_length > max_length)
8007 b_length=max_length;
8008 diff=memcmp(a,b,min(a_length,b_length));
8009 return diff ? diff : (int) (a_length - b_length);
8010 }
8011
8012
8013 /* The following is used only when comparing a key */
8014
get_key_image(uchar * buff,uint length,imagetype type_arg)8015 uint Field_blob::get_key_image(uchar *buff,uint length, imagetype type_arg)
8016 {
8017 uint32 blob_length= get_length(ptr);
8018 uchar *blob;
8019
8020 #ifdef HAVE_SPATIAL
8021 if (type_arg == itMBR)
8022 {
8023 MBR mbr;
8024 Geometry_buffer buffer;
8025 Geometry *gobj;
8026 const uint image_length= SIZEOF_STORED_DOUBLE*4;
8027
8028 if (blob_length < SRID_SIZE)
8029 {
8030 memset(buff, 0, image_length);
8031 return image_length;
8032 }
8033 get_ptr(&blob);
8034 gobj= Geometry::construct(&buffer, (char*) blob, blob_length);
8035 if (!gobj || gobj->get_mbr(&mbr))
8036 memset(buff, 0, image_length);
8037 else
8038 {
8039 float8store(buff, mbr.xmin);
8040 float8store(buff+8, mbr.xmax);
8041 float8store(buff+16, mbr.ymin);
8042 float8store(buff+24, mbr.ymax);
8043 }
8044 return image_length;
8045 }
8046 #endif /*HAVE_SPATIAL*/
8047
8048 get_ptr(&blob);
8049 uint local_char_length= length / field_charset->mbmaxlen;
8050 local_char_length= my_charpos(field_charset, blob, blob + blob_length,
8051 local_char_length);
8052 set_if_smaller(blob_length, local_char_length);
8053
8054 if ((uint32) length > blob_length)
8055 {
8056 /*
8057 Must clear this as we do a memcmp in opt_range.cc to detect
8058 identical keys
8059 */
8060 memset(buff+HA_KEY_BLOB_LENGTH+blob_length, 0, (length-blob_length));
8061 length=(uint) blob_length;
8062 }
8063 int2store(buff,length);
8064 memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
8065 return HA_KEY_BLOB_LENGTH+length;
8066 }
8067
8068
set_key_image(const uchar * buff,uint length)8069 void Field_blob::set_key_image(const uchar *buff,uint length)
8070 {
8071 length= uint2korr(buff);
8072 (void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
8073 field_charset);
8074 }
8075
8076
key_cmp(const uchar * key_ptr,uint max_key_length)8077 int Field_blob::key_cmp(const uchar *key_ptr, uint max_key_length)
8078 {
8079 uchar *blob1;
8080 uint blob_length=get_length(ptr);
8081 memcpy(&blob1, ptr+packlength, sizeof(char*));
8082 const CHARSET_INFO *cs= charset();
8083 uint local_char_length= max_key_length / cs->mbmaxlen;
8084 local_char_length= my_charpos(cs, blob1, blob1+blob_length,
8085 local_char_length);
8086 set_if_smaller(blob_length, local_char_length);
8087 return Field_blob::cmp(blob1, blob_length,
8088 key_ptr+HA_KEY_BLOB_LENGTH,
8089 uint2korr(key_ptr));
8090 }
8091
key_cmp(const uchar * a,const uchar * b)8092 int Field_blob::key_cmp(const uchar *a,const uchar *b)
8093 {
8094 return Field_blob::cmp(a+HA_KEY_BLOB_LENGTH, uint2korr(a),
8095 b+HA_KEY_BLOB_LENGTH, uint2korr(b));
8096 }
8097
8098
8099 /**
8100 Save the field metadata for blob fields.
8101
8102 Saves the pack length in the first byte of the field metadata array
8103 at index of *metadata_ptr.
8104
8105 @param metadata_ptr First byte of field metadata
8106
8107 @returns number of bytes written to metadata_ptr
8108 */
do_save_field_metadata(uchar * metadata_ptr)8109 int Field_blob::do_save_field_metadata(uchar *metadata_ptr)
8110 {
8111 DBUG_ENTER("Field_blob::do_save_field_metadata");
8112 *metadata_ptr= pack_length_no_ptr();
8113 DBUG_PRINT("debug", ("metadata: %u (pack_length_no_ptr)", *metadata_ptr));
8114 DBUG_RETURN(1);
8115 }
8116
8117
sort_length() const8118 uint32 Field_blob::sort_length() const
8119 {
8120 return (uint32) (current_thd->variables.max_sort_length +
8121 (field_charset == &my_charset_bin ? 0 : packlength));
8122 }
8123
8124
make_sort_key(uchar * to,uint length)8125 void Field_blob::make_sort_key(uchar *to,uint length)
8126 {
8127 uchar *blob;
8128 uint blob_length=get_length();
8129
8130 if (!blob_length && field_charset->pad_char == 0)
8131 memset(to, 0, length);
8132 else
8133 {
8134 if (field_charset == &my_charset_bin)
8135 {
8136 uchar *pos;
8137
8138 /*
8139 Store length of blob last in blob to shorter blobs before longer blobs
8140 */
8141 length-= packlength;
8142 pos= to+length;
8143 uint key_length= MY_MIN(blob_length, length);
8144
8145 switch (packlength) {
8146 case 1:
8147 *pos= (char) key_length;
8148 break;
8149 case 2:
8150 mi_int2store(pos, key_length);
8151 break;
8152 case 3:
8153 mi_int3store(pos, key_length);
8154 break;
8155 case 4:
8156 mi_int4store(pos, key_length);
8157 break;
8158 }
8159 }
8160 memcpy(&blob, ptr+packlength, sizeof(char*));
8161
8162 blob_length= field_charset->coll->strnxfrm(field_charset,
8163 to, length, length,
8164 blob, blob_length,
8165 MY_STRXFRM_PAD_WITH_SPACE |
8166 MY_STRXFRM_PAD_TO_MAXLEN);
8167 DBUG_ASSERT(blob_length == length);
8168 }
8169 }
8170
8171
sql_type(String & res) const8172 void Field_blob::sql_type(String &res) const
8173 {
8174 const char *str;
8175 uint length;
8176 switch (packlength) {
8177 default: str="tiny"; length=4; break;
8178 case 2: str=""; length=0; break;
8179 case 3: str="medium"; length= 6; break;
8180 case 4: str="long"; length=4; break;
8181 }
8182 res.set_ascii(str,length);
8183 if (charset() == &my_charset_bin)
8184 res.append(STRING_WITH_LEN("blob"));
8185 else
8186 {
8187 res.append(STRING_WITH_LEN("text"));
8188 }
8189 }
8190
pack(uchar * to,const uchar * from,uint max_length,bool low_byte_first)8191 uchar *Field_blob::pack(uchar *to, const uchar *from,
8192 uint max_length, bool low_byte_first)
8193 {
8194 uchar *save= ptr;
8195 ptr= (uchar*) from;
8196 uint32 length=get_length(); // Length of from string
8197
8198 /*
8199 Store max length, which will occupy packlength bytes. If the max
8200 length given is smaller than the actual length of the blob, we
8201 just store the initial bytes of the blob.
8202 */
8203 store_length(to, packlength, min(length, max_length), low_byte_first);
8204
8205 /*
8206 Store the actual blob data, which will occupy 'length' bytes.
8207 */
8208 if (length > 0)
8209 {
8210 get_ptr((uchar**) &from);
8211 memcpy(to+packlength, from,length);
8212 }
8213 ptr=save; // Restore org row pointer
8214 return to+packlength+length;
8215 }
8216
8217
8218 /**
8219 Unpack a blob field from row data.
8220
8221 This method is used to unpack a blob field from a master whose size of
8222 the field is less than that of the slave. Note: This method is included
8223 to satisfy inheritance rules, but is not needed for blob fields. It
8224 simply is used as a pass-through to the original unpack() method for
8225 blob fields.
8226
8227 @param to Destination of the data
8228 @param from Source of the data
8229 @param param_data @c TRUE if base types should be stored in little-
8230 endian format, @c FALSE if native format should
8231 be used.
8232
8233 @return New pointer into memory based on from + length of the data
8234 */
unpack(uchar * to,const uchar * from,uint param_data,bool low_byte_first)8235 const uchar *Field_blob::unpack(uchar *to,
8236 const uchar *from,
8237 uint param_data,
8238 bool low_byte_first)
8239 {
8240 DBUG_ENTER("Field_blob::unpack");
8241 DBUG_PRINT("enter", ("to: 0x%lx; from: 0x%lx;"
8242 " param_data: %u; low_byte_first: %d",
8243 (ulong) to, (ulong) from, param_data, low_byte_first));
8244 uint const master_packlength=
8245 param_data > 0 ? param_data & 0xFF : packlength;
8246 uint32 const length= get_length(from, master_packlength, low_byte_first);
8247 DBUG_DUMP("packed", from, length + master_packlength);
8248 bitmap_set_bit(table->write_set, field_index);
8249 store(reinterpret_cast<const char*>(from) + master_packlength,
8250 length, field_charset);
8251 #ifndef DBUG_OFF
8252 uchar *vptr;
8253 get_ptr(&vptr);
8254 DBUG_DUMP("field", ptr, pack_length() /* len bytes + ptr bytes */);
8255 DBUG_DUMP("value", vptr, length /* the blob value length */);
8256 #endif
8257 DBUG_RETURN(from + master_packlength + length);
8258 }
8259
packed_col_length(const uchar * data_ptr,uint length)8260 uint Field_blob::packed_col_length(const uchar *data_ptr, uint length)
8261 {
8262 if (length > 255)
8263 return uint2korr(data_ptr)+2;
8264 return (uint) *data_ptr + 1;
8265 }
8266
8267
max_packed_col_length(uint max_length)8268 uint Field_blob::max_packed_col_length(uint max_length)
8269 {
8270 return (max_length > 255 ? 2 : 1)+max_length;
8271 }
8272
8273
is_equal(Create_field * new_field)8274 uint Field_blob::is_equal(Create_field *new_field)
8275 {
8276 /*
8277 changing column format to/from compressed or changing assiciated
8278 compression dictionary must result in table rebuild
8279 */
8280 return ((new_field->sql_type == get_blob_type_from_length(max_data_length()))
8281 && new_field->charset == field_charset &&
8282 new_field->pack_length == pack_length() &&
8283 !has_different_compression_attributes_with(*new_field));
8284 }
8285
8286
8287 #ifdef HAVE_SPATIAL
8288
sql_type(String & res) const8289 void Field_geom::sql_type(String &res) const
8290 {
8291 const CHARSET_INFO *cs= &my_charset_latin1;
8292 switch (geom_type)
8293 {
8294 case GEOM_POINT:
8295 res.set(STRING_WITH_LEN("point"), cs);
8296 break;
8297 case GEOM_LINESTRING:
8298 res.set(STRING_WITH_LEN("linestring"), cs);
8299 break;
8300 case GEOM_POLYGON:
8301 res.set(STRING_WITH_LEN("polygon"), cs);
8302 break;
8303 case GEOM_MULTIPOINT:
8304 res.set(STRING_WITH_LEN("multipoint"), cs);
8305 break;
8306 case GEOM_MULTILINESTRING:
8307 res.set(STRING_WITH_LEN("multilinestring"), cs);
8308 break;
8309 case GEOM_MULTIPOLYGON:
8310 res.set(STRING_WITH_LEN("multipolygon"), cs);
8311 break;
8312 case GEOM_GEOMETRYCOLLECTION:
8313 res.set(STRING_WITH_LEN("geometrycollection"), cs);
8314 break;
8315 default:
8316 res.set(STRING_WITH_LEN("geometry"), cs);
8317 }
8318 }
8319
8320
store(double nr)8321 type_conversion_status Field_geom::store(double nr)
8322 {
8323 my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
8324 ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
8325 return TYPE_ERR_BAD_VALUE;
8326 }
8327
8328
store(longlong nr,bool unsigned_val)8329 type_conversion_status Field_geom::store(longlong nr, bool unsigned_val)
8330 {
8331 my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
8332 ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
8333 return TYPE_ERR_BAD_VALUE;
8334 }
8335
8336
store_decimal(const my_decimal *)8337 type_conversion_status Field_geom::store_decimal(const my_decimal *)
8338 {
8339 my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
8340 ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
8341 return TYPE_ERR_BAD_VALUE;
8342 }
8343
8344
8345 type_conversion_status
store_internal(const char * from,uint length,const CHARSET_INFO * cs)8346 Field_geom::store_internal(const char *from, uint length,
8347 const CHARSET_INFO *cs)
8348 {
8349 uint32 wkb_type;
8350
8351 DBUG_ASSERT(length > 0);
8352
8353 // Check given WKB
8354 if (from == Geometry::bad_geometry_data.ptr() ||
8355 length < SRID_SIZE + WKB_HEADER_SIZE + SIZEOF_STORED_DOUBLE * 2 ||
8356 (wkb_type= uint4korr(from + SRID_SIZE + 1)) < (uint32) Geometry::wkb_point ||
8357 wkb_type > (uint32) Geometry::wkb_last)
8358 {
8359 memset(ptr, 0, Field_blob::pack_length());
8360 my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
8361 ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
8362 return TYPE_ERR_BAD_VALUE;
8363 }
8364
8365 if (table->copy_blobs || length <= MAX_FIELD_WIDTH)
8366 { // Must make a copy
8367 value.copy(from, length, cs);
8368 from= value.ptr();
8369 }
8370
8371 store_ptr_and_length(from, length);
8372 return TYPE_OK;
8373 }
8374
8375
is_equal(Create_field * new_field)8376 uint Field_geom::is_equal(Create_field *new_field)
8377 {
8378 return new_field->sql_type == real_type() &&
8379 new_field->geom_type == get_geometry_type() &&
8380 new_field->charset == field_charset &&
8381 new_field->pack_length == pack_length();
8382 }
8383
8384
8385 #endif /*HAVE_SPATIAL*/
8386
8387 /****************************************************************************
8388 ** enum type.
8389 ** This is a string which only can have a selection of different values.
8390 ** If one uses this string in a number context one gets the type number.
8391 ****************************************************************************/
8392
key_type() const8393 enum ha_base_keytype Field_enum::key_type() const
8394 {
8395 switch (packlength) {
8396 default: return HA_KEYTYPE_BINARY;
8397 case 2: return HA_KEYTYPE_USHORT_INT;
8398 case 3: return HA_KEYTYPE_UINT24;
8399 case 4: return HA_KEYTYPE_ULONG_INT;
8400 case 8: return HA_KEYTYPE_ULONGLONG;
8401 }
8402 }
8403
store_type(ulonglong value)8404 void Field_enum::store_type(ulonglong value)
8405 {
8406 switch (packlength) {
8407 case 1: ptr[0]= (uchar) value; break;
8408 case 2:
8409 #ifdef WORDS_BIGENDIAN
8410 if (table->s->db_low_byte_first)
8411 {
8412 int2store(ptr,(unsigned short) value);
8413 }
8414 else
8415 #endif
8416 shortstore(ptr,(unsigned short) value);
8417 break;
8418 case 3: int3store(ptr,(long) value); break;
8419 case 4:
8420 #ifdef WORDS_BIGENDIAN
8421 if (table->s->db_low_byte_first)
8422 {
8423 int4store(ptr,value);
8424 }
8425 else
8426 #endif
8427 longstore(ptr,(long) value);
8428 break;
8429 case 8:
8430 #ifdef WORDS_BIGENDIAN
8431 if (table->s->db_low_byte_first)
8432 {
8433 int8store(ptr,value);
8434 }
8435 else
8436 #endif
8437 longlongstore(ptr,value); break;
8438 }
8439 }
8440
8441
8442 /**
8443 @note
8444 Storing a empty string in a enum field gives a warning
8445 (if there isn't a empty value in the enum)
8446 */
8447
8448 type_conversion_status
store(const char * from,uint length,const CHARSET_INFO * cs)8449 Field_enum::store(const char *from,uint length,const CHARSET_INFO *cs)
8450 {
8451 ASSERT_COLUMN_MARKED_FOR_WRITE;
8452 int err= 0;
8453 type_conversion_status ret= TYPE_OK;
8454 char buff[STRING_BUFFER_USUAL_SIZE];
8455 String tmpstr(buff,sizeof(buff), &my_charset_bin);
8456
8457 /* Convert character set if necessary */
8458 if (String::needs_conversion_on_storage(length, cs, field_charset))
8459 {
8460 uint dummy_errors;
8461 tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
8462 from= tmpstr.ptr();
8463 length= tmpstr.length();
8464 }
8465
8466 /* Remove end space */
8467 length= field_charset->cset->lengthsp(field_charset, from, length);
8468 uint tmp=find_type2(typelib, from, length, field_charset);
8469 if (!tmp)
8470 {
8471 if (length < 6) // Can't be more than 99999 enums
8472 {
8473 /* This is for reading numbers with LOAD DATA INFILE */
8474 char *end;
8475 tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
8476 if (err || end != from+length || tmp > typelib->count)
8477 {
8478 tmp=0;
8479 set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8480 ret= TYPE_WARN_TRUNCATED;
8481 }
8482 if (!table->in_use->count_cuted_fields)
8483 ret= TYPE_OK;
8484 }
8485 else
8486 set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8487 }
8488 store_type((ulonglong) tmp);
8489 return ret;
8490 }
8491
8492
store(double nr)8493 type_conversion_status Field_enum::store(double nr)
8494 {
8495 return Field_enum::store((longlong) nr, FALSE);
8496 }
8497
8498
store(longlong nr,bool unsigned_val)8499 type_conversion_status Field_enum::store(longlong nr, bool unsigned_val)
8500 {
8501 ASSERT_COLUMN_MARKED_FOR_WRITE;
8502 type_conversion_status error= TYPE_OK;
8503 if ((ulonglong) nr > typelib->count || nr == 0)
8504 {
8505 set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8506 if (nr != 0 || table->in_use->count_cuted_fields)
8507 {
8508 nr= 0;
8509 error= TYPE_WARN_TRUNCATED;
8510 }
8511 }
8512 store_type((ulonglong) (uint) nr);
8513 return error;
8514 }
8515
8516
val_real(void)8517 double Field_enum::val_real(void)
8518 {
8519 return (double) Field_enum::val_int();
8520 }
8521
8522
val_decimal(my_decimal * decimal_value)8523 my_decimal *Field_enum::val_decimal(my_decimal *decimal_value)
8524 {
8525 ASSERT_COLUMN_MARKED_FOR_READ;
8526 int2my_decimal(E_DEC_FATAL_ERROR, val_int(), 0, decimal_value);
8527 return decimal_value;
8528 }
8529
8530
val_int(void)8531 longlong Field_enum::val_int(void)
8532 {
8533 ASSERT_COLUMN_MARKED_FOR_READ;
8534 switch (packlength) {
8535 case 1:
8536 return (longlong) ptr[0];
8537 case 2:
8538 {
8539 uint16 tmp;
8540 #ifdef WORDS_BIGENDIAN
8541 if (table->s->db_low_byte_first)
8542 tmp=sint2korr(ptr);
8543 else
8544 #endif
8545 shortget(tmp,ptr);
8546 return (longlong) tmp;
8547 }
8548 case 3:
8549 return (longlong) uint3korr(ptr);
8550 case 4:
8551 {
8552 uint32 tmp;
8553 #ifdef WORDS_BIGENDIAN
8554 if (table->s->db_low_byte_first)
8555 tmp=uint4korr(ptr);
8556 else
8557 #endif
8558 longget(tmp,ptr);
8559 return (longlong) tmp;
8560 }
8561 case 8:
8562 {
8563 longlong tmp;
8564 #ifdef WORDS_BIGENDIAN
8565 if (table->s->db_low_byte_first)
8566 tmp=sint8korr(ptr);
8567 else
8568 #endif
8569 longlongget(tmp,ptr);
8570 return tmp;
8571 }
8572 }
8573 return 0; // impossible
8574 }
8575
8576
8577 /**
8578 Save the field metadata for enum fields.
8579
8580 Saves the real type in the first byte and the pack length in the
8581 second byte of the field metadata array at index of *metadata_ptr and
8582 *(metadata_ptr + 1).
8583
8584 @param metadata_ptr First byte of field metadata
8585
8586 @returns number of bytes written to metadata_ptr
8587 */
do_save_field_metadata(uchar * metadata_ptr)8588 int Field_enum::do_save_field_metadata(uchar *metadata_ptr)
8589 {
8590 *metadata_ptr= real_type();
8591 *(metadata_ptr + 1)= pack_length();
8592 return 2;
8593 }
8594
8595
val_str(String * val_buffer MY_ATTRIBUTE ((unused)),String * val_ptr)8596 String *Field_enum::val_str(String *val_buffer MY_ATTRIBUTE((unused)),
8597 String *val_ptr)
8598 {
8599 uint tmp=(uint) Field_enum::val_int();
8600 if (!tmp || tmp > typelib->count)
8601 val_ptr->set("", 0, field_charset);
8602 else
8603 val_ptr->set((const char*) typelib->type_names[tmp-1],
8604 typelib->type_lengths[tmp-1],
8605 field_charset);
8606 return val_ptr;
8607 }
8608
cmp(const uchar * a_ptr,const uchar * b_ptr)8609 int Field_enum::cmp(const uchar *a_ptr, const uchar *b_ptr)
8610 {
8611 uchar *old= ptr;
8612 ptr= (uchar*) a_ptr;
8613 ulonglong a=Field_enum::val_int();
8614 ptr= (uchar*) b_ptr;
8615 ulonglong b=Field_enum::val_int();
8616 ptr= old;
8617 return (a < b) ? -1 : (a > b) ? 1 : 0;
8618 }
8619
make_sort_key(uchar * to,uint length)8620 void Field_enum::make_sort_key(uchar *to, uint length)
8621 {
8622 #ifdef WORDS_BIGENDIAN
8623 if (!table->s->db_low_byte_first)
8624 copy_integer<true>(to, length, ptr, packlength, true);
8625 else
8626 #endif
8627 copy_integer<false>(to, length, ptr, packlength, true);
8628 }
8629
8630
sql_type(String & res) const8631 void Field_enum::sql_type(String &res) const
8632 {
8633 char buffer[255];
8634 String enum_item(buffer, sizeof(buffer), res.charset());
8635
8636 res.length(0);
8637 res.append(STRING_WITH_LEN("enum("));
8638
8639 bool flag=0;
8640 uint *len= typelib->type_lengths;
8641 for (const char **pos= typelib->type_names; *pos; pos++, len++)
8642 {
8643 uint dummy_errors;
8644 if (flag)
8645 res.append(',');
8646 /* convert to res.charset() == utf8, then quote */
8647 enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
8648 append_unescaped(&res, enum_item.ptr(), enum_item.length());
8649 flag= 1;
8650 }
8651 res.append(')');
8652 }
8653
8654
new_field(MEM_ROOT * root,TABLE * new_table,bool keep_type)8655 Field *Field_enum::new_field(MEM_ROOT *root, TABLE *new_table,
8656 bool keep_type)
8657 {
8658 Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
8659 if (res)
8660 res->typelib= copy_typelib(root, typelib);
8661 return res;
8662 }
8663
8664
8665 /*
8666 set type.
8667 This is a string which can have a collection of different values.
8668 Each string value is separated with a ','.
8669 For example "One,two,five"
8670 If one uses this string in a number context one gets the bits as a longlong
8671 number.
8672 */
8673
8674
8675 type_conversion_status
store(const char * from,uint length,const CHARSET_INFO * cs)8676 Field_set::store(const char *from,uint length,const CHARSET_INFO *cs)
8677 {
8678 ASSERT_COLUMN_MARKED_FOR_WRITE;
8679 bool got_warning= 0;
8680 int err= 0;
8681 type_conversion_status ret= TYPE_OK;
8682 char *not_used;
8683 uint not_used2;
8684 char buff[STRING_BUFFER_USUAL_SIZE];
8685 String tmpstr(buff,sizeof(buff), &my_charset_bin);
8686
8687 /* Convert character set if necessary */
8688 if (String::needs_conversion_on_storage(length, cs, field_charset))
8689 {
8690 uint dummy_errors;
8691 tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
8692 from= tmpstr.ptr();
8693 length= tmpstr.length();
8694 }
8695 ulonglong tmp= find_set(typelib, from, length, field_charset,
8696 ¬_used, ¬_used2, &got_warning);
8697 if (!tmp && length && length < 22)
8698 {
8699 /* This is for reading numbers with LOAD DATA INFILE */
8700 char *end;
8701 tmp=my_strntoull(cs,from,length,10,&end,&err);
8702 if (err || end != from+length ||
8703 tmp > (ulonglong) (((longlong) 1 << typelib->count) - (longlong) 1))
8704 {
8705 tmp=0;
8706 set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8707 ret= TYPE_WARN_TRUNCATED;
8708 }
8709 }
8710 else if (got_warning)
8711 set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8712 store_type(tmp);
8713 return ret;
8714 }
8715
8716
store(longlong nr,bool unsigned_val)8717 type_conversion_status Field_set::store(longlong nr, bool unsigned_val)
8718 {
8719 ASSERT_COLUMN_MARKED_FOR_WRITE;
8720 type_conversion_status error= TYPE_OK;
8721 ulonglong max_nr;
8722
8723 if (sizeof(ulonglong)*8 <= typelib->count)
8724 max_nr= ULONGLONG_MAX;
8725 else
8726 max_nr= (ULL(1) << typelib->count) - 1;
8727
8728 if ((ulonglong) nr > max_nr)
8729 {
8730 nr&= max_nr;
8731 set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
8732 error= TYPE_WARN_TRUNCATED;
8733 }
8734 store_type((ulonglong) nr);
8735 return error;
8736 }
8737
8738
val_str(String * val_buffer,String * val_ptr MY_ATTRIBUTE ((unused)))8739 String *Field_set::val_str(String *val_buffer,
8740 String *val_ptr MY_ATTRIBUTE((unused)))
8741 {
8742 ulonglong tmp=(ulonglong) Field_enum::val_int();
8743 uint bitnr=0;
8744
8745 /*
8746 Some callers expect *val_buffer to contain the result,
8747 so we assign to it, rather than doing 'return &empty_set_string.
8748 */
8749 *val_buffer= empty_set_string;
8750 if (tmp == 0)
8751 {
8752 return val_buffer;
8753 }
8754
8755 val_buffer->set_charset(field_charset);
8756 val_buffer->length(0);
8757
8758 while (tmp && bitnr < (uint) typelib->count)
8759 {
8760 if (tmp & 1)
8761 {
8762 if (val_buffer->length())
8763 val_buffer->append(&field_separator, 1, &my_charset_latin1);
8764 String str(typelib->type_names[bitnr],
8765 typelib->type_lengths[bitnr],
8766 field_charset);
8767 val_buffer->append(str);
8768 }
8769 tmp>>=1;
8770 bitnr++;
8771 }
8772 return val_buffer;
8773 }
8774
8775
sql_type(String & res) const8776 void Field_set::sql_type(String &res) const
8777 {
8778 char buffer[255];
8779 String set_item(buffer, sizeof(buffer), res.charset());
8780
8781 res.length(0);
8782 res.append(STRING_WITH_LEN("set("));
8783
8784 bool flag=0;
8785 uint *len= typelib->type_lengths;
8786 for (const char **pos= typelib->type_names; *pos; pos++, len++)
8787 {
8788 uint dummy_errors;
8789 if (flag)
8790 res.append(',');
8791 /* convert to res.charset() == utf8, then quote */
8792 set_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
8793 append_unescaped(&res, set_item.ptr(), set_item.length());
8794 flag= 1;
8795 }
8796 res.append(')');
8797 }
8798
8799 /**
8800 @retval
8801 1 if the fields are equally defined
8802 @retval
8803 0 if the fields are unequally defined
8804 */
8805
eq_def(Field * field)8806 bool Field::eq_def(Field *field)
8807 {
8808 if (real_type() != field->real_type() || charset() != field->charset() ||
8809 pack_length() != field->pack_length())
8810 return 0;
8811 return 1;
8812 }
8813
8814
8815 /**
8816 Compare the first t1::count type names.
8817
8818 @return TRUE if the type names of t1 match those of t2. FALSE otherwise.
8819 */
8820
compare_type_names(const CHARSET_INFO * charset,TYPELIB * t1,TYPELIB * t2)8821 static bool compare_type_names(const CHARSET_INFO *charset,
8822 TYPELIB *t1, TYPELIB *t2)
8823 {
8824 for (uint i= 0; i < t1->count; i++)
8825 if (my_strnncoll(charset,
8826 (const uchar*) t1->type_names[i],
8827 t1->type_lengths[i],
8828 (const uchar*) t2->type_names[i],
8829 t2->type_lengths[i]))
8830 return FALSE;
8831 return TRUE;
8832 }
8833
8834 /**
8835 @return
8836 returns 1 if the fields are equally defined
8837 */
8838
eq_def(Field * field)8839 bool Field_enum::eq_def(Field *field)
8840 {
8841 TYPELIB *values;
8842
8843 if (!Field::eq_def(field))
8844 return FALSE;
8845
8846 values= ((Field_enum*) field)->typelib;
8847
8848 /* Definition must be strictly equal. */
8849 if (typelib->count != values->count)
8850 return FALSE;
8851
8852 return compare_type_names(field_charset, typelib, values);
8853 }
8854
8855
8856 /**
8857 Check whether two fields can be considered 'equal' for table
8858 alteration purposes. Fields are equal if they retain the same
8859 pack length and if new members are added to the end of the list.
8860
8861 @return IS_EQUAL_YES if fields are compatible.
8862 IS_EQUAL_NO otherwise.
8863 */
8864
is_equal(Create_field * new_field)8865 uint Field_enum::is_equal(Create_field *new_field)
8866 {
8867 TYPELIB *values= new_field->interval;
8868
8869 /*
8870 The fields are compatible if they have the same flags,
8871 type, charset and have the same underlying length.
8872 */
8873 if (new_field->sql_type != real_type() ||
8874 new_field->charset != field_charset ||
8875 new_field->pack_length != pack_length())
8876 return IS_EQUAL_NO;
8877
8878 /*
8879 Changing the definition of an ENUM or SET column by adding a new
8880 enumeration or set members to the end of the list of valid member
8881 values only alters table metadata and not table data.
8882 */
8883 if (typelib->count > values->count)
8884 return IS_EQUAL_NO;
8885
8886 /* Check whether there are modification before the end. */
8887 if (! compare_type_names(field_charset, typelib, new_field->interval))
8888 return IS_EQUAL_NO;
8889
8890 return IS_EQUAL_YES;
8891 }
8892
8893
pack(uchar * to,const uchar * from,uint max_length,bool low_byte_first)8894 uchar *Field_enum::pack(uchar *to, const uchar *from,
8895 uint max_length, bool low_byte_first)
8896 {
8897 DBUG_ENTER("Field_enum::pack");
8898 DBUG_PRINT("debug", ("packlength: %d", packlength));
8899 DBUG_DUMP("from", from, packlength);
8900
8901 switch (packlength)
8902 {
8903 case 1:
8904 if (max_length > 0) *to = *from;
8905 DBUG_RETURN(to + 1);
8906 case 2: DBUG_RETURN(pack_int16(to, from, low_byte_first));
8907 case 3: DBUG_RETURN(pack_int24(to, from, low_byte_first));
8908 case 4: DBUG_RETURN(pack_int32(to, from, low_byte_first));
8909 case 8: DBUG_RETURN(pack_int64(to, from, low_byte_first));
8910 default:
8911 DBUG_ASSERT(0);
8912 }
8913 MY_ASSERT_UNREACHABLE();
8914 DBUG_RETURN(NULL);
8915 }
8916
unpack(uchar * to,const uchar * from,uint param_data,bool low_byte_first)8917 const uchar *Field_enum::unpack(uchar *to, const uchar *from,
8918 uint param_data, bool low_byte_first)
8919 {
8920 DBUG_ENTER("Field_enum::unpack");
8921 DBUG_PRINT("debug", ("packlength: %d", packlength));
8922 DBUG_DUMP("from", from, packlength);
8923
8924 switch (packlength)
8925 {
8926 case 1:
8927 *to = *from;
8928 DBUG_RETURN(from + 1);
8929
8930 case 2: DBUG_RETURN(unpack_int16(to, from, low_byte_first));
8931 case 3: DBUG_RETURN(unpack_int24(to, from, low_byte_first));
8932 case 4: DBUG_RETURN(unpack_int32(to, from, low_byte_first));
8933 case 8: DBUG_RETURN(unpack_int64(to, from, low_byte_first));
8934 default:
8935 DBUG_ASSERT(0);
8936 }
8937 MY_ASSERT_UNREACHABLE();
8938 DBUG_RETURN(NULL);
8939 }
8940
8941
8942 /**
8943 @return
8944 returns 1 if the fields are equally defined
8945 */
eq_def(Field * field)8946 bool Field_num::eq_def(Field *field)
8947 {
8948 if (!Field::eq_def(field))
8949 return 0;
8950 Field_num *from_num= (Field_num*) field;
8951
8952 if (unsigned_flag != from_num->unsigned_flag ||
8953 (zerofill && !from_num->zerofill && !zero_pack()) ||
8954 dec != from_num->dec)
8955 return 0;
8956 return 1;
8957 }
8958
8959
8960 /**
8961 Check whether two numeric fields can be considered 'equal' for table
8962 alteration purposes. Fields are equal if they are of the same type
8963 and retain the same pack length.
8964 */
8965
is_equal(Create_field * new_field)8966 uint Field_num::is_equal(Create_field *new_field)
8967 {
8968 return ((new_field->sql_type == real_type()) &&
8969 ((new_field->flags & UNSIGNED_FLAG) ==
8970 (uint) (flags & UNSIGNED_FLAG)) &&
8971 ((new_field->flags & AUTO_INCREMENT_FLAG) ==
8972 (uint) (flags & AUTO_INCREMENT_FLAG)) &&
8973 (new_field->pack_length == pack_length()));
8974 }
8975
8976
8977 /*
8978 Bit field.
8979
8980 We store the first 0 - 6 uneven bits among the null bits
8981 at the start of the record. The rest bytes are stored in
8982 the record itself.
8983
8984 For example:
8985
8986 CREATE TABLE t1 (a int, b bit(17), c bit(21) not null, d bit(8));
8987 We would store data as follows in the record:
8988
8989 Byte Bit
8990 1 7 - reserve for delete
8991 6 - null bit for 'a'
8992 5 - null bit for 'b'
8993 4 - first (high) bit of 'b'
8994 3 - first (high) bit of 'c'
8995 2 - second bit of 'c'
8996 1 - third bit of 'c'
8997 0 - forth bit of 'c'
8998 2 7 - firth bit of 'c'
8999 6 - null bit for 'd'
9000 3 - 6 four bytes for 'a'
9001 7 - 8 two bytes for 'b'
9002 9 - 10 two bytes for 'c'
9003 11 one byte for 'd'
9004 */
9005
Field_bit(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,uchar * bit_ptr_arg,uchar bit_ofs_arg,enum utype unireg_check_arg,const char * field_name_arg)9006 Field_bit::Field_bit(uchar *ptr_arg, uint32 len_arg, uchar *null_ptr_arg,
9007 uchar null_bit_arg, uchar *bit_ptr_arg, uchar bit_ofs_arg,
9008 enum utype unireg_check_arg, const char *field_name_arg)
9009 : Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
9010 unireg_check_arg, field_name_arg),
9011 bit_ptr(bit_ptr_arg), bit_ofs(bit_ofs_arg), bit_len(len_arg & 7),
9012 bytes_in_rec(len_arg / 8)
9013 {
9014 DBUG_ENTER("Field_bit::Field_bit");
9015 DBUG_PRINT("enter", ("ptr_arg: %p, null_ptr_arg: %p, len_arg: %u, bit_len: %u, bytes_in_rec: %u",
9016 ptr_arg, null_ptr_arg, len_arg, bit_len, bytes_in_rec));
9017 flags|= UNSIGNED_FLAG;
9018 /*
9019 Ensure that Field::eq() can distinguish between two different bit fields.
9020 (two bit fields that are not null, may have same ptr and null_ptr)
9021 */
9022 if (!null_ptr_arg)
9023 null_bit= bit_ofs_arg;
9024 DBUG_VOID_RETURN;
9025 }
9026
9027
hash(ulong * nr,ulong * nr2)9028 void Field_bit::hash(ulong *nr, ulong *nr2)
9029 {
9030 if (is_null())
9031 {
9032 *nr^= (*nr << 1) | 1;
9033 }
9034 else
9035 {
9036 const CHARSET_INFO *cs= &my_charset_bin;
9037 longlong value= Field_bit::val_int();
9038 uchar tmp[8];
9039 mi_int8store(tmp,value);
9040 cs->coll->hash_sort(cs, tmp, 8, nr, nr2);
9041 }
9042 }
9043
9044
9045 size_t
do_last_null_byte() const9046 Field_bit::do_last_null_byte() const
9047 {
9048 /*
9049 Code elsewhere is assuming that bytes are 8 bits, so I'm using
9050 that value instead of the correct one: CHAR_BIT.
9051
9052 REFACTOR SUGGESTION (Matz): Change to use the correct number of
9053 bits. On systems with CHAR_BIT > 8 (not very common), the storage
9054 will lose the extra bits.
9055 */
9056 DBUG_PRINT("test", ("bit_ofs: %d, bit_len: %d bit_ptr: 0x%lx",
9057 bit_ofs, bit_len, (long) bit_ptr));
9058 uchar *result;
9059 if (bit_len == 0)
9060 result= null_ptr;
9061 else if (bit_ofs + bit_len > 8)
9062 result= bit_ptr + 1;
9063 else
9064 result= bit_ptr;
9065
9066 if (result)
9067 return (size_t) (result - table->record[0]) + 1;
9068 return LAST_NULL_BYTE_UNDEF;
9069 }
9070
9071
new_key_field(MEM_ROOT * root,TABLE * new_table,uchar * new_ptr,uchar * new_null_ptr,uint new_null_bit)9072 Field *Field_bit::new_key_field(MEM_ROOT *root,
9073 TABLE *new_table,
9074 uchar *new_ptr, uchar *new_null_ptr,
9075 uint new_null_bit)
9076 {
9077 Field_bit *res;
9078 if ((res= (Field_bit*) Field::new_key_field(root, new_table,
9079 new_ptr, new_null_ptr,
9080 new_null_bit)))
9081 {
9082 /* Move bits normally stored in null_pointer to new_ptr */
9083 res->bit_ptr= new_ptr;
9084 res->bit_ofs= 0;
9085 if (bit_len)
9086 res->ptr++; // Store rest of data here
9087 }
9088 return res;
9089 }
9090
9091
is_equal(Create_field * new_field)9092 uint Field_bit::is_equal(Create_field *new_field)
9093 {
9094 return (new_field->sql_type == real_type() &&
9095 new_field->length == max_display_length());
9096 }
9097
9098
9099 type_conversion_status
store(const char * from,uint length,const CHARSET_INFO * cs)9100 Field_bit::store(const char *from, uint length, const CHARSET_INFO *cs)
9101 {
9102 ASSERT_COLUMN_MARKED_FOR_WRITE;
9103 int delta;
9104
9105 for (; length && !*from; from++, length--) ; // skip left 0's
9106 delta= bytes_in_rec - length;
9107
9108 /*
9109 *from should probably be treated like uint here see BUG#13727586
9110 */
9111 if (delta < -1 ||
9112 (delta == -1 && (uchar) *from > ((1 << bit_len) - 1)) ||
9113 (!bit_len && delta < 0))
9114 {
9115 set_rec_bits((1 << bit_len) - 1, bit_ptr, bit_ofs, bit_len);
9116 memset(ptr, 0xff, bytes_in_rec);
9117 if (table->in_use->really_abort_on_warning())
9118 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_DATA_TOO_LONG, 1);
9119 else
9120 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
9121 return TYPE_WARN_OUT_OF_RANGE;
9122 }
9123 /* delta is >= -1 here */
9124 if (delta > 0)
9125 {
9126 if (bit_len)
9127 clr_rec_bits(bit_ptr, bit_ofs, bit_len);
9128 memset(ptr, 0, delta);
9129 memcpy(ptr + delta, from, length);
9130 }
9131 else if (delta == 0)
9132 {
9133 if (bit_len)
9134 clr_rec_bits(bit_ptr, bit_ofs, bit_len);
9135 memcpy(ptr, from, length);
9136 }
9137 else
9138 {
9139 if (bit_len)
9140 {
9141 set_rec_bits((uchar) *from, bit_ptr, bit_ofs, bit_len);
9142 from++;
9143 }
9144 memcpy(ptr, from, bytes_in_rec);
9145 }
9146 return TYPE_OK;
9147 }
9148
9149
store(double nr)9150 type_conversion_status Field_bit::store(double nr)
9151 {
9152 return Field_bit::store((longlong) nr, FALSE);
9153 }
9154
9155
store(longlong nr,bool unsigned_val)9156 type_conversion_status Field_bit::store(longlong nr, bool unsigned_val)
9157 {
9158 char buf[8];
9159
9160 mi_int8store(buf, nr);
9161 return store(buf, 8, NULL);
9162 }
9163
9164
store_decimal(const my_decimal * val)9165 type_conversion_status Field_bit::store_decimal(const my_decimal *val)
9166 {
9167 bool has_overflow= false;
9168 longlong i= convert_decimal2longlong(val, 1, &has_overflow);
9169 type_conversion_status res= store(i, TRUE);
9170 return has_overflow ? TYPE_WARN_OUT_OF_RANGE : res;
9171 }
9172
9173
val_real(void)9174 double Field_bit::val_real(void)
9175 {
9176 return (double) Field_bit::val_int();
9177 }
9178
9179
val_int(void)9180 longlong Field_bit::val_int(void)
9181 {
9182 ASSERT_COLUMN_MARKED_FOR_READ;
9183 ulonglong bits= 0;
9184 if (bit_len)
9185 {
9186 bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
9187 bits<<= (bytes_in_rec * 8);
9188 }
9189
9190 switch (bytes_in_rec) {
9191 case 0: return bits;
9192 case 1: return bits | (ulonglong) ptr[0];
9193 case 2: return bits | mi_uint2korr(ptr);
9194 case 3: return bits | mi_uint3korr(ptr);
9195 case 4: return bits | mi_uint4korr(ptr);
9196 case 5: return bits | mi_uint5korr(ptr);
9197 case 6: return bits | mi_uint6korr(ptr);
9198 case 7: return bits | mi_uint7korr(ptr);
9199 default: return mi_uint8korr(ptr + bytes_in_rec - sizeof(longlong));
9200 }
9201 }
9202
9203
val_str(String * val_buffer,String * val_ptr MY_ATTRIBUTE ((unused)))9204 String *Field_bit::val_str(String *val_buffer,
9205 String *val_ptr MY_ATTRIBUTE((unused)))
9206 {
9207 ASSERT_COLUMN_MARKED_FOR_READ;
9208 char buff[sizeof(longlong)];
9209 uint length= min<uint>(pack_length(), sizeof(longlong));
9210 ulonglong bits= val_int();
9211 mi_int8store(buff,bits);
9212
9213 val_buffer->alloc(length);
9214 memcpy((char *) val_buffer->ptr(), buff+8-length, length);
9215 val_buffer->length(length);
9216 val_buffer->set_charset(&my_charset_bin);
9217 return val_buffer;
9218 }
9219
9220
val_decimal(my_decimal * deciaml_value)9221 my_decimal *Field_bit::val_decimal(my_decimal *deciaml_value)
9222 {
9223 ASSERT_COLUMN_MARKED_FOR_READ;
9224 int2my_decimal(E_DEC_FATAL_ERROR, val_int(), 1, deciaml_value);
9225 return deciaml_value;
9226 }
9227
9228
9229 /*
9230 Compare two bit fields using pointers within the record.
9231 SYNOPSIS
9232 cmp_max()
9233 a Pointer to field->ptr in first record
9234 b Pointer to field->ptr in second record
9235 max_len Maximum length used in index
9236 DESCRIPTION
9237 This method is used from key_rec_cmp used by merge sorts used
9238 by partitioned index read and later other similar places.
9239 The a and b pointer must be pointers to the field in a record
9240 (not the table->record[0] necessarily)
9241 */
cmp_max(const uchar * a,const uchar * b,uint max_len)9242 int Field_bit::cmp_max(const uchar *a, const uchar *b, uint max_len)
9243 {
9244 my_ptrdiff_t a_diff= a - ptr;
9245 my_ptrdiff_t b_diff= b - ptr;
9246 if (bit_len)
9247 {
9248 int flag;
9249 uchar bits_a= get_rec_bits(bit_ptr+a_diff, bit_ofs, bit_len);
9250 uchar bits_b= get_rec_bits(bit_ptr+b_diff, bit_ofs, bit_len);
9251 if ((flag= (int) (bits_a - bits_b)))
9252 return flag;
9253 }
9254 return memcmp(a, b, pack_length());
9255 }
9256
9257
key_cmp(const uchar * str,uint length)9258 int Field_bit::key_cmp(const uchar *str, uint length)
9259 {
9260 if (bit_len)
9261 {
9262 int flag;
9263 uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
9264 if ((flag= (int) (bits - *str)))
9265 return flag;
9266 str++;
9267 length--;
9268 }
9269 return memcmp(ptr, str, length);
9270 }
9271
9272
cmp_offset(uint row_offset)9273 int Field_bit::cmp_offset(uint row_offset)
9274 {
9275 if (bit_len)
9276 {
9277 int flag;
9278 uchar bits_a= get_rec_bits(bit_ptr, bit_ofs, bit_len);
9279 uchar bits_b= get_rec_bits(bit_ptr + row_offset, bit_ofs, bit_len);
9280 if ((flag= (int) (bits_a - bits_b)))
9281 return flag;
9282 }
9283 return memcmp(ptr, ptr + row_offset, bytes_in_rec);
9284 }
9285
9286
get_key_image(uchar * buff,uint length,imagetype type_arg)9287 uint Field_bit::get_key_image(uchar *buff, uint length, imagetype type_arg)
9288 {
9289 if (bit_len)
9290 {
9291 uchar bits= get_rec_bits(bit_ptr, bit_ofs, bit_len);
9292 *buff++= bits;
9293 length--;
9294 }
9295 uint data_length = min(length, bytes_in_rec);
9296 memcpy(buff, ptr, data_length);
9297 return data_length + 1;
9298 }
9299
9300
9301 /**
9302 Save the field metadata for bit fields.
9303
9304 Saves the bit length in the first byte and bytes in record in the
9305 second byte of the field metadata array at index of *metadata_ptr and
9306 *(metadata_ptr + 1).
9307
9308 @param metadata_ptr First byte of field metadata
9309
9310 @returns number of bytes written to metadata_ptr
9311 */
do_save_field_metadata(uchar * metadata_ptr)9312 int Field_bit::do_save_field_metadata(uchar *metadata_ptr)
9313 {
9314 DBUG_ENTER("Field_bit::do_save_field_metadata");
9315 DBUG_PRINT("debug", ("bit_len: %d, bytes_in_rec: %d",
9316 bit_len, bytes_in_rec));
9317 /*
9318 Since this class and Field_bit_as_char have different ideas of
9319 what should be stored here, we compute the values of the metadata
9320 explicitly using the field_length.
9321 */
9322 metadata_ptr[0]= field_length % 8;
9323 metadata_ptr[1]= field_length / 8;
9324 DBUG_RETURN(2);
9325 }
9326
9327
9328 /**
9329 Returns the number of bytes field uses in row-based replication
9330 row packed size.
9331
9332 This method is used in row-based replication to determine the number
9333 of bytes that the field consumes in the row record format. This is
9334 used to skip fields in the master that do not exist on the slave.
9335
9336 @param field_metadata Encoded size in field metadata
9337
9338 @returns The size of the field based on the field metadata.
9339 */
pack_length_from_metadata(uint field_metadata)9340 uint Field_bit::pack_length_from_metadata(uint field_metadata)
9341 {
9342 uint const from_len= (field_metadata >> 8U) & 0x00ff;
9343 uint const from_bit_len= field_metadata & 0x00ff;
9344 uint const source_size= from_len + ((from_bit_len > 0) ? 1 : 0);
9345 return (source_size);
9346 }
9347
9348 /**
9349 Check to see if field size is compatible with destination.
9350
9351 This method is used in row-based replication to verify that the slave's
9352 field size is less than or equal to the master's field size. The
9353 encoded field metadata (from the master or source) is decoded and compared
9354 to the size of this field (the slave or destination).
9355
9356 @param field_metadata Encoded size in field metadata
9357 @param order_var Pointer to variable where the order
9358 between the source field and this field
9359 will be returned.
9360
9361 @return @c true
9362 */
9363 bool
compatible_field_size(uint field_metadata,Relay_log_info * MY_ATTRIBUTE ((unused)),uint16 mflags,int * order_var)9364 Field_bit::compatible_field_size(uint field_metadata,
9365 Relay_log_info * MY_ATTRIBUTE((unused)),
9366 uint16 mflags,
9367 int *order_var)
9368 {
9369 DBUG_ENTER("Field_bit::compatible_field_size");
9370 DBUG_ASSERT((field_metadata >> 16) == 0);
9371 uint from_bit_len= 8 * (field_metadata >> 8) + (field_metadata & 0xff);
9372 uint to_bit_len= max_display_length();
9373 DBUG_PRINT("debug", ("from_bit_len: %u, to_bit_len: %u",
9374 from_bit_len, to_bit_len));
9375 /*
9376 If the bit length exact flag is clear, we are dealing with an old
9377 master, so we allow some less strict behaviour if replicating by
9378 moving both bit lengths to an even multiple of 8.
9379
9380 We do this by computing the number of bytes to store the field
9381 instead, and then compare the result.
9382 */
9383 if (!(mflags & Table_map_log_event::TM_BIT_LEN_EXACT_F)) {
9384 from_bit_len= (from_bit_len + 7) / 8;
9385 to_bit_len= (to_bit_len + 7) / 8;
9386 }
9387
9388 *order_var= compare(from_bit_len, to_bit_len);
9389 DBUG_RETURN(TRUE);
9390 }
9391
9392
9393
sql_type(String & res) const9394 void Field_bit::sql_type(String &res) const
9395 {
9396 const CHARSET_INFO *cs= res.charset();
9397 ulong length= cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
9398 "bit(%d)", (int) field_length);
9399 res.length((uint) length);
9400 }
9401
9402
9403 uchar *
pack(uchar * to,const uchar * from,uint max_length,bool low_byte_first MY_ATTRIBUTE ((unused)))9404 Field_bit::pack(uchar *to, const uchar *from, uint max_length,
9405 bool low_byte_first MY_ATTRIBUTE((unused)))
9406 {
9407 DBUG_ASSERT(max_length > 0);
9408 uint length;
9409 if (bit_len > 0)
9410 {
9411 /*
9412 We have the following:
9413
9414 ptr Points into a field in record R1
9415 from Points to a field in a record R2
9416 bit_ptr Points to the byte (in the null bytes) that holds the
9417 odd bits of R1
9418 from_bitp Points to the byte that holds the odd bits of R2
9419
9420 We have the following:
9421
9422 ptr - bit_ptr = from - from_bitp
9423
9424 We want to isolate 'from_bitp', so this gives:
9425
9426 ptr - bit_ptr - from = - from_bitp
9427 - ptr + bit_ptr + from = from_bitp
9428 bit_ptr + from - ptr = from_bitp
9429 */
9430 uchar bits= get_rec_bits(bit_ptr + (from - ptr), bit_ofs, bit_len);
9431 *to++= bits;
9432 }
9433 length= min(bytes_in_rec, max_length - (bit_len > 0));
9434 memcpy(to, from, length);
9435 return to + length;
9436 }
9437
9438
9439 /**
9440 Unpack a bit field from row data.
9441
9442 This method is used to unpack a bit field from a master whose size
9443 of the field is less than that of the slave.
9444
9445 @param to Destination of the data
9446 @param from Source of the data
9447 @param param_data Bit length (upper) and length (lower) values
9448
9449 @return New pointer into memory based on from + length of the data
9450 */
9451 const uchar *
unpack(uchar * to,const uchar * from,uint param_data,bool low_byte_first MY_ATTRIBUTE ((unused)))9452 Field_bit::unpack(uchar *to, const uchar *from, uint param_data,
9453 bool low_byte_first MY_ATTRIBUTE((unused)))
9454 {
9455 DBUG_ENTER("Field_bit::unpack");
9456 DBUG_PRINT("enter", ("to: %p, from: %p, param_data: 0x%x",
9457 to, from, param_data));
9458 DBUG_PRINT("debug", ("bit_ptr: %p, bit_len: %u, bit_ofs: %u",
9459 bit_ptr, bit_len, bit_ofs));
9460 uint const from_len= (param_data >> 8U) & 0x00ff;
9461 uint const from_bit_len= param_data & 0x00ff;
9462 DBUG_PRINT("debug", ("from_len: %u, from_bit_len: %u",
9463 from_len, from_bit_len));
9464 /*
9465 If the parameter data is zero (i.e., undefined), or if the master
9466 and slave have the same sizes, then use the old unpack() method.
9467 */
9468 if (param_data == 0 ||
9469 ((from_bit_len == bit_len) && (from_len == bytes_in_rec)))
9470 {
9471 if (bit_len > 0)
9472 {
9473 /*
9474 set_rec_bits is a macro, don't put the post-increment in the
9475 argument since that might cause strange side-effects.
9476
9477 For the choice of the second argument, see the explanation for
9478 Field_bit::pack().
9479 */
9480 set_rec_bits(*from, bit_ptr + (to - ptr), bit_ofs, bit_len);
9481 from++;
9482 }
9483 memcpy(to, from, bytes_in_rec);
9484 DBUG_RETURN(from + bytes_in_rec);
9485 }
9486
9487 /*
9488 We are converting a smaller bit field to a larger one here.
9489 To do that, we first need to construct a raw value for the original
9490 bit value stored in the from buffer. Then that needs to be converted
9491 to the larger field then sent to store() for writing to the field.
9492 Lastly the odd bits need to be masked out if the bytes_in_rec > 0.
9493 Otherwise stray bits can cause spurious values.
9494 */
9495 uint new_len= (field_length + 7) / 8;
9496 char *value= (char *)my_alloca(new_len);
9497 memset(value, 0, new_len);
9498 uint len= from_len + ((from_bit_len > 0) ? 1 : 0);
9499 memcpy(value + (new_len - len), from, len);
9500 /*
9501 Mask out the unused bits in the partial byte.
9502 TODO: Add code to the master to always mask these bits and remove
9503 the following.
9504 */
9505 if ((from_bit_len > 0) && (from_len > 0))
9506 value[new_len - len]= value[new_len - len] & ((1U << from_bit_len) - 1);
9507 bitmap_set_bit(table->write_set,field_index);
9508 store(value, new_len, system_charset_info);
9509 my_afree(value);
9510 DBUG_RETURN(from + len);
9511 }
9512
9513
set_default()9514 void Field_bit::set_default()
9515 {
9516 if (bit_len > 0)
9517 {
9518 my_ptrdiff_t const offset= table->s->default_values - table->record[0];
9519 uchar bits= get_rec_bits(bit_ptr + offset, bit_ofs, bit_len);
9520 set_rec_bits(bits, bit_ptr, bit_ofs, bit_len);
9521 }
9522 Field::set_default();
9523 }
9524
9525 /*
9526 Bit field support for non-MyISAM tables.
9527 */
9528
Field_bit_as_char(uchar * ptr_arg,uint32 len_arg,uchar * null_ptr_arg,uchar null_bit_arg,enum utype unireg_check_arg,const char * field_name_arg)9529 Field_bit_as_char::Field_bit_as_char(uchar *ptr_arg, uint32 len_arg,
9530 uchar *null_ptr_arg, uchar null_bit_arg,
9531 enum utype unireg_check_arg,
9532 const char *field_name_arg)
9533 :Field_bit(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, 0, 0,
9534 unireg_check_arg, field_name_arg)
9535 {
9536 flags|= UNSIGNED_FLAG;
9537 bit_len= 0;
9538 bytes_in_rec= (len_arg + 7) / 8;
9539 }
9540
9541
store(const char * from,uint length,const CHARSET_INFO * cs)9542 type_conversion_status Field_bit_as_char::store(const char *from, uint length,
9543 const CHARSET_INFO *cs)
9544 {
9545 ASSERT_COLUMN_MARKED_FOR_WRITE;
9546 int delta;
9547 uchar bits= (uchar) (field_length & 7);
9548
9549 for (; length && !*from; from++, length--) ; // skip left 0's
9550 delta= bytes_in_rec - length;
9551
9552 if (delta < 0 ||
9553 (delta == 0 && bits && (uint) (uchar) *from >= (uint) (1 << bits)))
9554 {
9555 memset(ptr, 0xff, bytes_in_rec);
9556 if (bits)
9557 *ptr&= ((1 << bits) - 1); /* set first uchar */
9558 if (table->in_use->really_abort_on_warning())
9559 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_DATA_TOO_LONG, 1);
9560 else
9561 set_warning(Sql_condition::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
9562 return TYPE_WARN_OUT_OF_RANGE;
9563 }
9564 memset(ptr, 0, delta);
9565 memcpy(ptr + delta, from, length);
9566 return TYPE_OK;
9567 }
9568
9569
sql_type(String & res) const9570 void Field_bit_as_char::sql_type(String &res) const
9571 {
9572 const CHARSET_INFO *cs= res.charset();
9573 ulong length= cs->cset->snprintf(cs, (char*) res.ptr(), res.alloced_length(),
9574 "bit(%d)", (int) field_length);
9575 res.length((uint) length);
9576 }
9577
9578
9579 /*****************************************************************************
9580 Handling of field and Create_field
9581 *****************************************************************************/
9582
9583 /**
9584 Convert create_field::length from number of characters to number of bytes.
9585 */
9586
create_length_to_internal_length(void)9587 void Create_field::create_length_to_internal_length(void)
9588 {
9589 switch (sql_type) {
9590 case MYSQL_TYPE_TINY_BLOB:
9591 case MYSQL_TYPE_MEDIUM_BLOB:
9592 case MYSQL_TYPE_LONG_BLOB:
9593 case MYSQL_TYPE_BLOB:
9594 case MYSQL_TYPE_GEOMETRY:
9595 case MYSQL_TYPE_VAR_STRING:
9596 case MYSQL_TYPE_STRING:
9597 case MYSQL_TYPE_VARCHAR:
9598 length*= charset->mbmaxlen;
9599 key_length= length;
9600 pack_length= calc_pack_length(sql_type, length);
9601 break;
9602 case MYSQL_TYPE_ENUM:
9603 case MYSQL_TYPE_SET:
9604 /* Pack_length already calculated in sql_parse.cc */
9605 length*= charset->mbmaxlen;
9606 key_length= pack_length;
9607 break;
9608 case MYSQL_TYPE_BIT:
9609 if (f_bit_as_char(pack_flag))
9610 {
9611 key_length= pack_length= ((length + 7) & ~7) / 8;
9612 }
9613 else
9614 {
9615 pack_length= length / 8;
9616 /* We need one extra byte to store the bits we save among the null bits */
9617 key_length= pack_length + MY_TEST(length & 7);
9618 }
9619 break;
9620 case MYSQL_TYPE_NEWDECIMAL:
9621 key_length= pack_length=
9622 my_decimal_get_binary_size(my_decimal_length_to_precision(length,
9623 decimals,
9624 flags &
9625 UNSIGNED_FLAG),
9626 decimals);
9627 break;
9628 default:
9629 key_length= pack_length= calc_pack_length(sql_type, length);
9630 break;
9631 }
9632 }
9633
9634
9635 /**
9636 Init for a tmp table field. To be extended if need be.
9637 */
init_for_tmp_table(enum_field_types sql_type_arg,uint32 length_arg,uint32 decimals_arg,bool maybe_null,bool is_unsigned,uint pack_length_arg)9638 void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
9639 uint32 length_arg, uint32 decimals_arg,
9640 bool maybe_null, bool is_unsigned,
9641 uint pack_length_arg)
9642 {
9643 DBUG_ENTER("Create_field::init_for_tmp_table");
9644
9645 field_name= "";
9646 sql_type= sql_type_arg;
9647 char_length= length= length_arg;;
9648 unireg_check= Field::NONE;
9649 interval= 0;
9650 charset= &my_charset_bin;
9651 geom_type= Field::GEOM_GEOMETRY;
9652
9653 DBUG_PRINT("enter", ("sql_type: %d, length: %u, pack_length: %u",
9654 sql_type_arg, length_arg, pack_length_arg));
9655
9656 /*
9657 These pack flags are crafted to get it correctly through the
9658 branches of make_field().
9659 */
9660 switch (sql_type_arg)
9661 {
9662 case MYSQL_TYPE_VARCHAR:
9663 case MYSQL_TYPE_VAR_STRING:
9664 case MYSQL_TYPE_STRING:
9665 case MYSQL_TYPE_SET:
9666 pack_flag= 0;
9667 break;
9668
9669 case MYSQL_TYPE_GEOMETRY:
9670 pack_flag= FIELDFLAG_GEOM;
9671 break;
9672
9673 case MYSQL_TYPE_ENUM:
9674 pack_flag= FIELDFLAG_INTERVAL;
9675 break;
9676
9677 case MYSQL_TYPE_NEWDECIMAL:
9678 DBUG_ASSERT(decimals_arg <= DECIMAL_MAX_SCALE);
9679 case MYSQL_TYPE_DECIMAL:
9680 case MYSQL_TYPE_FLOAT:
9681 case MYSQL_TYPE_DOUBLE:
9682 pack_flag= FIELDFLAG_NUMBER |
9683 (decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT;
9684 break;
9685
9686 case MYSQL_TYPE_TINY_BLOB:
9687 case MYSQL_TYPE_MEDIUM_BLOB:
9688 case MYSQL_TYPE_LONG_BLOB:
9689 case MYSQL_TYPE_BLOB:
9690 pack_flag= FIELDFLAG_BLOB;
9691 break;
9692
9693 case MYSQL_TYPE_BIT:
9694 pack_flag= FIELDFLAG_NUMBER | FIELDFLAG_TREAT_BIT_AS_CHAR;
9695 break;
9696
9697 default:
9698 pack_flag= FIELDFLAG_NUMBER;
9699 break;
9700 }
9701
9702 /*
9703 Set the pack flag correctly for the blob-like types. This sets the
9704 packtype to something that make_field can use. If the pack type is
9705 not set correctly, the packlength will be reeeeally wierd (like
9706 129 or so).
9707 */
9708 switch (sql_type_arg)
9709 {
9710 case MYSQL_TYPE_ENUM:
9711 case MYSQL_TYPE_SET:
9712 case MYSQL_TYPE_TINY_BLOB:
9713 case MYSQL_TYPE_MEDIUM_BLOB:
9714 case MYSQL_TYPE_LONG_BLOB:
9715 case MYSQL_TYPE_BLOB:
9716 case MYSQL_TYPE_GEOMETRY:
9717 // If you are going to use the above types, you have to pass a
9718 // pack_length as parameter. Assert that is really done.
9719 DBUG_ASSERT(pack_length_arg != ~0U);
9720 pack_flag|= pack_length_to_packflag(pack_length_arg);
9721 break;
9722 default:
9723 /* Nothing */
9724 break;
9725 }
9726
9727 pack_flag|=
9728 (maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
9729 (is_unsigned ? 0 : FIELDFLAG_DECIMAL);
9730
9731 DBUG_PRINT("debug", ("pack_flag: %s%s%s%s%s%s, pack_type: %d",
9732 FLAGSTR(pack_flag, FIELDFLAG_BINARY),
9733 FLAGSTR(pack_flag, FIELDFLAG_NUMBER),
9734 FLAGSTR(pack_flag, FIELDFLAG_INTERVAL),
9735 FLAGSTR(pack_flag, FIELDFLAG_GEOM),
9736 FLAGSTR(pack_flag, FIELDFLAG_BLOB),
9737 FLAGSTR(pack_flag, FIELDFLAG_DECIMAL),
9738 f_packtype(pack_flag)));
9739 DBUG_VOID_RETURN;
9740 }
9741
9742
9743 /**
9744 Initialize a column definition object. Column definition objects can be used
9745 to construct Field objects.
9746
9747 @param thd Session/Thread handle.
9748 @param fld_name Column name.
9749 @param fld_type Column type.
9750 @param fld_length Column length.
9751 @param fld_decimals Number of digits to the right of the decimal
9752 point (if any.)
9753 @param fld_type_modifier Additional type information.
9754 @param fld_default_value Column default expression (if any.)
9755 @param fld_on_update_value The expression in the ON UPDATE clause.
9756 @param fld_comment Column comment.
9757 @param fld_change Column change.
9758 @param fld_interval_list Interval list (if any.)
9759 @param fld_charset Column charset.
9760 @param fld_geom_type Column geometry type (if any.)
9761 @param fld_zip_dict_name Column compression dictionary.
9762
9763 @retval
9764 FALSE on success.
9765 @retval
9766 TRUE on error.
9767 */
9768
init(THD * thd,const char * fld_name,enum_field_types fld_type,const char * fld_length,const char * fld_decimals,uint fld_type_modifier,Item * fld_default_value,Item * fld_on_update_value,LEX_STRING * fld_comment,const char * fld_change,List<String> * fld_interval_list,const CHARSET_INFO * fld_charset,uint fld_geom_type,const LEX_CSTRING * fld_zip_dict_name)9769 bool Create_field::init(THD *thd, const char *fld_name,
9770 enum_field_types fld_type, const char *fld_length,
9771 const char *fld_decimals, uint fld_type_modifier,
9772 Item *fld_default_value, Item *fld_on_update_value,
9773 LEX_STRING *fld_comment, const char *fld_change,
9774 List<String> *fld_interval_list,
9775 const CHARSET_INFO *fld_charset, uint fld_geom_type,
9776 const LEX_CSTRING *fld_zip_dict_name)
9777 {
9778 uint sign_len, allowed_type_modifier= 0;
9779 ulong max_field_charlength= MAX_FIELD_CHARLENGTH;
9780
9781 DBUG_ENTER("Create_field::init()");
9782
9783 field= 0;
9784 field_name= fld_name;
9785 flags= fld_type_modifier;
9786 charset= fld_charset;
9787
9788 const bool on_update_is_function=
9789 (fld_on_update_value != NULL &&
9790 fld_on_update_value->type() == Item::FUNC_ITEM);
9791
9792 if (fld_default_value != NULL && fld_default_value->type() == Item::FUNC_ITEM)
9793 {
9794 // We have a function default for insertions.
9795 def= NULL;
9796 unireg_check= on_update_is_function ?
9797 Field::TIMESTAMP_DNUN_FIELD : // for insertions and for updates.
9798 Field::TIMESTAMP_DN_FIELD; // only for insertions.
9799 }
9800 else
9801 {
9802 // No function default for insertions. Either NULL or a constant.
9803 def= fld_default_value;
9804 if (on_update_is_function)
9805 // We have a function default for updates only.
9806 unireg_check= Field::TIMESTAMP_UN_FIELD;
9807 else
9808 // No function defaults.
9809 unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG) != 0 ?
9810 Field::NEXT_NUMBER : // Automatic increment.
9811 Field::NONE;
9812 }
9813
9814 decimals= fld_decimals ? (uint)atoi(fld_decimals) : 0;
9815 if (is_temporal_real_type(fld_type))
9816 {
9817 flags|= BINARY_FLAG;
9818 charset= &my_charset_numeric;
9819 if (decimals > DATETIME_MAX_DECIMALS)
9820 {
9821 my_error(ER_TOO_BIG_PRECISION, MYF(0),
9822 decimals, fld_name, DATETIME_MAX_DECIMALS);
9823 DBUG_RETURN(TRUE);
9824 }
9825 }
9826 else if (decimals >= NOT_FIXED_DEC)
9827 {
9828 my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
9829 static_cast<ulong>(NOT_FIXED_DEC - 1));
9830 DBUG_RETURN(TRUE);
9831 }
9832
9833 sql_type= fld_type;
9834 length= 0;
9835 change= fld_change;
9836 interval= 0;
9837 pack_length= key_length= 0;
9838 geom_type= (Field::geometry_type) fld_geom_type;
9839 interval_list.empty();
9840
9841 comment= *fld_comment;
9842 /*
9843 Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
9844 it is NOT NULL and not an AUTO_INCREMENT field.
9845 */
9846 if (!fld_default_value &&
9847 (fld_type_modifier & NOT_NULL_FLAG) &&
9848 !(fld_type_modifier & AUTO_INCREMENT_FLAG))
9849 {
9850 /*
9851 TIMESTAMP columns get implicit DEFAULT value when
9852 explicit_defaults_for_timestamp is not set.
9853 */
9854 if (thd->variables.explicit_defaults_for_timestamp ||
9855 !is_timestamp_type(fld_type))
9856 {
9857 flags|= NO_DEFAULT_VALUE_FLAG;
9858 }
9859 }
9860
9861 if (fld_length != NULL)
9862 {
9863 errno= 0;
9864 length= strtoul(fld_length, NULL, 10);
9865 if ((errno != 0) || (length > MAX_FIELD_BLOBLENGTH))
9866 {
9867 my_error(ER_TOO_BIG_DISPLAYWIDTH, MYF(0), fld_name, MAX_FIELD_BLOBLENGTH);
9868 DBUG_RETURN(TRUE);
9869 }
9870
9871 if (length == 0)
9872 fld_length= NULL; /* purecov: inspected */
9873 }
9874
9875 sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
9876
9877 switch (fld_type) {
9878 case MYSQL_TYPE_TINY:
9879 if (!fld_length)
9880 length= MAX_TINYINT_WIDTH+sign_len;
9881 allowed_type_modifier= AUTO_INCREMENT_FLAG;
9882 break;
9883 case MYSQL_TYPE_SHORT:
9884 if (!fld_length)
9885 length= MAX_SMALLINT_WIDTH+sign_len;
9886 allowed_type_modifier= AUTO_INCREMENT_FLAG;
9887 break;
9888 case MYSQL_TYPE_INT24:
9889 if (!fld_length)
9890 length= MAX_MEDIUMINT_WIDTH+sign_len;
9891 allowed_type_modifier= AUTO_INCREMENT_FLAG;
9892 break;
9893 case MYSQL_TYPE_LONG:
9894 if (!fld_length)
9895 length= MAX_INT_WIDTH+sign_len;
9896 allowed_type_modifier= AUTO_INCREMENT_FLAG;
9897 break;
9898 case MYSQL_TYPE_LONGLONG:
9899 if (!fld_length)
9900 length= MAX_BIGINT_WIDTH;
9901 allowed_type_modifier= AUTO_INCREMENT_FLAG;
9902 break;
9903 case MYSQL_TYPE_NULL:
9904 break;
9905 case MYSQL_TYPE_NEWDECIMAL:
9906 my_decimal_trim(&length, &decimals);
9907 if (length > DECIMAL_MAX_PRECISION)
9908 {
9909 my_error(ER_TOO_BIG_PRECISION, MYF(0), static_cast<int>(length),
9910 fld_name, static_cast<ulong>(DECIMAL_MAX_PRECISION));
9911 DBUG_RETURN(TRUE);
9912 }
9913 if (length < decimals)
9914 {
9915 my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
9916 DBUG_RETURN(TRUE);
9917 }
9918 length=
9919 my_decimal_precision_to_length(length, decimals,
9920 fld_type_modifier & UNSIGNED_FLAG);
9921 pack_length=
9922 my_decimal_get_binary_size(length, decimals);
9923 break;
9924 case MYSQL_TYPE_VARCHAR:
9925 /*
9926 Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
9927 if they don't have a default value
9928 */
9929 max_field_charlength= MAX_FIELD_VARCHARLENGTH;
9930 break;
9931 case MYSQL_TYPE_STRING:
9932 break;
9933 case MYSQL_TYPE_BLOB:
9934 case MYSQL_TYPE_TINY_BLOB:
9935 case MYSQL_TYPE_LONG_BLOB:
9936 case MYSQL_TYPE_MEDIUM_BLOB:
9937 case MYSQL_TYPE_GEOMETRY:
9938 if (fld_default_value)
9939 {
9940 /* Allow empty as default value. */
9941 String str,*res;
9942 res= fld_default_value->val_str(&str);
9943 /*
9944 A default other than '' is always an error, and any non-NULL
9945 specified default is an error in strict mode.
9946 */
9947 if (res->length() || thd->is_strict_mode())
9948 {
9949 my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
9950 fld_name); /* purecov: inspected */
9951 DBUG_RETURN(TRUE);
9952 }
9953 else
9954 {
9955 /*
9956 Otherwise a default of '' is just a warning.
9957 */
9958 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
9959 ER_BLOB_CANT_HAVE_DEFAULT,
9960 ER(ER_BLOB_CANT_HAVE_DEFAULT),
9961 fld_name);
9962 }
9963 def= 0;
9964 }
9965 flags|= BLOB_FLAG;
9966 break;
9967 case MYSQL_TYPE_YEAR:
9968 if (!fld_length || length != 2)
9969 length= 4; /* Default length */
9970 flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
9971 break;
9972 case MYSQL_TYPE_FLOAT:
9973 /* change FLOAT(precision) to FLOAT or DOUBLE */
9974 allowed_type_modifier= AUTO_INCREMENT_FLAG;
9975 if (fld_length && !fld_decimals)
9976 {
9977 uint tmp_length= length;
9978 if (tmp_length > PRECISION_FOR_DOUBLE)
9979 {
9980 my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
9981 DBUG_RETURN(TRUE);
9982 }
9983 else if (tmp_length > PRECISION_FOR_FLOAT)
9984 {
9985 sql_type= MYSQL_TYPE_DOUBLE;
9986 length= MAX_DOUBLE_STR_LENGTH;
9987 }
9988 else
9989 length= MAX_FLOAT_STR_LENGTH;
9990 decimals= NOT_FIXED_DEC;
9991 break;
9992 }
9993 if (!fld_length && !fld_decimals)
9994 {
9995 length= MAX_FLOAT_STR_LENGTH;
9996 decimals= NOT_FIXED_DEC;
9997 }
9998 if (length < decimals &&
9999 decimals != NOT_FIXED_DEC)
10000 {
10001 my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
10002 DBUG_RETURN(TRUE);
10003 }
10004 break;
10005 case MYSQL_TYPE_DOUBLE:
10006 allowed_type_modifier= AUTO_INCREMENT_FLAG;
10007 if (!fld_length && !fld_decimals)
10008 {
10009 length= DBL_DIG+7;
10010 decimals= NOT_FIXED_DEC;
10011 }
10012 if (length < decimals &&
10013 decimals != NOT_FIXED_DEC)
10014 {
10015 my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
10016 DBUG_RETURN(TRUE);
10017 }
10018 break;
10019 case MYSQL_TYPE_TIMESTAMP:
10020 /* Add flags for TIMESTAMP for 4.0 MYD and 4.0 InnoDB compatibility */
10021 flags|= ZEROFILL_FLAG | UNSIGNED_FLAG;
10022 /* Fall through */
10023 case MYSQL_TYPE_TIMESTAMP2:
10024 if (fld_length == NULL)
10025 {
10026 length= MAX_DATETIME_WIDTH + (decimals ? (1 + decimals) : 0);
10027 }
10028 else if (length != MAX_DATETIME_WIDTH)
10029 {
10030 /*
10031 We support only even TIMESTAMP lengths less or equal than 14
10032 and 19 as length of 4.1 compatible representation. Silently
10033 shrink it to MAX_DATETIME_COMPRESSED_WIDTH.
10034 */
10035 DBUG_ASSERT(MAX_DATETIME_COMPRESSED_WIDTH < UINT_MAX);
10036 if (length != UINT_MAX) /* avoid overflow; is safe because of min() */
10037 length= ((length+1)/2)*2;
10038 length= min<ulong>(length, MAX_DATETIME_COMPRESSED_WIDTH);
10039 }
10040
10041 /*
10042 Since we silently rewrite down to MAX_DATETIME_COMPRESSED_WIDTH bytes,
10043 the parser should not raise errors unless bizzarely large.
10044 */
10045 max_field_charlength= UINT_MAX;
10046
10047 break;
10048 case MYSQL_TYPE_DATE:
10049 /* Old date type. */
10050 sql_type= MYSQL_TYPE_NEWDATE;
10051 // fallthrough
10052 case MYSQL_TYPE_NEWDATE:
10053 length= MAX_DATE_WIDTH;
10054 break;
10055 case MYSQL_TYPE_TIME:
10056 case MYSQL_TYPE_TIME2:
10057 length= MAX_TIME_WIDTH + (decimals ? (1 + decimals) : 0);
10058 break;
10059 case MYSQL_TYPE_DATETIME:
10060 case MYSQL_TYPE_DATETIME2:
10061 length= MAX_DATETIME_WIDTH + (decimals ? (1 + decimals) : 0);
10062 break;
10063 case MYSQL_TYPE_SET:
10064 {
10065 pack_length= get_set_pack_length(fld_interval_list->elements);
10066
10067 List_iterator<String> it(*fld_interval_list);
10068 String *tmp;
10069 while ((tmp= it++))
10070 interval_list.push_back(tmp);
10071 /*
10072 Set fake length to 1 to pass the below conditions.
10073 Real length will be set in mysql_prepare_table()
10074 when we know the character set of the column
10075 */
10076 length= 1;
10077 break;
10078 }
10079 case MYSQL_TYPE_ENUM:
10080 {
10081 /* Should be safe. */
10082 pack_length= get_enum_pack_length(fld_interval_list->elements);
10083
10084 List_iterator<String> it(*fld_interval_list);
10085 String *tmp;
10086 while ((tmp= it++))
10087 interval_list.push_back(tmp);
10088 length= 1; /* See comment for MYSQL_TYPE_SET above. */
10089 break;
10090 }
10091 case MYSQL_TYPE_VAR_STRING:
10092 DBUG_ASSERT(0); /* Impossible. */
10093 break;
10094 case MYSQL_TYPE_BIT:
10095 {
10096 if (!fld_length)
10097 length= 1;
10098 if (length > MAX_BIT_FIELD_LENGTH)
10099 {
10100 my_error(ER_TOO_BIG_DISPLAYWIDTH, MYF(0), fld_name,
10101 static_cast<ulong>(MAX_BIT_FIELD_LENGTH));
10102 DBUG_RETURN(TRUE);
10103 }
10104 pack_length= (length + 7) / 8;
10105 break;
10106 }
10107 case MYSQL_TYPE_DECIMAL:
10108 DBUG_ASSERT(0); /* Was obsolete */
10109 }
10110 /* Remember the value of length */
10111 char_length= length;
10112
10113 if (!(flags & BLOB_FLAG) &&
10114 ((length > max_field_charlength && fld_type != MYSQL_TYPE_SET &&
10115 fld_type != MYSQL_TYPE_ENUM &&
10116 (fld_type != MYSQL_TYPE_VARCHAR || fld_default_value)) ||
10117 ((length == 0) &&
10118 fld_type != MYSQL_TYPE_STRING &&
10119 fld_type != MYSQL_TYPE_VARCHAR && fld_type != MYSQL_TYPE_GEOMETRY)))
10120 {
10121 my_error((fld_type == MYSQL_TYPE_VAR_STRING ||
10122 fld_type == MYSQL_TYPE_VARCHAR ||
10123 fld_type == MYSQL_TYPE_STRING) ? ER_TOO_BIG_FIELDLENGTH :
10124 ER_TOO_BIG_DISPLAYWIDTH,
10125 MYF(0),
10126 fld_name, max_field_charlength); /* purecov: inspected */
10127 DBUG_RETURN(TRUE);
10128 }
10129 fld_type_modifier&= AUTO_INCREMENT_FLAG;
10130 if ((~allowed_type_modifier) & fld_type_modifier)
10131 {
10132 my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
10133 DBUG_RETURN(TRUE);
10134 }
10135
10136 zip_dict_name= *fld_zip_dict_name;
10137 DBUG_RETURN(FALSE); /* success */
10138 }
10139
10140
get_blob_type_from_length(ulong length)10141 enum_field_types get_blob_type_from_length(ulong length)
10142 {
10143 enum_field_types type;
10144 if (length < 256)
10145 type= MYSQL_TYPE_TINY_BLOB;
10146 else if (length < 65536)
10147 type= MYSQL_TYPE_BLOB;
10148 else if (length < 256L*256L*256L)
10149 type= MYSQL_TYPE_MEDIUM_BLOB;
10150 else
10151 type= MYSQL_TYPE_LONG_BLOB;
10152 return type;
10153 }
10154
10155
10156 /*
10157 Make a field from the .frm file info
10158 */
10159
calc_pack_length(enum_field_types type,uint32 length)10160 uint32 calc_pack_length(enum_field_types type,uint32 length)
10161 {
10162 switch (type) {
10163 case MYSQL_TYPE_VAR_STRING:
10164 case MYSQL_TYPE_STRING:
10165 case MYSQL_TYPE_DECIMAL: return (length);
10166 case MYSQL_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
10167 case MYSQL_TYPE_YEAR:
10168 case MYSQL_TYPE_TINY : return 1;
10169 case MYSQL_TYPE_SHORT : return 2;
10170 case MYSQL_TYPE_INT24:
10171 case MYSQL_TYPE_NEWDATE: return 3;
10172 case MYSQL_TYPE_TIME: return 3;
10173 case MYSQL_TYPE_TIME2:
10174 return length > MAX_TIME_WIDTH ?
10175 my_time_binary_length(length - MAX_TIME_WIDTH - 1) : 3;
10176 case MYSQL_TYPE_TIMESTAMP: return 4;
10177 case MYSQL_TYPE_TIMESTAMP2:
10178 return length > MAX_DATETIME_WIDTH ?
10179 my_timestamp_binary_length(length - MAX_DATETIME_WIDTH - 1) : 4;
10180 case MYSQL_TYPE_DATE:
10181 case MYSQL_TYPE_LONG : return 4;
10182 case MYSQL_TYPE_FLOAT : return sizeof(float);
10183 case MYSQL_TYPE_DOUBLE: return sizeof(double);
10184 case MYSQL_TYPE_DATETIME: return 8;
10185 case MYSQL_TYPE_DATETIME2:
10186 return length > MAX_DATETIME_WIDTH ?
10187 my_datetime_binary_length(length - MAX_DATETIME_WIDTH - 1) : 5;
10188 case MYSQL_TYPE_LONGLONG: return 8; /* Don't crash if no longlong */
10189 case MYSQL_TYPE_NULL : return 0;
10190 case MYSQL_TYPE_TINY_BLOB: return 1+portable_sizeof_char_ptr;
10191 case MYSQL_TYPE_BLOB: return 2+portable_sizeof_char_ptr;
10192 case MYSQL_TYPE_MEDIUM_BLOB: return 3+portable_sizeof_char_ptr;
10193 case MYSQL_TYPE_LONG_BLOB: return 4+portable_sizeof_char_ptr;
10194 case MYSQL_TYPE_GEOMETRY: return 4+portable_sizeof_char_ptr;
10195 case MYSQL_TYPE_SET:
10196 case MYSQL_TYPE_ENUM:
10197 case MYSQL_TYPE_NEWDECIMAL:
10198 abort(); return 0; // This shouldn't happen
10199 case MYSQL_TYPE_BIT: return length / 8;
10200 default:
10201 return 0;
10202 }
10203 }
10204
10205
pack_length_to_packflag(uint type)10206 uint pack_length_to_packflag(uint type)
10207 {
10208 switch (type) {
10209 case 1: return f_settype((uint) MYSQL_TYPE_TINY);
10210 case 2: return f_settype((uint) MYSQL_TYPE_SHORT);
10211 case 3: return f_settype((uint) MYSQL_TYPE_INT24);
10212 case 4: return f_settype((uint) MYSQL_TYPE_LONG);
10213 case 8: return f_settype((uint) MYSQL_TYPE_LONGLONG);
10214 }
10215 return 0; // This shouldn't happen
10216 }
10217
10218
make_field(TABLE_SHARE * share,uchar * ptr,uint32 field_length,uchar * null_pos,uchar null_bit,uint pack_flag,enum_field_types field_type,const CHARSET_INFO * field_charset,Field::geometry_type geom_type,Field::utype unireg_check,TYPELIB * interval,const char * field_name)10219 Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32 field_length,
10220 uchar *null_pos, uchar null_bit,
10221 uint pack_flag,
10222 enum_field_types field_type,
10223 const CHARSET_INFO *field_charset,
10224 Field::geometry_type geom_type,
10225 Field::utype unireg_check,
10226 TYPELIB *interval,
10227 const char *field_name)
10228 {
10229 uchar *UNINIT_VAR(bit_ptr);
10230 uchar UNINIT_VAR(bit_offset);
10231 if (field_type == MYSQL_TYPE_BIT && !f_bit_as_char(pack_flag))
10232 {
10233 bit_ptr= null_pos;
10234 bit_offset= null_bit;
10235 if (f_maybe_null(pack_flag)) // if null field
10236 {
10237 bit_ptr+= (null_bit == 7); // shift bit_ptr and bit_offset
10238 bit_offset= (bit_offset + 1) & 7;
10239 }
10240 }
10241
10242 if (!f_maybe_null(pack_flag))
10243 {
10244 null_pos=0;
10245 null_bit=0;
10246 }
10247 else
10248 {
10249 null_bit= ((uchar) 1) << null_bit;
10250 }
10251
10252 if (is_temporal_real_type(field_type))
10253 field_charset= &my_charset_numeric;
10254
10255 DBUG_PRINT("debug", ("field_type: %d, field_length: %u, interval: %p, pack_flag: %s%s%s%s%s",
10256 field_type, field_length, interval,
10257 FLAGSTR(pack_flag, FIELDFLAG_BINARY),
10258 FLAGSTR(pack_flag, FIELDFLAG_INTERVAL),
10259 FLAGSTR(pack_flag, FIELDFLAG_NUMBER),
10260 FLAGSTR(pack_flag, FIELDFLAG_PACK),
10261 FLAGSTR(pack_flag, FIELDFLAG_BLOB)));
10262
10263 if (f_is_alpha(pack_flag))
10264 {
10265 if (!f_is_packed(pack_flag))
10266 {
10267 if (field_type == MYSQL_TYPE_STRING ||
10268 field_type == MYSQL_TYPE_DECIMAL || // 3.23 or 4.0 string
10269 field_type == MYSQL_TYPE_VAR_STRING)
10270 return new Field_string(ptr,field_length,null_pos,null_bit,
10271 unireg_check, field_name,
10272 field_charset);
10273 if (field_type == MYSQL_TYPE_VARCHAR)
10274 return new Field_varstring(ptr,field_length,
10275 HA_VARCHAR_PACKLENGTH(field_length),
10276 null_pos,null_bit,
10277 unireg_check, field_name,
10278 share,
10279 field_charset);
10280 return 0; // Error
10281 }
10282
10283 uint pack_length=calc_pack_length((enum_field_types)
10284 f_packtype(pack_flag),
10285 field_length);
10286
10287 #ifdef HAVE_SPATIAL
10288 if (f_is_geom(pack_flag))
10289 return new Field_geom(ptr,null_pos,null_bit,
10290 unireg_check, field_name, share,
10291 pack_length, geom_type);
10292 #endif
10293 if (f_is_blob(pack_flag))
10294 return new Field_blob(ptr,null_pos,null_bit,
10295 unireg_check, field_name, share,
10296 pack_length, field_charset);
10297 if (interval)
10298 {
10299 if (f_is_enum(pack_flag))
10300 return new Field_enum(ptr,field_length,null_pos,null_bit,
10301 unireg_check, field_name,
10302 pack_length, interval, field_charset);
10303 else
10304 return new Field_set(ptr,field_length,null_pos,null_bit,
10305 unireg_check, field_name,
10306 pack_length, interval, field_charset);
10307 }
10308 }
10309
10310 switch (field_type) {
10311 case MYSQL_TYPE_DECIMAL:
10312 return new Field_decimal(ptr,field_length,null_pos,null_bit,
10313 unireg_check, field_name,
10314 f_decimals(pack_flag),
10315 f_is_zerofill(pack_flag) != 0,
10316 f_is_dec(pack_flag) == 0);
10317 case MYSQL_TYPE_NEWDECIMAL:
10318 return new Field_new_decimal(ptr,field_length,null_pos,null_bit,
10319 unireg_check, field_name,
10320 f_decimals(pack_flag),
10321 f_is_zerofill(pack_flag) != 0,
10322 f_is_dec(pack_flag) == 0);
10323 case MYSQL_TYPE_FLOAT:
10324 return new Field_float(ptr,field_length,null_pos,null_bit,
10325 unireg_check, field_name,
10326 f_decimals(pack_flag),
10327 f_is_zerofill(pack_flag) != 0,
10328 f_is_dec(pack_flag)== 0);
10329 case MYSQL_TYPE_DOUBLE:
10330 return new Field_double(ptr,field_length,null_pos,null_bit,
10331 unireg_check, field_name,
10332 f_decimals(pack_flag),
10333 f_is_zerofill(pack_flag) != 0,
10334 f_is_dec(pack_flag)== 0);
10335 case MYSQL_TYPE_TINY:
10336 return new Field_tiny(ptr,field_length,null_pos,null_bit,
10337 unireg_check, field_name,
10338 f_is_zerofill(pack_flag) != 0,
10339 f_is_dec(pack_flag) == 0);
10340 case MYSQL_TYPE_SHORT:
10341 return new Field_short(ptr,field_length,null_pos,null_bit,
10342 unireg_check, field_name,
10343 f_is_zerofill(pack_flag) != 0,
10344 f_is_dec(pack_flag) == 0);
10345 case MYSQL_TYPE_INT24:
10346 return new Field_medium(ptr,field_length,null_pos,null_bit,
10347 unireg_check, field_name,
10348 f_is_zerofill(pack_flag) != 0,
10349 f_is_dec(pack_flag) == 0);
10350 case MYSQL_TYPE_LONG:
10351 return new Field_long(ptr,field_length,null_pos,null_bit,
10352 unireg_check, field_name,
10353 f_is_zerofill(pack_flag) != 0,
10354 f_is_dec(pack_flag) == 0);
10355 case MYSQL_TYPE_LONGLONG:
10356 return new Field_longlong(ptr,field_length,null_pos,null_bit,
10357 unireg_check, field_name,
10358 f_is_zerofill(pack_flag) != 0,
10359 f_is_dec(pack_flag) == 0);
10360 case MYSQL_TYPE_TIMESTAMP:
10361 return new Field_timestamp(ptr, field_length, null_pos, null_bit,
10362 unireg_check, field_name);
10363 case MYSQL_TYPE_TIMESTAMP2:
10364 return new Field_timestampf(ptr, null_pos, null_bit,
10365 unireg_check, field_name,
10366 field_length > MAX_DATETIME_WIDTH ?
10367 field_length - 1 - MAX_DATETIME_WIDTH : 0);
10368 case MYSQL_TYPE_YEAR:
10369 return new Field_year(ptr,field_length,null_pos,null_bit,
10370 unireg_check, field_name);
10371 case MYSQL_TYPE_NEWDATE:
10372 return new Field_newdate(ptr, null_pos, null_bit, unireg_check, field_name);
10373
10374 case MYSQL_TYPE_TIME:
10375 return new Field_time(ptr, null_pos, null_bit,
10376 unireg_check, field_name);
10377 case MYSQL_TYPE_TIME2:
10378 return new Field_timef(ptr, null_pos, null_bit,
10379 unireg_check, field_name,
10380 (field_length > MAX_TIME_WIDTH) ?
10381 field_length - 1 - MAX_TIME_WIDTH : 0);
10382 case MYSQL_TYPE_DATETIME:
10383 return new Field_datetime(ptr, null_pos, null_bit,
10384 unireg_check, field_name);
10385 case MYSQL_TYPE_DATETIME2:
10386 return new Field_datetimef(ptr, null_pos, null_bit,
10387 unireg_check, field_name,
10388 (field_length > MAX_DATETIME_WIDTH) ?
10389 field_length - 1 - MAX_DATETIME_WIDTH : 0);
10390 case MYSQL_TYPE_NULL:
10391 return new Field_null(ptr, field_length, unireg_check, field_name,
10392 field_charset);
10393 case MYSQL_TYPE_BIT:
10394 return f_bit_as_char(pack_flag) ?
10395 new Field_bit_as_char(ptr, field_length, null_pos, null_bit,
10396 unireg_check, field_name) :
10397 new Field_bit(ptr, field_length, null_pos, null_bit, bit_ptr,
10398 bit_offset, unireg_check, field_name);
10399
10400 default: // Impossible (Wrong version)
10401 break;
10402 }
10403 return 0;
10404 }
10405
10406
10407 /**
10408 Constructs a column definition from an actual column object. This is a
10409 reverse-engineering procedure that creates a column definition object as
10410 produced by the parser (Create_field) from a resolved column object
10411 (Field).
10412
10413 @param old_field The column object from which the column definition is
10414 constructed.
10415 @param orig_field Used for copying default values. This parameter may be
10416 NULL, but if present it is used for copying default
10417 values.
10418
10419 Default values are copied into an Item_string unless:
10420 @li The default value is a function.
10421 @li There is no default value.
10422 @li old_field is a BLOB column.
10423 @li old_field has its data pointer improperly initialized.
10424 */
10425
Create_field(Field * old_field,Field * orig_field)10426 Create_field::Create_field(Field *old_field,Field *orig_field) :
10427 field_name(old_field->field_name),
10428 change(old_field->field_name),
10429 comment(old_field->comment),
10430 sql_type(old_field->real_type()),
10431 length(old_field->field_length),
10432 decimals(old_field->decimals()),
10433 flags(old_field->flags),
10434 pack_length(old_field->pack_length()),
10435 key_length(old_field->key_length()),
10436 unireg_check(old_field->unireg_check),
10437 charset(old_field->charset()), // May be NULL ptr
10438 field(old_field),
10439 zip_dict_name(old_field->zip_dict_name)
10440 {
10441
10442 switch (sql_type) {
10443 case MYSQL_TYPE_BLOB:
10444 switch (pack_length - portable_sizeof_char_ptr) {
10445 case 1: sql_type= MYSQL_TYPE_TINY_BLOB; break;
10446 case 2: sql_type= MYSQL_TYPE_BLOB; break;
10447 case 3: sql_type= MYSQL_TYPE_MEDIUM_BLOB; break;
10448 default: sql_type= MYSQL_TYPE_LONG_BLOB; break;
10449 }
10450 length/= charset->mbmaxlen;
10451 key_length/= charset->mbmaxlen;
10452 break;
10453 case MYSQL_TYPE_STRING:
10454 /* Change CHAR -> VARCHAR if dynamic record length */
10455 if (old_field->type() == MYSQL_TYPE_VAR_STRING)
10456 sql_type= MYSQL_TYPE_VARCHAR;
10457 /* fall through */
10458
10459 case MYSQL_TYPE_ENUM:
10460 case MYSQL_TYPE_SET:
10461 case MYSQL_TYPE_VARCHAR:
10462 case MYSQL_TYPE_VAR_STRING:
10463 /* This is corrected in create_length_to_internal_length */
10464 length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
10465 break;
10466 #ifdef HAVE_SPATIAL
10467 case MYSQL_TYPE_GEOMETRY:
10468 geom_type= ((Field_geom*)old_field)->geom_type;
10469 break;
10470 #endif
10471 case MYSQL_TYPE_YEAR:
10472 if (length != 4)
10473 {
10474 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
10475 ER_INVALID_YEAR_COLUMN_LENGTH,
10476 ER(ER_INVALID_YEAR_COLUMN_LENGTH),
10477 length);
10478 length= 4; // convert obsolete YEAR(2) to YEAR(4)
10479 }
10480 break;
10481 default:
10482 break;
10483 }
10484
10485 if (flags & (ENUM_FLAG | SET_FLAG))
10486 interval= ((Field_enum*) old_field)->typelib;
10487 else
10488 interval=0;
10489 def=0;
10490 char_length= length;
10491
10492 /*
10493 Copy the default (constant/function) from the column object orig_field, if
10494 supplied. We do this if all these conditions are met:
10495
10496 - The column allows a default.
10497
10498 - The column type is not a BLOB type.
10499
10500 - The original column (old_field) was properly initialized with a record
10501 buffer pointer.
10502 */
10503 if (!(flags & (NO_DEFAULT_VALUE_FLAG | BLOB_FLAG)) &&
10504 old_field->ptr != NULL &&
10505 orig_field != NULL)
10506 {
10507 bool default_now= false;
10508 if (real_type_with_now_as_default(sql_type))
10509 {
10510 // The SQL type of the new field allows a function default:
10511 default_now= orig_field->has_insert_default_function();
10512 bool update_now= orig_field->has_update_default_function();
10513
10514 if (default_now && update_now)
10515 unireg_check= Field::TIMESTAMP_DNUN_FIELD;
10516 else if (default_now)
10517 unireg_check= Field::TIMESTAMP_DN_FIELD;
10518 else if (update_now)
10519 unireg_check= Field::TIMESTAMP_UN_FIELD;
10520 }
10521 if (!default_now) // Give a constant default
10522 {
10523 char buff[MAX_FIELD_WIDTH];
10524 String tmp(buff,sizeof(buff), charset);
10525 my_ptrdiff_t diff;
10526
10527 /* Get the value from default_values */
10528 diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
10529 orig_field->table->record[0]);
10530 orig_field->move_field_offset(diff); // Points now at default_values
10531 if (!orig_field->is_real_null())
10532 {
10533 char buff[MAX_FIELD_WIDTH], *pos;
10534 String tmp(buff, sizeof(buff), charset), *res;
10535 res= orig_field->val_str(&tmp);
10536 pos= (char*) sql_strmake(res->ptr(), res->length());
10537 def= new Item_string(pos, res->length(), charset);
10538 }
10539 orig_field->move_field_offset(-diff); // Back to record[0]
10540 }
10541 }
10542 }
10543
10544
10545 /**
10546 maximum possible character length for blob.
10547
10548 This method is used in Item_field::set_field to calculate
10549 max_length for Item.
10550
10551 For example:
10552 CREATE TABLE t2 SELECT CONCAT(tinyblob_utf8_column) FROM t1;
10553 must create a "VARCHAR(255) CHARACTER SET utf8" column.
10554
10555 @return
10556 length
10557 */
10558
char_length() const10559 uint32 Field_blob::char_length() const
10560 {
10561 switch (packlength)
10562 {
10563 case 1:
10564 return 255;
10565 case 2:
10566 return 65535;
10567 case 3:
10568 return 16777215;
10569 case 4:
10570 return (uint32) 4294967295U;
10571 default:
10572 DBUG_ASSERT(0); // we should never go here
10573 return 0;
10574 }
10575 }
10576
10577
10578 /**
10579 maximum possible display length for blob.
10580
10581 @return
10582 length
10583 */
10584
max_display_length()10585 uint32 Field_blob::max_display_length()
10586 {
10587 switch (packlength)
10588 {
10589 case 1:
10590 return 255 * field_charset->mbmaxlen;
10591 case 2:
10592 return 65535 * field_charset->mbmaxlen;
10593 case 3:
10594 return 16777215 * field_charset->mbmaxlen;
10595 case 4:
10596 return (uint32) 4294967295U;
10597 default:
10598 DBUG_ASSERT(0); // we should never go here
10599 return 0;
10600 }
10601 }
10602
10603
10604 /*****************************************************************************
10605 Warning handling
10606 *****************************************************************************/
10607
10608 /**
10609 Produce warning or note about data saved into field.
10610
10611 @param level - level of message (Note/Warning/Error)
10612 @param code - error code of message to be produced
10613 @param cut_increment - whenever we should increase cut fields count
10614
10615 @note
10616 This function won't produce warning and increase cut fields counter
10617 if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
10618
10619 if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
10620 This allows us to avoid notes in optimisation, like convert_constant_item().
10621
10622 @retval
10623 1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
10624 @retval
10625 0 otherwise
10626 */
10627
10628 bool
set_warning(Sql_condition::enum_warning_level level,uint code,int cut_increment) const10629 Field::set_warning(Sql_condition::enum_warning_level level, uint code,
10630 int cut_increment) const
10631 {
10632 /*
10633 If this field was created only for type conversion purposes it
10634 will have table == NULL.
10635 */
10636 THD *thd= table ? table->in_use : current_thd;
10637 if (thd->count_cuted_fields)
10638 {
10639 thd->cuted_fields+= cut_increment;
10640 push_warning_printf(thd, level, code, ER(code), field_name,
10641 thd->get_stmt_da()->current_row_for_warning());
10642 return 0;
10643 }
10644 return level >= Sql_condition::WARN_LEVEL_WARN;
10645 }
10646
10647
10648 /**
10649 Produce warning or note about double datetime data saved into field.
10650
10651 @param level level of message (Note/Warning/Error)
10652 @param code error code of message to be produced
10653 @param val error parameter (the value)
10654 @param ts_type type of datetime value (datetime/date/time)
10655 @param cut_increment whenever we should increase cut fields count
10656 @note
10657 This function will always produce some warning but won't increase cut
10658 fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
10659 thread.
10660 */
10661 void
set_datetime_warning(Sql_condition::enum_warning_level level,uint code,ErrConvString val,timestamp_type ts_type,int cut_increment)10662 Field_temporal::set_datetime_warning(Sql_condition::enum_warning_level level,
10663 uint code,
10664 ErrConvString val,
10665 timestamp_type ts_type,
10666 int cut_increment)
10667 {
10668 THD *thd= table ? table->in_use : current_thd;
10669 if (thd->really_abort_on_warning() ||
10670 set_warning(level, code, cut_increment))
10671 make_truncated_value_warning(thd, level, val, ts_type, field_name);
10672 }
10673
is_part_of_actual_key(THD * thd,uint cur_index,KEY * cur_index_info)10674 bool Field::is_part_of_actual_key(THD *thd, uint cur_index, KEY *cur_index_info)
10675 {
10676 return
10677 thd->optimizer_switch_flag(OPTIMIZER_SWITCH_USE_INDEX_EXTENSIONS) &&
10678 !(cur_index_info->flags & HA_NOSAME) ?
10679 part_of_key.is_set(cur_index) :
10680 part_of_key_not_extended.is_set(cur_index);
10681 }
10682