~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: Monty Taylor
  • Date: 2008-10-16 09:12:23 UTC
  • mto: (511.1.6 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016091223-17ngih0qu9vssjs3
We pass -Wunused-macros now!

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
  variables must declare the size_of() member function.
23
23
*/
24
24
 
25
 
#ifndef DRIZZLED_FIELD_H
26
 
#define DRIZZLED_FIELD_H
27
 
 
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"
36
 
 
37
 
#include <string>
38
 
#include <vector>
39
 
 
40
 
namespace drizzled
41
 
{
42
25
 
43
26
#define DATETIME_DEC                     6
44
27
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
45
28
 
46
 
#ifdef DEBUG
47
 
#define ASSERT_COLUMN_MARKED_FOR_READ assert(!getTable() || (getTable()->read_set == NULL || isReadSet()))
48
 
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(!getTable() || (getTable()->write_set == NULL || isWriteSet()))
49
 
#else
50
 
#define ASSERT_COLUMN_MARKED_FOR_READ
51
 
#define ASSERT_COLUMN_MARKED_FOR_WRITE
52
 
#endif
53
 
 
54
 
typedef struct st_typelib TYPELIB;
55
 
 
56
29
const uint32_t max_field_size= (uint32_t) 4294967295U;
57
30
 
58
 
class SendField;
59
 
class CreateField;
60
 
class TableShare;
61
 
class Field;
62
 
struct CacheField;
63
 
 
 
31
class Send_field;
 
32
class Protocol;
 
33
class Create_field;
 
34
struct st_cache_field;
64
35
int field_conv(Field *to,Field *from);
65
36
 
66
 
/**
67
 
 * Class representing a Field in a Table
68
 
 *
69
 
 * @details
70
 
 *
71
 
 * The value stored in the Field object is stored in the
72
 
 * unsigned char pointer member variable called ptr.  The
73
 
 * val_xxx() methods retrieve this raw byte value and
74
 
 * convert the byte into the appropriate output (int, decimal, etc).
75
 
 *
76
 
 * The store_xxx() methods take various input and convert
77
 
 * the input into the raw bytes stored in the ptr member variable.
78
 
 */
 
37
inline uint32_t get_enum_pack_length(int elements)
 
38
{
 
39
  return elements < 256 ? 1 : 2;
 
40
}
 
41
 
 
42
inline uint32_t get_set_pack_length(int elements)
 
43
{
 
44
  uint32_t len= (elements + 7) / 8;
 
45
  return len > 4 ? 8 : len;
 
46
}
 
47
 
 
48
class virtual_column_info: public Sql_alloc
 
49
{
 
50
public:
 
51
  Item *expr_item;
 
52
  LEX_STRING expr_str;
 
53
  Item *item_free_list;
 
54
  virtual_column_info() 
 
55
  : expr_item(0), item_free_list(0),
 
56
    field_type(DRIZZLE_TYPE_VIRTUAL),
 
57
    is_stored(false), data_inited(false)
 
58
  {
 
59
    expr_str.str= NULL;
 
60
    expr_str.length= 0;
 
61
  };
 
62
  ~virtual_column_info() {}
 
63
  enum_field_types get_real_type()
 
64
  {
 
65
    assert(data_inited);
 
66
    return data_inited ? field_type : DRIZZLE_TYPE_VIRTUAL;
 
67
  }
 
68
  void set_field_type(enum_field_types fld_type)
 
69
  {
 
70
    /* Calling this function can only be done once. */
 
71
    assert(not data_inited);
 
72
    data_inited= true;
 
73
    field_type= fld_type;
 
74
  }
 
75
  bool get_field_stored()
 
76
  {
 
77
    assert(data_inited);
 
78
    return data_inited ? is_stored : true;
 
79
  }
 
80
  void set_field_stored(bool stored)
 
81
  {
 
82
    is_stored= stored;
 
83
  }
 
84
private:
 
85
  /*
 
86
    The following data is only updated by the parser and read
 
87
    when a Create_field object is created/initialized.
 
88
  */
 
89
  enum_field_types field_type;   /* Real field type*/
 
90
  bool is_stored;             /* Indication that the field is 
 
91
                                    phisically stored in the database*/
 
92
  /*
 
93
    This flag is used to prevent other applications from
 
94
    reading and using incorrect data.
 
95
  */
 
96
  bool data_inited; 
 
97
};
 
98
 
79
99
class Field
80
100
{
81
 
  /* Prevent use of these */
82
 
  Field(const Field&);
 
101
  Field(const Item &);                          /* Prevent use of these */
83
102
  void operator=(Field &);
84
103
public:
85
 
  unsigned char *ptr; /**< Position to field in record. Stores raw field value */
86
 
  unsigned char *null_ptr; /**< Byte where null_bit is */
87
 
 
88
 
  /**
89
 
   * Pointer to the Table object containing this Field
90
 
   *
91
 
   * @note You can use table->in_use as replacement for current_session member
92
 
   * only inside of val_*() and store() members (e.g. you can't use it in cons)
93
 
   */
94
 
private:
95
 
  Table *table;
96
 
public:
97
 
  Table *getTable()
98
 
  {
99
 
    assert(table);
100
 
    return table;
101
 
  }
102
 
 
103
 
  Table *getTable() const
104
 
  {
105
 
    assert(table);
106
 
    return table;
107
 
  }
108
 
 
109
 
  void setTable(Table *table_arg)
110
 
  {
111
 
    table= table_arg;
112
 
  }
113
 
 
114
 
  Table *orig_table; /**< Pointer to the original Table. @TODO What is "the original table"? */
115
 
  const char *field_name; /**< Name of the field */
116
 
  LEX_STRING comment; /**< A comment about the field */
117
 
 
118
 
  /** The field is part of the following keys */
119
 
  key_map       key_start;
120
 
  key_map part_of_key;
121
 
  key_map part_of_key_not_clustered;
122
 
  key_map part_of_sortkey;
123
 
 
 
104
  static void *operator new(size_t size) {return sql_alloc(size); }
 
105
  static void operator delete(void *ptr_arg __attribute__((unused)),
 
106
                              size_t size __attribute__((unused)))
 
107
  { TRASH(ptr_arg, size); }
 
108
 
 
109
  unsigned char         *ptr;                   // Position to field in record
 
110
  unsigned char         *null_ptr;              // Byte where null_bit is
124
111
  /*
125
 
    We use three additional unireg types for TIMESTAMP for hysterical
126
 
    raisins and limitations in the MySQL FRM file format.
127
 
 
128
 
    A good TODO is to clean this up as we can support just about
129
 
    anything in the table proto message now.
130
 
  */
131
 
  enum utype
132
 
  {
133
 
    NONE,
134
 
    NEXT_NUMBER,
135
 
    TIMESTAMP_OLD_FIELD,
136
 
    TIMESTAMP_DN_FIELD,
137
 
    TIMESTAMP_UN_FIELD,
138
 
    TIMESTAMP_DNUN_FIELD
139
 
  };
140
 
 
141
 
  utype unireg_check;
142
 
  uint32_t field_length; /**< Length of this field in bytes */
143
 
  uint32_t flags;
144
 
  uint16_t field_index; /**< Index of this Field in Table::fields array */
145
 
  unsigned char null_bit; /**< Bit used to test null bit */
 
112
    Note that you can use table->in_use as replacement for current_thd member 
 
113
    only inside of val_*() and store() members (e.g. you can't use it in cons)
 
114
  */
 
115
  Table *table;         // Pointer for table
 
116
  Table *orig_table;            // Pointer to original table
 
117
  const char    **table_name, *field_name;
 
118
  LEX_STRING    comment;
 
119
  /* Field is part of the following keys */
 
120
  key_map       key_start, part_of_key, part_of_key_not_clustered;
 
121
  key_map       part_of_sortkey;
 
122
  /* 
 
123
    We use three additional unireg types for TIMESTAMP to overcome limitation 
 
124
    of current binary format of .frm file. We'd like to be able to support 
 
125
    NOW() as default and on update value for such fields but unable to hold 
 
126
    this info anywhere except unireg_check field. This issue will be resolved
 
127
    in more clean way with transition to new text based .frm format.
 
128
    See also comment for Field_timestamp::Field_timestamp().
 
129
  */
 
130
  enum utype  { NONE,DATE,SHIELD,NOEMPTY,CASEUP,PNR,BGNR,PGNR,YES,NO,REL,
 
131
                CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
 
132
                BIT_FIELD, TIMESTAMP_OLD_FIELD, CAPITALIZE, BLOB_FIELD,
 
133
                TIMESTAMP_DN_FIELD, TIMESTAMP_UN_FIELD, TIMESTAMP_DNUN_FIELD};
 
134
  enum imagetype { itRAW, itMBR};
 
135
 
 
136
  utype         unireg_check;
 
137
  uint32_t      field_length;           // Length of field
 
138
  uint32_t      flags;
 
139
  uint16_t        field_index;            // field number in fields array
 
140
  unsigned char         null_bit;               // Bit used to test null bit
146
141
  /**
147
142
     If true, this field was created in create_tmp_field_from_item from a NULL
148
143
     value. This means that the type of the field is just a guess, and the type
150
145
 
151
146
     @see create_tmp_field_from_item
152
147
     @see Item_type_holder::get_real_type
 
148
 
153
149
   */
154
150
  bool is_created_from_null_item;
155
151
 
156
 
  static void *operator new(size_t size);
157
 
  static void *operator new(size_t size, memory::Root *mem_root);
158
 
  static void operator delete(void *, size_t)
159
 
  { }
 
152
  /* Virtual column data */
 
153
  virtual_column_info *vcol_info;
 
154
  /*
 
155
    Indication that the field is phycically stored in tables 
 
156
    rather than just generated on SQL queries.
 
157
    As of now, false can only be set for generated-only virtual columns.
 
158
  */
 
159
  bool is_stored;
160
160
 
161
 
  Field(unsigned char *ptr_arg,
162
 
        uint32_t length_arg,
163
 
        unsigned char *null_ptr_arg,
164
 
        unsigned char null_bit_arg,
165
 
        utype unireg_check_arg,
 
161
  Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
 
162
        unsigned char null_bit_arg, utype unireg_check_arg,
166
163
        const char *field_name_arg);
167
164
  virtual ~Field() {}
168
165
  /* Store functions returns 1 on overflow and -1 on fatal error */
169
 
  virtual int store(const char *to,
170
 
                    uint32_t length,
171
 
                    const CHARSET_INFO * const cs)=0;
172
 
  virtual int store(double nr)=0;
173
 
  virtual int store(int64_t nr, bool unsigned_val)=0;
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,
 
166
  virtual int  store(const char *to, uint32_t length, const CHARSET_INFO * const cs)=0;
 
167
  virtual int  store(double nr)=0;
 
168
  virtual int  store(int64_t nr, bool unsigned_val)=0;
 
169
  virtual int  store_decimal(const my_decimal *d)=0;
 
170
  virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
 
171
  int store(const char *to, uint32_t length, const CHARSET_INFO * const cs,
178
172
            enum_check_fields check_level);
179
 
  /**
180
 
    This is called when storing a date in a string.
181
 
 
182
 
    @note
183
 
      Needs to be changed if/when we want to support different time formats.
184
 
  */
185
 
  virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
186
173
  virtual double val_real(void)=0;
187
174
  virtual int64_t val_int(void)=0;
188
175
  virtual my_decimal *val_decimal(my_decimal *);
189
 
  inline String *val_str(String *str)
190
 
  {
191
 
    return val_str(str, str);
192
 
  }
 
176
  inline String *val_str(String *str) { return val_str(str, str); }
193
177
  /*
194
178
     val_str(buf1, buf2) gets two buffers and should use them as follows:
195
179
     if it needs a temp buffer to convert result to string - use buf1
202
186
     an unnecessary free (and later, may be an alloc).
203
187
     This trickery is used to decrease a number of malloc calls.
204
188
  */
205
 
  virtual String *val_str(String*, String *)=0;
 
189
  virtual String *val_str(String*,String *)=0;
 
190
  String *val_int_as_str(String *val_buffer, bool unsigned_flag);
206
191
  /*
207
192
   str_needs_quotes() returns true if the value returned by val_str() needs
208
193
   to be quoted when used in constructing an SQL query.
211
196
  virtual Item_result result_type () const=0;
212
197
  virtual Item_result cmp_type () const { return result_type(); }
213
198
  virtual Item_result cast_to_int_type () const { return result_type(); }
214
 
  /**
215
 
     Check whether a field type can be partially indexed by a key.
216
 
 
217
 
     This is a static method, rather than a virtual function, because we need
218
 
     to check the type of a non-Field in alter_table().
219
 
 
220
 
     @param type  field type
221
 
 
222
 
     @retval
223
 
       true  Type can have a prefixed key
224
 
     @retval
225
 
       false Type can not have a prefixed key
226
 
  */
227
199
  static bool type_can_have_key_part(enum_field_types);
228
 
  /**
229
 
    Return type of which can carry value of both given types in UNION result.
230
 
 
231
 
    @param a  type for merging
232
 
    @param b  type for merging
233
 
 
234
 
    @retval
235
 
      type of field
236
 
  */
237
200
  static enum_field_types field_type_merge(enum_field_types, enum_field_types);
238
 
 
239
 
  /**
240
 
     Detect Item_result by given field type of UNION merge result.
241
 
 
242
 
     @param field_type  given field type
243
 
 
244
 
     @return
245
 
       Item_result (type of internal MySQL expression result)
246
 
  */
247
201
  static Item_result result_merge_type(enum_field_types);
248
 
 
249
 
  virtual bool eq(Field *field);
250
 
  /**
251
 
   * Returns true if the fields are equally defined
252
 
   *
253
 
   * @retval
254
 
   *  true  This Field is equally defined to supplied Field
255
 
   * @retval
256
 
   *  false This Field is NOT equally defined to supplied Field
257
 
   */
 
202
  virtual bool eq(Field *field)
 
203
  {
 
204
    return (ptr == field->ptr && null_ptr == field->null_ptr &&
 
205
            null_bit == field->null_bit);
 
206
  }
258
207
  virtual bool eq_def(Field *field);
259
 
 
260
 
  /**
261
 
   * Returns size (in bytes) used to store field data in memory
262
 
   * (i.e. it returns the maximum size of the field in a row of the table,
263
 
   * which is located in RAM).
264
 
   */
265
 
  virtual uint32_t pack_length() const;
266
 
 
267
 
  /**
268
 
   * Returns size (in bytes) used to store field data on
269
 
   * storage (i.e. it returns the maximal size of the field in a row of the
270
 
   * table, which is located on disk).
271
 
   */
272
 
  virtual uint32_t pack_length_in_rec() const;
273
 
 
274
 
  /**
275
 
   * Return the "real size" of the data in memory.
276
 
   * For varstrings, this does _not_ include the length bytes.
277
 
   */
278
 
  virtual uint32_t data_length();
279
 
  /**
280
 
   * Returns the number of bytes actually used to store the data
281
 
   * of the field. So for a varstring it includes both lenght byte(s) and
282
 
   * string data, and anything after data_length() bytes are unused.
283
 
   */
284
 
  virtual uint32_t used_length();
285
 
  virtual uint32_t sort_length() const;
 
208
  
 
209
  /*
 
210
    pack_length() returns size (in bytes) used to store field data in memory
 
211
    (i.e. it returns the maximum size of the field in a row of the table,
 
212
    which is located in RAM).
 
213
  */
 
214
  virtual uint32_t pack_length() const { return (uint32_t) field_length; }
 
215
 
 
216
  /*
 
217
    pack_length_in_rec() returns size (in bytes) used to store field data on
 
218
    storage (i.e. it returns the maximal size of the field in a row of the
 
219
    table, which is located on disk).
 
220
  */
 
221
  virtual uint32_t pack_length_in_rec() const { return pack_length(); }
 
222
  virtual int compatible_field_size(uint32_t field_metadata);
 
223
  virtual uint32_t pack_length_from_metadata(uint32_t field_metadata)
 
224
  { return field_metadata; }
 
225
  /*
 
226
    This method is used to return the size of the data in a row-based
 
227
    replication row record. The default implementation of returning 0 is
 
228
    designed to allow fields that do not use metadata to return true (1)
 
229
    from compatible_field_size() which uses this function in the comparison.
 
230
    The default value for field metadata for fields that do not have 
 
231
    metadata is 0. Thus, 0 == 0 means the fields are compatible in size.
 
232
 
 
233
    Note: While most classes that override this method return pack_length(),
 
234
    the classes Field_varstring, and Field_blob return 
 
235
    field_length + 1, field_length, and pack_length_no_ptr() respectfully.
 
236
  */
 
237
  virtual uint32_t row_pack_length() { return 0; }
 
238
  virtual int save_field_metadata(unsigned char *first_byte)
 
239
  { return do_save_field_metadata(first_byte); }
 
240
 
 
241
  /*
 
242
    data_length() return the "real size" of the data in memory.
 
243
    For varstrings, this does _not_ include the length bytes.
 
244
  */
 
245
  virtual uint32_t data_length() { return pack_length(); }
 
246
  /*
 
247
    used_length() returns the number of bytes actually used to store the data
 
248
    of the field. So for a varstring it includes both lenght byte(s) and
 
249
    string data, and anything after data_length() bytes are unused.
 
250
  */
 
251
  virtual uint32_t used_length() { return pack_length(); }
 
252
  virtual uint32_t sort_length() const { return pack_length(); }
286
253
 
287
254
  /**
288
255
     Get the maximum size of the data in packed format.
290
257
     @return Maximum data length of the field when packed using the
291
258
     Field::pack() function.
292
259
   */
293
 
  virtual uint32_t max_data_length() const;
294
 
  virtual int reset(void);
295
 
  virtual void reset_fields();
296
 
  virtual void set_default();
297
 
  virtual bool binary() const;
298
 
  virtual bool zero_pack() const;
299
 
  virtual enum ha_base_keytype key_type() const;
300
 
  virtual uint32_t key_length() const;
 
260
  virtual uint32_t max_data_length() const {
 
261
    return pack_length();
 
262
  };
 
263
 
 
264
  virtual int reset(void) { memset(ptr, 0, pack_length()); return 0; }
 
265
  virtual void reset_fields() {}
 
266
  virtual void set_default()
 
267
  {
 
268
    my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->getDefaultValues() - table->record[0]);
 
269
    memcpy(ptr, ptr + l_offset, pack_length());
 
270
    if (null_ptr)
 
271
      *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
 
272
  }
 
273
  virtual bool binary() const { return 1; }
 
274
  virtual bool zero_pack() const { return 1; }
 
275
  virtual enum ha_base_keytype key_type() const { return HA_KEYTYPE_BINARY; }
 
276
  virtual uint32_t key_length() const { return pack_length(); }
301
277
  virtual enum_field_types type() const =0;
302
 
  virtual enum_field_types real_type() const;
 
278
  virtual enum_field_types real_type() const { return type(); }
303
279
  inline  int cmp(const unsigned char *str) { return cmp(ptr,str); }
304
280
  virtual int cmp_max(const unsigned char *a, const unsigned char *b,
305
 
                      uint32_t max_len);
 
281
                      uint32_t max_len __attribute__((unused)))
 
282
    { return cmp(a, b); }
306
283
  virtual int cmp(const unsigned char *,const unsigned char *)=0;
307
284
  virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
308
 
                         uint32_t max_length=UINT32_MAX);
309
 
  virtual int cmp_offset(uint32_t row_offset);
310
 
  virtual int cmp_binary_offset(uint32_t row_offset);
311
 
  virtual int key_cmp(const unsigned char *a,const unsigned char *b);
312
 
  virtual int key_cmp(const unsigned char *str, uint32_t length);
313
 
  virtual uint32_t decimals() const;
314
 
 
 
285
                         uint32_t  __attribute__((unused)) max_length=UINT32_MAX)
 
286
  { return memcmp(a,b,pack_length()); }
 
287
  virtual int cmp_offset(uint32_t row_offset)
 
288
  { return cmp(ptr,ptr+row_offset); }
 
289
  virtual int cmp_binary_offset(uint32_t row_offset)
 
290
  { return cmp_binary(ptr, ptr+row_offset); };
 
291
  virtual int key_cmp(const unsigned char *a,const unsigned char *b)
 
292
  { return cmp(a, b); }
 
293
  virtual int key_cmp(const unsigned char *str, uint32_t length __attribute__((unused)))
 
294
  { return cmp(ptr,str); }
 
295
  virtual uint32_t decimals() const { return 0; }
315
296
  /*
316
297
    Caller beware: sql_type can change str.Ptr, so check
317
298
    ptr() to see if it changed if you are using your own buffer
318
299
    in str and restore it with set() if needed
319
300
  */
320
301
  virtual void sql_type(String &str) const =0;
321
 
 
322
 
  // For new field
323
 
  virtual uint32_t size_of() const =0;
324
 
 
325
 
  bool is_null(ptrdiff_t row_offset= 0);
326
 
  bool is_real_null(ptrdiff_t row_offset= 0);
327
 
  bool is_null_in_record(const unsigned char *record);
328
 
  bool is_null_in_record_with_offset(ptrdiff_t offset);
329
 
  void set_null(ptrdiff_t row_offset= 0);
330
 
  void set_notnull(ptrdiff_t row_offset= 0);
331
 
  bool maybe_null(void);
332
 
  bool real_maybe_null(void);
333
 
 
334
 
  virtual void make_field(SendField *);
 
302
  virtual uint32_t size_of() const =0;          // For new field
 
303
  inline bool is_null(my_ptrdiff_t row_offset= 0)
 
304
  { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : table->null_row; }
 
305
  inline bool is_real_null(my_ptrdiff_t row_offset= 0)
 
306
    { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : 0; }
 
307
  inline bool is_null_in_record(const unsigned char *record)
 
308
  {
 
309
    if (!null_ptr)
 
310
      return 0;
 
311
    return test(record[(uint32_t) (null_ptr -table->record[0])] &
 
312
                null_bit);
 
313
  }
 
314
  inline bool is_null_in_record_with_offset(my_ptrdiff_t offset)
 
315
  {
 
316
    if (!null_ptr)
 
317
      return 0;
 
318
    return test(null_ptr[offset] & null_bit);
 
319
  }
 
320
  inline void set_null(my_ptrdiff_t row_offset= 0)
 
321
    { if (null_ptr) null_ptr[row_offset]|= null_bit; }
 
322
  inline void set_notnull(my_ptrdiff_t row_offset= 0)
 
323
    { if (null_ptr) null_ptr[row_offset]&= (unsigned char) ~null_bit; }
 
324
  inline bool maybe_null(void) { return null_ptr != 0 || table->maybe_null; }
 
325
  inline bool real_maybe_null(void) { return null_ptr != 0; }
 
326
 
 
327
  enum {
 
328
    LAST_NULL_BYTE_UNDEF= 0
 
329
  };
 
330
 
 
331
  /*
 
332
    Find the position of the last null byte for the field.
 
333
 
 
334
    SYNOPSIS
 
335
      last_null_byte()
 
336
 
 
337
    DESCRIPTION
 
338
      Return a pointer to the last byte of the null bytes where the
 
339
      field conceptually is placed.
 
340
 
 
341
    RETURN VALUE
 
342
      The position of the last null byte relative to the beginning of
 
343
      the record. If the field does not use any bits of the null
 
344
      bytes, the value 0 (LAST_NULL_BYTE_UNDEF) is returned.
 
345
   */
 
346
  size_t last_null_byte() const {
 
347
    size_t bytes= do_last_null_byte();
 
348
    assert(bytes <= table->getNullBytes());
 
349
    return bytes;
 
350
  }
 
351
 
 
352
  virtual void make_field(Send_field *);
335
353
  virtual void sort_string(unsigned char *buff,uint32_t length)=0;
336
354
  virtual bool optimize_range(uint32_t idx, uint32_t part);
337
 
  /**
338
 
   * Returns true for fields which, when compared with constant
339
 
   * items, can be casted to int64_t. In this case we will at 'fix_fields'
340
 
   * stage cast the constant items to int64_ts and at the execution stage
341
 
   * use field->val_int() for comparison.  Used to optimize clauses like
342
 
   * 'a_column BETWEEN date_const AND date_const'.
343
 
   */
344
 
  virtual bool can_be_compared_as_int64_t() const
345
 
  {
346
 
    return false;
347
 
  }
 
355
  /*
 
356
    This should be true for fields which, when compared with constant
 
357
    items, can be casted to int64_t. In this case we will at 'fix_fields'
 
358
    stage cast the constant items to int64_ts and at the execution stage
 
359
    use field->val_int() for comparison.  Used to optimize clauses like
 
360
    'a_column BETWEEN date_const, date_const'.
 
361
  */
 
362
  virtual bool can_be_compared_as_int64_t() const { return false; }
348
363
  virtual void free() {}
349
 
  virtual Field *new_field(memory::Root *root,
350
 
                           Table *new_table,
 
364
  virtual Field *new_field(MEM_ROOT *root, Table *new_table,
351
365
                           bool keep_type);
352
 
  virtual Field *new_key_field(memory::Root *root, Table *new_table,
353
 
                               unsigned char *new_ptr,
354
 
                               unsigned char *new_null_ptr,
 
366
  virtual Field *new_key_field(MEM_ROOT *root, Table *new_table,
 
367
                               unsigned char *new_ptr, unsigned char *new_null_ptr,
355
368
                               uint32_t new_null_bit);
356
 
  /** This is used to generate a field in Table from TableShare */
357
 
  Field *clone(memory::Root *mem_root, Table *new_table);
 
369
  Field *clone(MEM_ROOT *mem_root, Table *new_table);
358
370
  inline void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
359
371
  {
360
 
    ptr= ptr_arg;
361
 
    null_ptr= null_ptr_arg;
362
 
    null_bit= null_bit_arg;
 
372
    ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
363
373
  }
364
374
  inline void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
365
 
  virtual void move_field_offset(ptrdiff_t ptr_diff)
 
375
  virtual void move_field_offset(my_ptrdiff_t ptr_diff)
366
376
  {
367
 
    ptr= ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
 
377
    ptr=ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
368
378
    if (null_ptr)
369
 
      null_ptr= ADD_TO_PTR(null_ptr,ptr_diff,unsigned char*);
370
 
  }
371
 
  virtual void get_image(unsigned char *buff, uint32_t length, const CHARSET_INFO * const)
372
 
  {
373
 
    memcpy(buff,ptr,length);
374
 
  }
375
 
  virtual void get_image(std::basic_string<unsigned char> &buff, uint32_t length, const CHARSET_INFO * const)
376
 
  {
377
 
    buff.append(ptr,length);
378
 
  }
379
 
  virtual void set_image(const unsigned char *buff,uint32_t length, const CHARSET_INFO * const)
380
 
  {
381
 
    memcpy(ptr,buff,length);
382
 
  }
383
 
 
384
 
  /**
385
 
   * Copy a field part into an output buffer.
386
 
   *
387
 
   * @details
388
 
   *
389
 
   * This function makes a copy of field part of size equal to or
390
 
   * less than "length" parameter value.
391
 
   * For fields of string types (VARCHAR, TEXT) the rest of buffer
392
 
   * is padded by zero byte.
393
 
   *
394
 
   * @param output buffer
395
 
   * @param output buffer size
396
 
   *
397
 
   * @note
398
 
   *
399
 
   * For variable length character fields (i.e. UTF-8) the "length"
400
 
   * parameter means a number of output buffer bytes as if all field
401
 
   * characters have maximal possible size (mbmaxlen). In the other words,
402
 
   * "length" parameter is a number of characters multiplied by
403
 
   * field_charset->mbmaxlen.
404
 
   *
405
 
   * @retval
406
 
   *   Number of copied bytes (excluding padded zero bytes -- see above).
407
 
   */
408
 
  virtual uint32_t get_key_image(unsigned char *buff, uint32_t length)
409
 
  {
410
 
    get_image(buff, length, &my_charset_bin);
411
 
    return length;
412
 
  }
413
 
  virtual uint32_t get_key_image(std::basic_string<unsigned char> &buff, uint32_t length)
 
379
      null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,unsigned char*);
 
380
  }
 
381
  virtual void get_image(unsigned char *buff, uint32_t length,
 
382
                         const CHARSET_INFO * const cs __attribute__((unused)))
 
383
    { memcpy(buff,ptr,length); }
 
384
  virtual void set_image(const unsigned char *buff,uint32_t length,
 
385
                         const CHARSET_INFO * const cs __attribute__((unused)))
 
386
    { memcpy(ptr,buff,length); }
 
387
 
 
388
 
 
389
  /*
 
390
    Copy a field part into an output buffer.
 
391
 
 
392
    SYNOPSIS
 
393
      Field::get_key_image()
 
394
      buff   [out] output buffer
 
395
      length       output buffer size
 
396
      type         itMBR for geometry blobs, otherwise itRAW
 
397
 
 
398
    DESCRIPTION
 
399
      This function makes a copy of field part of size equal to or
 
400
      less than "length" parameter value.
 
401
      For fields of string types (CHAR, VARCHAR, TEXT) the rest of buffer
 
402
      is padded by zero byte.
 
403
 
 
404
    NOTES
 
405
      For variable length character fields (i.e. UTF-8) the "length"
 
406
      parameter means a number of output buffer bytes as if all field
 
407
      characters have maximal possible size (mbmaxlen). In the other words,
 
408
      "length" parameter is a number of characters multiplied by
 
409
      field_charset->mbmaxlen.
 
410
 
 
411
    RETURN
 
412
      Number of copied bytes (excluding padded zero bytes -- see above).
 
413
  */
 
414
 
 
415
  virtual uint32_t get_key_image(unsigned char *buff, uint32_t length,
 
416
                             imagetype type __attribute__((unused)))
414
417
  {
415
418
    get_image(buff, length, &my_charset_bin);
416
419
    return length;
417
420
  }
418
421
  virtual void set_key_image(const unsigned char *buff,uint32_t length)
419
 
  {
420
 
    set_image(buff,length, &my_charset_bin);
421
 
  }
 
422
    { set_image(buff,length, &my_charset_bin); }
422
423
  inline int64_t val_int_offset(uint32_t row_offset)
423
 
  {
424
 
    ptr+=row_offset;
425
 
    int64_t tmp=val_int();
426
 
    ptr-=row_offset;
427
 
    return tmp;
428
 
  }
429
 
 
 
424
    {
 
425
      ptr+=row_offset;
 
426
      int64_t tmp=val_int();
 
427
      ptr-=row_offset;
 
428
      return tmp;
 
429
    }
430
430
  inline int64_t val_int(const unsigned char *new_ptr)
431
431
  {
432
432
    unsigned char *old_ptr= ptr;
433
433
    int64_t return_value;
434
 
    ptr= const_cast<unsigned char*>(new_ptr);
 
434
    ptr= (unsigned char*) new_ptr;
435
435
    return_value= val_int();
436
436
    ptr= old_ptr;
437
437
    return return_value;
439
439
  inline String *val_str(String *str, const unsigned char *new_ptr)
440
440
  {
441
441
    unsigned char *old_ptr= ptr;
442
 
    ptr= const_cast<unsigned char*>(new_ptr);
 
442
    ptr= (unsigned char*) new_ptr;
443
443
    val_str(str);
444
444
    ptr= old_ptr;
445
445
    return str;
446
446
  }
447
 
 
448
 
  /**
449
 
    Pack the field into a format suitable for storage and transfer.
450
 
 
451
 
    To implement packing functionality, only the virtual function
452
 
    should be overridden. The other functions are just convenience
453
 
    functions and hence should not be overridden.
454
 
 
455
 
    The value of <code>low_byte_first</code> is dependent on how the
456
 
    packed data is going to be used: for local use, e.g., temporary
457
 
    store on disk or in memory, use the native format since that is
458
 
    faster. For data that is going to be transfered to other machines
459
 
    (e.g., when writing data to the binary log), data should always be
460
 
    stored in little-endian format.
461
 
 
462
 
    @note The default method for packing fields just copy the raw bytes
463
 
    of the record into the destination, but never more than
464
 
    <code>max_length</code> characters.
465
 
 
466
 
    @param to
467
 
    Pointer to memory area where representation of field should be put.
468
 
 
469
 
    @param from
470
 
    Pointer to memory area where record representation of field is
471
 
    stored.
472
 
 
473
 
    @param max_length
474
 
    Maximum length of the field, as given in the column definition. For
475
 
    example, for <code>CHAR(1000)</code>, the <code>max_length</code>
476
 
    is 1000. This information is sometimes needed to decide how to pack
477
 
    the data.
478
 
 
479
 
    @param low_byte_first
480
 
    @c true if integers should be stored little-endian, @c false if
481
 
    native format should be used. Note that for little-endian machines,
482
 
    the value of this flag is a moot point since the native format is
483
 
    little-endian.
484
 
  */
485
 
  virtual unsigned char *pack(unsigned char *to,
486
 
                              const unsigned char *from,
487
 
                              uint32_t max_length,
488
 
                              bool low_byte_first);
489
 
 
490
 
  unsigned char *pack(unsigned char *to, const unsigned char *from);
491
 
 
492
 
  /**
493
 
    Unpack a field from row data.
494
 
 
495
 
    This method is used to unpack a field from a master whose size of
496
 
    the field is less than that of the slave.
497
 
 
498
 
    The <code>param_data</code> parameter is a two-byte integer (stored
499
 
    in the least significant 16 bits of the unsigned integer) usually
500
 
    consisting of two parts: the real type in the most significant byte
501
 
    and a original pack length in the least significant byte.
502
 
 
503
 
    The exact layout of the <code>param_data</code> field is given by
504
 
    the <code>Table_map_log_event::save_field_metadata()</code>.
505
 
 
506
 
    This is the default method for unpacking a field. It just copies
507
 
    the memory block in byte order (of original pack length bytes or
508
 
    length of field, whichever is smaller).
509
 
 
510
 
    @param   to         Destination of the data
511
 
    @param   from       Source of the data
512
 
    @param   param_data Real type and original pack length of the field
513
 
                        data
514
 
 
515
 
    @param low_byte_first
516
 
    If this flag is @c true, all composite entities (e.g., lengths)
517
 
    should be unpacked in little-endian format; otherwise, the entities
518
 
    are unpacked in native order.
519
 
 
520
 
    @return  New pointer into memory based on from + length of the data
521
 
  */
522
 
  virtual const unsigned char *unpack(unsigned char* to,
523
 
                                      const unsigned char *from,
524
 
                                      uint32_t param_data,
525
 
                                      bool low_byte_first);
526
 
  /**
527
 
     @overload Field::unpack(unsigned char*, const unsigned char*,
528
 
                             uint32_t, bool)
529
 
  */
530
 
  const unsigned char *unpack(unsigned char* to,
531
 
                              const unsigned char *from);
532
 
 
533
 
  virtual unsigned char *pack_key(unsigned char* to,
534
 
                                  const unsigned char *from,
535
 
                                  uint32_t max_length,
536
 
                                  bool low_byte_first)
537
 
  {
538
 
    return pack(to, from, max_length, low_byte_first);
539
 
  }
540
 
  virtual const unsigned char *unpack_key(unsigned char* to,
541
 
                                          const unsigned char *from,
542
 
                                          uint32_t max_length,
543
 
                                          bool low_byte_first)
 
447
  virtual bool send_binary(Protocol *protocol);
 
448
 
 
449
  virtual unsigned char *pack(unsigned char *to, const unsigned char *from,
 
450
                      uint32_t max_length, bool low_byte_first);
 
451
  /**
 
452
     @overload Field::pack(unsigned char*, const unsigned char*, uint32_t, bool)
 
453
  */
 
454
  unsigned char *pack(unsigned char *to, const unsigned char *from)
 
455
  {
 
456
    unsigned char *result= this->pack(to, from, UINT_MAX, table->s->db_low_byte_first);
 
457
    return(result);
 
458
  }
 
459
 
 
460
  virtual const unsigned char *unpack(unsigned char* to, const unsigned char *from,
 
461
                              uint32_t param_data, bool low_byte_first);
 
462
  /**
 
463
     @overload Field::unpack(unsigned char*, const unsigned char*, uint32_t, bool)
 
464
  */
 
465
  const unsigned char *unpack(unsigned char* to, const unsigned char *from)
 
466
  {
 
467
    const unsigned char *result= unpack(to, from, 0U, table->s->db_low_byte_first);
 
468
    return(result);
 
469
  }
 
470
 
 
471
  virtual unsigned char *pack_key(unsigned char* to, const unsigned char *from,
 
472
                          uint32_t max_length, bool low_byte_first)
 
473
  {
 
474
    return pack(to, from, max_length, low_byte_first);
 
475
  }
 
476
  virtual unsigned char *pack_key_from_key_image(unsigned char* to, const unsigned char *from,
 
477
                                        uint32_t max_length, bool low_byte_first)
 
478
  {
 
479
    return pack(to, from, max_length, low_byte_first);
 
480
  }
 
481
  virtual const unsigned char *unpack_key(unsigned char* to, const unsigned char *from,
 
482
                                  uint32_t max_length, bool low_byte_first)
544
483
  {
545
484
    return unpack(to, from, max_length, low_byte_first);
546
485
  }
 
486
  virtual uint32_t packed_col_length(const unsigned char *to __attribute__((unused)),
 
487
                                 uint32_t length)
 
488
  { return length;}
547
489
  virtual uint32_t max_packed_col_length(uint32_t max_length)
548
 
  {
549
 
    return max_length;
550
 
  }
 
490
  { return max_length;}
551
491
 
552
 
  inline uint32_t offset(const unsigned char *record)
 
492
  virtual int pack_cmp(const unsigned char *a,const unsigned char *b,
 
493
                       uint32_t key_length_arg __attribute__((unused)),
 
494
                       bool insert_or_update __attribute__((unused)))
 
495
  { return cmp(a,b); }
 
496
  virtual int pack_cmp(const unsigned char *b,
 
497
                       uint32_t key_length_arg __attribute__((unused)),
 
498
                       bool insert_or_update __attribute__((unused)))
 
499
  { return cmp(ptr,b); }
 
500
  uint32_t offset(unsigned char *record)
553
501
  {
554
502
    return (uint32_t) (ptr - record);
555
503
  }
556
504
  void copy_from_tmp(int offset);
557
 
  uint32_t fill_cache_field(CacheField *copy);
 
505
  uint32_t fill_cache_field(struct st_cache_field *copy);
558
506
  virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
559
507
  virtual bool get_time(DRIZZLE_TIME *ltime);
560
508
  virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
561
509
  virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
562
510
  virtual bool has_charset(void) const { return false; }
563
 
  virtual void set_charset(const CHARSET_INFO * const)
564
 
  {}
 
511
  virtual void set_charset(const CHARSET_INFO * const charset_arg __attribute__((unused)))
 
512
  { }
565
513
  virtual enum Derivation derivation(void) const
566
 
  {
567
 
    return DERIVATION_IMPLICIT;
568
 
  }
569
 
  virtual void set_derivation(enum Derivation)
570
 
  {}
571
 
  /**
572
 
    Produce warning or note about data saved into field.
573
 
 
574
 
    @param level            - level of message (Note/Warning/Error)
575
 
    @param code             - error code of message to be produced
576
 
    @param cuted_increment  - whenever we should increase cut fields count or not
577
 
 
578
 
    @note
579
 
      This function won't produce warning and increase cut fields counter
580
 
      if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
581
 
 
582
 
      if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
583
 
      This allows us to avoid notes in optimisation, like convert_constant_item().
584
 
 
585
 
    @retval
586
 
      1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
587
 
    @retval
588
 
      0 otherwise
589
 
  */
590
 
  bool set_warning(DRIZZLE_ERROR::enum_warning_level,
591
 
                   unsigned int code,
 
514
  { return DERIVATION_IMPLICIT; }
 
515
  virtual void set_derivation(enum Derivation derivation_arg __attribute__((unused)))
 
516
  { }
 
517
  bool set_warning(DRIZZLE_ERROR::enum_warning_level, unsigned int code,
592
518
                   int cuted_increment);
593
 
  /**
594
 
    Produce warning or note about datetime string data saved into field.
595
 
 
596
 
    @param level            level of message (Note/Warning/Error)
597
 
    @param code             error code of message to be produced
598
 
    @param str              string value which we tried to save
599
 
    @param str_length       length of string which we tried to save
600
 
    @param ts_type          type of datetime value (datetime/date/time)
601
 
    @param cuted_increment  whenever we should increase cut fields count or not
602
 
 
603
 
    @note
604
 
      This function will always produce some warning but won't increase cut
605
 
      fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
606
 
      thread.
607
 
  */
608
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
609
 
                            uint32_t code,
610
 
                            const char *str,
611
 
                            uint32_t str_len,
612
 
                            enum enum_drizzle_timestamp_type ts_type,
613
 
                            int cuted_increment);
614
 
  /**
615
 
    Produce warning or note about integer datetime value saved into field.
616
 
 
617
 
    @param level            level of message (Note/Warning/Error)
618
 
    @param code             error code of message to be produced
619
 
    @param nr               numeric value which we tried to save
620
 
    @param ts_type          type of datetime value (datetime/date/time)
621
 
    @param cuted_increment  whenever we should increase cut fields count or not
622
 
 
623
 
    @note
624
 
      This function will always produce some warning but won't increase cut
625
 
      fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
626
 
      thread.
627
 
  */
628
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
629
 
                            uint32_t code,
630
 
                            int64_t nr,
631
 
                            enum enum_drizzle_timestamp_type ts_type,
632
 
                            int cuted_increment);
633
 
  /**
634
 
    Produce warning or note about double datetime data saved into field.
635
 
 
636
 
    @param level            level of message (Note/Warning/Error)
637
 
    @param code             error code of message to be produced
638
 
    @param nr               double value which we tried to save
639
 
    @param ts_type          type of datetime value (datetime/date/time)
640
 
 
641
 
    @note
642
 
      This function will always produce some warning but won't increase cut
643
 
      fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
644
 
      thread.
645
 
  */
646
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
647
 
                            const uint32_t code,
648
 
                            double nr,
649
 
                            enum enum_drizzle_timestamp_type ts_type);
 
519
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, uint32_t code, 
 
520
                            const char *str, uint32_t str_len,
 
521
                            enum enum_drizzle_timestamp_type ts_type, int cuted_increment);
 
522
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, uint32_t code, 
 
523
                            int64_t nr, enum enum_drizzle_timestamp_type ts_type,
 
524
                            int cuted_increment);
 
525
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, const uint32_t code, 
 
526
                            double nr, enum enum_drizzle_timestamp_type ts_type);
650
527
  inline bool check_overflow(int op_result)
651
528
  {
652
529
    return (op_result == E_DEC_OVERFLOW);
653
530
  }
654
 
  /**
655
 
    Process decimal library return codes and issue warnings for overflow and
656
 
    truncation.
657
 
 
658
 
    @param op_result  decimal library return code (E_DEC_* see include/decimal.h)
659
 
 
660
 
    @retval
661
 
      E_DEC_OVERFLOW   there was overflow
662
 
      E_DEC_TRUNCATED  there was truncation
663
 
    @retval
664
 
      0  no error or there was some other error except overflow or truncation
665
 
  */
666
531
  int warn_if_overflow(int op_result);
667
 
  void init(Table *table_arg);
 
532
  void init(Table *table_arg)
 
533
  {
 
534
    orig_table= table= table_arg;
 
535
    table_name= &table_arg->alias;
 
536
  }
668
537
 
669
538
  /* maximum possible display length */
670
539
  virtual uint32_t max_display_length()= 0;
671
540
 
672
 
  virtual uint32_t is_equal(CreateField *new_field);
673
 
  /**
674
 
    Conversion from decimal to int64_t with checking overflow and
675
 
    setting correct value (min/max) in case of overflow.
676
 
 
677
 
    @param val             value which have to be converted
678
 
    @param unsigned_flag   type of integer in which we convert val
679
 
    @param err             variable to pass error code
680
 
 
681
 
    @return
682
 
      value converted from val
683
 
  */
684
 
  int64_t convert_decimal2int64_t(const my_decimal *val,
685
 
                                  bool unsigned_flag,
686
 
                                  int *err);
 
541
  virtual uint32_t is_equal(Create_field *new_field);
 
542
  /* convert decimal to int64_t with overflow check */
 
543
  int64_t convert_decimal2int64_t(const my_decimal *val, bool unsigned_flag,
 
544
                                    int *err);
687
545
  /* The max. number of characters */
688
546
  inline uint32_t char_length() const
689
547
  {
698
556
 
699
557
  /* Hash value */
700
558
  virtual void hash(uint32_t *nr, uint32_t *nr2);
701
 
  friend bool reopen_table(Session *,Table *,bool);
702
 
 
703
 
  friend class CopyField;
 
559
  friend bool reopen_table(THD *,Table *,bool);
 
560
  friend int cre_myisam(char * name, register Table *form, uint32_t options,
 
561
                        uint64_t auto_increment_value);
 
562
  friend class Copy_field;
704
563
  friend class Item_avg_field;
705
564
  friend class Item_std_field;
706
565
  friend class Item_sum_num;
713
572
  friend class Item_sum_max;
714
573
  friend class Item_func_group_concat;
715
574
 
716
 
  bool isReadSet();
717
 
  bool isWriteSet();
718
 
  void setReadSet(bool arg= true);
719
 
  void setWriteSet(bool arg= true);
720
 
};
721
 
 
722
 
} /* namespace drizzled */
723
 
 
724
 
/** @TODO Why is this in the middle of the file???*/
725
 
#include "drizzled/create_field.h"
726
 
 
727
 
namespace drizzled
728
 
{
 
575
private:
 
576
  /*
 
577
    Primitive for implementing last_null_byte().
 
578
 
 
579
    SYNOPSIS
 
580
      do_last_null_byte()
 
581
 
 
582
    DESCRIPTION
 
583
      Primitive for the implementation of the last_null_byte()
 
584
      function. This represents the inheritance interface and can be
 
585
      overridden by subclasses.
 
586
   */
 
587
  virtual size_t do_last_null_byte() const;
729
588
 
730
589
/**
731
 
 * A class for sending field information to a client.
732
 
 *
733
 
 * @details
734
 
 *
735
 
 * Send_field is basically a stripped-down POD class for
736
 
 * representing basic information about a field...
737
 
 */
738
 
class SendField
 
590
   Retrieve the field metadata for fields.
 
591
 
 
592
   This default implementation returns 0 and saves 0 in the metadata_ptr
 
593
   value.
 
594
 
 
595
   @param   metadata_ptr   First byte of field metadata
 
596
 
 
597
   @returns 0 no bytes written.
 
598
*/
 
599
  virtual int do_save_field_metadata(unsigned char *metadata_ptr __attribute__((unused)))
 
600
  { return 0; }
 
601
};
 
602
 
 
603
/*
 
604
  Create field class for CREATE TABLE
 
605
*/
 
606
 
 
607
class Create_field :public Sql_alloc
739
608
{
740
609
public:
 
610
  const char *field_name;
 
611
  const char *change;                   // If done with alter table
 
612
  const char *after;                    // Put column after this one
 
613
  LEX_STRING comment;                   // Comment for field
 
614
  Item  *def;                           // Default value
 
615
  enum  enum_field_types sql_type;
 
616
  /*
 
617
    At various stages in execution this can be length of field in bytes or
 
618
    max number of characters. 
 
619
  */
 
620
  uint32_t length;
 
621
  /*
 
622
    The value of `length' as set by parser: is the number of characters
 
623
    for most of the types, or of bytes for BLOBs or numeric types.
 
624
  */
 
625
  uint32_t char_length;
 
626
  uint32_t  decimals, flags, pack_length, key_length;
 
627
  Field::utype unireg_check;
 
628
  TYPELIB *interval;                    // Which interval to use
 
629
  TYPELIB *save_interval;               // Temporary copy for the above
 
630
                                        // Used only for UCS2 intervals
 
631
  List<String> interval_list;
 
632
  const CHARSET_INFO *charset;
 
633
  Field *field;                         // For alter table
 
634
 
 
635
  uint8_t row,col,sc_length,interval_id;        // For rea_create_table
 
636
  uint32_t      offset,pack_flag;
 
637
 
 
638
  /* Virtual column expression statement */
 
639
  virtual_column_info *vcol_info;
 
640
  /*
 
641
    Indication that the field is phycically stored in tables 
 
642
    rather than just generated on SQL queries.
 
643
    As of now, FALSE can only be set for generated-only virtual columns.
 
644
  */
 
645
  bool is_stored;
 
646
 
 
647
  Create_field() :after(0) {}
 
648
  Create_field(Field *field, Field *orig_field);
 
649
  /* Used to make a clone of this object for ALTER/CREATE TABLE */
 
650
  Create_field *clone(MEM_ROOT *mem_root) const
 
651
    { return new (mem_root) Create_field(*this); }
 
652
  void create_length_to_internal_length(void);
 
653
 
 
654
  inline enum column_format_type column_format() const
 
655
  {
 
656
    return (enum column_format_type)
 
657
      ((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
 
658
  }
 
659
 
 
660
  /* Init for a tmp table field. To be extended if need be. */
 
661
  void init_for_tmp_table(enum_field_types sql_type_arg,
 
662
                          uint32_t max_length, uint32_t decimals,
 
663
                          bool maybe_null, bool is_unsigned);
 
664
 
 
665
  bool init(THD *thd, char *field_name, enum_field_types type, char *length,
 
666
            char *decimals, uint32_t type_modifier, Item *default_value,
 
667
            Item *on_update_value, LEX_STRING *comment, char *change,
 
668
            List<String> *interval_list, const CHARSET_INFO * const cs,
 
669
            uint32_t uint_geom_type,
 
670
            enum column_format_type column_format,
 
671
            virtual_column_info *vcol_info);
 
672
};
 
673
 
 
674
 
 
675
/*
 
676
  A class for sending info to the client
 
677
*/
 
678
 
 
679
class Send_field {
 
680
 public:
741
681
  const char *db_name;
742
 
  const char *table_name;
743
 
  const char *org_table_name;
744
 
  const char *col_name;
745
 
  const char *org_col_name;
 
682
  const char *table_name,*org_table_name;
 
683
  const char *col_name,*org_col_name;
746
684
  uint32_t length;
747
 
  uint32_t charsetnr;
748
 
  uint32_t flags;
749
 
  uint32_t decimals;
 
685
  uint32_t charsetnr, flags, decimals;
750
686
  enum_field_types type;
751
 
  SendField() {}
 
687
  Send_field() {}
752
688
};
753
689
 
754
 
/**
755
 
 * A class for quick copying data to fields
756
 
 */
757
 
class CopyField :public memory::SqlAlloc
758
 
{
 
690
 
 
691
/*
 
692
  A class for quick copying data to fields
 
693
*/
 
694
 
 
695
class Copy_field :public Sql_alloc {
759
696
  /**
760
697
    Convenience definition of a copy function returned by
761
698
    get_copy_func.
762
699
  */
763
 
  typedef void Copy_func(CopyField*);
 
700
  typedef void Copy_func(Copy_field*);
764
701
  Copy_func *get_copy_func(Field *to, Field *from);
765
702
public:
766
 
  unsigned char *from_ptr;
767
 
  unsigned char *to_ptr;
768
 
  unsigned char *from_null_ptr;
769
 
  unsigned char *to_null_ptr;
 
703
  unsigned char *from_ptr,*to_ptr;
 
704
  unsigned char *from_null_ptr,*to_null_ptr;
770
705
  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;
 
706
  uint32_t      from_bit,to_bit;
 
707
  uint32_t from_length,to_length;
 
708
  Field *from_field,*to_field;
777
709
  String tmp;                                   // For items
778
710
 
779
 
  CopyField() {}
780
 
  ~CopyField() {}
781
 
  void set(Field *to,Field *from,bool save);    // Field to field
 
711
  Copy_field() {}
 
712
  ~Copy_field() {}
 
713
  void set(Field *to,Field *from,bool save);    // Field to field 
782
714
  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
 
715
  void (*do_copy)(Copy_field *);
 
716
  void (*do_copy2)(Copy_field *);               // Used to handle null values
785
717
};
786
718
 
 
719
 
 
720
Field *make_field(TABLE_SHARE *share, unsigned char *ptr, uint32_t field_length,
 
721
                  unsigned char *null_pos, unsigned char null_bit,
 
722
                  uint32_t pack_flag, enum_field_types field_type,
 
723
                  const CHARSET_INFO * cs,
 
724
                  Field::utype unireg_check,
 
725
                  TYPELIB *interval, const char *field_name);
787
726
uint32_t pack_length_to_packflag(uint32_t type);
 
727
enum_field_types get_blob_type_from_length(uint32_t length);
788
728
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
789
729
int set_field_to_null(Field *field);
790
730
int set_field_to_null_with_conversions(Field *field, bool no_conversions);
791
731
 
792
 
/**
793
 
 * Tests if the given string contains important data:
794
 
 * not spaces for character string, or any data for binary string.
795
 
 *
796
 
 * @param pointer to the character set to use
797
 
 * @param String to test
798
 
 * @param String end
799
 
 *
800
 
 * @retval
801
 
 *  false - If string does not have important data
802
 
 * @retval
803
 
 *  true  - If string has some important data
 
732
 
 
733
bool
 
734
test_if_important_data(const CHARSET_INFO * const cs, 
 
735
                       const char *str,
 
736
                       const char *strend);
 
737
 
 
738
/*
 
739
  Field subclasses
804
740
 */
805
 
bool test_if_important_data(const CHARSET_INFO * const cs,
806
 
                            const char *str,
807
 
                            const char *strend);
808
 
 
809
 
} /* namespace drizzled */
810
 
 
811
 
#endif /* DRIZZLED_FIELD_H */
 
741
#include <drizzled/field/str.h>
 
742
#include <drizzled/field/longstr.h>
 
743
#include <drizzled/field/num.h>
 
744
#include <drizzled/field/blob.h>
 
745
#include <drizzled/field/enum.h>
 
746
#include <drizzled/field/null.h>
 
747
#include <drizzled/field/date.h>
 
748
#include <drizzled/field/fdecimal.h>
 
749
#include <drizzled/field/real.h>
 
750
#include <drizzled/field/double.h>
 
751
#include <drizzled/field/long.h>
 
752
#include <drizzled/field/int64_t.h>
 
753
#include <drizzled/field/num.h>
 
754
#include <drizzled/field/timetype.h>
 
755
#include <drizzled/field/timestamp.h>
 
756
#include <drizzled/field/datetime.h>
 
757
#include <drizzled/field/fstring.h>
 
758
#include <drizzled/field/varstring.h>
 
759
 
 
760
/*
 
761
  The following are for the interface with the .frm file
 
762
*/
 
763
 
 
764
#define FIELDFLAG_DECIMAL               1
 
765
#define FIELDFLAG_BINARY                1       // Shares same flag
 
766
#define FIELDFLAG_NUMBER                2
 
767
#define FIELDFLAG_DECIMAL_POSITION      4
 
768
#define FIELDFLAG_PACK                  120     // Bits used for packing
 
769
#define FIELDFLAG_INTERVAL              256     // mangled with decimals!
 
770
#define FIELDFLAG_BITFIELD              512     // mangled with decimals!
 
771
#define FIELDFLAG_BLOB                  1024    // mangled with decimals!
 
772
#define FIELDFLAG_GEOM                  2048    // mangled with decimals!
 
773
 
 
774
#define FIELDFLAG_TREAT_BIT_AS_CHAR     4096    /* use Field_bit_as_char */
 
775
 
 
776
#define FIELDFLAG_LEFT_FULLSCREEN       8192
 
777
#define FIELDFLAG_RIGHT_FULLSCREEN      16384
 
778
#define FIELDFLAG_FORMAT_NUMBER         16384   // predit: ###,,## in output
 
779
#define FIELDFLAG_NO_DEFAULT            16384   /* sql */
 
780
#define FIELDFLAG_SUM                   ((uint32_t) 32768)// predit: +#fieldflag
 
781
#define FIELDFLAG_MAYBE_NULL            ((uint32_t) 32768)// sql
 
782
#define FIELDFLAG_HEX_ESCAPE            ((uint32_t) 0x10000)
 
783
#define FIELDFLAG_PACK_SHIFT            3
 
784
#define FIELDFLAG_DEC_SHIFT             8
 
785
#define FIELDFLAG_MAX_DEC               31
 
786
#define FIELDFLAG_NUM_SCREEN_TYPE       0x7F01
 
787
#define FIELDFLAG_ALFA_SCREEN_TYPE      0x7800
 
788
 
 
789
#define MTYP_TYPENR(type) (type & 127)  /* Remove bits from type */
 
790
 
 
791
#define f_is_dec(x)             ((x) & FIELDFLAG_DECIMAL)
 
792
#define f_is_num(x)             ((x) & FIELDFLAG_NUMBER)
 
793
#define f_is_decimal_precision(x)       ((x) & FIELDFLAG_DECIMAL_POSITION)
 
794
#define f_is_packed(x)          ((x) & FIELDFLAG_PACK)
 
795
#define f_packtype(x)           (((x) >> FIELDFLAG_PACK_SHIFT) & 15)
 
796
#define f_decimals(x)           ((uint8_t) (((x) >> FIELDFLAG_DEC_SHIFT) & FIELDFLAG_MAX_DEC))
 
797
#define f_is_alpha(x)           (!f_is_num(x))
 
798
#define f_is_binary(x)          ((x) & FIELDFLAG_BINARY) // 4.0- compatibility
 
799
#define f_is_enum(x)            (((x) & (FIELDFLAG_INTERVAL | FIELDFLAG_NUMBER)) == FIELDFLAG_INTERVAL)
 
800
#define f_is_bitfield(x)        (((x) & (FIELDFLAG_BITFIELD | FIELDFLAG_NUMBER)) == FIELDFLAG_BITFIELD)
 
801
#define f_is_blob(x)            (((x) & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB)
 
802
#define f_is_equ(x)             ((x) & (1+2+FIELDFLAG_PACK+31*256))
 
803
#define f_settype(x)            (((int) x) << FIELDFLAG_PACK_SHIFT)
 
804
#define f_maybe_null(x)         (x & FIELDFLAG_MAYBE_NULL)
 
805
#define f_no_default(x)         (x & FIELDFLAG_NO_DEFAULT)
 
806
#define f_bit_as_char(x)        ((x) & FIELDFLAG_TREAT_BIT_AS_CHAR)
 
807
#define f_is_hex_escape(x)      ((x) & FIELDFLAG_HEX_ESCAPE)
 
808
 
 
809
bool
 
810
check_string_copy_error(Field_str *field,
 
811
                        const char *well_formed_error_pos,
 
812
                        const char *cannot_convert_error_pos,
 
813
                        const char *end,
 
814
                        const CHARSET_INFO * const cs);