18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#ifdef USE_PRAGMA_IMPLEMENTATION
22
#pragma implementation // gcc: Class implementation
22
25
#include <drizzled/server_includes.h>
23
26
#include <drizzled/field/blob.h>
24
#include <drizzled/table.h>
25
#include <drizzled/session.h>
32
blob_pack_length_to_max_length(uint32_t arg)
34
return (INT64_C(1) << cmin(arg, 4U) * 8) - INT64_C(1);
28
#define BLOB_PACK_LENGTH_TO_MAX_LENGH(arg) \
29
((uint32_t) ((1LL << min(arg, (uint)4) * 8) - 1LL))
38
31
/****************************************************************************
41
34
** packlength slot and may be from 1-4.
42
35
****************************************************************************/
44
Field_blob::Field_blob(unsigned char *ptr_arg, unsigned char *null_ptr_arg, unsigned char null_bit_arg,
37
Field_blob::Field_blob(uchar *ptr_arg, uchar *null_ptr_arg, uchar null_bit_arg,
45
38
enum utype unireg_check_arg, const char *field_name_arg,
46
TABLE_SHARE *share, uint32_t blob_pack_length,
39
TABLE_SHARE *share, uint blob_pack_length,
47
40
const CHARSET_INFO * const cs)
48
:Field_longstr(ptr_arg, blob_pack_length_to_max_length(blob_pack_length),
41
:Field_longstr(ptr_arg, BLOB_PACK_LENGTH_TO_MAX_LENGH(blob_pack_length),
49
42
null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
51
44
packlength(blob_pack_length)
59
void Field_blob::store_length(unsigned char *i_ptr,
60
uint32_t i_packlength,
52
void Field_blob::store_length(uchar *i_ptr,
62
55
bool low_byte_first __attribute__((unused)))
64
57
switch (i_packlength) {
66
i_ptr[0]= (unsigned char) i_number;
59
i_ptr[0]= (uchar) i_number;
69
62
#ifdef WORDS_BIGENDIAN
94
void Field_blob::store_length(unsigned char *i_ptr, uint32_t i_packlength,
97
store_length(i_ptr, i_packlength, i_number, table->s->db_low_byte_first);
101
uint32_t Field_blob::get_length(const unsigned char *pos,
102
uint32_t packlength_arg,
87
uint32_t Field_blob::get_length(const uchar *pos,
103
89
bool low_byte_first __attribute__((unused)))
105
91
switch (packlength_arg) {
137
uint32_t Field_blob::get_packed_size(const unsigned char *ptr_arg,
140
return packlength + get_length(ptr_arg, packlength, low_byte_first);
144
uint32_t Field_blob::get_length(uint32_t row_offset)
146
return get_length(ptr+row_offset, this->packlength,
147
table->s->db_low_byte_first);
151
uint32_t Field_blob::get_length(const unsigned char *ptr_arg)
153
return get_length(ptr_arg, this->packlength, table->s->db_low_byte_first);
158
124
Put a blob length field into a record buffer.
187
int Field_blob::store(const char *from,uint32_t length, const CHARSET_INFO * const cs)
153
int Field_blob::store(const char *from,uint length, const CHARSET_INFO * const cs)
189
uint32_t copy_length, new_length;
155
uint copy_length, new_length;
190
156
const char *well_formed_error_pos;
191
157
const char *cannot_convert_error_pos;
192
158
const char *from_end_pos, *tmp;
205
171
if (!String::needs_conversion(length, cs, field_charset, &dummy_offset))
207
173
Field_blob::store_length(length);
208
memmove(ptr+packlength, &from, sizeof(char*));
174
memcpy(ptr+packlength, &from, sizeof(char*));
211
177
if (tmpstr.copy(from, length, cs))
226
192
Field_blob::store_length(copy_length);
227
193
tmp= value.ptr();
228
memmove(ptr + packlength, &tmp, sizeof(char*));
194
memcpy(ptr + packlength, &tmp, sizeof(char*));
244
210
Field_blob::store_length(copy_length);
245
211
tmp= value.ptr();
246
memmove(ptr+packlength, &tmp, sizeof(char*));
212
memcpy(ptr+packlength, &tmp, sizeof(char*));
248
214
if (check_string_copy_error(this, well_formed_error_pos,
249
215
cannot_convert_error_pos, from + length, cs))
336
int Field_blob::cmp(const unsigned char *a,uint32_t a_length, const unsigned char *b,
302
int Field_blob::cmp(const uchar *a,uint32_t a_length, const uchar *b,
337
303
uint32_t b_length)
339
return field_charset->coll->strnncollsp(field_charset,
305
return field_charset->coll->strnncollsp(field_charset,
340
306
a, a_length, b, b_length,
345
int Field_blob::cmp_max(const unsigned char *a_ptr, const unsigned char *b_ptr,
311
int Field_blob::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
348
unsigned char *blob1,*blob2;
349
315
memcpy(&blob1,a_ptr+packlength,sizeof(char*));
350
316
memcpy(&blob2,b_ptr+packlength,sizeof(char*));
351
uint32_t a_len= get_length(a_ptr), b_len= get_length(b_ptr);
317
uint a_len= get_length(a_ptr), b_len= get_length(b_ptr);
352
318
set_if_smaller(a_len, max_length);
353
319
set_if_smaller(b_len, max_length);
354
320
return Field_blob::cmp(blob1,a_len,blob2,b_len);
358
int Field_blob::cmp_binary(const unsigned char *a_ptr, const unsigned char *b_ptr,
324
int Field_blob::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
359
325
uint32_t max_length)
363
329
uint32_t a_length,b_length;
364
330
memcpy(&a,a_ptr+packlength,sizeof(char*));
365
331
memcpy(&b,b_ptr+packlength,sizeof(char*));
369
335
b_length=get_length(b_ptr);
370
336
if (b_length > max_length)
371
337
b_length=max_length;
372
diff=memcmp(a,b,cmin(a_length,b_length));
338
diff=memcmp(a,b,min(a_length,b_length));
373
339
return diff ? diff : (int) (a_length - b_length);
377
343
/* The following is used only when comparing a key */
379
uint32_t Field_blob::get_key_image(unsigned char *buff,
345
uint Field_blob::get_key_image(uchar *buff,
347
imagetype type_arg __attribute__((unused)))
383
349
uint32_t blob_length= get_length(ptr);
387
uint32_t local_char_length= length / field_charset->mbmaxlen;
353
uint local_char_length= length / field_charset->mbmaxlen;
388
354
local_char_length= my_charpos(field_charset, blob, blob + blob_length,
389
355
local_char_length);
390
356
set_if_smaller(blob_length, local_char_length);
407
uint32_t Field_blob::get_key_image(basic_string<unsigned char> &buff,
411
uint32_t blob_length= get_length(ptr);
415
uint32_t local_char_length= length / field_charset->mbmaxlen;
416
local_char_length= my_charpos(field_charset, blob, blob + blob_length,
418
set_if_smaller(blob_length, local_char_length);
420
unsigned char len_buff[HA_KEY_BLOB_LENGTH];
421
int2store(len_buff,length);
422
buff.append(len_buff);
423
buff.append(blob, blob_length);
425
if (length > blob_length)
428
Must clear this as we do a memcmp in opt_range.cc to detect
432
buff.append(length-blob_length, '0');
434
return HA_KEY_BLOB_LENGTH+length;
438
void Field_blob::set_key_image(const unsigned char *buff,uint32_t length)
373
void Field_blob::set_key_image(const uchar *buff,uint length)
440
375
length= uint2korr(buff);
441
376
(void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
446
int Field_blob::key_cmp(const unsigned char *key_ptr, uint32_t max_key_length)
381
int Field_blob::key_cmp(const uchar *key_ptr, uint max_key_length)
448
unsigned char *blob1;
449
uint32_t blob_length=get_length(ptr);
384
uint blob_length=get_length(ptr);
450
385
memcpy(&blob1,ptr+packlength,sizeof(char*));
451
386
const CHARSET_INFO * const cs= charset();
452
uint32_t local_char_length= max_key_length / cs->mbmaxlen;
387
uint local_char_length= max_key_length / cs->mbmaxlen;
453
388
local_char_length= my_charpos(cs, blob1, blob1+blob_length,
454
389
local_char_length);
455
390
set_if_smaller(blob_length, local_char_length);
458
393
uint2korr(key_ptr));
461
int Field_blob::key_cmp(const unsigned char *a,const unsigned char *b)
396
int Field_blob::key_cmp(const uchar *a,const uchar *b)
463
398
return Field_blob::cmp(a+HA_KEY_BLOB_LENGTH, uint2korr(a),
464
399
b+HA_KEY_BLOB_LENGTH, uint2korr(b));
476
411
@returns number of bytes written to metadata_ptr
478
int Field_blob::do_save_field_metadata(unsigned char *metadata_ptr)
413
int Field_blob::do_save_field_metadata(uchar *metadata_ptr)
480
415
*metadata_ptr= pack_length_no_ptr();
485
420
uint32_t Field_blob::sort_length() const
487
return (uint32_t) (current_session->variables.max_sort_length +
422
return (uint32_t) (current_thd->variables.max_sort_length +
488
423
(field_charset == &my_charset_bin ? 0 : packlength));
492
void Field_blob::sort_string(unsigned char *to,uint32_t length)
427
void Field_blob::sort_string(uchar *to,uint length)
495
uint32_t blob_length=get_length();
430
uint blob_length=get_length();
497
432
if (!blob_length)
498
433
memset(to, 0, length);
526
461
memcpy(&blob,ptr+packlength,sizeof(char*));
528
463
blob_length=my_strnxfrm(field_charset,
529
464
to, length, blob, blob_length);
530
465
assert(blob_length == length);
546
475
res.set_ascii(STRING_WITH_LEN("text"));
549
unsigned char *Field_blob::pack(unsigned char *to, const unsigned char *from,
550
uint32_t max_length, bool low_byte_first)
478
uchar *Field_blob::pack(uchar *to, const uchar *from,
479
uint max_length, bool low_byte_first)
552
unsigned char *save= ptr;
553
ptr= (unsigned char*) from;
554
483
uint32_t length=get_length(); // Length of from string
558
487
length given is smaller than the actual length of the blob, we
559
488
just store the initial bytes of the blob.
561
store_length(to, packlength, cmin(length, max_length), low_byte_first);
490
store_length(to, packlength, min(length, max_length), low_byte_first);
564
493
Store the actual blob data, which will occupy 'length' bytes.
568
get_ptr((unsigned char**) &from);
497
get_ptr((uchar**) &from);
569
498
memcpy(to+packlength, from,length);
571
500
ptr=save; // Restore org row pointer
577
506
Unpack a blob field from row data.
579
This method is used to unpack a blob field from a master whose size of
508
This method is used to unpack a blob field from a master whose size of
580
509
the field is less than that of the slave. Note: This method is included
581
510
to satisfy inheritance rules, but is not needed for blob fields. It
582
511
simply is used as a pass-through to the original unpack() method for
591
520
@return New pointer into memory based on from + length of the data
593
const unsigned char *Field_blob::unpack(unsigned char *to __attribute__((unused)),
594
const unsigned char *from,
522
const uchar *Field_blob::unpack(uchar *to __attribute__((unused)),
596
525
bool low_byte_first)
598
uint32_t const master_packlength=
527
uint const master_packlength=
599
528
param_data > 0 ? param_data & 0xFF : packlength;
600
529
uint32_t const length= get_length(from, master_packlength, low_byte_first);
601
530
bitmap_set_bit(table->write_set, field_index);
607
536
/* Keys for blobs are like keys on varchars */
609
int Field_blob::pack_cmp(const unsigned char *a, const unsigned char *b, uint32_t key_length_arg,
538
int Field_blob::pack_cmp(const uchar *a, const uchar *b, uint key_length_arg,
610
539
bool insert_or_update)
612
uint32_t a_length, b_length;
541
uint a_length, b_length;
613
542
if (key_length_arg > 255)
615
544
a_length=uint2korr(a); a+=2;
630
int Field_blob::pack_cmp(const unsigned char *b, uint32_t key_length_arg,
559
int Field_blob::pack_cmp(const uchar *b, uint key_length_arg,
631
560
bool insert_or_update)
634
uint32_t a_length, b_length;
563
uint a_length, b_length;
635
564
memcpy(&a,ptr+packlength,sizeof(char*));
637
566
return key_length_arg > 0 ? -1 : 0;
652
581
/** Create a packed key that will be used for storage from a MySQL row. */
655
Field_blob::pack_key(unsigned char *to, const unsigned char *from, uint32_t max_length,
584
Field_blob::pack_key(uchar *to, const uchar *from, uint max_length,
656
585
bool low_byte_first __attribute__((unused)))
658
unsigned char *save= ptr;
659
ptr= (unsigned char*) from;
660
589
uint32_t length=get_length(); // Length of from string
661
uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
590
uint local_char_length= ((field_charset->mbmaxlen > 1) ?
662
591
max_length/field_charset->mbmaxlen : max_length);
664
get_ptr((unsigned char**) &from);
593
get_ptr((uchar**) &from);
665
594
if (length > local_char_length)
666
595
local_char_length= my_charpos(field_charset, from, from+length,
667
596
local_char_length);
668
597
set_if_smaller(length, local_char_length);
669
*to++= (unsigned char) length;
598
*to++= (uchar) length;
670
599
if (max_length > 255) // 2 byte length
671
*to++= (unsigned char) (length >> 8);
600
*to++= (uchar) (length >> 8);
672
601
memcpy(to, from, length);
673
602
ptr=save; // Restore org row pointer
674
603
return to+length;
696
625
Pointer into 'from' past the last byte copied from packed key.
699
const unsigned char *
700
Field_blob::unpack_key(unsigned char *to, const unsigned char *from, uint32_t max_length,
629
Field_blob::unpack_key(uchar *to, const uchar *from, uint max_length,
701
630
bool low_byte_first __attribute__((unused)))
703
632
/* get length of the blob key */
722
651
/** Create a packed key that will be used for storage from a MySQL key. */
725
Field_blob::pack_key_from_key_image(unsigned char *to, const unsigned char *from, uint32_t max_length,
654
Field_blob::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
726
655
bool low_byte_first __attribute__((unused)))
728
uint32_t length=uint2korr(from);
657
uint length=uint2korr(from);
729
658
if (length > max_length)
730
659
length=max_length;
731
660
*to++= (char) (length & 255);
748
uint32_t Field_blob::max_packed_col_length(uint32_t max_length)
677
uint Field_blob::max_packed_col_length(uint max_length)
750
679
return (max_length > 255 ? 2 : 1)+max_length;
754
uint32_t Field_blob::is_equal(Create_field *new_field)
683
uint Field_blob::is_equal(Create_field *new_field)
756
685
if (compare_str_field_flags(new_field, flags))
791
bool Field_blob::in_read_set()
793
return bitmap_is_set(table->read_set, field_index);
797
bool Field_blob::in_write_set()
799
return bitmap_is_set(table->write_set, field_index);