~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/varstring.cc

Removed DBUG symbols and fixed TRUE/FALSE

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/varstring.h>
24
 
 
25
 
/****************************************************************************
26
 
  VARCHAR type
27
 
  Data in field->ptr is stored as:
28
 
    1 or 2 bytes length-prefix-header  (from Field_varstring::length_bytes)
29
 
    data
30
 
 
31
 
  NOTE:
32
 
  When VARCHAR is stored in a key (for handler::index_read() etc) it's always
33
 
  stored with a 2 byte prefix. (Just like blob keys).
34
 
 
35
 
  Normally length_bytes is calculated as (field_length < 256 : 1 ? 2)
36
 
  The exception is if there is a prefix key field that is part of a long
37
 
  VARCHAR, in which case field_length for this may be 1 but the length_bytes
38
 
  is 2.
39
 
****************************************************************************/
40
 
 
41
 
const uint32_t Field_varstring::MAX_SIZE= UINT16_MAX;
42
 
 
43
 
/**
44
 
   Save the field metadata for varstring fields.
45
 
 
46
 
   Saves the field length in the first byte. Note: may consume
47
 
   2 bytes. Caller must ensure second byte is contiguous with
48
 
   first byte (e.g. array index 0,1).
49
 
 
50
 
   @param   metadata_ptr   First byte of field metadata
51
 
 
52
 
   @returns number of bytes written to metadata_ptr
53
 
*/
54
 
int Field_varstring::do_save_field_metadata(unsigned char *metadata_ptr)
55
 
{
56
 
  char *ptr= (char *)metadata_ptr;
57
 
  assert(field_length <= 65535);
58
 
  int2store(ptr, field_length);
59
 
  return 2;
60
 
}
61
 
 
62
 
int Field_varstring::store(const char *from,uint32_t length, const CHARSET_INFO * const cs)
63
 
{
64
 
  uint32_t copy_length;
65
 
  const char *well_formed_error_pos;
66
 
  const char *cannot_convert_error_pos;
67
 
  const char *from_end_pos;
68
 
 
69
 
  copy_length= well_formed_copy_nchars(field_charset,
70
 
                                       (char*) ptr + length_bytes,
71
 
                                       field_length,
72
 
                                       cs, from, length,
73
 
                                       field_length / field_charset->mbmaxlen,
74
 
                                       &well_formed_error_pos,
75
 
                                       &cannot_convert_error_pos,
76
 
                                       &from_end_pos);
77
 
 
78
 
  if (length_bytes == 1)
79
 
    *ptr= (unsigned char) copy_length;
80
 
  else
81
 
    int2store(ptr, copy_length);
82
 
 
83
 
  if (check_string_copy_error(this, well_formed_error_pos,
84
 
                              cannot_convert_error_pos, from + length, cs))
85
 
    return 2;
86
 
 
87
 
  return report_if_important_data(from_end_pos, from + length);
88
 
}
89
 
 
90
 
 
91
 
int Field_varstring::store(int64_t nr, bool unsigned_val)
92
 
{
93
 
  char buff[64];
94
 
  uint32_t  length;
95
 
  length= (uint) (field_charset->cset->int64_t10_to_str)(field_charset,
96
 
                                                          buff,
97
 
                                                          sizeof(buff),
98
 
                                                          (unsigned_val ? 10:
99
 
                                                           -10),
100
 
                                                           nr);
101
 
  return Field_varstring::store(buff, length, field_charset);
102
 
}
103
 
 
104
 
 
105
 
double Field_varstring::val_real(void)
106
 
{
107
 
  int not_used;
108
 
  char *end_not_used;
109
 
  uint32_t length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
110
 
  return my_strntod(field_charset, (char*) ptr+length_bytes, length,
111
 
                    &end_not_used, &not_used);
112
 
}
113
 
 
114
 
 
115
 
int64_t Field_varstring::val_int(void)
116
 
{
117
 
  int not_used;
118
 
  char *end_not_used;
119
 
  uint32_t length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
120
 
  return my_strntoll(field_charset, (char*) ptr+length_bytes, length, 10,
121
 
                     &end_not_used, &not_used);
122
 
}
123
 
 
124
 
String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
125
 
                                 String *val_ptr)
126
 
{
127
 
  uint32_t length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
128
 
  val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
129
 
  return val_ptr;
130
 
}
131
 
 
132
 
 
133
 
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
134
 
{
135
 
  uint32_t length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
136
 
  str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
137
 
                 charset(), decimal_value);
138
 
  return decimal_value;
139
 
}
140
 
 
141
 
 
142
 
int Field_varstring::cmp_max(const unsigned char *a_ptr, const unsigned char *b_ptr,
143
 
                             uint32_t max_len)
144
 
{
145
 
  uint32_t a_length, b_length;
146
 
  int diff;
147
 
 
148
 
  if (length_bytes == 1)
149
 
  {
150
 
    a_length= (uint) *a_ptr;
151
 
    b_length= (uint) *b_ptr;
152
 
  }
153
 
  else
154
 
  {
155
 
    a_length= uint2korr(a_ptr);
156
 
    b_length= uint2korr(b_ptr);
157
 
  }
158
 
  set_if_smaller(a_length, max_len);
159
 
  set_if_smaller(b_length, max_len);
160
 
  diff= field_charset->coll->strnncollsp(field_charset,
161
 
                                         a_ptr+
162
 
                                         length_bytes,
163
 
                                         a_length,
164
 
                                         b_ptr+
165
 
                                         length_bytes,
166
 
                                         b_length,0);
167
 
  return diff;
168
 
}
169
 
 
170
 
 
171
 
/**
172
 
  @note
173
 
    varstring and blob keys are ALWAYS stored with a 2 byte length prefix
174
 
*/
175
 
 
176
 
int Field_varstring::key_cmp(const unsigned char *key_ptr, uint32_t max_key_length)
177
 
{
178
 
  uint32_t length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
179
 
  uint32_t local_char_length= max_key_length / field_charset->mbmaxlen;
180
 
 
181
 
  local_char_length= my_charpos(field_charset, ptr + length_bytes,
182
 
                          ptr + length_bytes + length, local_char_length);
183
 
  set_if_smaller(length, local_char_length);
184
 
  return field_charset->coll->strnncollsp(field_charset, 
185
 
                                          ptr + length_bytes,
186
 
                                          length,
187
 
                                          key_ptr+
188
 
                                          HA_KEY_BLOB_LENGTH,
189
 
                                          uint2korr(key_ptr), 0);
190
 
}
191
 
 
192
 
 
193
 
/**
194
 
  Compare to key segments (always 2 byte length prefix).
195
 
 
196
 
  @note
197
 
    This is used only to compare key segments created for index_read().
198
 
    (keys are created and compared in key.cc)
199
 
*/
200
 
 
201
 
int Field_varstring::key_cmp(const unsigned char *a,const unsigned char *b)
202
 
{
203
 
  return field_charset->coll->strnncollsp(field_charset,
204
 
                                          a + HA_KEY_BLOB_LENGTH,
205
 
                                          uint2korr(a),
206
 
                                          b + HA_KEY_BLOB_LENGTH,
207
 
                                          uint2korr(b),
208
 
                                          0);
209
 
}
210
 
 
211
 
 
212
 
void Field_varstring::sort_string(unsigned char *to,uint32_t length)
213
 
{
214
 
  uint32_t tot_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
215
 
 
216
 
  if (field_charset == &my_charset_bin)
217
 
  {
218
 
    /* Store length last in high-byte order to sort longer strings first */
219
 
    if (length_bytes == 1)
220
 
      to[length-1]= tot_length;
221
 
    else
222
 
      mi_int2store(to+length-2, tot_length);
223
 
    length-= length_bytes;
224
 
  }
225
 
 
226
 
  tot_length= my_strnxfrm(field_charset,
227
 
                          to, length, ptr + length_bytes,
228
 
                          tot_length);
229
 
  assert(tot_length == length);
230
 
}
231
 
 
232
 
 
233
 
enum ha_base_keytype Field_varstring::key_type() const
234
 
{
235
 
  enum ha_base_keytype res;
236
 
 
237
 
  if (binary())
238
 
    res= length_bytes == 1 ? HA_KEYTYPE_VARBINARY1 : HA_KEYTYPE_VARBINARY2;
239
 
  else
240
 
    res= length_bytes == 1 ? HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2;
241
 
  return res;
242
 
}
243
 
 
244
 
 
245
 
void Field_varstring::sql_type(String &res) const
246
 
{
247
 
  const CHARSET_INFO * const cs=res.charset();
248
 
  uint32_t length;
249
 
 
250
 
  length= cs->cset->snprintf(cs,(char*) res.ptr(),
251
 
                             res.alloced_length(), "%s(%d)",
252
 
                              (has_charset() ? "varchar" : "varbinary"),
253
 
                             (int) field_length / charset()->mbmaxlen);
254
 
  res.length(length);
255
 
}
256
 
 
257
 
 
258
 
uint32_t Field_varstring::data_length()
259
 
{
260
 
  return length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
261
 
}
262
 
 
263
 
uint32_t Field_varstring::used_length()
264
 
{
265
 
  return length_bytes == 1 ? 1 + (uint32_t) (unsigned char) *ptr : 2 + uint2korr(ptr);
266
 
}
267
 
 
268
 
/*
269
 
  Functions to create a packed row.
270
 
  Here the number of length bytes are depending on the given max_length
271
 
*/
272
 
 
273
 
unsigned char *Field_varstring::pack(unsigned char *to, const unsigned char *from,
274
 
                             uint32_t max_length,
275
 
                             bool low_byte_first __attribute__((unused)))
276
 
{
277
 
  uint32_t length= length_bytes == 1 ? (uint) *from : uint2korr(from);
278
 
  set_if_smaller(max_length, field_length);
279
 
  if (length > max_length)
280
 
    length=max_length;
281
 
 
282
 
  /* Length always stored little-endian */
283
 
  *to++= length & 0xFF;
284
 
  if (max_length > 255)
285
 
    *to++= (length >> 8) & 0xFF;
286
 
 
287
 
  /* Store bytes of string */
288
 
  if (length > 0)
289
 
    memcpy(to, from+length_bytes, length);
290
 
  return to+length;
291
 
}
292
 
 
293
 
 
294
 
unsigned char *
295
 
Field_varstring::pack_key(unsigned char *to, const unsigned char *key, uint32_t max_length,
296
 
                          bool low_byte_first __attribute__((unused)))
297
 
{
298
 
  uint32_t length=  length_bytes == 1 ? (uint) *key : uint2korr(key);
299
 
  uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
300
 
                     max_length/field_charset->mbmaxlen : max_length);
301
 
  key+= length_bytes;
302
 
  if (length > local_char_length)
303
 
  {
304
 
    local_char_length= my_charpos(field_charset, key, key+length,
305
 
                                  local_char_length);
306
 
    set_if_smaller(length, local_char_length);
307
 
  }
308
 
  *to++= (char) (length & 255);
309
 
  if (max_length > 255)
310
 
    *to++= (char) (length >> 8);
311
 
  if (length)
312
 
    memcpy(to, key, length);
313
 
  return to+length;
314
 
}
315
 
 
316
 
 
317
 
/**
318
 
  Unpack a key into a record buffer.
319
 
 
320
 
  A VARCHAR key has a maximum size of 64K-1.
321
 
  In its packed form, the length field is one or two bytes long,
322
 
  depending on 'max_length'.
323
 
 
324
 
  @param to                          Pointer into the record buffer.
325
 
  @param key                         Pointer to the packed key.
326
 
  @param max_length                  Key length limit from key description.
327
 
 
328
 
  @return
329
 
    Pointer to end of 'key' (To the next key part if multi-segment key)
330
 
*/
331
 
 
332
 
const unsigned char *
333
 
Field_varstring::unpack_key(unsigned char *to __attribute__((unused)),
334
 
                            const unsigned char *key, uint32_t max_length,
335
 
                            bool low_byte_first __attribute__((unused)))
336
 
{
337
 
  /* get length of the blob key */
338
 
  uint32_t length= *key++;
339
 
  if (max_length > 255)
340
 
    length+= (*key++) << 8;
341
 
 
342
 
  /* put the length into the record buffer */
343
 
  if (length_bytes == 1)
344
 
    *ptr= (unsigned char) length;
345
 
  else
346
 
    int2store(ptr, length);
347
 
  memcpy(ptr + length_bytes, key, length);
348
 
  return key + length;
349
 
}
350
 
 
351
 
/**
352
 
  Create a packed key that will be used for storage in the index tree.
353
 
 
354
 
  @param to             Store packed key segment here
355
 
  @param from           Key segment (as given to index_read())
356
 
  @param max_length     Max length of key
357
 
 
358
 
  @return
359
 
    end of key storage
360
 
*/
361
 
 
362
 
unsigned char *
363
 
Field_varstring::pack_key_from_key_image(unsigned char *to, const unsigned char *from, uint32_t max_length,
364
 
                                         bool low_byte_first __attribute__((unused)))
365
 
{
366
 
  /* Key length is always stored as 2 bytes */
367
 
  uint32_t length= uint2korr(from);
368
 
  if (length > max_length)
369
 
    length= max_length;
370
 
  *to++= (char) (length & 255);
371
 
  if (max_length > 255)
372
 
    *to++= (char) (length >> 8);
373
 
  if (length)
374
 
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
375
 
  return to+length;
376
 
}
377
 
 
378
 
 
379
 
/**
380
 
   Unpack a varstring field from row data.
381
 
 
382
 
   This method is used to unpack a varstring field from a master
383
 
   whose size of the field is less than that of the slave.
384
 
 
385
 
   @note
386
 
   The string length is always packed little-endian.
387
 
  
388
 
   @param   to         Destination of the data
389
 
   @param   from       Source of the data
390
 
   @param   param_data Length bytes from the master's field data
391
 
 
392
 
   @return  New pointer into memory based on from + length of the data
393
 
*/
394
 
const unsigned char *
395
 
Field_varstring::unpack(unsigned char *to, const unsigned char *from,
396
 
                        uint32_t param_data,
397
 
                        bool low_byte_first __attribute__((unused)))
398
 
{
399
 
  uint32_t length;
400
 
  uint32_t l_bytes= (param_data && (param_data < field_length)) ? 
401
 
                (param_data <= 255) ? 1 : 2 : length_bytes;
402
 
  if (l_bytes == 1)
403
 
  {
404
 
    to[0]= *from++;
405
 
    length= to[0];
406
 
    if (length_bytes == 2)
407
 
      to[1]= 0;
408
 
  }
409
 
  else /* l_bytes == 2 */
410
 
  {
411
 
    length= uint2korr(from);
412
 
    to[0]= *from++;
413
 
    to[1]= *from++;
414
 
  }
415
 
  if (length)
416
 
    memcpy(to+ length_bytes, from, length);
417
 
  return from+length;
418
 
}
419
 
 
420
 
 
421
 
int Field_varstring::pack_cmp(const unsigned char *a, const unsigned char *b,
422
 
                              uint32_t key_length_arg,
423
 
                              bool insert_or_update)
424
 
{
425
 
  uint32_t a_length, b_length;
426
 
  if (key_length_arg > 255)
427
 
  {
428
 
    a_length=uint2korr(a); a+= 2;
429
 
    b_length=uint2korr(b); b+= 2;
430
 
  }
431
 
  else
432
 
  {
433
 
    a_length= (uint) *a++;
434
 
    b_length= (uint) *b++;
435
 
  }
436
 
  return field_charset->coll->strnncollsp(field_charset,
437
 
                                          a, a_length,
438
 
                                          b, b_length,
439
 
                                          insert_or_update);
440
 
}
441
 
 
442
 
 
443
 
int Field_varstring::pack_cmp(const unsigned char *b, uint32_t key_length_arg,
444
 
                              bool insert_or_update)
445
 
{
446
 
  unsigned char *a= ptr+ length_bytes;
447
 
  uint32_t a_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
448
 
  uint32_t b_length;
449
 
  uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
450
 
                           key_length_arg / field_charset->mbmaxlen :
451
 
                           key_length_arg);
452
 
 
453
 
  if (key_length_arg > 255)
454
 
  {
455
 
    b_length=uint2korr(b); b+= HA_KEY_BLOB_LENGTH;
456
 
  }
457
 
  else
458
 
    b_length= (uint) *b++;
459
 
 
460
 
  if (a_length > local_char_length)
461
 
  {
462
 
    local_char_length= my_charpos(field_charset, a, a+a_length,
463
 
                                  local_char_length);
464
 
    set_if_smaller(a_length, local_char_length);
465
 
  }
466
 
 
467
 
  return field_charset->coll->strnncollsp(field_charset,
468
 
                                          a, a_length,
469
 
                                          b, b_length,
470
 
                                          insert_or_update);
471
 
}
472
 
 
473
 
 
474
 
uint32_t Field_varstring::packed_col_length(const unsigned char *data_ptr, uint32_t length)
475
 
{
476
 
  if (length > 255)
477
 
    return uint2korr(data_ptr)+2;
478
 
  return (uint) *data_ptr + 1;
479
 
}
480
 
 
481
 
 
482
 
uint32_t Field_varstring::max_packed_col_length(uint32_t max_length)
483
 
{
484
 
  return (max_length > 255 ? 2 : 1)+max_length;
485
 
}
486
 
 
487
 
uint32_t Field_varstring::get_key_image(unsigned char *buff,
488
 
                                    uint32_t length,
489
 
                                    imagetype type __attribute__((unused)))
490
 
{
491
 
  uint32_t f_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
492
 
  uint32_t local_char_length= length / field_charset->mbmaxlen;
493
 
  unsigned char *pos= ptr+length_bytes;
494
 
  local_char_length= my_charpos(field_charset, pos, pos + f_length,
495
 
                                local_char_length);
496
 
  set_if_smaller(f_length, local_char_length);
497
 
  /* Key is always stored with 2 bytes */
498
 
  int2store(buff,f_length);
499
 
  memcpy(buff+HA_KEY_BLOB_LENGTH, pos, f_length);
500
 
  if (f_length < length)
501
 
  {
502
 
    /*
503
 
      Must clear this as we do a memcmp in opt_range.cc to detect
504
 
      identical keys
505
 
    */
506
 
    memset(buff+HA_KEY_BLOB_LENGTH+f_length, 0, (length-f_length));
507
 
  }
508
 
  return HA_KEY_BLOB_LENGTH+f_length;
509
 
}
510
 
 
511
 
 
512
 
void Field_varstring::set_key_image(const unsigned char *buff,uint32_t length)
513
 
{
514
 
  length= uint2korr(buff);                      // Real length is here
515
 
  (void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
516
 
                                field_charset);
517
 
}
518
 
 
519
 
 
520
 
int Field_varstring::cmp_binary(const unsigned char *a_ptr, const unsigned char *b_ptr,
521
 
                                uint32_t max_length)
522
 
{
523
 
  uint32_t a_length,b_length;
524
 
 
525
 
  if (length_bytes == 1)
526
 
  {
527
 
    a_length= (uint) *a_ptr;
528
 
    b_length= (uint) *b_ptr;
529
 
  }
530
 
  else
531
 
  {
532
 
    a_length= uint2korr(a_ptr);
533
 
    b_length= uint2korr(b_ptr);
534
 
  }
535
 
  set_if_smaller(a_length, max_length);
536
 
  set_if_smaller(b_length, max_length);
537
 
  if (a_length != b_length)
538
 
    return 1;
539
 
  return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
540
 
}
541
 
 
542
 
 
543
 
Field *Field_varstring::new_field(MEM_ROOT *root, Table *new_table, bool keep_type)
544
 
{
545
 
  Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
546
 
                                                            keep_type);
547
 
  if (res)
548
 
    res->length_bytes= length_bytes;
549
 
  return res;
550
 
}
551
 
 
552
 
 
553
 
Field *Field_varstring::new_key_field(MEM_ROOT *root,
554
 
                                      Table *new_table,
555
 
                                      unsigned char *new_ptr, unsigned char *new_null_ptr,
556
 
                                      uint32_t new_null_bit)
557
 
{
558
 
  Field_varstring *res;
559
 
  if ((res= (Field_varstring*) Field::new_key_field(root,
560
 
                                                    new_table,
561
 
                                                    new_ptr,
562
 
                                                    new_null_ptr,
563
 
                                                    new_null_bit)))
564
 
  {
565
 
    /* Keys length prefixes are always packed with 2 bytes */
566
 
    res->length_bytes= 2;
567
 
  }
568
 
  return res;
569
 
}
570
 
 
571
 
 
572
 
uint32_t Field_varstring::is_equal(Create_field *new_field)
573
 
{
574
 
  if (new_field->sql_type == real_type() &&
575
 
      new_field->charset == field_charset)
576
 
  {
577
 
    if (new_field->length == max_display_length())
578
 
      return IS_EQUAL_YES;
579
 
    if (new_field->length > max_display_length() &&
580
 
        ((new_field->length <= 255 && max_display_length() <= 255) ||
581
 
         (new_field->length > 255 && max_display_length() > 255)))
582
 
      return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
583
 
  }
584
 
  return IS_EQUAL_NO;
585
 
}
586
 
 
587
 
 
588
 
void Field_varstring::hash(uint32_t *nr, uint32_t *nr2)
589
 
{
590
 
  if (is_null())
591
 
  {
592
 
    *nr^= (*nr << 1) | 1;
593
 
  }
594
 
  else
595
 
  {
596
 
    uint32_t len=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
597
 
    const CHARSET_INFO * const cs= charset();
598
 
    cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);
599
 
  }
600
 
}
601
 
 
602