~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.cc

  • Committer: Monty Taylor
  • Date: 2009-04-14 19:16:51 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 994.
  • Revision ID: mordred@inaugust.com-20090414191651-ltbww6hpqks8k7qk
Clarified instructions in README.

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