~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field_conv.cc

  • Committer: Brian Aker
  • Date: 2010-12-17 00:08:06 UTC
  • mfrom: (2002.1.4 clean)
  • Revision ID: brian@tangent.org-20101217000806-fa6kmggjnhsl4q85
Rollup for field encapsulation, monty fix for bzrignore, and Andrew bug
fixes.

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>
28
 
 
 
27
#include "config.h"
29
28
#include <drizzled/error.h>
30
29
#include <drizzled/table.h>
31
30
#include <drizzled/session.h>
32
 
#include <drizzled/current_session.h>
33
31
 
34
 
#include <drizzled/copy_field.h>
 
32
#include <drizzled/field/str.h>
 
33
#include <drizzled/field/num.h>
35
34
#include <drizzled/field/blob.h>
 
35
#include <drizzled/field/enum.h>
 
36
#include <drizzled/field/null.h>
36
37
#include <drizzled/field/date.h>
37
 
#include <drizzled/field/datetime.h>
38
38
#include <drizzled/field/decimal.h>
 
39
#include <drizzled/field/real.h>
39
40
#include <drizzled/field/double.h>
40
 
#include <drizzled/field/enum.h>
41
 
#include <drizzled/field/epoch.h>
42
 
#include <drizzled/field/int32.h>
43
 
#include <drizzled/field/int64.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>
 
41
#include <drizzled/field/long.h>
 
42
#include <drizzled/field/int64_t.h>
 
43
#include <drizzled/field/num.h>
 
44
#include <drizzled/field/timestamp.h>
 
45
#include <drizzled/field/datetime.h>
49
46
#include <drizzled/field/varstring.h>
50
47
 
51
48
namespace drizzled
183
180
    field->reset();
184
181
    return 0;
185
182
  }
186
 
 
187
183
  if (no_conversions)
188
184
    return -1;
189
185
 
192
188
    when set to NULL (TIMESTAMP fields which allow setting to NULL
193
189
    are handled by first check).
194
190
  */
195
 
  if (field->is_timestamp())
 
191
  if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
196
192
  {
197
 
    ((field::Epoch::pointer) field)->set_time();
 
193
    ((Field_timestamp*) field)->set_time();
198
194
    return 0;                                   // Ok to set time to NULL
199
195
  }
200
 
 
201
196
  field->reset();
202
197
  if (field == field->getTable()->next_number_field)
203
198
  {
204
199
    field->getTable()->auto_increment_field_not_null= false;
205
200
    return 0;                             // field is set in fill_record()
206
201
  }
207
 
 
208
202
  if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
209
203
  {
210
204
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_BAD_NULL_ERROR, 1);
211
205
    return 0;
212
206
  }
213
 
 
214
207
  if (!field->getTable()->in_use->no_errors)
215
208
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
216
 
 
217
209
  return -1;
218
210
}
219
211
 
286
278
  if (*copy->from_null_ptr & copy->from_bit)
287
279
  {
288
280
    /* Same as in set_field_to_null_with_conversions() */
289
 
    ((field::Epoch::pointer) copy->to_field)->set_time();
 
281
    ((Field_timestamp*) copy->to_field)->set_time();
290
282
  }
291
283
  else
292
 
  {
293
284
    (copy->do_copy2)(copy);
294
 
  }
295
285
}
296
286
 
297
287
 
304
294
    copy->to_field->reset();
305
295
  }
306
296
  else
307
 
  {
308
297
    (copy->do_copy2)(copy);
309
 
  }
310
298
}
311
299
 
312
300
 
374
362
 
375
363
static void do_field_decimal(CopyField *copy)
376
364
{
377
 
  type::Decimal value;
 
365
  my_decimal value;
378
366
  copy->to_field->store_decimal(copy->from_field->val_decimal(&value));
379
367
}
380
368
 
387
375
static void do_cut_string(CopyField *copy)
388
376
{
389
377
  const CHARSET_INFO * const cs= copy->from_field->charset();
390
 
  memcpy(copy->to_ptr, copy->from_ptr, copy->to_length);
 
378
  memcpy(copy->to_ptr,copy->from_ptr,copy->to_length);
391
379
 
392
380
  /* Check if we loosed any important characters */
393
381
  if (cs->cset->scan(cs,
464
452
  {
465
453
    length= copy->to_length - 1;
466
454
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
467
 
    {
468
455
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
469
456
                                  ER_WARN_DATA_TRUNCATED, 1);
470
 
    }
471
457
  }
472
458
  *(unsigned char*) copy->to_ptr= (unsigned char) length;
473
459
  memcpy(copy->to_ptr+1, copy->from_ptr + 1, length);
487
473
  if (length < from_length)
488
474
  {
489
475
    if (current_session->count_cuted_fields)
490
 
    {
491
476
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
492
477
                                  ER_WARN_DATA_TRUNCATED, 1);
493
 
    }
494
478
  }
495
479
  *copy->to_ptr= (unsigned char) length;
496
480
  memcpy(copy->to_ptr + 1, from_ptr, length);
504
488
  {
505
489
    length=copy->to_length-HA_KEY_BLOB_LENGTH;
506
490
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
507
 
    {
508
491
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
509
492
                                  ER_WARN_DATA_TRUNCATED, 1);
510
 
    }
511
493
  }
512
494
  int2store(copy->to_ptr,length);
513
495
  memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, copy->from_ptr + HA_KEY_BLOB_LENGTH,
528
510
  if (length < from_length)
529
511
  {
530
512
    if (current_session->count_cuted_fields)
531
 
    {
532
513
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
533
514
                                  ER_WARN_DATA_TRUNCATED, 1);
534
 
    }
535
515
  }
536
516
  int2store(copy->to_ptr, length);
537
517
  memcpy(copy->to_ptr+HA_KEY_BLOB_LENGTH, from_beg, length);
631
611
    }
632
612
    else
633
613
    {
634
 
      if (to_field->is_timestamp())
 
614
      if (to_field->type() == DRIZZLE_TYPE_TIMESTAMP)
635
615
      {
636
616
        do_copy= do_copy_timestamp;               // Automatic timestamp
637
617
      }
675
655
    if (from_length != to_length || !compatible_db_low_byte_first)
676
656
    {
677
657
      // Correct pointer to point at char pointer
678
 
      to_ptr+= to_length - to->getTable()->getShare()->sizeBlobPtr();
679
 
      from_ptr+= from_length- from->getTable()->getShare()->sizeBlobPtr();
 
658
      to_ptr+= to_length - to->getTable()->getShare()->blob_ptr_size;
 
659
      from_ptr+= from_length- from->getTable()->getShare()->blob_ptr_size;
680
660
      return do_copy_blob;
681
661
    }
682
662
  }
702
682
          {
703
683
            return do_field_int;  // Convert SET to number
704
684
          }
705
 
 
 
685
          
706
686
          return do_field_string;
707
687
        }
708
688
      }
709
 
 
 
689
      
710
690
      if (to->real_type() == DRIZZLE_TYPE_ENUM)
711
691
      {
712
692
        if (!to->eq_def(from))
723
703
      else if (to->real_type() == DRIZZLE_TYPE_VARCHAR)
724
704
      {
725
705
        /* Field_blob is not part of the Field_varstring hierarchy,
726
 
          and casting to varstring for calling pack_length_no_ptr()
727
 
          is always incorrect. Previously the below comparison has
728
 
          always evaluated to false as pack_length_no_ptr() for BLOB
729
 
          will return 4 and varstring can only be <= 2.
730
 
          If your brain slightly bleeds as to why this worked for
731
 
          so many years, you are in no way alone.
 
706
           and casting to varstring for calling pack_length_no_ptr()
 
707
           is always incorrect. Previously the below comparison has
 
708
           always evaluated to false as pack_length_no_ptr() for BLOB
 
709
           will return 4 and varstring can only be <= 2.
 
710
           If your brain slightly bleeds as to why this worked for
 
711
           so many years, you are in no way alone.
732
712
        */
733
713
        if (from->flags & BLOB_FLAG)
734
714
          return do_field_string;
738
718
        {
739
719
          return do_field_string;
740
720
        }
741
 
 
 
721
        
742
722
        if (to_length != from_length)
743
723
        {
744
724
          return (((Field_varstring*) to)->pack_length_no_ptr() == 1 ?
745
725
                  (from->charset()->mbmaxlen == 1 ? do_varstring1 :
746
 
                   do_varstring1_mb) :
 
726
                                                    do_varstring1_mb) :
747
727
                  (from->charset()->mbmaxlen == 1 ? do_varstring2 :
748
 
                   do_varstring2_mb));
 
728
                                                    do_varstring2_mb));
749
729
        }
750
730
      }
751
731
      else if (to_length < from_length)
859
839
    return to->store(result.c_ptr_quick(),result.length(),from->charset());
860
840
  }
861
841
  else if (from->result_type() == REAL_RESULT)
862
 
  {
863
842
    return to->store(from->val_real());
864
 
  }
865
843
  else if (from->result_type() == DECIMAL_RESULT)
866
844
  {
867
 
    type::Decimal buff;
 
845
    my_decimal buff;
868
846
    return to->store_decimal(from->val_decimal(&buff));
869
847
  }
870
848
  else
871
 
  {
872
849
    return to->store(from->val_int(), test(from->flags & UNSIGNED_FLAG));
873
 
  }
874
850
}
875
851
 
876
852
} /* namespace drizzled */