~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.h

  • Committer: Lee
  • Date: 2008-10-03 23:31:06 UTC
  • mfrom: (413.2.3 drizzle)
  • mto: This revision was merged to the branch mainline in revision 459.
  • Revision ID: lbieber@lbieber-desktop-20081003233106-tgvzu0fh25gb3xeg
breaking out enum field classes

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
  variables must declare the size_of() member function.
23
23
*/
24
24
 
 
25
#ifdef USE_PRAGMA_INTERFACE
 
26
#pragma interface                       /* gcc class implementation */
 
27
#endif
25
28
 
26
29
#define DATETIME_DEC                     6
27
30
#define DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE FLOATING_POINT_BUFFER
34
37
struct st_cache_field;
35
38
int field_conv(Field *to,Field *from);
36
39
 
37
 
inline uint32_t get_enum_pack_length(int elements)
 
40
inline uint get_enum_pack_length(int elements)
38
41
{
39
42
  return elements < 256 ? 1 : 2;
40
43
}
41
44
 
42
 
inline uint32_t get_set_pack_length(int elements)
 
45
inline uint get_set_pack_length(int elements)
43
46
{
44
 
  uint32_t len= (elements + 7) / 8;
 
47
  uint len= (elements + 7) / 8;
45
48
  return len > 4 ? 8 : len;
46
49
}
47
50
 
48
 
class virtual_column_info: public Sql_alloc
49
 
{
50
 
public:
51
 
  Item *expr_item;
52
 
  LEX_STRING expr_str;
53
 
  Item *item_free_list;
54
 
  virtual_column_info() 
55
 
  : expr_item(0), item_free_list(0),
56
 
    field_type(DRIZZLE_TYPE_VIRTUAL),
57
 
    is_stored(false), data_inited(false)
58
 
  {
59
 
    expr_str.str= NULL;
60
 
    expr_str.length= 0;
61
 
  };
62
 
  ~virtual_column_info() {}
63
 
  enum_field_types get_real_type()
64
 
  {
65
 
    assert(data_inited);
66
 
    return data_inited ? field_type : DRIZZLE_TYPE_VIRTUAL;
67
 
  }
68
 
  void set_field_type(enum_field_types fld_type)
69
 
  {
70
 
    /* Calling this function can only be done once. */
71
 
    assert(not data_inited);
72
 
    data_inited= true;
73
 
    field_type= fld_type;
74
 
  }
75
 
  bool get_field_stored()
76
 
  {
77
 
    assert(data_inited);
78
 
    return data_inited ? is_stored : true;
79
 
  }
80
 
  void set_field_stored(bool stored)
81
 
  {
82
 
    is_stored= stored;
83
 
  }
84
 
private:
85
 
  /*
86
 
    The following data is only updated by the parser and read
87
 
    when a Create_field object is created/initialized.
88
 
  */
89
 
  enum_field_types field_type;   /* Real field type*/
90
 
  bool is_stored;             /* Indication that the field is 
91
 
                                    phisically stored in the database*/
92
 
  /*
93
 
    This flag is used to prevent other applications from
94
 
    reading and using incorrect data.
95
 
  */
96
 
  bool data_inited; 
97
 
};
98
 
 
99
51
class Field
100
52
{
101
53
  Field(const Item &);                          /* Prevent use of these */
106
58
                              size_t size __attribute__((unused)))
107
59
  { TRASH(ptr_arg, size); }
108
60
 
109
 
  unsigned char         *ptr;                   // Position to field in record
110
 
  unsigned char         *null_ptr;              // Byte where null_bit is
 
61
  uchar         *ptr;                   // Position to field in record
 
62
  uchar         *null_ptr;              // Byte where null_bit is
111
63
  /*
112
64
    Note that you can use table->in_use as replacement for current_thd member 
113
65
    only inside of val_*() and store() members (e.g. you can't use it in cons)
137
89
  uint32_t      field_length;           // Length of field
138
90
  uint32_t      flags;
139
91
  uint16_t        field_index;            // field number in fields array
140
 
  unsigned char         null_bit;               // Bit used to test null bit
 
92
  uchar         null_bit;               // Bit used to test null bit
141
93
  /**
142
94
     If true, this field was created in create_tmp_field_from_item from a NULL
143
95
     value. This means that the type of the field is just a guess, and the type
149
101
   */
150
102
  bool is_created_from_null_item;
151
103
 
152
 
  /* Virtual column data */
153
 
  virtual_column_info *vcol_info;
154
 
  /*
155
 
    Indication that the field is phycically stored in tables 
156
 
    rather than just generated on SQL queries.
157
 
    As of now, false can only be set for generated-only virtual columns.
158
 
  */
159
 
  bool is_stored;
160
 
 
161
 
  Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
162
 
        unsigned char null_bit_arg, 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,
163
106
        const char *field_name_arg);
164
107
  virtual ~Field() {}
165
108
  /* Store functions returns 1 on overflow and -1 on fatal error */
166
 
  virtual int  store(const char *to, uint32_t length, const CHARSET_INFO * const cs)=0;
 
109
  virtual int  store(const char *to, uint length, const CHARSET_INFO * const cs)=0;
167
110
  virtual int  store(double nr)=0;
168
111
  virtual int  store(int64_t nr, bool unsigned_val)=0;
169
112
  virtual int  store_decimal(const my_decimal *d)=0;
170
113
  virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
171
 
  int store(const char *to, uint32_t length, const CHARSET_INFO * const cs,
 
114
  int store(const char *to, uint length, const CHARSET_INFO * const cs,
172
115
            enum_check_fields check_level);
173
116
  virtual double val_real(void)=0;
174
117
  virtual int64_t val_int(void)=0;
219
162
    table, which is located on disk).
220
163
  */
221
164
  virtual uint32_t pack_length_in_rec() const { return pack_length(); }
222
 
  virtual int compatible_field_size(uint32_t field_metadata);
223
 
  virtual uint32_t pack_length_from_metadata(uint32_t field_metadata)
 
165
  virtual int compatible_field_size(uint field_metadata);
 
166
  virtual uint pack_length_from_metadata(uint field_metadata)
224
167
  { return field_metadata; }
225
168
  /*
226
169
    This method is used to return the size of the data in a row-based
234
177
    the classes Field_varstring, and Field_blob return 
235
178
    field_length + 1, field_length, and pack_length_no_ptr() respectfully.
236
179
  */
237
 
  virtual uint32_t row_pack_length() { return 0; }
238
 
  virtual int save_field_metadata(unsigned char *first_byte)
 
180
  virtual uint row_pack_length() { return 0; }
 
181
  virtual int save_field_metadata(uchar *first_byte)
239
182
  { return do_save_field_metadata(first_byte); }
240
183
 
241
184
  /*
268
211
    my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->getDefaultValues() - table->record[0]);
269
212
    memcpy(ptr, ptr + l_offset, pack_length());
270
213
    if (null_ptr)
271
 
      *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
 
214
      *null_ptr= ((*null_ptr & (uchar) ~null_bit) | (null_ptr[l_offset] & null_bit));
272
215
  }
273
216
  virtual bool binary() const { return 1; }
274
217
  virtual bool zero_pack() const { return 1; }
276
219
  virtual uint32_t key_length() const { return pack_length(); }
277
220
  virtual enum_field_types type() const =0;
278
221
  virtual enum_field_types real_type() const { return type(); }
279
 
  inline  int cmp(const unsigned char *str) { return cmp(ptr,str); }
280
 
  virtual int cmp_max(const unsigned char *a, const unsigned char *b,
281
 
                      uint32_t max_len __attribute__((unused)))
 
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)))
282
225
    { return cmp(a, b); }
283
 
  virtual int cmp(const unsigned char *,const unsigned char *)=0;
284
 
  virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
 
226
  virtual int cmp(const uchar *,const uchar *)=0;
 
227
  virtual int cmp_binary(const uchar *a,const uchar *b,
285
228
                         uint32_t  __attribute__((unused)) max_length=UINT32_MAX)
286
229
  { return memcmp(a,b,pack_length()); }
287
 
  virtual int cmp_offset(uint32_t row_offset)
 
230
  virtual int cmp_offset(uint row_offset)
288
231
  { return cmp(ptr,ptr+row_offset); }
289
 
  virtual int cmp_binary_offset(uint32_t row_offset)
 
232
  virtual int cmp_binary_offset(uint row_offset)
290
233
  { return cmp_binary(ptr, ptr+row_offset); };
291
 
  virtual int key_cmp(const unsigned char *a,const unsigned char *b)
 
234
  virtual int key_cmp(const uchar *a,const uchar *b)
292
235
  { return cmp(a, b); }
293
 
  virtual int key_cmp(const unsigned char *str, uint32_t length __attribute__((unused)))
 
236
  virtual int key_cmp(const uchar *str, uint length __attribute__((unused)))
294
237
  { return cmp(ptr,str); }
295
 
  virtual uint32_t decimals() const { return 0; }
 
238
  virtual uint decimals() const { return 0; }
296
239
  /*
297
240
    Caller beware: sql_type can change str.Ptr, so check
298
241
    ptr() to see if it changed if you are using your own buffer
299
242
    in str and restore it with set() if needed
300
243
  */
301
244
  virtual void sql_type(String &str) const =0;
302
 
  virtual uint32_t size_of() const =0;          // For new field
 
245
  virtual uint size_of() const =0;              // For new field
303
246
  inline bool is_null(my_ptrdiff_t row_offset= 0)
304
247
  { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : table->null_row; }
305
248
  inline bool is_real_null(my_ptrdiff_t row_offset= 0)
306
249
    { return null_ptr ? (null_ptr[row_offset] & null_bit ? 1 : 0) : 0; }
307
 
  inline bool is_null_in_record(const unsigned char *record)
 
250
  inline bool is_null_in_record(const uchar *record)
308
251
  {
309
252
    if (!null_ptr)
310
253
      return 0;
311
 
    return test(record[(uint32_t) (null_ptr -table->record[0])] &
 
254
    return test(record[(uint) (null_ptr -table->record[0])] &
312
255
                null_bit);
313
256
  }
314
257
  inline bool is_null_in_record_with_offset(my_ptrdiff_t offset)
320
263
  inline void set_null(my_ptrdiff_t row_offset= 0)
321
264
    { if (null_ptr) null_ptr[row_offset]|= null_bit; }
322
265
  inline void set_notnull(my_ptrdiff_t row_offset= 0)
323
 
    { if (null_ptr) null_ptr[row_offset]&= (unsigned char) ~null_bit; }
 
266
    { if (null_ptr) null_ptr[row_offset]&= (uchar) ~null_bit; }
324
267
  inline bool maybe_null(void) { return null_ptr != 0 || table->maybe_null; }
325
268
  inline bool real_maybe_null(void) { return null_ptr != 0; }
326
269
 
350
293
  }
351
294
 
352
295
  virtual void make_field(Send_field *);
353
 
  virtual void sort_string(unsigned char *buff,uint32_t length)=0;
354
 
  virtual bool optimize_range(uint32_t idx, uint32_t part);
 
296
  virtual void sort_string(uchar *buff,uint length)=0;
 
297
  virtual bool optimize_range(uint idx, uint part);
355
298
  /*
356
299
    This should be true for fields which, when compared with constant
357
300
    items, can be casted to int64_t. In this case we will at 'fix_fields'
364
307
  virtual Field *new_field(MEM_ROOT *root, Table *new_table,
365
308
                           bool keep_type);
366
309
  virtual Field *new_key_field(MEM_ROOT *root, Table *new_table,
367
 
                               unsigned char *new_ptr, unsigned char *new_null_ptr,
368
 
                               uint32_t new_null_bit);
 
310
                               uchar *new_ptr, uchar *new_null_ptr,
 
311
                               uint new_null_bit);
369
312
  Field *clone(MEM_ROOT *mem_root, Table *new_table);
370
 
  inline void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
 
313
  inline void move_field(uchar *ptr_arg,uchar *null_ptr_arg,uchar null_bit_arg)
371
314
  {
372
315
    ptr=ptr_arg; null_ptr=null_ptr_arg; null_bit=null_bit_arg;
373
316
  }
374
 
  inline void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
 
317
  inline void move_field(uchar *ptr_arg) { ptr=ptr_arg; }
375
318
  virtual void move_field_offset(my_ptrdiff_t ptr_diff)
376
319
  {
377
 
    ptr=ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
 
320
    ptr=ADD_TO_PTR(ptr,ptr_diff, uchar*);
378
321
    if (null_ptr)
379
 
      null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,unsigned char*);
 
322
      null_ptr=ADD_TO_PTR(null_ptr,ptr_diff,uchar*);
380
323
  }
381
 
  virtual void get_image(unsigned char *buff, uint32_t length,
 
324
  virtual void get_image(uchar *buff, uint length,
382
325
                         const CHARSET_INFO * const cs __attribute__((unused)))
383
326
    { memcpy(buff,ptr,length); }
384
 
  virtual void set_image(const unsigned char *buff,uint32_t length,
 
327
  virtual void set_image(const uchar *buff,uint length,
385
328
                         const CHARSET_INFO * const cs __attribute__((unused)))
386
329
    { memcpy(ptr,buff,length); }
387
330
 
412
355
      Number of copied bytes (excluding padded zero bytes -- see above).
413
356
  */
414
357
 
415
 
  virtual uint32_t get_key_image(unsigned char *buff, uint32_t length,
 
358
  virtual uint get_key_image(uchar *buff, uint length,
416
359
                             imagetype type __attribute__((unused)))
417
360
  {
418
361
    get_image(buff, length, &my_charset_bin);
419
362
    return length;
420
363
  }
421
 
  virtual void set_key_image(const unsigned char *buff,uint32_t length)
 
364
  virtual void set_key_image(const uchar *buff,uint length)
422
365
    { set_image(buff,length, &my_charset_bin); }
423
 
  inline int64_t val_int_offset(uint32_t row_offset)
 
366
  inline int64_t val_int_offset(uint row_offset)
424
367
    {
425
368
      ptr+=row_offset;
426
369
      int64_t tmp=val_int();
427
370
      ptr-=row_offset;
428
371
      return tmp;
429
372
    }
430
 
  inline int64_t val_int(const unsigned char *new_ptr)
 
373
  inline int64_t val_int(const uchar *new_ptr)
431
374
  {
432
 
    unsigned char *old_ptr= ptr;
 
375
    uchar *old_ptr= ptr;
433
376
    int64_t return_value;
434
 
    ptr= (unsigned char*) new_ptr;
 
377
    ptr= (uchar*) new_ptr;
435
378
    return_value= val_int();
436
379
    ptr= old_ptr;
437
380
    return return_value;
438
381
  }
439
 
  inline String *val_str(String *str, const unsigned char *new_ptr)
 
382
  inline String *val_str(String *str, const uchar *new_ptr)
440
383
  {
441
 
    unsigned char *old_ptr= ptr;
442
 
    ptr= (unsigned char*) new_ptr;
 
384
    uchar *old_ptr= ptr;
 
385
    ptr= (uchar*) new_ptr;
443
386
    val_str(str);
444
387
    ptr= old_ptr;
445
388
    return str;
446
389
  }
447
390
  virtual bool send_binary(Protocol *protocol);
448
391
 
449
 
  virtual unsigned char *pack(unsigned char *to, const unsigned char *from,
450
 
                      uint32_t max_length, bool low_byte_first);
451
 
  /**
452
 
     @overload Field::pack(unsigned char*, const unsigned char*, uint32_t, bool)
453
 
  */
454
 
  unsigned char *pack(unsigned char *to, const unsigned char *from)
455
 
  {
456
 
    unsigned char *result= this->pack(to, from, UINT_MAX, table->s->db_low_byte_first);
457
 
    return(result);
458
 
  }
459
 
 
460
 
  virtual const unsigned char *unpack(unsigned char* to, const unsigned char *from,
461
 
                              uint32_t param_data, bool low_byte_first);
462
 
  /**
463
 
     @overload Field::unpack(unsigned char*, const unsigned char*, uint32_t, bool)
464
 
  */
465
 
  const unsigned char *unpack(unsigned char* to, const unsigned char *from)
466
 
  {
467
 
    const unsigned char *result= unpack(to, from, 0U, table->s->db_low_byte_first);
468
 
    return(result);
469
 
  }
470
 
 
471
 
  virtual unsigned char *pack_key(unsigned char* to, const unsigned char *from,
472
 
                          uint32_t max_length, bool low_byte_first)
473
 
  {
474
 
    return pack(to, from, max_length, low_byte_first);
475
 
  }
476
 
  virtual unsigned char *pack_key_from_key_image(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 const unsigned char *unpack_key(unsigned char* to, const unsigned char *from,
482
 
                                  uint32_t max_length, bool low_byte_first)
 
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)
483
426
  {
484
427
    return unpack(to, from, max_length, low_byte_first);
485
428
  }
486
 
  virtual uint32_t packed_col_length(const unsigned char *to __attribute__((unused)),
487
 
                                 uint32_t length)
 
429
  virtual uint packed_col_length(const uchar *to __attribute__((unused)),
 
430
                                 uint length)
488
431
  { return length;}
489
 
  virtual uint32_t max_packed_col_length(uint32_t max_length)
 
432
  virtual uint max_packed_col_length(uint max_length)
490
433
  { return max_length;}
491
434
 
492
 
  virtual int pack_cmp(const unsigned char *a,const unsigned char *b,
493
 
                       uint32_t key_length_arg __attribute__((unused)),
 
435
  virtual int pack_cmp(const uchar *a,const uchar *b,
 
436
                       uint key_length_arg __attribute__((unused)),
494
437
                       bool insert_or_update __attribute__((unused)))
495
438
  { return cmp(a,b); }
496
 
  virtual int pack_cmp(const unsigned char *b,
497
 
                       uint32_t key_length_arg __attribute__((unused)),
 
439
  virtual int pack_cmp(const uchar *b,
 
440
                       uint key_length_arg __attribute__((unused)),
498
441
                       bool insert_or_update __attribute__((unused)))
499
442
  { return cmp(ptr,b); }
500
 
  uint32_t offset(unsigned char *record)
 
443
  uint offset(uchar *record)
501
444
  {
502
 
    return (uint32_t) (ptr - record);
 
445
    return (uint) (ptr - record);
503
446
  }
504
447
  void copy_from_tmp(int offset);
505
 
  uint32_t fill_cache_field(struct st_cache_field *copy);
506
 
  virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
 
448
  uint fill_cache_field(struct st_cache_field *copy);
 
449
  virtual bool get_date(DRIZZLE_TIME *ltime,uint fuzzydate);
507
450
  virtual bool get_time(DRIZZLE_TIME *ltime);
508
451
  virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
509
452
  virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
516
459
  { }
517
460
  bool set_warning(DRIZZLE_ERROR::enum_warning_level, unsigned int code,
518
461
                   int cuted_increment);
519
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, uint32_t code, 
520
 
                            const char *str, uint32_t str_len,
 
462
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, uint code, 
 
463
                            const char *str, uint str_len,
521
464
                            enum enum_drizzle_timestamp_type ts_type, int cuted_increment);
522
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, uint32_t code, 
 
465
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, uint code, 
523
466
                            int64_t nr, enum enum_drizzle_timestamp_type ts_type,
524
467
                            int cuted_increment);
525
 
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, const uint32_t code, 
 
468
  void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level, const uint code, 
526
469
                            double nr, enum enum_drizzle_timestamp_type ts_type);
527
470
  inline bool check_overflow(int op_result)
528
471
  {
538
481
  /* maximum possible display length */
539
482
  virtual uint32_t max_display_length()= 0;
540
483
 
541
 
  virtual uint32_t is_equal(Create_field *new_field);
 
484
  virtual uint is_equal(Create_field *new_field);
542
485
  /* convert decimal to int64_t with overflow check */
543
486
  int64_t convert_decimal2int64_t(const my_decimal *val, bool unsigned_flag,
544
487
                                    int *err);
557
500
  /* Hash value */
558
501
  virtual void hash(uint32_t *nr, uint32_t *nr2);
559
502
  friend bool reopen_table(THD *,Table *,bool);
560
 
  friend int cre_myisam(char * name, register Table *form, uint32_t options,
 
503
  friend int cre_myisam(char * name, register Table *form, uint options,
561
504
                        uint64_t auto_increment_value);
562
505
  friend class Copy_field;
563
506
  friend class Item_avg_field;
596
539
 
597
540
   @returns 0 no bytes written.
598
541
*/
599
 
  virtual int do_save_field_metadata(unsigned char *metadata_ptr __attribute__((unused)))
 
542
  virtual int do_save_field_metadata(uchar *metadata_ptr __attribute__((unused)))
600
543
  { return 0; }
601
544
};
602
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
 
603
693
/*
604
694
  Create field class for CREATE TABLE
605
695
*/
623
713
    for most of the types, or of bytes for BLOBs or numeric types.
624
714
  */
625
715
  uint32_t char_length;
626
 
  uint32_t  decimals, flags, pack_length, key_length;
 
716
  uint  decimals, flags, pack_length, key_length;
627
717
  Field::utype unireg_check;
628
718
  TYPELIB *interval;                    // Which interval to use
629
719
  TYPELIB *save_interval;               // Temporary copy for the above
633
723
  Field *field;                         // For alter table
634
724
 
635
725
  uint8_t row,col,sc_length,interval_id;        // For rea_create_table
636
 
  uint32_t      offset,pack_flag;
637
 
 
638
 
  /* Virtual column expression statement */
639
 
  virtual_column_info *vcol_info;
640
 
  /*
641
 
    Indication that the field is phycically stored in tables 
642
 
    rather than just generated on SQL queries.
643
 
    As of now, FALSE can only be set for generated-only virtual columns.
644
 
  */
645
 
  bool is_stored;
646
 
 
 
726
  uint  offset,pack_flag;
647
727
  Create_field() :after(0) {}
648
728
  Create_field(Field *field, Field *orig_field);
649
729
  /* Used to make a clone of this object for ALTER/CREATE TABLE */
663
743
                          bool maybe_null, bool is_unsigned);
664
744
 
665
745
  bool init(THD *thd, char *field_name, enum_field_types type, char *length,
666
 
            char *decimals, uint32_t type_modifier, Item *default_value,
 
746
            char *decimals, uint type_modifier, Item *default_value,
667
747
            Item *on_update_value, LEX_STRING *comment, char *change,
668
748
            List<String> *interval_list, const CHARSET_INFO * const cs,
669
 
            uint32_t uint_geom_type,
670
 
            enum column_format_type column_format,
671
 
            virtual_column_info *vcol_info);
 
749
            uint uint_geom_type,
 
750
            enum column_format_type column_format);
672
751
};
673
752
 
674
753
 
682
761
  const char *table_name,*org_table_name;
683
762
  const char *col_name,*org_col_name;
684
763
  uint32_t length;
685
 
  uint32_t charsetnr, flags, decimals;
 
764
  uint charsetnr, flags, decimals;
686
765
  enum_field_types type;
687
766
  Send_field() {}
688
767
};
700
779
  typedef void Copy_func(Copy_field*);
701
780
  Copy_func *get_copy_func(Field *to, Field *from);
702
781
public:
703
 
  unsigned char *from_ptr,*to_ptr;
704
 
  unsigned char *from_null_ptr,*to_null_ptr;
 
782
  uchar *from_ptr,*to_ptr;
 
783
  uchar *from_null_ptr,*to_null_ptr;
705
784
  bool *null_row;
706
 
  uint32_t      from_bit,to_bit;
707
 
  uint32_t from_length,to_length;
 
785
  uint  from_bit,to_bit;
 
786
  uint from_length,to_length;
708
787
  Field *from_field,*to_field;
709
788
  String tmp;                                   // For items
710
789
 
711
790
  Copy_field() {}
712
791
  ~Copy_field() {}
713
792
  void set(Field *to,Field *from,bool save);    // Field to field 
714
 
  void set(unsigned char *to,Field *from);              // Field to string
 
793
  void set(uchar *to,Field *from);              // Field to string
715
794
  void (*do_copy)(Copy_field *);
716
795
  void (*do_copy2)(Copy_field *);               // Used to handle null values
717
796
};
718
797
 
719
798
 
720
 
Field *make_field(TABLE_SHARE *share, unsigned char *ptr, uint32_t field_length,
721
 
                  unsigned char *null_pos, unsigned char null_bit,
722
 
                  uint32_t pack_flag, enum_field_types field_type,
 
799
Field *make_field(TABLE_SHARE *share, uchar *ptr, uint32_t field_length,
 
800
                  uchar *null_pos, uchar null_bit,
 
801
                  uint pack_flag, enum_field_types field_type,
723
802
                  const CHARSET_INFO * cs,
724
803
                  Field::utype unireg_check,
725
804
                  TYPELIB *interval, const char *field_name);
726
 
uint32_t pack_length_to_packflag(uint32_t type);
 
805
uint pack_length_to_packflag(uint type);
727
806
enum_field_types get_blob_type_from_length(uint32_t length);
728
807
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
729
808
int set_field_to_null(Field *field);
730
809
int set_field_to_null_with_conversions(Field *field, bool no_conversions);
731
810
 
732
 
 
733
811
bool
734
 
test_if_important_data(const CHARSET_INFO * const cs, 
735
 
                       const char *str,
736
 
                       const char *strend);
 
812
check_string_copy_error(Field_str *field,
 
813
                        const char *well_formed_error_pos,
 
814
                        const char *cannot_convert_error_pos,
 
815
                        const char *end,
 
816
                        const CHARSET_INFO * const cs);
737
817
 
738
818
/*
739
819
  Field subclasses
740
820
 */
741
 
#include <drizzled/field/str.h>
742
 
#include <drizzled/field/longstr.h>
743
 
#include <drizzled/field/num.h>
744
821
#include <drizzled/field/blob.h>
745
822
#include <drizzled/field/enum.h>
746
823
#include <drizzled/field/null.h>
747
824
#include <drizzled/field/date.h>
748
825
#include <drizzled/field/fdecimal.h>
749
 
#include <drizzled/field/real.h>
750
826
#include <drizzled/field/double.h>
751
827
#include <drizzled/field/long.h>
752
828
#include <drizzled/field/int64_t.h>
753
 
#include <drizzled/field/num.h>
754
829
#include <drizzled/field/timetype.h>
755
830
#include <drizzled/field/timestamp.h>
756
831
#include <drizzled/field/datetime.h>
777
852
#define FIELDFLAG_RIGHT_FULLSCREEN      16384
778
853
#define FIELDFLAG_FORMAT_NUMBER         16384   // predit: ###,,## in output
779
854
#define FIELDFLAG_NO_DEFAULT            16384   /* sql */
780
 
#define FIELDFLAG_SUM                   ((uint32_t) 32768)// predit: +#fieldflag
781
 
#define FIELDFLAG_MAYBE_NULL            ((uint32_t) 32768)// sql
782
 
#define FIELDFLAG_HEX_ESCAPE            ((uint32_t) 0x10000)
 
855
#define FIELDFLAG_SUM                   ((uint) 32768)// predit: +#fieldflag
 
856
#define FIELDFLAG_MAYBE_NULL            ((uint) 32768)// sql
 
857
#define FIELDFLAG_HEX_ESCAPE            ((uint) 0x10000)
783
858
#define FIELDFLAG_PACK_SHIFT            3
784
859
#define FIELDFLAG_DEC_SHIFT             8
785
860
#define FIELDFLAG_MAX_DEC               31
806
881
#define f_bit_as_char(x)        ((x) & FIELDFLAG_TREAT_BIT_AS_CHAR)
807
882
#define f_is_hex_escape(x)      ((x) & FIELDFLAG_HEX_ESCAPE)
808
883
 
809
 
bool
810
 
check_string_copy_error(Field_str *field,
811
 
                        const char *well_formed_error_pos,
812
 
                        const char *cannot_convert_error_pos,
813
 
                        const char *end,
814
 
                        const CHARSET_INFO * const cs);
815
 
 
816
 
 
817
 
class Field_tiny :public Field_num {
818
 
public:
819
 
  Field_tiny(unsigned char *ptr_arg, uint32_t len_arg, unsigned char *null_ptr_arg,
820
 
             unsigned char null_bit_arg,
821
 
             enum utype unireg_check_arg, const char *field_name_arg,
822
 
             bool zero_arg, bool unsigned_arg)
823
 
    :Field_num(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
824
 
               unireg_check_arg, field_name_arg,
825
 
               0, zero_arg,unsigned_arg)
826
 
    {}
827
 
  enum Item_result result_type () const { return INT_RESULT; }
828
 
  enum_field_types type() const { return DRIZZLE_TYPE_TINY;}
829
 
  enum ha_base_keytype key_type() const
830
 
    { return unsigned_flag ? HA_KEYTYPE_BINARY : HA_KEYTYPE_INT8; }
831
 
  int store(const char *to,uint32_t length, const CHARSET_INFO * const charset);
832
 
  int store(double nr);
833
 
  int store(int64_t nr, bool unsigned_val);
834
 
  int reset(void) { ptr[0]=0; return 0; }
835
 
  double val_real(void);
836
 
  int64_t val_int(void);
837
 
  String *val_str(String*,String *);
838
 
  bool send_binary(Protocol *protocol);
839
 
  int cmp(const unsigned char *,const unsigned char *);
840
 
  void sort_string(unsigned char *buff,uint32_t length);
841
 
  uint32_t pack_length() const { return 1; }
842
 
  void sql_type(String &str) const;
843
 
  uint32_t max_display_length() { return 4; }
844
 
 
845
 
  virtual unsigned char *pack(unsigned char* to, const unsigned char *from,
846
 
                      uint32_t max_length __attribute__((unused)),
847
 
                      bool low_byte_first __attribute__((unused)))
848
 
  {
849
 
    *to= *from;
850
 
    return to + 1;
851
 
  }
852
 
 
853
 
  virtual const unsigned char *unpack(unsigned char* to, const unsigned char *from,
854
 
                              uint32_t param_data __attribute__((unused)),
855
 
                              bool low_byte_first __attribute__((unused)))
856
 
  {
857
 
    *to= *from;
858
 
    return from + 1;
859
 
  }
860
 
};