32
30
#include "drizzled/field/str.h"
33
31
#include "drizzled/field/num.h"
34
32
#include "drizzled/field/blob.h"
35
#include "drizzled/field/boolean.h"
36
33
#include "drizzled/field/enum.h"
37
34
#include "drizzled/field/null.h"
38
35
#include "drizzled/field/date.h"
39
36
#include "drizzled/field/decimal.h"
40
37
#include "drizzled/field/real.h"
41
38
#include "drizzled/field/double.h"
42
#include "drizzled/field/int32.h"
43
#include "drizzled/field/int64.h"
39
#include "drizzled/field/long.h"
40
#include "drizzled/field/int64_t.h"
44
41
#include "drizzled/field/num.h"
45
#include "drizzled/field/time.h"
46
#include "drizzled/field/epoch.h"
42
#include "drizzled/field/timestamp.h"
47
43
#include "drizzled/field/datetime.h"
48
#include "drizzled/field/microtime.h"
49
44
#include "drizzled/field/varstring.h"
50
#include "drizzled/field/uuid.h"
51
45
#include "drizzled/time_functions.h"
52
46
#include "drizzled/internal/m_string.h"
54
#include "drizzled/display.h"
610
374
void *Field::operator new(size_t size, memory::Root *mem_root)
612
return mem_root->alloc_root(static_cast<uint32_t>(size));
376
return alloc_root(mem_root, static_cast<uint32_t>(size));
615
379
enum_field_types Field::field_type_merge(enum_field_types a,
616
380
enum_field_types b)
618
assert(a < enum_field_types_size);
619
assert(b < enum_field_types_size);
382
assert(a <= DRIZZLE_TYPE_MAX);
383
assert(b <= DRIZZLE_TYPE_MAX);
620
384
return field_types_merge_rules[a][b];
623
387
Item_result Field::result_merge_type(enum_field_types field_type)
625
assert(field_type < enum_field_types_size);
389
assert(field_type <= DRIZZLE_TYPE_MAX);
626
390
return field_types_result_type[field_type];
819
593
void Field::init(Table *table_arg)
821
595
orig_table= table= table_arg;
596
table_name= &table_arg->alias;
599
String *Field::val_int_as_str(String *val_buffer, bool unsigned_val)
601
const CHARSET_INFO * const cs= &my_charset_bin;
603
int64_t value= val_int();
605
if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
607
length= (uint32_t) (*cs->cset->int64_t10_to_str)(cs, (char*) val_buffer->ptr(),
608
MY_INT64_NUM_DECIMAL_DIGITS,
609
unsigned_val ? 10 : -10,
611
val_buffer->length(length);
824
615
/// This is used as a table name when the table structure is not set up
827
618
unsigned char *null_ptr_arg,
828
619
unsigned char null_bit_arg,
829
620
utype unireg_check_arg,
830
const char *field_name_arg) :
621
const char *field_name_arg)
832
624
null_ptr(null_ptr_arg),
834
626
orig_table(NULL),
835
628
field_name(field_name_arg),
836
comment(NULL_LEX_STRING),
839
631
part_of_key_not_clustered(0),
840
632
part_of_sortkey(0),
841
633
unireg_check(unireg_check_arg),
842
634
field_length(length_arg),
843
flags(null_ptr ? 0: NOT_NULL_FLAG),
845
635
null_bit(null_bit_arg),
846
636
is_created_from_null_item(false)
638
flags= null_ptr ? 0: NOT_NULL_FLAG;
639
comment.str= (char*) "";
850
644
void Field::hash(uint32_t *nr, uint32_t *nr2)
876
int Field::store_and_check(enum_check_fields check_level,
879
const CHARSET_INFO * const cs)
670
int Field::compatible_field_size(uint32_t field_metadata)
672
uint32_t const source_size= pack_length_from_metadata(field_metadata);
673
uint32_t const destination_size= row_pack_length();
674
return (source_size <= destination_size);
677
int Field::store(const char *to,
679
const CHARSET_INFO * const cs,
680
enum_check_fields check_level)
883
683
enum_check_fields old_check_level= table->in_use->count_cuted_fields;
936
736
const unsigned char *Field::unpack(unsigned char* to, const unsigned char *from)
938
const unsigned char *result= unpack(to, from, 0U, table->getShare()->db_low_byte_first);
738
const unsigned char *result= unpack(to, from, 0U, table->s->db_low_byte_first);
942
type::Decimal *Field::val_decimal(type::Decimal *)
742
my_decimal *Field::val_decimal(my_decimal *)
944
744
/* This never have to be called */
950
750
void Field::make_field(SendField *field)
952
if (orig_table && orig_table->getShare()->getSchemaName() && *orig_table->getShare()->getSchemaName())
752
if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
954
field->db_name= orig_table->getShare()->getSchemaName();
955
field->org_table_name= orig_table->getShare()->getTableName();
754
field->db_name= orig_table->s->db.str;
755
field->org_table_name= orig_table->s->table_name.str;
958
758
field->org_table_name= field->db_name= "";
961
field->table_name= orig_table->getAlias();
761
field->table_name= orig_table->alias;
962
762
field->org_col_name= field_name;
974
774
field->decimals= 0;
977
int64_t Field::convert_decimal2int64_t(const type::Decimal *val, bool, int *err)
777
int64_t Field::convert_decimal2int64_t(const my_decimal *val, bool, int *err)
980
if (warn_if_overflow(val->val_int32(E_DEC_ERROR &
780
if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
981
781
~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
984
784
i= (val->sign() ? INT64_MIN : INT64_MAX);
1008
808
return copy->length+ store_length;
1011
bool Field::get_date(type::Time <ime, uint32_t fuzzydate)
1013
char buff[type::Time::MAX_STRING_LENGTH];
1014
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1016
assert(getTable() and getTable()->getSession());
1018
if (not (res=val_str_internal(&tmp)) or
1019
str_to_datetime_with_warn(getTable()->getSession(),
1020
res->ptr(), res->length(),
1021
<ime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
1029
bool Field::get_time(type::Time <ime)
1031
char buff[type::Time::MAX_STRING_LENGTH];
1032
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1034
if (not (res= val_str_internal(&tmp)) or
1035
str_to_time_with_warn(getTable()->getSession(), res->ptr(), res->length(), <ime))
1043
int Field::store_time(type::Time <ime, type::timestamp_t)
1049
return store(tmp.ptr(), tmp.length(), &my_charset_bin);
811
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
814
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
815
if (!(res=val_str(&tmp)) ||
816
str_to_datetime_with_warn(res->ptr(), res->length(),
817
ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
822
bool Field::get_time(DRIZZLE_TIME *ltime)
825
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
826
if (!(res=val_str(&tmp)) ||
827
str_to_time_with_warn(res->ptr(), res->length(), ltime))
832
int Field::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
834
char buff[MAX_DATE_STRING_REP_LENGTH];
835
uint32_t length= (uint32_t) my_TIME_to_str(ltime, buff);
836
return store(buff, length, &my_charset_bin);
1052
839
bool Field::optimize_range(uint32_t idx, uint32_t)
1090
877
Field *Field::clone(memory::Root *root, Table *new_table)
1093
if ((tmp= (Field*) root->memdup_root((char*) this,size_of())))
880
if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1095
882
tmp->init(new_table);
1096
tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() -
1097
new_table->getDefaultValues()));
883
tmp->move_field_offset((ptrdiff_t) (new_table->record[0] -
884
new_table->s->default_values));
1118
905
if (!Field::eq_def(field))
1121
907
TYPELIB *from_lib=((Field_enum*) field)->typelib;
1123
909
if (typelib->count < from_lib->count)
1126
911
for (uint32_t i=0 ; i < from_lib->count ; i++)
1128
912
if (my_strnncoll(field_charset,
1129
913
(const unsigned char*)typelib->type_names[i],
1130
914
strlen(typelib->type_names[i]),
1131
915
(const unsigned char*)from_lib->type_names[i],
1132
916
strlen(from_lib->type_names[i])))
1142
924
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1143
case DRIZZLE_TYPE_UUID: return field::Uuid::max_string_length();
1144
case DRIZZLE_TYPE_MICROTIME: return field::Microtime::max_string_length();
1145
case DRIZZLE_TYPE_TIMESTAMP: return field::Epoch::max_string_length();
1146
case DRIZZLE_TYPE_BOOLEAN: return field::Boolean::max_string_length();
1147
case DRIZZLE_TYPE_DATE:
1148
case DRIZZLE_TYPE_ENUM:
925
case DRIZZLE_TYPE_DATE: return 3;
926
case DRIZZLE_TYPE_TIMESTAMP:
1149
927
case DRIZZLE_TYPE_LONG: return 4;
1150
928
case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1151
case DRIZZLE_TYPE_TIME:
1152
929
case DRIZZLE_TYPE_DATETIME:
1153
930
case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1154
931
case DRIZZLE_TYPE_NULL: return 0;
1155
932
case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
933
case DRIZZLE_TYPE_ENUM:
1156
934
case DRIZZLE_TYPE_DECIMAL:
1164
941
uint32_t pack_length_to_packflag(uint32_t type)
1167
944
case 1: return 1 << FIELDFLAG_PACK_SHIFT;
1168
945
case 2: assert(1);
1169
946
case 3: assert(1);
1170
case 4: return f_settype(DRIZZLE_TYPE_LONG);
1171
case 8: return f_settype(DRIZZLE_TYPE_LONGLONG);
947
case 4: return f_settype((uint32_t) DRIZZLE_TYPE_LONG);
948
case 8: return f_settype((uint32_t) DRIZZLE_TYPE_LONGLONG);
1173
950
return 0; // This shouldn't happen
953
Field *make_field(TableShare *share,
956
uint32_t field_length,
958
unsigned char *null_pos,
959
unsigned char null_bit,
961
enum_field_types field_type,
962
const CHARSET_INFO * field_charset,
963
Field::utype unireg_check,
965
const char *field_name)
968
root= current_mem_root();
977
null_bit= ((unsigned char) 1) << null_bit;
982
case DRIZZLE_TYPE_DATE:
983
case DRIZZLE_TYPE_DATETIME:
984
case DRIZZLE_TYPE_TIMESTAMP:
985
field_charset= &my_charset_bin;
989
if (field_type == DRIZZLE_TYPE_VARCHAR ||
990
field_type == DRIZZLE_TYPE_BLOB ||
991
field_type == DRIZZLE_TYPE_ENUM)
993
if (field_type == DRIZZLE_TYPE_VARCHAR)
994
return new (root) Field_varstring(ptr,field_length,
995
HA_VARCHAR_PACKLENGTH(field_length),
1001
if (field_type == DRIZZLE_TYPE_BLOB)
1003
return new (root) Field_blob(ptr,
1008
calc_pack_length(DRIZZLE_TYPE_LONG, 0),
1014
return new (root) Field_enum(ptr,
1019
get_enum_pack_length(interval->count),
1027
case DRIZZLE_TYPE_DECIMAL:
1028
return new (root) Field_decimal(ptr,
1036
false /* is_unsigned */);
1037
case DRIZZLE_TYPE_DOUBLE:
1038
return new (root) Field_double(ptr,
1046
false /* is_unsigned */);
1047
case DRIZZLE_TYPE_LONG:
1048
return new (root) Field_long(ptr,
1055
false /* is_unsigned */);
1056
case DRIZZLE_TYPE_LONGLONG:
1057
return new (root) Field_int64_t(ptr,
1064
false /* is_unsigned */);
1065
case DRIZZLE_TYPE_TIMESTAMP:
1066
return new (root) Field_timestamp(ptr,
1074
case DRIZZLE_TYPE_DATE:
1075
return new (root) Field_date(ptr,
1080
case DRIZZLE_TYPE_DATETIME:
1081
return new (root) Field_datetime(ptr,
1086
case DRIZZLE_TYPE_NULL:
1087
return new (root) Field_null(ptr,
1091
default: // Impossible (Wrong version)
1176
1097
/*****************************************************************************
1177
1098
Warning handling
1178
1099
*****************************************************************************/
1180
1101
bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
1181
drizzled::error_t code,
1182
1103
int cuted_increment)
1200
1121
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1201
drizzled::error_t code,
1202
1123
const char *str,
1203
1124
uint32_t str_length,
1204
type::timestamp_t ts_type,
1125
enum enum_drizzle_timestamp_type ts_type,
1205
1126
int cuted_increment)
1207
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1209
if ((session->abortOnWarning() and
1128
Session *session= table ? table->in_use : current_session;
1129
if ((session->really_abort_on_warning() &&
1210
1130
level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
1211
1131
set_warning(level, code, cuted_increment))
1212
1132
make_truncated_value_warning(session, level, str, str_length, ts_type,
1216
1136
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1217
drizzled::error_t code,
1219
type::timestamp_t ts_type,
1139
enum enum_drizzle_timestamp_type ts_type,
1220
1140
int cuted_increment)
1222
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1224
if (session->abortOnWarning() or
1142
Session *session= table ? table->in_use : current_session;
1143
if (session->really_abort_on_warning() ||
1225
1144
set_warning(level, code, cuted_increment))
1227
char str_nr[DECIMAL_LONGLONG_DIGITS];
1228
1147
char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
1229
1148
make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
1230
1149
ts_type, field_name);
1234
1153
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1235
const drizzled::error_t code,
1154
const uint32_t code,
1237
type::timestamp_t ts_type)
1156
enum enum_drizzle_timestamp_type ts_type)
1239
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1241
if (session->abortOnWarning() or
1158
Session *session= table ? table->in_use : current_session;
1159
if (session->really_abort_on_warning() ||
1242
1160
set_warning(level, code, 1))
1244
1162
/* DBL_DIG is enough to print '-[digits].E+###' */
1245
1163
char str_nr[DBL_DIG + 8];
1246
uint32_t str_len= snprintf(str_nr, sizeof(str_nr), "%g", nr);
1164
uint32_t str_len= sprintf(str_nr, "%g", nr);
1247
1165
make_truncated_value_warning(session, level, str_nr, str_len, ts_type,
1275
1193
table->clearWriteSet(field_index);
1278
void Field::pack_num(uint64_t arg, unsigned char *destination)
1280
if (not destination)
1283
int64_tstore(destination, arg);
1286
void Field::pack_num(uint32_t arg, unsigned char *destination)
1288
if (not destination)
1291
longstore(destination, arg);
1294
uint64_t Field::unpack_num(uint64_t &destination, const unsigned char *arg) const
1299
int64_tget(destination, arg);
1304
uint32_t Field::unpack_num(uint32_t &destination, const unsigned char *arg) const
1309
longget(destination, arg);
1314
std::ostream& operator<<(std::ostream& output, const Field &field)
1316
output << "Field:(";
1317
output << field.field_name;
1319
output << drizzled::display::type(field.real_type());
1322
if (field.flags & NOT_NULL_FLAG)
1323
output << " NOT_NULL";
1325
if (field.flags & PRI_KEY_FLAG)
1326
output << ", PRIMARY KEY";
1328
if (field.flags & UNIQUE_KEY_FLAG)
1329
output << ", UNIQUE KEY";
1331
if (field.flags & MULTIPLE_KEY_FLAG)
1332
output << ", MULTIPLE KEY";
1334
if (field.flags & BLOB_FLAG)
1337
if (field.flags & UNSIGNED_FLAG)
1338
output << ", UNSIGNED";
1340
if (field.flags & BINARY_FLAG)
1341
output << ", BINARY";
1345
return output; // for multiple << operators.
1348
1196
} /* namespace drizzled */