~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; -*-
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
22
#include <config.h>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
23
24
#include <float.h>
1241.9.24 by Monty Taylor
Removed m_string.h from server_includes.h.
25
#include <math.h>
26
27
#include <algorithm>
28
214 by Brian Aker
Rename of fields (fix issue with string and decimal .h clashing).
29
#include <drizzled/field/double.h>
549 by Monty Taylor
Took gettext.h out of header files.
30
#include <drizzled/error.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
31
#include <drizzled/table.h>
32
#include <drizzled/session.h>
2154.2.24 by Brian Aker
Merge in all changes for current_session, etc.
33
#include <drizzled/current_session.h>
34
#include <drizzled/internal/m_string.h>
1067.4.6 by Nathan Williams
Converted all usages of cmax in drizzled/field/ and drizzled/function/ to std::max.
35
36
using namespace std;
37
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
38
namespace drizzled
39
{
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
40
41
/****************************************************************************
42
  double precision floating point numbers
43
****************************************************************************/
44
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
45
int Field_double::store(const char *from,uint32_t len, const charset_info_st * const cs)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
46
{
47
  int error;
48
  char *end;
49
  double nr= my_strntod(cs,(char*) from, len, &end, &error);
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
50
51
  ASSERT_COLUMN_MARKED_FOR_WRITE;
1660.1.3 by Brian Aker
Encapsulate Table in field
52
  if (error || (!len || (((uint32_t) (end-from) != len) && getTable()->in_use->count_cuted_fields)))
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
53
  {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
54
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
55
                (error ? ER_WARN_DATA_OUT_OF_RANGE : ER_WARN_DATA_TRUNCATED), 1);
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
56
    error= error ? 1 : 2;
57
  }
58
  Field_double::store(nr);
59
  return error;
60
}
61
62
63
int Field_double::store(double nr)
64
{
65
  int error= truncate(&nr, DBL_MAX);
66
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
67
  ASSERT_COLUMN_MARKED_FOR_WRITE;
68
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
69
#ifdef WORDS_BIGENDIAN
1660.1.3 by Brian Aker
Encapsulate Table in field
70
  if (getTable()->getShare()->db_low_byte_first)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
71
  {
72
    float8store(ptr,nr);
73
  }
74
  else
75
#endif
76
    doublestore(ptr,nr);
77
  return error;
78
}
79
80
81
int Field_double::store(int64_t nr, bool unsigned_val)
82
{
83
  return Field_double::store(unsigned_val ? uint64_t2double((uint64_t) nr) :
84
                             (double) nr);
85
}
86
2181.2.1 by Brian Aker
Protect all of the val_* methods from modification.
87
double Field_double::val_real(void) const
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
88
{
89
  double j;
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
90
91
  ASSERT_COLUMN_MARKED_FOR_READ;
92
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
93
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
94
  if (getTable()->getShare()->db_low_byte_first)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
95
  {
96
    float8get(j,ptr);
97
  }
98
  else
99
#endif
100
    doubleget(j,ptr);
101
  return j;
102
}
103
2181.2.1 by Brian Aker
Protect all of the val_* methods from modification.
104
int64_t Field_double::val_int(void) const
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
105
{
106
  double j;
107
  int64_t res;
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
108
109
  ASSERT_COLUMN_MARKED_FOR_READ;
110
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
111
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
112
  if (getTable()->getShare()->db_low_byte_first)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
113
  {
114
    float8get(j,ptr);
115
  }
116
  else
117
#endif
118
    doubleget(j,ptr);
119
  /* Check whether we fit into int64_t range */
120
  if (j <= (double) INT64_MIN)
121
  {
122
    res= (int64_t) INT64_MIN;
123
    goto warn;
124
  }
125
  if (j >= (double) (uint64_t) INT64_MAX)
126
  {
127
    res= (int64_t) INT64_MAX;
128
    goto warn;
129
  }
130
  return (int64_t) rint(j);
131
132
warn:
133
  {
134
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
383.1.12 by Brian Aker
Much closer toward UTF8 being around all the time...
135
    String tmp(buf, sizeof(buf), &my_charset_utf8_general_ci), *str;
892.1.2 by Monty Taylor
Fixed solaris warnings.
136
    str= val_str(&tmp, &tmp);
2148.5.2 by Brian Aker
Additional remove of current_session.
137
    Session *session= getTable() ? getTable()->in_use : current_session;
138
    push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
139
                        ER_TRUNCATED_WRONG_VALUE,
140
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
141
                        str->c_ptr());
142
  }
143
  return res;
144
}
145
146
2181.2.1 by Brian Aker
Protect all of the val_* methods from modification.
147
String *Field_double::val_str(String *val_buffer, String *) const
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
148
{
149
  double nr;
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
150
151
  ASSERT_COLUMN_MARKED_FOR_READ;
152
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
153
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
154
  if (getTable()->getShare()->db_low_byte_first)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
155
  {
156
    float8get(nr,ptr);
157
  }
158
  else
159
#endif
160
    doubleget(nr,ptr);
161
1067.4.6 by Nathan Williams
Converted all usages of cmax in drizzled/field/ and drizzled/function/ to std::max.
162
  uint32_t to_length= max(field_length, (uint32_t)DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
163
  val_buffer->alloc(to_length);
164
  char *to=(char*) val_buffer->ptr();
165
  size_t len;
166
167
  if (dec >= NOT_FIXED_DEC)
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
168
    len= internal::my_gcvt(nr, internal::MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
169
  else
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
170
    len= internal::my_fcvt(nr, dec, to, NULL);
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
171
895 by Brian Aker
Completion (?) of uint conversion.
172
  val_buffer->length((uint32_t) len);
216 by Brian Aker
Remove completely ZEROFILL
173
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
174
  return val_buffer;
175
}
176
481 by Brian Aker
Remove all of uchar.
177
int Field_double::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
178
{
179
  double a,b;
180
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
181
  if (getTable()->getShare()->db_low_byte_first)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
182
  {
183
    float8get(a,a_ptr);
184
    float8get(b,b_ptr);
185
  }
186
  else
187
#endif
188
  {
189
    doubleget(a, a_ptr);
190
    doubleget(b, b_ptr);
191
  }
192
  return (a < b) ? -1 : (a > b) ? 1 : 0;
193
}
194
195
196
/* The following should work for IEEE */
197
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
198
void Field_double::sort_string(unsigned char *to,uint32_t )
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
199
{
200
  double nr;
201
#ifdef WORDS_BIGENDIAN
1830.1.3 by Brian Aker
Fix for Solaris
202
  if (getTable()->getShare()->db_low_byte_first)
173.1.10 by Toru Maesaka
forgot to add field/double.cc and field/double.h in previous commit
203
  {
204
    float8get(nr,ptr);
205
  }
206
  else
207
#endif
208
    doubleget(nr,ptr);
209
  change_double_for_sort(nr, to);
210
}
211
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
212
} /* namespace drizzled */