~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/field.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-10-02 14:17:48 UTC
  • mfrom: (1.1.1 upstream)
  • mto: (2.1.17 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20101002141748-m6vbfbfjhrw1153e
Tags: 2010.09.1802-1
* New upstream release.
* Removed pid-file argument hack.
* Updated GPL-2 address to be new address.
* Directly copy in drizzledump.1 since debian doesn't have sphinx 1.0 yet.
* Link to jquery from libjs-jquery. Add it as a depend.
* Add drizzled.8 symlink to the install files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 */
24
24
 
25
25
#include "config.h"
 
26
#include <cstdio>
26
27
#include <errno.h>
27
28
#include <float.h>
28
29
#include "drizzled/sql_select.h"
373
374
 
374
375
void *Field::operator new(size_t size, memory::Root *mem_root)
375
376
{
376
 
  return alloc_root(mem_root, static_cast<uint32_t>(size));
 
377
  return mem_root->alloc_root(static_cast<uint32_t>(size));
377
378
}
378
379
 
379
380
enum_field_types Field::field_type_merge(enum_field_types a,
437
438
 
438
439
void Field::set_default()
439
440
{
440
 
  ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->record[0]);
 
441
  ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->getInsertRecord());
441
442
  memcpy(ptr, ptr + l_offset, pack_length());
442
443
  if (null_ptr)
443
444
    *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
522
523
{
523
524
  if (! null_ptr)
524
525
    return false;
525
 
  return test(record[(uint32_t) (null_ptr -table->record[0])] & null_bit);
 
526
  return test(record[(uint32_t) (null_ptr -table->getInsertRecord())] & null_bit);
526
527
}
527
528
 
528
529
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset)
583
584
void Field::init(Table *table_arg)
584
585
{
585
586
  orig_table= table= table_arg;
586
 
  table_name= &table_arg->alias;
587
587
}
588
588
 
589
589
/// This is used as a table name when the table structure is not set up
598
598
    null_ptr(null_ptr_arg),
599
599
    table(NULL),
600
600
    orig_table(NULL),
601
 
    table_name(NULL),
602
601
    field_name(field_name_arg),
603
602
    key_start(0),
604
603
    part_of_key(0),
664
663
 
665
664
unsigned char *Field::pack(unsigned char *to, const unsigned char *from)
666
665
{
667
 
  unsigned char *result= this->pack(to, from, UINT32_MAX, table->s->db_low_byte_first);
 
666
  unsigned char *result= this->pack(to, from, UINT32_MAX, table->getShare()->db_low_byte_first);
668
667
  return(result);
669
668
}
670
669
 
702
701
 
703
702
const unsigned char *Field::unpack(unsigned char* to, const unsigned char *from)
704
703
{
705
 
  const unsigned char *result= unpack(to, from, 0U, table->s->db_low_byte_first);
 
704
  const unsigned char *result= unpack(to, from, 0U, table->getShare()->db_low_byte_first);
706
705
  return(result);
707
706
}
708
707
 
716
715
 
717
716
void Field::make_field(SendField *field)
718
717
{
719
 
  if (orig_table && orig_table->s->getSchemaName() && *orig_table->s->getSchemaName())
 
718
  if (orig_table && orig_table->getShare()->getSchemaName() && *orig_table->getShare()->getSchemaName())
720
719
  {
721
 
    field->db_name= orig_table->s->getSchemaName();
722
 
    field->org_table_name= orig_table->s->table_name.str;
 
720
    field->db_name= orig_table->getMutableShare()->getSchemaName();
 
721
    field->org_table_name= orig_table->getMutableShare()->getTableName();
723
722
  }
724
723
  else
725
724
    field->org_table_name= field->db_name= "";
726
725
  if (orig_table)
727
726
  {
728
 
    field->table_name= orig_table->alias;
 
727
    field->table_name= orig_table->getAlias();
729
728
    field->org_col_name= field_name;
730
729
  }
731
730
  else
754
753
  return i;
755
754
}
756
755
 
757
 
uint32_t Field::fill_cache_field(CACHE_FIELD *copy)
 
756
uint32_t Field::fill_cache_field(CacheField *copy)
758
757
{
759
758
  uint32_t store_length;
760
759
  copy->str=ptr;
764
763
  {
765
764
    copy->blob_field=(Field_blob*) this;
766
765
    copy->strip=0;
767
 
    copy->length-= table->s->blob_ptr_size;
 
766
    copy->length-= table->getShare()->blob_ptr_size;
768
767
    return copy->length;
769
768
  }
770
769
  else
811
810
Field *Field::new_field(memory::Root *root, Table *new_table, bool)
812
811
{
813
812
  Field *tmp;
814
 
  if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
 
813
  if (!(tmp= (Field*) root->memdup_root((char*) this,size_of())))
815
814
    return 0;
816
815
 
817
816
  if (tmp->table->maybe_null)
844
843
Field *Field::clone(memory::Root *root, Table *new_table)
845
844
{
846
845
  Field *tmp;
847
 
  if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
 
846
  if ((tmp= (Field*) root->memdup_root((char*) this,size_of())))
848
847
  {
849
848
    tmp->init(new_table);
850
 
    tmp->move_field_offset((ptrdiff_t) (new_table->record[0] -
851
 
                                           new_table->s->default_values));
 
849
    tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() -
 
850
                                           new_table->getDefaultValues()));
852
851
  }
853
852
  return tmp;
854
853
}
889
888
{
890
889
  switch (type) {
891
890
  case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
892
 
  case DRIZZLE_TYPE_DATE: return 3;
893
 
  case DRIZZLE_TYPE_TIMESTAMP:
 
891
  case DRIZZLE_TYPE_DATE:
 
892
  case DRIZZLE_TYPE_ENUM:
894
893
  case DRIZZLE_TYPE_LONG: return 4;
895
894
  case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
896
895
  case DRIZZLE_TYPE_DATETIME:
 
896
  case DRIZZLE_TYPE_TIMESTAMP:
897
897
  case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
898
898
  case DRIZZLE_TYPE_NULL: return 0;
899
899
  case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
900
 
  case DRIZZLE_TYPE_ENUM:
901
900
  case DRIZZLE_TYPE_DECIMAL:
902
901
    abort();
903
902
  default:
917
916
  return 0;                                     // This shouldn't happen
918
917
}
919
918
 
920
 
Field *make_field(TableShare *share,
921
 
                  memory::Root *root,
922
 
                  unsigned char *ptr,
923
 
                  uint32_t field_length,
924
 
                  bool is_nullable,
925
 
                  unsigned char *null_pos,
926
 
                  unsigned char null_bit,
927
 
                  uint8_t decimals,
928
 
                  enum_field_types field_type,
929
 
                  const CHARSET_INFO * field_charset,
930
 
                  Field::utype unireg_check,
931
 
                  TYPELIB *interval,
932
 
                  const char *field_name)
933
 
{
934
 
  if(! root)
935
 
    root= current_mem_root();
936
 
 
937
 
  if (! is_nullable)
938
 
  {
939
 
    null_pos=0;
940
 
    null_bit=0;
941
 
  }
942
 
  else
943
 
  {
944
 
    null_bit= ((unsigned char) 1) << null_bit;
945
 
  }
946
 
 
947
 
  switch (field_type) 
948
 
  {
949
 
  case DRIZZLE_TYPE_DATE:
950
 
  case DRIZZLE_TYPE_DATETIME:
951
 
  case DRIZZLE_TYPE_TIMESTAMP:
952
 
    field_charset= &my_charset_bin;
953
 
  default: break;
954
 
  }
955
 
 
956
 
  if (field_type == DRIZZLE_TYPE_VARCHAR ||
957
 
      field_type == DRIZZLE_TYPE_BLOB ||
958
 
      field_type == DRIZZLE_TYPE_ENUM)
959
 
  {
960
 
    if (field_type == DRIZZLE_TYPE_VARCHAR)
961
 
      return new (root) Field_varstring(ptr,field_length,
962
 
                                  HA_VARCHAR_PACKLENGTH(field_length),
963
 
                                  null_pos,null_bit,
964
 
                                  field_name,
965
 
                                  share,
966
 
                                  field_charset);
967
 
 
968
 
    if (field_type == DRIZZLE_TYPE_BLOB)
969
 
    {
970
 
      return new (root) Field_blob(ptr,
971
 
                                   null_pos,
972
 
                                   null_bit,
973
 
                                   field_name,
974
 
                                   share,
975
 
                                   calc_pack_length(DRIZZLE_TYPE_LONG, 0),
976
 
                                   field_charset);
977
 
    }
978
 
 
979
 
    if (interval)
980
 
    {
981
 
      return new (root) Field_enum(ptr,
982
 
                                   field_length,
983
 
                                   null_pos,
984
 
                                   null_bit,
985
 
                                   field_name,
986
 
                                   get_enum_pack_length(interval->count),
987
 
                                   interval,
988
 
                                   field_charset);
989
 
    }
990
 
  }
991
 
 
992
 
  switch (field_type)
993
 
  {
994
 
  case DRIZZLE_TYPE_DECIMAL:
995
 
    return new (root) Field_decimal(ptr,
996
 
                                    field_length,
997
 
                                    null_pos,
998
 
                                    null_bit,
999
 
                                    unireg_check,
1000
 
                                    field_name,
1001
 
                                    decimals,
1002
 
                                    false,
1003
 
                                    false /* is_unsigned */);
1004
 
  case DRIZZLE_TYPE_DOUBLE:
1005
 
    return new (root) Field_double(ptr,
1006
 
                                   field_length,
1007
 
                                   null_pos,
1008
 
                                   null_bit,
1009
 
                                   unireg_check,
1010
 
                                   field_name,
1011
 
                                   decimals,
1012
 
                                   false,
1013
 
                                   false /* is_unsigned */);
1014
 
  case DRIZZLE_TYPE_LONG:
1015
 
    return new (root) Field_long(ptr,
1016
 
                                 field_length,
1017
 
                                 null_pos,
1018
 
                                 null_bit,
1019
 
                                 unireg_check,
1020
 
                                 field_name,
1021
 
                                 false,
1022
 
                                 false /* is_unsigned */);
1023
 
  case DRIZZLE_TYPE_LONGLONG:
1024
 
    return new (root) Field_int64_t(ptr,
1025
 
                                    field_length,
1026
 
                                    null_pos,
1027
 
                                    null_bit,
1028
 
                                    unireg_check,
1029
 
                                    field_name,
1030
 
                                    false,
1031
 
                                    false /* is_unsigned */);
1032
 
  case DRIZZLE_TYPE_TIMESTAMP:
1033
 
    return new (root) Field_timestamp(ptr,
1034
 
                                      field_length,
1035
 
                                      null_pos,
1036
 
                                      null_bit,
1037
 
                                      unireg_check,
1038
 
                                      field_name,
1039
 
                                      share,
1040
 
                                      field_charset);
1041
 
  case DRIZZLE_TYPE_DATE:
1042
 
    return new (root) Field_date(ptr,
1043
 
                                 null_pos,
1044
 
                                 null_bit,
1045
 
                                 field_name,
1046
 
                                 field_charset);
1047
 
  case DRIZZLE_TYPE_DATETIME:
1048
 
    return new (root) Field_datetime(ptr,
1049
 
                                     null_pos,
1050
 
                                     null_bit,
1051
 
                                     field_name,
1052
 
                                     field_charset);
1053
 
  case DRIZZLE_TYPE_NULL:
1054
 
    return new (root) Field_null(ptr,
1055
 
                                 field_length,
1056
 
                                 field_name,
1057
 
                                 field_charset);
1058
 
  default: // Impossible (Wrong version)
1059
 
    break;
1060
 
  }
1061
 
  return 0;
1062
 
}
1063
 
 
1064
919
/*****************************************************************************
1065
920
 Warning handling
1066
921
*****************************************************************************/
1128
983
  {
1129
984
    /* DBL_DIG is enough to print '-[digits].E+###' */
1130
985
    char str_nr[DBL_DIG + 8];
1131
 
    uint32_t str_len= sprintf(str_nr, "%g", nr);
 
986
    uint32_t str_len= snprintf(str_nr, sizeof(str_nr), "%g", nr);
1132
987
    make_truncated_value_warning(session, level, str_nr, str_len, ts_type,
1133
988
                                 field_name);
1134
989
  }