drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
field.h
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/*
21 Because of the function new_field() all field classes that have static
22 variables must declare the size_of() member function.
23*/
24
25
26
27#pragma once
28
29#include <drizzled/common_fwd.h>
30#include <drizzled/sql_error.h>
31#include <drizzled/type/decimal.h>
32#include <drizzled/key_map.h>
33#include <drizzled/sql_list.h>
34#include <drizzled/charset.h>
35#include <drizzled/item_result.h>
36#include <drizzled/base.h>
37
38#include <string>
39#include <vector>
40
41#include <drizzled/visibility.h>
42
43#include <boost/noncopyable.hpp>
44
45namespace drizzled {
46
47#define DATETIME_DEC 6
48#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
49
50#define ASSERT_COLUMN_MARKED_FOR_READ assert(getTable() && (not getTable()->read_set || isReadSet()))
51#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(getTable() && (not getTable()->write_set || isWriteSet()))
52
53const uint32_t max_field_size= (uint32_t) 4294967295U;
54
55int field_conv(Field *to,Field *from);
56
70class DRIZZLED_API Field : boost::noncopyable
71{
72public:
73 unsigned char *ptr;
74 unsigned char *null_ptr;
75
82private:
84
85public:
86 Table *getTable()
87 {
88 assert(table);
89 return table;
90 }
91
92 Table *getTable() const
93 {
94 assert(table);
95 return table;
96 }
97
98 void setTable(Table *table_arg)
99 {
100 table= table_arg;
101 }
102
104 const char *field_name;
105 str_ref comment;
106
108 key_map key_start;
109 key_map part_of_key;
110 key_map part_of_key_not_clustered;
111 key_map part_of_sortkey;
112
113 /*
114 We use three additional unireg types for TIMESTAMP for hysterical
115 raisins and limitations in the MySQL FRM file format.
116
117 A good TODO is to clean this up as we can support just about
118 anything in the table proto message now.
119 */
120 enum utype
121 {
122 NONE,
123 NEXT_NUMBER,
124 TIMESTAMP_OLD_FIELD,
125 TIMESTAMP_DN_FIELD,
126 TIMESTAMP_UN_FIELD,
127 TIMESTAMP_DNUN_FIELD
128 };
129
130 utype unireg_check;
131 uint32_t field_length;
132 uint32_t flags;
133
134 bool isUnsigned() const
135 {
136 return flags & UNSIGNED_FLAG;
137 }
138
139private:
140 uint16_t field_index;
141
142public:
143
144 uint16_t position() const
145 {
146 return field_index;
147 }
148
149 void setPosition(uint32_t arg)
150 {
151 field_index= arg;
152 }
153
154 unsigned char null_bit;
164
165 static void *operator new(size_t size);
166 static void *operator new(size_t size, memory::Root *mem_root);
167 static void operator delete(void *, size_t)
168 { }
169 static void operator delete(void *, memory::Root *)
170 { }
171
172 Field(unsigned char *ptr_arg,
173 uint32_t length_arg,
174 unsigned char *null_ptr_arg,
175 unsigned char null_bit_arg,
176 utype unireg_check_arg,
177 const char *field_name_arg);
178 virtual ~Field() {}
179
180 bool hasDefault() const
181 {
182 return not (flags & NO_DEFAULT_VALUE_FLAG);
183 }
184
185 /* Store functions returns 1 on overflow and -1 on fatal error */
186 virtual int store(const char *to,
187 uint32_t length,
188 const charset_info_st * const cs)=0;
189 virtual int store(double nr)=0;
190 virtual int store(int64_t nr, bool unsigned_val)=0;
191 virtual int store_decimal(const type::Decimal *d)=0;
192 int store_and_check(enum_check_fields check_level,
193 const char *to,
194 uint32_t length,
195 const charset_info_st * const cs);
202 virtual int store_time(type::Time &ltime, type::timestamp_t t_type);
203 virtual double val_real() const=0;
204 virtual int64_t val_int() const =0;
205 virtual type::Decimal *val_decimal(type::Decimal *) const;
206 String *val_str_internal(String *str) const
207 {
208 return val_str(str, str);
209 }
210
211 /*
212 val_str(buf1, buf2) gets two buffers and should use them as follows:
213 if it needs a temp buffer to convert result to string - use buf1
214 example Field_tiny::val_str()
215 if the value exists as a string already - use buf2
216 example Field_varstring::val_str() (???)
217 consequently, buf2 may be created as 'String buf;' - no memory
218 will be allocated for it. buf1 will be allocated to hold a
219 value if it's too small. Using allocated buffer for buf2 may result in
220 an unnecessary free (and later, may be an alloc).
221 This trickery is used to decrease a number of malloc calls.
222 */
223 virtual String *val_str(String*, String *) const =0;
224
225 /*
226 str_needs_quotes() returns true if the value returned by val_str() needs
227 to be quoted when used in constructing an SQL query.
228 */
229 virtual bool str_needs_quotes() { return false; }
230 virtual Item_result result_type () const=0;
231 virtual Item_result cmp_type () const { return result_type(); }
232 virtual Item_result cast_to_int_type () const { return result_type(); }
233
247 static bool type_can_have_key_part(enum_field_types);
257 static enum_field_types field_type_merge(enum_field_types, enum_field_types);
258
267 static Item_result result_merge_type(enum_field_types);
268
269 virtual bool eq(Field *field);
278 virtual bool eq_def(Field *field);
279
280 virtual bool is_timestamp() const
281 {
282 return false;
283 }
284
290 virtual uint32_t pack_length() const;
291
297 virtual uint32_t pack_length_in_rec() const;
298
303 virtual uint32_t data_length();
309 virtual uint32_t used_length();
310 virtual uint32_t sort_length() const;
311
318 virtual uint32_t max_data_length() const;
319 virtual int reset(void);
320 virtual void reset_fields();
321 virtual void set_default();
322 virtual bool binary() const;
323 virtual bool zero_pack() const;
324 virtual enum ha_base_keytype key_type() const;
325 virtual uint32_t key_length() const;
326 virtual enum_field_types type() const =0;
327 virtual enum_field_types real_type() const;
328 virtual int cmp_max(const unsigned char *a, const unsigned char *b, uint32_t max_len);
329 virtual int cmp(const unsigned char *,const unsigned char *)=0;
330 int cmp_internal(const unsigned char *str) { return cmp(ptr,str); }
331 virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
332 uint32_t max_length=UINT32_MAX);
333 virtual int cmp_offset(uint32_t row_offset);
334 virtual int cmp_binary_offset(uint32_t row_offset);
335 virtual int key_cmp(const unsigned char *a,const unsigned char *b);
336 virtual int key_cmp(const unsigned char *str, uint32_t length);
337 virtual uint32_t decimals() const;
338
339 // For new field
340 virtual uint32_t size_of() const =0;
341
342 bool is_null(ptrdiff_t row_offset= 0) const;
343 bool is_real_null(ptrdiff_t row_offset= 0) const;
344 bool is_null_in_record(const unsigned char *record) const;
345 bool is_null_in_record_with_offset(ptrdiff_t offset) const;
346 void set_null(ptrdiff_t row_offset= 0);
347 void set_notnull(ptrdiff_t row_offset= 0);
348 bool maybe_null(void) const;
349 bool real_maybe_null(void) const;
350
351 virtual void make_field(SendField *);
352 virtual void sort_string(unsigned char *buff,uint32_t length)=0;
353 virtual bool optimize_range(uint32_t idx, uint32_t part);
361 virtual bool can_be_compared_as_int64_t() const
362 {
363 return false;
364 }
365 virtual void free() {}
366 virtual Field *new_field(memory::Root*, Table*, bool keep_type);
367 virtual Field *new_key_field(memory::Root *root, Table *new_table,
368 unsigned char *new_ptr,
369 unsigned char *new_null_ptr,
370 uint32_t new_null_bit);
372 Field* clone(memory::Root*, Table*);
373 void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
374 {
375 ptr= ptr_arg;
376 null_ptr= null_ptr_arg;
377 null_bit= null_bit_arg;
378 }
379 void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
380 virtual void move_field_offset(ptrdiff_t ptr_diff)
381 {
382 ptr= ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
383 if (null_ptr)
384 null_ptr= ADD_TO_PTR(null_ptr,ptr_diff,unsigned char*);
385 }
386 virtual void get_image(unsigned char *buff, uint32_t length, const charset_info_st * const)
387 {
388 memcpy(buff,ptr,length);
389 }
390 virtual void get_image(std::basic_string<unsigned char> &buff, uint32_t length, const charset_info_st * const)
391 {
392 buff.append(ptr,length);
393 }
394 virtual void set_image(const unsigned char *buff,uint32_t length, const charset_info_st * const)
395 {
396 memcpy(ptr,buff,length);
397 }
398
423 virtual uint32_t get_key_image(unsigned char *buff, uint32_t length)
424 {
425 get_image(buff, length, &my_charset_bin);
426 return length;
427 }
428 virtual uint32_t get_key_image(std::basic_string<unsigned char> &buff, uint32_t length)
429 {
430 get_image(buff, length, &my_charset_bin);
431 return length;
432 }
433 virtual void set_key_image(const unsigned char *buff,uint32_t length)
434 {
435 set_image(buff,length, &my_charset_bin);
436 }
437 int64_t val_int_offset(uint32_t row_offset)
438 {
439 ptr+=row_offset;
440 int64_t tmp=val_int();
441 ptr-=row_offset;
442 return tmp;
443 }
444
445 int64_t val_int_internal(const unsigned char *new_ptr)
446 {
447 unsigned char *old_ptr= ptr;
448 ptr= const_cast<unsigned char*>(new_ptr);
449 int64_t return_value= val_int();
450 ptr= old_ptr;
451 return return_value;
452 }
453
454 String *val_str_internal(String *str, const unsigned char *new_ptr)
455 {
456 unsigned char *old_ptr= ptr;
457 ptr= const_cast<unsigned char*>(new_ptr);
458 val_str_internal(str);
459 ptr= old_ptr;
460 return str;
461 }
462
500 virtual unsigned char *pack(unsigned char *to,
501 const unsigned char *from,
502 uint32_t max_length,
503 bool low_byte_first);
504
505 unsigned char *pack(unsigned char *to, const unsigned char *from);
506
537 virtual const unsigned char *unpack(unsigned char* to,
538 const unsigned char *from,
539 uint32_t param_data,
540 bool low_byte_first);
545 const unsigned char *unpack(unsigned char* to,
546 const unsigned char *from);
547
548 virtual unsigned char *pack_key(unsigned char* to,
549 const unsigned char *from,
550 uint32_t max_length,
551 bool low_byte_first)
552 {
553 return pack(to, from, max_length, low_byte_first);
554 }
555 virtual const unsigned char *unpack_key(unsigned char* to,
556 const unsigned char *from,
557 uint32_t max_length,
558 bool low_byte_first)
559 {
560 return unpack(to, from, max_length, low_byte_first);
561 }
562 virtual uint32_t max_packed_col_length(uint32_t max_length)
563 {
564 return max_length;
565 }
566
567 uint32_t offset(const unsigned char *record)
568 {
569 return (uint32_t) (ptr - record);
570 }
571 void copy_from_tmp(int offset);
572 uint32_t fill_cache_field(CacheField *copy);
573 virtual bool get_date(type::Time &ltime,uint32_t fuzzydate) const;
574 virtual bool get_time(type::Time &ltime) const;
575 virtual const charset_info_st *charset(void) const { return &my_charset_bin; }
576 virtual const charset_info_st *sort_charset(void) const { return charset(); }
577 virtual bool has_charset(void) const { return false; }
578 virtual void set_charset(const charset_info_st * const)
579 {}
580 virtual enum Derivation derivation(void) const
581 {
582 return DERIVATION_IMPLICIT;
583 }
584 virtual void set_derivation(enum Derivation)
585 {}
605 bool set_warning(DRIZZLE_ERROR::enum_warning_level,
606 drizzled::error_t code,
607 int cuted_increment);
623 void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
624 drizzled::error_t code,
625 const char *str,
626 uint32_t str_len,
627 type::timestamp_t ts_type,
628 int cuted_increment);
643 void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
644 drizzled::error_t code,
645 int64_t nr,
646 type::timestamp_t ts_type,
647 int cuted_increment);
661 void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
662 const drizzled::error_t code,
663 double nr,
664 type::timestamp_t ts_type);
665 bool check_overflow(int op_result)
666 {
667 return (op_result == E_DEC_OVERFLOW);
668 }
681 int warn_if_overflow(int op_result);
682 void init(Table *table_arg);
683
684 /* maximum possible display length */
685 virtual uint32_t max_display_length()= 0;
686
687 virtual uint32_t is_equal(CreateField *new_field);
699 int64_t convert_decimal2int64_t(const type::Decimal *val,
700 bool unsigned_flag,
701 int *err);
702 /* The max. number of characters */
703 uint32_t char_length() const
704 {
705 return field_length / charset()->mbmaxlen;
706 }
707
708 enum column_format_type column_format() const
709 {
710 return (enum column_format_type)
711 ((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
712 }
713
714 /* Hash value */
715 virtual void hash(uint32_t *nr, uint32_t *nr2) const;
716 friend bool reopen_table(Session *,Table *,bool);
717
718 friend class CopyField;
719 friend class Item_avg_field;
720 friend class Item_std_field;
721 friend class Item_sum_num;
722 friend class Item_sum_sum;
723 friend class Item_sum_str;
724 friend class Item_sum_count;
725 friend class Item_sum_avg;
726 friend class Item_sum_std;
727 friend class Item_sum_min;
728 friend class Item_sum_max;
729 friend class Item_func_group_concat;
730
731 bool isReadSet() const;
732 bool isWriteSet();
733 void setReadSet(bool arg= true);
734 void setWriteSet(bool arg= true);
735
736protected:
737
738 void pack_num(uint64_t arg, unsigned char *destination= NULL);
739 void pack_num(uint32_t arg, unsigned char *destination= NULL);
740 uint64_t unpack_num(uint64_t &destination, const unsigned char *arg= NULL) const;
741 uint32_t unpack_num(uint32_t &destination, const unsigned char *arg= NULL) const;
742};
743
744namespace field {
745
746inline bool isDateTime(const enum_field_types &arg)
747{
748 switch (arg)
749 {
750 case DRIZZLE_TYPE_DATE:
751 case DRIZZLE_TYPE_DATETIME:
752 case DRIZZLE_TYPE_MICROTIME:
753 case DRIZZLE_TYPE_TIME:
754 case DRIZZLE_TYPE_TIMESTAMP:
755 return true;
756
757 case DRIZZLE_TYPE_BLOB:
758 case DRIZZLE_TYPE_BOOLEAN:
759 case DRIZZLE_TYPE_DECIMAL:
760 case DRIZZLE_TYPE_DOUBLE:
761 case DRIZZLE_TYPE_ENUM:
762 case DRIZZLE_TYPE_LONG:
763 case DRIZZLE_TYPE_LONGLONG:
764 case DRIZZLE_TYPE_NULL:
765 case DRIZZLE_TYPE_UUID:
766 case DRIZZLE_TYPE_IPV6:
767 case DRIZZLE_TYPE_VARCHAR:
768 return false;
769 }
770
771 assert(0);
772 abort();
773}
774
775} // namespace field
776
777std::ostream& operator<<(std::ostream& output, const Field &field);
778
788{
789public:
790 const char *db_name;
791 const char *table_name;
792 const char *org_table_name;
793 const char *col_name;
794 const char *org_col_name;
795 uint32_t length;
796 uint32_t charsetnr;
797 uint32_t flags;
798 uint32_t decimals;
799 enum_field_types type;
800};
801
802uint32_t pack_length_to_packflag(uint32_t type);
803uint32_t calc_pack_length(enum_field_types type,uint32_t length);
804int set_field_to_null(Field *field);
805int set_field_to_null_with_conversions(Field *field, bool no_conversions);
806
820bool test_if_important_data(const charset_info_st * const cs,
821 const char *str,
822 const char *strend);
823
824} /* namespace drizzled */
unsigned char * ptr
Definition field.h:73
str_ref comment
Definition field.h:105
bool is_created_from_null_item
Definition field.h:163
unsigned char * null_ptr
Definition field.h:74
virtual uint32_t get_key_image(unsigned char *buff, uint32_t length)
Definition field.h:423
Table * table
Definition field.h:83
key_map key_start
Definition field.h:108
unsigned char null_bit
Definition field.h:154
Field(unsigned char *ptr_arg, uint32_t length_arg, unsigned char *null_ptr_arg, unsigned char null_bit_arg, utype unireg_check_arg, const char *field_name_arg)
This is used as a table name when the table structure is not set up.
Definition field.cc:892
virtual bool can_be_compared_as_int64_t() const
Definition field.h:361
Table * orig_table
Definition field.h:103
uint32_t field_length
Definition field.h:131
const char * field_name
Definition field.h:104
uint16_t field_index
Definition field.h:140
Convert from decimal to binary representation.
Definition common_fwd.h:215
TODO: Rename this file - func.h is stupid.
int field_conv(Field *to, Field *from)
int set_field_to_null_with_conversions(Field *field, bool no_conversions)
int key_cmp(KeyPartInfo *key_part, const unsigned char *key, uint32_t key_length)
Derivation
Definition enum.h:39
bool test_if_important_data(const charset_info_st *const cs, const char *str, const char *strend)
Definition field.cc:668
Visibility Control Macros.
#define DRIZZLED_API
Definition visibility.h:38