~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/enum.cc

Updated pandora-build files to version 0.133

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
 
21
 
21
22
#include "config.h"
22
 
#include <boost/lexical_cast.hpp>
23
23
#include "drizzled/field/enum.h"
24
24
#include "drizzled/error.h"
25
25
#include "drizzled/table.h"
38
38
** If one uses this string in a number context one gets the type number.
39
39
****************************************************************************/
40
40
 
 
41
enum ha_base_keytype Field_enum::key_type() const
 
42
{
 
43
  switch (packlength) 
 
44
  {
 
45
    default: return HA_KEYTYPE_BINARY;
 
46
    case 2: assert(1);
 
47
    case 3: assert(1);
 
48
    case 4: return HA_KEYTYPE_ULONG_INT;
 
49
    case 8: return HA_KEYTYPE_ULONGLONG;
 
50
  }
 
51
}
 
52
 
41
53
void Field_enum::store_type(uint64_t value)
42
54
{
43
 
  value--; /* we store as starting from 0, although SQL starts from 1 */
44
 
 
45
 
#ifdef WORDS_BIGENDIAN
46
 
  if (getTable()->getShare()->db_low_byte_first)
47
 
  {
48
 
    int4store(ptr, (unsigned short) value);
49
 
  }
50
 
  else
51
 
#endif
52
 
    longstore(ptr, (unsigned short) value);
 
55
  switch (packlength) {
 
56
  case 1: ptr[0]= (unsigned char) value;  break;
 
57
  case 2:
 
58
#ifdef WORDS_BIGENDIAN
 
59
  if (table->s->db_low_byte_first)
 
60
  {
 
61
    int2store(ptr,(unsigned short) value);
 
62
  }
 
63
  else
 
64
#endif
 
65
    shortstore(ptr,(unsigned short) value);
 
66
  break;
 
67
  case 3: int3store(ptr,(long) value); break;
 
68
  case 4:
 
69
#ifdef WORDS_BIGENDIAN
 
70
  if (table->s->db_low_byte_first)
 
71
  {
 
72
    int4store(ptr,value);
 
73
  }
 
74
  else
 
75
#endif
 
76
    longstore(ptr,(long) value);
 
77
  break;
 
78
  case 8:
 
79
#ifdef WORDS_BIGENDIAN
 
80
  if (table->s->db_low_byte_first)
 
81
  {
 
82
    int8store(ptr,value);
 
83
  }
 
84
  else
 
85
#endif
 
86
    int64_tstore(ptr,value); break;
 
87
  }
53
88
}
54
89
 
55
90
/**
56
91
 * 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,
 
92
 * and stores the found key.  Upon not finding an entry in the typelib, 
58
93
 * we always throw an error.
59
94
 */
60
95
int Field_enum::store(const char *from, uint32_t length, const CHARSET_INFO * const)
101
136
 * @note MySQL allows 0 values, saying that 0 is "the index of the
102
137
 * blank string error", whatever that means.  Uhm, Drizzle doesn't
103
138
 * 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
 
139
 * must specify the 1-based index of the enum column definition's 
105
140
 * key.
106
141
 */
107
142
int Field_enum::store(int64_t from, bool)
110
145
 
111
146
  if (from <= 0 || (uint64_t) from > typelib->count)
112
147
  {
113
 
    /* Convert the integer to a string using boost::lexical_cast */
114
 
    std::string tmp(boost::lexical_cast<std::string>(from));
 
148
    /* Convert the integer to a string using stringstream */
 
149
    std::stringstream ss;
 
150
    std::string tmp;
 
151
    ss << from; ss >> tmp;
115
152
 
116
153
    my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), tmp.c_str());
117
154
    return 1;
129
166
{
130
167
  ASSERT_COLUMN_MARKED_FOR_READ;
131
168
 
132
 
  uint16_t tmp;
133
 
#ifdef WORDS_BIGENDIAN
134
 
  if (getTable()->getShare()->db_low_byte_first)
135
 
    tmp= sint4korr(ptr);
136
 
  else
137
 
#endif
138
 
    longget(tmp,ptr);
139
 
  return ((int64_t) tmp) + 1; /* SQL is from 1, we store from 0 */
 
169
  switch (packlength) {
 
170
  case 1:
 
171
    return (int64_t) ptr[0];
 
172
  case 2:
 
173
  {
 
174
    uint16_t tmp;
 
175
#ifdef WORDS_BIGENDIAN
 
176
    if (table->s->db_low_byte_first)
 
177
      tmp=sint2korr(ptr);
 
178
    else
 
179
#endif
 
180
      shortget(tmp,ptr);
 
181
    return (int64_t) tmp;
 
182
  }
 
183
  case 3:
 
184
    return (int64_t) uint3korr(ptr);
 
185
  case 4:
 
186
  {
 
187
    uint32_t tmp;
 
188
#ifdef WORDS_BIGENDIAN
 
189
    if (table->s->db_low_byte_first)
 
190
      tmp=uint4korr(ptr);
 
191
    else
 
192
#endif
 
193
      longget(tmp,ptr);
 
194
    return (int64_t) tmp;
 
195
  }
 
196
  case 8:
 
197
  {
 
198
    int64_t tmp;
 
199
#ifdef WORDS_BIGENDIAN
 
200
    if (table->s->db_low_byte_first)
 
201
      tmp=sint8korr(ptr);
 
202
    else
 
203
#endif
 
204
      int64_tget(tmp,ptr);
 
205
    return tmp;
 
206
  }
 
207
  }
 
208
  return 0;                                     // impossible
140
209
}
141
210
 
142
211
String *Field_enum::val_str(String *, String *val_ptr)
145
214
 
146
215
  ASSERT_COLUMN_MARKED_FOR_READ;
147
216
 
148
 
  if (not tmp || tmp > typelib->count)
 
217
  if (!tmp || tmp > typelib->count)
149
218
  {
150
219
    val_ptr->set("", 0, field_charset);
151
220
  }
161
230
{
162
231
  unsigned char *old= ptr;
163
232
  ptr= (unsigned char*) a_ptr;
164
 
  uint64_t a= Field_enum::val_int();
 
233
  uint64_t a=Field_enum::val_int();
165
234
  ptr= (unsigned char*) b_ptr;
166
 
  uint64_t b= Field_enum::val_int();
 
235
  uint64_t b=Field_enum::val_int();
167
236
  ptr= old;
168
237
  return (a < b) ? -1 : (a > b) ? 1 : 0;
169
238
}
170
239
 
171
240
void Field_enum::sort_string(unsigned char *to,uint32_t )
172
241
{
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++)
 
242
  uint64_t value=Field_enum::val_int();
 
243
  to+=packlength-1;
 
244
  for (uint32_t i=0 ; i < packlength ; i++)
176
245
  {
177
246
    *to-- = (unsigned char) (value & 255);
178
247
    value>>=8;
191
260
  uint32_t *len= typelib->type_lengths;
192
261
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
193
262
  {
194
 
    size_t dummy_errors;
 
263
    uint32_t dummy_errors;
195
264
    if (flag)
196
265
      res.append(',');
197
266
    /* convert to res.charset() == utf8, then quote */
208
277
  Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
209
278
  if (res)
210
279
  {
211
 
    res->typelib= typelib->copy_typelib(root);
 
280
    res->typelib= copy_typelib(root, typelib);
212
281
  }
213
282
  return res;
214
283
}