1
/* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 MySQL
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <boost/lexical_cast.hpp>
23
#include "drizzled/field/enum.h"
24
#include "drizzled/error.h"
25
#include "drizzled/table.h"
26
#include "drizzled/session.h"
27
#include "drizzled/strfunc.h"
35
/****************************************************************************
37
** This is a string which only can have a selection of different values.
38
** If one uses this string in a number context one gets the type number.
39
****************************************************************************/
41
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);
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)
64
ASSERT_COLUMN_MARKED_FOR_WRITE;
66
/* Remove end space */
67
length= field_charset->cset->lengthsp(field_charset, from, length);
68
tmp= find_type2(typelib, from, length, field_charset);
71
if (length < 6) /* Can't be more than 99999 enums */
73
/* 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)
81
my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
87
my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
91
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)
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());
119
store_type((uint64_t) (uint32_t) from);
123
double Field_enum::val_real(void)
125
return (double) Field_enum::val_int();
128
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)
144
uint32_t tmp=(uint32_t) Field_enum::val_int();
146
ASSERT_COLUMN_MARKED_FOR_READ;
148
if (not tmp || tmp > typelib->count)
150
val_ptr->set("", 0, field_charset);
154
val_ptr->set((const char*) typelib->type_names[tmp-1], typelib->type_lengths[tmp-1], field_charset);
160
int Field_enum::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
162
unsigned char *old= ptr;
163
ptr= (unsigned char*) a_ptr;
164
uint64_t a= Field_enum::val_int();
165
ptr= (unsigned char*) b_ptr;
166
uint64_t b= Field_enum::val_int();
168
return (a < b) ? -1 : (a > b) ? 1 : 0;
171
void Field_enum::sort_string(unsigned char *to,uint32_t )
173
uint64_t value=Field_enum::val_int()-1; /* SQL is 1 based, stored as 0 based*/
174
to+=pack_length() -1;
175
for (uint32_t i=0 ; i < pack_length() ; i++)
177
*to-- = (unsigned char) (value & 255);
182
void Field_enum::sql_type(String &res) const
185
String enum_item(buffer, sizeof(buffer), res.charset());
188
res.append(STRING_WITH_LEN("enum("));
191
uint32_t *len= typelib->type_lengths;
192
for (const char **pos= typelib->type_names; *pos; pos++, len++)
197
/* convert to res.charset() == utf8, then quote */
198
enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
199
append_unescaped(&res, enum_item.c_ptr(), enum_item.length());
205
Field *Field_enum::new_field(memory::Root *root, Table *new_table,
208
Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
211
res->typelib= typelib->copy_typelib(root);
216
} /* namespace drizzled */