~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/fstring.cc

  • Committer: Brian Aker
  • Date: 2008-09-04 19:31:00 UTC
  • Revision ID: brian@tangent.org-20080904193100-l849hgghfy4urj43
Changing default character set from this point on.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
4
 *  Copyright (C) 2008 MySQL
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
 
21
#ifdef USE_PRAGMA_IMPLEMENTATION
 
22
#pragma implementation                          // gcc: Class implementation
 
23
#endif
21
24
 
22
25
#include <drizzled/server_includes.h>
23
26
#include <drizzled/field/fstring.h>
24
 
#include <drizzled/error.h>
 
27
#include <drizzled/drizzled_error_messages.h>
25
28
 
26
29
#define LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE 128
27
30
#define DECIMAL_TO_STRING_CONVERSION_BUFFER_SIZE 128
32
35
****************************************************************************/
33
36
 
34
37
/* Copy a string and fill with space */
35
 
int Field_string::store(const char *from,uint32_t length, const CHARSET_INFO * const cs)
 
38
int Field_string::store(const char *from,uint length, const CHARSET_INFO * const cs)
36
39
{
37
 
  uint32_t copy_length;
 
40
  uint copy_length;
38
41
  const char *well_formed_error_pos;
39
42
  const char *cannot_convert_error_pos;
40
43
  const char *from_end_pos;
41
44
 
42
 
  /* See the comment for Field_long::store(int64_t) */
43
 
  assert(table->in_use == current_session);
 
45
  /* See the comment for Field_long::store(long long) */
 
46
  assert(table->in_use == current_thd);
44
47
 
45
48
  copy_length= well_formed_copy_nchars(field_charset,
46
49
                                       (char*) ptr, field_length,
91
94
    char buf[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
92
95
    String tmp(buf, sizeof(buf), cs);
93
96
    tmp.copy((char*) ptr, field_length, cs);
94
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
97
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
95
98
                        ER_TRUNCATED_WRONG_VALUE, 
96
99
                        ER(ER_TRUNCATED_WRONG_VALUE),
97
100
                        "DOUBLE", tmp.c_ptr());
116
119
    char buf[LONGLONG_TO_STRING_CONVERSION_BUFFER_SIZE];
117
120
    String tmp(buf, sizeof(buf), cs);
118
121
    tmp.copy((char*) ptr, field_length, cs);
119
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
122
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
120
123
                        ER_TRUNCATED_WRONG_VALUE, 
121
124
                        ER(ER_TRUNCATED_WRONG_VALUE),
122
125
                        "INTEGER", tmp.c_ptr());
128
131
String *Field_string::val_str(String *val_buffer __attribute__((unused)),
129
132
                              String *val_ptr)
130
133
{
131
 
  /* See the comment for Field_long::store(int64_t) */
132
 
  assert(table->in_use == current_session);
133
 
  uint32_t length;
 
134
  /* See the comment for Field_long::store(long long) */
 
135
  assert(table->in_use == current_thd);
 
136
  uint length;
134
137
 
135
138
  length= field_charset->cset->lengthsp(field_charset, (const char*) ptr, field_length);
136
139
  val_ptr->set((const char*) ptr, length, field_charset);
149
152
    const CHARSET_INFO * const cs= charset();
150
153
    String tmp(buf, sizeof(buf), cs);
151
154
    tmp.copy((char*) ptr, field_length, cs);
152
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
155
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
153
156
                        ER_TRUNCATED_WRONG_VALUE, 
154
157
                        ER(ER_TRUNCATED_WRONG_VALUE),
155
158
                        "DECIMAL", tmp.c_ptr());
159
162
}
160
163
 
161
164
 
162
 
int Field_string::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
165
int Field_string::cmp(const uchar *a_ptr, const uchar *b_ptr)
163
166
{
164
 
  uint32_t a_len, b_len;
 
167
  uint a_len, b_len;
165
168
 
166
169
  if (field_charset->mbmaxlen != 1)
167
170
  {
168
 
    uint32_t char_len= field_length/field_charset->mbmaxlen;
 
171
    uint char_len= field_length/field_charset->mbmaxlen;
169
172
    a_len= my_charpos(field_charset, a_ptr, a_ptr + field_length, char_len);
170
173
    b_len= my_charpos(field_charset, b_ptr, b_ptr + field_length, char_len);
171
174
  }
182
185
}
183
186
 
184
187
 
185
 
void Field_string::sort_string(unsigned char *to,uint32_t length)
 
188
void Field_string::sort_string(uchar *to,uint length)
186
189
{
187
 
  uint32_t tmp= my_strnxfrm(field_charset,
 
190
  uint tmp= my_strnxfrm(field_charset,
188
191
                                 to, length,
189
192
                                 ptr, field_length);
190
193
  assert(tmp == length);
193
196
 
194
197
void Field_string::sql_type(String &res) const
195
198
{
196
 
  Session *session= table->in_use;
 
199
  THD *thd= table->in_use;
197
200
  const CHARSET_INFO * const cs= res.charset();
198
201
  uint32_t length;
199
202
 
200
203
  length= cs->cset->snprintf(cs,(char*) res.ptr(),
201
204
                             res.alloced_length(), "%s(%d)",
202
205
                             ((type() == DRIZZLE_TYPE_VARCHAR &&
203
 
                               !session->variables.new_mode) ?
 
206
                               !thd->variables.new_mode) ?
204
207
                              (has_charset() ? "varchar" : "varbinary") :
205
208
                              (has_charset() ? "char" : "binary")),
206
209
                             (int) field_length / charset()->mbmaxlen);
208
211
}
209
212
 
210
213
 
211
 
unsigned char *Field_string::pack(unsigned char *to, const unsigned char *from,
212
 
                          uint32_t max_length,
 
214
uchar *Field_string::pack(uchar *to, const uchar *from,
 
215
                          uint max_length,
213
216
                          bool low_byte_first __attribute__((unused)))
214
217
{
215
 
  uint32_t length=      cmin(field_length,max_length);
216
 
  uint32_t local_char_length= max_length/field_charset->mbmaxlen;
 
218
  uint length=      min(field_length,max_length);
 
219
  uint local_char_length= max_length/field_charset->mbmaxlen;
217
220
  if (length > local_char_length)
218
221
    local_char_length= my_charpos(field_charset, from, from+length,
219
222
                                  local_char_length);
222
225
    length--;
223
226
 
224
227
  // Length always stored little-endian
225
 
  *to++= (unsigned char) length;
 
228
  *to++= (uchar) length;
226
229
  if (field_length > 255)
227
 
    *to++= (unsigned char) (length >> 8);
 
230
    *to++= (uchar) (length >> 8);
228
231
 
229
232
  // Store the actual bytes of the string
230
233
  memcpy(to, from, length);
248
251
 
249
252
   @return  New pointer into memory based on from + length of the data
250
253
*/
251
 
const unsigned char *
252
 
Field_string::unpack(unsigned char *to,
253
 
                     const unsigned char *from,
254
 
                     uint32_t param_data,
 
254
const uchar *
 
255
Field_string::unpack(uchar *to,
 
256
                     const uchar *from,
 
257
                     uint param_data,
255
258
                     bool low_byte_first __attribute__((unused)))
256
259
{
257
 
  uint32_t from_length=
258
 
    param_data ? cmin(param_data & 0x00ff, field_length) : field_length;
259
 
  uint32_t length;
 
260
  uint from_length=
 
261
    param_data ? min(param_data & 0x00ff, field_length) : field_length;
 
262
  uint length;
260
263
 
261
264
  if (from_length > 255)
262
265
  {
284
287
 
285
288
   @returns number of bytes written to metadata_ptr
286
289
*/
287
 
int Field_string::do_save_field_metadata(unsigned char *metadata_ptr)
 
290
int Field_string::do_save_field_metadata(uchar *metadata_ptr)
288
291
{
289
292
  *metadata_ptr= real_type();
290
293
  *(metadata_ptr + 1)= field_length;
308
311
    > 0   a > b
309
312
*/
310
313
 
311
 
int Field_string::pack_cmp(const unsigned char *a, const unsigned char *b, uint32_t length,
 
314
int Field_string::pack_cmp(const uchar *a, const uchar *b, uint length,
312
315
                           bool insert_or_update)
313
316
{
314
 
  uint32_t a_length, b_length;
 
317
  uint a_length, b_length;
315
318
  if (length > 255)
316
319
  {
317
320
    a_length= uint2korr(a);
346
349
    > 0   row > key
347
350
*/
348
351
 
349
 
int Field_string::pack_cmp(const unsigned char *key, uint32_t length,
 
352
int Field_string::pack_cmp(const uchar *key, uint length,
350
353
                           bool insert_or_update)
351
354
{
352
 
  uint32_t row_length, local_key_length;
353
 
  unsigned char *end;
 
355
  uint row_length, local_key_length;
 
356
  uchar *end;
354
357
  if (length > 255)
355
358
  {
356
359
    local_key_length= uint2korr(key);
372
375
}
373
376
 
374
377
 
375
 
uint32_t Field_string::packed_col_length(const unsigned char *data_ptr, uint32_t length)
 
378
uint Field_string::packed_col_length(const uchar *data_ptr, uint length)
376
379
{
377
380
  if (length > 255)
378
381
    return uint2korr(data_ptr)+2;
380
383
}
381
384
 
382
385
 
383
 
uint32_t Field_string::max_packed_col_length(uint32_t max_length)
 
386
uint Field_string::max_packed_col_length(uint max_length)
384
387
{
385
388
  return (max_length > 255 ? 2 : 1)+max_length;
386
389
}
387
390
 
388
391
 
389
 
uint32_t Field_string::get_key_image(unsigned char *buff,
390
 
                                 uint32_t length,
 
392
uint Field_string::get_key_image(uchar *buff,
 
393
                                 uint length,
391
394
                                 imagetype type_arg __attribute__((unused)))
392
395
{
393
 
  uint32_t bytes = my_charpos(field_charset, (char*) ptr,
 
396
  uint bytes = my_charpos(field_charset, (char*) ptr,
394
397
                          (char*) ptr + field_length,
395
398
                          length / field_charset->mbmaxlen);
396
399
  memcpy(buff, ptr, bytes);