~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.cc

  • Committer: lbieber
  • Date: 2009-06-12 17:51:58 UTC
  • mfrom: (1061 staging)
  • mto: (1061.1.3 merge-all)
  • mto: This revision was merged to the branch mainline in revision 1062.
  • Revision ID: lbieber@lbieber-laptop-20090612175158-v6k2st8gvs81z3dn
sync with latest from trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
/**
22
 
  @file
 
22
 * @file This file implements the Field class and API
 
23
 */
23
24
 
24
 
  @brief
25
 
  This file implements classes defined in field.h
26
 
*/
27
 
#include <drizzled/server_includes.h>
28
 
#include "sql_select.h"
 
25
#include "drizzled/server_includes.h"
29
26
#include <errno.h>
30
 
#include <drizzled/error.h>
31
 
#include <drizzled/field/str.h>
32
 
#include <drizzled/field/num.h>
33
 
#include <drizzled/field/blob.h>
34
 
#include <drizzled/field/enum.h>
35
 
#include <drizzled/field/null.h>
36
 
#include <drizzled/field/date.h>
37
 
#include <drizzled/field/decimal.h>
38
 
#include <drizzled/field/real.h>
39
 
#include <drizzled/field/double.h>
40
 
#include <drizzled/field/long.h>
41
 
#include <drizzled/field/int64_t.h>
42
 
#include <drizzled/field/num.h>
43
 
#include <drizzled/field/timestamp.h>
44
 
#include <drizzled/field/datetime.h>
45
 
#include <drizzled/field/varstring.h>
46
 
 
 
27
#include "drizzled/sql_select.h"
 
28
#include "drizzled/error.h"
 
29
#include "drizzled/field/str.h"
 
30
#include "drizzled/field/num.h"
 
31
#include "drizzled/field/blob.h"
 
32
#include "drizzled/field/enum.h"
 
33
#include "drizzled/field/null.h"
 
34
#include "drizzled/field/date.h"
 
35
#include "drizzled/field/decimal.h"
 
36
#include "drizzled/field/real.h"
 
37
#include "drizzled/field/double.h"
 
38
#include "drizzled/field/long.h"
 
39
#include "drizzled/field/int64_t.h"
 
40
#include "drizzled/field/num.h"
 
41
#include "drizzled/field/timestamp.h"
 
42
#include "drizzled/field/datetime.h"
 
43
#include "drizzled/field/varstring.h"
47
44
 
48
45
/*****************************************************************************
49
46
  Instansiate templates and static variables
54
51
template class List_iterator<Create_field>;
55
52
#endif
56
53
 
57
 
 
58
54
static enum_field_types
59
55
field_types_merge_rules [DRIZZLE_TYPE_MAX+1][DRIZZLE_TYPE_MAX+1]=
60
56
{
383
379
  },
384
380
};
385
381
 
386
 
/**
387
 
  Return type of which can carry value of both given types in UNION result.
388
 
 
389
 
  @param a  type for merging
390
 
  @param b  type for merging
391
 
 
392
 
  @return
393
 
    type of field
394
 
*/
395
 
 
396
 
enum_field_types Field::field_type_merge(enum_field_types a,
397
 
                                         enum_field_types b)
398
 
{
399
 
  assert(a <= DRIZZLE_TYPE_MAX);
400
 
  assert(b <= DRIZZLE_TYPE_MAX);
401
 
  return field_types_merge_rules[a][b];
402
 
}
403
 
 
404
 
 
405
382
static Item_result field_types_result_type [DRIZZLE_TYPE_MAX+1]=
406
383
{
407
384
  //DRIZZLE_TYPE_TINY
430
407
  STRING_RESULT,
431
408
};
432
409
 
433
 
 
434
 
/*
435
 
  Test if the given string contains important data:
436
 
  not spaces for character string,
437
 
  or any data for binary string.
438
 
 
439
 
  SYNOPSIS
440
 
    test_if_important_data()
441
 
    cs          Character set
442
 
    str         String to test
443
 
    strend      String end
444
 
 
445
 
  RETURN
446
 
    false - If string does not have important data
447
 
    true  - If string has some important data
448
 
*/
449
 
 
450
 
bool
451
 
test_if_important_data(const CHARSET_INFO * const cs, const char *str,
452
 
                       const char *strend)
 
410
bool test_if_important_data(const CHARSET_INFO * const cs, 
 
411
                            const char *str,
 
412
                            const char *strend)
453
413
{
454
414
  if (cs != &my_charset_bin)
455
415
    str+= cs->cset->scan(cs, str, strend, MY_SEQ_SPACES);
456
416
  return (str < strend);
457
417
}
458
418
 
 
419
enum_field_types Field::field_type_merge(enum_field_types a,
 
420
                                         enum_field_types b)
 
421
{
 
422
  assert(a <= DRIZZLE_TYPE_MAX);
 
423
  assert(b <= DRIZZLE_TYPE_MAX);
 
424
  return field_types_merge_rules[a][b];
 
425
}
459
426
 
460
427
Item_result Field::result_merge_type(enum_field_types field_type)
461
428
{
463
430
  return field_types_result_type[field_type];
464
431
}
465
432
 
466
 
 
467
433
bool Field::eq(Field *field)
468
434
{
469
435
  return (ptr == field->ptr && null_ptr == field->null_ptr &&
470
436
          null_bit == field->null_bit);
471
437
}
472
438
 
473
 
 
474
439
uint32_t Field::pack_length() const
475
440
{
476
441
  return field_length;
477
442
}
478
443
 
479
 
 
480
444
uint32_t Field::pack_length_in_rec() const
481
445
{
482
446
  return pack_length();
483
447
}
484
448
 
485
 
 
486
449
uint32_t Field::pack_length_from_metadata(uint32_t field_metadata)
487
450
{
488
451
  return field_metadata;
489
452
}
490
453
 
491
 
 
492
454
uint32_t Field::row_pack_length()
493
455
{
494
456
  return 0;
495
457
}
496
458
 
497
 
 
498
459
int Field::save_field_metadata(unsigned char *first_byte)
499
460
{
500
461
  return do_save_field_metadata(first_byte);
501
462
}
502
463
 
503
 
 
504
464
uint32_t Field::data_length()
505
465
{
506
466
  return pack_length();
507
467
}
508
468
 
509
 
 
510
469
uint32_t Field::used_length()
511
470
{
512
471
  return pack_length();
513
472
}
514
473
 
515
 
 
516
474
uint32_t Field::sort_length() const
517
475
{
518
476
  return pack_length();
519
477
}
520
478
 
521
 
 
522
479
uint32_t Field::max_data_length() const
523
480
{
524
481
  return pack_length();
525
482
}
526
483
 
527
 
 
528
484
int Field::reset(void)
529
485
{
530
486
  memset(ptr, 0, pack_length());
531
487
  return 0;
532
488
}
533
489
 
534
 
 
535
490
void Field::reset_fields()
536
491
{}
537
492
 
538
 
 
539
493
void Field::set_default()
540
494
{
541
 
  my_ptrdiff_t l_offset= (my_ptrdiff_t) (table->getDefaultValues() - table->record[0]);
 
495
  ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->record[0]);
542
496
  memcpy(ptr, ptr + l_offset, pack_length());
543
497
  if (null_ptr)
544
498
    *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
545
499
 
546
 
  if(this == table->next_number_field)
547
 
    table->auto_increment_field_not_null= 0;
 
500
  if (this == table->next_number_field)
 
501
    table->auto_increment_field_not_null= false;
548
502
}
549
503
 
550
 
 
551
504
bool Field::binary() const
552
505
{
553
 
  return 1;
 
506
  return true;
554
507
}
555
508
 
556
 
 
557
509
bool Field::zero_pack() const
558
510
{
559
 
  return 1;
 
511
  return true;
560
512
}
561
513
 
562
 
 
563
514
enum ha_base_keytype Field::key_type() const
564
515
{
565
516
  return HA_KEYTYPE_BINARY;
566
517
}
567
518
 
568
 
 
569
519
uint32_t Field::key_length() const
570
520
{
571
521
  return pack_length();
572
522
}
573
523
 
574
 
 
575
524
enum_field_types Field::real_type() const
576
525
{
577
526
  return type();
578
527
}
579
528
 
580
 
 
581
529
int Field::cmp_max(const unsigned char *a, const unsigned char *b, uint32_t)
582
530
{
583
531
  return cmp(a, b);
584
532
}
585
533
 
586
 
 
587
534
int Field::cmp_binary(const unsigned char *a,const unsigned char *b, uint32_t)
588
535
{
589
536
  return memcmp(a,b,pack_length());
590
537
}
591
538
 
592
 
 
593
539
int Field::cmp_offset(uint32_t row_offset)
594
540
{
595
541
  return cmp(ptr,ptr+row_offset);
596
542
}
597
543
 
598
 
 
599
544
int Field::cmp_binary_offset(uint32_t row_offset)
600
545
{
601
546
  return cmp_binary(ptr, ptr+row_offset);
602
547
}
603
548
 
604
 
 
605
549
int Field::key_cmp(const unsigned char *a,const unsigned char *b)
606
550
{
607
551
  return cmp(a, b);
608
552
}
609
553
 
610
 
 
611
554
int Field::key_cmp(const unsigned char *str, uint32_t)
612
555
{
613
556
  return cmp(ptr,str);
614
557
}
615
558
 
616
 
 
617
559
uint32_t Field::decimals() const
618
560
{
619
561
  return 0;
620
562
}
621
563
 
622
 
 
623
564
bool Field::is_null(my_ptrdiff_t row_offset)
624
565
{
625
566
  return null_ptr ?
627
568
    table->null_row;
628
569
}
629
570
 
630
 
 
631
571
bool Field::is_real_null(my_ptrdiff_t row_offset)
632
572
{
633
573
  return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : false;
634
574
}
635
575
 
636
 
 
637
576
bool Field::is_null_in_record(const unsigned char *record)
638
577
{
639
 
  if (!null_ptr)
 
578
  if (! null_ptr)
640
579
    return false;
641
 
  return test(record[(uint32_t) (null_ptr -table->record[0])] &
642
 
              null_bit);
 
580
  return test(record[(uint32_t) (null_ptr -table->record[0])] & null_bit);
643
581
}
644
582
 
645
 
 
646
583
bool Field::is_null_in_record_with_offset(my_ptrdiff_t with_offset)
647
584
{
648
 
  if (!null_ptr)
 
585
  if (! null_ptr)
649
586
    return false;
650
587
  return test(null_ptr[with_offset] & null_bit);
651
588
}
652
589
 
653
 
 
654
590
void Field::set_null(my_ptrdiff_t row_offset)
655
591
{
656
592
  if (null_ptr)
657
593
    null_ptr[row_offset]|= null_bit;
658
594
}
659
595
 
660
 
 
661
596
void Field::set_notnull(my_ptrdiff_t row_offset)
662
597
{
663
598
  if (null_ptr)
664
599
    null_ptr[row_offset]&= (unsigned char) ~null_bit;
665
600
}
666
601
 
667
 
 
668
602
bool Field::maybe_null(void)
669
603
{
670
604
  return null_ptr != 0 || table->maybe_null;
671
605
}
672
606
 
673
 
 
674
607
bool Field::real_maybe_null(void)
675
608
{
676
609
  return null_ptr != 0;
677
610
}
678
611
 
679
 
 
680
 
size_t Field::last_null_byte() const
681
 
{
682
 
  size_t bytes= do_last_null_byte();
683
 
  assert(bytes <= table->getNullBytes());
684
 
  return bytes;
685
 
}
686
 
 
687
 
 
688
 
/*****************************************************************************
689
 
  Static help functions
690
 
*****************************************************************************/
691
 
 
692
612
bool Field::type_can_have_key_part(enum enum_field_types type)
693
613
{
694
614
  switch (type) {
700
620
  }
701
621
}
702
622
 
703
 
 
704
 
/**
705
 
  Process decimal library return codes and issue warnings for overflow and
706
 
  truncation.
707
 
 
708
 
  @param op_result  decimal library return code (E_DEC_* see include/decimal.h)
709
 
 
710
 
  @retval
711
 
    E_DEC_OVERFLOW   there was overflow
712
 
    E_DEC_TRUNCATED  there was truncation
713
 
  @retval
714
 
    0  no error or there was some other error except overflow or truncation
715
 
*/
716
 
 
717
623
int Field::warn_if_overflow(int op_result)
718
624
{
719
625
  if (op_result == E_DEC_OVERFLOW)
729
635
  return 0;
730
636
}
731
637
 
732
 
 
733
638
void Field::init(Table *table_arg)
734
639
{
735
640
  orig_table= table= table_arg;
736
641
  table_name= &table_arg->alias;
737
642
}
738
643
 
739
 
 
740
 
#ifdef NOT_USED
741
 
static bool test_if_real(const char *str,int length, const CHARSET_INFO * const cs)
742
 
{
743
 
  cs= system_charset_info; // QQ move test_if_real into CHARSET_INFO struct
744
 
 
745
 
  while (length && my_isspace(cs,*str))
746
 
  {                                             // Allow start space
747
 
    length--; str++;
748
 
  }
749
 
  if (!length)
750
 
    return 0;
751
 
  if (*str == '+' || *str == '-')
752
 
  {
753
 
    length--; str++;
754
 
    if (!length || !(my_isdigit(cs,*str) || *str == '.'))
755
 
      return 0;
756
 
  }
757
 
  while (length && my_isdigit(cs,*str))
758
 
  {
759
 
    length--; str++;
760
 
  }
761
 
  if (!length)
762
 
    return 1;
763
 
  if (*str == '.')
764
 
  {
765
 
    length--; str++;
766
 
    while (length && my_isdigit(cs,*str))
767
 
    {
768
 
      length--; str++;
769
 
    }
770
 
  }
771
 
  if (!length)
772
 
    return 1;
773
 
  if (*str == 'E' || *str == 'e')
774
 
  {
775
 
    if (length < 3 || (str[1] != '+' && str[1] != '-') ||
776
 
        !my_isdigit(cs,str[2]))
777
 
      return 0;
778
 
    length-=3;
779
 
    str+=3;
780
 
    while (length && my_isdigit(cs,*str))
781
 
    {
782
 
      length--; str++;
783
 
    }
784
 
  }
785
 
  for (; length ; length--, str++)
786
 
  {                                             // Allow end space
787
 
    if (!my_isspace(cs,*str))
788
 
      return 0;
789
 
  }
790
 
  return 1;
791
 
}
792
 
#endif
793
 
 
794
 
 
795
 
/**
796
 
  Interpret field value as an integer but return the result as a string.
797
 
 
798
 
  This is used for printing bit_fields as numbers while debugging.
799
 
*/
800
 
 
801
644
String *Field::val_int_as_str(String *val_buffer, bool unsigned_val)
802
645
{
803
646
  const CHARSET_INFO * const cs= &my_charset_bin;
814
657
  return val_buffer;
815
658
}
816
659
 
817
 
 
818
660
/// This is used as a table name when the table structure is not set up
819
 
Field::Field(unsigned char *ptr_arg,uint32_t length_arg,unsigned char *null_ptr_arg,
 
661
Field::Field(unsigned char *ptr_arg,
 
662
             uint32_t length_arg,
 
663
             unsigned char *null_ptr_arg,
820
664
             unsigned char null_bit_arg,
821
 
             utype unireg_check_arg, const char *field_name_arg)
822
 
  :ptr(ptr_arg), null_ptr(null_ptr_arg),
823
 
   table(0), orig_table(0), table_name(0),
824
 
   field_name(field_name_arg),
825
 
   key_start(0), part_of_key(0), part_of_key_not_clustered(0),
826
 
   part_of_sortkey(0), unireg_check(unireg_check_arg),
827
 
   field_length(length_arg), null_bit(null_bit_arg),
828
 
   is_created_from_null_item(false)
 
665
             utype unireg_check_arg, 
 
666
             const char *field_name_arg)
 
667
  :
 
668
    ptr(ptr_arg),
 
669
    null_ptr(null_ptr_arg),
 
670
    table(NULL),
 
671
    orig_table(NULL),
 
672
    table_name(NULL),
 
673
    field_name(field_name_arg),
 
674
    key_start(0),
 
675
    part_of_key(0),
 
676
    part_of_key_not_clustered(0),
 
677
    part_of_sortkey(0),
 
678
    unireg_check(unireg_check_arg),
 
679
    field_length(length_arg),
 
680
    null_bit(null_bit_arg),
 
681
    is_created_from_null_item(false)
829
682
{
830
 
  flags=null_ptr ? 0: NOT_NULL_FLAG;
 
683
  flags= null_ptr ? 0: NOT_NULL_FLAG;
831
684
  comment.str= (char*) "";
832
 
  comment.length=0;
 
685
  comment.length= 0;
833
686
  field_index= 0;
834
687
}
835
688
 
836
 
 
837
689
void Field::hash(uint32_t *nr, uint32_t *nr2)
838
690
{
839
691
  if (is_null())
848
700
  }
849
701
}
850
702
 
851
 
size_t
852
 
Field::do_last_null_byte() const
853
 
{
854
 
  assert(null_ptr == NULL || null_ptr >= table->record[0]);
855
 
  if (null_ptr)
856
 
    return (size_t) (null_ptr - table->record[0]) + 1;
857
 
  return LAST_NULL_BYTE_UNDEF;
858
 
}
859
 
 
860
 
 
861
703
void Field::copy_from_tmp(int row_offset)
862
704
{
863
705
  memcpy(ptr,ptr+row_offset,pack_length());
870
712
  }
871
713
}
872
714
 
873
 
/**
874
 
   Check to see if field size is compatible with destination.
875
 
 
876
 
   This method is used in row-based replication to verify that the slave's
877
 
   field size is less than or equal to the master's field size. The
878
 
   encoded field metadata (from the master or source) is decoded and compared
879
 
   to the size of this field (the slave or destination).
880
 
 
881
 
   @param   field_metadata   Encoded size in field metadata
882
 
 
883
 
   @retval 0 if this field's size is < the source field's size
884
 
   @retval 1 if this field's size is >= the source field's size
885
 
*/
886
715
int Field::compatible_field_size(uint32_t field_metadata)
887
716
{
888
717
  uint32_t const source_size= pack_length_from_metadata(field_metadata);
890
719
  return (source_size <= destination_size);
891
720
}
892
721
 
893
 
 
894
 
int Field::store(const char *to, uint32_t length,
 
722
int Field::store(const char *to, 
 
723
                 uint32_t length,
895
724
                 const CHARSET_INFO * const cs,
896
725
                 enum_check_fields check_level)
897
726
{
903
732
  return res;
904
733
}
905
734
 
906
 
 
907
 
/**
908
 
   Pack the field into a format suitable for storage and transfer.
909
 
 
910
 
   To implement packing functionality, only the virtual function
911
 
   should be overridden. The other functions are just convenience
912
 
   functions and hence should not be overridden.
913
 
 
914
 
   The value of <code>low_byte_first</code> is dependent on how the
915
 
   packed data is going to be used: for local use, e.g., temporary
916
 
   store on disk or in memory, use the native format since that is
917
 
   faster. For data that is going to be transfered to other machines
918
 
   (e.g., when writing data to the binary log), data should always be
919
 
   stored in little-endian format.
920
 
 
921
 
   @note The default method for packing fields just copy the raw bytes
922
 
   of the record into the destination, but never more than
923
 
   <code>max_length</code> characters.
924
 
 
925
 
   @param to
926
 
   Pointer to memory area where representation of field should be put.
927
 
 
928
 
   @param from
929
 
   Pointer to memory area where record representation of field is
930
 
   stored.
931
 
 
932
 
   @param max_length
933
 
   Maximum length of the field, as given in the column definition. For
934
 
   example, for <code>CHAR(1000)</code>, the <code>max_length</code>
935
 
   is 1000. This information is sometimes needed to decide how to pack
936
 
   the data.
937
 
 
938
 
   @param low_byte_first
939
 
   @c true if integers should be stored little-endian, @c false if
940
 
   native format should be used. Note that for little-endian machines,
941
 
   the value of this flag is a moot point since the native format is
942
 
   little-endian.
943
 
*/
944
 
unsigned char *
945
 
Field::pack(unsigned char *to, const unsigned char *from, uint32_t max_length,
946
 
            bool)
 
735
unsigned char *Field::pack(unsigned char *to, const unsigned char *from, uint32_t max_length, bool)
947
736
{
948
737
  uint32_t length= pack_length();
949
738
  set_if_smaller(length, max_length);
951
740
  return to+length;
952
741
}
953
742
 
954
 
 
955
743
unsigned char *Field::pack(unsigned char *to, const unsigned char *from)
956
744
{
957
 
  unsigned char *result= this->pack(to, from, UINT32_MAX,
958
 
                                    table->s->db_low_byte_first);
 
745
  unsigned char *result= this->pack(to, from, UINT32_MAX, table->s->db_low_byte_first);
959
746
  return(result);
960
747
}
961
748
 
962
 
 
963
 
/**
964
 
   Unpack a field from row data.
965
 
 
966
 
   This method is used to unpack a field from a master whose size of
967
 
   the field is less than that of the slave.
968
 
 
969
 
   The <code>param_data</code> parameter is a two-byte integer (stored
970
 
   in the least significant 16 bits of the unsigned integer) usually
971
 
   consisting of two parts: the real type in the most significant byte
972
 
   and a original pack length in the least significant byte.
973
 
 
974
 
   The exact layout of the <code>param_data</code> field is given by
975
 
   the <code>Table_map_log_event::save_field_metadata()</code>.
976
 
 
977
 
   This is the default method for unpacking a field. It just copies
978
 
   the memory block in byte order (of original pack length bytes or
979
 
   length of field, whichever is smaller).
980
 
 
981
 
   @param   to         Destination of the data
982
 
   @param   from       Source of the data
983
 
   @param   param_data Real type and original pack length of the field
984
 
                       data
985
 
 
986
 
   @param low_byte_first
987
 
   If this flag is @c true, all composite entities (e.g., lengths)
988
 
   should be unpacked in little-endian format; otherwise, the entities
989
 
   are unpacked in native order.
990
 
 
991
 
   @return  New pointer into memory based on from + length of the data
992
 
*/
993
 
const unsigned char *
994
 
Field::unpack(unsigned char* to, const unsigned char *from, uint32_t param_data,
995
 
              bool)
 
749
const unsigned char *Field::unpack(unsigned char* to,
 
750
                                   const unsigned char *from, 
 
751
                                   uint32_t param_data,
 
752
                                   bool)
996
753
{
997
754
  uint32_t length=pack_length();
998
755
  int from_type= 0;
1018
775
            param_data : length;
1019
776
 
1020
777
  memcpy(to, from, param_data > length ? length : len);
1021
 
  return from+len;
 
778
  return (from + len);
1022
779
}
1023
780
 
1024
 
 
1025
 
const unsigned char *Field::unpack(unsigned char* to,
1026
 
                                   const unsigned char *from)
 
781
const unsigned char *Field::unpack(unsigned char* to, const unsigned char *from)
1027
782
{
1028
 
  const unsigned char *result= unpack(to, from, 0U,
1029
 
                                      table->s->db_low_byte_first);
 
783
  const unsigned char *result= unpack(to, from, 0U, table->s->db_low_byte_first);
1030
784
  return(result);
1031
785
}
1032
786
 
1033
 
 
1034
787
uint32_t Field::packed_col_length(const unsigned char *, uint32_t length)
1035
788
{
1036
789
  return length;
1037
790
}
1038
791
 
1039
 
 
1040
792
int Field::pack_cmp(const unsigned char *a, const unsigned char *b,
1041
793
                    uint32_t, bool)
1042
794
{
1043
795
  return cmp(a,b);
1044
796
}
1045
797
 
1046
 
 
1047
798
int Field::pack_cmp(const unsigned char *b, uint32_t, bool)
1048
799
{
1049
800
  return cmp(ptr,b);
1050
801
}
1051
802
 
1052
 
 
1053
803
my_decimal *Field::val_decimal(my_decimal *)
1054
804
{
1055
805
  /* This never have to be called */
1057
807
  return 0;
1058
808
}
1059
809
 
1060
 
 
1061
810
void Field::make_field(Send_field *field)
1062
811
{
1063
812
  if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
1079
828
  }
1080
829
  field->col_name= field_name;
1081
830
  field->charsetnr= charset()->number;
1082
 
  field->length=field_length;
1083
 
  field->type=type();
1084
 
  field->flags=table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
 
831
  field->length= field_length;
 
832
  field->type= type();
 
833
  field->flags= table->maybe_null ? (flags & ~NOT_NULL_FLAG) : flags;
1085
834
  field->decimals= 0;
1086
835
}
1087
836
 
1088
 
 
1089
 
/**
1090
 
  Conversion from decimal to int64_t with checking overflow and
1091
 
  setting correct value (min/max) in case of overflow.
1092
 
 
1093
 
  @param val             value which have to be converted
1094
 
  @param unsigned_flag   type of integer in which we convert val
1095
 
  @param err             variable to pass error code
1096
 
 
1097
 
  @return
1098
 
    value converted from val
1099
 
*/
1100
837
int64_t Field::convert_decimal2int64_t(const my_decimal *val, bool, int *err)
1101
838
{
1102
839
  int64_t i;
1131
868
  return copy->length+ store_length;
1132
869
}
1133
870
 
1134
 
 
1135
871
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
1136
872
{
1137
873
  char buff[40];
1153
889
  return 0;
1154
890
}
1155
891
 
1156
 
/**
1157
 
  This is called when storing a date in a string.
1158
 
 
1159
 
  @note
1160
 
    Needs to be changed if/when we want to support different time formats.
1161
 
*/
1162
 
 
1163
892
int Field::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
1164
893
{
1165
894
  char buff[MAX_DATE_STRING_REP_LENGTH];
1167
896
  return store(buff, length, &my_charset_bin);
1168
897
}
1169
898
 
1170
 
 
1171
899
bool Field::optimize_range(uint32_t idx, uint32_t part)
1172
900
{
1173
901
  return test(table->file->index_flags(idx, part, 1) & HA_READ_RANGE);
1174
902
}
1175
903
 
1176
 
 
1177
904
Field *Field::new_field(MEM_ROOT *root, Table *new_table, bool)
1178
905
{
1179
906
  Field *tmp;
1192
919
  return tmp;
1193
920
}
1194
921
 
1195
 
 
1196
922
Field *Field::new_key_field(MEM_ROOT *root, Table *new_table,
1197
 
                            unsigned char *new_ptr, unsigned char *new_null_ptr,
 
923
                            unsigned char *new_ptr,
 
924
                            unsigned char *new_null_ptr,
1198
925
                            uint32_t new_null_bit)
1199
926
{
1200
927
  Field *tmp;
1201
928
  if ((tmp= new_field(root, new_table, table == new_table)))
1202
929
  {
1203
 
    tmp->ptr=      new_ptr;
 
930
    tmp->ptr= new_ptr;
1204
931
    tmp->null_ptr= new_null_ptr;
1205
932
    tmp->null_bit= new_null_bit;
1206
933
  }
1207
934
  return tmp;
1208
935
}
1209
936
 
1210
 
 
1211
 
/* This is used to generate a field in Table from TableShare */
1212
 
 
1213
937
Field *Field::clone(MEM_ROOT *root, Table *new_table)
1214
938
{
1215
939
  Field *tmp;
1222
946
  return tmp;
1223
947
}
1224
948
 
1225
 
 
1226
949
uint32_t Field::is_equal(Create_field *new_field_ptr)
1227
950
{
1228
951
  return (new_field_ptr->sql_type == real_type());
1229
952
}
1230
953
 
1231
 
/**
1232
 
  @retval
1233
 
    1  if the fields are equally defined
1234
 
  @retval
1235
 
    0  if the fields are unequally defined
1236
 
*/
1237
 
 
1238
954
bool Field::eq_def(Field *field)
1239
955
{
1240
956
  if (real_type() != field->real_type() || charset() != field->charset() ||
1243
959
  return 1;
1244
960
}
1245
961
 
1246
 
/**
1247
 
  @return
1248
 
  returns 1 if the fields are equally defined
1249
 
*/
1250
962
bool Field_enum::eq_def(Field *field)
1251
963
{
1252
964
  if (!Field::eq_def(field))
1265
977
  return 1;
1266
978
}
1267
979
 
1268
 
/*****************************************************************************
1269
 
  Handling of field and Create_field
1270
 
*****************************************************************************/
1271
 
 
1272
 
/**
1273
 
  Convert create_field::length from number of characters to number of bytes.
1274
 
*/
1275
 
 
1276
 
void Create_field::create_length_to_internal_length(void)
1277
 
{
1278
 
  switch (sql_type) {
1279
 
  case DRIZZLE_TYPE_BLOB:
1280
 
  case DRIZZLE_TYPE_VARCHAR:
1281
 
    length*= charset->mbmaxlen;
1282
 
    key_length= length;
1283
 
    pack_length= calc_pack_length(sql_type, length);
1284
 
    break;
1285
 
  case DRIZZLE_TYPE_ENUM:
1286
 
    /* Pack_length already calculated in ::init() */
1287
 
    length*= charset->mbmaxlen;
1288
 
    key_length= pack_length;
1289
 
    break;
1290
 
  case DRIZZLE_TYPE_NEWDECIMAL:
1291
 
    key_length= pack_length=
1292
 
      my_decimal_get_binary_size(my_decimal_length_to_precision(length,
1293
 
                                                                decimals,
1294
 
                                                                flags &
1295
 
                                                                UNSIGNED_FLAG),
1296
 
                                 decimals);
1297
 
    break;
1298
 
  default:
1299
 
    key_length= pack_length= calc_pack_length(sql_type, length);
1300
 
    break;
1301
 
  }
1302
 
}
1303
 
 
1304
 
 
1305
 
/**
1306
 
  Init for a tmp table field. To be extended if need be.
1307
 
*/
1308
 
void Create_field::init_for_tmp_table(enum_field_types sql_type_arg,
1309
 
                                      uint32_t length_arg, uint32_t decimals_arg,
1310
 
                                      bool maybe_null, bool is_unsigned)
1311
 
{
1312
 
  field_name= "";
1313
 
  sql_type= sql_type_arg;
1314
 
  char_length= length= length_arg;;
1315
 
  unireg_check= Field::NONE;
1316
 
  interval= 0;
1317
 
  charset= &my_charset_bin;
1318
 
  pack_flag= (FIELDFLAG_NUMBER |
1319
 
              ((decimals_arg & FIELDFLAG_MAX_DEC) << FIELDFLAG_DEC_SHIFT) |
1320
 
              (maybe_null ? FIELDFLAG_MAYBE_NULL : 0) |
1321
 
              (is_unsigned ? 0 : FIELDFLAG_DECIMAL));
1322
 
}
1323
 
 
1324
 
 
1325
 
/**
1326
 
  Initialize field definition for create.
1327
 
 
1328
 
  @param session                   Thread handle
1329
 
  @param fld_name              Field name
1330
 
  @param fld_type              Field type
1331
 
  @param fld_length            Field length
1332
 
  @param fld_decimals          Decimal (if any)
1333
 
  @param fld_type_modifier     Additional type information
1334
 
  @param fld_default_value     Field default value (if any)
1335
 
  @param fld_on_update_value   The value of ON UPDATE clause
1336
 
  @param fld_comment           Field comment
1337
 
  @param fld_change            Field change
1338
 
  @param fld_interval_list     Interval list (if any)
1339
 
  @param fld_charset           Field charset
1340
 
 
1341
 
  @retval
1342
 
    false on success
1343
 
  @retval
1344
 
    true  on error
1345
 
*/
1346
 
 
1347
 
bool Create_field::init(Session *, char *fld_name, enum_field_types fld_type,
1348
 
                        char *fld_length, char *fld_decimals,
1349
 
                        uint32_t fld_type_modifier, Item *fld_default_value,
1350
 
                        Item *fld_on_update_value, LEX_STRING *fld_comment,
1351
 
                        char *fld_change, List<String> *fld_interval_list,
1352
 
                        const CHARSET_INFO * const fld_charset,
1353
 
                        uint32_t, enum column_format_type column_format_in)
1354
 
                        
1355
 
{
1356
 
  uint32_t sign_len, allowed_type_modifier= 0;
1357
 
  uint32_t max_field_charlength= MAX_FIELD_CHARLENGTH;
1358
 
 
1359
 
  field= 0;
1360
 
  field_name= fld_name;
1361
 
  def= fld_default_value;
1362
 
  flags= fld_type_modifier;
1363
 
  flags|= (((uint32_t)column_format_in & COLUMN_FORMAT_MASK) << COLUMN_FORMAT_FLAGS);
1364
 
  unireg_check= (fld_type_modifier & AUTO_INCREMENT_FLAG ?
1365
 
                 Field::NEXT_NUMBER : Field::NONE);
1366
 
  decimals= fld_decimals ? (uint32_t)atoi(fld_decimals) : 0;
1367
 
  if (decimals >= NOT_FIXED_DEC)
1368
 
  {
1369
 
    my_error(ER_TOO_BIG_SCALE, MYF(0), decimals, fld_name,
1370
 
             NOT_FIXED_DEC-1);
1371
 
    return(true);
1372
 
  }
1373
 
 
1374
 
  sql_type= fld_type;
1375
 
  length= 0;
1376
 
  change= fld_change;
1377
 
  interval= 0;
1378
 
  pack_length= key_length= 0;
1379
 
  charset= fld_charset;
1380
 
  interval_list.empty();
1381
 
 
1382
 
  comment= *fld_comment;
1383
 
 
1384
 
  /*
1385
 
    Set NO_DEFAULT_VALUE_FLAG if this field doesn't have a default value and
1386
 
    it is NOT NULL, not an AUTO_INCREMENT field and not a TIMESTAMP.
1387
 
  */
1388
 
  if (!fld_default_value && !(fld_type_modifier & AUTO_INCREMENT_FLAG) &&
1389
 
      (fld_type_modifier & NOT_NULL_FLAG) && fld_type != DRIZZLE_TYPE_TIMESTAMP)
1390
 
    flags|= NO_DEFAULT_VALUE_FLAG;
1391
 
 
1392
 
  if (fld_length && !(length= (uint32_t) atoi(fld_length)))
1393
 
    fld_length= 0; /* purecov: inspected */
1394
 
  sign_len= fld_type_modifier & UNSIGNED_FLAG ? 0 : 1;
1395
 
 
1396
 
  switch (fld_type) {
1397
 
  case DRIZZLE_TYPE_TINY:
1398
 
    if (!fld_length)
1399
 
      length= MAX_TINYINT_WIDTH+sign_len;
1400
 
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1401
 
    break;
1402
 
  case DRIZZLE_TYPE_LONG:
1403
 
    if (!fld_length)
1404
 
      length= MAX_INT_WIDTH+sign_len;
1405
 
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1406
 
    break;
1407
 
  case DRIZZLE_TYPE_LONGLONG:
1408
 
    if (!fld_length)
1409
 
      length= MAX_BIGINT_WIDTH;
1410
 
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1411
 
    break;
1412
 
  case DRIZZLE_TYPE_NULL:
1413
 
    break;
1414
 
  case DRIZZLE_TYPE_NEWDECIMAL:
1415
 
    my_decimal_trim(&length, &decimals);
1416
 
    if (length > DECIMAL_MAX_PRECISION)
1417
 
    {
1418
 
      my_error(ER_TOO_BIG_PRECISION, MYF(0), length, fld_name,
1419
 
               DECIMAL_MAX_PRECISION);
1420
 
      return(true);
1421
 
    }
1422
 
    if (length < decimals)
1423
 
    {
1424
 
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1425
 
      return(true);
1426
 
    }
1427
 
    length=
1428
 
      my_decimal_precision_to_length(length, decimals,
1429
 
                                     fld_type_modifier & UNSIGNED_FLAG);
1430
 
    pack_length=
1431
 
      my_decimal_get_binary_size(length, decimals);
1432
 
    break;
1433
 
  case DRIZZLE_TYPE_VARCHAR:
1434
 
    /*
1435
 
      Long VARCHAR's are automaticly converted to blobs in mysql_prepare_table
1436
 
      if they don't have a default value
1437
 
    */
1438
 
    max_field_charlength= MAX_FIELD_VARCHARLENGTH;
1439
 
    break;
1440
 
  case DRIZZLE_TYPE_BLOB:
1441
 
    if (fld_default_value)
1442
 
    {
1443
 
      /* Allow empty as default value. */
1444
 
      String str,*res;
1445
 
      res= fld_default_value->val_str(&str);
1446
 
      if (res->length())
1447
 
      {
1448
 
        my_error(ER_BLOB_CANT_HAVE_DEFAULT, MYF(0),
1449
 
                 fld_name);
1450
 
        return(true);
1451
 
      }
1452
 
 
1453
 
    }
1454
 
    flags|= BLOB_FLAG;
1455
 
    break;
1456
 
  case DRIZZLE_TYPE_DOUBLE:
1457
 
    allowed_type_modifier= AUTO_INCREMENT_FLAG;
1458
 
    if (!fld_length && !fld_decimals)
1459
 
    {
1460
 
      length= DBL_DIG+7;
1461
 
      decimals= NOT_FIXED_DEC;
1462
 
    }
1463
 
    if (length < decimals &&
1464
 
        decimals != NOT_FIXED_DEC)
1465
 
    {
1466
 
      my_error(ER_M_BIGGER_THAN_D, MYF(0), fld_name);
1467
 
      return(true);
1468
 
    }
1469
 
    break;
1470
 
  case DRIZZLE_TYPE_TIMESTAMP:
1471
 
    if (!fld_length)
1472
 
    {
1473
 
      /* Compressed date YYYYMMDDHHMMSS */
1474
 
      length= MAX_DATETIME_COMPRESSED_WIDTH;
1475
 
    }
1476
 
    else if (length != MAX_DATETIME_WIDTH)
1477
 
    {
1478
 
      /*
1479
 
        We support only even TIMESTAMP lengths less or equal than 14
1480
 
        and 19 as length of 4.1 compatible representation.
1481
 
      */
1482
 
      length= ((length+1)/2)*2; /* purecov: inspected */
1483
 
      length= cmin(length, (uint32_t)MAX_DATETIME_COMPRESSED_WIDTH); /* purecov: inspected */
1484
 
    }
1485
 
    flags|= UNSIGNED_FLAG;
1486
 
    if (fld_default_value)
1487
 
    {
1488
 
      /* Grammar allows only NOW() value for ON UPDATE clause */
1489
 
      if (fld_default_value->type() == Item::FUNC_ITEM &&
1490
 
          ((Item_func*)fld_default_value)->functype() == Item_func::NOW_FUNC)
1491
 
      {
1492
 
        unireg_check= (fld_on_update_value ? Field::TIMESTAMP_DNUN_FIELD:
1493
 
                                             Field::TIMESTAMP_DN_FIELD);
1494
 
        /*
1495
 
          We don't need default value any longer moreover it is dangerous.
1496
 
          Everything handled by unireg_check further.
1497
 
        */
1498
 
        def= 0;
1499
 
      }
1500
 
      else
1501
 
        unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD:
1502
 
                                             Field::NONE);
1503
 
    }
1504
 
    else
1505
 
    {
1506
 
      /*
1507
 
        If we have default TIMESTAMP NOT NULL column without explicit DEFAULT
1508
 
        or ON UPDATE values then for the sake of compatiblity we should treat
1509
 
        this column as having DEFAULT NOW() ON UPDATE NOW() (when we don't
1510
 
        have another TIMESTAMP column with auto-set option before this one)
1511
 
        or DEFAULT 0 (in other cases).
1512
 
        So here we are setting TIMESTAMP_OLD_FIELD only temporary, and will
1513
 
        replace this value by TIMESTAMP_DNUN_FIELD or NONE later when
1514
 
        information about all TIMESTAMP fields in table will be availiable.
1515
 
 
1516
 
        If we have TIMESTAMP NULL column without explicit DEFAULT value
1517
 
        we treat it as having DEFAULT NULL attribute.
1518
 
      */
1519
 
      unireg_check= (fld_on_update_value ? Field::TIMESTAMP_UN_FIELD :
1520
 
                     (flags & NOT_NULL_FLAG ? Field::TIMESTAMP_OLD_FIELD :
1521
 
                                              Field::NONE));
1522
 
    }
1523
 
    break;
1524
 
  case DRIZZLE_TYPE_DATE:
1525
 
    length= 10;
1526
 
    break;
1527
 
  case DRIZZLE_TYPE_DATETIME:
1528
 
    length= MAX_DATETIME_WIDTH;
1529
 
    break;
1530
 
  case DRIZZLE_TYPE_ENUM:
1531
 
    {
1532
 
      /* Should be safe. */
1533
 
      pack_length= get_enum_pack_length(fld_interval_list->elements);
1534
 
 
1535
 
      List_iterator<String> it(*fld_interval_list);
1536
 
      String *tmp;
1537
 
      while ((tmp= it++))
1538
 
        interval_list.push_back(tmp);
1539
 
      length= 1; /* See comment for DRIZZLE_TYPE_SET above. */
1540
 
      break;
1541
 
   }
1542
 
  }
1543
 
  /* Remember the value of length */
1544
 
  char_length= length;
1545
 
 
1546
 
  if (!(flags & BLOB_FLAG) &&
1547
 
      ((length > max_field_charlength &&
1548
 
        fld_type != DRIZZLE_TYPE_ENUM &&
1549
 
        (fld_type != DRIZZLE_TYPE_VARCHAR || fld_default_value)) ||
1550
 
       (!length && fld_type != DRIZZLE_TYPE_VARCHAR)))
1551
 
  {
1552
 
    my_error((fld_type == DRIZZLE_TYPE_VARCHAR) ?  ER_TOO_BIG_FIELDLENGTH : ER_TOO_BIG_DISPLAYWIDTH,
1553
 
              MYF(0),
1554
 
              fld_name, max_field_charlength); /* purecov: inspected */
1555
 
    return(true);
1556
 
  }
1557
 
  fld_type_modifier&= AUTO_INCREMENT_FLAG;
1558
 
  if ((~allowed_type_modifier) & fld_type_modifier)
1559
 
  {
1560
 
    my_error(ER_WRONG_FIELD_SPEC, MYF(0), fld_name);
1561
 
    return(true);
1562
 
  }
1563
 
 
1564
 
  return(false); /* success */
1565
 
}
1566
 
 
1567
 
 
1568
 
enum_field_types get_blob_type_from_length(uint32_t)
1569
 
{
1570
 
  enum_field_types type;
1571
 
 
1572
 
  type= DRIZZLE_TYPE_BLOB;
1573
 
 
1574
 
  return type;
1575
 
}
1576
 
 
1577
 
 
1578
980
/*
1579
981
  Make a field from the .frm file info
1580
982
*/
1581
 
 
1582
983
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
1583
984
{
1584
985
  switch (type) {
1585
 
  case DRIZZLE_TYPE_VARCHAR:     return (length + (length < 256 ? 1: 2));
1586
 
  case DRIZZLE_TYPE_TINY        : return 1;
 
986
  case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
 
987
  case DRIZZLE_TYPE_TINY: return 1;
1587
988
  case DRIZZLE_TYPE_DATE: return 3;
1588
989
  case DRIZZLE_TYPE_TIMESTAMP:
1589
 
  case DRIZZLE_TYPE_LONG        : return 4;
 
990
  case DRIZZLE_TYPE_LONG: return 4;
1590
991
  case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1591
992
  case DRIZZLE_TYPE_DATETIME:
1592
993
  case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1593
 
  case DRIZZLE_TYPE_NULL        : return 0;
1594
 
  case DRIZZLE_TYPE_BLOB:               return 4+portable_sizeof_char_ptr;
 
994
  case DRIZZLE_TYPE_NULL: return 0;
 
995
  case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
1595
996
  case DRIZZLE_TYPE_ENUM:
1596
997
  case DRIZZLE_TYPE_NEWDECIMAL:
1597
998
    abort();
1600
1001
  }
1601
1002
}
1602
1003
 
1603
 
 
1604
1004
uint32_t pack_length_to_packflag(uint32_t type)
1605
1005
{
1606
1006
  switch (type) {
1613
1013
  return 0;                                     // This shouldn't happen
1614
1014
}
1615
1015
 
1616
 
 
1617
 
Field *make_field(TableShare *share, MEM_ROOT *root,
1618
 
                  unsigned char *ptr, uint32_t field_length,
1619
 
                  unsigned char *null_pos, unsigned char null_bit,
1620
 
                  uint32_t pack_flag,
1621
 
                  enum_field_types field_type,
1622
 
                  const CHARSET_INFO * field_charset,
1623
 
                  Field::utype unireg_check,
1624
 
                  TYPELIB *interval,
1625
 
                  const char *field_name)
 
1016
Field *make_field(TableShare *share,
 
1017
                  MEM_ROOT *root,
 
1018
                  unsigned char *ptr,
 
1019
                  uint32_t field_length,
 
1020
                  unsigned char *null_pos,
 
1021
                  unsigned char null_bit,
 
1022
                  uint32_t pack_flag,
 
1023
                  enum_field_types field_type,
 
1024
                  const CHARSET_INFO * field_charset,
 
1025
                  Field::utype unireg_check,
 
1026
                  TYPELIB *interval,
 
1027
                  const char *field_name)
1626
1028
{
1627
1029
  if(!root)
1628
1030
    root= current_mem_root();
1723
1125
  return 0;
1724
1126
}
1725
1127
 
1726
 
 
1727
 
/** Create a field suitable for create of table. */
1728
 
 
1729
 
Create_field::Create_field(Field *old_field,Field *orig_field)
1730
 
{
1731
 
  field=      old_field;
1732
 
  field_name=change=old_field->field_name;
1733
 
  length=     old_field->field_length;
1734
 
  flags=      old_field->flags;
1735
 
  unireg_check=old_field->unireg_check;
1736
 
  pack_length=old_field->pack_length();
1737
 
  key_length= old_field->key_length();
1738
 
  sql_type=   old_field->real_type();
1739
 
  charset=    old_field->charset();             // May be NULL ptr
1740
 
  comment=    old_field->comment;
1741
 
  decimals=   old_field->decimals();
1742
 
 
1743
 
  /* Fix if the original table had 4 byte pointer blobs */
1744
 
  if (flags & BLOB_FLAG)
1745
 
    pack_length= (pack_length- old_field->table->s->blob_ptr_size +
1746
 
                  portable_sizeof_char_ptr);
1747
 
 
1748
 
  switch (sql_type) {
1749
 
  case DRIZZLE_TYPE_BLOB:
1750
 
    sql_type= DRIZZLE_TYPE_BLOB;
1751
 
    length/= charset->mbmaxlen;
1752
 
    key_length/= charset->mbmaxlen;
1753
 
    break;
1754
 
    /* Change CHAR -> VARCHAR if dynamic record length */
1755
 
  case DRIZZLE_TYPE_ENUM:
1756
 
  case DRIZZLE_TYPE_VARCHAR:
1757
 
    /* This is corrected in create_length_to_internal_length */
1758
 
    length= (length+charset->mbmaxlen-1) / charset->mbmaxlen;
1759
 
    break;
1760
 
  default:
1761
 
    break;
1762
 
  }
1763
 
 
1764
 
  if (flags & (ENUM_FLAG | SET_FLAG))
1765
 
    interval= ((Field_enum*) old_field)->typelib;
1766
 
  else
1767
 
    interval=0;
1768
 
  def=0;
1769
 
  char_length= length;
1770
 
 
1771
 
  if (!(flags & (NO_DEFAULT_VALUE_FLAG )) &&
1772
 
      old_field->ptr && orig_field &&
1773
 
      (sql_type != DRIZZLE_TYPE_TIMESTAMP ||                /* set def only if */
1774
 
       old_field->table->timestamp_field != old_field ||  /* timestamp field */
1775
 
       unireg_check == Field::TIMESTAMP_UN_FIELD))        /* has default val */
1776
 
  {
1777
 
    my_ptrdiff_t diff;
1778
 
 
1779
 
    /* Get the value from default_values */
1780
 
    diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
1781
 
                          orig_field->table->record[0]);
1782
 
    orig_field->move_field_offset(diff);        // Points now at default_values
1783
 
    if (!orig_field->is_real_null())
1784
 
    {
1785
 
      char buff[MAX_FIELD_WIDTH], *pos;
1786
 
      String tmp(buff, sizeof(buff), charset), *res;
1787
 
      res= orig_field->val_str(&tmp);
1788
 
      pos= (char*) sql_strmake(res->ptr(), res->length());
1789
 
      def= new Item_string(pos, res->length(), charset);
1790
 
    }
1791
 
    orig_field->move_field_offset(-diff);       // Back to record[0]
1792
 
  }
1793
 
}
1794
 
 
1795
 
 
1796
1128
/*****************************************************************************
1797
1129
 Warning handling
1798
1130
*****************************************************************************/
1799
1131
 
1800
 
/**
1801
 
  Produce warning or note about data saved into field.
1802
 
 
1803
 
  @param level            - level of message (Note/Warning/Error)
1804
 
  @param code             - error code of message to be produced
1805
 
  @param cuted_increment  - whenever we should increase cut fields count or not
1806
 
 
1807
 
  @note
1808
 
    This function won't produce warning and increase cut fields counter
1809
 
    if count_cuted_fields == CHECK_FIELD_IGNORE for current thread.
1810
 
 
1811
 
    if count_cuted_fields == CHECK_FIELD_IGNORE then we ignore notes.
1812
 
    This allows us to avoid notes in optimisation, like convert_constant_item().
1813
 
 
1814
 
  @retval
1815
 
    1 if count_cuted_fields == CHECK_FIELD_IGNORE and error level is not NOTE
1816
 
  @retval
1817
 
    0 otherwise
1818
 
*/
1819
 
 
1820
 
bool
1821
 
Field::set_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
1822
 
                   int cuted_increment)
 
1132
bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
 
1133
                        uint32_t code,
 
1134
                        int cuted_increment)
1823
1135
{
1824
1136
  /*
1825
1137
    If this field was created only for type conversion purposes it
1837
1149
}
1838
1150
 
1839
1151
 
1840
 
/**
1841
 
  Produce warning or note about datetime string data saved into field.
1842
 
 
1843
 
  @param level            level of message (Note/Warning/Error)
1844
 
  @param code             error code of message to be produced
1845
 
  @param str              string value which we tried to save
1846
 
  @param str_length       length of string which we tried to save
1847
 
  @param ts_type          type of datetime value (datetime/date/time)
1848
 
  @param cuted_increment  whenever we should increase cut fields count or not
1849
 
 
1850
 
  @note
1851
 
    This function will always produce some warning but won't increase cut
1852
 
    fields counter if count_cuted_fields ==FIELD_CHECK_IGNORE for current
1853
 
    thread.
1854
 
*/
1855
 
 
1856
 
void
1857
 
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1858
 
                            unsigned int code,
1859
 
                            const char *str, uint32_t str_length,
1860
 
                            enum enum_drizzle_timestamp_type ts_type, int cuted_increment)
 
1152
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
 
1153
                                 unsigned int code,
 
1154
                                 const char *str, 
 
1155
                                 uint32_t str_length,
 
1156
                                 enum enum_drizzle_timestamp_type ts_type, 
 
1157
                                 int cuted_increment)
1861
1158
{
1862
1159
  Session *session= table ? table->in_use : current_session;
1863
1160
  if ((session->really_abort_on_warning() &&
1867
1164
                                 field_name);
1868
1165
}
1869
1166
 
1870
 
 
1871
 
/**
1872
 
  Produce warning or note about integer datetime value saved into field.
1873
 
 
1874
 
  @param level            level of message (Note/Warning/Error)
1875
 
  @param code             error code of message to be produced
1876
 
  @param nr               numeric value which we tried to save
1877
 
  @param ts_type          type of datetime value (datetime/date/time)
1878
 
  @param cuted_increment  whenever we should increase cut fields count or not
1879
 
 
1880
 
  @note
1881
 
    This function will always produce some warning but won't increase cut
1882
 
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
1883
 
    thread.
1884
 
*/
1885
 
 
1886
 
void
1887
 
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, uint32_t code,
1888
 
                            int64_t nr, enum enum_drizzle_timestamp_type ts_type,
1889
 
                            int cuted_increment)
 
1167
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, 
 
1168
                                 uint32_t code,
 
1169
                                 int64_t nr, 
 
1170
                                 enum enum_drizzle_timestamp_type ts_type,
 
1171
                                 int cuted_increment)
1890
1172
{
1891
1173
  Session *session= table ? table->in_use : current_session;
1892
1174
  if (session->really_abort_on_warning() ||
1899
1181
  }
1900
1182
}
1901
1183
 
1902
 
 
1903
 
/**
1904
 
  Produce warning or note about double datetime data saved into field.
1905
 
 
1906
 
  @param level            level of message (Note/Warning/Error)
1907
 
  @param code             error code of message to be produced
1908
 
  @param nr               double value which we tried to save
1909
 
  @param ts_type          type of datetime value (datetime/date/time)
1910
 
 
1911
 
  @note
1912
 
    This function will always produce some warning but won't increase cut
1913
 
    fields counter if count_cuted_fields == FIELD_CHECK_IGNORE for current
1914
 
    thread.
1915
 
*/
1916
 
 
1917
 
void
1918
 
Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, const uint32_t code,
1919
 
                            double nr, enum enum_drizzle_timestamp_type ts_type)
 
1184
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
 
1185
                                 const uint32_t code,
 
1186
                                 double nr, 
 
1187
                                 enum enum_drizzle_timestamp_type ts_type)
1920
1188
{
1921
1189
  Session *session= table ? table->in_use : current_session;
1922
1190
  if (session->really_abort_on_warning() ||
1935
1203
  return table->isReadSet(field_index); 
1936
1204
}
1937
1205
 
1938
 
 
1939
1206
bool Field::isWriteSet()
1940
1207
1941
1208
  return table->isWriteSet(field_index); 
1942
1209
}
1943