22
#include <drizzled/server_includes.h>
23
23
#include <drizzled/field/blob.h>
24
#include <drizzled/table.h>
25
#include <drizzled/session.h>
26
#include "plugin/myisam/myisam.h"
36
static uint32_t blob_pack_length_to_max_length(uint32_t arg)
38
return max(UINT32_MAX,
39
(uint32_t)((INT64_C(1) << min(arg, 4U) * 8) - INT64_C(1)));
26
blob_pack_length_to_max_length(uint32_t arg)
28
return (INT64_C(1) << cmin(arg, 4U) * 8) - INT64_C(1);
46
35
** packlength slot and may be from 1-4.
47
36
****************************************************************************/
49
Field_blob::Field_blob(unsigned char *ptr_arg,
50
unsigned char *null_ptr_arg,
51
unsigned char null_bit_arg,
52
const char *field_name_arg,
54
uint32_t blob_pack_length,
55
const CHARSET_INFO * const cs)
57
blob_pack_length_to_max_length(blob_pack_length),
38
Field_blob::Field_blob(unsigned char *ptr_arg, unsigned char *null_ptr_arg, unsigned char null_bit_arg,
39
enum utype unireg_check_arg, const char *field_name_arg,
40
TABLE_SHARE *share, uint32_t blob_pack_length,
41
const CHARSET_INFO * const cs)
42
:Field_longstr(ptr_arg, blob_pack_length_to_max_length(blob_pack_length),
43
null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
62
45
packlength(blob_pack_length)
107
void Field_blob::store_length(unsigned char *i_ptr, uint32_t i_packlength,
110
store_length(i_ptr, i_packlength, i_number, table->s->db_low_byte_first);
114
88
uint32_t Field_blob::get_length(const unsigned char *pos,
115
uint32_t packlength_arg,
89
uint32_t packlength_arg,
90
bool low_byte_first __attribute__((unused)))
118
#ifndef WORDS_BIGENDIAN
119
(void)low_byte_first;
121
92
switch (packlength_arg) {
123
94
return (uint32_t) pos[0];
153
uint32_t Field_blob::get_packed_size(const unsigned char *ptr_arg,
156
return packlength + get_length(ptr_arg, packlength, low_byte_first);
160
uint32_t Field_blob::get_length(uint32_t row_offset)
162
return get_length(ptr+row_offset, this->packlength,
163
table->s->db_low_byte_first);
167
uint32_t Field_blob::get_length(const unsigned char *ptr_arg)
169
return get_length(ptr_arg, this->packlength, table->s->db_low_byte_first);
174
125
Put a blob length field into a record buffer.
223
172
if (!String::needs_conversion(length, cs, field_charset, &dummy_offset))
225
174
Field_blob::store_length(length);
226
memmove(ptr+packlength, &from, sizeof(char*));
175
memcpy(ptr+packlength, &from, sizeof(char*));
229
178
if (tmpstr.copy(from, length, cs))
231
180
from= tmpstr.ptr();
234
new_length= min(max_data_length(), field_charset->mbmaxlen * length);
183
new_length= cmin(max_data_length(), field_charset->mbmaxlen * length);
235
184
if (value.alloc(new_length))
188
if (f_is_hex_escape(flags))
190
copy_length= my_copy_with_hex_escaping(field_charset,
191
(char*) value.ptr(), new_length,
193
Field_blob::store_length(copy_length);
195
memcpy(ptr + packlength, &tmp, sizeof(char*));
239
199
"length" is OK as "nchars" argument to well_formed_copy_nchars as this
240
200
is never used to limit the length of the data. The cut of long data
251
211
Field_blob::store_length(copy_length);
252
212
tmp= value.ptr();
253
memmove(ptr+packlength, &tmp, sizeof(char*));
213
memcpy(ptr+packlength, &tmp, sizeof(char*));
255
215
if (check_string_copy_error(this, well_formed_error_pos,
256
216
cannot_convert_error_pos, from + length, cs))
262
222
/* Fatal OOM error */
263
223
memset(ptr, 0, Field_blob::pack_length());
268
228
int Field_blob::store(double nr)
270
230
const CHARSET_INFO * const cs=charset();
271
ASSERT_COLUMN_MARKED_FOR_WRITE;
272
231
value.set_real(nr, NOT_FIXED_DEC, cs);
273
return Field_blob::store(value.ptr(),(uint32_t) value.length(), cs);
232
return Field_blob::store(value.ptr(),(uint) value.length(), cs);
277
236
int Field_blob::store(int64_t nr, bool unsigned_val)
279
238
const CHARSET_INFO * const cs=charset();
280
ASSERT_COLUMN_MARKED_FOR_WRITE;
281
239
value.set_int(nr, unsigned_val, cs);
282
return Field_blob::store(value.ptr(), (uint32_t) value.length(), cs);
240
return Field_blob::store(value.ptr(), (uint) value.length(), cs);
315
268
return my_strntoll(charset(),blob,length,10,NULL,¬_used);
318
String *Field_blob::val_str(String *,
271
String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
323
ASSERT_COLUMN_MARKED_FOR_READ;
325
275
memcpy(&blob,ptr+packlength,sizeof(char*));
327
277
val_ptr->set("",0,charset()); // A bit safer than ->length(0)
356
303
int Field_blob::cmp(const unsigned char *a,uint32_t a_length, const unsigned char *b,
357
304
uint32_t b_length)
359
return field_charset->coll->strnncollsp(field_charset,
306
return field_charset->coll->strnncollsp(field_charset,
360
307
a, a_length, b, b_length,
378
325
int Field_blob::cmp_binary(const unsigned char *a_ptr, const unsigned char *b_ptr,
383
330
uint32_t a_length,b_length;
384
331
memcpy(&a,a_ptr+packlength,sizeof(char*));
385
332
memcpy(&b,b_ptr+packlength,sizeof(char*));
387
a_length= get_length(a_ptr);
333
a_length=get_length(a_ptr);
389
334
if (a_length > max_length)
390
a_length= max_length;
392
b_length= get_length(b_ptr);
336
b_length=get_length(b_ptr);
394
337
if (b_length > max_length)
395
b_length= max_length;
397
diff= memcmp(a,b,min(a_length,b_length));
339
diff=memcmp(a,b,cmin(a_length,b_length));
399
340
return diff ? diff : (int) (a_length - b_length);
402
344
/* The following is used only when comparing a key */
403
uint32_t Field_blob::get_key_image(unsigned char *buff, uint32_t length)
346
uint32_t Field_blob::get_key_image(unsigned char *buff,
348
imagetype type_arg __attribute__((unused)))
405
350
uint32_t blob_length= get_length(ptr);
406
351
unsigned char *blob;
414
359
if ((uint32_t) length > blob_length)
417
Must clear this as we do a memcmp in optimizer/range.cc to detect
362
Must clear this as we do a memcmp in opt_range.cc to detect
420
365
memset(buff+HA_KEY_BLOB_LENGTH+blob_length, 0, (length-blob_length));
421
length=(uint32_t) blob_length;
366
length=(uint) blob_length;
423
368
int2store(buff,length);
424
369
memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
429
uint32_t Field_blob::get_key_image(basic_string<unsigned char> &buff, uint32_t length)
431
uint32_t blob_length= get_length(ptr);
435
uint32_t local_char_length= length / field_charset->mbmaxlen;
436
local_char_length= my_charpos(field_charset, blob, blob + blob_length,
438
set_if_smaller(blob_length, local_char_length);
440
unsigned char len_buff[HA_KEY_BLOB_LENGTH];
441
int2store(len_buff,length);
442
buff.append(len_buff);
443
buff.append(blob, blob_length);
445
if (length > blob_length)
448
Must clear this as we do a memcmp in optimizer/range.cc to detect
452
buff.append(length-blob_length, '0');
454
return HA_KEY_BLOB_LENGTH+length;
457
374
void Field_blob::set_key_image(const unsigned char *buff,uint32_t length)
459
376
length= uint2korr(buff);
460
(void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length, field_charset);
377
(void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
463
382
int Field_blob::key_cmp(const unsigned char *key_ptr, uint32_t max_key_length)
465
384
unsigned char *blob1;
481
400
b+HA_KEY_BLOB_LENGTH, uint2korr(b));
405
Save the field metadata for blob fields.
407
Saves the pack length in the first byte of the field metadata array
408
at index of *metadata_ptr.
410
@param metadata_ptr First byte of field metadata
412
@returns number of bytes written to metadata_ptr
414
int Field_blob::do_save_field_metadata(unsigned char *metadata_ptr)
416
*metadata_ptr= pack_length_no_ptr();
484
421
uint32_t Field_blob::sort_length() const
486
return (uint32_t) (current_session->variables.max_sort_length +
423
return (uint32_t) (current_session->variables.max_sort_length +
487
424
(field_charset == &my_charset_bin ? 0 : packlength));
490
428
void Field_blob::sort_string(unsigned char *to,uint32_t length)
492
430
unsigned char *blob;
524
462
memcpy(&blob,ptr+packlength,sizeof(char*));
526
464
blob_length=my_strnxfrm(field_charset,
527
465
to, length, blob, blob_length);
528
466
assert(blob_length == length);
532
uint32_t Field_blob::pack_length() const
534
return (uint32_t) (packlength+table->s->blob_ptr_size);
537
471
void Field_blob::sql_type(String &res) const
545
479
unsigned char *Field_blob::pack(unsigned char *to, const unsigned char *from,
546
uint32_t max_length, bool low_byte_first)
480
uint32_t max_length, bool low_byte_first)
548
482
unsigned char *save= ptr;
549
483
ptr= (unsigned char*) from;
550
uint32_t length= get_length(); // Length of from string
484
uint32_t length=get_length(); // Length of from string
553
487
Store max length, which will occupy packlength bytes. If the max
554
488
length given is smaller than the actual length of the blob, we
555
489
just store the initial bytes of the blob.
557
store_length(to, packlength, min(length, max_length), low_byte_first);
491
store_length(to, packlength, cmin(length, max_length), low_byte_first);
560
494
Store the actual blob data, which will occupy 'length' bytes.
564
498
get_ptr((unsigned char**) &from);
565
499
memcpy(to+packlength, from,length);
568
ptr= save; // Restore org row pointer
501
ptr=save; // Restore org row pointer
569
502
return(to+packlength+length);
573
507
Unpack a blob field from row data.
575
This method is used to unpack a blob field from a master whose size of
509
This method is used to unpack a blob field from a master whose size of
576
510
the field is less than that of the slave. Note: This method is included
577
511
to satisfy inheritance rules, but is not needed for blob fields. It
578
512
simply is used as a pass-through to the original unpack() method for
587
521
@return New pointer into memory based on from + length of the data
589
const unsigned char *Field_blob::unpack(unsigned char *,
590
const unsigned char *from,
523
const unsigned char *Field_blob::unpack(unsigned char *to __attribute__((unused)),
524
const unsigned char *from,
594
528
uint32_t const master_packlength=
595
529
param_data > 0 ? param_data & 0xFF : packlength;
596
530
uint32_t const length= get_length(from, master_packlength, low_byte_first);
597
table->setWriteSet(field_index);
531
bitmap_set_bit(table->write_set, field_index);
598
532
store(reinterpret_cast<const char*>(from) + master_packlength,
599
533
length, field_charset);
600
534
return(from + master_packlength + length);
537
/* Keys for blobs are like keys on varchars */
539
int Field_blob::pack_cmp(const unsigned char *a, const unsigned char *b, uint32_t key_length_arg,
540
bool insert_or_update)
542
uint32_t a_length, b_length;
543
if (key_length_arg > 255)
545
a_length=uint2korr(a); a+=2;
546
b_length=uint2korr(b); b+=2;
550
a_length= (uint) *a++;
551
b_length= (uint) *b++;
553
return field_charset->coll->strnncollsp(field_charset,
560
int Field_blob::pack_cmp(const unsigned char *b, uint32_t key_length_arg,
561
bool insert_or_update)
564
uint32_t a_length, b_length;
565
memcpy(&a,ptr+packlength,sizeof(char*));
567
return key_length_arg > 0 ? -1 : 0;
569
a_length= get_length(ptr);
570
if (key_length_arg > 255)
572
b_length= uint2korr(b); b+=2;
575
b_length= (uint) *b++;
576
return field_charset->coll->strnncollsp(field_charset,
603
582
/** Create a packed key that will be used for storage from a MySQL row. */
606
585
Field_blob::pack_key(unsigned char *to, const unsigned char *from, uint32_t max_length,
586
bool low_byte_first __attribute__((unused)))
609
588
unsigned char *save= ptr;
610
589
ptr= (unsigned char*) from;
609
Unpack a blob key into a record buffer.
611
A blob key has a maximum size of 64K-1.
612
In its packed form, the length field is one or two bytes long,
613
depending on 'max_length'.
614
Depending on the maximum length of a blob, its length field is
615
put into 1 to 4 bytes. This is a property of the blob object,
616
described by 'packlength'.
617
Blobs are internally stored apart from the record buffer, which
618
contains a pointer to the blob buffer.
621
@param to Pointer into the record buffer.
622
@param from Pointer to the packed key.
623
@param max_length Key length limit from key description.
626
Pointer into 'from' past the last byte copied from packed key.
629
const unsigned char *
630
Field_blob::unpack_key(unsigned char *to, const unsigned char *from, uint32_t max_length,
631
bool low_byte_first __attribute__((unused)))
633
/* get length of the blob key */
634
uint32_t length= *from++;
635
if (max_length > 255)
636
length+= *from++ << 8;
638
/* put the length into the record buffer */
639
put_length(to, length);
641
/* put the address of the blob buffer or NULL */
643
memcpy(to + packlength, &from, sizeof(from));
645
memset(to + packlength, 0, sizeof(from));
647
/* point to first byte of next field in 'from' */
648
return from + length;
652
/** Create a packed key that will be used for storage from a MySQL key. */
655
Field_blob::pack_key_from_key_image(unsigned char *to, const unsigned char *from, uint32_t max_length,
656
bool low_byte_first __attribute__((unused)))
658
uint32_t length=uint2korr(from);
659
if (length > max_length)
661
*to++= (char) (length & 255);
662
if (max_length > 255)
663
*to++= (char) (length >> 8);
665
memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
670
uint32_t Field_blob::packed_col_length(const unsigned char *data_ptr, uint32_t length)
673
return uint2korr(data_ptr)+2;
674
return (uint) *data_ptr + 1;
678
uint32_t Field_blob::max_packed_col_length(uint32_t max_length)
680
return (max_length > 255 ? 2 : 1)+max_length;
684
uint32_t Field_blob::is_equal(Create_field *new_field)
686
if (compare_str_field_flags(new_field, flags))
689
return ((new_field->sql_type == get_blob_type_from_length(max_data_length()))
690
&& new_field->charset == field_charset &&
691
((Field_blob *)new_field->field)->max_data_length() ==
630
697
maximum possible display length for blob.