~drizzle-trunk/drizzle/development

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