1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
/* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2008 MySQL
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "drizzled/field/enum.h"
#include "drizzled/error.h"
#include "drizzled/table.h"
#include "drizzled/session.h"
#include "drizzled/strfunc.h"
#include <sstream>
#include <string>
namespace drizzled
{
/****************************************************************************
** enum type.
** This is a string which only can have a selection of different values.
** If one uses this string in a number context one gets the type number.
****************************************************************************/
enum ha_base_keytype Field_enum::key_type() const
{
switch (packlength)
{
case 1:
return HA_KEYTYPE_BINARY;
case 2:
return HA_KEYTYPE_ULONG_INT;
default:
assert(packlength <= 2);
return HA_KEYTYPE_ULONG_INT;
}
}
void Field_enum::store_type(uint64_t value)
{
value--; /* we store as starting from 0, although SQL starts from 1 */
switch (packlength) {
case 1: ptr[0]= (unsigned char) value; break;
case 2:
#ifdef WORDS_BIGENDIAN
if (getTable()->s->db_low_byte_first)
{
int2store(ptr,(unsigned short) value);
}
else
#endif
shortstore(ptr,(unsigned short) value);
break;
default:
assert(packlength <= 2);
}
}
/**
* Given a supplied string, looks up the string in the internal typelib
* and stores the found key. Upon not finding an entry in the typelib,
* we always throw an error.
*/
int Field_enum::store(const char *from, uint32_t length, const CHARSET_INFO * const)
{
uint32_t tmp;
ASSERT_COLUMN_MARKED_FOR_WRITE;
/* Remove end space */
length= field_charset->cset->lengthsp(field_charset, from, length);
tmp= find_type2(typelib, from, length, field_charset);
if (! tmp)
{
if (length < 6) /* Can't be more than 99999 enums */
{
/* This is for reading numbers with LOAD DATA INFILE */
/* Convert the string to an integer using stringstream */
std::stringstream ss;
ss << from;
ss >> tmp;
if (tmp == 0 || tmp > typelib->count)
{
my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
return 1;
}
}
else
{
my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
return 1;
}
}
store_type((uint64_t) tmp);
return 0;
}
int Field_enum::store(double from)
{
return Field_enum::store((int64_t) from, false);
}
/**
* @note MySQL allows 0 values, saying that 0 is "the index of the
* blank string error", whatever that means. Uhm, Drizzle doesn't
* allow this. To store an ENUM column value using an integer, you
* must specify the 1-based index of the enum column definition's
* key.
*/
int Field_enum::store(int64_t from, bool)
{
ASSERT_COLUMN_MARKED_FOR_WRITE;
if (from <= 0 || (uint64_t) from > typelib->count)
{
/* Convert the integer to a string using stringstream */
std::stringstream ss;
std::string tmp;
ss << from; ss >> tmp;
my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), tmp.c_str());
return 1;
}
store_type((uint64_t) (uint32_t) from);
return 0;
}
double Field_enum::val_real(void)
{
return (double) Field_enum::val_int();
}
int64_t Field_enum::val_int(void)
{
ASSERT_COLUMN_MARKED_FOR_READ;
switch (packlength) {
case 1:
return ((int64_t) ptr[0]) + 1; /* SQL is from 1, we store from 0 */
case 2:
{
uint16_t tmp;
#ifdef WORDS_BIGENDIAN
if (getTable()->s->db_low_byte_first)
tmp=sint2korr(ptr);
else
#endif
shortget(tmp,ptr);
return ((int64_t) tmp) + 1; /* SQL is from 1, we store from 0 */
}
default:
assert(packlength <= 2);
}
return 0; // impossible
}
String *Field_enum::val_str(String *, String *val_ptr)
{
uint32_t tmp=(uint32_t) Field_enum::val_int();
ASSERT_COLUMN_MARKED_FOR_READ;
if (!tmp || tmp > typelib->count)
{
val_ptr->set("", 0, field_charset);
}
else
{
val_ptr->set((const char*) typelib->type_names[tmp-1], typelib->type_lengths[tmp-1], field_charset);
}
return val_ptr;
}
int Field_enum::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
{
unsigned char *old= ptr;
ptr= (unsigned char*) a_ptr;
uint64_t a=Field_enum::val_int();
ptr= (unsigned char*) b_ptr;
uint64_t b=Field_enum::val_int();
ptr= old;
return (a < b) ? -1 : (a > b) ? 1 : 0;
}
void Field_enum::sort_string(unsigned char *to,uint32_t )
{
uint64_t value=Field_enum::val_int()-1; /* SQL is 1 based, stored as 0 based*/
to+=packlength-1;
for (uint32_t i=0 ; i < packlength ; i++)
{
*to-- = (unsigned char) (value & 255);
value>>=8;
}
}
void Field_enum::sql_type(String &res) const
{
char buffer[255];
String enum_item(buffer, sizeof(buffer), res.charset());
res.length(0);
res.append(STRING_WITH_LEN("enum("));
bool flag=0;
uint32_t *len= typelib->type_lengths;
for (const char **pos= typelib->type_names; *pos; pos++, len++)
{
uint32_t dummy_errors;
if (flag)
res.append(',');
/* convert to res.charset() == utf8, then quote */
enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
append_unescaped(&res, enum_item.c_ptr(), enum_item.length());
flag= 1;
}
res.append(')');
}
Field *Field_enum::new_field(memory::Root *root, Table *new_table,
bool keep_type)
{
Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
if (res)
{
res->typelib= copy_typelib(root, typelib);
}
return res;
}
} /* namespace drizzled */
|