~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: Brian Aker
  • Date: 2009-01-24 09:43:35 UTC
  • Revision ID: brian@gir-3.local-20090124094335-6qdtvc35gl5fvivz
Adding in an example singe thread scheduler

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