18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#ifdef USE_PRAGMA_IMPLEMENTATION
22
#pragma implementation // gcc: Class implementation
25
#include <drizzled/server_includes.h>
26
#include <drizzled/field/enum.h>
22
#include "drizzled/server_includes.h"
23
#include "drizzled/field/enum.h"
24
#include "drizzled/error.h"
25
#include "drizzled/table.h"
26
#include "drizzled/session.h"
28
31
/****************************************************************************
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)
87
* Given a supplied string, looks up the string in the internal typelib
88
* and stores the found key. Upon not finding an entry in the typelib,
89
* we always throw an error.
91
int Field_enum::store(const char *from, uint32_t length, const CHARSET_INFO * const)
93
char buff[STRING_BUFFER_USUAL_SIZE];
94
String tmpstr(buff,sizeof(buff), &my_charset_bin);
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();
95
ASSERT_COLUMN_MARKED_FOR_WRITE;
105
97
/* Remove end space */
106
98
length= field_charset->cset->lengthsp(field_charset, from, length);
107
uint32_t tmp=find_type2(typelib, from, length, field_charset);
99
tmp= find_type2(typelib, from, length, field_charset);
110
if (length < 6) // Can't be more than 99999 enums
102
if (length < 6) /* Can't be more than 99999 enums */
112
104
/* This is for reading numbers with LOAD DATA INFILE */
114
tmp=(uint) my_strntoul(cs,from,length,10,&end,&err);
115
if (err || end != from+length || tmp > typelib->count)
105
/* Convert the string to an integer using stringstream */
106
std::stringstream ss;
110
if (tmp == 0 || tmp > typelib->count)
118
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
112
my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
120
if (!table->in_use->count_cuted_fields)
124
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
118
my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
126
122
store_type((uint64_t) tmp);
131
int Field_enum::store(double nr)
133
return Field_enum::store((int64_t) nr, false);
137
int Field_enum::store(int64_t nr,
138
bool unsigned_val __attribute__((unused)))
141
if ((uint64_t) nr > typelib->count || nr == 0)
126
int Field_enum::store(double from)
128
return Field_enum::store((int64_t) from, false);
132
* @note MySQL allows 0 values, saying that 0 is "the index of the
133
* blank string error", whatever that means. Uhm, Drizzle doesn't
134
* allow this. To store an ENUM column value using an integer, you
135
* must specify the 1-based index of the enum column definition's
138
int Field_enum::store(int64_t from, bool)
140
ASSERT_COLUMN_MARKED_FOR_WRITE;
142
if (from <= 0 || (uint64_t) from > typelib->count)
143
set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
144
if (nr != 0 || table->in_use->count_cuted_fields)
144
/* Convert the integer to a string using stringstream */
145
std::stringstream ss;
147
ss << from; ss >> tmp;
149
my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), tmp.c_str());
150
store_type((uint64_t) (uint) nr);
152
store_type((uint64_t) (uint32_t) from);
155
156
double Field_enum::val_real(void)
157
158
return (double) Field_enum::val_int();
161
161
int64_t Field_enum::val_int(void)
163
ASSERT_COLUMN_MARKED_FOR_READ;
163
165
switch (packlength) {
165
167
return (int64_t) ptr[0];