~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: lbieber
  • Date: 2010-10-02 19:48:35 UTC
  • mfrom: (1730.6.19 drizzle-make-lcov)
  • Revision ID: lbieber@orisndriz08-20101002194835-q5zd9qc4lvx1xnfo
Merge Hartmut - clean up lex, now require flex to build, also "make lcov" improvements

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, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
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
 
 
27
25
#ifndef DRIZZLED_FIELD_H
28
26
#define DRIZZLED_FIELD_H
29
27
 
30
28
#include "drizzled/sql_error.h"
31
 
#include "drizzled/type/decimal.h"
 
29
#include "drizzled/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
34
#include "drizzled/charset_info.h"
36
35
#include "drizzled/item_result.h"
37
 
#include "drizzled/charset_info.h"
38
36
 
39
37
#include <string>
40
38
#include <vector>
41
39
 
42
 
#include "drizzled/visibility.h"
43
 
 
44
40
namespace drizzled
45
41
{
46
42
 
51
47
#define ASSERT_COLUMN_MARKED_FOR_READ assert(!getTable() || (getTable()->read_set == NULL || isReadSet()))
52
48
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(!getTable() || (getTable()->write_set == NULL || isWriteSet()))
53
49
#else
54
 
#define ASSERT_COLUMN_MARKED_FOR_READ assert(getTable())
55
 
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(getTable())
 
50
#define ASSERT_COLUMN_MARKED_FOR_READ
 
51
#define ASSERT_COLUMN_MARKED_FOR_WRITE
56
52
#endif
57
53
 
58
54
typedef struct st_typelib TYPELIB;
80
76
 * The store_xxx() methods take various input and convert
81
77
 * the input into the raw bytes stored in the ptr member variable.
82
78
 */
83
 
class DRIZZLED_API Field
 
79
class Field
84
80
{
85
81
  /* Prevent use of these */
86
82
  Field(const Field&);
87
83
  void operator=(Field &);
88
 
 
89
84
public:
90
85
  unsigned char *ptr; /**< Position to field in record. Stores raw field value */
91
86
  unsigned char *null_ptr; /**< Byte where null_bit is */
98
93
   */
99
94
private:
100
95
  Table *table;
101
 
 
102
96
public:
103
97
  Table *getTable()
104
98
  {
147
141
  utype unireg_check;
148
142
  uint32_t field_length; /**< Length of this field in bytes */
149
143
  uint32_t flags;
150
 
 
151
 
  bool isUnsigned() const
152
 
  {
153
 
    return flags & UNSIGNED_FLAG;
154
 
  }
155
 
 
156
 
private:
157
144
  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
 
 
171
145
  unsigned char null_bit; /**< Bit used to test null bit */
172
146
  /**
173
147
     If true, this field was created in create_tmp_field_from_item from a NULL
183
157
  static void *operator new(size_t size, memory::Root *mem_root);
184
158
  static void operator delete(void *, size_t)
185
159
  { }
186
 
  static void operator delete(void *, memory::Root *)
187
 
  { }
188
160
 
189
161
  Field(unsigned char *ptr_arg,
190
162
        uint32_t length_arg,
193
165
        utype unireg_check_arg,
194
166
        const char *field_name_arg);
195
167
  virtual ~Field() {}
196
 
 
197
 
  bool hasDefault() const
198
 
  {
199
 
    return not (flags & NO_DEFAULT_VALUE_FLAG);
200
 
  }
201
 
 
202
168
  /* Store functions returns 1 on overflow and -1 on fatal error */
203
169
  virtual int store(const char *to,
204
170
                    uint32_t length,
205
171
                    const CHARSET_INFO * const cs)=0;
206
172
  virtual int store(double nr)=0;
207
173
  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,
210
 
                      const char *to,
211
 
                      uint32_t length,
212
 
                      const CHARSET_INFO * const cs);
 
174
  virtual int store_decimal(const my_decimal *d)=0;
 
175
  int store(const char *to,
 
176
            uint32_t length,
 
177
            const CHARSET_INFO * const cs,
 
178
            enum_check_fields check_level);
213
179
  /**
214
180
    This is called when storing a date in a string.
215
181
 
216
182
    @note
217
183
      Needs to be changed if/when we want to support different time formats.
218
184
  */
219
 
  virtual int store_time(type::Time &ltime, 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)
 
185
  virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
 
186
  virtual double val_real(void)=0;
 
187
  virtual int64_t val_int(void)=0;
 
188
  virtual my_decimal *val_decimal(my_decimal *);
 
189
  inline String *val_str(String *str)
224
190
  {
225
191
    return val_str(str, str);
226
192
  }
227
 
 
228
193
  /*
229
194
     val_str(buf1, buf2) gets two buffers and should use them as follows:
230
195
     if it needs a temp buffer to convert result to string - use buf1
238
203
     This trickery is used to decrease a number of malloc calls.
239
204
  */
240
205
  virtual String *val_str(String*, String *)=0;
241
 
 
242
206
  /*
243
207
   str_needs_quotes() returns true if the value returned by val_str() needs
244
208
   to be quoted when used in constructing an SQL query.
247
211
  virtual Item_result result_type () const=0;
248
212
  virtual Item_result cmp_type () const { return result_type(); }
249
213
  virtual Item_result cast_to_int_type () const { return result_type(); }
250
 
 
251
214
  /**
252
215
     Check whether a field type can be partially indexed by a key.
253
216
 
294
257
   */
295
258
  virtual bool eq_def(Field *field);
296
259
 
297
 
  virtual bool is_timestamp() const
298
 
  {
299
 
    return false;
300
 
  }
301
 
 
302
260
  /**
303
261
   * Returns size (in bytes) used to store field data in memory
304
262
   * (i.e. it returns the maximum size of the field in a row of the table,
342
300
  virtual uint32_t key_length() const;
343
301
  virtual enum_field_types type() const =0;
344
302
  virtual enum_field_types real_type() const;
345
 
  virtual int cmp_max(const unsigned char *a, const unsigned char *b, uint32_t max_len);
 
303
  inline  int cmp(const unsigned char *str) { return cmp(ptr,str); }
 
304
  virtual int cmp_max(const unsigned char *a, const unsigned char *b,
 
305
                      uint32_t max_len);
346
306
  virtual int cmp(const unsigned char *,const unsigned char *)=0;
347
 
  int cmp_internal(const unsigned char *str) { return cmp(ptr,str); }
348
307
  virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
349
308
                         uint32_t max_length=UINT32_MAX);
350
309
  virtual int cmp_offset(uint32_t row_offset);
396
355
                               uint32_t new_null_bit);
397
356
  /** This is used to generate a field in Table from TableShare */
398
357
  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)
 
358
  inline void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
400
359
  {
401
360
    ptr= ptr_arg;
402
361
    null_ptr= null_ptr_arg;
403
362
    null_bit= null_bit_arg;
404
363
  }
405
 
  void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
 
364
  inline void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
406
365
  virtual void move_field_offset(ptrdiff_t ptr_diff)
407
366
  {
408
367
    ptr= ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
460
419
  {
461
420
    set_image(buff,length, &my_charset_bin);
462
421
  }
463
 
  int64_t val_int_offset(uint32_t row_offset)
 
422
  inline int64_t val_int_offset(uint32_t row_offset)
464
423
  {
465
424
    ptr+=row_offset;
466
425
    int64_t tmp=val_int();
468
427
    return tmp;
469
428
  }
470
429
 
471
 
  int64_t val_int_internal(const unsigned char *new_ptr)
 
430
  inline int64_t val_int(const unsigned char *new_ptr)
472
431
  {
473
432
    unsigned char *old_ptr= ptr;
474
433
    int64_t return_value;
477
436
    ptr= old_ptr;
478
437
    return return_value;
479
438
  }
480
 
 
481
 
  String *val_str_internal(String *str, const unsigned char *new_ptr)
 
439
  inline String *val_str(String *str, const unsigned char *new_ptr)
482
440
  {
483
441
    unsigned char *old_ptr= ptr;
484
442
    ptr= const_cast<unsigned char*>(new_ptr);
485
 
    val_str_internal(str);
 
443
    val_str(str);
486
444
    ptr= old_ptr;
487
445
    return str;
488
446
  }
591
549
    return max_length;
592
550
  }
593
551
 
594
 
  uint32_t offset(const unsigned char *record)
 
552
  inline uint32_t offset(const unsigned char *record)
595
553
  {
596
554
    return (uint32_t) (ptr - record);
597
555
  }
598
556
  void copy_from_tmp(int offset);
599
557
  uint32_t fill_cache_field(CacheField *copy);
600
 
  virtual bool get_date(type::Time &ltime,uint32_t fuzzydate);
601
 
  virtual bool get_time(type::Time &ltime);
 
558
  virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
 
559
  virtual bool get_time(DRIZZLE_TIME *ltime);
602
560
  virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
603
561
  virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
604
562
  virtual bool has_charset(void) const { return false; }
630
588
      0 otherwise
631
589
  */
632
590
  bool set_warning(DRIZZLE_ERROR::enum_warning_level,
633
 
                   drizzled::error_t code,
 
591
                   unsigned int code,
634
592
                   int cuted_increment);
635
593
  /**
636
594
    Produce warning or note about datetime string data saved into field.
648
606
      thread.
649
607
  */
650
608
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
651
 
                            drizzled::error_t code,
 
609
                            uint32_t code,
652
610
                            const char *str,
653
611
                            uint32_t str_len,
654
 
                            type::timestamp_t ts_type,
 
612
                            enum enum_drizzle_timestamp_type ts_type,
655
613
                            int cuted_increment);
656
614
  /**
657
615
    Produce warning or note about integer datetime value saved into field.
668
626
      thread.
669
627
  */
670
628
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
671
 
                            drizzled::error_t code,
 
629
                            uint32_t code,
672
630
                            int64_t nr,
673
 
                            type::timestamp_t ts_type,
 
631
                            enum enum_drizzle_timestamp_type ts_type,
674
632
                            int cuted_increment);
675
633
  /**
676
634
    Produce warning or note about double datetime data saved into field.
686
644
      thread.
687
645
  */
688
646
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
689
 
                            const drizzled::error_t code,
 
647
                            const uint32_t code,
690
648
                            double nr,
691
 
                            type::timestamp_t ts_type);
692
 
  bool check_overflow(int op_result)
 
649
                            enum enum_drizzle_timestamp_type ts_type);
 
650
  inline bool check_overflow(int op_result)
693
651
  {
694
652
    return (op_result == E_DEC_OVERFLOW);
695
653
  }
723
681
    @return
724
682
      value converted from val
725
683
  */
726
 
  int64_t convert_decimal2int64_t(const type::Decimal *val,
 
684
  int64_t convert_decimal2int64_t(const my_decimal *val,
727
685
                                  bool unsigned_flag,
728
686
                                  int *err);
729
687
  /* The max. number of characters */
730
 
  uint32_t char_length() const
 
688
  inline uint32_t char_length() const
731
689
  {
732
690
    return field_length / charset()->mbmaxlen;
733
691
  }
734
692
 
735
 
  enum column_format_type column_format() const
 
693
  inline enum column_format_type column_format() const
736
694
  {
737
695
    return (enum column_format_type)
738
696
      ((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
759
717
  bool isWriteSet();
760
718
  void setReadSet(bool arg= true);
761
719
  void setWriteSet(bool arg= true);
762
 
 
763
 
protected:
764
 
 
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;
769
720
};
770
721
 
771
 
std::ostream& operator<<(std::ostream& output, const Field &field);
772
 
 
773
722
} /* namespace drizzled */
774
723
 
775
724
/** @TODO Why is this in the middle of the file???*/
802
751
  SendField() {}
803
752
};
804
753
 
 
754
/**
 
755
 * A class for quick copying data to fields
 
756
 */
 
757
class CopyField :public memory::SqlAlloc
 
758
{
 
759
  /**
 
760
    Convenience definition of a copy function returned by
 
761
    get_copy_func.
 
762
  */
 
763
  typedef void Copy_func(CopyField*);
 
764
  Copy_func *get_copy_func(Field *to, Field *from);
 
765
public:
 
766
  unsigned char *from_ptr;
 
767
  unsigned char *to_ptr;
 
768
  unsigned char *from_null_ptr;
 
769
  unsigned char *to_null_ptr;
 
770
  bool *null_row;
 
771
  uint32_t from_bit;
 
772
  uint32_t to_bit;
 
773
  uint32_t from_length;
 
774
  uint32_t to_length;
 
775
  Field *from_field;
 
776
  Field *to_field;
 
777
  String tmp;                                   // For items
 
778
 
 
779
  CopyField() {}
 
780
  ~CopyField() {}
 
781
  void set(Field *to,Field *from,bool save);    // Field to field
 
782
  void set(unsigned char *to,Field *from);              // Field to string
 
783
  void (*do_copy)(CopyField *);
 
784
  void (*do_copy2)(CopyField *);                // Used to handle null values
 
785
};
 
786
 
805
787
uint32_t pack_length_to_packflag(uint32_t type);
806
788
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
807
789
int set_field_to_null(Field *field);