Drizzled Public API Documentation

num.cc
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 #include <drizzled/util/test.h>
29 #include <drizzled/create_field.h>
30 
31 namespace drizzled {
32 
36 Field_num::Field_num(unsigned char *ptr_arg,
37  uint32_t len_arg,
38  unsigned char *null_ptr_arg,
39  unsigned char null_bit_arg,
40  utype unireg_check_arg,
41  const char *field_name_arg,
42  uint8_t dec_arg,
43  bool zero_arg,
44  bool unsigned_arg) :
45  Field(ptr_arg,
46  len_arg,
47  null_ptr_arg,
48  null_bit_arg,
49  unireg_check_arg,
50  field_name_arg),
51  dec(dec_arg),
52  decimal_precision(zero_arg),
53  unsigned_flag(unsigned_arg)
54  {
55 }
56 
57 
80 int Field_num::check_int(const charset_info_st * const cs, const char *str, int length,
81  const char *int_end, int error)
82 {
83  /* Test if we get an empty string or wrong integer */
84  if (str == int_end || error == EDOM)
85  {
86  char buff[128];
87  String tmp(buff, (uint32_t) sizeof(buff), system_charset_info);
88  tmp.copy(str, length, system_charset_info);
89  push_warning_printf(getTable()->in_use, DRIZZLE_ERROR::WARN_LEVEL_WARN,
90  ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
91  ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
92  "integer", tmp.c_ptr(), field_name,
93  (uint32_t) getTable()->in_use->row_count);
94  return 1;
95  }
96  /* Test if we have garbage at the end of the given string. */
97  if (test_if_important_data(cs, int_end, str + length))
98  {
99  set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
100  return 2;
101  }
102  return 0;
103 }
104 
105 /*
106  Conver a string to an integer then check bounds.
107 
108  SYNOPSIS
109  Field_num::get_int
110  cs Character set
111  from String to convert
112  len Length of the string
113  rnd OUT int64_t value
114  unsigned_max max unsigned value
115  signed_min min signed value
116  signed_max max signed value
117 
118  DESCRIPTION
119  The function calls strntoull10rnd() to get an integer value then
120  check bounds and errors returned. In case of any error a warning
121  is raised.
122 
123  RETURN
124  0 ok
125  1 error
126 */
127 
128 bool Field_num::get_int(const charset_info_st * const cs, const char *from, uint32_t len,
129  int64_t *rnd, uint64_t ,
130  int64_t signed_min, int64_t signed_max)
131 {
132  char *end;
133  int error;
134 
135  *rnd= (int64_t) cs->cset->strntoull10rnd(cs, from, len, false, &end, &error);
136  if (*rnd < signed_min)
137  {
138  *rnd= signed_min;
139  goto out_of_range;
140  }
141  else if (*rnd > signed_max)
142  {
143  *rnd= signed_max;
144  goto out_of_range;
145  }
146 
147  if (getTable()->in_use->count_cuted_fields &&
148  check_int(cs, from, len, end, error))
149  return 1;
150 
151  return 0;
152 
153 out_of_range:
154  set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
155  return 1;
156 }
157 
173 {
174  int err= 0;
175  int64_t i= convert_decimal2int64_t(val, false, &err);
176  return test(err | store(i, false));
177 }
178 
194 {
195  assert(result_type() == INT_RESULT);
196 
197  int64_t nr= val_int();
198  int2_class_decimal(E_DEC_FATAL_ERROR, nr, false, decimal_value);
199  return decimal_value;
200 }
201 
202 
203 void Field_num::make_field(SendField *field)
204 {
205  Field::make_field(field);
206  field->decimals= dec;
207 }
208 
214 {
215  if (!Field::eq_def(field))
216  return 0;
217  Field_num *from_num= (Field_num*) field;
218 
219  if (dec != from_num->dec)
220  return 0;
221  return 1;
222 }
223 
224 uint32_t Field_num::is_equal(CreateField *new_field_ptr)
225 {
226  return ((new_field_ptr->sql_type == real_type()) &&
227  ((new_field_ptr->flags & UNSIGNED_FLAG) == (uint32_t) (flags & UNSIGNED_FLAG)) &&
228  ((new_field_ptr->flags & AUTO_INCREMENT_FLAG) == (uint32_t) (flags & AUTO_INCREMENT_FLAG)) &&
229  (new_field_ptr->length <= max_display_length()));
230 }
231 
232 
233 } /* namespace drizzled */
bool set_warning(DRIZZLE_ERROR::enum_warning_level, drizzled::error_t code, int cuted_increment)
Definition: field.cc:1217
uint32_t row_count
Definition: session.h:447
TODO: Rename this file - func.h is stupid.
int check_int(const charset_info_st *const cs, const char *str, int length, const char *int_end, int error)
Definition: num.cc:80
int64_t convert_decimal2int64_t(const type::Decimal *val, bool unsigned_flag, int *err)
Definition: field.cc:1040
virtual bool eq_def(Field *field)
Definition: field.cc:1146
type::Decimal * val_decimal(type::Decimal *) const
Definition: num.cc:193
bool eq_def(Field *field)
Definition: num.cc:213
int store_decimal(const type::Decimal *)
Definition: num.cc:172
Session * in_use
Definition: table.h:123
bool test_if_important_data(const charset_info_st *const cs, const char *str, const char *strend)
Definition: field.cc:668
enum_field_types sql_type
Definition: create_field.h:40
Field_num(unsigned char *ptr_arg, uint32_t len_arg, unsigned char *null_ptr_arg, unsigned char null_bit_arg, utype unireg_check_arg, const char *field_name_arg, uint8_t dec_arg, bool zero_arg, bool unsigned_arg)
Definition: num.cc:36
const char * field_name
Definition: field.h:102