~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/varstring.cc

Remove PLUGIN and MODULES.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
 
22
 
#include <drizzled/server_includes.h>
 
22
#include "config.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"
26
27
 
27
28
#include <string>
28
29
 
 
30
using namespace drizzled;
29
31
using namespace std;
30
32
 
31
33
/****************************************************************************
80
82
  share->varchar_fields++;
81
83
}
82
84
 
83
 
/**
84
 
   Save the field metadata for varstring fields.
85
 
 
86
 
   Saves the field length in the first byte. Note: may consume
87
 
   2 bytes. Caller must ensure second byte is contiguous with
88
 
   first byte (e.g. array index 0,1).
89
 
 
90
 
   @param   metadata_ptr   First byte of field metadata
91
 
 
92
 
   @returns number of bytes written to metadata_ptr
93
 
*/
94
 
int Field_varstring::do_save_field_metadata(unsigned char *metadata_ptr)
95
 
{
96
 
  assert(field_length <= 65535);
97
 
  int2store(metadata_ptr, field_length);
98
 
  return 2;
99
 
}
100
 
 
101
85
int Field_varstring::store(const char *from,uint32_t length, const CHARSET_INFO * const cs)
102
86
{
103
87
  uint32_t copy_length;
314
298
}
315
299
 
316
300
 
317
 
uint32_t Field_varstring::data_length()
318
 
{
319
 
  return length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
320
 
}
321
 
 
322
301
uint32_t Field_varstring::used_length()
323
302
{
324
303
  return length_bytes == 1 ? 1 + (uint32_t) (unsigned char) *ptr : 2 + uint2korr(ptr);
350
329
}
351
330
 
352
331
 
353
 
unsigned char *
354
 
Field_varstring::pack_key(unsigned char *to, const unsigned char *key, uint32_t max_length,
355
 
                          bool )
356
 
{
357
 
  uint32_t length=  length_bytes == 1 ? (uint32_t) *key : uint2korr(key);
358
 
  uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
359
 
                     max_length/field_charset->mbmaxlen : max_length);
360
 
  key+= length_bytes;
361
 
  if (length > local_char_length)
362
 
  {
363
 
    local_char_length= my_charpos(field_charset, key, key+length,
364
 
                                  local_char_length);
365
 
    set_if_smaller(length, local_char_length);
366
 
  }
367
 
  *to++= (char) (length & 255);
368
 
  if (max_length > 255)
369
 
    *to++= (char) (length >> 8);
370
 
  if (length)
371
 
    memcpy(to, key, length);
372
 
  return to+length;
373
 
}
374
 
 
375
 
 
376
 
/**
377
 
  Unpack a key into a record buffer.
378
 
 
379
 
  A VARCHAR key has a maximum size of 64K-1.
380
 
  In its packed form, the length field is one or two bytes long,
381
 
  depending on 'max_length'.
382
 
 
383
 
  @param to                          Pointer into the record buffer.
384
 
  @param key                         Pointer to the packed key.
385
 
  @param max_length                  Key length limit from key description.
386
 
 
387
 
  @return
388
 
    Pointer to end of 'key' (To the next key part if multi-segment key)
389
 
*/
390
 
 
391
 
const unsigned char *
392
 
Field_varstring::unpack_key(unsigned char *,
393
 
                            const unsigned char *key, uint32_t max_length,
394
 
                            bool )
395
 
{
396
 
  /* get length of the blob key */
397
 
  uint32_t length= *key++;
398
 
  if (max_length > 255)
399
 
    length+= (*key++) << 8;
400
 
 
401
 
  /* put the length into the record buffer */
402
 
  if (length_bytes == 1)
403
 
    *ptr= (unsigned char) length;
404
 
  else
405
 
    int2store(ptr, length);
406
 
  memcpy(ptr + length_bytes, key, length);
407
 
  return key + length;
408
 
}
409
 
 
410
 
/**
411
 
  Create a packed key that will be used for storage in the index tree.
412
 
 
413
 
  @param to             Store packed key segment here
414
 
  @param from           Key segment (as given to index_read())
415
 
  @param max_length     Max length of key
416
 
 
417
 
  @return
418
 
    end of key storage
419
 
*/
420
 
 
421
 
unsigned char *
422
 
Field_varstring::pack_key_from_key_image(unsigned char *to, const unsigned char *from, uint32_t max_length,
423
 
                                         bool )
424
 
{
425
 
  /* Key length is always stored as 2 bytes */
426
 
  uint32_t length= uint2korr(from);
427
 
  if (length > max_length)
428
 
    length= max_length;
429
 
  *to++= (char) (length & 255);
430
 
  if (max_length > 255)
431
 
    *to++= (char) (length >> 8);
432
 
  if (length)
433
 
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
434
 
  return to+length;
435
 
}
436
 
 
437
 
 
438
332
/**
439
333
   Unpack a varstring field from row data.
440
334
 
477
371
}
478
372
 
479
373
 
480
 
int Field_varstring::pack_cmp(const unsigned char *a, const unsigned char *b,
481
 
                              uint32_t key_length_arg,
482
 
                              bool insert_or_update)
483
 
{
484
 
  uint32_t a_length, b_length;
485
 
  if (key_length_arg > 255)
486
 
  {
487
 
    a_length=uint2korr(a); a+= 2;
488
 
    b_length=uint2korr(b); b+= 2;
489
 
  }
490
 
  else
491
 
  {
492
 
    a_length= (uint32_t) *a++;
493
 
    b_length= (uint32_t) *b++;
494
 
  }
495
 
  return field_charset->coll->strnncollsp(field_charset,
496
 
                                          a, a_length,
497
 
                                          b, b_length,
498
 
                                          insert_or_update);
499
 
}
500
 
 
501
 
 
502
 
int Field_varstring::pack_cmp(const unsigned char *b, uint32_t key_length_arg,
503
 
                              bool insert_or_update)
504
 
{
505
 
  unsigned char *a= ptr+ length_bytes;
506
 
  uint32_t a_length=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
507
 
  uint32_t b_length;
508
 
  uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
509
 
                           key_length_arg / field_charset->mbmaxlen :
510
 
                           key_length_arg);
511
 
 
512
 
  if (key_length_arg > 255)
513
 
  {
514
 
    b_length=uint2korr(b); b+= HA_KEY_BLOB_LENGTH;
515
 
  }
516
 
  else
517
 
    b_length= (uint32_t) *b++;
518
 
 
519
 
  if (a_length > local_char_length)
520
 
  {
521
 
    local_char_length= my_charpos(field_charset, a, a+a_length,
522
 
                                  local_char_length);
523
 
    set_if_smaller(a_length, local_char_length);
524
 
  }
525
 
 
526
 
  return field_charset->coll->strnncollsp(field_charset,
527
 
                                          a, a_length,
528
 
                                          b, b_length,
529
 
                                          insert_or_update);
530
 
}
531
 
 
532
 
 
533
 
uint32_t Field_varstring::packed_col_length(const unsigned char *data_ptr, uint32_t length)
534
 
{
535
 
  if (length > 255)
536
 
    return uint2korr(data_ptr)+2;
537
 
  return (uint32_t) *data_ptr + 1;
538
 
}
539
 
 
540
 
 
541
374
uint32_t Field_varstring::max_packed_col_length(uint32_t max_length)
542
375
{
543
376
  return (max_length > 255 ? 2 : 1)+max_length;
560
393
  if (f_length < length)
561
394
  {
562
395
    /*
563
 
      Must clear this as we do a memcmp in opt_range.cc to detect
 
396
      Must clear this as we do a memcmp in optimizer/range.cc to detect
564
397
      identical keys
565
398
    */
566
399
    buff.append(length-f_length, 0);
583
416
  if (f_length < length)
584
417
  {
585
418
    /*
586
 
      Must clear this as we do a memcmp in opt_range.cc to detect
 
419
      Must clear this as we do a memcmp in optimizer/range.cc to detect
587
420
      identical keys
588
421
    */
589
422
    memset(buff+HA_KEY_BLOB_LENGTH+f_length, 0, (length-f_length));
621
454
}
622
455
 
623
456
 
624
 
Field *Field_varstring::new_field(MEM_ROOT *root, Table *new_table, bool keep_type)
 
457
Field *Field_varstring::new_field(memory::Root *root, Table *new_table, bool keep_type)
625
458
{
626
459
  Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
627
460
                                                            keep_type);
631
464
}
632
465
 
633
466
 
634
 
Field *Field_varstring::new_key_field(MEM_ROOT *root,
 
467
Field *Field_varstring::new_key_field(memory::Root *root,
635
468
                                      Table *new_table,
636
469
                                      unsigned char *new_ptr, unsigned char *new_null_ptr,
637
470
                                      uint32_t new_null_bit)
648
481
  }
649
482
  return res;
650
483
}
651
 
 
652
 
 
653
 
uint32_t Field_varstring::is_equal(CreateField *new_field_ptr)
654
 
{
655
 
  if (new_field_ptr->sql_type == real_type() &&
656
 
      new_field_ptr->charset == field_charset)
657
 
  {
658
 
    if (new_field_ptr->length == max_display_length())
659
 
      return IS_EQUAL_YES;
660
 
    if (new_field_ptr->length > max_display_length() &&
661
 
        ((new_field_ptr->length <= 255 && max_display_length() <= 255) ||
662
 
         (new_field_ptr->length > 255 && max_display_length() > 255)))
663
 
      return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
664
 
  }
665
 
  return IS_EQUAL_NO;
666
 
}
667
 
 
668
 
 
669
 
void Field_varstring::hash(uint32_t *nr, uint32_t *nr2)
670
 
{
671
 
  if (is_null())
672
 
  {
673
 
    *nr^= (*nr << 1) | 1;
674
 
  }
675
 
  else
676
 
  {
677
 
    uint32_t len=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
678
 
    const CHARSET_INFO * const cs= charset();
679
 
    cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);
680
 
  }
681
 
}
682
 
 
683