~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: Monty
  • Date: 2008-10-02 05:41:33 UTC
  • mfrom: (398.1.10 codestyle)
  • Revision ID: mordred@scylla.inaugust.com-20081002054133-tyxv5bmqpazfaqqi
Merged up to 408 of stdint-includes-fix.

Show diffs side-by-side

added added

removed removed

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