~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/varstring.cc

  • Committer: Brian Aker
  • Date: 2008-07-14 16:24:25 UTC
  • Revision ID: brian@tangent.org-20080714162425-juw3vw221gs9kysh
Cleanup around intptr_t

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 MySQL
5
 
 *
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.
10
 
 *
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.
15
 
 *
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
19
 
 */
20
 
 
21
 
#ifdef USE_PRAGMA_IMPLEMENTATION
22
 
#pragma implementation                          // gcc: Class implementation
23
 
#endif
24
 
 
25
 
#include <drizzled/server_includes.h>
26
 
#include <drizzled/field/varstring.h>
27
 
 
28
 
/****************************************************************************
29
 
  VARCHAR type
30
 
  Data in field->ptr is stored as:
31
 
    1 or 2 bytes length-prefix-header  (from Field_varstring::length_bytes)
32
 
    data
33
 
 
34
 
  NOTE:
35
 
  When VARCHAR is stored in a key (for handler::index_read() etc) it's always
36
 
  stored with a 2 byte prefix. (Just like blob keys).
37
 
 
38
 
  Normally length_bytes is calculated as (field_length < 256 : 1 ? 2)
39
 
  The exception is if there is a prefix key field that is part of a long
40
 
  VARCHAR, in which case field_length for this may be 1 but the length_bytes
41
 
  is 2.
42
 
****************************************************************************/
43
 
 
44
 
const uint32_t Field_varstring::MAX_SIZE= UINT16_MAX;
45
 
 
46
 
/**
47
 
   Save the field metadata for varstring fields.
48
 
 
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).
52
 
 
53
 
   @param   metadata_ptr   First byte of field metadata
54
 
 
55
 
   @returns number of bytes written to metadata_ptr
56
 
*/
57
 
int Field_varstring::do_save_field_metadata(unsigned char *metadata_ptr)
58
 
{
59
 
  char *ptr= (char *)metadata_ptr;
60
 
  assert(field_length <= 65535);
61
 
  int2store(ptr, field_length);
62
 
  return 2;
63
 
}
64
 
 
65
 
int Field_varstring::store(const char *from,uint32_t length, const CHARSET_INFO * const cs)
66
 
{
67
 
  uint32_t copy_length;
68
 
  const char *well_formed_error_pos;
69
 
  const char *cannot_convert_error_pos;
70
 
  const char *from_end_pos;
71
 
 
72
 
  copy_length= well_formed_copy_nchars(field_charset,
73
 
                                       (char*) ptr + length_bytes,
74
 
                                       field_length,
75
 
                                       cs, from, length,
76
 
                                       field_length / field_charset->mbmaxlen,
77
 
                                       &well_formed_error_pos,
78
 
                                       &cannot_convert_error_pos,
79
 
                                       &from_end_pos);
80
 
 
81
 
  if (length_bytes == 1)
82
 
    *ptr= (unsigned char) copy_length;
83
 
  else
84
 
    int2store(ptr, copy_length);
85
 
 
86
 
  if (check_string_copy_error(this, well_formed_error_pos,
87
 
                              cannot_convert_error_pos, from + length, cs))
88
 
    return 2;
89
 
 
90
 
  return report_if_important_data(from_end_pos, from + length);
91
 
}
92
 
 
93
 
 
94
 
int Field_varstring::store(int64_t nr, bool unsigned_val)
95
 
{
96
 
  char buff[64];
97
 
  uint32_t  length;
98
 
  length= (uint) (field_charset->cset->int64_t10_to_str)(field_charset,
99
 
                                                          buff,
100
 
                                                          sizeof(buff),
101
 
                                                          (unsigned_val ? 10:
102
 
                                                           -10),
103
 
                                                           nr);
104
 
  return Field_varstring::store(buff, length, field_charset);
105
 
}
106
 
 
107
 
 
108
 
double Field_varstring::val_real(void)
109
 
{
110
 
  int not_used;
111
 
  char *end_not_used;
112
 
  uint32_t length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
113
 
  return my_strntod(field_charset, (char*) ptr+length_bytes, length,
114
 
                    &end_not_used, &not_used);
115
 
}
116
 
 
117
 
 
118
 
int64_t Field_varstring::val_int(void)
119
 
{
120
 
  int not_used;
121
 
  char *end_not_used;
122
 
  uint32_t length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
123
 
  return my_strntoll(field_charset, (char*) ptr+length_bytes, length, 10,
124
 
                     &end_not_used, &not_used);
125
 
}
126
 
 
127
 
String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
128
 
                                 String *val_ptr)
129
 
{
130
 
  uint32_t length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
131
 
  val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
132
 
  return val_ptr;
133
 
}
134
 
 
135
 
 
136
 
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
137
 
{
138
 
  uint32_t length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
139
 
  str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
140
 
                 charset(), decimal_value);
141
 
  return decimal_value;
142
 
}
143
 
 
144
 
 
145
 
int Field_varstring::cmp_max(const unsigned char *a_ptr, const unsigned char *b_ptr,
146
 
                             uint32_t max_len)
147
 
{
148
 
  uint32_t a_length, b_length;
149
 
  int diff;
150
 
 
151
 
  if (length_bytes == 1)
152
 
  {
153
 
    a_length= (uint) *a_ptr;
154
 
    b_length= (uint) *b_ptr;
155
 
  }
156
 
  else
157
 
  {
158
 
    a_length= uint2korr(a_ptr);
159
 
    b_length= uint2korr(b_ptr);
160
 
  }
161
 
  set_if_smaller(a_length, max_len);
162
 
  set_if_smaller(b_length, max_len);
163
 
  diff= field_charset->coll->strnncollsp(field_charset,
164
 
                                         a_ptr+
165
 
                                         length_bytes,
166
 
                                         a_length,
167
 
                                         b_ptr+
168
 
                                         length_bytes,
169
 
                                         b_length,0);
170
 
  return diff;
171
 
}
172
 
 
173
 
 
174
 
/**
175
 
  @note
176
 
    varstring and blob keys are ALWAYS stored with a 2 byte length prefix
177
 
*/
178
 
 
179
 
int Field_varstring::key_cmp(const unsigned char *key_ptr, uint32_t max_key_length)
180
 
{
181
 
  uint32_t length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
182
 
  uint32_t local_char_length= max_key_length / field_charset->mbmaxlen;
183
 
 
184
 
  local_char_length= my_charpos(field_charset, ptr + length_bytes,
185
 
                          ptr + length_bytes + length, local_char_length);
186
 
  set_if_smaller(length, local_char_length);
187
 
  return field_charset->coll->strnncollsp(field_charset, 
188
 
                                          ptr + length_bytes,
189
 
                                          length,
190
 
                                          key_ptr+
191
 
                                          HA_KEY_BLOB_LENGTH,
192
 
                                          uint2korr(key_ptr), 0);
193
 
}
194
 
 
195
 
 
196
 
/**
197
 
  Compare to key segments (always 2 byte length prefix).
198
 
 
199
 
  @note
200
 
    This is used only to compare key segments created for index_read().
201
 
    (keys are created and compared in key.cc)
202
 
*/
203
 
 
204
 
int Field_varstring::key_cmp(const unsigned char *a,const unsigned char *b)
205
 
{
206
 
  return field_charset->coll->strnncollsp(field_charset,
207
 
                                          a + HA_KEY_BLOB_LENGTH,
208
 
                                          uint2korr(a),
209
 
                                          b + HA_KEY_BLOB_LENGTH,
210
 
                                          uint2korr(b),
211
 
                                          0);
212
 
}
213
 
 
214
 
 
215
 
void Field_varstring::sort_string(unsigned char *to,uint32_t length)
216
 
{
217
 
  uint32_t tot_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
218
 
 
219
 
  if (field_charset == &my_charset_bin)
220
 
  {
221
 
    /* Store length last in high-byte order to sort longer strings first */
222
 
    if (length_bytes == 1)
223
 
      to[length-1]= tot_length;
224
 
    else
225
 
      mi_int2store(to+length-2, tot_length);
226
 
    length-= length_bytes;
227
 
  }
228
 
 
229
 
  tot_length= my_strnxfrm(field_charset,
230
 
                          to, length, ptr + length_bytes,
231
 
                          tot_length);
232
 
  assert(tot_length == length);
233
 
}
234
 
 
235
 
 
236
 
enum ha_base_keytype Field_varstring::key_type() const
237
 
{
238
 
  enum ha_base_keytype res;
239
 
 
240
 
  if (binary())
241
 
    res= length_bytes == 1 ? HA_KEYTYPE_VARBINARY1 : HA_KEYTYPE_VARBINARY2;
242
 
  else
243
 
    res= length_bytes == 1 ? HA_KEYTYPE_VARTEXT1 : HA_KEYTYPE_VARTEXT2;
244
 
  return res;
245
 
}
246
 
 
247
 
 
248
 
void Field_varstring::sql_type(String &res) const
249
 
{
250
 
  const CHARSET_INFO * const cs=res.charset();
251
 
  uint32_t length;
252
 
 
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);
257
 
  res.length(length);
258
 
}
259
 
 
260
 
 
261
 
uint32_t Field_varstring::data_length()
262
 
{
263
 
  return length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
264
 
}
265
 
 
266
 
uint32_t Field_varstring::used_length()
267
 
{
268
 
  return length_bytes == 1 ? 1 + (uint32_t) (unsigned char) *ptr : 2 + uint2korr(ptr);
269
 
}
270
 
 
271
 
/*
272
 
  Functions to create a packed row.
273
 
  Here the number of length bytes are depending on the given max_length
274
 
*/
275
 
 
276
 
unsigned char *Field_varstring::pack(unsigned char *to, const unsigned char *from,
277
 
                             uint32_t max_length,
278
 
                             bool low_byte_first __attribute__((unused)))
279
 
{
280
 
  uint32_t length= length_bytes == 1 ? (uint) *from : uint2korr(from);
281
 
  set_if_smaller(max_length, field_length);
282
 
  if (length > max_length)
283
 
    length=max_length;
284
 
 
285
 
  /* Length always stored little-endian */
286
 
  *to++= length & 0xFF;
287
 
  if (max_length > 255)
288
 
    *to++= (length >> 8) & 0xFF;
289
 
 
290
 
  /* Store bytes of string */
291
 
  if (length > 0)
292
 
    memcpy(to, from+length_bytes, length);
293
 
  return to+length;
294
 
}
295
 
 
296
 
 
297
 
unsigned char *
298
 
Field_varstring::pack_key(unsigned char *to, const unsigned char *key, uint32_t max_length,
299
 
                          bool low_byte_first __attribute__((unused)))
300
 
{
301
 
  uint32_t length=  length_bytes == 1 ? (uint) *key : uint2korr(key);
302
 
  uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
303
 
                     max_length/field_charset->mbmaxlen : max_length);
304
 
  key+= length_bytes;
305
 
  if (length > local_char_length)
306
 
  {
307
 
    local_char_length= my_charpos(field_charset, key, key+length,
308
 
                                  local_char_length);
309
 
    set_if_smaller(length, local_char_length);
310
 
  }
311
 
  *to++= (char) (length & 255);
312
 
  if (max_length > 255)
313
 
    *to++= (char) (length >> 8);
314
 
  if (length)
315
 
    memcpy(to, key, length);
316
 
  return to+length;
317
 
}
318
 
 
319
 
 
320
 
/**
321
 
  Unpack a key into a record buffer.
322
 
 
323
 
  A VARCHAR key has a maximum size of 64K-1.
324
 
  In its packed form, the length field is one or two bytes long,
325
 
  depending on 'max_length'.
326
 
 
327
 
  @param to                          Pointer into the record buffer.
328
 
  @param key                         Pointer to the packed key.
329
 
  @param max_length                  Key length limit from key description.
330
 
 
331
 
  @return
332
 
    Pointer to end of 'key' (To the next key part if multi-segment key)
333
 
*/
334
 
 
335
 
const unsigned char *
336
 
Field_varstring::unpack_key(unsigned char *to __attribute__((unused)),
337
 
                            const unsigned char *key, uint32_t max_length,
338
 
                            bool low_byte_first __attribute__((unused)))
339
 
{
340
 
  /* get length of the blob key */
341
 
  uint32_t length= *key++;
342
 
  if (max_length > 255)
343
 
    length+= (*key++) << 8;
344
 
 
345
 
  /* put the length into the record buffer */
346
 
  if (length_bytes == 1)
347
 
    *ptr= (unsigned char) length;
348
 
  else
349
 
    int2store(ptr, length);
350
 
  memcpy(ptr + length_bytes, key, length);
351
 
  return key + length;
352
 
}
353
 
 
354
 
/**
355
 
  Create a packed key that will be used for storage in the index tree.
356
 
 
357
 
  @param to             Store packed key segment here
358
 
  @param from           Key segment (as given to index_read())
359
 
  @param max_length     Max length of key
360
 
 
361
 
  @return
362
 
    end of key storage
363
 
*/
364
 
 
365
 
unsigned char *
366
 
Field_varstring::pack_key_from_key_image(unsigned char *to, const unsigned char *from, uint32_t max_length,
367
 
                                         bool low_byte_first __attribute__((unused)))
368
 
{
369
 
  /* Key length is always stored as 2 bytes */
370
 
  uint32_t length= uint2korr(from);
371
 
  if (length > max_length)
372
 
    length= max_length;
373
 
  *to++= (char) (length & 255);
374
 
  if (max_length > 255)
375
 
    *to++= (char) (length >> 8);
376
 
  if (length)
377
 
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
378
 
  return to+length;
379
 
}
380
 
 
381
 
 
382
 
/**
383
 
   Unpack a varstring field from row data.
384
 
 
385
 
   This method is used to unpack a varstring field from a master
386
 
   whose size of the field is less than that of the slave.
387
 
 
388
 
   @note
389
 
   The string length is always packed little-endian.
390
 
  
391
 
   @param   to         Destination of the data
392
 
   @param   from       Source of the data
393
 
   @param   param_data Length bytes from the master's field data
394
 
 
395
 
   @return  New pointer into memory based on from + length of the data
396
 
*/
397
 
const unsigned char *
398
 
Field_varstring::unpack(unsigned char *to, const unsigned char *from,
399
 
                        uint32_t param_data,
400
 
                        bool low_byte_first __attribute__((unused)))
401
 
{
402
 
  uint32_t length;
403
 
  uint32_t l_bytes= (param_data && (param_data < field_length)) ? 
404
 
                (param_data <= 255) ? 1 : 2 : length_bytes;
405
 
  if (l_bytes == 1)
406
 
  {
407
 
    to[0]= *from++;
408
 
    length= to[0];
409
 
    if (length_bytes == 2)
410
 
      to[1]= 0;
411
 
  }
412
 
  else /* l_bytes == 2 */
413
 
  {
414
 
    length= uint2korr(from);
415
 
    to[0]= *from++;
416
 
    to[1]= *from++;
417
 
  }
418
 
  if (length)
419
 
    memcpy(to+ length_bytes, from, length);
420
 
  return from+length;
421
 
}
422
 
 
423
 
 
424
 
int Field_varstring::pack_cmp(const unsigned char *a, const unsigned char *b,
425
 
                              uint32_t key_length_arg,
426
 
                              bool insert_or_update)
427
 
{
428
 
  uint32_t a_length, b_length;
429
 
  if (key_length_arg > 255)
430
 
  {
431
 
    a_length=uint2korr(a); a+= 2;
432
 
    b_length=uint2korr(b); b+= 2;
433
 
  }
434
 
  else
435
 
  {
436
 
    a_length= (uint) *a++;
437
 
    b_length= (uint) *b++;
438
 
  }
439
 
  return field_charset->coll->strnncollsp(field_charset,
440
 
                                          a, a_length,
441
 
                                          b, b_length,
442
 
                                          insert_or_update);
443
 
}
444
 
 
445
 
 
446
 
int Field_varstring::pack_cmp(const unsigned char *b, uint32_t key_length_arg,
447
 
                              bool insert_or_update)
448
 
{
449
 
  unsigned char *a= ptr+ length_bytes;
450
 
  uint32_t a_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
451
 
  uint32_t b_length;
452
 
  uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
453
 
                           key_length_arg / field_charset->mbmaxlen :
454
 
                           key_length_arg);
455
 
 
456
 
  if (key_length_arg > 255)
457
 
  {
458
 
    b_length=uint2korr(b); b+= HA_KEY_BLOB_LENGTH;
459
 
  }
460
 
  else
461
 
    b_length= (uint) *b++;
462
 
 
463
 
  if (a_length > local_char_length)
464
 
  {
465
 
    local_char_length= my_charpos(field_charset, a, a+a_length,
466
 
                                  local_char_length);
467
 
    set_if_smaller(a_length, local_char_length);
468
 
  }
469
 
 
470
 
  return field_charset->coll->strnncollsp(field_charset,
471
 
                                          a, a_length,
472
 
                                          b, b_length,
473
 
                                          insert_or_update);
474
 
}
475
 
 
476
 
 
477
 
uint32_t Field_varstring::packed_col_length(const unsigned char *data_ptr, uint32_t length)
478
 
{
479
 
  if (length > 255)
480
 
    return uint2korr(data_ptr)+2;
481
 
  return (uint) *data_ptr + 1;
482
 
}
483
 
 
484
 
 
485
 
uint32_t Field_varstring::max_packed_col_length(uint32_t max_length)
486
 
{
487
 
  return (max_length > 255 ? 2 : 1)+max_length;
488
 
}
489
 
 
490
 
uint32_t Field_varstring::get_key_image(unsigned char *buff,
491
 
                                    uint32_t length,
492
 
                                    imagetype type __attribute__((unused)))
493
 
{
494
 
  uint32_t f_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
495
 
  uint32_t local_char_length= length / field_charset->mbmaxlen;
496
 
  unsigned char *pos= ptr+length_bytes;
497
 
  local_char_length= my_charpos(field_charset, pos, pos + f_length,
498
 
                                local_char_length);
499
 
  set_if_smaller(f_length, local_char_length);
500
 
  /* Key is always stored with 2 bytes */
501
 
  int2store(buff,f_length);
502
 
  memcpy(buff+HA_KEY_BLOB_LENGTH, pos, f_length);
503
 
  if (f_length < length)
504
 
  {
505
 
    /*
506
 
      Must clear this as we do a memcmp in opt_range.cc to detect
507
 
      identical keys
508
 
    */
509
 
    memset(buff+HA_KEY_BLOB_LENGTH+f_length, 0, (length-f_length));
510
 
  }
511
 
  return HA_KEY_BLOB_LENGTH+f_length;
512
 
}
513
 
 
514
 
 
515
 
void Field_varstring::set_key_image(const unsigned char *buff,uint32_t length)
516
 
{
517
 
  length= uint2korr(buff);                      // Real length is here
518
 
  (void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
519
 
                                field_charset);
520
 
}
521
 
 
522
 
 
523
 
int Field_varstring::cmp_binary(const unsigned char *a_ptr, const unsigned char *b_ptr,
524
 
                                uint32_t max_length)
525
 
{
526
 
  uint32_t a_length,b_length;
527
 
 
528
 
  if (length_bytes == 1)
529
 
  {
530
 
    a_length= (uint) *a_ptr;
531
 
    b_length= (uint) *b_ptr;
532
 
  }
533
 
  else
534
 
  {
535
 
    a_length= uint2korr(a_ptr);
536
 
    b_length= uint2korr(b_ptr);
537
 
  }
538
 
  set_if_smaller(a_length, max_length);
539
 
  set_if_smaller(b_length, max_length);
540
 
  if (a_length != b_length)
541
 
    return 1;
542
 
  return memcmp(a_ptr+length_bytes, b_ptr+length_bytes, a_length);
543
 
}
544
 
 
545
 
 
546
 
Field *Field_varstring::new_field(MEM_ROOT *root, Table *new_table, bool keep_type)
547
 
{
548
 
  Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
549
 
                                                            keep_type);
550
 
  if (res)
551
 
    res->length_bytes= length_bytes;
552
 
  return res;
553
 
}
554
 
 
555
 
 
556
 
Field *Field_varstring::new_key_field(MEM_ROOT *root,
557
 
                                      Table *new_table,
558
 
                                      unsigned char *new_ptr, unsigned char *new_null_ptr,
559
 
                                      uint32_t new_null_bit)
560
 
{
561
 
  Field_varstring *res;
562
 
  if ((res= (Field_varstring*) Field::new_key_field(root,
563
 
                                                    new_table,
564
 
                                                    new_ptr,
565
 
                                                    new_null_ptr,
566
 
                                                    new_null_bit)))
567
 
  {
568
 
    /* Keys length prefixes are always packed with 2 bytes */
569
 
    res->length_bytes= 2;
570
 
  }
571
 
  return res;
572
 
}
573
 
 
574
 
 
575
 
uint32_t Field_varstring::is_equal(Create_field *new_field)
576
 
{
577
 
  if (new_field->sql_type == real_type() &&
578
 
      new_field->charset == field_charset)
579
 
  {
580
 
    if (new_field->length == max_display_length())
581
 
      return IS_EQUAL_YES;
582
 
    if (new_field->length > max_display_length() &&
583
 
        ((new_field->length <= 255 && max_display_length() <= 255) ||
584
 
         (new_field->length > 255 && max_display_length() > 255)))
585
 
      return IS_EQUAL_PACK_LENGTH; // VARCHAR, longer variable length
586
 
  }
587
 
  return IS_EQUAL_NO;
588
 
}
589
 
 
590
 
 
591
 
void Field_varstring::hash(uint32_t *nr, uint32_t *nr2)
592
 
{
593
 
  if (is_null())
594
 
  {
595
 
    *nr^= (*nr << 1) | 1;
596
 
  }
597
 
  else
598
 
  {
599
 
    uint32_t len=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
600
 
    const CHARSET_INFO * const cs= charset();
601
 
    cs->coll->hash_sort(cs, ptr + length_bytes, len, nr, nr2);
602
 
  }
603
 
}
604
 
 
605