~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

Modernize our call to the parser, no more casting required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
22
22
  variables must declare the size_of() member function.
23
23
*/
24
24
 
 
25
 
 
26
 
25
27
#ifndef DRIZZLED_FIELD_H
26
28
#define DRIZZLED_FIELD_H
27
29
 
28
30
#include "drizzled/sql_error.h"
29
 
#include "drizzled/decimal.h"
 
31
#include "drizzled/type/decimal.h"
30
32
#include "drizzled/key_map.h"
31
33
#include "drizzled/sql_list.h"
32
34
#include "drizzled/structs.h"
33
35
#include "drizzled/charset_info.h"
34
36
#include "drizzled/item_result.h"
 
37
#include "drizzled/charset_info.h"
35
38
 
36
39
#include <string>
37
40
#include <vector>
38
41
 
 
42
#include "drizzled/visibility.h"
 
43
 
39
44
namespace drizzled
40
45
{
41
46
 
75
80
 * The store_xxx() methods take various input and convert
76
81
 * the input into the raw bytes stored in the ptr member variable.
77
82
 */
78
 
class Field
 
83
class DRIZZLED_API Field
79
84
{
80
85
  /* Prevent use of these */
81
86
  Field(const Field&);
93
98
   */
94
99
private:
95
100
  Table *table;
 
101
 
96
102
public:
97
103
  Table *getTable()
98
104
  {
141
147
  utype unireg_check;
142
148
  uint32_t field_length; /**< Length of this field in bytes */
143
149
  uint32_t flags;
 
150
 
 
151
  bool isUnsigned() const
 
152
  {
 
153
    return flags & UNSIGNED_FLAG;
 
154
  }
 
155
 
144
156
private:
145
157
  uint16_t field_index; /**< Index of this Field in Table::fields array */
146
158
 
193
205
                    const CHARSET_INFO * const cs)=0;
194
206
  virtual int store(double nr)=0;
195
207
  virtual int store(int64_t nr, bool unsigned_val)=0;
196
 
  virtual int store_decimal(const my_decimal *d)=0;
 
208
  virtual int store_decimal(const type::Decimal *d)=0;
197
209
  int store_and_check(enum_check_fields check_level,
198
210
                      const char *to,
199
211
                      uint32_t length,
204
216
    @note
205
217
      Needs to be changed if/when we want to support different time formats.
206
218
  */
207
 
  virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
 
219
  virtual int store_time(type::Time &ltime, type::timestamp_t t_type);
208
220
  virtual double val_real()=0;
209
221
  virtual int64_t val_int()=0;
210
 
  virtual my_decimal *val_decimal(my_decimal *);
 
222
  virtual type::Decimal *val_decimal(type::Decimal *);
211
223
  String *val_str_internal(String *str)
212
224
  {
213
225
    return val_str(str, str);
214
226
  }
 
227
 
215
228
  /*
216
229
     val_str(buf1, buf2) gets two buffers and should use them as follows:
217
230
     if it needs a temp buffer to convert result to string - use buf1
225
238
     This trickery is used to decrease a number of malloc calls.
226
239
  */
227
240
  virtual String *val_str(String*, String *)=0;
 
241
 
228
242
  /*
229
243
   str_needs_quotes() returns true if the value returned by val_str() needs
230
244
   to be quoted when used in constructing an SQL query.
233
247
  virtual Item_result result_type () const=0;
234
248
  virtual Item_result cmp_type () const { return result_type(); }
235
249
  virtual Item_result cast_to_int_type () const { return result_type(); }
 
250
 
236
251
  /**
237
252
     Check whether a field type can be partially indexed by a key.
238
253
 
279
294
   */
280
295
  virtual bool eq_def(Field *field);
281
296
 
 
297
  virtual bool is_timestamp() const
 
298
  {
 
299
    return false;
 
300
  }
 
301
 
282
302
  /**
283
303
   * Returns size (in bytes) used to store field data in memory
284
304
   * (i.e. it returns the maximum size of the field in a row of the table,
451
471
  int64_t val_int_internal(const unsigned char *new_ptr)
452
472
  {
453
473
    unsigned char *old_ptr= ptr;
454
 
    int64_t return_value;
455
474
    ptr= const_cast<unsigned char*>(new_ptr);
456
 
    return_value= val_int();
 
475
    int64_t return_value= val_int();
457
476
    ptr= old_ptr;
458
477
    return return_value;
459
478
  }
577
596
  }
578
597
  void copy_from_tmp(int offset);
579
598
  uint32_t fill_cache_field(CacheField *copy);
580
 
  virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
581
 
  virtual bool get_time(DRIZZLE_TIME *ltime);
 
599
  virtual bool get_date(type::Time &ltime,uint32_t fuzzydate);
 
600
  virtual bool get_time(type::Time &ltime);
582
601
  virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
583
602
  virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
584
603
  virtual bool has_charset(void) const { return false; }
610
629
      0 otherwise
611
630
  */
612
631
  bool set_warning(DRIZZLE_ERROR::enum_warning_level,
613
 
                   unsigned int code,
 
632
                   drizzled::error_t code,
614
633
                   int cuted_increment);
615
634
  /**
616
635
    Produce warning or note about datetime string data saved into field.
628
647
      thread.
629
648
  */
630
649
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
631
 
                            uint32_t code,
 
650
                            drizzled::error_t code,
632
651
                            const char *str,
633
652
                            uint32_t str_len,
634
 
                            enum enum_drizzle_timestamp_type ts_type,
 
653
                            type::timestamp_t ts_type,
635
654
                            int cuted_increment);
636
655
  /**
637
656
    Produce warning or note about integer datetime value saved into field.
648
667
      thread.
649
668
  */
650
669
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
651
 
                            uint32_t code,
 
670
                            drizzled::error_t code,
652
671
                            int64_t nr,
653
 
                            enum enum_drizzle_timestamp_type ts_type,
 
672
                            type::timestamp_t ts_type,
654
673
                            int cuted_increment);
655
674
  /**
656
675
    Produce warning or note about double datetime data saved into field.
666
685
      thread.
667
686
  */
668
687
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
669
 
                            const uint32_t code,
 
688
                            const drizzled::error_t code,
670
689
                            double nr,
671
 
                            enum enum_drizzle_timestamp_type ts_type);
 
690
                            type::timestamp_t ts_type);
672
691
  bool check_overflow(int op_result)
673
692
  {
674
693
    return (op_result == E_DEC_OVERFLOW);
703
722
    @return
704
723
      value converted from val
705
724
  */
706
 
  int64_t convert_decimal2int64_t(const my_decimal *val,
 
725
  int64_t convert_decimal2int64_t(const type::Decimal *val,
707
726
                                  bool unsigned_flag,
708
727
                                  int *err);
709
728
  /* The max. number of characters */
739
758
  bool isWriteSet();
740
759
  void setReadSet(bool arg= true);
741
760
  void setWriteSet(bool arg= true);
 
761
 
 
762
protected:
 
763
 
 
764
  void pack_num(uint64_t arg, unsigned char *destination= NULL);
 
765
  void pack_num(uint32_t arg, unsigned char *destination= NULL);
 
766
  uint64_t unpack_num(uint64_t &destination, const unsigned char *arg= NULL) const;
 
767
  uint32_t unpack_num(uint32_t &destination, const unsigned char *arg= NULL) const;
742
768
};
743
769
 
 
770
namespace field {
 
771
 
 
772
inline bool isDateTime(const enum_field_types &arg)
 
773
{
 
774
  switch (arg)
 
775
  {
 
776
  case DRIZZLE_TYPE_DATE:
 
777
  case DRIZZLE_TYPE_DATETIME:
 
778
  case DRIZZLE_TYPE_MICROTIME:
 
779
  case DRIZZLE_TYPE_TIME:
 
780
  case DRIZZLE_TYPE_TIMESTAMP:
 
781
    return true;
 
782
 
 
783
  case DRIZZLE_TYPE_BLOB:
 
784
  case DRIZZLE_TYPE_BOOLEAN:
 
785
  case DRIZZLE_TYPE_DECIMAL:
 
786
  case DRIZZLE_TYPE_DOUBLE:
 
787
  case DRIZZLE_TYPE_ENUM:
 
788
  case DRIZZLE_TYPE_LONG:
 
789
  case DRIZZLE_TYPE_LONGLONG:
 
790
  case DRIZZLE_TYPE_NULL:
 
791
  case DRIZZLE_TYPE_UUID:
 
792
  case DRIZZLE_TYPE_VARCHAR:
 
793
    return false;
 
794
  }
 
795
 
 
796
  assert(0);
 
797
  abort();
 
798
}
 
799
 
 
800
} // namespace field
 
801
 
744
802
std::ostream& operator<<(std::ostream& output, const Field &field);
745
803
 
746
804
} /* namespace drizzled */
775
833
  SendField() {}
776
834
};
777
835
 
778
 
/**
779
 
 * A class for quick copying data to fields
780
 
 */
781
 
class CopyField :public memory::SqlAlloc
782
 
{
783
 
  /**
784
 
    Convenience definition of a copy function returned by
785
 
    get_copy_func.
786
 
  */
787
 
  typedef void Copy_func(CopyField*);
788
 
  Copy_func *get_copy_func(Field *to, Field *from);
789
 
public:
790
 
  unsigned char *from_ptr;
791
 
  unsigned char *to_ptr;
792
 
  unsigned char *from_null_ptr;
793
 
  unsigned char *to_null_ptr;
794
 
  bool *null_row;
795
 
  uint32_t from_bit;
796
 
  uint32_t to_bit;
797
 
  uint32_t from_length;
798
 
  uint32_t to_length;
799
 
  Field *from_field;
800
 
  Field *to_field;
801
 
  String tmp;                                   // For items
802
 
 
803
 
  CopyField() {}
804
 
  ~CopyField() {}
805
 
  void set(Field *to,Field *from,bool save);    // Field to field
806
 
  void set(unsigned char *to,Field *from);              // Field to string
807
 
  void (*do_copy)(CopyField *);
808
 
  void (*do_copy2)(CopyField *);                // Used to handle null values
809
 
};
810
 
 
811
836
uint32_t pack_length_to_packflag(uint32_t type);
812
837
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
813
838
int set_field_to_null(Field *field);