~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: Stewart Smith
  • Author(s): Marko Mäkelä
  • Date: 2010-12-20 03:21:44 UTC
  • mto: (2021.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 2022.
  • Revision ID: stewart@flamingspork.com-20101220032144-7aqh2z403u7d7bdp
Merge Revision revid:marko.makela@oracle.com-20101104131215-pfxnpidlrzd4krg0 from MySQL InnoDB

Original revid:marko.makela@oracle.com-20101104131215-pfxnpidlrzd4krg0

Original Authors: Marko Mäkelä <marko.makela@oracle.com>
Original commit message:
row_ins_index_entry(): Note that only CREATE INDEX sets foreign=FALSE.

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