~ubuntu-branches/ubuntu/trusty/drizzle/trusty

1 by Monty Taylor
Import upstream version 2010.03.1347
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
1 by Monty Taylor
Import upstream version 2010.03.1347
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
20
/*
21
  Because of the function new_field() all field classes that have static
22
  variables must declare the size_of() member function.
23
*/
24
1.2.7 by Monty Taylor
Import upstream version 2011.02.09
25
26
1.1.2 by Monty Taylor
Import upstream version 2011.03.13
27
#pragma once
28
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
29
#include <drizzled/common_fwd.h>
1.2.9 by Monty Taylor
Import upstream version 2011.03.11
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>
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
35
#include <drizzled/charset.h>
1.2.9 by Monty Taylor
Import upstream version 2011.03.11
36
#include <drizzled/item_result.h>
1 by Monty Taylor
Import upstream version 2010.03.1347
37
38
#include <string>
39
#include <vector>
40
1.2.9 by Monty Taylor
Import upstream version 2011.03.11
41
#include <drizzled/visibility.h>
1.2.7 by Monty Taylor
Import upstream version 2011.02.09
42
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
43
namespace drizzled {
1 by Monty Taylor
Import upstream version 2010.03.1347
44
45
#define DATETIME_DEC                     6
46
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
47
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
48
#define ASSERT_COLUMN_MARKED_FOR_READ assert(getTable() && (not getTable()->read_set || isReadSet()))
49
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(getTable() && (not getTable()->write_set || isWriteSet()))
1 by Monty Taylor
Import upstream version 2010.03.1347
50
51
const uint32_t max_field_size= (uint32_t) 4294967295U;
52
53
int field_conv(Field *to,Field *from);
54
55
/**
56
 * Class representing a Field in a Table
57
 *
58
 * @details
59
 *
60
 * The value stored in the Field object is stored in the
61
 * unsigned char pointer member variable called ptr.  The
62
 * val_xxx() methods retrieve this raw byte value and
63
 * convert the byte into the appropriate output (int, decimal, etc).
64
 *
65
 * The store_xxx() methods take various input and convert
66
 * the input into the raw bytes stored in the ptr member variable.
67
 */
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
68
class DRIZZLED_API Field : boost::noncopyable
1 by Monty Taylor
Import upstream version 2010.03.1347
69
{
70
public:
71
  unsigned char *ptr; /**< Position to field in record. Stores raw field value */
72
  unsigned char *null_ptr; /**< Byte where null_bit is */
73
74
  /**
75
   * Pointer to the Table object containing this Field
76
   *
77
   * @note You can use table->in_use as replacement for current_session member
78
   * only inside of val_*() and store() members (e.g. you can't use it in cons)
79
   */
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
80
private:
1 by Monty Taylor
Import upstream version 2010.03.1347
81
  Table *table;
1.2.6 by Monty Taylor
Import upstream version 2011.01.08
82
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
83
public:
84
  Table *getTable()
85
  {
86
    assert(table);
87
    return table;
88
  }
89
90
  Table *getTable() const
91
  {
92
    assert(table);
93
    return table;
94
  }
95
96
  void setTable(Table *table_arg)
97
  {
98
    table= table_arg;
99
  }
100
1 by Monty Taylor
Import upstream version 2010.03.1347
101
  Table *orig_table; /**< Pointer to the original Table. @TODO What is "the original table"? */
102
  const char *field_name; /**< Name of the field */
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
103
  str_ref comment; /**< A comment about the field */
1 by Monty Taylor
Import upstream version 2010.03.1347
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;
11 by Monty Taylor
* Fixed missing build depends.
131
132
  bool isUnsigned() const
133
  {
134
    return flags & UNSIGNED_FLAG;
135
  }
136
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
137
private:
1 by Monty Taylor
Import upstream version 2010.03.1347
138
  uint16_t field_index; /**< Index of this Field in Table::fields array */
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
139
140
public:
141
142
  uint16_t position() const
143
  {
144
    return field_index;
145
  }
146
147
  void setPosition(uint32_t arg)
148
  {
149
    field_index= arg;
150
  }
151
1 by Monty Taylor
Import upstream version 2010.03.1347
152
  unsigned char null_bit; /**< Bit used to test null bit */
153
  /**
154
     If true, this field was created in create_tmp_field_from_item from a NULL
155
     value. This means that the type of the field is just a guess, and the type
156
     may be freely coerced to another type.
157
158
     @see create_tmp_field_from_item
159
     @see Item_type_holder::get_real_type
160
   */
161
  bool is_created_from_null_item;
162
163
  static void *operator new(size_t size);
164
  static void *operator new(size_t size, memory::Root *mem_root);
165
  static void operator delete(void *, size_t)
166
  { }
1.2.1 by Monty Taylor
Import upstream version 2010.11.03
167
  static void operator delete(void *, memory::Root *)
168
  { }
1 by Monty Taylor
Import upstream version 2010.03.1347
169
170
  Field(unsigned char *ptr_arg,
171
        uint32_t length_arg,
172
        unsigned char *null_ptr_arg,
173
        unsigned char null_bit_arg,
174
        utype unireg_check_arg,
175
        const char *field_name_arg);
176
  virtual ~Field() {}
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
177
178
  bool hasDefault() const
179
  {
180
    return not (flags & NO_DEFAULT_VALUE_FLAG);
181
  }
182
1 by Monty Taylor
Import upstream version 2010.03.1347
183
  /* Store functions returns 1 on overflow and -1 on fatal error */
184
  virtual int store(const char *to,
185
                    uint32_t length,
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
186
                    const charset_info_st * const cs)=0;
1 by Monty Taylor
Import upstream version 2010.03.1347
187
  virtual int store(double nr)=0;
188
  virtual int store(int64_t nr, bool unsigned_val)=0;
11 by Monty Taylor
* Fixed missing build depends.
189
  virtual int store_decimal(const type::Decimal *d)=0;
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
190
  int store_and_check(enum_check_fields check_level,
191
                      const char *to,
192
                      uint32_t length,
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
193
                      const charset_info_st * const cs);
1 by Monty Taylor
Import upstream version 2010.03.1347
194
  /**
195
    This is called when storing a date in a string.
196
197
    @note
198
      Needs to be changed if/when we want to support different time formats.
199
  */
1.2.7 by Monty Taylor
Import upstream version 2011.02.09
200
  virtual int store_time(type::Time &ltime, type::timestamp_t t_type);
1.2.9 by Monty Taylor
Import upstream version 2011.03.11
201
  virtual double val_real() const=0;
202
  virtual int64_t val_int() const =0;
203
  virtual type::Decimal *val_decimal(type::Decimal *) const;
204
  String *val_str_internal(String *str) const
1 by Monty Taylor
Import upstream version 2010.03.1347
205
  {
206
    return val_str(str, str);
207
  }
1.2.8 by Monty Taylor
Import upstream version 2011.02.10
208
1 by Monty Taylor
Import upstream version 2010.03.1347
209
  /*
210
     val_str(buf1, buf2) gets two buffers and should use them as follows:
211
     if it needs a temp buffer to convert result to string - use buf1
212
       example Field_tiny::val_str()
213
     if the value exists as a string already - use buf2
214
       example Field_varstring::val_str() (???)
215
     consequently, buf2 may be created as 'String buf;' - no memory
216
     will be allocated for it. buf1 will be allocated to hold a
217
     value if it's too small. Using allocated buffer for buf2 may result in
218
     an unnecessary free (and later, may be an alloc).
219
     This trickery is used to decrease a number of malloc calls.
220
  */
1.2.9 by Monty Taylor
Import upstream version 2011.03.11
221
  virtual String *val_str(String*, String *) const =0;
1.2.8 by Monty Taylor
Import upstream version 2011.02.10
222
1 by Monty Taylor
Import upstream version 2010.03.1347
223
  /*
224
   str_needs_quotes() returns true if the value returned by val_str() needs
225
   to be quoted when used in constructing an SQL query.
226
  */
227
  virtual bool str_needs_quotes() { return false; }
228
  virtual Item_result result_type () const=0;
229
  virtual Item_result cmp_type () const { return result_type(); }
230
  virtual Item_result cast_to_int_type () const { return result_type(); }
11 by Monty Taylor
* Fixed missing build depends.
231
1 by Monty Taylor
Import upstream version 2010.03.1347
232
  /**
233
     Check whether a field type can be partially indexed by a key.
234
235
     This is a static method, rather than a virtual function, because we need
236
     to check the type of a non-Field in alter_table().
237
238
     @param type  field type
239
240
     @retval
241
       true  Type can have a prefixed key
242
     @retval
243
       false Type can not have a prefixed key
244
  */
245
  static bool type_can_have_key_part(enum_field_types);
246
  /**
247
    Return type of which can carry value of both given types in UNION result.
248
249
    @param a  type for merging
250
    @param b  type for merging
251
252
    @retval
253
      type of field
254
  */
255
  static enum_field_types field_type_merge(enum_field_types, enum_field_types);
256
257
  /**
258
     Detect Item_result by given field type of UNION merge result.
259
260
     @param field_type  given field type
261
262
     @return
263
       Item_result (type of internal MySQL expression result)
264
  */
265
  static Item_result result_merge_type(enum_field_types);
266
267
  virtual bool eq(Field *field);
268
  /**
269
   * Returns true if the fields are equally defined
270
   *
271
   * @retval
272
   *  true  This Field is equally defined to supplied Field
273
   * @retval
274
   *  false This Field is NOT equally defined to supplied Field
275
   */
276
  virtual bool eq_def(Field *field);
277
1.2.6 by Monty Taylor
Import upstream version 2011.01.08
278
  virtual bool is_timestamp() const
279
  {
280
    return false;
281
  }
282
1 by Monty Taylor
Import upstream version 2010.03.1347
283
  /**
284
   * Returns size (in bytes) used to store field data in memory
285
   * (i.e. it returns the maximum size of the field in a row of the table,
286
   * which is located in RAM).
287
   */
288
  virtual uint32_t pack_length() const;
289
290
  /**
291
   * Returns size (in bytes) used to store field data on
292
   * storage (i.e. it returns the maximal size of the field in a row of the
293
   * table, which is located on disk).
294
   */
295
  virtual uint32_t pack_length_in_rec() const;
296
297
  /**
298
   * Return the "real size" of the data in memory.
299
   * For varstrings, this does _not_ include the length bytes.
300
   */
301
  virtual uint32_t data_length();
302
  /**
303
   * Returns the number of bytes actually used to store the data
304
   * of the field. So for a varstring it includes both lenght byte(s) and
305
   * string data, and anything after data_length() bytes are unused.
306
   */
307
  virtual uint32_t used_length();
308
  virtual uint32_t sort_length() const;
309
310
  /**
311
     Get the maximum size of the data in packed format.
312
313
     @return Maximum data length of the field when packed using the
314
     Field::pack() function.
315
   */
316
  virtual uint32_t max_data_length() const;
317
  virtual int reset(void);
318
  virtual void reset_fields();
319
  virtual void set_default();
320
  virtual bool binary() const;
321
  virtual bool zero_pack() const;
322
  virtual enum ha_base_keytype key_type() const;
323
  virtual uint32_t key_length() const;
324
  virtual enum_field_types type() const =0;
325
  virtual enum_field_types real_type() const;
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
326
  virtual int cmp_max(const unsigned char *a, const unsigned char *b, uint32_t max_len);
1 by Monty Taylor
Import upstream version 2010.03.1347
327
  virtual int cmp(const unsigned char *,const unsigned char *)=0;
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
328
  int cmp_internal(const unsigned char *str) { return cmp(ptr,str); }
1 by Monty Taylor
Import upstream version 2010.03.1347
329
  virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
330
                         uint32_t max_length=UINT32_MAX);
331
  virtual int cmp_offset(uint32_t row_offset);
332
  virtual int cmp_binary_offset(uint32_t row_offset);
333
  virtual int key_cmp(const unsigned char *a,const unsigned char *b);
334
  virtual int key_cmp(const unsigned char *str, uint32_t length);
335
  virtual uint32_t decimals() const;
336
337
  // For new field
338
  virtual uint32_t size_of() const =0;
339
1.2.9 by Monty Taylor
Import upstream version 2011.03.11
340
  bool is_null(ptrdiff_t row_offset= 0) const;
341
  bool is_real_null(ptrdiff_t row_offset= 0) const;
342
  bool is_null_in_record(const unsigned char *record) const;
343
  bool is_null_in_record_with_offset(ptrdiff_t offset) const;
1 by Monty Taylor
Import upstream version 2010.03.1347
344
  void set_null(ptrdiff_t row_offset= 0);
345
  void set_notnull(ptrdiff_t row_offset= 0);
1.2.9 by Monty Taylor
Import upstream version 2011.03.11
346
  bool maybe_null(void) const;
347
  bool real_maybe_null(void) const;
1 by Monty Taylor
Import upstream version 2010.03.1347
348
349
  virtual void make_field(SendField *);
350
  virtual void sort_string(unsigned char *buff,uint32_t length)=0;
351
  virtual bool optimize_range(uint32_t idx, uint32_t part);
352
  /**
353
   * Returns true for fields which, when compared with constant
354
   * items, can be casted to int64_t. In this case we will at 'fix_fields'
355
   * stage cast the constant items to int64_ts and at the execution stage
356
   * use field->val_int() for comparison.  Used to optimize clauses like
357
   * 'a_column BETWEEN date_const AND date_const'.
358
   */
359
  virtual bool can_be_compared_as_int64_t() const
360
  {
361
    return false;
362
  }
363
  virtual void free() {}
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
364
  virtual Field *new_field(memory::Root*, Table*, bool keep_type);
1 by Monty Taylor
Import upstream version 2010.03.1347
365
  virtual Field *new_key_field(memory::Root *root, Table *new_table,
366
                               unsigned char *new_ptr,
367
                               unsigned char *new_null_ptr,
368
                               uint32_t new_null_bit);
369
  /** This is used to generate a field in Table from TableShare */
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
370
  Field* clone(memory::Root*, Table*);
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
371
  void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
1 by Monty Taylor
Import upstream version 2010.03.1347
372
  {
373
    ptr= ptr_arg;
374
    null_ptr= null_ptr_arg;
375
    null_bit= null_bit_arg;
376
  }
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
377
  void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
1 by Monty Taylor
Import upstream version 2010.03.1347
378
  virtual void move_field_offset(ptrdiff_t ptr_diff)
379
  {
380
    ptr= ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
381
    if (null_ptr)
382
      null_ptr= ADD_TO_PTR(null_ptr,ptr_diff,unsigned char*);
383
  }
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
384
  virtual void get_image(unsigned char *buff, uint32_t length, const charset_info_st * const)
1 by Monty Taylor
Import upstream version 2010.03.1347
385
  {
386
    memcpy(buff,ptr,length);
387
  }
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
388
  virtual void get_image(std::basic_string<unsigned char> &buff, uint32_t length, const charset_info_st * const)
1 by Monty Taylor
Import upstream version 2010.03.1347
389
  {
390
    buff.append(ptr,length);
391
  }
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
392
  virtual void set_image(const unsigned char *buff,uint32_t length, const charset_info_st * const)
1 by Monty Taylor
Import upstream version 2010.03.1347
393
  {
394
    memcpy(ptr,buff,length);
395
  }
396
397
  /**
398
   * Copy a field part into an output buffer.
399
   *
400
   * @details
401
   *
402
   * This function makes a copy of field part of size equal to or
403
   * less than "length" parameter value.
404
   * For fields of string types (VARCHAR, TEXT) the rest of buffer
405
   * is padded by zero byte.
406
   *
407
   * @param output buffer
408
   * @param output buffer size
409
   *
410
   * @note
411
   *
412
   * For variable length character fields (i.e. UTF-8) the "length"
413
   * parameter means a number of output buffer bytes as if all field
414
   * characters have maximal possible size (mbmaxlen). In the other words,
415
   * "length" parameter is a number of characters multiplied by
416
   * field_charset->mbmaxlen.
417
   *
418
   * @retval
419
   *   Number of copied bytes (excluding padded zero bytes -- see above).
420
   */
421
  virtual uint32_t get_key_image(unsigned char *buff, uint32_t length)
422
  {
423
    get_image(buff, length, &my_charset_bin);
424
    return length;
425
  }
426
  virtual uint32_t get_key_image(std::basic_string<unsigned char> &buff, uint32_t length)
427
  {
428
    get_image(buff, length, &my_charset_bin);
429
    return length;
430
  }
431
  virtual void set_key_image(const unsigned char *buff,uint32_t length)
432
  {
433
    set_image(buff,length, &my_charset_bin);
434
  }
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
435
  int64_t val_int_offset(uint32_t row_offset)
1 by Monty Taylor
Import upstream version 2010.03.1347
436
  {
437
    ptr+=row_offset;
438
    int64_t tmp=val_int();
439
    ptr-=row_offset;
440
    return tmp;
441
  }
442
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
443
  int64_t val_int_internal(const unsigned char *new_ptr)
1 by Monty Taylor
Import upstream version 2010.03.1347
444
  {
445
    unsigned char *old_ptr= ptr;
446
    ptr= const_cast<unsigned char*>(new_ptr);
1.2.8 by Monty Taylor
Import upstream version 2011.02.10
447
    int64_t return_value= val_int();
1 by Monty Taylor
Import upstream version 2010.03.1347
448
    ptr= old_ptr;
449
    return return_value;
450
  }
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
451
452
  String *val_str_internal(String *str, const unsigned char *new_ptr)
1 by Monty Taylor
Import upstream version 2010.03.1347
453
  {
454
    unsigned char *old_ptr= ptr;
455
    ptr= const_cast<unsigned char*>(new_ptr);
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
456
    val_str_internal(str);
1 by Monty Taylor
Import upstream version 2010.03.1347
457
    ptr= old_ptr;
458
    return str;
459
  }
460
461
  /**
462
    Pack the field into a format suitable for storage and transfer.
463
464
    To implement packing functionality, only the virtual function
465
    should be overridden. The other functions are just convenience
466
    functions and hence should not be overridden.
467
468
    The value of <code>low_byte_first</code> is dependent on how the
469
    packed data is going to be used: for local use, e.g., temporary
470
    store on disk or in memory, use the native format since that is
471
    faster. For data that is going to be transfered to other machines
472
    (e.g., when writing data to the binary log), data should always be
473
    stored in little-endian format.
474
475
    @note The default method for packing fields just copy the raw bytes
476
    of the record into the destination, but never more than
477
    <code>max_length</code> characters.
478
479
    @param to
480
    Pointer to memory area where representation of field should be put.
481
482
    @param from
483
    Pointer to memory area where record representation of field is
484
    stored.
485
486
    @param max_length
487
    Maximum length of the field, as given in the column definition. For
488
    example, for <code>CHAR(1000)</code>, the <code>max_length</code>
489
    is 1000. This information is sometimes needed to decide how to pack
490
    the data.
491
492
    @param low_byte_first
493
    @c true if integers should be stored little-endian, @c false if
494
    native format should be used. Note that for little-endian machines,
495
    the value of this flag is a moot point since the native format is
496
    little-endian.
497
  */
498
  virtual unsigned char *pack(unsigned char *to,
499
                              const unsigned char *from,
500
                              uint32_t max_length,
501
                              bool low_byte_first);
502
503
  unsigned char *pack(unsigned char *to, const unsigned char *from);
504
505
  /**
506
    Unpack a field from row data.
507
508
    This method is used to unpack a field from a master whose size of
509
    the field is less than that of the slave.
510
511
    The <code>param_data</code> parameter is a two-byte integer (stored
512
    in the least significant 16 bits of the unsigned integer) usually
513
    consisting of two parts: the real type in the most significant byte
514
    and a original pack length in the least significant byte.
515
516
    The exact layout of the <code>param_data</code> field is given by
517
    the <code>Table_map_log_event::save_field_metadata()</code>.
518
519
    This is the default method for unpacking a field. It just copies
520
    the memory block in byte order (of original pack length bytes or
521
    length of field, whichever is smaller).
522
523
    @param   to         Destination of the data
524
    @param   from       Source of the data
525
    @param   param_data Real type and original pack length of the field
526
                        data
527
528
    @param low_byte_first
529
    If this flag is @c true, all composite entities (e.g., lengths)
530
    should be unpacked in little-endian format; otherwise, the entities
531
    are unpacked in native order.
532
533
    @return  New pointer into memory based on from + length of the data
534
  */
535
  virtual const unsigned char *unpack(unsigned char* to,
536
                                      const unsigned char *from,
537
                                      uint32_t param_data,
538
                                      bool low_byte_first);
539
  /**
540
     @overload Field::unpack(unsigned char*, const unsigned char*,
541
                             uint32_t, bool)
542
  */
543
  const unsigned char *unpack(unsigned char* to,
544
                              const unsigned char *from);
545
546
  virtual unsigned char *pack_key(unsigned char* to,
547
                                  const unsigned char *from,
548
                                  uint32_t max_length,
549
                                  bool low_byte_first)
550
  {
551
    return pack(to, from, max_length, low_byte_first);
552
  }
553
  virtual const unsigned char *unpack_key(unsigned char* to,
554
                                          const unsigned char *from,
555
                                          uint32_t max_length,
556
                                          bool low_byte_first)
557
  {
558
    return unpack(to, from, max_length, low_byte_first);
559
  }
560
  virtual uint32_t max_packed_col_length(uint32_t max_length)
561
  {
562
    return max_length;
563
  }
564
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
565
  uint32_t offset(const unsigned char *record)
1 by Monty Taylor
Import upstream version 2010.03.1347
566
  {
567
    return (uint32_t) (ptr - record);
568
  }
569
  void copy_from_tmp(int offset);
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
570
  uint32_t fill_cache_field(CacheField *copy);
1.2.9 by Monty Taylor
Import upstream version 2011.03.11
571
  virtual bool get_date(type::Time &ltime,uint32_t fuzzydate) const;
572
  virtual bool get_time(type::Time &ltime) const;
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
573
  virtual const charset_info_st *charset(void) const { return &my_charset_bin; }
574
  virtual const charset_info_st *sort_charset(void) const { return charset(); }
1 by Monty Taylor
Import upstream version 2010.03.1347
575
  virtual bool has_charset(void) const { return false; }
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
576
  virtual void set_charset(const charset_info_st * const)
1 by Monty Taylor
Import upstream version 2010.03.1347
577
  {}
578
  virtual enum Derivation derivation(void) const
579
  {
580
    return DERIVATION_IMPLICIT;
581
  }
582
  virtual void set_derivation(enum Derivation)
583
  {}
584
  /**
585
    Produce warning or note about data saved into field.
586
587
    @param level            - level of message (Note/Warning/Error)
588
    @param code             - error code of message to be produced
589
    @param cuted_increment  - whenever we should increase cut fields count or not
590
591
    @note
592
      This function won't produce warning and increase cut fields counter
593
      if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
594
595
      if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
596
      This allows us to avoid notes in optimisation, like convert_constant_item().
597
598
    @retval
599
      1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
600
    @retval
601
      0 otherwise
602
  */
603
  bool set_warning(DRIZZLE_ERROR::enum_warning_level,
1.2.6 by Monty Taylor
Import upstream version 2011.01.08
604
                   drizzled::error_t code,
1 by Monty Taylor
Import upstream version 2010.03.1347
605
                   int cuted_increment);
606
  /**
607
    Produce warning or note about datetime string data saved into field.
608
609
    @param level            level of message (Note/Warning/Error)
610
    @param code             error code of message to be produced
611
    @param str              string value which we tried to save
612
    @param str_length       length of string which we tried to save
613
    @param ts_type          type of datetime value (datetime/date/time)
614
    @param cuted_increment  whenever we should increase cut fields count or not
615
616
    @note
617
      This function will always produce some warning but won't increase cut
618
      fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
619
      thread.
620
  */
621
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
1.2.6 by Monty Taylor
Import upstream version 2011.01.08
622
                            drizzled::error_t code,
1 by Monty Taylor
Import upstream version 2010.03.1347
623
                            const char *str,
624
                            uint32_t str_len,
1.2.7 by Monty Taylor
Import upstream version 2011.02.09
625
                            type::timestamp_t ts_type,
1 by Monty Taylor
Import upstream version 2010.03.1347
626
                            int cuted_increment);
627
  /**
628
    Produce warning or note about integer datetime value saved into field.
629
630
    @param level            level of message (Note/Warning/Error)
631
    @param code             error code of message to be produced
632
    @param nr               numeric value which we tried to save
633
    @param ts_type          type of datetime value (datetime/date/time)
634
    @param cuted_increment  whenever we should increase cut fields count or not
635
636
    @note
637
      This function will always produce some warning but won't increase cut
638
      fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
639
      thread.
640
  */
641
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
1.2.6 by Monty Taylor
Import upstream version 2011.01.08
642
                            drizzled::error_t code,
1 by Monty Taylor
Import upstream version 2010.03.1347
643
                            int64_t nr,
1.2.7 by Monty Taylor
Import upstream version 2011.02.09
644
                            type::timestamp_t ts_type,
1 by Monty Taylor
Import upstream version 2010.03.1347
645
                            int cuted_increment);
646
  /**
647
    Produce warning or note about double datetime data saved into field.
648
649
    @param level            level of message (Note/Warning/Error)
650
    @param code             error code of message to be produced
651
    @param nr               double value which we tried to save
652
    @param ts_type          type of datetime value (datetime/date/time)
653
654
    @note
655
      This function will always produce some warning but won't increase cut
656
      fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
657
      thread.
658
  */
659
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
1.2.6 by Monty Taylor
Import upstream version 2011.01.08
660
                            const drizzled::error_t code,
1 by Monty Taylor
Import upstream version 2010.03.1347
661
                            double nr,
1.2.7 by Monty Taylor
Import upstream version 2011.02.09
662
                            type::timestamp_t ts_type);
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
663
  bool check_overflow(int op_result)
1 by Monty Taylor
Import upstream version 2010.03.1347
664
  {
665
    return (op_result == E_DEC_OVERFLOW);
666
  }
667
  /**
668
    Process decimal library return codes and issue warnings for overflow and
669
    truncation.
670
671
    @param op_result  decimal library return code (E_DEC_* see include/decimal.h)
672
673
    @retval
674
      E_DEC_OVERFLOW   there was overflow
675
      E_DEC_TRUNCATED  there was truncation
676
    @retval
677
      0  no error or there was some other error except overflow or truncation
678
  */
679
  int warn_if_overflow(int op_result);
680
  void init(Table *table_arg);
681
682
  /* maximum possible display length */
683
  virtual uint32_t max_display_length()= 0;
684
685
  virtual uint32_t is_equal(CreateField *new_field);
686
  /**
687
    Conversion from decimal to int64_t with checking overflow and
688
    setting correct value (min/max) in case of overflow.
689
690
    @param val             value which have to be converted
691
    @param unsigned_flag   type of integer in which we convert val
692
    @param err             variable to pass error code
693
694
    @return
695
      value converted from val
696
  */
11 by Monty Taylor
* Fixed missing build depends.
697
  int64_t convert_decimal2int64_t(const type::Decimal *val,
1 by Monty Taylor
Import upstream version 2010.03.1347
698
                                  bool unsigned_flag,
699
                                  int *err);
700
  /* The max. number of characters */
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
701
  uint32_t char_length() const
1 by Monty Taylor
Import upstream version 2010.03.1347
702
  {
703
    return field_length / charset()->mbmaxlen;
704
  }
705
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
706
  enum column_format_type column_format() const
1 by Monty Taylor
Import upstream version 2010.03.1347
707
  {
708
    return (enum column_format_type)
709
      ((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
710
  }
711
712
  /* Hash value */
1.2.9 by Monty Taylor
Import upstream version 2011.03.11
713
  virtual void hash(uint32_t *nr, uint32_t *nr2) const;
1 by Monty Taylor
Import upstream version 2010.03.1347
714
  friend bool reopen_table(Session *,Table *,bool);
715
716
  friend class CopyField;
717
  friend class Item_avg_field;
718
  friend class Item_std_field;
719
  friend class Item_sum_num;
720
  friend class Item_sum_sum;
721
  friend class Item_sum_str;
722
  friend class Item_sum_count;
723
  friend class Item_sum_avg;
724
  friend class Item_sum_std;
725
  friend class Item_sum_min;
726
  friend class Item_sum_max;
727
  friend class Item_func_group_concat;
728
1.2.9 by Monty Taylor
Import upstream version 2011.03.11
729
  bool isReadSet() const;
1 by Monty Taylor
Import upstream version 2010.03.1347
730
  bool isWriteSet();
731
  void setReadSet(bool arg= true);
732
  void setWriteSet(bool arg= true);
11 by Monty Taylor
* Fixed missing build depends.
733
734
protected:
735
736
  void pack_num(uint64_t arg, unsigned char *destination= NULL);
1.2.6 by Monty Taylor
Import upstream version 2011.01.08
737
  void pack_num(uint32_t arg, unsigned char *destination= NULL);
11 by Monty Taylor
* Fixed missing build depends.
738
  uint64_t unpack_num(uint64_t &destination, const unsigned char *arg= NULL) const;
1.2.6 by Monty Taylor
Import upstream version 2011.01.08
739
  uint32_t unpack_num(uint32_t &destination, const unsigned char *arg= NULL) const;
1 by Monty Taylor
Import upstream version 2010.03.1347
740
};
741
1.2.9 by Monty Taylor
Import upstream version 2011.03.11
742
namespace field {
743
744
inline bool isDateTime(const enum_field_types &arg)
745
{
746
  switch (arg)
747
  {
748
  case DRIZZLE_TYPE_DATE:
749
  case DRIZZLE_TYPE_DATETIME:
750
  case DRIZZLE_TYPE_MICROTIME:
751
  case DRIZZLE_TYPE_TIME:
752
  case DRIZZLE_TYPE_TIMESTAMP:
753
    return true;
754
755
  case DRIZZLE_TYPE_BLOB:
756
  case DRIZZLE_TYPE_BOOLEAN:
757
  case DRIZZLE_TYPE_DECIMAL:
758
  case DRIZZLE_TYPE_DOUBLE:
759
  case DRIZZLE_TYPE_ENUM:
760
  case DRIZZLE_TYPE_LONG:
761
  case DRIZZLE_TYPE_LONGLONG:
762
  case DRIZZLE_TYPE_NULL:
763
  case DRIZZLE_TYPE_UUID:
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
764
  case DRIZZLE_TYPE_IPV6:
1.2.9 by Monty Taylor
Import upstream version 2011.03.11
765
  case DRIZZLE_TYPE_VARCHAR:
766
    return false;
767
  }
768
769
  assert(0);
770
  abort();
771
}
772
773
} // namespace field
774
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
775
std::ostream& operator<<(std::ostream& output, const Field &field);
776
1 by Monty Taylor
Import upstream version 2010.03.1347
777
/**
778
 * A class for sending field information to a client.
779
 *
780
 * @details
781
 *
782
 * Send_field is basically a stripped-down POD class for
783
 * representing basic information about a field...
784
 */
785
class SendField
786
{
787
public:
788
  const char *db_name;
789
  const char *table_name;
790
  const char *org_table_name;
791
  const char *col_name;
792
  const char *org_col_name;
793
  uint32_t length;
794
  uint32_t charsetnr;
795
  uint32_t flags;
796
  uint32_t decimals;
797
  enum_field_types type;
798
};
799
800
uint32_t pack_length_to_packflag(uint32_t type);
801
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
802
int set_field_to_null(Field *field);
803
int set_field_to_null_with_conversions(Field *field, bool no_conversions);
804
805
/**
806
 * Tests if the given string contains important data:
807
 * not spaces for character string, or any data for binary string.
808
 *
809
 * @param pointer to the character set to use
810
 * @param String to test
811
 * @param String end
812
 *
813
 * @retval
814
 *  false - If string does not have important data
815
 * @retval
816
 *  true  - If string has some important data
817
 */
1.1.3 by Tobias Frost
Import upstream version 2012.01.30
818
bool test_if_important_data(const charset_info_st * const cs,
1 by Monty Taylor
Import upstream version 2010.03.1347
819
                            const char *str,
820
                            const char *strend);
821
822
} /* namespace drizzled */
823