~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/double.cc

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:59 UTC
  • mfrom: (518 drizzle)
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063259-fwbqogq7lnezct0l
Merged with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
 
22
 
#include <config.h>
23
 
 
24
 
#include <float.h>
25
 
#include <math.h>
26
 
 
27
 
#include <algorithm>
28
 
 
 
22
#include <drizzled/server_includes.h>
29
23
#include <drizzled/field/double.h>
30
 
#include <drizzled/error.h>
31
 
#include <drizzled/table.h>
32
 
#include <drizzled/session.h>
33
 
#include <drizzled/current_session.h>
34
 
#include <drizzled/internal/m_string.h>
35
 
 
36
 
using namespace std;
37
 
 
38
 
namespace drizzled
39
 
{
 
24
#include <drizzled/drizzled_error_messages.h>
40
25
 
41
26
/****************************************************************************
42
27
  double precision floating point numbers
47
32
  int error;
48
33
  char *end;
49
34
  double nr= my_strntod(cs,(char*) from, len, &end, &error);
50
 
 
51
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
52
 
  if (error || (!len || (((uint32_t) (end-from) != len) && getTable()->in_use->count_cuted_fields)))
 
35
  if (error || (!len || (((uint) (end-from) != len) && table->in_use->count_cuted_fields)))
53
36
  {
54
37
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
55
38
                (error ? ER_WARN_DATA_OUT_OF_RANGE : ER_WARN_DATA_TRUNCATED), 1);
64
47
{
65
48
  int error= truncate(&nr, DBL_MAX);
66
49
 
67
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
68
 
 
69
50
#ifdef WORDS_BIGENDIAN
70
 
  if (getTable()->getShare()->db_low_byte_first)
 
51
  if (table->s->db_low_byte_first)
71
52
  {
72
53
    float8store(ptr,nr);
73
54
  }
84
65
                             (double) nr);
85
66
}
86
67
 
87
 
double Field_double::val_real(void) const
 
68
double Field_double::val_real(void)
88
69
{
89
70
  double j;
90
 
 
91
 
  ASSERT_COLUMN_MARKED_FOR_READ;
92
 
 
93
71
#ifdef WORDS_BIGENDIAN
94
 
  if (getTable()->getShare()->db_low_byte_first)
 
72
  if (table->s->db_low_byte_first)
95
73
  {
96
74
    float8get(j,ptr);
97
75
  }
101
79
  return j;
102
80
}
103
81
 
104
 
int64_t Field_double::val_int(void) const
 
82
int64_t Field_double::val_int(void)
105
83
{
106
84
  double j;
107
85
  int64_t res;
108
 
 
109
 
  ASSERT_COLUMN_MARKED_FOR_READ;
110
 
 
111
86
#ifdef WORDS_BIGENDIAN
112
 
  if (getTable()->getShare()->db_low_byte_first)
 
87
  if (table->s->db_low_byte_first)
113
88
  {
114
89
    float8get(j,ptr);
115
90
  }
133
108
  {
134
109
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
135
110
    String tmp(buf, sizeof(buf), &my_charset_utf8_general_ci), *str;
136
 
    str= val_str(&tmp, &tmp);
137
 
    Session *session= getTable() ? getTable()->in_use : current_session;
138
 
    push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
111
    str= val_str(&tmp, 0);
 
112
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
139
113
                        ER_TRUNCATED_WRONG_VALUE,
140
114
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
141
115
                        str->c_ptr());
144
118
}
145
119
 
146
120
 
147
 
String *Field_double::val_str(String *val_buffer, String *) const
 
121
String *Field_double::val_str(String *val_buffer,
 
122
                              String *val_ptr __attribute__((unused)))
148
123
{
149
124
  double nr;
150
 
 
151
 
  ASSERT_COLUMN_MARKED_FOR_READ;
152
 
 
153
125
#ifdef WORDS_BIGENDIAN
154
 
  if (getTable()->getShare()->db_low_byte_first)
 
126
  if (table->s->db_low_byte_first)
155
127
  {
156
128
    float8get(nr,ptr);
157
129
  }
159
131
#endif
160
132
    doubleget(nr,ptr);
161
133
 
162
 
  uint32_t to_length= max(field_length, (uint32_t)DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
 
134
  uint32_t to_length=cmax(field_length, (uint32_t)DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE);
163
135
  val_buffer->alloc(to_length);
164
136
  char *to=(char*) val_buffer->ptr();
165
137
  size_t len;
166
138
 
167
139
  if (dec >= NOT_FIXED_DEC)
168
 
    len= internal::my_gcvt(nr, internal::MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
 
140
    len= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, to_length - 1, to, NULL);
169
141
  else
170
 
    len= internal::my_fcvt(nr, dec, to, NULL);
 
142
    len= my_fcvt(nr, dec, to, NULL);
171
143
 
172
 
  val_buffer->length((uint32_t) len);
 
144
  val_buffer->length((uint) len);
173
145
 
174
146
  return val_buffer;
175
147
}
176
148
 
 
149
bool Field_double::send_binary(Protocol *protocol)
 
150
{
 
151
  return protocol->store((double) Field_double::val_real(), dec, (String*) 0);
 
152
}
 
153
 
 
154
 
177
155
int Field_double::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
178
156
{
179
157
  double a,b;
180
158
#ifdef WORDS_BIGENDIAN
181
 
  if (getTable()->getShare()->db_low_byte_first)
 
159
  if (table->s->db_low_byte_first)
182
160
  {
183
161
    float8get(a,a_ptr);
184
162
    float8get(b,b_ptr);
193
171
}
194
172
 
195
173
 
 
174
#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
 
175
 
196
176
/* The following should work for IEEE */
197
177
 
198
 
void Field_double::sort_string(unsigned char *to,uint32_t )
 
178
void Field_double::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
199
179
{
200
180
  double nr;
201
181
#ifdef WORDS_BIGENDIAN
202
 
  if (getTable()->getShare()->db_low_byte_first)
 
182
  if (table->s->db_low_byte_first)
203
183
  {
204
184
    float8get(nr,ptr);
205
185
  }
210
190
}
211
191
 
212
192
 
 
193
/**
 
194
   Save the field metadata for double fields.
 
195
 
 
196
   Saves the pack length in the first byte of the field metadata array
 
197
   at index of *metadata_ptr.
 
198
 
 
199
   @param   metadata_ptr   First byte of field metadata
 
200
 
 
201
   @returns number of bytes written to metadata_ptr
 
202
*/
 
203
int Field_double::do_save_field_metadata(unsigned char *metadata_ptr)
 
204
{
 
205
  *metadata_ptr= pack_length();
 
206
  return 1;
 
207
}
 
208
 
 
209
 
213
210
void Field_double::sql_type(String &res) const
214
211
{
215
212
  const CHARSET_INFO * const cs=res.charset();
224
221
  }
225
222
}
226
223
 
227
 
} /* namespace drizzled */