~drizzle-trunk/drizzle/development

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