28 #include "sql_class.h"
34 memcpy(copy->to_ptr,copy->from_ptr,copy->
from_length);
39 copy->to_ptr[0]=copy->from_ptr[0];
44 copy->to_ptr[0]=copy->from_ptr[0];
45 copy->to_ptr[1]=copy->from_ptr[1];
50 copy->to_ptr[0]=copy->from_ptr[0];
51 copy->to_ptr[1]=copy->from_ptr[1];
52 copy->to_ptr[2]=copy->from_ptr[2];
57 copy->to_ptr[0]=copy->from_ptr[0];
58 copy->to_ptr[1]=copy->from_ptr[1];
59 copy->to_ptr[2]=copy->from_ptr[2];
60 copy->to_ptr[3]=copy->from_ptr[3];
65 copy->to_ptr[0]=copy->from_ptr[0];
66 copy->to_ptr[1]=copy->from_ptr[1];
67 copy->to_ptr[2]=copy->from_ptr[2];
68 copy->to_ptr[3]=copy->from_ptr[3];
69 copy->to_ptr[4]=copy->from_ptr[4];
70 copy->to_ptr[5]=copy->from_ptr[5];
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];
79 copy->to_ptr[4]=copy->from_ptr[4];
80 copy->to_ptr[5]=copy->from_ptr[5];
81 copy->to_ptr[6]=copy->from_ptr[6];
82 copy->to_ptr[7]=copy->from_ptr[7];
85 static void do_field_to_null_str(
Copy_field *copy)
87 if (*copy->null_row ||
88 (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
91 copy->to_null_ptr[0]=1;
95 copy->to_null_ptr[0]=0;
96 memcpy(copy->to_ptr,copy->from_ptr,copy->
from_length);
101 type_conversion_status set_field_to_null(
Field *field)
110 switch (field->table->in_use->count_cuted_fields) {
111 case CHECK_FIELD_WARN:
112 field->
set_warning(Sql_condition::WARN_LEVEL_WARN, WARN_DATA_TRUNCATED, 1);
114 case CHECK_FIELD_IGNORE:
116 case CHECK_FIELD_ERROR_FOR_NULL:
117 if (!field->table->in_use->no_errors)
118 my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
119 return TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
122 return TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
143 type_conversion_status
153 return TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
167 if (field->type() == MYSQL_TYPE_TIMESTAMP &&
168 !field->table->in_use->variables.explicit_defaults_for_timestamp)
177 if (field == field->table->next_number_field)
179 field->table->auto_increment_field_not_null= FALSE;
182 switch (field->table->in_use->count_cuted_fields) {
183 case CHECK_FIELD_WARN:
184 field->
set_warning(Sql_condition::WARN_LEVEL_WARN, ER_BAD_NULL_ERROR, 1);
186 case CHECK_FIELD_IGNORE:
188 case CHECK_FIELD_ERROR_FOR_NULL:
189 if (!field->table->in_use->no_errors)
190 my_error(ER_BAD_NULL_ERROR, MYF(0), field->field_name);
191 return TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
194 return TYPE_ERR_NULL_CONSTRAINT_VIOLATION;
198 static void do_skip(
Copy_field *copy __attribute__((unused)))
205 if (*copy->null_row ||
206 (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
208 *copy->to_null_ptr|=copy->to_bit;
209 copy->to_field->reset();
213 *copy->to_null_ptr&= ~copy->to_bit;
214 (copy->do_copy2)(copy);
219 static void do_copy_not_null(
Copy_field *copy)
221 if (*copy->null_row ||
222 (copy->from_null_ptr && (*copy->from_null_ptr & copy->from_bit)))
224 copy->to_field->
set_warning(Sql_condition::WARN_LEVEL_WARN,
225 WARN_DATA_TRUNCATED, 1);
226 copy->to_field->reset();
229 (copy->do_copy2)(copy);
233 static void do_copy_maybe_null(
Copy_field *copy)
235 *copy->to_null_ptr&= ~copy->to_bit;
236 (copy->do_copy2)(copy);
241 static void do_copy_timestamp(
Copy_field *copy)
243 if (*copy->null_row || (*copy->from_null_ptr & copy->from_bit))
249 (copy->do_copy2)(copy);
253 static void do_copy_next_number(
Copy_field *copy)
255 if (*copy->null_row || (*copy->from_null_ptr & copy->from_bit))
258 copy->to_field->table->auto_increment_field_not_null= FALSE;
259 copy->to_field->reset();
262 (copy->do_copy2)(copy);
268 ulong length=((
Field_blob*) copy->from_field)->get_length();
269 ((
Field_blob*) copy->to_field)->store_length(length);
270 memcpy(copy->to_ptr, copy->from_ptr,
sizeof(
char*));
275 copy->from_field->val_str(©->tmp);
276 ((
Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
278 copy->tmp.charset());
285 char buff[MAX_FIELD_WIDTH];
286 String res(buff,
sizeof(buff),copy->tmp.charset());
287 copy->from_field->val_str(&res);
289 ((
Field_blob *) copy->to_field)->store(copy->tmp.ptr(),
291 copy->tmp.charset());
297 char buff[MAX_FIELD_WIDTH];
298 String res(buff,
sizeof(buff), copy->from_field->charset());
301 copy->from_field->val_str(&res);
302 copy->to_field->store(res.c_ptr_quick(), res.length(), res.charset());
308 if (copy->from_field->val_int() == 0)
309 ((
Field_enum *) copy->to_field)->store_type((ulonglong) 0);
311 do_field_string(copy);
315 static void do_field_varbinary_pre50(
Copy_field *copy)
317 char buff[MAX_FIELD_WIDTH];
318 copy->tmp.set_quick(buff,
sizeof(buff),copy->tmp.charset());
319 copy->from_field->val_str(©->tmp);
322 uint length= my_lengthsp_8bit(&my_charset_bin, copy->tmp.c_ptr_quick(),
323 copy->from_field->field_length);
325 copy->to_field->store(copy->tmp.c_ptr_quick(), length,
326 copy->tmp.charset());
332 longlong value= copy->from_field->val_int();
333 copy->to_field->store(value,
334 test(copy->from_field->flags & UNSIGNED_FLAG));
339 double value=copy->from_field->val_real();
340 copy->to_field->store(value);
344 static void do_field_decimal(
Copy_field *copy)
347 copy->to_field->store_decimal(copy->from_field->val_decimal(&value));
351 inline type_conversion_status copy_time_to_time(
Field *from,
Field *
to)
354 from->get_time(<ime);
363 (void) copy_time_to_time(copy->from_field, copy->to_field);
375 memcpy(copy->to_ptr,copy->from_ptr,copy->to_length);
378 if (cs->cset->scan(cs,
379 (
char*) copy->from_ptr + copy->to_length,
381 MY_SEQ_SPACES) < copy->
from_length - copy->to_length)
383 copy->to_field->
set_warning(Sql_condition::WARN_LEVEL_WARN,
384 WARN_DATA_TRUNCATED, 1);
394 static void do_cut_string_complex(
Copy_field *copy)
396 int well_formed_error;
398 const uchar *from_end= copy->from_ptr + copy->
from_length;
399 uint copy_length= cs->cset->well_formed_len(cs,
400 (
char*) copy->from_ptr,
402 copy->to_length / cs->mbmaxlen,
404 if (copy->to_length < copy_length)
405 copy_length= copy->to_length;
406 memcpy(copy->to_ptr, copy->from_ptr, copy_length);
409 if (well_formed_error ||
410 cs->cset->scan(cs, (
char*) copy->from_ptr + copy_length,
412 MY_SEQ_SPACES) < (copy->
from_length - copy_length))
414 copy->to_field->
set_warning(Sql_condition::WARN_LEVEL_WARN,
415 WARN_DATA_TRUNCATED, 1);
418 if (copy_length < copy->to_length)
419 cs->cset->fill(cs, (
char*) copy->to_ptr + copy_length,
420 copy->to_length - copy_length,
' ');
426 static void do_expand_binary(
Copy_field *copy)
429 memcpy(copy->to_ptr,copy->from_ptr,copy->
from_length);
430 cs->cset->fill(cs, (
char*) copy->to_ptr+copy->
from_length,
436 static void do_expand_string(
Copy_field *copy)
439 memcpy(copy->to_ptr,copy->from_ptr,copy->
from_length);
440 cs->cset->fill(cs, (
char*) copy->to_ptr+copy->
from_length,
459 const bool is_multibyte_charset= (cs->mbmaxlen != 1);
460 const uint to_byte_length= to->row_pack_length();
463 if (from->length_bytes == 1)
464 bytes_to_copy= *from->ptr;
466 bytes_to_copy= uint2korr(from->ptr);
468 if (is_multibyte_charset)
470 int well_formed_error;
471 const char *from_beg=
reinterpret_cast<char*
>(from->ptr + from->length_bytes);
472 const uint to_char_length= (to_byte_length) / cs->mbmaxlen;
473 const uint from_byte_length= bytes_to_copy;
475 cs->cset->well_formed_len(cs, from_beg,
476 from_beg + from_byte_length,
479 if (bytes_to_copy < from_byte_length)
481 if (from->table->in_use->count_cuted_fields)
483 WARN_DATA_TRUNCATED, 1);
488 if (bytes_to_copy > (to_byte_length))
490 bytes_to_copy= to_byte_length;
491 if (from->table->in_use->count_cuted_fields)
493 WARN_DATA_TRUNCATED, 1);
496 return bytes_to_copy;
516 const uint length_bytes= from->length_bytes;
517 DBUG_ASSERT(length_bytes == to->length_bytes);
518 DBUG_ASSERT(length_bytes == 1 || length_bytes == 2);
520 const uint bytes_to_copy= get_varstring_copy_length(to, from);
521 if (length_bytes == 1)
522 *to->ptr=
static_cast<uchar
>(bytes_to_copy);
524 int2store(to->ptr, bytes_to_copy);
527 DBUG_ASSERT(to->ptr != from->ptr);
528 memcpy(to->ptr + length_bytes, from->ptr + length_bytes, bytes_to_copy);
533 copy_field_varstring(static_cast<Field_varstring*>(copy->to_field),
534 static_cast<Field_varstring*>(copy->from_field));
550 void Copy_field::set(uchar *to,
Field *from)
555 null_row= &from->table->null_row;
556 if (from->maybe_null())
559 from_bit= from->null_bit;
561 to_null_ptr= (uchar*) to_ptr++;
563 do_copy= do_field_to_null_str;
568 do_copy= do_field_eq;
585 void Copy_field::set(
Field *to,
Field *from,
bool save)
587 if (to->type() == MYSQL_TYPE_NULL)
599 to_length=to_field->pack_length();
602 from_null_ptr=to_null_ptr=0;
603 null_row= &from->table->null_row;
604 if (from->maybe_null())
607 from_bit= from->null_bit;
611 to_bit= to->null_bit;
612 do_copy= do_copy_null;
616 if (to_field->type() == MYSQL_TYPE_TIMESTAMP)
617 do_copy= do_copy_timestamp;
618 else if (to_field == to_field->table->next_number_field)
619 do_copy= do_copy_next_number;
621 do_copy= do_copy_not_null;
627 to_bit= to->null_bit;
628 do_copy= do_copy_maybe_null;
633 if ((to->flags & BLOB_FLAG) && save)
634 do_copy2= do_save_blob;
636 do_copy2= get_copy_func(to,from);
642 Copy_field::Copy_func *
643 Copy_field::get_copy_func(
Field *to,
Field *from)
645 bool compatible_db_low_byte_first= (to->table->s->db_low_byte_first ==
646 from->table->s->db_low_byte_first);
647 if (to->flags & BLOB_FLAG)
649 if (!(from->flags & BLOB_FLAG) || from->charset() != to->charset())
651 if (
from_length != to_length || !compatible_db_low_byte_first)
654 to_ptr+= to_length - portable_sizeof_char_ptr;
661 if (to->real_type() == MYSQL_TYPE_BIT ||
662 from->real_type() == MYSQL_TYPE_BIT)
664 if (to->result_type() == DECIMAL_RESULT)
665 return do_field_decimal;
667 if (from->result_type() == STRING_RESULT)
669 if (from->is_temporal())
671 if (to->is_temporal())
673 return do_field_time;
677 if (to->result_type() == INT_RESULT)
679 if (to->result_type() == REAL_RESULT)
680 return do_field_real;
689 if (from->type() == MYSQL_TYPE_VAR_STRING && !from->has_charset() &&
690 to->type() == MYSQL_TYPE_VARCHAR && !to->has_charset())
691 return do_field_varbinary_pre50;
697 if (to->real_type() != from->real_type() ||
698 to->decimals() != from->decimals() ||
699 !compatible_db_low_byte_first ||
700 (((to->table->in_use->variables.sql_mode &
701 (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) &&
702 to->type() == MYSQL_TYPE_DATE) ||
703 to->type() == MYSQL_TYPE_DATETIME))
705 if (from->real_type() == MYSQL_TYPE_ENUM ||
706 from->real_type() == MYSQL_TYPE_SET)
707 if (to->result_type() != STRING_RESULT)
709 return do_field_string;
711 if (to->real_type() == MYSQL_TYPE_ENUM ||
712 to->real_type() == MYSQL_TYPE_SET)
716 if (from->real_type() == MYSQL_TYPE_ENUM &&
717 to->real_type() == MYSQL_TYPE_ENUM)
718 return do_field_enum;
720 return do_field_string;
723 else if (to->charset() != from->charset())
724 return do_field_string;
725 else if (to->real_type() == MYSQL_TYPE_VARCHAR)
729 return do_field_string;
734 return (from->charset()->mbmaxlen == 1 ?
735 do_cut_string : do_cut_string_complex);
738 if (to->charset() == &my_charset_bin)
739 return do_expand_binary;
741 return do_expand_string;
745 else if (to->real_type() != from->real_type() ||
747 !compatible_db_low_byte_first)
749 if (to->real_type() == MYSQL_TYPE_DECIMAL ||
750 to->result_type() == STRING_RESULT)
751 return do_field_string;
752 if (to->result_type() == INT_RESULT)
754 return do_field_real;
758 if (!to->
eq_def(from) || !compatible_db_low_byte_first)
760 if (to->real_type() == MYSQL_TYPE_DECIMAL)
761 return do_field_string;
762 if (to->result_type() == INT_RESULT)
765 return do_field_real;
771 case 1:
return do_field_1;
772 case 2:
return do_field_2;
773 case 3:
return do_field_3;
774 case 4:
return do_field_4;
775 case 6:
return do_field_6;
776 case 8:
return do_field_8;
786 if (to->real_type() == from->real_type() &&
787 !(to->type() == MYSQL_TYPE_BLOB && to->table->copy_blobs) &&
788 to->charset() == from->charset())
790 if (to->real_type() == MYSQL_TYPE_VARCHAR &&
791 from->real_type() == MYSQL_TYPE_VARCHAR)
795 if (to_vc->length_bytes == from_vc->length_bytes)
797 copy_field_varstring(to_vc, from_vc);
801 if (to->pack_length() == from->pack_length() &&
802 !(to->flags & UNSIGNED_FLAG && !(from->flags & UNSIGNED_FLAG)) &&
803 to->real_type() != MYSQL_TYPE_ENUM &&
804 to->real_type() != MYSQL_TYPE_SET &&
805 to->real_type() != MYSQL_TYPE_BIT &&
806 (!to->is_temporal_with_time() ||
807 to->decimals() == from->decimals()) &&
808 (to->real_type() != MYSQL_TYPE_NEWDECIMAL ||
809 (to->field_length == from->field_length &&
811 to->table->s->db_low_byte_first == from->table->s->db_low_byte_first &&
812 (!(to->table->in_use->variables.sql_mode &
813 (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE | MODE_INVALID_DATES)) ||
814 (to->type() != MYSQL_TYPE_DATE &&
815 to->type() != MYSQL_TYPE_DATETIME &&
816 (!to->table->in_use->variables.explicit_defaults_for_timestamp ||
817 to->type() != MYSQL_TYPE_TIMESTAMP))) &&
818 (from->real_type() != MYSQL_TYPE_VARCHAR))
821 memmove(to->ptr, from->ptr, to->pack_length());
825 if (to->type() == MYSQL_TYPE_BLOB)
828 from->val_str(&blob->
value);
833 if (to->table->copy_blobs ||
834 (!blob->
value.is_alloced() &&
835 from->real_type() != MYSQL_TYPE_STRING &&
836 from->real_type() != MYSQL_TYPE_VARCHAR))
838 return blob->store(blob->
value.ptr(),blob->
value.length(),from->charset());
840 if (from->real_type() == MYSQL_TYPE_ENUM &&
841 to->real_type() == MYSQL_TYPE_ENUM &&
842 from->val_int() == 0)
847 else if (from->is_temporal() && to->result_type() == INT_RESULT)
851 if (from->type() == MYSQL_TYPE_TIME)
853 from->get_time(<ime);
854 nr= TIME_to_ulonglong_time_round(<ime);
858 from->get_date(<ime, TIME_FUZZY_DATE);
859 nr= TIME_to_ulonglong_datetime_round(<ime);
861 return to->store(ltime.neg ? -nr : nr, 0);
863 else if (from->is_temporal() &&
864 (to->result_type() == REAL_RESULT ||
865 to->result_type() == DECIMAL_RESULT ||
866 to->result_type() == INT_RESULT))
873 return to->store_decimal(from->val_decimal(&tmp));
875 else if (from->is_temporal() && to->is_temporal())
877 return copy_time_to_time(from, to);
879 else if ((from->result_type() == STRING_RESULT &&
880 (to->result_type() == STRING_RESULT ||
881 (from->real_type() != MYSQL_TYPE_ENUM &&
882 from->real_type() != MYSQL_TYPE_SET))) ||
883 to->type() == MYSQL_TYPE_DECIMAL)
885 char buff[MAX_FIELD_WIDTH];
886 String result(buff,
sizeof(buff),from->charset());
887 from->val_str(&result);
894 return to->store(result.c_ptr_quick(),result.length(),from->charset());
896 else if (from->result_type() == REAL_RESULT)
897 return to->store(from->val_real());
898 else if (from->result_type() == DECIMAL_RESULT)
901 return to->store_decimal(from->val_decimal(&buff));
904 return to->store(from->val_int(),
test(from->flags & UNSIGNED_FLAG));