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,
38
enum utype unireg_check_arg, const char *field_name_arg,
39
TABLE_SHARE *share, uint blob_pack_length,
40
const CHARSET_INFO * const cs)
41
:Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
42
null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
45
Field_blob::Field_blob(unsigned char *ptr_arg,
46
unsigned char *null_ptr_arg,
47
unsigned char null_bit_arg,
48
const char *field_name_arg,
50
uint32_t blob_pack_length,
51
const CHARSET_INFO * const cs)
53
blob_pack_length_to_max_length(blob_pack_length),
44
58
packlength(blob_pack_length)
221
258
/* Fatal OOM error */
222
259
memset(ptr, 0, Field_blob::pack_length());
227
264
int Field_blob::store(double nr)
229
266
const CHARSET_INFO * const cs=charset();
267
ASSERT_COLUMN_MARKED_FOR_WRITE;
230
268
value.set_real(nr, NOT_FIXED_DEC, cs);
231
return Field_blob::store(value.ptr(),(uint) value.length(), cs);
269
return Field_blob::store(value.ptr(),(uint32_t) value.length(), cs);
235
273
int Field_blob::store(int64_t nr, bool unsigned_val)
237
275
const CHARSET_INFO * const cs=charset();
276
ASSERT_COLUMN_MARKED_FOR_WRITE;
238
277
value.set_int(nr, unsigned_val, cs);
239
return Field_blob::store(value.ptr(), (uint) value.length(), cs);
278
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,
352
int Field_blob::cmp(const unsigned char *a,uint32_t a_length, const unsigned char *b,
303
353
uint32_t b_length)
305
return field_charset->coll->strnncollsp(field_charset,
355
return field_charset->coll->strnncollsp(field_charset,
306
356
a, a_length, b, b_length,
311
int Field_blob::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
361
int Field_blob::cmp_max(const unsigned char *a_ptr, const unsigned char *b_ptr,
364
unsigned char *blob1,*blob2;
315
365
memcpy(&blob1,a_ptr+packlength,sizeof(char*));
316
366
memcpy(&blob2,b_ptr+packlength,sizeof(char*));
317
uint a_len= get_length(a_ptr), b_len= get_length(b_ptr);
367
uint32_t a_len= get_length(a_ptr), b_len= get_length(b_ptr);
318
368
set_if_smaller(a_len, max_length);
319
369
set_if_smaller(b_len, max_length);
320
370
return Field_blob::cmp(blob1,a_len,blob2,b_len);
324
int Field_blob::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
374
int Field_blob::cmp_binary(const unsigned char *a_ptr, const unsigned char *b_ptr,
329
379
uint32_t a_length,b_length;
330
380
memcpy(&a,a_ptr+packlength,sizeof(char*));
331
381
memcpy(&b,b_ptr+packlength,sizeof(char*));
332
a_length=get_length(a_ptr);
383
a_length= get_length(a_ptr);
333
385
if (a_length > max_length)
335
b_length=get_length(b_ptr);
386
a_length= max_length;
388
b_length= get_length(b_ptr);
336
390
if (b_length > max_length)
338
diff=memcmp(a,b,min(a_length,b_length));
391
b_length= max_length;
393
diff= memcmp(a,b,min(a_length,b_length));
339
395
return diff ? diff : (int) (a_length - b_length);
343
398
/* The following is used only when comparing a key */
345
uint Field_blob::get_key_image(uchar *buff,
347
imagetype type_arg __attribute__((unused)))
399
uint32_t Field_blob::get_key_image(unsigned char *buff, uint32_t length)
349
401
uint32_t blob_length= get_length(ptr);
353
uint local_char_length= length / field_charset->mbmaxlen;
405
uint32_t local_char_length= length / field_charset->mbmaxlen;
354
406
local_char_length= my_charpos(field_charset, blob, blob + blob_length,
355
407
local_char_length);
356
408
set_if_smaller(blob_length, local_char_length);
373
void Field_blob::set_key_image(const uchar *buff,uint length)
425
uint32_t Field_blob::get_key_image(basic_string<unsigned char> &buff, uint32_t length)
427
uint32_t blob_length= get_length(ptr);
431
uint32_t local_char_length= length / field_charset->mbmaxlen;
432
local_char_length= my_charpos(field_charset, blob, blob + blob_length,
434
set_if_smaller(blob_length, local_char_length);
436
unsigned char len_buff[HA_KEY_BLOB_LENGTH];
437
int2store(len_buff,length);
438
buff.append(len_buff);
439
buff.append(blob, blob_length);
441
if (length > blob_length)
444
Must clear this as we do a memcmp in opt_range.cc to detect
448
buff.append(length-blob_length, '0');
450
return HA_KEY_BLOB_LENGTH+length;
453
void Field_blob::set_key_image(const unsigned char *buff,uint32_t length)
375
455
length= uint2korr(buff);
376
(void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
456
(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)
459
int Field_blob::key_cmp(const unsigned char *key_ptr, uint32_t max_key_length)
384
uint blob_length=get_length(ptr);
461
unsigned char *blob1;
462
uint32_t blob_length=get_length(ptr);
385
463
memcpy(&blob1,ptr+packlength,sizeof(char*));
386
464
const CHARSET_INFO * const cs= charset();
387
uint local_char_length= max_key_length / cs->mbmaxlen;
465
uint32_t local_char_length= max_key_length / cs->mbmaxlen;
388
466
local_char_length= my_charpos(cs, blob1, blob1+blob_length,
389
467
local_char_length);
390
468
set_if_smaller(blob_length, local_char_length);
520
599
@return New pointer into memory based on from + length of the data
522
const uchar *Field_blob::unpack(uchar *to __attribute__((unused)),
601
const unsigned char *Field_blob::unpack(unsigned char *,
602
const unsigned char *from,
525
604
bool low_byte_first)
527
uint const master_packlength=
606
uint32_t const master_packlength=
528
607
param_data > 0 ? param_data & 0xFF : packlength;
529
608
uint32_t const length= get_length(from, master_packlength, low_byte_first);
530
bitmap_set_bit(table->write_set, field_index);
609
table->setWriteSet(field_index);
531
610
store(reinterpret_cast<const char*>(from) + master_packlength,
532
611
length, field_charset);
533
612
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
615
/** 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)))
618
Field_blob::pack_key(unsigned char *to, const unsigned char *from, uint32_t max_length,
621
unsigned char *save= ptr;
622
ptr= (unsigned char*) from;
589
623
uint32_t length=get_length(); // Length of from string
590
uint local_char_length= ((field_charset->mbmaxlen > 1) ?
624
uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
591
625
max_length/field_charset->mbmaxlen : max_length);
593
get_ptr((uchar**) &from);
627
get_ptr((unsigned char**) &from);
594
628
if (length > local_char_length)
595
629
local_char_length= my_charpos(field_charset, from, from+length,
596
630
local_char_length);
597
631
set_if_smaller(length, local_char_length);
598
*to++= (uchar) length;
632
*to++= (unsigned char) length;
599
633
if (max_length > 255) // 2 byte length
600
*to++= (uchar) (length >> 8);
634
*to++= (unsigned char) (length >> 8);
601
635
memcpy(to, from, length);
602
636
ptr=save; // Restore org row pointer
603
637
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))
641
uint32_t Field_blob::is_equal(CreateField *new_field_ptr)
643
if (compare_str_field_flags(new_field_ptr, flags))
645
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() ==
647
return (new_field_ptr->sql_type == DRIZZLE_TYPE_BLOB
648
&& new_field_ptr->charset == field_charset
649
&& blob_field_ptr->max_data_length() == max_data_length());