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
25
22
#include <drizzled/server_includes.h>
26
23
#include <drizzled/field/varstring.h>
24
#include <drizzled/table.h>
25
#include <drizzled/session.h>
28
31
/****************************************************************************
42
45
****************************************************************************/
44
const uint Field_varstring::MAX_SIZE= UINT16_MAX;
47
const uint32_t Field_varstring::MAX_SIZE= UINT16_MAX;
49
Field_varstring::Field_varstring(unsigned char *ptr_arg,
50
uint32_t len_arg, uint32_t length_bytes_arg,
51
unsigned char *null_ptr_arg,
52
unsigned char null_bit_arg,
53
enum utype unireg_check_arg,
54
const char *field_name_arg,
56
const CHARSET_INFO * const cs)
57
:Field_str(ptr_arg, len_arg, null_ptr_arg, null_bit_arg,
58
unireg_check_arg, field_name_arg, cs),
59
length_bytes(length_bytes_arg)
61
share->varchar_fields++;
64
Field_varstring::Field_varstring(uint32_t len_arg,bool maybe_null_arg,
65
const char *field_name_arg,
67
const CHARSET_INFO * const cs)
68
:Field_str((unsigned char*) 0,len_arg,
69
maybe_null_arg ? (unsigned char*) "": 0, 0,
70
NONE, field_name_arg, cs),
71
length_bytes(len_arg < 256 ? 1 :2)
73
share->varchar_fields++;
47
78
Save the field metadata for varstring fields.
55
86
@returns number of bytes written to metadata_ptr
57
int Field_varstring::do_save_field_metadata(uchar *metadata_ptr)
88
int Field_varstring::do_save_field_metadata(unsigned char *metadata_ptr)
59
char *ptr= (char *)metadata_ptr;
60
90
assert(field_length <= 65535);
61
int2store(ptr, field_length);
91
int2store(metadata_ptr, field_length);
65
int Field_varstring::store(const char *from,uint length, const CHARSET_INFO * const cs)
95
int Field_varstring::store(const char *from,uint32_t length, const CHARSET_INFO * const cs)
68
98
const char *well_formed_error_pos;
69
99
const char *cannot_convert_error_pos;
70
100
const char *from_end_pos;
102
ASSERT_COLUMN_MARKED_FOR_WRITE;
72
104
copy_length= well_formed_copy_nchars(field_charset,
73
105
(char*) ptr + length_bytes,
111
143
char *end_not_used;
112
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
145
ASSERT_COLUMN_MARKED_FOR_READ;
147
uint32_t length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
113
149
return my_strntod(field_charset, (char*) ptr+length_bytes, length,
114
150
&end_not_used, ¬_used);
121
157
char *end_not_used;
122
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
160
ASSERT_COLUMN_MARKED_FOR_READ;
162
length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
123
164
return my_strntoll(field_charset, (char*) ptr+length_bytes, length, 10,
124
165
&end_not_used, ¬_used);
127
String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
168
String *Field_varstring::val_str(String *,
130
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
171
uint32_t length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
173
ASSERT_COLUMN_MARKED_FOR_READ;
131
175
val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
136
181
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
138
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
185
ASSERT_COLUMN_MARKED_FOR_READ;
187
length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
139
189
str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
140
190
charset(), decimal_value);
141
191
return decimal_value;
145
int Field_varstring::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
195
int Field_varstring::cmp_max(const unsigned char *a_ptr, const unsigned char *b_ptr,
148
uint a_length, b_length;
198
uint32_t a_length, b_length;
151
201
if (length_bytes == 1)
153
a_length= (uint) *a_ptr;
154
b_length= (uint) *b_ptr;
203
a_length= (uint32_t) *a_ptr;
204
b_length= (uint32_t) *b_ptr;
176
226
varstring and blob keys are ALWAYS stored with a 2 byte length prefix
179
int Field_varstring::key_cmp(const uchar *key_ptr, uint max_key_length)
229
int Field_varstring::key_cmp(const unsigned char *key_ptr, uint32_t max_key_length)
181
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
182
uint local_char_length= max_key_length / field_charset->mbmaxlen;
231
uint32_t length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
232
uint32_t local_char_length= max_key_length / field_charset->mbmaxlen;
184
234
local_char_length= my_charpos(field_charset, ptr + length_bytes,
185
235
ptr + length_bytes + length, local_char_length);
186
236
set_if_smaller(length, local_char_length);
187
return field_charset->coll->strnncollsp(field_charset,
237
return field_charset->coll->strnncollsp(field_charset,
188
238
ptr + length_bytes,
201
251
(keys are created and compared in key.cc)
204
int Field_varstring::key_cmp(const uchar *a,const uchar *b)
254
int Field_varstring::key_cmp(const unsigned char *a,const unsigned char *b)
206
256
return field_charset->coll->strnncollsp(field_charset,
207
257
a + HA_KEY_BLOB_LENGTH,
215
void Field_varstring::sort_string(uchar *to,uint length)
265
void Field_varstring::sort_string(unsigned char *to,uint32_t length)
217
uint tot_length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
267
uint32_t tot_length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
219
269
if (field_charset == &my_charset_bin)
225
275
mi_int2store(to+length-2, tot_length);
226
276
length-= length_bytes;
229
279
tot_length= my_strnxfrm(field_charset,
230
280
to, length, ptr + length_bytes,
256
305
(has_charset() ? "varchar" : "varbinary"),
257
306
(int) field_length / charset()->mbmaxlen);
258
307
res.length(length);
259
if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
260
has_charset() && (charset()->state & MY_CS_BINSORT))
261
res.append(STRING_WITH_LEN(" binary"));
277
323
Here the number of length bytes are depending on the given max_length
280
uchar *Field_varstring::pack(uchar *to, const uchar *from,
282
bool low_byte_first __attribute__((unused)))
326
unsigned char *Field_varstring::pack(unsigned char *to, const unsigned char *from,
284
uint length= length_bytes == 1 ? (uint) *from : uint2korr(from);
330
uint32_t length= length_bytes == 1 ? (uint32_t) *from : uint2korr(from);
285
331
set_if_smaller(max_length, field_length);
286
332
if (length > max_length)
287
333
length=max_length;
302
Field_varstring::pack_key(uchar *to, const uchar *key, uint max_length,
303
bool low_byte_first __attribute__((unused)))
348
Field_varstring::pack_key(unsigned char *to, const unsigned char *key, uint32_t max_length,
305
uint length= length_bytes == 1 ? (uint) *key : uint2korr(key);
306
uint local_char_length= ((field_charset->mbmaxlen > 1) ?
351
uint32_t length= length_bytes == 1 ? (uint32_t) *key : uint2korr(key);
352
uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
307
353
max_length/field_charset->mbmaxlen : max_length);
308
354
key+= length_bytes;
309
355
if (length > local_char_length)
336
382
Pointer to end of 'key' (To the next key part if multi-segment key)
340
Field_varstring::unpack_key(uchar *to __attribute__((unused)),
341
const uchar *key, uint max_length,
342
bool low_byte_first __attribute__((unused)))
385
const unsigned char *
386
Field_varstring::unpack_key(unsigned char *,
387
const unsigned char *key, uint32_t max_length,
344
390
/* get length of the blob key */
345
391
uint32_t length= *key++;
366
412
end of key storage
370
Field_varstring::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
371
bool low_byte_first __attribute__((unused)))
416
Field_varstring::pack_key_from_key_image(unsigned char *to, const unsigned char *from, uint32_t max_length,
373
419
/* Key length is always stored as 2 bytes */
374
uint length= uint2korr(from);
420
uint32_t length= uint2korr(from);
375
421
if (length > max_length)
376
422
length= max_length;
377
423
*to++= (char) (length & 255);
393
439
The string length is always packed little-endian.
395
441
@param to Destination of the data
396
442
@param from Source of the data
397
443
@param param_data Length bytes from the master's field data
399
445
@return New pointer into memory based on from + length of the data
402
Field_varstring::unpack(uchar *to, const uchar *from,
404
bool low_byte_first __attribute__((unused)))
447
const unsigned char *
448
Field_varstring::unpack(unsigned char *to, const unsigned char *from,
407
uint l_bytes= (param_data && (param_data < field_length)) ?
453
uint32_t l_bytes= (param_data && (param_data < field_length)) ?
408
454
(param_data <= 255) ? 1 : 2 : length_bytes;
409
455
if (l_bytes == 1)
428
int Field_varstring::pack_cmp(const uchar *a, const uchar *b,
474
int Field_varstring::pack_cmp(const unsigned char *a, const unsigned char *b,
475
uint32_t key_length_arg,
430
476
bool insert_or_update)
432
uint a_length, b_length;
478
uint32_t a_length, b_length;
433
479
if (key_length_arg > 255)
435
481
a_length=uint2korr(a); a+= 2;
450
int Field_varstring::pack_cmp(const uchar *b, uint key_length_arg,
496
int Field_varstring::pack_cmp(const unsigned char *b, uint32_t key_length_arg,
451
497
bool insert_or_update)
453
uchar *a= ptr+ length_bytes;
454
uint a_length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
456
uint local_char_length= ((field_charset->mbmaxlen > 1) ?
499
unsigned char *a= ptr+ length_bytes;
500
uint32_t a_length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
502
uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
457
503
key_length_arg / field_charset->mbmaxlen :
481
uint Field_varstring::packed_col_length(const uchar *data_ptr, uint length)
527
uint32_t Field_varstring::packed_col_length(const unsigned char *data_ptr, uint32_t length)
483
529
if (length > 255)
484
530
return uint2korr(data_ptr)+2;
485
return (uint) *data_ptr + 1;
531
return (uint32_t) *data_ptr + 1;
489
uint Field_varstring::max_packed_col_length(uint max_length)
535
uint32_t Field_varstring::max_packed_col_length(uint32_t max_length)
491
537
return (max_length > 255 ? 2 : 1)+max_length;
494
uint Field_varstring::get_key_image(uchar *buff,
496
imagetype type __attribute__((unused)))
498
uint f_length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
499
uint local_char_length= length / field_charset->mbmaxlen;
500
uchar *pos= ptr+length_bytes;
540
uint32_t Field_varstring::get_key_image(basic_string<unsigned char> &buff, uint32_t length)
542
/* Key is always stored with 2 bytes */
543
const uint32_t key_len= 2;
544
uint32_t f_length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
545
uint32_t local_char_length= length / field_charset->mbmaxlen;
546
unsigned char *pos= ptr+length_bytes;
547
local_char_length= my_charpos(field_charset, pos, pos + f_length,
549
set_if_smaller(f_length, local_char_length);
550
unsigned char len_buff[key_len];
551
int2store(len_buff,f_length);
552
buff.append(len_buff);
553
buff.append(pos, f_length);
554
if (f_length < length)
557
Must clear this as we do a memcmp in opt_range.cc to detect
560
buff.append(length-f_length, 0);
562
return key_len+f_length;
566
uint32_t Field_varstring::get_key_image(unsigned char *buff, uint32_t length)
568
uint32_t f_length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
569
uint32_t local_char_length= length / field_charset->mbmaxlen;
570
unsigned char *pos= ptr+length_bytes;
501
571
local_char_length= my_charpos(field_charset, pos, pos + f_length,
502
572
local_char_length);
503
573
set_if_smaller(f_length, local_char_length);
515
585
return HA_KEY_BLOB_LENGTH+f_length;
519
void Field_varstring::set_key_image(const uchar *buff,uint length)
588
void Field_varstring::set_key_image(const unsigned char *buff, uint32_t length)
521
590
length= uint2korr(buff); // Real length is here
522
(void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
591
(void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length, field_charset);
527
int Field_varstring::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
594
int Field_varstring::cmp_binary(const unsigned char *a_ptr,
595
const unsigned char *b_ptr,
528
596
uint32_t max_length)
530
598
uint32_t a_length,b_length;
532
600
if (length_bytes == 1)
534
a_length= (uint) *a_ptr;
535
b_length= (uint) *b_ptr;
602
a_length= (uint32_t) *a_ptr;
603
b_length= (uint32_t) *b_ptr;
550
Field *Field_varstring::new_field(MEM_ROOT *root, struct st_table *new_table,
618
Field *Field_varstring::new_field(MEM_ROOT *root, Table *new_table, bool keep_type)
553
620
Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
561
628
Field *Field_varstring::new_key_field(MEM_ROOT *root,
562
struct st_table *new_table,
563
uchar *new_ptr, uchar *new_null_ptr,
630
unsigned char *new_ptr, unsigned char *new_null_ptr,
631
uint32_t new_null_bit)
566
633
Field_varstring *res;
567
634
if ((res= (Field_varstring*) Field::new_key_field(root,
580
uint Field_varstring::is_equal(Create_field *new_field)
647
uint32_t Field_varstring::is_equal(CreateField *new_field_ptr)
582
if (new_field->sql_type == real_type() &&
583
new_field->charset == field_charset)
649
if (new_field_ptr->sql_type == real_type() &&
650
new_field_ptr->charset == field_charset)
585
if (new_field->length == max_display_length())
652
if (new_field_ptr->length == max_display_length())
586
653
return IS_EQUAL_YES;
587
if (new_field->length > max_display_length() &&
588
((new_field->length <= 255 && max_display_length() <= 255) ||
589
(new_field->length > 255 && max_display_length() > 255)))
654
if (new_field_ptr->length > max_display_length() &&
655
((new_field_ptr->length <= 255 && max_display_length() <= 255) ||
656
(new_field_ptr->length > 255 && max_display_length() > 255)))
590
657
return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
592
659
return IS_EQUAL_NO;
604
uint32_t len= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
671
uint32_t len= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
605
672
const CHARSET_INFO * const cs= charset();
606
673
cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);