~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: Eric Lambert
  • Date: 2009-06-11 21:19:36 UTC
  • mto: (1061.1.1 merge-all)
  • mto: This revision was merged to the branch mainline in revision 1062.
  • Revision ID: eric.d.lambert@gmail.com-20090611211936-g2gij43xakupz1dw
-removed rm_dir_w_symlink method call with rmdir since dir should not be a sym link in the first place.

Show diffs side-by-side

added added

removed removed

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