~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/blob.cc

  • Committer: Olaf van der Spek
  • Date: 2011-10-24 21:23:54 UTC
  • mto: This revision was merged to the branch mainline in revision 2449.
  • Revision ID: olafvdspek@gmail.com-20111024212354-j32gbc2sbsw0985q
Use str_ref

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
 
22
 
#include "config.h"
 
22
#include <config.h>
23
23
#include <drizzled/field/blob.h>
24
24
#include <drizzled/table.h>
25
25
#include <drizzled/session.h>
26
 
#include "plugin/myisam/myisam.h"
 
26
#include <plugin/myisam/myisam.h>
 
27
#include <drizzled/system_variables.h>
27
28
 
28
29
#include <string>
29
30
#include <algorithm>
30
31
 
31
32
using namespace std;
32
33
 
33
 
namespace drizzled
34
 
{
 
34
namespace drizzled {
35
35
 
36
36
static uint32_t blob_pack_length_to_max_length(uint32_t arg)
37
37
{
51
51
                       unsigned char null_bit_arg,
52
52
                                   const char *field_name_arg,
53
53
                       TableShare *share,
54
 
                       const CHARSET_INFO * const cs)
 
54
                       const charset_info_st * const cs)
55
55
  :Field_str(ptr_arg,
56
56
             blob_pack_length_to_max_length(sizeof(uint32_t)),
57
57
             null_ptr_arg,
90
90
 
91
91
 
92
92
uint32_t Field_blob::get_length(const unsigned char *pos,
93
 
                                bool low_byte_first)
 
93
                                bool low_byte_first) const
94
94
{
95
95
#ifndef WORDS_BIGENDIAN
96
96
  (void)low_byte_first;
113
113
}
114
114
 
115
115
 
116
 
uint32_t Field_blob::get_length(uint32_t row_offset)
 
116
uint32_t Field_blob::get_length(uint32_t row_offset) const
117
117
{
118
118
  return get_length(ptr+row_offset,
119
119
                    getTable()->getShare()->db_low_byte_first);
120
120
}
121
121
 
122
122
 
123
 
uint32_t Field_blob::get_length(const unsigned char *ptr_arg)
 
123
uint32_t Field_blob::get_length(const unsigned char *ptr_arg) const
124
124
{
125
125
  return get_length(ptr_arg, getTable()->getShare()->db_low_byte_first);
126
126
}
141
141
}
142
142
 
143
143
 
144
 
int Field_blob::store(const char *from,uint32_t length, const CHARSET_INFO * const cs)
 
144
int Field_blob::store(const char *from,uint32_t length, const charset_info_st * const cs)
145
145
{
146
146
  uint32_t copy_length, new_length;
147
147
  const char *well_formed_error_pos;
160
160
 
161
161
  if (from == value.ptr())
162
162
  {
163
 
    size_t dummy_offset;
164
 
    if (!String::needs_conversion(length, cs, field_charset, &dummy_offset))
 
163
    if (!String::needs_conversion(length, cs, field_charset))
165
164
    {
166
165
      Field_blob::store_length(length);
167
166
      memmove(ptr+sizeof(uint32_t), &from, sizeof(char*));
168
167
      return 0;
169
168
    }
170
 
    if (tmpstr.copy(from, length, cs))
171
 
      goto oom_error;
 
169
    tmpstr.copy(from, length, cs);
172
170
    from= tmpstr.ptr();
173
171
  }
174
172
 
175
173
  new_length= min(max_data_length(), field_charset->mbmaxlen * length);
176
 
  if (value.alloc(new_length))
177
 
    goto oom_error;
 
174
  value.alloc(new_length);
178
175
 
179
176
  /*
180
177
    "length" is OK as "nchars" argument to well_formed_copy_nchars as this
198
195
    return 2;
199
196
 
200
197
  return report_if_important_data(from_end_pos, from + length);
201
 
 
202
 
oom_error:
203
 
  /* Fatal OOM error */
204
 
  memset(ptr, 0, Field_blob::pack_length());
205
 
  return -1;
206
198
}
207
199
 
208
 
 
209
200
int Field_blob::store(double nr)
210
201
{
211
 
  const CHARSET_INFO * const cs=charset();
 
202
  const charset_info_st * const cs=charset();
212
203
  ASSERT_COLUMN_MARKED_FOR_WRITE;
213
204
  value.set_real(nr, NOT_FIXED_DEC, cs);
214
205
  return Field_blob::store(value.ptr(),(uint32_t) value.length(), cs);
215
206
}
216
207
 
217
 
 
218
208
int Field_blob::store(int64_t nr, bool unsigned_val)
219
209
{
220
 
  const CHARSET_INFO * const cs=charset();
 
210
  const charset_info_st * const cs=charset();
221
211
  ASSERT_COLUMN_MARKED_FOR_WRITE;
222
212
  value.set_int(nr, unsigned_val, cs);
223
213
  return Field_blob::store(value.ptr(), (uint32_t) value.length(), cs);
224
214
}
225
215
 
226
216
 
227
 
double Field_blob::val_real(void)
 
217
double Field_blob::val_real(void) const
228
218
{
229
219
  int not_used;
230
220
  char *end_not_used, *blob;
231
221
  uint32_t length;
232
 
  const CHARSET_INFO *cs;
 
222
  const charset_info_st *cs;
233
223
 
234
224
  ASSERT_COLUMN_MARKED_FOR_READ;
235
225
 
242
232
}
243
233
 
244
234
 
245
 
int64_t Field_blob::val_int(void)
 
235
int64_t Field_blob::val_int(void) const
246
236
{
247
237
  int not_used;
248
238
  char *blob;
252
242
  memcpy(&blob,ptr+sizeof(uint32_t),sizeof(char*));
253
243
  if (!blob)
254
244
    return 0;
255
 
  uint32_t length=get_length(ptr);
 
245
  uint32_t length= get_length(ptr);
256
246
  return my_strntoll(charset(),blob,length,10,NULL,&not_used);
257
247
}
258
248
 
259
 
String *Field_blob::val_str(String *,
260
 
                            String *val_ptr)
 
249
String *Field_blob::val_str(String *, String *val_ptr) const
261
250
{
262
251
  char *blob;
263
252
 
267
256
  if (!blob)
268
257
    val_ptr->set("",0,charset());       // A bit safer than ->length(0)
269
258
  else
270
 
    val_ptr->set((const char*) blob,get_length(ptr),charset());
 
259
    val_ptr->set(blob,get_length(ptr),charset());
271
260
  return val_ptr;
272
261
}
273
262
 
274
263
 
275
 
my_decimal *Field_blob::val_decimal(my_decimal *decimal_value)
 
264
type::Decimal *Field_blob::val_decimal(type::Decimal *decimal_value) const
276
265
{
277
266
  const char *blob;
278
267
  size_t length;
286
275
    length= 0;
287
276
  }
288
277
  else
 
278
  {
289
279
    length= get_length(ptr);
290
 
 
291
 
  str2my_decimal(E_DEC_FATAL_ERROR, blob, length, charset(),
292
 
                 decimal_value);
 
280
  }
 
281
 
 
282
  decimal_value->store(E_DEC_FATAL_ERROR, blob, length, charset());
 
283
 
293
284
  return decimal_value;
294
285
}
295
286
 
344
335
uint32_t Field_blob::get_key_image(unsigned char *buff, uint32_t length)
345
336
{
346
337
  uint32_t blob_length= get_length(ptr);
347
 
  unsigned char *blob;
348
 
 
349
 
  get_ptr(&blob);
 
338
  unsigned char *blob= get_ptr();
350
339
  uint32_t local_char_length= length / field_charset->mbmaxlen;
351
340
  local_char_length= my_charpos(field_charset, blob, blob + blob_length,
352
341
                          local_char_length);
366
355
  return HA_KEY_BLOB_LENGTH+length;
367
356
}
368
357
 
369
 
 
370
358
uint32_t Field_blob::get_key_image(basic_string<unsigned char> &buff, uint32_t length)
371
359
{
372
360
  uint32_t blob_length= get_length(ptr);
373
 
  unsigned char *blob;
374
 
 
375
 
  get_ptr(&blob);
 
361
  unsigned char* blob= get_ptr();
376
362
  uint32_t local_char_length= length / field_charset->mbmaxlen;
377
363
  local_char_length= my_charpos(field_charset, blob, blob + blob_length,
378
364
                                local_char_length);
406
392
  unsigned char *blob1;
407
393
  uint32_t blob_length=get_length(ptr);
408
394
  memcpy(&blob1,ptr+sizeof(uint32_t),sizeof(char*));
409
 
  const CHARSET_INFO * const cs= charset();
 
395
  const charset_info_st * const cs= charset();
410
396
  uint32_t local_char_length= max_key_length / cs->mbmaxlen;
411
397
  local_char_length= my_charpos(cs, blob1, blob1+blob_length,
412
398
                                local_char_length);
424
410
 
425
411
uint32_t Field_blob::sort_length() const
426
412
{
427
 
  return (uint32_t) (current_session->variables.max_sort_length +
 
413
  return (uint32_t) (getTable()->getSession()->variables.max_sort_length +
428
414
                     (field_charset == &my_charset_bin ? 0 : sizeof(uint32_t)));
429
415
}
430
416
 
451
437
    }
452
438
    memcpy(&blob,ptr+sizeof(uint32_t),sizeof(char*));
453
439
 
454
 
    blob_length=my_strnxfrm(field_charset,
455
 
                            to, length, blob, blob_length);
 
440
    blob_length= field_charset->strnxfrm(to, length, blob, blob_length);
456
441
    assert(blob_length == length);
457
442
  }
458
443
}
462
447
  return (uint32_t) (sizeof(uint32_t) + portable_sizeof_char_ptr);
463
448
}
464
449
 
465
 
void Field_blob::sql_type(String &res) const
466
 
{
467
 
  if (charset() == &my_charset_bin)
468
 
    res.set_ascii(STRING_WITH_LEN("blob"));
469
 
  else
470
 
    res.set_ascii(STRING_WITH_LEN("text"));
471
 
}
472
 
 
473
450
unsigned char *Field_blob::pack(unsigned char *to, const unsigned char *from,
474
451
                                uint32_t max_length, bool low_byte_first)
475
452
{
489
466
   */
490
467
  if (length > 0)
491
468
  {
492
 
    get_ptr((unsigned char**) &from);
 
469
    from= get_ptr();
493
470
    memcpy(to+sizeof(uint32_t), from,length);
494
471
  }
495
472
 
538
515
  uint32_t local_char_length= ((field_charset->mbmaxlen > 1) ?
539
516
                           max_length/field_charset->mbmaxlen : max_length);
540
517
  if (length)
541
 
    get_ptr((unsigned char**) &from);
 
518
    from= get_ptr();
542
519
  if (length > local_char_length)
543
520
    local_char_length= my_charpos(field_charset, from, from+length,
544
521
                                  local_char_length);