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>
28
#include "drizzled/sql_select.h"
29
#include "drizzled/error.h"
30
#include "drizzled/field/str.h"
31
#include "drizzled/field/num.h"
32
#include "drizzled/field/blob.h"
33
#include "drizzled/field/enum.h"
34
#include "drizzled/field/null.h"
35
#include "drizzled/field/date.h"
36
#include "drizzled/field/decimal.h"
37
#include "drizzled/field/real.h"
38
#include "drizzled/field/double.h"
39
#include "drizzled/field/long.h"
40
#include "drizzled/field/int64_t.h"
41
#include "drizzled/field/num.h"
42
#include "drizzled/field/timestamp.h"
43
#include "drizzled/field/datetime.h"
44
#include "drizzled/field/varstring.h"
45
#include "drizzled/time_functions.h"
46
#include "drizzled/internal/m_string.h"
614
374
void *Field::operator new(size_t size, memory::Root *mem_root)
616
return mem_root->alloc_root(static_cast<uint32_t>(size));
376
return alloc_root(mem_root, static_cast<uint32_t>(size));
619
379
enum_field_types Field::field_type_merge(enum_field_types a,
620
380
enum_field_types b)
622
assert(a < enum_field_types_size);
623
assert(b < enum_field_types_size);
382
assert(a <= DRIZZLE_TYPE_MAX);
383
assert(b <= DRIZZLE_TYPE_MAX);
624
384
return field_types_merge_rules[a][b];
627
387
Item_result Field::result_merge_type(enum_field_types field_type)
629
assert(field_type < enum_field_types_size);
389
assert(field_type <= DRIZZLE_TYPE_MAX);
630
390
return field_types_result_type[field_type];
749
bool Field::is_null(ptrdiff_t row_offset) const
509
bool Field::is_null(ptrdiff_t row_offset)
751
511
return null_ptr ?
752
512
(null_ptr[row_offset] & null_bit ? true : false) :
756
bool Field::is_real_null(ptrdiff_t row_offset) const
516
bool Field::is_real_null(ptrdiff_t row_offset)
758
518
return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : false;
761
bool Field::is_null_in_record(const unsigned char *record) const
521
bool Field::is_null_in_record(const unsigned char *record)
765
return test(record[(uint32_t) (null_ptr -table->getInsertRecord())] & null_bit);
525
return test(record[(uint32_t) (null_ptr -table->record[0])] & null_bit);
768
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset) const
528
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset)
831
592
unsigned char *null_ptr_arg,
832
593
unsigned char null_bit_arg,
833
594
utype unireg_check_arg,
834
const char *field_name_arg) :
595
const char *field_name_arg)
836
598
null_ptr(null_ptr_arg),
838
600
orig_table(NULL),
839
602
field_name(field_name_arg),
840
comment(NULL_LEX_STRING),
843
605
part_of_key_not_clustered(0),
844
606
part_of_sortkey(0),
845
607
unireg_check(unireg_check_arg),
846
608
field_length(length_arg),
847
flags(null_ptr ? 0: NOT_NULL_FLAG),
849
609
null_bit(null_bit_arg),
850
610
is_created_from_null_item(false)
612
flags= null_ptr ? 0: NOT_NULL_FLAG;
613
comment.str= (char*) "";
854
void Field::hash(uint32_t *nr, uint32_t *nr2) const
618
void Field::hash(uint32_t *nr, uint32_t *nr2)
940
703
const unsigned char *Field::unpack(unsigned char* to, const unsigned char *from)
942
const unsigned char *result= unpack(to, from, 0U, table->getShare()->db_low_byte_first);
705
const unsigned char *result= unpack(to, from, 0U, table->s->db_low_byte_first);
946
type::Decimal *Field::val_decimal(type::Decimal *) const
709
my_decimal *Field::val_decimal(my_decimal *)
948
711
/* This never have to be called */
954
717
void Field::make_field(SendField *field)
956
if (orig_table && orig_table->getShare()->getSchemaName() && *orig_table->getShare()->getSchemaName())
719
if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
958
field->db_name= orig_table->getShare()->getSchemaName();
959
field->org_table_name= orig_table->getShare()->getTableName();
721
field->db_name= orig_table->s->db.str;
722
field->org_table_name= orig_table->s->table_name.str;
962
725
field->org_table_name= field->db_name= "";
965
field->table_name= orig_table->getAlias();
728
field->table_name= orig_table->alias;
966
729
field->org_col_name= field_name;
978
741
field->decimals= 0;
981
int64_t Field::convert_decimal2int64_t(const type::Decimal *val, bool, int *err)
744
int64_t Field::convert_decimal2int64_t(const my_decimal *val, bool, int *err)
984
if (warn_if_overflow(val->val_int32(E_DEC_ERROR &
747
if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
985
748
~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
988
751
i= (val->sign() ? INT64_MIN : INT64_MAX);
1012
775
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);
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);
1056
806
bool Field::optimize_range(uint32_t idx, uint32_t)
1094
844
Field *Field::clone(memory::Root *root, Table *new_table)
1097
if ((tmp= (Field*) root->memdup_root((char*) this,size_of())))
847
if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1099
849
tmp->init(new_table);
1100
tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() -
1101
new_table->getDefaultValues()));
850
tmp->move_field_offset((ptrdiff_t) (new_table->record[0] -
851
new_table->s->default_values));
1122
872
if (!Field::eq_def(field))
1125
874
TYPELIB *from_lib=((Field_enum*) field)->typelib;
1127
876
if (typelib->count < from_lib->count)
1130
878
for (uint32_t i=0 ; i < from_lib->count ; i++)
1132
879
if (my_strnncoll(field_charset,
1133
880
(const unsigned char*)typelib->type_names[i],
1134
881
strlen(typelib->type_names[i]),
1135
882
(const unsigned char*)from_lib->type_names[i],
1136
883
strlen(from_lib->type_names[i])))
1146
891
case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1147
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
case DRIZZLE_TYPE_DATE:
1152
case DRIZZLE_TYPE_ENUM:
892
case DRIZZLE_TYPE_DATE: return 3;
893
case DRIZZLE_TYPE_TIMESTAMP:
1153
894
case DRIZZLE_TYPE_LONG: return 4;
1154
895
case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1155
case DRIZZLE_TYPE_TIME:
1156
896
case DRIZZLE_TYPE_DATETIME:
1157
897
case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1158
898
case DRIZZLE_TYPE_NULL: return 0;
1159
899
case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
900
case DRIZZLE_TYPE_ENUM:
1160
901
case DRIZZLE_TYPE_DECIMAL:
1168
908
uint32_t pack_length_to_packflag(uint32_t type)
1171
911
case 1: return 1 << FIELDFLAG_PACK_SHIFT;
1172
912
case 2: assert(1);
1173
913
case 3: assert(1);
1174
case 4: return f_settype(DRIZZLE_TYPE_LONG);
1175
case 8: return f_settype(DRIZZLE_TYPE_LONGLONG);
914
case 4: return f_settype((uint32_t) DRIZZLE_TYPE_LONG);
915
case 8: return f_settype((uint32_t) DRIZZLE_TYPE_LONGLONG);
1177
917
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)
1180
1064
/*****************************************************************************
1181
1065
Warning handling
1182
1066
*****************************************************************************/
1184
1068
bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
1185
drizzled::error_t code,
1186
1070
int cuted_increment)
1204
1088
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1205
drizzled::error_t code,
1206
1090
const char *str,
1207
1091
uint32_t str_length,
1208
type::timestamp_t ts_type,
1092
enum enum_drizzle_timestamp_type ts_type,
1209
1093
int cuted_increment)
1211
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1213
if ((session->abortOnWarning() and
1095
Session *session= table ? table->in_use : current_session;
1096
if ((session->really_abort_on_warning() &&
1214
1097
level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
1215
1098
set_warning(level, code, cuted_increment))
1216
1099
make_truncated_value_warning(session, level, str, str_length, ts_type,
1220
1103
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1221
drizzled::error_t code,
1223
type::timestamp_t ts_type,
1106
enum enum_drizzle_timestamp_type ts_type,
1224
1107
int cuted_increment)
1226
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1228
if (session->abortOnWarning() or
1109
Session *session= table ? table->in_use : current_session;
1110
if (session->really_abort_on_warning() ||
1229
1111
set_warning(level, code, cuted_increment))
1231
char str_nr[DECIMAL_LONGLONG_DIGITS];
1232
1114
char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
1233
1115
make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
1234
1116
ts_type, field_name);
1238
1120
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1239
const drizzled::error_t code,
1121
const uint32_t code,
1241
type::timestamp_t ts_type)
1123
enum enum_drizzle_timestamp_type ts_type)
1243
Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1245
if (session->abortOnWarning() or
1125
Session *session= table ? table->in_use : current_session;
1126
if (session->really_abort_on_warning() ||
1246
1127
set_warning(level, code, 1))
1248
1129
/* DBL_DIG is enough to print '-[digits].E+###' */
1249
1130
char str_nr[DBL_DIG + 8];
1250
uint32_t str_len= snprintf(str_nr, sizeof(str_nr), "%g", nr);
1131
uint32_t str_len= sprintf(str_nr, "%g", nr);
1251
1132
make_truncated_value_warning(session, level, str_nr, str_len, ts_type,
1256
bool Field::isReadSet() const
1137
bool Field::isReadSet()
1258
1139
return table->isReadSet(field_index);
1279
1160
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
std::ostream& operator<<(std::ostream& output, const Field &field)
1320
output << "Field:(";
1321
output << field.field_name;
1323
output << drizzled::display::type(field.real_type());
1326
if (field.flags & NOT_NULL_FLAG)
1327
output << " NOT_NULL";
1329
if (field.flags & PRI_KEY_FLAG)
1330
output << ", PRIMARY KEY";
1332
if (field.flags & UNIQUE_KEY_FLAG)
1333
output << ", UNIQUE KEY";
1335
if (field.flags & MULTIPLE_KEY_FLAG)
1336
output << ", MULTIPLE KEY";
1338
if (field.flags & BLOB_FLAG)
1341
if (field.flags & UNSIGNED_FLAG)
1342
output << ", UNSIGNED";
1344
if (field.flags & BINARY_FLAG)
1345
output << ", BINARY";
1349
return output; // for multiple << operators.
1352
1163
} /* namespace drizzled */