38
31
** If one uses this string in a number context one gets the type number.
39
32
****************************************************************************/
34
enum ha_base_keytype Field_enum::key_type() const
37
default: return HA_KEYTYPE_BINARY;
40
case 4: return HA_KEYTYPE_ULONG_INT;
41
case 8: return HA_KEYTYPE_ULONGLONG;
41
45
void Field_enum::store_type(uint64_t value)
43
value--; /* we store as starting from 0, although SQL starts from 1 */
45
#ifdef WORDS_BIGENDIAN
46
if (getTable()->getShare()->db_low_byte_first)
48
int4store(ptr, (unsigned short) value);
52
longstore(ptr, (unsigned short) value);
48
case 1: ptr[0]= (unsigned char) value; break;
50
#ifdef WORDS_BIGENDIAN
51
if (table->s->db_low_byte_first)
53
int2store(ptr,(unsigned short) value);
57
shortstore(ptr,(unsigned short) value);
59
case 3: int3store(ptr,(long) value); break;
61
#ifdef WORDS_BIGENDIAN
62
if (table->s->db_low_byte_first)
68
longstore(ptr,(long) value);
71
#ifdef WORDS_BIGENDIAN
72
if (table->s->db_low_byte_first)
78
int64_tstore(ptr,value); break;
56
* Given a supplied string, looks up the string in the internal typelib
57
* and stores the found key. Upon not finding an entry in the typelib,
58
* we always throw an error.
60
int Field_enum::store(const char *from, uint32_t length, const CHARSET_INFO * const)
85
Storing a empty string in a enum field gives a warning
86
(if there isn't a empty value in the enum)
89
int Field_enum::store(const char *from, uint32_t length, const CHARSET_INFO * const cs)
93
char buff[STRING_BUFFER_USUAL_SIZE];
94
String tmpstr(buff,sizeof(buff), &my_charset_bin);
64
ASSERT_COLUMN_MARKED_FOR_WRITE;
96
/* Convert character set if necessary */
97
if (String::needs_conversion(length, cs, field_charset, ¬_used))
99
uint32_t dummy_errors;
100
tmpstr.copy(from, length, cs, field_charset, &dummy_errors);
102
length= tmpstr.length();
66
105
/* Remove end space */
67
106
length= field_charset->cset->lengthsp(field_charset, from, length);
68
tmp= find_type2(typelib, from, length, field_charset);
107
uint32_t tmp=find_type2(typelib, from, length, field_charset);
71
if (length < 6) /* Can't be more than 99999 enums */
110
if (length < 6) // Can't be more than 99999 enums
73
112
/* This is for reading numbers with LOAD DATA INFILE */
74
/* Convert the string to an integer using stringstream */
79
if (tmp == 0 || tmp > typelib->count)
114
tmp=(uint32_t) my_strntoul(cs,from,length,10,&end,&err);
115
if (err || end != from+length || tmp > typelib->count)
81
my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
118
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
120
if (!table->in_use->count_cuted_fields)
87
my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
124
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
91
126
store_type((uint64_t) tmp);
95
int Field_enum::store(double from)
97
return Field_enum::store((int64_t) from, false);
101
* @note MySQL allows 0 values, saying that 0 is "the index of the
102
* blank string error", whatever that means. Uhm, Drizzle doesn't
103
* allow this. To store an ENUM column value using an integer, you
104
* must specify the 1-based index of the enum column definition's
107
int Field_enum::store(int64_t from, bool)
109
ASSERT_COLUMN_MARKED_FOR_WRITE;
111
if (from <= 0 || (uint64_t) from > typelib->count)
131
int Field_enum::store(double nr)
133
return Field_enum::store((int64_t) nr, false);
137
int Field_enum::store(int64_t nr,
141
if ((uint64_t) nr > typelib->count || nr == 0)
113
/* Convert the integer to a string using boost::lexical_cast */
114
std::string tmp(boost::lexical_cast<std::string>(from));
116
my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), tmp.c_str());
143
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
144
if (nr != 0 || table->in_use->count_cuted_fields)
119
store_type((uint64_t) (uint32_t) from);
150
store_type((uint64_t) (uint32_t) nr);
123
155
double Field_enum::val_real(void)
125
157
return (double) Field_enum::val_int();
128
161
int64_t Field_enum::val_int(void)
130
ASSERT_COLUMN_MARKED_FOR_READ;
133
#ifdef WORDS_BIGENDIAN
134
if (getTable()->getShare()->db_low_byte_first)
139
return ((int64_t) tmp) + 1; /* SQL is from 1, we store from 0 */
142
String *Field_enum::val_str(String *, String *val_ptr)
163
switch (packlength) {
165
return (int64_t) ptr[0];
169
#ifdef WORDS_BIGENDIAN
170
if (table->s->db_low_byte_first)
175
return (int64_t) tmp;
178
return (int64_t) uint3korr(ptr);
182
#ifdef WORDS_BIGENDIAN
183
if (table->s->db_low_byte_first)
188
return (int64_t) tmp;
193
#ifdef WORDS_BIGENDIAN
194
if (table->s->db_low_byte_first)
202
return 0; // impossible
207
Save the field metadata for enum fields.
209
Saves the real type in the first byte and the pack length in the
210
second byte of the field metadata array at index of *metadata_ptr and
213
@param metadata_ptr First byte of field metadata
215
@returns number of bytes written to metadata_ptr
217
int Field_enum::do_save_field_metadata(unsigned char *metadata_ptr)
219
*metadata_ptr= real_type();
220
*(metadata_ptr + 1)= pack_length();
225
String *Field_enum::val_str(String *,
144
228
uint32_t tmp=(uint32_t) Field_enum::val_int();
146
ASSERT_COLUMN_MARKED_FOR_READ;
148
if (not tmp || tmp > typelib->count)
229
if (!tmp || tmp > typelib->count)
150
230
val_ptr->set("", 0, field_charset);
154
val_ptr->set((const char*) typelib->type_names[tmp-1], typelib->type_lengths[tmp-1], field_charset);
232
val_ptr->set((const char*) typelib->type_names[tmp-1],
233
typelib->type_lengths[tmp-1],