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"
30
using namespace drizzled;
29
31
using namespace std;
31
33
/****************************************************************************
80
82
share->varchar_fields++;
84
Save the field metadata for varstring fields.
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).
90
@param metadata_ptr First byte of field metadata
92
@returns number of bytes written to metadata_ptr
94
int Field_varstring::do_save_field_metadata(unsigned char *metadata_ptr)
96
assert(field_length <= 65535);
97
int2store(metadata_ptr, field_length);
101
85
int Field_varstring::store(const char *from,uint32_t length, const CHARSET_INFO * const cs)
103
87
uint32_t copy_length;
354
Field_varstring::pack_key(unsigned char *to, const unsigned char *key, uint32_t max_length,
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);
361
if (length > local_char_length)
363
local_char_length= my_charpos(field_charset, key, key+length,
365
set_if_smaller(length, local_char_length);
367
*to++= (char) (length & 255);
368
if (max_length > 255)
369
*to++= (char) (length >> 8);
371
memcpy(to, key, length);
377
Unpack a key into a record buffer.
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'.
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.
388
Pointer to end of 'key' (To the next key part if multi-segment key)
391
const unsigned char *
392
Field_varstring::unpack_key(unsigned char *,
393
const unsigned char *key, uint32_t max_length,
396
/* get length of the blob key */
397
uint32_t length= *key++;
398
if (max_length > 255)
399
length+= (*key++) << 8;
401
/* put the length into the record buffer */
402
if (length_bytes == 1)
403
*ptr= (unsigned char) length;
405
int2store(ptr, length);
406
memcpy(ptr + length_bytes, key, length);
411
Create a packed key that will be used for storage in the index tree.
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
422
Field_varstring::pack_key_from_key_image(unsigned char *to, const unsigned char *from, uint32_t max_length,
425
/* Key length is always stored as 2 bytes */
426
uint32_t length= uint2korr(from);
427
if (length > max_length)
429
*to++= (char) (length & 255);
430
if (max_length > 255)
431
*to++= (char) (length >> 8);
433
memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
439
333
Unpack a varstring field from row data.
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)
484
uint32_t a_length, b_length;
485
if (key_length_arg > 255)
487
a_length=uint2korr(a); a+= 2;
488
b_length=uint2korr(b); b+= 2;
492
a_length= (uint32_t) *a++;
493
b_length= (uint32_t) *b++;
495
return field_charset->coll->strnncollsp(field_charset,
502
int Field_varstring::pack_cmp(const unsigned char *b, uint32_t key_length_arg,
503
bool insert_or_update)
505
unsigned char *a= ptr+ length_bytes;
506
uint32_t a_length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
508
uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
509
key_length_arg / field_charset->mbmaxlen :
512
if (key_length_arg > 255)
514
b_length=uint2korr(b); b+= HA_KEY_BLOB_LENGTH;
517
b_length= (uint32_t) *b++;
519
if (a_length > local_char_length)
521
local_char_length= my_charpos(field_charset, a, a+a_length,
523
set_if_smaller(a_length, local_char_length);
526
return field_charset->coll->strnncollsp(field_charset,
533
uint32_t Field_varstring::packed_col_length(const unsigned char *data_ptr, uint32_t length)
536
return uint2korr(data_ptr)+2;
537
return (uint32_t) *data_ptr + 1;
541
374
uint32_t Field_varstring::max_packed_col_length(uint32_t max_length)
543
376
return (max_length > 255 ? 2 : 1)+max_length;
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)
626
459
Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
653
uint32_t Field_varstring::is_equal(CreateField *new_field_ptr)
655
if (new_field_ptr->sql_type == real_type() &&
656
new_field_ptr->charset == field_charset)
658
if (new_field_ptr->length == max_display_length())
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
669
void Field_varstring::hash(uint32_t *nr, uint32_t *nr2)
673
*nr^= (*nr << 1) | 1;
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);