~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/str.cc

code clean move Item_func_num1 and Item_func_connection_id to functions directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
 
22
 
#include "config.h"
 
22
#include <drizzled/server_includes.h>
23
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)
 
24
 
 
25
Field_str::Field_str(unsigned char *ptr_arg,uint32_t len_arg, unsigned char *null_ptr_arg,
 
26
                     unsigned char null_bit_arg, utype unireg_check_arg,
 
27
                     const char *field_name_arg, const CHARSET_INFO * const charset_arg)
 
28
  :Field(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
29
         unireg_check_arg, field_name_arg)
48
30
{
49
31
  field_charset= charset_arg;
50
32
  if (charset_arg->state & MY_CS_BINSORT)
51
 
    flags|= BINARY_FLAG;
 
33
    flags|=BINARY_FLAG;
52
34
  field_derivation= DERIVATION_IMPLICIT;
53
35
}
54
36
 
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) && getTable()->in_use->count_cuted_fields)
77
 
  {
78
 
    set_warning(DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
79
 
 
80
 
    return 2;
81
 
  }
82
 
  return 0;
83
 
}
84
 
 
85
37
/**
86
38
  Decimal representation of Field_str.
87
39
 
104
56
 
105
57
int Field_str::store_decimal(const my_decimal *d)
106
58
{
107
 
  char buff[DECIMAL_MAX_STR_LENGTH+1];
108
 
  String str(buff, sizeof(buff), &my_charset_bin);
109
 
  my_decimal2string(E_DEC_FATAL_ERROR, d, 0, 0, 0, &str);
110
 
  return store(str.ptr(), str.length(), str.charset());
 
59
  double val;
 
60
  /* TODO: use decimal2string? */
 
61
  int err= warn_if_overflow(my_decimal2double(E_DEC_FATAL_ERROR &
 
62
                                            ~E_DEC_OVERFLOW, d, &val));
 
63
  return err | store(val);
111
64
}
112
65
 
113
66
my_decimal *Field_str::val_decimal(my_decimal *decimal_value)
132
85
  size_t length;
133
86
  bool error;
134
87
 
135
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
136
 
 
137
 
  length= internal::my_gcvt(nr, internal::MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
 
88
  length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
138
89
  if (error)
139
90
  {
140
 
    if (getTable()->in_use->abort_on_warning)
 
91
    if (table->in_use->abort_on_warning)
141
92
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
142
93
    else
143
94
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
145
96
  return store(buff, length, charset());
146
97
}
147
98
 
148
 
 
149
 
bool check_string_copy_error(Field_str *field,
150
 
                             const char *well_formed_error_pos,
151
 
                             const char *cannot_convert_error_pos,
152
 
                             const char *end,
153
 
                             const CHARSET_INFO * const cs)
154
 
{
155
 
  const char *pos, *end_orig;
156
 
  char tmp[64], *t;
157
 
 
158
 
  if (!(pos= well_formed_error_pos) &&
159
 
      !(pos= cannot_convert_error_pos))
160
 
    return false;
161
 
 
162
 
  end_orig= end;
163
 
  set_if_smaller(end, pos + 6);
164
 
 
165
 
  for (t= tmp; pos < end; pos++)
166
 
  {
167
 
    /*
168
 
      If the source string is ASCII compatible (mbminlen==1)
169
 
      and the source character is in ASCII printable range (0x20..0x7F),
170
 
      then display the character as is.
171
 
 
172
 
      Otherwise, if the source string is not ASCII compatible (e.g. UCS2),
173
 
      or the source character is not in the printable range,
174
 
      then print the character using HEX notation.
175
 
    */
176
 
    if (((unsigned char) *pos) >= 0x20 &&
177
 
        ((unsigned char) *pos) <= 0x7F &&
178
 
        cs->mbminlen == 1)
179
 
    {
180
 
      *t++= *pos;
181
 
    }
182
 
    else
183
 
    {
184
 
      *t++= '\\';
185
 
      *t++= 'x';
186
 
      *t++= internal::_dig_vec_upper[((unsigned char) *pos) >> 4];
187
 
      *t++= internal::_dig_vec_upper[((unsigned char) *pos) & 15];
188
 
    }
189
 
  }
190
 
  if (end_orig > end)
191
 
  {
192
 
    *t++= '.';
193
 
    *t++= '.';
194
 
    *t++= '.';
195
 
  }
196
 
  *t= '\0';
197
 
  push_warning_printf(field->getTable()->in_use,
198
 
                      field->getTable()->in_use->abort_on_warning ?
199
 
                      DRIZZLE_ERROR::WARN_LEVEL_ERROR :
200
 
                      DRIZZLE_ERROR::WARN_LEVEL_WARN,
201
 
                      ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
202
 
                      ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
203
 
                      "string", tmp, field->field_name,
204
 
                      (uint32_t) field->getTable()->in_use->row_count);
205
 
  return true;
206
 
}
207
 
 
208
 
uint32_t Field_str::max_data_length() const
209
 
{
210
 
  return field_length + (field_length > 255 ? 2 : 1);
211
 
}
212
 
 
213
 
} /* namespace drizzled */
 
99
/* If one of the fields is binary and the other one isn't return 1 else 0 */
 
100
 
 
101
bool Field_str::compare_str_field_flags(Create_field *new_field, uint32_t flag_arg)
 
102
{
 
103
  return (((new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
 
104
          !(flag_arg & (BINCMP_FLAG | BINARY_FLAG))) ||
 
105
         (!(new_field->flags & (BINCMP_FLAG | BINARY_FLAG)) &&
 
106
          (flag_arg & (BINCMP_FLAG | BINARY_FLAG))));
 
107
}
 
108
 
 
109
 
 
110
uint32_t Field_str::is_equal(Create_field *new_field)
 
111
{
 
112
  if (compare_str_field_flags(new_field, flags))
 
113
    return 0;
 
114
 
 
115
  return ((new_field->sql_type == real_type()) &&
 
116
          new_field->charset == field_charset &&
 
117
          new_field->length == max_display_length());
 
118
}