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>
44
45
#include <drizzled/field/timestamp.h>
45
46
#include <drizzled/field/datetime.h>
46
47
#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)
50
static void do_field_eq(Copy_field *copy)
52
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
55
static void do_field_1(Copy_field *copy)
57
copy->to_ptr[0]=copy->from_ptr[0];
60
static void do_field_2(Copy_field *copy)
62
copy->to_ptr[0]=copy->from_ptr[0];
63
copy->to_ptr[1]=copy->from_ptr[1];
66
static void do_field_3(Copy_field *copy)
68
copy->to_ptr[0]=copy->from_ptr[0];
69
copy->to_ptr[1]=copy->from_ptr[1];
70
copy->to_ptr[2]=copy->from_ptr[2];
73
static void do_field_4(Copy_field *copy)
75
copy->to_ptr[0]=copy->from_ptr[0];
76
copy->to_ptr[1]=copy->from_ptr[1];
77
copy->to_ptr[2]=copy->from_ptr[2];
78
copy->to_ptr[3]=copy->from_ptr[3];
81
static void do_field_6(Copy_field *copy)
83
copy->to_ptr[0]=copy->from_ptr[0];
84
copy->to_ptr[1]=copy->from_ptr[1];
85
copy->to_ptr[2]=copy->from_ptr[2];
86
copy->to_ptr[3]=copy->from_ptr[3];
87
copy->to_ptr[4]=copy->from_ptr[4];
88
copy->to_ptr[5]=copy->from_ptr[5];
91
static void do_field_8(Copy_field *copy)
93
copy->to_ptr[0]=copy->from_ptr[0];
94
copy->to_ptr[1]=copy->from_ptr[1];
95
copy->to_ptr[2]=copy->from_ptr[2];
96
copy->to_ptr[3]=copy->from_ptr[3];
97
copy->to_ptr[4]=copy->from_ptr[4];
98
copy->to_ptr[5]=copy->from_ptr[5];
99
copy->to_ptr[6]=copy->from_ptr[6];
100
copy->to_ptr[7]=copy->from_ptr[7];
104
static void do_field_to_null_str(Copy_field *copy)
107
106
if (*copy->from_null_ptr & copy->from_bit)
109
108
memset(copy->to_ptr, 0, copy->from_length);
110
copy->to_null_ptr[0]= 1; // Always bit 1
109
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);
113
copy->to_null_ptr[0]=0;
114
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
120
static void do_outer_field_to_null_str(CopyField *copy)
119
static void do_outer_field_to_null_str(Copy_field *copy)
122
121
if (*copy->null_row ||
123
122
(copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
125
124
memset(copy->to_ptr, 0, copy->from_length);
126
copy->to_null_ptr[0]= 1; // Always bit 1
125
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);
129
copy->to_null_ptr[0]=0;
130
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
194
193
return 0; // Ok to set time to NULL
197
if (field == field->getTable()->next_number_field)
196
if (field == field->table->next_number_field)
199
field->getTable()->auto_increment_field_not_null= false;
198
field->table->auto_increment_field_not_null= false;
200
199
return 0; // field is set in fill_record()
202
if (field->getTable()->in_use->count_cuted_fields == CHECK_FIELD_WARN)
201
if (field->table->in_use->count_cuted_fields == CHECK_FIELD_WARN)
204
203
field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_BAD_NULL_ERROR, 1);
207
if (!field->getTable()->in_use->no_errors)
206
if (!field->table->in_use->no_errors)
208
207
my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
213
static void do_skip(CopyField *)
212
static void do_skip(Copy_field *)
218
static void do_copy_null(CopyField *copy)
217
static void do_copy_null(Copy_field *copy)
220
219
if (*copy->from_null_ptr & copy->from_bit)
222
*copy->to_null_ptr|= copy->to_bit;
221
*copy->to_null_ptr|=copy->to_bit;
223
222
copy->to_field->reset();
301
static void do_copy_blob(CopyField *copy)
294
static void do_copy_blob(Copy_field *copy)
303
ulong length= ((Field_blob*) copy->from_field)->get_length();
296
ulong length=((Field_blob*) copy->from_field)->get_length();
304
297
((Field_blob*) copy->to_field)->store_length(length);
305
memcpy(copy->to_ptr, copy->from_ptr, sizeof(char*));
298
memcpy(copy->to_ptr,copy->from_ptr,sizeof(char*));
308
static void do_conv_blob(CopyField *copy)
301
static void do_conv_blob(Copy_field *copy)
310
copy->from_field->val_str_internal(©->tmp);
303
copy->from_field->val_str(©->tmp);
311
304
((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
313
copy->tmp.charset());
306
copy->tmp.charset());
316
309
/** Save blob in copy->tmp for GROUP BY. */
318
static void do_save_blob(CopyField *copy)
311
static void do_save_blob(Copy_field *copy)
320
313
char buff[MAX_FIELD_WIDTH];
321
String res(buff, sizeof(buff), copy->tmp.charset());
322
copy->from_field->val_str_internal(&res);
314
String res(buff,sizeof(buff),copy->tmp.charset());
315
copy->from_field->val_str(&res);
323
316
copy->tmp.copy(res);
324
317
((Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
326
copy->tmp.charset());
319
copy->tmp.charset());
330
static void do_field_string(CopyField *copy)
323
static void do_field_string(Copy_field *copy)
332
325
char buff[MAX_FIELD_WIDTH];
333
326
copy->tmp.set_quick(buff,sizeof(buff),copy->tmp.charset());
334
copy->from_field->val_str_internal(©->tmp);
327
copy->from_field->val_str(©->tmp);
335
328
copy->to_field->store(copy->tmp.c_ptr_quick(),copy->tmp.length(),
336
329
copy->tmp.charset());
340
static void do_field_enum(CopyField *copy)
333
static void do_field_enum(Copy_field *copy)
342
335
if (copy->from_field->val_int() == 0)
343
336
((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)
422
static void do_expand_binary(Copy_field *copy)
424
const CHARSET_INFO * const cs= copy->from_field->charset();
425
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
426
cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
427
copy->to_length-copy->from_length, '\0');
432
static void do_expand_string(Copy_field *copy)
434
const CHARSET_INFO * const cs= copy->from_field->charset();
435
memcpy(copy->to_ptr,copy->from_ptr,copy->from_length);
436
cs->cset->fill(cs, (char*) copy->to_ptr+copy->from_length,
437
copy->to_length-copy->from_length, ' ');
441
static void do_varstring1(Copy_field *copy)
450
443
uint32_t length= (uint32_t) *(unsigned char*) copy->from_ptr;
451
444
if (length > copy->to_length- 1)
453
length= copy->to_length - 1;
454
if (copy->from_field->getTable()->in_use->count_cuted_fields)
446
length=copy->to_length - 1;
447
if (copy->from_field->table->in_use->count_cuted_fields)
455
448
copy->to_field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
456
449
ER_WARN_DATA_TRUNCATED, 1);
530
523
The 'to' buffer should have a size of field->pack_length()+1
533
void CopyField::set(unsigned char *to,Field *from)
526
void Copy_field::set(unsigned char *to,Field *from)
537
from_length= from->pack_length();
530
from_length=from->pack_length();
538
531
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)
533
from_null_ptr=from->null_ptr;
534
from_bit= from->null_bit;
535
to_ptr[0]= 1; // Null as default value
536
to_null_ptr= (unsigned char*) to_ptr++;
538
if (from->table->maybe_null)
547
null_row= &from->getTable()->null_row;
548
do_copy= do_outer_field_to_null_str;
540
null_row= &from->table->null_row;
541
do_copy= do_outer_field_to_null_str;
551
do_copy= do_field_to_null_str;
544
do_copy= do_field_to_null_str;
555
to_null_ptr= 0; // For easy debugging
556
do_copy= do_field_eq;
548
to_null_ptr= 0; // For easy debugging
549
do_copy= do_field_eq;
573
566
- The above causes a truncation to MAX_FIELD_WIDTH. Is this the intended
574
567
effect? Truncation is handled by well_formed_copy_nchars anyway.
576
void CopyField::set(Field *to,Field *from,bool save)
569
void Copy_field::set(Field *to,Field *from,bool save)
578
571
if (to->type() == DRIZZLE_TYPE_NULL)
580
to_null_ptr= 0; // For easy debugging
573
to_null_ptr=0; // For easy debugging
588
from_length= from->pack_length();
590
to_length= to_field->pack_length();
581
from_length=from->pack_length();
583
to_length=to_field->pack_length();
592
585
// set up null handling
593
from_null_ptr= to_null_ptr= 0;
586
from_null_ptr=to_null_ptr=0;
594
587
if (from->maybe_null())
596
from_null_ptr= from->null_ptr;
597
from_bit= from->null_bit;
589
from_null_ptr= from->null_ptr;
590
from_bit= from->null_bit;
598
591
if (to_field->real_maybe_null())
600
to_null_ptr= to->null_ptr;
601
to_bit= to->null_bit;
593
to_null_ptr= to->null_ptr;
594
to_bit= to->null_bit;
602
595
if (from_null_ptr)
604
do_copy= do_copy_null;
596
do_copy= do_copy_null;
608
null_row= &from->getTable()->null_row;
609
do_copy= do_outer_field_null;
599
null_row= &from->table->null_row;
600
do_copy= do_outer_field_null;
614
605
if (to_field->type() == DRIZZLE_TYPE_TIMESTAMP)
616
606
do_copy= do_copy_timestamp; // Automatic timestamp
618
else if (to_field == to_field->getTable()->next_number_field)
607
else if (to_field == to_field->table->next_number_field)
620
608
do_copy= do_copy_next_number;
624
610
do_copy= do_copy_not_null;
628
613
else if (to_field->real_maybe_null())
630
to_null_ptr= to->null_ptr;
631
to_bit= to->null_bit;
615
to_null_ptr= to->null_ptr;
616
to_bit= to->null_bit;
632
617
do_copy= do_copy_maybe_null;
637
622
if ((to->flags & BLOB_FLAG) && save)
638
623
do_copy2= do_save_blob;
640
625
do_copy2= get_copy_func(to,from);
641
if (!do_copy) // Not null
626
if (!do_copy) // Not null
646
CopyField::Copy_func *
647
CopyField::get_copy_func(Field *to,Field *from)
631
Copy_field::Copy_func *
632
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);
634
bool compatible_db_low_byte_first= (to->table->s->db_low_byte_first ==
635
from->table->s->db_low_byte_first);
651
636
if (to->flags & BLOB_FLAG)
653
638
if (!(from->flags & BLOB_FLAG) || from->charset() != to->charset())
701
679
else if (to->charset() != from->charset())
702
return do_field_string;
680
return do_field_string;
703
681
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;
683
if (((Field_varstring*) to)->length_bytes !=
684
((Field_varstring*) from)->length_bytes)
685
return do_field_string;
722
686
if (to_length != from_length)
724
return (((Field_varstring*) to)->pack_length_no_ptr() == 1 ?
687
return (((Field_varstring*) to)->length_bytes == 1 ?
725
688
(from->charset()->mbmaxlen == 1 ? do_varstring1 :
727
690
(from->charset()->mbmaxlen == 1 ? do_varstring2 :
731
693
else if (to_length < from_length)
733
return (from->charset()->mbmaxlen == 1 ?
694
return (from->charset()->mbmaxlen == 1 ?
734
695
do_cut_string : do_cut_string_complex);
736
696
else if (to_length > from_length)
738
698
if (to->charset() == &my_charset_bin)
784
741
int field_conv(Field *to,Field *from)
786
743
if (to->real_type() == from->real_type() &&
787
!(to->type() == DRIZZLE_TYPE_BLOB && to->getTable()->copy_blobs))
744
!(to->type() == DRIZZLE_TYPE_BLOB && to->table->copy_blobs))
789
746
/* Please god, will someone rewrite this to be readable :( */
790
747
if (to->pack_length() == from->pack_length() &&
791
748
!(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) &&
792
749
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))) &&
750
(to->real_type() != DRIZZLE_TYPE_NEWDECIMAL || (to->field_length == from->field_length && (((Field_num*)to)->dec == ((Field_num*)from)->dec))) &&
794
751
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()))
752
to->table->s->db_low_byte_first == from->table->s->db_low_byte_first &&
753
(!(to->table->in_use->variables.sql_mode & (MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) || (to->type() != DRIZZLE_TYPE_DATE && to->type() != DRIZZLE_TYPE_DATETIME)) &&
754
(from->real_type() != DRIZZLE_TYPE_VARCHAR || ((Field_varstring*)from)->length_bytes == ((Field_varstring*)to)->length_bytes))
798
755
{ // Identical fields
799
756
/* This may happen if one does 'UPDATE ... SET x=x' */
800
757
if (to->ptr != from->ptr)