26
26
#define DRIZZLED_FIELD_H
28
28
#include "drizzled/sql_error.h"
29
#include "drizzled/decimal.h"
29
#include "drizzled/my_decimal.h"
30
30
#include "drizzled/key_map.h"
31
#include "drizzled/sql_bitmap.h"
31
32
#include "drizzled/sql_list.h"
32
33
#include "drizzled/structs.h"
33
#include "drizzled/charset_info.h"
34
#include "drizzled/item_result.h"
42
38
#define DATETIME_DEC 6
43
39
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
46
#define ASSERT_COLUMN_MARKED_FOR_READ assert(!getTable() || (getTable()->read_set == NULL || isReadSet()))
47
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(!getTable() || (getTable()->write_set == NULL || isWriteSet()))
42
#define ASSERT_COLUMN_MARKED_FOR_READ assert(!table || (table->read_set == NULL || isReadSet()))
43
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(!table || (table->write_set == NULL || isWriteSet()))
49
#define ASSERT_COLUMN_MARKED_FOR_READ assert(getTable())
50
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(getTable())
45
#define ASSERT_COLUMN_MARKED_FOR_READ
46
#define ASSERT_COLUMN_MARKED_FOR_WRITE
53
typedef struct st_typelib TYPELIB;
55
49
const uint32_t max_field_size= (uint32_t) 4294967295U;
55
struct st_cache_field;
63
57
int field_conv(Field *to,Field *from);
59
inline uint32_t get_enum_pack_length(int elements)
61
return elements < 256 ? 1 : 2;
66
65
* Class representing a Field in a Table
70
* The value stored in the Field object is stored in the
69
* The value stored in the Field object is stored in the
71
70
* unsigned char pointer member variable called ptr. The
72
* val_xxx() methods retrieve this raw byte value and
71
* val_xxx() methods retrieve this raw byte value and
73
72
* convert the byte into the appropriate output (int, decimal, etc).
75
74
* The store_xxx() methods take various input and convert
80
79
/* Prevent use of these */
82
81
void operator=(Field &);
85
83
unsigned char *ptr; /**< Position to field in record. Stores raw field value */
86
84
unsigned char *null_ptr; /**< Byte where null_bit is */
89
* Pointer to the Table object containing this Field
87
* Pointer to the Table object containing this Field
91
89
* @note You can use table->in_use as replacement for current_session member
92
90
* only inside of val_*() and store() members (e.g. you can't use it in cons)
103
Table *getTable() const
109
void setTable(Table *table_arg)
114
93
Table *orig_table; /**< Pointer to the original Table. @TODO What is "the original table"? */
94
const char **table_name; /**< Pointer to the name of the table. @TODO This is redundant with Table::table_name. */
115
95
const char *field_name; /**< Name of the field */
116
96
LEX_STRING comment; /**< A comment about the field */
122
102
key_map part_of_sortkey;
125
We use three additional unireg types for TIMESTAMP for hysterical
126
raisins and limitations in the MySQL FRM file format.
128
A good TODO is to clean this up as we can support just about
129
anything in the table proto message now.
105
We use three additional unireg types for TIMESTAMP to overcome limitation
106
of current binary format of .frm file. We'd like to be able to support
107
NOW() as default and on update value for such fields but unable to hold
108
this info anywhere except unireg_check field. This issue will be resolved
109
in more clean way with transition to new text based .frm format.
110
See also comment for Field_timestamp::Field_timestamp().
135
116
TIMESTAMP_OLD_FIELD,
141
122
utype unireg_check;
142
123
uint32_t field_length; /**< Length of this field in bytes */
145
125
uint16_t field_index; /**< Index of this Field in Table::fields array */
149
uint16_t position() const
154
void setPosition(uint32_t arg)
159
126
unsigned char null_bit; /**< Bit used to test null bit */
161
128
If true, this field was created in create_tmp_field_from_item from a NULL
168
135
bool is_created_from_null_item;
170
static void *operator new(size_t size);
171
static void *operator new(size_t size, memory::Root *mem_root);
137
static void *operator new(size_t size) {return sql_alloc(size); }
138
static void *operator new(size_t size, MEM_ROOT *mem_root)
139
{ return (void*) alloc_root(mem_root, (uint32_t) size); }
172
140
static void operator delete(void *, size_t)
174
static void operator delete(void *, memory::Root *)
141
{ TRASH(ptr_arg, size); }
177
143
Field(unsigned char *ptr_arg,
178
144
uint32_t length_arg,
181
147
utype unireg_check_arg,
182
148
const char *field_name_arg);
183
149
virtual ~Field() {}
185
bool hasDefault() const
187
return not (flags & NO_DEFAULT_VALUE_FLAG);
190
150
/* Store functions returns 1 on overflow and -1 on fatal error */
191
virtual int store(const char *to,
151
virtual int store(const char *to,
193
153
const CHARSET_INFO * const cs)=0;
194
154
virtual int store(double nr)=0;
195
155
virtual int store(int64_t nr, bool unsigned_val)=0;
196
156
virtual int store_decimal(const my_decimal *d)=0;
197
int store_and_check(enum_check_fields check_level,
200
const CHARSET_INFO * const cs);
157
int store(const char *to,
159
const CHARSET_INFO * const cs,
160
enum_check_fields check_level);
202
162
This is called when storing a date in a string.
205
165
Needs to be changed if/when we want to support different time formats.
207
167
virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
208
virtual double val_real()=0;
209
virtual int64_t val_int()=0;
168
virtual double val_real(void)=0;
169
virtual int64_t val_int(void)=0;
210
170
virtual my_decimal *val_decimal(my_decimal *);
211
String *val_str_internal(String *str)
171
inline String *val_str(String *str)
213
173
return val_str(str, str);
225
185
This trickery is used to decrease a number of malloc calls.
227
187
virtual String *val_str(String*, String *)=0;
189
* Interpret field value as an integer but return the result as a string.
191
* This is used for printing bit_fields as numbers while debugging.
193
String *val_int_as_str(String *val_buffer, bool unsigned_flag);
229
195
str_needs_quotes() returns true if the value returned by val_str() needs
230
196
to be quoted when used in constructing an SQL query.
237
203
Check whether a field type can be partially indexed by a key.
239
205
This is a static method, rather than a virtual function, because we need
240
to check the type of a non-Field in alter_table().
206
to check the type of a non-Field in mysql_alter_table().
242
208
@param type field type
292
258
* table, which is located on disk).
294
260
virtual uint32_t pack_length_in_rec() const;
297
* Return the "real size" of the data in memory.
262
Check to see if field size is compatible with destination.
264
This method is used in row-based replication to verify that the slave's
265
field size is less than or equal to the master's field size. The
266
encoded field metadata (from the master or source) is decoded and compared
267
to the size of this field (the slave or destination).
269
@param field_metadata Encoded size in field metadata
271
@retval 0 if this field's size is < the source field's size
272
@retval 1 if this field's size is >= the source field's size
274
virtual int compatible_field_size(uint32_t field_metadata);
275
virtual uint32_t pack_length_from_metadata(uint32_t field_metadata);
278
This method is used to return the size of the data in a row-based
279
replication row record. The default implementation of returning 0 is
280
designed to allow fields that do not use metadata to return true (1)
281
from compatible_field_size() which uses this function in the comparison.
282
The default value for field metadata for fields that do not have
283
metadata is 0. Thus, 0 == 0 means the fields are compatible in size.
285
Note: While most classes that override this method return pack_length(),
286
the classes Field_varstring, and Field_blob return
287
field_length + 1, field_length, and pack_length_no_ptr() respectfully.
289
virtual uint32_t row_pack_length();
290
virtual int save_field_metadata(unsigned char *first_byte);
293
* Return the "real size" of the data in memory.
298
294
* For varstrings, this does _not_ include the length bytes.
300
296
virtual uint32_t data_length();
322
318
virtual uint32_t key_length() const;
323
319
virtual enum_field_types type() const =0;
324
320
virtual enum_field_types real_type() const;
325
virtual int cmp_max(const unsigned char *a, const unsigned char *b, uint32_t max_len);
321
inline int cmp(const unsigned char *str) { return cmp(ptr,str); }
322
virtual int cmp_max(const unsigned char *a, const unsigned char *b,
326
324
virtual int cmp(const unsigned char *,const unsigned char *)=0;
327
int cmp_internal(const unsigned char *str) { return cmp(ptr,str); }
328
325
virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
329
326
uint32_t max_length=UINT32_MAX);
330
327
virtual int cmp_offset(uint32_t row_offset);
362
359
* use field->val_int() for comparison. Used to optimize clauses like
363
360
* 'a_column BETWEEN date_const AND date_const'.
365
virtual bool can_be_compared_as_int64_t() const
362
virtual bool can_be_compared_as_int64_t() const
369
366
virtual void free() {}
370
virtual Field *new_field(memory::Root *root,
367
virtual Field *new_field(MEM_ROOT *root,
371
368
Table *new_table,
373
virtual Field *new_key_field(memory::Root *root, Table *new_table,
374
unsigned char *new_ptr,
370
virtual Field *new_key_field(MEM_ROOT *root, Table *new_table,
371
unsigned char *new_ptr,
375
372
unsigned char *new_null_ptr,
376
373
uint32_t new_null_bit);
377
374
/** This is used to generate a field in Table from TableShare */
378
Field *clone(memory::Root *mem_root, Table *new_table);
379
void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
375
Field *clone(MEM_ROOT *mem_root, Table *new_table);
376
inline void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
382
379
null_ptr= null_ptr_arg;
383
380
null_bit= null_bit_arg;
385
void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
382
inline void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
386
383
virtual void move_field_offset(ptrdiff_t ptr_diff)
388
385
ptr= ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
422
419
* characters have maximal possible size (mbmaxlen). In the other words,
423
420
* "length" parameter is a number of characters multiplied by
424
421
* field_charset->mbmaxlen.
427
424
* Number of copied bytes (excluding padded zero bytes -- see above).
451
int64_t val_int_internal(const unsigned char *new_ptr)
448
inline int64_t val_int(const unsigned char *new_ptr)
453
450
unsigned char *old_ptr= ptr;
454
451
int64_t return_value;
455
ptr= const_cast<unsigned char*>(new_ptr);
452
ptr= (unsigned char*) new_ptr;
456
453
return_value= val_int();
458
455
return return_value;
461
String *val_str_internal(String *str, const unsigned char *new_ptr)
457
inline String *val_str(String *str, const unsigned char *new_ptr)
463
459
unsigned char *old_ptr= ptr;
464
ptr= const_cast<unsigned char*>(new_ptr);
465
val_str_internal(str);
460
ptr= (unsigned char*) new_ptr;
555
551
virtual unsigned char *pack_key(unsigned char* to,
556
552
const unsigned char *from,
558
554
bool low_byte_first)
560
556
return pack(to, from, max_length, low_byte_first);
558
virtual unsigned char *pack_key_from_key_image(unsigned char* to,
559
const unsigned char *from,
563
return pack(to, from, max_length, low_byte_first);
562
565
virtual const unsigned char *unpack_key(unsigned char* to,
563
566
const unsigned char *from,
564
567
uint32_t max_length,
567
570
return unpack(to, from, max_length, low_byte_first);
572
virtual uint32_t packed_col_length(const unsigned char *to, uint32_t length);
569
573
virtual uint32_t max_packed_col_length(uint32_t max_length)
571
575
return max_length;
574
uint32_t offset(const unsigned char *record)
578
virtual int pack_cmp(const unsigned char *a,
579
const unsigned char *b,
580
uint32_t key_length_arg,
581
bool insert_or_update);
582
virtual int pack_cmp(const unsigned char *b,
583
uint32_t key_length_arg,
584
bool insert_or_update);
586
inline uint32_t offset(unsigned char *record)
576
588
return (uint32_t) (ptr - record);
578
590
void copy_from_tmp(int offset);
579
uint32_t fill_cache_field(CacheField *copy);
591
uint32_t fill_cache_field(struct st_cache_field *copy);
580
592
virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
581
593
virtual bool get_time(DRIZZLE_TIME *ltime);
582
594
virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
627
639
fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
630
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
642
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
633
645
uint32_t str_len,
634
enum enum_drizzle_timestamp_type ts_type,
646
enum enum_drizzle_timestamp_type ts_type,
635
647
int cuted_increment);
637
649
Produce warning or note about integer datetime value saved into field.
647
659
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
650
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
662
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
653
665
enum enum_drizzle_timestamp_type ts_type,
654
666
int cuted_increment);
665
677
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
668
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
680
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
669
681
const uint32_t code,
671
683
enum enum_drizzle_timestamp_type ts_type);
672
bool check_overflow(int op_result)
684
inline bool check_overflow(int op_result)
674
686
return (op_result == E_DEC_OVERFLOW);
704
716
value converted from val
706
int64_t convert_decimal2int64_t(const my_decimal *val,
718
int64_t convert_decimal2int64_t(const my_decimal *val,
707
719
bool unsigned_flag,
709
721
/* The max. number of characters */
710
uint32_t char_length() const
722
inline uint32_t char_length() const
712
724
return field_length / charset()->mbmaxlen;
715
enum column_format_type column_format() const
727
inline enum column_format_type column_format() const
717
729
return (enum column_format_type)
718
730
((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
739
751
bool isWriteSet();
740
752
void setReadSet(bool arg= true);
741
753
void setWriteSet(bool arg= true);
758
Retrieve the field metadata for fields.
760
This default implementation returns 0 and saves 0 in the metadata_ptr
763
@param metadata_ptr First byte of field metadata
765
@returns 0 no bytes written.
767
virtual int do_save_field_metadata(unsigned char *)
744
std::ostream& operator<<(std::ostream& output, const Field &field);
746
} /* namespace drizzled */
748
/** @TODO Why is this in the middle of the file???*/
749
773
#include "drizzled/create_field.h"
755
776
* A class for sending field information to a client.
808
829
void (*do_copy2)(CopyField *); // Used to handle null values
832
Field *make_field(TableShare *share,
835
uint32_t field_length,
836
unsigned char *null_pos,
837
unsigned char null_bit,
839
enum_field_types field_type,
840
const CHARSET_INFO * cs,
841
Field::utype unireg_check,
843
const char *field_name);
811
845
uint32_t pack_length_to_packflag(uint32_t type);
812
846
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
813
847
int set_field_to_null(Field *field);