~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.cc

  • Committer: Mark Atwood
  • Date: 2011-08-11 03:05:03 UTC
  • mfrom: (2385.1.12 refactor4)
  • Revision ID: me@mark.atwood.name-20110811030503-rp9xjihc5x3y0x4q
mergeĀ lp:~olafvdspek/drizzle/refactor4

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
#include <drizzled/create_field.h>
 
60
 
 
61
namespace drizzled {
58
62
 
59
63
/*****************************************************************************
60
64
  Instansiate templates and static variables
593
597
  STRING_RESULT,
594
598
};
595
599
 
596
 
bool test_if_important_data(const CHARSET_INFO * const cs, 
 
600
bool test_if_important_data(const charset_info_st * const cs, 
597
601
                            const char *str,
598
602
                            const char *strend)
599
603
{
609
613
 
610
614
void *Field::operator new(size_t size, memory::Root *mem_root)
611
615
{
612
 
  return mem_root->alloc_root(static_cast<uint32_t>(size));
 
616
  return mem_root->alloc(size);
613
617
}
614
618
 
615
619
enum_field_types Field::field_type_merge(enum_field_types a,
742
746
  return 0;
743
747
}
744
748
 
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);
 
749
bool Field::is_null(ptrdiff_t row_offset) const
 
750
{
 
751
  return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : table->null_row;
 
752
}
 
753
 
 
754
bool Field::is_real_null(ptrdiff_t row_offset) const
 
755
{
 
756
  return null_ptr && (null_ptr[row_offset] & null_bit);
 
757
}
 
758
 
 
759
bool Field::is_null_in_record(const unsigned char *record) const
 
760
{
 
761
  return null_ptr && 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) const
 
765
{
 
766
  return null_ptr && test(null_ptr[with_offset] & null_bit);
769
767
}
770
768
 
771
769
void Field::set_null(ptrdiff_t row_offset)
780
778
    null_ptr[row_offset]&= (unsigned char) ~null_bit;
781
779
}
782
780
 
783
 
bool Field::maybe_null(void)
 
781
bool Field::maybe_null(void) const
784
782
{
785
783
  return null_ptr != 0 || table->maybe_null;
786
784
}
787
785
 
788
 
bool Field::real_maybe_null(void)
 
786
bool Field::real_maybe_null(void) const
789
787
{
790
788
  return null_ptr != 0;
791
789
}
792
790
 
793
791
bool Field::type_can_have_key_part(enum enum_field_types type)
794
792
{
795
 
  switch (type) {
 
793
  switch (type) 
 
794
  {
796
795
  case DRIZZLE_TYPE_VARCHAR:
797
796
  case DRIZZLE_TYPE_BLOB:
798
797
    return true;
833
832
    table(NULL),
834
833
    orig_table(NULL),
835
834
    field_name(field_name_arg),
836
 
    comment(NULL_LEX_STRING),
 
835
    comment(null_lex_string()),
837
836
    key_start(0),
838
837
    part_of_key(0),
839
838
    part_of_key_not_clustered(0),
847
846
{
848
847
}
849
848
 
850
 
void Field::hash(uint32_t *nr, uint32_t *nr2)
 
849
void Field::hash(uint32_t *nr, uint32_t *nr2) const
851
850
{
852
851
  if (is_null())
853
852
  {
856
855
  else
857
856
  {
858
857
    uint32_t len= pack_length();
859
 
    const CHARSET_INFO * const cs= charset();
 
858
    const charset_info_st * const cs= charset();
860
859
    cs->coll->hash_sort(cs, ptr, len, nr, nr2);
861
860
  }
862
861
}
876
875
int Field::store_and_check(enum_check_fields check_level,
877
876
                           const char *to, 
878
877
                           uint32_t length,
879
 
                           const CHARSET_INFO * const cs)
 
878
                           const charset_info_st * const cs)
880
879
 
881
880
{
882
 
  int res;
883
881
  enum_check_fields old_check_level= table->in_use->count_cuted_fields;
884
882
  table->in_use->count_cuted_fields= check_level;
885
 
  res= store(to, length, cs);
 
883
  int res= store(to, length, cs);
886
884
  table->in_use->count_cuted_fields= old_check_level;
887
885
  return res;
888
886
}
897
895
 
898
896
unsigned char *Field::pack(unsigned char *to, const unsigned char *from)
899
897
{
900
 
  unsigned char *result= this->pack(to, from, UINT32_MAX, table->getShare()->db_low_byte_first);
901
 
  return(result);
 
898
  return this->pack(to, from, UINT32_MAX, table->getShare()->db_low_byte_first);
902
899
}
903
900
 
904
901
const unsigned char *Field::unpack(unsigned char* to,
935
932
 
936
933
const unsigned char *Field::unpack(unsigned char* to, const unsigned char *from)
937
934
{
938
 
  const unsigned char *result= unpack(to, from, 0U, table->getShare()->db_low_byte_first);
939
 
  return(result);
 
935
  return unpack(to, from, 0, table->getShare()->db_low_byte_first);
940
936
}
941
937
 
942
 
type::Decimal *Field::val_decimal(type::Decimal *)
 
938
type::Decimal *Field::val_decimal(type::Decimal *) const
943
939
{
944
940
  /* This never have to be called */
945
 
  assert(0);
 
941
  assert(false);
946
942
  return 0;
947
943
}
948
944
 
1008
1004
  return copy->length+ store_length;
1009
1005
}
1010
1006
 
1011
 
bool Field::get_date(type::Time &ltime, uint32_t fuzzydate)
 
1007
bool Field::get_date(type::Time &ltime, uint32_t fuzzydate) const
1012
1008
{
1013
1009
  char buff[type::Time::MAX_STRING_LENGTH];
1014
 
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
 
1010
  String tmp(buff,sizeof(buff),&my_charset_bin);
1015
1011
 
1016
1012
  assert(getTable() and getTable()->getSession());
1017
1013
 
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;
 
1014
  String* res= val_str_internal(&tmp);
 
1015
  return not res or str_to_datetime_with_warn(getTable()->getSession(), res->ptr(), res->length(), &ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR;
1027
1016
}
1028
1017
 
1029
 
bool Field::get_time(type::Time &ltime)
 
1018
bool Field::get_time(type::Time &ltime) const
1030
1019
{
1031
1020
  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;
 
1021
  String tmp(buff,sizeof(buff),&my_charset_bin);
 
1022
 
 
1023
  String* res= val_str_internal(&tmp);
 
1024
  return not res or str_to_time_with_warn(getTable()->getSession(), res->ptr(), res->length(), &ltime);
1041
1025
}
1042
1026
 
1043
1027
int Field::store_time(type::Time &ltime, type::timestamp_t)
1044
1028
{
1045
1029
  String tmp;
1046
 
 
1047
1030
  ltime.convert(tmp);
1048
 
 
1049
1031
  return store(tmp.ptr(), tmp.length(), &my_charset_bin);
1050
1032
}
1051
1033
 
1056
1038
 
1057
1039
Field *Field::new_field(memory::Root *root, Table *new_table, bool)
1058
1040
{
1059
 
  Field *tmp;
1060
 
  if (!(tmp= (Field*) root->memdup_root((char*) this,size_of())))
1061
 
    return 0;
1062
 
 
 
1041
  Field* tmp= (Field*) root->memdup(this,size_of());
1063
1042
  if (tmp->table->maybe_null)
1064
1043
    tmp->flags&= ~NOT_NULL_FLAG;
1065
1044
  tmp->table= new_table;
1077
1056
                            unsigned char *new_null_ptr,
1078
1057
                            uint32_t new_null_bit)
1079
1058
{
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
 
  }
 
1059
  Field *tmp= new_field(root, new_table, table == new_table);
 
1060
  tmp->ptr= new_ptr;
 
1061
  tmp->null_ptr= new_null_ptr;
 
1062
  tmp->null_bit= new_null_bit;
1087
1063
  return tmp;
1088
1064
}
1089
1065
 
1090
1066
Field *Field::clone(memory::Root *root, Table *new_table)
1091
1067
{
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
 
  }
 
1068
  Field *tmp= (Field*) root->memdup(this,size_of());
 
1069
  tmp->init(new_table);
 
1070
  tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() - new_table->getDefaultValues()));
1099
1071
  return tmp;
1100
1072
}
1101
1073
 
1102
 
 
1103
1074
uint32_t Field::is_equal(CreateField *new_field_ptr)
1104
1075
{
1105
 
  return (new_field_ptr->sql_type == real_type());
 
1076
  return new_field_ptr->sql_type == real_type();
1106
1077
}
1107
1078
 
1108
1079
bool Field::eq_def(Field *field)
1109
1080
{
1110
 
  if (real_type() != field->real_type() || charset() != field->charset() ||
1111
 
      pack_length() != field->pack_length())
1112
 
    return 0;
1113
 
  return 1;
 
1081
  return real_type() == field->real_type() && charset() == field->charset() && pack_length() == field->pack_length();
1114
1082
}
1115
1083
 
1116
1084
bool Field_enum::eq_def(Field *field)
1126
1094
  for (uint32_t i=0 ; i < from_lib->count ; i++)
1127
1095
  {
1128
1096
    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])))
 
1097
                     (const unsigned char*)typelib->type_names[i], strlen(typelib->type_names[i]),
 
1098
                     (const unsigned char*)from_lib->type_names[i], strlen(from_lib->type_names[i])))
1133
1099
      return 0;
1134
1100
  }
1135
1101
 
1138
1104
 
1139
1105
uint32_t calc_pack_length(enum_field_types type,uint32_t length)
1140
1106
{
1141
 
  switch (type) {
 
1107
  switch (type) 
 
1108
  {
1142
1109
  case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1143
1110
  case DRIZZLE_TYPE_UUID: return field::Uuid::max_string_length();
1144
1111
  case DRIZZLE_TYPE_MICROTIME: return field::Microtime::max_string_length();
1163
1130
 
1164
1131
uint32_t pack_length_to_packflag(uint32_t type)
1165
1132
{
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);
 
1133
  switch (type) 
 
1134
  {
 
1135
  case 1: return 1 << FIELDFLAG_PACK_SHIFT;
 
1136
  case 2: assert(0);
 
1137
  case 3: assert(0);
 
1138
  case 4: return f_settype(DRIZZLE_TYPE_LONG);
 
1139
  case 8: return f_settype(DRIZZLE_TYPE_LONGLONG);
1172
1140
  }
 
1141
  assert(false);
1173
1142
  return 0;                                     // This shouldn't happen
1174
1143
}
1175
1144
 
1249
1218
  }
1250
1219
}
1251
1220
 
1252
 
bool Field::isReadSet() 
 
1221
bool Field::isReadSet() const 
1253
1222
1254
1223
  return table->isReadSet(field_index); 
1255
1224
}