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

« back to all changes in this revision

Viewing changes to .pc/debian-changes-2010.12.06-0ubuntu4/drizzled/field/varstring.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2011-01-04 09:31:58 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20110104093158-smhgvkfdi2y9au3i
Tags: 2011.01.07-0ubuntu1
New upstream release.

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/varstring.h>
24
 
#include <drizzled/table.h>
25
 
#include <drizzled/session.h>
26
 
#include "plugin/myisam/myisam.h"
27
 
 
28
 
#include <string>
29
 
 
30
 
using namespace std;
31
 
 
32
 
namespace drizzled
33
 
{
34
 
 
35
 
/****************************************************************************
36
 
  VARCHAR type
37
 
  Data in field->ptr is stored as:
38
 
    1 or 2 bytes length-prefix-header  (from Field_varstring::length_bytes)
39
 
    data
40
 
 
41
 
  NOTE:
42
 
  When VARCHAR is stored in a key (for handler::index_read() etc) it's always
43
 
  stored with a 2 byte prefix. (Just like blob keys).
44
 
 
45
 
  Normally length_bytes is calculated as (field_length < 256 : 1 ? 2)
46
 
  The exception is if there is a prefix key field that is part of a long
47
 
  VARCHAR, in which case field_length for this may be 1 but the length_bytes
48
 
  is 2.
49
 
****************************************************************************/
50
 
 
51
 
const uint32_t Field_varstring::MAX_SIZE= UINT16_MAX;
52
 
 
53
 
Field_varstring::Field_varstring(unsigned char *ptr_arg,
54
 
                                 uint32_t len_arg,
55
 
                                 uint32_t length_bytes_arg,
56
 
                                 unsigned char *null_ptr_arg,
57
 
                                 unsigned char null_bit_arg,
58
 
                                 const char *field_name_arg,
59
 
                                 const CHARSET_INFO * const cs) :
60
 
  Field_str(ptr_arg,
61
 
            len_arg,
62
 
            null_ptr_arg,
63
 
            null_bit_arg,
64
 
            field_name_arg, cs),
65
 
length_bytes(length_bytes_arg)
66
 
{
67
 
}
68
 
 
69
 
Field_varstring::Field_varstring(uint32_t len_arg,
70
 
                                 bool maybe_null_arg,
71
 
                                 const char *field_name_arg,
72
 
                                 const CHARSET_INFO * const cs) :
73
 
  Field_str((unsigned char*) 0,
74
 
            len_arg,
75
 
            maybe_null_arg ? (unsigned char*) "": 0,
76
 
            0,
77
 
            field_name_arg,
78
 
            cs),
79
 
  length_bytes(len_arg < 256 ? 1 :2)
80
 
{
81
 
}
82
 
 
83
 
int Field_varstring::store(const char *from,uint32_t length, const CHARSET_INFO * const cs)
84
 
{
85
 
  uint32_t copy_length;
86
 
  const char *well_formed_error_pos;
87
 
  const char *cannot_convert_error_pos;
88
 
  const char *from_end_pos;
89
 
 
90
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
91
 
 
92
 
  copy_length= well_formed_copy_nchars(field_charset,
93
 
                                       (char*) ptr + length_bytes,
94
 
                                       field_length,
95
 
                                       cs, from, length,
96
 
                                       field_length / field_charset->mbmaxlen,
97
 
                                       &well_formed_error_pos,
98
 
                                       &cannot_convert_error_pos,
99
 
                                       &from_end_pos);
100
 
 
101
 
  if (length_bytes == 1)
102
 
    *ptr= (unsigned char) copy_length;
103
 
  else
104
 
    int2store(ptr, copy_length);
105
 
 
106
 
  if (check_string_copy_error(this, well_formed_error_pos,
107
 
                              cannot_convert_error_pos, from + length, cs))
108
 
    return 2;
109
 
 
110
 
  return report_if_important_data(from_end_pos, from + length);
111
 
}
112
 
 
113
 
 
114
 
int Field_varstring::store(int64_t nr, bool unsigned_val)
115
 
{
116
 
  char buff[64];
117
 
  uint32_t  length;
118
 
  length= (uint32_t) (field_charset->cset->int64_t10_to_str)(field_charset,
119
 
                                                             buff,
120
 
                                                             sizeof(buff),
121
 
                                                             (unsigned_val ? 10: -10),
122
 
                                                             nr);
123
 
  return Field_varstring::store(buff, length, field_charset);
124
 
}
125
 
 
126
 
 
127
 
double Field_varstring::val_real(void)
128
 
{
129
 
  int not_used;
130
 
  char *end_not_used;
131
 
 
132
 
  ASSERT_COLUMN_MARKED_FOR_READ;
133
 
 
134
 
  uint32_t length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
135
 
 
136
 
  return my_strntod(field_charset, (char*) ptr+length_bytes, length,
137
 
                    &end_not_used, &not_used);
138
 
}
139
 
 
140
 
 
141
 
int64_t Field_varstring::val_int(void)
142
 
{
143
 
  int not_used;
144
 
  char *end_not_used;
145
 
  uint32_t length;
146
 
 
147
 
  ASSERT_COLUMN_MARKED_FOR_READ;
148
 
 
149
 
  length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
150
 
 
151
 
  return my_strntoll(field_charset, (char*) ptr+length_bytes, length, 10,
152
 
                     &end_not_used, &not_used);
153
 
}
154
 
 
155
 
String *Field_varstring::val_str(String *,
156
 
                                 String *val_ptr)
157
 
{
158
 
  uint32_t length=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
159
 
 
160
 
  ASSERT_COLUMN_MARKED_FOR_READ;
161
 
 
162
 
  val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
163
 
 
164
 
  return val_ptr;
165
 
}
166
 
 
167
 
 
168
 
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
169
 
{
170
 
  uint32_t length;
171
 
 
172
 
  ASSERT_COLUMN_MARKED_FOR_READ;
173
 
 
174
 
  length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
175
 
 
176
 
  str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
177
 
                 charset(), decimal_value);
178
 
  return decimal_value;
179
 
}
180
 
 
181
 
 
182
 
int Field_varstring::cmp_max(const unsigned char *a_ptr, const unsigned char *b_ptr,
183
 
                             uint32_t max_len)
184
 
{
185
 
  uint32_t a_length, b_length;
186
 
  int diff;
187
 
 
188
 
  if (length_bytes == 1)
189
 
  {
190
 
    a_length= (uint32_t) *a_ptr;
191
 
    b_length= (uint32_t) *b_ptr;
192
 
  }
193
 
  else
194
 
  {
195
 
    a_length= uint2korr(a_ptr);
196
 
    b_length= uint2korr(b_ptr);
197
 
  }
198
 
  set_if_smaller(a_length, max_len);
199
 
  set_if_smaller(b_length, max_len);
200
 
  diff= field_charset->coll->strnncollsp(field_charset,
201
 
                                         a_ptr+
202
 
                                         length_bytes,
203
 
                                         a_length,
204
 
                                         b_ptr+
205
 
                                         length_bytes,
206
 
                                         b_length,0);
207
 
  return diff;
208
 
}
209
 
 
210
 
 
211
 
/**
212
 
  @note
213
 
    varstring and blob keys are ALWAYS stored with a 2 byte length prefix
214
 
*/
215
 
 
216
 
int Field_varstring::key_cmp(const unsigned char *key_ptr, uint32_t max_key_length)
217
 
{
218
 
  uint32_t length=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
219
 
  uint32_t local_char_length= max_key_length / field_charset->mbmaxlen;
220
 
 
221
 
  local_char_length= my_charpos(field_charset, ptr + length_bytes,
222
 
                                ptr + length_bytes + length, local_char_length);
223
 
  set_if_smaller(length, local_char_length);
224
 
  return field_charset->coll->strnncollsp(field_charset,
225
 
                                          ptr + length_bytes,
226
 
                                          length,
227
 
                                          key_ptr+
228
 
                                          HA_KEY_BLOB_LENGTH,
229
 
                                          uint2korr(key_ptr), 0);
230
 
}
231
 
 
232
 
 
233
 
/**
234
 
  Compare to key segments (always 2 byte length prefix).
235
 
 
236
 
  @note
237
 
    This is used only to compare key segments created for index_read().
238
 
    (keys are created and compared in key.cc)
239
 
*/
240
 
 
241
 
int Field_varstring::key_cmp(const unsigned char *a,const unsigned char *b)
242
 
{
243
 
  return field_charset->coll->strnncollsp(field_charset,
244
 
                                          a + HA_KEY_BLOB_LENGTH,
245
 
                                          uint2korr(a),
246
 
                                          b + HA_KEY_BLOB_LENGTH,
247
 
                                          uint2korr(b),
248
 
                                          0);
249
 
}
250
 
 
251
 
 
252
 
void Field_varstring::sort_string(unsigned char *to,uint32_t length)
253
 
{
254
 
  uint32_t tot_length=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
255
 
 
256
 
  if (field_charset == &my_charset_bin)
257
 
  {
258
 
    /* Store length last in high-byte order to sort longer strings first */
259
 
    if (length_bytes == 1)
260
 
      to[length-1]= tot_length;
261
 
    else
262
 
      mi_int2store(to+length-2, tot_length);
263
 
    length-= length_bytes;
264
 
  }
265
 
 
266
 
  tot_length= my_strnxfrm(field_charset,
267
 
                          to, length, ptr + length_bytes,
268
 
                          tot_length);
269
 
  assert(tot_length == length);
270
 
}
271
 
 
272
 
 
273
 
enum ha_base_keytype Field_varstring::key_type() const
274
 
{
275
 
  enum ha_base_keytype res;
276
 
 
277
 
  if (binary())
278
 
    res= length_bytes == 1 ? HA_KEYTYPE_VARBINARY1 : HA_KEYTYPE_VARBINARY2;
279
 
  else
280
 
    res= length_bytes == 1 ? HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2;
281
 
  return res;
282
 
}
283
 
 
284
 
 
285
 
void Field_varstring::sql_type(String &res) const
286
 
{
287
 
  const CHARSET_INFO * const cs=res.charset();
288
 
  uint32_t length;
289
 
 
290
 
  length= cs->cset->snprintf(cs,(char*) res.ptr(),
291
 
                             res.alloced_length(), "%s(%d)",
292
 
                              (has_charset() ? "varchar" : "varbinary"),
293
 
                             (int) field_length / charset()->mbmaxlen);
294
 
  res.length(length);
295
 
}
296
 
 
297
 
 
298
 
uint32_t Field_varstring::used_length()
299
 
{
300
 
  return length_bytes == 1 ? 1 + (uint32_t) (unsigned char) *ptr : 2 + uint2korr(ptr);
301
 
}
302
 
 
303
 
/*
304
 
  Functions to create a packed row.
305
 
  Here the number of length bytes are depending on the given max_length
306
 
*/
307
 
 
308
 
unsigned char *Field_varstring::pack(unsigned char *to, const unsigned char *from,
309
 
                             uint32_t max_length,
310
 
                             bool )
311
 
{
312
 
  uint32_t length= length_bytes == 1 ? (uint32_t) *from : uint2korr(from);
313
 
  set_if_smaller(max_length, field_length);
314
 
  if (length > max_length)
315
 
    length=max_length;
316
 
 
317
 
  /* Length always stored little-endian */
318
 
  *to++= length & 0xFF;
319
 
  if (max_length > 255)
320
 
    *to++= (length >> 8) & 0xFF;
321
 
 
322
 
  /* Store bytes of string */
323
 
  if (length > 0)
324
 
    memcpy(to, from+length_bytes, length);
325
 
  return to+length;
326
 
}
327
 
 
328
 
 
329
 
/**
330
 
   Unpack a varstring field from row data.
331
 
 
332
 
   This method is used to unpack a varstring field from a master
333
 
   whose size of the field is less than that of the slave.
334
 
 
335
 
   @note
336
 
   The string length is always packed little-endian.
337
 
 
338
 
   @param   to         Destination of the data
339
 
   @param   from       Source of the data
340
 
   @param   param_data Length bytes from the master's field data
341
 
 
342
 
   @return  New pointer into memory based on from + length of the data
343
 
*/
344
 
const unsigned char *
345
 
Field_varstring::unpack(unsigned char *to, const unsigned char *from,
346
 
                        uint32_t param_data,
347
 
                        bool )
348
 
{
349
 
  uint32_t length;
350
 
  uint32_t l_bytes= (param_data && (param_data < field_length)) ?
351
 
                (param_data <= 255) ? 1 : 2 : length_bytes;
352
 
  if (l_bytes == 1)
353
 
  {
354
 
    to[0]= *from++;
355
 
    length= to[0];
356
 
    if (length_bytes == 2)
357
 
      to[1]= 0;
358
 
  }
359
 
  else /* l_bytes == 2 */
360
 
  {
361
 
    length= uint2korr(from);
362
 
    to[0]= *from++;
363
 
    to[1]= *from++;
364
 
  }
365
 
  if (length)
366
 
    memcpy(to+ length_bytes, from, length);
367
 
  return from+length;
368
 
}
369
 
 
370
 
 
371
 
uint32_t Field_varstring::max_packed_col_length(uint32_t max_length)
372
 
{
373
 
  return (max_length > 255 ? 2 : 1)+max_length;
374
 
}
375
 
 
376
 
uint32_t Field_varstring::get_key_image(basic_string<unsigned char> &buff, uint32_t length)
377
 
{
378
 
  /* Key is always stored with 2 bytes */
379
 
  const uint32_t key_len= 2;
380
 
  uint32_t f_length=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
381
 
  uint32_t local_char_length= length / field_charset->mbmaxlen;
382
 
  unsigned char *pos= ptr+length_bytes;
383
 
  local_char_length= my_charpos(field_charset, pos, pos + f_length,
384
 
                                local_char_length);
385
 
  set_if_smaller(f_length, local_char_length);
386
 
  unsigned char len_buff[key_len];
387
 
  int2store(len_buff,f_length);
388
 
  buff.append(len_buff);
389
 
  buff.append(pos, f_length);
390
 
  if (f_length < length)
391
 
  {
392
 
    /*
393
 
      Must clear this as we do a memcmp in optimizer/range.cc to detect
394
 
      identical keys
395
 
    */
396
 
    buff.append(length-f_length, 0);
397
 
  }
398
 
  return key_len+f_length;
399
 
}
400
 
 
401
 
 
402
 
uint32_t Field_varstring::get_key_image(unsigned char *buff, uint32_t length)
403
 
{
404
 
  uint32_t f_length=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
405
 
  uint32_t local_char_length= length / field_charset->mbmaxlen;
406
 
  unsigned char *pos= ptr+length_bytes;
407
 
  local_char_length= my_charpos(field_charset, pos, pos + f_length,
408
 
                                local_char_length);
409
 
  set_if_smaller(f_length, local_char_length);
410
 
  /* Key is always stored with 2 bytes */
411
 
  int2store(buff,f_length);
412
 
  memcpy(buff+HA_KEY_BLOB_LENGTH, pos, f_length);
413
 
  if (f_length < length)
414
 
  {
415
 
    /*
416
 
      Must clear this as we do a memcmp in optimizer/range.cc to detect
417
 
      identical keys
418
 
    */
419
 
    memset(buff+HA_KEY_BLOB_LENGTH+f_length, 0, (length-f_length));
420
 
  }
421
 
  return HA_KEY_BLOB_LENGTH+f_length;
422
 
}
423
 
 
424
 
void Field_varstring::set_key_image(const unsigned char *buff, uint32_t length)
425
 
{
426
 
  length= uint2korr(buff);                      // Real length is here
427
 
  (void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length, field_charset);
428
 
}
429
 
 
430
 
int Field_varstring::cmp_binary(const unsigned char *a_ptr,
431
 
                                const unsigned char *b_ptr,
432
 
                                uint32_t max_length)
433
 
{
434
 
  uint32_t a_length,b_length;
435
 
 
436
 
  if (length_bytes == 1)
437
 
  {
438
 
    a_length= (uint32_t) *a_ptr;
439
 
    b_length= (uint32_t) *b_ptr;
440
 
  }
441
 
  else
442
 
  {
443
 
    a_length= uint2korr(a_ptr);
444
 
    b_length= uint2korr(b_ptr);
445
 
  }
446
 
  set_if_smaller(a_length, max_length);
447
 
  set_if_smaller(b_length, max_length);
448
 
  if (a_length != b_length)
449
 
    return 1;
450
 
  return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
451
 
}
452
 
 
453
 
 
454
 
Field *Field_varstring::new_field(memory::Root *root, Table *new_table, bool keep_type)
455
 
{
456
 
  Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
457
 
                                                            keep_type);
458
 
  if (res)
459
 
    res->length_bytes= length_bytes;
460
 
  return res;
461
 
}
462
 
 
463
 
 
464
 
Field *Field_varstring::new_key_field(memory::Root *root,
465
 
                                      Table *new_table,
466
 
                                      unsigned char *new_ptr, unsigned char *new_null_ptr,
467
 
                                      uint32_t new_null_bit)
468
 
{
469
 
  Field_varstring *res;
470
 
  if ((res= (Field_varstring*) Field::new_key_field(root,
471
 
                                                    new_table,
472
 
                                                    new_ptr,
473
 
                                                    new_null_ptr,
474
 
                                                    new_null_bit)))
475
 
  {
476
 
    /* Keys length prefixes are always packed with 2 bytes */
477
 
    res->length_bytes= 2;
478
 
  }
479
 
  return res;
480
 
}
481
 
 
482
 
} /* namespace drizzled */