~drizzle-trunk/drizzle/development

466 by Monty Taylor
Fixed modelines... these files are c++.
1
/* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
438.4.1 by Lee
breaking out enum field classes
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
21
#include "config.h"
1775.5.1 by earney
modified files containing stringstream to use boost:lexical_cast instead as
22
#include <boost/lexical_cast.hpp>
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
23
#include "drizzled/field/enum.h"
24
#include "drizzled/error.h"
25
#include "drizzled/table.h"
26
#include "drizzled/session.h"
1241.9.12 by Monty Taylor
Trims more out of server_includes.h.
27
#include "drizzled/strfunc.h"
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
28
29
#include <sstream>
30
#include <string>
438.4.1 by Lee
breaking out enum field classes
31
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
32
namespace drizzled
33
{
1253.1.3 by Monty Taylor
MEM_ROOT == memory::Root
34
438.4.1 by Lee
breaking out enum field classes
35
/****************************************************************************
36
** enum type.
37
** This is a string which only can have a selection of different values.
38
** If one uses this string in a number context one gets the type number.
39
****************************************************************************/
40
41
void Field_enum::store_type(uint64_t value)
42
{
1579.3.13 by Stewart Smith
fix storage, display and sorting of enum with 65536 elements.
43
  value--; /* we store as starting from 0, although SQL starts from 1 */
44
438.4.1 by Lee
breaking out enum field classes
45
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
46
  if (getTable()->getShare()->db_low_byte_first)
438.4.1 by Lee
breaking out enum field classes
47
  {
1782.4.4 by Brian Aker
Fix enum at being an intefer (which is what PG did, and it saves on
48
    int4store(ptr, (unsigned short) value);
438.4.1 by Lee
breaking out enum field classes
49
  }
50
  else
51
#endif
1782.4.4 by Brian Aker
Fix enum at being an intefer (which is what PG did, and it saves on
52
    longstore(ptr, (unsigned short) value);
438.4.1 by Lee
breaking out enum field classes
53
}
54
55
/**
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
56
 * Given a supplied string, looks up the string in the internal typelib
1579.3.1 by Stewart Smith
fix trailing whitespace in field/enum
57
 * and stores the found key.  Upon not finding an entry in the typelib,
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
58
 * we always throw an error.
59
 */
60
int Field_enum::store(const char *from, uint32_t length, const CHARSET_INFO * const)
438.4.1 by Lee
breaking out enum field classes
61
{
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
62
  uint32_t tmp;
438.4.1 by Lee
breaking out enum field classes
63
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
64
  ASSERT_COLUMN_MARKED_FOR_WRITE;
65
438.4.1 by Lee
breaking out enum field classes
66
  /* Remove end space */
67
  length= field_charset->cset->lengthsp(field_charset, from, length);
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
68
  tmp= find_type2(typelib, from, length, field_charset);
69
  if (! tmp)
438.4.1 by Lee
breaking out enum field classes
70
  {
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
71
    if (length < 6) /* Can't be more than 99999 enums */
438.4.1 by Lee
breaking out enum field classes
72
    {
73
      /* This is for reading numbers with LOAD DATA INFILE */
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
74
      /* Convert the string to an integer using stringstream */
75
      std::stringstream ss;
76
      ss << from;
77
      ss >> tmp;
78
79
      if (tmp == 0 || tmp > typelib->count)
438.4.1 by Lee
breaking out enum field classes
80
      {
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
81
        my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
82
        return 1;
438.4.1 by Lee
breaking out enum field classes
83
      }
84
    }
85
    else
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
86
    {
87
      my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
88
      return 1;
89
    }
438.4.1 by Lee
breaking out enum field classes
90
  }
91
  store_type((uint64_t) tmp);
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
92
  return 0;
93
}
94
95
int Field_enum::store(double from)
96
{
97
  return Field_enum::store((int64_t) from, false);
98
}
99
100
/**
101
 * @note MySQL allows 0 values, saying that 0 is "the index of the
102
 * blank string error", whatever that means.  Uhm, Drizzle doesn't
103
 * allow this.  To store an ENUM column value using an integer, you
1579.3.1 by Stewart Smith
fix trailing whitespace in field/enum
104
 * must specify the 1-based index of the enum column definition's
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
105
 * key.
106
 */
107
int Field_enum::store(int64_t from, bool)
108
{
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
109
  ASSERT_COLUMN_MARKED_FOR_WRITE;
110
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
111
  if (from <= 0 || (uint64_t) from > typelib->count)
438.4.1 by Lee
breaking out enum field classes
112
  {
1775.5.2 by earney
removed old commented out statements and uncommmeted out first occurance of stringstream in date.cc since it sets the precision.
113
    /* Convert the integer to a string using boost::lexical_cast */
1775.5.1 by earney
modified files containing stringstream to use boost:lexical_cast instead as
114
    std::string tmp(boost::lexical_cast<std::string>(from));
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
115
116
    my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), tmp.c_str());
117
    return 1;
438.4.1 by Lee
breaking out enum field classes
118
  }
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
119
  store_type((uint64_t) (uint32_t) from);
120
  return 0;
438.4.1 by Lee
breaking out enum field classes
121
}
122
123
double Field_enum::val_real(void)
124
{
125
  return (double) Field_enum::val_int();
126
}
127
128
int64_t Field_enum::val_int(void)
129
{
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
130
  ASSERT_COLUMN_MARKED_FOR_READ;
131
1782.4.4 by Brian Aker
Fix enum at being an intefer (which is what PG did, and it saves on
132
  uint16_t tmp;
438.4.1 by Lee
breaking out enum field classes
133
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
134
  if (getTable()->getShare()->db_low_byte_first)
1782.4.4 by Brian Aker
Fix enum at being an intefer (which is what PG did, and it saves on
135
    tmp= sint4korr(ptr);
136
  else
438.4.1 by Lee
breaking out enum field classes
137
#endif
1782.4.4 by Brian Aker
Fix enum at being an intefer (which is what PG did, and it saves on
138
    longget(tmp,ptr);
139
  return ((int64_t) tmp) + 1; /* SQL is from 1, we store from 0 */
438.4.1 by Lee
breaking out enum field classes
140
}
141
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
142
String *Field_enum::val_str(String *, String *val_ptr)
438.4.1 by Lee
breaking out enum field classes
143
{
895 by Brian Aker
Completion (?) of uint conversion.
144
  uint32_t tmp=(uint32_t) Field_enum::val_int();
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
145
146
  ASSERT_COLUMN_MARKED_FOR_READ;
147
438.4.1 by Lee
breaking out enum field classes
148
  if (!tmp || tmp > typelib->count)
1530.1.1 by Brian Aker
Removes typelib usage from fieldname
149
  {
438.4.1 by Lee
breaking out enum field classes
150
    val_ptr->set("", 0, field_charset);
1530.1.1 by Brian Aker
Removes typelib usage from fieldname
151
  }
438.4.1 by Lee
breaking out enum field classes
152
  else
1530.1.1 by Brian Aker
Removes typelib usage from fieldname
153
  {
154
    val_ptr->set((const char*) typelib->type_names[tmp-1], typelib->type_lengths[tmp-1], field_charset);
155
  }
156
438.4.1 by Lee
breaking out enum field classes
157
  return val_ptr;
158
}
159
481 by Brian Aker
Remove all of uchar.
160
int Field_enum::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
438.4.1 by Lee
breaking out enum field classes
161
{
481 by Brian Aker
Remove all of uchar.
162
  unsigned char *old= ptr;
163
  ptr= (unsigned char*) a_ptr;
1782.4.4 by Brian Aker
Fix enum at being an intefer (which is what PG did, and it saves on
164
  uint64_t a= Field_enum::val_int();
481 by Brian Aker
Remove all of uchar.
165
  ptr= (unsigned char*) b_ptr;
1782.4.4 by Brian Aker
Fix enum at being an intefer (which is what PG did, and it saves on
166
  uint64_t b= Field_enum::val_int();
438.4.1 by Lee
breaking out enum field classes
167
  ptr= old;
168
  return (a < b) ? -1 : (a > b) ? 1 : 0;
169
}
170
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
171
void Field_enum::sort_string(unsigned char *to,uint32_t )
438.4.1 by Lee
breaking out enum field classes
172
{
1579.3.13 by Stewart Smith
fix storage, display and sorting of enum with 65536 elements.
173
  uint64_t value=Field_enum::val_int()-1; /* SQL is 1 based, stored as 0 based*/
1782.4.4 by Brian Aker
Fix enum at being an intefer (which is what PG did, and it saves on
174
  to+=pack_length() -1;
175
  for (uint32_t i=0 ; i < pack_length() ; i++)
438.4.1 by Lee
breaking out enum field classes
176
  {
481 by Brian Aker
Remove all of uchar.
177
    *to-- = (unsigned char) (value & 255);
438.4.1 by Lee
breaking out enum field classes
178
    value>>=8;
179
  }
180
}
181
182
void Field_enum::sql_type(String &res) const
183
{
184
  char buffer[255];
185
  String enum_item(buffer, sizeof(buffer), res.charset());
186
187
  res.length(0);
188
  res.append(STRING_WITH_LEN("enum("));
189
190
  bool flag=0;
482 by Brian Aker
Remove uint.
191
  uint32_t *len= typelib->type_lengths;
438.4.1 by Lee
breaking out enum field classes
192
  for (const char **pos= typelib->type_names; *pos; pos++, len++)
193
  {
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
194
    size_t dummy_errors;
438.4.1 by Lee
breaking out enum field classes
195
    if (flag)
196
      res.append(',');
197
    /* convert to res.charset() == utf8, then quote */
198
    enum_item.copy(*pos, *len, charset(), res.charset(), &dummy_errors);
814.1.1 by Jay Pipes
Fix for Bug 314502 "show create table crashes with multi-byte character in enum description"
199
    append_unescaped(&res, enum_item.c_ptr(), enum_item.length());
438.4.1 by Lee
breaking out enum field classes
200
    flag= 1;
201
  }
202
  res.append(')');
203
}
204
1253.1.3 by Monty Taylor
MEM_ROOT == memory::Root
205
Field *Field_enum::new_field(memory::Root *root, Table *new_table,
438.4.1 by Lee
breaking out enum field classes
206
                             bool keep_type)
207
{
208
  Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
209
  if (res)
1530.1.1 by Brian Aker
Removes typelib usage from fieldname
210
  {
438.4.1 by Lee
breaking out enum field classes
211
    res->typelib= copy_typelib(root, typelib);
1530.1.1 by Brian Aker
Removes typelib usage from fieldname
212
  }
438.4.1 by Lee
breaking out enum field classes
213
  return res;
214
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
215
216
} /* namespace drizzled */