34
42
** packlength slot and may be from 1-4.
35
43
****************************************************************************/
37
Field_blob::Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
45
Field_blob::Field_blob(unsigned char *ptr_arg, unsigned char *null_ptr_arg, unsigned char null_bit_arg,
38
46
enum utype unireg_check_arg, const char *field_name_arg,
39
TABLE_SHARE *share, uint blob_pack_length,
47
TableShare *share, uint32_t blob_pack_length,
40
48
const CHARSET_INFO * const cs)
41
:Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
49
:Field_str(ptr_arg, blob_pack_length_to_max_length(blob_pack_length),
42
50
null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
44
52
packlength(blob_pack_length)
221
264
/* Fatal OOM error */
222
265
memset(ptr, 0, Field_blob::pack_length());
227
270
int Field_blob::store(double nr)
229
272
const CHARSET_INFO * const cs=charset();
273
ASSERT_COLUMN_MARKED_FOR_WRITE;
230
274
value.set_real(nr, NOT_FIXED_DEC, cs);
231
return Field_blob::store(value.ptr(),(uint) value.length(), cs);
275
return Field_blob::store(value.ptr(),(uint32_t) value.length(), cs);
235
279
int Field_blob::store(int64_t nr, bool unsigned_val)
237
281
const CHARSET_INFO * const cs=charset();
282
ASSERT_COLUMN_MARKED_FOR_WRITE;
238
283
value.set_int(nr, unsigned_val, cs);
239
return Field_blob::store(value.ptr(), (uint) value.length(), cs);
284
return Field_blob::store(value.ptr(), (uint32_t) value.length(), cs);
302
int Field_blob::cmp(const uchar *a,uint32_t a_length, const uchar *b,
358
int Field_blob::cmp(const unsigned char *a,uint32_t a_length, const unsigned char *b,
303
359
uint32_t b_length)
305
return field_charset->coll->strnncollsp(field_charset,
361
return field_charset->coll->strnncollsp(field_charset,
306
362
a, a_length, b, b_length,
311
int Field_blob::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
367
int Field_blob::cmp_max(const unsigned char *a_ptr, const unsigned char *b_ptr,
370
unsigned char *blob1,*blob2;
315
371
memcpy(&blob1,a_ptr+packlength,sizeof(char*));
316
372
memcpy(&blob2,b_ptr+packlength,sizeof(char*));
317
uint a_len= get_length(a_ptr), b_len= get_length(b_ptr);
373
uint32_t a_len= get_length(a_ptr), b_len= get_length(b_ptr);
318
374
set_if_smaller(a_len, max_length);
319
375
set_if_smaller(b_len, max_length);
320
376
return Field_blob::cmp(blob1,a_len,blob2,b_len);
324
int Field_blob::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
380
int Field_blob::cmp_binary(const unsigned char *a_ptr, const unsigned char *b_ptr,
329
385
uint32_t a_length,b_length;
330
386
memcpy(&a,a_ptr+packlength,sizeof(char*));
331
387
memcpy(&b,b_ptr+packlength,sizeof(char*));
332
a_length=get_length(a_ptr);
389
a_length= get_length(a_ptr);
333
391
if (a_length > max_length)
335
b_length=get_length(b_ptr);
392
a_length= max_length;
394
b_length= get_length(b_ptr);
336
396
if (b_length > max_length)
338
diff=memcmp(a,b,min(a_length,b_length));
397
b_length= max_length;
399
diff= memcmp(a,b,min(a_length,b_length));
339
401
return diff ? diff : (int) (a_length - b_length);
343
404
/* The following is used only when comparing a key */
345
uint Field_blob::get_key_image(uchar *buff,
347
imagetype type_arg __attribute__((unused)))
405
uint32_t Field_blob::get_key_image(unsigned char *buff, uint32_t length)
349
407
uint32_t blob_length= get_length(ptr);
353
uint local_char_length= length / field_charset->mbmaxlen;
411
uint32_t local_char_length= length / field_charset->mbmaxlen;
354
412
local_char_length= my_charpos(field_charset, blob, blob + blob_length,
355
413
local_char_length);
356
414
set_if_smaller(blob_length, local_char_length);
373
void Field_blob::set_key_image(const uchar *buff,uint length)
431
uint32_t Field_blob::get_key_image(basic_string<unsigned char> &buff, uint32_t length)
433
uint32_t blob_length= get_length(ptr);
437
uint32_t local_char_length= length / field_charset->mbmaxlen;
438
local_char_length= my_charpos(field_charset, blob, blob + blob_length,
440
set_if_smaller(blob_length, local_char_length);
442
unsigned char len_buff[HA_KEY_BLOB_LENGTH];
443
int2store(len_buff,length);
444
buff.append(len_buff);
445
buff.append(blob, blob_length);
447
if (length > blob_length)
450
Must clear this as we do a memcmp in opt_range.cc to detect
454
buff.append(length-blob_length, '0');
456
return HA_KEY_BLOB_LENGTH+length;
459
void Field_blob::set_key_image(const unsigned char *buff,uint32_t length)
375
461
length= uint2korr(buff);
376
(void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
462
(void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length, field_charset);
381
int Field_blob::key_cmp(const uchar *key_ptr, uint max_key_length)
465
int Field_blob::key_cmp(const unsigned char *key_ptr, uint32_t max_key_length)
384
uint blob_length=get_length(ptr);
467
unsigned char *blob1;
468
uint32_t blob_length=get_length(ptr);
385
469
memcpy(&blob1,ptr+packlength,sizeof(char*));
386
470
const CHARSET_INFO * const cs= charset();
387
uint local_char_length= max_key_length / cs->mbmaxlen;
471
uint32_t local_char_length= max_key_length / cs->mbmaxlen;
388
472
local_char_length= my_charpos(cs, blob1, blob1+blob_length,
389
473
local_char_length);
390
474
set_if_smaller(blob_length, local_char_length);
520
605
@return New pointer into memory based on from + length of the data
522
const uchar *Field_blob::unpack(uchar *to __attribute__((unused)),
607
const unsigned char *Field_blob::unpack(unsigned char *,
608
const unsigned char *from,
525
610
bool low_byte_first)
527
uint const master_packlength=
612
uint32_t const master_packlength=
528
613
param_data > 0 ? param_data & 0xFF : packlength;
529
614
uint32_t const length= get_length(from, master_packlength, low_byte_first);
530
bitmap_set_bit(table->write_set, field_index);
615
table->setWriteSet(field_index);
531
616
store(reinterpret_cast<const char*>(from) + master_packlength,
532
617
length, field_charset);
533
618
return(from + master_packlength + length);
536
/* Keys for blobs are like keys on varchars */
538
int Field_blob::pack_cmp(const uchar *a, const uchar *b, uint key_length_arg,
539
bool insert_or_update)
541
uint a_length, b_length;
542
if (key_length_arg > 255)
544
a_length=uint2korr(a); a+=2;
545
b_length=uint2korr(b); b+=2;
549
a_length= (uint) *a++;
550
b_length= (uint) *b++;
552
return field_charset->coll->strnncollsp(field_charset,
559
int Field_blob::pack_cmp(const uchar *b, uint key_length_arg,
560
bool insert_or_update)
563
uint a_length, b_length;
564
memcpy(&a,ptr+packlength,sizeof(char*));
566
return key_length_arg > 0 ? -1 : 0;
568
a_length= get_length(ptr);
569
if (key_length_arg > 255)
571
b_length= uint2korr(b); b+=2;
574
b_length= (uint) *b++;
575
return field_charset->coll->strnncollsp(field_charset,
581
621
/** Create a packed key that will be used for storage from a MySQL row. */
584
Field_blob::pack_key(uchar *to, const uchar *from, uint max_length,
585
bool low_byte_first __attribute__((unused)))
624
Field_blob::pack_key(unsigned char *to, const unsigned char *from, uint32_t max_length,
627
unsigned char *save= ptr;
628
ptr= (unsigned char*) from;
589
629
uint32_t length=get_length(); // Length of from string
590
uint local_char_length= ((field_charset->mbmaxlen > 1) ?
630
uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
591
631
max_length/field_charset->mbmaxlen : max_length);
593
get_ptr((uchar**) &from);
633
get_ptr((unsigned char**) &from);
594
634
if (length > local_char_length)
595
635
local_char_length= my_charpos(field_charset, from, from+length,
596
636
local_char_length);
597
637
set_if_smaller(length, local_char_length);
598
*to++= (uchar) length;
638
*to++= (unsigned char) length;
599
639
if (max_length > 255) // 2 byte length
600
*to++= (uchar) (length >> 8);
640
*to++= (unsigned char) (length >> 8);
601
641
memcpy(to, from, length);
602
642
ptr=save; // Restore org row pointer
603
643
return to+length;
608
Unpack a blob key into a record buffer.
610
A blob key has a maximum size of 64K-1.
611
In its packed form, the length field is one or two bytes long,
612
depending on 'max_length'.
613
Depending on the maximum length of a blob, its length field is
614
put into 1 to 4 bytes. This is a property of the blob object,
615
described by 'packlength'.
616
Blobs are internally stored apart from the record buffer, which
617
contains a pointer to the blob buffer.
620
@param to Pointer into the record buffer.
621
@param from Pointer to the packed key.
622
@param max_length Key length limit from key description.
625
Pointer into 'from' past the last byte copied from packed key.
629
Field_blob::unpack_key(uchar *to, const uchar *from, uint max_length,
630
bool low_byte_first __attribute__((unused)))
632
/* get length of the blob key */
633
uint32_t length= *from++;
634
if (max_length > 255)
635
length+= *from++ << 8;
637
/* put the length into the record buffer */
638
put_length(to, length);
640
/* put the address of the blob buffer or NULL */
642
memcpy(to + packlength, &from, sizeof(from));
644
memset(to + packlength, 0, sizeof(from));
646
/* point to first byte of next field in 'from' */
647
return from + length;
651
/** Create a packed key that will be used for storage from a MySQL key. */
654
Field_blob::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
655
bool low_byte_first __attribute__((unused)))
657
uint length=uint2korr(from);
658
if (length > max_length)
660
*to++= (char) (length & 255);
661
if (max_length > 255)
662
*to++= (char) (length >> 8);
664
memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
669
uint Field_blob::packed_col_length(const uchar *data_ptr, uint length)
672
return uint2korr(data_ptr)+2;
673
return (uint) *data_ptr + 1;
677
uint Field_blob::max_packed_col_length(uint max_length)
679
return (max_length > 255 ? 2 : 1)+max_length;
683
uint Field_blob::is_equal(Create_field *new_field)
685
if (compare_str_field_flags(new_field, flags))
647
uint32_t Field_blob::is_equal(CreateField *new_field_ptr)
649
if (compare_str_field_flags(new_field_ptr, flags))
651
Field_blob *blob_field_ptr= static_cast<Field_blob *>(new_field_ptr->field);
688
return ((new_field->sql_type == get_blob_type_from_length(max_data_length()))
689
&& new_field->charset == field_charset &&
690
((Field_blob *)new_field->field)->max_data_length() ==
653
return (new_field_ptr->sql_type == DRIZZLE_TYPE_BLOB
654
&& new_field_ptr->charset == field_charset
655
&& blob_field_ptr->max_data_length() == max_data_length());