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()))
49
#define ASSERT_COLUMN_MARKED_FOR_READ assert(getTable())
50
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(getTable())
53
typedef struct st_typelib TYPELIB;
55
41
const uint32_t max_field_size= (uint32_t) 4294967295U;
47
struct st_cache_field;
63
49
int field_conv(Field *to,Field *from);
51
inline uint32_t get_enum_pack_length(int elements)
53
return elements < 256 ? 1 : 2;
66
57
* Class representing a Field in a Table
70
* The value stored in the Field object is stored in the
61
* The value stored in the Field object is stored in the
71
62
* unsigned char pointer member variable called ptr. The
72
* val_xxx() methods retrieve this raw byte value and
63
* val_xxx() methods retrieve this raw byte value and
73
64
* convert the byte into the appropriate output (int, decimal, etc).
75
66
* The store_xxx() methods take various input and convert
80
71
/* Prevent use of these */
82
73
void operator=(Field &);
85
75
unsigned char *ptr; /**< Position to field in record. Stores raw field value */
86
76
unsigned char *null_ptr; /**< Byte where null_bit is */
89
* Pointer to the Table object containing this Field
79
* Pointer to the Table object containing this Field
91
81
* @note You can use table->in_use as replacement for current_session member
92
82
* 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
85
Table *orig_table; /**< Pointer to the original Table. @TODO What is "the original table"? */
86
const char **table_name; /**< Pointer to the name of the table. @TODO This is redundant with Table::table_name. */
115
87
const char *field_name; /**< Name of the field */
116
88
LEX_STRING comment; /**< A comment about the field */
122
94
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.
97
We use three additional unireg types for TIMESTAMP to overcome limitation
98
of current binary format of .frm file. We'd like to be able to support
99
NOW() as default and on update value for such fields but unable to hold
100
this info anywhere except unireg_check field. This issue will be resolved
101
in more clean way with transition to new text based .frm format.
102
See also comment for Field_timestamp::Field_timestamp().
135
108
TIMESTAMP_OLD_FIELD,
141
114
utype unireg_check;
142
115
uint32_t field_length; /**< Length of this field in bytes */
145
117
uint16_t field_index; /**< Index of this Field in Table::fields array */
149
uint16_t position() const
154
void setPosition(uint32_t arg)
159
118
unsigned char null_bit; /**< Bit used to test null bit */
161
120
If true, this field was created in create_tmp_field_from_item from a NULL
168
127
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);
129
static void *operator new(size_t size) {return sql_alloc(size); }
130
static void *operator new(size_t size, MEM_ROOT *mem_root)
131
{ return (void*) alloc_root(mem_root, (uint32_t) size); }
172
132
static void operator delete(void *, size_t)
174
static void operator delete(void *, memory::Root *)
133
{ TRASH(ptr_arg, size); }
177
135
Field(unsigned char *ptr_arg,
178
136
uint32_t length_arg,
181
139
utype unireg_check_arg,
182
140
const char *field_name_arg);
183
141
virtual ~Field() {}
185
bool hasDefault() const
187
return not (flags & NO_DEFAULT_VALUE_FLAG);
190
142
/* Store functions returns 1 on overflow and -1 on fatal error */
191
virtual int store(const char *to,
143
virtual int store(const char *to,
193
145
const CHARSET_INFO * const cs)=0;
194
146
virtual int store(double nr)=0;
195
147
virtual int store(int64_t nr, bool unsigned_val)=0;
196
148
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);
149
int store(const char *to,
151
const CHARSET_INFO * const cs,
152
enum_check_fields check_level);
202
154
This is called when storing a date in a string.
205
157
Needs to be changed if/when we want to support different time formats.
207
159
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;
160
virtual double val_real(void)=0;
161
virtual int64_t val_int(void)=0;
210
162
virtual my_decimal *val_decimal(my_decimal *);
211
String *val_str_internal(String *str)
163
inline String *val_str(String *str)
213
165
return val_str(str, str);
225
177
This trickery is used to decrease a number of malloc calls.
227
179
virtual String *val_str(String*, String *)=0;
181
* Interpret field value as an integer but return the result as a string.
183
* This is used for printing bit_fields as numbers while debugging.
185
String *val_int_as_str(String *val_buffer, bool unsigned_flag);
229
187
str_needs_quotes() returns true if the value returned by val_str() needs
230
188
to be quoted when used in constructing an SQL query.
237
195
Check whether a field type can be partially indexed by a key.
239
197
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().
198
to check the type of a non-Field in mysql_alter_table().
242
200
@param type field type
292
250
* table, which is located on disk).
294
252
virtual uint32_t pack_length_in_rec() const;
297
* Return the "real size" of the data in memory.
254
Check to see if field size is compatible with destination.
256
This method is used in row-based replication to verify that the slave's
257
field size is less than or equal to the master's field size. The
258
encoded field metadata (from the master or source) is decoded and compared
259
to the size of this field (the slave or destination).
261
@param field_metadata Encoded size in field metadata
263
@retval 0 if this field's size is < the source field's size
264
@retval 1 if this field's size is >= the source field's size
266
virtual int compatible_field_size(uint32_t field_metadata);
267
virtual uint32_t pack_length_from_metadata(uint32_t field_metadata);
270
This method is used to return the size of the data in a row-based
271
replication row record. The default implementation of returning 0 is
272
designed to allow fields that do not use metadata to return true (1)
273
from compatible_field_size() which uses this function in the comparison.
274
The default value for field metadata for fields that do not have
275
metadata is 0. Thus, 0 == 0 means the fields are compatible in size.
277
Note: While most classes that override this method return pack_length(),
278
the classes Field_varstring, and Field_blob return
279
field_length + 1, field_length, and pack_length_no_ptr() respectfully.
281
virtual uint32_t row_pack_length();
282
virtual int save_field_metadata(unsigned char *first_byte);
285
* Return the "real size" of the data in memory.
298
286
* For varstrings, this does _not_ include the length bytes.
300
288
virtual uint32_t data_length();
322
310
virtual uint32_t key_length() const;
323
311
virtual enum_field_types type() const =0;
324
312
virtual enum_field_types real_type() const;
325
virtual int cmp_max(const unsigned char *a, const unsigned char *b, uint32_t max_len);
313
inline int cmp(const unsigned char *str) { return cmp(ptr,str); }
314
virtual int cmp_max(const unsigned char *a, const unsigned char *b,
326
316
virtual int cmp(const unsigned char *,const unsigned char *)=0;
327
int cmp_internal(const unsigned char *str) { return cmp(ptr,str); }
328
317
virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
329
318
uint32_t max_length=UINT32_MAX);
330
319
virtual int cmp_offset(uint32_t row_offset);
344
333
virtual uint32_t size_of() const =0;
346
bool is_null(ptrdiff_t row_offset= 0);
347
bool is_real_null(ptrdiff_t row_offset= 0);
335
bool is_null(my_ptrdiff_t row_offset= 0);
336
bool is_real_null(my_ptrdiff_t row_offset= 0);
348
337
bool is_null_in_record(const unsigned char *record);
349
338
bool is_null_in_record_with_offset(ptrdiff_t offset);
350
339
void set_null(ptrdiff_t row_offset= 0);
362
351
* use field->val_int() for comparison. Used to optimize clauses like
363
352
* 'a_column BETWEEN date_const AND date_const'.
365
virtual bool can_be_compared_as_int64_t() const
354
virtual bool can_be_compared_as_int64_t() const
369
358
virtual void free() {}
370
virtual Field *new_field(memory::Root *root,
359
virtual Field *new_field(MEM_ROOT *root,
371
360
Table *new_table,
373
virtual Field *new_key_field(memory::Root *root, Table *new_table,
374
unsigned char *new_ptr,
362
virtual Field *new_key_field(MEM_ROOT *root, Table *new_table,
363
unsigned char *new_ptr,
375
364
unsigned char *new_null_ptr,
376
365
uint32_t new_null_bit);
377
366
/** 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)
367
Field *clone(MEM_ROOT *mem_root, Table *new_table);
368
inline void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
382
371
null_ptr= null_ptr_arg;
383
372
null_bit= null_bit_arg;
385
void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
374
inline void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
386
375
virtual void move_field_offset(ptrdiff_t ptr_diff)
388
377
ptr= ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
422
411
* characters have maximal possible size (mbmaxlen). In the other words,
423
412
* "length" parameter is a number of characters multiplied by
424
413
* field_charset->mbmaxlen.
427
416
* Number of copied bytes (excluding padded zero bytes -- see above).
451
int64_t val_int_internal(const unsigned char *new_ptr)
440
inline int64_t val_int(const unsigned char *new_ptr)
453
442
unsigned char *old_ptr= ptr;
454
443
int64_t return_value;
455
ptr= const_cast<unsigned char*>(new_ptr);
444
ptr= (unsigned char*) new_ptr;
456
445
return_value= val_int();
458
447
return return_value;
461
String *val_str_internal(String *str, const unsigned char *new_ptr)
449
inline String *val_str(String *str, const unsigned char *new_ptr)
463
451
unsigned char *old_ptr= ptr;
464
ptr= const_cast<unsigned char*>(new_ptr);
465
val_str_internal(str);
452
ptr= (unsigned char*) new_ptr;
555
543
virtual unsigned char *pack_key(unsigned char* to,
556
544
const unsigned char *from,
558
546
bool low_byte_first)
560
548
return pack(to, from, max_length, low_byte_first);
550
virtual unsigned char *pack_key_from_key_image(unsigned char* to,
551
const unsigned char *from,
555
return pack(to, from, max_length, low_byte_first);
562
557
virtual const unsigned char *unpack_key(unsigned char* to,
563
558
const unsigned char *from,
564
559
uint32_t max_length,
567
562
return unpack(to, from, max_length, low_byte_first);
564
virtual uint32_t packed_col_length(const unsigned char *to, uint32_t length);
569
565
virtual uint32_t max_packed_col_length(uint32_t max_length)
571
567
return max_length;
574
uint32_t offset(const unsigned char *record)
570
virtual int pack_cmp(const unsigned char *a,
571
const unsigned char *b,
572
uint32_t key_length_arg,
573
bool insert_or_update);
574
virtual int pack_cmp(const unsigned char *b,
575
uint32_t key_length_arg,
576
bool insert_or_update);
578
inline uint32_t offset(unsigned char *record)
576
580
return (uint32_t) (ptr - record);
578
582
void copy_from_tmp(int offset);
579
uint32_t fill_cache_field(CacheField *copy);
583
uint32_t fill_cache_field(struct st_cache_field *copy);
580
584
virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
581
585
virtual bool get_time(DRIZZLE_TIME *ltime);
582
586
virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
627
631
fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
630
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
634
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
633
637
uint32_t str_len,
634
enum enum_drizzle_timestamp_type ts_type,
638
enum enum_drizzle_timestamp_type ts_type,
635
639
int cuted_increment);
637
641
Produce warning or note about integer datetime value saved into field.
647
651
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
650
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
654
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
653
657
enum enum_drizzle_timestamp_type ts_type,
654
658
int cuted_increment);
665
669
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
668
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
672
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
669
673
const uint32_t code,
671
675
enum enum_drizzle_timestamp_type ts_type);
672
bool check_overflow(int op_result)
676
inline bool check_overflow(int op_result)
674
678
return (op_result == E_DEC_OVERFLOW);
704
708
value converted from val
706
int64_t convert_decimal2int64_t(const my_decimal *val,
710
int64_t convert_decimal2int64_t(const my_decimal *val,
707
711
bool unsigned_flag,
709
713
/* The max. number of characters */
710
uint32_t char_length() const
714
inline uint32_t char_length() const
712
716
return field_length / charset()->mbmaxlen;
715
enum column_format_type column_format() const
719
inline enum column_format_type column_format() const
717
721
return (enum column_format_type)
718
722
((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
738
742
bool isReadSet();
739
743
bool isWriteSet();
740
void setReadSet(bool arg= true);
741
void setWriteSet(bool arg= true);
748
Retrieve the field metadata for fields.
750
This default implementation returns 0 and saves 0 in the metadata_ptr
753
@param metadata_ptr First byte of field metadata
755
@returns 0 no bytes written.
757
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
763
#include "drizzled/create_field.h"
755
766
* A class for sending field information to a client.
808
819
void (*do_copy2)(CopyField *); // Used to handle null values
822
Field *make_field(TableShare *share,
825
uint32_t field_length,
826
unsigned char *null_pos,
827
unsigned char null_bit,
829
enum_field_types field_type,
830
const CHARSET_INFO * cs,
831
Field::utype unireg_check,
833
const char *field_name);
811
835
uint32_t pack_length_to_packflag(uint32_t type);
812
836
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
813
837
int set_field_to_null(Field *field);