~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/num.cc

Added autoconf tests for location of cstdint and cinttypes. Use those in C++ programs now, so that we don't have to define _STDC_LIMIT_MACROS, etc by hand. Stop, in fact, defining those by hand.

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