Drizzled Public API Documentation

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/structs.h>
35 #include <drizzled/charset.h>
36 #include <drizzled/item_result.h>
37 
38 #include <string>
39 #include <vector>
40 
41 #include <drizzled/visibility.h>
42 
43 namespace drizzled {
44 
45 #define DATETIME_DEC 6
46 #define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
47 
48 #define ASSERT_COLUMN_MARKED_FOR_READ assert(getTable() && (not getTable()->read_set || isReadSet()))
49 #define ASSERT_COLUMN_MARKED_FOR_WRITE assert(getTable() && (not getTable()->write_set || isWriteSet()))
50 
51 const uint32_t max_field_size= (uint32_t) 4294967295U;
52 
53 int field_conv(Field *to,Field *from);
54 
68 class DRIZZLED_API Field : boost::noncopyable
69 {
70 public:
71  unsigned char *ptr;
72  unsigned char *null_ptr;
80 private:
82 
83 public:
84  Table *getTable()
85  {
86  assert(table);
87  return table;
88  }
89 
90  Table *getTable() const
91  {
92  assert(table);
93  return table;
94  }
95 
96  void setTable(Table *table_arg)
97  {
98  table= table_arg;
99  }
100 
102  const char *field_name;
106  key_map key_start;
107  key_map part_of_key;
108  key_map part_of_key_not_clustered;
109  key_map part_of_sortkey;
110 
111  /*
112  We use three additional unireg types for TIMESTAMP for hysterical
113  raisins and limitations in the MySQL FRM file format.
114 
115  A good TODO is to clean this up as we can support just about
116  anything in the table proto message now.
117  */
118  enum utype
119  {
120  NONE,
121  NEXT_NUMBER,
122  TIMESTAMP_OLD_FIELD,
123  TIMESTAMP_DN_FIELD,
124  TIMESTAMP_UN_FIELD,
125  TIMESTAMP_DNUN_FIELD
126  };
127 
128  utype unireg_check;
129  uint32_t field_length;
130  uint32_t flags;
131 
132  bool isUnsigned() const
133  {
134  return flags & UNSIGNED_FLAG;
135  }
136 
137 private:
138  uint16_t field_index;
140 public:
141 
142  uint16_t position() const
143  {
144  return field_index;
145  }
146 
147  void setPosition(uint32_t arg)
148  {
149  field_index= arg;
150  }
151 
152  unsigned char null_bit;
162 
163  static void *operator new(size_t size);
164  static void *operator new(size_t size, memory::Root *mem_root);
165  static void operator delete(void *, size_t)
166  { }
167  static void operator delete(void *, memory::Root *)
168  { }
169 
170  Field(unsigned char *ptr_arg,
171  uint32_t length_arg,
172  unsigned char *null_ptr_arg,
173  unsigned char null_bit_arg,
174  utype unireg_check_arg,
175  const char *field_name_arg);
176  virtual ~Field() {}
177 
178  bool hasDefault() const
179  {
180  return not (flags & NO_DEFAULT_VALUE_FLAG);
181  }
182 
183  /* Store functions returns 1 on overflow and -1 on fatal error */
184  virtual int store(const char *to,
185  uint32_t length,
186  const charset_info_st * const cs)=0;
187  virtual int store(double nr)=0;
188  virtual int store(int64_t nr, bool unsigned_val)=0;
189  virtual int store_decimal(const type::Decimal *d)=0;
190  int store_and_check(enum_check_fields check_level,
191  const char *to,
192  uint32_t length,
193  const charset_info_st * const cs);
200  virtual int store_time(type::Time &ltime, type::timestamp_t t_type);
201  virtual double val_real() const=0;
202  virtual int64_t val_int() const =0;
203  virtual type::Decimal *val_decimal(type::Decimal *) const;
204  String *val_str_internal(String *str) const
205  {
206  return val_str(str, str);
207  }
208 
209  /*
210  val_str(buf1, buf2) gets two buffers and should use them as follows:
211  if it needs a temp buffer to convert result to string - use buf1
212  example Field_tiny::val_str()
213  if the value exists as a string already - use buf2
214  example Field_varstring::val_str() (???)
215  consequently, buf2 may be created as 'String buf;' - no memory
216  will be allocated for it. buf1 will be allocated to hold a
217  value if it's too small. Using allocated buffer for buf2 may result in
218  an unnecessary free (and later, may be an alloc).
219  This trickery is used to decrease a number of malloc calls.
220  */
221  virtual String *val_str(String*, String *) const =0;
222 
223  /*
224  str_needs_quotes() returns true if the value returned by val_str() needs
225  to be quoted when used in constructing an SQL query.
226  */
227  virtual bool str_needs_quotes() { return false; }
228  virtual Item_result result_type () const=0;
229  virtual Item_result cmp_type () const { return result_type(); }
230  virtual Item_result cast_to_int_type () const { return result_type(); }
231 
245  static bool type_can_have_key_part(enum_field_types);
255  static enum_field_types field_type_merge(enum_field_types, enum_field_types);
256 
265  static Item_result result_merge_type(enum_field_types);
266 
267  virtual bool eq(Field *field);
276  virtual bool eq_def(Field *field);
277 
278  virtual bool is_timestamp() const
279  {
280  return false;
281  }
282 
288  virtual uint32_t pack_length() const;
289 
295  virtual uint32_t pack_length_in_rec() const;
296 
301  virtual uint32_t data_length();
307  virtual uint32_t used_length();
308  virtual uint32_t sort_length() const;
309 
316  virtual uint32_t max_data_length() const;
317  virtual int reset(void);
318  virtual void reset_fields();
319  virtual void set_default();
320  virtual bool binary() const;
321  virtual bool zero_pack() const;
322  virtual enum ha_base_keytype key_type() const;
323  virtual uint32_t key_length() const;
324  virtual enum_field_types type() const =0;
325  virtual enum_field_types real_type() const;
326  virtual int cmp_max(const unsigned char *a, const unsigned char *b, uint32_t max_len);
327  virtual int cmp(const unsigned char *,const unsigned char *)=0;
328  int cmp_internal(const unsigned char *str) { return cmp(ptr,str); }
329  virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
330  uint32_t max_length=UINT32_MAX);
331  virtual int cmp_offset(uint32_t row_offset);
332  virtual int cmp_binary_offset(uint32_t row_offset);
333  virtual int key_cmp(const unsigned char *a,const unsigned char *b);
334  virtual int key_cmp(const unsigned char *str, uint32_t length);
335  virtual uint32_t decimals() const;
336 
337  // For new field
338  virtual uint32_t size_of() const =0;
339 
340  bool is_null(ptrdiff_t row_offset= 0) const;
341  bool is_real_null(ptrdiff_t row_offset= 0) const;
342  bool is_null_in_record(const unsigned char *record) const;
343  bool is_null_in_record_with_offset(ptrdiff_t offset) const;
344  void set_null(ptrdiff_t row_offset= 0);
345  void set_notnull(ptrdiff_t row_offset= 0);
346  bool maybe_null(void) const;
347  bool real_maybe_null(void) const;
348 
349  virtual void make_field(SendField *);
350  virtual void sort_string(unsigned char *buff,uint32_t length)=0;
351  virtual bool optimize_range(uint32_t idx, uint32_t part);
359  virtual bool can_be_compared_as_int64_t() const
360  {
361  return false;
362  }
363  virtual void free() {}
364  virtual Field *new_field(memory::Root*, Table*, bool keep_type);
365  virtual Field *new_key_field(memory::Root *root, Table *new_table,
366  unsigned char *new_ptr,
367  unsigned char *new_null_ptr,
368  uint32_t new_null_bit);
370  Field* clone(memory::Root*, Table*);
371  void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
372  {
373  ptr= ptr_arg;
374  null_ptr= null_ptr_arg;
375  null_bit= null_bit_arg;
376  }
377  void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
378  virtual void move_field_offset(ptrdiff_t ptr_diff)
379  {
380  ptr= ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
381  if (null_ptr)
382  null_ptr= ADD_TO_PTR(null_ptr,ptr_diff,unsigned char*);
383  }
384  virtual void get_image(unsigned char *buff, uint32_t length, const charset_info_st * const)
385  {
386  memcpy(buff,ptr,length);
387  }
388  virtual void get_image(std::basic_string<unsigned char> &buff, uint32_t length, const charset_info_st * const)
389  {
390  buff.append(ptr,length);
391  }
392  virtual void set_image(const unsigned char *buff,uint32_t length, const charset_info_st * const)
393  {
394  memcpy(ptr,buff,length);
395  }
396 
421  virtual uint32_t get_key_image(unsigned char *buff, uint32_t length)
422  {
423  get_image(buff, length, &my_charset_bin);
424  return length;
425  }
426  virtual uint32_t get_key_image(std::basic_string<unsigned char> &buff, uint32_t length)
427  {
428  get_image(buff, length, &my_charset_bin);
429  return length;
430  }
431  virtual void set_key_image(const unsigned char *buff,uint32_t length)
432  {
433  set_image(buff,length, &my_charset_bin);
434  }
435  int64_t val_int_offset(uint32_t row_offset)
436  {
437  ptr+=row_offset;
438  int64_t tmp=val_int();
439  ptr-=row_offset;
440  return tmp;
441  }
442 
443  int64_t val_int_internal(const unsigned char *new_ptr)
444  {
445  unsigned char *old_ptr= ptr;
446  ptr= const_cast<unsigned char*>(new_ptr);
447  int64_t return_value= val_int();
448  ptr= old_ptr;
449  return return_value;
450  }
451 
452  String *val_str_internal(String *str, const unsigned char *new_ptr)
453  {
454  unsigned char *old_ptr= ptr;
455  ptr= const_cast<unsigned char*>(new_ptr);
456  val_str_internal(str);
457  ptr= old_ptr;
458  return str;
459  }
460 
498  virtual unsigned char *pack(unsigned char *to,
499  const unsigned char *from,
500  uint32_t max_length,
501  bool low_byte_first);
502 
503  unsigned char *pack(unsigned char *to, const unsigned char *from);
504 
535  virtual const unsigned char *unpack(unsigned char* to,
536  const unsigned char *from,
537  uint32_t param_data,
538  bool low_byte_first);
543  const unsigned char *unpack(unsigned char* to,
544  const unsigned char *from);
545 
546  virtual unsigned char *pack_key(unsigned char* to,
547  const unsigned char *from,
548  uint32_t max_length,
549  bool low_byte_first)
550  {
551  return pack(to, from, max_length, low_byte_first);
552  }
553  virtual const unsigned char *unpack_key(unsigned char* to,
554  const unsigned char *from,
555  uint32_t max_length,
556  bool low_byte_first)
557  {
558  return unpack(to, from, max_length, low_byte_first);
559  }
560  virtual uint32_t max_packed_col_length(uint32_t max_length)
561  {
562  return max_length;
563  }
564 
565  uint32_t offset(const unsigned char *record)
566  {
567  return (uint32_t) (ptr - record);
568  }
569  void copy_from_tmp(int offset);
570  uint32_t fill_cache_field(CacheField *copy);
571  virtual bool get_date(type::Time &ltime,uint32_t fuzzydate) const;
572  virtual bool get_time(type::Time &ltime) const;
573  virtual const charset_info_st *charset(void) const { return &my_charset_bin; }
574  virtual const charset_info_st *sort_charset(void) const { return charset(); }
575  virtual bool has_charset(void) const { return false; }
576  virtual void set_charset(const charset_info_st * const)
577  {}
578  virtual enum Derivation derivation(void) const
579  {
580  return DERIVATION_IMPLICIT;
581  }
582  virtual void set_derivation(enum Derivation)
583  {}
603  bool set_warning(DRIZZLE_ERROR::enum_warning_level,
604  drizzled::error_t code,
605  int cuted_increment);
621  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
622  drizzled::error_t code,
623  const char *str,
624  uint32_t str_len,
625  type::timestamp_t ts_type,
626  int cuted_increment);
641  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
642  drizzled::error_t code,
643  int64_t nr,
644  type::timestamp_t ts_type,
645  int cuted_increment);
659  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
660  const drizzled::error_t code,
661  double nr,
662  type::timestamp_t ts_type);
663  bool check_overflow(int op_result)
664  {
665  return (op_result == E_DEC_OVERFLOW);
666  }
679  int warn_if_overflow(int op_result);
680  void init(Table *table_arg);
681 
682  /* maximum possible display length */
683  virtual uint32_t max_display_length()= 0;
684 
685  virtual uint32_t is_equal(CreateField *new_field);
697  int64_t convert_decimal2int64_t(const type::Decimal *val,
698  bool unsigned_flag,
699  int *err);
700  /* The max. number of characters */
701  uint32_t char_length() const
702  {
703  return field_length / charset()->mbmaxlen;
704  }
705 
706  enum column_format_type column_format() const
707  {
708  return (enum column_format_type)
709  ((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
710  }
711 
712  /* Hash value */
713  virtual void hash(uint32_t *nr, uint32_t *nr2) const;
714  friend bool reopen_table(Session *,Table *,bool);
715 
716  friend class CopyField;
717  friend class Item_avg_field;
718  friend class Item_std_field;
719  friend class Item_sum_num;
720  friend class Item_sum_sum;
721  friend class Item_sum_str;
722  friend class Item_sum_count;
723  friend class Item_sum_avg;
724  friend class Item_sum_std;
725  friend class Item_sum_min;
726  friend class Item_sum_max;
727  friend class Item_func_group_concat;
728 
729  bool isReadSet() const;
730  bool isWriteSet();
731  void setReadSet(bool arg= true);
732  void setWriteSet(bool arg= true);
733 
734 protected:
735 
736  void pack_num(uint64_t arg, unsigned char *destination= NULL);
737  void pack_num(uint32_t arg, unsigned char *destination= NULL);
738  uint64_t unpack_num(uint64_t &destination, const unsigned char *arg= NULL) const;
739  uint32_t unpack_num(uint32_t &destination, const unsigned char *arg= NULL) const;
740 };
741 
742 namespace field {
743 
744 inline bool isDateTime(const enum_field_types &arg)
745 {
746  switch (arg)
747  {
748  case DRIZZLE_TYPE_DATE:
749  case DRIZZLE_TYPE_DATETIME:
750  case DRIZZLE_TYPE_MICROTIME:
751  case DRIZZLE_TYPE_TIME:
752  case DRIZZLE_TYPE_TIMESTAMP:
753  return true;
754 
755  case DRIZZLE_TYPE_BLOB:
756  case DRIZZLE_TYPE_BOOLEAN:
757  case DRIZZLE_TYPE_DECIMAL:
758  case DRIZZLE_TYPE_DOUBLE:
759  case DRIZZLE_TYPE_ENUM:
760  case DRIZZLE_TYPE_LONG:
761  case DRIZZLE_TYPE_LONGLONG:
762  case DRIZZLE_TYPE_NULL:
763  case DRIZZLE_TYPE_UUID:
764  case DRIZZLE_TYPE_IPV6:
765  case DRIZZLE_TYPE_VARCHAR:
766  return false;
767  }
768 
769  assert(0);
770  abort();
771 }
772 
773 } // namespace field
774 
775 std::ostream& operator<<(std::ostream& output, const Field &field);
776 
786 {
787 public:
788  const char *db_name;
789  const char *table_name;
790  const char *org_table_name;
791  const char *col_name;
792  const char *org_col_name;
793  uint32_t length;
794  uint32_t charsetnr;
795  uint32_t flags;
796  uint32_t decimals;
797  enum_field_types type;
798 };
799 
800 uint32_t pack_length_to_packflag(uint32_t type);
801 uint32_t calc_pack_length(enum_field_types type,uint32_t length);
802 int set_field_to_null(Field *field);
803 int set_field_to_null_with_conversions(Field *field, bool no_conversions);
804 
818 bool test_if_important_data(const charset_info_st * const cs,
819  const char *str,
820  const char *strend);
821 
822 } /* namespace drizzled */
823 
key_map key_start
Definition: field.h:106
uint16_t field_index
Definition: field.h:138
uint32_t field_length
Definition: field.h:129
virtual uint32_t get_key_image(unsigned char *buff, uint32_t length)
Definition: field.h:421
Table * orig_table
Definition: field.h:101
str_ref comment
Definition: field.h:103
#define DRIZZLED_API
Definition: visibility.h:62
Table * table
Definition: field.h:81
int key_cmp(KeyPartInfo *key_part, const unsigned char *key, uint32_t key_length)
Definition: key.cc:438
Visibility Control Macros.
virtual bool can_be_compared_as_int64_t() const
Definition: field.h:359
int field_conv(Field *to, Field *from)
Definition: field_conv.cc:795
int set_field_to_null_with_conversions(Field *field, bool no_conversions)
Definition: field_conv.cc:179
unsigned char null_bit
Definition: field.h:152
bool test_if_important_data(const charset_info_st *const cs, const char *str, const char *strend)
Definition: field.cc:668
bool is_created_from_null_item
Definition: field.h:161
Derivation
Definition: enum.h:38
unsigned char * null_ptr
Definition: field.h:72
const char * field_name
Definition: field.h:102
unsigned char * ptr
Definition: field.h:71