~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/varstring.cc

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
Remove uint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
 
21
#ifdef USE_PRAGMA_IMPLEMENTATION
 
22
#pragma implementation                          // gcc: Class implementation
 
23
#endif
21
24
 
22
 
#include "config.h"
 
25
#include <drizzled/server_includes.h>
23
26
#include <drizzled/field/varstring.h>
24
 
#include <drizzled/table.h>
25
 
#include <drizzled/session.h>
26
 
#include "plugin/myisam/myisam.h"
27
 
 
28
 
#include <string>
29
 
 
30
 
using namespace std;
31
 
 
32
 
namespace drizzled
33
 
{
34
27
 
35
28
/****************************************************************************
36
29
  VARCHAR type
50
43
 
51
44
const uint32_t Field_varstring::MAX_SIZE= UINT16_MAX;
52
45
 
53
 
Field_varstring::Field_varstring(unsigned char *ptr_arg,
54
 
                                 uint32_t len_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
 
                                 TableShare *share,
60
 
                                 const CHARSET_INFO * const cs)
61
 
  :Field_str(ptr_arg,
62
 
             len_arg,
63
 
             null_ptr_arg,
64
 
             null_bit_arg,
65
 
             field_name_arg, cs),
66
 
   length_bytes(length_bytes_arg)
67
 
{
68
 
  share->varchar_fields++;
69
 
}
70
 
 
71
 
Field_varstring::Field_varstring(uint32_t len_arg,
72
 
                                 bool maybe_null_arg,
73
 
                                 const char *field_name_arg,
74
 
                                 TableShare *share,
75
 
                                 const CHARSET_INFO * const cs)
76
 
  :Field_str((unsigned char*) 0,
77
 
             len_arg,
78
 
             maybe_null_arg ? (unsigned char*) "": 0,
79
 
             0,
80
 
             field_name_arg,
81
 
             cs),
82
 
   length_bytes(len_arg < 256 ? 1 :2)
83
 
{
84
 
  share->varchar_fields++;
 
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;
85
63
}
86
64
 
87
65
int Field_varstring::store(const char *from,uint32_t length, const CHARSET_INFO * const cs)
91
69
  const char *cannot_convert_error_pos;
92
70
  const char *from_end_pos;
93
71
 
94
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
95
 
 
96
72
  copy_length= well_formed_copy_nchars(field_charset,
97
73
                                       (char*) ptr + length_bytes,
98
74
                                       field_length,
119
95
{
120
96
  char buff[64];
121
97
  uint32_t  length;
122
 
  length= (uint32_t) (field_charset->cset->int64_t10_to_str)(field_charset,
 
98
  length= (uint) (field_charset->cset->int64_t10_to_str)(field_charset,
123
99
                                                          buff,
124
100
                                                          sizeof(buff),
125
101
                                                          (unsigned_val ? 10:
133
109
{
134
110
  int not_used;
135
111
  char *end_not_used;
136
 
 
137
 
  ASSERT_COLUMN_MARKED_FOR_READ;
138
 
 
139
 
  uint32_t length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
140
 
 
 
112
  uint32_t length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
141
113
  return my_strntod(field_charset, (char*) ptr+length_bytes, length,
142
114
                    &end_not_used, &not_used);
143
115
}
147
119
{
148
120
  int not_used;
149
121
  char *end_not_used;
150
 
  uint32_t length;
151
 
 
152
 
  ASSERT_COLUMN_MARKED_FOR_READ;
153
 
 
154
 
  length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
155
 
 
 
122
  uint32_t length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
156
123
  return my_strntoll(field_charset, (char*) ptr+length_bytes, length, 10,
157
124
                     &end_not_used, &not_used);
158
125
}
159
126
 
160
 
String *Field_varstring::val_str(String *,
 
127
String *Field_varstring::val_str(String *val_buffer __attribute__((unused)),
161
128
                                 String *val_ptr)
162
129
{
163
 
  uint32_t length=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
164
 
 
165
 
  ASSERT_COLUMN_MARKED_FOR_READ;
166
 
 
 
130
  uint32_t length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
167
131
  val_ptr->set((const char*) ptr+length_bytes, length, field_charset);
168
 
 
169
132
  return val_ptr;
170
133
}
171
134
 
172
135
 
173
136
my_decimal *Field_varstring::val_decimal(my_decimal *decimal_value)
174
137
{
175
 
  uint32_t length;
176
 
 
177
 
  ASSERT_COLUMN_MARKED_FOR_READ;
178
 
 
179
 
  length= length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
180
 
 
 
138
  uint32_t length= length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
181
139
  str2my_decimal(E_DEC_FATAL_ERROR, (char*) ptr+length_bytes, length,
182
140
                 charset(), decimal_value);
183
141
  return decimal_value;
192
150
 
193
151
  if (length_bytes == 1)
194
152
  {
195
 
    a_length= (uint32_t) *a_ptr;
196
 
    b_length= (uint32_t) *b_ptr;
 
153
    a_length= (uint) *a_ptr;
 
154
    b_length= (uint) *b_ptr;
197
155
  }
198
156
  else
199
157
  {
220
178
 
221
179
int Field_varstring::key_cmp(const unsigned char *key_ptr, uint32_t max_key_length)
222
180
{
223
 
  uint32_t length=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
 
181
  uint32_t length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
224
182
  uint32_t local_char_length= max_key_length / field_charset->mbmaxlen;
225
183
 
226
184
  local_char_length= my_charpos(field_charset, ptr + length_bytes,
227
185
                          ptr + length_bytes + length, local_char_length);
228
186
  set_if_smaller(length, local_char_length);
229
 
  return field_charset->coll->strnncollsp(field_charset,
 
187
  return field_charset->coll->strnncollsp(field_charset, 
230
188
                                          ptr + length_bytes,
231
189
                                          length,
232
190
                                          key_ptr+
256
214
 
257
215
void Field_varstring::sort_string(unsigned char *to,uint32_t length)
258
216
{
259
 
  uint32_t tot_length=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
 
217
  uint32_t tot_length=  length_bytes == 1 ? (uint) *ptr : uint2korr(ptr);
260
218
 
261
219
  if (field_charset == &my_charset_bin)
262
220
  {
267
225
      mi_int2store(to+length-2, tot_length);
268
226
    length-= length_bytes;
269
227
  }
270
 
 
 
228
 
271
229
  tot_length= my_strnxfrm(field_charset,
272
230
                          to, length, ptr + length_bytes,
273
231
                          tot_length);
300
258
}
301
259
 
302
260
 
 
261
uint32_t Field_varstring::data_length()
 
262
{
 
263
  return length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
 
264
}
 
265
 
303
266
uint32_t Field_varstring::used_length()
304
267
{
305
268
  return length_bytes == 1 ? 1 + (uint32_t) (unsigned char) *ptr : 2 + uint2korr(ptr);
312
275
 
313
276
unsigned char *Field_varstring::pack(unsigned char *to, const unsigned char *from,
314
277
                             uint32_t max_length,
315
 
                             bool )
 
278
                             bool low_byte_first __attribute__((unused)))
316
279
{
317
 
  uint32_t length= length_bytes == 1 ? (uint32_t) *from : uint2korr(from);
 
280
  uint32_t length= length_bytes == 1 ? (uint) *from : uint2korr(from);
318
281
  set_if_smaller(max_length, field_length);
319
282
  if (length > max_length)
320
283
    length=max_length;
331
294
}
332
295
 
333
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
 
334
382
/**
335
383
   Unpack a varstring field from row data.
336
384
 
339
387
 
340
388
   @note
341
389
   The string length is always packed little-endian.
342
 
 
 
390
  
343
391
   @param   to         Destination of the data
344
392
   @param   from       Source of the data
345
393
   @param   param_data Length bytes from the master's field data
349
397
const unsigned char *
350
398
Field_varstring::unpack(unsigned char *to, const unsigned char *from,
351
399
                        uint32_t param_data,
352
 
                        bool )
 
400
                        bool low_byte_first __attribute__((unused)))
353
401
{
354
402
  uint32_t length;
355
 
  uint32_t l_bytes= (param_data && (param_data < field_length)) ?
 
403
  uint32_t l_bytes= (param_data && (param_data < field_length)) ? 
356
404
                (param_data <= 255) ? 1 : 2 : length_bytes;
357
405
  if (l_bytes == 1)
358
406
  {
373
421
}
374
422
 
375
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
 
376
485
uint32_t Field_varstring::max_packed_col_length(uint32_t max_length)
377
486
{
378
487
  return (max_length > 255 ? 2 : 1)+max_length;
379
488
}
380
489
 
381
 
uint32_t Field_varstring::get_key_image(basic_string<unsigned char> &buff, uint32_t length)
382
 
{
383
 
  /* Key is always stored with 2 bytes */
384
 
  const uint32_t key_len= 2;
385
 
  uint32_t f_length=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
386
 
  uint32_t local_char_length= length / field_charset->mbmaxlen;
387
 
  unsigned char *pos= ptr+length_bytes;
388
 
  local_char_length= my_charpos(field_charset, pos, pos + f_length,
389
 
                                local_char_length);
390
 
  set_if_smaller(f_length, local_char_length);
391
 
  unsigned char len_buff[key_len];
392
 
  int2store(len_buff,f_length);
393
 
  buff.append(len_buff);
394
 
  buff.append(pos, f_length);
395
 
  if (f_length < length)
396
 
  {
397
 
    /*
398
 
      Must clear this as we do a memcmp in optimizer/range.cc to detect
399
 
      identical keys
400
 
    */
401
 
    buff.append(length-f_length, 0);
402
 
  }
403
 
  return key_len+f_length;
404
 
}
405
 
 
406
 
 
407
 
uint32_t Field_varstring::get_key_image(unsigned char *buff, uint32_t length)
408
 
{
409
 
  uint32_t f_length=  length_bytes == 1 ? (uint32_t) *ptr : uint2korr(ptr);
 
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);
410
495
  uint32_t local_char_length= length / field_charset->mbmaxlen;
411
496
  unsigned char *pos= ptr+length_bytes;
412
497
  local_char_length= my_charpos(field_charset, pos, pos + f_length,
418
503
  if (f_length < length)
419
504
  {
420
505
    /*
421
 
      Must clear this as we do a memcmp in optimizer/range.cc to detect
 
506
      Must clear this as we do a memcmp in opt_range.cc to detect
422
507
      identical keys
423
508
    */
424
509
    memset(buff+HA_KEY_BLOB_LENGTH+f_length, 0, (length-f_length));
426
511
  return HA_KEY_BLOB_LENGTH+f_length;
427
512
}
428
513
 
429
 
void Field_varstring::set_key_image(const unsigned char *buff, uint32_t length)
 
514
 
 
515
void Field_varstring::set_key_image(const unsigned char *buff,uint32_t length)
430
516
{
431
517
  length= uint2korr(buff);                      // Real length is here
432
 
  (void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length, field_charset);
 
518
  (void) Field_varstring::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
 
519
                                field_charset);
433
520
}
434
521
 
435
 
int Field_varstring::cmp_binary(const unsigned char *a_ptr,
436
 
                                const unsigned char *b_ptr,
 
522
 
 
523
int Field_varstring::cmp_binary(const unsigned char *a_ptr, const unsigned char *b_ptr,
437
524
                                uint32_t max_length)
438
525
{
439
526
  uint32_t a_length,b_length;
440
527
 
441
528
  if (length_bytes == 1)
442
529
  {
443
 
    a_length= (uint32_t) *a_ptr;
444
 
    b_length= (uint32_t) *b_ptr;
 
530
    a_length= (uint) *a_ptr;
 
531
    b_length= (uint) *b_ptr;
445
532
  }
446
533
  else
447
534
  {
456
543
}
457
544
 
458
545
 
459
 
Field *Field_varstring::new_field(memory::Root *root, Table *new_table, bool keep_type)
 
546
Field *Field_varstring::new_field(MEM_ROOT *root, Table *new_table, bool keep_type)
460
547
{
461
548
  Field_varstring *res= (Field_varstring*) Field::new_field(root, new_table,
462
549
                                                            keep_type);
466
553
}
467
554
 
468
555
 
469
 
Field *Field_varstring::new_key_field(memory::Root *root,
 
556
Field *Field_varstring::new_key_field(MEM_ROOT *root,
470
557
                                      Table *new_table,
471
558
                                      unsigned char *new_ptr, unsigned char *new_null_ptr,
472
559
                                      uint32_t new_null_bit)
484
571
  return res;
485
572
}
486
573
 
487
 
} /* namespace drizzled */
 
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