~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2006 MySQL AB
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
 
 
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
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
 */
16
19
 
17
20
/*
18
21
  Because of the function new_field() all field classes that have static
19
22
  variables must declare the size_of() member function.
20
23
*/
21
24
 
22
 
#ifdef USE_PRAGMA_INTERFACE
23
 
#pragma interface                       /* gcc class implementation */
24
 
#endif
 
25
#ifndef DRIZZLED_FIELD_H
 
26
#define DRIZZLED_FIELD_H
 
27
 
 
28
#include <drizzled/sql_error.h>
 
29
#include <drizzled/my_decimal.h>
25
30
 
26
31
#define DATETIME_DEC                     6
27
32
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
34
39
struct st_cache_field;
35
40
int field_conv(Field *to,Field *from);
36
41
 
37
 
inline uint get_enum_pack_length(int elements)
 
42
inline uint32_t get_enum_pack_length(int elements)
38
43
{
39
44
  return elements < 256 ? 1 : 2;
40
45
}
41
46
 
42
 
inline uint get_set_pack_length(int elements)
 
47
inline uint32_t get_set_pack_length(int elements)
43
48
{
44
 
  uint len= (elements + 7) / 8;
 
49
  uint32_t len= (elements + 7) / 8;
45
50
  return len > 4 ? 8 : len;
46
51
}
47
52
 
 
53
class virtual_column_info: public Sql_alloc
 
54
{
 
55
public:
 
56
  Item *expr_item;
 
57
  LEX_STRING expr_str;
 
58
  Item *item_free_list;
 
59
  virtual_column_info() 
 
60
  : expr_item(0), item_free_list(0),
 
61
    field_type(DRIZZLE_TYPE_VIRTUAL),
 
62
    is_stored(false), data_inited(false)
 
63
  {
 
64
    expr_str.str= NULL;
 
65
    expr_str.length= 0;
 
66
  };
 
67
  ~virtual_column_info() {}
 
68
  enum_field_types get_real_type()
 
69
  {
 
70
    assert(data_inited);
 
71
    return data_inited ? field_type : DRIZZLE_TYPE_VIRTUAL;
 
72
  }
 
73
  void set_field_type(enum_field_types fld_type)
 
74
  {
 
75
    /* Calling this function can only be done once. */
 
76
    assert(not data_inited);
 
77
    data_inited= true;
 
78
    field_type= fld_type;
 
79
  }
 
80
  bool get_field_stored()
 
81
  {
 
82
    assert(data_inited);
 
83
    return data_inited ? is_stored : true;
 
84
  }
 
85
  void set_field_stored(bool stored)
 
86
  {
 
87
    is_stored= stored;
 
88
  }
 
89
private:
 
90
  /*
 
91
    The following data is only updated by the parser and read
 
92
    when a Create_field object is created/initialized.
 
93
  */
 
94
  enum_field_types field_type;   /* Real field type*/
 
95
  bool is_stored;             /* Indication that the field is 
 
96
                                    phisically stored in the database*/
 
97
  /*
 
98
    This flag is used to prevent other applications from
 
99
    reading and using incorrect data.
 
100
  */
 
101
  bool data_inited; 
 
102
};
 
103
 
48
104
class Field
49
105
{
50
106
  Field(const Item &);                          /* Prevent use of these */
55
111
                              size_t size __attribute__((unused)))
56
112
  { TRASH(ptr_arg, size); }
57
113
 
58
 
  uchar         *ptr;                   // Position to field in record
59
 
  uchar         *null_ptr;              // Byte where null_bit is
 
114
  unsigned char         *ptr;                   // Position to field in record
 
115
  unsigned char         *null_ptr;              // Byte where null_bit is
60
116
  /*
61
 
    Note that you can use table->in_use as replacement for current_thd member 
 
117
    Note that you can use table->in_use as replacement for current_session member 
62
118
    only inside of val_*() and store() members (e.g. you can't use it in cons)
63
119
  */
64
120
  Table *table;         // Pointer for table
86
142
  uint32_t      field_length;           // Length of field
87
143
  uint32_t      flags;
88
144
  uint16_t        field_index;            // field number in fields array
89
 
  uchar         null_bit;               // Bit used to test null bit
 
145
  unsigned char         null_bit;               // Bit used to test null bit
90
146
  /**
91
147
     If true, this field was created in create_tmp_field_from_item from a NULL
92
148
     value. This means that the type of the field is just a guess, and the type
98
154
   */
99
155
  bool is_created_from_null_item;
100
156
 
101
 
  Field(uchar *ptr_arg,uint32_t length_arg,uchar *null_ptr_arg,
102
 
        uchar null_bit_arg, utype unireg_check_arg,
 
157
  /* Virtual column data */
 
158
  virtual_column_info *vcol_info;
 
159
  /*
 
160
    Indication that the field is phycically stored in tables 
 
161
    rather than just generated on SQL queries.
 
162
    As of now, false can only be set for generated-only virtual columns.
 
163
  */
 
164
  bool is_stored;
 
165
 
 
166
  Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
 
167
        unsigned char null_bit_arg, utype unireg_check_arg,
103
168
        const char *field_name_arg);
104
169
  virtual ~Field() {}
105
170
  /* Store functions returns 1 on overflow and -1 on fatal error */
106
 
  virtual int  store(const char *to, uint length, const CHARSET_INFO * const cs)=0;
 
171
  virtual int  store(const char *to, uint32_t length, const CHARSET_INFO * const cs)=0;
107
172
  virtual int  store(double nr)=0;
108
173
  virtual int  store(int64_t nr, bool unsigned_val)=0;
109
174
  virtual int  store_decimal(const my_decimal *d)=0;
110
 
  virtual int store_time(DRIZZLE_TIME *ltime, timestamp_type t_type);
111
 
  int store(const char *to, uint length, const CHARSET_INFO * const cs,
 
175
  virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
 
176
  int store(const char *to, uint32_t length, const CHARSET_INFO * const cs,
112
177
            enum_check_fields check_level);
113
178
  virtual double val_real(void)=0;
114
179
  virtual int64_t val_int(void)=0;
159
224
    table, which is located on disk).
160
225
  */
161
226
  virtual uint32_t pack_length_in_rec() const { return pack_length(); }
162
 
  virtual int compatible_field_size(uint field_metadata);
163
 
  virtual uint pack_length_from_metadata(uint field_metadata)
 
227
  virtual int compatible_field_size(uint32_t field_metadata);
 
228
  virtual uint32_t pack_length_from_metadata(uint32_t field_metadata)
164
229
  { return field_metadata; }
165
230
  /*
166
231
    This method is used to return the size of the data in a row-based
174
239
    the classes Field_varstring, and Field_blob return 
175
240
    field_length + 1, field_length, and pack_length_no_ptr() respectfully.
176
241
  */
177
 
  virtual uint row_pack_length() { return 0; }
178
 
  virtual int save_field_metadata(uchar *first_byte)
 
242
  virtual uint32_t row_pack_length() { return 0; }
 
243
  virtual int save_field_metadata(unsigned char *first_byte)
179
244
  { return do_save_field_metadata(first_byte); }
180
245
 
181
246
  /*
208
273
    my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->getDefaultValues() - table->record[0]);
209
274
    memcpy(ptr, ptr + l_offset, pack_length());
210
275
    if (null_ptr)
211
 
      *null_ptr= ((*null_ptr & (uchar) ~null_bit) | (null_ptr[l_offset] & null_bit));
 
276
      *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
212
277
  }
213
278
  virtual bool binary() const { return 1; }
214
279
  virtual bool zero_pack() const { return 1; }
216
281
  virtual uint32_t key_length() const { return pack_length(); }
217
282
  virtual enum_field_types type() const =0;
218
283
  virtual enum_field_types real_type() const { return type(); }
219
 
  inline  int cmp(const uchar *str) { return cmp(ptr,str); }
220
 
  virtual int cmp_max(const uchar *a, const uchar *b,
221
 
                      uint max_len __attribute__((unused)))
 
284
  inline  int cmp(const unsigned char *str) { return cmp(ptr,str); }
 
285
  virtual int cmp_max(const unsigned char *a, const unsigned char *b,
 
286
                      uint32_t max_len __attribute__((unused)))
222
287
    { return cmp(a, b); }
223
 
  virtual int cmp(const uchar *,const uchar *)=0;
224
 
  virtual int cmp_binary(const uchar *a,const uchar *b,
 
288
  virtual int cmp(const unsigned char *,const unsigned char *)=0;
 
289
  virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
225
290
                         uint32_t  __attribute__((unused)) max_length=UINT32_MAX)
226
291
  { return memcmp(a,b,pack_length()); }
227
 
  virtual int cmp_offset(uint row_offset)
 
292
  virtual int cmp_offset(uint32_t row_offset)
228
293
  { return cmp(ptr,ptr+row_offset); }
229
 
  virtual int cmp_binary_offset(uint row_offset)
 
294
  virtual int cmp_binary_offset(uint32_t row_offset)
230
295
  { return cmp_binary(ptr, ptr+row_offset); };
231
 
  virtual int key_cmp(const uchar *a,const uchar *b)
 
296
  virtual int key_cmp(const unsigned char *a,const unsigned char *b)
232
297
  { return cmp(a, b); }
233
 
  virtual int key_cmp(const uchar *str, uint length __attribute__((unused)))
 
298
  virtual int key_cmp(const unsigned char *str, uint32_t length __attribute__((unused)))
234
299
  { return cmp(ptr,str); }
235
 
  virtual uint decimals() const { return 0; }
 
300
  virtual uint32_t decimals() const { return 0; }
236
301
  /*
237
302
    Caller beware: sql_type can change str.Ptr, so check
238
303
    ptr() to see if it changed if you are using your own buffer
239
304
    in str and restore it with set() if needed
240
305
  */
241
306
  virtual void sql_type(String &str) const =0;
242
 
  virtual uint size_of() const =0;              // For new field
 
307
  virtual uint32_t size_of() const =0;          // For new field
243
308
  inline bool is_null(my_ptrdiff_t row_offset= 0)
244
309
  { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : table->null_row; }
245
310
  inline bool is_real_null(my_ptrdiff_t row_offset= 0)
246
311
    { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : 0; }
247
 
  inline bool is_null_in_record(const uchar *record)
 
312
  inline bool is_null_in_record(const unsigned char *record)
248
313
  {
249
314
    if (!null_ptr)
250
315
      return 0;
251
 
    return test(record[(uint) (null_ptr -table->record[0])] &
 
316
    return test(record[(uint32_t) (null_ptr -table->record[0])] &
252
317
                null_bit);
253
318
  }
254
319
  inline bool is_null_in_record_with_offset(my_ptrdiff_t offset)
260
325
  inline void set_null(my_ptrdiff_t row_offset= 0)
261
326
    { if (null_ptr) null_ptr[row_offset]|= null_bit; }
262
327
  inline void set_notnull(my_ptrdiff_t row_offset= 0)
263
 
    { if (null_ptr) null_ptr[row_offset]&= (uchar) ~null_bit; }
 
328
    { if (null_ptr) null_ptr[row_offset]&= (unsigned char) ~null_bit; }
264
329
  inline bool maybe_null(void) { return null_ptr != 0 || table->maybe_null; }
265
330
  inline bool real_maybe_null(void) { return null_ptr != 0; }
266
331
 
290
355
  }
291
356
 
292
357
  virtual void make_field(Send_field *);
293
 
  virtual void sort_string(uchar *buff,uint length)=0;
294
 
  virtual bool optimize_range(uint idx, uint part);
 
358
  virtual void sort_string(unsigned char *buff,uint32_t length)=0;
 
359
  virtual bool optimize_range(uint32_t idx, uint32_t part);
295
360
  /*
296
361
    This should be true for fields which, when compared with constant
297
362
    items, can be casted to int64_t. In this case we will at 'fix_fields'
304
369
  virtual Field *new_field(MEM_ROOT *root, Table *new_table,
305
370
                           bool keep_type);
306
371
  virtual Field *new_key_field(MEM_ROOT *root, Table *new_table,
307
 
                               uchar *new_ptr, uchar *new_null_ptr,
308
 
                               uint new_null_bit);
 
372
                               unsigned char *new_ptr, unsigned char *new_null_ptr,
 
373
                               uint32_t new_null_bit);
309
374
  Field *clone(MEM_ROOT *mem_root, Table *new_table);
310
 
  inline void move_field(uchar *ptr_arg,uchar *null_ptr_arg,uchar null_bit_arg)
 
375
  inline void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
311
376
  {
312
377
    ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
313
378
  }
314
 
  inline void move_field(uchar *ptr_arg) { ptr=ptr_arg; }
 
379
  inline void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
315
380
  virtual void move_field_offset(my_ptrdiff_t ptr_diff)
316
381
  {
317
 
    ptr=ADD_TO_PTR(ptr,ptr_diff, uchar*);
 
382
    ptr=ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
318
383
    if (null_ptr)
319
 
      null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,uchar*);
 
384
      null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,unsigned char*);
320
385
  }
321
 
  virtual void get_image(uchar *buff, uint length,
 
386
  virtual void get_image(unsigned char *buff, uint32_t length,
322
387
                         const CHARSET_INFO * const cs __attribute__((unused)))
323
388
    { memcpy(buff,ptr,length); }
324
 
  virtual void set_image(const uchar *buff,uint length,
 
389
  virtual void set_image(const unsigned char *buff,uint32_t length,
325
390
                         const CHARSET_INFO * const cs __attribute__((unused)))
326
391
    { memcpy(ptr,buff,length); }
327
392
 
352
417
      Number of copied bytes (excluding padded zero bytes -- see above).
353
418
  */
354
419
 
355
 
  virtual uint get_key_image(uchar *buff, uint length,
 
420
  virtual uint32_t get_key_image(unsigned char *buff, uint32_t length,
356
421
                             imagetype type __attribute__((unused)))
357
422
  {
358
423
    get_image(buff, length, &my_charset_bin);
359
424
    return length;
360
425
  }
361
 
  virtual void set_key_image(const uchar *buff,uint length)
 
426
  virtual void set_key_image(const unsigned char *buff,uint32_t length)
362
427
    { set_image(buff,length, &my_charset_bin); }
363
 
  inline int64_t val_int_offset(uint row_offset)
 
428
  inline int64_t val_int_offset(uint32_t row_offset)
364
429
    {
365
430
      ptr+=row_offset;
366
431
      int64_t tmp=val_int();
367
432
      ptr-=row_offset;
368
433
      return tmp;
369
434
    }
370
 
  inline int64_t val_int(const uchar *new_ptr)
 
435
  inline int64_t val_int(const unsigned char *new_ptr)
371
436
  {
372
 
    uchar *old_ptr= ptr;
 
437
    unsigned char *old_ptr= ptr;
373
438
    int64_t return_value;
374
 
    ptr= (uchar*) new_ptr;
 
439
    ptr= (unsigned char*) new_ptr;
375
440
    return_value= val_int();
376
441
    ptr= old_ptr;
377
442
    return return_value;
378
443
  }
379
 
  inline String *val_str(String *str, const uchar *new_ptr)
 
444
  inline String *val_str(String *str, const unsigned char *new_ptr)
380
445
  {
381
 
    uchar *old_ptr= ptr;
382
 
    ptr= (uchar*) new_ptr;
 
446
    unsigned char *old_ptr= ptr;
 
447
    ptr= (unsigned char*) new_ptr;
383
448
    val_str(str);
384
449
    ptr= old_ptr;
385
450
    return str;
386
451
  }
387
452
  virtual bool send_binary(Protocol *protocol);
388
453
 
389
 
  virtual uchar *pack(uchar *to, const uchar *from,
390
 
                      uint max_length, bool low_byte_first);
391
 
  /**
392
 
     @overload Field::pack(uchar*, const uchar*, uint, bool)
393
 
  */
394
 
  uchar *pack(uchar *to, const uchar *from)
395
 
  {
396
 
    uchar *result= this->pack(to, from, UINT_MAX, table->s->db_low_byte_first);
397
 
    return(result);
398
 
  }
399
 
 
400
 
  virtual const uchar *unpack(uchar* to, const uchar *from,
401
 
                              uint param_data, bool low_byte_first);
402
 
  /**
403
 
     @overload Field::unpack(uchar*, const uchar*, uint, bool)
404
 
  */
405
 
  const uchar *unpack(uchar* to, const uchar *from)
406
 
  {
407
 
    const uchar *result= unpack(to, from, 0U, table->s->db_low_byte_first);
408
 
    return(result);
409
 
  }
410
 
 
411
 
  virtual uchar *pack_key(uchar* to, const uchar *from,
412
 
                          uint max_length, bool low_byte_first)
413
 
  {
414
 
    return pack(to, from, max_length, low_byte_first);
415
 
  }
416
 
  virtual uchar *pack_key_from_key_image(uchar* to, const uchar *from,
417
 
                                        uint max_length, bool low_byte_first)
418
 
  {
419
 
    return pack(to, from, max_length, low_byte_first);
420
 
  }
421
 
  virtual const uchar *unpack_key(uchar* to, const uchar *from,
422
 
                                  uint max_length, bool low_byte_first)
 
454
  virtual unsigned char *pack(unsigned char *to, const unsigned char *from,
 
455
                      uint32_t max_length, bool low_byte_first);
 
456
  /**
 
457
     @overload Field::pack(unsigned char*, const unsigned char*, uint32_t, bool)
 
458
  */
 
459
  unsigned char *pack(unsigned char *to, const unsigned char *from)
 
460
  {
 
461
    unsigned char *result= this->pack(to, from, UINT_MAX, table->s->db_low_byte_first);
 
462
    return(result);
 
463
  }
 
464
 
 
465
  virtual const unsigned char *unpack(unsigned char* to, const unsigned char *from,
 
466
                              uint32_t param_data, bool low_byte_first);
 
467
  /**
 
468
     @overload Field::unpack(unsigned char*, const unsigned char*, uint32_t, bool)
 
469
  */
 
470
  const unsigned char *unpack(unsigned char* to, const unsigned char *from)
 
471
  {
 
472
    const unsigned char *result= unpack(to, from, 0U, table->s->db_low_byte_first);
 
473
    return(result);
 
474
  }
 
475
 
 
476
  virtual unsigned char *pack_key(unsigned char* to, const unsigned char *from,
 
477
                          uint32_t max_length, bool low_byte_first)
 
478
  {
 
479
    return pack(to, from, max_length, low_byte_first);
 
480
  }
 
481
  virtual unsigned char *pack_key_from_key_image(unsigned char* to, const unsigned char *from,
 
482
                                        uint32_t max_length, bool low_byte_first)
 
483
  {
 
484
    return pack(to, from, max_length, low_byte_first);
 
485
  }
 
486
  virtual const unsigned char *unpack_key(unsigned char* to, const unsigned char *from,
 
487
                                  uint32_t max_length, bool low_byte_first)
423
488
  {
424
489
    return unpack(to, from, max_length, low_byte_first);
425
490
  }
426
 
  virtual uint packed_col_length(const uchar *to __attribute__((unused)),
427
 
                                 uint length)
 
491
  virtual uint32_t packed_col_length(const unsigned char *to __attribute__((unused)),
 
492
                                 uint32_t length)
428
493
  { return length;}
429
 
  virtual uint max_packed_col_length(uint max_length)
 
494
  virtual uint32_t max_packed_col_length(uint32_t max_length)
430
495
  { return max_length;}
431
496
 
432
 
  virtual int pack_cmp(const uchar *a,const uchar *b,
433
 
                       uint key_length_arg __attribute__((unused)),
 
497
  virtual int pack_cmp(const unsigned char *a,const unsigned char *b,
 
498
                       uint32_t key_length_arg __attribute__((unused)),
434
499
                       bool insert_or_update __attribute__((unused)))
435
500
  { return cmp(a,b); }
436
 
  virtual int pack_cmp(const uchar *b,
437
 
                       uint key_length_arg __attribute__((unused)),
 
501
  virtual int pack_cmp(const unsigned char *b,
 
502
                       uint32_t key_length_arg __attribute__((unused)),
438
503
                       bool insert_or_update __attribute__((unused)))
439
504
  { return cmp(ptr,b); }
440
 
  uint offset(uchar *record)
 
505
  uint32_t offset(unsigned char *record)
441
506
  {
442
 
    return (uint) (ptr - record);
 
507
    return (uint32_t) (ptr - record);
443
508
  }
444
509
  void copy_from_tmp(int offset);
445
 
  uint fill_cache_field(struct st_cache_field *copy);
446
 
  virtual bool get_date(DRIZZLE_TIME *ltime,uint fuzzydate);
 
510
  uint32_t fill_cache_field(struct st_cache_field *copy);
 
511
  virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
447
512
  virtual bool get_time(DRIZZLE_TIME *ltime);
448
513
  virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
449
514
  virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
456
521
  { }
457
522
  bool set_warning(DRIZZLE_ERROR::enum_warning_level, unsigned int code,
458
523
                   int cuted_increment);
459
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, uint code, 
460
 
                            const char *str, uint str_len,
461
 
                            timestamp_type ts_type, int cuted_increment);
462
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, uint code, 
463
 
                            int64_t nr, timestamp_type ts_type,
 
524
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, uint32_t code, 
 
525
                            const char *str, uint32_t str_len,
 
526
                            enum enum_drizzle_timestamp_type ts_type, int cuted_increment);
 
527
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, uint32_t code, 
 
528
                            int64_t nr, enum enum_drizzle_timestamp_type ts_type,
464
529
                            int cuted_increment);
465
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, const uint code, 
466
 
                            double nr, timestamp_type ts_type);
 
530
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, const uint32_t code, 
 
531
                            double nr, enum enum_drizzle_timestamp_type ts_type);
467
532
  inline bool check_overflow(int op_result)
468
533
  {
469
534
    return (op_result == E_DEC_OVERFLOW);
478
543
  /* maximum possible display length */
479
544
  virtual uint32_t max_display_length()= 0;
480
545
 
481
 
  virtual uint is_equal(Create_field *new_field);
 
546
  virtual uint32_t is_equal(Create_field *new_field);
482
547
  /* convert decimal to int64_t with overflow check */
483
548
  int64_t convert_decimal2int64_t(const my_decimal *val, bool unsigned_flag,
484
549
                                    int *err);
496
561
 
497
562
  /* Hash value */
498
563
  virtual void hash(uint32_t *nr, uint32_t *nr2);
499
 
  friend bool reopen_table(THD *,Table *,bool);
500
 
  friend int cre_myisam(char * name, register Table *form, uint options,
 
564
  friend bool reopen_table(Session *,Table *,bool);
 
565
  friend int cre_myisam(char * name, register Table *form, uint32_t options,
501
566
                        uint64_t auto_increment_value);
502
567
  friend class Copy_field;
503
568
  friend class Item_avg_field;
536
601
 
537
602
   @returns 0 no bytes written.
538
603
*/
539
 
  virtual int do_save_field_metadata(uchar *metadata_ptr __attribute__((unused)))
540
 
  { return 0; }
541
 
};
542
 
 
543
 
 
544
 
class Field_num :public Field {
545
 
public:
546
 
  const uint8_t dec;
547
 
  bool decimal_precision;       // Purify cannot handle bit fields & only for decimal type
548
 
  bool unsigned_flag;   // Purify cannot handle bit fields
549
 
  Field_num(uchar *ptr_arg,uint32_t len_arg, uchar *null_ptr_arg,
550
 
            uchar null_bit_arg, utype unireg_check_arg,
551
 
            const char *field_name_arg,
552
 
            uint8_t dec_arg, bool zero_arg, bool unsigned_arg);
553
 
  Item_result result_type () const { return REAL_RESULT; }
554
 
  void add_unsigned(String &res) const;
555
 
  friend class Create_field;
556
 
  void make_field(Send_field *);
557
 
  uint decimals() const { return (uint) dec; }
558
 
  uint size_of() const { return sizeof(*this); }
559
 
  bool eq_def(Field *field);
560
 
  int store_decimal(const my_decimal *);
561
 
  my_decimal *val_decimal(my_decimal *);
562
 
  uint is_equal(Create_field *new_field);
563
 
  int check_int(const CHARSET_INFO * const cs, const char *str, int length,
564
 
                const char *int_end, int error);
565
 
  bool get_int(const CHARSET_INFO * const cs, const char *from, uint len, 
566
 
               int64_t *rnd, uint64_t unsigned_max, 
567
 
               int64_t signed_min, int64_t signed_max);
568
 
};
569
 
 
570
 
/* base class for all string related classes */
571
 
 
572
 
class Field_str :public Field {
573
 
protected:
574
 
  const CHARSET_INFO *field_charset;
575
 
  enum Derivation field_derivation;
576
 
public:
577
 
  Field_str(uchar *ptr_arg,uint32_t len_arg, uchar *null_ptr_arg,
578
 
            uchar null_bit_arg, utype unireg_check_arg,
579
 
            const char *field_name_arg, const CHARSET_INFO * const charset);
580
 
  Item_result result_type () const { return STRING_RESULT; }
581
 
  uint decimals() const { return NOT_FIXED_DEC; }
582
 
  int  store(double nr);
583
 
  int  store(int64_t nr, bool unsigned_val)=0;
584
 
  int  store_decimal(const my_decimal *);
585
 
  int  store(const char *to,uint length, const CHARSET_INFO * const cs)=0;
586
 
  uint size_of() const { return sizeof(*this); }
587
 
  const CHARSET_INFO *charset(void) const { return field_charset; }
588
 
  void set_charset(const CHARSET_INFO * const charset_arg) { field_charset= charset_arg; }
589
 
  enum Derivation derivation(void) const { return field_derivation; }
590
 
  virtual void set_derivation(enum Derivation derivation_arg)
591
 
  { field_derivation= derivation_arg; }
592
 
  bool binary() const { return field_charset == &my_charset_bin; }
593
 
  uint32_t max_display_length() { return field_length; }
594
 
  friend class Create_field;
595
 
  my_decimal *val_decimal(my_decimal *);
596
 
  virtual bool str_needs_quotes() { return true; }
597
 
  bool compare_str_field_flags(Create_field *new_field, uint32_t flags);
598
 
  uint is_equal(Create_field *new_field);
599
 
};
600
 
 
601
 
 
602
 
/* base class for Field_varstring and Field_blob */
603
 
 
604
 
class Field_longstr :public Field_str
605
 
{
606
 
protected:
607
 
  int report_if_important_data(const char *ptr, const char *end);
608
 
public:
609
 
  Field_longstr(uchar *ptr_arg, uint32_t len_arg, uchar *null_ptr_arg,
610
 
                uchar null_bit_arg, utype unireg_check_arg,
611
 
                const char *field_name_arg, const CHARSET_INFO * const charset_arg)
612
 
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
613
 
               field_name_arg, charset_arg)
614
 
    {}
615
 
 
616
 
  int store_decimal(const my_decimal *d);
617
 
  uint32_t max_data_length() const;
618
 
};
619
 
 
620
 
/* base class for float and double and decimal (old one) */
621
 
class Field_real :public Field_num {
622
 
public:
623
 
  bool not_fixed;
624
 
 
625
 
  Field_real(uchar *ptr_arg, uint32_t len_arg, uchar *null_ptr_arg,
626
 
             uchar null_bit_arg, utype unireg_check_arg,
627
 
             const char *field_name_arg,
628
 
             uint8_t dec_arg, bool zero_arg, bool unsigned_arg)
629
 
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg, unireg_check_arg,
630
 
               field_name_arg, dec_arg, zero_arg, unsigned_arg),
631
 
    not_fixed(dec_arg >= NOT_FIXED_DEC)
632
 
    {}
633
 
  int store_decimal(const my_decimal *);
634
 
  my_decimal *val_decimal(my_decimal *);
635
 
  int truncate(double *nr, double max_length);
636
 
  uint32_t max_display_length() { return field_length; }
637
 
  uint size_of() const { return sizeof(*this); }
638
 
  virtual const uchar *unpack(uchar* to, const uchar *from,
639
 
                              uint param_data, bool low_byte_first);
640
 
  virtual uchar *pack(uchar* to, const uchar *from,
641
 
                      uint max_length, bool low_byte_first);
642
 
};
643
 
 
644
 
 
645
 
class Field_tiny :public Field_num {
646
 
public:
647
 
  Field_tiny(uchar *ptr_arg, uint32_t len_arg, uchar *null_ptr_arg,
648
 
             uchar null_bit_arg,
649
 
             enum utype unireg_check_arg, const char *field_name_arg,
650
 
             bool zero_arg, bool unsigned_arg)
651
 
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
652
 
               unireg_check_arg, field_name_arg,
653
 
               0, zero_arg,unsigned_arg)
654
 
    {}
655
 
  enum Item_result result_type () const { return INT_RESULT; }
656
 
  enum_field_types type() const { return DRIZZLE_TYPE_TINY;}
657
 
  enum ha_base_keytype key_type() const
658
 
    { return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
659
 
  int store(const char *to,uint length, const CHARSET_INFO * const charset);
660
 
  int store(double nr);
661
 
  int store(int64_t nr, bool unsigned_val);
662
 
  int reset(void) { ptr[0]=0; return 0; }
663
 
  double val_real(void);
664
 
  int64_t val_int(void);
665
 
  String *val_str(String*,String *);
666
 
  bool send_binary(Protocol *protocol);
667
 
  int cmp(const uchar *,const uchar *);
668
 
  void sort_string(uchar *buff,uint length);
669
 
  uint32_t pack_length() const { return 1; }
670
 
  void sql_type(String &str) const;
671
 
  uint32_t max_display_length() { return 4; }
672
 
 
673
 
  virtual uchar *pack(uchar* to, const uchar *from,
674
 
                      uint max_length __attribute__((unused)),
675
 
                      bool low_byte_first __attribute__((unused)))
676
 
  {
677
 
    *to= *from;
678
 
    return to + 1;
679
 
  }
680
 
 
681
 
  virtual const uchar *unpack(uchar* to, const uchar *from,
682
 
                              uint param_data __attribute__((unused)),
683
 
                              bool low_byte_first __attribute__((unused)))
684
 
  {
685
 
    *to= *from;
686
 
    return from + 1;
687
 
  }
688
 
};
689
 
 
690
 
 
691
 
class Field_enum :public Field_str {
692
 
protected:
693
 
  uint packlength;
694
 
public:
695
 
  TYPELIB *typelib;
696
 
  Field_enum(uchar *ptr_arg, uint32_t len_arg, uchar *null_ptr_arg,
697
 
             uchar null_bit_arg,
698
 
             enum utype unireg_check_arg, const char *field_name_arg,
699
 
             uint packlength_arg,
700
 
             TYPELIB *typelib_arg,
701
 
             const CHARSET_INFO * const charset_arg)
702
 
    :Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
703
 
               unireg_check_arg, field_name_arg, charset_arg),
704
 
    packlength(packlength_arg),typelib(typelib_arg)
705
 
  {
706
 
      flags|=ENUM_FLAG;
707
 
  }
708
 
  Field *new_field(MEM_ROOT *root, Table *new_table, bool keep_type);
709
 
  enum_field_types type() const { return DRIZZLE_TYPE_ENUM; }
710
 
  enum Item_result cmp_type () const { return INT_RESULT; }
711
 
  enum Item_result cast_to_int_type () const { return INT_RESULT; }
712
 
  enum ha_base_keytype key_type() const;
713
 
  int  store(const char *to,uint length, const CHARSET_INFO * const charset);
714
 
  int  store(double nr);
715
 
  int  store(int64_t nr, bool unsigned_val);
716
 
  double val_real(void);
717
 
  int64_t val_int(void);
718
 
  String *val_str(String*,String *);
719
 
  int cmp(const uchar *,const uchar *);
720
 
  void sort_string(uchar *buff,uint length);
721
 
  uint32_t pack_length() const { return (uint32_t) packlength; }
722
 
  void store_type(uint64_t value);
723
 
  void sql_type(String &str) const;
724
 
  uint size_of() const { return sizeof(*this); }
725
 
  enum_field_types real_type() const { return DRIZZLE_TYPE_ENUM; }
726
 
  uint pack_length_from_metadata(uint field_metadata)
727
 
  { return (field_metadata & 0x00ff); }
728
 
  uint row_pack_length() { return pack_length(); }
729
 
  virtual bool zero_pack() const { return 0; }
730
 
  bool optimize_range(uint idx __attribute__((unused)),
731
 
                      uint part __attribute__((unused)))
732
 
  { return 0; }
733
 
  bool eq_def(Field *field);
734
 
  bool has_charset(void) const { return true; }
735
 
  /* enum and set are sorted as integers */
736
 
  const CHARSET_INFO *sort_charset(void) const { return &my_charset_bin; }
737
 
private:
738
 
  int do_save_field_metadata(uchar *first_byte);
 
604
  virtual int do_save_field_metadata(unsigned char *metadata_ptr __attribute__((unused)))
 
605
  { return 0; }
739
606
};
740
607
 
741
608
/*
761
628
    for most of the types, or of bytes for BLOBs or numeric types.
762
629
  */
763
630
  uint32_t char_length;
764
 
  uint  decimals, flags, pack_length, key_length;
 
631
  uint32_t  decimals, flags, pack_length, key_length;
765
632
  Field::utype unireg_check;
766
633
  TYPELIB *interval;                    // Which interval to use
767
634
  TYPELIB *save_interval;               // Temporary copy for the above
771
638
  Field *field;                         // For alter table
772
639
 
773
640
  uint8_t row,col,sc_length,interval_id;        // For rea_create_table
774
 
  uint  offset,pack_flag;
 
641
  uint32_t      offset,pack_flag;
 
642
 
 
643
  /* Virtual column expression statement */
 
644
  virtual_column_info *vcol_info;
 
645
  /*
 
646
    Indication that the field is phycically stored in tables 
 
647
    rather than just generated on SQL queries.
 
648
    As of now, FALSE can only be set for generated-only virtual columns.
 
649
  */
 
650
  bool is_stored;
 
651
 
775
652
  Create_field() :after(0) {}
776
653
  Create_field(Field *field, Field *orig_field);
777
654
  /* Used to make a clone of this object for ALTER/CREATE TABLE */
790
667
                          uint32_t max_length, uint32_t decimals,
791
668
                          bool maybe_null, bool is_unsigned);
792
669
 
793
 
  bool init(THD *thd, char *field_name, enum_field_types type, char *length,
794
 
            char *decimals, uint type_modifier, Item *default_value,
 
670
  bool init(Session *session, char *field_name, enum_field_types type, char *length,
 
671
            char *decimals, uint32_t type_modifier, Item *default_value,
795
672
            Item *on_update_value, LEX_STRING *comment, char *change,
796
673
            List<String> *interval_list, const CHARSET_INFO * const cs,
797
 
            uint uint_geom_type,
798
 
            enum column_format_type column_format);
 
674
            uint32_t uint_geom_type,
 
675
            enum column_format_type column_format,
 
676
            virtual_column_info *vcol_info);
799
677
};
800
678
 
801
679
 
809
687
  const char *table_name,*org_table_name;
810
688
  const char *col_name,*org_col_name;
811
689
  uint32_t length;
812
 
  uint charsetnr, flags, decimals;
 
690
  uint32_t charsetnr, flags, decimals;
813
691
  enum_field_types type;
814
692
  Send_field() {}
815
693
};
827
705
  typedef void Copy_func(Copy_field*);
828
706
  Copy_func *get_copy_func(Field *to, Field *from);
829
707
public:
830
 
  uchar *from_ptr,*to_ptr;
831
 
  uchar *from_null_ptr,*to_null_ptr;
 
708
  unsigned char *from_ptr,*to_ptr;
 
709
  unsigned char *from_null_ptr,*to_null_ptr;
832
710
  bool *null_row;
833
 
  uint  from_bit,to_bit;
834
 
  uint from_length,to_length;
 
711
  uint32_t      from_bit,to_bit;
 
712
  uint32_t from_length,to_length;
835
713
  Field *from_field,*to_field;
836
714
  String tmp;                                   // For items
837
715
 
838
716
  Copy_field() {}
839
717
  ~Copy_field() {}
840
718
  void set(Field *to,Field *from,bool save);    // Field to field 
841
 
  void set(uchar *to,Field *from);              // Field to string
 
719
  void set(unsigned char *to,Field *from);              // Field to string
842
720
  void (*do_copy)(Copy_field *);
843
721
  void (*do_copy2)(Copy_field *);               // Used to handle null values
844
722
};
845
723
 
846
724
 
847
 
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32_t field_length,
848
 
                  uchar *null_pos, uchar null_bit,
849
 
                  uint pack_flag, enum_field_types field_type,
 
725
Field *make_field(TABLE_SHARE *share, unsigned char *ptr, uint32_t field_length,
 
726
                  unsigned char *null_pos, unsigned char null_bit,
 
727
                  uint32_t pack_flag, enum_field_types field_type,
850
728
                  const CHARSET_INFO * cs,
851
729
                  Field::utype unireg_check,
852
730
                  TYPELIB *interval, const char *field_name);
853
 
uint pack_length_to_packflag(uint type);
 
731
uint32_t pack_length_to_packflag(uint32_t type);
854
732
enum_field_types get_blob_type_from_length(uint32_t length);
855
733
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
856
734
int set_field_to_null(Field *field);
857
735
int set_field_to_null_with_conversions(Field *field, bool no_conversions);
858
736
 
 
737
 
859
738
bool
860
 
check_string_copy_error(Field_str *field,
861
 
                        const char *well_formed_error_pos,
862
 
                        const char *cannot_convert_error_pos,
863
 
                        const char *end,
864
 
                        const CHARSET_INFO * const cs);
 
739
test_if_important_data(const CHARSET_INFO * const cs, 
 
740
                       const char *str,
 
741
                       const char *strend);
865
742
 
866
743
/*
867
744
  Field subclasses
868
745
 */
 
746
#include <drizzled/field/str.h>
 
747
#include <drizzled/field/longstr.h>
 
748
#include <drizzled/field/num.h>
869
749
#include <drizzled/field/blob.h>
 
750
#include <drizzled/field/enum.h>
870
751
#include <drizzled/field/null.h>
871
752
#include <drizzled/field/date.h>
872
753
#include <drizzled/field/fdecimal.h>
 
754
#include <drizzled/field/real.h>
873
755
#include <drizzled/field/double.h>
874
 
#include <drizzled/field/short.h>
875
756
#include <drizzled/field/long.h>
876
757
#include <drizzled/field/int64_t.h>
 
758
#include <drizzled/field/num.h>
877
759
#include <drizzled/field/timetype.h>
878
760
#include <drizzled/field/timestamp.h>
879
761
#include <drizzled/field/datetime.h>
890
772
#define FIELDFLAG_DECIMAL_POSITION      4
891
773
#define FIELDFLAG_PACK                  120     // Bits used for packing
892
774
#define FIELDFLAG_INTERVAL              256     // mangled with decimals!
893
 
#define FIELDFLAG_BITFIELD              512     // mangled with decimals!
894
775
#define FIELDFLAG_BLOB                  1024    // mangled with decimals!
895
 
#define FIELDFLAG_GEOM                  2048    // mangled with decimals!
896
 
 
897
 
#define FIELDFLAG_TREAT_BIT_AS_CHAR     4096    /* use Field_bit_as_char */
898
 
 
899
 
#define FIELDFLAG_LEFT_FULLSCREEN       8192
900
 
#define FIELDFLAG_RIGHT_FULLSCREEN      16384
901
 
#define FIELDFLAG_FORMAT_NUMBER         16384   // predit: ###,,## in output
 
776
 
902
777
#define FIELDFLAG_NO_DEFAULT            16384   /* sql */
903
 
#define FIELDFLAG_SUM                   ((uint) 32768)// predit: +#fieldflag
904
 
#define FIELDFLAG_MAYBE_NULL            ((uint) 32768)// sql
905
 
#define FIELDFLAG_HEX_ESCAPE            ((uint) 0x10000)
 
778
#define FIELDFLAG_SUM                   ((uint32_t) 32768)// predit: +#fieldflag
 
779
#define FIELDFLAG_MAYBE_NULL            ((uint32_t) 32768)// sql
 
780
#define FIELDFLAG_HEX_ESCAPE            ((uint32_t) 0x10000)
906
781
#define FIELDFLAG_PACK_SHIFT            3
907
782
#define FIELDFLAG_DEC_SHIFT             8
908
783
#define FIELDFLAG_MAX_DEC               31
909
 
#define FIELDFLAG_NUM_SCREEN_TYPE       0x7F01
910
 
#define FIELDFLAG_ALFA_SCREEN_TYPE      0x7800
911
784
 
912
785
#define MTYP_TYPENR(type) (type & 127)  /* Remove bits from type */
913
786
 
920
793
#define f_is_alpha(x)           (!f_is_num(x))
921
794
#define f_is_binary(x)          ((x) & FIELDFLAG_BINARY) // 4.0- compatibility
922
795
#define f_is_enum(x)            (((x) & (FIELDFLAG_INTERVAL | FIELDFLAG_NUMBER)) == FIELDFLAG_INTERVAL)
923
 
#define f_is_bitfield(x)        (((x) & (FIELDFLAG_BITFIELD | FIELDFLAG_NUMBER)) == FIELDFLAG_BITFIELD)
924
796
#define f_is_blob(x)            (((x) & (FIELDFLAG_BLOB | FIELDFLAG_NUMBER)) == FIELDFLAG_BLOB)
925
797
#define f_is_equ(x)             ((x) & (1+2+FIELDFLAG_PACK+31*256))
926
 
#define f_settype(x)            (((int) x) << FIELDFLAG_PACK_SHIFT)
 
798
#define f_settype(x)   (((int) x) << FIELDFLAG_PACK_SHIFT)
927
799
#define f_maybe_null(x)         (x & FIELDFLAG_MAYBE_NULL)
928
800
#define f_no_default(x)         (x & FIELDFLAG_NO_DEFAULT)
929
 
#define f_bit_as_char(x)        ((x) & FIELDFLAG_TREAT_BIT_AS_CHAR)
930
801
#define f_is_hex_escape(x)      ((x) & FIELDFLAG_HEX_ESCAPE)
931
802
 
 
803
bool
 
804
check_string_copy_error(Field_str *field,
 
805
                        const char *well_formed_error_pos,
 
806
                        const char *cannot_convert_error_pos,
 
807
                        const char *end,
 
808
                        const CHARSET_INFO * const cs);
 
809
 
 
810
#endif /* DRIZZLED_FIELD_H */