~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-03-21 01:02:23 UTC
  • mto: (960.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 961.
  • Revision ID: osullivan.padraig@gmail.com-20090321010223-j8cph7eeyt1u3xol
Fixed function object to ensure it correctly returns a boolean type since
memcmp returns an integer. Added some more comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
/**
22
 
 * @file This file implements the Field class and API
23
 
 */
 
22
  @file
24
23
 
25
 
#include "config.h"
26
 
#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"
27
29
#include <errno.h>
28
 
#include <float.h>
29
 
#include "drizzled/sql_select.h"
30
 
#include "drizzled/error.h"
31
 
#include "drizzled/field/str.h"
32
 
#include "drizzled/field/num.h"
33
 
#include "drizzled/field/blob.h"
34
 
#include "drizzled/field/enum.h"
35
 
#include "drizzled/field/null.h"
36
 
#include "drizzled/field/date.h"
37
 
#include "drizzled/field/decimal.h"
38
 
#include "drizzled/field/real.h"
39
 
#include "drizzled/field/double.h"
40
 
#include "drizzled/field/int32.h"
41
 
#include "drizzled/field/int64.h"
42
 
#include "drizzled/field/num.h"
43
 
#include "drizzled/field/timestamp.h"
44
 
#include "drizzled/field/datetime.h"
45
 
#include "drizzled/field/varstring.h"
46
 
#include "drizzled/field/uuid.h"
47
 
#include "drizzled/time_functions.h"
48
 
#include "drizzled/internal/m_string.h"
49
 
 
50
 
#include "drizzled/display.h"
51
 
 
52
 
namespace drizzled
53
 
{
 
30
#include <drizzled/error.h>
 
31
#include <drizzled/virtual_column_info.h>
 
32
#include <drizzled/field/str.h>
 
33
#include <drizzled/field/longstr.h>
 
34
#include <drizzled/field/num.h>
 
35
#include <drizzled/field/blob.h>
 
36
#include <drizzled/field/enum.h>
 
37
#include <drizzled/field/null.h>
 
38
#include <drizzled/field/date.h>
 
39
#include <drizzled/field/decimal.h>
 
40
#include <drizzled/field/real.h>
 
41
#include <drizzled/field/double.h>
 
42
#include <drizzled/field/long.h>
 
43
#include <drizzled/field/int64_t.h>
 
44
#include <drizzled/field/num.h>
 
45
#include <drizzled/field/timestamp.h>
 
46
#include <drizzled/field/datetime.h>
 
47
#include <drizzled/field/varstring.h>
 
48
 
54
49
 
55
50
/*****************************************************************************
56
51
  Instansiate templates and static variables
57
52
*****************************************************************************/
58
53
 
 
54
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
55
template class List<Create_field>;
 
56
template class List_iterator<Create_field>;
 
57
#endif
 
58
 
 
59
 
59
60
static enum_field_types
60
 
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]=
61
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
  },
62
92
  /* DRIZZLE_TYPE_LONG -> */
63
93
  {
 
94
    //DRIZZLE_TYPE_TINY
 
95
    DRIZZLE_TYPE_LONG,
64
96
    //DRIZZLE_TYPE_LONG
65
97
    DRIZZLE_TYPE_LONG,
66
98
    //DRIZZLE_TYPE_DOUBLE
77
109
    DRIZZLE_TYPE_VARCHAR,
78
110
    //DRIZZLE_TYPE_VARCHAR
79
111
    DRIZZLE_TYPE_VARCHAR,
80
 
    //DRIZZLE_TYPE_DECIMAL
81
 
    DRIZZLE_TYPE_DECIMAL,
 
112
    //DRIZZLE_TYPE_VIRTUAL
 
113
    DRIZZLE_TYPE_VIRTUAL,
 
114
    //DRIZZLE_TYPE_NEWDECIMAL
 
115
    DRIZZLE_TYPE_NEWDECIMAL,
82
116
    //DRIZZLE_TYPE_ENUM
83
117
    DRIZZLE_TYPE_VARCHAR,
84
118
    //DRIZZLE_TYPE_BLOB
85
119
    DRIZZLE_TYPE_BLOB,
86
 
    //DRIZZLE_TYPE_UUID
87
 
    DRIZZLE_TYPE_VARCHAR,
88
120
  },
89
121
  /* DRIZZLE_TYPE_DOUBLE -> */
90
122
  {
 
123
    //DRIZZLE_TYPE_TINY
 
124
    DRIZZLE_TYPE_DOUBLE,
91
125
    //DRIZZLE_TYPE_LONG
92
126
    DRIZZLE_TYPE_DOUBLE,
93
127
    //DRIZZLE_TYPE_DOUBLE
104
138
    DRIZZLE_TYPE_VARCHAR,
105
139
    //DRIZZLE_TYPE_VARCHAR
106
140
    DRIZZLE_TYPE_VARCHAR,
107
 
    //DRIZZLE_TYPE_DECIMAL
 
141
    //DRIZZLE_TYPE_VIRTUAL
 
142
    DRIZZLE_TYPE_VIRTUAL,
 
143
    //DRIZZLE_TYPE_NEWDECIMAL
108
144
    DRIZZLE_TYPE_DOUBLE,
109
145
    //DRIZZLE_TYPE_ENUM
110
146
    DRIZZLE_TYPE_VARCHAR,
111
147
    //DRIZZLE_TYPE_BLOB
112
148
    DRIZZLE_TYPE_BLOB,
113
 
    //DRIZZLE_TYPE_UUID
114
 
    DRIZZLE_TYPE_VARCHAR,
115
149
  },
116
150
  /* DRIZZLE_TYPE_NULL -> */
117
151
  {
 
152
    //DRIZZLE_TYPE_TINY
 
153
    DRIZZLE_TYPE_TINY,
118
154
    //DRIZZLE_TYPE_LONG
119
155
    DRIZZLE_TYPE_LONG,
120
156
    //DRIZZLE_TYPE_DOUBLE
131
167
    DRIZZLE_TYPE_DATE,
132
168
    //DRIZZLE_TYPE_VARCHAR
133
169
    DRIZZLE_TYPE_VARCHAR,
134
 
    //DRIZZLE_TYPE_DECIMAL
135
 
    DRIZZLE_TYPE_DECIMAL,
 
170
    //DRIZZLE_TYPE_VIRTUAL
 
171
    DRIZZLE_TYPE_VIRTUAL,
 
172
    //DRIZZLE_TYPE_NEWDECIMAL
 
173
    DRIZZLE_TYPE_NEWDECIMAL,
136
174
    //DRIZZLE_TYPE_ENUM
137
175
    DRIZZLE_TYPE_ENUM,
138
176
    //DRIZZLE_TYPE_BLOB
139
177
    DRIZZLE_TYPE_BLOB,
140
 
    //DRIZZLE_TYPE_UUID
141
 
    DRIZZLE_TYPE_UUID,
142
178
  },
143
179
  /* DRIZZLE_TYPE_TIMESTAMP -> */
144
180
  {
 
181
    //DRIZZLE_TYPE_TINY
 
182
    DRIZZLE_TYPE_VARCHAR,
145
183
    //DRIZZLE_TYPE_LONG
146
184
    DRIZZLE_TYPE_VARCHAR,
147
185
    //DRIZZLE_TYPE_DOUBLE
158
196
    DRIZZLE_TYPE_DATE,
159
197
    //DRIZZLE_TYPE_VARCHAR
160
198
    DRIZZLE_TYPE_VARCHAR,
161
 
    //DRIZZLE_TYPE_DECIMAL
 
199
    //DRIZZLE_TYPE_VIRTUAL
 
200
    DRIZZLE_TYPE_VIRTUAL,
 
201
    //DRIZZLE_TYPE_NEWDECIMAL
162
202
    DRIZZLE_TYPE_VARCHAR,
163
203
    //DRIZZLE_TYPE_ENUM
164
204
    DRIZZLE_TYPE_VARCHAR,
165
205
    //DRIZZLE_TYPE_BLOB
166
206
    DRIZZLE_TYPE_BLOB,
167
 
    //DRIZZLE_TYPE_UUID
168
 
    DRIZZLE_TYPE_VARCHAR,
169
207
  },
170
208
  /* DRIZZLE_TYPE_LONGLONG -> */
171
209
  {
 
210
    //DRIZZLE_TYPE_TINY
 
211
    DRIZZLE_TYPE_LONGLONG,
172
212
    //DRIZZLE_TYPE_LONG
173
213
    DRIZZLE_TYPE_LONGLONG,
174
214
    //DRIZZLE_TYPE_DOUBLE
185
225
    DRIZZLE_TYPE_DATE,
186
226
    //DRIZZLE_TYPE_VARCHAR
187
227
    DRIZZLE_TYPE_VARCHAR,
188
 
    //DRIZZLE_TYPE_DECIMAL DRIZZLE_TYPE_ENUM
189
 
    DRIZZLE_TYPE_DECIMAL,
 
228
    //DRIZZLE_TYPE_VIRTUAL
 
229
    DRIZZLE_TYPE_VIRTUAL,
 
230
    //DRIZZLE_TYPE_NEWDECIMAL
 
231
    //DRIZZLE_TYPE_ENUM
 
232
    DRIZZLE_TYPE_NEWDECIMAL,
190
233
    DRIZZLE_TYPE_VARCHAR,
191
234
    //DRIZZLE_TYPE_BLOB
192
235
    DRIZZLE_TYPE_BLOB,
193
 
    //DRIZZLE_TYPE_UUID
194
 
    DRIZZLE_TYPE_VARCHAR,
195
236
  },
196
237
  /* DRIZZLE_TYPE_DATETIME -> */
197
238
  {
 
239
    //DRIZZLE_TYPE_TINY
 
240
    DRIZZLE_TYPE_VARCHAR,
198
241
    //DRIZZLE_TYPE_LONG
199
242
    DRIZZLE_TYPE_VARCHAR,
200
243
    //DRIZZLE_TYPE_DOUBLE
211
254
    DRIZZLE_TYPE_DATE,
212
255
    //DRIZZLE_TYPE_VARCHAR
213
256
    DRIZZLE_TYPE_VARCHAR,
214
 
    //DRIZZLE_TYPE_DECIMAL
 
257
    //DRIZZLE_TYPE_VIRTUAL
 
258
    DRIZZLE_TYPE_VIRTUAL,
 
259
    //DRIZZLE_TYPE_NEWDECIMAL
215
260
    DRIZZLE_TYPE_VARCHAR,
216
261
    //DRIZZLE_TYPE_ENUM
217
262
    DRIZZLE_TYPE_VARCHAR,
218
263
    //DRIZZLE_TYPE_BLOB
219
264
    DRIZZLE_TYPE_BLOB,
220
 
    //DRIZZLE_TYPE_UUID
221
 
    DRIZZLE_TYPE_VARCHAR,
222
265
  },
223
266
  /* DRIZZLE_TYPE_DATE -> */
224
267
  {
 
268
    //DRIZZLE_TYPE_TINY
 
269
    DRIZZLE_TYPE_VARCHAR,
225
270
    //DRIZZLE_TYPE_LONG
226
271
    DRIZZLE_TYPE_VARCHAR,
227
272
    //DRIZZLE_TYPE_DOUBLE
238
283
    DRIZZLE_TYPE_DATE,
239
284
    //DRIZZLE_TYPE_VARCHAR
240
285
    DRIZZLE_TYPE_VARCHAR,
241
 
    //DRIZZLE_TYPE_DECIMAL
 
286
    //DRIZZLE_TYPE_VIRTUAL
 
287
    DRIZZLE_TYPE_VIRTUAL,
 
288
    //DRIZZLE_TYPE_NEWDECIMAL
242
289
    DRIZZLE_TYPE_VARCHAR,
243
290
    //DRIZZLE_TYPE_ENUM
244
291
    DRIZZLE_TYPE_VARCHAR,
245
292
    //DRIZZLE_TYPE_BLOB
246
293
    DRIZZLE_TYPE_BLOB,
247
 
    //DRIZZLE_TYPE_UUID
248
 
    DRIZZLE_TYPE_VARCHAR,
249
294
  },
250
295
  /* DRIZZLE_TYPE_VARCHAR -> */
251
296
  {
 
297
    //DRIZZLE_TYPE_TINY
 
298
    DRIZZLE_TYPE_VARCHAR,
252
299
    //DRIZZLE_TYPE_LONG
253
300
    DRIZZLE_TYPE_VARCHAR,
254
301
    //DRIZZLE_TYPE_DOUBLE
265
312
    DRIZZLE_TYPE_VARCHAR,
266
313
    //DRIZZLE_TYPE_VARCHAR
267
314
    DRIZZLE_TYPE_VARCHAR,
268
 
    //DRIZZLE_TYPE_DECIMAL
 
315
    //DRIZZLE_TYPE_VIRTUAL
 
316
    DRIZZLE_TYPE_VIRTUAL,
 
317
    //DRIZZLE_TYPE_NEWDECIMAL
269
318
    DRIZZLE_TYPE_VARCHAR,
270
319
    //DRIZZLE_TYPE_ENUM
271
320
    DRIZZLE_TYPE_VARCHAR,
272
321
    //DRIZZLE_TYPE_BLOB
273
322
    DRIZZLE_TYPE_BLOB,
274
 
    //DRIZZLE_TYPE_UUID
275
 
    DRIZZLE_TYPE_VARCHAR,
276
 
  },
277
 
  /* DRIZZLE_TYPE_DECIMAL -> */
278
 
  {
279
 
    //DRIZZLE_TYPE_LONG
280
 
    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,
281
359
    //DRIZZLE_TYPE_DOUBLE
282
360
    DRIZZLE_TYPE_DOUBLE,
283
361
    //DRIZZLE_TYPE_NULL
284
 
    DRIZZLE_TYPE_DECIMAL,
 
362
    DRIZZLE_TYPE_NEWDECIMAL,
285
363
    //DRIZZLE_TYPE_TIMESTAMP
286
364
    DRIZZLE_TYPE_VARCHAR,
287
365
    //DRIZZLE_TYPE_LONGLONG
288
 
    DRIZZLE_TYPE_DECIMAL,
 
366
    DRIZZLE_TYPE_NEWDECIMAL,
289
367
    //DRIZZLE_TYPE_DATETIME
290
368
    DRIZZLE_TYPE_VARCHAR,
291
369
    //DRIZZLE_TYPE_DATE
292
370
    DRIZZLE_TYPE_VARCHAR,
293
371
    //DRIZZLE_TYPE_VARCHAR
294
372
    DRIZZLE_TYPE_VARCHAR,
295
 
    //DRIZZLE_TYPE_DECIMAL
296
 
    DRIZZLE_TYPE_DECIMAL,
 
373
    //DRIZZLE_TYPE_VIRTUAL
 
374
    DRIZZLE_TYPE_VIRTUAL,
 
375
    //DRIZZLE_TYPE_NEWDECIMAL
 
376
    DRIZZLE_TYPE_NEWDECIMAL,
297
377
    //DRIZZLE_TYPE_ENUM
298
378
    DRIZZLE_TYPE_VARCHAR,
299
379
    //DRIZZLE_TYPE_BLOB
300
380
    DRIZZLE_TYPE_BLOB,
301
 
    //DRIZZLE_TYPE_UUID
302
 
    DRIZZLE_TYPE_VARCHAR,
303
381
  },
304
382
  /* DRIZZLE_TYPE_ENUM -> */
305
383
  {
 
384
    //DRIZZLE_TYPE_TINY
 
385
    DRIZZLE_TYPE_VARCHAR,
306
386
    //DRIZZLE_TYPE_LONG
307
387
    DRIZZLE_TYPE_VARCHAR,
308
388
    //DRIZZLE_TYPE_DOUBLE
319
399
    DRIZZLE_TYPE_VARCHAR,
320
400
    //DRIZZLE_TYPE_VARCHAR
321
401
    DRIZZLE_TYPE_VARCHAR,
322
 
    //DRIZZLE_TYPE_DECIMAL
 
402
    //DRIZZLE_TYPE_VIRTUAL
 
403
    DRIZZLE_TYPE_VIRTUAL,
 
404
    //DRIZZLE_TYPE_NEWDECIMAL
323
405
    DRIZZLE_TYPE_VARCHAR,
324
406
    //DRIZZLE_TYPE_ENUM
325
407
    DRIZZLE_TYPE_VARCHAR,
326
408
    //DRIZZLE_TYPE_BLOB
327
409
    DRIZZLE_TYPE_BLOB,
328
 
    //DRIZZLE_TYPE_UUID
329
 
    DRIZZLE_TYPE_VARCHAR,
330
 
   },
 
410
  },
331
411
  /* DRIZZLE_TYPE_BLOB -> */
332
412
  {
 
413
    //DRIZZLE_TYPE_TINY
 
414
    DRIZZLE_TYPE_BLOB,
333
415
    //DRIZZLE_TYPE_LONG
334
416
    DRIZZLE_TYPE_BLOB,
335
417
    //DRIZZLE_TYPE_DOUBLE
346
428
    DRIZZLE_TYPE_BLOB,
347
429
    //DRIZZLE_TYPE_VARCHAR
348
430
    DRIZZLE_TYPE_BLOB,
349
 
    //DRIZZLE_TYPE_DECIMAL
 
431
    //DRIZZLE_TYPE_VIRTUAL
 
432
    DRIZZLE_TYPE_VIRTUAL,
 
433
    //DRIZZLE_TYPE_NEWDECIMAL
350
434
    DRIZZLE_TYPE_BLOB,
351
435
    //DRIZZLE_TYPE_ENUM
352
436
    DRIZZLE_TYPE_BLOB,
353
437
    //DRIZZLE_TYPE_BLOB
354
438
    DRIZZLE_TYPE_BLOB,
355
 
    //DRIZZLE_TYPE_UUID
356
 
    DRIZZLE_TYPE_VARCHAR,
357
 
  },
358
 
  /* DRIZZLE_TYPE_UUID -> */
359
 
  {
360
 
    //DRIZZLE_TYPE_LONG
361
 
    DRIZZLE_TYPE_VARCHAR,
362
 
    //DRIZZLE_TYPE_DOUBLE
363
 
    DRIZZLE_TYPE_VARCHAR,
364
 
    //DRIZZLE_TYPE_NULL
365
 
    DRIZZLE_TYPE_UUID,
366
 
    //DRIZZLE_TYPE_TIMESTAMP
367
 
    DRIZZLE_TYPE_VARCHAR,
368
 
    //DRIZZLE_TYPE_LONGLONG
369
 
    DRIZZLE_TYPE_VARCHAR,
370
 
    //DRIZZLE_TYPE_DATETIME
371
 
    DRIZZLE_TYPE_VARCHAR,
372
 
    //DRIZZLE_TYPE_DATE
373
 
    DRIZZLE_TYPE_VARCHAR,
374
 
    //DRIZZLE_TYPE_VARCHAR
375
 
    DRIZZLE_TYPE_VARCHAR,
376
 
    //DRIZZLE_TYPE_DECIMAL
377
 
    DRIZZLE_TYPE_VARCHAR,
378
 
    //DRIZZLE_TYPE_VARCHAR,
379
 
    DRIZZLE_TYPE_VARCHAR,
380
 
    //DRIZZLE_TYPE_BLOB
381
 
    DRIZZLE_TYPE_BLOB,
382
 
    //DRIZZLE_TYPE_UUID
383
 
    DRIZZLE_TYPE_UUID,
384
439
  },
385
440
};
386
441
 
387
 
static Item_result field_types_result_type [enum_field_types_size]=
388
 
{
 
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,
389
465
  //DRIZZLE_TYPE_LONG
390
466
  INT_RESULT,
391
467
  //DRIZZLE_TYPE_DOUBLE
402
478
  STRING_RESULT,
403
479
  //DRIZZLE_TYPE_VARCHAR
404
480
  STRING_RESULT,
405
 
  //DRIZZLE_TYPE_DECIMAL   
 
481
  //DRIZZLE_TYPE_VIRTUAL
 
482
  STRING_RESULT,
 
483
  //DRIZZLE_TYPE_NEWDECIMAL   
406
484
  DECIMAL_RESULT,           
407
485
  //DRIZZLE_TYPE_ENUM
408
486
  STRING_RESULT,
409
487
  //DRIZZLE_TYPE_BLOB
410
488
  STRING_RESULT,
411
 
  //DRIZZLE_TYPE_UUID
412
 
  STRING_RESULT,
413
489
};
414
490
 
415
 
bool test_if_important_data(const CHARSET_INFO * const cs, 
416
 
                            const char *str,
417
 
                            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)
418
511
{
419
512
  if (cs != &my_charset_bin)
420
513
    str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
421
514
  return (str < strend);
422
515
}
423
516
 
424
 
void *Field::operator new(size_t size)
425
 
{
426
 
  return memory::sql_alloc(size);
427
 
}
428
 
 
429
 
void *Field::operator new(size_t size, memory::Root *mem_root)
430
 
{
431
 
  return mem_root->alloc_root(static_cast<uint32_t>(size));
432
 
}
433
 
 
434
 
enum_field_types Field::field_type_merge(enum_field_types a,
435
 
                                         enum_field_types b)
436
 
{
437
 
  assert(a < enum_field_types_size);
438
 
  assert(b < enum_field_types_size);
439
 
  return field_types_merge_rules[a][b];
440
 
}
441
517
 
442
518
Item_result Field::result_merge_type(enum_field_types field_type)
443
519
{
444
 
  assert(field_type < enum_field_types_size);
 
520
  assert(field_type <= DRIZZLE_TYPE_MAX);
445
521
  return field_types_result_type[field_type];
446
522
}
447
523
 
 
524
 
448
525
bool Field::eq(Field *field)
449
526
{
450
527
  return (ptr == field->ptr && null_ptr == field->null_ptr &&
451
528
          null_bit == field->null_bit);
452
529
}
453
530
 
 
531
 
454
532
uint32_t Field::pack_length() const
455
533
{
456
534
  return field_length;
457
535
}
458
536
 
 
537
 
459
538
uint32_t Field::pack_length_in_rec() const
460
539
{
461
540
  return pack_length();
462
541
}
463
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
 
464
562
uint32_t Field::data_length()
465
563
{
466
564
  return pack_length();
467
565
}
468
566
 
 
567
 
469
568
uint32_t Field::used_length()
470
569
{
471
570
  return pack_length();
472
571
}
473
572
 
 
573
 
474
574
uint32_t Field::sort_length() const
475
575
{
476
576
  return pack_length();
477
577
}
478
578
 
 
579
 
479
580
uint32_t Field::max_data_length() const
480
581
{
481
582
  return pack_length();
482
583
}
483
584
 
 
585
 
484
586
int Field::reset(void)
485
587
{
486
588
  memset(ptr, 0, pack_length());
487
589
  return 0;
488
590
}
489
591
 
 
592
 
490
593
void Field::reset_fields()
491
594
{}
492
595
 
 
596
 
493
597
void Field::set_default()
494
598
{
495
 
  ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->getInsertRecord());
 
599
  my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->getDefaultValues() - table->record[0]);
496
600
  memcpy(ptr, ptr + l_offset, pack_length());
497
601
  if (null_ptr)
498
602
    *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
499
 
 
500
 
  if (this == table->next_number_field)
501
 
    table->auto_increment_field_not_null= false;
502
603
}
503
604
 
 
605
 
504
606
bool Field::binary() const
505
607
{
506
 
  return true;
 
608
  return 1;
507
609
}
508
610
 
 
611
 
509
612
bool Field::zero_pack() const
510
613
{
511
 
  return true;
 
614
  return 1;
512
615
}
513
616
 
 
617
 
514
618
enum ha_base_keytype Field::key_type() const
515
619
{
516
620
  return HA_KEYTYPE_BINARY;
517
621
}
518
622
 
 
623
 
519
624
uint32_t Field::key_length() const
520
625
{
521
626
  return pack_length();
522
627
}
523
628
 
 
629
 
524
630
enum_field_types Field::real_type() const
525
631
{
526
632
  return type();
527
633
}
528
634
 
 
635
 
529
636
int Field::cmp_max(const unsigned char *a, const unsigned char *b, uint32_t)
530
637
{
531
638
  return cmp(a, b);
532
639
}
533
640
 
 
641
 
534
642
int Field::cmp_binary(const unsigned char *a,const unsigned char *b, uint32_t)
535
643
{
536
644
  return memcmp(a,b,pack_length());
537
645
}
538
646
 
 
647
 
539
648
int Field::cmp_offset(uint32_t row_offset)
540
649
{
541
650
  return cmp(ptr,ptr+row_offset);
542
651
}
543
652
 
 
653
 
544
654
int Field::cmp_binary_offset(uint32_t row_offset)
545
655
{
546
656
  return cmp_binary(ptr, ptr+row_offset);
547
657
}
548
658
 
 
659
 
549
660
int Field::key_cmp(const unsigned char *a,const unsigned char *b)
550
661
{
551
662
  return cmp(a, b);
552
663
}
553
664
 
 
665
 
554
666
int Field::key_cmp(const unsigned char *str, uint32_t)
555
667
{
556
668
  return cmp(ptr,str);
557
669
}
558
670
 
 
671
 
559
672
uint32_t Field::decimals() const
560
673
{
561
674
  return 0;
562
675
}
563
676
 
564
 
bool Field::is_null(ptrdiff_t row_offset)
 
677
 
 
678
bool Field::is_null(my_ptrdiff_t row_offset)
565
679
{
566
680
  return null_ptr ?
567
681
    (null_ptr[row_offset] & null_bit ? true : false) :
568
682
    table->null_row;
569
683
}
570
684
 
571
 
bool Field::is_real_null(ptrdiff_t row_offset)
 
685
 
 
686
bool Field::is_real_null(my_ptrdiff_t row_offset)
572
687
{
573
688
  return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : false;
574
689
}
575
690
 
 
691
 
576
692
bool Field::is_null_in_record(const unsigned char *record)
577
693
{
578
 
  if (! null_ptr)
 
694
  if (!null_ptr)
579
695
    return false;
580
 
  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);
581
698
}
582
699
 
583
 
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset)
 
700
 
 
701
bool Field::is_null_in_record_with_offset(my_ptrdiff_t with_offset)
584
702
{
585
 
  if (! null_ptr)
 
703
  if (!null_ptr)
586
704
    return false;
587
705
  return test(null_ptr[with_offset] & null_bit);
588
706
}
589
707
 
590
 
void Field::set_null(ptrdiff_t row_offset)
 
708
 
 
709
void Field::set_null(my_ptrdiff_t row_offset)
591
710
{
592
711
  if (null_ptr)
593
712
    null_ptr[row_offset]|= null_bit;
594
713
}
595
714
 
596
 
void Field::set_notnull(ptrdiff_t row_offset)
 
715
 
 
716
void Field::set_notnull(my_ptrdiff_t row_offset)
597
717
{
598
718
  if (null_ptr)
599
719
    null_ptr[row_offset]&= (unsigned char) ~null_bit;
600
720
}
601
721
 
 
722
 
602
723
bool Field::maybe_null(void)
603
724
{
604
725
  return null_ptr != 0 || table->maybe_null;
605
726
}
606
727
 
 
728
 
607
729
bool Field::real_maybe_null(void)
608
730
{
609
731
  return null_ptr != 0;
610
732
}
611
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
 
612
747
bool Field::type_can_have_key_part(enum enum_field_types type)
613
748
{
614
749
  switch (type) {
620
755
  }
621
756
}
622
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
 
623
771
int Field::warn_if_overflow(int op_result)
624
772
{
625
773
  if (op_result == E_DEC_OVERFLOW)
626
774
  {
627
775
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
628
 
    return E_DEC_OVERFLOW;
 
776
    return 1;
629
777
  }
630
778
  if (op_result == E_DEC_TRUNCATED)
631
779
  {
632
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
633
 
    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 */
634
782
  }
635
783
  return 0;
636
784
}
637
785
 
 
786
 
638
787
void Field::init(Table *table_arg)
639
788
{
640
789
  orig_table= table= table_arg;
641
 
}
 
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
 
642
871
 
643
872
/// This is used as a table name when the table structure is not set up
644
 
Field::Field(unsigned char *ptr_arg,
645
 
             uint32_t length_arg,
646
 
             unsigned char *null_ptr_arg,
 
873
Field::Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
647
874
             unsigned char null_bit_arg,
648
 
             utype unireg_check_arg, 
649
 
             const char *field_name_arg)
650
 
  :
651
 
    ptr(ptr_arg),
652
 
    null_ptr(null_ptr_arg),
653
 
    table(NULL),
654
 
    orig_table(NULL),
655
 
    field_name(field_name_arg),
656
 
    key_start(0),
657
 
    part_of_key(0),
658
 
    part_of_key_not_clustered(0),
659
 
    part_of_sortkey(0),
660
 
    unireg_check(unireg_check_arg),
661
 
    field_length(length_arg),
662
 
    null_bit(null_bit_arg),
663
 
    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)
664
884
{
665
 
  flags= null_ptr ? 0: NOT_NULL_FLAG;
 
885
  flags=null_ptr ? 0: NOT_NULL_FLAG;
666
886
  comment.str= (char*) "";
667
 
  comment.length= 0;
 
887
  comment.length=0;
668
888
  field_index= 0;
669
889
}
670
890
 
 
891
 
671
892
void Field::hash(uint32_t *nr, uint32_t *nr2)
672
893
{
673
894
  if (is_null())
682
903
  }
683
904
}
684
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
 
685
916
void Field::copy_from_tmp(int row_offset)
686
917
{
687
918
  memcpy(ptr,ptr+row_offset,pack_length());
694
925
  }
695
926
}
696
927
 
697
 
int Field::store_and_check(enum_check_fields check_level,
698
 
                           const char *to, 
699
 
                           uint32_t length,
700
 
                           const CHARSET_INFO * const cs)
701
 
 
 
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)
702
952
{
703
953
  int res;
704
954
  enum_check_fields old_check_level= table->in_use->count_cuted_fields;
708
958
  return res;
709
959
}
710
960
 
711
 
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)
712
1002
{
713
1003
  uint32_t length= pack_length();
714
1004
  set_if_smaller(length, max_length);
716
1006
  return to+length;
717
1007
}
718
1008
 
 
1009
 
719
1010
unsigned char *Field::pack(unsigned char *to, const unsigned char *from)
720
1011
{
721
 
  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);
722
1014
  return(result);
723
1015
}
724
1016
 
725
 
const unsigned char *Field::unpack(unsigned char* to,
726
 
                                   const unsigned char *from, 
727
 
                                   uint32_t param_data,
728
 
                                   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)
729
1051
{
730
1052
  uint32_t length=pack_length();
731
1053
  int from_type= 0;
751
1073
            param_data : length;
752
1074
 
753
1075
  memcpy(to, from, param_data > length ? length : len);
754
 
  return (from + len);
 
1076
  return from+len;
755
1077
}
756
1078
 
757
 
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)
758
1082
{
759
 
  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);
760
1085
  return(result);
761
1086
}
762
1087
 
 
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
 
763
1108
my_decimal *Field::val_decimal(my_decimal *)
764
1109
{
765
1110
  /* This never have to be called */
768
1113
}
769
1114
 
770
1115
 
771
 
void Field::make_field(SendField *field)
 
1116
void Field::make_field(Send_field *field)
772
1117
{
773
 
  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)
774
1119
  {
775
 
    field->db_name= orig_table->getShare()->getSchemaName();
776
 
    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;
777
1122
  }
778
1123
  else
779
1124
    field->org_table_name= field->db_name= "";
780
1125
  if (orig_table)
781
1126
  {
782
 
    field->table_name= orig_table->getAlias();
 
1127
    field->table_name= orig_table->alias;
783
1128
    field->org_col_name= field_name;
784
1129
  }
785
1130
  else
789
1134
  }
790
1135
  field->col_name= field_name;
791
1136
  field->charsetnr= charset()->number;
792
 
  field->length= field_length;
793
 
  field->type= type();
794
 
  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;
795
1140
  field->decimals= 0;
796
1141
}
797
1142
 
 
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
*/
798
1155
int64_t Field::convert_decimal2int64_t(const my_decimal *val, bool, int *err)
799
1156
{
800
1157
  int64_t i;
808
1165
  return i;
809
1166
}
810
1167
 
811
 
uint32_t Field::fill_cache_field(CacheField *copy)
 
1168
uint32_t Field::fill_cache_field(CACHE_FIELD *copy)
812
1169
{
813
1170
  uint32_t store_length;
814
1171
  copy->str=ptr;
818
1175
  {
819
1176
    copy->blob_field=(Field_blob*) this;
820
1177
    copy->strip=0;
821
 
    copy->length-= table->getShare()->blob_ptr_size;
 
1178
    copy->length-= table->s->blob_ptr_size;
822
1179
    return copy->length;
823
1180
  }
824
1181
  else
829
1186
  return copy->length+ store_length;
830
1187
}
831
1188
 
 
1189
 
832
1190
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
833
1191
{
834
1192
  char buff[40];
835
1193
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
836
 
  if (!(res=val_str_internal(&tmp)) || str_to_datetime_with_warn(res->ptr(), res->length(),
837
 
                                                                 ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
838
 
  {
 
1194
  if (!(res=val_str(&tmp)) ||
 
1195
      str_to_datetime_with_warn(res->ptr(), res->length(),
 
1196
                                ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
839
1197
    return 1;
840
 
  }
841
 
 
842
1198
  return 0;
843
1199
}
844
1200
 
846
1202
{
847
1203
  char buff[40];
848
1204
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
849
 
 
850
 
  if (!(res=val_str_internal(&tmp)) || str_to_time_with_warn(res->ptr(), res->length(), ltime))
851
 
  {
 
1205
  if (!(res=val_str(&tmp)) ||
 
1206
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
852
1207
    return 1;
853
 
  }
854
 
 
855
1208
  return 0;
856
1209
}
857
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
 
858
1218
int Field::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
859
1219
{
860
1220
  char buff[MAX_DATE_STRING_REP_LENGTH];
862
1222
  return store(buff, length, &my_charset_bin);
863
1223
}
864
1224
 
865
 
bool Field::optimize_range(uint32_t idx, uint32_t)
 
1225
 
 
1226
bool Field::optimize_range(uint32_t idx, uint32_t part)
866
1227
{
867
 
  return test(table->index_flags(idx) & HA_READ_RANGE);
 
1228
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
868
1229
}
869
1230
 
870
 
Field *Field::new_field(memory::Root *root, Table *new_table, bool)
 
1231
 
 
1232
Field *Field::new_field(MEM_ROOT *root, Table *new_table, bool)
871
1233
{
872
1234
  Field *tmp;
873
 
  if (!(tmp= (Field*) root->memdup_root((char*) this,size_of())))
 
1235
  if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
874
1236
    return 0;
875
1237
 
876
1238
  if (tmp->table->maybe_null)
877
1239
    tmp->flags&= ~NOT_NULL_FLAG;
878
1240
  tmp->table= new_table;
879
 
  tmp->key_start.reset();
880
 
  tmp->part_of_key.reset();
881
 
  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);
882
1244
  tmp->unireg_check= Field::NONE;
883
 
  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);
884
1246
  tmp->reset_fields();
885
1247
  return tmp;
886
1248
}
887
1249
 
888
 
Field *Field::new_key_field(memory::Root *root, Table *new_table,
889
 
                            unsigned char *new_ptr,
890
 
                            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,
891
1253
                            uint32_t new_null_bit)
892
1254
{
893
1255
  Field *tmp;
894
1256
  if ((tmp= new_field(root, new_table, table == new_table)))
895
1257
  {
896
 
    tmp->ptr= new_ptr;
 
1258
    tmp->ptr=      new_ptr;
897
1259
    tmp->null_ptr= new_null_ptr;
898
1260
    tmp->null_bit= new_null_bit;
899
1261
  }
900
1262
  return tmp;
901
1263
}
902
1264
 
903
 
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)
904
1269
{
905
1270
  Field *tmp;
906
 
  if ((tmp= (Field*) root->memdup_root((char*) this,size_of())))
 
1271
  if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
907
1272
  {
908
1273
    tmp->init(new_table);
909
 
    tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() -
910
 
                                           new_table->getDefaultValues()));
 
1274
    tmp->move_field_offset((my_ptrdiff_t) (new_table->record[0] -
 
1275
                                           new_table->s->default_values));
911
1276
  }
912
1277
  return tmp;
913
1278
}
914
1279
 
915
1280
 
916
 
uint32_t Field::is_equal(CreateField *new_field_ptr)
 
1281
uint32_t Field::is_equal(Create_field *new_field_ptr)
917
1282
{
918
1283
  return (new_field_ptr->sql_type == real_type());
919
1284
}
920
1285
 
 
1286
/**
 
1287
  @retval
 
1288
    1  if the fields are equally defined
 
1289
  @retval
 
1290
    0  if the fields are unequally defined
 
1291
*/
 
1292
 
921
1293
bool Field::eq_def(Field *field)
922
1294
{
923
1295
  if (real_type() != field->real_type() || charset() != field->charset() ||
926
1298
  return 1;
927
1299
}
928
1300
 
 
1301
/**
 
1302
  @return
 
1303
  returns 1 if the fields are equally defined
 
1304
*/
929
1305
bool Field_enum::eq_def(Field *field)
930
1306
{
931
1307
  if (!Field::eq_def(field))
944
1320
  return 1;
945
1321
}
946
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
 
947
1689
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
948
1690
{
949
1691
  switch (type) {
950
 
  case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
951
 
  case DRIZZLE_TYPE_UUID: return field::Uuid::max_string_length();
952
 
  case DRIZZLE_TYPE_DATE:
953
 
  case DRIZZLE_TYPE_ENUM:
954
 
  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;
955
1697
  case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
956
1698
  case DRIZZLE_TYPE_DATETIME:
957
 
  case DRIZZLE_TYPE_TIMESTAMP:
958
1699
  case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
959
 
  case DRIZZLE_TYPE_NULL: return 0;
960
 
  case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
961
 
  case DRIZZLE_TYPE_DECIMAL:
962
 
                          assert(0);
963
 
                          abort();
 
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;
964
1707
  }
965
 
 
966
 
  assert(0);
967
 
  abort();
968
1708
}
969
1709
 
 
1710
 
970
1711
uint32_t pack_length_to_packflag(uint32_t type)
971
1712
{
972
1713
  switch (type) {
973
1714
    case 1: return 1 << FIELDFLAG_PACK_SHIFT;
974
1715
    case 2: assert(1);
975
1716
    case 3: assert(1);
976
 
    case 4: return f_settype(DRIZZLE_TYPE_LONG);
977
 
    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);
978
1719
  }
979
1720
  return 0;                                     // This shouldn't happen
980
1721
}
981
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
 
982
1907
/*****************************************************************************
983
1908
 Warning handling
984
1909
*****************************************************************************/
985
1910
 
986
 
bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
987
 
                        uint32_t code,
988
 
                        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)
989
1934
{
990
1935
  /*
991
1936
    If this field was created only for type conversion purposes it
1003
1948
}
1004
1949
 
1005
1950
 
1006
 
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1007
 
                                 unsigned int code,
1008
 
                                 const char *str, 
1009
 
                                 uint32_t str_length,
1010
 
                                 enum enum_drizzle_timestamp_type ts_type, 
1011
 
                                 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)
1012
1972
{
1013
1973
  Session *session= table ? table->in_use : current_session;
1014
1974
  if ((session->really_abort_on_warning() &&
1018
1978
                                 field_name);
1019
1979
}
1020
1980
 
1021
 
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, 
1022
 
                                 uint32_t code,
1023
 
                                 int64_t nr, 
1024
 
                                 enum enum_drizzle_timestamp_type ts_type,
1025
 
                                 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)
1026
2001
{
1027
2002
  Session *session= table ? table->in_use : current_session;
1028
2003
  if (session->really_abort_on_warning() ||
1029
2004
      set_warning(level, code, cuted_increment))
1030
2005
  {
1031
2006
    char str_nr[22];
1032
 
    char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
 
2007
    char *str_end= int64_t10_to_str(nr, str_nr, -10);
1033
2008
    make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
1034
2009
                                 ts_type, field_name);
1035
2010
  }
1036
2011
}
1037
2012
 
1038
 
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1039
 
                                 const uint32_t code,
1040
 
                                 double nr, 
1041
 
                                 enum enum_drizzle_timestamp_type 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)
1042
2031
{
1043
2032
  Session *session= table ? table->in_use : current_session;
1044
2033
  if (session->really_abort_on_warning() ||
1046
2035
  {
1047
2036
    /* DBL_DIG is enough to print '-[digits].E+###' */
1048
2037
    char str_nr[DBL_DIG + 8];
1049
 
    uint32_t str_len= snprintf(str_nr, sizeof(str_nr), "%g", nr);
 
2038
    uint32_t str_len= sprintf(str_nr, "%g", nr);
1050
2039
    make_truncated_value_warning(session, level, str_nr, str_len, ts_type,
1051
2040
                                 field_name);
1052
2041
  }
1053
2042
}
1054
2043
 
1055
 
bool Field::isReadSet() 
1056
 
1057
 
  return table->isReadSet(field_index); 
1058
 
}
1059
 
 
1060
 
bool Field::isWriteSet()
1061
 
1062
 
  return table->isWriteSet(field_index); 
1063
 
}
1064
 
 
1065
 
void Field::setReadSet(bool arg)
1066
 
{
1067
 
  if (arg)
1068
 
    table->setReadSet(field_index);
1069
 
  else
1070
 
    table->clearReadSet(field_index);
1071
 
}
1072
 
 
1073
 
void Field::setWriteSet(bool arg)
1074
 
{
1075
 
  if (arg)
1076
 
    table->setWriteSet(field_index);
1077
 
  else
1078
 
    table->clearWriteSet(field_index);
1079
 
}
1080
 
 
1081
 
std::ostream& operator<<(std::ostream& output, const Field &field)
1082
 
{
1083
 
  output << "Field:(";
1084
 
  output <<  field.field_name;
1085
 
  output << ", ";
1086
 
  output << drizzled::display::type(field.real_type());
1087
 
  output << ")";
1088
 
 
1089
 
  return output;  // for multiple << operators.
1090
 
}
1091
 
 
1092
 
} /* namespace drizzled */