~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/num.cc

  • Committer: Brian Aker
  • Date: 2010-12-18 18:24:57 UTC
  • mfrom: (1999.6.3 trunk)
  • Revision ID: brian@tangent.org-20101218182457-yi1wd0so2hml1k1w
Merge in Lee's copyright header fix

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