39
31
** If one uses this string in a number context one gets the type number.
40
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;
42
45
void Field_enum::store_type(uint64_t value)
44
value--; /* we store as starting from 0, although SQL starts from 1 */
46
#ifdef WORDS_BIGENDIAN
47
if (getTable()->getShare()->db_low_byte_first)
49
int4store(ptr, (unsigned short) value);
53
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;
57
* Given a supplied string, looks up the string in the internal typelib
58
* and stores the found key. Upon not finding an entry in the typelib,
59
* we always throw an error.
61
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);
65
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();
67
105
/* Remove end space */
68
106
length= field_charset->cset->lengthsp(field_charset, from, length);
69
tmp= typelib->find_type2(from, length, field_charset);
107
uint32_t tmp=find_type2(typelib, from, length, field_charset);
72
if (length < 6) /* Can't be more than 99999 enums */
110
if (length < 6) // Can't be more than 99999 enums
74
112
/* This is for reading numbers with LOAD DATA INFILE */
75
/* Convert the string to an integer using stringstream */
80
if (tmp == 0 || tmp > typelib->count)
114
tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
115
if (err || end != from+length || tmp > typelib->count)
82
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)
88
my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
124
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
92
126
store_type((uint64_t) tmp);
96
int Field_enum::store(double from)
98
return Field_enum::store((int64_t) from, false);
102
* @note MySQL allows 0 values, saying that 0 is "the index of the
103
* blank string error", whatever that means. Uhm, Drizzle doesn't
104
* allow this. To store an ENUM column value using an integer, you
105
* must specify the 1-based index of the enum column definition's
108
int Field_enum::store(int64_t from, bool)
110
ASSERT_COLUMN_MARKED_FOR_WRITE;
112
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)
114
/* Convert the integer to a string using boost::lexical_cast */
115
std::string tmp(boost::lexical_cast<std::string>(from));
117
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)
120
store_type((uint64_t) (uint32_t) from);
150
store_type((uint64_t) (uint) nr);
124
double Field_enum::val_real(void) const
155
double Field_enum::val_real(void)
126
157
return (double) Field_enum::val_int();
129
int64_t Field_enum::val_int(void) const
131
ASSERT_COLUMN_MARKED_FOR_READ;
134
#ifdef WORDS_BIGENDIAN
135
if (getTable()->getShare()->db_low_byte_first)
140
return ((int64_t) tmp) + 1; /* SQL is from 1, we store from 0 */
143
String *Field_enum::val_str(String *, String *val_ptr) const
145
uint32_t tmp=(uint32_t) Field_enum::val_int();
147
ASSERT_COLUMN_MARKED_FOR_READ;
149
if (not tmp || tmp > typelib->count)
161
int64_t Field_enum::val_int(void)
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 *,
228
uint32_t tmp=(uint) Field_enum::val_int();
229
if (!tmp || tmp > typelib->count)
151
230
val_ptr->set("", 0, field_charset);
155
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],