~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

[patch 125/129] Merge patch for revision 1943 from InnoDB SVN:
revno: 1943
revision-id: svn-v4:16c675df-0fcb-4bc9-8058-dcc011a37293:branches/zip:6207
parent: svn-v4:16c675df-0fcb-4bc9-8058-dcc011a37293:branches/zip:6202
committer: vasil
timestamp: Fri 2009-11-20 08:19:14 +0000
message:
  branches/zip: Merge r6198:6206 from branches/5.1:
  
  (r6203 was skipped as it is already in branches/zip)
  
    ------------------------------------------------------------------------
    r6200 | vasil | 2009-11-19 12:14:23 +0200 (Thu, 19 Nov 2009) | 4 lines
    Changed paths:
       M /branches/5.1/btr/btr0btr.c
    
    branches/5.1:
    
    White space fixup - indent under the opening (
    
    ------------------------------------------------------------------------
    r6203 | jyang | 2009-11-19 15:12:22 +0200 (Thu, 19 Nov 2009) | 8 lines
    Changed paths:
       M /branches/5.1/btr/btr0btr.c
    
    branches/5.1: Use btr_free_root() instead of fseg_free() for
    the fix of bug #48469, because fseg_free() is not defined
    in the zip branch. And we could save one mini-trasaction started
    by fseg_free().
    
    Approved by Marko.
    
    
    ------------------------------------------------------------------------
    r6205 | jyang | 2009-11-20 07:55:48 +0200 (Fri, 20 Nov 2009) | 11 lines
    Changed paths:
       M /branches/5.1/handler/ha_innodb.cc
    
    branches/5.1: Add a special case to handle the Duplicated Key error
    and return DB_ERROR instead. This is to avoid a possible SIGSEGV
    by mysql error handling re-entering the storage layer for dup key
    info without proper table handle.
    This is to prevent a server crash when error situation in bug
    #45961 "DDL on partitioned innodb tables leaves data dictionary
    in an inconsistent state" happens.
    
    rb://157 approved by Sunny Bains.
    
    
    ------------------------------------------------------------------------
    r6206 | jyang | 2009-11-20 09:38:43 +0200 (Fri, 20 Nov 2009) | 5 lines
    Changed paths:
       M /branches/5.1/handler/ha_innodb.cc
    
    branches/5.1: Fix a minor code formating issue for 
    the parenthesis iplacement of the if condition in
    rename_table().
    
    
    ------------------------------------------------------------------------
modified:
  handler/ha_innodb.cc           2@16c675df-0fcb-4bc9-8058-dcc011a37293:trunk%2Fhandler%2Fha_innodb.cc
diff:
=== modified file 'handler/ha_innodb.cc'

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