~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/str.cc

  • Committer: Monty Taylor
  • Date: 2008-12-18 07:24:54 UTC
  • mto: This revision was merged to the branch mainline in revision 714.
  • Revision ID: monty@bitters-20081218072454-8pnep622damjgqli
Fixed one more my_time thing.

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
24
#include <drizzled/error.h>
25
25
#include <drizzled/table.h>
26
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)
 
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)
48
34
{
49
35
  field_charset= charset_arg;
50
36
  if (charset_arg->state & MY_CS_BINSORT)
51
 
    flags|= BINARY_FLAG;
 
37
    flags|=BINARY_FLAG;
52
38
  field_derivation= DERIVATION_IMPLICIT;
53
39
}
54
40
 
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
41
/**
86
42
  Decimal representation of Field_str.
87
43
 
102
58
    !=0  error
103
59
*/
104
60
 
105
 
int Field_str::store_decimal(const type::Decimal *d)
 
61
int Field_str::store_decimal(const my_decimal *d)
106
62
{
107
 
  char buff[DECIMAL_MAX_STR_LENGTH+1];
108
 
  String str(buff, sizeof(buff), &my_charset_bin);
109
 
  class_decimal2string(d, 0, &str);
110
 
  return store(str.ptr(), str.length(), str.charset());
 
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);
111
68
}
112
69
 
113
 
type::Decimal *Field_str::val_decimal(type::Decimal *decimal_value)
 
70
my_decimal *Field_str::val_decimal(my_decimal *decimal_value)
114
71
{
115
72
  int64_t nr= val_int();
116
 
  int2_class_decimal(E_DEC_FATAL_ERROR, nr, 0, decimal_value);
 
73
  int2my_decimal(E_DEC_FATAL_ERROR, nr, 0, decimal_value);
117
74
  return decimal_value;
118
75
}
119
76
 
132
89
  size_t length;
133
90
  bool error;
134
91
 
135
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
136
 
 
137
 
  length= internal::my_gcvt(nr, internal::MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
 
92
  length= my_gcvt(nr, MY_GCVT_ARG_DOUBLE, local_char_length, buff, &error);
138
93
  if (error)
139
94
  {
140
 
    if (getTable()->getSession()->abortOnWarning())
141
 
    {
 
95
    if (table->in_use->abort_on_warning)
142
96
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DATA_TOO_LONG, 1);
143
 
    }
144
97
    else
145
 
    {
146
98
      set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
147
 
    }
148
99
  }
149
100
  return store(buff, length, charset());
150
101
}
151
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
 
152
124
 
153
125
bool check_string_copy_error(Field_str *field,
154
126
                             const char *well_formed_error_pos,
187
159
    {
188
160
      *t++= '\\';
189
161
      *t++= 'x';
190
 
      *t++= internal::_dig_vec_upper[((unsigned char) *pos) >> 4];
191
 
      *t++= internal::_dig_vec_upper[((unsigned char) *pos) & 15];
 
162
      *t++= _dig_vec_upper[((unsigned char) *pos) >> 4];
 
163
      *t++= _dig_vec_upper[((unsigned char) *pos) & 15];
192
164
    }
193
165
  }
194
166
  if (end_orig > end)
198
170
    *t++= '.';
199
171
  }
200
172
  *t= '\0';
201
 
  push_warning_printf(field->getTable()->in_use,
202
 
                      field->getTable()->in_use->abortOnWarning() ?
 
173
  push_warning_printf(field->table->in_use,
 
174
                      field->table->in_use->abort_on_warning ?
203
175
                      DRIZZLE_ERROR::WARN_LEVEL_ERROR :
204
176
                      DRIZZLE_ERROR::WARN_LEVEL_WARN,
205
177
                      ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
206
178
                      ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
207
179
                      "string", tmp, field->field_name,
208
 
                      (uint32_t) field->getTable()->in_use->row_count);
 
180
                      (uint32_t) field->table->in_use->row_count);
209
181
  return true;
210
182
}
211
183
 
212
 
uint32_t Field_str::max_data_length() const
213
 
{
214
 
  return field_length + (field_length > 255 ? 2 : 1);
215
 
}
216
 
 
217
 
} /* namespace drizzled */