30
32
#include "drizzled/field/str.h"
31
33
#include "drizzled/field/num.h"
32
34
#include "drizzled/field/blob.h"
35
#include "drizzled/field/boolean.h"
33
36
#include "drizzled/field/enum.h"
34
37
#include "drizzled/field/null.h"
35
38
#include "drizzled/field/date.h"
36
39
#include "drizzled/field/decimal.h"
37
40
#include "drizzled/field/real.h"
38
41
#include "drizzled/field/double.h"
39
#include "drizzled/field/long.h"
40
#include "drizzled/field/int64_t.h"
42
#include "drizzled/field/int32.h"
43
#include "drizzled/field/int64.h"
41
44
#include "drizzled/field/num.h"
42
#include "drizzled/field/timestamp.h"
45
#include "drizzled/field/time.h"
46
#include "drizzled/field/epoch.h"
43
47
#include "drizzled/field/datetime.h"
48
#include "drizzled/field/microtime.h"
44
49
#include "drizzled/field/varstring.h"
50
#include "drizzled/field/uuid.h"
45
51
#include "drizzled/time_functions.h"
46
52
#include "drizzled/internal/m_string.h"
54
#include "drizzled/display.h"
374
610
void *Field::operator new(size_t size, memory::Root *mem_root)
376
return alloc_root(mem_root, static_cast<uint32_t>(size));
612
return mem_root->alloc_root(static_cast<uint32_t>(size));
379
615
enum_field_types Field::field_type_merge(enum_field_types a,
380
616
enum_field_types b)
382
assert(a <= DRIZZLE_TYPE_MAX);
383
assert(b <= DRIZZLE_TYPE_MAX);
618
assert(a < enum_field_types_size);
619
assert(b < enum_field_types_size);
384
620
return field_types_merge_rules[a][b];
387
623
Item_result Field::result_merge_type(enum_field_types field_type)
389
assert(field_type <= DRIZZLE_TYPE_MAX);
625
assert(field_type < enum_field_types_size);
390
626
return field_types_result_type[field_type];
592
827
unsigned char *null_ptr_arg,
593
828
unsigned char null_bit_arg,
594
829
utype unireg_check_arg,
595
const char *field_name_arg)
830
const char *field_name_arg) :
598
832
null_ptr(null_ptr_arg),
600
834
orig_table(NULL),
602
835
field_name(field_name_arg),
836
comment(NULL_LEX_STRING),
605
839
part_of_key_not_clustered(0),
606
840
part_of_sortkey(0),
607
841
unireg_check(unireg_check_arg),
608
842
field_length(length_arg),
843
flags(null_ptr ? 0: NOT_NULL_FLAG),
609
845
null_bit(null_bit_arg),
610
846
is_created_from_null_item(false)
612
flags= null_ptr ? 0: NOT_NULL_FLAG;
613
comment.str= (char*) "";
618
850
void Field::hash(uint32_t *nr, uint32_t *nr2)
703
936
const unsigned char *Field::unpack(unsigned char* to, const unsigned char *from)
705
const unsigned char *result= unpack(to, from, 0U, table->s->db_low_byte_first);
938
const unsigned char *result= unpack(to, from, 0U, table->getShare()->db_low_byte_first);
709
my_decimal *Field::val_decimal(my_decimal *)
942
type::Decimal *Field::val_decimal(type::Decimal *)
711
944
/* This never have to be called */
717
950
void Field::make_field(SendField *field)
719
if (orig_table && orig_table->s->getSchemaName() && *orig_table->s->getSchemaName())
952
if (orig_table && orig_table->getShare()->getSchemaName() && *orig_table->getShare()->getSchemaName())
721
field->db_name= orig_table->s->getSchemaName();
722
field->org_table_name= orig_table->s->table_name.str;
954
field->db_name= orig_table->getShare()->getSchemaName();
955
field->org_table_name= orig_table->getShare()->getTableName();
725
958
field->org_table_name= field->db_name= "";
728
field->table_name= orig_table->alias;
961
field->table_name= orig_table->getAlias();
729
962
field->org_col_name= field_name;
741
974
field->decimals= 0;
744
int64_t Field::convert_decimal2int64_t(const my_decimal *val, bool, int *err)
977
int64_t Field::convert_decimal2int64_t(const type::Decimal *val, bool, int *err)
747
if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
980
if (warn_if_overflow(val->val_int32(E_DEC_ERROR &
748
981
~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
751
984
i= (val->sign() ? INT64_MIN : INT64_MAX);
775
1008
return copy->length+ store_length;
778
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
781
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
782
if (!(res=val_str(&tmp)) ||
783
str_to_datetime_with_warn(res->ptr(), res->length(),
784
ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
789
bool Field::get_time(DRIZZLE_TIME *ltime)
792
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
793
if (!(res=val_str(&tmp)) ||
794
str_to_time_with_warn(res->ptr(), res->length(), ltime))
799
int Field::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
801
char buff[MAX_DATE_STRING_REP_LENGTH];
802
uint32_t length= (uint32_t) my_TIME_to_str(ltime, buff);
803
return store(buff, length, &my_charset_bin);
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);
806
1052
bool Field::optimize_range(uint32_t idx, uint32_t)
844
1090
Field *Field::clone(memory::Root *root, Table *new_table)
847
if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1093
if ((tmp= (Field*) root->memdup_root((char*) this,size_of())))
849
1095
tmp->init(new_table);
850
tmp->move_field_offset((ptrdiff_t) (new_table->record[0] -
851
new_table->s->default_values));
1096
tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() -
1097
new_table->getDefaultValues()));
872
1118
if (!Field::eq_def(field))
874
1121
TYPELIB *from_lib=((Field_enum*) field)->typelib;
876
1123
if (typelib->count < from_lib->count)
878
1126
for (uint32_t i=0 ; i < from_lib->count ; i++)
879
1128
if (my_strnncoll(field_charset,
880
1129
(const unsigned char*)typelib->type_names[i],
881
1130
strlen(typelib->type_names[i]),
882
1131
(const unsigned char*)from_lib->type_names[i],
883
1132
strlen(from_lib->type_names[i])))
891
1142
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
892
case DRIZZLE_TYPE_DATE: return 3;
893
case DRIZZLE_TYPE_TIMESTAMP:
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:
894
1149
case DRIZZLE_TYPE_LONG: return 4;
895
1150
case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1151
case DRIZZLE_TYPE_TIME:
896
1152
case DRIZZLE_TYPE_DATETIME:
897
1153
case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
898
1154
case DRIZZLE_TYPE_NULL: return 0;
899
1155
case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
900
case DRIZZLE_TYPE_ENUM:
901
1156
case DRIZZLE_TYPE_DECIMAL:
908
1164
uint32_t pack_length_to_packflag(uint32_t type)
911
1167
case 1: return 1 << FIELDFLAG_PACK_SHIFT;
912
1168
case 2: assert(1);
913
1169
case 3: assert(1);
914
case 4: return f_settype((uint32_t) DRIZZLE_TYPE_LONG);
915
case 8: return f_settype((uint32_t) DRIZZLE_TYPE_LONGLONG);
1170
case 4: return f_settype(DRIZZLE_TYPE_LONG);
1171
case 8: return f_settype(DRIZZLE_TYPE_LONGLONG);
917
1173
return 0; // This shouldn't happen
920
Field *make_field(TableShare *share,
923
uint32_t field_length,
925
unsigned char *null_pos,
926
unsigned char null_bit,
928
enum_field_types field_type,
929
const CHARSET_INFO * field_charset,
930
Field::utype unireg_check,
932
const char *field_name)
935
root= current_mem_root();
944
null_bit= ((unsigned char) 1) << null_bit;
949
case DRIZZLE_TYPE_DATE:
950
case DRIZZLE_TYPE_DATETIME:
951
case DRIZZLE_TYPE_TIMESTAMP:
952
field_charset= &my_charset_bin;
956
if (field_type == DRIZZLE_TYPE_VARCHAR ||
957
field_type == DRIZZLE_TYPE_BLOB ||
958
field_type == DRIZZLE_TYPE_ENUM)
960
if (field_type == DRIZZLE_TYPE_VARCHAR)
961
return new (root) Field_varstring(ptr,field_length,
962
HA_VARCHAR_PACKLENGTH(field_length),
968
if (field_type == DRIZZLE_TYPE_BLOB)
970
return new (root) Field_blob(ptr,
975
calc_pack_length(DRIZZLE_TYPE_LONG, 0),
981
return new (root) Field_enum(ptr,
986
get_enum_pack_length(interval->count),
994
case DRIZZLE_TYPE_DECIMAL:
995
return new (root) Field_decimal(ptr,
1003
false /* is_unsigned */);
1004
case DRIZZLE_TYPE_DOUBLE:
1005
return new (root) Field_double(ptr,
1013
false /* is_unsigned */);
1014
case DRIZZLE_TYPE_LONG:
1015
return new (root) Field_long(ptr,
1022
false /* is_unsigned */);
1023
case DRIZZLE_TYPE_LONGLONG:
1024
return new (root) Field_int64_t(ptr,
1031
false /* is_unsigned */);
1032
case DRIZZLE_TYPE_TIMESTAMP:
1033
return new (root) Field_timestamp(ptr,
1041
case DRIZZLE_TYPE_DATE:
1042
return new (root) Field_date(ptr,
1047
case DRIZZLE_TYPE_DATETIME:
1048
return new (root) Field_datetime(ptr,
1053
case DRIZZLE_TYPE_NULL:
1054
return new (root) Field_null(ptr,
1058
default: // Impossible (Wrong version)
1064
1176
/*****************************************************************************
1065
1177
Warning handling
1066
1178
*****************************************************************************/
1068
1180
bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
1181
drizzled::error_t code,
1070
1182
int cuted_increment)
1088
1200
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1201
drizzled::error_t code,
1090
1202
const char *str,
1091
1203
uint32_t str_length,
1092
enum enum_drizzle_timestamp_type ts_type,
1204
type::timestamp_t ts_type,
1093
1205
int cuted_increment)
1095
Session *session= table ? table->in_use : current_session;
1096
if ((session->really_abort_on_warning() &&
1207
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1209
if ((session->abortOnWarning() and
1097
1210
level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
1098
1211
set_warning(level, code, cuted_increment))
1099
1212
make_truncated_value_warning(session, level, str, str_length, ts_type,
1103
1216
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1217
drizzled::error_t code,
1106
enum enum_drizzle_timestamp_type ts_type,
1219
type::timestamp_t ts_type,
1107
1220
int cuted_increment)
1109
Session *session= table ? table->in_use : current_session;
1110
if (session->really_abort_on_warning() ||
1222
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1224
if (session->abortOnWarning() or
1111
1225
set_warning(level, code, cuted_increment))
1227
char str_nr[DECIMAL_LONGLONG_DIGITS];
1114
1228
char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
1115
1229
make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
1116
1230
ts_type, field_name);
1120
1234
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1121
const uint32_t code,
1235
const drizzled::error_t code,
1123
enum enum_drizzle_timestamp_type ts_type)
1237
type::timestamp_t ts_type)
1125
Session *session= table ? table->in_use : current_session;
1126
if (session->really_abort_on_warning() ||
1239
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1241
if (session->abortOnWarning() or
1127
1242
set_warning(level, code, 1))
1129
1244
/* DBL_DIG is enough to print '-[digits].E+###' */
1160
1275
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.
1163
1348
} /* namespace drizzled */