~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/str.cc

pandora-build v0.100 - Fixes several bugs found by cb1kenobi. Add several thoughts from folks at LCA.

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/str.h>
 
24
#include <drizzled/error.h>
 
25
#include <drizzled/table.h>
 
26
#include <drizzled/session.h>
 
27
#include "drizzled/internal/m_string.h"
 
28
 
 
29
extern char _dig_vec_upper[];
 
30
 
 
31
Field_str::Field_str(unsigned char *ptr_arg,
 
32
                     uint32_t len_arg,
 
33
                     unsigned char *null_ptr_arg,
 
34
                     unsigned char null_bit_arg,
 
35
                     const char *field_name_arg,
 
36
                     const CHARSET_INFO * const charset_arg)
 
37
  :Field(ptr_arg, len_arg,
 
38
         null_ptr_arg,
 
39
         null_bit_arg,
 
40
         Field::NONE,
 
41
         field_name_arg)
 
42
{
 
43
  field_charset= charset_arg;
 
44
  if (charset_arg->state & MY_CS_BINSORT)
 
45
    flags|= BINARY_FLAG;
 
46
  field_derivation= DERIVATION_IMPLICIT;
 
47
}
 
48
 
 
49
/*
 
50
  Check if we lost any important data and send a truncation error/warning
 
51
 
 
52
  SYNOPSIS
 
53
    Field_str::report_if_important_data()
 
54
    ptr                      - Truncated rest of string
 
55
    end                      - End of truncated string
 
56
 
 
57
  RETURN VALUES
 
58
    0   - None was truncated (or we don't count cut fields)
 
59
    2   - Some bytes was truncated
 
60
 
 
61
  NOTE
 
62
    Check if we lost any important data (anything in a binary string,
 
63
    or any non-space in others). If only trailing spaces was lost,
 
64
    send a truncation note, otherwise send a truncation error.
 
65
*/
 
66
 
 
67
int
 
68
Field_str::report_if_important_data(const char *field_ptr, const char *end)
 
69
{
 
70
  if ((field_ptr < end) && table->in_use->count_cuted_fields)
 
71
  {
 
72
    if (test_if_important_data(field_charset, field_ptr, end))
 
73
    {
 
74
      if (table->in_use->abort_on_warning)
 
75
        set_warning(DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
 
76
      else
 
77
        set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
 
78
    }
 
79
    else /* If we lost only spaces then produce a NOTE, not a WARNING */
 
80
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_NOTE, ER_WARN_DATA_TRUNCATED, 1);
 
81
    return 2;
 
82
  }
 
83
  return 0;
 
84
}
 
85
 
 
86
/**
 
87
  Decimal representation of Field_str.
 
88
 
 
89
  @param d         value for storing
 
90
 
 
91
  @note
 
92
    Field_str is the base class for fields like Field_enum,
 
93
    Field_date and some similar. Some dates use fraction and also
 
94
    string value should be converted to floating point value according
 
95
    our rules, so we use double to store value of decimal in string.
 
96
 
 
97
  @todo
 
98
    use decimal2string?
 
99
 
 
100
  @retval
 
101
    0     OK
 
102
  @retval
 
103
    !=0  error
 
104
*/
 
105
 
 
106
int Field_str::store_decimal(const my_decimal *d)
 
107
{
 
108
  char buff[DECIMAL_MAX_STR_LENGTH+1];
 
109
  String str(buff, sizeof(buff), &my_charset_bin);
 
110
  my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
 
111
  return store(str.ptr(), str.length(), str.charset());
 
112
}
 
113
 
 
114
my_decimal *Field_str::val_decimal(my_decimal *decimal_value)
 
115
{
 
116
  int64_t nr= val_int();
 
117
  int2my_decimal(E_DEC_FATAL_ERROR, nr, 0, decimal_value);
 
118
  return decimal_value;
 
119
}
 
120
 
 
121
/**
 
122
  Store double value in Field_varstring.
 
123
 
 
124
  Pretty prints double number into field_length characters buffer.
 
125
 
 
126
  @param nr            number
 
127
*/
 
128
 
 
129
int Field_str::store(double nr)
 
130
{
 
131
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
 
132
  uint32_t local_char_length= field_length / charset()->mbmaxlen;
 
133
  size_t length;
 
134
  bool error;
 
135
 
 
136
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
137
 
 
138
  length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
 
139
  if (error)
 
140
  {
 
141
    if (table->in_use->abort_on_warning)
 
142
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
 
143
    else
 
144
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
 
145
  }
 
146
  return store(buff, length, charset());
 
147
}
 
148
 
 
149
 
 
150
bool check_string_copy_error(Field_str *field,
 
151
                             const char *well_formed_error_pos,
 
152
                             const char *cannot_convert_error_pos,
 
153
                             const char *end,
 
154
                             const CHARSET_INFO * const cs)
 
155
{
 
156
  const char *pos, *end_orig;
 
157
  char tmp[64], *t;
 
158
 
 
159
  if (!(pos= well_formed_error_pos) &&
 
160
      !(pos= cannot_convert_error_pos))
 
161
    return false;
 
162
 
 
163
  end_orig= end;
 
164
  set_if_smaller(end, pos + 6);
 
165
 
 
166
  for (t= tmp; pos < end; pos++)
 
167
  {
 
168
    /*
 
169
      If the source string is ASCII compatible (mbminlen==1)
 
170
      and the source character is in ASCII printable range (0x20..0x7F),
 
171
      then display the character as is.
 
172
 
 
173
      Otherwise, if the source string is not ASCII compatible (e.g. UCS2),
 
174
      or the source character is not in the printable range,
 
175
      then print the character using HEX notation.
 
176
    */
 
177
    if (((unsigned char) *pos) >= 0x20 &&
 
178
        ((unsigned char) *pos) <= 0x7F &&
 
179
        cs->mbminlen == 1)
 
180
    {
 
181
      *t++= *pos;
 
182
    }
 
183
    else
 
184
    {
 
185
      *t++= '\\';
 
186
      *t++= 'x';
 
187
      *t++= _dig_vec_upper[((unsigned char) *pos) >> 4];
 
188
      *t++= _dig_vec_upper[((unsigned char) *pos) & 15];
 
189
    }
 
190
  }
 
191
  if (end_orig > end)
 
192
  {
 
193
    *t++= '.';
 
194
    *t++= '.';
 
195
    *t++= '.';
 
196
  }
 
197
  *t= '\0';
 
198
  push_warning_printf(field->table->in_use,
 
199
                      field->table->in_use->abort_on_warning ?
 
200
                      DRIZZLE_ERROR::WARN_LEVEL_ERROR :
 
201
                      DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
202
                      ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
 
203
                      ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
 
204
                      "string", tmp, field->field_name,
 
205
                      (uint32_t) field->table->in_use->row_count);
 
206
  return true;
 
207
}
 
208
 
 
209
uint32_t Field_str::max_data_length() const
 
210
{
 
211
  return field_length + (field_length > 255 ? 2 : 1);
 
212
}
 
213