~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field_conv.cc

Added the testsuite location finding code to support in-plugin-dir test suites.

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
/**
24
24
    gives much more speed.
25
25
*/
26
26
 
27
 
#include "config.h"
 
27
#include <drizzled/server_includes.h>
28
28
#include <drizzled/error.h>
29
29
#include <drizzled/table.h>
30
30
#include <drizzled/session.h>
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>
46
46
#include <drizzled/field/varstring.h>
47
47
 
48
 
namespace drizzled
49
 
{
50
48
 
51
49
static void do_field_eq(CopyField *copy)
52
50
{
143
141
    return 0;
144
142
  }
145
143
  field->reset();
146
 
  if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
 
144
  if (field->table->in_use->count_cuted_fields == CHECK_FIELD_WARN)
147
145
  {
148
146
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED, 1);
149
147
    return 0;
150
148
  }
151
 
  if (!field->getTable()->in_use->no_errors)
 
149
  if (!field->table->in_use->no_errors)
152
150
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
153
151
  return -1;
154
152
}
194
192
    return 0;                                   // Ok to set time to NULL
195
193
  }
196
194
  field->reset();
197
 
  if (field == field->getTable()->next_number_field)
 
195
  if (field == field->table->next_number_field)
198
196
  {
199
 
    field->getTable()->auto_increment_field_not_null= false;
 
197
    field->table->auto_increment_field_not_null= false;
200
198
    return 0;                             // field is set in fill_record()
201
199
  }
202
 
  if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
 
200
  if (field->table->in_use->count_cuted_fields == CHECK_FIELD_WARN)
203
201
  {
204
202
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_BAD_NULL_ERROR, 1);
205
203
    return 0;
206
204
  }
207
 
  if (!field->getTable()->in_use->no_errors)
 
205
  if (!field->table->in_use->no_errors)
208
206
    my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
209
207
  return -1;
210
208
}
248
246
 
249
247
static void do_copy_not_null(CopyField *copy)
250
248
{
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)
 
249
  if (*copy->from_null_ptr & copy->from_bit)
256
250
  {
257
251
    copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
258
252
                                ER_WARN_DATA_TRUNCATED, 1);
259
253
    copy->to_field->reset();
260
254
  }
261
255
  else
262
 
  {
263
256
    (copy->do_copy2)(copy);
264
 
  }
265
257
}
266
258
 
267
259
 
290
282
  if (*copy->from_null_ptr & copy->from_bit)
291
283
  {
292
284
    /* Same as in set_field_to_null_with_conversions() */
293
 
    copy->to_field->getTable()->auto_increment_field_not_null= false;
 
285
    copy->to_field->table->auto_increment_field_not_null= false;
294
286
    copy->to_field->reset();
295
287
  }
296
288
  else
307
299
 
308
300
static void do_conv_blob(CopyField *copy)
309
301
{
310
 
  copy->from_field->val_str_internal(&copy->tmp);
 
302
  copy->from_field->val_str(&copy->tmp);
311
303
  ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
312
304
                                         copy->tmp.length(),
313
305
                                         copy->tmp.charset());
319
311
{
320
312
  char buff[MAX_FIELD_WIDTH];
321
313
  String res(buff, sizeof(buff), copy->tmp.charset());
322
 
  copy->from_field->val_str_internal(&res);
 
314
  copy->from_field->val_str(&res);
323
315
  copy->tmp.copy(res);
324
316
  ((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
325
317
                                         copy->tmp.length(),
331
323
{
332
324
  char buff[MAX_FIELD_WIDTH];
333
325
  copy->tmp.set_quick(buff,sizeof(buff),copy->tmp.charset());
334
 
  copy->from_field->val_str_internal(&copy->tmp);
 
326
  copy->from_field->val_str(&copy->tmp);
335
327
  copy->to_field->store(copy->tmp.c_ptr_quick(),copy->tmp.length(),
336
328
                        copy->tmp.charset());
337
329
}
451
443
  if (length > copy->to_length- 1)
452
444
  {
453
445
    length= copy->to_length - 1;
454
 
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
 
446
    if (copy->from_field->table->in_use->count_cuted_fields)
455
447
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
456
448
                                  ER_WARN_DATA_TRUNCATED, 1);
457
449
  }
487
479
  if (length > copy->to_length- HA_KEY_BLOB_LENGTH)
488
480
  {
489
481
    length=copy->to_length-HA_KEY_BLOB_LENGTH;
490
 
    if (copy->from_field->getTable()->in_use->count_cuted_fields)
 
482
    if (copy->from_field->table->in_use->count_cuted_fields)
491
483
      copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
492
484
                                  ER_WARN_DATA_TRUNCATED, 1);
493
485
  }
542
534
    to_ptr[0]= 1;                             // Null as default value
543
535
    to_null_ptr= (unsigned char*) to_ptr++;
544
536
    to_bit= 1;
545
 
    if (from->getTable()->maybe_null)
 
537
    if (from->table->maybe_null)
546
538
    {
547
 
      null_row= &from->getTable()->null_row;
 
539
      null_row= &from->table->null_row;
548
540
      do_copy= do_outer_field_to_null_str;
549
541
    }
550
542
    else
600
592
      to_null_ptr= to->null_ptr;
601
593
      to_bit= to->null_bit;
602
594
      if (from_null_ptr)
603
 
      {
604
595
        do_copy= do_copy_null;
605
 
      }
606
596
      else
607
597
      {
608
 
        null_row= &from->getTable()->null_row;
 
598
        null_row= &from->table->null_row;
609
599
        do_copy= do_outer_field_null;
610
600
      }
611
601
    }
612
602
    else
613
603
    {
614
604
      if (to_field->type() == DRIZZLE_TYPE_TIMESTAMP)
615
 
      {
616
605
        do_copy= do_copy_timestamp;               // Automatic timestamp
617
 
      }
618
 
      else if (to_field == to_field->getTable()->next_number_field)
619
 
      {
 
606
      else if (to_field == to_field->table->next_number_field)
620
607
        do_copy= do_copy_next_number;
621
 
      }
622
608
      else
623
 
      {
624
609
        do_copy= do_copy_not_null;
625
 
      }
626
610
    }
627
611
  }
628
612
  else if (to_field->real_maybe_null())
646
630
CopyField::Copy_func *
647
631
CopyField::get_copy_func(Field *to,Field *from)
648
632
{
649
 
  bool compatible_db_low_byte_first= (to->getTable()->getShare()->db_low_byte_first ==
650
 
                                     from->getTable()->getShare()->db_low_byte_first);
 
633
  bool compatible_db_low_byte_first= (to->table->s->db_low_byte_first ==
 
634
                                     from->table->s->db_low_byte_first);
651
635
  if (to->flags & BLOB_FLAG)
652
636
  {
653
637
    if (!(from->flags & BLOB_FLAG) || from->charset() != to->charset())
655
639
    if (from_length != to_length || !compatible_db_low_byte_first)
656
640
    {
657
641
      // 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;
 
642
      to_ptr+= to_length - to->table->s->blob_ptr_size;
 
643
      from_ptr+= from_length- from->table->s->blob_ptr_size;
660
644
      return do_copy_blob;
661
645
    }
662
646
  }
674
658
      */
675
659
      if (to->real_type() != from->real_type() ||
676
660
          !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))
 
661
          (((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
662
      {
679
663
        if (from->real_type() == DRIZZLE_TYPE_ENUM)
680
664
        {
682
666
          {
683
667
            return do_field_int;  // Convert SET to number
684
668
          }
685
 
 
 
669
          
686
670
          return do_field_string;
687
671
        }
688
672
      }
689
 
 
 
673
      
690
674
      if (to->real_type() == DRIZZLE_TYPE_ENUM)
691
675
      {
692
676
        if (!to->eq_def(from))
702
686
        return do_field_string;
703
687
      else if (to->real_type() == DRIZZLE_TYPE_VARCHAR)
704
688
      {
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())
 
689
        if (((Field_varstring*) to)->length_bytes !=
 
690
            ((Field_varstring*) from)->length_bytes)
718
691
        {
719
692
          return do_field_string;
720
693
        }
721
 
 
 
694
        
722
695
        if (to_length != from_length)
723
696
        {
724
 
          return (((Field_varstring*) to)->pack_length_no_ptr() == 1 ?
 
697
          return (((Field_varstring*) to)->length_bytes == 1 ?
725
698
                  (from->charset()->mbmaxlen == 1 ? do_varstring1 :
726
 
                   do_varstring1_mb) :
 
699
                                                    do_varstring1_mb) :
727
700
                  (from->charset()->mbmaxlen == 1 ? do_varstring2 :
728
 
                   do_varstring2_mb));
 
701
                                                    do_varstring2_mb));
729
702
        }
730
703
      }
731
704
      else if (to_length < from_length)
784
757
int field_conv(Field *to,Field *from)
785
758
{
786
759
  if (to->real_type() == from->real_type() &&
787
 
      !(to->type() == DRIZZLE_TYPE_BLOB && to->getTable()->copy_blobs))
 
760
      !(to->type() == DRIZZLE_TYPE_BLOB && to->table->copy_blobs))
788
761
  {
789
762
    /* Please god, will someone rewrite this to be readable :( */
790
763
    if (to->pack_length() == from->pack_length() &&
791
764
        !(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) &&
792
765
        to->real_type() != DRIZZLE_TYPE_ENUM &&
793
 
        (to->real_type() != DRIZZLE_TYPE_DECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
 
766
        (to->real_type() != DRIZZLE_TYPE_NEWDECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
794
767
        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()))
 
768
        to->table->s->db_low_byte_first == from->table->s->db_low_byte_first &&
 
769
        (!(to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || (to->type() != DRIZZLE_TYPE_DATE && to->type() != DRIZZLE_TYPE_DATETIME)) &&
 
770
        (from->real_type() != DRIZZLE_TYPE_VARCHAR || ((Field_varstring*)from)->length_bytes == ((Field_varstring*)to)->length_bytes))
798
771
    {                                           // Identical fields
799
772
      /* This may happen if one does 'UPDATE ... SET x=x' */
800
773
      if (to->ptr != from->ptr)
805
778
  if (to->type() == DRIZZLE_TYPE_BLOB)
806
779
  {                                             // Be sure the value is stored
807
780
    Field_blob *blob=(Field_blob*) to;
808
 
    from->val_str_internal(&blob->value);
 
781
    from->val_str(&blob->value);
809
782
    /*
810
783
      Copy value if copy_blobs is set, or source is not a string and
811
784
      we have a pointer to its internal string conversion buffer.
812
785
    */
813
 
    if (to->getTable()->copy_blobs ||
 
786
    if (to->table->copy_blobs ||
814
787
        (!blob->value.is_alloced() &&
815
788
         from->real_type() != DRIZZLE_TYPE_VARCHAR))
816
789
      blob->value.copy();
829
802
  {
830
803
    char buff[MAX_FIELD_WIDTH];
831
804
    String result(buff,sizeof(buff),from->charset());
832
 
    from->val_str_internal(&result);
 
805
    from->val_str(&result);
833
806
    /*
834
807
      We use c_ptr_quick() here to make it easier if to is a float/double
835
808
      as the conversion routines will do a copy of the result doesn't
848
821
  else
849
822
    return to->store(from->val_int(), test(from->flags & UNSIGNED_FLAG));
850
823
}
851
 
 
852
 
} /* namespace drizzled */