~drizzle-trunk/drizzle/development

483.1.5 by Lee
clean up code for Field_str
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
22
#include <config.h>
483.1.5 by Lee
clean up code for Field_str
23
#include <drizzled/field/str.h>
550 by Monty Taylor
Moved error.h into just the files that need it.
24
#include <drizzled/error.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
25
#include <drizzled/table.h>
26
#include <drizzled/session.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
27
#include <drizzled/internal/m_string.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
28
2318.6.13 by Olaf van der Spek
Refactor
29
namespace drizzled {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
30
31
namespace internal
32
{
2318.6.13 by Olaf van der Spek
Refactor
33
  extern char _dig_vec_upper[];
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
34
}
483.1.5 by Lee
clean up code for Field_str
35
1119.9.12 by Jay Pipes
First phase removal of MTYP_TYPENR() macro. This removes the unireg_check argument for all Field types where it is irrelevant (everything but numeric types and timestamp.
36
Field_str::Field_str(unsigned char *ptr_arg,
37
                     uint32_t len_arg,
779.3.10 by Monty Taylor
Turned on -Wshadow.
38
                     unsigned char *null_ptr_arg,
1119.9.12 by Jay Pipes
First phase removal of MTYP_TYPENR() macro. This removes the unireg_check argument for all Field types where it is irrelevant (everything but numeric types and timestamp.
39
                     unsigned char null_bit_arg,
779.3.10 by Monty Taylor
Turned on -Wshadow.
40
                     const char *field_name_arg,
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
41
                     const charset_info_st * const charset_arg)
1119.9.12 by Jay Pipes
First phase removal of MTYP_TYPENR() macro. This removes the unireg_check argument for all Field types where it is irrelevant (everything but numeric types and timestamp.
42
  :Field(ptr_arg, len_arg,
43
         null_ptr_arg,
44
         null_bit_arg,
45
         Field::NONE,
46
         field_name_arg)
483.1.5 by Lee
clean up code for Field_str
47
{
48
  field_charset= charset_arg;
49
  if (charset_arg->state & MY_CS_BINSORT)
1119.9.12 by Jay Pipes
First phase removal of MTYP_TYPENR() macro. This removes the unireg_check argument for all Field types where it is irrelevant (everything but numeric types and timestamp.
50
    flags|= BINARY_FLAG;
483.1.5 by Lee
clean up code for Field_str
51
  field_derivation= DERIVATION_IMPLICIT;
52
}
53
1034.1.3 by Brian Aker
Collapse field classes (this does change enum/time decimal conversion
54
/*
55
  Check if we lost any important data and send a truncation error/warning
56
57
  SYNOPSIS
58
    Field_str::report_if_important_data()
59
    ptr                      - Truncated rest of string
60
    end                      - End of truncated string
61
62
  RETURN VALUES
63
    0   - None was truncated (or we don't count cut fields)
64
    2   - Some bytes was truncated
65
66
  NOTE
67
    Check if we lost any important data (anything in a binary string,
68
    or any non-space in others). If only trailing spaces was lost,
69
    send a truncation note, otherwise send a truncation error.
70
*/
71
2318.6.13 by Olaf van der Spek
Refactor
72
int Field_str::report_if_important_data(const char *field_ptr, const char *end)
1034.1.3 by Brian Aker
Collapse field classes (this does change enum/time decimal conversion
73
{
2318.6.13 by Olaf van der Spek
Refactor
74
  if (field_ptr < end && getTable()->in_use->count_cuted_fields)
1034.1.3 by Brian Aker
Collapse field classes (this does change enum/time decimal conversion
75
  {
1637 by Brian Aker
Merge in changes to call error on bad data input.
76
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
1034.1.3 by Brian Aker
Collapse field classes (this does change enum/time decimal conversion
77
    return 2;
78
  }
79
  return 0;
80
}
81
483.1.5 by Lee
clean up code for Field_str
82
/**
83
  Decimal representation of Field_str.
84
85
  @param d         value for storing
86
87
  @note
88
    Field_str is the base class for fields like Field_enum,
89
    Field_date and some similar. Some dates use fraction and also
90
    string value should be converted to floating point value according
91
    our rules, so we use double to store value of decimal in string.
92
93
  @todo
94
    use decimal2string?
95
96
  @retval
97
    0     OK
98
  @retval
99
    !=0  error
100
*/
101
2030.1.4 by Brian Aker
Change my_decimal to Decimal
102
int Field_str::store_decimal(const type::Decimal *d)
483.1.5 by Lee
clean up code for Field_str
103
{
1034.1.3 by Brian Aker
Collapse field classes (this does change enum/time decimal conversion
104
  char buff[DECIMAL_MAX_STR_LENGTH+1];
105
  String str(buff, sizeof(buff), &my_charset_bin);
2137.1.8 by Brian Aker
Remove error type from str convert since we always want an error.
106
  class_decimal2string(d, 0, &str);
1034.1.3 by Brian Aker
Collapse field classes (this does change enum/time decimal conversion
107
  return store(str.ptr(), str.length(), str.charset());
483.1.5 by Lee
clean up code for Field_str
108
}
109
2181.2.1 by Brian Aker
Protect all of the val_* methods from modification.
110
type::Decimal *Field_str::val_decimal(type::Decimal *decimal_value) const
483.1.5 by Lee
clean up code for Field_str
111
{
2318.6.13 by Olaf van der Spek
Refactor
112
  int2_class_decimal(E_DEC_FATAL_ERROR, val_int(), 0, decimal_value);
483.1.5 by Lee
clean up code for Field_str
113
  return decimal_value;
114
}
115
116
/**
117
  Store double value in Field_varstring.
118
119
  Pretty prints double number into field_length characters buffer.
120
121
  @param nr            number
122
*/
123
124
int Field_str::store(double nr)
125
{
126
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
127
  uint32_t local_char_length= field_length / charset()->mbmaxlen;
128
  bool error;
129
1089.1.3 by Brian Aker
Fix protobuf to release memory. Add in assert() for wrong column usage. Fix
130
  ASSERT_COLUMN_MARKED_FOR_WRITE;
131
2318.6.13 by Olaf van der Spek
Refactor
132
  size_t length= internal::my_gcvt(nr, internal::MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
483.1.5 by Lee
clean up code for Field_str
133
  if (error)
134
  {
2114.5.2 by Brian Aker
Just remove second ecapsulated method call.
135
    if (getTable()->getSession()->abortOnWarning())
2046.2.3 by Brian Aker
Add basic tests for microtime.
136
    {
483.1.5 by Lee
clean up code for Field_str
137
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
2046.2.3 by Brian Aker
Add basic tests for microtime.
138
    }
483.1.5 by Lee
clean up code for Field_str
139
    else
2046.2.3 by Brian Aker
Add basic tests for microtime.
140
    {
483.1.5 by Lee
clean up code for Field_str
141
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
2046.2.3 by Brian Aker
Add basic tests for microtime.
142
    }
483.1.5 by Lee
clean up code for Field_str
143
  }
144
  return store(buff, length, charset());
145
}
146
584.5.1 by Monty Taylor
Removed field includes from field.h.
147
148
bool check_string_copy_error(Field_str *field,
149
                             const char *well_formed_error_pos,
150
                             const char *cannot_convert_error_pos,
151
                             const char *end,
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
152
                             const charset_info_st * const cs)
584.5.1 by Monty Taylor
Removed field includes from field.h.
153
{
2318.6.13 by Olaf van der Spek
Refactor
154
  const char* pos= well_formed_error_pos;
155
  if (not pos && not (pos= cannot_convert_error_pos))
584.5.1 by Monty Taylor
Removed field includes from field.h.
156
    return false;
157
2318.6.13 by Olaf van der Spek
Refactor
158
  const char* end_orig= end;
584.5.1 by Monty Taylor
Removed field includes from field.h.
159
  set_if_smaller(end, pos + 6);
160
2318.6.13 by Olaf van der Spek
Refactor
161
  char tmp[64];
162
  char* t= tmp;
163
  for (; pos < end; pos++)
584.5.1 by Monty Taylor
Removed field includes from field.h.
164
  {
165
    /*
166
      If the source string is ASCII compatible (mbminlen==1)
167
      and the source character is in ASCII printable range (0x20..0x7F),
168
      then display the character as is.
169
170
      Otherwise, if the source string is not ASCII compatible (e.g. UCS2),
171
      or the source character is not in the printable range,
172
      then print the character using HEX notation.
173
    */
2318.6.13 by Olaf van der Spek
Refactor
174
    if (((unsigned char) *pos) >= 0x20 && ((unsigned char) *pos) <= 0x7F && cs->mbminlen == 1)
584.5.1 by Monty Taylor
Removed field includes from field.h.
175
    {
176
      *t++= *pos;
177
    }
178
    else
179
    {
180
      *t++= '\\';
181
      *t++= 'x';
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
182
      *t++= internal::_dig_vec_upper[((unsigned char) *pos) >> 4];
183
      *t++= internal::_dig_vec_upper[((unsigned char) *pos) & 15];
584.5.1 by Monty Taylor
Removed field includes from field.h.
184
    }
185
  }
186
  if (end_orig > end)
187
  {
188
    *t++= '.';
189
    *t++= '.';
190
    *t++= '.';
191
  }
192
  *t= '\0';
1660.1.3 by Brian Aker
Encapsulate Table in field
193
  push_warning_printf(field->getTable()->in_use,
2318.6.13 by Olaf van der Spek
Refactor
194
                      field->getTable()->in_use->abortOnWarning() ? DRIZZLE_ERROR::WARN_LEVEL_ERROR : DRIZZLE_ERROR::WARN_LEVEL_WARN,
584.5.1 by Monty Taylor
Removed field includes from field.h.
195
                      ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
196
                      ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
197
                      "string", tmp, field->field_name,
1660.1.3 by Brian Aker
Encapsulate Table in field
198
                      (uint32_t) field->getTable()->in_use->row_count);
584.5.1 by Monty Taylor
Removed field includes from field.h.
199
  return true;
200
}
201
1034.1.3 by Brian Aker
Collapse field classes (this does change enum/time decimal conversion
202
uint32_t Field_str::max_data_length() const
203
{
204
  return field_length + (field_length > 255 ? 2 : 1);
205
}
206
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
207
} /* namespace drizzled */