~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.cc

  • Committer: Olaf van der Spek
  • Date: 2011-07-04 19:11:47 UTC
  • mto: This revision was merged to the branch mainline in revision 2367.
  • Revision ID: olafvdspek@gmail.com-20110704191147-s99ojek811zi1fzj
RemoveĀ unusedĀ Name_resolution_context::error_reporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 * @file This file implements the Field class and API
24
24
 */
25
25
 
26
 
#include "config.h"
 
26
#include <config.h>
27
27
#include <cstdio>
28
28
#include <errno.h>
29
29
#include <float.h>
30
 
#include "drizzled/sql_select.h"
31
 
#include "drizzled/error.h"
32
 
#include "drizzled/field/str.h"
33
 
#include "drizzled/field/num.h"
34
 
#include "drizzled/field/blob.h"
35
 
#include "drizzled/field/boolean.h"
36
 
#include "drizzled/field/enum.h"
37
 
#include "drizzled/field/null.h"
38
 
#include "drizzled/field/date.h"
39
 
#include "drizzled/field/decimal.h"
40
 
#include "drizzled/field/real.h"
41
 
#include "drizzled/field/double.h"
42
 
#include "drizzled/field/int32.h"
43
 
#include "drizzled/field/int64.h"
44
 
#include "drizzled/field/num.h"
45
 
#include "drizzled/field/time.h"
46
 
#include "drizzled/field/epoch.h"
47
 
#include "drizzled/field/datetime.h"
48
 
#include "drizzled/field/microtime.h"
49
 
#include "drizzled/field/varstring.h"
50
 
#include "drizzled/field/uuid.h"
51
 
#include "drizzled/time_functions.h"
52
 
#include "drizzled/internal/m_string.h"
53
 
 
54
 
#include "drizzled/display.h"
55
 
 
56
 
namespace drizzled
57
 
{
 
30
#include <drizzled/sql_select.h>
 
31
#include <drizzled/error.h>
 
32
#include <drizzled/field/str.h>
 
33
#include <drizzled/field/num.h>
 
34
#include <drizzled/field/blob.h>
 
35
#include <drizzled/field/boolean.h>
 
36
#include <drizzled/field/enum.h>
 
37
#include <drizzled/field/null.h>
 
38
#include <drizzled/field/date.h>
 
39
#include <drizzled/field/decimal.h>
 
40
#include <drizzled/field/real.h>
 
41
#include <drizzled/field/double.h>
 
42
#include <drizzled/field/int32.h>
 
43
#include <drizzled/field/int64.h>
 
44
#include <drizzled/field/num.h>
 
45
#include <drizzled/field/time.h>
 
46
#include <drizzled/field/epoch.h>
 
47
#include <drizzled/field/datetime.h>
 
48
#include <drizzled/field/microtime.h>
 
49
#include <drizzled/field/varstring.h>
 
50
#include <drizzled/field/uuid.h>
 
51
#include <drizzled/time_functions.h>
 
52
#include <drizzled/internal/m_string.h>
 
53
#include <drizzled/table.h>
 
54
#include <drizzled/util/test.h>
 
55
#include <drizzled/session.h>
 
56
#include <drizzled/current_session.h>
 
57
#include <drizzled/display.h>
 
58
#include <drizzled/typelib.h>
 
59
 
 
60
namespace drizzled {
58
61
 
59
62
/*****************************************************************************
60
63
  Instansiate templates and static variables
593
596
  STRING_RESULT,
594
597
};
595
598
 
596
 
bool test_if_important_data(const CHARSET_INFO * const cs, 
 
599
bool test_if_important_data(const charset_info_st * const cs, 
597
600
                            const char *str,
598
601
                            const char *strend)
599
602
{
609
612
 
610
613
void *Field::operator new(size_t size, memory::Root *mem_root)
611
614
{
612
 
  return mem_root->alloc_root(static_cast<uint32_t>(size));
 
615
  return mem_root->alloc(size);
613
616
}
614
617
 
615
618
enum_field_types Field::field_type_merge(enum_field_types a,
742
745
  return 0;
743
746
}
744
747
 
745
 
bool Field::is_null(ptrdiff_t row_offset)
746
 
{
747
 
  return null_ptr ?
748
 
    (null_ptr[row_offset] & null_bit ? true : false) :
749
 
    table->null_row;
750
 
}
751
 
 
752
 
bool Field::is_real_null(ptrdiff_t row_offset)
753
 
{
754
 
  return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : false;
755
 
}
756
 
 
757
 
bool Field::is_null_in_record(const unsigned char *record)
758
 
{
759
 
  if (! null_ptr)
760
 
    return false;
761
 
  return test(record[(uint32_t) (null_ptr -table->getInsertRecord())] & null_bit);
762
 
}
763
 
 
764
 
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset)
765
 
{
766
 
  if (! null_ptr)
767
 
    return false;
768
 
  return test(null_ptr[with_offset] & null_bit);
 
748
bool Field::is_null(ptrdiff_t row_offset) const
 
749
{
 
750
  return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : table->null_row;
 
751
}
 
752
 
 
753
bool Field::is_real_null(ptrdiff_t row_offset) const
 
754
{
 
755
  return null_ptr && (null_ptr[row_offset] & null_bit);
 
756
}
 
757
 
 
758
bool Field::is_null_in_record(const unsigned char *record) const
 
759
{
 
760
  return null_ptr && test(record[(uint32_t) (null_ptr -table->getInsertRecord())] & null_bit);
 
761
}
 
762
 
 
763
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset) const
 
764
{
 
765
  return null_ptr && test(null_ptr[with_offset] & null_bit);
769
766
}
770
767
 
771
768
void Field::set_null(ptrdiff_t row_offset)
780
777
    null_ptr[row_offset]&= (unsigned char) ~null_bit;
781
778
}
782
779
 
783
 
bool Field::maybe_null(void)
 
780
bool Field::maybe_null(void) const
784
781
{
785
782
  return null_ptr != 0 || table->maybe_null;
786
783
}
787
784
 
788
 
bool Field::real_maybe_null(void)
 
785
bool Field::real_maybe_null(void) const
789
786
{
790
787
  return null_ptr != 0;
791
788
}
792
789
 
793
790
bool Field::type_can_have_key_part(enum enum_field_types type)
794
791
{
795
 
  switch (type) {
 
792
  switch (type) 
 
793
  {
796
794
  case DRIZZLE_TYPE_VARCHAR:
797
795
  case DRIZZLE_TYPE_BLOB:
798
796
    return true;
847
845
{
848
846
}
849
847
 
850
 
void Field::hash(uint32_t *nr, uint32_t *nr2)
 
848
void Field::hash(uint32_t *nr, uint32_t *nr2) const
851
849
{
852
850
  if (is_null())
853
851
  {
856
854
  else
857
855
  {
858
856
    uint32_t len= pack_length();
859
 
    const CHARSET_INFO * const cs= charset();
 
857
    const charset_info_st * const cs= charset();
860
858
    cs->coll->hash_sort(cs, ptr, len, nr, nr2);
861
859
  }
862
860
}
876
874
int Field::store_and_check(enum_check_fields check_level,
877
875
                           const char *to, 
878
876
                           uint32_t length,
879
 
                           const CHARSET_INFO * const cs)
 
877
                           const charset_info_st * const cs)
880
878
 
881
879
{
882
 
  int res;
883
880
  enum_check_fields old_check_level= table->in_use->count_cuted_fields;
884
881
  table->in_use->count_cuted_fields= check_level;
885
 
  res= store(to, length, cs);
 
882
  int res= store(to, length, cs);
886
883
  table->in_use->count_cuted_fields= old_check_level;
887
884
  return res;
888
885
}
897
894
 
898
895
unsigned char *Field::pack(unsigned char *to, const unsigned char *from)
899
896
{
900
 
  unsigned char *result= this->pack(to, from, UINT32_MAX, table->getShare()->db_low_byte_first);
901
 
  return(result);
 
897
  return this->pack(to, from, UINT32_MAX, table->getShare()->db_low_byte_first);
902
898
}
903
899
 
904
900
const unsigned char *Field::unpack(unsigned char* to,
935
931
 
936
932
const unsigned char *Field::unpack(unsigned char* to, const unsigned char *from)
937
933
{
938
 
  const unsigned char *result= unpack(to, from, 0U, table->getShare()->db_low_byte_first);
939
 
  return(result);
 
934
  return unpack(to, from, 0, table->getShare()->db_low_byte_first);
940
935
}
941
936
 
942
 
type::Decimal *Field::val_decimal(type::Decimal *)
 
937
type::Decimal *Field::val_decimal(type::Decimal *) const
943
938
{
944
939
  /* This never have to be called */
945
 
  assert(0);
 
940
  assert(false);
946
941
  return 0;
947
942
}
948
943
 
1008
1003
  return copy->length+ store_length;
1009
1004
}
1010
1005
 
1011
 
bool Field::get_date(type::Time &ltime, uint32_t fuzzydate)
 
1006
bool Field::get_date(type::Time &ltime, uint32_t fuzzydate) const
1012
1007
{
1013
1008
  char buff[type::Time::MAX_STRING_LENGTH];
1014
 
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
 
1009
  String tmp(buff,sizeof(buff),&my_charset_bin);
1015
1010
 
1016
1011
  assert(getTable() and getTable()->getSession());
1017
1012
 
1018
 
  if (not (res=val_str_internal(&tmp)) or
1019
 
      str_to_datetime_with_warn(getTable()->getSession(),
1020
 
                                res->ptr(), res->length(),
1021
 
                                &ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
1022
 
  {
1023
 
    return true;
1024
 
  }
1025
 
 
1026
 
  return false;
 
1013
  String* res= val_str_internal(&tmp);
 
1014
  return not res or str_to_datetime_with_warn(getTable()->getSession(), res->ptr(), res->length(), &ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR;
1027
1015
}
1028
1016
 
1029
 
bool Field::get_time(type::Time &ltime)
 
1017
bool Field::get_time(type::Time &ltime) const
1030
1018
{
1031
1019
  char buff[type::Time::MAX_STRING_LENGTH];
1032
 
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1033
 
 
1034
 
  if (not (res= val_str_internal(&tmp)) or
1035
 
      str_to_time_with_warn(getTable()->getSession(), res->ptr(), res->length(), &ltime))
1036
 
  {
1037
 
    return true;
1038
 
  }
1039
 
 
1040
 
  return false;
 
1020
  String tmp(buff,sizeof(buff),&my_charset_bin);
 
1021
 
 
1022
  String* res= val_str_internal(&tmp);
 
1023
  return not res or str_to_time_with_warn(getTable()->getSession(), res->ptr(), res->length(), &ltime);
1041
1024
}
1042
1025
 
1043
1026
int Field::store_time(type::Time &ltime, type::timestamp_t)
1044
1027
{
1045
1028
  String tmp;
1046
 
 
1047
1029
  ltime.convert(tmp);
1048
 
 
1049
1030
  return store(tmp.ptr(), tmp.length(), &my_charset_bin);
1050
1031
}
1051
1032
 
1056
1037
 
1057
1038
Field *Field::new_field(memory::Root *root, Table *new_table, bool)
1058
1039
{
1059
 
  Field *tmp;
1060
 
  if (!(tmp= (Field*) root->memdup_root((char*) this,size_of())))
1061
 
    return 0;
1062
 
 
 
1040
  Field* tmp= (Field*) root->memdup(this,size_of());
1063
1041
  if (tmp->table->maybe_null)
1064
1042
    tmp->flags&= ~NOT_NULL_FLAG;
1065
1043
  tmp->table= new_table;
1077
1055
                            unsigned char *new_null_ptr,
1078
1056
                            uint32_t new_null_bit)
1079
1057
{
1080
 
  Field *tmp;
1081
 
  if ((tmp= new_field(root, new_table, table == new_table)))
1082
 
  {
1083
 
    tmp->ptr= new_ptr;
1084
 
    tmp->null_ptr= new_null_ptr;
1085
 
    tmp->null_bit= new_null_bit;
1086
 
  }
 
1058
  Field *tmp= new_field(root, new_table, table == new_table);
 
1059
  tmp->ptr= new_ptr;
 
1060
  tmp->null_ptr= new_null_ptr;
 
1061
  tmp->null_bit= new_null_bit;
1087
1062
  return tmp;
1088
1063
}
1089
1064
 
1090
1065
Field *Field::clone(memory::Root *root, Table *new_table)
1091
1066
{
1092
 
  Field *tmp;
1093
 
  if ((tmp= (Field*) root->memdup_root((char*) this,size_of())))
1094
 
  {
1095
 
    tmp->init(new_table);
1096
 
    tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() -
1097
 
                                           new_table->getDefaultValues()));
1098
 
  }
 
1067
  Field *tmp= (Field*) root->memdup(this,size_of());
 
1068
  tmp->init(new_table);
 
1069
  tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() - new_table->getDefaultValues()));
1099
1070
  return tmp;
1100
1071
}
1101
1072
 
1102
 
 
1103
1073
uint32_t Field::is_equal(CreateField *new_field_ptr)
1104
1074
{
1105
 
  return (new_field_ptr->sql_type == real_type());
 
1075
  return new_field_ptr->sql_type == real_type();
1106
1076
}
1107
1077
 
1108
1078
bool Field::eq_def(Field *field)
1109
1079
{
1110
 
  if (real_type() != field->real_type() || charset() != field->charset() ||
1111
 
      pack_length() != field->pack_length())
1112
 
    return 0;
1113
 
  return 1;
 
1080
  return real_type() == field->real_type() && charset() == field->charset() && pack_length() == field->pack_length();
1114
1081
}
1115
1082
 
1116
1083
bool Field_enum::eq_def(Field *field)
1126
1093
  for (uint32_t i=0 ; i < from_lib->count ; i++)
1127
1094
  {
1128
1095
    if (my_strnncoll(field_charset,
1129
 
                     (const unsigned char*)typelib->type_names[i],
1130
 
                     strlen(typelib->type_names[i]),
1131
 
                     (const unsigned char*)from_lib->type_names[i],
1132
 
                     strlen(from_lib->type_names[i])))
 
1096
                     (const unsigned char*)typelib->type_names[i], strlen(typelib->type_names[i]),
 
1097
                     (const unsigned char*)from_lib->type_names[i], strlen(from_lib->type_names[i])))
1133
1098
      return 0;
1134
1099
  }
1135
1100
 
1138
1103
 
1139
1104
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
1140
1105
{
1141
 
  switch (type) {
 
1106
  switch (type) 
 
1107
  {
1142
1108
  case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1143
1109
  case DRIZZLE_TYPE_UUID: return field::Uuid::max_string_length();
1144
1110
  case DRIZZLE_TYPE_MICROTIME: return field::Microtime::max_string_length();
1163
1129
 
1164
1130
uint32_t pack_length_to_packflag(uint32_t type)
1165
1131
{
1166
 
  switch (type) {
1167
 
    case 1: return 1 << FIELDFLAG_PACK_SHIFT;
1168
 
    case 2: assert(1);
1169
 
    case 3: assert(1);
1170
 
    case 4: return f_settype(DRIZZLE_TYPE_LONG);
1171
 
    case 8: return f_settype(DRIZZLE_TYPE_LONGLONG);
 
1132
  switch (type) 
 
1133
  {
 
1134
  case 1: return 1 << FIELDFLAG_PACK_SHIFT;
 
1135
  case 2: assert(0);
 
1136
  case 3: assert(0);
 
1137
  case 4: return f_settype(DRIZZLE_TYPE_LONG);
 
1138
  case 8: return f_settype(DRIZZLE_TYPE_LONGLONG);
1172
1139
  }
 
1140
  assert(false);
1173
1141
  return 0;                                     // This shouldn't happen
1174
1142
}
1175
1143
 
1249
1217
  }
1250
1218
}
1251
1219
 
1252
 
bool Field::isReadSet() 
 
1220
bool Field::isReadSet() const 
1253
1221
1254
1222
  return table->isReadSet(field_index); 
1255
1223
}