~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/str.cc

  • Committer: Monty Taylor
  • Date: 2008-08-04 19:37:18 UTC
  • mto: (261.2.2 codestyle)
  • mto: This revision was merged to the branch mainline in revision 262.
  • Revision ID: monty@inaugust.com-20080804193718-f0rz13uli4429ozb
Changed gettext_noop() to N_()

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 <drizzled/server_includes.h>
23
 
#include <drizzled/field/str.h>
24
 
#include <drizzled/error.h>
25
 
#include <drizzled/table.h>
26
 
#include <drizzled/session.h>
27
 
 
28
 
 
29
 
Field_str::Field_str(unsigned char *ptr_arg,uint32_t len_arg, unsigned char *null_ptr_arg,
30
 
                     unsigned char null_bit_arg, utype unireg_check_arg,
31
 
                     const char *field_name_arg, const CHARSET_INFO * const charset_arg)
32
 
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
33
 
         unireg_check_arg, field_name_arg)
34
 
{
35
 
  field_charset= charset_arg;
36
 
  if (charset_arg->state & MY_CS_BINSORT)
37
 
    flags|=BINARY_FLAG;
38
 
  field_derivation= DERIVATION_IMPLICIT;
39
 
}
40
 
 
41
 
/**
42
 
  Decimal representation of Field_str.
43
 
 
44
 
  @param d         value for storing
45
 
 
46
 
  @note
47
 
    Field_str is the base class for fields like Field_enum,
48
 
    Field_date and some similar. Some dates use fraction and also
49
 
    string value should be converted to floating point value according
50
 
    our rules, so we use double to store value of decimal in string.
51
 
 
52
 
  @todo
53
 
    use decimal2string?
54
 
 
55
 
  @retval
56
 
    0     OK
57
 
  @retval
58
 
    !=0  error
59
 
*/
60
 
 
61
 
int Field_str::store_decimal(const my_decimal *d)
62
 
{
63
 
  double val;
64
 
  /* TODO: use decimal2string? */
65
 
  int err= warn_if_overflow(my_decimal2double(E_DEC_FATAL_ERROR &
66
 
                                            ~E_DEC_OVERFLOW, d, &val));
67
 
  return err | store(val);
68
 
}
69
 
 
70
 
my_decimal *Field_str::val_decimal(my_decimal *decimal_value)
71
 
{
72
 
  int64_t nr= val_int();
73
 
  int2my_decimal(E_DEC_FATAL_ERROR, nr, 0, decimal_value);
74
 
  return decimal_value;
75
 
}
76
 
 
77
 
/**
78
 
  Store double value in Field_varstring.
79
 
 
80
 
  Pretty prints double number into field_length characters buffer.
81
 
 
82
 
  @param nr            number
83
 
*/
84
 
 
85
 
int Field_str::store(double nr)
86
 
{
87
 
  char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
88
 
  uint32_t local_char_length= field_length / charset()->mbmaxlen;
89
 
  size_t length;
90
 
  bool error;
91
 
 
92
 
  length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
93
 
  if (error)
94
 
  {
95
 
    if (table->in_use->abort_on_warning)
96
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
97
 
    else
98
 
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
99
 
  }
100
 
  return store(buff, length, charset());
101
 
}
102
 
 
103
 
/* If one of the fields is binary and the other one isn't return 1 else 0 */
104
 
 
105
 
bool Field_str::compare_str_field_flags(Create_field *new_field, uint32_t flag_arg)
106
 
{
107
 
  return (((new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
108
 
          !(flag_arg & (BINCMP_FLAG | BINARY_FLAG))) ||
109
 
         (!(new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
110
 
          (flag_arg & (BINCMP_FLAG | BINARY_FLAG))));
111
 
}
112
 
 
113
 
 
114
 
uint32_t Field_str::is_equal(Create_field *new_field)
115
 
{
116
 
  if (compare_str_field_flags(new_field, flags))
117
 
    return 0;
118
 
 
119
 
  return ((new_field->sql_type == real_type()) &&
120
 
          new_field->charset == field_charset &&
121
 
          new_field->length == max_display_length());
122
 
}
123
 
 
124
 
 
125
 
bool check_string_copy_error(Field_str *field,
126
 
                             const char *well_formed_error_pos,
127
 
                             const char *cannot_convert_error_pos,
128
 
                             const char *end,
129
 
                             const CHARSET_INFO * const cs)
130
 
{
131
 
  const char *pos, *end_orig;
132
 
  char tmp[64], *t;
133
 
 
134
 
  if (!(pos= well_formed_error_pos) &&
135
 
      !(pos= cannot_convert_error_pos))
136
 
    return false;
137
 
 
138
 
  end_orig= end;
139
 
  set_if_smaller(end, pos + 6);
140
 
 
141
 
  for (t= tmp; pos < end; pos++)
142
 
  {
143
 
    /*
144
 
      If the source string is ASCII compatible (mbminlen==1)
145
 
      and the source character is in ASCII printable range (0x20..0x7F),
146
 
      then display the character as is.
147
 
 
148
 
      Otherwise, if the source string is not ASCII compatible (e.g. UCS2),
149
 
      or the source character is not in the printable range,
150
 
      then print the character using HEX notation.
151
 
    */
152
 
    if (((unsigned char) *pos) >= 0x20 &&
153
 
        ((unsigned char) *pos) <= 0x7F &&
154
 
        cs->mbminlen == 1)
155
 
    {
156
 
      *t++= *pos;
157
 
    }
158
 
    else
159
 
    {
160
 
      *t++= '\\';
161
 
      *t++= 'x';
162
 
      *t++= _dig_vec_upper[((unsigned char) *pos) >> 4];
163
 
      *t++= _dig_vec_upper[((unsigned char) *pos) & 15];
164
 
    }
165
 
  }
166
 
  if (end_orig > end)
167
 
  {
168
 
    *t++= '.';
169
 
    *t++= '.';
170
 
    *t++= '.';
171
 
  }
172
 
  *t= '\0';
173
 
  push_warning_printf(field->table->in_use,
174
 
                      field->table->in_use->abort_on_warning ?
175
 
                      DRIZZLE_ERROR::WARN_LEVEL_ERROR :
176
 
                      DRIZZLE_ERROR::WARN_LEVEL_WARN,
177
 
                      ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
178
 
                      ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
179
 
                      "string", tmp, field->field_name,
180
 
                      (uint32_t) field->table->in_use->row_count);
181
 
  return true;
182
 
}
183