drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
enum.cc
1/* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 MySQL
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <config.h>
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/typelib.h>
28
29#include <sstream>
30#include <string>
31
32namespace drizzled {
33
34/****************************************************************************
35** enum type.
36** This is a string which only can have a selection of different values.
37** If one uses this string in a number context one gets the type number.
38****************************************************************************/
39
40void Field_enum::store_type(uint64_t value)
41{
42 value--; /* we store as starting from 0, although SQL starts from 1 */
43
44#ifdef WORDS_BIGENDIAN
45 if (getTable()->getShare()->db_low_byte_first)
46 {
47 int4store(ptr, (unsigned short) value);
48 }
49 else
50#endif
51 longstore(ptr, (unsigned short) value);
52}
53
59int Field_enum::store(const char *from, uint32_t length, const charset_info_st * const)
60{
61 uint32_t tmp;
62
63 ASSERT_COLUMN_MARKED_FOR_WRITE;
64
65 /* Remove end space */
66 length= field_charset->cset->lengthsp(field_charset, from, length);
67 tmp= typelib->find_type2(from, length, field_charset);
68 if (! tmp)
69 {
70 if (length < 6) /* Can't be more than 99999 enums */
71 {
72 /* This is for reading numbers with LOAD DATA INFILE */
73 /* Convert the string to an integer using stringstream */
74 std::stringstream ss;
75 ss << from;
76 ss >> tmp;
77
78 if (tmp == 0 || tmp > typelib->count)
79 {
80 my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
81 return 1;
82 }
83 }
84 else
85 {
86 my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
87 return 1;
88 }
89 }
90 store_type((uint64_t) tmp);
91 return 0;
92}
93
94int Field_enum::store(double from)
95{
96 return Field_enum::store((int64_t) from, false);
97}
98
106int Field_enum::store(int64_t from, bool)
107{
108 ASSERT_COLUMN_MARKED_FOR_WRITE;
109
110 if (from <= 0 || (uint64_t) from > typelib->count)
111 {
112 /* Convert the integer to a string using boost::lexical_cast */
113 std::string tmp(boost::lexical_cast<std::string>(from));
114
115 my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), tmp.c_str());
116 return 1;
117 }
118 store_type((uint64_t) (uint32_t) from);
119 return 0;
120}
121
122double Field_enum::val_real(void) const
123{
124 return (double) Field_enum::val_int();
125}
126
127int64_t Field_enum::val_int(void) const
128{
129 ASSERT_COLUMN_MARKED_FOR_READ;
130
131 uint16_t tmp;
132#ifdef WORDS_BIGENDIAN
133 if (getTable()->getShare()->db_low_byte_first)
134 tmp= sint4korr(ptr);
135 else
136#endif
137 longget(tmp,ptr);
138 return ((int64_t) tmp) + 1; /* SQL is from 1, we store from 0 */
139}
140
141String *Field_enum::val_str(String *, String *val_ptr) const
142{
143 uint32_t tmp=(uint32_t) Field_enum::val_int();
144
145 ASSERT_COLUMN_MARKED_FOR_READ;
146
147 if (not tmp || tmp > typelib->count)
148 {
149 val_ptr->set("", 0, field_charset);
150 }
151 else
152 {
153 val_ptr->set((const char*) typelib->type_names[tmp-1], typelib->type_lengths[tmp-1], field_charset);
154 }
155
156 return val_ptr;
157}
158
159int Field_enum::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
160{
161 unsigned char *old= ptr;
162 ptr= (unsigned char*) a_ptr;
163 uint64_t a= Field_enum::val_int();
164 ptr= (unsigned char*) b_ptr;
165 uint64_t b= Field_enum::val_int();
166 ptr= old;
167 return (a < b) ? -1 : (a > b) ? 1 : 0;
168}
169
170void Field_enum::sort_string(unsigned char *to,uint32_t )
171{
172 uint64_t value=Field_enum::val_int()-1; /* SQL is 1 based, stored as 0 based*/
173 to+=pack_length() -1;
174 for (uint32_t i=0 ; i < pack_length() ; i++)
175 {
176 *to-- = (unsigned char) (value & 255);
177 value>>=8;
178 }
179}
180
181Field *Field_enum::new_field(memory::Root *root, Table *new_table,
182 bool keep_type)
183{
184 Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
185 if (res)
186 {
187 res->typelib= typelib->copy_typelib(*root);
188 }
189 return res;
190}
191
192} /* namespace drizzled */
uint32_t pack_length() const
Definition field/enum.h:82
int store(const char *to, uint32_t length, const charset_info_st *const)
Definition enum.cc:59
unsigned char * ptr
Definition field.h:73
TODO: Rename this file - func.h is stupid.