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