1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 MySQL
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
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/field/varstring.h>
27
/****************************************************************************
29
Data in field->ptr is stored as:
30
1 or 2 bytes length-prefix-header (from Field_varstring::length_bytes)
34
When VARCHAR is stored in a key (for handler::index_read() etc) it's always
35
stored with a 2 byte prefix. (Just like blob keys).
37
Normally length_bytes is calculated as (field_length < 256 : 1 ? 2)
38
The exception is if there is a prefix key field that is part of a long
39
VARCHAR, in which case field_length for this may be 1 but the length_bytes
41
****************************************************************************/
43
const uint Field_varstring::MAX_SIZE= UINT16_MAX;
46
Save the field metadata for varstring fields.
48
Saves the field length in the first byte. Note: may consume
49
2 bytes. Caller must ensure second byte is contiguous with
50
first byte (e.g. array index 0,1).
52
@param metadata_ptr First byte of field metadata
54
@returns number of bytes written to metadata_ptr
56
int Field_varstring::do_save_field_metadata(uchar *metadata_ptr)
58
char *ptr= (char *)metadata_ptr;
59
assert(field_length <= 65535);
60
int2store(ptr, field_length);
64
int Field_varstring::store(const char *from,uint length,CHARSET_INFO *cs)
67
const char *well_formed_error_pos;
68
const char *cannot_convert_error_pos;
69
const char *from_end_pos;
71
copy_length= well_formed_copy_nchars(field_charset,
72
(char*) ptr + length_bytes,
75
field_length / field_charset->mbmaxlen,
76
&well_formed_error_pos,
77
&cannot_convert_error_pos,
80
if (length_bytes == 1)
81
*ptr= (uchar) copy_length;
83
int2store(ptr, copy_length);
85
if (check_string_copy_error(this, well_formed_error_pos,
86
cannot_convert_error_pos, from + length, cs))
89
return report_if_important_data(from_end_pos, from + length);
93
int Field_varstring::store(int64_t nr, bool unsigned_val)
97
length= (uint) (field_charset->cset->int64_t10_to_str)(field_charset,
103
return Field_varstring::store(buff, length, field_charset);
107
double Field_varstring::val_real(void)
111
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
112
return my_strntod(field_charset, (char*) ptr+length_bytes, length,
113
&end_not_used, ¬_used);
117
int64_t Field_varstring::val_int(void)
121
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
122
return my_strntoll(field_charset, (char*) ptr+length_bytes, length, 10,
123
&end_not_used, ¬_used);
126
String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
129
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
130
val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
135
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
137
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
138
str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
139
charset(), decimal_value);
140
return decimal_value;
144
int Field_varstring::cmp_max(const uchar *a_ptr, const uchar *b_ptr,
147
uint a_length, b_length;
150
if (length_bytes == 1)
152
a_length= (uint) *a_ptr;
153
b_length= (uint) *b_ptr;
157
a_length= uint2korr(a_ptr);
158
b_length= uint2korr(b_ptr);
160
set_if_smaller(a_length, max_len);
161
set_if_smaller(b_length, max_len);
162
diff= field_charset->coll->strnncollsp(field_charset,
175
varstring and blob keys are ALWAYS stored with a 2 byte length prefix
178
int Field_varstring::key_cmp(const uchar *key_ptr, uint max_key_length)
180
uint length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
181
uint local_char_length= max_key_length / field_charset->mbmaxlen;
183
local_char_length= my_charpos(field_charset, ptr + length_bytes,
184
ptr + length_bytes + length, local_char_length);
185
set_if_smaller(length, local_char_length);
186
return field_charset->coll->strnncollsp(field_charset,
191
uint2korr(key_ptr), 0);
196
Compare to key segments (always 2 byte length prefix).
199
This is used only to compare key segments created for index_read().
200
(keys are created and compared in key.cc)
203
int Field_varstring::key_cmp(const uchar *a,const uchar *b)
205
return field_charset->coll->strnncollsp(field_charset,
206
a + HA_KEY_BLOB_LENGTH,
208
b + HA_KEY_BLOB_LENGTH,
214
void Field_varstring::sort_string(uchar *to,uint length)
216
uint tot_length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
218
if (field_charset == &my_charset_bin)
220
/* Store length last in high-byte order to sort longer strings first */
221
if (length_bytes == 1)
222
to[length-1]= tot_length;
224
mi_int2store(to+length-2, tot_length);
225
length-= length_bytes;
228
tot_length= my_strnxfrm(field_charset,
229
to, length, ptr + length_bytes,
231
assert(tot_length == length);
235
enum ha_base_keytype Field_varstring::key_type() const
237
enum ha_base_keytype res;
240
res= length_bytes == 1 ? HA_KEYTYPE_VARBINARY1 : HA_KEYTYPE_VARBINARY2;
242
res= length_bytes == 1 ? HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2;
247
void Field_varstring::sql_type(String &res) const
249
THD *thd= table->in_use;
250
CHARSET_INFO *cs=res.charset();
253
length= cs->cset->snprintf(cs,(char*) res.ptr(),
254
res.alloced_length(), "%s(%d)",
255
(has_charset() ? "varchar" : "varbinary"),
256
(int) field_length / charset()->mbmaxlen);
258
if ((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40)) &&
259
has_charset() && (charset()->state & MY_CS_BINSORT))
260
res.append(STRING_WITH_LEN(" binary"));
264
uint32_t Field_varstring::data_length()
266
return length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
269
uint32_t Field_varstring::used_length()
271
return length_bytes == 1 ? 1 + (uint32_t) (uchar) *ptr : 2 + uint2korr(ptr);
275
Functions to create a packed row.
276
Here the number of length bytes are depending on the given max_length
279
uchar *Field_varstring::pack(uchar *to, const uchar *from,
281
bool low_byte_first __attribute__((unused)))
283
uint length= length_bytes == 1 ? (uint) *from : uint2korr(from);
284
set_if_smaller(max_length, field_length);
285
if (length > max_length)
288
/* Length always stored little-endian */
289
*to++= length & 0xFF;
290
if (max_length > 255)
291
*to++= (length >> 8) & 0xFF;
293
/* Store bytes of string */
295
memcpy(to, from+length_bytes, length);
301
Field_varstring::pack_key(uchar *to, const uchar *key, uint max_length,
302
bool low_byte_first __attribute__((unused)))
304
uint length= length_bytes == 1 ? (uint) *key : uint2korr(key);
305
uint local_char_length= ((field_charset->mbmaxlen > 1) ?
306
max_length/field_charset->mbmaxlen : max_length);
308
if (length > local_char_length)
310
local_char_length= my_charpos(field_charset, key, key+length,
312
set_if_smaller(length, local_char_length);
314
*to++= (char) (length & 255);
315
if (max_length > 255)
316
*to++= (char) (length >> 8);
318
memcpy(to, key, length);
324
Unpack a key into a record buffer.
326
A VARCHAR key has a maximum size of 64K-1.
327
In its packed form, the length field is one or two bytes long,
328
depending on 'max_length'.
330
@param to Pointer into the record buffer.
331
@param key Pointer to the packed key.
332
@param max_length Key length limit from key description.
335
Pointer to end of 'key' (To the next key part if multi-segment key)
339
Field_varstring::unpack_key(uchar *to __attribute__((unused)),
340
const uchar *key, uint max_length,
341
bool low_byte_first __attribute__((unused)))
343
/* get length of the blob key */
344
uint32_t length= *key++;
345
if (max_length > 255)
346
length+= (*key++) << 8;
348
/* put the length into the record buffer */
349
if (length_bytes == 1)
350
*ptr= (uchar) length;
352
int2store(ptr, length);
353
memcpy(ptr + length_bytes, key, length);
358
Create a packed key that will be used for storage in the index tree.
360
@param to Store packed key segment here
361
@param from Key segment (as given to index_read())
362
@param max_length Max length of key
369
Field_varstring::pack_key_from_key_image(uchar *to, const uchar *from, uint max_length,
370
bool low_byte_first __attribute__((unused)))
372
/* Key length is always stored as 2 bytes */
373
uint length= uint2korr(from);
374
if (length > max_length)
376
*to++= (char) (length & 255);
377
if (max_length > 255)
378
*to++= (char) (length >> 8);
380
memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
386
Unpack a varstring field from row data.
388
This method is used to unpack a varstring field from a master
389
whose size of the field is less than that of the slave.
392
The string length is always packed little-endian.
394
@param to Destination of the data
395
@param from Source of the data
396
@param param_data Length bytes from the master's field data
398
@return New pointer into memory based on from + length of the data
401
Field_varstring::unpack(uchar *to, const uchar *from,
403
bool low_byte_first __attribute__((unused)))
406
uint l_bytes= (param_data && (param_data < field_length)) ?
407
(param_data <= 255) ? 1 : 2 : length_bytes;
412
if (length_bytes == 2)
415
else /* l_bytes == 2 */
417
length= uint2korr(from);
422
memcpy(to+ length_bytes, from, length);
427
int Field_varstring::pack_cmp(const uchar *a, const uchar *b,
429
my_bool insert_or_update)
431
uint a_length, b_length;
432
if (key_length_arg > 255)
434
a_length=uint2korr(a); a+= 2;
435
b_length=uint2korr(b); b+= 2;
439
a_length= (uint) *a++;
440
b_length= (uint) *b++;
442
return field_charset->coll->strnncollsp(field_charset,
449
int Field_varstring::pack_cmp(const uchar *b, uint key_length_arg,
450
my_bool insert_or_update)
452
uchar *a= ptr+ length_bytes;
453
uint a_length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
455
uint local_char_length= ((field_charset->mbmaxlen > 1) ?
456
key_length_arg / field_charset->mbmaxlen :
459
if (key_length_arg > 255)
461
b_length=uint2korr(b); b+= HA_KEY_BLOB_LENGTH;
464
b_length= (uint) *b++;
466
if (a_length > local_char_length)
468
local_char_length= my_charpos(field_charset, a, a+a_length,
470
set_if_smaller(a_length, local_char_length);
473
return field_charset->coll->strnncollsp(field_charset,
480
uint Field_varstring::packed_col_length(const uchar *data_ptr, uint length)
483
return uint2korr(data_ptr)+2;
484
return (uint) *data_ptr + 1;
488
uint Field_varstring::max_packed_col_length(uint max_length)
490
return (max_length > 255 ? 2 : 1)+max_length;
493
uint Field_varstring::get_key_image(uchar *buff,
495
imagetype type __attribute__((unused)))
497
uint f_length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
498
uint local_char_length= length / field_charset->mbmaxlen;
499
uchar *pos= ptr+length_bytes;
500
local_char_length= my_charpos(field_charset, pos, pos + f_length,
502
set_if_smaller(f_length, local_char_length);
503
/* Key is always stored with 2 bytes */
504
int2store(buff,f_length);
505
memcpy(buff+HA_KEY_BLOB_LENGTH, pos, f_length);
506
if (f_length < length)
509
Must clear this as we do a memcmp in opt_range.cc to detect
512
memset(buff+HA_KEY_BLOB_LENGTH+f_length, 0, (length-f_length));
514
return HA_KEY_BLOB_LENGTH+f_length;
518
void Field_varstring::set_key_image(const uchar *buff,uint length)
520
length= uint2korr(buff); // Real length is here
521
(void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
526
int Field_varstring::cmp_binary(const uchar *a_ptr, const uchar *b_ptr,
529
uint32_t a_length,b_length;
531
if (length_bytes == 1)
533
a_length= (uint) *a_ptr;
534
b_length= (uint) *b_ptr;
538
a_length= uint2korr(a_ptr);
539
b_length= uint2korr(b_ptr);
541
set_if_smaller(a_length, max_length);
542
set_if_smaller(b_length, max_length);
543
if (a_length != b_length)
545
return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
549
Field *Field_varstring::new_field(MEM_ROOT *root, struct st_table *new_table,
552
Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
555
res->length_bytes= length_bytes;
560
Field *Field_varstring::new_key_field(MEM_ROOT *root,
561
struct st_table *new_table,
562
uchar *new_ptr, uchar *new_null_ptr,
565
Field_varstring *res;
566
if ((res= (Field_varstring*) Field::new_key_field(root,
572
/* Keys length prefixes are always packed with 2 bytes */
573
res->length_bytes= 2;
579
uint Field_varstring::is_equal(Create_field *new_field)
581
if (new_field->sql_type == real_type() &&
582
new_field->charset == field_charset)
584
if (new_field->length == max_display_length())
586
if (new_field->length > max_display_length() &&
587
((new_field->length <= 255 && max_display_length() <= 255) ||
588
(new_field->length > 255 && max_display_length() > 255)))
589
return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
595
void Field_varstring::hash(ulong *nr, ulong *nr2)
599
*nr^= (*nr << 1) | 1;
603
uint len= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
604
CHARSET_INFO *cs= charset();
605
cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);