1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
4
* Copyright (C) 2008 Sun Microsystems
6
6
* This program is free software; you can redistribute it and/or modify
7
7
* it under the terms of the GNU General Public License as published by
22
22
variables must declare the size_of() member function.
27
25
#ifndef DRIZZLED_FIELD_H
28
26
#define DRIZZLED_FIELD_H
30
#include <drizzled/sql_error.h>
31
#include <drizzled/type/decimal.h>
32
#include <drizzled/key_map.h>
33
#include <drizzled/sql_list.h>
34
#include <drizzled/structs.h>
35
#include <drizzled/charset_info.h>
36
#include <drizzled/item_result.h>
37
#include <drizzled/charset_info.h>
28
#include "drizzled/sql_error.h"
29
#include "drizzled/decimal.h"
30
#include "drizzled/key_map.h"
31
#include "drizzled/sql_bitmap.h"
32
#include "drizzled/sql_list.h"
33
#include "drizzled/structs.h"
34
#include "drizzled/charset_info.h"
35
#include "drizzled/item_result.h"
42
#include <drizzled/visibility.h>
51
47
#define ASSERT_COLUMN_MARKED_FOR_READ assert(!getTable() || (getTable()->read_set == NULL || isReadSet()))
52
48
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(!getTable() || (getTable()->write_set == NULL || isWriteSet()))
54
#define ASSERT_COLUMN_MARKED_FOR_READ assert(getTable())
55
#define ASSERT_COLUMN_MARKED_FOR_WRITE assert(getTable())
50
#define ASSERT_COLUMN_MARKED_FOR_READ
51
#define ASSERT_COLUMN_MARKED_FOR_WRITE
58
54
typedef struct st_typelib TYPELIB;
68
64
int field_conv(Field *to,Field *from);
66
inline uint32_t get_enum_pack_length(int elements)
68
return elements < 256 ? 1 : 2;
71
72
* Class representing a Field in a Table
80
81
* The store_xxx() methods take various input and convert
81
82
* the input into the raw bytes stored in the ptr member variable.
83
class DRIZZLED_API Field
85
86
/* Prevent use of these */
86
87
Field(const Field&);
87
88
void operator=(Field &);
90
90
unsigned char *ptr; /**< Position to field in record. Stores raw field value */
91
91
unsigned char *null_ptr; /**< Byte where null_bit is */
147
146
utype unireg_check;
148
147
uint32_t field_length; /**< Length of this field in bytes */
151
bool isUnsigned() const
153
return flags & UNSIGNED_FLAG;
157
149
uint16_t field_index; /**< Index of this Field in Table::fields array */
161
uint16_t position() const
166
void setPosition(uint32_t arg)
171
150
unsigned char null_bit; /**< Bit used to test null bit */
173
152
If true, this field was created in create_tmp_field_from_item from a NULL
183
162
static void *operator new(size_t size, memory::Root *mem_root);
184
163
static void operator delete(void *, size_t)
186
static void operator delete(void *, memory::Root *)
189
166
Field(unsigned char *ptr_arg,
190
167
uint32_t length_arg,
193
170
utype unireg_check_arg,
194
171
const char *field_name_arg);
195
172
virtual ~Field() {}
197
bool hasDefault() const
199
return not (flags & NO_DEFAULT_VALUE_FLAG);
202
173
/* Store functions returns 1 on overflow and -1 on fatal error */
203
174
virtual int store(const char *to,
205
176
const CHARSET_INFO * const cs)=0;
206
177
virtual int store(double nr)=0;
207
178
virtual int store(int64_t nr, bool unsigned_val)=0;
208
virtual int store_decimal(const type::Decimal *d)=0;
209
int store_and_check(enum_check_fields check_level,
212
const CHARSET_INFO * const cs);
179
virtual int store_decimal(const my_decimal *d)=0;
180
int store(const char *to,
182
const CHARSET_INFO * const cs,
183
enum_check_fields check_level);
214
185
This is called when storing a date in a string.
217
188
Needs to be changed if/when we want to support different time formats.
219
virtual int store_time(type::Time <ime, type::timestamp_t t_type);
220
virtual double val_real() const=0;
221
virtual int64_t val_int() const =0;
222
virtual type::Decimal *val_decimal(type::Decimal *) const;
223
String *val_str_internal(String *str) const
190
virtual int store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type t_type);
191
virtual double val_real(void)=0;
192
virtual int64_t val_int(void)=0;
193
virtual my_decimal *val_decimal(my_decimal *);
194
inline String *val_str(String *str)
225
196
return val_str(str, str);
229
199
val_str(buf1, buf2) gets two buffers and should use them as follows:
230
200
if it needs a temp buffer to convert result to string - use buf1
237
207
an unnecessary free (and later, may be an alloc).
238
208
This trickery is used to decrease a number of malloc calls.
240
virtual String *val_str(String*, String *) const =0;
210
virtual String *val_str(String*, String *)=0;
243
212
str_needs_quotes() returns true if the value returned by val_str() needs
244
213
to be quoted when used in constructing an SQL query.
247
216
virtual Item_result result_type () const=0;
248
217
virtual Item_result cmp_type () const { return result_type(); }
249
218
virtual Item_result cast_to_int_type () const { return result_type(); }
252
220
Check whether a field type can be partially indexed by a key.
295
263
virtual bool eq_def(Field *field);
297
virtual bool is_timestamp() const
303
266
* Returns size (in bytes) used to store field data in memory
304
267
* (i.e. it returns the maximum size of the field in a row of the table,
342
305
virtual uint32_t key_length() const;
343
306
virtual enum_field_types type() const =0;
344
307
virtual enum_field_types real_type() const;
345
virtual int cmp_max(const unsigned char *a, const unsigned char *b, uint32_t max_len);
308
inline int cmp(const unsigned char *str) { return cmp(ptr,str); }
309
virtual int cmp_max(const unsigned char *a, const unsigned char *b,
346
311
virtual int cmp(const unsigned char *,const unsigned char *)=0;
347
int cmp_internal(const unsigned char *str) { return cmp(ptr,str); }
348
312
virtual int cmp_binary(const unsigned char *a,const unsigned char *b,
349
313
uint32_t max_length=UINT32_MAX);
350
314
virtual int cmp_offset(uint32_t row_offset);
364
328
virtual uint32_t size_of() const =0;
366
bool is_null(ptrdiff_t row_offset= 0) const;
367
bool is_real_null(ptrdiff_t row_offset= 0) const;
368
bool is_null_in_record(const unsigned char *record) const;
369
bool is_null_in_record_with_offset(ptrdiff_t offset) const;
330
bool is_null(ptrdiff_t row_offset= 0);
331
bool is_real_null(ptrdiff_t row_offset= 0);
332
bool is_null_in_record(const unsigned char *record);
333
bool is_null_in_record_with_offset(ptrdiff_t offset);
370
334
void set_null(ptrdiff_t row_offset= 0);
371
335
void set_notnull(ptrdiff_t row_offset= 0);
372
bool maybe_null(void) const;
373
bool real_maybe_null(void) const;
336
bool maybe_null(void);
337
bool real_maybe_null(void);
375
339
virtual void make_field(SendField *);
376
340
virtual void sort_string(unsigned char *buff,uint32_t length)=0;
396
360
uint32_t new_null_bit);
397
361
/** This is used to generate a field in Table from TableShare */
398
362
Field *clone(memory::Root *mem_root, Table *new_table);
399
void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
363
inline void move_field(unsigned char *ptr_arg,unsigned char *null_ptr_arg,unsigned char null_bit_arg)
402
366
null_ptr= null_ptr_arg;
403
367
null_bit= null_bit_arg;
405
void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
369
inline void move_field(unsigned char *ptr_arg) { ptr=ptr_arg; }
406
370
virtual void move_field_offset(ptrdiff_t ptr_diff)
408
372
ptr= ADD_TO_PTR(ptr,ptr_diff, unsigned char*);
471
int64_t val_int_internal(const unsigned char *new_ptr)
435
inline int64_t val_int(const unsigned char *new_ptr)
473
437
unsigned char *old_ptr= ptr;
438
int64_t return_value;
474
439
ptr= const_cast<unsigned char*>(new_ptr);
475
int64_t return_value= val_int();
440
return_value= val_int();
477
442
return return_value;
480
String *val_str_internal(String *str, const unsigned char *new_ptr)
444
inline String *val_str(String *str, const unsigned char *new_ptr)
482
446
unsigned char *old_ptr= ptr;
483
447
ptr= const_cast<unsigned char*>(new_ptr);
484
val_str_internal(str);
590
554
return max_length;
593
uint32_t offset(const unsigned char *record)
557
inline uint32_t offset(const unsigned char *record)
595
559
return (uint32_t) (ptr - record);
597
561
void copy_from_tmp(int offset);
598
562
uint32_t fill_cache_field(CacheField *copy);
599
virtual bool get_date(type::Time <ime,uint32_t fuzzydate) const;
600
virtual bool get_time(type::Time <ime) const;
563
virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
564
virtual bool get_time(DRIZZLE_TIME *ltime);
601
565
virtual const CHARSET_INFO *charset(void) const { return &my_charset_bin; }
602
566
virtual const CHARSET_INFO *sort_charset(void) const { return charset(); }
603
567
virtual bool has_charset(void) const { return false; }
649
613
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
650
drizzled::error_t code,
652
616
uint32_t str_len,
653
type::timestamp_t ts_type,
617
enum enum_drizzle_timestamp_type ts_type,
654
618
int cuted_increment);
656
620
Produce warning or note about integer datetime value saved into field.
669
633
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
670
drizzled::error_t code,
672
type::timestamp_t ts_type,
636
enum enum_drizzle_timestamp_type ts_type,
673
637
int cuted_increment);
675
639
Produce warning or note about double datetime data saved into field.
687
651
void set_datetime_warning(DRIZZLE_ERROR::enum_warning_level,
688
const drizzled::error_t code,
690
type::timestamp_t ts_type);
691
bool check_overflow(int op_result)
654
enum enum_drizzle_timestamp_type ts_type);
655
inline bool check_overflow(int op_result)
693
657
return (op_result == E_DEC_OVERFLOW);
723
687
value converted from val
725
int64_t convert_decimal2int64_t(const type::Decimal *val,
689
int64_t convert_decimal2int64_t(const my_decimal *val,
726
690
bool unsigned_flag,
728
692
/* The max. number of characters */
729
uint32_t char_length() const
693
inline uint32_t char_length() const
731
695
return field_length / charset()->mbmaxlen;
734
enum column_format_type column_format() const
698
inline enum column_format_type column_format() const
736
700
return (enum column_format_type)
737
701
((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
741
virtual void hash(uint32_t *nr, uint32_t *nr2) const;
705
virtual void hash(uint32_t *nr, uint32_t *nr2);
742
706
friend bool reopen_table(Session *,Table *,bool);
744
708
friend class CopyField;
754
718
friend class Item_sum_max;
755
719
friend class Item_func_group_concat;
757
bool isReadSet() const;
758
722
bool isWriteSet();
759
723
void setReadSet(bool arg= true);
760
724
void setWriteSet(bool arg= true);
764
void pack_num(uint64_t arg, unsigned char *destination= NULL);
765
void pack_num(uint32_t arg, unsigned char *destination= NULL);
766
uint64_t unpack_num(uint64_t &destination, const unsigned char *arg= NULL) const;
767
uint32_t unpack_num(uint32_t &destination, const unsigned char *arg= NULL) const;
772
inline bool isDateTime(const enum_field_types &arg)
776
case DRIZZLE_TYPE_DATE:
777
case DRIZZLE_TYPE_DATETIME:
778
case DRIZZLE_TYPE_MICROTIME:
779
case DRIZZLE_TYPE_TIME:
780
case DRIZZLE_TYPE_TIMESTAMP:
783
case DRIZZLE_TYPE_BLOB:
784
case DRIZZLE_TYPE_BOOLEAN:
785
case DRIZZLE_TYPE_DECIMAL:
786
case DRIZZLE_TYPE_DOUBLE:
787
case DRIZZLE_TYPE_ENUM:
788
case DRIZZLE_TYPE_LONG:
789
case DRIZZLE_TYPE_LONGLONG:
790
case DRIZZLE_TYPE_NULL:
791
case DRIZZLE_TYPE_UUID:
792
case DRIZZLE_TYPE_VARCHAR:
802
std::ostream& operator<<(std::ostream& output, const Field &field);
804
727
} /* namespace drizzled */
806
729
/** @TODO Why is this in the middle of the file???*/
807
#include <drizzled/create_field.h>
730
#include "drizzled/create_field.h"
809
732
namespace drizzled
760
* A class for quick copying data to fields
762
class CopyField :public memory::SqlAlloc
765
Convenience definition of a copy function returned by
768
typedef void Copy_func(CopyField*);
769
Copy_func *get_copy_func(Field *to, Field *from);
771
unsigned char *from_ptr;
772
unsigned char *to_ptr;
773
unsigned char *from_null_ptr;
774
unsigned char *to_null_ptr;
778
uint32_t from_length;
782
String tmp; // For items
786
void set(Field *to,Field *from,bool save); // Field to field
787
void set(unsigned char *to,Field *from); // Field to string
788
void (*do_copy)(CopyField *);
789
void (*do_copy2)(CopyField *); // Used to handle null values
836
792
uint32_t pack_length_to_packflag(uint32_t type);
837
793
uint32_t calc_pack_length(enum_field_types type,uint32_t length);
838
794
int set_field_to_null(Field *field);