~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/enum.cc

  • Committer: Patrick Crews
  • Date: 2010-09-14 20:21:03 UTC
  • mto: (1771.1.1 pcrews)
  • mto: This revision was merged to the branch mainline in revision 1772.
  • Revision ID: gleebix@gmail.com-20100914202103-1db2n0bshzafep19
Moved transaction_log tests into updated non-publisher-based 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
 
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
  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
 
41
55
void Field_enum::store_type(uint64_t value)
42
56
{
43
57
  value--; /* we store as starting from 0, although SQL starts from 1 */
44
58
 
 
59
  switch (packlength) {
 
60
  case 1: ptr[0]= (unsigned char) value;  break;
 
61
  case 2:
45
62
#ifdef WORDS_BIGENDIAN
46
 
  if (getTable()->getShare()->db_low_byte_first)
 
63
  if (getTable()->s->db_low_byte_first)
47
64
  {
48
 
    int4store(ptr, (unsigned short) value);
 
65
    int2store(ptr,(unsigned short) value);
49
66
  }
50
67
  else
51
68
#endif
52
 
    longstore(ptr, (unsigned short) value);
 
69
    shortstore(ptr,(unsigned short) value);
 
70
  break;
 
71
  default:
 
72
    assert(packlength <= 2);
 
73
  }
53
74
}
54
75
 
55
76
/**
110
131
 
111
132
  if (from <= 0 || (uint64_t) from > typelib->count)
112
133
  {
113
 
    /* Convert the integer to a string using boost::lexical_cast */
114
 
    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;
115
138
 
116
139
    my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), tmp.c_str());
117
140
    return 1;
129
152
{
130
153
  ASSERT_COLUMN_MARKED_FOR_READ;
131
154
 
132
 
  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;
133
161
#ifdef WORDS_BIGENDIAN
134
 
  if (getTable()->getShare()->db_low_byte_first)
135
 
    tmp= sint4korr(ptr);
136
 
  else
 
162
    if (getTable()->s->db_low_byte_first)
 
163
      tmp=sint2korr(ptr);
 
164
    else
137
165
#endif
138
 
    longget(tmp,ptr);
139
 
  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
140
173
}
141
174
 
142
175
String *Field_enum::val_str(String *, String *val_ptr)
145
178
 
146
179
  ASSERT_COLUMN_MARKED_FOR_READ;
147
180
 
148
 
  if (not tmp || tmp > typelib->count)
 
181
  if (!tmp || tmp > typelib->count)
149
182
  {
150
183
    val_ptr->set("", 0, field_charset);
151
184
  }
161
194
{
162
195
  unsigned char *old= ptr;
163
196
  ptr= (unsigned char*) a_ptr;
164
 
  uint64_t a= Field_enum::val_int();
 
197
  uint64_t a=Field_enum::val_int();
165
198
  ptr= (unsigned char*) b_ptr;
166
 
  uint64_t b= Field_enum::val_int();
 
199
  uint64_t b=Field_enum::val_int();
167
200
  ptr= old;
168
201
  return (a < b) ? -1 : (a > b) ? 1 : 0;
169
202
}
171
204
void Field_enum::sort_string(unsigned char *to,uint32_t )
172
205
{
173
206
  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++)
 
207
  to+=packlength-1;
 
208
  for (uint32_t i=0 ; i < packlength ; i++)
176
209
  {
177
210
    *to-- = (unsigned char) (value & 255);
178
211
    value>>=8;
191
224
  uint32_t *len= typelib->type_lengths;
192
225
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
193
226
  {
194
 
    size_t dummy_errors;
 
227
    uint32_t dummy_errors;
195
228
    if (flag)
196
229
      res.append(',');
197
230
    /* convert to res.charset() == utf8, then quote */
208
241
  Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
209
242
  if (res)
210
243
  {
211
 
    res->typelib= typelib->copy_typelib(root);
 
244
    res->typelib= copy_typelib(root, typelib);
212
245
  }
213
246
  return res;
214
247
}