~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/enum.cc

  • Committer: Monty Taylor
  • Date: 2010-08-12 20:27:32 UTC
  • mto: (1720.1.5 build)
  • mto: This revision was merged to the branch mainline in revision 1722.
  • Revision ID: mordred@inaugust.com-20100812202732-9kzchbkvkyki4n3u
Merged libdrizzle directly into tree.

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
 
#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/strfunc.h>
28
 
#include <drizzled/typelib.h>
 
21
 
 
22
#include "config.h"
 
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"
29
28
 
30
29
#include <sstream>
31
30
#include <string>
39
38
** If one uses this string in a number context one gets the type number.
40
39
****************************************************************************/
41
40
 
 
41
enum ha_base_keytype Field_enum::key_type() const
 
42
{
 
43
  switch (packlength)
 
44
  {
 
45
  case 1:
 
46
    return HA_KEYTYPE_BINARY;
 
47
  case 2:
 
48
    return HA_KEYTYPE_ULONG_INT;
 
49
  default:
 
50
    assert(packlength <= 2);
 
51
    return HA_KEYTYPE_ULONG_INT;
 
52
  }
 
53
}
 
54
 
42
55
void Field_enum::store_type(uint64_t value)
43
56
{
44
57
  value--; /* we store as starting from 0, although SQL starts from 1 */
45
58
 
 
59
  switch (packlength) {
 
60
  case 1: ptr[0]= (unsigned char) value;  break;
 
61
  case 2:
46
62
#ifdef WORDS_BIGENDIAN
47
 
  if (getTable()->getShare()->db_low_byte_first)
 
63
  if (getTable()->s->db_low_byte_first)
48
64
  {
49
 
    int4store(ptr, (unsigned short) value);
 
65
    int2store(ptr,(unsigned short) value);
50
66
  }
51
67
  else
52
68
#endif
53
 
    longstore(ptr, (unsigned short) value);
 
69
    shortstore(ptr,(unsigned short) value);
 
70
  break;
 
71
  default:
 
72
    assert(packlength <= 2);
 
73
  }
54
74
}
55
75
 
56
76
/**
66
86
 
67
87
  /* Remove end space */
68
88
  length= field_charset->cset->lengthsp(field_charset, from, length);
69
 
  tmp= typelib->find_type2(from, length, field_charset);
 
89
  tmp= find_type2(typelib, from, length, field_charset);
70
90
  if (! tmp)
71
91
  {
72
92
    if (length < 6) /* Can't be more than 99999 enums */
111
131
 
112
132
  if (from <= 0 || (uint64_t) from > typelib->count)
113
133
  {
114
 
    /* Convert the integer to a string using boost::lexical_cast */
115
 
    std::string tmp(boost::lexical_cast<std::string>(from));
 
134
    /* Convert the integer to a string using stringstream */
 
135
    std::stringstream ss;
 
136
    std::string tmp;
 
137
    ss << from; ss >> tmp;
116
138
 
117
139
    my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), tmp.c_str());
118
140
    return 1;
121
143
  return 0;
122
144
}
123
145
 
124
 
double Field_enum::val_real(void) const
 
146
double Field_enum::val_real(void)
125
147
{
126
148
  return (double) Field_enum::val_int();
127
149
}
128
150
 
129
 
int64_t Field_enum::val_int(void) const
 
151
int64_t Field_enum::val_int(void)
130
152
{
131
153
  ASSERT_COLUMN_MARKED_FOR_READ;
132
154
 
133
 
  uint16_t tmp;
 
155
  switch (packlength) {
 
156
  case 1:
 
157
    return ((int64_t) ptr[0]) + 1; /* SQL is from 1, we store from 0 */
 
158
  case 2:
 
159
  {
 
160
    uint16_t tmp;
134
161
#ifdef WORDS_BIGENDIAN
135
 
  if (getTable()->getShare()->db_low_byte_first)
136
 
    tmp= sint4korr(ptr);
137
 
  else
 
162
    if (getTable()->s->db_low_byte_first)
 
163
      tmp=sint2korr(ptr);
 
164
    else
138
165
#endif
139
 
    longget(tmp,ptr);
140
 
  return ((int64_t) tmp) + 1; /* SQL is from 1, we store from 0 */
 
166
      shortget(tmp,ptr);
 
167
    return ((int64_t) tmp) + 1; /* SQL is from 1, we store from 0 */
 
168
  }
 
169
  default:
 
170
    assert(packlength <= 2);
 
171
  }
 
172
  return 0;                                     // impossible
141
173
}
142
174
 
143
 
String *Field_enum::val_str(String *, String *val_ptr) const
 
175
String *Field_enum::val_str(String *, String *val_ptr)
144
176
{
145
177
  uint32_t tmp=(uint32_t) Field_enum::val_int();
146
178
 
147
179
  ASSERT_COLUMN_MARKED_FOR_READ;
148
180
 
149
 
  if (not tmp || tmp > typelib->count)
 
181
  if (!tmp || tmp > typelib->count)
150
182
  {
151
183
    val_ptr->set("", 0, field_charset);
152
184
  }
162
194
{
163
195
  unsigned char *old= ptr;
164
196
  ptr= (unsigned char*) a_ptr;
165
 
  uint64_t a= Field_enum::val_int();
 
197
  uint64_t a=Field_enum::val_int();
166
198
  ptr= (unsigned char*) b_ptr;
167
 
  uint64_t b= Field_enum::val_int();
 
199
  uint64_t b=Field_enum::val_int();
168
200
  ptr= old;
169
201
  return (a < b) ? -1 : (a > b) ? 1 : 0;
170
202
}
172
204
void Field_enum::sort_string(unsigned char *to,uint32_t )
173
205
{
174
206
  uint64_t value=Field_enum::val_int()-1; /* SQL is 1 based, stored as 0 based*/
175
 
  to+=pack_length() -1;
176
 
  for (uint32_t i=0 ; i < pack_length() ; i++)
 
207
  to+=packlength-1;
 
208
  for (uint32_t i=0 ; i < packlength ; i++)
177
209
  {
178
210
    *to-- = (unsigned char) (value & 255);
179
211
    value>>=8;
192
224
  uint32_t *len= typelib->type_lengths;
193
225
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
194
226
  {
195
 
    size_t dummy_errors;
 
227
    uint32_t dummy_errors;
196
228
    if (flag)
197
229
      res.append(',');
198
230
    /* convert to res.charset() == utf8, then quote */
209
241
  Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
210
242
  if (res)
211
243
  {
212
 
    res->typelib= typelib->copy_typelib(root);
 
244
    res->typelib= copy_typelib(root, typelib);
213
245
  }
214
246
  return res;
215
247
}