~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/enum.cc

  • Committer: Mark Atwood
  • Date: 2008-10-03 01:39:40 UTC
  • mto: This revision was merged to the branch mainline in revision 437.
  • Revision ID: mark@fallenpegasus.com-20081003013940-mvefjo725dltz41h
rename logging_noop to logging_query

Show diffs side-by-side

added added

removed removed

Lines of Context:
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/strfunc.h>
28
 
#include <drizzled/typelib.h>
29
 
 
30
 
#include <sstream>
31
 
#include <string>
32
 
 
33
 
namespace drizzled
34
 
{
35
 
 
36
 
/****************************************************************************
37
 
** enum type.
38
 
** This is a string which only can have a selection of different values.
39
 
** If one uses this string in a number context one gets the type number.
40
 
****************************************************************************/
41
 
 
42
 
void Field_enum::store_type(uint64_t value)
43
 
{
44
 
  value--; /* we store as starting from 0, although SQL starts from 1 */
45
 
 
46
 
#ifdef WORDS_BIGENDIAN
47
 
  if (getTable()->getShare()->db_low_byte_first)
48
 
  {
49
 
    int4store(ptr, (unsigned short) value);
50
 
  }
51
 
  else
52
 
#endif
53
 
    longstore(ptr, (unsigned short) value);
54
 
}
55
 
 
56
 
/**
57
 
 * Given a supplied string, looks up the string in the internal typelib
58
 
 * and stores the found key.  Upon not finding an entry in the typelib,
59
 
 * we always throw an error.
60
 
 */
61
 
int Field_enum::store(const char *from, uint32_t length, const CHARSET_INFO * const)
62
 
{
63
 
  uint32_t tmp;
64
 
 
65
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
66
 
 
67
 
  /* Remove end space */
68
 
  length= field_charset->cset->lengthsp(field_charset, from, length);
69
 
  tmp= typelib->find_type2(from, length, field_charset);
70
 
  if (! tmp)
71
 
  {
72
 
    if (length < 6) /* Can't be more than 99999 enums */
73
 
    {
74
 
      /* This is for reading numbers with LOAD DATA INFILE */
75
 
      /* Convert the string to an integer using stringstream */
76
 
      std::stringstream ss;
77
 
      ss << from;
78
 
      ss >> tmp;
79
 
 
80
 
      if (tmp == 0 || tmp > typelib->count)
81
 
      {
82
 
        my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
83
 
        return 1;
84
 
      }
85
 
    }
86
 
    else
87
 
    {
88
 
      my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
89
 
      return 1;
90
 
    }
91
 
  }
92
 
  store_type((uint64_t) tmp);
93
 
  return 0;
94
 
}
95
 
 
96
 
int Field_enum::store(double from)
97
 
{
98
 
  return Field_enum::store((int64_t) from, false);
99
 
}
100
 
 
101
 
/**
102
 
 * @note MySQL allows 0 values, saying that 0 is "the index of the
103
 
 * blank string error", whatever that means.  Uhm, Drizzle doesn't
104
 
 * allow this.  To store an ENUM column value using an integer, you
105
 
 * must specify the 1-based index of the enum column definition's
106
 
 * key.
107
 
 */
108
 
int Field_enum::store(int64_t from, bool)
109
 
{
110
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
111
 
 
112
 
  if (from <= 0 || (uint64_t) from > typelib->count)
113
 
  {
114
 
    /* Convert the integer to a string using boost::lexical_cast */
115
 
    std::string tmp(boost::lexical_cast<std::string>(from));
116
 
 
117
 
    my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), tmp.c_str());
118
 
    return 1;
119
 
  }
120
 
  store_type((uint64_t) (uint32_t) from);
121
 
  return 0;
122
 
}
123
 
 
124
 
double Field_enum::val_real(void) const
125
 
{
126
 
  return (double) Field_enum::val_int();
127
 
}
128
 
 
129
 
int64_t Field_enum::val_int(void) const
130
 
{
131
 
  ASSERT_COLUMN_MARKED_FOR_READ;
132
 
 
133
 
  uint16_t tmp;
134
 
#ifdef WORDS_BIGENDIAN
135
 
  if (getTable()->getShare()->db_low_byte_first)
136
 
    tmp= sint4korr(ptr);
137
 
  else
138
 
#endif
139
 
    longget(tmp,ptr);
140
 
  return ((int64_t) tmp) + 1; /* SQL is from 1, we store from 0 */
141
 
}
142
 
 
143
 
String *Field_enum::val_str(String *, String *val_ptr) const
144
 
{
145
 
  uint32_t tmp=(uint32_t) Field_enum::val_int();
146
 
 
147
 
  ASSERT_COLUMN_MARKED_FOR_READ;
148
 
 
149
 
  if (not tmp || tmp > typelib->count)
150
 
  {
151
 
    val_ptr->set("", 0, field_charset);
152
 
  }
153
 
  else
154
 
  {
155
 
    val_ptr->set((const char*) typelib->type_names[tmp-1], typelib->type_lengths[tmp-1], field_charset);
156
 
  }
157
 
 
158
 
  return val_ptr;
159
 
}
160
 
 
161
 
int Field_enum::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
162
 
{
163
 
  unsigned char *old= ptr;
164
 
  ptr= (unsigned char*) a_ptr;
165
 
  uint64_t a= Field_enum::val_int();
166
 
  ptr= (unsigned char*) b_ptr;
167
 
  uint64_t b= Field_enum::val_int();
168
 
  ptr= old;
169
 
  return (a < b) ? -1 : (a > b) ? 1 : 0;
170
 
}
171
 
 
172
 
void Field_enum::sort_string(unsigned char *to,uint32_t )
173
 
{
174
 
  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++)
177
 
  {
178
 
    *to-- = (unsigned char) (value & 255);
179
 
    value>>=8;
180
 
  }
181
 
}
182
 
 
183
 
void Field_enum::sql_type(String &res) const
184
 
{
185
 
  char buffer[255];
186
 
  String enum_item(buffer, sizeof(buffer), res.charset());
187
 
 
188
 
  res.length(0);
189
 
  res.append(STRING_WITH_LEN("enum("));
190
 
 
191
 
  bool flag=0;
192
 
  uint32_t *len= typelib->type_lengths;
193
 
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
194
 
  {
195
 
    size_t dummy_errors;
196
 
    if (flag)
197
 
      res.append(',');
198
 
    /* convert to res.charset() == utf8, then quote */
199
 
    enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
200
 
    append_unescaped(&res, enum_item.c_ptr(), enum_item.length());
201
 
    flag= 1;
202
 
  }
203
 
  res.append(')');
204
 
}
205
 
 
206
 
Field *Field_enum::new_field(memory::Root *root, Table *new_table,
207
 
                             bool keep_type)
208
 
{
209
 
  Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
210
 
  if (res)
211
 
  {
212
 
    res->typelib= typelib->copy_typelib(root);
213
 
  }
214
 
  return res;
215
 
}
216
 
 
217
 
} /* namespace drizzled */