~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.cc

  • Committer: Monty Taylor
  • Date: 2009-03-16 16:56:48 UTC
  • mto: This revision was merged to the branch mainline in revision 938.
  • Revision ID: mordred@inaugust.com-20090316165648-0dsce73jne0qikk0
Addd -Wshadow to PROTOSKIP warnings and turned -Wstrict-aliasing off. Jumped the gun...

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2010 Brian Aker
5
4
 *  Copyright (C) 2008 MySQL
6
5
 *
7
6
 *  This program is free software; you can redistribute it and/or modify
20
19
 */
21
20
 
22
21
/**
23
 
 * @file This file implements the Field class and API
24
 
 */
 
22
  @file
25
23
 
26
 
#include <config.h>
27
 
#include <cstdio>
 
24
  @brief
 
25
  This file implements classes defined in field.h
 
26
*/
 
27
#include <drizzled/server_includes.h>
 
28
#include "sql_select.h"
28
29
#include <errno.h>
29
 
#include <float.h>
30
 
#include <drizzled/sql_select.h>
31
30
#include <drizzled/error.h>
 
31
#include <drizzled/virtual_column_info.h>
32
32
#include <drizzled/field/str.h>
 
33
#include <drizzled/field/longstr.h>
33
34
#include <drizzled/field/num.h>
34
35
#include <drizzled/field/blob.h>
35
 
#include <drizzled/field/boolean.h>
36
36
#include <drizzled/field/enum.h>
37
37
#include <drizzled/field/null.h>
38
38
#include <drizzled/field/date.h>
39
39
#include <drizzled/field/decimal.h>
40
40
#include <drizzled/field/real.h>
41
41
#include <drizzled/field/double.h>
42
 
#include <drizzled/field/int32.h>
43
 
#include <drizzled/field/int64.h>
 
42
#include <drizzled/field/long.h>
 
43
#include <drizzled/field/int64_t.h>
44
44
#include <drizzled/field/num.h>
45
 
#include <drizzled/field/time.h>
46
 
#include <drizzled/field/epoch.h>
 
45
#include <drizzled/field/timestamp.h>
47
46
#include <drizzled/field/datetime.h>
48
 
#include <drizzled/field/microtime.h>
49
47
#include <drizzled/field/varstring.h>
50
 
#include <drizzled/field/uuid.h>
51
 
#include <drizzled/time_functions.h>
52
 
#include <drizzled/internal/m_string.h>
53
 
#include <drizzled/table.h>
54
 
#include <drizzled/util/test.h>
55
 
#include <drizzled/session.h>
56
 
#include <drizzled/current_session.h>
57
 
#include <drizzled/display.h>
58
 
#include <drizzled/typelib.h>
59
48
 
60
 
namespace drizzled
61
 
{
62
49
 
63
50
/*****************************************************************************
64
51
  Instansiate templates and static variables
65
52
*****************************************************************************/
66
53
 
 
54
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
55
template class List<Create_field>;
 
56
template class List_iterator<Create_field>;
 
57
#endif
 
58
 
 
59
 
67
60
static enum_field_types
68
 
field_types_merge_rules [enum_field_types_size][enum_field_types_size]=
 
61
field_types_merge_rules [DRIZZLE_TYPE_MAX+1][DRIZZLE_TYPE_MAX+1]=
69
62
{
 
63
  /* DRIZZLE_TYPE_TINY -> */
 
64
  {
 
65
    //DRIZZLE_TYPE_TINY
 
66
    DRIZZLE_TYPE_TINY,
 
67
    //DRIZZLE_TYPE_LONG
 
68
    DRIZZLE_TYPE_LONG,
 
69
    //DRIZZLE_TYPE_DOUBLE
 
70
    DRIZZLE_TYPE_DOUBLE,
 
71
    //DRIZZLE_TYPE_NULL
 
72
    DRIZZLE_TYPE_TINY,
 
73
    //DRIZZLE_TYPE_TIMESTAMP
 
74
    DRIZZLE_TYPE_VARCHAR,
 
75
    //DRIZZLE_TYPE_LONGLONG
 
76
    DRIZZLE_TYPE_LONGLONG,
 
77
    //DRIZZLE_TYPE_DATETIME
 
78
    DRIZZLE_TYPE_VARCHAR,
 
79
    //DRIZZLE_TYPE_DATE
 
80
    DRIZZLE_TYPE_VARCHAR,
 
81
    //DRIZZLE_TYPE_VARCHAR
 
82
    DRIZZLE_TYPE_VARCHAR,
 
83
    //DRIZZLE_TYPE_VIRTUAL
 
84
    DRIZZLE_TYPE_VIRTUAL,
 
85
    //DRIZZLE_TYPE_NEWDECIMAL
 
86
    DRIZZLE_TYPE_NEWDECIMAL,
 
87
    //DRIZZLE_TYPE_ENUM
 
88
    DRIZZLE_TYPE_VARCHAR,
 
89
    //DRIZZLE_TYPE_BLOB
 
90
    DRIZZLE_TYPE_BLOB,
 
91
  },
70
92
  /* DRIZZLE_TYPE_LONG -> */
71
93
  {
 
94
    //DRIZZLE_TYPE_TINY
 
95
    DRIZZLE_TYPE_LONG,
72
96
    //DRIZZLE_TYPE_LONG
73
97
    DRIZZLE_TYPE_LONG,
74
98
    //DRIZZLE_TYPE_DOUBLE
85
109
    DRIZZLE_TYPE_VARCHAR,
86
110
    //DRIZZLE_TYPE_VARCHAR
87
111
    DRIZZLE_TYPE_VARCHAR,
88
 
    //DRIZZLE_TYPE_DECIMAL
89
 
    DRIZZLE_TYPE_DECIMAL,
 
112
    //DRIZZLE_TYPE_VIRTUAL
 
113
    DRIZZLE_TYPE_VIRTUAL,
 
114
    //DRIZZLE_TYPE_NEWDECIMAL
 
115
    DRIZZLE_TYPE_NEWDECIMAL,
90
116
    //DRIZZLE_TYPE_ENUM
91
117
    DRIZZLE_TYPE_VARCHAR,
92
118
    //DRIZZLE_TYPE_BLOB
93
119
    DRIZZLE_TYPE_BLOB,
94
 
    //DRIZZLE_TYPE_TIME
95
 
    DRIZZLE_TYPE_TIME,
96
 
    //DRIZZLE_TYPE_BOOLEAN
97
 
    DRIZZLE_TYPE_VARCHAR,
98
 
    //DRIZZLE_TYPE_UUID
99
 
    DRIZZLE_TYPE_VARCHAR,
100
 
    //DRIZZLE_TYPE_MICROTIME
101
 
    DRIZZLE_TYPE_VARCHAR,
102
120
  },
103
121
  /* DRIZZLE_TYPE_DOUBLE -> */
104
122
  {
 
123
    //DRIZZLE_TYPE_TINY
 
124
    DRIZZLE_TYPE_DOUBLE,
105
125
    //DRIZZLE_TYPE_LONG
106
126
    DRIZZLE_TYPE_DOUBLE,
107
127
    //DRIZZLE_TYPE_DOUBLE
118
138
    DRIZZLE_TYPE_VARCHAR,
119
139
    //DRIZZLE_TYPE_VARCHAR
120
140
    DRIZZLE_TYPE_VARCHAR,
121
 
    //DRIZZLE_TYPE_DECIMAL
 
141
    //DRIZZLE_TYPE_VIRTUAL
 
142
    DRIZZLE_TYPE_VIRTUAL,
 
143
    //DRIZZLE_TYPE_NEWDECIMAL
122
144
    DRIZZLE_TYPE_DOUBLE,
123
145
    //DRIZZLE_TYPE_ENUM
124
146
    DRIZZLE_TYPE_VARCHAR,
125
147
    //DRIZZLE_TYPE_BLOB
126
148
    DRIZZLE_TYPE_BLOB,
127
 
    //DRIZZLE_TYPE_TIME
128
 
    DRIZZLE_TYPE_TIME,
129
 
    //DRIZZLE_TYPE_BOOLEAN
130
 
    DRIZZLE_TYPE_VARCHAR,
131
 
    //DRIZZLE_TYPE_UUID
132
 
    DRIZZLE_TYPE_VARCHAR,
133
 
    //DRIZZLE_TYPE_MICROTIME
134
 
    DRIZZLE_TYPE_VARCHAR,
135
149
  },
136
150
  /* DRIZZLE_TYPE_NULL -> */
137
151
  {
 
152
    //DRIZZLE_TYPE_TINY
 
153
    DRIZZLE_TYPE_TINY,
138
154
    //DRIZZLE_TYPE_LONG
139
155
    DRIZZLE_TYPE_LONG,
140
156
    //DRIZZLE_TYPE_DOUBLE
151
167
    DRIZZLE_TYPE_DATE,
152
168
    //DRIZZLE_TYPE_VARCHAR
153
169
    DRIZZLE_TYPE_VARCHAR,
154
 
    //DRIZZLE_TYPE_DECIMAL
155
 
    DRIZZLE_TYPE_DECIMAL,
 
170
    //DRIZZLE_TYPE_VIRTUAL
 
171
    DRIZZLE_TYPE_VIRTUAL,
 
172
    //DRIZZLE_TYPE_NEWDECIMAL
 
173
    DRIZZLE_TYPE_NEWDECIMAL,
156
174
    //DRIZZLE_TYPE_ENUM
157
175
    DRIZZLE_TYPE_ENUM,
158
176
    //DRIZZLE_TYPE_BLOB
159
177
    DRIZZLE_TYPE_BLOB,
160
 
    //DRIZZLE_TYPE_TIME
161
 
    DRIZZLE_TYPE_TIME,
162
 
    //DRIZZLE_TYPE_BOOLEAN
163
 
    DRIZZLE_TYPE_BOOLEAN,
164
 
    //DRIZZLE_TYPE_UUID
165
 
    DRIZZLE_TYPE_UUID,
166
 
    //DRIZZLE_TYPE_MICROTIME
167
 
    DRIZZLE_TYPE_MICROTIME,
168
178
  },
169
179
  /* DRIZZLE_TYPE_TIMESTAMP -> */
170
180
  {
 
181
    //DRIZZLE_TYPE_TINY
 
182
    DRIZZLE_TYPE_VARCHAR,
171
183
    //DRIZZLE_TYPE_LONG
172
184
    DRIZZLE_TYPE_VARCHAR,
173
185
    //DRIZZLE_TYPE_DOUBLE
184
196
    DRIZZLE_TYPE_DATE,
185
197
    //DRIZZLE_TYPE_VARCHAR
186
198
    DRIZZLE_TYPE_VARCHAR,
187
 
    //DRIZZLE_TYPE_DECIMAL
 
199
    //DRIZZLE_TYPE_VIRTUAL
 
200
    DRIZZLE_TYPE_VIRTUAL,
 
201
    //DRIZZLE_TYPE_NEWDECIMAL
188
202
    DRIZZLE_TYPE_VARCHAR,
189
203
    //DRIZZLE_TYPE_ENUM
190
204
    DRIZZLE_TYPE_VARCHAR,
191
205
    //DRIZZLE_TYPE_BLOB
192
206
    DRIZZLE_TYPE_BLOB,
193
 
    //DRIZZLE_TYPE_TIME
194
 
    DRIZZLE_TYPE_TIME,
195
 
    //DRIZZLE_TYPE_BOOLEAN
196
 
    DRIZZLE_TYPE_VARCHAR,
197
 
    //DRIZZLE_TYPE_UUID
198
 
    DRIZZLE_TYPE_VARCHAR,
199
 
    //DRIZZLE_TYPE_MICROTIME
200
 
    DRIZZLE_TYPE_VARCHAR,
201
207
  },
202
208
  /* DRIZZLE_TYPE_LONGLONG -> */
203
209
  {
 
210
    //DRIZZLE_TYPE_TINY
 
211
    DRIZZLE_TYPE_LONGLONG,
204
212
    //DRIZZLE_TYPE_LONG
205
213
    DRIZZLE_TYPE_LONGLONG,
206
214
    //DRIZZLE_TYPE_DOUBLE
217
225
    DRIZZLE_TYPE_DATE,
218
226
    //DRIZZLE_TYPE_VARCHAR
219
227
    DRIZZLE_TYPE_VARCHAR,
220
 
    //DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_ENUM
221
 
    DRIZZLE_TYPE_DECIMAL,
 
228
    //DRIZZLE_TYPE_VIRTUAL
 
229
    DRIZZLE_TYPE_VIRTUAL,
 
230
    //DRIZZLE_TYPE_NEWDECIMAL
 
231
    //DRIZZLE_TYPE_ENUM
 
232
    DRIZZLE_TYPE_NEWDECIMAL,
222
233
    DRIZZLE_TYPE_VARCHAR,
223
234
    //DRIZZLE_TYPE_BLOB
224
235
    DRIZZLE_TYPE_BLOB,
225
 
    //DRIZZLE_TYPE_TIME
226
 
    DRIZZLE_TYPE_TIME,
227
 
    //DRIZZLE_TYPE_BOOLEAN
228
 
    DRIZZLE_TYPE_VARCHAR,
229
 
    //DRIZZLE_TYPE_UUID
230
 
    DRIZZLE_TYPE_VARCHAR,
231
 
    //DRIZZLE_TYPE_MICROTIME
232
 
    DRIZZLE_TYPE_VARCHAR,
233
236
  },
234
237
  /* DRIZZLE_TYPE_DATETIME -> */
235
238
  {
 
239
    //DRIZZLE_TYPE_TINY
 
240
    DRIZZLE_TYPE_VARCHAR,
236
241
    //DRIZZLE_TYPE_LONG
237
242
    DRIZZLE_TYPE_VARCHAR,
238
243
    //DRIZZLE_TYPE_DOUBLE
249
254
    DRIZZLE_TYPE_DATE,
250
255
    //DRIZZLE_TYPE_VARCHAR
251
256
    DRIZZLE_TYPE_VARCHAR,
252
 
    //DRIZZLE_TYPE_DECIMAL
 
257
    //DRIZZLE_TYPE_VIRTUAL
 
258
    DRIZZLE_TYPE_VIRTUAL,
 
259
    //DRIZZLE_TYPE_NEWDECIMAL
253
260
    DRIZZLE_TYPE_VARCHAR,
254
261
    //DRIZZLE_TYPE_ENUM
255
262
    DRIZZLE_TYPE_VARCHAR,
256
263
    //DRIZZLE_TYPE_BLOB
257
264
    DRIZZLE_TYPE_BLOB,
258
 
    //DRIZZLE_TYPE_TIME
259
 
    DRIZZLE_TYPE_TIME,
260
 
    //DRIZZLE_TYPE_BOOLEAN
261
 
    DRIZZLE_TYPE_VARCHAR,
262
 
    //DRIZZLE_TYPE_UUID
263
 
    DRIZZLE_TYPE_VARCHAR,
264
 
    //DRIZZLE_TYPE_MICROTIME
265
 
    DRIZZLE_TYPE_VARCHAR,
266
265
  },
267
266
  /* DRIZZLE_TYPE_DATE -> */
268
267
  {
 
268
    //DRIZZLE_TYPE_TINY
 
269
    DRIZZLE_TYPE_VARCHAR,
269
270
    //DRIZZLE_TYPE_LONG
270
271
    DRIZZLE_TYPE_VARCHAR,
271
272
    //DRIZZLE_TYPE_DOUBLE
282
283
    DRIZZLE_TYPE_DATE,
283
284
    //DRIZZLE_TYPE_VARCHAR
284
285
    DRIZZLE_TYPE_VARCHAR,
285
 
    //DRIZZLE_TYPE_DECIMAL
 
286
    //DRIZZLE_TYPE_VIRTUAL
 
287
    DRIZZLE_TYPE_VIRTUAL,
 
288
    //DRIZZLE_TYPE_NEWDECIMAL
286
289
    DRIZZLE_TYPE_VARCHAR,
287
290
    //DRIZZLE_TYPE_ENUM
288
291
    DRIZZLE_TYPE_VARCHAR,
289
292
    //DRIZZLE_TYPE_BLOB
290
293
    DRIZZLE_TYPE_BLOB,
291
 
    //DRIZZLE_TYPE_TIME
292
 
    DRIZZLE_TYPE_TIME,
293
 
    //DRIZZLE_TYPE_BOOLEAN
294
 
    DRIZZLE_TYPE_VARCHAR,
295
 
    //DRIZZLE_TYPE_UUID
296
 
    DRIZZLE_TYPE_VARCHAR,
297
 
    //DRIZZLE_TYPE_MICROTIME
298
 
    DRIZZLE_TYPE_VARCHAR,
299
294
  },
300
295
  /* DRIZZLE_TYPE_VARCHAR -> */
301
296
  {
 
297
    //DRIZZLE_TYPE_TINY
 
298
    DRIZZLE_TYPE_VARCHAR,
302
299
    //DRIZZLE_TYPE_LONG
303
300
    DRIZZLE_TYPE_VARCHAR,
304
301
    //DRIZZLE_TYPE_DOUBLE
315
312
    DRIZZLE_TYPE_VARCHAR,
316
313
    //DRIZZLE_TYPE_VARCHAR
317
314
    DRIZZLE_TYPE_VARCHAR,
318
 
    //DRIZZLE_TYPE_DECIMAL
 
315
    //DRIZZLE_TYPE_VIRTUAL
 
316
    DRIZZLE_TYPE_VIRTUAL,
 
317
    //DRIZZLE_TYPE_NEWDECIMAL
319
318
    DRIZZLE_TYPE_VARCHAR,
320
319
    //DRIZZLE_TYPE_ENUM
321
320
    DRIZZLE_TYPE_VARCHAR,
322
321
    //DRIZZLE_TYPE_BLOB
323
322
    DRIZZLE_TYPE_BLOB,
324
 
    //DRIZZLE_TYPE_TIME
325
 
    DRIZZLE_TYPE_TIME,
326
 
    //DRIZZLE_TYPE_BOOLEAN
327
 
    DRIZZLE_TYPE_VARCHAR,
328
 
    //DRIZZLE_TYPE_UUID
329
 
    DRIZZLE_TYPE_VARCHAR,
330
 
    //DRIZZLE_TYPE_MICROTIME
331
 
    DRIZZLE_TYPE_VARCHAR,
332
 
  },
333
 
  /* DRIZZLE_TYPE_DECIMAL -> */
334
 
  {
335
 
    //DRIZZLE_TYPE_LONG
336
 
    DRIZZLE_TYPE_DECIMAL,
 
323
  },
 
324
  /* DRIZZLE_TYPE_VIRTUAL -> */
 
325
  {
 
326
    //DRIZZLE_TYPE_TINY
 
327
    DRIZZLE_TYPE_VIRTUAL,
 
328
    //DRIZZLE_TYPE_LONG
 
329
    DRIZZLE_TYPE_VIRTUAL,
 
330
    //DRIZZLE_TYPE_DOUBLE
 
331
    DRIZZLE_TYPE_VIRTUAL,
 
332
    //DRIZZLE_TYPE_NULL
 
333
    DRIZZLE_TYPE_VIRTUAL,
 
334
    //DRIZZLE_TYPE_TIMESTAMP
 
335
    DRIZZLE_TYPE_VIRTUAL,
 
336
    //DRIZZLE_TYPE_LONGLONG
 
337
    DRIZZLE_TYPE_VIRTUAL,
 
338
    //DRIZZLE_TYPE_DATETIME
 
339
    DRIZZLE_TYPE_VIRTUAL,
 
340
    //DRIZZLE_TYPE_DATE
 
341
    DRIZZLE_TYPE_VIRTUAL,
 
342
    //DRIZZLE_TYPE_VARCHAR
 
343
    DRIZZLE_TYPE_VIRTUAL,
 
344
    //DRIZZLE_TYPE_VIRTUAL
 
345
    DRIZZLE_TYPE_VIRTUAL,
 
346
    //DRIZZLE_TYPE_NEWDECIMAL
 
347
    DRIZZLE_TYPE_VIRTUAL,
 
348
    //DRIZZLE_TYPE_ENUM
 
349
    DRIZZLE_TYPE_VIRTUAL,
 
350
    //DRIZZLE_TYPE_BLOB
 
351
    DRIZZLE_TYPE_VIRTUAL,
 
352
  },
 
353
  /* DRIZZLE_TYPE_NEWDECIMAL -> */
 
354
  {
 
355
    //DRIZZLE_TYPE_TINY
 
356
    DRIZZLE_TYPE_NEWDECIMAL,
 
357
    //DRIZZLE_TYPE_LONG
 
358
    DRIZZLE_TYPE_NEWDECIMAL,
337
359
    //DRIZZLE_TYPE_DOUBLE
338
360
    DRIZZLE_TYPE_DOUBLE,
339
361
    //DRIZZLE_TYPE_NULL
340
 
    DRIZZLE_TYPE_DECIMAL,
 
362
    DRIZZLE_TYPE_NEWDECIMAL,
341
363
    //DRIZZLE_TYPE_TIMESTAMP
342
364
    DRIZZLE_TYPE_VARCHAR,
343
365
    //DRIZZLE_TYPE_LONGLONG
344
 
    DRIZZLE_TYPE_DECIMAL,
 
366
    DRIZZLE_TYPE_NEWDECIMAL,
345
367
    //DRIZZLE_TYPE_DATETIME
346
368
    DRIZZLE_TYPE_VARCHAR,
347
369
    //DRIZZLE_TYPE_DATE
348
370
    DRIZZLE_TYPE_VARCHAR,
349
371
    //DRIZZLE_TYPE_VARCHAR
350
372
    DRIZZLE_TYPE_VARCHAR,
351
 
    //DRIZZLE_TYPE_DECIMAL
352
 
    DRIZZLE_TYPE_DECIMAL,
 
373
    //DRIZZLE_TYPE_VIRTUAL
 
374
    DRIZZLE_TYPE_VIRTUAL,
 
375
    //DRIZZLE_TYPE_NEWDECIMAL
 
376
    DRIZZLE_TYPE_NEWDECIMAL,
353
377
    //DRIZZLE_TYPE_ENUM
354
378
    DRIZZLE_TYPE_VARCHAR,
355
379
    //DRIZZLE_TYPE_BLOB
356
380
    DRIZZLE_TYPE_BLOB,
357
 
    //DRIZZLE_TYPE_TIME
358
 
    DRIZZLE_TYPE_TIME,
359
 
    //DRIZZLE_TYPE_BOOLEAN
360
 
    DRIZZLE_TYPE_VARCHAR,
361
 
    //DRIZZLE_TYPE_UUID
362
 
    DRIZZLE_TYPE_VARCHAR,
363
 
    //DRIZZLE_TYPE_MICROTIME
364
 
    DRIZZLE_TYPE_VARCHAR,
365
381
  },
366
382
  /* DRIZZLE_TYPE_ENUM -> */
367
383
  {
 
384
    //DRIZZLE_TYPE_TINY
 
385
    DRIZZLE_TYPE_VARCHAR,
368
386
    //DRIZZLE_TYPE_LONG
369
387
    DRIZZLE_TYPE_VARCHAR,
370
388
    //DRIZZLE_TYPE_DOUBLE
381
399
    DRIZZLE_TYPE_VARCHAR,
382
400
    //DRIZZLE_TYPE_VARCHAR
383
401
    DRIZZLE_TYPE_VARCHAR,
384
 
    //DRIZZLE_TYPE_DECIMAL
 
402
    //DRIZZLE_TYPE_VIRTUAL
 
403
    DRIZZLE_TYPE_VIRTUAL,
 
404
    //DRIZZLE_TYPE_NEWDECIMAL
385
405
    DRIZZLE_TYPE_VARCHAR,
386
406
    //DRIZZLE_TYPE_ENUM
387
407
    DRIZZLE_TYPE_VARCHAR,
388
408
    //DRIZZLE_TYPE_BLOB
389
409
    DRIZZLE_TYPE_BLOB,
390
 
    //DRIZZLE_TYPE_TIME
391
 
    DRIZZLE_TYPE_TIME,
392
 
    //DRIZZLE_TYPE_BOOLEAN
393
 
    DRIZZLE_TYPE_VARCHAR,
394
 
    //DRIZZLE_TYPE_UUID
395
 
    DRIZZLE_TYPE_VARCHAR,
396
 
    //DRIZZLE_TYPE_MICROTIME
397
 
    DRIZZLE_TYPE_VARCHAR,
398
 
   },
 
410
  },
399
411
  /* DRIZZLE_TYPE_BLOB -> */
400
412
  {
 
413
    //DRIZZLE_TYPE_TINY
 
414
    DRIZZLE_TYPE_BLOB,
401
415
    //DRIZZLE_TYPE_LONG
402
416
    DRIZZLE_TYPE_BLOB,
403
417
    //DRIZZLE_TYPE_DOUBLE
414
428
    DRIZZLE_TYPE_BLOB,
415
429
    //DRIZZLE_TYPE_VARCHAR
416
430
    DRIZZLE_TYPE_BLOB,
417
 
    //DRIZZLE_TYPE_DECIMAL
 
431
    //DRIZZLE_TYPE_VIRTUAL
 
432
    DRIZZLE_TYPE_VIRTUAL,
 
433
    //DRIZZLE_TYPE_NEWDECIMAL
418
434
    DRIZZLE_TYPE_BLOB,
419
435
    //DRIZZLE_TYPE_ENUM
420
436
    DRIZZLE_TYPE_BLOB,
421
437
    //DRIZZLE_TYPE_BLOB
422
438
    DRIZZLE_TYPE_BLOB,
423
 
    //DRIZZLE_TYPE_TIME
424
 
    DRIZZLE_TYPE_TIME,
425
 
    //DRIZZLE_TYPE_BOOLEAN
426
 
    DRIZZLE_TYPE_VARCHAR,
427
 
    //DRIZZLE_TYPE_UUID
428
 
    DRIZZLE_TYPE_VARCHAR,
429
 
    //DRIZZLE_TYPE_MICROTIME
430
 
    DRIZZLE_TYPE_VARCHAR,
431
 
  },
432
 
  /* DRIZZLE_TYPE_TIME -> */
433
 
  {
434
 
    //DRIZZLE_TYPE_LONG
435
 
    DRIZZLE_TYPE_VARCHAR,
436
 
    //DRIZZLE_TYPE_DOUBLE
437
 
    DRIZZLE_TYPE_VARCHAR,
438
 
    //DRIZZLE_TYPE_NULL
439
 
    DRIZZLE_TYPE_TIME,
440
 
    //DRIZZLE_TYPE_TIMESTAMP
441
 
    DRIZZLE_TYPE_VARCHAR,
442
 
    //DRIZZLE_TYPE_LONGLONG
443
 
    DRIZZLE_TYPE_VARCHAR,
444
 
    //DRIZZLE_TYPE_DATETIME
445
 
    DRIZZLE_TYPE_VARCHAR,
446
 
    //DRIZZLE_TYPE_DATE
447
 
    DRIZZLE_TYPE_VARCHAR,
448
 
    //DRIZZLE_TYPE_VARCHAR
449
 
    DRIZZLE_TYPE_VARCHAR,
450
 
    //DRIZZLE_TYPE_DECIMAL
451
 
    DRIZZLE_TYPE_VARCHAR,
452
 
    //DRIZZLE_TYPE_VARCHAR,
453
 
    DRIZZLE_TYPE_VARCHAR,
454
 
    //DRIZZLE_TYPE_BLOB
455
 
    DRIZZLE_TYPE_BLOB,
456
 
    //DRIZZLE_TYPE_TIME
457
 
    DRIZZLE_TYPE_TIME,
458
 
    //DRIZZLE_TYPE_BOOLEAN
459
 
    DRIZZLE_TYPE_VARCHAR,
460
 
    //DRIZZLE_TYPE_UUID
461
 
    DRIZZLE_TYPE_UUID,
462
 
    //DRIZZLE_TYPE_MICROTIME
463
 
    DRIZZLE_TYPE_VARCHAR,
464
 
  },
465
 
  /* DRIZZLE_TYPE_BOOLEAN -> */
466
 
  {
467
 
    //DRIZZLE_TYPE_LONG
468
 
    DRIZZLE_TYPE_VARCHAR,
469
 
    //DRIZZLE_TYPE_DOUBLE
470
 
    DRIZZLE_TYPE_VARCHAR,
471
 
    //DRIZZLE_TYPE_NULL
472
 
    DRIZZLE_TYPE_BOOLEAN,
473
 
    //DRIZZLE_TYPE_TIMESTAMP
474
 
    DRIZZLE_TYPE_VARCHAR,
475
 
    //DRIZZLE_TYPE_LONGLONG
476
 
    DRIZZLE_TYPE_VARCHAR,
477
 
    //DRIZZLE_TYPE_DATETIME
478
 
    DRIZZLE_TYPE_VARCHAR,
479
 
    //DRIZZLE_TYPE_DATE
480
 
    DRIZZLE_TYPE_VARCHAR,
481
 
    //DRIZZLE_TYPE_VARCHAR
482
 
    DRIZZLE_TYPE_VARCHAR,
483
 
    //DRIZZLE_TYPE_DECIMAL
484
 
    DRIZZLE_TYPE_VARCHAR,
485
 
    //DRIZZLE_TYPE_VARCHAR,
486
 
    DRIZZLE_TYPE_VARCHAR,
487
 
    //DRIZZLE_TYPE_BLOB
488
 
    DRIZZLE_TYPE_BLOB,
489
 
    //DRIZZLE_TYPE_TIME
490
 
    DRIZZLE_TYPE_TIME,
491
 
    //DRIZZLE_TYPE_BOOLEAN
492
 
    DRIZZLE_TYPE_BOOLEAN,
493
 
    //DRIZZLE_TYPE_UUID
494
 
    DRIZZLE_TYPE_VARCHAR,
495
 
    //DRIZZLE_TYPE_MICROTIME
496
 
    DRIZZLE_TYPE_VARCHAR,
497
 
  },
498
 
  /* DRIZZLE_TYPE_UUID -> */
499
 
  {
500
 
    //DRIZZLE_TYPE_LONG
501
 
    DRIZZLE_TYPE_VARCHAR,
502
 
    //DRIZZLE_TYPE_DOUBLE
503
 
    DRIZZLE_TYPE_VARCHAR,
504
 
    //DRIZZLE_TYPE_NULL
505
 
    DRIZZLE_TYPE_UUID,
506
 
    //DRIZZLE_TYPE_TIMESTAMP
507
 
    DRIZZLE_TYPE_VARCHAR,
508
 
    //DRIZZLE_TYPE_LONGLONG
509
 
    DRIZZLE_TYPE_VARCHAR,
510
 
    //DRIZZLE_TYPE_DATETIME
511
 
    DRIZZLE_TYPE_VARCHAR,
512
 
    //DRIZZLE_TYPE_DATE
513
 
    DRIZZLE_TYPE_VARCHAR,
514
 
    //DRIZZLE_TYPE_VARCHAR
515
 
    DRIZZLE_TYPE_VARCHAR,
516
 
    //DRIZZLE_TYPE_DECIMAL
517
 
    DRIZZLE_TYPE_VARCHAR,
518
 
    //DRIZZLE_TYPE_VARCHAR,
519
 
    DRIZZLE_TYPE_VARCHAR,
520
 
    //DRIZZLE_TYPE_BLOB
521
 
    DRIZZLE_TYPE_BLOB,
522
 
    //DRIZZLE_TYPE_TIME
523
 
    DRIZZLE_TYPE_TIME,
524
 
    //DRIZZLE_TYPE_BOOLEAN
525
 
    DRIZZLE_TYPE_VARCHAR,
526
 
    //DRIZZLE_TYPE_UUID
527
 
    DRIZZLE_TYPE_UUID,
528
 
    //DRIZZLE_TYPE_MICROTIME
529
 
    DRIZZLE_TYPE_VARCHAR,
530
 
  },
531
 
  /* DRIZZLE_TYPE_MICROTIME -> */
532
 
  {
533
 
    //DRIZZLE_TYPE_LONG
534
 
    DRIZZLE_TYPE_VARCHAR,
535
 
    //DRIZZLE_TYPE_DOUBLE
536
 
    DRIZZLE_TYPE_VARCHAR,
537
 
    //DRIZZLE_TYPE_NULL
538
 
    DRIZZLE_TYPE_MICROTIME,
539
 
    //DRIZZLE_TYPE_TIMESTAMP
540
 
    DRIZZLE_TYPE_VARCHAR,
541
 
    //DRIZZLE_TYPE_LONGLONG
542
 
    DRIZZLE_TYPE_VARCHAR,
543
 
    //DRIZZLE_TYPE_DATETIME
544
 
    DRIZZLE_TYPE_VARCHAR,
545
 
    //DRIZZLE_TYPE_DATE
546
 
    DRIZZLE_TYPE_VARCHAR,
547
 
    //DRIZZLE_TYPE_VARCHAR
548
 
    DRIZZLE_TYPE_VARCHAR,
549
 
    //DRIZZLE_TYPE_DECIMAL
550
 
    DRIZZLE_TYPE_VARCHAR,
551
 
    //DRIZZLE_TYPE_VARCHAR,
552
 
    DRIZZLE_TYPE_VARCHAR,
553
 
    //DRIZZLE_TYPE_BLOB
554
 
    DRIZZLE_TYPE_BLOB,
555
 
    //DRIZZLE_TYPE_TIME
556
 
    DRIZZLE_TYPE_TIME,
557
 
    //DRIZZLE_TYPE_BOOLEAN
558
 
    DRIZZLE_TYPE_VARCHAR,
559
 
    //DRIZZLE_TYPE_UUID
560
 
    DRIZZLE_TYPE_UUID,
561
 
    //DRIZZLE_TYPE_MICROTIME
562
 
    DRIZZLE_TYPE_MICROTIME,
563
439
  },
564
440
};
565
441
 
566
 
static Item_result field_types_result_type [enum_field_types_size]=
567
 
{
 
442
/**
 
443
  Return type of which can carry value of both given types in UNION result.
 
444
 
 
445
  @param a  type for merging
 
446
  @param b  type for merging
 
447
 
 
448
  @return
 
449
    type of field
 
450
*/
 
451
 
 
452
enum_field_types Field::field_type_merge(enum_field_types a,
 
453
                                         enum_field_types b)
 
454
{
 
455
  assert(a <= DRIZZLE_TYPE_MAX);
 
456
  assert(b <= DRIZZLE_TYPE_MAX);
 
457
  return field_types_merge_rules[a][b];
 
458
}
 
459
 
 
460
 
 
461
static Item_result field_types_result_type [DRIZZLE_TYPE_MAX+1]=
 
462
{
 
463
  //DRIZZLE_TYPE_TINY
 
464
  INT_RESULT,
568
465
  //DRIZZLE_TYPE_LONG
569
466
  INT_RESULT,
570
467
  //DRIZZLE_TYPE_DOUBLE
581
478
  STRING_RESULT,
582
479
  //DRIZZLE_TYPE_VARCHAR
583
480
  STRING_RESULT,
584
 
  //DRIZZLE_TYPE_DECIMAL   
 
481
  //DRIZZLE_TYPE_VIRTUAL
 
482
  STRING_RESULT,
 
483
  //DRIZZLE_TYPE_NEWDECIMAL   
585
484
  DECIMAL_RESULT,           
586
485
  //DRIZZLE_TYPE_ENUM
587
486
  STRING_RESULT,
588
487
  //DRIZZLE_TYPE_BLOB
589
488
  STRING_RESULT,
590
 
  //DRIZZLE_TYPE_TIME
591
 
  STRING_RESULT,
592
 
  //DRIZZLE_TYPE_BOOLEAN
593
 
  STRING_RESULT,
594
 
  //DRIZZLE_TYPE_UUID
595
 
  STRING_RESULT,
596
 
  //DRIZZLE_TYPE_MICROTIME
597
 
  STRING_RESULT,
598
489
};
599
490
 
600
 
bool test_if_important_data(const CHARSET_INFO * const cs, 
601
 
                            const char *str,
602
 
                            const char *strend)
 
491
 
 
492
/*
 
493
  Test if the given string contains important data:
 
494
  not spaces for character string,
 
495
  or any data for binary string.
 
496
 
 
497
  SYNOPSIS
 
498
    test_if_important_data()
 
499
    cs          Character set
 
500
    str         String to test
 
501
    strend      String end
 
502
 
 
503
  RETURN
 
504
    false - If string does not have important data
 
505
    true  - If string has some important data
 
506
*/
 
507
 
 
508
bool
 
509
test_if_important_data(const CHARSET_INFO * const cs, const char *str,
 
510
                       const char *strend)
603
511
{
604
512
  if (cs != &my_charset_bin)
605
513
    str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
606
514
  return (str < strend);
607
515
}
608
516
 
609
 
void *Field::operator new(size_t size)
610
 
{
611
 
  return memory::sql_alloc(size);
612
 
}
613
 
 
614
 
void *Field::operator new(size_t size, memory::Root *mem_root)
615
 
{
616
 
  return mem_root->alloc_root(static_cast<uint32_t>(size));
617
 
}
618
 
 
619
 
enum_field_types Field::field_type_merge(enum_field_types a,
620
 
                                         enum_field_types b)
621
 
{
622
 
  assert(a < enum_field_types_size);
623
 
  assert(b < enum_field_types_size);
624
 
  return field_types_merge_rules[a][b];
625
 
}
626
517
 
627
518
Item_result Field::result_merge_type(enum_field_types field_type)
628
519
{
629
 
  assert(field_type < enum_field_types_size);
 
520
  assert(field_type <= DRIZZLE_TYPE_MAX);
630
521
  return field_types_result_type[field_type];
631
522
}
632
523
 
 
524
 
633
525
bool Field::eq(Field *field)
634
526
{
635
527
  return (ptr == field->ptr && null_ptr == field->null_ptr &&
636
528
          null_bit == field->null_bit);
637
529
}
638
530
 
 
531
 
639
532
uint32_t Field::pack_length() const
640
533
{
641
534
  return field_length;
642
535
}
643
536
 
 
537
 
644
538
uint32_t Field::pack_length_in_rec() const
645
539
{
646
540
  return pack_length();
647
541
}
648
542
 
 
543
 
 
544
uint32_t Field::pack_length_from_metadata(uint32_t field_metadata)
 
545
{
 
546
  return field_metadata;
 
547
}
 
548
 
 
549
 
 
550
uint32_t Field::row_pack_length()
 
551
{
 
552
  return 0;
 
553
}
 
554
 
 
555
 
 
556
int Field::save_field_metadata(unsigned char *first_byte)
 
557
{
 
558
  return do_save_field_metadata(first_byte);
 
559
}
 
560
 
 
561
 
649
562
uint32_t Field::data_length()
650
563
{
651
564
  return pack_length();
652
565
}
653
566
 
 
567
 
654
568
uint32_t Field::used_length()
655
569
{
656
570
  return pack_length();
657
571
}
658
572
 
 
573
 
659
574
uint32_t Field::sort_length() const
660
575
{
661
576
  return pack_length();
662
577
}
663
578
 
 
579
 
664
580
uint32_t Field::max_data_length() const
665
581
{
666
582
  return pack_length();
667
583
}
668
584
 
 
585
 
669
586
int Field::reset(void)
670
587
{
671
588
  memset(ptr, 0, pack_length());
672
589
  return 0;
673
590
}
674
591
 
 
592
 
675
593
void Field::reset_fields()
676
594
{}
677
595
 
 
596
 
678
597
void Field::set_default()
679
598
{
680
 
  ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->getInsertRecord());
 
599
  my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->getDefaultValues() - table->record[0]);
681
600
  memcpy(ptr, ptr + l_offset, pack_length());
682
601
  if (null_ptr)
683
602
    *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
684
 
 
685
 
  if (this == table->next_number_field)
686
 
    table->auto_increment_field_not_null= false;
687
603
}
688
604
 
 
605
 
689
606
bool Field::binary() const
690
607
{
691
 
  return true;
 
608
  return 1;
692
609
}
693
610
 
 
611
 
694
612
bool Field::zero_pack() const
695
613
{
696
 
  return true;
 
614
  return 1;
697
615
}
698
616
 
 
617
 
699
618
enum ha_base_keytype Field::key_type() const
700
619
{
701
620
  return HA_KEYTYPE_BINARY;
702
621
}
703
622
 
 
623
 
704
624
uint32_t Field::key_length() const
705
625
{
706
626
  return pack_length();
707
627
}
708
628
 
 
629
 
709
630
enum_field_types Field::real_type() const
710
631
{
711
632
  return type();
712
633
}
713
634
 
 
635
 
714
636
int Field::cmp_max(const unsigned char *a, const unsigned char *b, uint32_t)
715
637
{
716
638
  return cmp(a, b);
717
639
}
718
640
 
 
641
 
719
642
int Field::cmp_binary(const unsigned char *a,const unsigned char *b, uint32_t)
720
643
{
721
644
  return memcmp(a,b,pack_length());
722
645
}
723
646
 
 
647
 
724
648
int Field::cmp_offset(uint32_t row_offset)
725
649
{
726
650
  return cmp(ptr,ptr+row_offset);
727
651
}
728
652
 
 
653
 
729
654
int Field::cmp_binary_offset(uint32_t row_offset)
730
655
{
731
656
  return cmp_binary(ptr, ptr+row_offset);
732
657
}
733
658
 
 
659
 
734
660
int Field::key_cmp(const unsigned char *a,const unsigned char *b)
735
661
{
736
662
  return cmp(a, b);
737
663
}
738
664
 
 
665
 
739
666
int Field::key_cmp(const unsigned char *str, uint32_t)
740
667
{
741
668
  return cmp(ptr,str);
742
669
}
743
670
 
 
671
 
744
672
uint32_t Field::decimals() const
745
673
{
746
674
  return 0;
747
675
}
748
676
 
749
 
bool Field::is_null(ptrdiff_t row_offset) const
 
677
 
 
678
bool Field::is_null(my_ptrdiff_t row_offset)
750
679
{
751
680
  return null_ptr ?
752
681
    (null_ptr[row_offset] & null_bit ? true : false) :
753
682
    table->null_row;
754
683
}
755
684
 
756
 
bool Field::is_real_null(ptrdiff_t row_offset) const
 
685
 
 
686
bool Field::is_real_null(my_ptrdiff_t row_offset)
757
687
{
758
688
  return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : false;
759
689
}
760
690
 
761
 
bool Field::is_null_in_record(const unsigned char *record) const
 
691
 
 
692
bool Field::is_null_in_record(const unsigned char *record)
762
693
{
763
 
  if (! null_ptr)
 
694
  if (!null_ptr)
764
695
    return false;
765
 
  return test(record[(uint32_t) (null_ptr -table->getInsertRecord())] & null_bit);
 
696
  return test(record[(uint32_t) (null_ptr -table->record[0])] &
 
697
              null_bit);
766
698
}
767
699
 
768
 
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset) const
 
700
 
 
701
bool Field::is_null_in_record_with_offset(my_ptrdiff_t with_offset)
769
702
{
770
 
  if (! null_ptr)
 
703
  if (!null_ptr)
771
704
    return false;
772
705
  return test(null_ptr[with_offset] & null_bit);
773
706
}
774
707
 
775
 
void Field::set_null(ptrdiff_t row_offset)
 
708
 
 
709
void Field::set_null(my_ptrdiff_t row_offset)
776
710
{
777
711
  if (null_ptr)
778
712
    null_ptr[row_offset]|= null_bit;
779
713
}
780
714
 
781
 
void Field::set_notnull(ptrdiff_t row_offset)
 
715
 
 
716
void Field::set_notnull(my_ptrdiff_t row_offset)
782
717
{
783
718
  if (null_ptr)
784
719
    null_ptr[row_offset]&= (unsigned char) ~null_bit;
785
720
}
786
721
 
787
 
bool Field::maybe_null(void) const
 
722
 
 
723
bool Field::maybe_null(void)
788
724
{
789
725
  return null_ptr != 0 || table->maybe_null;
790
726
}
791
727
 
792
 
bool Field::real_maybe_null(void) const
 
728
 
 
729
bool Field::real_maybe_null(void)
793
730
{
794
731
  return null_ptr != 0;
795
732
}
796
733
 
 
734
 
 
735
size_t Field::last_null_byte() const
 
736
{
 
737
  size_t bytes= do_last_null_byte();
 
738
  assert(bytes <= table->getNullBytes());
 
739
  return bytes;
 
740
}
 
741
 
 
742
 
 
743
/*****************************************************************************
 
744
  Static help functions
 
745
*****************************************************************************/
 
746
 
797
747
bool Field::type_can_have_key_part(enum enum_field_types type)
798
748
{
799
749
  switch (type) {
805
755
  }
806
756
}
807
757
 
 
758
 
 
759
/**
 
760
  Process decimal library return codes and issue warnings for overflow and
 
761
  truncation.
 
762
 
 
763
  @param op_result  decimal library return code (E_DEC_* see include/decimal.h)
 
764
 
 
765
  @retval
 
766
    1  there was overflow
 
767
  @retval
 
768
    0  no error or some other errors except overflow
 
769
*/
 
770
 
808
771
int Field::warn_if_overflow(int op_result)
809
772
{
810
773
  if (op_result == E_DEC_OVERFLOW)
811
774
  {
812
775
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
813
 
    return E_DEC_OVERFLOW;
 
776
    return 1;
814
777
  }
815
778
  if (op_result == E_DEC_TRUNCATED)
816
779
  {
817
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
818
 
    return E_DEC_TRUNCATED;
 
780
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE, ER_WARN_DATA_TRUNCATED, 1);
 
781
    /* We return 0 here as this is not a critical issue */
819
782
  }
820
783
  return 0;
821
784
}
822
785
 
 
786
 
823
787
void Field::init(Table *table_arg)
824
788
{
825
789
  orig_table= table= table_arg;
826
 
}
 
790
  table_name= &table_arg->alias;
 
791
}
 
792
 
 
793
 
 
794
#ifdef NOT_USED
 
795
static bool test_if_real(const char *str,int length, const CHARSET_INFO * const cs)
 
796
{
 
797
  cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
 
798
 
 
799
  while (length && my_isspace(cs,*str))
 
800
  {                                             // Allow start space
 
801
    length--; str++;
 
802
  }
 
803
  if (!length)
 
804
    return 0;
 
805
  if (*str == '+' || *str == '-')
 
806
  {
 
807
    length--; str++;
 
808
    if (!length || !(my_isdigit(cs,*str) || *str == '.'))
 
809
      return 0;
 
810
  }
 
811
  while (length && my_isdigit(cs,*str))
 
812
  {
 
813
    length--; str++;
 
814
  }
 
815
  if (!length)
 
816
    return 1;
 
817
  if (*str == '.')
 
818
  {
 
819
    length--; str++;
 
820
    while (length && my_isdigit(cs,*str))
 
821
    {
 
822
      length--; str++;
 
823
    }
 
824
  }
 
825
  if (!length)
 
826
    return 1;
 
827
  if (*str == 'E' || *str == 'e')
 
828
  {
 
829
    if (length < 3 || (str[1] != '+' && str[1] != '-') ||
 
830
        !my_isdigit(cs,str[2]))
 
831
      return 0;
 
832
    length-=3;
 
833
    str+=3;
 
834
    while (length && my_isdigit(cs,*str))
 
835
    {
 
836
      length--; str++;
 
837
    }
 
838
  }
 
839
  for (; length ; length--, str++)
 
840
  {                                             // Allow end space
 
841
    if (!my_isspace(cs,*str))
 
842
      return 0;
 
843
  }
 
844
  return 1;
 
845
}
 
846
#endif
 
847
 
 
848
 
 
849
/**
 
850
  Interpret field value as an integer but return the result as a string.
 
851
 
 
852
  This is used for printing bit_fields as numbers while debugging.
 
853
*/
 
854
 
 
855
String *Field::val_int_as_str(String *val_buffer, bool unsigned_val)
 
856
{
 
857
  const CHARSET_INFO * const cs= &my_charset_bin;
 
858
  uint32_t length;
 
859
  int64_t value= val_int();
 
860
 
 
861
  if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
 
862
    return 0;
 
863
  length= (uint32_t) (*cs->cset->int64_t10_to_str)(cs, (char*) val_buffer->ptr(),
 
864
                                                MY_INT64_NUM_DECIMAL_DIGITS,
 
865
                                                unsigned_val ? 10 : -10,
 
866
                                                value);
 
867
  val_buffer->length(length);
 
868
  return val_buffer;
 
869
}
 
870
 
827
871
 
828
872
/// This is used as a table name when the table structure is not set up
829
 
Field::Field(unsigned char *ptr_arg,
830
 
             uint32_t length_arg,
831
 
             unsigned char *null_ptr_arg,
 
873
Field::Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
832
874
             unsigned char null_bit_arg,
833
 
             utype unireg_check_arg, 
834
 
             const char *field_name_arg) :
835
 
    ptr(ptr_arg),
836
 
    null_ptr(null_ptr_arg),
837
 
    table(NULL),
838
 
    orig_table(NULL),
839
 
    field_name(field_name_arg),
840
 
    comment(NULL_LEX_STRING),
841
 
    key_start(0),
842
 
    part_of_key(0),
843
 
    part_of_key_not_clustered(0),
844
 
    part_of_sortkey(0),
845
 
    unireg_check(unireg_check_arg),
846
 
    field_length(length_arg),
847
 
    flags(null_ptr ? 0: NOT_NULL_FLAG),
848
 
    field_index(0),
849
 
    null_bit(null_bit_arg),
850
 
    is_created_from_null_item(false)
 
875
             utype unireg_check_arg, const char *field_name_arg)
 
876
  :ptr(ptr_arg), null_ptr(null_ptr_arg),
 
877
   table(0), orig_table(0), table_name(0),
 
878
   field_name(field_name_arg),
 
879
   key_start(0), part_of_key(0), part_of_key_not_clustered(0),
 
880
   part_of_sortkey(0), unireg_check(unireg_check_arg),
 
881
   field_length(length_arg), null_bit(null_bit_arg),
 
882
   is_created_from_null_item(false),
 
883
   vcol_info(NULL), is_stored(true)
851
884
{
 
885
  flags=null_ptr ? 0: NOT_NULL_FLAG;
 
886
  comment.str= (char*) "";
 
887
  comment.length=0;
 
888
  field_index= 0;
852
889
}
853
890
 
854
 
void Field::hash(uint32_t *nr, uint32_t *nr2) const
 
891
 
 
892
void Field::hash(uint32_t *nr, uint32_t *nr2)
855
893
{
856
894
  if (is_null())
857
895
  {
865
903
  }
866
904
}
867
905
 
 
906
size_t
 
907
Field::do_last_null_byte() const
 
908
{
 
909
  assert(null_ptr == NULL || null_ptr >= table->record[0]);
 
910
  if (null_ptr)
 
911
    return (size_t) (null_ptr - table->record[0]) + 1;
 
912
  return LAST_NULL_BYTE_UNDEF;
 
913
}
 
914
 
 
915
 
868
916
void Field::copy_from_tmp(int row_offset)
869
917
{
870
918
  memcpy(ptr,ptr+row_offset,pack_length());
877
925
  }
878
926
}
879
927
 
880
 
int Field::store_and_check(enum_check_fields check_level,
881
 
                           const char *to, 
882
 
                           uint32_t length,
883
 
                           const CHARSET_INFO * const cs)
884
 
 
 
928
/**
 
929
   Check to see if field size is compatible with destination.
 
930
 
 
931
   This method is used in row-based replication to verify that the slave's
 
932
   field size is less than or equal to the master's field size. The
 
933
   encoded field metadata (from the master or source) is decoded and compared
 
934
   to the size of this field (the slave or destination).
 
935
 
 
936
   @param   field_metadata   Encoded size in field metadata
 
937
 
 
938
   @retval 0 if this field's size is < the source field's size
 
939
   @retval 1 if this field's size is >= the source field's size
 
940
*/
 
941
int Field::compatible_field_size(uint32_t field_metadata)
 
942
{
 
943
  uint32_t const source_size= pack_length_from_metadata(field_metadata);
 
944
  uint32_t const destination_size= row_pack_length();
 
945
  return (source_size <= destination_size);
 
946
}
 
947
 
 
948
 
 
949
int Field::store(const char *to, uint32_t length,
 
950
                 const CHARSET_INFO * const cs,
 
951
                 enum_check_fields check_level)
885
952
{
886
953
  int res;
887
954
  enum_check_fields old_check_level= table->in_use->count_cuted_fields;
891
958
  return res;
892
959
}
893
960
 
894
 
unsigned char *Field::pack(unsigned char *to, const unsigned char *from, uint32_t max_length, bool)
 
961
 
 
962
/**
 
963
   Pack the field into a format suitable for storage and transfer.
 
964
 
 
965
   To implement packing functionality, only the virtual function
 
966
   should be overridden. The other functions are just convenience
 
967
   functions and hence should not be overridden.
 
968
 
 
969
   The value of <code>low_byte_first</code> is dependent on how the
 
970
   packed data is going to be used: for local use, e.g., temporary
 
971
   store on disk or in memory, use the native format since that is
 
972
   faster. For data that is going to be transfered to other machines
 
973
   (e.g., when writing data to the binary log), data should always be
 
974
   stored in little-endian format.
 
975
 
 
976
   @note The default method for packing fields just copy the raw bytes
 
977
   of the record into the destination, but never more than
 
978
   <code>max_length</code> characters.
 
979
 
 
980
   @param to
 
981
   Pointer to memory area where representation of field should be put.
 
982
 
 
983
   @param from
 
984
   Pointer to memory area where record representation of field is
 
985
   stored.
 
986
 
 
987
   @param max_length
 
988
   Maximum length of the field, as given in the column definition. For
 
989
   example, for <code>CHAR(1000)</code>, the <code>max_length</code>
 
990
   is 1000. This information is sometimes needed to decide how to pack
 
991
   the data.
 
992
 
 
993
   @param low_byte_first
 
994
   @c true if integers should be stored little-endian, @c false if
 
995
   native format should be used. Note that for little-endian machines,
 
996
   the value of this flag is a moot point since the native format is
 
997
   little-endian.
 
998
*/
 
999
unsigned char *
 
1000
Field::pack(unsigned char *to, const unsigned char *from, uint32_t max_length,
 
1001
            bool)
895
1002
{
896
1003
  uint32_t length= pack_length();
897
1004
  set_if_smaller(length, max_length);
899
1006
  return to+length;
900
1007
}
901
1008
 
 
1009
 
902
1010
unsigned char *Field::pack(unsigned char *to, const unsigned char *from)
903
1011
{
904
 
  unsigned char *result= this->pack(to, from, UINT32_MAX, table->getShare()->db_low_byte_first);
 
1012
  unsigned char *result= this->pack(to, from, UINT32_MAX,
 
1013
                                    table->s->db_low_byte_first);
905
1014
  return(result);
906
1015
}
907
1016
 
908
 
const unsigned char *Field::unpack(unsigned char* to,
909
 
                                   const unsigned char *from, 
910
 
                                   uint32_t param_data,
911
 
                                   bool)
 
1017
 
 
1018
/**
 
1019
   Unpack a field from row data.
 
1020
 
 
1021
   This method is used to unpack a field from a master whose size of
 
1022
   the field is less than that of the slave.
 
1023
 
 
1024
   The <code>param_data</code> parameter is a two-byte integer (stored
 
1025
   in the least significant 16 bits of the unsigned integer) usually
 
1026
   consisting of two parts: the real type in the most significant byte
 
1027
   and a original pack length in the least significant byte.
 
1028
 
 
1029
   The exact layout of the <code>param_data</code> field is given by
 
1030
   the <code>Table_map_log_event::save_field_metadata()</code>.
 
1031
 
 
1032
   This is the default method for unpacking a field. It just copies
 
1033
   the memory block in byte order (of original pack length bytes or
 
1034
   length of field, whichever is smaller).
 
1035
 
 
1036
   @param   to         Destination of the data
 
1037
   @param   from       Source of the data
 
1038
   @param   param_data Real type and original pack length of the field
 
1039
                       data
 
1040
 
 
1041
   @param low_byte_first
 
1042
   If this flag is @c true, all composite entities (e.g., lengths)
 
1043
   should be unpacked in little-endian format; otherwise, the entities
 
1044
   are unpacked in native order.
 
1045
 
 
1046
   @return  New pointer into memory based on from + length of the data
 
1047
*/
 
1048
const unsigned char *
 
1049
Field::unpack(unsigned char* to, const unsigned char *from, uint32_t param_data,
 
1050
              bool)
912
1051
{
913
1052
  uint32_t length=pack_length();
914
1053
  int from_type= 0;
934
1073
            param_data : length;
935
1074
 
936
1075
  memcpy(to, from, param_data > length ? length : len);
937
 
  return (from + len);
 
1076
  return from+len;
938
1077
}
939
1078
 
940
 
const unsigned char *Field::unpack(unsigned char* to, const unsigned char *from)
 
1079
 
 
1080
const unsigned char *Field::unpack(unsigned char* to,
 
1081
                                   const unsigned char *from)
941
1082
{
942
 
  const unsigned char *result= unpack(to, from, 0U, table->getShare()->db_low_byte_first);
 
1083
  const unsigned char *result= unpack(to, from, 0U,
 
1084
                                      table->s->db_low_byte_first);
943
1085
  return(result);
944
1086
}
945
1087
 
946
 
type::Decimal *Field::val_decimal(type::Decimal *) const
 
1088
 
 
1089
uint32_t Field::packed_col_length(const unsigned char *, uint32_t length)
 
1090
{
 
1091
  return length;
 
1092
}
 
1093
 
 
1094
 
 
1095
int Field::pack_cmp(const unsigned char *a, const unsigned char *b,
 
1096
                    uint32_t, bool)
 
1097
{
 
1098
  return cmp(a,b);
 
1099
}
 
1100
 
 
1101
 
 
1102
int Field::pack_cmp(const unsigned char *b, uint32_t, bool)
 
1103
{
 
1104
  return cmp(ptr,b);
 
1105
}
 
1106
 
 
1107
 
 
1108
my_decimal *Field::val_decimal(my_decimal *)
947
1109
{
948
1110
  /* This never have to be called */
949
1111
  assert(0);
951
1113
}
952
1114
 
953
1115
 
954
 
void Field::make_field(SendField *field)
 
1116
void Field::make_field(Send_field *field)
955
1117
{
956
 
  if (orig_table && orig_table->getShare()->getSchemaName() && *orig_table->getShare()->getSchemaName())
 
1118
  if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
957
1119
  {
958
 
    field->db_name= orig_table->getShare()->getSchemaName();
959
 
    field->org_table_name= orig_table->getShare()->getTableName();
 
1120
    field->db_name= orig_table->s->db.str;
 
1121
    field->org_table_name= orig_table->s->table_name.str;
960
1122
  }
961
1123
  else
962
1124
    field->org_table_name= field->db_name= "";
963
1125
  if (orig_table)
964
1126
  {
965
 
    field->table_name= orig_table->getAlias();
 
1127
    field->table_name= orig_table->alias;
966
1128
    field->org_col_name= field_name;
967
1129
  }
968
1130
  else
972
1134
  }
973
1135
  field->col_name= field_name;
974
1136
  field->charsetnr= charset()->number;
975
 
  field->length= field_length;
976
 
  field->type= type();
977
 
  field->flags= table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
 
1137
  field->length=field_length;
 
1138
  field->type=type();
 
1139
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
978
1140
  field->decimals= 0;
979
1141
}
980
1142
 
981
 
int64_t Field::convert_decimal2int64_t(const type::Decimal *val, bool, int *err)
 
1143
 
 
1144
/**
 
1145
  Conversion from decimal to int64_t with checking overflow and
 
1146
  setting correct value (min/max) in case of overflow.
 
1147
 
 
1148
  @param val             value which have to be converted
 
1149
  @param unsigned_flag   type of integer in which we convert val
 
1150
  @param err             variable to pass error code
 
1151
 
 
1152
  @return
 
1153
    value converted from val
 
1154
*/
 
1155
int64_t Field::convert_decimal2int64_t(const my_decimal *val, bool, int *err)
982
1156
{
983
1157
  int64_t i;
984
 
  if (warn_if_overflow(val->val_int32(E_DEC_ERROR &
 
1158
  if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
985
1159
                                      ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
986
 
                                      false, &i)))
 
1160
                                      val, false, &i)))
987
1161
  {
988
1162
    i= (val->sign() ? INT64_MIN : INT64_MAX);
989
1163
    *err= 1;
991
1165
  return i;
992
1166
}
993
1167
 
994
 
uint32_t Field::fill_cache_field(CacheField *copy)
 
1168
uint32_t Field::fill_cache_field(CACHE_FIELD *copy)
995
1169
{
996
1170
  uint32_t store_length;
997
1171
  copy->str=ptr;
1001
1175
  {
1002
1176
    copy->blob_field=(Field_blob*) this;
1003
1177
    copy->strip=0;
1004
 
    copy->length-= table->getShare()->sizeBlobPtr();
 
1178
    copy->length-= table->s->blob_ptr_size;
1005
1179
    return copy->length;
1006
1180
  }
1007
1181
  else
1012
1186
  return copy->length+ store_length;
1013
1187
}
1014
1188
 
1015
 
bool Field::get_date(type::Time &ltime, uint32_t fuzzydate) const
1016
 
{
1017
 
  char buff[type::Time::MAX_STRING_LENGTH];
1018
 
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1019
 
 
1020
 
  assert(getTable() and getTable()->getSession());
1021
 
 
1022
 
  if (not (res= val_str_internal(&tmp)) or
1023
 
      str_to_datetime_with_warn(getTable()->getSession(),
1024
 
                                res->ptr(), res->length(),
1025
 
                                &ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
1026
 
  {
1027
 
    return true;
1028
 
  }
1029
 
 
1030
 
  return false;
1031
 
}
1032
 
 
1033
 
bool Field::get_time(type::Time &ltime) const
1034
 
{
1035
 
  char buff[type::Time::MAX_STRING_LENGTH];
1036
 
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1037
 
 
1038
 
  if (not (res= val_str_internal(&tmp)) or
1039
 
      str_to_time_with_warn(getTable()->getSession(), res->ptr(), res->length(), &ltime))
1040
 
  {
1041
 
    return true;
1042
 
  }
1043
 
 
1044
 
  return false;
1045
 
}
1046
 
 
1047
 
int Field::store_time(type::Time &ltime, type::timestamp_t)
1048
 
{
1049
 
  String tmp;
1050
 
 
1051
 
  ltime.convert(tmp);
1052
 
 
1053
 
  return store(tmp.ptr(), tmp.length(), &my_charset_bin);
1054
 
}
1055
 
 
1056
 
bool Field::optimize_range(uint32_t idx, uint32_t)
1057
 
{
1058
 
  return test(table->index_flags(idx) & HA_READ_RANGE);
1059
 
}
1060
 
 
1061
 
Field *Field::new_field(memory::Root *root, Table *new_table, bool)
 
1189
 
 
1190
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
1191
{
 
1192
  char buff[40];
 
1193
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
 
1194
  if (!(res=val_str(&tmp)) ||
 
1195
      str_to_datetime_with_warn(res->ptr(), res->length(),
 
1196
                                ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
 
1197
    return 1;
 
1198
  return 0;
 
1199
}
 
1200
 
 
1201
bool Field::get_time(DRIZZLE_TIME *ltime)
 
1202
{
 
1203
  char buff[40];
 
1204
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
 
1205
  if (!(res=val_str(&tmp)) ||
 
1206
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
 
1207
    return 1;
 
1208
  return 0;
 
1209
}
 
1210
 
 
1211
/**
 
1212
  This is called when storing a date in a string.
 
1213
 
 
1214
  @note
 
1215
    Needs to be changed if/when we want to support different time formats.
 
1216
*/
 
1217
 
 
1218
int Field::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
 
1219
{
 
1220
  char buff[MAX_DATE_STRING_REP_LENGTH];
 
1221
  uint32_t length= (uint32_t) my_TIME_to_str(ltime, buff);
 
1222
  return store(buff, length, &my_charset_bin);
 
1223
}
 
1224
 
 
1225
 
 
1226
bool Field::optimize_range(uint32_t idx, uint32_t part)
 
1227
{
 
1228
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
 
1229
}
 
1230
 
 
1231
 
 
1232
Field *Field::new_field(MEM_ROOT *root, Table *new_table, bool)
1062
1233
{
1063
1234
  Field *tmp;
1064
 
  if (!(tmp= (Field*) root->memdup_root((char*) this,size_of())))
 
1235
  if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1065
1236
    return 0;
1066
1237
 
1067
1238
  if (tmp->table->maybe_null)
1068
1239
    tmp->flags&= ~NOT_NULL_FLAG;
1069
1240
  tmp->table= new_table;
1070
 
  tmp->key_start.reset();
1071
 
  tmp->part_of_key.reset();
1072
 
  tmp->part_of_sortkey.reset();
 
1241
  tmp->key_start.init(0);
 
1242
  tmp->part_of_key.init(0);
 
1243
  tmp->part_of_sortkey.init(0);
1073
1244
  tmp->unireg_check= Field::NONE;
1074
 
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG);
 
1245
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1075
1246
  tmp->reset_fields();
1076
1247
  return tmp;
1077
1248
}
1078
1249
 
1079
 
Field *Field::new_key_field(memory::Root *root, Table *new_table,
1080
 
                            unsigned char *new_ptr,
1081
 
                            unsigned char *new_null_ptr,
 
1250
 
 
1251
Field *Field::new_key_field(MEM_ROOT *root, Table *new_table,
 
1252
                            unsigned char *new_ptr, unsigned char *new_null_ptr,
1082
1253
                            uint32_t new_null_bit)
1083
1254
{
1084
1255
  Field *tmp;
1085
1256
  if ((tmp= new_field(root, new_table, table == new_table)))
1086
1257
  {
1087
 
    tmp->ptr= new_ptr;
 
1258
    tmp->ptr=      new_ptr;
1088
1259
    tmp->null_ptr= new_null_ptr;
1089
1260
    tmp->null_bit= new_null_bit;
1090
1261
  }
1091
1262
  return tmp;
1092
1263
}
1093
1264
 
1094
 
Field *Field::clone(memory::Root *root, Table *new_table)
 
1265
 
 
1266
/* This is used to generate a field in Table from TABLE_SHARE */
 
1267
 
 
1268
Field *Field::clone(MEM_ROOT *root, Table *new_table)
1095
1269
{
1096
1270
  Field *tmp;
1097
 
  if ((tmp= (Field*) root->memdup_root((char*) this,size_of())))
 
1271
  if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1098
1272
  {
1099
1273
    tmp->init(new_table);
1100
 
    tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() -
1101
 
                                           new_table->getDefaultValues()));
 
1274
    tmp->move_field_offset((my_ptrdiff_t) (new_table->record[0] -
 
1275
                                           new_table->s->default_values));
1102
1276
  }
1103
1277
  return tmp;
1104
1278
}
1105
1279
 
1106
1280
 
1107
 
uint32_t Field::is_equal(CreateField *new_field_ptr)
 
1281
uint32_t Field::is_equal(Create_field *new_field_ptr)
1108
1282
{
1109
1283
  return (new_field_ptr->sql_type == real_type());
1110
1284
}
1111
1285
 
 
1286
/**
 
1287
  @retval
 
1288
    1  if the fields are equally defined
 
1289
  @retval
 
1290
    0  if the fields are unequally defined
 
1291
*/
 
1292
 
1112
1293
bool Field::eq_def(Field *field)
1113
1294
{
1114
1295
  if (real_type() != field->real_type() || charset() != field->charset() ||
1117
1298
  return 1;
1118
1299
}
1119
1300
 
 
1301
/**
 
1302
  @return
 
1303
  returns 1 if the fields are equally defined
 
1304
*/
1120
1305
bool Field_enum::eq_def(Field *field)
1121
1306
{
1122
1307
  if (!Field::eq_def(field))
1123
1308
    return 0;
1124
 
 
1125
1309
  TYPELIB *from_lib=((Field_enum*) field)->typelib;
1126
1310
 
1127
1311
  if (typelib->count < from_lib->count)
1128
1312
    return 0;
1129
 
 
1130
1313
  for (uint32_t i=0 ; i < from_lib->count ; i++)
1131
 
  {
1132
1314
    if (my_strnncoll(field_charset,
1133
1315
                     (const unsigned char*)typelib->type_names[i],
1134
1316
                     strlen(typelib->type_names[i]),
1135
1317
                     (const unsigned char*)from_lib->type_names[i],
1136
1318
                     strlen(from_lib->type_names[i])))
1137
1319
      return 0;
1138
 
  }
1139
 
 
1140
1320
  return 1;
1141
1321
}
1142
1322
 
 
1323
/*****************************************************************************
 
1324
  Handling of field and Create_field
 
1325
*****************************************************************************/
 
1326
 
 
1327
/**
 
1328
  Convert create_field::length from number of characters to number of bytes.
 
1329
*/
 
1330
 
 
1331
void Create_field::create_length_to_internal_length(void)
 
1332
{
 
1333
  switch (sql_type) {
 
1334
  case DRIZZLE_TYPE_BLOB:
 
1335
  case DRIZZLE_TYPE_VARCHAR:
 
1336
    length*= charset->mbmaxlen;
 
1337
    key_length= length;
 
1338
    pack_length= calc_pack_length(sql_type, length);
 
1339
    break;
 
1340
  case DRIZZLE_TYPE_ENUM:
 
1341
    /* Pack_length already calculated in ::init() */
 
1342
    length*= charset->mbmaxlen;
 
1343
    key_length= pack_length;
 
1344
    break;
 
1345
  case DRIZZLE_TYPE_NEWDECIMAL:
 
1346
    key_length= pack_length=
 
1347
      my_decimal_get_binary_size(my_decimal_length_to_precision(length,
 
1348
                                                                decimals,
 
1349
                                                                flags &
 
1350
                                                                UNSIGNED_FLAG),
 
1351
                                 decimals);
 
1352
    break;
 
1353
  default:
 
1354
    key_length= pack_length= calc_pack_length(sql_type, length);
 
1355
    break;
 
1356
  }
 
1357
}
 
1358
 
 
1359
 
 
1360
/**
 
1361
  Init for a tmp table field. To be extended if need be.
 
1362
*/
 
1363
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
 
1364
                                      uint32_t length_arg, uint32_t decimals_arg,
 
1365
                                      bool maybe_null, bool is_unsigned)
 
1366
{
 
1367
  field_name= "";
 
1368
  sql_type= sql_type_arg;
 
1369
  char_length= length= length_arg;;
 
1370
  unireg_check= Field::NONE;
 
1371
  interval= 0;
 
1372
  charset= &my_charset_bin;
 
1373
  pack_flag= (FIELDFLAG_NUMBER |
 
1374
              ((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
 
1375
              (maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
 
1376
              (is_unsigned ? 0 : FIELDFLAG_DECIMAL));
 
1377
  vcol_info= NULL;
 
1378
  is_stored= true;
 
1379
}
 
1380
 
 
1381
 
 
1382
/**
 
1383
  Initialize field definition for create.
 
1384
 
 
1385
  @param session                   Thread handle
 
1386
  @param fld_name              Field name
 
1387
  @param fld_type              Field type
 
1388
  @param fld_length            Field length
 
1389
  @param fld_decimals          Decimal (if any)
 
1390
  @param fld_type_modifier     Additional type information
 
1391
  @param fld_default_value     Field default value (if any)
 
1392
  @param fld_on_update_value   The value of ON UPDATE clause
 
1393
  @param fld_comment           Field comment
 
1394
  @param fld_change            Field change
 
1395
  @param fld_interval_list     Interval list (if any)
 
1396
  @param fld_charset           Field charset
 
1397
  @param fld_vcol_info         Virtual column data
 
1398
 
 
1399
  @retval
 
1400
    false on success
 
1401
  @retval
 
1402
    true  on error
 
1403
*/
 
1404
 
 
1405
bool Create_field::init(Session *, char *fld_name, enum_field_types fld_type,
 
1406
                        char *fld_length, char *fld_decimals,
 
1407
                        uint32_t fld_type_modifier, Item *fld_default_value,
 
1408
                        Item *fld_on_update_value, LEX_STRING *fld_comment,
 
1409
                        char *fld_change, List<String> *fld_interval_list,
 
1410
                        const CHARSET_INFO * const fld_charset,
 
1411
                        uint32_t, enum column_format_type column_format_in,
 
1412
                        virtual_column_info *fld_vcol_info)
 
1413
{
 
1414
  uint32_t sign_len, allowed_type_modifier= 0;
 
1415
  uint32_t max_field_charlength= MAX_FIELD_CHARLENGTH;
 
1416
 
 
1417
  field= 0;
 
1418
  field_name= fld_name;
 
1419
  def= fld_default_value;
 
1420
  flags= fld_type_modifier;
 
1421
  flags|= (((uint32_t)column_format_in & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
 
1422
  unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
 
1423
                 Field::NEXT_NUMBER : Field::NONE);
 
1424
  decimals= fld_decimals ? (uint32_t)atoi(fld_decimals) : 0;
 
1425
  if (decimals >= NOT_FIXED_DEC)
 
1426
  {
 
1427
    my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
 
1428
             NOT_FIXED_DEC-1);
 
1429
    return(true);
 
1430
  }
 
1431
 
 
1432
  sql_type= fld_type;
 
1433
  length= 0;
 
1434
  change= fld_change;
 
1435
  interval= 0;
 
1436
  pack_length= key_length= 0;
 
1437
  charset= fld_charset;
 
1438
  interval_list.empty();
 
1439
 
 
1440
  comment= *fld_comment;
 
1441
  vcol_info= fld_vcol_info;
 
1442
  is_stored= true;
 
1443
 
 
1444
  /* Initialize data for a virtual field */
 
1445
  if (fld_type == DRIZZLE_TYPE_VIRTUAL)
 
1446
  {
 
1447
    assert(vcol_info && vcol_info->expr_item);
 
1448
    is_stored= vcol_info->get_field_stored();
 
1449
    /*
 
1450
      Perform per item-type checks to determine if the expression is
 
1451
      allowed for a virtual column.
 
1452
      Note that validation of the specific function is done later in
 
1453
      procedures open_table_from_share and fix_fields_vcol_func
 
1454
    */
 
1455
    switch (vcol_info->expr_item->type()) {
 
1456
    case Item::FUNC_ITEM:
 
1457
         if (((Item_func *)vcol_info->expr_item)->functype() == Item_func::FUNC_SP)
 
1458
         {
 
1459
           my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
 
1460
           return(true);
 
1461
         }
 
1462
         break;
 
1463
    case Item::COPY_STR_ITEM:
 
1464
    case Item::FIELD_AVG_ITEM:
 
1465
    case Item::PROC_ITEM:
 
1466
    case Item::REF_ITEM:
 
1467
    case Item::FIELD_STD_ITEM:
 
1468
    case Item::FIELD_VARIANCE_ITEM:
 
1469
    case Item::INSERT_VALUE_ITEM:
 
1470
    case Item::SUBSELECT_ITEM:
 
1471
    case Item::CACHE_ITEM:
 
1472
    case Item::TYPE_HOLDER:
 
1473
    case Item::PARAM_ITEM:
 
1474
         my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
 
1475
         return true;
 
1476
    default:
 
1477
      // Continue with the field creation
 
1478
      break;
 
1479
    }
 
1480
    /*
 
1481
      Make a field created for the real type.
 
1482
      Note that "real" and virtual fields differ from each other
 
1483
      only by Field::vcol_info, which is always 0 for normal columns.
 
1484
      vcol_info is updated for fields later in procedure open_binary_frm.
 
1485
    */
 
1486
    sql_type= fld_type= vcol_info->get_real_type();
 
1487
  }
 
1488
 
 
1489
  /*
 
1490
    Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
 
1491
    it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
 
1492
  */
 
1493
  if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
 
1494
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != DRIZZLE_TYPE_TIMESTAMP)
 
1495
    flags|= NO_DEFAULT_VALUE_FLAG;
 
1496
 
 
1497
  if (fld_length && !(length= (uint32_t) atoi(fld_length)))
 
1498
    fld_length= 0; /* purecov: inspected */
 
1499
  sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
 
1500
 
 
1501
  switch (fld_type) {
 
1502
  case DRIZZLE_TYPE_TINY:
 
1503
    if (!fld_length)
 
1504
      length= MAX_TINYINT_WIDTH+sign_len;
 
1505
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
1506
    break;
 
1507
  case DRIZZLE_TYPE_LONG:
 
1508
    if (!fld_length)
 
1509
      length= MAX_INT_WIDTH+sign_len;
 
1510
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
1511
    break;
 
1512
  case DRIZZLE_TYPE_LONGLONG:
 
1513
    if (!fld_length)
 
1514
      length= MAX_BIGINT_WIDTH;
 
1515
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
1516
    break;
 
1517
  case DRIZZLE_TYPE_NULL:
 
1518
    break;
 
1519
  case DRIZZLE_TYPE_NEWDECIMAL:
 
1520
    my_decimal_trim(&length, &decimals);
 
1521
    if (length > DECIMAL_MAX_PRECISION)
 
1522
    {
 
1523
      my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
 
1524
               DECIMAL_MAX_PRECISION);
 
1525
      return(true);
 
1526
    }
 
1527
    if (length < decimals)
 
1528
    {
 
1529
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
 
1530
      return(true);
 
1531
    }
 
1532
    length=
 
1533
      my_decimal_precision_to_length(length, decimals,
 
1534
                                     fld_type_modifier & UNSIGNED_FLAG);
 
1535
    pack_length=
 
1536
      my_decimal_get_binary_size(length, decimals);
 
1537
    break;
 
1538
  case DRIZZLE_TYPE_VARCHAR:
 
1539
    /*
 
1540
      Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
 
1541
      if they don't have a default value
 
1542
    */
 
1543
    max_field_charlength= MAX_FIELD_VARCHARLENGTH;
 
1544
    break;
 
1545
  case DRIZZLE_TYPE_BLOB:
 
1546
    if (fld_default_value)
 
1547
    {
 
1548
      /* Allow empty as default value. */
 
1549
      String str,*res;
 
1550
      res= fld_default_value->val_str(&str);
 
1551
      if (res->length())
 
1552
      {
 
1553
        my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
 
1554
                 fld_name);
 
1555
        return(true);
 
1556
      }
 
1557
 
 
1558
    }
 
1559
    flags|= BLOB_FLAG;
 
1560
    break;
 
1561
  case DRIZZLE_TYPE_DOUBLE:
 
1562
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
 
1563
    if (!fld_length && !fld_decimals)
 
1564
    {
 
1565
      length= DBL_DIG+7;
 
1566
      decimals= NOT_FIXED_DEC;
 
1567
    }
 
1568
    if (length < decimals &&
 
1569
        decimals != NOT_FIXED_DEC)
 
1570
    {
 
1571
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
 
1572
      return(true);
 
1573
    }
 
1574
    break;
 
1575
  case DRIZZLE_TYPE_TIMESTAMP:
 
1576
    if (!fld_length)
 
1577
    {
 
1578
      /* Compressed date YYYYMMDDHHMMSS */
 
1579
      length= MAX_DATETIME_COMPRESSED_WIDTH;
 
1580
    }
 
1581
    else if (length != MAX_DATETIME_WIDTH)
 
1582
    {
 
1583
      /*
 
1584
        We support only even TIMESTAMP lengths less or equal than 14
 
1585
        and 19 as length of 4.1 compatible representation.
 
1586
      */
 
1587
      length= ((length+1)/2)*2; /* purecov: inspected */
 
1588
      length= cmin(length, (uint32_t)MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
 
1589
    }
 
1590
    flags|= UNSIGNED_FLAG;
 
1591
    if (fld_default_value)
 
1592
    {
 
1593
      /* Grammar allows only NOW() value for ON UPDATE clause */
 
1594
      if (fld_default_value->type() == Item::FUNC_ITEM &&
 
1595
          ((Item_func*)fld_default_value)->functype() == Item_func::NOW_FUNC)
 
1596
      {
 
1597
        unireg_check= (fld_on_update_value ? Field::TIMESTAMP_DNUN_FIELD:
 
1598
                                             Field::TIMESTAMP_DN_FIELD);
 
1599
        /*
 
1600
          We don't need default value any longer moreover it is dangerous.
 
1601
          Everything handled by unireg_check further.
 
1602
        */
 
1603
        def= 0;
 
1604
      }
 
1605
      else
 
1606
        unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
 
1607
                                             Field::NONE);
 
1608
    }
 
1609
    else
 
1610
    {
 
1611
      /*
 
1612
        If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
 
1613
        or ON UPDATE values then for the sake of compatiblity we should treat
 
1614
        this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
 
1615
        have another TIMESTAMP column with auto-set option before this one)
 
1616
        or DEFAULT 0 (in other cases).
 
1617
        So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
 
1618
        replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
 
1619
        information about all TIMESTAMP fields in table will be availiable.
 
1620
 
 
1621
        If we have TIMESTAMP NULL column without explicit DEFAULT value
 
1622
        we treat it as having DEFAULT NULL attribute.
 
1623
      */
 
1624
      unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD :
 
1625
                     (flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
 
1626
                                              Field::NONE));
 
1627
    }
 
1628
    break;
 
1629
  case DRIZZLE_TYPE_DATE:
 
1630
    length= 10;
 
1631
    break;
 
1632
  case DRIZZLE_TYPE_DATETIME:
 
1633
    length= MAX_DATETIME_WIDTH;
 
1634
    break;
 
1635
  case DRIZZLE_TYPE_ENUM:
 
1636
    {
 
1637
      /* Should be safe. */
 
1638
      pack_length= get_enum_pack_length(fld_interval_list->elements);
 
1639
 
 
1640
      List_iterator<String> it(*fld_interval_list);
 
1641
      String *tmp;
 
1642
      while ((tmp= it++))
 
1643
        interval_list.push_back(tmp);
 
1644
      length= 1; /* See comment for DRIZZLE_TYPE_SET above. */
 
1645
      break;
 
1646
   }
 
1647
  case DRIZZLE_TYPE_VIRTUAL: // Must not happen
 
1648
    assert(0);
 
1649
  }
 
1650
  /* Remember the value of length */
 
1651
  char_length= length;
 
1652
 
 
1653
  if (!(flags & BLOB_FLAG) &&
 
1654
      ((length > max_field_charlength &&
 
1655
        fld_type != DRIZZLE_TYPE_ENUM &&
 
1656
        (fld_type != DRIZZLE_TYPE_VARCHAR || fld_default_value)) ||
 
1657
       (!length && fld_type != DRIZZLE_TYPE_VARCHAR)))
 
1658
  {
 
1659
    my_error((fld_type == DRIZZLE_TYPE_VARCHAR) ?  ER_TOO_BIG_FIELDLENGTH : ER_TOO_BIG_DISPLAYWIDTH,
 
1660
              MYF(0),
 
1661
              fld_name, max_field_charlength); /* purecov: inspected */
 
1662
    return(true);
 
1663
  }
 
1664
  fld_type_modifier&= AUTO_INCREMENT_FLAG;
 
1665
  if ((~allowed_type_modifier) & fld_type_modifier)
 
1666
  {
 
1667
    my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
 
1668
    return(true);
 
1669
  }
 
1670
 
 
1671
  return(false); /* success */
 
1672
}
 
1673
 
 
1674
 
 
1675
enum_field_types get_blob_type_from_length(uint32_t)
 
1676
{
 
1677
  enum_field_types type;
 
1678
 
 
1679
  type= DRIZZLE_TYPE_BLOB;
 
1680
 
 
1681
  return type;
 
1682
}
 
1683
 
 
1684
 
 
1685
/*
 
1686
  Make a field from the .frm file info
 
1687
*/
 
1688
 
1143
1689
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
1144
1690
{
1145
1691
  switch (type) {
1146
 
  case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1147
 
  case DRIZZLE_TYPE_UUID: return field::Uuid::max_string_length();
1148
 
  case DRIZZLE_TYPE_MICROTIME: return field::Microtime::max_string_length();
1149
 
  case DRIZZLE_TYPE_TIMESTAMP: return field::Epoch::max_string_length();
1150
 
  case DRIZZLE_TYPE_BOOLEAN: return field::Boolean::max_string_length();
1151
 
  case DRIZZLE_TYPE_DATE:
1152
 
  case DRIZZLE_TYPE_ENUM:
1153
 
  case DRIZZLE_TYPE_LONG: return 4;
 
1692
  case DRIZZLE_TYPE_VARCHAR:     return (length + (length < 256 ? 1: 2));
 
1693
  case DRIZZLE_TYPE_TINY        : return 1;
 
1694
  case DRIZZLE_TYPE_DATE: return 3;
 
1695
  case DRIZZLE_TYPE_TIMESTAMP:
 
1696
  case DRIZZLE_TYPE_LONG        : return 4;
1154
1697
  case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1155
 
  case DRIZZLE_TYPE_TIME:
1156
1698
  case DRIZZLE_TYPE_DATETIME:
1157
1699
  case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1158
 
  case DRIZZLE_TYPE_NULL: return 0;
1159
 
  case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
1160
 
  case DRIZZLE_TYPE_DECIMAL:
1161
 
                          break;
 
1700
  case DRIZZLE_TYPE_NULL        : return 0;
 
1701
  case DRIZZLE_TYPE_BLOB:               return 4+portable_sizeof_char_ptr;
 
1702
  case DRIZZLE_TYPE_ENUM:
 
1703
  case DRIZZLE_TYPE_NEWDECIMAL:
 
1704
    abort();
 
1705
  default:
 
1706
    return 0;
1162
1707
  }
1163
 
 
1164
 
  assert(0);
1165
 
  abort();
1166
1708
}
1167
1709
 
 
1710
 
1168
1711
uint32_t pack_length_to_packflag(uint32_t type)
1169
1712
{
1170
1713
  switch (type) {
1171
1714
    case 1: return 1 << FIELDFLAG_PACK_SHIFT;
1172
1715
    case 2: assert(1);
1173
1716
    case 3: assert(1);
1174
 
    case 4: return f_settype(DRIZZLE_TYPE_LONG);
1175
 
    case 8: return f_settype(DRIZZLE_TYPE_LONGLONG);
 
1717
    case 4: return f_settype((uint32_t) DRIZZLE_TYPE_LONG);
 
1718
    case 8: return f_settype((uint32_t) DRIZZLE_TYPE_LONGLONG);
1176
1719
  }
1177
1720
  return 0;                                     // This shouldn't happen
1178
1721
}
1179
1722
 
 
1723
 
 
1724
Field *make_field(TABLE_SHARE *share, MEM_ROOT *root,
 
1725
                  unsigned char *ptr, uint32_t field_length,
 
1726
                  unsigned char *null_pos, unsigned char null_bit,
 
1727
                  uint32_t pack_flag,
 
1728
                  enum_field_types field_type,
 
1729
                  const CHARSET_INFO * field_charset,
 
1730
                  Field::utype unireg_check,
 
1731
                  TYPELIB *interval,
 
1732
                  const char *field_name)
 
1733
{
 
1734
  if(!root)
 
1735
    root= current_mem_root();
 
1736
 
 
1737
  if (!f_maybe_null(pack_flag))
 
1738
  {
 
1739
    null_pos=0;
 
1740
    null_bit=0;
 
1741
  }
 
1742
  else
 
1743
  {
 
1744
    null_bit= ((unsigned char) 1) << null_bit;
 
1745
  }
 
1746
 
 
1747
  switch (field_type) {
 
1748
  case DRIZZLE_TYPE_DATE:
 
1749
  case DRIZZLE_TYPE_DATETIME:
 
1750
  case DRIZZLE_TYPE_TIMESTAMP:
 
1751
    field_charset= &my_charset_bin;
 
1752
  default: break;
 
1753
  }
 
1754
 
 
1755
  if (f_is_alpha(pack_flag))
 
1756
  {
 
1757
    if (!f_is_packed(pack_flag))
 
1758
    {
 
1759
      if (field_type == DRIZZLE_TYPE_VARCHAR)
 
1760
        return new (root) Field_varstring(ptr,field_length,
 
1761
                                   HA_VARCHAR_PACKLENGTH(field_length),
 
1762
                                   null_pos,null_bit,
 
1763
                                   unireg_check, field_name,
 
1764
                                   share,
 
1765
                                   field_charset);
 
1766
      return 0;                                 // Error
 
1767
    }
 
1768
 
 
1769
    uint32_t pack_length=calc_pack_length((enum_field_types)
 
1770
                                      f_packtype(pack_flag),
 
1771
                                      field_length);
 
1772
 
 
1773
    if (f_is_blob(pack_flag))
 
1774
      return new (root) Field_blob(ptr,null_pos,null_bit,
 
1775
                            unireg_check, field_name, share,
 
1776
                            pack_length, field_charset);
 
1777
    if (interval)
 
1778
    {
 
1779
      if (f_is_enum(pack_flag))
 
1780
      {
 
1781
        return new (root) Field_enum(ptr,field_length,null_pos,null_bit,
 
1782
                                  unireg_check, field_name,
 
1783
                                  get_enum_pack_length(interval->count),
 
1784
                                  interval, field_charset);
 
1785
      }
 
1786
    }
 
1787
  }
 
1788
 
 
1789
  switch (field_type) {
 
1790
  case DRIZZLE_TYPE_NEWDECIMAL:
 
1791
    return new (root) Field_new_decimal(ptr,field_length,null_pos,null_bit,
 
1792
                                 unireg_check, field_name,
 
1793
                                 f_decimals(pack_flag),
 
1794
                                 f_is_decimal_precision(pack_flag) != 0,
 
1795
                                 f_is_dec(pack_flag) == 0);
 
1796
  case DRIZZLE_TYPE_DOUBLE:
 
1797
    return new (root) Field_double(ptr,field_length,null_pos,null_bit,
 
1798
                            unireg_check, field_name,
 
1799
                            f_decimals(pack_flag),
 
1800
                            false,
 
1801
                            f_is_dec(pack_flag)== 0);
 
1802
  case DRIZZLE_TYPE_TINY:
 
1803
    assert(0);
 
1804
  case DRIZZLE_TYPE_LONG:
 
1805
    return new (root) Field_long(ptr,field_length,null_pos,null_bit,
 
1806
                           unireg_check, field_name,
 
1807
                           false,
 
1808
                           f_is_dec(pack_flag) == 0);
 
1809
  case DRIZZLE_TYPE_LONGLONG:
 
1810
    return new (root) Field_int64_t(ptr,field_length,null_pos,null_bit,
 
1811
                              unireg_check, field_name,
 
1812
                              false,
 
1813
                              f_is_dec(pack_flag) == 0);
 
1814
  case DRIZZLE_TYPE_TIMESTAMP:
 
1815
    return new (root) Field_timestamp(ptr,field_length, null_pos, null_bit,
 
1816
                               unireg_check, field_name, share,
 
1817
                               field_charset);
 
1818
  case DRIZZLE_TYPE_DATE:
 
1819
    return new (root) Field_date(ptr,null_pos,null_bit,
 
1820
                             unireg_check, field_name, field_charset);
 
1821
  case DRIZZLE_TYPE_DATETIME:
 
1822
    return new (root) Field_datetime(ptr,null_pos,null_bit,
 
1823
                              unireg_check, field_name, field_charset);
 
1824
  case DRIZZLE_TYPE_NULL:
 
1825
    return new (root) Field_null(ptr, field_length, unireg_check, field_name,
 
1826
                          field_charset);
 
1827
  case DRIZZLE_TYPE_VIRTUAL:                    // Must not happen
 
1828
    assert(0);
 
1829
  default:                                      // Impossible (Wrong version)
 
1830
    break;
 
1831
  }
 
1832
  return 0;
 
1833
}
 
1834
 
 
1835
 
 
1836
/** Create a field suitable for create of table. */
 
1837
 
 
1838
Create_field::Create_field(Field *old_field,Field *orig_field)
 
1839
{
 
1840
  field=      old_field;
 
1841
  field_name=change=old_field->field_name;
 
1842
  length=     old_field->field_length;
 
1843
  flags=      old_field->flags;
 
1844
  unireg_check=old_field->unireg_check;
 
1845
  pack_length=old_field->pack_length();
 
1846
  key_length= old_field->key_length();
 
1847
  sql_type=   old_field->real_type();
 
1848
  charset=    old_field->charset();             // May be NULL ptr
 
1849
  comment=    old_field->comment;
 
1850
  decimals=   old_field->decimals();
 
1851
  vcol_info=  old_field->vcol_info;
 
1852
  is_stored= old_field->is_stored;
 
1853
 
 
1854
  /* Fix if the original table had 4 byte pointer blobs */
 
1855
  if (flags & BLOB_FLAG)
 
1856
    pack_length= (pack_length- old_field->table->s->blob_ptr_size +
 
1857
                  portable_sizeof_char_ptr);
 
1858
 
 
1859
  switch (sql_type) {
 
1860
  case DRIZZLE_TYPE_BLOB:
 
1861
    sql_type= DRIZZLE_TYPE_BLOB;
 
1862
    length/= charset->mbmaxlen;
 
1863
    key_length/= charset->mbmaxlen;
 
1864
    break;
 
1865
    /* Change CHAR -> VARCHAR if dynamic record length */
 
1866
  case DRIZZLE_TYPE_ENUM:
 
1867
  case DRIZZLE_TYPE_VARCHAR:
 
1868
    /* This is corrected in create_length_to_internal_length */
 
1869
    length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
 
1870
    break;
 
1871
  default:
 
1872
    break;
 
1873
  }
 
1874
 
 
1875
  if (flags & (ENUM_FLAG | SET_FLAG))
 
1876
    interval= ((Field_enum*) old_field)->typelib;
 
1877
  else
 
1878
    interval=0;
 
1879
  def=0;
 
1880
  char_length= length;
 
1881
 
 
1882
  if (!(flags & (NO_DEFAULT_VALUE_FLAG )) &&
 
1883
      old_field->ptr && orig_field &&
 
1884
      (sql_type != DRIZZLE_TYPE_TIMESTAMP ||                /* set def only if */
 
1885
       old_field->table->timestamp_field != old_field ||  /* timestamp field */
 
1886
       unireg_check == Field::TIMESTAMP_UN_FIELD))        /* has default val */
 
1887
  {
 
1888
    my_ptrdiff_t diff;
 
1889
 
 
1890
    /* Get the value from default_values */
 
1891
    diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
 
1892
                          orig_field->table->record[0]);
 
1893
    orig_field->move_field_offset(diff);        // Points now at default_values
 
1894
    if (!orig_field->is_real_null())
 
1895
    {
 
1896
      char buff[MAX_FIELD_WIDTH], *pos;
 
1897
      String tmp(buff, sizeof(buff), charset), *res;
 
1898
      res= orig_field->val_str(&tmp);
 
1899
      pos= (char*) sql_strmake(res->ptr(), res->length());
 
1900
      def= new Item_string(pos, res->length(), charset);
 
1901
    }
 
1902
    orig_field->move_field_offset(-diff);       // Back to record[0]
 
1903
  }
 
1904
}
 
1905
 
 
1906
 
1180
1907
/*****************************************************************************
1181
1908
 Warning handling
1182
1909
*****************************************************************************/
1183
1910
 
1184
 
bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
1185
 
                        drizzled::error_t code,
1186
 
                        int cuted_increment)
 
1911
/**
 
1912
  Produce warning or note about data saved into field.
 
1913
 
 
1914
  @param level            - level of message (Note/Warning/Error)
 
1915
  @param code             - error code of message to be produced
 
1916
  @param cuted_increment  - whenever we should increase cut fields count or not
 
1917
 
 
1918
  @note
 
1919
    This function won't produce warning and increase cut fields counter
 
1920
    if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
 
1921
 
 
1922
    if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
 
1923
    This allows us to avoid notes in optimisation, like convert_constant_item().
 
1924
 
 
1925
  @retval
 
1926
    1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
 
1927
  @retval
 
1928
    0 otherwise
 
1929
*/
 
1930
 
 
1931
bool
 
1932
Field::set_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
 
1933
                   int cuted_increment)
1187
1934
{
1188
1935
  /*
1189
1936
    If this field was created only for type conversion purposes it
1201
1948
}
1202
1949
 
1203
1950
 
1204
 
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1205
 
                                 drizzled::error_t code,
1206
 
                                 const char *str, 
1207
 
                                 uint32_t str_length,
1208
 
                                 type::timestamp_t ts_type, 
1209
 
                                 int cuted_increment)
 
1951
/**
 
1952
  Produce warning or note about datetime string data saved into field.
 
1953
 
 
1954
  @param level            level of message (Note/Warning/Error)
 
1955
  @param code             error code of message to be produced
 
1956
  @param str              string value which we tried to save
 
1957
  @param str_length       length of string which we tried to save
 
1958
  @param ts_type          type of datetime value (datetime/date/time)
 
1959
  @param cuted_increment  whenever we should increase cut fields count or not
 
1960
 
 
1961
  @note
 
1962
    This function will always produce some warning but won't increase cut
 
1963
    fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
 
1964
    thread.
 
1965
*/
 
1966
 
 
1967
void
 
1968
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
 
1969
                            unsigned int code,
 
1970
                            const char *str, uint32_t str_length,
 
1971
                            enum enum_drizzle_timestamp_type ts_type, int cuted_increment)
1210
1972
{
1211
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1212
 
 
1213
 
  if ((session->abortOnWarning() and
 
1973
  Session *session= table ? table->in_use : current_session;
 
1974
  if ((session->really_abort_on_warning() &&
1214
1975
       level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
1215
1976
      set_warning(level, code, cuted_increment))
1216
1977
    make_truncated_value_warning(session, level, str, str_length, ts_type,
1217
1978
                                 field_name);
1218
1979
}
1219
1980
 
1220
 
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, 
1221
 
                                 drizzled::error_t code,
1222
 
                                 int64_t nr, 
1223
 
                                 type::timestamp_t ts_type,
1224
 
                                 int cuted_increment)
 
1981
 
 
1982
/**
 
1983
  Produce warning or note about integer datetime value saved into field.
 
1984
 
 
1985
  @param level            level of message (Note/Warning/Error)
 
1986
  @param code             error code of message to be produced
 
1987
  @param nr               numeric value which we tried to save
 
1988
  @param ts_type          type of datetime value (datetime/date/time)
 
1989
  @param cuted_increment  whenever we should increase cut fields count or not
 
1990
 
 
1991
  @note
 
1992
    This function will always produce some warning but won't increase cut
 
1993
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
 
1994
    thread.
 
1995
*/
 
1996
 
 
1997
void
 
1998
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
 
1999
                            int64_t nr, enum enum_drizzle_timestamp_type ts_type,
 
2000
                            int cuted_increment)
1225
2001
{
1226
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1227
 
 
1228
 
  if (session->abortOnWarning() or
 
2002
  Session *session= table ? table->in_use : current_session;
 
2003
  if (session->really_abort_on_warning() ||
1229
2004
      set_warning(level, code, cuted_increment))
1230
2005
  {
1231
 
    char str_nr[DECIMAL_LONGLONG_DIGITS];
1232
 
    char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
 
2006
    char str_nr[22];
 
2007
    char *str_end= int64_t10_to_str(nr, str_nr, -10);
1233
2008
    make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
1234
2009
                                 ts_type, field_name);
1235
2010
  }
1236
2011
}
1237
2012
 
1238
 
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1239
 
                                 const drizzled::error_t code,
1240
 
                                 double nr, 
1241
 
                                 type::timestamp_t ts_type)
 
2013
 
 
2014
/**
 
2015
  Produce warning or note about double datetime data saved into field.
 
2016
 
 
2017
  @param level            level of message (Note/Warning/Error)
 
2018
  @param code             error code of message to be produced
 
2019
  @param nr               double value which we tried to save
 
2020
  @param ts_type          type of datetime value (datetime/date/time)
 
2021
 
 
2022
  @note
 
2023
    This function will always produce some warning but won't increase cut
 
2024
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
 
2025
    thread.
 
2026
*/
 
2027
 
 
2028
void
 
2029
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, const uint32_t code,
 
2030
                            double nr, enum enum_drizzle_timestamp_type ts_type)
1242
2031
{
1243
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1244
 
 
1245
 
  if (session->abortOnWarning() or
 
2032
  Session *session= table ? table->in_use : current_session;
 
2033
  if (session->really_abort_on_warning() ||
1246
2034
      set_warning(level, code, 1))
1247
2035
  {
1248
2036
    /* DBL_DIG is enough to print '-[digits].E+###' */
1249
2037
    char str_nr[DBL_DIG + 8];
1250
 
    uint32_t str_len= snprintf(str_nr, sizeof(str_nr), "%g", nr);
 
2038
    uint32_t str_len= sprintf(str_nr, "%g", nr);
1251
2039
    make_truncated_value_warning(session, level, str_nr, str_len, ts_type,
1252
2040
                                 field_name);
1253
2041
  }
1254
2042
}
1255
2043
 
1256
 
bool Field::isReadSet() const 
1257
 
1258
 
  return table->isReadSet(field_index); 
1259
 
}
1260
 
 
1261
 
bool Field::isWriteSet()
1262
 
1263
 
  return table->isWriteSet(field_index); 
1264
 
}
1265
 
 
1266
 
void Field::setReadSet(bool arg)
1267
 
{
1268
 
  if (arg)
1269
 
    table->setReadSet(field_index);
1270
 
  else
1271
 
    table->clearReadSet(field_index);
1272
 
}
1273
 
 
1274
 
void Field::setWriteSet(bool arg)
1275
 
{
1276
 
  if (arg)
1277
 
    table->setWriteSet(field_index);
1278
 
  else
1279
 
    table->clearWriteSet(field_index);
1280
 
}
1281
 
 
1282
 
void Field::pack_num(uint64_t arg, unsigned char *destination)
1283
 
{
1284
 
  if (not destination)
1285
 
    destination= ptr;
1286
 
 
1287
 
  int64_tstore(destination, arg);
1288
 
}
1289
 
 
1290
 
void Field::pack_num(uint32_t arg, unsigned char *destination)
1291
 
{
1292
 
  if (not destination)
1293
 
    destination= ptr;
1294
 
 
1295
 
  longstore(destination, arg);
1296
 
}
1297
 
 
1298
 
uint64_t Field::unpack_num(uint64_t &destination, const unsigned char *arg) const
1299
 
{
1300
 
  if (not arg)
1301
 
    arg= ptr;
1302
 
 
1303
 
  int64_tget(destination, arg);
1304
 
 
1305
 
  return destination;
1306
 
}
1307
 
 
1308
 
uint32_t Field::unpack_num(uint32_t &destination, const unsigned char *arg) const
1309
 
{
1310
 
  if (not arg)
1311
 
    arg= ptr;
1312
 
 
1313
 
  longget(destination, arg);
1314
 
 
1315
 
  return destination;
1316
 
}
1317
 
 
1318
 
std::ostream& operator<<(std::ostream& output, const Field &field)
1319
 
{
1320
 
  output << "Field:(";
1321
 
  output <<  field.field_name;
1322
 
  output << ", ";
1323
 
  output << drizzled::display::type(field.real_type());
1324
 
  output << ", { ";
1325
 
 
1326
 
  if (field.flags & NOT_NULL_FLAG)
1327
 
    output << " NOT_NULL";
1328
 
 
1329
 
  if (field.flags & PRI_KEY_FLAG)
1330
 
    output << ", PRIMARY KEY";
1331
 
 
1332
 
  if (field.flags & UNIQUE_KEY_FLAG)
1333
 
    output << ", UNIQUE KEY";
1334
 
 
1335
 
  if (field.flags & MULTIPLE_KEY_FLAG)
1336
 
    output << ", MULTIPLE KEY";
1337
 
 
1338
 
  if (field.flags & BLOB_FLAG)
1339
 
    output << ", BLOB";
1340
 
 
1341
 
  if (field.flags & UNSIGNED_FLAG)
1342
 
    output << ", UNSIGNED";
1343
 
 
1344
 
  if (field.flags & BINARY_FLAG)
1345
 
    output << ", BINARY";
1346
 
  output << "}, ";
1347
 
  output << ")";
1348
 
 
1349
 
  return output;  // for multiple << operators.
1350
 
}
1351
 
 
1352
 
} /* namespace drizzled */