~ubuntu-branches/ubuntu/saucy/drizzle/saucy-proposed

« back to all changes in this revision

Viewing changes to drizzled/field/str.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

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