22
22
variables must declare the size_of() member function.
27
25
#ifndef DRIZZLED_FIELD_H
28
26
#define DRIZZLED_FIELD_H
30
28
#include "drizzled/sql_error.h"
31
#include "drizzled/type/decimal.h"
29
#include "drizzled/my_decimal.h"
32
30
#include "drizzled/key_map.h"
31
#include "drizzled/sql_bitmap.h"
33
32
#include "drizzled/sql_list.h"
34
33
#include "drizzled/structs.h"
35
#include "drizzled/charset_info.h"
36
#include "drizzled/item_result.h"
37
#include "drizzled/charset_info.h"
42
#include "drizzled/visibility.h"
47
38
#define DATETIME_DEC 6
48
39
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
51
#define ASSERT_COLUMN_MARKED_FOR_READ assert(!getTable() || (getTable()->read_set == NULL || isReadSet()))
52
#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()))
54
#define ASSERT_COLUMN_MARKED_FOR_READ assert(getTable())
55
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(getTable())
45
#define ASSERT_COLUMN_MARKED_FOR_READ
46
#define ASSERT_COLUMN_MARKED_FOR_WRITE
58
typedef struct st_typelib TYPELIB;
60
49
const uint32_t max_field_size= (uint32_t) 4294967295U;
55
struct st_cache_field;
68
57
int field_conv(Field *to,Field *from);
59
inline uint32_t get_enum_pack_length(int elements)
61
return elements < 256 ? 1 : 2;
71
65
* Class representing a Field in a Table
75
* The value stored in the Field object is stored in the
69
* The value stored in the Field object is stored in the
76
70
* unsigned char pointer member variable called ptr. The
77
* val_xxx() methods retrieve this raw byte value and
71
* val_xxx() methods retrieve this raw byte value and
78
72
* convert the byte into the appropriate output (int, decimal, etc).
80
74
* The store_xxx() methods take various input and convert
81
75
* the input into the raw bytes stored in the ptr member variable.
83
class DRIZZLED_API Field
85
79
/* Prevent use of these */
87
81
void operator=(Field &);
90
83
unsigned char *ptr; /**< Position to field in record. Stores raw field value */
91
84
unsigned char *null_ptr; /**< Byte where null_bit is */
94
* Pointer to the Table object containing this Field
87
* Pointer to the Table object containing this Field
96
89
* @note You can use table->in_use as replacement for current_session member
97
90
* only inside of val_*() and store() members (e.g. you can't use it in cons)
109
Table *getTable() const
115
void setTable(Table *table_arg)
120
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. */
121
95
const char *field_name; /**< Name of the field */
122
96
LEX_STRING comment; /**< A comment about the field */
128
102
key_map part_of_sortkey;
131
We use three additional unireg types for TIMESTAMP for hysterical
132
raisins and limitations in the MySQL FRM file format.
134
A good TODO is to clean this up as we can support just about
135
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().
141
116
TIMESTAMP_OLD_FIELD,
147
122
utype unireg_check;
148
123
uint32_t field_length; /**< Length of this field in bytes */
151
bool isUnsigned() const
153
return flags & UNSIGNED_FLAG;
157
125
uint16_t field_index; /**< Index of this Field in Table::fields array */
161
uint16_t position() const
166
void setPosition(uint32_t arg)
171
126
unsigned char null_bit; /**< Bit used to test null bit */
173
128
If true, this field was created in create_tmp_field_from_item from a NULL
180
135
bool is_created_from_null_item;
182
static void *operator new(size_t size);
183
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); }
184
140
static void operator delete(void *, size_t)
186
static void operator delete(void *, memory::Root *)
141
{ TRASH(ptr_arg, size); }
189
143
Field(unsigned char *ptr_arg,
190
144
uint32_t length_arg,
193
147
utype unireg_check_arg,
194
148
const char *field_name_arg);
195
149
virtual ~Field() {}
197
bool hasDefault() const
199
return not (flags & NO_DEFAULT_VALUE_FLAG);
202
150
/* Store functions returns 1 on overflow and -1 on fatal error */
203
virtual int store(const char *to,
151
virtual int store(const char *to,
205
153
const CHARSET_INFO * const cs)=0;
206
154
virtual int store(double nr)=0;
207
155
virtual int store(int64_t nr, bool unsigned_val)=0;
208
virtual int store_decimal(const type::Decimal *d)=0;
209
int store_and_check(enum_check_fields check_level,
212
const CHARSET_INFO * const cs);
156
virtual int store_decimal(const my_decimal *d)=0;
157
int store(const char *to,
159
const CHARSET_INFO * const cs,
160
enum_check_fields check_level);
214
162
This is called when storing a date in a string.
217
165
Needs to be changed if/when we want to support different time formats.
219
virtual int store_time(type::Time <ime, type::timestamp_t t_type);
220
virtual double val_real()=0;
221
virtual int64_t val_int()=0;
222
virtual type::Decimal *val_decimal(type::Decimal *);
223
String *val_str_internal(String *str)
167
virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
168
virtual double val_real(void)=0;
169
virtual int64_t val_int(void)=0;
170
virtual my_decimal *val_decimal(my_decimal *);
171
inline String *val_str(String *str)
225
173
return val_str(str, str);
229
176
val_str(buf1, buf2) gets two buffers and should use them as follows:
230
177
if it needs a temp buffer to convert result to string - use buf1
238
185
This trickery is used to decrease a number of malloc calls.
240
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);
243
195
str_needs_quotes() returns true if the value returned by val_str() needs
244
196
to be quoted when used in constructing an SQL query.
247
199
virtual Item_result result_type () const=0;
248
200
virtual Item_result cmp_type () const { return result_type(); }
249
201
virtual Item_result cast_to_int_type () const { return result_type(); }
252
203
Check whether a field type can be partially indexed by a key.
254
205
This is a static method, rather than a virtual function, because we need
255
to check the type of a non-Field in alter_table().
206
to check the type of a non-Field in drizzled::alter_table().
257
208
@param type field type
312
258
* table, which is located on disk).
314
260
virtual uint32_t pack_length_in_rec() const;
317
* 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();
292
* Return the "real size" of the data in memory.
318
293
* For varstrings, this does _not_ include the length bytes.
320
295
virtual uint32_t data_length();
342
317
virtual uint32_t key_length() const;
343
318
virtual enum_field_types type() const =0;
344
319
virtual enum_field_types real_type() const;
345
virtual int cmp_max(const unsigned char *a, const unsigned char *b, uint32_t max_len);
320
inline int cmp(const unsigned char *str) { return cmp(ptr,str); }
321
virtual int cmp_max(const unsigned char *a, const unsigned char *b,
346
323
virtual int cmp(const unsigned char *,const unsigned char *)=0;
347
int cmp_internal(const unsigned char *str) { return cmp(ptr,str); }
348
324
virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
349
325
uint32_t max_length=UINT32_MAX);
350
326
virtual int cmp_offset(uint32_t row_offset);
382
358
* use field->val_int() for comparison. Used to optimize clauses like
383
359
* 'a_column BETWEEN date_const AND date_const'.
385
virtual bool can_be_compared_as_int64_t() const
361
virtual bool can_be_compared_as_int64_t() const
389
365
virtual void free() {}
390
virtual Field *new_field(memory::Root *root,
366
virtual Field *new_field(MEM_ROOT *root,
391
367
Table *new_table,
393
virtual Field *new_key_field(memory::Root *root, Table *new_table,
394
unsigned char *new_ptr,
369
virtual Field *new_key_field(MEM_ROOT *root, Table *new_table,
370
unsigned char *new_ptr,
395
371
unsigned char *new_null_ptr,
396
372
uint32_t new_null_bit);
397
373
/** This is used to generate a field in Table from TableShare */
398
Field *clone(memory::Root *mem_root, Table *new_table);
399
void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
374
Field *clone(MEM_ROOT *mem_root, Table *new_table);
375
inline void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
402
378
null_ptr= null_ptr_arg;
403
379
null_bit= null_bit_arg;
405
void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
381
inline void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
406
382
virtual void move_field_offset(ptrdiff_t ptr_diff)
408
384
ptr= ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
471
int64_t val_int_internal(const unsigned char *new_ptr)
447
inline int64_t val_int(const unsigned char *new_ptr)
473
449
unsigned char *old_ptr= ptr;
474
450
int64_t return_value;
475
ptr= const_cast<unsigned char*>(new_ptr);
451
ptr= (unsigned char*) new_ptr;
476
452
return_value= val_int();
478
454
return return_value;
481
String *val_str_internal(String *str, const unsigned char *new_ptr)
456
inline String *val_str(String *str, const unsigned char *new_ptr)
483
458
unsigned char *old_ptr= ptr;
484
ptr= const_cast<unsigned char*>(new_ptr);
485
val_str_internal(str);
459
ptr= (unsigned char*) new_ptr;
591
566
return max_length;
594
uint32_t offset(const unsigned char *record)
569
inline uint32_t offset(unsigned char *record)
596
571
return (uint32_t) (ptr - record);
598
573
void copy_from_tmp(int offset);
599
uint32_t fill_cache_field(CacheField *copy);
600
virtual bool get_date(type::Time <ime,uint32_t fuzzydate);
601
virtual bool get_time(type::Time <ime);
574
uint32_t fill_cache_field(struct st_cache_field *copy);
575
virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
576
virtual bool get_time(DRIZZLE_TIME *ltime);
602
577
virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
603
578
virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
604
579
virtual bool has_charset(void) const { return false; }
647
622
fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
650
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
651
drizzled::error_t code,
625
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
653
628
uint32_t str_len,
654
type::timestamp_t ts_type,
629
enum enum_drizzle_timestamp_type ts_type,
655
630
int cuted_increment);
657
632
Produce warning or note about integer datetime value saved into field.
667
642
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
670
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
671
drizzled::error_t code,
673
type::timestamp_t ts_type,
645
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
648
enum enum_drizzle_timestamp_type ts_type,
674
649
int cuted_increment);
676
651
Produce warning or note about double datetime data saved into field.
685
660
fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
688
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
689
const drizzled::error_t code,
691
type::timestamp_t ts_type);
692
bool check_overflow(int op_result)
663
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
666
enum enum_drizzle_timestamp_type ts_type);
667
inline bool check_overflow(int op_result)
694
669
return (op_result == E_DEC_OVERFLOW);
724
699
value converted from val
726
int64_t convert_decimal2int64_t(const type::Decimal *val,
701
int64_t convert_decimal2int64_t(const my_decimal *val,
727
702
bool unsigned_flag,
729
704
/* The max. number of characters */
730
uint32_t char_length() const
705
inline uint32_t char_length() const
732
707
return field_length / charset()->mbmaxlen;
735
enum column_format_type column_format() const
710
inline enum column_format_type column_format() const
737
712
return (enum column_format_type)
738
713
((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
759
734
bool isWriteSet();
760
735
void setReadSet(bool arg= true);
761
736
void setWriteSet(bool arg= true);
765
void pack_num(uint64_t arg, unsigned char *destination= NULL);
766
void pack_num(uint32_t arg, unsigned char *destination= NULL);
767
uint64_t unpack_num(uint64_t &destination, const unsigned char *arg= NULL) const;
768
uint32_t unpack_num(uint32_t &destination, const unsigned char *arg= NULL) const;
771
std::ostream& operator<<(std::ostream& output, const Field &field);
773
} /* namespace drizzled */
775
/** @TODO Why is this in the middle of the file???*/
776
739
#include "drizzled/create_field.h"
782
742
* A class for sending field information to a client.
766
* A class for quick copying data to fields
768
class CopyField :public Sql_alloc
771
Convenience definition of a copy function returned by
774
typedef void Copy_func(CopyField*);
775
Copy_func *get_copy_func(Field *to, Field *from);
777
unsigned char *from_ptr;
778
unsigned char *to_ptr;
779
unsigned char *from_null_ptr;
780
unsigned char *to_null_ptr;
784
uint32_t from_length;
788
String tmp; // For items
792
void set(Field *to,Field *from,bool save); // Field to field
793
void set(unsigned char *to,Field *from); // Field to string
794
void (*do_copy)(CopyField *);
795
void (*do_copy2)(CopyField *); // Used to handle null values
798
Field *make_field(TableShare *share,
801
uint32_t field_length,
803
unsigned char *null_pos,
804
unsigned char null_bit,
806
enum_field_types field_type,
807
const CHARSET_INFO * cs,
808
Field::utype unireg_check,
810
const char *field_name);
805
812
uint32_t pack_length_to_packflag(uint32_t type);
806
813
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
807
814
int set_field_to_null(Field *field);