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
#include <drizzled/server_includes.h>
26
23
#include <drizzled/field/varstring.h>
24
#include <drizzled/table.h>
25
#include <drizzled/session.h>
26
#include "plugin/myisam/myisam.h"
28
35
/****************************************************************************
42
49
****************************************************************************/
44
const uint Field_varstring::MAX_SIZE= UINT16_MAX;
47
Save the field metadata for varstring fields.
49
Saves the field length in the first byte. Note: may consume
50
2 bytes. Caller must ensure second byte is contiguous with
51
first byte (e.g. array index 0,1).
53
@param metadata_ptr First byte of field metadata
55
@returns number of bytes written to metadata_ptr
57
int Field_varstring::do_save_field_metadata(uchar *metadata_ptr)
59
char *ptr= (char *)metadata_ptr;
60
assert(field_length <= 65535);
61
int2store(ptr, field_length);
65
int Field_varstring::store(const char *from,uint length, const CHARSET_INFO * const cs)
51
const uint32_t Field_varstring::MAX_SIZE= UINT16_MAX;
53
Field_varstring::Field_varstring(unsigned char *ptr_arg,
55
uint32_t length_bytes_arg,
56
unsigned char *null_ptr_arg,
57
unsigned char null_bit_arg,
58
const char *field_name_arg,
59
const CHARSET_INFO * const cs) :
65
length_bytes(length_bytes_arg)
69
Field_varstring::Field_varstring(uint32_t len_arg,
71
const char *field_name_arg,
72
const CHARSET_INFO * const cs) :
73
Field_str((unsigned char*) 0,
75
maybe_null_arg ? (unsigned char*) "": 0,
79
length_bytes(len_arg < 256 ? 1 :2)
83
int Field_varstring::store(const char *from,uint32_t length, const CHARSET_INFO * const cs)
68
86
const char *well_formed_error_pos;
69
87
const char *cannot_convert_error_pos;
70
88
const char *from_end_pos;
90
ASSERT_COLUMN_MARKED_FOR_WRITE;
72
92
copy_length= well_formed_copy_nchars(field_charset,
73
93
(char*) ptr + length_bytes,
121
144
char *end_not_used;
122
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
147
ASSERT_COLUMN_MARKED_FOR_READ;
149
length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
123
151
return my_strntoll(field_charset, (char*) ptr+length_bytes, length, 10,
124
152
&end_not_used, ¬_used);
127
String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
155
String *Field_varstring::val_str(String *,
130
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
158
uint32_t length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
160
ASSERT_COLUMN_MARKED_FOR_READ;
131
162
val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
136
168
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
138
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
172
ASSERT_COLUMN_MARKED_FOR_READ;
174
length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
139
176
str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
140
177
charset(), decimal_value);
141
178
return decimal_value;
145
int Field_varstring::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
182
int Field_varstring::cmp_max(const unsigned char *a_ptr, const unsigned char *b_ptr,
148
uint a_length, b_length;
185
uint32_t a_length, b_length;
151
188
if (length_bytes == 1)
153
a_length= (uint) *a_ptr;
154
b_length= (uint) *b_ptr;
190
a_length= (uint32_t) *a_ptr;
191
b_length= (uint32_t) *b_ptr;
176
213
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)
216
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;
218
uint32_t length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
219
uint32_t local_char_length= max_key_length / field_charset->mbmaxlen;
184
221
local_char_length= my_charpos(field_charset, ptr + length_bytes,
185
ptr + length_bytes + length, local_char_length);
222
ptr + length_bytes + length, local_char_length);
186
223
set_if_smaller(length, local_char_length);
187
return field_charset->coll->strnncollsp(field_charset,
224
return field_charset->coll->strnncollsp(field_charset,
188
225
ptr + length_bytes,
256
292
(has_charset() ? "varchar" : "varbinary"),
257
293
(int) field_length / charset()->mbmaxlen);
258
294
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"));
265
uint32_t Field_varstring::data_length()
267
return length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
270
298
uint32_t Field_varstring::used_length()
272
return length_bytes == 1 ? 1 + (uint32_t) (uchar) *ptr : 2 + uint2korr(ptr);
300
return length_bytes == 1 ? 1 + (uint32_t) (unsigned char) *ptr : 2 + uint2korr(ptr);
277
305
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)))
308
unsigned char *Field_varstring::pack(unsigned char *to, const unsigned char *from,
284
uint length= length_bytes == 1 ? (uint) *from : uint2korr(from);
312
uint32_t length= length_bytes == 1 ? (uint32_t) *from : uint2korr(from);
285
313
set_if_smaller(max_length, field_length);
286
314
if (length > max_length)
287
315
length=max_length;
302
Field_varstring::pack_key(uchar *to, const uchar *key, uint max_length,
303
bool low_byte_first __attribute__((unused)))
305
uint length= length_bytes == 1 ? (uint) *key : uint2korr(key);
306
uint local_char_length= ((field_charset->mbmaxlen > 1) ?
307
max_length/field_charset->mbmaxlen : max_length);
309
if (length > local_char_length)
311
local_char_length= my_charpos(field_charset, key, key+length,
313
set_if_smaller(length, local_char_length);
315
*to++= (char) (length & 255);
316
if (max_length > 255)
317
*to++= (char) (length >> 8);
319
memcpy(to, key, length);
325
Unpack a key into a record buffer.
327
A VARCHAR key has a maximum size of 64K-1.
328
In its packed form, the length field is one or two bytes long,
329
depending on 'max_length'.
331
@param to Pointer into the record buffer.
332
@param key Pointer to the packed key.
333
@param max_length Key length limit from key description.
336
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)))
344
/* get length of the blob key */
345
uint32_t length= *key++;
346
if (max_length > 255)
347
length+= (*key++) << 8;
349
/* put the length into the record buffer */
350
if (length_bytes == 1)
351
*ptr= (uchar) length;
353
int2store(ptr, length);
354
memcpy(ptr + length_bytes, key, length);
359
Create a packed key that will be used for storage in the index tree.
361
@param to Store packed key segment here
362
@param from Key segment (as given to index_read())
363
@param max_length Max length of key
370
Field_varstring::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
371
bool low_byte_first __attribute__((unused)))
373
/* Key length is always stored as 2 bytes */
374
uint length= uint2korr(from);
375
if (length > max_length)
377
*to++= (char) (length & 255);
378
if (max_length > 255)
379
*to++= (char) (length >> 8);
381
memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
387
330
Unpack a varstring field from row data.
393
336
The string length is always packed little-endian.
395
338
@param to Destination of the data
396
339
@param from Source of the data
397
340
@param param_data Length bytes from the master's field data
399
342
@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)))
344
const unsigned char *
345
Field_varstring::unpack(unsigned char *to, const unsigned char *from,
407
uint l_bytes= (param_data && (param_data < field_length)) ?
350
uint32_t l_bytes= (param_data && (param_data < field_length)) ?
408
351
(param_data <= 255) ? 1 : 2 : length_bytes;
409
352
if (l_bytes == 1)
428
int Field_varstring::pack_cmp(const uchar *a, const uchar *b,
430
bool insert_or_update)
432
uint a_length, b_length;
433
if (key_length_arg > 255)
435
a_length=uint2korr(a); a+= 2;
436
b_length=uint2korr(b); b+= 2;
440
a_length= (uint) *a++;
441
b_length= (uint) *b++;
443
return field_charset->coll->strnncollsp(field_charset,
450
int Field_varstring::pack_cmp(const uchar *b, uint key_length_arg,
451
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) ?
457
key_length_arg / field_charset->mbmaxlen :
460
if (key_length_arg > 255)
462
b_length=uint2korr(b); b+= HA_KEY_BLOB_LENGTH;
465
b_length= (uint) *b++;
467
if (a_length > local_char_length)
469
local_char_length= my_charpos(field_charset, a, a+a_length,
471
set_if_smaller(a_length, local_char_length);
474
return field_charset->coll->strnncollsp(field_charset,
481
uint Field_varstring::packed_col_length(const uchar *data_ptr, uint length)
484
return uint2korr(data_ptr)+2;
485
return (uint) *data_ptr + 1;
489
uint Field_varstring::max_packed_col_length(uint max_length)
371
uint32_t Field_varstring::max_packed_col_length(uint32_t max_length)
491
373
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;
376
uint32_t Field_varstring::get_key_image(basic_string<unsigned char> &buff, uint32_t length)
378
/* Key is always stored with 2 bytes */
379
const uint32_t key_len= 2;
380
uint32_t f_length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
381
uint32_t local_char_length= length / field_charset->mbmaxlen;
382
unsigned char *pos= ptr+length_bytes;
383
local_char_length= my_charpos(field_charset, pos, pos + f_length,
385
set_if_smaller(f_length, local_char_length);
386
unsigned char len_buff[key_len];
387
int2store(len_buff,f_length);
388
buff.append(len_buff);
389
buff.append(pos, f_length);
390
if (f_length < length)
393
Must clear this as we do a memcmp in optimizer/range.cc to detect
396
buff.append(length-f_length, 0);
398
return key_len+f_length;
402
uint32_t Field_varstring::get_key_image(unsigned char *buff, uint32_t length)
404
uint32_t f_length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
405
uint32_t local_char_length= length / field_charset->mbmaxlen;
406
unsigned char *pos= ptr+length_bytes;
501
407
local_char_length= my_charpos(field_charset, pos, pos + f_length,
502
408
local_char_length);
503
409
set_if_smaller(f_length, local_char_length);
515
421
return HA_KEY_BLOB_LENGTH+f_length;
519
void Field_varstring::set_key_image(const uchar *buff,uint length)
424
void Field_varstring::set_key_image(const unsigned char *buff, uint32_t length)
521
426
length= uint2korr(buff); // Real length is here
522
(void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
427
(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,
430
int Field_varstring::cmp_binary(const unsigned char *a_ptr,
431
const unsigned char *b_ptr,
528
432
uint32_t max_length)
530
434
uint32_t a_length,b_length;
532
436
if (length_bytes == 1)
534
a_length= (uint) *a_ptr;
535
b_length= (uint) *b_ptr;
438
a_length= (uint32_t) *a_ptr;
439
b_length= (uint32_t) *b_ptr;
561
Field *Field_varstring::new_key_field(MEM_ROOT *root,
562
struct st_table *new_table,
563
uchar *new_ptr, uchar *new_null_ptr,
464
Field *Field_varstring::new_key_field(memory::Root *root,
466
unsigned char *new_ptr, unsigned char *new_null_ptr,
467
uint32_t new_null_bit)
566
469
Field_varstring *res;
567
470
if ((res= (Field_varstring*) Field::new_key_field(root,
580
uint Field_varstring::is_equal(Create_field *new_field)
582
if (new_field->sql_type == real_type() &&
583
new_field->charset == field_charset)
585
if (new_field->length == max_display_length())
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)))
590
return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
596
void Field_varstring::hash(uint32_t *nr, uint32_t *nr2)
600
*nr^= (*nr << 1) | 1;
604
uint32_t len= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
605
const CHARSET_INFO * const cs= charset();
606
cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);
482
} /* namespace drizzled */