~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/num.cc

  • Committer: Brian Aker
  • Date: 2008-10-08 02:28:58 UTC
  • mfrom: (489.1.13 codestyle)
  • Revision ID: brian@tangent.org-20081008022858-ea8esagkxmn0dupc
Merge of Monty's work.

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
 
 
22
#include <drizzled/server_includes.h>
 
23
#include <drizzled/field/num.h>
 
24
#include <drizzled/drizzled_error_messages.h>
 
25
 
 
26
/**
 
27
  Numeric fields base class constructor.
 
28
*/
 
29
Field_num::Field_num(unsigned char *ptr_arg,uint32_t len_arg, unsigned char *null_ptr_arg,
 
30
                     unsigned char null_bit_arg, utype unireg_check_arg,
 
31
                     const char *field_name_arg,
 
32
                     uint8_t dec_arg, bool zero_arg, bool unsigned_arg)
 
33
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
34
         unireg_check_arg, field_name_arg),
 
35
  dec(dec_arg),decimal_precision(zero_arg),unsigned_flag(unsigned_arg)
 
36
{
 
37
  if (unsigned_flag)
 
38
    flags|=UNSIGNED_FLAG;
 
39
}
 
40
 
 
41
 
 
42
/**
 
43
  Test if given number is a int.
 
44
 
 
45
  @todo
 
46
    Make this multi-byte-character safe
 
47
 
 
48
  @param str            String to test
 
49
  @param length        Length of 'str'
 
50
  @param int_end        Pointer to char after last used digit
 
51
  @param cs             Character set
 
52
 
 
53
  @note
 
54
    This is called after one has called strntoull10rnd() function.
 
55
 
 
56
  @retval
 
57
    0   OK
 
58
  @retval
 
59
    1   error: empty string or wrong integer.
 
60
  @retval
 
61
    2   error: garbage at the end of string.
 
62
*/
 
63
 
 
64
int Field_num::check_int(const CHARSET_INFO * const cs, const char *str, int length,
 
65
                         const char *int_end, int error)
 
66
{
 
67
  /* Test if we get an empty string or wrong integer */
 
68
  if (str == int_end || error == MY_ERRNO_EDOM)
 
69
  {
 
70
    char buff[128];
 
71
    String tmp(buff, (uint32_t) sizeof(buff), system_charset_info);
 
72
    tmp.copy(str, length, system_charset_info);
 
73
    push_warning_printf(table->in_use, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
74
                        ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
 
75
                        ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
 
76
                        "integer", tmp.c_ptr(), field_name,
 
77
                        (uint32_t) table->in_use->row_count);
 
78
    return 1;
 
79
  }
 
80
  /* Test if we have garbage at the end of the given string. */
 
81
  if (test_if_important_data(cs, int_end, str + length))
 
82
  {
 
83
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
 
84
    return 2;
 
85
  }
 
86
  return 0;
 
87
}
 
88
 
 
89
/*
 
90
  Conver a string to an integer then check bounds.
 
91
 
 
92
  SYNOPSIS
 
93
    Field_num::get_int
 
94
    cs            Character set
 
95
    from          String to convert
 
96
    len           Length of the string
 
97
    rnd           OUT int64_t value
 
98
    unsigned_max  max unsigned value
 
99
    signed_min    min signed value
 
100
    signed_max    max signed value
 
101
 
 
102
  DESCRIPTION
 
103
    The function calls strntoull10rnd() to get an integer value then
 
104
    check bounds and errors returned. In case of any error a warning
 
105
    is raised.
 
106
 
 
107
  RETURN
 
108
    0   ok
 
109
    1   error
 
110
*/
 
111
 
 
112
bool Field_num::get_int(const CHARSET_INFO * const cs, const char *from, uint32_t len,
 
113
                        int64_t *rnd, uint64_t unsigned_max,
 
114
                        int64_t signed_min, int64_t signed_max)
 
115
{
 
116
  char *end;
 
117
  int error;
 
118
 
 
119
  *rnd= (int64_t) cs->cset->strntoull10rnd(cs, from, len,
 
120
                                            unsigned_flag, &end,
 
121
                                            &error);
 
122
  if (unsigned_flag)
 
123
  {
 
124
 
 
125
    if ((((uint64_t) *rnd > unsigned_max) && (*rnd= (int64_t) unsigned_max)) ||
 
126
        error == MY_ERRNO_ERANGE)
 
127
    {
 
128
      goto out_of_range;
 
129
    }
 
130
  }
 
131
  else
 
132
  {
 
133
    if (*rnd < signed_min)
 
134
    {
 
135
      *rnd= signed_min;
 
136
      goto out_of_range;
 
137
    }
 
138
    else if (*rnd > signed_max)
 
139
    {
 
140
      *rnd= signed_max;
 
141
      goto out_of_range;
 
142
    }
 
143
  }
 
144
  if (table->in_use->count_cuted_fields &&
 
145
      check_int(cs, from, len, end, error))
 
146
    return 1;
 
147
  return 0;
 
148
 
 
149
out_of_range:
 
150
  set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
 
151
  return 1;
 
152
}
 
153
 
 
154
void Field_num::add_unsigned(String &res) const
 
155
{
 
156
  if (unsigned_flag)
 
157
    res.append(STRING_WITH_LEN(" unsigned"));
 
158
}
 
159
 
 
160
/**
 
161
  Storing decimal in integer fields.
 
162
 
 
163
  @param val       value for storing
 
164
 
 
165
  @note
 
166
    This method is used by all integer fields, real/decimal redefine it
 
167
 
 
168
  @retval
 
169
    0     OK
 
170
  @retval
 
171
    !=0  error
 
172
*/
 
173
 
 
174
int Field_num::store_decimal(const my_decimal *val)
 
175
{
 
176
  int err= 0;
 
177
  int64_t i= convert_decimal2int64_t(val, unsigned_flag, &err);
 
178
  return test(err | store(i, unsigned_flag));
 
179
}
 
180
 
 
181
/**
 
182
  Return decimal value of integer field.
 
183
 
 
184
  @param decimal_value     buffer for storing decimal value
 
185
 
 
186
  @note
 
187
    This method is used by all integer fields, real/decimal redefine it.
 
188
    All int64_t values fit in our decimal buffer which cal store 8*9=72
 
189
    digits of integer number
 
190
 
 
191
  @return
 
192
    pointer to decimal buffer with value of field
 
193
*/
 
194
 
 
195
my_decimal* Field_num::val_decimal(my_decimal *decimal_value)
 
196
{
 
197
  assert(result_type() == INT_RESULT);
 
198
  int64_t nr= val_int();
 
199
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
 
200
  return decimal_value;
 
201
}
 
202
 
 
203
 
 
204
void Field_num::make_field(Send_field *field)
 
205
{
 
206
  Field::make_field(field);
 
207
  field->decimals= dec;
 
208
}
 
209
 
 
210
/**
 
211
  @return
 
212
  returns 1 if the fields are equally defined
 
213
*/
 
214
bool Field_num::eq_def(Field *field)
 
215
{
 
216
  if (!Field::eq_def(field))
 
217
    return 0;
 
218
  Field_num *from_num= (Field_num*) field;
 
219
 
 
220
  if (unsigned_flag != from_num->unsigned_flag ||
 
221
      dec != from_num->dec)
 
222
    return 0;
 
223
  return 1;
 
224
}
 
225
 
 
226
uint32_t Field_num::is_equal(Create_field *new_field)
 
227
{
 
228
  return ((new_field->sql_type == real_type()) &&
 
229
          ((new_field->flags & UNSIGNED_FLAG) == (uint32_t) (flags &
 
230
                                                         UNSIGNED_FLAG)) &&
 
231
          ((new_field->flags & AUTO_INCREMENT_FLAG) ==
 
232
           (uint32_t) (flags & AUTO_INCREMENT_FLAG)) &&
 
233
          (new_field->length <= max_display_length()));
 
234
}
 
235
 
 
236