38
39
#include <drizzled/field/decimal.h>
39
40
#include <drizzled/field/real.h>
40
41
#include <drizzled/field/double.h>
41
#include <drizzled/field/int32.h>
42
#include <drizzled/field/int64.h>
42
#include <drizzled/field/long.h>
43
#include <drizzled/field/int64_t.h>
43
44
#include <drizzled/field/num.h>
45
#include <drizzled/field/timetype.h>
44
46
#include <drizzled/field/timestamp.h>
45
47
#include <drizzled/field/datetime.h>
46
48
#include <drizzled/field/varstring.h>
51
static void do_field_eq(CopyField *copy)
53
memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
56
static void do_field_1(CopyField *copy)
58
copy->to_ptr[0]= copy->from_ptr[0];
61
static void do_field_2(CopyField *copy)
63
copy->to_ptr[0]= copy->from_ptr[0];
64
copy->to_ptr[1]= copy->from_ptr[1];
67
static void do_field_3(CopyField *copy)
69
copy->to_ptr[0]= copy->from_ptr[0];
70
copy->to_ptr[1]= copy->from_ptr[1];
71
copy->to_ptr[2]= copy->from_ptr[2];
74
static void do_field_4(CopyField *copy)
76
copy->to_ptr[0]= copy->from_ptr[0];
77
copy->to_ptr[1]= copy->from_ptr[1];
78
copy->to_ptr[2]= copy->from_ptr[2];
79
copy->to_ptr[3]= copy->from_ptr[3];
82
static void do_field_6(CopyField *copy)
84
copy->to_ptr[0]= copy->from_ptr[0];
85
copy->to_ptr[1]= copy->from_ptr[1];
86
copy->to_ptr[2]= copy->from_ptr[2];
87
copy->to_ptr[3]= copy->from_ptr[3];
88
copy->to_ptr[4]= copy->from_ptr[4];
89
copy->to_ptr[5]= copy->from_ptr[5];
92
static void do_field_8(CopyField *copy)
94
copy->to_ptr[0]= copy->from_ptr[0];
95
copy->to_ptr[1]= copy->from_ptr[1];
96
copy->to_ptr[2]= copy->from_ptr[2];
97
copy->to_ptr[3]= copy->from_ptr[3];
98
copy->to_ptr[4]= copy->from_ptr[4];
99
copy->to_ptr[5]= copy->from_ptr[5];
100
copy->to_ptr[6]= copy->from_ptr[6];
101
copy->to_ptr[7]= copy->from_ptr[7];
105
static void do_field_to_null_str(CopyField *copy)
51
static void do_field_eq(Copy_field *copy)
53
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
56
static void do_field_1(Copy_field *copy)
58
copy->to_ptr[0]=copy->from_ptr[0];
61
static void do_field_2(Copy_field *copy)
63
copy->to_ptr[0]=copy->from_ptr[0];
64
copy->to_ptr[1]=copy->from_ptr[1];
67
static void do_field_3(Copy_field *copy)
69
copy->to_ptr[0]=copy->from_ptr[0];
70
copy->to_ptr[1]=copy->from_ptr[1];
71
copy->to_ptr[2]=copy->from_ptr[2];
74
static void do_field_4(Copy_field *copy)
76
copy->to_ptr[0]=copy->from_ptr[0];
77
copy->to_ptr[1]=copy->from_ptr[1];
78
copy->to_ptr[2]=copy->from_ptr[2];
79
copy->to_ptr[3]=copy->from_ptr[3];
82
static void do_field_6(Copy_field *copy)
84
copy->to_ptr[0]=copy->from_ptr[0];
85
copy->to_ptr[1]=copy->from_ptr[1];
86
copy->to_ptr[2]=copy->from_ptr[2];
87
copy->to_ptr[3]=copy->from_ptr[3];
88
copy->to_ptr[4]=copy->from_ptr[4];
89
copy->to_ptr[5]=copy->from_ptr[5];
92
static void do_field_8(Copy_field *copy)
94
copy->to_ptr[0]=copy->from_ptr[0];
95
copy->to_ptr[1]=copy->from_ptr[1];
96
copy->to_ptr[2]=copy->from_ptr[2];
97
copy->to_ptr[3]=copy->from_ptr[3];
98
copy->to_ptr[4]=copy->from_ptr[4];
99
copy->to_ptr[5]=copy->from_ptr[5];
100
copy->to_ptr[6]=copy->from_ptr[6];
101
copy->to_ptr[7]=copy->from_ptr[7];
105
static void do_field_to_null_str(Copy_field *copy)
107
107
if (*copy->from_null_ptr & copy->from_bit)
109
109
memset(copy->to_ptr, 0, copy->from_length);
110
copy->to_null_ptr[0]= 1; // Always bit 1
110
copy->to_null_ptr[0]=1; // Always bit 1
114
copy->to_null_ptr[0]= 0;
115
memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
114
copy->to_null_ptr[0]=0;
115
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
120
static void do_outer_field_to_null_str(CopyField *copy)
120
static void do_outer_field_to_null_str(Copy_field *copy)
122
122
if (*copy->null_row ||
123
123
(copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
125
125
memset(copy->to_ptr, 0, copy->from_length);
126
copy->to_null_ptr[0]= 1; // Always bit 1
126
copy->to_null_ptr[0]=1; // Always bit 1
130
copy->to_null_ptr[0]= 0;
131
memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
130
copy->to_null_ptr[0]=0;
131
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
194
194
return 0; // Ok to set time to NULL
197
if (field == field->getTable()->next_number_field)
197
if (field == field->table->next_number_field)
199
field->getTable()->auto_increment_field_not_null= false;
199
field->table->auto_increment_field_not_null= false;
200
200
return 0; // field is set in fill_record()
202
if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
202
if (field->table->in_use->count_cuted_fields == CHECK_FIELD_WARN)
204
204
field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_BAD_NULL_ERROR, 1);
207
if (!field->getTable()->in_use->no_errors)
207
if (!field->table->in_use->no_errors)
208
208
my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
213
static void do_skip(CopyField *)
213
static void do_skip(Copy_field *copy __attribute__((unused)))
218
static void do_copy_null(CopyField *copy)
218
static void do_copy_null(Copy_field *copy)
220
220
if (*copy->from_null_ptr & copy->from_bit)
222
*copy->to_null_ptr|= copy->to_bit;
222
*copy->to_null_ptr|=copy->to_bit;
223
223
copy->to_field->reset();
301
static void do_copy_blob(CopyField *copy)
295
static void do_copy_blob(Copy_field *copy)
303
ulong length= ((Field_blob*) copy->from_field)->get_length();
297
ulong length=((Field_blob*) copy->from_field)->get_length();
304
298
((Field_blob*) copy->to_field)->store_length(length);
305
memcpy(copy->to_ptr, copy->from_ptr, sizeof(char*));
299
memcpy(copy->to_ptr,copy->from_ptr,sizeof(char*));
308
static void do_conv_blob(CopyField *copy)
302
static void do_conv_blob(Copy_field *copy)
310
copy->from_field->val_str_internal(©->tmp);
304
copy->from_field->val_str(©->tmp);
311
305
((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
313
copy->tmp.charset());
307
copy->tmp.charset());
316
310
/** Save blob in copy->tmp for GROUP BY. */
318
static void do_save_blob(CopyField *copy)
312
static void do_save_blob(Copy_field *copy)
320
314
char buff[MAX_FIELD_WIDTH];
321
String res(buff, sizeof(buff), copy->tmp.charset());
322
copy->from_field->val_str_internal(&res);
315
String res(buff,sizeof(buff),copy->tmp.charset());
316
copy->from_field->val_str(&res);
323
317
copy->tmp.copy(res);
324
318
((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
326
copy->tmp.charset());
320
copy->tmp.charset());
330
static void do_field_string(CopyField *copy)
324
static void do_field_string(Copy_field *copy)
332
326
char buff[MAX_FIELD_WIDTH];
333
327
copy->tmp.set_quick(buff,sizeof(buff),copy->tmp.charset());
334
copy->from_field->val_str_internal(©->tmp);
328
copy->from_field->val_str(©->tmp);
335
329
copy->to_field->store(copy->tmp.c_ptr_quick(),copy->tmp.length(),
336
330
copy->tmp.charset());
340
static void do_field_enum(CopyField *copy)
334
static void do_field_enum(Copy_field *copy)
342
336
if (copy->from_field->val_int() == 0)
343
337
((Field_enum *) copy->to_field)->store_type((uint64_t) 0);
429
static void do_expand_binary(CopyField *copy)
431
const CHARSET_INFO * const cs= copy->from_field->charset();
432
memcpy(copy->to_ptr, copy->from_ptr, copy->from_length);
433
cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
434
copy->to_length-copy->from_length, '\0');
439
static void do_expand_string(CopyField *copy)
441
const CHARSET_INFO * const cs= copy->from_field->charset();
442
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
443
cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
444
copy->to_length-copy->from_length, ' ');
448
static void do_varstring1(CopyField *copy)
450
uint32_t length= (uint32_t) *(unsigned char*) copy->from_ptr;
423
static void do_expand_binary(Copy_field *copy)
425
const CHARSET_INFO * const cs= copy->from_field->charset();
426
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
427
cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
428
copy->to_length-copy->from_length, '\0');
433
static void do_expand_string(Copy_field *copy)
435
const CHARSET_INFO * const cs= copy->from_field->charset();
436
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
437
cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
438
copy->to_length-copy->from_length, ' ');
442
static void do_varstring1(Copy_field *copy)
444
uint32_t length= (uint) *(unsigned char*) copy->from_ptr;
451
445
if (length > copy->to_length- 1)
453
length= copy->to_length - 1;
454
if (copy->from_field->getTable()->in_use->count_cuted_fields)
447
length=copy->to_length - 1;
448
if (copy->from_field->table->in_use->count_cuted_fields)
455
449
copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
456
450
ER_WARN_DATA_TRUNCATED, 1);
530
524
The 'to' buffer should have a size of field->pack_length()+1
533
void CopyField::set(unsigned char *to,Field *from)
527
void Copy_field::set(unsigned char *to,Field *from)
537
from_length= from->pack_length();
531
from_length=from->pack_length();
538
532
if (from->maybe_null())
540
from_null_ptr= from->null_ptr;
541
from_bit= from->null_bit;
542
to_ptr[0]= 1; // Null as default value
543
to_null_ptr= (unsigned char*) to_ptr++;
545
if (from->getTable()->maybe_null)
534
from_null_ptr=from->null_ptr;
535
from_bit= from->null_bit;
536
to_ptr[0]= 1; // Null as default value
537
to_null_ptr= (unsigned char*) to_ptr++;
539
if (from->table->maybe_null)
547
null_row= &from->getTable()->null_row;
548
do_copy= do_outer_field_to_null_str;
541
null_row= &from->table->null_row;
542
do_copy= do_outer_field_to_null_str;
551
do_copy= do_field_to_null_str;
545
do_copy= do_field_to_null_str;
555
to_null_ptr= 0; // For easy debugging
556
do_copy= do_field_eq;
549
to_null_ptr= 0; // For easy debugging
550
do_copy= do_field_eq;
573
567
- The above causes a truncation to MAX_FIELD_WIDTH. Is this the intended
574
568
effect? Truncation is handled by well_formed_copy_nchars anyway.
576
void CopyField::set(Field *to,Field *from,bool save)
570
void Copy_field::set(Field *to,Field *from,bool save)
578
572
if (to->type() == DRIZZLE_TYPE_NULL)
580
to_null_ptr= 0; // For easy debugging
574
to_null_ptr=0; // For easy debugging
588
from_length= from->pack_length();
590
to_length= to_field->pack_length();
582
from_length=from->pack_length();
584
to_length=to_field->pack_length();
592
586
// set up null handling
593
from_null_ptr= to_null_ptr= 0;
587
from_null_ptr=to_null_ptr=0;
594
588
if (from->maybe_null())
596
from_null_ptr= from->null_ptr;
597
from_bit= from->null_bit;
590
from_null_ptr= from->null_ptr;
591
from_bit= from->null_bit;
598
592
if (to_field->real_maybe_null())
600
to_null_ptr= to->null_ptr;
601
to_bit= to->null_bit;
594
to_null_ptr= to->null_ptr;
595
to_bit= to->null_bit;
602
596
if (from_null_ptr)
604
do_copy= do_copy_null;
597
do_copy= do_copy_null;
608
null_row= &from->getTable()->null_row;
609
do_copy= do_outer_field_null;
600
null_row= &from->table->null_row;
601
do_copy= do_outer_field_null;
614
606
if (to_field->type() == DRIZZLE_TYPE_TIMESTAMP)
616
607
do_copy= do_copy_timestamp; // Automatic timestamp
618
else if (to_field == to_field->getTable()->next_number_field)
608
else if (to_field == to_field->table->next_number_field)
620
609
do_copy= do_copy_next_number;
624
611
do_copy= do_copy_not_null;
628
614
else if (to_field->real_maybe_null())
630
to_null_ptr= to->null_ptr;
631
to_bit= to->null_bit;
616
to_null_ptr= to->null_ptr;
617
to_bit= to->null_bit;
632
618
do_copy= do_copy_maybe_null;
637
623
if ((to->flags & BLOB_FLAG) && save)
638
624
do_copy2= do_save_blob;
640
626
do_copy2= get_copy_func(to,from);
641
if (!do_copy) // Not null
627
if (!do_copy) // Not null
646
CopyField::Copy_func *
647
CopyField::get_copy_func(Field *to,Field *from)
632
Copy_field::Copy_func *
633
Copy_field::get_copy_func(Field *to,Field *from)
649
bool compatible_db_low_byte_first= (to->getTable()->getShare()->db_low_byte_first ==
650
from->getTable()->getShare()->db_low_byte_first);
635
bool compatible_db_low_byte_first= (to->table->s->db_low_byte_first ==
636
from->table->s->db_low_byte_first);
651
637
if (to->flags & BLOB_FLAG)
653
639
if (!(from->flags & BLOB_FLAG) || from->charset() != to->charset())
701
680
else if (to->charset() != from->charset())
702
return do_field_string;
681
return do_field_string;
703
682
else if (to->real_type() == DRIZZLE_TYPE_VARCHAR)
705
/* Field_blob is not part of the Field_varstring hierarchy,
706
and casting to varstring for calling pack_length_no_ptr()
707
is always incorrect. Previously the below comparison has
708
always evaluated to false as pack_length_no_ptr() for BLOB
709
will return 4 and varstring can only be <= 2.
710
If your brain slightly bleeds as to why this worked for
711
so many years, you are in no way alone.
713
if (from->flags & BLOB_FLAG)
714
return do_field_string;
716
if ((static_cast<Field_varstring*>(to))->pack_length_no_ptr() !=
717
(static_cast<Field_varstring*>(from))->pack_length_no_ptr())
719
return do_field_string;
684
if (((Field_varstring*) to)->length_bytes !=
685
((Field_varstring*) from)->length_bytes)
686
return do_field_string;
722
687
if (to_length != from_length)
724
return (((Field_varstring*) to)->pack_length_no_ptr() == 1 ?
688
return (((Field_varstring*) to)->length_bytes == 1 ?
725
689
(from->charset()->mbmaxlen == 1 ? do_varstring1 :
727
691
(from->charset()->mbmaxlen == 1 ? do_varstring2 :
731
694
else if (to_length < from_length)
733
return (from->charset()->mbmaxlen == 1 ?
695
return (from->charset()->mbmaxlen == 1 ?
734
696
do_cut_string : do_cut_string_complex);
736
697
else if (to_length > from_length)
738
699
if (to->charset() == &my_charset_bin)
784
742
int field_conv(Field *to,Field *from)
786
744
if (to->real_type() == from->real_type() &&
787
!(to->type() == DRIZZLE_TYPE_BLOB && to->getTable()->copy_blobs))
745
!(to->type() == DRIZZLE_TYPE_BLOB && to->table->copy_blobs))
789
747
/* Please god, will someone rewrite this to be readable :( */
790
748
if (to->pack_length() == from->pack_length() &&
791
749
!(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) &&
792
750
to->real_type() != DRIZZLE_TYPE_ENUM &&
793
(to->real_type() != DRIZZLE_TYPE_DECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
751
(to->real_type() != DRIZZLE_TYPE_NEWDECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
794
752
from->charset() == to->charset() &&
795
to->getTable()->getShare()->db_low_byte_first == from->getTable()->getShare()->db_low_byte_first &&
796
(!(to->getTable()->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || (to->type() != DRIZZLE_TYPE_DATE && to->type() != DRIZZLE_TYPE_DATETIME)) &&
797
(from->real_type() != DRIZZLE_TYPE_VARCHAR || ((Field_varstring*)from)->pack_length_no_ptr() == ((Field_varstring*)to)->pack_length_no_ptr()))
753
to->table->s->db_low_byte_first == from->table->s->db_low_byte_first &&
754
(!(to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || (to->type() != DRIZZLE_TYPE_DATE && to->type() != DRIZZLE_TYPE_DATETIME)) &&
755
(from->real_type() != DRIZZLE_TYPE_VARCHAR || ((Field_varstring*)from)->length_bytes == ((Field_varstring*)to)->length_bytes))
798
756
{ // Identical fields
799
757
/* This may happen if one does 'UPDATE ... SET x=x' */
800
758
if (to->ptr != from->ptr)