~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.cc

  • Committer: Monty Taylor
  • Date: 2010-04-14 17:24:09 UTC
  • mto: This revision was merged to the branch mainline in revision 1470.
  • Revision ID: mordred@inaugust.com-20100414172409-msncde88yewjrlhx
Moved gcc-specific optoins to their own thing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 */
24
24
 
25
25
#include "config.h"
26
 
#include <cstdio>
27
26
#include <errno.h>
28
27
#include <float.h>
29
28
#include "drizzled/sql_select.h"
37
36
#include "drizzled/field/decimal.h"
38
37
#include "drizzled/field/real.h"
39
38
#include "drizzled/field/double.h"
40
 
#include "drizzled/field/int32.h"
41
 
#include "drizzled/field/int64.h"
 
39
#include "drizzled/field/long.h"
 
40
#include "drizzled/field/int64_t.h"
42
41
#include "drizzled/field/num.h"
43
42
#include "drizzled/field/timestamp.h"
44
43
#include "drizzled/field/datetime.h"
45
44
#include "drizzled/field/varstring.h"
46
 
#include "drizzled/field/uuid.h"
47
45
#include "drizzled/time_functions.h"
48
46
#include "drizzled/internal/m_string.h"
49
47
 
50
 
#include "drizzled/display.h"
51
 
 
52
48
namespace drizzled
53
49
{
54
50
 
57
53
*****************************************************************************/
58
54
 
59
55
static enum_field_types
60
 
field_types_merge_rules [enum_field_types_size][enum_field_types_size]=
 
56
field_types_merge_rules [DRIZZLE_TYPE_MAX+1][DRIZZLE_TYPE_MAX+1]=
61
57
{
62
58
  /* DRIZZLE_TYPE_LONG -> */
63
59
  {
83
79
    DRIZZLE_TYPE_VARCHAR,
84
80
    //DRIZZLE_TYPE_BLOB
85
81
    DRIZZLE_TYPE_BLOB,
86
 
    //DRIZZLE_TYPE_UUID
87
 
    DRIZZLE_TYPE_VARCHAR,
88
82
  },
89
83
  /* DRIZZLE_TYPE_DOUBLE -> */
90
84
  {
110
104
    DRIZZLE_TYPE_VARCHAR,
111
105
    //DRIZZLE_TYPE_BLOB
112
106
    DRIZZLE_TYPE_BLOB,
113
 
    //DRIZZLE_TYPE_UUID
114
 
    DRIZZLE_TYPE_VARCHAR,
115
107
  },
116
108
  /* DRIZZLE_TYPE_NULL -> */
117
109
  {
137
129
    DRIZZLE_TYPE_ENUM,
138
130
    //DRIZZLE_TYPE_BLOB
139
131
    DRIZZLE_TYPE_BLOB,
140
 
    //DRIZZLE_TYPE_UUID
141
 
    DRIZZLE_TYPE_UUID,
142
132
  },
143
133
  /* DRIZZLE_TYPE_TIMESTAMP -> */
144
134
  {
164
154
    DRIZZLE_TYPE_VARCHAR,
165
155
    //DRIZZLE_TYPE_BLOB
166
156
    DRIZZLE_TYPE_BLOB,
167
 
    //DRIZZLE_TYPE_UUID
168
 
    DRIZZLE_TYPE_VARCHAR,
169
157
  },
170
158
  /* DRIZZLE_TYPE_LONGLONG -> */
171
159
  {
190
178
    DRIZZLE_TYPE_VARCHAR,
191
179
    //DRIZZLE_TYPE_BLOB
192
180
    DRIZZLE_TYPE_BLOB,
193
 
    //DRIZZLE_TYPE_UUID
194
 
    DRIZZLE_TYPE_VARCHAR,
195
181
  },
196
182
  /* DRIZZLE_TYPE_DATETIME -> */
197
183
  {
217
203
    DRIZZLE_TYPE_VARCHAR,
218
204
    //DRIZZLE_TYPE_BLOB
219
205
    DRIZZLE_TYPE_BLOB,
220
 
    //DRIZZLE_TYPE_UUID
221
 
    DRIZZLE_TYPE_VARCHAR,
222
206
  },
223
207
  /* DRIZZLE_TYPE_DATE -> */
224
208
  {
244
228
    DRIZZLE_TYPE_VARCHAR,
245
229
    //DRIZZLE_TYPE_BLOB
246
230
    DRIZZLE_TYPE_BLOB,
247
 
    //DRIZZLE_TYPE_UUID
248
 
    DRIZZLE_TYPE_VARCHAR,
249
231
  },
250
232
  /* DRIZZLE_TYPE_VARCHAR -> */
251
233
  {
271
253
    DRIZZLE_TYPE_VARCHAR,
272
254
    //DRIZZLE_TYPE_BLOB
273
255
    DRIZZLE_TYPE_BLOB,
274
 
    //DRIZZLE_TYPE_UUID
275
 
    DRIZZLE_TYPE_VARCHAR,
276
256
  },
277
257
  /* DRIZZLE_TYPE_DECIMAL -> */
278
258
  {
298
278
    DRIZZLE_TYPE_VARCHAR,
299
279
    //DRIZZLE_TYPE_BLOB
300
280
    DRIZZLE_TYPE_BLOB,
301
 
    //DRIZZLE_TYPE_UUID
302
 
    DRIZZLE_TYPE_VARCHAR,
303
281
  },
304
282
  /* DRIZZLE_TYPE_ENUM -> */
305
283
  {
325
303
    DRIZZLE_TYPE_VARCHAR,
326
304
    //DRIZZLE_TYPE_BLOB
327
305
    DRIZZLE_TYPE_BLOB,
328
 
    //DRIZZLE_TYPE_UUID
329
 
    DRIZZLE_TYPE_VARCHAR,
330
 
   },
 
306
  },
331
307
  /* DRIZZLE_TYPE_BLOB -> */
332
308
  {
333
309
    //DRIZZLE_TYPE_LONG
352
328
    DRIZZLE_TYPE_BLOB,
353
329
    //DRIZZLE_TYPE_BLOB
354
330
    DRIZZLE_TYPE_BLOB,
355
 
    //DRIZZLE_TYPE_UUID
356
 
    DRIZZLE_TYPE_VARCHAR,
357
 
  },
358
 
  /* DRIZZLE_TYPE_UUID -> */
359
 
  {
360
 
    //DRIZZLE_TYPE_LONG
361
 
    DRIZZLE_TYPE_VARCHAR,
362
 
    //DRIZZLE_TYPE_DOUBLE
363
 
    DRIZZLE_TYPE_VARCHAR,
364
 
    //DRIZZLE_TYPE_NULL
365
 
    DRIZZLE_TYPE_UUID,
366
 
    //DRIZZLE_TYPE_TIMESTAMP
367
 
    DRIZZLE_TYPE_VARCHAR,
368
 
    //DRIZZLE_TYPE_LONGLONG
369
 
    DRIZZLE_TYPE_VARCHAR,
370
 
    //DRIZZLE_TYPE_DATETIME
371
 
    DRIZZLE_TYPE_VARCHAR,
372
 
    //DRIZZLE_TYPE_DATE
373
 
    DRIZZLE_TYPE_VARCHAR,
374
 
    //DRIZZLE_TYPE_VARCHAR
375
 
    DRIZZLE_TYPE_VARCHAR,
376
 
    //DRIZZLE_TYPE_DECIMAL
377
 
    DRIZZLE_TYPE_VARCHAR,
378
 
    //DRIZZLE_TYPE_VARCHAR,
379
 
    DRIZZLE_TYPE_VARCHAR,
380
 
    //DRIZZLE_TYPE_BLOB
381
 
    DRIZZLE_TYPE_BLOB,
382
 
    //DRIZZLE_TYPE_UUID
383
 
    DRIZZLE_TYPE_UUID,
384
331
  },
385
332
};
386
333
 
387
 
static Item_result field_types_result_type [enum_field_types_size]=
 
334
static Item_result field_types_result_type [DRIZZLE_TYPE_MAX+1]=
388
335
{
389
336
  //DRIZZLE_TYPE_LONG
390
337
  INT_RESULT,
408
355
  STRING_RESULT,
409
356
  //DRIZZLE_TYPE_BLOB
410
357
  STRING_RESULT,
411
 
  //DRIZZLE_TYPE_UUID
412
 
  STRING_RESULT,
413
358
};
414
359
 
415
360
bool test_if_important_data(const CHARSET_INFO * const cs, 
428
373
 
429
374
void *Field::operator new(size_t size, memory::Root *mem_root)
430
375
{
431
 
  return mem_root->alloc_root(static_cast<uint32_t>(size));
 
376
  return alloc_root(mem_root, static_cast<uint32_t>(size));
432
377
}
433
378
 
434
379
enum_field_types Field::field_type_merge(enum_field_types a,
435
380
                                         enum_field_types b)
436
381
{
437
 
  assert(a < enum_field_types_size);
438
 
  assert(b < enum_field_types_size);
 
382
  assert(a <= DRIZZLE_TYPE_MAX);
 
383
  assert(b <= DRIZZLE_TYPE_MAX);
439
384
  return field_types_merge_rules[a][b];
440
385
}
441
386
 
442
387
Item_result Field::result_merge_type(enum_field_types field_type)
443
388
{
444
 
  assert(field_type < enum_field_types_size);
 
389
  assert(field_type <= DRIZZLE_TYPE_MAX);
445
390
  return field_types_result_type[field_type];
446
391
}
447
392
 
492
437
 
493
438
void Field::set_default()
494
439
{
495
 
  ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->getInsertRecord());
 
440
  ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->record[0]);
496
441
  memcpy(ptr, ptr + l_offset, pack_length());
497
442
  if (null_ptr)
498
443
    *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
577
522
{
578
523
  if (! null_ptr)
579
524
    return false;
580
 
  return test(record[(uint32_t) (null_ptr -table->getInsertRecord())] & null_bit);
 
525
  return test(record[(uint32_t) (null_ptr -table->record[0])] & null_bit);
581
526
}
582
527
 
583
528
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset)
638
583
void Field::init(Table *table_arg)
639
584
{
640
585
  orig_table= table= table_arg;
 
586
  table_name= &table_arg->alias;
641
587
}
642
588
 
643
589
/// This is used as a table name when the table structure is not set up
652
598
    null_ptr(null_ptr_arg),
653
599
    table(NULL),
654
600
    orig_table(NULL),
 
601
    table_name(NULL),
655
602
    field_name(field_name_arg),
656
603
    key_start(0),
657
604
    part_of_key(0),
694
641
  }
695
642
}
696
643
 
697
 
int Field::store_and_check(enum_check_fields check_level,
698
 
                           const char *to, 
699
 
                           uint32_t length,
700
 
                           const CHARSET_INFO * const cs)
701
 
 
 
644
int Field::store(const char *to, 
 
645
                 uint32_t length,
 
646
                 const CHARSET_INFO * const cs,
 
647
                 enum_check_fields check_level)
702
648
{
703
649
  int res;
704
650
  enum_check_fields old_check_level= table->in_use->count_cuted_fields;
718
664
 
719
665
unsigned char *Field::pack(unsigned char *to, const unsigned char *from)
720
666
{
721
 
  unsigned char *result= this->pack(to, from, UINT32_MAX, table->getShare()->db_low_byte_first);
 
667
  unsigned char *result= this->pack(to, from, UINT32_MAX, table->s->db_low_byte_first);
722
668
  return(result);
723
669
}
724
670
 
756
702
 
757
703
const unsigned char *Field::unpack(unsigned char* to, const unsigned char *from)
758
704
{
759
 
  const unsigned char *result= unpack(to, from, 0U, table->getShare()->db_low_byte_first);
 
705
  const unsigned char *result= unpack(to, from, 0U, table->s->db_low_byte_first);
760
706
  return(result);
761
707
}
762
708
 
770
716
 
771
717
void Field::make_field(SendField *field)
772
718
{
773
 
  if (orig_table && orig_table->getShare()->getSchemaName() && *orig_table->getShare()->getSchemaName())
 
719
  if (orig_table && orig_table->s->getSchemaName() && *orig_table->s->getSchemaName())
774
720
  {
775
 
    field->db_name= orig_table->getShare()->getSchemaName();
776
 
    field->org_table_name= orig_table->getShare()->getTableName();
 
721
    field->db_name= orig_table->s->getSchemaName();
 
722
    field->org_table_name= orig_table->s->table_name.str;
777
723
  }
778
724
  else
779
725
    field->org_table_name= field->db_name= "";
780
726
  if (orig_table)
781
727
  {
782
 
    field->table_name= orig_table->getAlias();
 
728
    field->table_name= orig_table->alias;
783
729
    field->org_col_name= field_name;
784
730
  }
785
731
  else
808
754
  return i;
809
755
}
810
756
 
811
 
uint32_t Field::fill_cache_field(CacheField *copy)
 
757
uint32_t Field::fill_cache_field(CACHE_FIELD *copy)
812
758
{
813
759
  uint32_t store_length;
814
760
  copy->str=ptr;
818
764
  {
819
765
    copy->blob_field=(Field_blob*) this;
820
766
    copy->strip=0;
821
 
    copy->length-= table->getShare()->blob_ptr_size;
 
767
    copy->length-= table->s->blob_ptr_size;
822
768
    return copy->length;
823
769
  }
824
770
  else
833
779
{
834
780
  char buff[40];
835
781
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
836
 
  if (!(res=val_str_internal(&tmp)) || str_to_datetime_with_warn(res->ptr(), res->length(),
837
 
                                                                 ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
838
 
  {
 
782
  if (!(res=val_str(&tmp)) ||
 
783
      str_to_datetime_with_warn(res->ptr(), res->length(),
 
784
                                ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
839
785
    return 1;
840
 
  }
841
 
 
842
786
  return 0;
843
787
}
844
788
 
846
790
{
847
791
  char buff[40];
848
792
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
849
 
 
850
 
  if (!(res=val_str_internal(&tmp)) || str_to_time_with_warn(res->ptr(), res->length(), ltime))
851
 
  {
 
793
  if (!(res=val_str(&tmp)) ||
 
794
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
852
795
    return 1;
853
 
  }
854
 
 
855
796
  return 0;
856
797
}
857
798
 
870
811
Field *Field::new_field(memory::Root *root, Table *new_table, bool)
871
812
{
872
813
  Field *tmp;
873
 
  if (!(tmp= (Field*) root->memdup_root((char*) this,size_of())))
 
814
  if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
874
815
    return 0;
875
816
 
876
817
  if (tmp->table->maybe_null)
903
844
Field *Field::clone(memory::Root *root, Table *new_table)
904
845
{
905
846
  Field *tmp;
906
 
  if ((tmp= (Field*) root->memdup_root((char*) this,size_of())))
 
847
  if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
907
848
  {
908
849
    tmp->init(new_table);
909
 
    tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() -
910
 
                                           new_table->getDefaultValues()));
 
850
    tmp->move_field_offset((ptrdiff_t) (new_table->record[0] -
 
851
                                           new_table->s->default_values));
911
852
  }
912
853
  return tmp;
913
854
}
948
889
{
949
890
  switch (type) {
950
891
  case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
951
 
  case DRIZZLE_TYPE_UUID: return field::Uuid::max_string_length();
952
 
  case DRIZZLE_TYPE_DATE:
953
 
  case DRIZZLE_TYPE_ENUM:
 
892
  case DRIZZLE_TYPE_DATE: return 3;
 
893
  case DRIZZLE_TYPE_TIMESTAMP:
954
894
  case DRIZZLE_TYPE_LONG: return 4;
955
895
  case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
956
896
  case DRIZZLE_TYPE_DATETIME:
957
 
  case DRIZZLE_TYPE_TIMESTAMP:
958
897
  case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
959
898
  case DRIZZLE_TYPE_NULL: return 0;
960
899
  case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
 
900
  case DRIZZLE_TYPE_ENUM:
961
901
  case DRIZZLE_TYPE_DECIMAL:
962
 
                          assert(0);
963
 
                          abort();
 
902
    abort();
 
903
  default:
 
904
    return 0;
964
905
  }
965
 
 
966
 
  assert(0);
967
 
  abort();
968
906
}
969
907
 
970
908
uint32_t pack_length_to_packflag(uint32_t type)
973
911
    case 1: return 1 << FIELDFLAG_PACK_SHIFT;
974
912
    case 2: assert(1);
975
913
    case 3: assert(1);
976
 
    case 4: return f_settype(DRIZZLE_TYPE_LONG);
977
 
    case 8: return f_settype(DRIZZLE_TYPE_LONGLONG);
 
914
    case 4: return f_settype((uint32_t) DRIZZLE_TYPE_LONG);
 
915
    case 8: return f_settype((uint32_t) DRIZZLE_TYPE_LONGLONG);
978
916
  }
979
917
  return 0;                                     // This shouldn't happen
980
918
}
981
919
 
 
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
 
982
1064
/*****************************************************************************
983
1065
 Warning handling
984
1066
*****************************************************************************/
1078
1160
    table->clearWriteSet(field_index);
1079
1161
}
1080
1162
 
1081
 
std::ostream& operator<<(std::ostream& output, const Field &field)
1082
 
{
1083
 
  output << "Field:(";
1084
 
  output <<  field.field_name;
1085
 
  output << ", ";
1086
 
  output << drizzled::display::type(field.real_type());
1087
 
  output << ")";
1088
 
 
1089
 
  return output;  // for multiple << operators.
1090
 
}
1091
 
 
1092
1163
} /* namespace drizzled */