~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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
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>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
23
#include <drizzled/field/enum.h>
24
#include <drizzled/error.h>
25
#include <drizzled/table.h>
26
#include <drizzled/session.h>
27
#include <drizzled/typelib.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
2275.2.20 by Olaf van der Spek
Typelib Options
32
namespace drizzled {
1253.1.3 by Monty Taylor
MEM_ROOT == memory::Root
33
438.4.1 by Lee
breaking out enum field classes
34
/****************************************************************************
35
** enum type.
36
** This is a string which only can have a selection of different values.
37
** If one uses this string in a number context one gets the type number.
38
****************************************************************************/
39
40
void Field_enum::store_type(uint64_t value)
41
{
1579.3.13 by Stewart Smith
fix storage, display and sorting of enum with 65536 elements.
42
  value--; /* we store as starting from 0, although SQL starts from 1 */
43
438.4.1 by Lee
breaking out enum field classes
44
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
45
  if (getTable()->getShare()->db_low_byte_first)
438.4.1 by Lee
breaking out enum field classes
46
  {
1782.4.4 by Brian Aker
Fix enum at being an intefer (which is what PG did, and it saves on
47
    int4store(ptr, (unsigned short) value);
438.4.1 by Lee
breaking out enum field classes
48
  }
49
  else
50
#endif
1782.4.4 by Brian Aker
Fix enum at being an intefer (which is what PG did, and it saves on
51
    longstore(ptr, (unsigned short) value);
438.4.1 by Lee
breaking out enum field classes
52
}
53
54
/**
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
55
 * Given a supplied string, looks up the string in the internal typelib
1579.3.1 by Stewart Smith
fix trailing whitespace in field/enum
56
 * 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
57
 * we always throw an error.
58
 */
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
59
int Field_enum::store(const char *from, uint32_t length, const charset_info_st * const)
438.4.1 by Lee
breaking out enum field classes
60
{
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
61
  uint32_t tmp;
438.4.1 by Lee
breaking out enum field classes
62
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
63
  ASSERT_COLUMN_MARKED_FOR_WRITE;
64
438.4.1 by Lee
breaking out enum field classes
65
  /* Remove end space */
66
  length= field_charset->cset->lengthsp(field_charset, from, length);
2151.5.4 by Olaf van der Spek
Move strfunc functions into TYPELIB class
67
  tmp= typelib->find_type2(from, length, field_charset);
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
68
  if (! tmp)
438.4.1 by Lee
breaking out enum field classes
69
  {
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
70
    if (length < 6) /* Can't be more than 99999 enums */
438.4.1 by Lee
breaking out enum field classes
71
    {
72
      /* 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
73
      /* Convert the string to an integer using stringstream */
74
      std::stringstream ss;
75
      ss << from;
76
      ss >> tmp;
77
78
      if (tmp == 0 || tmp > typelib->count)
438.4.1 by Lee
breaking out enum field classes
79
      {
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
80
        my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
81
        return 1;
438.4.1 by Lee
breaking out enum field classes
82
      }
83
    }
84
    else
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
85
    {
86
      my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), from);
87
      return 1;
88
    }
438.4.1 by Lee
breaking out enum field classes
89
  }
90
  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
91
  return 0;
92
}
93
94
int Field_enum::store(double from)
95
{
96
  return Field_enum::store((int64_t) from, false);
97
}
98
99
/**
100
 * @note MySQL allows 0 values, saying that 0 is "the index of the
101
 * blank string error", whatever that means.  Uhm, Drizzle doesn't
102
 * allow this.  To store an ENUM column value using an integer, you
1579.3.1 by Stewart Smith
fix trailing whitespace in field/enum
103
 * 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
104
 * key.
105
 */
106
int Field_enum::store(int64_t from, bool)
107
{
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
108
  ASSERT_COLUMN_MARKED_FOR_WRITE;
109
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
110
  if (from <= 0 || (uint64_t) from > typelib->count)
438.4.1 by Lee
breaking out enum field classes
111
  {
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.
112
    /* 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
113
    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
114
115
    my_error(ER_INVALID_ENUM_VALUE, MYF(ME_FATALERROR), tmp.c_str());
116
    return 1;
438.4.1 by Lee
breaking out enum field classes
117
  }
934.4.1 by Jay Pipes
Fixes ENUM field type to throw an error on bad data input. 0 is now not
118
  store_type((uint64_t) (uint32_t) from);
119
  return 0;
438.4.1 by Lee
breaking out enum field classes
120
}
121
2181.2.1 by Brian Aker
Protect all of the val_* methods from modification.
122
double Field_enum::val_real(void) const
438.4.1 by Lee
breaking out enum field classes
123
{
124
  return (double) Field_enum::val_int();
125
}
126
2181.2.1 by Brian Aker
Protect all of the val_* methods from modification.
127
int64_t Field_enum::val_int(void) const
438.4.1 by Lee
breaking out enum field classes
128
{
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
129
  ASSERT_COLUMN_MARKED_FOR_READ;
130
1782.4.4 by Brian Aker
Fix enum at being an intefer (which is what PG did, and it saves on
131
  uint16_t tmp;
438.4.1 by Lee
breaking out enum field classes
132
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
133
  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
134
    tmp= sint4korr(ptr);
135
  else
438.4.1 by Lee
breaking out enum field classes
136
#endif
1782.4.4 by Brian Aker
Fix enum at being an intefer (which is what PG did, and it saves on
137
    longget(tmp,ptr);
138
  return ((int64_t) tmp) + 1; /* SQL is from 1, we store from 0 */
438.4.1 by Lee
breaking out enum field classes
139
}
140
2181.2.1 by Brian Aker
Protect all of the val_* methods from modification.
141
String *Field_enum::val_str(String *, String *val_ptr) const
438.4.1 by Lee
breaking out enum field classes
142
{
895 by Brian Aker
Completion (?) of uint conversion.
143
  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
144
145
  ASSERT_COLUMN_MARKED_FOR_READ;
146
1996.2.1 by Brian Aker
uuid type code.
147
  if (not tmp || tmp > typelib->count)
1530.1.1 by Brian Aker
Removes typelib usage from fieldname
148
  {
438.4.1 by Lee
breaking out enum field classes
149
    val_ptr->set("", 0, field_charset);
1530.1.1 by Brian Aker
Removes typelib usage from fieldname
150
  }
438.4.1 by Lee
breaking out enum field classes
151
  else
1530.1.1 by Brian Aker
Removes typelib usage from fieldname
152
  {
153
    val_ptr->set((const char*) typelib->type_names[tmp-1], typelib->type_lengths[tmp-1], field_charset);
154
  }
155
438.4.1 by Lee
breaking out enum field classes
156
  return val_ptr;
157
}
158
481 by Brian Aker
Remove all of uchar.
159
int Field_enum::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
438.4.1 by Lee
breaking out enum field classes
160
{
481 by Brian Aker
Remove all of uchar.
161
  unsigned char *old= ptr;
162
  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
163
  uint64_t a= Field_enum::val_int();
481 by Brian Aker
Remove all of uchar.
164
  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
165
  uint64_t b= Field_enum::val_int();
438.4.1 by Lee
breaking out enum field classes
166
  ptr= old;
167
  return (a < b) ? -1 : (a > b) ? 1 : 0;
168
}
169
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
170
void Field_enum::sort_string(unsigned char *to,uint32_t )
438.4.1 by Lee
breaking out enum field classes
171
{
1579.3.13 by Stewart Smith
fix storage, display and sorting of enum with 65536 elements.
172
  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
173
  to+=pack_length() -1;
174
  for (uint32_t i=0 ; i < pack_length() ; i++)
438.4.1 by Lee
breaking out enum field classes
175
  {
481 by Brian Aker
Remove all of uchar.
176
    *to-- = (unsigned char) (value & 255);
438.4.1 by Lee
breaking out enum field classes
177
    value>>=8;
178
  }
179
}
180
1253.1.3 by Monty Taylor
MEM_ROOT == memory::Root
181
Field *Field_enum::new_field(memory::Root *root, Table *new_table,
438.4.1 by Lee
breaking out enum field classes
182
                             bool keep_type)
183
{
184
  Field_enum *res= (Field_enum*) Field::new_field(root, new_table, keep_type);
185
  if (res)
1530.1.1 by Brian Aker
Removes typelib usage from fieldname
186
  {
2275.2.20 by Olaf van der Spek
Typelib Options
187
    res->typelib= typelib->copy_typelib(*root);
1530.1.1 by Brian Aker
Removes typelib usage from fieldname
188
  }
438.4.1 by Lee
breaking out enum field classes
189
  return res;
190
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
191
192
} /* namespace drizzled */