23
22
* @file This file implements the Field class and API
30
#include <drizzled/sql_select.h>
31
#include <drizzled/error.h>
32
#include <drizzled/field/str.h>
33
#include <drizzled/field/num.h>
34
#include <drizzled/field/blob.h>
35
#include <drizzled/field/boolean.h>
36
#include <drizzled/field/enum.h>
37
#include <drizzled/field/null.h>
38
#include <drizzled/field/date.h>
39
#include <drizzled/field/decimal.h>
40
#include <drizzled/field/real.h>
41
#include <drizzled/field/double.h>
42
#include <drizzled/field/int32.h>
43
#include <drizzled/field/int64.h>
44
#include <drizzled/field/num.h>
45
#include <drizzled/field/time.h>
46
#include <drizzled/field/epoch.h>
47
#include <drizzled/field/datetime.h>
48
#include <drizzled/field/microtime.h>
49
#include <drizzled/field/varstring.h>
50
#include <drizzled/field/uuid.h>
51
#include <drizzled/time_functions.h>
52
#include <drizzled/internal/m_string.h>
53
#include <drizzled/table.h>
54
#include <drizzled/util/test.h>
55
#include <drizzled/session.h>
56
#include <drizzled/current_session.h>
57
#include <drizzled/display.h>
58
#include <drizzled/typelib.h>
29
#include "drizzled/sql_select.h"
30
#include "drizzled/error.h"
31
#include "drizzled/field/str.h"
32
#include "drizzled/field/num.h"
33
#include "drizzled/field/blob.h"
34
#include "drizzled/field/enum.h"
35
#include "drizzled/field/null.h"
36
#include "drizzled/field/date.h"
37
#include "drizzled/field/decimal.h"
38
#include "drizzled/field/real.h"
39
#include "drizzled/field/double.h"
40
#include "drizzled/field/int32.h"
41
#include "drizzled/field/int64.h"
42
#include "drizzled/field/num.h"
43
#include "drizzled/field/timestamp.h"
44
#include "drizzled/field/datetime.h"
45
#include "drizzled/field/varstring.h"
46
#include "drizzled/field/uuid.h"
47
#include "drizzled/time_functions.h"
48
#include "drizzled/internal/m_string.h"
50
#include "drizzled/display.h"
749
bool Field::is_null(ptrdiff_t row_offset) const
564
bool Field::is_null(ptrdiff_t row_offset)
751
566
return null_ptr ?
752
567
(null_ptr[row_offset] & null_bit ? true : false) :
756
bool Field::is_real_null(ptrdiff_t row_offset) const
571
bool Field::is_real_null(ptrdiff_t row_offset)
758
573
return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : false;
761
bool Field::is_null_in_record(const unsigned char *record) const
576
bool Field::is_null_in_record(const unsigned char *record)
765
580
return test(record[(uint32_t) (null_ptr -table->getInsertRecord())] & null_bit);
768
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset) const
583
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset)
831
646
unsigned char *null_ptr_arg,
832
647
unsigned char null_bit_arg,
833
648
utype unireg_check_arg,
834
const char *field_name_arg) :
649
const char *field_name_arg)
836
652
null_ptr(null_ptr_arg),
838
654
orig_table(NULL),
839
655
field_name(field_name_arg),
840
comment(NULL_LEX_STRING),
843
658
part_of_key_not_clustered(0),
844
659
part_of_sortkey(0),
845
660
unireg_check(unireg_check_arg),
846
661
field_length(length_arg),
847
flags(null_ptr ? 0: NOT_NULL_FLAG),
849
662
null_bit(null_bit_arg),
850
663
is_created_from_null_item(false)
665
flags= null_ptr ? 0: NOT_NULL_FLAG;
666
comment.str= (char*) "";
854
void Field::hash(uint32_t *nr, uint32_t *nr2) const
671
void Field::hash(uint32_t *nr, uint32_t *nr2)
978
795
field->decimals= 0;
981
int64_t Field::convert_decimal2int64_t(const type::Decimal *val, bool, int *err)
798
int64_t Field::convert_decimal2int64_t(const my_decimal *val, bool, int *err)
984
if (warn_if_overflow(val->val_int32(E_DEC_ERROR &
801
if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
985
802
~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
988
805
i= (val->sign() ? INT64_MIN : INT64_MAX);
1012
829
return copy->length+ store_length;
1015
bool Field::get_date(type::Time <ime, uint32_t fuzzydate) const
1017
char buff[type::Time::MAX_STRING_LENGTH];
1018
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1020
assert(getTable() and getTable()->getSession());
1022
if (not (res= val_str_internal(&tmp)) or
1023
str_to_datetime_with_warn(getTable()->getSession(),
1024
res->ptr(), res->length(),
1025
<ime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
1033
bool Field::get_time(type::Time <ime) const
1035
char buff[type::Time::MAX_STRING_LENGTH];
1036
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1038
if (not (res= val_str_internal(&tmp)) or
1039
str_to_time_with_warn(getTable()->getSession(), res->ptr(), res->length(), <ime))
1047
int Field::store_time(type::Time <ime, type::timestamp_t)
1053
return store(tmp.ptr(), tmp.length(), &my_charset_bin);
832
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
835
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
836
if (!(res=val_str_internal(&tmp)) || str_to_datetime_with_warn(res->ptr(), res->length(),
837
ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
845
bool Field::get_time(DRIZZLE_TIME *ltime)
848
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
850
if (!(res=val_str_internal(&tmp)) || str_to_time_with_warn(res->ptr(), res->length(), ltime))
858
int Field::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
860
char buff[MAX_DATE_STRING_REP_LENGTH];
861
uint32_t length= (uint32_t) my_TIME_to_str(ltime, buff);
862
return store(buff, length, &my_charset_bin);
1056
865
bool Field::optimize_range(uint32_t idx, uint32_t)
1122
931
if (!Field::eq_def(field))
1125
933
TYPELIB *from_lib=((Field_enum*) field)->typelib;
1127
935
if (typelib->count < from_lib->count)
1130
937
for (uint32_t i=0 ; i < from_lib->count ; i++)
1132
938
if (my_strnncoll(field_charset,
1133
939
(const unsigned char*)typelib->type_names[i],
1134
940
strlen(typelib->type_names[i]),
1135
941
(const unsigned char*)from_lib->type_names[i],
1136
942
strlen(from_lib->type_names[i])))
1146
950
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1147
951
case DRIZZLE_TYPE_UUID: return field::Uuid::max_string_length();
1148
case DRIZZLE_TYPE_MICROTIME: return field::Microtime::max_string_length();
1149
case DRIZZLE_TYPE_TIMESTAMP: return field::Epoch::max_string_length();
1150
case DRIZZLE_TYPE_BOOLEAN: return field::Boolean::max_string_length();
1151
952
case DRIZZLE_TYPE_DATE:
1152
953
case DRIZZLE_TYPE_ENUM:
1153
954
case DRIZZLE_TYPE_LONG: return 4;
1154
955
case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1155
case DRIZZLE_TYPE_TIME:
1156
956
case DRIZZLE_TYPE_DATETIME:
957
case DRIZZLE_TYPE_TIMESTAMP:
1157
958
case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1158
959
case DRIZZLE_TYPE_NULL: return 0;
1159
960
case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
1160
961
case DRIZZLE_TYPE_DECIMAL:
1204
1006
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1205
drizzled::error_t code,
1206
1008
const char *str,
1207
1009
uint32_t str_length,
1208
type::timestamp_t ts_type,
1010
enum enum_drizzle_timestamp_type ts_type,
1209
1011
int cuted_increment)
1211
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1213
if ((session->abortOnWarning() and
1013
Session *session= table ? table->in_use : current_session;
1014
if ((session->really_abort_on_warning() &&
1214
1015
level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
1215
1016
set_warning(level, code, cuted_increment))
1216
1017
make_truncated_value_warning(session, level, str, str_length, ts_type,
1220
1021
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1221
drizzled::error_t code,
1223
type::timestamp_t ts_type,
1024
enum enum_drizzle_timestamp_type ts_type,
1224
1025
int cuted_increment)
1226
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1228
if (session->abortOnWarning() or
1027
Session *session= table ? table->in_use : current_session;
1028
if (session->really_abort_on_warning() ||
1229
1029
set_warning(level, code, cuted_increment))
1231
char str_nr[DECIMAL_LONGLONG_DIGITS];
1232
1032
char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
1233
1033
make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
1234
1034
ts_type, field_name);
1238
1038
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1239
const drizzled::error_t code,
1039
const uint32_t code,
1241
type::timestamp_t ts_type)
1041
enum enum_drizzle_timestamp_type ts_type)
1243
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1245
if (session->abortOnWarning() or
1043
Session *session= table ? table->in_use : current_session;
1044
if (session->really_abort_on_warning() ||
1246
1045
set_warning(level, code, 1))
1248
1047
/* DBL_DIG is enough to print '-[digits].E+###' */
1279
1078
table->clearWriteSet(field_index);
1282
void Field::pack_num(uint64_t arg, unsigned char *destination)
1284
if (not destination)
1287
int64_tstore(destination, arg);
1290
void Field::pack_num(uint32_t arg, unsigned char *destination)
1292
if (not destination)
1295
longstore(destination, arg);
1298
uint64_t Field::unpack_num(uint64_t &destination, const unsigned char *arg) const
1303
int64_tget(destination, arg);
1308
uint32_t Field::unpack_num(uint32_t &destination, const unsigned char *arg) const
1313
longget(destination, arg);
1318
1081
std::ostream& operator<<(std::ostream& output, const Field &field)
1320
1083
output << "Field:(";