~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field_conv.cc

  • Committer: Stewart Smith
  • Date: 2010-03-18 12:01:34 UTC
  • mto: (1666.2.3 build)
  • mto: This revision was merged to the branch mainline in revision 1596.
  • Revision ID: stewart@flamingspork.com-20100318120134-45fdnsw8g3j6c7oy
move RAND() into a plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/**
38
38
#include <drizzled/field/decimal.h>
39
39
#include <drizzled/field/real.h>
40
40
#include <drizzled/field/double.h>
41
 
#include <drizzled/field/int32.h>
42
 
#include <drizzled/field/int64.h>
 
41
#include <drizzled/field/long.h>
 
42
#include <drizzled/field/int64_t.h>
43
43
#include <drizzled/field/num.h>
44
44
#include <drizzled/field/timestamp.h>
45
45
#include <drizzled/field/datetime.h>
143
143
    return 0;
144
144
  }
145
145
  field->reset();
146
 
  if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
 
146
  if (field->table->in_use->count_cuted_fields == CHECK_FIELD_WARN)
147
147
  {
148
148
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
149
149
    return 0;
150
150
  }
151
 
  if (!field->getTable()->in_use->no_errors)
 
151
  if (!field->table->in_use->no_errors)
152
152
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
153
153
  return -1;
154
154
}
194
194
    return 0;                                   // Ok to set time to NULL
195
195
  }
196
196
  field->reset();
197
 
  if (field == field->getTable()->next_number_field)
 
197
  if (field == field->table->next_number_field)
198
198
  {
199
 
    field->getTable()->auto_increment_field_not_null= false;
 
199
    field->table->auto_increment_field_not_null= false;
200
200
    return 0;                             // field is set in fill_record()
201
201
  }
202
 
  if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
 
202
  if (field->table->in_use->count_cuted_fields == CHECK_FIELD_WARN)
203
203
  {
204
204
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_BAD_NULL_ERROR, 1);
205
205
    return 0;
206
206
  }
207
 
  if (!field->getTable()->in_use->no_errors)
 
207
  if (!field->table->in_use->no_errors)
208
208
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
209
209
  return -1;
210
210
}
248
248
 
249
249
static void do_copy_not_null(CopyField *copy)
250
250
{
251
 
  if (copy->to_field->hasDefault() and *copy->from_null_ptr & copy->from_bit)
252
 
  {
253
 
    copy->to_field->set_default();
254
 
  }
255
 
  else if (*copy->from_null_ptr & copy->from_bit)
 
251
  if (*copy->from_null_ptr & copy->from_bit)
256
252
  {
257
253
    copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
258
254
                                ER_WARN_DATA_TRUNCATED, 1);
259
255
    copy->to_field->reset();
260
256
  }
261
257
  else
262
 
  {
263
258
    (copy->do_copy2)(copy);
264
 
  }
265
259
}
266
260
 
267
261
 
290
284
  if (*copy->from_null_ptr & copy->from_bit)
291
285
  {
292
286
    /* Same as in set_field_to_null_with_conversions() */
293
 
    copy->to_field->getTable()->auto_increment_field_not_null= false;
 
287
    copy->to_field->table->auto_increment_field_not_null= false;
294
288
    copy->to_field->reset();
295
289
  }
296
290
  else
307
301
 
308
302
static void do_conv_blob(CopyField *copy)
309
303
{
310
 
  copy->from_field->val_str_internal(&copy->tmp);
 
304
  copy->from_field->val_str(&copy->tmp);
311
305
  ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
312
306
                                         copy->tmp.length(),
313
307
                                         copy->tmp.charset());
319
313
{
320
314
  char buff[MAX_FIELD_WIDTH];
321
315
  String res(buff, sizeof(buff), copy->tmp.charset());
322
 
  copy->from_field->val_str_internal(&res);
 
316
  copy->from_field->val_str(&res);
323
317
  copy->tmp.copy(res);
324
318
  ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
325
319
                                         copy->tmp.length(),
331
325
{
332
326
  char buff[MAX_FIELD_WIDTH];
333
327
  copy->tmp.set_quick(buff,sizeof(buff),copy->tmp.charset());
334
 
  copy->from_field->val_str_internal(&copy->tmp);
 
328
  copy->from_field->val_str(&copy->tmp);
335
329
  copy->to_field->store(copy->tmp.c_ptr_quick(),copy->tmp.length(),
336
330
                        copy->tmp.charset());
337
331
}
451
445
  if (length > copy->to_length- 1)
452
446
  {
453
447
    length= copy->to_length - 1;
454
 
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
 
448
    if (copy->from_field->table->in_use->count_cuted_fields)
455
449
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
456
450
                                  ER_WARN_DATA_TRUNCATED, 1);
457
451
  }
487
481
  if (length > copy->to_length- HA_KEY_BLOB_LENGTH)
488
482
  {
489
483
    length=copy->to_length-HA_KEY_BLOB_LENGTH;
490
 
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
 
484
    if (copy->from_field->table->in_use->count_cuted_fields)
491
485
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
492
486
                                  ER_WARN_DATA_TRUNCATED, 1);
493
487
  }
542
536
    to_ptr[0]= 1;                             // Null as default value
543
537
    to_null_ptr= (unsigned char*) to_ptr++;
544
538
    to_bit= 1;
545
 
    if (from->getTable()->maybe_null)
 
539
    if (from->table->maybe_null)
546
540
    {
547
 
      null_row= &from->getTable()->null_row;
 
541
      null_row= &from->table->null_row;
548
542
      do_copy= do_outer_field_to_null_str;
549
543
    }
550
544
    else
600
594
      to_null_ptr= to->null_ptr;
601
595
      to_bit= to->null_bit;
602
596
      if (from_null_ptr)
603
 
      {
604
597
        do_copy= do_copy_null;
605
 
      }
606
598
      else
607
599
      {
608
 
        null_row= &from->getTable()->null_row;
 
600
        null_row= &from->table->null_row;
609
601
        do_copy= do_outer_field_null;
610
602
      }
611
603
    }
612
604
    else
613
605
    {
614
606
      if (to_field->type() == DRIZZLE_TYPE_TIMESTAMP)
615
 
      {
616
607
        do_copy= do_copy_timestamp;               // Automatic timestamp
617
 
      }
618
 
      else if (to_field == to_field->getTable()->next_number_field)
619
 
      {
 
608
      else if (to_field == to_field->table->next_number_field)
620
609
        do_copy= do_copy_next_number;
621
 
      }
622
610
      else
623
 
      {
624
611
        do_copy= do_copy_not_null;
625
 
      }
626
612
    }
627
613
  }
628
614
  else if (to_field->real_maybe_null())
646
632
CopyField::Copy_func *
647
633
CopyField::get_copy_func(Field *to,Field *from)
648
634
{
649
 
  bool compatible_db_low_byte_first= (to->getTable()->getShare()->db_low_byte_first ==
650
 
                                     from->getTable()->getShare()->db_low_byte_first);
 
635
  bool compatible_db_low_byte_first= (to->table->s->db_low_byte_first ==
 
636
                                     from->table->s->db_low_byte_first);
651
637
  if (to->flags & BLOB_FLAG)
652
638
  {
653
639
    if (!(from->flags & BLOB_FLAG) || from->charset() != to->charset())
655
641
    if (from_length != to_length || !compatible_db_low_byte_first)
656
642
    {
657
643
      // 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;
 
644
      to_ptr+= to_length - to->table->s->blob_ptr_size;
 
645
      from_ptr+= from_length- from->table->s->blob_ptr_size;
660
646
      return do_copy_blob;
661
647
    }
662
648
  }
674
660
      */
675
661
      if (to->real_type() != from->real_type() ||
676
662
          !compatible_db_low_byte_first ||
677
 
          (((to->getTable()->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) && to->type() == DRIZZLE_TYPE_DATE) || to->type() == DRIZZLE_TYPE_DATETIME))
 
663
          (((to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) && to->type() == DRIZZLE_TYPE_DATE) || to->type() == DRIZZLE_TYPE_DATETIME))
678
664
      {
679
665
        if (from->real_type() == DRIZZLE_TYPE_ENUM)
680
666
        {
682
668
          {
683
669
            return do_field_int;  // Convert SET to number
684
670
          }
685
 
 
 
671
          
686
672
          return do_field_string;
687
673
        }
688
674
      }
689
 
 
 
675
      
690
676
      if (to->real_type() == DRIZZLE_TYPE_ENUM)
691
677
      {
692
678
        if (!to->eq_def(from))
702
688
        return do_field_string;
703
689
      else if (to->real_type() == DRIZZLE_TYPE_VARCHAR)
704
690
      {
705
 
        /* Field_blob is not part of the Field_varstring hierarchy,
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.
712
 
        */
713
 
        if (from->flags & BLOB_FLAG)
714
 
          return do_field_string;
715
 
 
716
 
        if ((static_cast<Field_varstring*>(to))->pack_length_no_ptr() !=
717
 
            (static_cast<Field_varstring*>(from))->pack_length_no_ptr())
 
691
        if (((Field_varstring*) to)->length_bytes !=
 
692
            ((Field_varstring*) from)->length_bytes)
718
693
        {
719
694
          return do_field_string;
720
695
        }
721
 
 
 
696
        
722
697
        if (to_length != from_length)
723
698
        {
724
 
          return (((Field_varstring*) to)->pack_length_no_ptr() == 1 ?
 
699
          return (((Field_varstring*) to)->length_bytes == 1 ?
725
700
                  (from->charset()->mbmaxlen == 1 ? do_varstring1 :
726
 
                   do_varstring1_mb) :
 
701
                                                    do_varstring1_mb) :
727
702
                  (from->charset()->mbmaxlen == 1 ? do_varstring2 :
728
 
                   do_varstring2_mb));
 
703
                                                    do_varstring2_mb));
729
704
        }
730
705
      }
731
706
      else if (to_length < from_length)
784
759
int field_conv(Field *to,Field *from)
785
760
{
786
761
  if (to->real_type() == from->real_type() &&
787
 
      !(to->type() == DRIZZLE_TYPE_BLOB && to->getTable()->copy_blobs))
 
762
      !(to->type() == DRIZZLE_TYPE_BLOB && to->table->copy_blobs))
788
763
  {
789
764
    /* Please god, will someone rewrite this to be readable :( */
790
765
    if (to->pack_length() == from->pack_length() &&
792
767
        to->real_type() != DRIZZLE_TYPE_ENUM &&
793
768
        (to->real_type() != DRIZZLE_TYPE_DECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
794
769
        from->charset() == to->charset() &&
795
 
        to->getTable()->getShare()->db_low_byte_first == from->getTable()->getShare()->db_low_byte_first &&
796
 
        (!(to->getTable()->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || (to->type() != DRIZZLE_TYPE_DATE && to->type() != DRIZZLE_TYPE_DATETIME)) &&
797
 
        (from->real_type() != DRIZZLE_TYPE_VARCHAR || ((Field_varstring*)from)->pack_length_no_ptr() == ((Field_varstring*)to)->pack_length_no_ptr()))
 
770
        to->table->s->db_low_byte_first == from->table->s->db_low_byte_first &&
 
771
        (!(to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || (to->type() != DRIZZLE_TYPE_DATE && to->type() != DRIZZLE_TYPE_DATETIME)) &&
 
772
        (from->real_type() != DRIZZLE_TYPE_VARCHAR || ((Field_varstring*)from)->length_bytes == ((Field_varstring*)to)->length_bytes))
798
773
    {                                           // Identical fields
799
774
      /* This may happen if one does 'UPDATE ... SET x=x' */
800
775
      if (to->ptr != from->ptr)
805
780
  if (to->type() == DRIZZLE_TYPE_BLOB)
806
781
  {                                             // Be sure the value is stored
807
782
    Field_blob *blob=(Field_blob*) to;
808
 
    from->val_str_internal(&blob->value);
 
783
    from->val_str(&blob->value);
809
784
    /*
810
785
      Copy value if copy_blobs is set, or source is not a string and
811
786
      we have a pointer to its internal string conversion buffer.
812
787
    */
813
 
    if (to->getTable()->copy_blobs ||
 
788
    if (to->table->copy_blobs ||
814
789
        (!blob->value.is_alloced() &&
815
790
         from->real_type() != DRIZZLE_TYPE_VARCHAR))
816
791
      blob->value.copy();
829
804
  {
830
805
    char buff[MAX_FIELD_WIDTH];
831
806
    String result(buff,sizeof(buff),from->charset());
832
 
    from->val_str_internal(&result);
 
807
    from->val_str(&result);
833
808
    /*
834
809
      We use c_ptr_quick() here to make it easier if to is a float/double
835
810
      as the conversion routines will do a copy of the result doesn't