34
37
struct st_cache_field;
35
38
int field_conv(Field *to,Field *from);
37
inline uint32_t get_enum_pack_length(int elements)
40
inline uint get_enum_pack_length(int elements)
39
42
return elements < 256 ? 1 : 2;
42
inline uint32_t get_set_pack_length(int elements)
45
inline uint get_set_pack_length(int elements)
44
uint32_t len= (elements + 7) / 8;
47
uint len= (elements + 7) / 8;
45
48
return len > 4 ? 8 : len;
48
class virtual_column_info: public Sql_alloc
55
: expr_item(0), item_free_list(0),
56
field_type(DRIZZLE_TYPE_VIRTUAL),
57
is_stored(false), data_inited(false)
62
~virtual_column_info() {}
63
enum_field_types get_real_type()
66
return data_inited ? field_type : DRIZZLE_TYPE_VIRTUAL;
68
void set_field_type(enum_field_types fld_type)
70
/* Calling this function can only be done once. */
71
assert(not data_inited);
75
bool get_field_stored()
78
return data_inited ? is_stored : true;
80
void set_field_stored(bool stored)
86
The following data is only updated by the parser and read
87
when a Create_field object is created/initialized.
89
enum_field_types field_type; /* Real field type*/
90
bool is_stored; /* Indication that the field is
91
phisically stored in the database*/
93
This flag is used to prevent other applications from
94
reading and using incorrect data.
101
53
Field(const Item &); /* Prevent use of these */
150
102
bool is_created_from_null_item;
152
/* Virtual column data */
153
virtual_column_info *vcol_info;
155
Indication that the field is phycically stored in tables
156
rather than just generated on SQL queries.
157
As of now, false can only be set for generated-only virtual columns.
161
Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
162
unsigned char null_bit_arg, utype unireg_check_arg,
104
Field(uchar *ptr_arg,uint32_t length_arg,uchar *null_ptr_arg,
105
uchar null_bit_arg, utype unireg_check_arg,
163
106
const char *field_name_arg);
164
107
virtual ~Field() {}
165
108
/* Store functions returns 1 on overflow and -1 on fatal error */
166
virtual int store(const char *to, uint32_t length, const CHARSET_INFO * const cs)=0;
109
virtual int store(const char *to, uint length, const CHARSET_INFO * const cs)=0;
167
110
virtual int store(double nr)=0;
168
111
virtual int store(int64_t nr, bool unsigned_val)=0;
169
112
virtual int store_decimal(const my_decimal *d)=0;
170
113
virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
171
int store(const char *to, uint32_t length, const CHARSET_INFO * const cs,
114
int store(const char *to, uint length, const CHARSET_INFO * const cs,
172
115
enum_check_fields check_level);
173
116
virtual double val_real(void)=0;
174
117
virtual int64_t val_int(void)=0;
276
219
virtual uint32_t key_length() const { return pack_length(); }
277
220
virtual enum_field_types type() const =0;
278
221
virtual enum_field_types real_type() const { return type(); }
279
inline int cmp(const unsigned char *str) { return cmp(ptr,str); }
280
virtual int cmp_max(const unsigned char *a, const unsigned char *b,
281
uint32_t max_len __attribute__((unused)))
222
inline int cmp(const uchar *str) { return cmp(ptr,str); }
223
virtual int cmp_max(const uchar *a, const uchar *b,
224
uint max_len __attribute__((unused)))
282
225
{ return cmp(a, b); }
283
virtual int cmp(const unsigned char *,const unsigned char *)=0;
284
virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
226
virtual int cmp(const uchar *,const uchar *)=0;
227
virtual int cmp_binary(const uchar *a,const uchar *b,
285
228
uint32_t __attribute__((unused)) max_length=UINT32_MAX)
286
229
{ return memcmp(a,b,pack_length()); }
287
virtual int cmp_offset(uint32_t row_offset)
230
virtual int cmp_offset(uint row_offset)
288
231
{ return cmp(ptr,ptr+row_offset); }
289
virtual int cmp_binary_offset(uint32_t row_offset)
232
virtual int cmp_binary_offset(uint row_offset)
290
233
{ return cmp_binary(ptr, ptr+row_offset); };
291
virtual int key_cmp(const unsigned char *a,const unsigned char *b)
234
virtual int key_cmp(const uchar *a,const uchar *b)
292
235
{ return cmp(a, b); }
293
virtual int key_cmp(const unsigned char *str, uint32_t length __attribute__((unused)))
236
virtual int key_cmp(const uchar *str, uint length __attribute__((unused)))
294
237
{ return cmp(ptr,str); }
295
virtual uint32_t decimals() const { return 0; }
238
virtual uint decimals() const { return 0; }
297
240
Caller beware: sql_type can change str.Ptr, so check
298
241
ptr() to see if it changed if you are using your own buffer
299
242
in str and restore it with set() if needed
301
244
virtual void sql_type(String &str) const =0;
302
virtual uint32_t size_of() const =0; // For new field
245
virtual uint size_of() const =0; // For new field
303
246
inline bool is_null(my_ptrdiff_t row_offset= 0)
304
247
{ return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : table->null_row; }
305
248
inline bool is_real_null(my_ptrdiff_t row_offset= 0)
306
249
{ return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : 0; }
307
inline bool is_null_in_record(const unsigned char *record)
250
inline bool is_null_in_record(const uchar *record)
311
return test(record[(uint32_t) (null_ptr -table->record[0])] &
254
return test(record[(uint) (null_ptr -table->record[0])] &
314
257
inline bool is_null_in_record_with_offset(my_ptrdiff_t offset)
364
307
virtual Field *new_field(MEM_ROOT *root, Table *new_table,
366
309
virtual Field *new_key_field(MEM_ROOT *root, Table *new_table,
367
unsigned char *new_ptr, unsigned char *new_null_ptr,
368
uint32_t new_null_bit);
310
uchar *new_ptr, uchar *new_null_ptr,
369
312
Field *clone(MEM_ROOT *mem_root, Table *new_table);
370
inline void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
313
inline void move_field(uchar *ptr_arg,uchar *null_ptr_arg,uchar null_bit_arg)
372
315
ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
374
inline void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
317
inline void move_field(uchar *ptr_arg) { ptr=ptr_arg; }
375
318
virtual void move_field_offset(my_ptrdiff_t ptr_diff)
377
ptr=ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
320
ptr=ADD_TO_PTR(ptr,ptr_diff, uchar*);
379
null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,unsigned char*);
322
null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,uchar*);
381
virtual void get_image(unsigned char *buff, uint32_t length,
324
virtual void get_image(uchar *buff, uint length,
382
325
const CHARSET_INFO * const cs __attribute__((unused)))
383
326
{ memcpy(buff,ptr,length); }
384
virtual void set_image(const unsigned char *buff,uint32_t length,
327
virtual void set_image(const uchar *buff,uint length,
385
328
const CHARSET_INFO * const cs __attribute__((unused)))
386
329
{ memcpy(ptr,buff,length); }
412
355
Number of copied bytes (excluding padded zero bytes -- see above).
415
virtual uint32_t get_key_image(unsigned char *buff, uint32_t length,
358
virtual uint get_key_image(uchar *buff, uint length,
416
359
imagetype type __attribute__((unused)))
418
361
get_image(buff, length, &my_charset_bin);
421
virtual void set_key_image(const unsigned char *buff,uint32_t length)
364
virtual void set_key_image(const uchar *buff,uint length)
422
365
{ set_image(buff,length, &my_charset_bin); }
423
inline int64_t val_int_offset(uint32_t row_offset)
366
inline int64_t val_int_offset(uint row_offset)
426
369
int64_t tmp=val_int();
430
inline int64_t val_int(const unsigned char *new_ptr)
373
inline int64_t val_int(const uchar *new_ptr)
432
unsigned char *old_ptr= ptr;
433
376
int64_t return_value;
434
ptr= (unsigned char*) new_ptr;
377
ptr= (uchar*) new_ptr;
435
378
return_value= val_int();
437
380
return return_value;
439
inline String *val_str(String *str, const unsigned char *new_ptr)
382
inline String *val_str(String *str, const uchar *new_ptr)
441
unsigned char *old_ptr= ptr;
442
ptr= (unsigned char*) new_ptr;
385
ptr= (uchar*) new_ptr;
447
390
virtual bool send_binary(Protocol *protocol);
449
virtual unsigned char *pack(unsigned char *to, const unsigned char *from,
450
uint32_t max_length, bool low_byte_first);
452
@overload Field::pack(unsigned char*, const unsigned char*, uint32_t, bool)
454
unsigned char *pack(unsigned char *to, const unsigned char *from)
456
unsigned char *result= this->pack(to, from, UINT_MAX, table->s->db_low_byte_first);
460
virtual const unsigned char *unpack(unsigned char* to, const unsigned char *from,
461
uint32_t param_data, bool low_byte_first);
463
@overload Field::unpack(unsigned char*, const unsigned char*, uint32_t, bool)
465
const unsigned char *unpack(unsigned char* to, const unsigned char *from)
467
const unsigned char *result= unpack(to, from, 0U, table->s->db_low_byte_first);
471
virtual unsigned char *pack_key(unsigned char* to, const unsigned char *from,
472
uint32_t max_length, bool low_byte_first)
474
return pack(to, from, max_length, low_byte_first);
476
virtual unsigned char *pack_key_from_key_image(unsigned char* to, const unsigned char *from,
477
uint32_t max_length, bool low_byte_first)
479
return pack(to, from, max_length, low_byte_first);
481
virtual const unsigned char *unpack_key(unsigned char* to, const unsigned char *from,
482
uint32_t max_length, bool low_byte_first)
392
virtual uchar *pack(uchar *to, const uchar *from,
393
uint max_length, bool low_byte_first);
395
@overload Field::pack(uchar*, const uchar*, uint, bool)
397
uchar *pack(uchar *to, const uchar *from)
399
uchar *result= this->pack(to, from, UINT_MAX, table->s->db_low_byte_first);
403
virtual const uchar *unpack(uchar* to, const uchar *from,
404
uint param_data, bool low_byte_first);
406
@overload Field::unpack(uchar*, const uchar*, uint, bool)
408
const uchar *unpack(uchar* to, const uchar *from)
410
const uchar *result= unpack(to, from, 0U, table->s->db_low_byte_first);
414
virtual uchar *pack_key(uchar* to, const uchar *from,
415
uint max_length, bool low_byte_first)
417
return pack(to, from, max_length, low_byte_first);
419
virtual uchar *pack_key_from_key_image(uchar* to, const uchar *from,
420
uint max_length, bool low_byte_first)
422
return pack(to, from, max_length, low_byte_first);
424
virtual const uchar *unpack_key(uchar* to, const uchar *from,
425
uint max_length, bool low_byte_first)
484
427
return unpack(to, from, max_length, low_byte_first);
486
virtual uint32_t packed_col_length(const unsigned char *to __attribute__((unused)),
429
virtual uint packed_col_length(const uchar *to __attribute__((unused)),
488
431
{ return length;}
489
virtual uint32_t max_packed_col_length(uint32_t max_length)
432
virtual uint max_packed_col_length(uint max_length)
490
433
{ return max_length;}
492
virtual int pack_cmp(const unsigned char *a,const unsigned char *b,
493
uint32_t key_length_arg __attribute__((unused)),
435
virtual int pack_cmp(const uchar *a,const uchar *b,
436
uint key_length_arg __attribute__((unused)),
494
437
bool insert_or_update __attribute__((unused)))
495
438
{ return cmp(a,b); }
496
virtual int pack_cmp(const unsigned char *b,
497
uint32_t key_length_arg __attribute__((unused)),
439
virtual int pack_cmp(const uchar *b,
440
uint key_length_arg __attribute__((unused)),
498
441
bool insert_or_update __attribute__((unused)))
499
442
{ return cmp(ptr,b); }
500
uint32_t offset(unsigned char *record)
443
uint offset(uchar *record)
502
return (uint32_t) (ptr - record);
445
return (uint) (ptr - record);
504
447
void copy_from_tmp(int offset);
505
uint32_t fill_cache_field(struct st_cache_field *copy);
506
virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
448
uint fill_cache_field(struct st_cache_field *copy);
449
virtual bool get_date(DRIZZLE_TIME *ltime,uint fuzzydate);
507
450
virtual bool get_time(DRIZZLE_TIME *ltime);
508
451
virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
509
452
virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
597
540
@returns 0 no bytes written.
599
virtual int do_save_field_metadata(unsigned char *metadata_ptr __attribute__((unused)))
542
virtual int do_save_field_metadata(uchar *metadata_ptr __attribute__((unused)))
547
class Field_num :public Field {
550
bool decimal_precision; // Purify cannot handle bit fields & only for decimal type
551
bool unsigned_flag; // Purify cannot handle bit fields
552
Field_num(uchar *ptr_arg,uint32_t len_arg, uchar *null_ptr_arg,
553
uchar null_bit_arg, utype unireg_check_arg,
554
const char *field_name_arg,
555
uint8_t dec_arg, bool zero_arg, bool unsigned_arg);
556
Item_result result_type () const { return REAL_RESULT; }
557
void add_unsigned(String &res) const;
558
friend class Create_field;
559
void make_field(Send_field *);
560
uint decimals() const { return (uint) dec; }
561
uint size_of() const { return sizeof(*this); }
562
bool eq_def(Field *field);
563
int store_decimal(const my_decimal *);
564
my_decimal *val_decimal(my_decimal *);
565
uint is_equal(Create_field *new_field);
566
int check_int(const CHARSET_INFO * const cs, const char *str, int length,
567
const char *int_end, int error);
568
bool get_int(const CHARSET_INFO * const cs, const char *from, uint len,
569
int64_t *rnd, uint64_t unsigned_max,
570
int64_t signed_min, int64_t signed_max);
573
/* base class for all string related classes */
575
class Field_str :public Field {
577
const CHARSET_INFO *field_charset;
578
enum Derivation field_derivation;
580
Field_str(uchar *ptr_arg,uint32_t len_arg, uchar *null_ptr_arg,
581
uchar null_bit_arg, utype unireg_check_arg,
582
const char *field_name_arg, const CHARSET_INFO * const charset);
583
Item_result result_type () const { return STRING_RESULT; }
584
uint decimals() const { return NOT_FIXED_DEC; }
585
int store(double nr);
586
int store(int64_t nr, bool unsigned_val)=0;
587
int store_decimal(const my_decimal *);
588
int store(const char *to,uint length, const CHARSET_INFO * const cs)=0;
589
uint size_of() const { return sizeof(*this); }
590
const CHARSET_INFO *charset(void) const { return field_charset; }
591
void set_charset(const CHARSET_INFO * const charset_arg) { field_charset= charset_arg; }
592
enum Derivation derivation(void) const { return field_derivation; }
593
virtual void set_derivation(enum Derivation derivation_arg)
594
{ field_derivation= derivation_arg; }
595
bool binary() const { return field_charset == &my_charset_bin; }
596
uint32_t max_display_length() { return field_length; }
597
friend class Create_field;
598
my_decimal *val_decimal(my_decimal *);
599
virtual bool str_needs_quotes() { return true; }
600
bool compare_str_field_flags(Create_field *new_field, uint32_t flags);
601
uint is_equal(Create_field *new_field);
605
/* base class for Field_varstring and Field_blob */
607
class Field_longstr :public Field_str
610
int report_if_important_data(const char *ptr, const char *end);
612
Field_longstr(uchar *ptr_arg, uint32_t len_arg, uchar *null_ptr_arg,
613
uchar null_bit_arg, utype unireg_check_arg,
614
const char *field_name_arg, const CHARSET_INFO * const charset_arg)
615
:Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
616
field_name_arg, charset_arg)
619
int store_decimal(const my_decimal *d);
620
uint32_t max_data_length() const;
623
/* base class for float and double and decimal (old one) */
624
class Field_real :public Field_num {
628
Field_real(uchar *ptr_arg, uint32_t len_arg, uchar *null_ptr_arg,
629
uchar null_bit_arg, utype unireg_check_arg,
630
const char *field_name_arg,
631
uint8_t dec_arg, bool zero_arg, bool unsigned_arg)
632
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
633
field_name_arg, dec_arg, zero_arg, unsigned_arg),
634
not_fixed(dec_arg >= NOT_FIXED_DEC)
636
int store_decimal(const my_decimal *);
637
my_decimal *val_decimal(my_decimal *);
638
int truncate(double *nr, double max_length);
639
uint32_t max_display_length() { return field_length; }
640
uint size_of() const { return sizeof(*this); }
641
virtual const uchar *unpack(uchar* to, const uchar *from,
642
uint param_data, bool low_byte_first);
643
virtual uchar *pack(uchar* to, const uchar *from,
644
uint max_length, bool low_byte_first);
648
class Field_tiny :public Field_num {
650
Field_tiny(uchar *ptr_arg, uint32_t len_arg, uchar *null_ptr_arg,
652
enum utype unireg_check_arg, const char *field_name_arg,
653
bool zero_arg, bool unsigned_arg)
654
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
655
unireg_check_arg, field_name_arg,
656
0, zero_arg,unsigned_arg)
658
enum Item_result result_type () const { return INT_RESULT; }
659
enum_field_types type() const { return DRIZZLE_TYPE_TINY;}
660
enum ha_base_keytype key_type() const
661
{ return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
662
int store(const char *to,uint length, const CHARSET_INFO * const charset);
663
int store(double nr);
664
int store(int64_t nr, bool unsigned_val);
665
int reset(void) { ptr[0]=0; return 0; }
666
double val_real(void);
667
int64_t val_int(void);
668
String *val_str(String*,String *);
669
bool send_binary(Protocol *protocol);
670
int cmp(const uchar *,const uchar *);
671
void sort_string(uchar *buff,uint length);
672
uint32_t pack_length() const { return 1; }
673
void sql_type(String &str) const;
674
uint32_t max_display_length() { return 4; }
676
virtual uchar *pack(uchar* to, const uchar *from,
677
uint max_length __attribute__((unused)),
678
bool low_byte_first __attribute__((unused)))
684
virtual const uchar *unpack(uchar* to, const uchar *from,
685
uint param_data __attribute__((unused)),
686
bool low_byte_first __attribute__((unused)))
604
694
Create field class for CREATE TABLE
700
779
typedef void Copy_func(Copy_field*);
701
780
Copy_func *get_copy_func(Field *to, Field *from);
703
unsigned char *from_ptr,*to_ptr;
704
unsigned char *from_null_ptr,*to_null_ptr;
782
uchar *from_ptr,*to_ptr;
783
uchar *from_null_ptr,*to_null_ptr;
706
uint32_t from_bit,to_bit;
707
uint32_t from_length,to_length;
785
uint from_bit,to_bit;
786
uint from_length,to_length;
708
787
Field *from_field,*to_field;
709
788
String tmp; // For items
713
792
void set(Field *to,Field *from,bool save); // Field to field
714
void set(unsigned char *to,Field *from); // Field to string
793
void set(uchar *to,Field *from); // Field to string
715
794
void (*do_copy)(Copy_field *);
716
795
void (*do_copy2)(Copy_field *); // Used to handle null values
720
Field *make_field(TABLE_SHARE *share, unsigned char *ptr, uint32_t field_length,
721
unsigned char *null_pos, unsigned char null_bit,
722
uint32_t pack_flag, enum_field_types field_type,
799
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32_t field_length,
800
uchar *null_pos, uchar null_bit,
801
uint pack_flag, enum_field_types field_type,
723
802
const CHARSET_INFO * cs,
724
803
Field::utype unireg_check,
725
804
TYPELIB *interval, const char *field_name);
726
uint32_t pack_length_to_packflag(uint32_t type);
805
uint pack_length_to_packflag(uint type);
727
806
enum_field_types get_blob_type_from_length(uint32_t length);
728
807
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
729
808
int set_field_to_null(Field *field);
730
809
int set_field_to_null_with_conversions(Field *field, bool no_conversions);
734
test_if_important_data(const CHARSET_INFO * const cs,
812
check_string_copy_error(Field_str *field,
813
const char *well_formed_error_pos,
814
const char *cannot_convert_error_pos,
816
const CHARSET_INFO * const cs);
741
#include <drizzled/field/str.h>
742
#include <drizzled/field/longstr.h>
743
#include <drizzled/field/num.h>
744
821
#include <drizzled/field/blob.h>
745
822
#include <drizzled/field/enum.h>
746
823
#include <drizzled/field/null.h>
747
824
#include <drizzled/field/date.h>
748
825
#include <drizzled/field/fdecimal.h>
749
#include <drizzled/field/real.h>
750
826
#include <drizzled/field/double.h>
751
827
#include <drizzled/field/long.h>
752
828
#include <drizzled/field/int64_t.h>
753
#include <drizzled/field/num.h>
754
829
#include <drizzled/field/timetype.h>
755
830
#include <drizzled/field/timestamp.h>
756
831
#include <drizzled/field/datetime.h>
806
881
#define f_bit_as_char(x) ((x) & FIELDFLAG_TREAT_BIT_AS_CHAR)
807
882
#define f_is_hex_escape(x) ((x) & FIELDFLAG_HEX_ESCAPE)
810
check_string_copy_error(Field_str *field,
811
const char *well_formed_error_pos,
812
const char *cannot_convert_error_pos,
814
const CHARSET_INFO * const cs);
817
class Field_tiny :public Field_num {
819
Field_tiny(unsigned char *ptr_arg, uint32_t len_arg, unsigned char *null_ptr_arg,
820
unsigned char null_bit_arg,
821
enum utype unireg_check_arg, const char *field_name_arg,
822
bool zero_arg, bool unsigned_arg)
823
:Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
824
unireg_check_arg, field_name_arg,
825
0, zero_arg,unsigned_arg)
827
enum Item_result result_type () const { return INT_RESULT; }
828
enum_field_types type() const { return DRIZZLE_TYPE_TINY;}
829
enum ha_base_keytype key_type() const
830
{ return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
831
int store(const char *to,uint32_t length, const CHARSET_INFO * const charset);
832
int store(double nr);
833
int store(int64_t nr, bool unsigned_val);
834
int reset(void) { ptr[0]=0; return 0; }
835
double val_real(void);
836
int64_t val_int(void);
837
String *val_str(String*,String *);
838
bool send_binary(Protocol *protocol);
839
int cmp(const unsigned char *,const unsigned char *);
840
void sort_string(unsigned char *buff,uint32_t length);
841
uint32_t pack_length() const { return 1; }
842
void sql_type(String &str) const;
843
uint32_t max_display_length() { return 4; }
845
virtual unsigned char *pack(unsigned char* to, const unsigned char *from,
846
uint32_t max_length __attribute__((unused)),
847
bool low_byte_first __attribute__((unused)))
853
virtual const unsigned char *unpack(unsigned char* to, const unsigned char *from,
854
uint32_t param_data __attribute__((unused)),
855
bool low_byte_first __attribute__((unused)))