~drizzle-trunk/drizzle/development

520.1.25 by Monty Taylor
Removed the split.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
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/blob.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>
26
656.1.1 by Monty Taylor
OOOh doggie. Got rid of my_alloca.
27
#include <string>
28
29
using namespace std;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
30
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
31
uint32_t
482 by Brian Aker
Remove uint.
32
blob_pack_length_to_max_length(uint32_t arg)
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
33
{
422 by Monty
Various int64 constant fixes.
34
  return (INT64_C(1) << cmin(arg, 4U) * 8) - INT64_C(1);
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
35
}
36
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
37
38
/****************************************************************************
39
** blob type
40
** A blob is saved as a length and a pointer. The length is stored in the
41
** packlength slot and may be from 1-4.
42
****************************************************************************/
43
481 by Brian Aker
Remove all of uchar.
44
Field_blob::Field_blob(unsigned char *ptr_arg, unsigned char *null_ptr_arg, unsigned char null_bit_arg,
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
45
		       enum utype unireg_check_arg, const char *field_name_arg,
482 by Brian Aker
Remove uint.
46
                       TABLE_SHARE *share, uint32_t blob_pack_length,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
47
		       const CHARSET_INFO * const cs)
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
48
  :Field_longstr(ptr_arg, blob_pack_length_to_max_length(blob_pack_length),
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
49
                 null_ptr_arg, null_bit_arg, unireg_check_arg, field_name_arg,
50
                 cs),
51
   packlength(blob_pack_length)
52
{
53
  flags|= BLOB_FLAG;
54
  share->blob_fields++;
55
  /* TODO: why do not fill table->s->blob_field array here? */
56
}
57
58
481 by Brian Aker
Remove all of uchar.
59
void Field_blob::store_length(unsigned char *i_ptr,
482 by Brian Aker
Remove uint.
60
                              uint32_t i_packlength,
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
61
                              uint32_t i_number,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
62
                              bool low_byte_first __attribute__((unused)))
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
63
{
64
  switch (i_packlength) {
65
  case 1:
481 by Brian Aker
Remove all of uchar.
66
    i_ptr[0]= (unsigned char) i_number;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
67
    break;
68
  case 2:
69
#ifdef WORDS_BIGENDIAN
70
    if (low_byte_first)
71
    {
72
      int2store(i_ptr,(unsigned short) i_number);
73
    }
74
    else
75
#endif
76
      shortstore(i_ptr,(unsigned short) i_number);
77
    break;
78
  case 3:
79
    int3store(i_ptr,i_number);
80
    break;
81
  case 4:
82
#ifdef WORDS_BIGENDIAN
83
    if (low_byte_first)
84
    {
85
      int4store(i_ptr,i_number);
86
    }
87
    else
88
#endif
89
      longstore(i_ptr,i_number);
90
  }
91
}
92
93
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
94
void Field_blob::store_length(unsigned char *i_ptr, uint32_t i_packlength,
95
                  uint32_t i_number)
96
{
97
  store_length(i_ptr, i_packlength, i_number, table->s->db_low_byte_first);
98
}
99
100
481 by Brian Aker
Remove all of uchar.
101
uint32_t Field_blob::get_length(const unsigned char *pos,
482 by Brian Aker
Remove uint.
102
                              uint32_t packlength_arg,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
103
                              bool low_byte_first __attribute__((unused)))
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
104
{
105
  switch (packlength_arg) {
106
  case 1:
107
    return (uint32_t) pos[0];
108
  case 2:
109
    {
206 by Brian Aker
Removed final uint dead types.
110
      uint16_t tmp;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
111
#ifdef WORDS_BIGENDIAN
112
      if (low_byte_first)
113
	tmp=sint2korr(pos);
114
      else
115
#endif
116
	shortget(tmp,pos);
117
      return (uint32_t) tmp;
118
    }
119
  case 3:
120
    return (uint32_t) uint3korr(pos);
121
  case 4:
122
    {
123
      uint32_t tmp;
124
#ifdef WORDS_BIGENDIAN
125
      if (low_byte_first)
126
	tmp=uint4korr(pos);
127
      else
128
#endif
129
	longget(tmp,pos);
130
      return (uint32_t) tmp;
131
    }
132
  }
133
  return 0;					// Impossible
134
}
135
136
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
137
uint32_t Field_blob::get_packed_size(const unsigned char *ptr_arg,
138
                                bool low_byte_first)
139
{
140
  return packlength + get_length(ptr_arg, packlength, low_byte_first);
141
}
142
143
144
uint32_t Field_blob::get_length(uint32_t row_offset)
145
{
146
  return get_length(ptr+row_offset, this->packlength,
147
                    table->s->db_low_byte_first);
148
}
149
150
151
uint32_t Field_blob::get_length(const unsigned char *ptr_arg)
152
{
153
  return get_length(ptr_arg, this->packlength, table->s->db_low_byte_first);
154
}
155
156
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
157
/**
158
  Put a blob length field into a record buffer.
159
160
  Depending on the maximum length of a blob, its length field is
161
  put into 1 to 4 bytes. This is a property of the blob object,
162
  described by 'packlength'.
163
164
  @param pos                 Pointer into the record buffer.
165
  @param length              The length value to put.
166
*/
167
481 by Brian Aker
Remove all of uchar.
168
void Field_blob::put_length(unsigned char *pos, uint32_t length)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
169
{
170
  switch (packlength) {
171
  case 1:
172
    *pos= (char) length;
173
    break;
174
  case 2:
175
    int2store(pos, length);
176
    break;
177
  case 3:
178
    int3store(pos, length);
179
    break;
180
  case 4:
181
    int4store(pos, length);
182
    break;
183
  }
184
}
185
186
482 by Brian Aker
Remove uint.
187
int Field_blob::store(const char *from,uint32_t length, const CHARSET_INFO * const cs)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
188
{
482 by Brian Aker
Remove uint.
189
  uint32_t copy_length, new_length;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
190
  const char *well_formed_error_pos;
191
  const char *cannot_convert_error_pos;
192
  const char *from_end_pos, *tmp;
193
  char buff[STRING_BUFFER_USUAL_SIZE];
194
  String tmpstr(buff,sizeof(buff), &my_charset_bin);
195
196
  if (!length)
197
  {
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
198
    memset(ptr, 0, Field_blob::pack_length());
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
199
    return 0;
200
  }
201
202
  if (from == value.ptr())
203
  {
204
    uint32_t dummy_offset;
205
    if (!String::needs_conversion(length, cs, field_charset, &dummy_offset))
206
    {
207
      Field_blob::store_length(length);
629.3.6 by Kristian Nielsen
A couple more fixes of previous bmove()->memcpy(), changed to
208
      memmove(ptr+packlength, &from, sizeof(char*));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
209
      return 0;
210
    }
211
    if (tmpstr.copy(from, length, cs))
212
      goto oom_error;
213
    from= tmpstr.ptr();
214
  }
215
398.1.4 by Monty Taylor
Renamed max/min.
216
  new_length= cmin(max_data_length(), field_charset->mbmaxlen * length);
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
217
  if (value.alloc(new_length))
218
    goto oom_error;
219
220
221
  if (f_is_hex_escape(flags))
222
  {
223
    copy_length= my_copy_with_hex_escaping(field_charset,
224
                                           (char*) value.ptr(), new_length,
225
                                            from, length);
226
    Field_blob::store_length(copy_length);
227
    tmp= value.ptr();
629.3.6 by Kristian Nielsen
A couple more fixes of previous bmove()->memcpy(), changed to
228
    memmove(ptr + packlength, &tmp, sizeof(char*));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
229
    return 0;
230
  }
231
  /*
232
    "length" is OK as "nchars" argument to well_formed_copy_nchars as this
233
    is never used to limit the length of the data. The cut of long data
234
    is done with the new_length value.
235
  */
236
  copy_length= well_formed_copy_nchars(field_charset,
237
                                       (char*) value.ptr(), new_length,
238
                                       cs, from, length,
239
                                       length,
240
                                       &well_formed_error_pos,
241
                                       &cannot_convert_error_pos,
242
                                       &from_end_pos);
243
244
  Field_blob::store_length(copy_length);
245
  tmp= value.ptr();
629.3.6 by Kristian Nielsen
A couple more fixes of previous bmove()->memcpy(), changed to
246
  memmove(ptr+packlength, &tmp, sizeof(char*));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
247
248
  if (check_string_copy_error(this, well_formed_error_pos,
249
                              cannot_convert_error_pos, from + length, cs))
250
    return 2;
251
252
  return report_if_important_data(from_end_pos, from + length);
253
254
oom_error:
255
  /* Fatal OOM error */
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
256
  memset(ptr, 0, Field_blob::pack_length());
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
257
  return -1;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
258
}
259
260
261
int Field_blob::store(double nr)
262
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
263
  const CHARSET_INFO * const cs=charset();
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
264
  value.set_real(nr, NOT_FIXED_DEC, cs);
265
  return Field_blob::store(value.ptr(),(uint) value.length(), cs);
266
}
267
268
269
int Field_blob::store(int64_t nr, bool unsigned_val)
270
{
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
271
  const CHARSET_INFO * const cs=charset();
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
272
  value.set_int(nr, unsigned_val, cs);
273
  return Field_blob::store(value.ptr(), (uint) value.length(), cs);
274
}
275
276
277
double Field_blob::val_real(void)
278
{
279
  int not_used;
280
  char *end_not_used, *blob;
281
  uint32_t length;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
282
  const CHARSET_INFO *cs;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
283
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
284
  memcpy(&blob,ptr+packlength,sizeof(char*));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
285
  if (!blob)
286
    return 0.0;
287
  length= get_length(ptr);
288
  cs= charset();
289
  return my_strntod(cs, blob, length, &end_not_used, &not_used);
290
}
291
292
293
int64_t Field_blob::val_int(void)
294
{
295
  int not_used;
296
  char *blob;
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
297
  memcpy(&blob,ptr+packlength,sizeof(char*));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
298
  if (!blob)
299
    return 0;
300
  uint32_t length=get_length(ptr);
301
  return my_strntoll(charset(),blob,length,10,NULL,&not_used);
302
}
303
304
String *Field_blob::val_str(String *val_buffer __attribute__((unused)),
305
			    String *val_ptr)
306
{
307
  char *blob;
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
308
  memcpy(&blob,ptr+packlength,sizeof(char*));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
309
  if (!blob)
310
    val_ptr->set("",0,charset());	// A bit safer than ->length(0)
311
  else
312
    val_ptr->set((const char*) blob,get_length(ptr),charset());
313
  return val_ptr;
314
}
315
316
317
my_decimal *Field_blob::val_decimal(my_decimal *decimal_value)
318
{
319
  const char *blob;
320
  size_t length;
481 by Brian Aker
Remove all of uchar.
321
  memcpy(&blob, ptr+packlength, sizeof(const unsigned char*));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
322
  if (!blob)
323
  {
324
    blob= "";
325
    length= 0;
326
  }
327
  else
328
    length= get_length(ptr);
329
330
  str2my_decimal(E_DEC_FATAL_ERROR, blob, length, charset(),
331
                 decimal_value);
332
  return decimal_value;
333
}
334
335
481 by Brian Aker
Remove all of uchar.
336
int Field_blob::cmp(const unsigned char *a,uint32_t a_length, const unsigned char *b,
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
337
		    uint32_t b_length)
338
{
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
339
  return field_charset->coll->strnncollsp(field_charset,
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
340
                                          a, a_length, b, b_length,
341
                                          0);
342
}
343
344
481 by Brian Aker
Remove all of uchar.
345
int Field_blob::cmp_max(const unsigned char *a_ptr, const unsigned char *b_ptr,
482 by Brian Aker
Remove uint.
346
                        uint32_t max_length)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
347
{
481 by Brian Aker
Remove all of uchar.
348
  unsigned char *blob1,*blob2;
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
349
  memcpy(&blob1,a_ptr+packlength,sizeof(char*));
350
  memcpy(&blob2,b_ptr+packlength,sizeof(char*));
482 by Brian Aker
Remove uint.
351
  uint32_t a_len= get_length(a_ptr), b_len= get_length(b_ptr);
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
352
  set_if_smaller(a_len, max_length);
353
  set_if_smaller(b_len, max_length);
354
  return Field_blob::cmp(blob1,a_len,blob2,b_len);
355
}
356
357
481 by Brian Aker
Remove all of uchar.
358
int Field_blob::cmp_binary(const unsigned char *a_ptr, const unsigned char *b_ptr,
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
359
			   uint32_t max_length)
360
{
361
  char *a,*b;
482 by Brian Aker
Remove uint.
362
  uint32_t diff;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
363
  uint32_t a_length,b_length;
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
364
  memcpy(&a,a_ptr+packlength,sizeof(char*));
365
  memcpy(&b,b_ptr+packlength,sizeof(char*));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
366
  a_length=get_length(a_ptr);
367
  if (a_length > max_length)
368
    a_length=max_length;
369
  b_length=get_length(b_ptr);
370
  if (b_length > max_length)
371
    b_length=max_length;
398.1.4 by Monty Taylor
Renamed max/min.
372
  diff=memcmp(a,b,cmin(a_length,b_length));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
373
  return diff ? diff : (int) (a_length - b_length);
374
}
375
376
377
/* The following is used only when comparing a key */
378
482 by Brian Aker
Remove uint.
379
uint32_t Field_blob::get_key_image(unsigned char *buff,
656.1.1 by Monty Taylor
OOOh doggie. Got rid of my_alloca.
380
                                   uint32_t length,
381
                                   imagetype)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
382
{
383
  uint32_t blob_length= get_length(ptr);
481 by Brian Aker
Remove all of uchar.
384
  unsigned char *blob;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
385
386
  get_ptr(&blob);
482 by Brian Aker
Remove uint.
387
  uint32_t local_char_length= length / field_charset->mbmaxlen;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
388
  local_char_length= my_charpos(field_charset, blob, blob + blob_length,
389
                          local_char_length);
390
  set_if_smaller(blob_length, local_char_length);
391
392
  if ((uint32_t) length > blob_length)
393
  {
394
    /*
395
      Must clear this as we do a memcmp in opt_range.cc to detect
396
      identical keys
397
    */
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
398
    memset(buff+HA_KEY_BLOB_LENGTH+blob_length, 0, (length-blob_length));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
399
    length=(uint) blob_length;
400
  }
401
  int2store(buff,length);
402
  memcpy(buff+HA_KEY_BLOB_LENGTH, blob, length);
403
  return HA_KEY_BLOB_LENGTH+length;
404
}
405
406
656.1.1 by Monty Taylor
OOOh doggie. Got rid of my_alloca.
407
uint32_t Field_blob::get_key_image(basic_string<unsigned char> &buff,
408
                                   uint32_t length,
409
                                   imagetype)
410
{
411
  uint32_t blob_length= get_length(ptr);
412
  unsigned char *blob;
413
414
  get_ptr(&blob);
415
  uint32_t local_char_length= length / field_charset->mbmaxlen;
416
  local_char_length= my_charpos(field_charset, blob, blob + blob_length,
417
                                local_char_length);
418
  set_if_smaller(blob_length, local_char_length);
419
420
  unsigned char len_buff[HA_KEY_BLOB_LENGTH];
421
  int2store(len_buff,length);
422
  buff.append(len_buff);
423
  buff.append(blob, blob_length);
424
425
  if (length > blob_length)
426
  {
427
    /*
428
      Must clear this as we do a memcmp in opt_range.cc to detect
429
      identical keys
430
    */
431
432
    buff.append(length-blob_length, '0');
433
  }
434
  return HA_KEY_BLOB_LENGTH+length;
435
}
436
437
482 by Brian Aker
Remove uint.
438
void Field_blob::set_key_image(const unsigned char *buff,uint32_t length)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
439
{
440
  length= uint2korr(buff);
441
  (void) Field_blob::store((const char*) buff+HA_KEY_BLOB_LENGTH, length,
442
                           field_charset);
443
}
444
445
482 by Brian Aker
Remove uint.
446
int Field_blob::key_cmp(const unsigned char *key_ptr, uint32_t max_key_length)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
447
{
481 by Brian Aker
Remove all of uchar.
448
  unsigned char *blob1;
482 by Brian Aker
Remove uint.
449
  uint32_t blob_length=get_length(ptr);
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
450
  memcpy(&blob1,ptr+packlength,sizeof(char*));
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
451
  const CHARSET_INFO * const cs= charset();
482 by Brian Aker
Remove uint.
452
  uint32_t local_char_length= max_key_length / cs->mbmaxlen;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
453
  local_char_length= my_charpos(cs, blob1, blob1+blob_length,
454
                                local_char_length);
455
  set_if_smaller(blob_length, local_char_length);
456
  return Field_blob::cmp(blob1, blob_length,
457
			 key_ptr+HA_KEY_BLOB_LENGTH,
458
			 uint2korr(key_ptr));
459
}
460
481 by Brian Aker
Remove all of uchar.
461
int Field_blob::key_cmp(const unsigned char *a,const unsigned char *b)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
462
{
463
  return Field_blob::cmp(a+HA_KEY_BLOB_LENGTH, uint2korr(a),
464
			 b+HA_KEY_BLOB_LENGTH, uint2korr(b));
465
}
466
467
468
/**
469
   Save the field metadata for blob fields.
470
471
   Saves the pack length in the first byte of the field metadata array
472
   at index of *metadata_ptr.
473
474
   @param   metadata_ptr   First byte of field metadata
475
476
   @returns number of bytes written to metadata_ptr
477
*/
481 by Brian Aker
Remove all of uchar.
478
int Field_blob::do_save_field_metadata(unsigned char *metadata_ptr)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
479
{
480
  *metadata_ptr= pack_length_no_ptr();
481
  return 1;
482
}
483
484
485
uint32_t Field_blob::sort_length() const
486
{
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.
487
  return (uint32_t) (current_session->variables.max_sort_length +
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
488
                   (field_charset == &my_charset_bin ? 0 : packlength));
489
}
490
491
482 by Brian Aker
Remove uint.
492
void Field_blob::sort_string(unsigned char *to,uint32_t length)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
493
{
481 by Brian Aker
Remove all of uchar.
494
  unsigned char *blob;
482 by Brian Aker
Remove uint.
495
  uint32_t blob_length=get_length();
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
496
497
  if (!blob_length)
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
498
    memset(to, 0, length);
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
499
  else
500
  {
501
    if (field_charset == &my_charset_bin)
502
    {
481 by Brian Aker
Remove all of uchar.
503
      unsigned char *pos;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
504
505
      /*
506
        Store length of blob last in blob to shorter blobs before longer blobs
507
      */
508
      length-= packlength;
509
      pos= to+length;
510
511
      switch (packlength) {
512
      case 1:
513
        *pos= (char) blob_length;
514
        break;
515
      case 2:
516
        mi_int2store(pos, blob_length);
517
        break;
518
      case 3:
519
        mi_int3store(pos, blob_length);
520
        break;
521
      case 4:
522
        mi_int4store(pos, blob_length);
523
        break;
524
      }
525
    }
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
526
    memcpy(&blob,ptr+packlength,sizeof(char*));
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
527
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
528
    blob_length=my_strnxfrm(field_charset,
529
                            to, length, blob, blob_length);
530
    assert(blob_length == length);
531
  }
532
}
533
534
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
535
uint32_t Field_blob::pack_length() const
536
{
537
  return (uint32_t) (packlength+table->s->blob_ptr_size);
538
}
539
540
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
541
void Field_blob::sql_type(String &res) const
542
{
543
  if (charset() == &my_charset_bin)
221 by Brian Aker
First pass of removing length types for ints.
544
    res.set_ascii(STRING_WITH_LEN("blob"));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
545
  else
221 by Brian Aker
First pass of removing length types for ints.
546
    res.set_ascii(STRING_WITH_LEN("text"));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
547
}
548
481 by Brian Aker
Remove all of uchar.
549
unsigned char *Field_blob::pack(unsigned char *to, const unsigned char *from,
482 by Brian Aker
Remove uint.
550
                        uint32_t max_length, bool low_byte_first)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
551
{
481 by Brian Aker
Remove all of uchar.
552
  unsigned char *save= ptr;
553
  ptr= (unsigned char*) from;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
554
  uint32_t length=get_length();			// Length of from string
555
556
  /*
557
    Store max length, which will occupy packlength bytes. If the max
558
    length given is smaller than the actual length of the blob, we
559
    just store the initial bytes of the blob.
560
  */
398.1.4 by Monty Taylor
Renamed max/min.
561
  store_length(to, packlength, cmin(length, max_length), low_byte_first);
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
562
563
  /*
564
    Store the actual blob data, which will occupy 'length' bytes.
565
   */
566
  if (length > 0)
567
  {
481 by Brian Aker
Remove all of uchar.
568
    get_ptr((unsigned char**) &from);
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
569
    memcpy(to+packlength, from,length);
570
  }
571
  ptr=save;					// Restore org row pointer
572
  return(to+packlength+length);
573
}
574
575
576
/**
577
   Unpack a blob field from row data.
578
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
579
   This method is used to unpack a blob field from a master whose size of
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
580
   the field is less than that of the slave. Note: This method is included
581
   to satisfy inheritance rules, but is not needed for blob fields. It
582
   simply is used as a pass-through to the original unpack() method for
583
   blob fields.
584
585
   @param   to         Destination of the data
586
   @param   from       Source of the data
587
   @param   param_data @c true if base types should be stored in little-
588
                       endian format, @c false if native format should
589
                       be used.
590
591
   @return  New pointer into memory based on from + length of the data
592
*/
481 by Brian Aker
Remove all of uchar.
593
const unsigned char *Field_blob::unpack(unsigned char *to __attribute__((unused)),
594
                                const unsigned char *from,
482 by Brian Aker
Remove uint.
595
                                uint32_t param_data,
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
596
                                bool low_byte_first)
597
{
482 by Brian Aker
Remove uint.
598
  uint32_t const master_packlength=
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
599
    param_data > 0 ? param_data & 0xFF : packlength;
600
  uint32_t const length= get_length(from, master_packlength, low_byte_first);
601
  bitmap_set_bit(table->write_set, field_index);
602
  store(reinterpret_cast<const char*>(from) + master_packlength,
603
        length, field_charset);
604
  return(from + master_packlength + length);
605
}
606
607
/* Keys for blobs are like keys on varchars */
608
482 by Brian Aker
Remove uint.
609
int Field_blob::pack_cmp(const unsigned char *a, const unsigned char *b, uint32_t key_length_arg,
275 by Brian Aker
Full removal of my_bool from central server.
610
                         bool insert_or_update)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
611
{
482 by Brian Aker
Remove uint.
612
  uint32_t a_length, b_length;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
613
  if (key_length_arg > 255)
614
  {
615
    a_length=uint2korr(a); a+=2;
616
    b_length=uint2korr(b); b+=2;
617
  }
618
  else
619
  {
620
    a_length= (uint) *a++;
621
    b_length= (uint) *b++;
622
  }
623
  return field_charset->coll->strnncollsp(field_charset,
624
                                          a, a_length,
625
                                          b, b_length,
626
                                          insert_or_update);
627
}
628
629
482 by Brian Aker
Remove uint.
630
int Field_blob::pack_cmp(const unsigned char *b, uint32_t key_length_arg,
275 by Brian Aker
Full removal of my_bool from central server.
631
                         bool insert_or_update)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
632
{
481 by Brian Aker
Remove all of uchar.
633
  unsigned char *a;
482 by Brian Aker
Remove uint.
634
  uint32_t a_length, b_length;
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
635
  memcpy(&a,ptr+packlength,sizeof(char*));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
636
  if (!a)
637
    return key_length_arg > 0 ? -1 : 0;
638
639
  a_length= get_length(ptr);
640
  if (key_length_arg > 255)
641
  {
642
    b_length= uint2korr(b); b+=2;
643
  }
644
  else
645
    b_length= (uint) *b++;
646
  return field_charset->coll->strnncollsp(field_charset,
647
                                          a, a_length,
648
                                          b, b_length,
649
                                          insert_or_update);
650
}
651
652
/** Create a packed key that will be used for storage from a MySQL row. */
653
481 by Brian Aker
Remove all of uchar.
654
unsigned char *
482 by Brian Aker
Remove uint.
655
Field_blob::pack_key(unsigned char *to, const unsigned char *from, uint32_t max_length,
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
656
                     bool low_byte_first __attribute__((unused)))
657
{
481 by Brian Aker
Remove all of uchar.
658
  unsigned char *save= ptr;
659
  ptr= (unsigned char*) from;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
660
  uint32_t length=get_length();        // Length of from string
482 by Brian Aker
Remove uint.
661
  uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
662
                           max_length/field_charset->mbmaxlen : max_length);
663
  if (length)
481 by Brian Aker
Remove all of uchar.
664
    get_ptr((unsigned char**) &from);
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
665
  if (length > local_char_length)
666
    local_char_length= my_charpos(field_charset, from, from+length,
667
                                  local_char_length);
668
  set_if_smaller(length, local_char_length);
481 by Brian Aker
Remove all of uchar.
669
  *to++= (unsigned char) length;
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
670
  if (max_length > 255)				// 2 byte length
481 by Brian Aker
Remove all of uchar.
671
    *to++= (unsigned char) (length >> 8);
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
672
  memcpy(to, from, length);
673
  ptr=save;					// Restore org row pointer
674
  return to+length;
675
}
676
677
678
/**
679
  Unpack a blob key into a record buffer.
680
681
  A blob key has a maximum size of 64K-1.
682
  In its packed form, the length field is one or two bytes long,
683
  depending on 'max_length'.
684
  Depending on the maximum length of a blob, its length field is
685
  put into 1 to 4 bytes. This is a property of the blob object,
686
  described by 'packlength'.
687
  Blobs are internally stored apart from the record buffer, which
688
  contains a pointer to the blob buffer.
689
690
691
  @param to                          Pointer into the record buffer.
692
  @param from                        Pointer to the packed key.
693
  @param max_length                  Key length limit from key description.
694
695
  @return
696
    Pointer into 'from' past the last byte copied from packed key.
697
*/
698
481 by Brian Aker
Remove all of uchar.
699
const unsigned char *
482 by Brian Aker
Remove uint.
700
Field_blob::unpack_key(unsigned char *to, const unsigned char *from, uint32_t max_length,
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
701
                       bool low_byte_first __attribute__((unused)))
702
{
703
  /* get length of the blob key */
704
  uint32_t length= *from++;
705
  if (max_length > 255)
706
    length+= *from++ << 8;
707
708
  /* put the length into the record buffer */
709
  put_length(to, length);
710
711
  /* put the address of the blob buffer or NULL */
712
  if (length)
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
713
    memcpy(to + packlength, &from, sizeof(from));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
714
  else
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
715
    memset(to + packlength, 0, sizeof(from));
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
716
717
  /* point to first byte of next field in 'from' */
718
  return from + length;
719
}
720
721
722
/** Create a packed key that will be used for storage from a MySQL key. */
723
481 by Brian Aker
Remove all of uchar.
724
unsigned char *
482 by Brian Aker
Remove uint.
725
Field_blob::pack_key_from_key_image(unsigned char *to, const unsigned char *from, uint32_t max_length,
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
726
                                    bool low_byte_first __attribute__((unused)))
727
{
482 by Brian Aker
Remove uint.
728
  uint32_t length=uint2korr(from);
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
729
  if (length > max_length)
730
    length=max_length;
731
  *to++= (char) (length & 255);
732
  if (max_length > 255)
733
    *to++= (char) (length >> 8);
734
  if (length)
735
    memcpy(to, from+HA_KEY_BLOB_LENGTH, length);
736
  return to+length;
737
}
738
739
482 by Brian Aker
Remove uint.
740
uint32_t Field_blob::packed_col_length(const unsigned char *data_ptr, uint32_t length)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
741
{
742
  if (length > 255)
743
    return uint2korr(data_ptr)+2;
744
  return (uint) *data_ptr + 1;
745
}
746
747
482 by Brian Aker
Remove uint.
748
uint32_t Field_blob::max_packed_col_length(uint32_t max_length)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
749
{
750
  return (max_length > 255 ? 2 : 1)+max_length;
751
}
752
753
482 by Brian Aker
Remove uint.
754
uint32_t Field_blob::is_equal(Create_field *new_field)
173.1.2 by Toru Maesaka
forgot to bzr-add new files in the previous push
755
{
756
  if (compare_str_field_flags(new_field, flags))
757
    return 0;
758
759
  return ((new_field->sql_type == get_blob_type_from_length(max_data_length()))
760
          && new_field->charset == field_charset &&
761
          ((Field_blob *)new_field->field)->max_data_length() ==
762
          max_data_length());
763
}
764
765
766
/**
767
  maximum possible display length for blob.
768
769
  @return
770
    length
771
*/
772
773
uint32_t Field_blob::max_display_length()
774
{
775
  switch (packlength)
776
  {
777
  case 1:
778
    return 255 * field_charset->mbmaxlen;
779
  case 2:
780
    return 65535 * field_charset->mbmaxlen;
781
  case 3:
782
    return 16777215 * field_charset->mbmaxlen;
783
  case 4:
784
    return (uint32_t) 4294967295U;
785
  default:
786
    assert(0); // we should never go here
787
    return 0;
788
  }
789
}
790
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
791
bool Field_blob::in_read_set()
792
{
793
  return bitmap_is_set(table->read_set, field_index);
794
}
795
796
797
bool Field_blob::in_write_set()
798
{
799
  return bitmap_is_set(table->write_set, field_index);
800
}