~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field.cc

Merge Stewart.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2010 Brian Aker
5
4
 *  Copyright (C) 2008 MySQL
6
5
 *
7
6
 *  This program is free software; you can redistribute it and/or modify
24
23
 */
25
24
 
26
25
#include "config.h"
27
 
#include <cstdio>
28
26
#include <errno.h>
29
27
#include <float.h>
30
28
#include "drizzled/sql_select.h"
32
30
#include "drizzled/field/str.h"
33
31
#include "drizzled/field/num.h"
34
32
#include "drizzled/field/blob.h"
35
 
#include "drizzled/field/boolean.h"
36
33
#include "drizzled/field/enum.h"
37
34
#include "drizzled/field/null.h"
38
35
#include "drizzled/field/date.h"
39
36
#include "drizzled/field/decimal.h"
40
37
#include "drizzled/field/real.h"
41
38
#include "drizzled/field/double.h"
42
 
#include "drizzled/field/int32.h"
43
 
#include "drizzled/field/int64.h"
 
39
#include "drizzled/field/long.h"
 
40
#include "drizzled/field/int64_t.h"
44
41
#include "drizzled/field/num.h"
45
 
#include "drizzled/field/time.h"
46
 
#include "drizzled/field/epoch.h"
 
42
#include "drizzled/field/timestamp.h"
47
43
#include "drizzled/field/datetime.h"
48
 
#include "drizzled/field/microtime.h"
49
44
#include "drizzled/field/varstring.h"
50
 
#include "drizzled/field/uuid.h"
51
45
#include "drizzled/time_functions.h"
52
46
#include "drizzled/internal/m_string.h"
53
47
 
54
 
#include "drizzled/display.h"
55
 
 
56
48
namespace drizzled
57
49
{
58
50
 
61
53
*****************************************************************************/
62
54
 
63
55
static enum_field_types
64
 
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]=
65
57
{
66
58
  /* DRIZZLE_TYPE_LONG -> */
67
59
  {
87
79
    DRIZZLE_TYPE_VARCHAR,
88
80
    //DRIZZLE_TYPE_BLOB
89
81
    DRIZZLE_TYPE_BLOB,
90
 
    //DRIZZLE_TYPE_TIME
91
 
    DRIZZLE_TYPE_TIME,
92
 
    //DRIZZLE_TYPE_BOOLEAN
93
 
    DRIZZLE_TYPE_VARCHAR,
94
 
    //DRIZZLE_TYPE_UUID
95
 
    DRIZZLE_TYPE_VARCHAR,
96
 
    //DRIZZLE_TYPE_MICROTIME
97
 
    DRIZZLE_TYPE_VARCHAR,
98
82
  },
99
83
  /* DRIZZLE_TYPE_DOUBLE -> */
100
84
  {
120
104
    DRIZZLE_TYPE_VARCHAR,
121
105
    //DRIZZLE_TYPE_BLOB
122
106
    DRIZZLE_TYPE_BLOB,
123
 
    //DRIZZLE_TYPE_TIME
124
 
    DRIZZLE_TYPE_TIME,
125
 
    //DRIZZLE_TYPE_BOOLEAN
126
 
    DRIZZLE_TYPE_VARCHAR,
127
 
    //DRIZZLE_TYPE_UUID
128
 
    DRIZZLE_TYPE_VARCHAR,
129
 
    //DRIZZLE_TYPE_MICROTIME
130
 
    DRIZZLE_TYPE_VARCHAR,
131
107
  },
132
108
  /* DRIZZLE_TYPE_NULL -> */
133
109
  {
153
129
    DRIZZLE_TYPE_ENUM,
154
130
    //DRIZZLE_TYPE_BLOB
155
131
    DRIZZLE_TYPE_BLOB,
156
 
    //DRIZZLE_TYPE_TIME
157
 
    DRIZZLE_TYPE_TIME,
158
 
    //DRIZZLE_TYPE_BOOLEAN
159
 
    DRIZZLE_TYPE_BOOLEAN,
160
 
    //DRIZZLE_TYPE_UUID
161
 
    DRIZZLE_TYPE_UUID,
162
 
    //DRIZZLE_TYPE_MICROTIME
163
 
    DRIZZLE_TYPE_MICROTIME,
164
132
  },
165
133
  /* DRIZZLE_TYPE_TIMESTAMP -> */
166
134
  {
186
154
    DRIZZLE_TYPE_VARCHAR,
187
155
    //DRIZZLE_TYPE_BLOB
188
156
    DRIZZLE_TYPE_BLOB,
189
 
    //DRIZZLE_TYPE_TIME
190
 
    DRIZZLE_TYPE_TIME,
191
 
    //DRIZZLE_TYPE_BOOLEAN
192
 
    DRIZZLE_TYPE_VARCHAR,
193
 
    //DRIZZLE_TYPE_UUID
194
 
    DRIZZLE_TYPE_VARCHAR,
195
 
    //DRIZZLE_TYPE_MICROTIME
196
 
    DRIZZLE_TYPE_VARCHAR,
197
157
  },
198
158
  /* DRIZZLE_TYPE_LONGLONG -> */
199
159
  {
218
178
    DRIZZLE_TYPE_VARCHAR,
219
179
    //DRIZZLE_TYPE_BLOB
220
180
    DRIZZLE_TYPE_BLOB,
221
 
    //DRIZZLE_TYPE_TIME
222
 
    DRIZZLE_TYPE_TIME,
223
 
    //DRIZZLE_TYPE_BOOLEAN
224
 
    DRIZZLE_TYPE_VARCHAR,
225
 
    //DRIZZLE_TYPE_UUID
226
 
    DRIZZLE_TYPE_VARCHAR,
227
 
    //DRIZZLE_TYPE_MICROTIME
228
 
    DRIZZLE_TYPE_VARCHAR,
229
181
  },
230
182
  /* DRIZZLE_TYPE_DATETIME -> */
231
183
  {
251
203
    DRIZZLE_TYPE_VARCHAR,
252
204
    //DRIZZLE_TYPE_BLOB
253
205
    DRIZZLE_TYPE_BLOB,
254
 
    //DRIZZLE_TYPE_TIME
255
 
    DRIZZLE_TYPE_TIME,
256
 
    //DRIZZLE_TYPE_BOOLEAN
257
 
    DRIZZLE_TYPE_VARCHAR,
258
 
    //DRIZZLE_TYPE_UUID
259
 
    DRIZZLE_TYPE_VARCHAR,
260
 
    //DRIZZLE_TYPE_MICROTIME
261
 
    DRIZZLE_TYPE_VARCHAR,
262
206
  },
263
207
  /* DRIZZLE_TYPE_DATE -> */
264
208
  {
284
228
    DRIZZLE_TYPE_VARCHAR,
285
229
    //DRIZZLE_TYPE_BLOB
286
230
    DRIZZLE_TYPE_BLOB,
287
 
    //DRIZZLE_TYPE_TIME
288
 
    DRIZZLE_TYPE_TIME,
289
 
    //DRIZZLE_TYPE_BOOLEAN
290
 
    DRIZZLE_TYPE_VARCHAR,
291
 
    //DRIZZLE_TYPE_UUID
292
 
    DRIZZLE_TYPE_VARCHAR,
293
 
    //DRIZZLE_TYPE_MICROTIME
294
 
    DRIZZLE_TYPE_VARCHAR,
295
231
  },
296
232
  /* DRIZZLE_TYPE_VARCHAR -> */
297
233
  {
317
253
    DRIZZLE_TYPE_VARCHAR,
318
254
    //DRIZZLE_TYPE_BLOB
319
255
    DRIZZLE_TYPE_BLOB,
320
 
    //DRIZZLE_TYPE_TIME
321
 
    DRIZZLE_TYPE_TIME,
322
 
    //DRIZZLE_TYPE_BOOLEAN
323
 
    DRIZZLE_TYPE_VARCHAR,
324
 
    //DRIZZLE_TYPE_UUID
325
 
    DRIZZLE_TYPE_VARCHAR,
326
 
    //DRIZZLE_TYPE_MICROTIME
327
 
    DRIZZLE_TYPE_VARCHAR,
328
256
  },
329
257
  /* DRIZZLE_TYPE_DECIMAL -> */
330
258
  {
350
278
    DRIZZLE_TYPE_VARCHAR,
351
279
    //DRIZZLE_TYPE_BLOB
352
280
    DRIZZLE_TYPE_BLOB,
353
 
    //DRIZZLE_TYPE_TIME
354
 
    DRIZZLE_TYPE_TIME,
355
 
    //DRIZZLE_TYPE_BOOLEAN
356
 
    DRIZZLE_TYPE_VARCHAR,
357
 
    //DRIZZLE_TYPE_UUID
358
 
    DRIZZLE_TYPE_VARCHAR,
359
 
    //DRIZZLE_TYPE_MICROTIME
360
 
    DRIZZLE_TYPE_VARCHAR,
361
281
  },
362
282
  /* DRIZZLE_TYPE_ENUM -> */
363
283
  {
383
303
    DRIZZLE_TYPE_VARCHAR,
384
304
    //DRIZZLE_TYPE_BLOB
385
305
    DRIZZLE_TYPE_BLOB,
386
 
    //DRIZZLE_TYPE_TIME
387
 
    DRIZZLE_TYPE_TIME,
388
 
    //DRIZZLE_TYPE_BOOLEAN
389
 
    DRIZZLE_TYPE_VARCHAR,
390
 
    //DRIZZLE_TYPE_UUID
391
 
    DRIZZLE_TYPE_VARCHAR,
392
 
    //DRIZZLE_TYPE_MICROTIME
393
 
    DRIZZLE_TYPE_VARCHAR,
394
 
   },
 
306
  },
395
307
  /* DRIZZLE_TYPE_BLOB -> */
396
308
  {
397
309
    //DRIZZLE_TYPE_LONG
416
328
    DRIZZLE_TYPE_BLOB,
417
329
    //DRIZZLE_TYPE_BLOB
418
330
    DRIZZLE_TYPE_BLOB,
419
 
    //DRIZZLE_TYPE_TIME
420
 
    DRIZZLE_TYPE_TIME,
421
 
    //DRIZZLE_TYPE_BOOLEAN
422
 
    DRIZZLE_TYPE_VARCHAR,
423
 
    //DRIZZLE_TYPE_UUID
424
 
    DRIZZLE_TYPE_VARCHAR,
425
 
    //DRIZZLE_TYPE_MICROTIME
426
 
    DRIZZLE_TYPE_VARCHAR,
427
 
  },
428
 
  /* DRIZZLE_TYPE_TIME -> */
429
 
  {
430
 
    //DRIZZLE_TYPE_LONG
431
 
    DRIZZLE_TYPE_VARCHAR,
432
 
    //DRIZZLE_TYPE_DOUBLE
433
 
    DRIZZLE_TYPE_VARCHAR,
434
 
    //DRIZZLE_TYPE_NULL
435
 
    DRIZZLE_TYPE_TIME,
436
 
    //DRIZZLE_TYPE_TIMESTAMP
437
 
    DRIZZLE_TYPE_VARCHAR,
438
 
    //DRIZZLE_TYPE_LONGLONG
439
 
    DRIZZLE_TYPE_VARCHAR,
440
 
    //DRIZZLE_TYPE_DATETIME
441
 
    DRIZZLE_TYPE_VARCHAR,
442
 
    //DRIZZLE_TYPE_DATE
443
 
    DRIZZLE_TYPE_VARCHAR,
444
 
    //DRIZZLE_TYPE_VARCHAR
445
 
    DRIZZLE_TYPE_VARCHAR,
446
 
    //DRIZZLE_TYPE_DECIMAL
447
 
    DRIZZLE_TYPE_VARCHAR,
448
 
    //DRIZZLE_TYPE_VARCHAR,
449
 
    DRIZZLE_TYPE_VARCHAR,
450
 
    //DRIZZLE_TYPE_BLOB
451
 
    DRIZZLE_TYPE_BLOB,
452
 
    //DRIZZLE_TYPE_TIME
453
 
    DRIZZLE_TYPE_TIME,
454
 
    //DRIZZLE_TYPE_BOOLEAN
455
 
    DRIZZLE_TYPE_VARCHAR,
456
 
    //DRIZZLE_TYPE_UUID
457
 
    DRIZZLE_TYPE_UUID,
458
 
    //DRIZZLE_TYPE_MICROTIME
459
 
    DRIZZLE_TYPE_VARCHAR,
460
 
  },
461
 
  /* DRIZZLE_TYPE_BOOLEAN -> */
462
 
  {
463
 
    //DRIZZLE_TYPE_LONG
464
 
    DRIZZLE_TYPE_VARCHAR,
465
 
    //DRIZZLE_TYPE_DOUBLE
466
 
    DRIZZLE_TYPE_VARCHAR,
467
 
    //DRIZZLE_TYPE_NULL
468
 
    DRIZZLE_TYPE_BOOLEAN,
469
 
    //DRIZZLE_TYPE_TIMESTAMP
470
 
    DRIZZLE_TYPE_VARCHAR,
471
 
    //DRIZZLE_TYPE_LONGLONG
472
 
    DRIZZLE_TYPE_VARCHAR,
473
 
    //DRIZZLE_TYPE_DATETIME
474
 
    DRIZZLE_TYPE_VARCHAR,
475
 
    //DRIZZLE_TYPE_DATE
476
 
    DRIZZLE_TYPE_VARCHAR,
477
 
    //DRIZZLE_TYPE_VARCHAR
478
 
    DRIZZLE_TYPE_VARCHAR,
479
 
    //DRIZZLE_TYPE_DECIMAL
480
 
    DRIZZLE_TYPE_VARCHAR,
481
 
    //DRIZZLE_TYPE_VARCHAR,
482
 
    DRIZZLE_TYPE_VARCHAR,
483
 
    //DRIZZLE_TYPE_BLOB
484
 
    DRIZZLE_TYPE_BLOB,
485
 
    //DRIZZLE_TYPE_TIME
486
 
    DRIZZLE_TYPE_TIME,
487
 
    //DRIZZLE_TYPE_BOOLEAN
488
 
    DRIZZLE_TYPE_BOOLEAN,
489
 
    //DRIZZLE_TYPE_UUID
490
 
    DRIZZLE_TYPE_VARCHAR,
491
 
    //DRIZZLE_TYPE_MICROTIME
492
 
    DRIZZLE_TYPE_VARCHAR,
493
 
  },
494
 
  /* DRIZZLE_TYPE_UUID -> */
495
 
  {
496
 
    //DRIZZLE_TYPE_LONG
497
 
    DRIZZLE_TYPE_VARCHAR,
498
 
    //DRIZZLE_TYPE_DOUBLE
499
 
    DRIZZLE_TYPE_VARCHAR,
500
 
    //DRIZZLE_TYPE_NULL
501
 
    DRIZZLE_TYPE_UUID,
502
 
    //DRIZZLE_TYPE_TIMESTAMP
503
 
    DRIZZLE_TYPE_VARCHAR,
504
 
    //DRIZZLE_TYPE_LONGLONG
505
 
    DRIZZLE_TYPE_VARCHAR,
506
 
    //DRIZZLE_TYPE_DATETIME
507
 
    DRIZZLE_TYPE_VARCHAR,
508
 
    //DRIZZLE_TYPE_DATE
509
 
    DRIZZLE_TYPE_VARCHAR,
510
 
    //DRIZZLE_TYPE_VARCHAR
511
 
    DRIZZLE_TYPE_VARCHAR,
512
 
    //DRIZZLE_TYPE_DECIMAL
513
 
    DRIZZLE_TYPE_VARCHAR,
514
 
    //DRIZZLE_TYPE_VARCHAR,
515
 
    DRIZZLE_TYPE_VARCHAR,
516
 
    //DRIZZLE_TYPE_BLOB
517
 
    DRIZZLE_TYPE_BLOB,
518
 
    //DRIZZLE_TYPE_TIME
519
 
    DRIZZLE_TYPE_TIME,
520
 
    //DRIZZLE_TYPE_BOOLEAN
521
 
    DRIZZLE_TYPE_VARCHAR,
522
 
    //DRIZZLE_TYPE_UUID
523
 
    DRIZZLE_TYPE_UUID,
524
 
    //DRIZZLE_TYPE_MICROTIME
525
 
    DRIZZLE_TYPE_VARCHAR,
526
 
  },
527
 
  /* DRIZZLE_TYPE_MICROTIME -> */
528
 
  {
529
 
    //DRIZZLE_TYPE_LONG
530
 
    DRIZZLE_TYPE_VARCHAR,
531
 
    //DRIZZLE_TYPE_DOUBLE
532
 
    DRIZZLE_TYPE_VARCHAR,
533
 
    //DRIZZLE_TYPE_NULL
534
 
    DRIZZLE_TYPE_MICROTIME,
535
 
    //DRIZZLE_TYPE_TIMESTAMP
536
 
    DRIZZLE_TYPE_VARCHAR,
537
 
    //DRIZZLE_TYPE_LONGLONG
538
 
    DRIZZLE_TYPE_VARCHAR,
539
 
    //DRIZZLE_TYPE_DATETIME
540
 
    DRIZZLE_TYPE_VARCHAR,
541
 
    //DRIZZLE_TYPE_DATE
542
 
    DRIZZLE_TYPE_VARCHAR,
543
 
    //DRIZZLE_TYPE_VARCHAR
544
 
    DRIZZLE_TYPE_VARCHAR,
545
 
    //DRIZZLE_TYPE_DECIMAL
546
 
    DRIZZLE_TYPE_VARCHAR,
547
 
    //DRIZZLE_TYPE_VARCHAR,
548
 
    DRIZZLE_TYPE_VARCHAR,
549
 
    //DRIZZLE_TYPE_BLOB
550
 
    DRIZZLE_TYPE_BLOB,
551
 
    //DRIZZLE_TYPE_TIME
552
 
    DRIZZLE_TYPE_TIME,
553
 
    //DRIZZLE_TYPE_BOOLEAN
554
 
    DRIZZLE_TYPE_VARCHAR,
555
 
    //DRIZZLE_TYPE_UUID
556
 
    DRIZZLE_TYPE_UUID,
557
 
    //DRIZZLE_TYPE_MICROTIME
558
 
    DRIZZLE_TYPE_MICROTIME,
559
331
  },
560
332
};
561
333
 
562
 
static Item_result field_types_result_type [enum_field_types_size]=
 
334
static Item_result field_types_result_type [DRIZZLE_TYPE_MAX+1]=
563
335
{
564
336
  //DRIZZLE_TYPE_LONG
565
337
  INT_RESULT,
583
355
  STRING_RESULT,
584
356
  //DRIZZLE_TYPE_BLOB
585
357
  STRING_RESULT,
586
 
  //DRIZZLE_TYPE_TIME
587
 
  STRING_RESULT,
588
 
  //DRIZZLE_TYPE_BOOLEAN
589
 
  STRING_RESULT,
590
 
  //DRIZZLE_TYPE_UUID
591
 
  STRING_RESULT,
592
 
  //DRIZZLE_TYPE_MICROTIME
593
 
  STRING_RESULT,
594
358
};
595
359
 
596
360
bool test_if_important_data(const CHARSET_INFO * const cs, 
609
373
 
610
374
void *Field::operator new(size_t size, memory::Root *mem_root)
611
375
{
612
 
  return mem_root->alloc_root(static_cast<uint32_t>(size));
 
376
  return alloc_root(mem_root, static_cast<uint32_t>(size));
613
377
}
614
378
 
615
379
enum_field_types Field::field_type_merge(enum_field_types a,
616
380
                                         enum_field_types b)
617
381
{
618
 
  assert(a < enum_field_types_size);
619
 
  assert(b < enum_field_types_size);
 
382
  assert(a <= DRIZZLE_TYPE_MAX);
 
383
  assert(b <= DRIZZLE_TYPE_MAX);
620
384
  return field_types_merge_rules[a][b];
621
385
}
622
386
 
623
387
Item_result Field::result_merge_type(enum_field_types field_type)
624
388
{
625
 
  assert(field_type < enum_field_types_size);
 
389
  assert(field_type <= DRIZZLE_TYPE_MAX);
626
390
  return field_types_result_type[field_type];
627
391
}
628
392
 
642
406
  return pack_length();
643
407
}
644
408
 
 
409
uint32_t Field::pack_length_from_metadata(uint32_t field_metadata)
 
410
{
 
411
  return field_metadata;
 
412
}
 
413
 
 
414
uint32_t Field::row_pack_length()
 
415
{
 
416
  return 0;
 
417
}
 
418
 
645
419
uint32_t Field::data_length()
646
420
{
647
421
  return pack_length();
673
447
 
674
448
void Field::set_default()
675
449
{
676
 
  ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->getInsertRecord());
 
450
  ptrdiff_t l_offset= (ptrdiff_t) (table->getDefaultValues() - table->record[0]);
677
451
  memcpy(ptr, ptr + l_offset, pack_length());
678
452
  if (null_ptr)
679
453
    *null_ptr= ((*null_ptr & (unsigned char) ~null_bit) | (null_ptr[l_offset] & null_bit));
758
532
{
759
533
  if (! null_ptr)
760
534
    return false;
761
 
  return test(record[(uint32_t) (null_ptr -table->getInsertRecord())] & null_bit);
 
535
  return test(record[(uint32_t) (null_ptr -table->record[0])] & null_bit);
762
536
}
763
537
 
764
538
bool Field::is_null_in_record_with_offset(ptrdiff_t with_offset)
819
593
void Field::init(Table *table_arg)
820
594
{
821
595
  orig_table= table= table_arg;
 
596
  table_name= &table_arg->alias;
 
597
}
 
598
 
 
599
String *Field::val_int_as_str(String *val_buffer, bool unsigned_val)
 
600
{
 
601
  const CHARSET_INFO * const cs= &my_charset_bin;
 
602
  uint32_t length;
 
603
  int64_t value= val_int();
 
604
 
 
605
  if (val_buffer->alloc(MY_INT64_NUM_DECIMAL_DIGITS))
 
606
    return 0;
 
607
  length= (uint32_t) (*cs->cset->int64_t10_to_str)(cs, (char*) val_buffer->ptr(),
 
608
                                                MY_INT64_NUM_DECIMAL_DIGITS,
 
609
                                                unsigned_val ? 10 : -10,
 
610
                                                value);
 
611
  val_buffer->length(length);
 
612
  return val_buffer;
822
613
}
823
614
 
824
615
/// This is used as a table name when the table structure is not set up
827
618
             unsigned char *null_ptr_arg,
828
619
             unsigned char null_bit_arg,
829
620
             utype unireg_check_arg, 
830
 
             const char *field_name_arg) :
 
621
             const char *field_name_arg)
 
622
  :
831
623
    ptr(ptr_arg),
832
624
    null_ptr(null_ptr_arg),
833
625
    table(NULL),
834
626
    orig_table(NULL),
 
627
    table_name(NULL),
835
628
    field_name(field_name_arg),
836
 
    comment(NULL_LEX_STRING),
837
629
    key_start(0),
838
630
    part_of_key(0),
839
631
    part_of_key_not_clustered(0),
840
632
    part_of_sortkey(0),
841
633
    unireg_check(unireg_check_arg),
842
634
    field_length(length_arg),
843
 
    flags(null_ptr ? 0: NOT_NULL_FLAG),
844
 
    field_index(0),
845
635
    null_bit(null_bit_arg),
846
636
    is_created_from_null_item(false)
847
637
{
 
638
  flags= null_ptr ? 0: NOT_NULL_FLAG;
 
639
  comment.str= (char*) "";
 
640
  comment.length= 0;
 
641
  field_index= 0;
848
642
}
849
643
 
850
644
void Field::hash(uint32_t *nr, uint32_t *nr2)
873
667
  }
874
668
}
875
669
 
876
 
int Field::store_and_check(enum_check_fields check_level,
877
 
                           const char *to, 
878
 
                           uint32_t length,
879
 
                           const CHARSET_INFO * const cs)
 
670
int Field::compatible_field_size(uint32_t field_metadata)
 
671
{
 
672
  uint32_t const source_size= pack_length_from_metadata(field_metadata);
 
673
  uint32_t const destination_size= row_pack_length();
 
674
  return (source_size <= destination_size);
 
675
}
880
676
 
 
677
int Field::store(const char *to, 
 
678
                 uint32_t length,
 
679
                 const CHARSET_INFO * const cs,
 
680
                 enum_check_fields check_level)
881
681
{
882
682
  int res;
883
683
  enum_check_fields old_check_level= table->in_use->count_cuted_fields;
897
697
 
898
698
unsigned char *Field::pack(unsigned char *to, const unsigned char *from)
899
699
{
900
 
  unsigned char *result= this->pack(to, from, UINT32_MAX, table->getShare()->db_low_byte_first);
 
700
  unsigned char *result= this->pack(to, from, UINT32_MAX, table->s->db_low_byte_first);
901
701
  return(result);
902
702
}
903
703
 
935
735
 
936
736
const unsigned char *Field::unpack(unsigned char* to, const unsigned char *from)
937
737
{
938
 
  const unsigned char *result= unpack(to, from, 0U, table->getShare()->db_low_byte_first);
 
738
  const unsigned char *result= unpack(to, from, 0U, table->s->db_low_byte_first);
939
739
  return(result);
940
740
}
941
741
 
942
 
type::Decimal *Field::val_decimal(type::Decimal *)
 
742
my_decimal *Field::val_decimal(my_decimal *)
943
743
{
944
744
  /* This never have to be called */
945
745
  assert(0);
949
749
 
950
750
void Field::make_field(SendField *field)
951
751
{
952
 
  if (orig_table && orig_table->getShare()->getSchemaName() && *orig_table->getShare()->getSchemaName())
 
752
  if (orig_table && orig_table->s->db.str && *orig_table->s->db.str)
953
753
  {
954
 
    field->db_name= orig_table->getShare()->getSchemaName();
955
 
    field->org_table_name= orig_table->getShare()->getTableName();
 
754
    field->db_name= orig_table->s->db.str;
 
755
    field->org_table_name= orig_table->s->table_name.str;
956
756
  }
957
757
  else
958
758
    field->org_table_name= field->db_name= "";
959
759
  if (orig_table)
960
760
  {
961
 
    field->table_name= orig_table->getAlias();
 
761
    field->table_name= orig_table->alias;
962
762
    field->org_col_name= field_name;
963
763
  }
964
764
  else
974
774
  field->decimals= 0;
975
775
}
976
776
 
977
 
int64_t Field::convert_decimal2int64_t(const type::Decimal *val, bool, int *err)
 
777
int64_t Field::convert_decimal2int64_t(const my_decimal *val, bool, int *err)
978
778
{
979
779
  int64_t i;
980
 
  if (warn_if_overflow(val->val_int32(E_DEC_ERROR &
 
780
  if (warn_if_overflow(my_decimal2int(E_DEC_ERROR &
981
781
                                      ~E_DEC_OVERFLOW & ~E_DEC_TRUNCATED,
982
 
                                      false, &i)))
 
782
                                      val, false, &i)))
983
783
  {
984
784
    i= (val->sign() ? INT64_MIN : INT64_MAX);
985
785
    *err= 1;
987
787
  return i;
988
788
}
989
789
 
990
 
uint32_t Field::fill_cache_field(CacheField *copy)
 
790
uint32_t Field::fill_cache_field(CACHE_FIELD *copy)
991
791
{
992
792
  uint32_t store_length;
993
793
  copy->str=ptr;
997
797
  {
998
798
    copy->blob_field=(Field_blob*) this;
999
799
    copy->strip=0;
1000
 
    copy->length-= table->getShare()->sizeBlobPtr();
 
800
    copy->length-= table->s->blob_ptr_size;
1001
801
    return copy->length;
1002
802
  }
1003
803
  else
1008
808
  return copy->length+ store_length;
1009
809
}
1010
810
 
1011
 
bool Field::get_date(type::Time &ltime, uint32_t fuzzydate)
1012
 
{
1013
 
  char buff[type::Time::MAX_STRING_LENGTH];
1014
 
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
1015
 
 
1016
 
  assert(getTable() and getTable()->getSession());
1017
 
 
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;
1027
 
}
1028
 
 
1029
 
bool Field::get_time(type::Time &ltime)
1030
 
{
1031
 
  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;
1041
 
}
1042
 
 
1043
 
int Field::store_time(type::Time &ltime, type::timestamp_t)
1044
 
{
1045
 
  String tmp;
1046
 
 
1047
 
  ltime.convert(tmp);
1048
 
 
1049
 
  return store(tmp.ptr(), tmp.length(), &my_charset_bin);
 
811
bool Field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
812
{
 
813
  char buff[40];
 
814
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
 
815
  if (!(res=val_str(&tmp)) ||
 
816
      str_to_datetime_with_warn(res->ptr(), res->length(),
 
817
                                ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
 
818
    return 1;
 
819
  return 0;
 
820
}
 
821
 
 
822
bool Field::get_time(DRIZZLE_TIME *ltime)
 
823
{
 
824
  char buff[40];
 
825
  String tmp(buff,sizeof(buff),&my_charset_bin),*res;
 
826
  if (!(res=val_str(&tmp)) ||
 
827
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
 
828
    return 1;
 
829
  return 0;
 
830
}
 
831
 
 
832
int Field::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
 
833
{
 
834
  char buff[MAX_DATE_STRING_REP_LENGTH];
 
835
  uint32_t length= (uint32_t) my_TIME_to_str(ltime, buff);
 
836
  return store(buff, length, &my_charset_bin);
1050
837
}
1051
838
 
1052
839
bool Field::optimize_range(uint32_t idx, uint32_t)
1057
844
Field *Field::new_field(memory::Root *root, Table *new_table, bool)
1058
845
{
1059
846
  Field *tmp;
1060
 
  if (!(tmp= (Field*) root->memdup_root((char*) this,size_of())))
 
847
  if (!(tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1061
848
    return 0;
1062
849
 
1063
850
  if (tmp->table->maybe_null)
1067
854
  tmp->part_of_key.reset();
1068
855
  tmp->part_of_sortkey.reset();
1069
856
  tmp->unireg_check= Field::NONE;
1070
 
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG);
 
857
  tmp->flags&= (NOT_NULL_FLAG | BLOB_FLAG | UNSIGNED_FLAG | BINARY_FLAG | ENUM_FLAG | SET_FLAG);
1071
858
  tmp->reset_fields();
1072
859
  return tmp;
1073
860
}
1090
877
Field *Field::clone(memory::Root *root, Table *new_table)
1091
878
{
1092
879
  Field *tmp;
1093
 
  if ((tmp= (Field*) root->memdup_root((char*) this,size_of())))
 
880
  if ((tmp= (Field*) memdup_root(root,(char*) this,size_of())))
1094
881
  {
1095
882
    tmp->init(new_table);
1096
 
    tmp->move_field_offset((ptrdiff_t) (new_table->getInsertRecord() -
1097
 
                                           new_table->getDefaultValues()));
 
883
    tmp->move_field_offset((ptrdiff_t) (new_table->record[0] -
 
884
                                           new_table->s->default_values));
1098
885
  }
1099
886
  return tmp;
1100
887
}
1117
904
{
1118
905
  if (!Field::eq_def(field))
1119
906
    return 0;
1120
 
 
1121
907
  TYPELIB *from_lib=((Field_enum*) field)->typelib;
1122
908
 
1123
909
  if (typelib->count < from_lib->count)
1124
910
    return 0;
1125
 
 
1126
911
  for (uint32_t i=0 ; i < from_lib->count ; i++)
1127
 
  {
1128
912
    if (my_strnncoll(field_charset,
1129
913
                     (const unsigned char*)typelib->type_names[i],
1130
914
                     strlen(typelib->type_names[i]),
1131
915
                     (const unsigned char*)from_lib->type_names[i],
1132
916
                     strlen(from_lib->type_names[i])))
1133
917
      return 0;
1134
 
  }
1135
 
 
1136
918
  return 1;
1137
919
}
1138
920
 
1140
922
{
1141
923
  switch (type) {
1142
924
  case DRIZZLE_TYPE_VARCHAR: return (length + (length < 256 ? 1: 2));
1143
 
  case DRIZZLE_TYPE_UUID: return field::Uuid::max_string_length();
1144
 
  case DRIZZLE_TYPE_MICROTIME: return field::Microtime::max_string_length();
1145
 
  case DRIZZLE_TYPE_TIMESTAMP: return field::Epoch::max_string_length();
1146
 
  case DRIZZLE_TYPE_BOOLEAN: return field::Boolean::max_string_length();
1147
 
  case DRIZZLE_TYPE_DATE:
1148
 
  case DRIZZLE_TYPE_ENUM:
 
925
  case DRIZZLE_TYPE_DATE: return 3;
 
926
  case DRIZZLE_TYPE_TIMESTAMP:
1149
927
  case DRIZZLE_TYPE_LONG: return 4;
1150
928
  case DRIZZLE_TYPE_DOUBLE: return sizeof(double);
1151
 
  case DRIZZLE_TYPE_TIME:
1152
929
  case DRIZZLE_TYPE_DATETIME:
1153
930
  case DRIZZLE_TYPE_LONGLONG: return 8; /* Don't crash if no int64_t */
1154
931
  case DRIZZLE_TYPE_NULL: return 0;
1155
932
  case DRIZZLE_TYPE_BLOB: return 4 + portable_sizeof_char_ptr;
 
933
  case DRIZZLE_TYPE_ENUM:
1156
934
  case DRIZZLE_TYPE_DECIMAL:
1157
 
                          break;
 
935
    abort();
 
936
  default:
 
937
    return 0;
1158
938
  }
1159
 
 
1160
 
  assert(0);
1161
 
  abort();
1162
939
}
1163
940
 
1164
941
uint32_t pack_length_to_packflag(uint32_t type)
1167
944
    case 1: return 1 << FIELDFLAG_PACK_SHIFT;
1168
945
    case 2: assert(1);
1169
946
    case 3: assert(1);
1170
 
    case 4: return f_settype(DRIZZLE_TYPE_LONG);
1171
 
    case 8: return f_settype(DRIZZLE_TYPE_LONGLONG);
 
947
    case 4: return f_settype((uint32_t) DRIZZLE_TYPE_LONG);
 
948
    case 8: return f_settype((uint32_t) DRIZZLE_TYPE_LONGLONG);
1172
949
  }
1173
950
  return 0;                                     // This shouldn't happen
1174
951
}
1175
952
 
 
953
Field *make_field(TableShare *share,
 
954
                  memory::Root *root,
 
955
                  unsigned char *ptr,
 
956
                  uint32_t field_length,
 
957
                  bool is_nullable,
 
958
                  unsigned char *null_pos,
 
959
                  unsigned char null_bit,
 
960
                  uint8_t decimals,
 
961
                  enum_field_types field_type,
 
962
                  const CHARSET_INFO * field_charset,
 
963
                  Field::utype unireg_check,
 
964
                  TYPELIB *interval,
 
965
                  const char *field_name)
 
966
{
 
967
  if(! root)
 
968
    root= current_mem_root();
 
969
 
 
970
  if (! is_nullable)
 
971
  {
 
972
    null_pos=0;
 
973
    null_bit=0;
 
974
  }
 
975
  else
 
976
  {
 
977
    null_bit= ((unsigned char) 1) << null_bit;
 
978
  }
 
979
 
 
980
  switch (field_type) 
 
981
  {
 
982
  case DRIZZLE_TYPE_DATE:
 
983
  case DRIZZLE_TYPE_DATETIME:
 
984
  case DRIZZLE_TYPE_TIMESTAMP:
 
985
    field_charset= &my_charset_bin;
 
986
  default: break;
 
987
  }
 
988
 
 
989
  if (field_type == DRIZZLE_TYPE_VARCHAR ||
 
990
      field_type == DRIZZLE_TYPE_BLOB ||
 
991
      field_type == DRIZZLE_TYPE_ENUM)
 
992
  {
 
993
    if (field_type == DRIZZLE_TYPE_VARCHAR)
 
994
      return new (root) Field_varstring(ptr,field_length,
 
995
                                  HA_VARCHAR_PACKLENGTH(field_length),
 
996
                                  null_pos,null_bit,
 
997
                                  field_name,
 
998
                                  share,
 
999
                                  field_charset);
 
1000
 
 
1001
    if (field_type == DRIZZLE_TYPE_BLOB)
 
1002
    {
 
1003
      return new (root) Field_blob(ptr,
 
1004
                                   null_pos,
 
1005
                                   null_bit,
 
1006
                                   field_name,
 
1007
                                   share,
 
1008
                                   calc_pack_length(DRIZZLE_TYPE_LONG, 0),
 
1009
                                   field_charset);
 
1010
    }
 
1011
 
 
1012
    if (interval)
 
1013
    {
 
1014
      return new (root) Field_enum(ptr,
 
1015
                                   field_length,
 
1016
                                   null_pos,
 
1017
                                   null_bit,
 
1018
                                   field_name,
 
1019
                                   get_enum_pack_length(interval->count),
 
1020
                                   interval,
 
1021
                                   field_charset);
 
1022
    }
 
1023
  }
 
1024
 
 
1025
  switch (field_type)
 
1026
  {
 
1027
  case DRIZZLE_TYPE_DECIMAL:
 
1028
    return new (root) Field_decimal(ptr,
 
1029
                                    field_length,
 
1030
                                    null_pos,
 
1031
                                    null_bit,
 
1032
                                    unireg_check,
 
1033
                                    field_name,
 
1034
                                    decimals,
 
1035
                                    false,
 
1036
                                    false /* is_unsigned */);
 
1037
  case DRIZZLE_TYPE_DOUBLE:
 
1038
    return new (root) Field_double(ptr,
 
1039
                                   field_length,
 
1040
                                   null_pos,
 
1041
                                   null_bit,
 
1042
                                   unireg_check,
 
1043
                                   field_name,
 
1044
                                   decimals,
 
1045
                                   false,
 
1046
                                   false /* is_unsigned */);
 
1047
  case DRIZZLE_TYPE_LONG:
 
1048
    return new (root) Field_long(ptr,
 
1049
                                 field_length,
 
1050
                                 null_pos,
 
1051
                                 null_bit,
 
1052
                                 unireg_check,
 
1053
                                 field_name,
 
1054
                                 false,
 
1055
                                 false /* is_unsigned */);
 
1056
  case DRIZZLE_TYPE_LONGLONG:
 
1057
    return new (root) Field_int64_t(ptr,
 
1058
                                    field_length,
 
1059
                                    null_pos,
 
1060
                                    null_bit,
 
1061
                                    unireg_check,
 
1062
                                    field_name,
 
1063
                                    false,
 
1064
                                    false /* is_unsigned */);
 
1065
  case DRIZZLE_TYPE_TIMESTAMP:
 
1066
    return new (root) Field_timestamp(ptr,
 
1067
                                      field_length,
 
1068
                                      null_pos,
 
1069
                                      null_bit,
 
1070
                                      unireg_check,
 
1071
                                      field_name,
 
1072
                                      share,
 
1073
                                      field_charset);
 
1074
  case DRIZZLE_TYPE_DATE:
 
1075
    return new (root) Field_date(ptr,
 
1076
                                 null_pos,
 
1077
                                 null_bit,
 
1078
                                 field_name,
 
1079
                                 field_charset);
 
1080
  case DRIZZLE_TYPE_DATETIME:
 
1081
    return new (root) Field_datetime(ptr,
 
1082
                                     null_pos,
 
1083
                                     null_bit,
 
1084
                                     field_name,
 
1085
                                     field_charset);
 
1086
  case DRIZZLE_TYPE_NULL:
 
1087
    return new (root) Field_null(ptr,
 
1088
                                 field_length,
 
1089
                                 field_name,
 
1090
                                 field_charset);
 
1091
  default: // Impossible (Wrong version)
 
1092
    break;
 
1093
  }
 
1094
  return 0;
 
1095
}
 
1096
 
1176
1097
/*****************************************************************************
1177
1098
 Warning handling
1178
1099
*****************************************************************************/
1179
1100
 
1180
1101
bool Field::set_warning(DRIZZLE_ERROR::enum_warning_level level,
1181
 
                        drizzled::error_t code,
 
1102
                        uint32_t code,
1182
1103
                        int cuted_increment)
1183
1104
{
1184
1105
  /*
1198
1119
 
1199
1120
 
1200
1121
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1201
 
                                 drizzled::error_t code,
 
1122
                                 unsigned int code,
1202
1123
                                 const char *str, 
1203
1124
                                 uint32_t str_length,
1204
 
                                 type::timestamp_t ts_type, 
 
1125
                                 enum enum_drizzle_timestamp_type ts_type, 
1205
1126
                                 int cuted_increment)
1206
1127
{
1207
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1208
 
 
1209
 
  if ((session->abortOnWarning() and
 
1128
  Session *session= table ? table->in_use : current_session;
 
1129
  if ((session->really_abort_on_warning() &&
1210
1130
       level >= DRIZZLE_ERROR::WARN_LEVEL_WARN) ||
1211
1131
      set_warning(level, code, cuted_increment))
1212
1132
    make_truncated_value_warning(session, level, str, str_length, ts_type,
1214
1134
}
1215
1135
 
1216
1136
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level, 
1217
 
                                 drizzled::error_t code,
 
1137
                                 uint32_t code,
1218
1138
                                 int64_t nr, 
1219
 
                                 type::timestamp_t ts_type,
 
1139
                                 enum enum_drizzle_timestamp_type ts_type,
1220
1140
                                 int cuted_increment)
1221
1141
{
1222
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1223
 
 
1224
 
  if (session->abortOnWarning() or
 
1142
  Session *session= table ? table->in_use : current_session;
 
1143
  if (session->really_abort_on_warning() ||
1225
1144
      set_warning(level, code, cuted_increment))
1226
1145
  {
1227
 
    char str_nr[DECIMAL_LONGLONG_DIGITS];
 
1146
    char str_nr[22];
1228
1147
    char *str_end= internal::int64_t10_to_str(nr, str_nr, -10);
1229
1148
    make_truncated_value_warning(session, level, str_nr, (uint32_t) (str_end - str_nr),
1230
1149
                                 ts_type, field_name);
1232
1151
}
1233
1152
 
1234
1153
void Field::set_datetime_warning(DRIZZLE_ERROR::enum_warning_level level,
1235
 
                                 const drizzled::error_t code,
 
1154
                                 const uint32_t code,
1236
1155
                                 double nr, 
1237
 
                                 type::timestamp_t ts_type)
 
1156
                                 enum enum_drizzle_timestamp_type ts_type)
1238
1157
{
1239
 
  Session *session= (getTable() and getTable()->getSession()) ? getTable()->getSession() : current_session;
1240
 
 
1241
 
  if (session->abortOnWarning() or
 
1158
  Session *session= table ? table->in_use : current_session;
 
1159
  if (session->really_abort_on_warning() ||
1242
1160
      set_warning(level, code, 1))
1243
1161
  {
1244
1162
    /* DBL_DIG is enough to print '-[digits].E+###' */
1245
1163
    char str_nr[DBL_DIG + 8];
1246
 
    uint32_t str_len= snprintf(str_nr, sizeof(str_nr), "%g", nr);
 
1164
    uint32_t str_len= sprintf(str_nr, "%g", nr);
1247
1165
    make_truncated_value_warning(session, level, str_nr, str_len, ts_type,
1248
1166
                                 field_name);
1249
1167
  }
1275
1193
    table->clearWriteSet(field_index);
1276
1194
}
1277
1195
 
1278
 
void Field::pack_num(uint64_t arg, unsigned char *destination)
1279
 
{
1280
 
  if (not destination)
1281
 
    destination= ptr;
1282
 
 
1283
 
  int64_tstore(destination, arg);
1284
 
}
1285
 
 
1286
 
void Field::pack_num(uint32_t arg, unsigned char *destination)
1287
 
{
1288
 
  if (not destination)
1289
 
    destination= ptr;
1290
 
 
1291
 
  longstore(destination, arg);
1292
 
}
1293
 
 
1294
 
uint64_t Field::unpack_num(uint64_t &destination, const unsigned char *arg) const
1295
 
{
1296
 
  if (not arg)
1297
 
    arg= ptr;
1298
 
 
1299
 
  int64_tget(destination, arg);
1300
 
 
1301
 
  return destination;
1302
 
}
1303
 
 
1304
 
uint32_t Field::unpack_num(uint32_t &destination, const unsigned char *arg) const
1305
 
{
1306
 
  if (not arg)
1307
 
    arg= ptr;
1308
 
 
1309
 
  longget(destination, arg);
1310
 
 
1311
 
  return destination;
1312
 
}
1313
 
 
1314
 
std::ostream& operator<<(std::ostream& output, const Field &field)
1315
 
{
1316
 
  output << "Field:(";
1317
 
  output <<  field.field_name;
1318
 
  output << ", ";
1319
 
  output << drizzled::display::type(field.real_type());
1320
 
  output << ", { ";
1321
 
 
1322
 
  if (field.flags & NOT_NULL_FLAG)
1323
 
    output << " NOT_NULL";
1324
 
 
1325
 
  if (field.flags & PRI_KEY_FLAG)
1326
 
    output << ", PRIMARY KEY";
1327
 
 
1328
 
  if (field.flags & UNIQUE_KEY_FLAG)
1329
 
    output << ", UNIQUE KEY";
1330
 
 
1331
 
  if (field.flags & MULTIPLE_KEY_FLAG)
1332
 
    output << ", MULTIPLE KEY";
1333
 
 
1334
 
  if (field.flags & BLOB_FLAG)
1335
 
    output << ", BLOB";
1336
 
 
1337
 
  if (field.flags & UNSIGNED_FLAG)
1338
 
    output << ", UNSIGNED";
1339
 
 
1340
 
  if (field.flags & BINARY_FLAG)
1341
 
    output << ", BINARY";
1342
 
  output << "}, ";
1343
 
  output << ")";
1344
 
 
1345
 
  return output;  // for multiple << operators.
1346
 
}
1347
 
 
1348
1196
} /* namespace drizzled */