~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: Brian Aker
  • Date: 2011-02-22 06:12:02 UTC
  • mfrom: (2190.1.6 drizzle-build)
  • Revision ID: brian@tangent.org-20110222061202-k03czxykqy4x9hjs
List update, header fixes, multiple symbols, and David deletes some code.

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
 
#include "drizzled/sql_error.h"
29
 
#include "drizzled/decimal.h"
30
 
#include "drizzled/key_map.h"
31
 
#include "drizzled/sql_list.h"
32
 
#include "drizzled/structs.h"
33
 
#include "drizzled/charset_info.h"
34
 
#include "drizzled/item_result.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_info.h>
 
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&);
82
87
  void operator=(Field &);
 
88
 
83
89
public:
84
90
  unsigned char *ptr; /**< Position to field in record. Stores raw field value */
85
91
  unsigned char *null_ptr; /**< Byte where null_bit is */
92
98
   */
93
99
private:
94
100
  Table *table;
 
101
 
95
102
public:
96
103
  Table *getTable()
97
104
  {
140
147
  utype unireg_check;
141
148
  uint32_t field_length; /**< Length of this field in bytes */
142
149
  uint32_t flags;
 
150
 
 
151
  bool isUnsigned() const
 
152
  {
 
153
    return flags & UNSIGNED_FLAG;
 
154
  }
 
155
 
 
156
private:
143
157
  uint16_t field_index; /**< Index of this Field in Table::fields array */
 
158
 
 
159
public:
 
160
 
 
161
  uint16_t position() const
 
162
  {
 
163
    return field_index;
 
164
  }
 
165
 
 
166
  void setPosition(uint32_t arg)
 
167
  {
 
168
    field_index= arg;
 
169
  }
 
170
 
144
171
  unsigned char null_bit; /**< Bit used to test null bit */
145
172
  /**
146
173
     If true, this field was created in create_tmp_field_from_item from a NULL
166
193
        utype unireg_check_arg,
167
194
        const char *field_name_arg);
168
195
  virtual ~Field() {}
 
196
 
 
197
  bool hasDefault() const
 
198
  {
 
199
    return not (flags & NO_DEFAULT_VALUE_FLAG);
 
200
  }
 
201
 
169
202
  /* Store functions returns 1 on overflow and -1 on fatal error */
170
203
  virtual int store(const char *to,
171
204
                    uint32_t length,
172
205
                    const CHARSET_INFO * const cs)=0;
173
206
  virtual int store(double nr)=0;
174
207
  virtual int store(int64_t nr, bool unsigned_val)=0;
175
 
  virtual int store_decimal(const my_decimal *d)=0;
176
 
  int store(const char *to,
177
 
            uint32_t length,
178
 
            const CHARSET_INFO * const cs,
179
 
            enum_check_fields check_level);
 
208
  virtual int store_decimal(const type::Decimal *d)=0;
 
209
  int store_and_check(enum_check_fields check_level,
 
210
                      const char *to,
 
211
                      uint32_t length,
 
212
                      const CHARSET_INFO * const cs);
180
213
  /**
181
214
    This is called when storing a date in a string.
182
215
 
183
216
    @note
184
217
      Needs to be changed if/when we want to support different time formats.
185
218
  */
186
 
  virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
187
 
  virtual double val_real(void)=0;
188
 
  virtual int64_t val_int(void)=0;
189
 
  virtual my_decimal *val_decimal(my_decimal *);
190
 
  inline String *val_str(String *str)
 
219
  virtual int store_time(type::Time &ltime, type::timestamp_t t_type);
 
220
  virtual double val_real() const=0;
 
221
  virtual int64_t val_int() const =0;
 
222
  virtual type::Decimal *val_decimal(type::Decimal *) const;
 
223
  String *val_str_internal(String *str) const
191
224
  {
192
225
    return val_str(str, str);
193
226
  }
 
227
 
194
228
  /*
195
229
     val_str(buf1, buf2) gets two buffers and should use them as follows:
196
230
     if it needs a temp buffer to convert result to string - use buf1
203
237
     an unnecessary free (and later, may be an alloc).
204
238
     This trickery is used to decrease a number of malloc calls.
205
239
  */
206
 
  virtual String *val_str(String*, String *)=0;
 
240
  virtual String *val_str(String*, String *) const =0;
 
241
 
207
242
  /*
208
243
   str_needs_quotes() returns true if the value returned by val_str() needs
209
244
   to be quoted when used in constructing an SQL query.
212
247
  virtual Item_result result_type () const=0;
213
248
  virtual Item_result cmp_type () const { return result_type(); }
214
249
  virtual Item_result cast_to_int_type () const { return result_type(); }
 
250
 
215
251
  /**
216
252
     Check whether a field type can be partially indexed by a key.
217
253
 
258
294
   */
259
295
  virtual bool eq_def(Field *field);
260
296
 
 
297
  virtual bool is_timestamp() const
 
298
  {
 
299
    return false;
 
300
  }
 
301
 
261
302
  /**
262
303
   * Returns size (in bytes) used to store field data in memory
263
304
   * (i.e. it returns the maximum size of the field in a row of the table,
301
342
  virtual uint32_t key_length() const;
302
343
  virtual enum_field_types type() const =0;
303
344
  virtual enum_field_types real_type() const;
304
 
  inline  int cmp(const unsigned char *str) { return cmp(ptr,str); }
305
 
  virtual int cmp_max(const unsigned char *a, const unsigned char *b,
306
 
                      uint32_t max_len);
 
345
  virtual int cmp_max(const unsigned char *a, const unsigned char *b, uint32_t max_len);
307
346
  virtual int cmp(const unsigned char *,const unsigned char *)=0;
 
347
  int cmp_internal(const unsigned char *str) { return cmp(ptr,str); }
308
348
  virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
309
349
                         uint32_t max_length=UINT32_MAX);
310
350
  virtual int cmp_offset(uint32_t row_offset);
323
363
  // For new field
324
364
  virtual uint32_t size_of() const =0;
325
365
 
326
 
  bool is_null(ptrdiff_t row_offset= 0);
327
 
  bool is_real_null(ptrdiff_t row_offset= 0);
328
 
  bool is_null_in_record(const unsigned char *record);
329
 
  bool is_null_in_record_with_offset(ptrdiff_t offset);
 
366
  bool is_null(ptrdiff_t row_offset= 0) const;
 
367
  bool is_real_null(ptrdiff_t row_offset= 0) const;
 
368
  bool is_null_in_record(const unsigned char *record) const;
 
369
  bool is_null_in_record_with_offset(ptrdiff_t offset) const;
330
370
  void set_null(ptrdiff_t row_offset= 0);
331
371
  void set_notnull(ptrdiff_t row_offset= 0);
332
 
  bool maybe_null(void);
333
 
  bool real_maybe_null(void);
 
372
  bool maybe_null(void) const;
 
373
  bool real_maybe_null(void) const;
334
374
 
335
375
  virtual void make_field(SendField *);
336
376
  virtual void sort_string(unsigned char *buff,uint32_t length)=0;
356
396
                               uint32_t new_null_bit);
357
397
  /** This is used to generate a field in Table from TableShare */
358
398
  Field *clone(memory::Root *mem_root, Table *new_table);
359
 
  inline void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
 
399
  void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
360
400
  {
361
401
    ptr= ptr_arg;
362
402
    null_ptr= null_ptr_arg;
363
403
    null_bit= null_bit_arg;
364
404
  }
365
 
  inline void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
 
405
  void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
366
406
  virtual void move_field_offset(ptrdiff_t ptr_diff)
367
407
  {
368
408
    ptr= ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
420
460
  {
421
461
    set_image(buff,length, &my_charset_bin);
422
462
  }
423
 
  inline int64_t val_int_offset(uint32_t row_offset)
 
463
  int64_t val_int_offset(uint32_t row_offset)
424
464
  {
425
465
    ptr+=row_offset;
426
466
    int64_t tmp=val_int();
428
468
    return tmp;
429
469
  }
430
470
 
431
 
  inline int64_t val_int(const unsigned char *new_ptr)
 
471
  int64_t val_int_internal(const unsigned char *new_ptr)
432
472
  {
433
473
    unsigned char *old_ptr= ptr;
434
 
    int64_t return_value;
435
474
    ptr= const_cast<unsigned char*>(new_ptr);
436
 
    return_value= val_int();
 
475
    int64_t return_value= val_int();
437
476
    ptr= old_ptr;
438
477
    return return_value;
439
478
  }
440
 
  inline String *val_str(String *str, const unsigned char *new_ptr)
 
479
 
 
480
  String *val_str_internal(String *str, const unsigned char *new_ptr)
441
481
  {
442
482
    unsigned char *old_ptr= ptr;
443
483
    ptr= const_cast<unsigned char*>(new_ptr);
444
 
    val_str(str);
 
484
    val_str_internal(str);
445
485
    ptr= old_ptr;
446
486
    return str;
447
487
  }
550
590
    return max_length;
551
591
  }
552
592
 
553
 
  inline uint32_t offset(const unsigned char *record)
 
593
  uint32_t offset(const unsigned char *record)
554
594
  {
555
595
    return (uint32_t) (ptr - record);
556
596
  }
557
597
  void copy_from_tmp(int offset);
558
598
  uint32_t fill_cache_field(CacheField *copy);
559
 
  virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
560
 
  virtual bool get_time(DRIZZLE_TIME *ltime);
 
599
  virtual bool get_date(type::Time &ltime,uint32_t fuzzydate) const;
 
600
  virtual bool get_time(type::Time &ltime) const;
561
601
  virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
562
602
  virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
563
603
  virtual bool has_charset(void) const { return false; }
589
629
      0 otherwise
590
630
  */
591
631
  bool set_warning(DRIZZLE_ERROR::enum_warning_level,
592
 
                   unsigned int code,
 
632
                   drizzled::error_t code,
593
633
                   int cuted_increment);
594
634
  /**
595
635
    Produce warning or note about datetime string data saved into field.
607
647
      thread.
608
648
  */
609
649
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
610
 
                            uint32_t code,
 
650
                            drizzled::error_t code,
611
651
                            const char *str,
612
652
                            uint32_t str_len,
613
 
                            enum enum_drizzle_timestamp_type ts_type,
 
653
                            type::timestamp_t ts_type,
614
654
                            int cuted_increment);
615
655
  /**
616
656
    Produce warning or note about integer datetime value saved into field.
627
667
      thread.
628
668
  */
629
669
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
630
 
                            uint32_t code,
 
670
                            drizzled::error_t code,
631
671
                            int64_t nr,
632
 
                            enum enum_drizzle_timestamp_type ts_type,
 
672
                            type::timestamp_t ts_type,
633
673
                            int cuted_increment);
634
674
  /**
635
675
    Produce warning or note about double datetime data saved into field.
645
685
      thread.
646
686
  */
647
687
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
648
 
                            const uint32_t code,
 
688
                            const drizzled::error_t code,
649
689
                            double nr,
650
 
                            enum enum_drizzle_timestamp_type ts_type);
651
 
  inline bool check_overflow(int op_result)
 
690
                            type::timestamp_t ts_type);
 
691
  bool check_overflow(int op_result)
652
692
  {
653
693
    return (op_result == E_DEC_OVERFLOW);
654
694
  }
682
722
    @return
683
723
      value converted from val
684
724
  */
685
 
  int64_t convert_decimal2int64_t(const my_decimal *val,
 
725
  int64_t convert_decimal2int64_t(const type::Decimal *val,
686
726
                                  bool unsigned_flag,
687
727
                                  int *err);
688
728
  /* The max. number of characters */
689
 
  inline uint32_t char_length() const
 
729
  uint32_t char_length() const
690
730
  {
691
731
    return field_length / charset()->mbmaxlen;
692
732
  }
693
733
 
694
 
  inline enum column_format_type column_format() const
 
734
  enum column_format_type column_format() const
695
735
  {
696
736
    return (enum column_format_type)
697
737
      ((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
698
738
  }
699
739
 
700
740
  /* Hash value */
701
 
  virtual void hash(uint32_t *nr, uint32_t *nr2);
 
741
  virtual void hash(uint32_t *nr, uint32_t *nr2) const;
702
742
  friend bool reopen_table(Session *,Table *,bool);
703
743
 
704
744
  friend class CopyField;
714
754
  friend class Item_sum_max;
715
755
  friend class Item_func_group_concat;
716
756
 
717
 
  bool isReadSet();
 
757
  bool isReadSet() const;
718
758
  bool isWriteSet();
719
759
  void setReadSet(bool arg= true);
720
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;
721
768
};
722
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
 
 
802
std::ostream& operator<<(std::ostream& output, const Field &field);
 
803
 
723
804
} /* namespace drizzled */
724
805
 
725
806
/** @TODO Why is this in the middle of the file???*/
726
 
#include "drizzled/create_field.h"
 
807
#include <drizzled/create_field.h>
727
808
 
728
809
namespace drizzled
729
810
{
752
833
  SendField() {}
753
834
};
754
835
 
755
 
/**
756
 
 * A class for quick copying data to fields
757
 
 */
758
 
class CopyField :public memory::SqlAlloc
759
 
{
760
 
  /**
761
 
    Convenience definition of a copy function returned by
762
 
    get_copy_func.
763
 
  */
764
 
  typedef void Copy_func(CopyField*);
765
 
  Copy_func *get_copy_func(Field *to, Field *from);
766
 
public:
767
 
  unsigned char *from_ptr;
768
 
  unsigned char *to_ptr;
769
 
  unsigned char *from_null_ptr;
770
 
  unsigned char *to_null_ptr;
771
 
  bool *null_row;
772
 
  uint32_t from_bit;
773
 
  uint32_t to_bit;
774
 
  uint32_t from_length;
775
 
  uint32_t to_length;
776
 
  Field *from_field;
777
 
  Field *to_field;
778
 
  String tmp;                                   // For items
779
 
 
780
 
  CopyField() {}
781
 
  ~CopyField() {}
782
 
  void set(Field *to,Field *from,bool save);    // Field to field
783
 
  void set(unsigned char *to,Field *from);              // Field to string
784
 
  void (*do_copy)(CopyField *);
785
 
  void (*do_copy2)(CopyField *);                // Used to handle null values
786
 
};
787
 
 
788
836
uint32_t pack_length_to_packflag(uint32_t type);
789
837
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
790
838
int set_field_to_null(Field *field);