~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/varstring.cc

  • Committer: Monty Taylor
  • Date: 2009-04-25 20:45:19 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 1003.
  • Revision ID: mordred@inaugust.com-20090425204519-lgrl7mz2r66v0jby
Blackhole.

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/varstring.h>
24
24
#include <drizzled/table.h>
25
25
#include <drizzled/session.h>
26
 
#include "plugin/myisam/myisam.h"
27
26
 
28
27
#include <string>
29
28
 
30
29
using namespace std;
31
30
 
32
 
namespace drizzled
33
 
{
34
 
 
35
31
/****************************************************************************
36
32
  VARCHAR type
37
33
  Data in field->ptr is stored as:
51
47
const uint32_t Field_varstring::MAX_SIZE= UINT16_MAX;
52
48
 
53
49
Field_varstring::Field_varstring(unsigned char *ptr_arg,
54
 
                                 uint32_t len_arg,
55
 
                                 uint32_t length_bytes_arg,
 
50
                                 uint32_t len_arg, uint32_t length_bytes_arg,
56
51
                                 unsigned char *null_ptr_arg,
57
52
                                 unsigned char null_bit_arg,
 
53
                                 enum utype unireg_check_arg,
58
54
                                 const char *field_name_arg,
59
 
                                 TableShare *share,
 
55
                                 TABLE_SHARE *share,
60
56
                                 const CHARSET_INFO * const cs)
61
 
  :Field_str(ptr_arg,
62
 
             len_arg,
63
 
             null_ptr_arg,
64
 
             null_bit_arg,
65
 
             field_name_arg, cs),
 
57
  :Field_longstr(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
 
58
                 unireg_check_arg, field_name_arg, cs),
66
59
   length_bytes(length_bytes_arg)
67
60
{
68
61
  share->varchar_fields++;
69
62
}
70
63
 
71
 
Field_varstring::Field_varstring(uint32_t len_arg,
72
 
                                 bool maybe_null_arg,
 
64
Field_varstring::Field_varstring(uint32_t len_arg,bool maybe_null_arg,
73
65
                                 const char *field_name_arg,
74
 
                                 TableShare *share,
 
66
                                 TABLE_SHARE *share,
75
67
                                 const CHARSET_INFO * const cs)
76
 
  :Field_str((unsigned char*) 0,
77
 
             len_arg,
78
 
             maybe_null_arg ? (unsigned char*) "": 0,
79
 
             0,
80
 
             field_name_arg,
81
 
             cs),
 
68
  :Field_longstr((unsigned char*) 0,len_arg,
 
69
                 maybe_null_arg ? (unsigned char*) "": 0, 0,
 
70
                 NONE, field_name_arg, cs),
82
71
   length_bytes(len_arg < 256 ? 1 :2)
83
72
{
84
73
  share->varchar_fields++;
85
74
}
86
75
 
 
76
 
 
77
/**
 
78
   Save the field metadata for varstring fields.
 
79
 
 
80
   Saves the field length in the first byte. Note: may consume
 
81
   2 bytes. Caller must ensure second byte is contiguous with
 
82
   first byte (e.g. array index 0,1).
 
83
 
 
84
   @param   metadata_ptr   First byte of field metadata
 
85
 
 
86
   @returns number of bytes written to metadata_ptr
 
87
*/
 
88
int Field_varstring::do_save_field_metadata(unsigned char *metadata_ptr)
 
89
{
 
90
  assert(field_length <= 65535);
 
91
  int2store(metadata_ptr, field_length);
 
92
  return 2;
 
93
}
 
94
 
87
95
int Field_varstring::store(const char *from,uint32_t length, const CHARSET_INFO * const cs)
88
96
{
89
97
  uint32_t copy_length;
91
99
  const char *cannot_convert_error_pos;
92
100
  const char *from_end_pos;
93
101
 
94
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
95
 
 
96
102
  copy_length= well_formed_copy_nchars(field_charset,
97
103
                                       (char*) ptr + length_bytes,
98
104
                                       field_length,
133
139
{
134
140
  int not_used;
135
141
  char *end_not_used;
136
 
 
137
 
  ASSERT_COLUMN_MARKED_FOR_READ;
138
 
 
139
142
  uint32_t length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
140
 
 
141
143
  return my_strntod(field_charset, (char*) ptr+length_bytes, length,
142
144
                    &end_not_used, &not_used);
143
145
}
147
149
{
148
150
  int not_used;
149
151
  char *end_not_used;
150
 
  uint32_t length;
151
 
 
152
 
  ASSERT_COLUMN_MARKED_FOR_READ;
153
 
 
154
 
  length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
155
 
 
 
152
  uint32_t length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
156
153
  return my_strntoll(field_charset, (char*) ptr+length_bytes, length, 10,
157
154
                     &end_not_used, &not_used);
158
155
}
161
158
                                 String *val_ptr)
162
159
{
163
160
  uint32_t length=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
164
 
 
165
 
  ASSERT_COLUMN_MARKED_FOR_READ;
166
 
 
167
161
  val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
168
 
 
169
162
  return val_ptr;
170
163
}
171
164
 
172
165
 
173
166
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
174
167
{
175
 
  uint32_t length;
176
 
 
177
 
  ASSERT_COLUMN_MARKED_FOR_READ;
178
 
 
179
 
  length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
180
 
 
 
168
  uint32_t length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
181
169
  str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
182
170
                 charset(), decimal_value);
183
171
  return decimal_value;
300
288
}
301
289
 
302
290
 
 
291
uint32_t Field_varstring::data_length()
 
292
{
 
293
  return length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
 
294
}
 
295
 
303
296
uint32_t Field_varstring::used_length()
304
297
{
305
298
  return length_bytes == 1 ? 1 + (uint32_t) (unsigned char) *ptr : 2 + uint2korr(ptr);
331
324
}
332
325
 
333
326
 
 
327
unsigned char *
 
328
Field_varstring::pack_key(unsigned char *to, const unsigned char *key, uint32_t max_length,
 
329
                          bool )
 
330
{
 
331
  uint32_t length=  length_bytes == 1 ? (uint32_t) *key : uint2korr(key);
 
332
  uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
 
333
                     max_length/field_charset->mbmaxlen : max_length);
 
334
  key+= length_bytes;
 
335
  if (length > local_char_length)
 
336
  {
 
337
    local_char_length= my_charpos(field_charset, key, key+length,
 
338
                                  local_char_length);
 
339
    set_if_smaller(length, local_char_length);
 
340
  }
 
341
  *to++= (char) (length & 255);
 
342
  if (max_length > 255)
 
343
    *to++= (char) (length >> 8);
 
344
  if (length)
 
345
    memcpy(to, key, length);
 
346
  return to+length;
 
347
}
 
348
 
 
349
 
 
350
/**
 
351
  Unpack a key into a record buffer.
 
352
 
 
353
  A VARCHAR key has a maximum size of 64K-1.
 
354
  In its packed form, the length field is one or two bytes long,
 
355
  depending on 'max_length'.
 
356
 
 
357
  @param to                          Pointer into the record buffer.
 
358
  @param key                         Pointer to the packed key.
 
359
  @param max_length                  Key length limit from key description.
 
360
 
 
361
  @return
 
362
    Pointer to end of 'key' (To the next key part if multi-segment key)
 
363
*/
 
364
 
 
365
const unsigned char *
 
366
Field_varstring::unpack_key(unsigned char *,
 
367
                            const unsigned char *key, uint32_t max_length,
 
368
                            bool )
 
369
{
 
370
  /* get length of the blob key */
 
371
  uint32_t length= *key++;
 
372
  if (max_length > 255)
 
373
    length+= (*key++) << 8;
 
374
 
 
375
  /* put the length into the record buffer */
 
376
  if (length_bytes == 1)
 
377
    *ptr= (unsigned char) length;
 
378
  else
 
379
    int2store(ptr, length);
 
380
  memcpy(ptr + length_bytes, key, length);
 
381
  return key + length;
 
382
}
 
383
 
 
384
/**
 
385
  Create a packed key that will be used for storage in the index tree.
 
386
 
 
387
  @param to             Store packed key segment here
 
388
  @param from           Key segment (as given to index_read())
 
389
  @param max_length     Max length of key
 
390
 
 
391
  @return
 
392
    end of key storage
 
393
*/
 
394
 
 
395
unsigned char *
 
396
Field_varstring::pack_key_from_key_image(unsigned char *to, const unsigned char *from, uint32_t max_length,
 
397
                                         bool )
 
398
{
 
399
  /* Key length is always stored as 2 bytes */
 
400
  uint32_t length= uint2korr(from);
 
401
  if (length > max_length)
 
402
    length= max_length;
 
403
  *to++= (char) (length & 255);
 
404
  if (max_length > 255)
 
405
    *to++= (char) (length >> 8);
 
406
  if (length)
 
407
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
 
408
  return to+length;
 
409
}
 
410
 
 
411
 
334
412
/**
335
413
   Unpack a varstring field from row data.
336
414
 
373
451
}
374
452
 
375
453
 
 
454
int Field_varstring::pack_cmp(const unsigned char *a, const unsigned char *b,
 
455
                              uint32_t key_length_arg,
 
456
                              bool insert_or_update)
 
457
{
 
458
  uint32_t a_length, b_length;
 
459
  if (key_length_arg > 255)
 
460
  {
 
461
    a_length=uint2korr(a); a+= 2;
 
462
    b_length=uint2korr(b); b+= 2;
 
463
  }
 
464
  else
 
465
  {
 
466
    a_length= (uint32_t) *a++;
 
467
    b_length= (uint32_t) *b++;
 
468
  }
 
469
  return field_charset->coll->strnncollsp(field_charset,
 
470
                                          a, a_length,
 
471
                                          b, b_length,
 
472
                                          insert_or_update);
 
473
}
 
474
 
 
475
 
 
476
int Field_varstring::pack_cmp(const unsigned char *b, uint32_t key_length_arg,
 
477
                              bool insert_or_update)
 
478
{
 
479
  unsigned char *a= ptr+ length_bytes;
 
480
  uint32_t a_length=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
 
481
  uint32_t b_length;
 
482
  uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
 
483
                           key_length_arg / field_charset->mbmaxlen :
 
484
                           key_length_arg);
 
485
 
 
486
  if (key_length_arg > 255)
 
487
  {
 
488
    b_length=uint2korr(b); b+= HA_KEY_BLOB_LENGTH;
 
489
  }
 
490
  else
 
491
    b_length= (uint32_t) *b++;
 
492
 
 
493
  if (a_length > local_char_length)
 
494
  {
 
495
    local_char_length= my_charpos(field_charset, a, a+a_length,
 
496
                                  local_char_length);
 
497
    set_if_smaller(a_length, local_char_length);
 
498
  }
 
499
 
 
500
  return field_charset->coll->strnncollsp(field_charset,
 
501
                                          a, a_length,
 
502
                                          b, b_length,
 
503
                                          insert_or_update);
 
504
}
 
505
 
 
506
 
 
507
uint32_t Field_varstring::packed_col_length(const unsigned char *data_ptr, uint32_t length)
 
508
{
 
509
  if (length > 255)
 
510
    return uint2korr(data_ptr)+2;
 
511
  return (uint32_t) *data_ptr + 1;
 
512
}
 
513
 
 
514
 
376
515
uint32_t Field_varstring::max_packed_col_length(uint32_t max_length)
377
516
{
378
517
  return (max_length > 255 ? 2 : 1)+max_length;
379
518
}
380
519
 
381
 
uint32_t Field_varstring::get_key_image(basic_string<unsigned char> &buff, uint32_t length)
 
520
uint32_t Field_varstring::get_key_image(basic_string<unsigned char> &buff,
 
521
                                        uint32_t length, imagetype)
382
522
{
383
523
  /* Key is always stored with 2 bytes */
384
524
  const uint32_t key_len= 2;
395
535
  if (f_length < length)
396
536
  {
397
537
    /*
398
 
      Must clear this as we do a memcmp in optimizer/range.cc to detect
 
538
      Must clear this as we do a memcmp in opt_range.cc to detect
399
539
      identical keys
400
540
    */
401
541
    buff.append(length-f_length, 0);
404
544
}
405
545
 
406
546
 
407
 
uint32_t Field_varstring::get_key_image(unsigned char *buff, uint32_t length)
 
547
uint32_t Field_varstring::get_key_image(unsigned char *buff,
 
548
                                    uint32_t length,
 
549
                                    imagetype )
408
550
{
409
551
  uint32_t f_length=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
410
552
  uint32_t local_char_length= length / field_charset->mbmaxlen;
418
560
  if (f_length < length)
419
561
  {
420
562
    /*
421
 
      Must clear this as we do a memcmp in optimizer/range.cc to detect
 
563
      Must clear this as we do a memcmp in opt_range.cc to detect
422
564
      identical keys
423
565
    */
424
566
    memset(buff+HA_KEY_BLOB_LENGTH+f_length, 0, (length-f_length));
426
568
  return HA_KEY_BLOB_LENGTH+f_length;
427
569
}
428
570
 
429
 
void Field_varstring::set_key_image(const unsigned char *buff, uint32_t length)
 
571
 
 
572
void Field_varstring::set_key_image(const unsigned char *buff,uint32_t length)
430
573
{
431
574
  length= uint2korr(buff);                      // Real length is here
432
 
  (void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length, field_charset);
 
575
  (void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
 
576
                                field_charset);
433
577
}
434
578
 
435
 
int Field_varstring::cmp_binary(const unsigned char *a_ptr,
436
 
                                const unsigned char *b_ptr,
 
579
 
 
580
int Field_varstring::cmp_binary(const unsigned char *a_ptr, const unsigned char *b_ptr,
437
581
                                uint32_t max_length)
438
582
{
439
583
  uint32_t a_length,b_length;
456
600
}
457
601
 
458
602
 
459
 
Field *Field_varstring::new_field(memory::Root *root, Table *new_table, bool keep_type)
 
603
Field *Field_varstring::new_field(MEM_ROOT *root, Table *new_table, bool keep_type)
460
604
{
461
605
  Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
462
606
                                                            keep_type);
466
610
}
467
611
 
468
612
 
469
 
Field *Field_varstring::new_key_field(memory::Root *root,
 
613
Field *Field_varstring::new_key_field(MEM_ROOT *root,
470
614
                                      Table *new_table,
471
615
                                      unsigned char *new_ptr, unsigned char *new_null_ptr,
472
616
                                      uint32_t new_null_bit)
484
628
  return res;
485
629
}
486
630
 
487
 
} /* namespace drizzled */
 
631
 
 
632
uint32_t Field_varstring::is_equal(Create_field *new_field_ptr)
 
633
{
 
634
  if (new_field_ptr->sql_type == real_type() &&
 
635
      new_field_ptr->charset == field_charset)
 
636
  {
 
637
    if (new_field_ptr->length == max_display_length())
 
638
      return IS_EQUAL_YES;
 
639
    if (new_field_ptr->length > max_display_length() &&
 
640
        ((new_field_ptr->length <= 255 && max_display_length() <= 255) ||
 
641
         (new_field_ptr->length > 255 && max_display_length() > 255)))
 
642
      return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
 
643
  }
 
644
  return IS_EQUAL_NO;
 
645
}
 
646
 
 
647
 
 
648
void Field_varstring::hash(uint32_t *nr, uint32_t *nr2)
 
649
{
 
650
  if (is_null())
 
651
  {
 
652
    *nr^= (*nr << 1) | 1;
 
653
  }
 
654
  else
 
655
  {
 
656
    uint32_t len=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
 
657
    const CHARSET_INFO * const cs= charset();
 
658
    cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);
 
659
  }
 
660
}
 
661
 
 
662