~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.cc

edit

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
 
#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>
 
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"
59
55
 
60
56
namespace drizzled
61
57
{
746
742
  return 0;
747
743
}
748
744
 
749
 
bool Field::is_null(ptrdiff_t row_offset) const
 
745
bool Field::is_null(ptrdiff_t row_offset)
750
746
{
751
747
  return null_ptr ?
752
748
    (null_ptr[row_offset] & null_bit ? true : false) :
753
749
    table->null_row;
754
750
}
755
751
 
756
 
bool Field::is_real_null(ptrdiff_t row_offset) const
 
752
bool Field::is_real_null(ptrdiff_t row_offset)
757
753
{
758
754
  return null_ptr ? (null_ptr[row_offset] & null_bit ? true : false) : false;
759
755
}
760
756
 
761
 
bool Field::is_null_in_record(const unsigned char *record) const
 
757
bool Field::is_null_in_record(const unsigned char *record)
762
758
{
763
759
  if (! null_ptr)
764
760
    return false;
765
761
  return test(record[(uint32_t) (null_ptr -table->getInsertRecord())] & null_bit);
766
762
}
767
763
 
768
 
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset) const
 
764
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset)
769
765
{
770
766
  if (! null_ptr)
771
767
    return false;
784
780
    null_ptr[row_offset]&= (unsigned char) ~null_bit;
785
781
}
786
782
 
787
 
bool Field::maybe_null(void) const
 
783
bool Field::maybe_null(void)
788
784
{
789
785
  return null_ptr != 0 || table->maybe_null;
790
786
}
791
787
 
792
 
bool Field::real_maybe_null(void) const
 
788
bool Field::real_maybe_null(void)
793
789
{
794
790
  return null_ptr != 0;
795
791
}
831
827
             unsigned char *null_ptr_arg,
832
828
             unsigned char null_bit_arg,
833
829
             utype unireg_check_arg, 
834
 
             const char *field_name_arg) :
 
830
             const char *field_name_arg)
 
831
  :
835
832
    ptr(ptr_arg),
836
833
    null_ptr(null_ptr_arg),
837
834
    table(NULL),
838
835
    orig_table(NULL),
839
836
    field_name(field_name_arg),
840
 
    comment(NULL_LEX_STRING),
841
837
    key_start(0),
842
838
    part_of_key(0),
843
839
    part_of_key_not_clustered(0),
844
840
    part_of_sortkey(0),
845
841
    unireg_check(unireg_check_arg),
846
842
    field_length(length_arg),
847
 
    flags(null_ptr ? 0: NOT_NULL_FLAG),
848
 
    field_index(0),
849
843
    null_bit(null_bit_arg),
850
844
    is_created_from_null_item(false)
851
845
{
 
846
  flags= null_ptr ? 0: NOT_NULL_FLAG;
 
847
  comment.str= (char*) "";
 
848
  comment.length= 0;
 
849
  field_index= 0;
852
850
}
853
851
 
854
 
void Field::hash(uint32_t *nr, uint32_t *nr2) const
 
852
void Field::hash(uint32_t *nr, uint32_t *nr2)
855
853
{
856
854
  if (is_null())
857
855
  {
943
941
  return(result);
944
942
}
945
943
 
946
 
type::Decimal *Field::val_decimal(type::Decimal *) const
 
944
type::Decimal *Field::val_decimal(type::Decimal *)
947
945
{
948
946
  /* This never have to be called */
949
947
  assert(0);
1001
999
  {
1002
1000
    copy->blob_field=(Field_blob*) this;
1003
1001
    copy->strip=0;
1004
 
    copy->length-= table->getShare()->sizeBlobPtr();
 
1002
    copy->length-= table->getShare()->blob_ptr_size;
1005
1003
    return copy->length;
1006
1004
  }
1007
1005
  else
1012
1010
  return copy->length+ store_length;
1013
1011
}
1014
1012
 
1015
 
bool Field::get_date(type::Time &ltime, uint32_t fuzzydate) const
1016
 
{
1017
 
  char buff[type::Time::MAX_STRING_LENGTH];
1018
 
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1019
 
 
1020
 
  assert(getTable() and getTable()->getSession());
1021
 
 
1022
 
  if (not (res= val_str_internal(&tmp)) or
1023
 
      str_to_datetime_with_warn(getTable()->getSession(),
1024
 
                                res->ptr(), res->length(),
1025
 
                                &ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
1026
 
  {
1027
 
    return true;
1028
 
  }
1029
 
 
1030
 
  return false;
1031
 
}
1032
 
 
1033
 
bool Field::get_time(type::Time &ltime) const
1034
 
{
1035
 
  char buff[type::Time::MAX_STRING_LENGTH];
1036
 
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1037
 
 
1038
 
  if (not (res= val_str_internal(&tmp)) or
1039
 
      str_to_time_with_warn(getTable()->getSession(), res->ptr(), res->length(), &ltime))
1040
 
  {
1041
 
    return true;
1042
 
  }
1043
 
 
1044
 
  return false;
1045
 
}
1046
 
 
1047
 
int Field::store_time(type::Time &ltime, type::timestamp_t)
1048
 
{
1049
 
  String tmp;
1050
 
 
1051
 
  ltime.convert(tmp);
1052
 
 
1053
 
  return store(tmp.ptr(), tmp.length(), &my_charset_bin);
 
1013
bool Field::get_date(type::Time *ltime,uint32_t fuzzydate)
 
1014
{
 
1015
  char buff[40];
 
1016
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
 
1017
  if (!(res=val_str_internal(&tmp)) || str_to_datetime_with_warn(res->ptr(), res->length(),
 
1018
                                                                 ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
 
1019
  {
 
1020
    return 1;
 
1021
  }
 
1022
 
 
1023
  return 0;
 
1024
}
 
1025
 
 
1026
bool Field::get_time(type::Time *ltime)
 
1027
{
 
1028
  char buff[40];
 
1029
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
 
1030
 
 
1031
  if (!(res=val_str_internal(&tmp)) || str_to_time_with_warn(res->ptr(), res->length(), ltime))
 
1032
  {
 
1033
    return 1;
 
1034
  }
 
1035
 
 
1036
  return 0;
 
1037
}
 
1038
 
 
1039
int Field::store_time(type::Time *ltime, enum enum_drizzle_timestamp_type)
 
1040
{
 
1041
  char buff[MAX_DATE_STRING_REP_LENGTH];
 
1042
  uint32_t length= (uint32_t) my_TIME_to_str(ltime, buff);
 
1043
  return store(buff, length, &my_charset_bin);
1054
1044
}
1055
1045
 
1056
1046
bool Field::optimize_range(uint32_t idx, uint32_t)
1205
1195
                                 drizzled::error_t code,
1206
1196
                                 const char *str, 
1207
1197
                                 uint32_t str_length,
1208
 
                                 type::timestamp_t ts_type, 
 
1198
                                 enum enum_drizzle_timestamp_type ts_type, 
1209
1199
                                 int cuted_increment)
1210
1200
{
1211
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1212
 
 
1213
 
  if ((session->abortOnWarning() and
 
1201
  Session *session= table ? table->in_use : current_session;
 
1202
  if ((session->really_abort_on_warning() &&
1214
1203
       level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
1215
1204
      set_warning(level, code, cuted_increment))
1216
1205
    make_truncated_value_warning(session, level, str, str_length, ts_type,
1220
1209
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, 
1221
1210
                                 drizzled::error_t code,
1222
1211
                                 int64_t nr, 
1223
 
                                 type::timestamp_t ts_type,
 
1212
                                 enum enum_drizzle_timestamp_type ts_type,
1224
1213
                                 int cuted_increment)
1225
1214
{
1226
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1227
 
 
1228
 
  if (session->abortOnWarning() or
 
1215
  Session *session= table ? table->in_use : current_session;
 
1216
  if (session->really_abort_on_warning() ||
1229
1217
      set_warning(level, code, cuted_increment))
1230
1218
  {
1231
 
    char str_nr[DECIMAL_LONGLONG_DIGITS];
 
1219
    char str_nr[22];
1232
1220
    char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
1233
1221
    make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
1234
1222
                                 ts_type, field_name);
1238
1226
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1239
1227
                                 const drizzled::error_t code,
1240
1228
                                 double nr, 
1241
 
                                 type::timestamp_t ts_type)
 
1229
                                 enum enum_drizzle_timestamp_type ts_type)
1242
1230
{
1243
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1244
 
 
1245
 
  if (session->abortOnWarning() or
 
1231
  Session *session= table ? table->in_use : current_session;
 
1232
  if (session->really_abort_on_warning() ||
1246
1233
      set_warning(level, code, 1))
1247
1234
  {
1248
1235
    /* DBL_DIG is enough to print '-[digits].E+###' */
1253
1240
  }
1254
1241
}
1255
1242
 
1256
 
bool Field::isReadSet() const 
 
1243
bool Field::isReadSet() 
1257
1244
1258
1245
  return table->isReadSet(field_index); 
1259
1246
}