~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field_conv.cc

  • Committer: Olaf van der Spek
  • Date: 2011-03-29 12:04:36 UTC
  • mto: (2257.1.1 build) (2276.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 2258.
  • Revision ID: olafvdspek@gmail.com-20110329120436-vozkuer8vqgh027p
Always call assert()

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
    gives much more speed.
25
25
*/
26
26
 
27
 
#include "config.h"
 
27
#include <config.h>
 
28
 
28
29
#include <drizzled/error.h>
29
30
#include <drizzled/table.h>
30
31
#include <drizzled/session.h>
 
32
#include <drizzled/current_session.h>
31
33
 
32
 
#include <drizzled/field/str.h>
33
 
#include <drizzled/field/num.h>
 
34
#include <drizzled/copy_field.h>
34
35
#include <drizzled/field/blob.h>
35
 
#include <drizzled/field/enum.h>
36
 
#include <drizzled/field/null.h>
37
36
#include <drizzled/field/date.h>
 
37
#include <drizzled/field/datetime.h>
38
38
#include <drizzled/field/decimal.h>
39
 
#include <drizzled/field/real.h>
40
39
#include <drizzled/field/double.h>
 
40
#include <drizzled/field/enum.h>
 
41
#include <drizzled/field/epoch.h>
41
42
#include <drizzled/field/int32.h>
42
43
#include <drizzled/field/int64.h>
43
 
#include <drizzled/field/num.h>
44
 
#include <drizzled/field/timestamp.h>
45
 
#include <drizzled/field/datetime.h>
 
44
#include <drizzled/field/null.h>
 
45
#include <drizzled/field/num.h>
 
46
#include <drizzled/field/num.h>
 
47
#include <drizzled/field/real.h>
 
48
#include <drizzled/field/str.h>
46
49
#include <drizzled/field/varstring.h>
 
50
#include <drizzled/util/test.h>
 
51
#include <drizzled/system_variables.h>
47
52
 
48
 
namespace drizzled
49
 
{
 
53
namespace drizzled {
50
54
 
51
55
static void do_field_eq(CopyField *copy)
52
56
{
180
184
    field->reset();
181
185
    return 0;
182
186
  }
 
187
 
183
188
  if (no_conversions)
184
189
    return -1;
185
190
 
188
193
    when set to NULL (TIMESTAMP fields which allow setting to NULL
189
194
    are handled by first check).
190
195
  */
191
 
  if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
 
196
  if (field->is_timestamp())
192
197
  {
193
 
    ((Field_timestamp*) field)->set_time();
 
198
    ((field::Epoch::pointer) field)->set_time();
194
199
    return 0;                                   // Ok to set time to NULL
195
200
  }
 
201
 
196
202
  field->reset();
197
203
  if (field == field->getTable()->next_number_field)
198
204
  {
199
205
    field->getTable()->auto_increment_field_not_null= false;
200
206
    return 0;                             // field is set in fill_record()
201
207
  }
 
208
 
202
209
  if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
203
210
  {
204
211
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_BAD_NULL_ERROR, 1);
205
212
    return 0;
206
213
  }
 
214
 
207
215
  if (!field->getTable()->in_use->no_errors)
208
216
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
 
217
 
209
218
  return -1;
210
219
}
211
220
 
278
287
  if (*copy->from_null_ptr & copy->from_bit)
279
288
  {
280
289
    /* Same as in set_field_to_null_with_conversions() */
281
 
    ((Field_timestamp*) copy->to_field)->set_time();
 
290
    ((field::Epoch::pointer) copy->to_field)->set_time();
282
291
  }
283
292
  else
 
293
  {
284
294
    (copy->do_copy2)(copy);
 
295
  }
285
296
}
286
297
 
287
298
 
294
305
    copy->to_field->reset();
295
306
  }
296
307
  else
 
308
  {
297
309
    (copy->do_copy2)(copy);
 
310
  }
298
311
}
299
312
 
300
313
 
362
375
 
363
376
static void do_field_decimal(CopyField *copy)
364
377
{
365
 
  my_decimal value;
 
378
  type::Decimal value;
366
379
  copy->to_field->store_decimal(copy->from_field->val_decimal(&value));
367
380
}
368
381
 
374
387
 
375
388
static void do_cut_string(CopyField *copy)
376
389
{
377
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
378
 
  memcpy(copy->to_ptr,copy->from_ptr,copy->to_length);
 
390
  const charset_info_st * const cs= copy->from_field->charset();
 
391
  memcpy(copy->to_ptr, copy->from_ptr, copy->to_length);
379
392
 
380
393
  /* Check if we loosed any important characters */
381
394
  if (cs->cset->scan(cs,
397
410
static void do_cut_string_complex(CopyField *copy)
398
411
{                                               // Shorter string field
399
412
  int well_formed_error;
400
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
 
413
  const charset_info_st * const cs= copy->from_field->charset();
401
414
  const unsigned char *from_end= copy->from_ptr + copy->from_length;
402
415
  uint32_t copy_length= cs->cset->well_formed_len(cs,
403
416
                                              (char*) copy->from_ptr,
428
441
 
429
442
static void do_expand_binary(CopyField *copy)
430
443
{
431
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
 
444
  const charset_info_st * const cs= copy->from_field->charset();
432
445
  memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
433
446
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
434
447
                 copy->to_length-copy->from_length, '\0');
438
451
 
439
452
static void do_expand_string(CopyField *copy)
440
453
{
441
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
 
454
  const charset_info_st * const cs= copy->from_field->charset();
442
455
  memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
443
456
  cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
444
457
                 copy->to_length-copy->from_length, ' ');
452
465
  {
453
466
    length= copy->to_length - 1;
454
467
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
 
468
    {
455
469
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
456
470
                                  ER_WARN_DATA_TRUNCATED, 1);
 
471
    }
457
472
  }
458
473
  *(unsigned char*) copy->to_ptr= (unsigned char) length;
459
474
  memcpy(copy->to_ptr+1, copy->from_ptr + 1, length);
463
478
static void do_varstring1_mb(CopyField *copy)
464
479
{
465
480
  int well_formed_error;
466
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
 
481
  const charset_info_st * const cs= copy->from_field->charset();
467
482
  uint32_t from_length= (uint32_t) *(unsigned char*) copy->from_ptr;
468
483
  const unsigned char *from_ptr= copy->from_ptr + 1;
469
484
  uint32_t to_char_length= (copy->to_length - 1) / cs->mbmaxlen;
473
488
  if (length < from_length)
474
489
  {
475
490
    if (current_session->count_cuted_fields)
 
491
    {
476
492
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
477
493
                                  ER_WARN_DATA_TRUNCATED, 1);
 
494
    }
478
495
  }
479
496
  *copy->to_ptr= (unsigned char) length;
480
497
  memcpy(copy->to_ptr + 1, from_ptr, length);
488
505
  {
489
506
    length=copy->to_length-HA_KEY_BLOB_LENGTH;
490
507
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
 
508
    {
491
509
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
492
510
                                  ER_WARN_DATA_TRUNCATED, 1);
 
511
    }
493
512
  }
494
513
  int2store(copy->to_ptr,length);
495
514
  memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, copy->from_ptr + HA_KEY_BLOB_LENGTH,
500
519
static void do_varstring2_mb(CopyField *copy)
501
520
{
502
521
  int well_formed_error;
503
 
  const CHARSET_INFO * const cs= copy->from_field->charset();
 
522
  const charset_info_st * const cs= copy->from_field->charset();
504
523
  uint32_t char_length= (copy->to_length - HA_KEY_BLOB_LENGTH) / cs->mbmaxlen;
505
524
  uint32_t from_length= uint2korr(copy->from_ptr);
506
525
  const unsigned char *from_beg= copy->from_ptr + HA_KEY_BLOB_LENGTH;
510
529
  if (length < from_length)
511
530
  {
512
531
    if (current_session->count_cuted_fields)
 
532
    {
513
533
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
514
534
                                  ER_WARN_DATA_TRUNCATED, 1);
 
535
    }
515
536
  }
516
537
  int2store(copy->to_ptr, length);
517
538
  memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, from_beg, length);
611
632
    }
612
633
    else
613
634
    {
614
 
      if (to_field->type() == DRIZZLE_TYPE_TIMESTAMP)
 
635
      if (to_field->is_timestamp())
615
636
      {
616
637
        do_copy= do_copy_timestamp;               // Automatic timestamp
617
638
      }
655
676
    if (from_length != to_length || !compatible_db_low_byte_first)
656
677
    {
657
678
      // Correct pointer to point at char pointer
658
 
      to_ptr+= to_length - to->getTable()->getShare()->blob_ptr_size;
659
 
      from_ptr+= from_length- from->getTable()->getShare()->blob_ptr_size;
 
679
      to_ptr+= to_length - to->getTable()->getShare()->sizeBlobPtr();
 
680
      from_ptr+= from_length- from->getTable()->getShare()->sizeBlobPtr();
660
681
      return do_copy_blob;
661
682
    }
662
683
  }
839
860
    return to->store(result.c_ptr_quick(),result.length(),from->charset());
840
861
  }
841
862
  else if (from->result_type() == REAL_RESULT)
 
863
  {
842
864
    return to->store(from->val_real());
 
865
  }
843
866
  else if (from->result_type() == DECIMAL_RESULT)
844
867
  {
845
 
    my_decimal buff;
 
868
    type::Decimal buff;
846
869
    return to->store_decimal(from->val_decimal(&buff));
847
870
  }
848
871
  else
 
872
  {
849
873
    return to->store(from->val_int(), test(from->flags & UNSIGNED_FLAG));
 
874
  }
850
875
}
851
876
 
852
877
} /* namespace drizzled */